500 Commits

Author SHA1 Message Date
Thurston Dang
7002ecb4c6
[msan] Convert vector shadow to scalar before zext (#96722)
zext does not allow converting vector shadow to scalar, so we must
manually convert it prior to calling zext in materializeOneCheck, for
which the 'ConvertedShadow' parameter isn't actually guaranteed to be
scalar (1). Note that it is safe/no-op to call convertShadowToScalar on
a shadow that is already scalar.

In contrast, the storeOrigin function already converts the (potentially
vector) shadow to scalar; we add a comment to note why it is load
bearing.

(1) In materializeInstructionChecks():
"// Disable combining in some cases. TrackOrigins checks each shadow to
pick
 // correct origin.
 bool Combine = !MS.TrackOrigins;
 ...
       if (!Combine) {
        materializeOneCheck(IRB, ConvertedShadow, ShadowData.Origin);
        continue;
      }"
2024-07-03 12:40:12 -07:00
Kazu Hirata
4b28b3fae4
[Transforms] Use range-based for loops (NFC) (#97195) 2024-07-02 16:20:44 -07:00
Nikita Popov
9df71d7673
[IR] Add getDataLayout() helpers to Function and GlobalValue (#96919)
Similar to https://github.com/llvm/llvm-project/pull/96902, this adds
`getDataLayout()` helpers to Function and GlobalValue, replacing the
current `getParent()->getDataLayout()` pattern.
2024-06-28 08:36:49 +02:00
Vitaly Buka
34aa6c5d9a [msan] Handle blendv intrinsics (#94882)
blendvs are very similar to select, so we adjust
arguments and forward them into select handler.
2024-06-12 19:44:18 -07:00
Vitaly Buka
3bd9d4dedf
[msan] Implement shadow propagation for _mm_dp_pd, _mm_dp_ps, _mm256_dp_ps (#94875)
Default intrinsic handling was to report any
uninitialized part of argument. However intrinsics
use mask which allow to ignore parts of input, so
it's OK to have vectors partially initialized.
2024-06-11 22:48:40 -07:00
NMiehlbradt
1b66306c9c
[KMSAN] Enable on PowerPC64 (#73611)
Enable -fsanitize=kernel-memory support in Clang.

Add tests.

---------

Co-authored-by: Nicholas Miehlbradt <nicholas@linux.ibm.com>
2024-06-12 13:32:39 +08:00
Vitaly Buka
983bf65794
[NFC][msan] Extract handleSelectLikeInst (#94881)
`blendv` instructions are very similar to `select`.
We will add support for them in followup patches.
2024-06-10 13:12:00 -07:00
Vitaly Buka
4f416989d8
[NFC][msan] Prepare function to extract main logic (#94880) 2024-06-10 13:05:05 -07:00
Vitaly Buka
3016c0636f
[NFCI][msan] Use IntPtr for vscales origin for consistency (#90920) 2024-05-02 17:23:57 -07:00
Vitaly Buka
83fdcf234f
[msan] Fix vscale alloca poisoning (#90912) 2024-05-02 16:44:10 -07:00
Vitaly Buka
a2be1b8d03
[msan] Don't modify CFG iterating it (#90691)
In rare cases `SplitBlockAndInsertSimpleForLoop` in `paintOrigin`
crashes outsize iterators.

Somehow existing `SplitBlockAndInsertIfThen` do not invalidate
iterators.
2024-05-01 14:47:00 -07:00
Vitaly Buka
8cf0f9ab2f
[msan] Add conservative handling of vscale params (#90167)
Msan uses `__msan_param_tls` to pass shadow of
arguments. Position of arguments is expected to be
available during compile time, if size of the
argument is know. This is not true for vscale.

As work around we require that vscale parameters
are always initialized, then we don't need to pass
shadow.

Ret val should work out of the box as we don't
need to know size compile time.
2024-04-26 15:26:57 -07:00
Vitaly Buka
21b84928f9
[msan] Don't crash in CreateShadowCast on vscale (#90126)
Code expects `VectorOrPrimitiveTypeSizeInBits` compile time value,
which is not available for vscale.
    
In trivial case of the same type we need to do nothing.
2024-04-25 21:29:24 -07:00
Vitaly Buka
4f4ebee10e
[msan] Eliminate non-deterministic behavior in the pass (#89831)
Almost NFC, instrumentation is as correct as it was before.

We need InstrumentationList grouped by origin instruction,
so we used stable_sort. However these objects already grouped
because we never interleave sequences of `insertShadowCheck`
of different instrunction.

Pointer sort has artifact that it was deppendent on allocator behavior,
so we could inserted checks in a different order.

There is no test, as I failed to reproduce this with `opt`. My guess
is that for reproducer we need to increase fragmentation in the
allocator.
2024-04-23 16:19:47 -07:00
Vitaly Buka
1d14034873 [NFC][msan] Add DebugInstrumentInstruction DEBUG_COUNTER 2024-04-23 11:20:50 -07:00
Vitaly Buka
06cc1754f8 [NFC][msan] Fix typo in comment 2024-04-23 11:20:50 -07:00
Kazu Hirata
d674f45d51
[Transforms] Remove extraneous ArrayRef (NFC) (#89535)
We don't need to create these instances of ArrayRef because
ConstantDataVector::get takes ArrayRef, and ArrayRef can be implicitly
constructed from C arrays.
2024-04-21 08:21:23 -07:00
Vitaly Buka
c60aa430dc
[NFCI][sanitizers][metadata] Exctract create{Unlikely,Likely}BranchWeights (#89464)
We have a lot of repeated code with random constants.
Particular values are not important, the one just needs to be
bigger then another.

UR_NONTAKEN_WEIGHT is selected as it's the most common one.
2024-04-19 17:03:23 -07:00
Harald van Dijk
60de56c743
[ValueTracking] Restore isKnownNonZero parameter order. (#88873)
Prior to #85863, the required parameters of llvm::isKnownNonZero were
Value and DataLayout. After, they are Value, Depth, and SimplifyQuery,
where SimplifyQuery is implicitly constructible from DataLayout. The
change to move Depth before SimplifyQuery needed callers to be updated
unnecessarily, and as commented in #85863, we actually want Depth to be
after SimplifyQuery anyway so that it can be defaulted and the caller
does not need to specify it.
2024-04-16 15:21:09 +01:00
Yingwei Zheng
e0a628715a
[ValueTracking] Convert isKnownNonZero to use SimplifyQuery (#85863)
This patch converts `isKnownNonZero` to use SimplifyQuery. Then we can
use the context information from `DomCondCache`.

Fixes https://github.com/llvm/llvm-project/issues/85823.
Alive2: https://alive2.llvm.org/ce/z/QUvHVj
2024-04-12 23:47:20 +08:00
Evgenii Stepanov
e72c949c15
[msan] Overflow intrinsics. (#88210) 2024-04-10 09:12:25 -07:00
Evgenii Stepanov
5bc87dac75 Revert "Overflow and saturating intrinsics (#88068)"
This reverts commit 118a5d8236d8a483dd401fa35c8b1fcd058eacc1.
2024-04-08 17:02:21 -07:00
Evgenii Stepanov
118a5d8236
Overflow and saturating intrinsics (#88068) 2024-04-08 16:33:45 -07:00
Fangrui Song
9b91c54d9b
[msan] Unpoison indirect outputs for userspace using memset for large operands (#79924)
Modify #77393 to clear shadow memory using `llvm.memset.*` when the size
is large, similar to `shouldUseBZeroPlusStoresToInitialize` in clang for
`-ftrivial-auto-var-init=`. The intrinsic, if lowered to libcall, will
use the msan interceptor.

The instruction selector lowers a `StoreInst` to multiple stores, not
utilizing `memset`. When the size is large (e.g.
`store { [100 x i32] } zeroinitializer, ptr %12, align 1`), the
generated code will be long (and `CodeGenPrepare::optimizeInst` will
even crash for a huge size).

```
// Test stack size
template <class T>
void DoNotOptimize(const T& var) { // deprecated by https://github.com/google/benchmark/pull/1493
  asm volatile("" : "+m"(const_cast<T&>(var)));
}

int main() {
  using LargeArray = std::array<int, 1000000>;
  auto large_stack = []() { DoNotOptimize(LargeArray()); };
  /////// CodeGenPrepare::optimizeInst triggers an assertion failure when creating an integer type with a bit width>2**23
  large_stack();
}
```
2024-01-30 13:45:47 -08:00
Fangrui Song
1ae0448ed3
[msan] Enable msan-handle-asm-conservative for userspace by default (#79251)
msan-handle-asm-conservative is enabled by KMSAN by default.
Enable the userspace by default as well after #77393.
2024-01-24 15:31:43 -08:00
Fangrui Song
c71a5bf940
[msan] Unpoison indirect outputs for userspace when -msan-handle-asm-conservative is specified (#77393)
KMSAN defaults to `msan-handle-asm-conservative`, which inserts
`__msan_instrument_asm_store` calls to unpoison indirect outputs in
inline assembly (e.g. `=m` constraints in source).

```c
unsigned f() {
  unsigned v;
  // __msan_instrument_asm_store unpoisons v before invoking the asm.
  asm("movl $1,%0" : "=m"(v));
  return v;
}
```

Extend the mechanism to userspace, but require explicit
`-mllvm -msan-handle-asm-conservative` for experiments for now.

As

https://docs.kernel.org/dev-tools/kmsan.html#inline-assembly-instrumentation
says, this approach may mask certain errors (an indirect output may not
actually be initialized), but it also helps to avoid a lot of false
positives.

Link: https://github.com/google/sanitizers/issues/192
2024-01-19 16:18:28 -08:00
Nikita Popov
6c2fbc3a68
[IRBuilder] Add CreatePtrAdd() method (NFC) (#77582)
This abstracts over the common pattern of creating a gep with i8 element
type.
2024-01-12 14:21:21 +01:00
Vitaly Buka
66e9429e75
[msan][aarch64] Improve argument classification
Arm64 use multiple registers (varg slots) to pass arrays.

Reviewers: kstoimenov, thurstond

Reviewed By: thurstond

Pull Request: https://github.com/llvm/llvm-project/pull/72728
2023-11-17 17:01:34 -08:00
Vitaly Buka
e7f350951b
[msan][aarch64] Fix cleanup of unused part of overflow area
Similar to a05e736d288a7f2009ee9d057e78713d9adeeb5f.

Reviewers: thurstond, kstoimenov

Reviewed By: thurstond

Pull Request: https://github.com/llvm/llvm-project/pull/72722
2023-11-17 16:48:05 -08:00
Vitaly Buka
a05e736d28
[msan][x86] Fix shadow if vararg overflow beyond kParamTLSSize
Caller puts argument shadow one by one into __msan_va_arg_tls, until it
reaches kParamTLSSize. After that it still increment OverflowOffset but
does not store the shadow.

Callee needs OverflowOffset to prepare a shadow for the entire overflow
area. It's done by creating "varargs shadow copy" for complete list of
args, copying available shadow from __msan_va_arg_tls, and clearing the
rest.

However callee does not know if the tail of __msan_va_arg_tls was not
able to fit an argument, and callee will copy tail shadow into "varargs
shadow copy", and later used as a shadow for an omitted argument.

So that unused tail of the __msan_va_arg_tls must be cleared if left
unused.

This allows us to enable compiler-rt/test/msan/vararg_shadow.cpp for
x86.

Reviewers: kstoimenov, thurstond

Reviewed By: thurstond

Pull Request: https://github.com/llvm/llvm-project/pull/72707
2023-11-17 15:13:11 -08:00
Vitaly Buka
a30e9a1a57 [NFC][msan] Fix formating 2023-11-17 14:31:44 -08:00
Vitaly Buka
fbb2d9383c
[msan][x86] Fix shadow for FP80 or long double (#72706)
FP80 is passed using stack.
2023-11-17 14:29:18 -08:00
Vitaly Buka
341ca1ad0c
[test][msan] s390x already passes the test
3bc439bdff8bb5518098bd9ef52c56ac071276bc implemented overflow copying in a different way.

It's lucky to pass this test, but may fails in a different way.

Reviewers: thurstond, iii-i

Reviewed By: thurstond

Pull Request: https://github.com/llvm/llvm-project/pull/72710
2023-11-17 14:26:41 -08:00
Vitaly Buka
07d4680dc1 [NFC][msan] Remove unused parameter from getOriginPtrForVAArgument (#72687) 2023-11-17 11:30:00 -08:00
Vitaly Buka
9052ac954d
[NFCI][msan] Reduce code duplication by extracting VarArgHelperBase
Reviewers: thurstond, kstoimenov

Reviewed By: thurstond, kstoimenov

Pull Request: https://github.com/llvm/llvm-project/pull/72686
2023-11-17 11:25:11 -08: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
Vitaly Buka
4c08cbee7b [NFC][msan] Fix typo in comment 2023-11-14 20:52:41 -08:00
Vitaly Buka
77048378ff [NFC][msan] Fix misleading comments
These variables are used by other platforms as well.
2023-11-14 15:42:53 -08:00
Alexander Potapenko
f577bfb995
[sanitizer][msan] fix AArch64 vararg support for KMSAN (#70660)
Cast StackSaveAreaPtr, GrRegSaveAreaPtr, VrRegSaveAreaPtr to pointers to
fix assertions in getShadowOriginPtrKernel().

Fixes: https://github.com/llvm/llvm-project/issues/69738

Patch by Mark Johnston.
2023-11-10 09:33:49 +01: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
Kazu Hirata
3b7bfeb483 [llvm] Stop including llvm/ADT/SmallString.h (NFC)
Identified with misc-include-cleaner.
2023-10-22 10:42:15 -07:00
Youngsuk Kim
e5026f0179 [llvm] Remove uses of Type::getPointerTo() (NFC)
Partial progress towards removing in-tree uses of `getPointerTo()`,
by employing the following options:

* Drop the call entirely if the sole purpose of it is to support a no-op
  bitcast (remove the no-op bitcast as well).

* Replace with `PointerType::get()`/`PointerType::getUnqual()`

This is a NFC cleanup effort.

Reviewed By: barannikov88

Differential Revision: https://reviews.llvm.org/D155232
2023-09-22 19:44:38 -04:00
zhanglimin
ec42c78cc4 [sanitizer][msan] VarArgHelper for loongarch64
This patch adds support for variadic argument for loongarch64,
which is based on MIPS64. And `check-msan` all pass.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D158587
2023-09-12 09:51:18 +08:00
Bjorn Pettersson
2c85b24fb8 [msan] Simplify code based on opaque pointers
Stop using Type::getPointerTo as we do not need typed pointers
nowadays.

Remove no longer used arguments to getOriginPtrForArgument,
getShadowPtrForRetval and getOriginPtrForRetval.
2023-08-13 16:46:56 +02:00
Bjorn Pettersson
4ce7c4a92a [llvm] Drop some typed pointer handling/bitcasts
Differential Revision: https://reviews.llvm.org/D157016
2023-08-03 22:54:33 +02:00
Bjorn Pettersson
fd05c34b18 Stop using legacy helpers indicating typed pointer types. NFC
Since we no longer support typed LLVM IR pointer types, the code can
be simplified into for example using PointerType::get directly instead
of using Type::getInt8PtrTy and Type::getInt32PtrTy etc.

Differential Revision: https://reviews.llvm.org/D156733
2023-08-02 12:08:37 +02: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
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
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
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