catchorg / catchorg/Catch2

catch_discover_tests with ASan results in errors running test executable

Open
#2,507 0 comments 2 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
21.5k
Forks
3.5k
Avg merge
3d 16h
Merged PRs (30d)
2

Description

**Describe the bug**
Compiling [my project](https://github.com/volt-software/Ichor) with address sanitizer enabled results in Catch2 not being able to run the text executables:
```
cd /home/oipo-unencrypted/Programming/ichor/build2/test && /usr/bin/cmake -D TEST_TARGET=DependencyManagerTests -D TEST_EXECUTABLE=/home/oipo-unencrypted/Programming/ichor/bin/DependencyManagerTests -D TEST_EXECUTOR= -D TEST_WORKING_DIR=/home/oipo-unencrypted/Programming/ichor/build2/test -D TEST_SPEC= -D TEST_EXTRA_ARGS= -D TEST_PROPERTIES= -D TEST_PREFIX= -D TEST_SUFFIX= -D TEST_LIST=DependencyManagerTests_TESTS -D TEST_REPORTER= -D TEST_OUTPUT_DIR= -D TEST_OUTPUT_PREFIX= -D TEST_OUTPUT_SUFFIX= -D TEST_DL_PATHS= -D CTEST_FILE=/home/oipo-unencrypted/Programming/ichor/build2/test/DependencyManagerTests_tests-b12d07c.cmake -P /home/oipo-unencrypted/Programming/ichor/external/Catch2/extras/CatchAddTests.cmake
==46102==ASan runtime does not come first in initial library list; you should either link runtime to your application or manually preload it with LD_PRELOAD.
CMake Error at /home/oipo-unencrypted/Programming/ichor/external/Catch2/extras/CatchAddTests.cmake:58 (message):
Error running test executable
'/home/oipo-unencrypted/Programming/ichor/bin/DependencyManagerTests':

Result: 1
Output:

make[2]: *** [test/CMakeFiles/DependencyManagerTests.dir/build.make:91: ../bin/DependencyManagerTests] Error 1
make[2]: *** Deleting file '../bin/DependencyManagerTests'

```

**Expected behavior**
Being able to detect the tests properly.

**Reproduction steps**
See the [test CMakeLists.txt](https://github.com/volt-software/Ichor/blob/80614d375af4e662867e8947f4cff43e01d598d6/test/CMakeLists.txt)
Using `-fsanitize=address` with this setup results in the bug.

**Platform information:**

- OS: **Ubuntu 20.04**
- Compiler+version: **GCC v11.2**
- Catch version: **v3.1.0**

**Additional information**
A workaround is to link to `static-libasan`:
```
foreach(filename ${PROJECT_TEST_SOURCES})
get_filename_component(testname ${filename} NAME_WE)
add_executable(${testname} ${filename})
target_link_libraries(${testname} ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries(${testname} ichor)
target_link_libraries(${testname} Catch2::Catch2WithMain)

if(ICHOR_USE_SANITIZERS)
target_link_libraries(${testname} -static-libasan)
endif()

catch_discover_tests(${testname})
endforeach()
```

Contributor guide

Open the contributing guide

Research direction

Start with extras/CatchAddTests.cmake around the error at line 58 and compare it with the catch_discover_tests call in the linked test/CMakeLists.txt. Reproduce with -fsanitize=address on Ubuntu using the provided setup, then verify that test discovery succeeds without requiring the static-libasan workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
cmake, cpp
Domain
build-system, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.