Commit Graph

1006 Commits

Author SHA1 Message Date
Florian Mayer
2ec5c69b68 Revert "[Sanitizers] Intercept timer_create" (#113710)
Reverts llvm/llvm-project#112285
2024-10-25 09:42:01 -07:00
Florian Mayer
b373278767 [Sanitizers] Intercept timer_create (#112285) 2024-10-14 20:46:45 -07:00
Vitaly Buka
36bd9aebc4 [sanitizer] VReport BeforeFork/AfterFork (#111900)
Forks are common suspects for unusual sanitizer behavior.
It can be handy to see them without rebuild.
2024-10-10 13:12:36 -07:00
Vitaly Buka
a0bb2e21c1 [NFC][sanitizer] Move InitTlsSize into InitializePlatformEarly (#108921) 2024-09-18 16:19:35 -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
Vitaly Buka
b07f1be92c [sanitizer] Remove DTLS_on_libc_memalign (#108120)
`DTLS_on_libc_memalign` is called from primary
allocator, so `__sanitizer_get_allocated_begin`
should also be aware of allocation,
and correctly handled by `GetDTLSRange`.
2024-09-12 00:50:54 -07:00
Steven Wu
16f4e85860 Revert "[sanitizer] Remove GetCurrentThread nullness checks from Allocate"
This reverts commit 4411d1e392 for
breaking Darwin bots:
  AddressSanitizer-Unit :: ./Asan-x86_64-calls-Noinst-Test/10/16
  AddressSanitizer-Unit :: ./Asan-x86_64-calls-Noinst-Test/12/16
  AddressSanitizer-Unit :: ./Asan-x86_64-calls-Noinst-Test/13/16
  AddressSanitizer-Unit :: ./Asan-x86_64-inline-Noinst-Test/10/16
  AddressSanitizer-Unit :: ./Asan-x86_64-inline-Noinst-Test/12/16
  AddressSanitizer-Unit :: ./Asan-x86_64-inline-Noinst-Test/13/16
  AddressSanitizer-Unit :: ./Asan-x86_64h-calls-Noinst-Test/10/16
  AddressSanitizer-Unit :: ./Asan-x86_64h-calls-Noinst-Test/12/16
  AddressSanitizer-Unit :: ./Asan-x86_64h-calls-Noinst-Test/13/16
  AddressSanitizer-Unit :: ./Asan-x86_64h-inline-Noinst-Test/10/16
  AddressSanitizer-Unit :: ./Asan-x86_64h-inline-Noinst-Test/12/16
  AddressSanitizer-Unit :: ./Asan-x86_64h-inline-Noinst-Test/13/16
2024-08-15 12:50:03 -07:00
Fangrui Song
4411d1e392 [sanitizer] Remove GetCurrentThread nullness checks from Allocate
The nullness check is unreachable.

* For the main thead and pthread_create created threads, the `*Allocate` functions must be called after `*_current_thread` is set.
set.
* For threads created by Linux's `clone`, static TLS is either reused or
  set to a new value (CLONE_SETTLS).

Make this change for asan/msan and possibly extend the change to other
sanitizers. (asan supports many platforms and I am not 100% certain that
all platforms have the property.)

Pull Request: https://github.com/llvm/llvm-project/pull/102828
2024-08-14 18:32:29 -07:00
Fangrui Song
1d0d1f20e7 [msan] Remove unneeded nullness CHECK
The pointer will immediate be dereferenced.
2024-08-11 12:35:34 -07:00
Fangrui Song
2438f41bc4 Make msan_allocator.cpp more conventional. NFC
nsan will port msan_allocator.cpp.
2024-08-11 12:30:01 -07:00
Fangrui Song
e0ddd42735 [msan] Use namespace qualifier. NFC
nsan will port msan_allocator.cpp and msan_thread.cpp. Clean up the two
files first.
2024-08-09 20:45:39 -07:00
Fangrui Song
ba66d60b1c [sanitizer] Replace ALIGNED with alignas
C++11 `alignas` is already used extensively. `alignas` must precede
`static`, so adjust the ordering accordingly.

msan.cpp: Clang 15 doesn't allow `__attribute__((visibility("default"))) alignas(16)`.
Use the order `alignas(16) SANITIZER_INTERFACE_ATTRIBUTE`. Tested with Clang 7.

Pull Request: https://github.com/llvm/llvm-project/pull/98958
2024-07-15 16:12:42 -07:00
Vitaly Buka
56b73f2a73 [msan] Fix compilation broken by invalid conflict resolution 2024-07-11 19:27:55 -07:00
Vitaly Buka
6dd1f080e1 [NFC][msan] Use %p to print addresses 2024-07-11 18:47:52 -07:00
Vitaly Buka
79bd6287a9 [NFC][msan] Mention sanitizer in error messages
And remove spaces around '-' printing ranges.
2024-07-11 18:38:21 -07:00
Thurston Dang
fff8b3236a [msan] Block signals during MsanThread::TSDDtor (#98405)
MSan may segfault inside a signal handler, if MSan instrumentation is
trying to access thread-local storage that has already been destroyed.
This fixes the issue by blocking asychronous signals inside
MsanThread::TSDDtor. This is based on an idea suggested by Paul
Pluzhnikov (block async signals in MsanThread::Destroy()) and refined by
Vitaly Buka.
    
Note: ed8565cf0b changed *BlockSignals to
only block asynchronous signals, despite the name.
2024-07-11 08:26:04 -07:00
Thurston Dang
ae9bab563a [NFC] Clang-format the file 2024-07-10 18:24:14 -07:00
Vitaly Buka
f0f774ebf0 [sanitizer] Rename DEFINE_REAL_PTHREAD_FUNCTIONS (#96527)
We use REAL() calls in interceptors, but
DEFINE_REAL_PTHREAD_FUNCTIONS has nothing to do
with them and only used for internal maintenance
threads.

This is done to avoid confusion like in #96456.
2024-06-25 09:42:01 -07:00
Thurston Dang
62c61aa2bf [msan] Change #ifdef SANITIZER_PPC to #if (#94009)
0e96eebc7f
accidentally turned the prior patch
(57a507930b)
into a no-op because this macro is always defined (as either 1 or 0).
This patch changes it to correctly use #if.
2024-05-31 14:26:49 -07:00
Thurston Dang
0e96eebc7f [msan] Reland: Increase k num stack origin descrs (limited to non-PowerPC) (#93117)
The original pull request
(https://github.com/llvm/llvm-project/pull/92838) was reverted due to a
PowerPC buildbot breakage
(df626dd11c).
This reland limits the scope of the change to non-PowerPC platforms. I
am unaware of any PowerPC use cases that would benefit from a larger
kNumStackOriginDescrs constant.

Original CL description: This increases the constant size of
kNumStackOriginDescrs to 4M (64GB of BSS across two arrays), which ought
to be enough for anybody.

This is the easier alternative suggested by eugenis@ in
https://github.com/llvm/llvm-project/pull/92826.
2024-05-28 12:52:45 -07:00
Wu Yingcong
511077df76 [sanitizers] Bump malloc limit to 1TB for MSAN, LSAN & DFSAN (#89728)
We already have `const uptr kMaxAllowedMallocSize = 1ULL << 40;` set for
ASAN, HWASAN, memprof, TSAN. This patch bumps the malloc limit for MSAN,
LSAN and DFSAN to 1TB as well. 8GB is simply not enough nowadays.
2024-05-22 10:15:22 +08:00
Thurston Dang
df626dd11c Revert "[msan] Increase kNumStackOriginDescrs constant (#92838)"
This reverts commit 57a507930b.

Reason: buildbot breakage
(https://lab.llvm.org/buildbot/#/builders/57/builds/35160)
2024-05-21 21:45:37 +00:00
Thurston Dang
57a507930b [msan] Increase kNumStackOriginDescrs constant (#92838)
This increases the constant size of kNumStackOriginDescrs to 4M (64GB of
BSS across two arrays), which ought to be enough for anybody.

This is the easier alternative suggested by eugenis@ in
https://github.com/llvm/llvm-project/pull/92826.
2024-05-21 12:41:36 -07:00
Thurston Dang
dc0cb17241 [msan] Nit: use init_origins parameter instead of __msan_get_track_origins() in InitShadowWithReExec (#86994)
This fixes a nit I had accidentally introduced in
https://github.com/llvm/llvm-project/pull/85142

I don't think the value of __msan_get_track_origins() will change
between the start and end of InitShadowWithReExec, but it's cleaner to
use the parameter.
2024-03-31 18:49:31 -07:00
NAKAMURA Takumi
8f86c6bf95 compiler-rt: Fix variadic macro warnings [-Wc++20-extensions]
They began complaining since #84520.
2024-03-26 08:45:12 +09:00
Alexander Richardson
6f9297fc4d [compiler-rt] Fix build race with COMPILER_RT_TEST_STANDALONE_BUILD_LIBS
Since this standalone build configuration uses the runtime libraries that
are being built just now, we need to ensure that e.g. the TSan unit tests
depend on the tsan runtime library. Also fix TSAN_DEPS being overridden
to not include the tsan runtime (commit .....).
This change fixes a build race seen in the CI checks for
TsanRtlTest-x86_64-Test in https://github.com/llvm/llvm-project/pull/83088.

Reviewed By: vitalybuka

Pull Request: https://github.com/llvm/llvm-project/pull/83650
2024-03-21 15:22:06 -07:00
Alexander Richardson
ba2dc2953c [compiler-rt] Don't check COMPILER_RT_STANDALONE_BUILD for test deps
With https://github.com/llvm/llvm-project/pull/83088, we now need the
runtimes to be built before running test if
COMPILER_RT_TEST_STANDALONE_BUILD_LIBS is true, since otherwise we
get failures running `ninja check-all` such as the following:
```
/usr/bin/ld: cannot find .../compiler-rt/cmake-build-all-sanitizers/lib/linux/libclang_rt.fuzzer-x86_64.a: No such file or directory
/usr/bin/ld: cannot find .../compiler-rt/cmake-build-all-sanitizers/lib/linux/libclang_rt.xray-x86_64.a: No such file or directory
/usr/bin/ld: cannot find .../compiler-rt/cmake-build-all-sanitizers/lib/linux/libclang_rt.xray-basic-x86_64.a: No such file or directory
/usr/bin/ld: cannot find .../compiler-rt/cmake-build-all-sanitizers/lib/linux/libclang_rt.xray-fdr-x86_64.a: No such file or directory
```

This is a follow-up to 058e9b03 which started removing these checks
and it should make it easier to stop forcing COMPILER_RT_STANDALONE_BUILD
for runtimes builds in the future.

Reviewed By: vitalybuka

Pull Request: https://github.com/llvm/llvm-project/pull/83651
2024-03-18 16:48:37 -07:00
Thurston Dang
58f7251820 [msan] Re-exec with no ASLR if memory layout is incompatible on Linux (#85142)
This ports the change from TSan
(0784b1eefa).

Testing notes: run 'sudo sysctl vm.mmap_rnd_bits=32; ninja check-msan'
before and after this patch.

N.B. aggressive ASLR may also cause the app to overlap with the
allocator region; for MSan, this was fixed in
af2bf86a37
2024-03-15 09:49:00 -07:00
Thurston Dang
af2bf86a37 [msan] Add 'MappingDesc::ALLOCATOR' type and check it is available (#85153)
MSan divides the virtual address space into APP, INVALID, SHADOW and
ORIGIN memory. The allocator usually just steals a bit of the APP
address space: typically the bottom portion of the PIE binaries section,
which works because the Linux kernel maps from the top of the PIE
binaries section. However, if ASLR is very aggressive, the binary may
end up mapped in the same location where the allocator wants to live;
this results in a segfault.

This patch adds in a MappingDesc::ALLOCATOR type and enforces that the
memory range for the allocator is not occupied by anything else.

Since the allocator range information is not readily available in
msan.h, we duplicate the information from msan_allocator.cpp.

Note: aggressive ASLR can also lead to a different type of failure,
where the PIE binaries/libraries are mapped entirely outside of the
APP/ALLOCATOR sections; that will be addressed in a separate patch
(https://github.com/llvm/llvm-project/pull/85142).
2024-03-14 16:19:30 -07:00
Alexander Richardson
55b90b5140 [compiler-rt] Remove llvm_gtest dependency from unit tests
All these unit tests already include ${COMPILER_RT_GTEST_SOURCE} as an
input source file and the target llvm_gtest does not exist for
standalone builds. Currently the DEPS argument is ignored for standalone
builds so the missing target is not a problem, but as part of fixing a
build race for standalone builds I am planning to include those
dependencies in COMPILER_RT_TEST_STANDALONE_BUILD_LIBS configurations.

Reviewed By: vitalybuka

Pull Request: https://github.com/llvm/llvm-project/pull/83649
2024-03-13 11:28:44 -07:00
Fangrui Song
fe1d02b08c [sanitizer] Reject unsupported -static at link time (#83524)
Most sanitizers don't support static linking. One primary reason is the
incompatibility with interceptors. `GetTlsSize` is another reason.
asan/memprof use `__interception::DoesNotSupportStaticLinking`
(`_DYNAMIC` reference) to reject -static at link time. Port this
detector to other sanitizers. dfsan actually supports -static for
certain cases. Don't touch dfsan.
2024-03-12 23:09:36 -07:00
Alexander Richardson
95bde4b9d6 [MSan] Pass -fsanitize-ignorelist to the instrumented libcxxabi
This ensures that the MSan unit tests are able to pass with an
uninstrumented libunwind. We need to avoid instrumentation for
__gxx_personality_v0, which is part of the default msan_ignorelist.txt
that is installed into the resource directory. However, if we are trying
to test the just-built libraries, this global ignore list may not be
present yet, so we still instrument the function.

Arguably this function should not be on the default ignore list since it
is only a problem when building libcxxabi with MSan instrumentation and
without an instrumented libunwind, so maybe the logic should really be
part of the libcxxabi build. However, that could be done as a follow-up.

See 2f856a36e0 for more context.

Reviewed By: vitalybuka

Pull Request: https://github.com/llvm/llvm-project/pull/83652
2024-03-06 22:14:15 -08:00
Vitaly Buka
8980936380 [msan] Unwind stack before fatal reports (#77168)
Msan does not unwind stack in malloc without origins, but we still need
trace
for fatal errors.
2024-01-08 17:15:16 -08:00
Vitaly Buka
eea627e3e3 [NFC][msan] Switch allocator interface to use BufferedStackTrace (#77363)
We will need it to unwind for fatal errors.
2024-01-08 11:52:07 -08:00
David CARLIER
44089c21c1 [compiler-rt] intercept sched_getaffinity on freebsd. (#76509)
freebsd releases up to 13.1 are eol and sched_getaffinity wrapper is
available since 13.1.
2023-12-28 11:55:16 -08:00
Vitaly Buka
0e07bf91f7 [NFC][sanitizer] Rename to Lock{Before,After}Fork StackDepotBase locking (#76303)
Followup to #76279
2023-12-23 16:23:55 -08:00
Vitaly Buka
f78a742ab8 [NFC][sanitizer] Rename Lock{Before,After}Fork suffixes locking StackDepotBase (#76279)
This is preparation for performance optimization.

We need to highlight that this is very specific lock, and should not be
used for other purposes.
Add `fork_child` parameter to distinguish processes after fork.
2023-12-22 23:38:01 -08:00
Vitaly Buka
fcce843227 [msan] Use pthread_atfork instead of interceptor (#75398)
This is done for consistency with other sanitizers.
Also lock the allocator.
2023-12-13 15:36:38 -08:00
Vitaly Buka
192840113a [NFC][msan] Clang-format some includes 2023-12-13 14:46:08 -08:00
Vitaly Buka
771e9cda23 [msan] Intercept mallinfo2 only on GLIBC 2.33+
Followup to #73729
2023-11-29 12:15:14 -08:00
Vitaly Buka
6a634953e1 [msan] Intercept mallinfo2 (#73729) 2023-11-29 11:00:17 -08:00
Vitaly Buka
c954414c0a [msan][aarch64] Fix mallinfo interceptor (#73728)
Not sure how the previous implementation supposed to work, but the test
was disabled.

This implementation works for x86_64 and aarch64.
2023-11-28 21:09:13 -08:00
Andres Villegas
f8ae2e4277 Reland: [sanitizer_symbolizer] Add StackTracePrinter virtual class (#66689)
Introduce a new virtual class StackTracePrinter and an implementation
FormattedStackTracePrinter in preparation of enabling symbolizer markup
for linux.
This change allows us to implement other behaviour under the same api
for StackTracePrinter, for example, MarkupStackTracePrinter.

Reason for revert: A missing header file for the
sanitizer_symbolizer_markup.cpp files.
This was not caught in local builds or pre-merge checks given that to
trigger the error, the code
has to be compiled for Fuchsia.
For this reland I've build for the fuchsia targets as well as linux.
2023-09-21 11:28:20 -07:00
Andrés Villegas
80e810f5a7 Revert "[NFC][sanitizer_symbolizer]Add StackTracePrinter class (#66530)"
Reason: Not including appropiated headers for
sanitizer_symbolizer_markup.cpp which only runs
in fuchsia.

This reverts commit d4152c5cbf.
2023-09-15 22:16:26 +00:00
Andres Villegas
d4152c5cbf [NFC][sanitizer_symbolizer]Add StackTracePrinter class (#66530)
Introduce a new virtual class StackTracePrinter and an implementation
FormattedStackTracePrinter in preparation of enabling symbolizer markup
for linux. 
This change allows us to implement other behaviour under the same api
for StackTracePrinter, for example, MarkupStackTracePrinter.
2023-09-15 13:52:09 -07:00
Vitaly Buka
058e9b0374 [test] Don't check COMPILER_RT_STANDALONE_BUILD for deps (#66259)
COMPILER_RT_STANDALONE_BUILD is only needed to be
checked for dependencies outside of compiler-rt.
2023-09-15 12:29:59 -07:00
Ulrich Weigand
208f9a2afc [msan][s390x] Fix long double interceptors
s390x is one of the architectures where the "long double" type was changed
from a 64-bit IEEE to a 128-bit IEEE type back in the glibc 2.4 days.
This means that glibc still exports two versions of the long double functions
(those that already existed back then), and we have to intercept the correct
version. There is already an existing define SANITIZER_NLDBL_VERSION that
indicates this situation, we simply have to respect it when intercepting
strtold and wcstold.

In addition, on s390x a long double return value is passed in memory via
implicit reference. This means the interceptor for functions returning
long double has to unpoison that memory slot, or else we will get
false-positive uninitialized memory reference warnings when the caller
accesses that return value - similar to what is already done in the
mallinfo interceptor. Create a variant macro INTERCEPTOR_STRTO_SRET and
use it on s390x.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D159378
2023-09-02 12:36:53 +02:00
Brooks Davis
692344d873 [msan] Fix compilation on non-glibc
SANITIZER_GLIBC is always defined so should be tested with an if not an
ifdef.

Fixes: ad7e250100

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D159041
2023-08-28 21:22:29 -07:00