Skip to content

Commit

Permalink
Support symbol prefixes
Browse files Browse the repository at this point in the history
- In base.h, if BORINGSSL_PREFIX is defined, include
  boringssl_prefix_symbols.h
- In all .S files, if BORINGSSL_PREFIX is defined, include
  boringssl_prefix_symbols_asm.h
- In base.h, BSSL_NAMESPACE_BEGIN and BSSL_NAMESPACE_END are
  defined with appropriate values depending on whether
  BORINGSSL_PREFIX is defined; these macros are used in place
  of 'namespace bssl {' and '}'
- Add util/make_prefix_headers.go, which takes a list of symbols
  and auto-generates the header files mentioned above
- In CMakeLists.txt, if BORINGSSL_PREFIX and BORINGSSL_PREFIX_SYMBOLS
  are defined, run util/make_prefix_headers.go to generate header
  files
- In various CMakeLists.txt files, add "global_target" that all
  targets depend on to give us a place to hook logic that must run
  before all other targets (in particular, the header file generation
  logic)
- Document this in BUILDING.md, including the fact that it is
  the caller's responsibility to provide the symbol list and keep it
  up to date
- Note that this scheme has not been tested on Windows, and likely
  does not work on it; Windows support will need to be added in a
  future commit

Change-Id: If66a7157f46b5b66230ef91e15826b910cf979a2
Reviewed-on: https://boringssl-review.googlesource.com/31364
Commit-Queue: David Benjamin <[email protected]>
CQ-Verified: CQ bot account: [email protected] <[email protected]>
Reviewed-by: David Benjamin <[email protected]>
  • Loading branch information
joshlf authored and CQ bot account: [email protected] committed Sep 6, 2018
1 parent 492c9aa commit 8c7c635
Show file tree
Hide file tree
Showing 81 changed files with 477 additions and 151 deletions.
22 changes: 22 additions & 0 deletions BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,28 @@ architecture, matching values used in the `-arch` flag in Apple's toolchain.
Passing multiple architectures for a multiple-architecture build is not
supported.

### Building with Prefixed Symbols

BoringSSL's build system has experimental support for adding a custom prefix to
all symbols. This can be useful when linking multiple versions of BoringSSL in
the same project to avoid symbol conflicts.

In order to build with prefixed symbols, the `BORINGSSL_PREFIX` CMake variable
should specify the prefix to add to all symbols, and the
`BORINGSSL_PREFIX_SYMBOLS` CMake variable should specify the path to a file
which contains a list of symbols which should be prefixed (one per line;
comments are supported with `#`). In other words, `cmake ..
-DBORINGSSL_PREFIX=MY_CUSTOM_PREFIX
-DBORINGSSL_PREFIX_SYMBOLS=/path/to/symbols.txt` will configure the build to add
the prefix `MY_CUSTOM_PREFIX` to all of the symbols listed in
`/path/to/symbols.txt`.

It is currently the caller's responsibility to create and maintain the list of
symbols to be prefixed.

This mechanism is under development and may change over time. Please contact the
BoringSSL maintainers if making use of it.

## Known Limitations on Windows

* Versions of CMake since 3.0.2 have a bug in its Ninja generator that causes
Expand Down
32 changes: 32 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ include(sources.cmake)
enable_language(C)
enable_language(CXX)

# This is a dummy target which all other targets depend on (manually - see other
# CMakeLists.txt files). This gives us a hook to add any targets which need to
# run before all other targets.
add_custom_target(global_target)

if(ANDROID)
# Android-NDK CMake files reconfigure the path and so Go and Perl won't be
# found. However, ninja will still find them in $PATH if we just name them.
Expand All @@ -41,10 +46,37 @@ endif()
if(USE_CUSTOM_LIBCXX)
set(BORINGSSL_ALLOW_CXX_RUNTIME 1)
endif()

if(BORINGSSL_ALLOW_CXX_RUNTIME)
add_definitions(-DBORINGSSL_ALLOW_CXX_RUNTIME)
endif()

if(BORINGSSL_PREFIX AND BORINGSSL_PREFIX_SYMBOLS)
add_definitions(-DBORINGSSL_PREFIX=${BORINGSSL_PREFIX})

# Use "symbol_prefix_include" to store generated header files
include_directories(${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include)
add_custom_command(
OUTPUT symbol_prefix_include/boringssl_prefix_symbols.h
symbol_prefix_include/boringssl_prefix_symbols_asm.h
symbol_prefix_include/boringssl_prefix_symbols_nasm.inc
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include
COMMAND ${GO_EXECUTABLE} run ${CMAKE_CURRENT_SOURCE_DIR}/util/make_prefix_headers.go -out ${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include ${BORINGSSL_PREFIX_SYMBOLS}
DEPENDS util/make_prefix_headers.go
${CMAKE_BINARY_DIR}/${BORINGSSL_PREFIX_SYMBOLS})

# add_dependencies needs a target, not a file, so we add an intermediate
# target.
add_custom_target(
boringssl_prefix_symbols
DEPENDS symbol_prefix_include/boringssl_prefix_symbols.h
symbol_prefix_include/boringssl_prefix_symbols_asm.h
symbol_prefix_include/boringssl_prefix_symbols_nasm.inc)
add_dependencies(global_target boringssl_prefix_symbols)
elseif(BORINGSSL_PREFIX OR BORINGSSL_PREFIX_SYMBOLS)
message(FATAL_ERROR "Must specify both or neither of BORINGSSL_PREFIX and BORINGSSL_PREFIX_SYMBOLS")
endif()

if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CLANG 1)
endif()
Expand Down
4 changes: 4 additions & 0 deletions crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,8 @@ add_library(
${CRYPTO_FIPS_OBJECTS}
)

add_dependencies(crypto global_target)

if(FIPS_DELOCATE)
add_dependencies(crypto bcm_o_target)
endif()
Expand Down Expand Up @@ -476,6 +478,8 @@ add_executable(
$<TARGET_OBJECTS:test_support>
)

add_dependencies(crypto_test global_target)

target_link_libraries(crypto_test crypto boringssl_gtest)
if(WIN32)
target_link_libraries(crypto_test ws2_32)
Expand Down
4 changes: 2 additions & 2 deletions crypto/err/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ OPENSSL_EXPORT void ERR_restore_state(const ERR_SAVE_STATE *state);

extern "C++" {

namespace bssl {
BSSL_NAMESPACE_BEGIN

BORINGSSL_MAKE_DELETER(ERR_SAVE_STATE, ERR_SAVE_STATE_free)

} // namespace bssl
BSSL_NAMESPACE_END

} // extern C++
#endif
Expand Down
8 changes: 8 additions & 0 deletions crypto/fipsmodule/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ if(FIPS_DELOCATE)
bcm.c
)

add_dependencies(bcm_c_generated_asm global_target)

set_target_properties(bcm_c_generated_asm PROPERTIES COMPILE_OPTIONS "-S")
set_target_properties(bcm_c_generated_asm PROPERTIES POSITION_INDEPENDENT_CODE ON)

Expand Down Expand Up @@ -164,6 +166,8 @@ if(FIPS_DELOCATE)
bcm-delocated.S
)

add_dependencies(bcm_hashunset global_target)

set_target_properties(bcm_hashunset PROPERTIES POSITION_INDEPENDENT_CODE ON)
set_target_properties(bcm_hashunset PROPERTIES LINKER_LANGUAGE C)

Expand All @@ -187,6 +191,8 @@ if(FIPS_DELOCATE)
is_fips.c
)

add_dependencies(fipsmodule global_target)

set_target_properties(fipsmodule PROPERTIES LINKER_LANGUAGE C)
else()
add_library(
Expand All @@ -199,4 +205,6 @@ else()

${BCM_ASM_SOURCES}
)

add_dependencies(fipsmodule global_target)
endif()
4 changes: 2 additions & 2 deletions crypto/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ OPENSSL_EXPORT void CRYPTO_STATIC_MUTEX_unlock_write(
#if defined(__cplusplus)
extern "C++" {

namespace bssl {
BSSL_NAMESPACE_BEGIN

namespace internal {

Expand Down Expand Up @@ -516,7 +516,7 @@ using MutexWriteLock =
using MutexReadLock =
internal::MutexLockBase<CRYPTO_MUTEX_lock_read, CRYPTO_MUTEX_unlock_read>;

} // namespace bssl
BSSL_NAMESPACE_END

} // extern "C++"
#endif // defined(__cplusplus)
Expand Down
7 changes: 7 additions & 0 deletions crypto/perlasm/x86_64-xlate.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1129,13 +1129,20 @@ sub rxb {
%define XMMWORD
%define YMMWORD
%define ZMMWORD
%ifdef BORINGSSL_PREFIX
%include "boringssl_prefix_symbols_nasm.inc"
%endif
___
} elsif ($masm) {
print <<___;
OPTION DOTNAME
___
}
print STDOUT "#if defined(__x86_64__) && !defined(OPENSSL_NO_ASM)\n" if ($gas);
print STDOUT "#if defined(BORINGSSL_PREFIX)\n" if ($gas);
print STDOUT "#include <boringssl_prefix_symbols_asm.h>\n" if ($gas);
print STDOUT "#endif\n" if ($gas);
while(defined(my $line=<>)) {
Expand Down
4 changes: 4 additions & 0 deletions crypto/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ add_library(
wycheproof_util.cc
)

add_dependencies(test_support global_target)

add_library(
boringssl_gtest_main

OBJECT

gtest_main.cc
)

add_dependencies(boringssl_gtest_main global_target)
4 changes: 2 additions & 2 deletions crypto/test/gtest_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ OPENSSL_MSVC_PRAGMA(warning(pop))
#endif


namespace bssl {
BSSL_NAMESPACE_BEGIN

class ErrorTestEventListener : public testing::EmptyTestEventListener {
public:
Expand Down Expand Up @@ -73,7 +73,7 @@ inline void SetupGoogleTest() {
new ErrorTestEventListener);
}

} // namespace bssl
BSSL_NAMESPACE_END


#endif // OPENSSL_HEADER_CRYPTO_TEST_GTEST_MAIN_H
4 changes: 4 additions & 0 deletions decrepit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ add_library(
xts/xts.c
)

add_dependencies(decrepit global_target)

target_link_libraries(decrepit crypto ssl)

add_executable(
Expand All @@ -34,6 +36,8 @@ add_executable(
$<TARGET_OBJECTS:test_support>
)

add_dependencies(decrepit_test global_target)

target_link_libraries(decrepit_test crypto decrepit boringssl_gtest)
if(WIN32)
target_link_libraries(decrepit_test ws2_32)
Expand Down
4 changes: 4 additions & 0 deletions fipstools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ if(FIPS)
$<TARGET_OBJECTS:test_support>
)

add_dependencies(cavp global_target)

add_executable(
test_fips

test_fips.c
$<TARGET_OBJECTS:test_support>
)

add_dependencies(test_fips global_target)

target_link_libraries(cavp crypto)
target_link_libraries(test_fips crypto)
endif()
1 change: 1 addition & 0 deletions fuzz/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-missing-prototypes")

macro(fuzzer name)
add_executable(${name} ${name}.cc)
add_dependencies(${name} global_target)
target_link_libraries(${name} crypto ${ARGN})
set_target_properties(${name} PROPERTIES LINK_FLAGS "-fsanitize=fuzzer")
endmacro()
Expand Down
4 changes: 2 additions & 2 deletions include/openssl/aead.h
Original file line number Diff line number Diff line change
Expand Up @@ -425,15 +425,15 @@ OPENSSL_EXPORT int EVP_AEAD_CTX_tag_len(const EVP_AEAD_CTX *ctx,
#if !defined(BORINGSSL_NO_CXX)
extern "C++" {

namespace bssl {
BSSL_NAMESPACE_BEGIN

using ScopedEVP_AEAD_CTX =
internal::StackAllocated<EVP_AEAD_CTX, void, EVP_AEAD_CTX_zero,
EVP_AEAD_CTX_cleanup>;

BORINGSSL_MAKE_DELETER(EVP_AEAD_CTX, EVP_AEAD_CTX_free)

} // namespace bssl
BSSL_NAMESPACE_END

} // extern C++
#endif
Expand Down
4 changes: 2 additions & 2 deletions include/openssl/asn1.h
Original file line number Diff line number Diff line change
Expand Up @@ -875,13 +875,13 @@ OPENSSL_EXPORT ASN1_TYPE *ASN1_generate_v3(char *str, X509V3_CTX *cnf);

extern "C++" {

namespace bssl {
BSSL_NAMESPACE_BEGIN

BORINGSSL_MAKE_DELETER(ASN1_OBJECT, ASN1_OBJECT_free)
BORINGSSL_MAKE_DELETER(ASN1_STRING, ASN1_STRING_free)
BORINGSSL_MAKE_DELETER(ASN1_TYPE, ASN1_TYPE_free)

} // namespace bssl
BSSL_NAMESPACE_END

} /* extern C++ */

Expand Down
21 changes: 19 additions & 2 deletions include/openssl/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
#include <openssl/is_boringssl.h>
#include <openssl/opensslconf.h>

#if defined(BORINGSSL_PREFIX)
#include <boringssl_prefix_symbols.h>
#endif

#if defined(__cplusplus)
extern "C" {
#endif
Expand Down Expand Up @@ -366,6 +370,19 @@ typedef void *OPENSSL_BLOCK;
#endif

#if !defined(BORINGSSL_NO_CXX)

#if defined(BORINGSSL_PREFIX)
#define BSSL_NAMESPACE_BEGIN \
namespace bssl { \
inline namespace BORINGSSL_PREFIX {
#define BSSL_NAMESPACE_END \
} \
}
#else
#define BSSL_NAMESPACE_BEGIN namespace bssl {
#define BSSL_NAMESPACE_END }
#endif

extern "C++" {

#include <memory>
Expand All @@ -387,7 +404,7 @@ extern "C++" {

extern "C++" {

namespace bssl {
BSSL_NAMESPACE_BEGIN

namespace internal {

Expand Down Expand Up @@ -464,7 +481,7 @@ using UniquePtr = std::unique_ptr<T, internal::Deleter<T>>;
return UpRef(ptr.get()); \
}

} // namespace bssl
BSSL_NAMESPACE_END

} // extern C++

Expand Down
4 changes: 2 additions & 2 deletions include/openssl/bio.h
Original file line number Diff line number Diff line change
Expand Up @@ -894,12 +894,12 @@ struct bio_st {

extern "C++" {

namespace bssl {
BSSL_NAMESPACE_BEGIN

BORINGSSL_MAKE_DELETER(BIO, BIO_free)
BORINGSSL_MAKE_UP_REF(BIO, BIO_up_ref)

} // namespace bssl
BSSL_NAMESPACE_END

} // extern C++

Expand Down
4 changes: 2 additions & 2 deletions include/openssl/bn.h
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ OPENSSL_EXPORT unsigned BN_num_bits_word(BN_ULONG l);
#if !defined(BORINGSSL_NO_CXX)
extern "C++" {

namespace bssl {
BSSL_NAMESPACE_BEGIN

BORINGSSL_MAKE_DELETER(BIGNUM, BN_free)
BORINGSSL_MAKE_DELETER(BN_CTX, BN_CTX_free)
Expand All @@ -1005,7 +1005,7 @@ class BN_CTXScope {
BN_CTXScope &operator=(BN_CTXScope &) = delete;
};

} // namespace bssl
BSSL_NAMESPACE_END

} // extern C++
#endif
Expand Down
4 changes: 2 additions & 2 deletions include/openssl/buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ OPENSSL_EXPORT size_t BUF_strlcat(char *dst, const char *src, size_t dst_size);

extern "C++" {

namespace bssl {
BSSL_NAMESPACE_BEGIN

BORINGSSL_MAKE_DELETER(BUF_MEM, BUF_MEM_free)

} // namespace bssl
BSSL_NAMESPACE_END

} // extern C++

Expand Down
Loading

0 comments on commit 8c7c635

Please sign in to comment.