-
Notifications
You must be signed in to change notification settings - Fork 671
Add opt-in QUIC_OPENSSL_SYMBOL_PREFIX to namespace bundled OpenSSL symbols (Linux) #6029
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+542
−4
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| # | ||
| # PrefixOpenSSLArchives | ||
| # | ||
| # Produce namespace-prefixed copies of the bundled OpenSSL static archives so | ||
| # the resulting MsQuic binary can coexist in the same process with a different | ||
| # copy of OpenSSL (e.g. a system `libcrypto.so.3` pulled in by an unrelated | ||
| # transitive dependency) without colliding on global C symbols. | ||
| # | ||
| # Mechanism: extract every globally-defined external symbol from the input | ||
| # archives and use `objcopy --redefine-syms` to rewrite the symbol table so | ||
| # both definitions and undefined references carry a `${QUIC_OPENSSL_SYMBOL_PREFIX}` | ||
| # prefix. The rename is applied to: | ||
| # - The bundled `libssl.a` and `libcrypto.a` (their *defined* symbols). | ||
| # - Any consumer's static archive that references OpenSSL (the *undefined* | ||
| # references inside, so they resolve against the prefixed archives at the | ||
| # final link step). For MsQuic this is `libmsquic_platform.a`, handled in | ||
| # `src/platform/CMakeLists.txt`. | ||
| # | ||
| # This is a Linux-only feature today. Honoring `${CMAKE_NM}` / `${CMAKE_OBJCOPY}` | ||
| # makes the rename step cross-compile-safe. macOS support would require | ||
| # `llvm-objcopy` >= 13 (untested); PE/COFF lacks a flat-namespace symbol table | ||
| # and would need a fundamentally different approach. | ||
| # | ||
| # Function: | ||
| # prefix_openssl_archives( | ||
| # PREFIX <symbol_prefix> # e.g. "msquic_" | ||
| # INPUT_TARGET <bundled-openssl-target> # INTERFACE target whose | ||
| # # INTERFACE_LINK_LIBRARIES | ||
| # # contains the .a paths | ||
| # OUTPUT_TARGET <new-interface-target> # name of the renamed target | ||
| # # to create | ||
| # ) | ||
| # | ||
| # After the call: | ||
| # - INTERFACE IMPORTED target `<OUTPUT_TARGET>` exists with include dirs | ||
| # copied from `<INPUT_TARGET>` and link libs pointing at the renamed | ||
| # archives. | ||
| # - Cache variables: | ||
| # `<OUTPUT_TARGET>_REDEFINE_SYMS` absolute path to the syms file | ||
| # (so a consumer's POST_BUILD step can | ||
| # apply the same rename to its own | ||
| # archive) | ||
| # `<OUTPUT_TARGET>_PREFIX_SCRIPT` absolute path to | ||
| # `cmake/openssl-prefix-rename.sh` | ||
| # (so consumers do not have to | ||
| # re-resolve the script path) | ||
| # | ||
|
|
||
| # Capture this module's directory at parse time so the script-path lookup is | ||
| # independent of the parent build's `CMAKE_SOURCE_DIR` (which may not be | ||
| # msquic's own root when msquic is consumed via `add_subdirectory`). | ||
| set(_PREFIX_OPENSSL_SCRIPT "${CMAKE_CURRENT_LIST_DIR}/openssl-prefix-rename.sh") | ||
|
|
||
| function(prefix_openssl_archives) | ||
| set(options) | ||
| set(oneValueArgs PREFIX INPUT_TARGET OUTPUT_TARGET) | ||
| set(multiValueArgs) | ||
| cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) | ||
|
|
||
| if(NOT ARG_PREFIX OR NOT ARG_INPUT_TARGET OR NOT ARG_OUTPUT_TARGET) | ||
| message(FATAL_ERROR "prefix_openssl_archives requires PREFIX, INPUT_TARGET, and OUTPUT_TARGET") | ||
| endif() | ||
|
|
||
| if(NOT TARGET ${ARG_INPUT_TARGET}) | ||
| message(FATAL_ERROR "prefix_openssl_archives: INPUT_TARGET '${ARG_INPUT_TARGET}' does not exist") | ||
| endif() | ||
|
|
||
| # Pull the original archive paths off the INPUT_TARGET INTERFACE properties. | ||
| get_target_property(_openssl_libs ${ARG_INPUT_TARGET} INTERFACE_LINK_LIBRARIES) | ||
| get_target_property(_openssl_inc ${ARG_INPUT_TARGET} INTERFACE_INCLUDE_DIRECTORIES) | ||
|
|
||
| set(_orig_libssl) | ||
| set(_orig_libcrypto) | ||
| foreach(_lib IN LISTS _openssl_libs) | ||
| if(_lib MATCHES "libssl\\.a$") | ||
| set(_orig_libssl "${_lib}") | ||
| elseif(_lib MATCHES "libcrypto\\.a$") | ||
| set(_orig_libcrypto "${_lib}") | ||
| endif() | ||
| endforeach() | ||
|
|
||
| if(NOT _orig_libssl OR NOT _orig_libcrypto) | ||
| message(FATAL_ERROR | ||
| "prefix_openssl_archives: ${ARG_INPUT_TARGET} does not expose both libssl.a and libcrypto.a as plain archive paths on its INTERFACE_LINK_LIBRARIES. " | ||
| "Got INTERFACE_LINK_LIBRARIES='${_openssl_libs}', found libssl.a='${_orig_libssl}' libcrypto.a='${_orig_libcrypto}'. " | ||
| "This helper only handles bundled OpenSSL configurations whose interface points directly at the two static archives; " | ||
| "generator-expression-wrapped or target-name link items are not supported.") | ||
| endif() | ||
|
|
||
| set(_out_dir "${CMAKE_BINARY_DIR}/openssl-prefixed/${ARG_PREFIX}") | ||
| set(_syms_file "${_out_dir}/redefine.syms") | ||
| set(_renamed_libssl "${_out_dir}/libssl.a") | ||
| set(_renamed_libcrypto "${_out_dir}/libcrypto.a") | ||
|
leikong marked this conversation as resolved.
|
||
| set(_script "${_PREFIX_OPENSSL_SCRIPT}") | ||
|
|
||
| file(MAKE_DIRECTORY "${_out_dir}") | ||
|
leikong marked this conversation as resolved.
|
||
|
|
||
| # Forward the cross-compile-aware nm/objcopy to the helper script. If | ||
| # ${CMAKE_NM} / ${CMAKE_OBJCOPY} are unset, the script falls back to its | ||
| # own defaults (plain `nm` / `objcopy`). | ||
| set(_env_args) | ||
| if(CMAKE_NM) | ||
| list(APPEND _env_args "NM=${CMAKE_NM}") | ||
| endif() | ||
| if(CMAKE_OBJCOPY) | ||
| list(APPEND _env_args "OBJCOPY=${CMAKE_OBJCOPY}") | ||
| endif() | ||
|
|
||
| add_custom_command( | ||
| OUTPUT "${_syms_file}" | ||
| DEPENDS "${_orig_libssl}" "${_orig_libcrypto}" "${_script}" | ||
| COMMAND ${CMAKE_COMMAND} -E env ${_env_args} | ||
| "${_script}" gen-syms "${ARG_PREFIX}" "${_syms_file}" | ||
| "${_orig_libssl}" "${_orig_libcrypto}" | ||
| COMMENT "Generating OpenSSL symbol prefix-map (prefix=${ARG_PREFIX})" | ||
| VERBATIM | ||
| ) | ||
|
|
||
| add_custom_command( | ||
| OUTPUT "${_renamed_libssl}" | ||
| DEPENDS "${_orig_libssl}" "${_syms_file}" "${_script}" | ||
| COMMAND ${CMAKE_COMMAND} -E env ${_env_args} | ||
| "${_script}" apply "${_syms_file}" "${_orig_libssl}" "${_renamed_libssl}" | ||
| COMMENT "Prefix-renaming libssl.a (prefix=${ARG_PREFIX})" | ||
| VERBATIM | ||
| ) | ||
|
|
||
| add_custom_command( | ||
| OUTPUT "${_renamed_libcrypto}" | ||
| DEPENDS "${_orig_libcrypto}" "${_syms_file}" "${_script}" | ||
| COMMAND ${CMAKE_COMMAND} -E env ${_env_args} | ||
| "${_script}" apply "${_syms_file}" "${_orig_libcrypto}" "${_renamed_libcrypto}" | ||
| COMMENT "Prefix-renaming libcrypto.a (prefix=${ARG_PREFIX})" | ||
| VERBATIM | ||
| ) | ||
|
|
||
| add_custom_target( | ||
| ${ARG_OUTPUT_TARGET}_Build | ||
| DEPENDS "${_renamed_libssl}" "${_renamed_libcrypto}" "${_syms_file}" | ||
| ) | ||
|
|
||
| add_library(${ARG_OUTPUT_TARGET} INTERFACE IMPORTED GLOBAL) | ||
| add_dependencies(${ARG_OUTPUT_TARGET} ${ARG_OUTPUT_TARGET}_Build) | ||
| set_target_properties( | ||
| ${ARG_OUTPUT_TARGET} | ||
| PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${_openssl_inc}" | ||
|
leikong marked this conversation as resolved.
|
||
| ) | ||
| target_link_libraries( | ||
| ${ARG_OUTPUT_TARGET} | ||
| INTERFACE "${_renamed_libssl}" "${_renamed_libcrypto}" | ||
| ) | ||
|
|
||
| set(${ARG_OUTPUT_TARGET}_REDEFINE_SYMS "${_syms_file}" CACHE INTERNAL | ||
| "Path to the prefix-rename syms file used by ${ARG_OUTPUT_TARGET}") | ||
| set(${ARG_OUTPUT_TARGET}_PREFIX_SCRIPT "${_script}" CACHE INTERNAL | ||
| "Path to cmake/openssl-prefix-rename.sh used by ${ARG_OUTPUT_TARGET}") | ||
|
|
||
| # NOTE: We do NOT pre-create empty placeholder libssl.a / libcrypto.a here. | ||
| # Earlier revisions did, ostensibly to keep Ninja's dep check happy on a | ||
| # clean tree, but empty placeholders mask real failures: if gen-syms or | ||
| # apply silently produced a 0-byte archive, the placeholder would let the | ||
| # link step blow up with a baffling "no symbols" error instead of the | ||
| # original tool failure. Ninja is perfectly happy to schedule the custom | ||
| # commands above for files that don't yet exist. | ||
| # | ||
| # NOTE: ${CMAKE_BINARY_DIR}/openssl-prefixed/${ARG_PREFIX} is NOT made | ||
| # config-specific. The feature is Linux-only today (enforced upstream), | ||
| # and Linux builds use single-config generators (Make/Ninja) where | ||
| # CMAKE_BINARY_DIR is per-config. Revisit if/when we ever extend to a | ||
| # multi-config generator. | ||
|
|
||
| message(STATUS "Configured prefixed OpenSSL archives (${ARG_OUTPUT_TARGET}):") | ||
| message(STATUS " Prefix: ${ARG_PREFIX}") | ||
| message(STATUS " Syms file: ${_syms_file}") | ||
| message(STATUS " libssl.a: ${_renamed_libssl}") | ||
| message(STATUS " libcrypto.a: ${_renamed_libcrypto}") | ||
| endfunction() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| #!/bin/bash | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| # openssl-prefix-rename.sh | ||
| # | ||
| # Generate a symbol-prefix map from one or more static archives and apply it | ||
| # via `objcopy --redefine-syms` to produce renamed copies. | ||
| # | ||
| # Used by `cmake/PrefixOpenSSLArchives.cmake` to namespace-prefix the bundled | ||
| # OpenSSL static archives (and any consuming archive's undefined references) | ||
| # when `QUIC_OPENSSL_SYMBOL_PREFIX` is set. This lets the resulting binary | ||
| # coexist with another copy of OpenSSL loaded into the same process (e.g. a | ||
| # system `libcrypto.so.3` pulled in by a transitive dependency) without | ||
| # global-symbol collisions. | ||
| # | ||
| # Modes: | ||
| # gen-syms PREFIX OUT_SYMS_FILE INPUT_AR [INPUT_AR ...] | ||
| # Extract all defined external globals (nm types T/D/R/B/W/V/C) from the | ||
| # input archives, sort+unique, and emit `<sym> <PREFIX><sym>` lines. | ||
| # | ||
| # apply SYMS_FILE INPUT_AR OUTPUT_AR | ||
| # Copy INPUT_AR to OUTPUT_AR (skipped if same path) and run | ||
| # `objcopy --redefine-syms=SYMS_FILE` on the copy. The rename touches both | ||
| # definitions and undefined references inside the archive's member objects. | ||
| # | ||
| # Environment overrides (honored for cross-compilation): | ||
| # NM path to the binutils `nm` matching the target architecture | ||
| # (defaults to `nm`; host BFD multi-target usually works but | ||
| # `${CMAKE_NM}` is the safe choice) | ||
| # OBJCOPY path to the binutils `objcopy` matching the target architecture | ||
| # (defaults to `objcopy`) | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| NM=${NM:-nm} | ||
| OBJCOPY=${OBJCOPY:-objcopy} | ||
|
|
||
| # Surface unusable tools as a clear configuration error instead of a generic | ||
| # "no such file or directory" from the underlying exec. | ||
| command -v "$NM" >/dev/null 2>&1 || { echo "$0: NM='$NM' not found" >&2; exit 1; } | ||
| command -v "$OBJCOPY" >/dev/null 2>&1 || { echo "$0: OBJCOPY='$OBJCOPY' not found" >&2; exit 1; } | ||
|
|
||
| usage() { | ||
| cat >&2 <<EOF | ||
| usage: | ||
| $0 gen-syms PREFIX OUT_SYMS_FILE INPUT_AR [INPUT_AR ...] | ||
| $0 apply SYMS_FILE INPUT_AR OUTPUT_AR | ||
|
|
||
|
leikong marked this conversation as resolved.
|
||
| env (optional, for cross-compile): | ||
|
leikong marked this conversation as resolved.
|
||
| NM (default: nm) | ||
| OBJCOPY (default: objcopy) | ||
| EOF | ||
| exit 2 | ||
| } | ||
|
|
||
| cmd=${1:-} | ||
| shift || usage | ||
|
|
||
| case "$cmd" in | ||
| gen-syms) | ||
| [[ $# -ge 3 ]] || usage | ||
| prefix=$1 | ||
| out_syms=$2 | ||
|
leikong marked this conversation as resolved.
|
||
| shift 2 | ||
|
leikong marked this conversation as resolved.
|
||
| # Defined-extern symbol-type filter: | ||
| # T/D/R/B = text / data / read-only / bss globals | ||
| # W/V/C = weak (func / object) and common | ||
| # I = STT_GNU_IFUNC (e.g. AES-NI/SHA-NI dispatch resolvers OpenSSL | ||
| # 3.x emits on x86_64); these are externally-visible defined | ||
| # symbols, so they must end up in the rename map or MsQuic's | ||
| # undefs to them won't get prefixed and unprefixed OpenSSL | ||
| # symbols will still leak through the consumer archive. | ||
| # Do NOT pipe nm's stderr to /dev/null: an unreadable archive, a wrong | ||
| # cross-compile nm, or a corrupt input file should surface its real error | ||
| # message in the custom-command log. | ||
| "$NM" --defined-only --extern-only "$@" \ | ||
| | awk 'NF==3 && $2 ~ /^[TDRBWVCI]$/ {print $3}' \ | ||
| | LC_ALL=C sort -u \ | ||
| | awk -v p="$prefix" '{print $1 " " p $1}' \ | ||
| > "$out_syms" | ||
| # A 0-line syms file would make `objcopy --redefine-syms` a no-op, leaving | ||
| # the build green with unprefixed symbols. Fail loudly instead. | ||
| [[ -s "$out_syms" ]] || { | ||
| echo "$0: gen-syms produced an empty syms file from: $*" >&2 | ||
| echo "$0: (check that nm '$NM' supports the input archives and emits defined-extern globals)" >&2 | ||
| exit 1 | ||
| } | ||
| ;; | ||
|
|
||
| apply) | ||
| [[ $# -eq 3 ]] || usage | ||
| syms=$1 | ||
| in_ar=$2 | ||
| out_ar=$3 | ||
| if [[ "$(readlink -f -- "$in_ar")" != "$(readlink -f -- "$out_ar")" ]]; then | ||
| # Tmp-file + atomic rename so an interrupted run does not leave a | ||
| # fresh-mtime, partially-renamed (or un-renamed) archive at out_ar | ||
| # that the next build would mistake for up-to-date. | ||
| tmp="${out_ar}.tmp.$$" | ||
| trap 'rm -f -- "$tmp"' EXIT | ||
| cp -- "$in_ar" "$tmp" | ||
| "$OBJCOPY" --redefine-syms="$syms" "$tmp" | ||
| mv -- "$tmp" "$out_ar" | ||
| trap - EXIT | ||
| else | ||
| # In-place rename on the same path. No atomic-rename protection is | ||
| # possible here (callers that need crash-safety should pass a distinct | ||
| # out_ar). Used by the POST_BUILD step on libmsquic_platform.a. | ||
| "$OBJCOPY" --redefine-syms="$syms" "$out_ar" | ||
| fi | ||
|
leikong marked this conversation as resolved.
|
||
| ;; | ||
|
|
||
| *) | ||
| usage | ||
| ;; | ||
| esac | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.