Skip to content

Commit

Permalink
test: shrink LUA_CPATH and {DY}LD_LIBRARY_PATH
Browse files Browse the repository at this point in the history
This patch sets the unique value of each of these variables for each of
the tests and only where they are needed. Also, it drops the comment
about SIP [1] tricks since it is obsolete after
2989756 ("test: stop using
utils.selfrun in tests").

[1]: https://support.apple.com/en-us/HT204899

Resolves tarantool/tarantool#9898
  • Loading branch information
Buristan committed Sep 20, 2024
1 parent 13bc083 commit 575bae5
Showing 1 changed file with 61 additions and 47 deletions.
108 changes: 61 additions & 47 deletions test/tarantool-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ add_custom_target(tarantool-tests-libs
macro(BuildTestCLib lib sources)
AddTestLib(${lib} ${sources})
add_dependencies(tarantool-tests-libs ${lib})
# Add the directory where the lib is built to the list with
# entries for LUA_CPATH environment variable, so LuaJIT can find
# and load it. See the comment about extending the list in the
# parent scope few lines above.
set(LUA_CPATHS "${CMAKE_CURRENT_BINARY_DIR}/?${CMAKE_SHARED_LIBRARY_SUFFIX};${LUA_CPATHS}" PARENT_SCOPE)
# Also add this directory to LD_LIBRARY_PATH environment
# variable, so FFI machinery can find and load it.
set(LD_LIBRARY_PATH "${CMAKE_CURRENT_BINARY_DIR}:${LD_LIBRARY_PATH}" PARENT_SCOPE)
# Workaround for profilers c-related tests, see the comment
# below.
if(NOT "${CMAKE_CURRENT_BINARY_DIR}" MATCHES profilers)
set(CLIB_DIRS "${CMAKE_CURRENT_BINARY_DIR};${CLIB_DIRS}" PARENT_SCOPE)
endif()
endmacro()

# FIXME: This is used only due to ancient CMake requirements.
Expand Down Expand Up @@ -76,46 +73,8 @@ make_lua_path(LUA_PATH
${LUAJIT_BINARY_DIR}/?.lua
)

# Update LUA_CPATH with the library paths collected within
# <BuildTestLib> macro.
make_lua_path(LUA_CPATH PATHS ${LUA_CPATHS})

set(LUA_TEST_SUFFIX .test.lua)

# XXX: Since the auxiliary libraries are built as a dynamically
# loaded modules on MacOS instead of shared libraries as it is
# done on Linux and BSD, another environment variable should be
# used to guide <ffi.load> while searching the extension.
# XXX: Be noticed that we shouldn't use `"` here to wrap
# the variable's content. If we do this, the variable value will
# contain `"` at the beginning and the end, so this `"` at the
# beginning will be treated as the directory for the first entry
# (the last subdirectory added).
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
# XXX: Apple tries their best to "protect their users from
# malware". As a result SIP (see the link[1] below) has been
# designed and released. Now, Apple developers are so protected,
# that they can load nothing being not installed in the system,
# since some programs sanitize the environment before they start
# child processes. Specifically, environment variables starting
# with DYLD_ and LD_ are unset for child process started by
# other programs (like `ctest` using for launching tests).
# For more info, see the docs[2] below.
#
# These environment variables are used by FFI machinery to find
# the proper shared library, hence we can still tweak testing
# environment before calling <ffi.load>. However, the value
# can't be passed via the standard environment variable, so we
# use ENVIRONMENT property in `set_tests_properties` to get
# around SIP magic tricks.
#
# [1]: https://support.apple.com/en-us/HT204899
# [2]: https://developer.apple.com/library/archive/documentation/Security/Conceptual/System_Integrity_Protection_Guide/RuntimeProtections/RuntimeProtections.html
list(APPEND LUA_TEST_ENV_MORE DYLD_LIBRARY_PATH=${LD_LIBRARY_PATH})
else()
list(APPEND LUA_TEST_ENV_MORE LD_LIBRARY_PATH=${LD_LIBRARY_PATH})
endif()

# Some tests use `LD_PRELOAD` to mock system calls (like
# <lj-802-panic-at-mcode-protfail.test.lua> overwrites
# `mprotect()`. When compiling with ASan support under GCC, it is
Expand Down Expand Up @@ -152,12 +111,67 @@ foreach(test_path ${tests})
# LUA_CPATH and LD_LIBRARY_PATH variables and also
# dependencies list with libraries are set in scope of
# BuildTestLib macro.
ENVIRONMENT "LUA_PATH=${LUA_PATH};LUA_CPATH=${LUA_CPATH};${LUA_TEST_ENV_MORE}"
ENVIRONMENT "LUA_PATH=${LUA_PATH};${LUA_TEST_ENV_MORE}"
LABELS ${TEST_SUITE_NAME}
DEPENDS tarantool-tests-deps
)
endforeach()

# We use the following naming convention: the name of the
# directory containing the C library to be loaded should match the
# prefix of the test itself. The same library may be used in
# several tests. See <lj-1166-error-stitch>, for example.
foreach(clib_dir ${CLIB_DIRS})
file(GLOB_RECURSE tests_using_clib
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
"${clib_dir}*${LUA_TEST_SUFFIX}"
)
foreach(test_name ${tests_using_clib})
set(test_title "test/${TEST_SUITE_NAME}/${test_name}")
# Add the directory where the library is built to the list
# with entries for the LUA_CPATH environment variable, so
# LuaJIT can find and load it.
AppendTestEnvVar(${test_title} LUA_CPATH
"${clib_dir}/?${CMAKE_SHARED_LIBRARY_SUFFIX}\;"
)
# Also add this directory to the LD_LIBRARY_PATH environment
# variable so FFI machinery can find and load it.
# XXX: Be noticed that we shouldn't use `"` here to wrap the
# variable's content. If we do this, the variable value will
# contain `"` at the beginning and the end, so this `"` at the
# beginning will be treated as the directory for the first
# entry (the last subdirectory added).
# XXX: Since the auxiliary libraries are built as dynamically
# loaded modules on MacOS instead of shared libraries as it is
# done on Linux and BSD, another environment variable should
# be used to guide <ffi.load> while searching the extension.
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
AppendTestEnvVar(${test_title} DYLD_LIBRARY_PATH ${clib_dir}:)
else()
AppendTestEnvVar(${test_title} LD_LIBRARY_PATH ${clib_dir}:)
endif()
endforeach()
endforeach()

# XXX: Special workaround for several different loaded libraries
# in the gh-5813-resolving-of-c-symbols.test.lua. Since the libs
# can't be flatterned to one directory (see the comment in their
# CMakeLists.txt), just set them manually here.
set(CSYMBOLS_DIR
${CMAKE_CURRENT_SOURCE_DIR}/profilers/gh-5813-resolving-of-c-symbols
)
make_lua_path(CSYMBOLS_CPATHS
PATHS
${CSYMBOLS_DIR}/both/?${CMAKE_SHARED_LIBRARY_SUFFIX}
${CSYMBOLS_DIR}/gnuhash/?${CMAKE_SHARED_LIBRARY_SUFFIX}
${CSYMBOLS_DIR}/hash/?${CMAKE_SHARED_LIBRARY_SUFFIX}
${CSYMBOLS_DIR}/stripped/?${CMAKE_SHARED_LIBRARY_SUFFIX}
)
AppendTestEnvVar(
test/${TEST_SUITE_NAME}/profilers/gh-5813-resolving-of-c-symbols.test.lua
LUA_CPATH "${CSYMBOLS_CPATHS}"
)

# The part of the profilers toolchain is located in the tools
# directory, so LUA_PATH needs to be updated.
file(GLOB_RECURSE profilers_tests
Expand Down

0 comments on commit 575bae5

Please sign in to comment.