4 Commits

Author SHA1 Message Date
Fangrui Song
93a31cdf7a [nsan] Make #include more conventional 2024-08-09 16:47:05 -07:00
Fangrui Song
a853fe25df
[nsan] Add nsan_preinit.cpp and make it static library only
#94322 defines .preinit_array to initialize nsan early.
DT_PREINIT_ARRAY can only be used with the main executable. GNU ld would
complain when a DSO has .preinit_array. Therefore,
nsan_preinit.cpp cannot be linked into `libclang_rt.nsan.so` (#98415).

Working with @alexander-shaposhnikov, we noticed that `Nsan-x86_64-Test
--gtest_output=json` without `.preinit_array` will sigsegv. This is
because googletest with the JSON output calls `localtime_r` , which
calls `free(0)` and fails when `REAL(free)` remains uninitialized
(nullptr). This is benign with the default output because malloc/free
are all paired and `REAL(free)(ptr)` is not called.

To fix the unittest failure, `__nsan_init` needs to be called early
(.preinit_array).
`asan/tests/CMakeLists.txt:ASAN_UNITTEST_INSTRUMENTED_LINK_FLAGS` ues
`-fsanitize=address` to ensure `asan_preinit.cpp.o` is linked into the
unittest executable. Port the approach and remove
`NSAN_TEST_RUNTIME_OBJECTS`.

Fix #98523

Pull Request: https://github.com/llvm/llvm-project/pull/98564
2024-07-11 18:22:52 -07:00
Vitaly Buka
0b15f89182 Revert "[nsan] Add nsan_preinit.cpp and make it static library only"
https://lab.llvm.org/buildbot/#/builders/66/builds/1345

This reverts commit 2cec041a103137343e1019f6f883bdcdf60db708.
2024-07-10 23:50:41 -07:00
Fangrui Song
2cec041a10 [nsan] Add nsan_preinit.cpp and make it static library only
#94322 defines .preinit_array to initialize nsan early.
DT_PREINIT_ARRAY can only be used with the main executable. GNU ld would
complain when a DSO has .preinit_array .
2024-07-10 22:03:16 -07:00