Commit Graph

219 Commits

Author SHA1 Message Date
Leonard Chan 21f72c05c4 [hwasan] Add __hwasan_add_frame_record to the hwasan interface
Hwasan includes instructions in the prologue that mix the PC and SP and store
it into the stack ring buffer stored at __hwasan_tls. This is a thread_local
global exposed from the hwasan runtime. However, if TLS-mechanisms or the
hwasan runtime haven't been setup yet, it will be invalid to access __hwasan_tls.
This is the case for Fuchsia where we instrument libc, so some functions that
are instrumented but can run before hwasan initialization will incorrectly
access this global. Additionally, libc cannot have any TLS variables, so we
cannot weakly define __hwasan_tls until the runtime is loaded.

A way we can work around this is by moving the instructions into a hwasan
function that does the store into the ring buffer and creating a weak definition
of that function locally in libc. This way __hwasan_tls will not actually be
referenced. This is not our long-term solution, but this will allow us to roll
out hwasan in the meantime.

This patch includes:

- A new llvm flag for choosing to emit a libcall rather than instructions in the
  prologue (off by default)
- The libcall for storing into the ringbuffer (__hwasan_add_frame_record)

Differential Revision: https://reviews.llvm.org/D128387
2022-07-13 15:15:15 -07:00
Leonard Chan d843d5c8e6 Revert "[hwasan] Add __hwasan_record_frame_record to the hwasan interface"
This reverts commit 4956620387.

This broke a sanitizer builder: https://lab.llvm.org/buildbot/#/builders/77/builds/19597
2022-07-13 15:06:07 -07:00
leonardchan 4956620387 [hwasan] Add __hwasan_record_frame_record to the hwasan interface
Hwasan includes instructions in the prologue that mix the PC and SP and store
it into the stack ring buffer stored at __hwasan_tls. This is a thread_local
global exposed from the hwasan runtime. However, if TLS-mechanisms or the
hwasan runtime haven't been setup yet, it will be invalid to access __hwasan_tls.
This is the case for Fuchsia where we instrument libc, so some functions that
are instrumented but can run before hwasan initialization will incorrectly
access this global. Additionally, libc cannot have any TLS variables, so we
cannot weakly define __hwasan_tls until the runtime is loaded.

A way we can work around this is by moving the instructions into a hwasan
function that does the store into the ring buffer and creating a weak definition
of that function locally in libc. This way __hwasan_tls will not actually be
referenced. This is not our long-term solution, but this will allow us to roll
out hwasan in the meantime.

This patch includes:

- A new llvm flag for choosing to emit a libcall rather than instructions in the
  prologue (off by default)
- The libcall for storing into the ringbuffer (__hwasan_record_frame_record)

Differential Revision: https://reviews.llvm.org/D128387
2022-07-14 05:07:11 +08:00
Mitch Phillips faf5e0ec73 Add no_sanitize('hwaddress') (and 'memtag', but that's a no-op).
Currently, `__attribute__((no_sanitize('hwaddress')))` is not possible. Add this piece of plumbing, and now that we properly support copying attributes between an old and a new global variable, add a regression test for the GlobalOpt bug that previously lost the attribute.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D127544
2022-06-24 12:04:11 -07:00
Leonard Chan e1d84c421d [compiler-rt][sanitizer] Have all OOM-related error messages start with the same format
This way downstream tools that read sanitizer output can differentiate between OOM errors
reported by sanitizers from other sanitizer errors.

Changes:

- Introduce ErrorIsOOM for checking if a platform-specific error code from an "mmap" is an OOM err.
- Add ReportOOMError which just prepends this error message to the start of a Report call.
- Replace some Reports for OOMs with calls to ReportOOMError.
- Update necessary tests.

Differential Revision: https://reviews.llvm.org/D127161
2022-06-07 16:46:01 -07:00
Florian Mayer b77d16f7f3 [HWASan] deflake hwasan_symbolize test
Also enable on X86_64.

The directory would change during the test execution. This should not
necessarily prevent us from indexing a directory (a user might
potentially do that if they specify a parent directory of the actual
symbols directory, and change unrelated files).

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D125143
2022-05-09 14:11:52 -07:00
Florian Mayer d8564dcbcf [HWASan] Allow to linkify symbolizer output.
Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D124950
2022-05-06 15:44:14 -07:00
Nico Weber c9faea04b1 Revert "[HWASan] Allow to linkify symbolizer output."
This reverts commit 4af9392e13.
The new test fails on several machines (including some bots),
see https://reviews.llvm.org/D124950
2022-05-06 09:53:45 -04:00
Florian Mayer 4af9392e13 [HWASan] Allow to linkify symbolizer output.
Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D124950
2022-05-05 16:40:09 -07:00
Aaron Ballman 37a47aacba Speculatively fix build bot
This should address the issue in:
https://lab.llvm.org/buildbot/#/builders/77/builds/16683
2022-04-20 12:32:57 -04:00
Hans Wennborg 3e6cfc631b [NFC][hwasan] Check __GLIBCXX__ before checking _GLIBCXX_RELEASE in test
_GLIBCXX_RELEASE wasn't defined before GCC 7.1.

This is another follow-up to https://reviews.llvm.org/D119161
2022-03-01 09:39:26 +01:00
Vitaly Buka 924032c592 [NFC][hwasan] Check _GLIBCXX_RELEASE in test
Differential Revision: https://reviews.llvm.org/D119161
2022-02-23 14:31:47 -08:00
Vitaly Buka c990d56d42 [HWASan] Use hwasan_memalign for aligned new.
Aligned new does not require size to be a multiple of alignment, so
memalign is the correct choice instead of aligned_alloc.

Fixes false reports for unaligned sizes.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D119161
2022-02-23 14:22:03 -08:00
Nico Weber a908c535f9 Revert "[HWASan] Use hwasan_memalign for aligned new."
This reverts commit 4fb7c120fe.
Breaks tests on Linux were system gcc doesn't support C++17
(but gcc used to build LLVM does), see https://reviews.llvm.org/D119161
2022-02-12 13:57:28 -05:00
Matt Morehouse 4fb7c120fe [HWASan] Use hwasan_memalign for aligned new.
Aligned new does not require size to be a multiple of alignment, so
memalign is the correct choice instead of aligned_alloc.

Fixes false reports for unaligned sizes.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D119161
2022-02-07 13:10:16 -08:00
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
Fangrui Song 9477a308ca [hwasan][test] Remove obsoleted/removed -fno-experimental-new-pass-manager 2022-02-01 13:24:39 -08:00
Florian Mayer aefb2e134d [hwasan] work around lifetime issue with setjmp.
setjmp can return twice, but PostDominatorTree is unaware of this. as
such, it overestimates postdominance, leaving some cases (see attached
compiler-rt) where memory does not get untagged on return. this causes
false positives later in the program execution.

this is a crude workaround to unblock use-after-scope for now, in the
longer term PostDominatorTree should bemade aware of returns_twice
function, as this may cause problems elsewhere.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D118647
2022-02-01 12:14:20 -08:00
Matt Morehouse de4e8bc3ac [HWASan] Properly handle musttail calls.
Fixes a compile error when the `clang::musttail` attribute is used.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D118712
2022-02-01 11:23:43 -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
Florian Mayer edd2b99a57 [sanitizers] include build ids in stacks on linux.
Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D114294
2021-12-10 14:24:03 -08:00
Florian Mayer 06430acc00 Revert "[sanitizers] include build ids in stacks on linux."
This reverts commit 2a31b240df.
2021-12-10 11:52:26 -08:00
Florian Mayer 2a31b240df [sanitizers] include build ids in stacks on linux.
Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D114294
2021-12-10 11:30:34 -08:00
Matt Morehouse 2022e2fcd0 [HWASan] Disable LTO test on aarch64.
It fails for non-Android aarch64 bots as well.
2021-11-29 08:08:17 -08:00
Vitaly Buka 8ae815cb19 [sanitizer] Switch StackStore to 8 MiB blocks
Larger blocks are more convenient for compressions.
Blocks are allocated with MmapNoReserveOrDie to save some memory.

Also it's 15% faster on StackDepotBenchmarkSuite

Depends on D114464.

Reviewed By: morehouse

Differential Revision: https://reviews.llvm.org/D114488
2021-11-26 14:00:10 -08:00
Matt Morehouse 615ecd8afc [HWASan] Move LTO test to separate file.
The test fails on Android for an unknown reason but is still worth
having for x86.
2021-11-22 13:19:11 -08:00
Matt Morehouse 5684a614dd [HWASan] Remove -Wa,-mrelax-relocations=no flag.
We no longer need it after https://reviews.llvm.org/D113220.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D113221
2021-11-22 11:21:27 -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
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
Matt Morehouse 4d8b0aa5c0 [HWASan] Apply TagMaskByte to every global tag.
Previously we only applied it to the first one, which could allow
subsequent global tags to exceed the valid number of bits.

Reviewed By: hctim

Differential Revision: https://reviews.llvm.org/D112853
2021-11-01 06:31:44 -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
Matt Morehouse e1e2635327 [HWASan] Use tagged-globals feature on x86.
Allows us to use the small code model when we disable relocation
relaxation.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D111344
2021-10-19 05:56:50 -07:00
Evgenii Stepanov 7a3fb71c3c [hwasan] Fix TestCases/thread-uaf.c.
On newer glibc, this test detects an extra match somewhere under
pthread_getattr_np. This results in Thread: lines getting spread out in
the report and failing to match the CHECKs.

Fix the CHECKs to allow this possibility.

Reviewed By: fmayer

Differential Revision: https://reviews.llvm.org/D111841
2021-10-14 14:56:57 -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 acaca56471 [HWASan] Test longjmp(jmpbuf, 0).
Our interceptor needs to change retval to 1 when 0 is passed.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D109788
2021-09-15 12:11:59 -07:00
Florian Mayer 2706b1a6e2 [hwasan] Test use-after-scope with -fno-exceptions.
Reviewed By: hctim

Differential Revision: https://reviews.llvm.org/D109224
2021-09-06 10:10:09 +01:00
Florian Mayer abf8ed8a82 [hwasan] Support more complicated lifetimes.
This is important as with exceptions enabled, non-POD allocas often have
two lifetime ends: the exception handler, and the normal one.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D108365
2021-09-03 10:29:50 +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
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 8c23669eeb [hwasan] Ignore lit config.enable_aliases on non-x86.
This re-enables a number of Android tests that have been lost in
check-hwasan.

Differential Revision: https://reviews.llvm.org/D108064
2021-08-16 13:29:37 -07:00
Florian Mayer e772e25547 [hwasan] Fix wild free tests on x86. 2021-08-13 11:56:02 +01: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 ab41f37ef6 [hwasan] Fix test with TCO eliminating free frame.
This broke https://lab.llvm.org/buildbot/#/builders/37/builds/6016/steps/10/logs/stdio

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D107864
2021-08-11 09:51:14 +01:00
Florian Mayer be59a332eb [hwasan] Fix test on x86.
Reviewed By: dvyukov

Differential Revision: https://reviews.llvm.org/D107816
2021-08-10 13:06:12 +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
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