Commit Graph

318 Commits

Author SHA1 Message Date
Matt Morehouse 95d609b549 [HWASan] Add __hwasan_init to .preinit_array.
Fixes segfaults on x86_64 caused by instrumented code running before
shadow is set up.

Reviewed By: pcc

Differential Revision: https://reviews.llvm.org/D118171
2022-02-03 13:07:58 -08:00
Matt Morehouse f7c28332de [HWASan] Leave pointer tagged when calling memmove.
Fixes a false positive that occurs when a user-implemented memmove is
instrumented by HWASan.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D118180
2022-01-28 06:15:38 -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
Mitch Phillips 572a0721a0 [HWASan] Try 'google' prefixed apex directories in symbolizer.
Google-signed apexes appear on Android build servers' symbol files as
being under /apex/com.google.android.<foo>/. In reality, the apexes are
always installed as /apex/com.android.<foo>/ (note the lack of
'google'). In order for local symbolization under hwasan_symbolize to
work correctly, we also try the 'google' directory.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D114919
2021-12-03 10:35:03 -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
Florian Mayer 26d1edfb10 [hwasan] support python3 in hwasan_sanitize
Verified no diff exist between previous version, new version python 2, and python 3 for an example stack.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D114404
2021-11-23 15:30:30 -08:00
Florian Mayer ed8b5b37ab [hwasan] fix arguments to symbolizer.
new versions do not accept -inlining of -functions (tested with 11 and
13).

Reviewed By: hctim

Differential Revision: https://reviews.llvm.org/D114303
2021-11-22 09:59:06 -08: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 651797f488 [NFC][sanitizer] Move GET_MALLOC_STACK_TRACE closer to the use 2021-11-10 15:42:07 -08:00
Matt Morehouse 846ec2c3cc [HWASan] Ensure RNG is initialized in GenerateRandomTag
Fixes a CHECK-failure caused by glibc's pthread_getattr_np
implementation calling realloc.  Essentially, Thread::GenerateRandomTag
gets called during Thread::Init and before Thread::InitRandomState:

  HWAddressSanitizer: CHECK failed: hwasan_thread.cpp:134 "((random_buffer_)) != (0)" (0x0, 0x0) (tid=314)
    #0 0x55845475a662 in __hwasan::CheckUnwind()
    #1 0x558454778797 in __sanitizer::CheckFailed(char const*, int, char const*, unsigned long long, unsigned long long)
    #2 0x558454766461 in __hwasan::Thread::GenerateRandomTag(unsigned long)
    #3 0x55845475c58b in __hwasan::HwasanAllocate(__sanitizer::StackTrace*, unsigned long, unsigned long, bool)
    #4 0x55845475c80a in __hwasan::hwasan_realloc(void*, unsigned long, __sanitizer::StackTrace*)
    #5 0x5584547608aa in realloc
    #6 0x7f6f3a3d8c2c in pthread_getattr_np
    #7 0x5584547790dc in __sanitizer::GetThreadStackTopAndBottom(bool, unsigned long*, unsigned long*)
    #8 0x558454779651 in __sanitizer::GetThreadStackAndTls(bool, unsigned long*, unsigned long*, unsigned long*, unsigned long*)
    #9 0x558454761bca in __hwasan::Thread::InitStackAndTls(__hwasan::Thread::InitState const*)
    #10 0x558454761e5c in __hwasan::HwasanThreadList::CreateCurrentThread(__hwasan::Thread::InitState const*)
    #11 0x55845476184f in __hwasan_thread_enter
    #12 0x558454760def in HwasanThreadStartFunc(void*)
    #13 0x7f6f3a3d6fa2 in start_thread
    #14 0x7f6f3a15b4ce in __clone

Also reverts 7a3fb71c3c, as it's now
unneeded.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D113045
2021-11-09 07:01:55 -08:00
Fangrui Song 815b9f53d8 [hwasan] Replace _Unwind_Word with uintptr_t
GCC introduced `__attribute__((mode(unwind_word)))` to work around
Cell Broadband Engine SPU (which was removed from GCC in 2019-09),
which is irrelevant to hwasan.
_Unwind_GetGR/_Unwind_GetCFA from llvm-project/libunwind don't use unwind_word.

Using _Unwind_Word can lead to build failures if libunwind's unwind.h is
preferred over unwind.h in the Clang resource directory (e.g. built with GCC).
2021-11-06 22:34:50 -07:00
Martin Liska 13a442ca49 Enable -Wformat-pedantic and fix fallout.
Differential Revision: https://reviews.llvm.org/D113172
2021-11-05 13:12:35 +01:00
Matt Morehouse 498a4c2fd7 [HWASan] Print short tags in tag mismatch description.
I recently spent some extra time debugging a false positive because I
didn't realize the "real" tag was in the short granule.  Adding the
short tag here makes it more obvious that we could be dealing with a
short granule.

Reviewed By: hctim, eugenis

Differential Revision: https://reviews.llvm.org/D112949
2021-11-02 08:00:33 -07:00
Matt Morehouse 8c60e0b632 [HWASan] Print short tags in __hwasan_print_shadow.
Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D112959
2021-11-02 07:59:25 -07:00
Florian Mayer dd943ebc6d [hwasan] print exact mismatch offset for short granules.
Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D104463
2021-10-28 03:31:11 +01:00
Evgenii Stepanov 039096ee71 [hwasan] Add default "/" prefix.
Add a default "/" prefix to the symbol search path in the
symbolization script. Without this, the binary itself is not considered
a valid source of symbol info.

Differential Revision: https://reviews.llvm.org/D111840
2021-10-14 14:56:55 -07:00
H.J. Lu c960c8c339 Reland [sanitizer] Support Intel CET
1. Include <cet.h> in sanitizer_common/sanitizer_asm.h, if it exists, to
mark Intel CET support when Intel CET is enabled.
2. Define _CET_ENDBR as empty if it isn't defined.
3. Add _CET_ENDBR to function entries in assembly codes so that ENDBR
instruction will be generated when Intel CET is enabled.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D111185
2021-10-08 10:22:39 -07:00
Arthur Eubanks d4c1f222f2 Revert "[sanitizer] Support Intel CET"
This reverts commit fdf4c03522.

Breaks macOS bots, e.g. https://crbug.com/1257863.
Still figuring out if this is actually supported on macOS. Other places
that include <cet.h> only do so on Linux.
2021-10-07 21:03:12 -07:00
H.J. Lu fdf4c03522 [sanitizer] Support Intel CET
1. Include <cet.h> in sanitizer_common/sanitizer_asm.h to mark Intel CET
support when Intel CET is enabled.
2. Add _CET_ENDBR to function entries in assembly codes so that ENDBR
instruction will be generated when Intel CET is enabled.

Reviewed By: dvyukov

Differential Revision: https://reviews.llvm.org/D111185
2021-10-06 10:12:50 -07:00
Vitaly Buka 7c1128f3bb [NFC][sanitizer] Return StackDepotStats by value
Differential Revision: https://reviews.llvm.org/D110644
2021-09-28 15:42:21 -07:00
Matt Morehouse 1aedf77ece [HWASan] Use a single .weak binding in asm.
Specifying .global and .weak causes a compiler warning:

  warning: __sigsetjmp changed binding to STB_WEAK

Specifying only .weak should have the same effect without causing a
warning.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D110178
2021-09-22 09:35:09 -07:00
Matt Morehouse 750d5fc65c [HWASan] Intercept setjmp/longjmp on x86_64.
Reviewed By: xiangzhangllvm

Differential Revision: https://reviews.llvm.org/D109790
2021-09-17 07:10:57 -07:00
Florian Mayer 95ba9f957a [hwasan] print globals in symbolizer-friendly format.
Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D109698
2021-09-16 13:29:17 +01:00
Matt Morehouse 0a07789fe9 [HWASan] Add missing newlines. 2021-09-15 09:06:01 -07:00
Matt Morehouse 1a3b3301d7 [HWASan] Catch cases where libc populated jmp_buf.
Some setjmp calls within libc cannot be intercepted while their matching
longjmp calls can be.  This causes problems if our setjmp/longjmp
interceptors don't use the exact same format as libc for populating and
reading the jmp_buf.

We add a magic field to our jmp_buf and populate it in setjmp.  This
allows our longjmp interceptor to notice when a libc jmp_buf is passed
to it.

See discussion on https://reviews.llvm.org/D109699 and
https://reviews.llvm.org/D69045.

Fixes https://github.com/google/sanitizers/issues/1244.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D109787
2021-09-15 07:53:54 -07:00
Florian Mayer 039fd9af45 [NFC] [hwasan] move prints closer together.
this makes the code slightly more readable.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D109442
2021-09-09 13:39:11 +01:00
Florian Mayer 023f18bbaf [hwasan] do not check if freed pointer belonged to allocator.
In that case it is very likely that there will be a tag mismatch anyway.

We handle the case that the pointer belongs to neither of the allocators
by getting a nullptr from allocator.GetBlockBegin.

Reviewed By: hctim, eugenis

Differential Revision: https://reviews.llvm.org/D108383
2021-08-25 09:31:01 +01:00
Mitch Phillips 433b2eaf91 [hwasan] Always untag short granule in shadow.
Fixes a regression when the allocator is disabled, and a dirty
allocation is re-used. This only occurs when the allocator is disabled,
so a test-only fix, but still necessary.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D108650
2021-08-24 14:10:04 -07:00
Florian Mayer de916a7b12 Revert "[hwasan] do not check if freed pointer belonged to allocator."
This reverts commit 119146f8ae.
2021-08-20 12:21:00 +01:00
Florian Mayer 119146f8ae [hwasan] do not check if freed pointer belonged to allocator.
In that case it is very likely that there will be a tag mismatch anyway.

We handle the case that the pointer belongs to neither of the allocators
by getting a nullptr from allocator.GetBlockBegin.

Reviewed By: hctim, eugenis

Differential Revision: https://reviews.llvm.org/D108383
2021-08-20 10:12:47 +01:00
Mitch Phillips fd51ab6341 [hwasan] Don't report short-granule shadow as overwritten.
The shadow for a short granule is stored in the last byte of the
granule. Currently, if there's a tail-overwrite report (a
buffer-overflow-write in uninstrumented code), we report the shadow byte
as a mismatch against the magic.

Fix this bug by slapping the shadow into the expected value. This also
makes sure that if the uninstrumented WRITE does clobber the shadow
byte, it reports the shadow was actually clobbered as well.

Reviewed By: eugenis, fmayer

Differential Revision: https://reviews.llvm.org/D107938
2021-08-18 11:25:57 -07:00
Evgenii Stepanov 8a570a873b [hwasan] Support malloc in atfork.
Before this change we were locking the StackDepot in the fork()
interceptor. This results in a deadlock when allocator functions are
used in a pthread_atfork() callback.

Instead, set up a pthread_atfork() callback at init that locks/unlocks
both StackDepot and the allocator. Since our callback is set up very
early, the pre-fork callback is executed late, and both post-fork ones
are executed early, which works perfect for us.

Differential Revision: https://reviews.llvm.org/D108063
2021-08-17 15:29:49 -07:00
Evgenii Stepanov c9ce76febb (NFC) clang-format hwasan/hwasan_linux.cpp
Differential Revision: https://reviews.llvm.org/D108224
2021-08-17 15:03:46 -07:00
Peter Collingbourne affb132ab8 hwasan: Move stack ring buffer initialization before InitStackAndTls.
D104248 moved the call to GetThreadStackAndTls to before the
initialization of the ring buffer TLS slot. As a result, if libc
is instrumented we crash in pthread_getattr_np which is called from
__sanitizer::GetThreadStackTopAndBottom.

Fix the problem by moving the stack ring buffer initialization before
the call to InitStackAndTls.

Differential Revision: https://reviews.llvm.org/D108184
2021-08-17 10:18:07 -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
Florian Mayer bae9527c20 [hwasan] Add report for wild frees.
Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D107577
2021-08-13 09:04:48 +01:00
Florian Mayer 6794593fdb [hwasan] State correct PC in first error message.
We would put the return address there, rather than the fault address.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D107578
2021-08-10 09:29:29 +01:00
Mitch Phillips 0ebb756087 [hwasan] Add __hwasan_init constructor to runtime lib.
Found by an Android toolchain upgrade, inherited module constructors
(like init_have_lse_atomics from the builtins) can sneak into the hwasan
runtime. If these inherited constructors call hwasanified libc
functions, then the HWASan runtime isn't setup enough, and the code
crashes.

Mark the initialized as a high-priority initializer to fix this.

Reviewed By: pcc, yabinc

Differential Revision: https://reviews.llvm.org/D107391
2021-08-04 11:18:23 -07: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
Leonard Chan b4c00b3882 [compiler-rt][hwasan] Remove __sanitizer allocation functions from hwasan interface
These functions should not be externally used. We also do not need them internally for Fuchsia.

Differential Revision: https://reviews.llvm.org/D99381
2021-07-30 11:37:19 -07:00
Dmitry Vyukov 4e15ee2867 sanitizer_common: remove BlockingMutex and RWMutex
Remove the legacy typedefs and use Mutex/Lock types directly.

Reviewed By: melver

Differential Revision: https://reviews.llvm.org/D107043
2021-07-29 12:38:53 +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
Leonard Chan 86832c9440 [compiler-rt][hwasan][Fuchsia] Do not emit FindDynamicShadowStart for Fuchsia
This function is unused because fuchsia does not support a dynamic shadow.

Differential Revision: https://reviews.llvm.org/D105735
2021-07-27 15:47:11 -07:00
Leonard Chan b49a3bf7c0 [compiler-rt][hwasan][fuchsia] Implement TagMemoryAligned for fuchsia
This will just be a call into __sanitizer_fill_shadow defined in the fuchsia source tree. This depends on D105663.

Differential Revision: https://reviews.llvm.org/D105664
2021-07-26 16:28:08 -07:00
Leonard Chan 71af002d15 [compiler-rt][hwasan][fuchsia] Implement InitializeOsSupport
This is empty for now, but we will add a check that TBI is enabled once the
tagged pointer ABI for zircon is finalized. This depends on D105667.

Differential Revision: https://reviews.llvm.org/D105668
2021-07-26 16:04:59 -07:00
Leonard Chan b50fb58695 [compiler-rt][hwasan][fuchsia] Define shadow bound globals
These are required by MemIsShadow for checking if an address actually is shadow memory.

Differential Revision: https://reviews.llvm.org/D105745
2021-07-26 15:40:45 -07: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
Leonard Chan 189c552518 [compiler-rt][hwasan] Refactor kAliasRegionStart usage
This moves logic for setting kAliasRegionStart into hwasan_allocator.cpp
so other platforms that do not support aliasing mode will not need to define
kAliasRegionStart.

Differential Revision: https://reviews.llvm.org/D105725
2021-07-12 16:33:05 -07:00
Leonard Chan b270c3fbde [NFC][compiler-rt][hwasan] Move shadow bound variables to hwasan.cpp
This way, other platforms can define these values.

Differential Revision: https://reviews.llvm.org/D105744
2021-07-12 10:16:28 -07:00
Leonard Chan 8b68aeebd3 [NFC][compiler-rt][hwasan] Move TagMemoryAligned into hwasan_linux.cpp
Based on comments in D91466, we can make the current implementation
linux-speciic. The fuchsia implementation will just be a call to
`__sanitizer_fill_shadow`.

Differential Revision: https://reviews.llvm.org/D105663
2021-07-09 11:02:44 -07:00