246 Commits

Author SHA1 Message Date
Youngsuk Kim
6989859254 [llvm][HWASan] Replace calls to Type::getPointerTo (NFC)
If `Type::getPointerTo` is called solely to support an unnecessary
pointer-cast, remove the call entirely.

Otherwise, replace with IRB.getPtrTy().

Clean-up work towards removing method `Type::getPointerTo`.
2023-11-28 14:45:59 -06:00
Fangrui Song
107185fa85
[sanitizer] Remove unneeded pointer casts and migrate away from getInt8PtrTy. NFC (#72327)
After opaque pointer migration, getInt8PtrTy() is considered legacy.
Replace it with getPtrTy(), and while here, remove some unneeded pointer
casts.
2023-11-15 10:48:58 -08:00
Simon Pilgrim
3ca4fe80d4 [Transforms] Use StringRef::starts_with/ends_with instead of startswith/endswith. NFC.
startswith/endswith wrap starts_with/ends_with and will eventually go away (to more closely match string_view)
2023-11-06 16:50:18 +00:00
Youngsuk Kim
7523b89643 [llvm] Remove no-op ptr-to-ptr bitcasts (NFC)
Opaque pointer cleanup effort. NFC.
2023-10-25 19:01:05 -05:00
Kirill Stoimenov
844c731f2d
[HWASAN] Mark built-ins as not built-ins to prevent optimizations (#68936)
The other 3 sanitizers (ASAN, TSAN and MSAN) all use
maybeMarkSanitizerLibraryCallNoBuiltin to make disable optimizations
which inline functions like memcmp for example. The lack of this
optimization was allowing ExpandMemCmpPass to convert a memcmp call to
inlined assembly and cause a false negative in HWASAN.
2023-10-13 11:43:29 -07:00
Thurston Dang
3f608ab710 [hwasan] Replace &LI with *LI, to fix build breakage
My patch (b3b6edefdb) broke the build
(https://lab.llvm.org/buildbot/#/builders/5/builds/37053) because it incorrectly assumed LoopInfo could not be null
and used a reference. This fixes forward by replacing &LI with *LI.
2023-09-28 19:34:21 +00:00
Thurston Dang
b3b6edefdb
[hwasan] Update (Post-)DominatorTreeAnalysis and LoopAnalysis incrementally (#66935)
HWAddressSanitizerPass::run sanitizes functions one by one. The
sanitization of each function - which may split blocks via
insertShadowTagCheck - may result in some cached analyses are invalid.
This matters because sanitizeFunction(F', FAM) may indirectly call the
global stack safety analysis, hence we need to make sure the analyses of
F are up to date.

Bug report: https://github.com/llvm/llvm-project/issues/66934
2023-09-28 09:09:38 -07:00
Vitaly Buka
be601928e1 [HWASAN] Inline fast pass of instrumentMemAccessOutline
Usually pointer tag will match tag in the shadow, so we can keep
inlining this check keeping the rest in outlined part.

It imroves performance by about 25%, but increases code size by 30%.
Existing outlining reduces performance by 30%, but saves code size by 80%.
So we still significantly reduce code size with minimal performance loss.

Reviewed By: fmayer

Differential Revision: https://reviews.llvm.org/D159172
2023-08-31 21:26:48 -07:00
Vitaly Buka
c6aaf2e521 [NFC][HWASAN] Extract insertShadowTagCheck()
Reviewed By: fmayer

Differential Revision: https://reviews.llvm.org/D159165
2023-08-31 13:22:51 -07:00
Vitaly Buka
b80fa58bdc [NFC][hwasan] Rename local variable 2023-08-31 12:25:46 -07:00
Vitaly Buka
bb637396db [test][HWASAN] Precommit -hwasan-inline-fast-path-checks tests
Reviewed By: fmayer

Differential Revision: https://reviews.llvm.org/D159157
2023-08-31 11:24:36 -07:00
Vitaly Buka
5a902312f1 [NFC][hwasan] Make most class functions private 2023-08-29 15:57:49 -07:00
Florian Mayer
9a67c6beb2 [NFC] [HWASan] simplify code
Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D156382
2023-07-26 17:05:19 -07:00
Florian Mayer
12982d250d [NFC] [HWASan] remove unused include 2023-07-26 14:34:31 -07:00
Florian Mayer
6cc9244baa Enable hwasan-use-after-scope by default
This has been in use for a long time without any issues.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D156267
2023-07-25 17:36:15 -07:00
Enna1
f28ed7f695 [NFC][HWASAN] make variables names for callback functions more consistent
This patch makes the variables names for callback functions more consistent. Changes no functionality.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D151605
2023-05-31 14:04:12 +08:00
Enna1
ba13e1b438 [hwasan] support hwasan-match-all-tag flag for hwasan meminstrinsic calls
This patch implements `__hwasan_memset_match_all`, `__hwasan_memcpy_match_all` and `__hwasan_memmove_match_all`, making hwasan-match-all-tag flag working for hwasan versions of memset, memcpy and memmove.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D149943
2023-05-27 10:35:52 +08:00
Enna1
b33dcc5b1e [hwasan] support hwasan-match-all-tag flag for callback memory access instrumentation
Currently, hwasan-match-all-tag flag is supported in inline memory access instrumentation and outline memory access instrumentation, but not supported in callback memory access instrumentation.

- For inline memory access instrumentation: a hwasan-match-all-tag check is added following the tag-mismtach check, if tag from pointer is mismatched with tag from shadow memory and tag from pointer is not equal with hwasan-match-all-tag, then a tag-mismatch will be report.
- For outline memory acess instrumentation: MatchAllTag is encoded in AccessInfo, when emit HWASAN memaccess symbols, asm-printer emits assembly instructions to check if tag from pointer is equal with hwasan-match-all-tag.
- For callback memory access instrumentation: hwasan-match-all-tag check is not implemented in `__hwasan_load`/`__hwasan_store`.

This patch implements a set of callback functions: `__hwasan_[load|store][1|2|4|8|16|n]_match_all` and `__hwasan_load[load|store][1|2|4|8|16|n]_match_all_noabort`, making hwasan-match-all-tag flag working for callback memory access instrumentation.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D149580
2023-05-27 10:27:50 +08:00
Mitch Phillips
8f061edef8 [HWASan] Ignore shortgranules for global tag selection
Tag selection for global variables is sequential, starting at a
pseduo-ish seed that's based on the hash of the file name.

Previously, it was possible for a global to be assigned a tag in the
range [1,15]. If the global's size was not a multiple of granules (i.e.
`size % 16 != 0`), then the last granule of the global would be assigned
a short granule tag as well.

If the real memory tag of the global (e.g. '04') happened to collide
with the short granule tag (e.g. '04'), then __hwasan_check would
see that the memory tag matched the short granule tag, and dutifully
assume (in this fast check) that everthing is okay.

Unfortunately, if you tried to access the [5,15]th byte, you never get
to the short granule check. This means you miss intra-granule overflows
on the last granule of a global, if said global was assigned a real
memory tag in the range [1,15].

This causes flakiness in certain global tests, if the SHA of the
filename changes between runs.

This problem also exists for heap and stack allocations as well, but
there's a concern that if we exclude the [1,15] range for heap and stack
that it's an unhappy tradeoff. On Android, this would mean that a 1/255
chance of false positive becomes 1/240. On other platforms though (that
have a less-than-8-bit tag space), this may be unacceptable. We can
think about potential fixes for that in other places, but globals are
fine to reduce the space, as really the only thing that matters is
catching sequential overflow. The false-negative scenario is much, much
more common in use-after-free and use-after-scope, which globals don't
have.

Differential Revision: https://reviews.llvm.org/D150742
2023-05-18 17:49:15 -07:00
Enna1
11dd1fe32f [NFC][HWASAN] replace redundant calls to IRBuilder::get*Ty() with saved types
Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D149628
2023-05-10 09:29:32 +08:00
Vitaly Buka
e8893133d1 Revert "[NFC][HWASAN] Handle tags as Int8"
More tests need updates.

This reverts commit e876ba5db98830db427395ed9b3718d20bf519fb.
2023-04-30 20:59:43 -07:00
Vitaly Buka
e876ba5db9 [NFC][HWASAN] Handle tags as Int8 2023-04-30 19:58:01 -07:00
Vitaly Buka
0b97aff4d2 [NFC][HWASAN] Rename local variable 2023-04-30 19:49:25 -07:00
Vitaly Buka
f42f863c33 [NFC][HWASAN] Set constant type from another operand 2023-04-30 19:07:57 -07:00
Vitaly Buka
37f6c9f852 [HWASAN] Untag before tagging alloca pointers
This is folloup to b5595836, which missed the
Replacemen variable.

Before b5595836 the code assumed that alloca
ptrs are not tagged so tagging is implemented
as simple OR.

So this patch completes support of tagged SP
by passing untagged alloca pointers into
tagPointer.
2023-04-30 18:26:58 -07:00
Vitaly Buka
d3c37e2cd1 [NFC][HWASAN] Use pointercast instead of bitcast 2023-04-29 17:51:19 -07:00
Vitaly Buka
a1cca2e2d1 [NFC][HWASAN] Add cont to parameter 2023-04-29 17:51:19 -07:00
Vitaly Buka
2db925659e [NFC][HWASAN] Fix comment 2023-04-29 17:51:19 -07:00
Vitaly Buka
87d473af69 [NFC][HWASAN] Remove unused parameter 2023-04-29 17:51:18 -07:00
Vitaly Buka
cb6099ba43 [HWASAN] Untag pointer with TagMaskByte instead 0xFF
This applies only X86_64 where mask is 0x3F.

Reviewed By: kstoimenov

Differential Revision: https://reviews.llvm.org/D149227
2023-04-26 23:38:11 -07:00
Vitaly Buka
fb1ecab023 [HWASAN] Fix X86 ClInstrumentWithCalls
It was always ON for X86.
2023-04-26 23:30:01 -07:00
Vitaly Buka
b5595836bc [HWASAN] Support tagged stack pointer
If stack was allocated using regular allocator, it may be tagged
and it will make memToShadow calculate invalid offset.

Also when UAR tag should be the tag of the stack frame pointer.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D149228
2023-04-26 19:27:19 -07:00
Vitaly Buka
b6da4bf1f8 [NFC][HWASAN] Move getUARTag into the Entry
Reviewed By: kstoimenov, eugenis

Differential Revision: https://reviews.llvm.org/D149221
2023-04-26 15:28:35 -07:00
Vitaly Buka
6e8b2a8054 [HWASAN] Remove UAR tag branch !ClUARRetagToZero
This is leftover from older version of HWASAN.
The current HWASAN assumes that the new stack
frames are tagged with zeroes, which make getNextTagWithCall
or StackTag ^ TagMaskByte unusable.

Reviewed By: kstoimenov, eugenis

Differential Revision: https://reviews.llvm.org/D149220
2023-04-26 15:27:36 -07:00
Vitaly Buka
8fc1764ef3 [NFC][HWASAN] Check TagMaskByte instead of TargetTriple
We need to apply mask for x86_64 because the mask is not full byte. So
instead checking the arch we can check the mask.
2023-04-25 20:04:58 -07:00
Vitaly Buka
4bb6816018 [NFC][HWASAN] Fix top comment 2023-04-25 20:04:58 -07:00
Vitaly Buka
12d8e17511 [NFC][hwasan] Add const to static array 2023-04-25 17:53:55 -07:00
Philip Reames
5eb9acf9be [HWASAN] Instrument scalable load/store without crashing
We can simply push them down the existing call slowpath with some minor changes to how we compute the size argument.
2023-03-23 08:24:30 -07:00
Arthur Eubanks
60ebe901eb [HWAsan] Fix returned PreservedAnalyses
Initialization modifies the module.
2023-03-16 09:42:09 -07:00
Philip Reames
45b6a33b51 [ASAN] Use TypeSize in InterestingMemoryOperand [mostly NFC]
This is a mechanical prep change for scalable vector support.  All it does is move the point of TypeSize to unsigned (i.e. the unsafe cast) closer to point of use.
2023-03-02 07:57:40 -08:00
Liren Peng
529ee9750b [NFC] Use single quotes for single char output during printPipline
Reviewed By: arsenm

Differential Revision: https://reviews.llvm.org/D144365
2023-02-22 02:35:13 +00:00
Archibald Elliott
62c7f035b4 [NFC][TargetParser] Remove llvm/ADT/Triple.h
I also ran `git clang-format` to get the headers in the right order for
the new location, which has changed the order of other headers in two
files.
2023-02-07 12:39:46 +00:00
Kazu Hirata
55e2cd1609 Use llvm::count{lr}_{zero,one} (NFC) 2023-01-28 12:41:20 -08:00
Kazu Hirata
da6642a1c8 [llvm] Include <optional> instead of "llvm/ADT/Optional.h" (NFC)
This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-14 15:09:00 -08:00
Kazu Hirata
77c90c8ce0 [llvm] Use std::optional instead of Optional
This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-20 15:42:32 -08:00
Florian Mayer
f361777168 [NFC] use Optional instead of separate bool 2022-12-19 15:52:24 -08:00
Florian Mayer
eddb728046 [NFC] use bitwise or instead of addition
as the bits are all distinct, these two operations have the same result,
but the bitwise operation is more explicit about what's happening.

Reviewed By: hctim

Differential Revision: https://reviews.llvm.org/D140346
2022-12-19 15:34:31 -08:00
Florian Mayer
c29d867833 [NFC] fix some clang-tidy warnings for hwasan pass 2022-12-19 12:57:58 -08:00
Alexey Baturo
f87382124f re-land [RISC-V][HWASAN] Add support for HWASAN code instrumentation for RISC-V
now with the fixed warning and updated lit tests
---
[RISC-V][HWASAN] Add support for HWASAN code instrumentation for RISC-V

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D131575
2022-12-14 14:55:06 +03:00
Krasimir Georgiev
1f9fe3427b Revert "[RISC-V][HWASAN] Add support for HWASAN code instrumentation for RISC-V"
This reverts commit 0eb694bec09df488bb6ab33d0207b6b061718d98.
Temporarily revert as it causes a new warning:
https://reviews.llvm.org/D131575#inline-1351186
2022-12-13 16:30:54 +01:00