Commit Graph

38 Commits

Author SHA1 Message Date
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
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
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
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
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
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
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
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
Florian Mayer 745758acf3 [hwasan] Fix incorrect candidate matching for stack OOB.
We would find an address with matching tag, only to discover in
ShowCandidate that it's very far away from [stack].

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D105197
2021-07-06 12:24:07 +01:00
Florian Mayer a0b1f3aac5 [hwasan] Check for overflow when searching candidates.
If the fault address is at the boundary of memory regions, this could
cause us to segfault otherwise.

Ran test with old compiler_rt to make sure it fails.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D105032
2021-07-06 11:22:13 +01:00
Florian Mayer 90f244c843 [hwasan] Add missing newline in report.
Reviewed By: glider

Differential Revision: https://reviews.llvm.org/D105190
2021-06-30 15:48:45 +01:00
Florian Mayer b458bb8c04 [hwasan] Display causes in order of probability.
A heap or global buffer that is far away from the faulting address is
unlikely to be the cause, especially if there is a potential
use-after-free as well, so we want to show it after the other
causes.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D104781
2021-06-29 13:00:05 +01:00
Florian Mayer 400509238a Revert "[hwasan] print exact mismatch offset for short granules."
Broke x86 LAM bot.

This reverts commit 2a60ab76a7.
2021-06-29 11:57:04 +01:00
Florian Mayer 2a60ab76a7 [hwasan] print exact mismatch offset for short granules.
Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D104463
2021-06-28 19:01:31 +01:00
Florian Mayer 8f9db0aeeb [hwasan] Show sp in register dump.
Reviewed By: hctim, eugenis

Differential Revision: https://reviews.llvm.org/D104787
2021-06-28 10:28:59 +01:00
Vitaly Buka a9f3ac9e3d Revert "[hwasan] print exact mismatch offset for short granules."
Breaks sanitizer-x86_64-linux-android and sanitizer-x86_64-linux-qemu bots.

This reverts commit 7e3f8b8aff.
2021-06-24 17:33:24 -07:00
Florian Mayer 7e3f8b8aff [hwasan] print exact mismatch offset for short granules.
Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D104463
2021-06-24 09:58:02 +01:00
Florian Mayer e0b68f7149 [hwasan] Clarify report for allocation-tail-overwritten.
Explain what the given stack trace means before showing it, rather than
only in the paragraph at the end.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D104523
2021-06-18 18:52:41 +01:00
Florian Mayer ccc0f777f6 [hwasan] Improve report for addresses within regions.
Before: ADDR is located -320 bytes to the right of 1072-byte region
After: ADDR is located 752 bytes inside 1072-byte region

Reviewed By: eugenis, walli99

Differential Revision: https://reviews.llvm.org/D104412
2021-06-17 12:01:30 +01:00
Leonard Chan 312011899a [compiler-rt][hwasan] Add newline between record_addr lines on frame record dumps
If SymbolizePC failes, it's possible for the newline to not be emitted.

Differential Revision: https://reviews.llvm.org/D103845
2021-06-14 17:13:13 -07: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
Evgenii Stepanov 08d90f72ce [hwasan] Implement error report callback.
Similar to __asan_set_error_report_callback, pass the entire report to a
user provided callback function.

Differential Revision: https://reviews.llvm.org/D91825
2020-11-20 16:48:19 -08:00
Matthew Malcomson 83ac18205e Hwasan reporting check for dladdr failing
In `GetGlobalSizeFromDescriptor` we use `dladdr` to get info on the the
current address.  `dladdr` returns 0 if it failed.
During testing on Linux this returned 0 to indicate failure, and
populated the `info` structure with a NULL pointer which was
dereferenced later.

This patch checks for `dladdr` returning 0, and in that case returns 0
from `GetGlobalSizeFromDescriptor` to indicate failure of identifying
the address.

This occurs when `GetModuleNameAndOffsetForPC` succeeds for some address
not in a dynamically loaded library.  One example is when the found
"module" is '[stack]' having come from parsing /proc/self/maps.

Differential Revision: https://reviews.llvm.org/D91344
2020-11-16 12:25:27 +00:00
Teresa Johnson 4d5b1de40e [sanitizer] Skip stack symbolization when not required for print format
Adds a check to avoid symbolization when printing stack traces if the
stack_trace_format flag does not need it. While there is a symbolize
flag that can be turned off to skip some of the symbolization,
SymbolizePC() still unconditionally looks up the module name and offset.
Avoid invoking SymbolizePC() at all if not needed.

This is an efficiency improvement when dumping all stack traces as part
of the memory profiler in D87120, for large stripped apps where we want
to symbolize as a post pass.

Differential Revision: https://reviews.llvm.org/D88361
2020-10-07 15:38:52 -07:00
Arthur Eubanks bf124017a2 [NFC] Rename variable to workaround old gcc bug
Summary:
gcc 5.1 is still supported according to
https://releases.llvm.org/10.0.0/docs/GettingStarted.html

We're hitting the following bug due to a variable created in the loop header being the same as a variable used in the loop header:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54430

Reviewers: hctim, pcc

Subscribers: #sanitizers

Tags: #sanitizers

Differential Revision: https://reviews.llvm.org/D81594
2020-06-10 12:14:53 -07:00
Mitch Phillips e26b25f8b1 [HWASan] Add sizeof(global) in report even if symbols missing.
Summary: Refactor the current global header iteration to be callback-based, and add a feature that reports the size of the global variable during reporting. This allows binaries without symbols to still report the size of the global variable, which is always available in the HWASan globals PT_NOTE metadata.

Reviewers: eugenis, pcc

Reviewed By: pcc

Subscribers: mgorny, llvm-commits, #sanitizers

Tags: #sanitizers, #llvm

Differential Revision: https://reviews.llvm.org/D80599
2020-06-09 13:02:13 -07:00
Peter Collingbourne d2a26ad0dc hwasan: Collect ring buffer statistics and include in dev note.
These statistics are intended to help us tune the scudo MTE
implementation.

Differential Revision: https://reviews.llvm.org/D79913
2020-05-14 09:47:42 -07:00
Martin Liska 2045d2c90e
Make memory dump same as the one in asan.
Shadow memory (and short granules) are not prepended with memory
address and arrow at the end of line is removed.

Differential Revision: https://reviews.llvm.org/D70707
2019-11-27 18:46:38 +01:00
Evgenii Stepanov 7b9d6a2256 [hwasan] Fix typo in the error type.
"alocation-tail-overwritten" -> "allocation-tail-overwritten"
2019-10-28 13:25:45 -07:00
Peter Collingbourne 0930643ff6 hwasan: Instrument globals.
Globals are instrumented by adding a pointer tag to their symbol values
and emitting metadata into a special section that allows the runtime to tag
their memory when the library is loaded.

Due to order of initialization issues explained in more detail in the comments,
shadow initialization cannot happen during regular global initialization.
Instead, the location of the global section is marked using an ELF note,
and we require libc support for calling a function provided by the HWASAN
runtime when libraries are loaded and unloaded.

Based on ideas discussed with @evgeny777 in D56672.

Differential Revision: https://reviews.llvm.org/D65770

llvm-svn: 368102
2019-08-06 22:07:29 +00:00
Peter Collingbourne 1366262b74 hwasan: Improve precision of checks using short granule tags.
A short granule is a granule of size between 1 and `TG-1` bytes. The size
of a short granule is stored at the location in shadow memory where the
granule's tag is normally stored, while the granule's actual tag is stored
in the last byte of the granule. This means that in order to verify that a
pointer tag matches a memory tag, HWASAN must check for two possibilities:

* the pointer tag is equal to the memory tag in shadow memory, or
* the shadow memory tag is actually a short granule size, the value being loaded
  is in bounds of the granule and the pointer tag is equal to the last byte of
  the granule.

Pointer tags between 1 to `TG-1` are possible and are as likely as any other
tag. This means that these tags in memory have two interpretations: the full
tag interpretation (where the pointer tag is between 1 and `TG-1` and the
last byte of the granule is ordinary data) and the short tag interpretation
(where the pointer tag is stored in the granule).

When HWASAN detects an error near a memory tag between 1 and `TG-1`, it
will show both the memory tag and the last byte of the granule. Currently,
it is up to the user to disambiguate the two possibilities.

Because this functionality obsoletes the right aligned heap feature of
the HWASAN memory allocator (and because we can no longer easily test
it), the feature is removed.

Also update the documentation to cover both short granule tags and
outlined checks.

Differential Revision: https://reviews.llvm.org/D63908

llvm-svn: 365551
2019-07-09 20:22:36 +00:00
Peter Collingbourne 559b2e0187 hwasan: Fix an off-by-one error in PrintTagsAroundAddr.
Previously we were printing 16 rows of tags, not 17.

Differential Revision: https://reviews.llvm.org/D63906

llvm-svn: 364609
2019-06-27 23:24:36 +00:00
Peter Collingbourne d11ea6518c hwasan: Teach the runtime to identify the local variable being accessed in UAR reports.
Each function's PC is recorded in the ring buffer. From there we can access
the function's local variables and reconstruct the tag of each one with the
help of the information printed by llvm-symbolizer's new FRAME command. We
can then find the variable that was likely being accessed by matching the
pointer's tag against the reconstructed tag.

Differential Revision: https://reviews.llvm.org/D63469

llvm-svn: 364607
2019-06-27 23:16:13 +00:00
Mitch Phillips 621de86907 [HWASan] Use less Printf() calls in register dump.
Summary:
Explicitly print 4 registers/line in each iteration during register
dump. Reduces logcat spam as we get a single logcat message per call to
Printf(), even if the output isn't newline-terminated. This brings the
output format in logcat closer to that of the normal textual dump.

Reviewers: eugenis, pcc

Reviewed By: pcc

Subscribers: kubamracek, #sanitizers, llvm-commits

Tags: #sanitizers, #llvm

Differential Revision: https://reviews.llvm.org/D59320

llvm-svn: 356166
2019-03-14 17:05:53 +00:00
Mitch Phillips 790edbc16e [HWASan] Save + print registers when tag mismatch occurs in AArch64.
Summary:
This change change the instrumentation to allow users to view the registers at the point at which tag mismatch occured. Most of the heavy lifting is done in the runtime library, where we save the registers to the stack and emit unwind information. This allows us to reduce the overhead, as very little additional work needs to be done in each __hwasan_check instance.

In this implementation, the fast path of __hwasan_check is unmodified. There are an additional 4 instructions (16B) emitted in the slow path in every __hwasan_check instance. This may increase binary size somewhat, but as most of the work is done in the runtime library, it's manageable.

The failure trace now contains a list of registers at the point of which the failure occured, in a format similar to that of Android's tombstones. It currently has the following format:

Registers where the failure occurred (pc 0x0055555561b4):
    x0  0000000000000014  x1  0000007ffffff6c0  x2  1100007ffffff6d0  x3  12000056ffffe025
    x4  0000007fff800000  x5  0000000000000014  x6  0000007fff800000  x7  0000000000000001
    x8  12000056ffffe020  x9  0200007700000000  x10 0200007700000000  x11 0000000000000000
    x12 0000007fffffdde0  x13 0000000000000000  x14 02b65b01f7a97490  x15 0000000000000000
    x16 0000007fb77376b8  x17 0000000000000012  x18 0000007fb7ed6000  x19 0000005555556078
    x20 0000007ffffff768  x21 0000007ffffff778  x22 0000000000000001  x23 0000000000000000
    x24 0000000000000000  x25 0000000000000000  x26 0000000000000000  x27 0000000000000000
    x28 0000000000000000  x29 0000007ffffff6f0  x30 00000055555561b4

... and prints after the dump of memory tags around the buggy address.

Every register is saved exactly as it was at the point where the tag mismatch occurs, with the exception of x16/x17. These registers are used in the tag mismatch calculation as scratch registers during __hwasan_check, and cannot be saved without affecting the fast path. As these registers are designated as scratch registers for linking, there should be no important information in them that could aid in debugging.

Reviewers: pcc, eugenis

Reviewed By: pcc, eugenis

Subscribers: srhines, kubamracek, mgorny, javed.absar, krytarowski, kristof.beyls, hiraditya, jdoerfert, llvm-commits, #sanitizers

Tags: #sanitizers, #llvm

Differential Revision: https://reviews.llvm.org/D58857

llvm-svn: 355738
2019-03-08 21:22:35 +00:00
Nico Weber e3b6d11038 hwasan: Rename source files from cc to cpp
As discussed elsewhere: LLVM uses cpp as its C++ source extension; the
sanitizers should too. This updates files in hwasan.

Patch generated by

    for f in lib/hwasan/*.cc ; do svn mv $f ${f%.cc}.cpp; done

followed by

    for f in lib/hwasan/*.cpp ; do sed -i '' -e '1s/\.cc -/.cpp /' $f; done

CMakeLists.txt updated manually.

Differential Revision: https://reviews.llvm.org/D58620

llvm-svn: 354989
2019-02-27 15:44:03 +00:00