33 Commits

Author SHA1 Message Date
Stefan Schulze Frielinghaus
9a156f6b2b [sanitizer] Replace uptr by usize/SIZE_T in interfaces
For some targets uptr is mapped to unsigned int and size_t to unsigned
long and sizeof(int)==sizeof(long) holds.  Still, these are distinct
types and type checking may fail.  Therefore, replace uptr by
usize/SIZE_T wherever a size_t is expected.

Part of #116957
2024-12-07 20:20:27 -08:00
c8ef
86c6403232
[compiler-rt][nsan] Add check-cmp flag (#108707)
Add check-cmp flag.

Closes #108435.
2024-09-23 18:49:36 -07:00
Vitaly Buka
f13b7d0b02
Reland "[sanitizer] Fix partially initialized static TLS range" (#108883)
Reland llvm/llvm-project#108685

Arguments order was wrong on Windows and Darwin.
2024-09-16 14:05:00 -07:00
Vitaly Buka
9a1d0744ed
Revert "[sanitizer] Fix partially initialized static TLS range" (#108881)
Reverts llvm/llvm-project#108685

Breaks Darwin and Windows
https://lab.llvm.org/buildbot/#/builders/107/builds/2930
https://ci.swift.org/view/all/job/llvm.org/view/LLDB/job/as-lldb-cmake/11684/
2024-09-16 13:51:23 -07:00
Vitaly Buka
b7c9ebe4ec
[sanitizer] Fix partially initialized static TLS range (#108685)
Fixes asan, msan crash on check added in #108684.
The #108684 includes reproducer of the issue.

Change interface of `GetThreadStackAndTls` to
set `tls_begin` and `tls_end` at the same time.
2024-09-16 12:48:24 -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
1601879f5d
[compiler-rt][nsan] Fix strsep interceptor (#106307)
Fix strsep interceptor.
For strsep description see
https://www.man7.org/linux/man-pages/man3/strsep.3.html
2024-08-27 17:40:13 -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
Daniel Martinez
2c3eb8db05
[nsan] Remove mallopt from nsan_interceptors (#101055)
Fixes a build failure on 19.1.0-rc1 when building on linux with musl as
the libc

musl does not provide mallopt, whereas glibc does. mallopt has
portability issues with other libc implementations. Just remove the use.

Co-authored-by: Daniel Martinez <danielmartinez@cock.li>
2024-07-29 15:20:18 -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
Dmitriy Chestnykh
52139d8f9a
[compiler-rt][nsan] Modify NSAN_CFLAGS setting (#98768)
Use -fno-rtti flag to avoid vtables in the runtime library (similarly to asan, dfsan, msan).
Remove unneeded -fPIC from NSAN_CFLAGS.

Fix #98767
2024-07-13 23:13:38 -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
80ff3acd36
[nsan] Add shared runtime
so that `clang -fsanitize=numerical -shared-libsan` will use
`libclang_rt.nsan.so` on Linux.

Shared runtime is preferred for some platforms (Android, Apple, Fuchsia;
though they are not supported yet) and helps plugin use cases (#98302).

* Update `ninja nsan` to build `libclang_rt.nsan.so`
* Fix `nsan.syms.extra`: `nsan_*` is unneeded. Add `__ubsan_*` so that
  `-fsanitize=numerical,undefined -shared-libsan` works.
* Move allocation functions to `nsan_malloc_linux.cpp`. While Apple
  platforms aren't supported yet, this separation makes it easier to add
  Apple support.
* Delete interceptors for very obsoleted pvalloc/valloc but retain
  memalign.
* Replace `HandleEarlyAlloc` with `DlsymAlloc`.

Pull Request: https://github.com/llvm/llvm-project/pull/98415
2024-07-10 18:35:05 -07:00
Vitaly Buka
b96eb76d21
Revert "Revert "[compiler-rt] Remove redundant COMPILER_RT_HAS_*SAN checks"" (#98395)
Reverts llvm/llvm-project#98380

Probably some incremental build issue, the build before revert was
green:
https://lab.llvm.org/buildbot/#/builders/109/builds/817
2024-07-10 14:35:45 -07:00
Vitaly Buka
145ae81fa4
Revert "[compiler-rt] Remove redundant COMPILER_RT_HAS_*SAN checks" (#98380)
Reverts llvm/llvm-project#98240

https://lab.llvm.org/buildbot/#/builders/109/builds/816
2024-07-10 13:30:19 -07:00
Vitaly Buka
e6352604cd
[compiler-rt] Remove redundant COMPILER_RT_HAS_*SAN checks (#98240)
They are checked in the parent CMakeLists in
`compiler_rt_build_runtime` and `compiler_rt_test_runtime`.

There are non-redundant checks when a sanitizer checks
for the presense of another one. They should not be removed.
2024-07-10 13:03:48 -07:00
Alexander Shaposhnikov
1c6bc0458d
[compiler-rt][nsan] cmake cleanup (#98224)
CMake cleanup. NFC.
2024-07-09 17:51:32 -07:00
Alexander Shaposhnikov
e674e739d7
[compiler-rt] Fix build breakage (#96235)
Add missing flags in cmake.
Follow-up to cae6d458a.
2024-06-20 21:51:18 -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
Alexander Shaposhnikov
cae6d458a0
[CompilerRT] Add support for numerical sanitizer (#94322)
This diff contains the compiler-rt changes / preparations for nsan.

Test plan:

1. cd build/runtimes/runtimes-bins && ninja check-nsan
2. ninja check-all
2024-06-19 15:20:36 -07:00