Commit Graph

432 Commits

Author SHA1 Message Date
Nikita Popov 304f1d59ca [IR] Switch everything to use memory attribute
This switches everything to use the memory attribute proposed in
https://discourse.llvm.org/t/rfc-unify-memory-effect-attributes/65579.
The old argmemonly, inaccessiblememonly and inaccessiblemem_or_argmemonly
attributes are dropped. The readnone, readonly and writeonly attributes
are restricted to parameters only.

The old attributes are auto-upgraded both in bitcode and IR.
The bitcode upgrade is a policy requirement that has to be retained
indefinitely. The IR upgrade is mainly there so it's not necessary
to update all tests using memory attributes in this patch, which
is already large enough. We could drop that part after migrating
tests, or retain it longer term, to make it easier to import IR
from older LLVM versions.

High-level Function/CallBase APIs like doesNotAccessMemory() or
setDoesNotAccessMemory() are mapped transparently to the memory
attribute. Code that directly manipulates attributes (e.g. via
AttributeList) on the other hand needs to switch to working with
the memory attribute instead.

Differential Revision: https://reviews.llvm.org/D135780
2022-11-04 10:21:38 +01:00
Kevin Athey 31bfa4a69b [MSAN] Add handleCountZeroes for ctlz and cttz.
This addresses a bug where vector versions of ctlz are creating false positive reports.

Depends on D136369

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D136523
2022-10-24 17:31:34 -07:00
Vitaly Buka ba39a6e14a [msan] Instrument vtest instrinsics
Instrumentation just ORs shadow of inputs.
I assume some result shadow bits can be reset if we go into specifics of particular checks,
but as-is it is still an improvement against existing default strict instruction handler, when
every set bit of input shadow is  reported as an error.

Reviewed By: kda

Differential Revision: https://reviews.llvm.org/D134123
2022-09-21 16:57:44 -07:00
Vitaly Buka 6fd959d625 [msan] Handle x86_avx_cmp_pd_256 and x86_avx_cmp_ps_256
Removed FIXME which looks irrelevant. The error message happens only without -mattr=+avx.
E.g.
GOOD: opt llvm/test/Instrumentation/MemorySanitizer/avx-intrinsics-x86.ll -passes=msan -o - | llc -O3 -o /dev/null -mattr=+avx
BAD: opt llvm/test/Instrumentation/MemorySanitizer/avx-intrinsics-x86.ll -passes=msan -o - | llc -O3 -o /dev/null

So nothing to fix here.

Reviewed By: RKSimon

Differential Revision: https://reviews.llvm.org/D134119
2022-09-21 15:17:02 -07:00
Vitaly Buka 4fa8df20ff [msan] Handle shadow of masked instruction
Origin handling is not implemented yet.

Reviewed By: kda

Differential Revision: https://reviews.llvm.org/D133682
2022-09-19 17:57:43 -07:00
Vitaly Buka 6f3276d57e [msan] Check mask and pointers shadow
Msan has default handler for unknown instructions which
previously applied to these as well. However depending on
mask, not all pointers or passthru part will be used. This
allows other passes to insert undef into sum arguments.
As result,  default strict instruction handler can produce false reports.

Reviewed By: kda, kstoimenov

Differential Revision: https://reviews.llvm.org/D133678
2022-09-19 13:09:56 -07:00
Vitaly Buka f0c2ffa8f8 [msan] Add msan-insert-check DEBUG_COUNTER 2022-09-15 21:52:58 -07:00
Vitaly Buka f221720e82 [nfc][msan] getShadowOriginPtr on <N x ptr>
Some vector instructions can benefit from
of Addr as <N x ptr>.

Differential Revision: https://reviews.llvm.org/D133681
2022-09-14 19:18:52 -07:00
Vitaly Buka f404169f24 [NFC][msan] Rename variables to match definition 2022-09-14 19:16:27 -07:00
Vitaly Buka 2209be15a5 [NFC][msan] Convert some code to early returns
Reviewed By: kda

Differential Revision: https://reviews.llvm.org/D133673
2022-09-14 19:16:11 -07:00
Vitaly Buka bcf3d666b4 [NFC][msan] Simplify llvm.masked.load origin code
Reviewed By: kda

Differential Revision: https://reviews.llvm.org/D133652
2022-09-14 19:14:29 -07:00
Vitaly Buka d421223e25 [msan] Resolve FIXME from D133880
We don't need to change tests we convertToBool
unconditionally only before OR.
2022-09-14 18:55:57 -07:00
Vitaly Buka bf204881b6 [msan] Change logic of ClInstrumentationWithCallThreshold
According to logs, ClInstrumentationWithCallThreshold is workaround
for slow backend with large number of basic blocks.
However, I can't reproduce that one, but I see significant slowdown
after ClCheckConstantShadow. Without ClInstrumentationWithCallThreshold
compiler is able to eliminate many of the branches.

So maybe we should drop ClInstrumentationWithCallThreshold completly.

For now I just change the logic to ignore constant shadow so it will
not trigger callback fallback too early.

Reviewed By: kstoimenov

Differential Revision: https://reviews.llvm.org/D133880
2022-09-14 14:58:12 -07:00
Vitaly Buka b51d1f1fbd [msan] Don't deppend on argumens evaluation order 2022-09-10 15:28:32 -07:00
Vitaly Buka 71c5e7b26a [msan] Do not deppend on arguments evaluation order
Clang and GCC do this differently making IR inconsistent.
https://lab.llvm.org/buildbot#builders/6/builds/13120
2022-09-10 13:50:32 -07:00
Vitaly Buka 1819d5999c [NFC][msan] Remove unused return type 2022-09-10 12:20:54 -07:00
Vitaly Buka 6fc31712f1 [msan] Relax handling of llvm.masked.expandload and llvm.masked.gather
This is work around for new false positives. Real implementation will
follow.
2022-09-10 12:19:16 -07:00
Vitaly Buka 1cf5c7fe8c [msan] Disambiguate warnings debug location
If multiple warnings created on the same instruction (debug location)
it can be difficult to figure out which input value is the cause.

This patches chains origins just before the warning using last origins
update debug information.

To avoid inflating the binary unnecessarily, do this only when uncertainty is
high enough, 3 warnings by default. On average it adds 0.4% to the
.text size.

Reviewed By: kda, fmayer

Differential Revision: https://reviews.llvm.org/D133232
2022-09-08 14:17:07 -07:00
Vitaly Buka 0f2f1c2be1 [sanitizers] Invalidate GlobalsAA
GlobalsAA is considered stateless as usually transformations do not introduce
new global accesses, and removed global access is not a problem for GlobalsAA
users.
Sanitizers introduce new global accesses:
 - Msan and Dfsan tracks origins and parameters with TLS, and to store stack origins.
  - Sancov uses global counters. HWAsan store tag state in TLS.
  - Asan modifies globals, but I am not sure if invalidation is required.

I see no evidence that TSan needs invalidation.

Reviewed By: aeubanks

Differential Revision: https://reviews.llvm.org/D133394
2022-09-08 14:00:43 -07:00
Vitaly Buka 5e38b2a456 [NFC][msan] Rename ModuleMemorySanitizerPass 2022-09-06 20:30:35 -07:00
Vitaly Buka e7bac3b9fa [msan] Convert Msan to ModulePass
MemorySanitizerPass function pass violatied requirement 4 of function
pass to do not insert globals. Msan nees to insert globals for origin
tracking, and paramereters tracking.

https://llvm.org/docs/WritingAnLLVMPass.html#the-functionpass-class

Reviewed By: kstoimenov, fmayer

Differential Revision: https://reviews.llvm.org/D133336
2022-09-06 15:01:04 -07:00
Vitaly Buka ad3a77df2d [msan] Fix debug info with getNextNode
When we want to add instrumentation after
an instruction, instrumentation still should
keep debug info of the instruction.

Reviewed By: kda, kstoimenov

Differential Revision: https://reviews.llvm.org/D133091
2022-09-01 20:13:56 -07:00
Vitaly Buka ad2b356f85 [msan] Use no-origin functions when possible
Saves 1.8% of .text size on CTMark

Reviewed By: kda

Differential Revision: https://reviews.llvm.org/D133077
2022-09-01 19:18:38 -07:00
Vitaly Buka ef0f866718 [msan] Combine shadow check of the same instruction
Reduces .text size by 1% on our large binary.

On CTMark (-O2 -fsanitize=memory -fsanitize-memory-use-after-dtor -fsanitize-memory-param-retval)
Size -0.4%
Time -0.8%

Reviewed By: kda

Differential Revision: https://reviews.llvm.org/D133071
2022-09-01 13:55:59 -07:00
Vitaly Buka 9110673062 [nfc][msan] Group checks per instruction
It's a preparation of to combine shadow checks of the same instruction

Reviewed By: kda, kstoimenov

Differential Revision: https://reviews.llvm.org/D133065
2022-09-01 13:10:16 -07:00
Jordan Rupprecht 3031a250de [MSan] Fix determinism issue when using msan-track-origins.
When instrumenting `alloca`s, we use a `SmallSet` (i.e. `SmallPtrSet`). When there are fewer elements than the `SmallSet` size, it behaves like a vector, offering stable iteration order. Once we have too many `alloca`s to instrument, the iteration order becomes unstable. This manifests as non-deterministic builds because of the global constant we create while instrumenting the alloca.

The test added is a simple IR file, but was discovered while building `libcxx/src/filesystem/operations.cpp` from libc++. A reduced C++ example from that:

```
// clang++ -fsanitize=memory -fsanitize-memory-track-origins \
//   -fno-discard-value-names -S -emit-llvm \
//   -c op.cpp -o op.ll
struct Foo {
  ~Foo();
};
bool func1(Foo);
void func2(Foo);
void func3(int) {
  int f_st, t_st;
  Foo f, t;
  func1(f) || func1(f) || func1(t) || func1(f) && func1(t);
  func2(f);
}
```

Reviewed By: kda

Differential Revision: https://reviews.llvm.org/D133034
2022-09-01 09:15:57 -07:00
Vitaly Buka 53d1ae88f8 [nfc][msan] Prepare the code for check sorting 2022-08-31 15:36:49 -07:00
Kazu Hirata b18ff9c461 [Transform] Use range-based for loops (NFC) 2022-08-27 23:54:32 -07:00
Vitaly Buka 0d59969abb [msan] Enable msan-check-constant-shadow by default
Depends on D132761.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D132765
2022-08-26 16:34:47 -07:00
Vitaly Buka 134986a720 [msan] Fix handling of constant shadow
If constant shadown enabled we had false reports because
!isZeroValue() does not guaranty that the values is actually not zero.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D132761
2022-08-26 15:51:02 -07:00
Vitaly Buka 072a2fd738 [NFC][msan] Clang-format the file 2022-08-26 15:11:12 -07:00
Keno Fischer 30d7d74d5c [MSAN] Handle array alloca with non-i64 size specification
The array size specification of the an alloca can be any integer,
so zext or trunc it to intptr before attempting to multiply it
with an intptr constant.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D131846
2022-08-24 03:24:21 +00:00
Keno Fischer 5739d29cde [MSAN] Correct shadow type for atomicrmw instrumentation
We were passing the type of `Val` to `getShadowOriginPtr`, rather
than the type of `Val`'s shadow resulting in broken IR. The fix
is simple.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D131845
2022-08-24 03:24:19 +00:00
Kevin Athey 532564de17 [MSAN] add flag to suppress storage of stack variable names with -sanitize-memory-track-origins
Allows for even more savings in the binary image while simultaneously removing the name of the offending stack variable.

Depends on D131631

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D131728
2022-08-12 11:59:53 -07:00
Kevin Athey ec277b67eb [MSAN] Separate id ptr from constant string for variable names used in track origins.
The goal is to reduce the size of the MSAN with track origins binary, by making
the variable name locations constant which will allow the linker to compress
them.

Follows: https://reviews.llvm.org/D131415

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D131631
2022-08-12 08:47:36 -07:00
Marco Elver c47ec95531 [MemorySanitizer] Support memcpy.inline and memset.inline
Other sanitizers (ASan, TSan, see added tests) already handle
memcpy.inline and memset.inline by not relying on InstVisitor to turn
the intrinsics into calls. Only MSan instrumentation currently does not
support them due to missing InstVisitor callbacks.

Fix it by actually making InstVisitor handle Mem*InlineInst.

While the mem*.inline intrinsics promise no calls to external functions
as an optimization, for the sanitizers we need to break this guarantee
since access into the runtime is required either way, and performance
can no longer be guaranteed. All other cases, where generating a call is
incorrect, should instead use no_sanitize.

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

Reviewed By: vitalybuka, dvyukov

Differential Revision: https://reviews.llvm.org/D131577
2022-08-11 10:43:49 +02:00
Kevin Athey 057cabd997 Remove function name from sanitize-memory-track-origins binary.
This work is being done to reduce the size of MSAN with track origins binary.

Builds upon: https://reviews.llvm.org/D131205

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D131415
2022-08-10 15:45:40 -07:00
Kevin Athey d7a47a9bb5 Desist from passing function location to __msan_set_alloca_origin4.
This is done by calling __msan_set_alloca_origin and providing the location of the variable by using the call stack.
This is prepatory work for dropping variable names when track-origins is enabled.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D131205
2022-08-10 09:02:53 -07:00
Kazu Hirata bf6021709a Use drop_begin (NFC) 2022-07-31 15:17:09 -07:00
Andrew Turner b850762b62 Add the FreeBSD AArch64 memory layout
Use the FreeBSD AArch64 memory layout values when building for it.
These are based on the x86_64 values, scaled to take into account the
larger address space on AArch64.

Reviewed by: vitalybuka

Differential Revision: https://reviews.llvm.org/D125883
2022-07-19 09:58:07 -04:00
Guillaume Chatelet 589c8d6fb9 [NFC] Simplify alignment code in MemorySanitizer 2022-06-20 15:15:53 +00:00
Guillaume Chatelet 45a5cd41e5 [NFC][Alignment] Simplify code in MemorySanitizer 2022-06-13 13:36:36 +00:00
Enna1 52992f136b Add !nosanitize to FixedMetadataKinds
This patch adds !nosanitize metadata to FixedMetadataKinds.def, !nosanitize indicates that LLVM should not insert any sanitizer instrumentation.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D126294
2022-05-27 09:46:13 +08:00
Nicolas Capens c153c61fad Handle instrumentation of scalar single-precision (_ss) intrinsics
Instrumentation of scalar double-precision intrinsics such as
x86_sse41_round_sd was already handled by https://reviews.llvm.org/D82398,
but not their single-precision counterparts.

https://issuetracker.google.com/172238865

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D124871
2022-05-19 13:56:51 -07:00
serge-sans-paille 7030654296 [iwyu] Handle regressions in libLLVM header include
Running iwyu-diff on LLVM codebase since fa5a4e1b95 detected a few
regressions, fixing them.

Differential Revision: https://reviews.llvm.org/D124847
2022-05-04 08:32:38 +02:00
Fangrui Song d133538b8b [LegacyPM] Remove MemorySanitizerLegacyPass
Using the legacy PM for the optimization pipeline was deprecated in 13.0.0.
Following recent changes to remove non-core features of the legacy
PM/optimization pipeline, remove MemorySanitizerLegacyPass.

Differential Revision: https://reviews.llvm.org/D123894
2022-04-21 10:21:46 -07:00
Vitaly Buka 79fa8be4ae [NFC][msan] Switch pointer to a reference 2022-04-12 18:45:50 -07:00
Nikita Popov 0adadfa68f [MSan] Ensure argument shadow initialized on memcpy
We need to explicitly query the shadow here, because it is lazily
initialized for byval arguments. Without opaque pointers this used to
mostly work out, because there would be a bitcast to `i8*` present, and
that would query, and copy in case of byval, the argument shadow.

Reviewed By: vitalybuka, eugenis

Differential Revision: https://reviews.llvm.org/D123602
2022-04-12 14:53:02 -07:00
Vitaly Buka efdc90baaa Revert "[MSan] Ensure argument shadow initialized on memcpy"
Invalid author.

This reverts commit 163a9f4552.
2022-04-12 14:53:02 -07:00
Vitaly Buka 163a9f4552 [MSan] Ensure argument shadow initialized on memcpy
We need to explicitly query the shadow here, because it is lazily
initialized for byval arguments. Without opaque pointers this used to
mostly work out, because there would be a bitcast to `i8*` present, and
that would query, and copy in case of byval, the argument shadow.

Reviewed By: vitalybuka, eugenis

Differential Revision: https://reviews.llvm.org/D123602
2022-04-12 14:49:52 -07:00