Commit Graph

514 Commits

Author SHA1 Message Date
Leonard Grey b15cf06fb3 [lsan][nfc] Add missing newline to VReport 2022-07-15 15:44:23 -04:00
Vitaly Buka 7788b0c097 [lsan] malloc_usable_size returns 0 for nullptr 2022-07-02 20:16:30 -07:00
Leonard Chan 4b15e665f8 [compiler-rt][lsan] Choose lsan allocator via SANITIZER_CAN_USE_ALLOCATOR64
Rather than checking a bunch of individual platforms.

Differential Revision: https://reviews.llvm.org/D126825
2022-06-07 17:23:38 -07: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 12e137ab24 [NFC][sanitizer] Consolidate malloc hook invocations 2022-04-12 20:08:29 -07:00
Vitaly Buka ce1cb9d2c1 [NFC][sanitizer] Clang format some code 2022-04-12 18:45:50 -07:00
Roland McGrath fd7ec90abd [lsan] On Fuchsia, don't use atexit hook for leak checks
This is a re-land of https://reviews.llvm.org/D86171 with fix.

Fuchsia's system libraries are instrumented and use the lsan
allocator for internal purposes.  So leak checking needs to run
after all atexit hooks and after the system libraries' internal
exit-time hooks.  The <zircon/sanitizer.h> hook API calls the
__sanitizer_process_exit_hook function at exactly the right time.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D88248
2022-03-29 17:13:55 -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
Fangrui Song a53ea51dec [lsan] ppc64: dynamically pick address range for allocator
For ppc64 PIE, it seems that [0xa00000000000,0xc00000000000) may be occupied
which will lead to a segfault in certain kernel configurations
(clang-ppc64le-rhel). Use the `!kUsingConstantSpaceBeg` code path like Fuchsia.

Reviewed By: jsji

Differential Revision: https://reviews.llvm.org/D121257
2022-03-11 20:16:17 -08:00
Dmitry Vyukov 595d340dce sanitizer_common: make internal/external headers compatible
This is a follow up to 4f3f4d6722
("sanitizer_common: fix __sanitizer_get_module_and_offset_for_pc signature mismatch")
which fixes a similar problem for msan build.

I am getting the following error compiling a unit test for code that
uses sanitizer_common headers and googletest transitively includes
sanitizer interface headers:

In file included from third_party/gwp_sanitizers/singlestep_test.cpp:3:
In file included from sanitizer_common/sanitizer_common.h:19:
sanitizer_interface_internal.h:41:5: error: typedef redefinition with different types
('struct __sanitizer_sandbox_arguments' vs 'struct __sanitizer_sandbox_arguments')
  } __sanitizer_sandbox_arguments;
common_interface_defs.h:39:3: note: previous definition is here
} __sanitizer_sandbox_arguments;

Reviewed By: melver

Differential Revision: https://reviews.llvm.org/D119546
2022-02-11 19:39:44 +01:00
Julian Lettner 6f480655e6 [ASan][Darwin] Avoid crash during ASan initialization
Always pass `depth=1` to `vm_region_recurse_64()`.  `depth` is a in-out
parameter and gets reset to 0 after the first call, so we incorrectly
pass `depth=0` on subsequent calls.

We want to avoid the following crash:
```
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x0000000180000000
Exception Codes: 0x0000000000000001, 0x0000000180000000
VM Region Info: 0x180000000 is not in any region. Bytes after previous region: 277577729 Bytes before following region: 384270336
   REGION TYPE         START - END   [ VSIZE] PRT/MAX SHRMOD REGION DETAIL
   Stack          16f64c000-16f748000 [ 1008K] rw-/rwx SM=PRV thread 0
---> GAP OF 0x27730000 BYTES
   unused shlib __TEXT   196e78000-196eac000 [ 208K] r-x/r-x SM=COW ... this process
Termination Reason: SIGNAL 11 Segmentation fault: 11
Terminating Process: exc handler [767]
```

Crashing code:
```
static mach_header *get_dyld_image_header() {
 unsigned depth = 1;
 vm_size_t size = 0;
 vm_address_t address = 0;
 kern_return_t err = KERN_SUCCESS;
 mach_msg_type_number_t count = VM_REGION_SUBMAP_INFO_COUNT_64;

 while (true) {
  struct vm_region_submap_info_64 info;
  err = vm_region_recurse_64(mach_task_self(), &address, &size, &depth,
                (vm_region_info_t)&info, &count);
  if (err != KERN_SUCCESS) return nullptr;

  if (size >= sizeof(mach_header) && info.protection & kProtectionRead) {
   mach_header *hdr = (mach_header *)address;
   if ((hdr->magic == MH_MAGIC || hdr->magic == MH_MAGIC_64) &&   // << CRASH: sanitizer_procmaps_mac.cpp:176
     hdr->filetype == MH_DYLINKER) {
    return hdr;
   }
  }
  address += size;
 }
}
```

Radar-Id: rdar://problem/86773501

Differential Revision: https://reviews.llvm.org/D116240
2022-01-07 12:28:38 -08:00
Dmitry Vyukov 765921de5b sanitizer_common: prefix thread-safety macros with SANITIZER_
Currently we use very common names for macros like ACQUIRE/RELEASE,
which cause conflicts with system headers.
Prefix all macros with SANITIZER_ to avoid conflicts.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D116652
2022-01-07 15:11:00 +01:00
Clemens Wasser 66d1fdb29b [lsan] Format CAN_SANITIZE_LEAKS defines
Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D115584
2021-12-16 11:55:16 -08: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
Clemens Wasser d9aa5f6158 Format lsan.h
Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D115262
2021-12-07 10:52:35 -08:00
Clemens Wasser 33e3554ea3 Change __lsan::kMaxAllowedMallocSize literal types to unsigned long long
It is required for the [Leak Sanitizer port to Windows](https://reviews.llvm.org/D115103).
The currently used `unsigned long` type is 64 bits wide on UNIX like systems but only 32 bits wide on Windows.
Because of that, the literal `8UL << 30` causes an integer overflow on Windows.
By changing the type of the literals to `unsigned long long`, we have consistent behavior and no overflows on all Platforms.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D115186
2021-12-07 10:38:13 -08:00
Vitaly Buka 061f22dda9 [NFC][lsan] Refactor LockThreadRegistry/LockAllocator calls 2021-12-06 22:27:11 -08:00
Vitaly Buka 6318001209 [sanitizer] Support IsRssLimitExceeded in all sanitizers
Reviewed By: kstoimenov

Differential Revision: https://reviews.llvm.org/D115000
2021-12-03 12:45:44 -08:00
Vitaly Buka 323bfad72d [sanitizer] DEFINE_REAL_PTHREAD_FUNCTIONS for hwasan, lsan, msan
It should be NFC, as they already intercept pthread_create.

This will let us to fix BackgroundThread for these sanitizerts.
In in followup patches I will fix MaybeStartBackgroudThread for them
and corresponding tests.

Reviewed By: kstoimenov

Differential Revision: https://reviews.llvm.org/D114935
2021-12-02 10:24:04 -08:00
Dmitry Vyukov 033b94ab52 lsan: remove pthread_detach/join interceptors
They don't seem to do anything useful in lsan.
They are needed only if a tools needs to execute
some custom logic during detach/join, or if it uses
thread registry quarantine. Lsan does none of this.
And if a tool cares then it would also need to intercept
pthread_tryjoin_np and pthread_timedjoin_np, otherwise
it will mess thread states.
Fwiw, asan does not intercept these functions either.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D113920
2021-11-17 14:49:15 +01:00
Vitaly Buka cb0e14ce6d [sanitizer] Switch dlsym hack to internal_allocator
Since glibc 2.34, dlsym does
  1. malloc 1
  2. malloc 2
  3. free pointer from malloc 1
  4. free pointer from malloc 2
These sequence was not handled by trivial dlsym hack.

This fixes https://bugs.llvm.org/show_bug.cgi?id=52278

Reviewed By: eugenis, morehouse

Differential Revision: https://reviews.llvm.org/D112588
2021-11-12 16:11:10 -08:00
Vitaly Buka 64d4420824 [NFC][lsan] Simplify root_regions initialization 2021-11-11 23:42:46 -08:00
Emily Shi 919ca9fc04 [compiler-rt] separate lsan, ubsan, stats configuration from SANITIZER_COMMON_SUPPORTED_OS
Currently, SANITIZER_COMMON_SUPPORTED_OS is being used to enable many libraries.
Unfortunately this makes it impossible to selectively disable a library based on the OS.
This patch removes this limitation by adding a separate list of supported OSs for the lsan, ubsan, ubsan_minimal, and stats libraries.

Reviewed By: delcypher

Differential Revision: https://reviews.llvm.org/D113444
2021-11-10 19:16:20 -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
Brian Cain 5ac9d41067 [sanitizer] Add hexagon support to lsan
Adds build support for hexagon linux to lsan.
2021-08-17 19:59:24 -07:00
Dmitry Vyukov 123c58ea26 sanitizer_common: enable format string checking
Enable -Wformat in sanitizer_common now that it's
cleaned up from existing warnings.
But disable it in all sanitizers for now since
they are not cleaned up yet, but inherit sanitizer_common CFLAGS.

Depends on D107980.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D107981
2021-08-13 13:44:52 +02: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
Dmitry Vyukov 440e936c47 Revert "sanitizers: increase .clang-format columns to 100"
This reverts commit 5d1df6d220.

There is a strong objection to this change:
https://reviews.llvm.org/D106436#2905618

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D106847
2021-07-28 09:40:21 +02:00
Dmitry Vyukov 5d1df6d220 sanitizers: increase .clang-format columns to 100
The current (default) line length is 80 columns.
That's based on old hardware and historical conventions.
There are no existent reasons to keep line length that small,
especially provided that our coding style uses quite lengthy
identifiers. The Linux kernel recently switched to 100,
let's start with 100 as well.

This change intentionally does not re-format code.
Re-formatting is intended to happen incrementally,
or on dir-by-dir basis separately.

Reviewed By: vitalybuka, melver, MaskRay

Differential Revision: https://reviews.llvm.org/D106436
2021-07-22 11:15:02 +02:00
Dmitry Vyukov dfd9808b6c sanitizer_common: add simpler ThreadRegistry ctor
Currently ThreadRegistry is overcomplicated because of tsan,
it needs tid quarantine and reuse counters. Other sanitizers
don't need that. It also seems that no other sanitizer now
needs max number of threads. Asan used to need 2^24 limit,
but it does not seem to be needed now. Other sanitizers blindly
copy-pasted that without reasons. Lsan also uses quarantine,
but I don't see why that may be potentially needed.

Add a ThreadRegistry ctor that does not require any sizes
and use it in all sanitizers except for tsan.
In preparation for new tsan runtime, which won't need
any of these parameters as well.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D105713
2021-07-13 22:52:25 -07:00
Vitaly Buka b4a6fa12d1 Revert "sanitizer_common: add simpler ThreadRegistry ctor"
Breaks https://lab.llvm.org/buildbot/#/builders/sanitizer-x86_64-linux-android

This reverts commit 6062c672bc.
This reverts commit 8e489b4b96.
2021-07-12 12:04:12 -07:00
Dmitry Vyukov 0da172b176 sanitizer_common: add thread safety annotations
Enable clang Thread Safety Analysis for sanitizers:
https://clang.llvm.org/docs/ThreadSafetyAnalysis.html

Thread Safety Analysis can detect inconsistent locking,
deadlocks and data races. Without GUARDED_BY annotations
it has limited value. But this does all the heavy lifting
to enable analysis and allows to add GUARDED_BY incrementally.

Reviewed By: melver

Differential Revision: https://reviews.llvm.org/D105716
2021-07-12 11:46:49 +02:00
Dmitry Vyukov 8e489b4b96 sanitizer_common: add simpler ThreadRegistry ctor
Currently ThreadRegistry is overcomplicated because of tsan,
it needs tid quarantine and reuse counters. Other sanitizers
don't need that. It also seems that no other sanitizer now
needs max number of threads. Asan used to need 2^24 limit,
but it does not seem to be needed now. Other sanitizers blindly
copy-pasted that without reasons. Lsan also uses quarantine,
but I don't see why that may be potentially needed.

Add a ThreadRegistry ctor that does not require any sizes
and use it in all sanitizers except for tsan.
In preparation for new tsan runtime, which won't need
any of these parameters as well.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D105713
2021-07-11 12:36:28 +02:00
Fangrui Song 35fb4b9a7c [lsan] Bump the thread limit from 1<<13 to 1<<22
This matches asan. Some applications need more than 1<<13 threads.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D103731
2021-06-04 20:15:56 -07:00
Fangrui Song 261d6e05d5 [sanitizer] Simplify __sanitizer::BufferedStackTrace::UnwindImpl implementations
Intended to be NFC. D102046 relies on the refactoring for stack boundaries.
2021-05-13 21:26:31 -07:00
Fangrui Song 2fec8860d8 [sanitizer] Set IndentPPDirectives: AfterHash in .clang-format
Code patterns like this are common, `#` at the line beginning
(https://google.github.io/styleguide/cppguide.html#Preprocessor_Directives),
one space indentation for if/elif/else directives.
```
#if SANITIZER_LINUX
# if defined(__aarch64__)
# endif
#endif
```

However, currently clang-format wants to reformat the code to
```
#if SANITIZER_LINUX
#if defined(__aarch64__)
#endif
#endif
```

This significantly harms readability in my review.  Use `IndentPPDirectives:
AfterHash` to defeat the diagnostic. clang-format will now suggest:

```
#if SANITIZER_LINUX
#  if defined(__aarch64__)
#  endif
#endif
```

Unfortunately there is no clang-format option using indent with 1 for
just preprocessor directives. However, this is still one step forward
from the current behavior.

Reviewed By: #sanitizers, vitalybuka

Differential Revision: https://reviews.llvm.org/D100238
2021-05-03 13:49:41 -07:00
Dmitry Vyukov 92a3a2dc3e sanitizer_common: introduce kInvalidTid/kMainTid
Currently we have a bit of a mess related to tids:
 - sanitizers re-declare kInvalidTid multiple times
 - some call it kUnknownTid
 - implicit assumptions that main tid is 0
 - asan/memprof claim their tids need to fit into 24 bits,
   but this does not seem to be true anymore
 - inconsistent use of u32/int to store tids

Introduce kInvalidTid/kMainTid in sanitizer_common
and use them consistently.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D101428
2021-04-30 15:58:05 +02:00
Nico Weber 0e92cbd6a6 Revert "[sanitizer] Simplify GetTls with dl_iterate_phdr on Linux"
This reverts commit ec575e3b0a.
Still doesn't work, see https://crbug.com/1196037
2021-04-05 19:00:18 -04:00
Fangrui Song ec575e3b0a [sanitizer] Simplify GetTls with dl_iterate_phdr on Linux
This was reverted by f176803ef1 due to
Ubuntu 16.04 x86-64 glibc 2.23 problems.
This commit additionally calls `__tls_get_addr({modid,0})` to work around the
dlpi_tls_data==NULL issues for glibc<2.25
(https://sourceware.org/bugzilla/show_bug.cgi?id=19826)

GetTls is the range of

* thread control block and optional TLS_PRE_TCB_SIZE
* static TLS blocks plus static TLS surplus

On glibc, lsan requires the range to include
`pthread::{specific_1stblock,specific}` so that allocations only referenced by
`pthread_setspecific` can be scanned.

This patch uses `dl_iterate_phdr` to collect TLS blocks. Find the one
with `dlpi_tls_modid==1` as one of the initially loaded module, then find
consecutive ranges. The boundaries give us addr and size.

This allows us to drop the glibc internal `_dl_get_tls_static_info` and
`InitTlsSize` entirely. Use the simplified method with non-Android Linux for
now, but in theory this can be used with *BSD and potentially other ELF OSes.

This simplification enables D99566 for TLS Variant I architectures.

See https://reviews.llvm.org/D93972#2480556 for analysis on GetTls usage
across various sanitizers.

Differential Revision: https://reviews.llvm.org/D98926
2021-04-04 15:35:53 -07:00
Nico Weber f176803ef1 Revert "[sanitizer] Simplify GetTls with dl_iterate_phdr"
This reverts commit 9be8f8b34d.
This breaks tsan on Ubuntu 16.04:

    $ cat tiny_race.c
    #include <pthread.h>
    int Global;
    void *Thread1(void *x) {
      Global = 42;
      return x;
    }
    int main() {
      pthread_t t;
      pthread_create(&t, NULL, Thread1, NULL);
      Global = 43;
      pthread_join(t, NULL);
      return Global;
    }
    $ out/gn/bin/clang -fsanitize=thread -g -O1 tiny_race.c --sysroot ~/src/chrome/src/build/linux/debian_sid_amd64-sysroot/
    $ docker run -v $PWD:/foo ubuntu:xenial /foo/a.out
    FATAL: ThreadSanitizer CHECK failed: ../../compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp:447 "((thr_beg)) >= ((tls_addr))" (0x7fddd76beb80, 0xfffffffffffff980)
        #0 <null> <null> (a.out+0x4960b6)
        #1 <null> <null> (a.out+0x4b677f)
        #2 <null> <null> (a.out+0x49cf94)
        #3 <null> <null> (a.out+0x499bd2)
        #4 <null> <null> (a.out+0x42aaf1)
        #5 <null> <null> (libpthread.so.0+0x76b9)
        #6 <null> <null> (libc.so.6+0x1074dc)

(Get the sysroot from here: https://commondatastorage.googleapis.com/chrome-linux-sysroot/toolchain/500976182686961e34974ea7bdc0a21fca32be06/debian_sid_amd64_sysroot.tar.xz)

Also reverts follow-on commits:
This reverts commit 58c62fd976.
This reverts commit 31e541e375.
2021-04-02 18:19:17 -04:00