Skip to content
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

[aot] Clean up exported symbols for libtaichi_c_api.so #6140

Merged
merged 21 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/scripts/aot-demo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,26 @@ function build-and-test-headless-demo-desktop {
python3 ci/run_tests.py -l $TAICHI_C_API_INSTALL_DIR
}

function check-c-api-export-symbols {
cd taichi
TAICHI_REPO_DIR=$(pwd)
TAICHI_C_API_DIR=$(find $TAICHI_REPO_DIR -name libtaichi_c_api.* | head -n 1)

# T: global functions
# B: global variables (uninitialized)
# D: global variables (initialized)
EXPORT_SYM=" T \| B \| D "

# Note: this has to be consistent with the version scripts (export_symbol_linux.ld, export_symbol_mac.ld)
CAPI_SYM=" _\?ti_"
jim19930609 marked this conversation as resolved.
Show resolved Hide resolved
CAPI_UTILS_SYM=" capi::utils::"

NUM_LEAK_SYM=$(nm -C --extern-only ${TAICHI_C_API_DIR} | grep "${EXPORT_SYM}" | grep -v "${CAPI_SYM}" | grep -v "${CAPI_UTILS_SYM}" | wc -l)
if [ ${NUM_LEAK_SYM} -gt 0 ]; then
echo "Following symbols leaked from libtaichi_c_api: "
nm -C --extern-only ${TAICHI_C_API_DIR} | grep "${EXPORT_SYM}" | grep -v "${CAPI_SYM}" | grep -v "${CAPI_UTILS_SYM}"
exit 1
fi
}

$1
16 changes: 16 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ jobs:
-DTI_BUILD_TESTS:BOOL=ON
-DTI_WITH_C_API=ON

- name: Check C-API Export Symbols
run: |
[[ ${{needs.check_files.outputs.run_job}} == false ]] && exit 0
. .github/workflows/scripts/common-utils.sh

ci-docker-run-gpu --name taichi-test-check-c-api-export-symbols \
registry.taichigraphics.com/taichidev-ubuntu18.04:v0.3.4 \
/home/dev/taichi/.github/workflows/scripts/aot-demo.sh check-c-api-export-symbols

- name: Test
id: test
run: |
Expand Down Expand Up @@ -359,6 +368,13 @@ jobs:
-DTI_WITH_BACKTRACE:BOOL=ON
-DTI_WITH_C_API=ON

- name: Check C-API Export Symbols
run: |
[[ ${{needs.check_files.outputs.run_job}} == false ]] && exit 0
. .github/workflows/scripts/common-utils.sh

.github/workflows/scripts/aot-demo.sh check-c-api-export-symbols

- name: Test
id: test
run: |
Expand Down
5 changes: 5 additions & 0 deletions c_api/version_scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Taichi C-API uses version script to control symbol exports from the shared library (https://github.com/taichi-dev/taichi/issues/6722)

Note that the two version scripts export_symbols_linux.ld and export_symbols_mac.ld should have consistent contents otherwise will cause symbols mismatch on different platforms.

CI will check for symbol leakage automatically, the regular expression rule of which should also stay consistent with the version scripts. CI scripts can be found at "check-c-api-export-symbols" function in .github/workflows/scripts/aot-demo.sh
8 changes: 8 additions & 0 deletions c_api/version_scripts/export_symbols_linux.lds
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
LIB_C_API_1.0 {
global: # specify symbols to be export from libtaichi_c_api
ti_*; # CAPI interface starts with "ti_*"
_ZN4capi5utils*; # matches "capi::utils::*" for CAPI tests

local:
*; # all other symbols will be convert to local bind type whatsoever
};
2 changes: 2 additions & 0 deletions c_api/version_scripts/export_symbols_mac.lds
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_ti_*
jim19930609 marked this conversation as resolved.
Show resolved Hide resolved
__ZN4capi5utils*
9 changes: 9 additions & 0 deletions cmake/TaichiCAPI.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ target_link_libraries(${TAICHI_C_API_NAME} PRIVATE taichi_ui_vulkan)
target_link_libraries(${TAICHI_C_API_NAME} PRIVATE taichi_ui)
endif()

# Avoid exporting third party symbols from libtaichi_c_api.so
# Note that on Windows, external symbols will be excluded from .dll automatically, by default.
if(LINUX)
target_link_options(${TAICHI_C_API_NAME} PRIVATE -Wl,--version-script,${CMAKE_CURRENT_SOURCE_DIR}/c_api/version_scripts/export_symbols_linux.lds)
elseif(APPLE)
# Unfortunately, ld on MacOS does not support --exclude-libs and we have to manually specify the exported symbols
target_link_options(${TAICHI_C_API_NAME} PRIVATE -Wl,-exported_symbols_list,${CMAKE_CURRENT_SOURCE_DIR}/c_api/version_scripts/export_symbols_mac.lds)
endif()

set(C_API_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/build")
set_target_properties(${TAICHI_C_API_NAME} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${C_API_OUTPUT_DIRECTORY}
Expand Down
1 change: 1 addition & 0 deletions cmake/TaichiCAPITests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ if (WIN32)
set_target_properties(${C_API_TESTS_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${C_API_TESTS_OUTPUT_DIR})
endif()
target_link_libraries(${C_API_TESTS_NAME} PRIVATE taichi_c_api)
target_link_libraries(${C_API_TESTS_NAME} PRIVATE taichi_common)
target_link_libraries(${C_API_TESTS_NAME} PRIVATE gtest_main)

if (TI_WITH_BACKTRACE)
Expand Down
1 change: 1 addition & 0 deletions cmake/TaichiTests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ if (WIN32)
endif()
target_link_libraries(${TESTS_NAME} PRIVATE taichi_core)
target_link_libraries(${TESTS_NAME} PRIVATE gtest_main)
target_link_libraries(${TESTS_NAME} PRIVATE taichi_common)

if (TI_WITH_BACKTRACE)
target_link_libraries(${TESTS_NAME} PRIVATE ${BACKWARD_ENABLE})
Expand Down