Commit Graph

2985 Commits

Author SHA1 Message Date
Marco Elver
4eef2e30d6 [ThreadSanitizer] Add fallback DebugLocation for memintrinsic calls
When building with debug info enabled, some load/store instructions do
not have a DebugLocation attached. When using the default IRBuilder, it
attempts to copy the DebugLocation from the insertion-point instruction.
When there's no DebugLocation, no attempt is made to add one.

Add a fallback DebugLocation with the help of InstrumentationIRBuilder for
memintrinsics. In particular, the compiler may optimize load/store without
debug info into memintrinsics, which then are missing debug info as well.
2023-07-17 17:52:16 +02:00
Jakob Koschel
913f7e93da [SanitizerCoverage] Add fallback DebugLocation for instrumented calls
When building the kernel with LTO, KCOV & debug information enabled,
multiple inlinable SanitizerCoverage functions require debug information
present.

In such cases we repurpose the InstrumentationIRBuilder that ensures
the necessary debug information is added if necessary.

This has been done analogous to the work for the ThreadSanitizer
in D124937.

Bug: https://github.com/ClangBuiltLinux/linux/issues/1721

Reviewed By: melver

Differential Revision: https://reviews.llvm.org/D155377
2023-07-17 17:52:06 +02:00
Jakob Koschel
4a8b124930 [AddressSanitizer] Add fallback DebugLocation for instrumented calls
When building the kernel with LTO, KASAN & debug information enabled,
multiple inlinable AddressSanitizer functions require debug information
present.

In such cases we repurpose the InstrumentationIRBuilder that ensures
the necessary debug information is added if necessary.

This has been done analogous to the work for the ThreadSanitizer
in D124937.

Bug: https://github.com/ClangBuiltLinux/linux/issues/1721

Reviewed By: melver

Differential Revision: https://reviews.llvm.org/D155376
2023-07-17 17:51:33 +02:00
Weining Lu
a926a2660a [Triple] Add llvm::Triple::isLoongArch{32,64}
Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D155163
2023-07-17 20:34:35 +08:00
Mircea Trofin
95d833d7c3 [nfc] rename AM to MAM in PGOInstrumentation
Consistency with how we tend to name `ModuleAnalysisManager` parameters.
2023-07-12 14:45:58 -07:00
Mircea Trofin
cdfd1475c0 [nfc] simplify promoteIndirectCalls
There's no caller to `promoteIndirectCalls` that would pass a nullptr
`ModuleAnalysisManager`, so passing it by reference does away with a
bunch of nullptr tests, and also removes the need for a "OwnedORE".

Differential Revision: https://reviews.llvm.org/D155027
2023-07-12 14:20:06 -07:00
Teresa Johnson
546ec641b4 Restore "[MemProf] Use new option/pass for profile feedback and matching"
This restores commit b4a82b6225, reverted
in 3ab7ef28ee because it was thought to
cause a bot failure, which ended up being unrelated to this patch set.

Differential Revision: https://reviews.llvm.org/D154856
2023-07-11 13:16:20 -07:00
Teresa Johnson
95014050da Restore "[MemProf] Refactor memory profile matching into MemProfiler (NFC)"
This restores commit 29252fdd53, reverted
in 3498cf52ba because it was thought to
cause a bot failure, which ended up being unrelated to this patch set.

Differential Revision: https://reviews.llvm.org/D154872
2023-07-11 13:16:20 -07:00
JP Lehr
3498cf52ba Revert "[MemProf] Refactor memory profile matching into MemProfiler (NFC)"
This reverts commit 29252fdd53.

This broke AMD GPU OpenMP Offload buildbot
2023-07-11 05:55:55 -04:00
JP Lehr
3ab7ef28ee Revert "[MemProf] Use new option/pass for profile feedback and matching"
This reverts commit b4a82b6225.

Broke AMDGPU OpenMP Offload buildbot
2023-07-11 05:44:42 -04:00
Yeting Kuo
f79ad31f17 [ASAN] Support asan for masked.expandload/compressstore.
The patch uses a way similiar to vp.load/store and consider the mask popcount as
the effetive vector length.

Reviewed By: craig.topper

Differential Revision: https://reviews.llvm.org/D151713
2023-07-11 14:40:50 +08:00
Teresa Johnson
b4a82b6225 [MemProf] Use new option/pass for profile feedback and matching
Previously the MemProf profile was expected to be in the same profile
file as a normal PGO profile, passed via the usual -fprofile-use=
option, and was matched in the same pass. To simplify profile
preparation, since the raw MemProf profile requires the binary for
symbolization and may be simpler to index separately from the raw PGO
profile, and also to enable providing a MemProf profile for a SamplePGO
build, separate out the MemProf feedback option and matching pass.

This patch adds the -fmemory-profile-use=${file} option, and the
provided file is passed down to LLVM and ultimately used in a new
MemProfUsePass which performs the matching of just the memory profile
contents of that file.

Note that a single profile file containing both normal PGO and MemProf
profile data is still supported, and the relevant profile data is
matched by the appropriate matching pass(es) based on which option(s)
the profile is provided with (the same profile file can be supplied to
both feedback options).

Differential Revision: https://reviews.llvm.org/D154856
2023-07-10 16:42:56 -07:00
Teresa Johnson
29252fdd53 [MemProf] Refactor memory profile matching into MemProfiler (NFC)
Split out of D154856, this prepares for the addition of a new dedicated
memory profile matching pass.

Differential Revision: https://reviews.llvm.org/D154872
2023-07-10 13:12:58 -07:00
Yeting Kuo
f2977fc456 [ASAN] Support memeory check for masked.gather/scatter.
The patch handle masked.gather/scatter just like the way D149245 handles
vp.gather/scatter.

Reviewed By: craig.topper

Differential Revision: https://reviews.llvm.org/D151545
2023-07-05 09:17:46 +08:00
zhanglimin
280d163887 [dfsan] Support Linux loongarch64
Make minor changes to enable DFSAN and its tests on
loongarch64. And port Linux loongarch64 memory mappings
from msan.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D140690
2023-07-03 16:05:02 +08:00
zhanglimin
3749292e50 [sanitizer][msan] The LLVM part of the LoongArch memory sanitizer implementation
This patch enabled msan in LLVM and fixed all failing tests in
check-msan.

It does not add VarArgHelper implementation on LoongArch, which
will be done separately later. And it adds a test for VarArgNoOpHelper,
which is based on the X86 one.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D152692
2023-06-29 11:41:27 +08:00
Youngsuk Kim
243f0566dc [llvm] Replace uses of Type::getPointerTo (NFC)
Partial progress towards removing in-tree uses of `Type::getPointerTo`,
before we can deprecate the API.

If the API is used solely to support an unnecessary bitcast, get rid of
the bitcast as well.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D153933
2023-06-28 09:21:34 -04:00
Elliot Goodrich
f0fa2d7c29 [llvm] Move AttributeMask to a separate header
Move `AttributeMask` out of `llvm/IR/Attributes.h` to a new file
`llvm/IR/AttributeMask.h`.  After doing this we can remove the
`#include <bitset>` and `#include <set>` directives from `Attributes.h`.
Since there are many headers including `Attributes.h`, but not needing
the definition of `AttributeMask`, this causes unnecessary bloating of
the translation units and slows down compilation.

This commit adds in the include directive for `llvm/IR/AttributeMask.h`
to the handful of source files that need to see the definition.

This reduces the total number of preprocessing tokens across the LLVM
source files in lib from (roughly) 1,917,509,187 to 1,902,982,273 - a
reduction of ~0.76%. This should result in a small improvement in
compilation time.

Differential Revision: https://reviews.llvm.org/D153728
2023-06-27 15:26:17 +01:00
Fangrui Song
e3cc8f3440 [asan] Fix shadow load alignment for unaligned 128-bit load/store
When a 128-bit load/store is aligned by 8, we incorrectly emit `load i16, ptr ..., align 2`
while the shadow memory address may not be aligned by 2.

This manifests as possibly-misaligned shadow memory load with `-mstrict-align`,
e.g. `clang --target=aarch64-linux -O2 -mstrict-align -fsanitize=address`
```
__attribute__((noinline)) void foo(unsigned long *ptr) {
  ptr[0] = 3;
  ptr[1] = 3;
}
// ldrh    w8, [x9, x8]  // the shadow memory load may not be aligned by 2
```

Infer the shadow memory alignment from the load/store alignment to set the
correct alignment. The generated code now uses two ldrb and one orr.

Fix https://github.com/llvm/llvm-project/issues/63258

Differential Revision: https://reviews.llvm.org/D152663
2023-06-14 13:16:49 -07:00
Sami Tolvanen
ce4bb083c0 [KCFI] Fix hash offset calculation in Thumb mode
ARM stores the Thumb state in the least significant bit of the
function pointers. When compiling for ARM or Thumb, as all
instructions are at least 16-bit aligned, ignore the LSB when
computing the prefix hash location, so we can support both
pure Thumb and mixed ARM/Thumb binaries.

Fixes #62936

Reviewed By: MaskRay, simon_tatham

Differential Revision: https://reviews.llvm.org/D152484
2023-06-12 19:43:34 +00:00
Sami Tolvanen
2831a271c8 [KCFI] Emit debugtrap to make indirect call checks recoverable
KCFI traps should always be recoverable, but as Intrinsic::trap
is marked noreturn, it's not possible to continue execution after
handling the trap as the compiler is free to assume we never
return. Switch to debugtrap instead to ensure we have the option
to resume execution after the trap.
2023-06-02 19:39:13 +00: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
Yeting Kuo
c80fe251d8 [ASAN] Support memory checks on vp.gather/scatter.
The patch supports vp.gather/scatter by allowing addresses being pointer vectors.
And then we just need to check each active pointer element of those pointer vectors.

Reviewed By: reames

Differential Revision: https://reviews.llvm.org/D149245
2023-05-26 15:28:40 +08:00
Enna1
693a1b7024 [gcov] Add nosanitize metadata to memory access instructions inserted by emitProfileNotes()
This patch adds nosantize metadata to memory access instructions inserted by gcov emitProfileNotes(), making sanitizers skip these instructions when gcov and sanitizer are used together.

Reviewed By: nickdesaulniers

Differential Revision: https://reviews.llvm.org/D150460
2023-05-25 10:13:52 +08:00
Joshua Cao
0c316f0067 [BBUtils][NFC] Delete SplitBlockAndInsertIfThen with DT.
The method is marked for deprecation. Delete the method and move all of
its consumers to use the DomTreeUpdater version.

Reviewed By: foad

Differential Revision: https://reviews.llvm.org/D149428
2023-05-23 21:02:37 -07:00
Enna1
e4e6c6510b [IR] Adds Instruction::setNoSanitizeMetadata()
This patch adds a new method setNoSanitizeMetadata() for Instruction, and use it in SanitizerMetadata and SanitizerCoverage.

Reviewed By: nickdesaulniers, MaskRay

Differential Revision: https://reviews.llvm.org/D150632
2023-05-19 19:18:57 +08:00
Yeting Kuo
deccb2df23 [ASAN] Support memory checks on experimental.vp.strided.load/store.
The patch adds new member MaybeStride into InterestingMemoryOperand to represent
the stride value of experimental.vp.strided.load/store.

Reviewed By: reames

Differential Revision: https://reviews.llvm.org/D149073
2023-05-19 14:46:19 +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
Zain Jaffal
5d3a884229 [IRGen] Change annotation metadata to support inserting tuple of strings into annotation metadata array.
Annotation metadata supports adding singular annotation strings to annotation block. This patch adds the ability to insert a tuple of strings into the metadata array.

The idea here is that each tuple of strings represents a piece of information that can be all related. It makes it easier to parse through related metadata information given it will be contained in one tuple.
For example in remarks any pass that implements annotation remarks can have different type of remarks and pass additional information for each.

The original behaviour of annotation remarks is preserved here and we can mix tuple annotations and single annotations for the same instruction.

Reviewed By: paquette

Differential Revision: https://reviews.llvm.org/D148328
2023-05-09 17:51:28 +03:00
Yeting Kuo
42601e116b [ASAN] Support memory checks on vp.load/store.
The patch adds new member MaybeEVL into InterestingMemoryOperand to represent
the effective vector length for vp intrinsics. It may be extended for some target intrinsics in the future.

Reviewed By: kito-cheng

Differential Revision: https://reviews.llvm.org/D146208
2023-05-07 19:30:16 +08:00
Teresa Johnson
a4bdb27538 [MemProf] Use profiled lifetime access density directly
Now that the runtime tracks the lifetime access density directly, we can
use that directly in the threshold checks instead of less accurately
computing from other statistics.

Differential Revision: https://reviews.llvm.org/D149684
2023-05-02 15:19:34 -07:00
Christian Ulmann
a4cc7e784f [PGO] Avoid potential const_cast UB (NFC)
This commit removes potential UB in the PGO instrumentation passes that
was caused by casting away constness and then potentially modifying the
object.

Reviewed By: gysit

Differential Revision: https://reviews.llvm.org/D148903
2023-05-02 09:18:37 +00:00
Vitaly Buka
e8893133d1 Revert "[NFC][HWASAN] Handle tags as Int8"
More tests need updates.

This reverts commit e876ba5db9.
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
67caff6f32 [msan] Improve handling of Intrinsic::is_fpclass after c55fffe
c55fffe replaced fcmp with fpclass.

```
declare i1 @llvm.is.fpclass(<fptype> <op>, i32 <test>)
declare <N x i1> @llvm.is.fpclass(<vector-fptype> <op>, i32 <test>)
```

Perfect fix will require checking bits of <op> corresponding to <test>
argument. For now just propagate shadow without reporting before
intrinsic. Still existing handling of fcmp is also simple OR, so it's
not making it worse.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D149491
2023-04-28 16:27:31 -07:00
Christian Ulmann
c67079f1be [PGO] Fix dead StringRef access
This commit fixes a dead StringRef access introduced in
https://reviews.llvm.org/D149324
2023-04-27 19:42:56 +00:00
Christian Ulmann
d8e15dc4ae [PGO] Minor instrumentation code cleanup (NFC)
This commit cleans up some parts of the PGO instrumentation. Most
importantly, it removes a template parameter shadowing of a class name
that could lead to confusion.

Reviewed By: gysit

Differential Revision: https://reviews.llvm.org/D149324
2023-04-27 16:10:10 +00:00
Christian Ulmann
a8dd375cbf [PGO] Move CFGMST.h into the include directory
This commit moves the CFGMST.h file into the include directory. The
implemented algorithm is can be helpful for downstream projects that
want to use the PGO data in a non-standard way.

Reviewed By: gysit

Differential Revision: https://reviews.llvm.org/D149336
2023-04-27 14:11:04 +00:00
Ilya Leoshkevich
a3e56a8792 [KMSAN] Enable on SystemZ
Enable -fsanitize=kernel-memory support in Clang.

The x86_64 ABI requires that shadow_origin_ptr_t must be returned via a
register pair, and the s390x ABI requires that it must be returned via
memory pointed to by a hidden parameter. Normally Clang takes care of
the ABI, but the sanitizers run long after it, so unfortunately they
have to duplicate the ABI logic.

Therefore add a special case for SystemZ and manually emit the
s390x-ABI-compliant calling sequences. Since it's only 2 architectures,
do not create a VarArgHelper-like abstraction layer.

The kernel functions are compiled with the "packed-stack" and
"use-soft-float" attributes. For the "packed-stack" functions, it's not
correct for copyRegSaveArea() to copy 160 bytes of shadow and origins,
since the save area is dynamically sized. Things are greatly simplified
by the fact that the vararg "use-soft-float" functions use precisely
56 bytes in order to save the argument registers to where va_arg() can
find them.

Make copyRegSaveArea() copy only 56 bytes in the "use-soft-float" case.
The "packed-stack" && !"use-soft-float" case has no practical uses at
the moment, so leave it for the future.

Add tests.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D148596
2023-04-27 13:44:54 +02: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