Skip to content

Commit

Permalink
Fixed use of mock binaries for Glibc hook unit tests under AddressSan…
Browse files Browse the repository at this point in the history
…itizer
  • Loading branch information
Madeeks committed Mar 3, 2022
1 parent e14c4aa commit 5c51219
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 24 deletions.
6 changes: 1 addition & 5 deletions CI/utility_docker_functions.bash
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,9 @@ sarus-utest() {
echo "${FUNCNAME^^} with:"
_print_parameters

if [ ${toolchain_file} = "gcc-asan.cmake" ]; then
privileged_excludes="GlibcHook"
fi

_run_cmd_in_container ${image_run} root \
". /sarus-source/CI/utility_functions.bash && \
run_unit_tests $(_build_dir_container) ${privileged_excludes}"
run_unit_tests $(_build_dir_container)"

fail_on_error "${FUNCNAME}: failed"
}
Expand Down
8 changes: 2 additions & 6 deletions CI/utility_functions.bash
Original file line number Diff line number Diff line change
Expand Up @@ -178,20 +178,16 @@ run_sarus_registry() {
}

run_unit_tests() {
local build_dir=$1; shift && privileged_excludes="$1"
local build_dir=$1; shift || error "${FUNCNAME}: missing build_dir argument"
cd ${build_dir}

if [ ! -z ${privileged_excludes} ]; then
privileged_excludes=("--exclude-regex" "${privileged_excludes}")
fi

# Note: use CI/LSan.supp to suppress errors about memory leaks in
# Sarus dependencies, e.g. OpenSSL. Such leaks are out of our control.

sudo -u docker CTEST_OUTPUT_ON_FAILURE=1 ASAN_OPTIONS=detect_leaks=1 LSAN_OPTIONS=suppressions=${build_dir}/../CI/LSan.supp ctest --exclude-regex AsRoot
fail_on_error "Unit tests as normal user failed"

CTEST_OUTPUT_ON_FAILURE=1 ASAN_OPTIONS=detect_leaks=1 LSAN_OPTIONS=suppressions=${build_dir}/../CI/LSan.supp ctest --tests-regex AsRoot "${privileged_excludes[@]}" # Add as many regex as you want with -R. Allow output with -VV.
CTEST_OUTPUT_ON_FAILURE=1 ASAN_OPTIONS=detect_leaks=1 LSAN_OPTIONS=suppressions=${build_dir}/../CI/LSan.supp ctest --tests-regex AsRoot # Add as many regex as you want with -R. Allow output with -VV.
# TODO: allow args forwarding to cpputest so we can run specific testswithin the filtered file with -n for example.
fail_on_error "Unit tests as root user failed"

Expand Down
10 changes: 1 addition & 9 deletions src/hooks/glibc/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,4 @@ set(object_files_directory "${CMAKE_BINARY_DIR}/src/hooks/glibc/CMakeFiles/hooks

add_unit_test(hooks_glibc_GlibcHook_AsRoot test_GlibcHook.cpp "${link_libraries}")

# Link statically only if address sanitizer is not enabled
string(FIND ${CMAKE_CXX_FLAGS} "-fsanitize=address" POS_FOUND)
if(POS_FOUND EQUAL -1)
link_libraries("-static")
endif()

add_executable(lddMockOlder "lddMockOlder.cpp")
add_executable(lddMockEqual "lddMockEqual.cpp")
add_executable(lddMockNewer "lddMockNewer.cpp")
add_subdirectory(mocks)
8 changes: 4 additions & 4 deletions src/hooks/glibc/test/Checker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,17 @@ class Checker {
}

Checker& mockLddWithOlderVersion() {
sarus::common::copyFile(boost::filesystem::current_path() / "lddMockOlder", rootfsDir / lddPath);
sarus::common::copyFile(boost::filesystem::current_path() / "mocks/lddMockOlder", rootfsDir / lddPath);
return *this;
}

Checker& mockLddWithEqualVersion() {
sarus::common::copyFile(boost::filesystem::current_path() / "lddMockEqual", rootfsDir / lddPath);
sarus::common::copyFile(boost::filesystem::current_path() / "mocks/lddMockEqual", rootfsDir / lddPath);
return *this;
}

Checker& mockLddWithNewerVersion() {
sarus::common::copyFile(boost::filesystem::current_path() / "lddMockNewer", rootfsDir / lddPath);
sarus::common::copyFile(boost::filesystem::current_path() / "mocks/lddMockNewer", rootfsDir / lddPath);
return *this;
}

Expand Down Expand Up @@ -130,7 +130,7 @@ class Checker {
sarus::common::writeJSON(doc, bundleDir / "config.json");
test_utility::ocihooks::writeContainerStateToStdin(bundleDir);

sarus::common::setEnvironmentVariable("LDD_PATH=" + (boost::filesystem::current_path() / "lddMockEqual").string());
sarus::common::setEnvironmentVariable("LDD_PATH=" + (boost::filesystem::current_path() / "mocks/lddMockEqual").string());
sarus::common::setEnvironmentVariable("LDCONFIG_PATH=ldconfig");
sarus::common::setEnvironmentVariable("READELF_PATH=readelf");
sarus::common::setEnvironmentVariable("GLIBC_LIBS="
Expand Down
8 changes: 8 additions & 0 deletions src/hooks/glibc/test/mocks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Disable sanitizing for static helper binaries mocking ldd
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-sanitize=all")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize=all")
link_libraries("-static")

add_executable(lddMockOlder "lddMockOlder.cpp")
add_executable(lddMockEqual "lddMockEqual.cpp")
add_executable(lddMockNewer "lddMockNewer.cpp")
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 5c51219

Please sign in to comment.