19 Commits

Author SHA1 Message Date
c8ef
86c6403232
[compiler-rt][nsan] Add check-cmp flag (#108707)
Add check-cmp flag.

Closes #108435.
2024-09-23 18:49:36 -07:00
Alexander Shaposhnikov
dd754cd262
[compiler-rt][nsan] Update UnwindImpl (#107313)
Implement __sanitizer::BufferedStackTrace::UnwindImpl following msan.
2024-09-04 14:32:46 -07:00
Alexander Shaposhnikov
65d6c47fde [compiler-rt][nsan] Adjust nan check 2024-08-26 06:34:56 +00:00
Alexander Shaposhnikov
5136521236 Reapply "[compiler-rt][nsan] Add support for nan detection" (#105909)
This reverts commit 1f89cd4a1970fee65f5ecb189c4d1a0a376d9bb2.
2024-08-25 10:17:36 +00:00
Vitaly Buka
1f89cd4a19
Revert "[compiler-rt][nsan] Add support for nan detection" (#105909)
Reverts llvm/llvm-project#101531

Fails https://lab.llvm.org/buildbot/#/builders/66/builds/3051
2024-08-23 17:34:04 -07:00
pokeslow
283dff4593
[compiler-rt][nsan] Add support for nan detection (#101531)
Add support for nan detection.
#100305
2024-08-23 14:32:31 -07:00
Fangrui Song
652707a645
[nsan] Use sanitizer allocator
* The performance is better than the glibc allocator.
* Allocator interface functions, sanitizer allocator options, and
  MallocHooks/FreeHooks are supported.
* Shadow memory has specific memory layout requirement. Using libc
  allocator could lead to conflicts.
* When we add a mmap interceptor for reliability (the VMA could reuse a
  previously released VMA that is poisoned): glibc may invoke an
  internal system call to call unmmap, which cannot be intercepted. We
  will not be able to return the shadow memory to the OS.

Similar to dfsan https://reviews.llvm.org/D101204 . Also intercept
operator new/delete to be similar to other sanitizers using the
sanitizer allocator. The align_val_t overload of operator new has
slightly less overhead.

Pull Request: https://github.com/llvm/llvm-project/pull/102764
2024-08-12 13:56:40 -07:00
Fangrui Song
249db518e3
[nsan] Add NsanThread and clear static TLS shadow
On thread creation, asan/hwasan/msan/tsan unpoison the thread stack and
static TLS blocks in case the blocks reuse previously freed memory that
is possibly poisoned. glibc nptl/allocatestack.c allocates thread stack
using a hidden, non-interceptable function.

nsan is similar: the shadow types for the thread stack and static TLS
blocks should be set to unknown, otherwise if the static TLS blocks
reuse previous shadow memory, and `*p += x` instead of `*p = x` is used
for the first assignment, the mismatching user and shadow memory could
lead to false positives.

NsanThread is also needed by the next patch to use the sanitizer
allocator.

Pull Request: https://github.com/llvm/llvm-project/pull/102718
2024-08-11 10:53:18 -07:00
Fangrui Song
6c8d479609 [nsan] GetShadowAddrFor: Use (const) void * to decrease the number of casts 2024-08-09 20:29:21 -07:00
Fangrui Song
93a31cdf7a [nsan] Make #include more conventional 2024-08-09 16:47:05 -07:00
Florian Mayer
76248da8af [compiler-rt] [NSan] leave BufferedStackTrace uninit
Otherwise we have to memset 2040 bytes (255 * 8) for each call

Pull Request: https://github.com/llvm/llvm-project/pull/102254
2024-08-07 15:19:27 -07:00
Dmitry Chestnykh
ddf5725ef1
[nsan] Emit calls to optimized functions (#98900)
As previously noted in nsan.cpp we can implement
optimized variants of `__nsan_copy_values` and
`__nsan_set_value_unknown` if a memory operation
size is known.
Now the instrumentation creates calls to optimized functions if there is
4, 8 or 16-byte memory operation like
`memset(X, value, 4/8/16)` or `memcpy(dst, src, 4/8/16)`
nsan.cpp provides definitions of the optimized functions.
2024-07-24 11:20:36 +03:00
Nikita Popov
e9b2a25e90
[nsan] Swap alignas and visibility order (NFC) (#98933)
Use `alignas(16) SANITIZER_INTERFACE_ATTRIBUTE` instead of
`SANITIZER_INTERFACE_ATTRIBUTE alignas(16)`, as the former is not
supported prior to clang 16. See https://clang.godbolt.org/z/Wj1193xWK.

This was broken by https://github.com/llvm/llvm-project/pull/96142 as
part of other style changes.
2024-07-17 11:36:07 +02:00
Dmitriy Chestnykh
56ee6a172a
[compiler-rt][nsan] Disable coredump creation (#98807)
Disable core dump creation. 
If NSAN_OPTIONS includes abort_on_error=1, 
the process may hang as the kernel attempts
to create an excessively large core file.



Fix #98806
2024-07-15 11:15:37 -07:00
Dmitriy Chestnykh
a8687dd026
[compiler-rt][nsan] Improve nsan reports (#98798)
Currently NSAN prints reports that are entirely red and the terminal
prompt after the program exits is red too. With this change we make red
only `WARNING` summary and the rest of the report isn't colored.
This behavior is similar to the behavior of other sanitizers.
2024-07-14 12:11:57 -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
Fangrui Song
ef83c25b0e
[nsan] Fix style issue
The initial check-in of compiler-rt/lib/nsan #94322 has a lot of style
issues. Fix them before the history becomes more useful.

Pull Request: https://github.com/llvm/llvm-project/pull/96142
2024-06-20 00:46:10 -07:00