Commit Graph

14462 Commits

Author SHA1 Message Date
Dmitry Vyukov 0bc626d516 tsan: refactor guard_acquire/release
Introduce named consts for magic values we use.

Differential Revision: https://reviews.llvm.org/D107445
2021-08-04 13:52:27 +02:00
Dmitry Vyukov 636428c727 tsan: unify __cxa_guard_acquire and pthread_once implementations
Currently we effectively duplicate "once" logic for __cxa_guard_acquire
and pthread_once. Unify the implementations.

This is not a no-op change:
 - constants used for pthread_once are changed to match __cxa_guard_acquire
   (__cxa_guard_acquire constants are tied to ABI, but it does not seem
   to be the case for pthread_once)
 - pthread_once now also uses PotentiallyBlockingRegion annotations
 - __cxa_guard_acquire checks thr->in_ignored_lib to skip user synchronization
It's unclear if these 2 differences are intentional or a mere sloppy inconsistency.
Since all tests still pass, let's assume the latter.

Reviewed By: vitalybuka, melver

Differential Revision: https://reviews.llvm.org/D107359
2021-08-04 13:44:19 +02:00
Dmitry Vyukov 14e306fa4b tsan: use DCHECK instead of CHECK in atomic functions
Atomic functions are semi-hot in profiles.
The CHECKs verify values passed by compiler
and they never fired, so replace them with DCHECKs.

Reviewed By: vitalybuka, melver

Differential Revision: https://reviews.llvm.org/D107373
2021-08-04 13:23:57 +02:00
Dmitry Vyukov d3faecbb7c tsan: minor MetaMap tweaks
1. Add some comments.
2. Use kInvalidStackID instead of literal 0.
3. Add more LIKELY/UNLIKELY.

Reviewed By: vitalybuka, melver

Differential Revision: https://reviews.llvm.org/D107371
2021-08-04 13:20:44 +02:00
Vitaly Buka 9ab590e3eb [msan] Add bsearch interceptor
Similar to qsort, bsearch can be called from non-instrumented
code of glibc. When it happends tls for arguments can be in uninitialized
state.

Unlike to qsort, bsearch does not move data, so we don't need to
check or initialize searched memory or key. Intrumented comparator will
do that on it's own.

Differential Revision: https://reviews.llvm.org/D107387
2021-08-03 18:39:14 -07:00
Dan Liew b4121b335c [Compiler-rt] Fix running ASan/TSan unit tests under macOS 12.0.
On macOS the unit tests currently rely on libmalloc being used for
allocations (due to no functioning interceptors) but also having the
ASan/TSan allocator initialized in the same process.

This leads to crashes with the macOS 12.0 libmalloc nano allocator so
disable use of the allocator while running unit tests as a workaround.

rdar://80086125

Differential Revision: https://reviews.llvm.org/D107412
2021-08-03 17:46:27 -07:00
Vitaly Buka 81b293ba36 [tests][sanitizers] Don't reflow comments
This lets us to apply ColumnLimit without breaking "RUN:" lines.
2021-08-03 12:54:00 -07:00
Kostya Serebryany ff163ef12b [libFuzzer] fix clang-tidy
[libFuzzer] fix clang-tidy

Reviewed By: morehouse

Differential Revision: https://reviews.llvm.org/D107382
2021-08-03 12:34:12 -07:00
Kostya Serebryany 7c921753e0 [libFuzzer] replace Vector/Set with std::vector/std::set. The custom names are not required any more since we now build with a private version of libc++. Fix some of the 81+ character lines. Mechanical change, NFC expected.
[libFuzzer] replace Vector/Set with std::vector/std::set.

Reviewed By: morehouse

Differential Revision: https://reviews.llvm.org/D107374
2021-08-03 11:31:04 -07:00
Dmitry Vyukov e34d1942a0 tsan: fix a typo in debug output
Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D107368
2021-08-03 18:46:33 +02:00
Vitaly Buka 6538aa8ce9 [NFC][tsan] Rename _inl.h to .inc
Differential Revision: https://reviews.llvm.org/D107319
2021-08-03 09:35:33 -07:00
Kostya Kortchinsky 23a94af449 [scudo] Make Vector() constexpr
A `Vector` that doesn't require an initial `reserve()` (eg: with a
default, or small enough capacity) can have a constant initializer.

This changes the code in a few places to make that possible:
- mark a few other functions as `constexpr`
- do without any `reinterpret_cast`
- allow to skip `reserve` from `init`

Differential Revision: https://reviews.llvm.org/D107308
2021-08-03 08:07:27 -07:00
Dmitry Vyukov ac2bc4e0fc tsan: remove mallopt calls
mallopt calls are left-over from the times we used
__libc_malloc/__libc_free for internal allocations.
Now we have own internal allocator, so this is not needed.

Reviewed By: melver

Differential Revision: https://reviews.llvm.org/D107342
2021-08-03 16:18:09 +02:00
Dmitry Vyukov 7779f49bc1 tsan: remove unused caller_pc from TsanInterceptorContext
Reviewed By: melver

Differential Revision: https://reviews.llvm.org/D107340
2021-08-03 16:17:44 +02:00
Dmitry Vyukov e72ad3c19a tsan: use semaphores for thread creation synchronization
We currently use ad-hoc spin waiting to synchronize thread creation
and thread start both ways. But spinning tend to degrade ungracefully
under high contention (lots of threads are created at the same time).
Use semaphores for synchronization instead.

Reviewed By: melver

Differential Revision: https://reviews.llvm.org/D107337
2021-08-03 13:47:01 +02:00
Dmitry Vyukov 559426ae76 tsan: use Tid/StackID types in MBlock
Replace more raw types with Tid/StackID typedefs.

Reviewed By: melver

Differential Revision: https://reviews.llvm.org/D107335
2021-08-03 12:43:02 +02:00
Dmitry Vyukov d77b476c19 tsan: avoid extra call indirection in unaligned access functions
Currently unaligned access functions are defined in tsan_interface.cpp
and do a real call to MemoryAccess. This means we have a real call
and no read/write constant propagation.

Unaligned memory access can be quite hot for some programs
(observed on some compression algorithms with ~90% of unaligned accesses).

Move them to tsan_interface_inl.h to avoid the additional call
and enable constant propagation.
Also reorder the actual store and memory access handling for
__sanitizer_unaligned_store callbacks to enable tail calling
in MemoryAccess.

Depends on D107282.

Reviewed By: vitalybuka, melver

Differential Revision: https://reviews.llvm.org/D107283
2021-08-03 11:12:49 +02:00
Dmitry Vyukov 18c6ed2f0f tsan: add AccessVptr
Add AccessVptr access type.
For now it's converted to the same thr->is_vptr_access,
but later it will be passed directly to ReportRace
and will enable efficient tail calling in MemoryAccess function
(currently __tsan_vptr_update/__tsan_vptr_read can't use
tail calls in MemoryAccess because of the trailing assignment
to thr->is_vptr_access).

Depends on D107276.

Reviewed By: vitalybuka, melver

Differential Revision: https://reviews.llvm.org/D107282
2021-08-03 11:03:36 +02:00
Dmitry Vyukov 831910c5c4 tsan: new MemoryAccess interface
Currently we have MemoryAccess function that accepts
"bool kAccessIsWrite, bool kIsAtomic" and 4 wrappers:
MemoryRead/MemoryWrite/MemoryReadAtomic/MemoryWriteAtomic.

Such scheme with bool flags is not particularly scalable/extendable.
Because of that we did not have Read/Write wrappers for UnalignedMemoryAccess,
and "true, false" or "false, true" at call sites is not very readable.

Moreover, the new tsan runtime will introduce more flags
(e.g. move "freed" and "vptr access" to memory acccess flags).
We can't have 16 wrappers and each flag also takes whole
64-bit register for non-inlined calls.

Introduce AccessType enum that contains bit mask of
read/write, atomic/non-atomic, and later free/non-free,
vptr/non-vptr.
Such scheme is more scalable, more readble, more efficient
(don't consume multiple registers for these flags during calls)
and allows to cover unaligned and range variations of memory
access functions as well.

Also switch from size log to just size.
The new tsan runtime won't have the limitation of supporting
only 1/2/4/8 access sizes, so we don't need the logarithms.

Also add an inline thunk that converts the new interface to the old one.
For inlined calls it should not add any overhead because
all flags/size can be computed as compile time.

Reviewed By: vitalybuka, melver

Differential Revision: https://reviews.llvm.org/D107276
2021-08-03 11:03:23 +02:00
Florian Mayer 150395c2bc [hwasan] report failing thread for invalid free.
Reviewed By: hctim

Differential Revision: https://reviews.llvm.org/D107270
2021-08-03 08:53:53 +01:00
Vitaly Buka 735da5f5ad [NFC][sanitizer] Add static to internal functions 2021-08-03 00:12:36 -07:00
Vitaly Buka 9205143f07 [NFC][tsan] clang-format two files 2021-08-02 16:28:26 -07:00
Vedant Kumar 3b0a9e7b39 [profile] Move assertIsZero to InstrProfilingUtil.c
... and rename it to 'warnIfNonZero' to better-reflect what it actually
does.

The goal is to minimize the amount of logic that's conditionally
compiled under '#if __APPLE__'.
2021-08-02 15:25:09 -07:00
Vitaly Buka ecc2c9ba45 [sanitizer] Add callbacks for epoll_pwait2
Depends on D107207.

Differential Revision: https://reviews.llvm.org/D107209
2021-08-02 14:14:19 -07:00
Vitaly Buka f6f724c02e [sanitizer] Fix __sanitizer_syscall_post_epoll_wait
Syscall return number of initialized events which
needs to be used for unposoning.

Differential Revision: https://reviews.llvm.org/D107207
2021-08-02 14:14:18 -07:00
Benjamin Kramer 1f04d8ed2e [tsan] Complete renaming kMaxSid to kThreadSlotCount
This was missing from 5c2b48fdb0
2021-08-02 16:52:29 +02:00
Florian Mayer cd2387b56d [hwasan] Commit missed REQUIRES: stable-runtime.
Differential Revision: https://reviews.llvm.org/D107268
2021-08-02 13:58:11 +01:00
Dmitry Vyukov 03372e72d3 tsan: remove unbalanced mutex unlock
The mutex is now unlocked by the scoped Lock object.

Differential Revision: https://reviews.llvm.org/D107266
2021-08-02 14:17:12 +02:00
Dmitry Vyukov 5c2b48fdb0 tsan: add new vector clock
Add new fixed-size vector clock for the new tsan runtime.
For now it's unused.

Reviewed By: melver

Differential Revision: https://reviews.llvm.org/D107167
2021-08-02 13:45:31 +02:00
Dmitry Vyukov 7bd81fe183 tsan: don't save creation stack for some sync objects
Currently we save the creation stack for sync objects always.
But it's not needed to some sync objects, most notably atomics.
We simply don't use atomic creation stack anywhere.
Allow callers to control saving of the creation stack
and don't save it for atomics.

Depends on D107257.

Reviewed By: melver

Differential Revision: https://reviews.llvm.org/D107258
2021-08-02 13:30:24 +02:00
Dmitry Vyukov 14c7507b9d tsan: add LIKELY/UNLIKELY to MetaMap::GetSync
MetaMap::GetSync shows up in profiles,
so add LIKELY/UNLIKELY annotations.

Depends on D107256.

Reviewed By: melver

Differential Revision: https://reviews.llvm.org/D107257
2021-08-02 13:29:55 +02:00
Dmitry Vyukov 9e3e97aa81 tsan: refactor MetaMap::GetAndLock interface
Don't lock the sync object inside of MetaMap methods.
This has several advantages:
 - the new interface does not confuse thread-safety analysis
   so we can remove a bunch of NO_THREAD_SAFETY_ANALYSIS attributes
 - this allows use of scoped lock objects
 - this allows more flexibility, e.g. locking some other mutex
   between searching and locking the sync object
Also prefix the methods with GetSync to be consistent with GetBlock method.
Also make interface wrappers inlinable, otherwise we either end up with
2 copies of the method, or with an additional call.

Reviewed By: melver

Differential Revision: https://reviews.llvm.org/D107256
2021-08-02 13:29:46 +02:00
Florian Mayer 66b4aafa2e [hwasan] Detect use after scope within function.
Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D105201
2021-08-02 11:34:12 +01:00
Dmitry Vyukov 7c6cca4b6e tsan: minor IgnoreSet refactoring
1. Move kMaxSize declaration to private section.
2. Inline Reset, it's trivial and called semi-frequently.

Reviewed By: melver

Differential Revision: https://reviews.llvm.org/D107215
2021-08-02 11:13:51 +02:00
Dmitry Vyukov 8a49e053ca tsan: inline ProcessPendingSignals check
ProcessPendingSignals is called in all interceptors
and user atomic operations. Make the fast-path check
(no pending signals) inlinable.

Reviewed By: melver

Differential Revision: https://reviews.llvm.org/D107217
2021-08-02 11:05:45 +02:00
Freddy Ye d268c20070 [X86] Support auto-detect for tigerlake and alderlake
Differential Revision: https://reviews.llvm.org/D107245
2021-08-02 11:01:01 +08:00
Fangrui Song 18ec93d9e6 [profile][test] Delete --path-equivalence=/tmp,%S
This causes the test to fail if %S is under /tmp
2021-07-31 00:36:17 -07:00
Dmitry Vyukov 61da95e8a1 tsan: prevent insertion of memset into BenignRaceImpl
Some bots started failing with the following error
after changing Alloc to New. Change it back.

ThreadSanitizer: CHECK failed: ((locked[i].recursion)) == ((0))
 4 __sanitizer::CheckedMutex::CheckNoLocks()
 5 __tsan::ScopedInterceptor::~ScopedInterceptor()
 6 memset
 7 __tsan::New<__tsan::ExpectRace>()
 8 __tsan::AddExpectRace()
 9 BenignRaceImpl()

Differential Revision: https://reviews.llvm.org/D107212
2021-07-31 09:17:14 +02:00
Dmitry Vyukov 103d075b05 tsan: introduce Tid and StackID typedefs
Currently we inconsistently use u32 and int for thread ids,
there are also "unique tid" and "os tid" and just lots of other
things identified by integers.
Additionally new tsan runtime will introduce yet another
thread identifier that is very different from current tids.
Similarly for stack IDs, it's easy to confuse u32 with other
integer identifiers. And when a function accepts u32 or a struct
contains u32 field, it's not always clear what it is.

Add Tid and StackID typedefs to make it clear what is what.

Reviewed By: melver

Differential Revision: https://reviews.llvm.org/D107152
2021-07-31 09:05:31 +02:00
Dmitry Vyukov 3ea3b6b2d4 sanitizers: build tests with -g
We currently build tests without -g, which is quite inconvenient.
Crash stacks don't have line numbers, gdb don't how line numbers either.
Always build tests with -g.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D107168
2021-07-31 08:55:12 +02:00
Dmitry Vyukov 5b30ebed96 tsan: remove "expected" races
"Expected" races is a very ancient facility used in tsanv1 tests.
It's not used/needed anymore. Remove it.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D107175
2021-07-31 08:54:49 +02:00
Dmitry Vyukov 53a526790d tsan: always setup sigaction signal handler
Currently we setup either sigaction signal handler with 3 arguments
or old style signal handler with 1 argument depending on user handler type.
This unnecessarily complicates code. Always setup sigaction handler.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D107186
2021-07-31 08:53:48 +02:00
Vitaly Buka 2564f27f18 [NFC][sanitizer] clang-format few files 2021-07-30 19:31:14 -07:00
Petr Hosek 83302c8489 [profile] Fix profile merging with binary IDs
This fixes support for merging profiles which broke as a consequence
of e50a38840d. The issue was missing
adjustment in merge logic to account for the binary IDs which are
now included in the raw profile just after header.

In addition, this change also:
* Includes the version in module signature that's used for merging
to avoid accidental attempts to merge incompatible profiles.
* Moves the binary IDs size field after version field in the header
as was suggested in the review.

Differential Revision: https://reviews.llvm.org/D107143
2021-07-30 18:54:27 -07:00
Petr Hosek d3dd07e3d0 Revert "[profile] Fix profile merging with binary IDs"
This reverts commit dcadd64986.
2021-07-30 18:53:48 -07:00
Vitaly Buka 44c83eccf9 [sanitizer] Remove cpplint annotations
cpplint was removed by D107197

Differential Revision: https://reviews.llvm.org/D107198
2021-07-30 18:20:40 -07:00
Petr Hosek dcadd64986 [profile] Fix profile merging with binary IDs
This fixes support for merging profiles which broke as a consequence
of e50a38840d. The issue was missing
adjustment in merge logic to account for the binary IDs which are
now included in the raw profile just after header.

In addition, this change also:
* Includes the version in module signature that's used for merging
to avoid accidental attempts to merge incompatible profiles.
* Moves the binary IDs size field after version field in the header
as was suggested in the review.

Differential Revision: https://reviews.llvm.org/D107143
2021-07-30 17:38:53 -07:00
Vitaly Buka f08229f49e [sanitizer] Remove cpplint
As code diverge from Google style we need
to add more and more exceptions to suppress
conflicts with clang-format and clang-tidy.
As this point it does not provide a additional value.

Reviewed By: morehouse

Differential Revision: https://reviews.llvm.org/D107197
2021-07-30 15:21:04 -07:00
Shoaib Meenai b8f04a670f [builtins] Try to ensure single copy of emulated TLS state
Multiple copies of emulated TLS state means inconsistent results when
accessing the same thread-local variable from different shared objects
(https://github.com/android/ndk/issues/1551). Making `__emutls_get_address`
be a weak default visibility symbol should make the dynamic linker
ensure only a single copy gets used at runtime. This is best-effort, but
the more robust approach of putting emulated TLS into its own shared
object would (a) be a much bigger change, and (b) shared objects are
pretty heavyweight, and adding a new one to a space-constrained
environment isn't an easy sell. Given the expected rarity of direct
accesses to emulated TLS variables across different shared objects, the
best-effort approach should suffice.

Reviewed By: danalbert, rprichard

Differential Revision: https://reviews.llvm.org/D107127
2021-07-30 14:52:38 -07:00
Petr Hosek 6ea2f31f3d Revert "[profile] Fix profile merging with binary IDs"
This reverts commit 89d6eb6f8c, this
seemed to have break a few builders.
2021-07-30 14:32:52 -07:00