Skip to content

[BUGFIX] run_tests under ASan, TSan and LSan: count allocations through the sanitizer hooks - #347

Open
squatchlr wants to merge 1 commit into
sdatkinson:mainfrom
Squatch-Stack:submit/alloc-sanitizers
Open

squatchlr wants to merge 1 commit into
sdatkinson:mainfrom
Squatch-Stack:submit/alloc-sanitizers

Conversation

@squatchlr

Copy link
Copy Markdown

Makes run_tests usable under AddressSanitizer, ThreadSanitizer and LeakSanitizer.

Problem. tools/test/allocation_tracking.cpp replaces malloc and friends and finds the originals through dlsym(RTLD_NEXT). Under a sanitizer the runtime owns malloc, and that replacement recurses: on main today, run_tests built with -fsanitize=address segfaults before printing anything (exit 139). With -fsanitize=leak it fails to link (duplicate operator new).

Fix. When built with a sanitizer, count allocations through the sanitizer's own hooks (__sanitizer_install_malloc_and_free_hooks) instead. The real-time-safety tests keep their meaning, since they only ever check "zero" against "more than zero". Builds without a sanitizer are unchanged: the new code sits entirely behind #ifdef NAM_TEST_UNDER_SANITIZER.

Reproduce.

cmake -B build-asan -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_FLAGS=-fsanitize=address -DCMAKE_EXE_LINKER_FLAGS=-fsanitize=address
cmake --build build-asan -j && ./build-asan/tools/run_tests

On main: exit 139, no output. With this change: Success!. Checked the same way with thread and leak, on Linux with clang 18.

One file, +35 lines.

…izer

Under ASan, TSan or LSan the runtime owns malloc, and replacing it through
dlsym(RTLD_NEXT) crashes run_tests before main (ASan: segfault, no output;
LSan: duplicate operator new at link time). With a sanitizer,
allocation_tracking now counts through __sanitizer_install_malloc_and_free_hooks
instead, so the real-time-safety tests keep their meaning there. Builds
without a sanitizer are unchanged.

Reproduce: configure with -DCMAKE_CXX_FLAGS=-fsanitize=address (or thread,
leak) and the matching linker flag; on main run_tests exits 139 with no
output, on this branch it passes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant