90 Commits

Author SHA1 Message Date
Hans Wennborg
ee5367bedb Revert "[compiler-rt]: fix CodeQL format-string warnings via explicit casts (#153843)"
It broke the build:

compiler-rt/lib/hwasan/hwasan_thread.cpp:177:11: error: unknown type name 'ssize_t'; did you mean 'size_t'?
   177 |          (ssize_t)unique_id_, (void *)this, (void *)stack_bottom(),
       |           ^~~~~~~
       |           size_t

> This change addresses CodeQL format-string warnings across multiple
> sanitizer libraries by adding explicit casts to ensure that printf-style
> format specifiers match the actual argument types.
>
> Key updates:
> - Cast pointer arguments to (void*) when used with %p.
> - Use appropriate integer types and specifiers (e.g., size_t -> %zu,
> ssize_t -> %zd) to avoid mismatches.
> - Fix format specifier mismatches across xray, memprof, lsan, hwasan,
> dfsan.
>
> These changes are no-ops at runtime but improve type safety, silence
> static analysis warnings, and reduce the risk of UB in variadic calls.

This reverts commit d3d5751a39452327690b4e011a23de8327f02e86.
2025-08-22 12:50:53 +02:00
Amit Kumar Pandey
d3d5751a39
[compiler-rt]: fix CodeQL format-string warnings via explicit casts (#153843)
This change addresses CodeQL format-string warnings across multiple
sanitizer libraries by adding explicit casts to ensure that printf-style
format specifiers match the actual argument types.

Key updates:
- Cast pointer arguments to (void*) when used with %p.
- Use appropriate integer types and specifiers (e.g., size_t -> %zu,
ssize_t -> %zd) to avoid mismatches.
- Fix format specifier mismatches across xray, memprof, lsan, hwasan,
dfsan.

These changes are no-ops at runtime but improve type safety, silence
static analysis warnings, and reduce the risk of UB in variadic calls.
2025-08-22 11:51:13 +05:30
Jake Egan
8e072b9d49
[sanitizer_common][nfc] Rename tid_t to avoid conflicting declarations (#149011)
`tid_t` is also defined in the AIX header `/usr/include/sys/types.h`
which is included by system `pthread.h`. The use of `tid_t` by AIX is
conforming according to
[POSIX](https://pubs.opengroup.org/onlinepubs/9799919799/functions/V2_chap02.html):
> Implementations may add symbols to the headers shown in the following
table [ ... ]
2025-07-23 09:39:19 -04:00
Florian Mayer
0eccf1385c
[LSan] skip leaks from dlerror (#142876)
We have known false positives, and the return value is never
user-managed anyway.
2025-06-04 20:04:16 -07:00
Fangrui Song
ac38ab594f
[lsan] Use VReport if not REGISTERS_AVAILABLE (#116555)
Certain tests (many are from lld/test) run `... '2>&1 | count 0` to
ensure that there is no stderr message.

GetRegistersAndSP may rarely fail, leading to
a spurious failure like (with a local hack to make `count` dump the
input):

```
+ /home/ray/llvm/out/asan/bin/ld.lld func1-gcs.o func2-gcs.o func3-gcs.o -o /dev/null -z gcs-report=warning -z gcs=never
+ /home/ray/llvm/out/asan/bin/count 0
Expected 0 lines, got 1.
==2403039==Unable to get registers from thread 2403018.
```

The failure can reliably be reproduced by running `ninja check-lld` a
few times under asan+lsan (see the bot
sanitizer-x86_64-linux-bootstrap-asan).
2024-11-20 01:07:28 -08:00
Vitaly Buka
b5fa4fee46
[lsan] Fix compilation on Android (#113003) 2024-10-18 18:40:29 -07:00
Vitaly Buka
0138adb68f
[nfc][lsan] Rename ScanExtraStack and pass region_type (#113004) 2024-10-18 16:35:59 -07:00
Vitaly Buka
f4c6088346
[lsan] Process non-suspended threads (#112807)
For such threads we have no registers, so no exact
stack range, and no guaranties that stack is mapped
at all.

To avoid crashes on unmapped memory,
 `MemCpyAccessible` copies intersting range into
 temporarily buffer, and we search for pointers there.
2024-10-18 15:23:52 -07:00
Vitaly Buka
d60fdc1ca3
[nfc][lsan] Parametrize ScanForPointers with loader (#112803)
Use `DirectLoader` which is equivalent to existing
behaviour of loading pointers directly from memory.
2024-10-18 11:42:13 -07:00
Vitaly Buka
3142dff704
[nfc][lsan] Extract significant part of the loop into a function (#112610)
Co-authored-by: thetruestblue <92476612+thetruestblue@users.noreply.github.com>
2024-10-16 22:45:40 -07:00
Vitaly Buka
6ffd3bbcd7
[nfc][lsan] Restructure loop in ProcessThreads (#112609)
The goal is to move `SuspendedThreadsList` related code into
the beginning of the loop, and prepare for extraction the rest
of the loop body into a function.
2024-10-16 18:32:14 -07:00
Vitaly Buka
dd9a34fd7e
[nfc][lsan] Move up vectors cleanup (#112608) 2024-10-16 18:30:20 -07:00
Vitaly Buka
1834660b4c
[nfc][lsan] Replace suspended_threads.GetThreadID(i) with local var (#112607) 2024-10-16 16:40:24 -07:00
Vitaly Buka
b2b0e6c01e [NFC][lsan] Fix name of local var 2024-10-15 20:44:51 -07:00
Vitaly Buka
fa81868fe6
[lsan] Log thread history (#111949)
Only with high verbosity and leak reports, or thread logging requested.
2024-10-11 18:09:37 -07:00
Vitaly Buka
e1cff8bf81
[lsan] Add debug option to "deflake" leaks (#112037)
There are hard to debug leaks which look like
false.

In general, repeating leak checking should not
affect set of leaks significantly, especial
`at_exit` leak checking.

But if we see significant discrepancy, it may give
us a clue for investigation.
2024-10-11 16:57:19 -07:00
Vitaly Buka
453d373e80 [lsan] Add a few "\n" missing from VReport 2024-10-10 11:24:36 -07:00
Hans Wennborg
3137b6a263 [lsan] Make ReportUnsuspendedThreads return bool also for Fuchsia
Follow-up to 9766ce4db57212646f135fe7033972a7a51aef4d
2024-10-07 11:19:32 +02:00
Vitaly Buka
9766ce4db5 [lsan] Add thread_suspend_fail flag 2024-10-04 22:53:37 -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
bf8c7cda49 Revert "[NFC][compiler-rt] Try to collect more info about crashes on bot"
Catches nothing, reported #84654.

This reverts commit 6f7ebcb71f4e89309c613da9600991850f15f74f.
2024-03-09 14:33:26 -08:00
Vitaly Buka
6f7ebcb71f [NFC][compiler-rt] Try to collect more info about crashes on bot 2024-03-09 12:51:02 -08:00
Vitaly Buka
23aabdd66f
[NFC][sanitizer] Move SymbolizedStackHolder into sanitizer_common (#77152)
And replace most `ClearAll()` uses.
2024-01-05 18:40:40 -08:00
Vitaly Buka
14d7e0bb0f
[lsan] Install pthread_atfork (#75281)
This prevents deadlocks in forked process on essencial
runtime components.
2023-12-13 13:09:36 -08:00
Vitaly Buka
5b7dfa968f
[NFC][sanitizer] Rename InternalScopedString::append to AppendF (#66558)
Prepare to introduce trivial InternalScopedString::Append(const char*).
2023-09-15 17:06:20 -07:00
Kirill Stoimenov
0365ccd2a1 [HWASAN][LSAN] Fix false positive memory leak reports on X86_64
Before this patch when running HWASAN on x86_64 with with memory tagging support we got a bunch of false memory leak reports. The reason for that is that the heuristic used to detect if an 8 bytes could be a user pointer was not valid when memory tagging is used as the top byte could contain non-zero information.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D155338
2023-07-18 19:04:30 +00:00
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