64 Commits

Author SHA1 Message Date
Leonard Grey
ac604cc310 [lsan][Darwin] Unconditionally strip high bits from potential pointers
The method cache stashes a mask in the high bits under some circumstances:
689525d556/runtime/objc-cache.mm (L589)

I'm hitting this now on macOS 13.4 arm64, so we can no longer rely on OBJC_FAST_IS_RW to identify potential pointers that need to be transformed

Differential Revision: https://reviews.llvm.org/D153471
2023-07-17 15:18:53 -04:00
Vitaly Buka
99dc68387b [NFC][lsan] Use hash table to track root regions
This avoid O(N) in __lsan_unregister_root_region.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D151784
2023-06-02 14:38:01 -07:00
Vitaly Buka
0375a2dc7d [NFC][sanitizer] Avoid O(N^2) algorithm
Usually root_regions size is small so unlikey
this change will provide a noticable difference.

However it's easy to make sure that even with
large number of root_regions it works reasonably
fast.

Differential Revision: https://reviews.llvm.org/D151781
2023-06-02 14:32:03 -07:00
Vitaly Buka
506923a12f [lsan] Don't use InternalMmapVectorNoCtor as local var 2023-05-28 23:33:47 -07:00
Vitaly Buka
19b7b93da9 [lsan] Iterate MemoryMappingLayout once
Iterating many time can be very expensive.
2023-05-28 23:09:59 -07:00
Vitaly Buka
0a6aec2fb3 [NFC][lsan] Change Mac root regions scan 2023-05-28 22:28:37 -07:00
Vitaly Buka
8ce66a1f79 [NFC][lsan] Add CHECKs for root regions 2023-05-28 22:10:00 -07:00
Vitaly Buka
e4b02c61d1 [NFC][lsan] Make mutex static 2023-05-28 21:52:20 -07:00
Vitaly Buka
1068cf787e [NFC][lsan] Rename RootRegion and replace size with end 2023-05-28 21:21:26 -07:00
Vitaly Buka
39c0602414 [lsan] Rename IgnoreObjectLocked into IgnoreObject
We don't lock allocator for this call.
2023-04-05 16:17:44 -07:00
Bojun Seo
cbb9369b9e [lsan] Initialize uptr value with number not boolean
Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D145354
2023-03-07 21:47:39 -08:00
Kirill Stoimenov
b4b5006be5 [LSAN] Mask out tags from pointers on ARM in MaybeUserPointer heuristic
This caused false positives because the existing logic was not taking into account that pointers could have a tag in them.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D144305
2023-02-18 03:37:56 +00:00
Kirill Stoimenov
eb3be66028 [LSAN] Add GetUserAddr function which returns the user visible address of an internal pointer
For HWASAN this would be the tagged address. It is the same pointer when pointer tagging is not used. Coincidently this also fixes some test which rely on comparing pointers.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D143121
2023-02-02 00:39:00 +00:00
Kirill Stoimenov
af210ee506 [LSAN][Fuchsia] Added ForEachExtraThreadStackRange to support Fuchsia code.
Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D141724
2023-01-13 21:20:41 -08:00
Youling Tang
3a58e11961 [lsan] Add lsan support for loongarch64
This patch enabled lsan for loongarch64 with 47-bit VMA layout.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D139686
2023-01-14 09:23:51 +08:00
Kirill Stoimenov
c184423e70 [LSAN] More LSAN interface tweaking.
Main goal is to remove thread registry dependency from the interface because HWASAN is using its own code to manage threads.

Reviewed By: vitalybuka, kstoimenov

Differential Revision: https://reviews.llvm.org/D140039
2023-01-12 17:58:11 -08:00
Vitaly Buka
eb2db81eba Revert "[LSAN] More LSAN interface tweaking."
Breaks bots.
Also it's missing changes we discussed on review.

This reverts commit f001e50f955c3cdf2deb79e38a9fd19c9a781882.
This reverts commit 2924189233fdb724453ead4b94595107b1ce9cfa.
2023-01-12 17:32:40 -08:00
Kirill Stoimenov
2924189233 [LSAN] More LSAN interface tweaking.
Main goal is to remove thread registry dependency from the interface because HWASAN is using its own code to manage threads.

Reviewed By: vitalybuka, kstoimenov

Differential Revision: https://reviews.llvm.org/D140039
2023-01-12 23:44:08 +00:00
Kirill Stoimenov
e43e0ffbe7 [LSAN][NFC] Eliminated GetThreadRegistryLocked from the LSAN interface to avoid the need to implement it in HWASAN.
Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D139976
2022-12-14 00:15:40 +00:00
Vitaly Buka
ca3c58f0d1 [lsan] Add logs for debugging 2022-11-28 22:59:38 -08:00
Wiktor Garbacz
e851f7dbca Fix LSan build
This change fixes a build bug introduced in
39db491957dcf095936d81bed89c2b4edae2a1e7

Differential Revision: https://reviews.llvm.org/D135860
2022-10-13 10:44:27 -07:00
Wiktor Garbacz
39db491957 [LeakSanitizer] Capture calling thread SP early to avoid false negatives.
As shown in https://github.com/llvm/llvm-project/issues/42932 dead
pointers might be overlapped by a new stack frame inside CheckForLeaks,
which does not use bytes with pointers. This leads to false negatives.

It's not a full solution for the problem as it does not solve
"overlapping" new/old frames for frames below the CheckForLeaks and in
other threads. It should improve leaks found in direct callers of
__lsan_do_leak_check.

Differential Revision: https://reviews.llvm.org/D130237
2022-10-12 16:46:32 -07:00
Leonard Grey
f458d9f6f8 [lsan][darwin] Unmask camouflaged class_rw_t pointers
Detailed motivation here: https://docs.google.com/document/d/1xUNo5ovPKJMYxitiHUQVRxGI3iUmspI51Jm4w8puMwo

check-asan (with LSAN enabled) and check-lsan are currently broken on recent macOS versions, due to pervasive false positives. Whenever the Objective-C runtime realizes a class, it allocates data for it, then stores that data with flags in the low bits. This means LSAN can not recognize it as a pointer while scanning.

This change checks every potential pointer on Apple platforms, and if the high bit is set, attempts to extract a pointer by masking out the high bit and flags. This is ugly, but it's also the best approach I could think of (see doc above); very open to other suggestions.

Differential Revision: https://reviews.llvm.org/D133126
2022-09-02 11:25:22 -04:00
Leonard Grey
b15cf06fb3 [lsan][nfc] Add missing newline to VReport 2022-07-15 15:44:23 -04:00
Andrew Ng
e06a81d810 [LSAN] Fix up LSAN weak symbols for Windows
Differential Revision: https://reviews.llvm.org/D126703
2022-06-01 10:18:51 +01:00
Mariusz Borsa
8246b2e156 [Sanitizers][Darwin] Replace SANITIZER_MAC with SANITIZER_APPLE in source files
This is a follow up to [Sanitizers][Darwin] Rename Apple macro SANITIZER_MAC -> SANITIZER_APPLE (D125816)

Performed a global search/replace as in title against LLVM sources

Differential Revision: https://reviews.llvm.org/D126263
2022-05-24 12:59:27 -07:00
Leonard Chan
c8644ea88e [compiler-rt][lsan] Update CanBeAHeapPointer for AArch64
While attempting to get the 64-bit lsan allocator working for Fuchsia, I
noticed this function would incorrectly return false for pointers returned
by the 64-bit allocator. On AArch64, this function attempts to get the VMA
size dynamically by counting the number of leading zeros from the function
frame address. This will fail if the frame address is significantly below an
allocated pointer (that is, the frame address has more leading zeros than an
allocated pointer). This is possible on Fuchsia and linux (when not called
from the initial thread stack).

It seems the intended use of this function is to speed up pointer scanning by
filtering out addresses that user code might not be able to access. Other
platforms this check is done on seem to hardcode the VMA size/shift, so it
seems appropriate to do this for aarch64 as well. This implies pointers on
aarch64 where the VMA size is <64 will pass through, but bad pointers will
still be caught by subsequent scan checks.

This patch also renames the function to something more fitting of what it's
trying to do.

Differential Revision: https://reviews.llvm.org/D123814
2022-05-24 10:22:46 -07:00
Vitaly Buka
a63932a815 [lsan] Allow suppression of "unknown module"
If sanitizer cannot determine name of the module it
will use "<unknown module>". Then it can be suppressed
if needed.

Reviewed By: kda

Differential Revision: https://reviews.llvm.org/D121674
2022-03-15 13:35:21 -07:00
Vitaly Buka
f86deb18ca [lsan] Move out suppression of invalid PCs from StopTheWorld
This removes the last use of StackDepot from StopTheWorld.

Depends on D115284.

Reviewed By: morehouse

Differential Revision: https://reviews.llvm.org/D115319
2021-12-09 12:17:35 -08:00
Vitaly Buka
f72e50946c [lsan] Reduce StopTheWorld access to StackDepot
StackDepot locks some stuff. As is there is small probability to
deadlock if we stop thread which locked the Depot.

We need either Lock/Unlock StackDepot for StopTheWorld, or don't
interact with StackDepot from there.

This patch does not run LeakReport under StopTheWorld. LeakReport
contains most of StackDepot access.

As a bonus this patch will help to resolve kMaxLeaksConsidered FIXME.

Depends on D114498.

Reviewed By: morehouse, kstoimenov

Differential Revision: https://reviews.llvm.org/D115284
2021-12-09 11:43:30 -08:00
Vitaly Buka
a9a1499080 [NFC][lsan] Change LeakSuppressionContext interface
Reviewed By: morehouse

Differential Revision: https://reviews.llvm.org/D115318
2021-12-09 11:38:02 -08:00
Vitaly Buka
b1c369e01f [lsan] Replace atomic_store/atomic_load with atomic_fetch_add 2021-12-07 22:39:12 -08:00
Vitaly Buka
dd1abb1102 [NFC][lsan] Move method implementation up 2021-12-07 21:45:26 -08:00
Vitaly Buka
b79ea567cf [NFC][lsan] Clang-format lsan_common.cpp 2021-12-07 21:45:26 -08:00
Vitaly Buka
8ed4d2819d [NFC][lsan] Use kIgnored in MarkInvalidPCCb
kIgnored didn't exist when the code was added, but it should be
equivalent to kReachable.
The goal is to refactor MarkInvalidPCCb to avoid StackDepotGet
in StopTheWorld.
2021-12-07 19:53:08 -08:00
Vitaly Buka
64d4420824 [NFC][lsan] Simplify root_regions initialization 2021-11-11 23:42:46 -08:00
Martin Liska
629b40dafa Fix -Wformat warnings reported by GCC.
Differential Revision: https://reviews.llvm.org/D113099
2021-11-04 08:51:43 +01:00
Vitaly Buka
ca0036df7d [sanitizer] Remove StackDepotReverseMap
Now StackDepotGet can retrive the stack in O(1).

Depends on D111612.

Reviewed By: dvyukov

Differential Revision: https://reviews.llvm.org/D111613
2021-10-12 15:59:27 -07:00
Vitaly Buka
051d766bae [NFC][lsan] Expand use StackDepotReverseMap
Before StackDepotReverseMap was used only by ProcessPC.
2021-09-30 19:26:47 -07:00
Dmitry Vyukov
56debbf52e sanitizers: switch BlockingMutex(LINKER_INITIALIZED) to Mutex
Mutex does not support LINKER_INITIALIZED support.
As preparation to switching BlockingMutex to Mutex,
proactively replace all BlockingMutex(LINKER_INITIALIZED) to Mutex.
All of these are objects with static storage duration and Mutex ctor
is constexpr, so it should be equivalent.

Reviewed By: melver

Differential Revision: https://reviews.llvm.org/D106944
2021-07-28 15:09:34 +02:00
Vitaly Buka
e0dadf3de2 [sanitizer] Remove max_len parameter from InternalScopedString
InternalScopedString uses InternalMmapVector internally
so it can be resized dynamically as needed.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D98751
2021-03-17 16:57:09 -07:00
Dan Liew
dd922bc2a6 [LSan] Introduce a callback mechanism to allow adding data reachable from ThreadContexts to the frontier.
This mechanism is intended to provide a way to treat the `arg` pointer
of a created (but not yet started) thread as reachable. In future
patches this will be implemented in `GetAdditionalThreadContextPtrs`.

A separate implementation of `GetAdditionalThreadContextPtrs` exists
for ASan and LSan runtimes because they need to be implemented
differently in future patches.

rdar://problem/63537240

Differential Revision: https://reviews.llvm.org/D95183
2021-01-22 19:26:02 -08:00
Vitaly Buka
9a0237011b [lsan] Ignore inderect leaks referenced by suppressed blocks
This makes suppression list to work similar to __lsan_ignore_object.

Existing behavior was inconsistent and very inconvenient for complex
data structures.

Example:

struct B;
struct A { B* ptr; };
A* t = makeA();
t->ptr = makeB();

Before the patch: if makeA suppressed by suppression file, lsan will
still report the makeB() leak, so we need two suppressions.

After the patch: a single makeA suppression is enough (the same as a
single __lsan_ignore_object(t)).

Differential Revision: https://reviews.llvm.org/D93884
2020-12-30 19:11:39 -08:00
Vitaly Buka
9b25b8068d [NFC][lsan] Extract PrintResults function 2020-12-29 17:28:31 -08:00
Vitaly Buka
9a5261efd7 [lsan] Parse suppressions just before leak reporting
Without leaks suppressions are not needed.
2020-12-29 17:28:31 -08:00
Vitaly Buka
4e74480e02 [NFC][sanitizer] Simplify InternalLowerBound 2020-12-29 14:01:43 -08:00
Vitaly Buka
adfefa5553 [NFC] Extract ForEachDVT 2020-12-01 16:15:32 -08:00
Vitaly Buka
6c164d6080 [NFC] Extract InitializePlatformCommonFlags
And move some Android specifi declarations from headers

Differential Revision: https://reviews.llvm.org/D90811
2020-11-04 19:23:28 -08:00
Vy Nguyen
484ec6be30 Reland [lsan] Enable LSAN for Android
Reland: a2291a58bf1c860d026581fee6fe96019dc25440.

New fixes for the breakages reported in D85927 include:
 - declare a weak decl for `dl_iterate_phdr`, because it does not exist on older  APIs
 - Do not enable leak-sanitizer if api_level is less than 29, because of  `ld.lld: error: undefined symbol: __aeabi_read_tp` for armv7, API level 16.
 - Put back the interceptor for `memalign` but still opt out intercepting `__libc_memalign` and `cfree` because both of these don't exist in Bionic.

Reviewed By: srhines, vitalybuka

Differential Revision: https://reviews.llvm.org/D89251
2020-11-04 18:00:25 -08:00
Vitaly Buka
a52852f694 [NFC][sanitizer] Reformat some code
Extracted from D89251
2020-11-04 15:37:48 -08:00