Commit Graph

195 Commits

Author SHA1 Message Date
Kazu Hirata 343de6856e [Transforms] Use std::nullopt instead of None (NFC)
This patch mechanically replaces None with std::nullopt where the
compiler would warn if None were deprecated.  The intent is to reduce
the amount of manual work required in migrating from Optional to
std::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-02 21:11:37 -08:00
Kazu Hirata 589725f6e8 [llvm] Use std::size (NFC)
std::size, introduced in C++17, allows us to directly obtain the
number of elements of an array.
2022-11-26 13:47:32 -08:00
Guillaume Chatelet e647b4f519 [reland][Alignment][NFC] Use the Align type in MCSection
Differential Revision: https://reviews.llvm.org/D138653
2022-11-24 13:19:18 +00:00
Guillaume Chatelet 3467f9c7d6 Revert D138653 [Alignment][NFC] Use the Align type in MCSection"
This breaks the bolt project.
This reverts commit 409f0dc4a4.
2022-11-24 12:42:30 +00:00
Guillaume Chatelet 409f0dc4a4 [Alignment][NFC] Use the Align type in MCSection
Differential Revision: https://reviews.llvm.org/D138653
2022-11-24 12:32:58 +00:00
Fangrui Song 87d98a7300 [hwasan] Remove no-op setDSOLocal. NFC
PrivateLinkage and HiddenVisibility are implicitly dso_local.
2022-11-02 00:17:16 -07:00
Florian Mayer e06c9b63bc [NFC] [HWASan] remove unnecessary cast 2022-09-28 17:48:19 -07:00
Florian Mayer 0401dc2913 [MTE] [HWASan] unify isInterestingAlloca
Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D134779
2022-09-28 15:52:34 -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
Kazu Hirata e20d210eef [llvm] Qualify auto (NFC)
Identified with readability-qualified-auto.
2022-08-07 23:55:27 -07:00
Kazu Hirata 0e37ef0186 [Transforms] Fix comment typos (NFC) 2022-08-07 23:55:24 -07:00
Leonard Chan 21f72c05c4 [hwasan] Add __hwasan_add_frame_record to the hwasan interface
Hwasan includes instructions in the prologue that mix the PC and SP and store
it into the stack ring buffer stored at __hwasan_tls. This is a thread_local
global exposed from the hwasan runtime. However, if TLS-mechanisms or the
hwasan runtime haven't been setup yet, it will be invalid to access __hwasan_tls.
This is the case for Fuchsia where we instrument libc, so some functions that
are instrumented but can run before hwasan initialization will incorrectly
access this global. Additionally, libc cannot have any TLS variables, so we
cannot weakly define __hwasan_tls until the runtime is loaded.

A way we can work around this is by moving the instructions into a hwasan
function that does the store into the ring buffer and creating a weak definition
of that function locally in libc. This way __hwasan_tls will not actually be
referenced. This is not our long-term solution, but this will allow us to roll
out hwasan in the meantime.

This patch includes:

- A new llvm flag for choosing to emit a libcall rather than instructions in the
  prologue (off by default)
- The libcall for storing into the ringbuffer (__hwasan_add_frame_record)

Differential Revision: https://reviews.llvm.org/D128387
2022-07-13 15:15:15 -07:00
Leonard Chan d843d5c8e6 Revert "[hwasan] Add __hwasan_record_frame_record to the hwasan interface"
This reverts commit 4956620387.

This broke a sanitizer builder: https://lab.llvm.org/buildbot/#/builders/77/builds/19597
2022-07-13 15:06:07 -07:00
leonardchan 4956620387 [hwasan] Add __hwasan_record_frame_record to the hwasan interface
Hwasan includes instructions in the prologue that mix the PC and SP and store
it into the stack ring buffer stored at __hwasan_tls. This is a thread_local
global exposed from the hwasan runtime. However, if TLS-mechanisms or the
hwasan runtime haven't been setup yet, it will be invalid to access __hwasan_tls.
This is the case for Fuchsia where we instrument libc, so some functions that
are instrumented but can run before hwasan initialization will incorrectly
access this global. Additionally, libc cannot have any TLS variables, so we
cannot weakly define __hwasan_tls until the runtime is loaded.

A way we can work around this is by moving the instructions into a hwasan
function that does the store into the ring buffer and creating a weak definition
of that function locally in libc. This way __hwasan_tls will not actually be
referenced. This is not our long-term solution, but this will allow us to roll
out hwasan in the meantime.

This patch includes:

- A new llvm flag for choosing to emit a libcall rather than instructions in the
  prologue (off by default)
- The libcall for storing into the ringbuffer (__hwasan_record_frame_record)

Differential Revision: https://reviews.llvm.org/D128387
2022-07-14 05:07:11 +08:00
Leonard Chan 0f589826a3 [hwasan] Refactor frame record info into function
This way it can be reused easily in D128387.

Note this changes the IR slightly. Before The steps for calculating and storing the frame record info were:

1. getPC
2. getSP
3. inttoptr
4. or SP, PC
5. store

Now the steps are:

1. getPC
2. getSP
3. or SP, PC
4. inttoptr
5. store

Differential Revision: https://reviews.llvm.org/D129315
2022-07-07 14:44:39 -07:00
Leonard Chan 9553d69580 [NFC][HWASan] Refactor hwasan pass
This moves some code for getting PC and SP into their own functions. Since SP
is also retrieved in the prologue and getting the stack tag, we can cache the
SP if we get it once in the prologue. This caching will really only be relevant
in D128387 where StackBaseTag may not be set in the prologue if __hwasan_tls
is not used.

Differential Revision: https://reviews.llvm.org/D128551
2022-06-28 12:09:20 -07:00
Mitch Phillips f57066401e [HWASan] Use new IR attribute for communicating unsanitized globals.
Globals that shouldn't be sanitized are currently communicated to HWASan
through the use of the llvm.asan.globals IR metadata. Now that we have
an on-GV attribute, use it.

Reviewed By: pcc

Differential Revision: https://reviews.llvm.org/D127543
2022-06-24 12:04:11 -07:00
Florian Mayer 9320a32bb9 [MTE] [HWASan] Use LoopInfo for reachability queries.
The reachability queries default to "reachable" after exploring too many
basic blocks. LoopInfo helps it skip over the whole loop.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D127917
2022-06-22 15:28:49 -07:00
Florian Mayer acc9721e38 [NFC] [HWASan] Remove indirection for getting analyses.
This was necessary for code reuse between the old and new passmanager.
With the old pass-manager gone, this is no longer necessary.

Reviewed By: eugenis, myhsu

Differential Revision: https://reviews.llvm.org/D127913
2022-06-22 10:53:20 -07:00
Fangrui Song 95a134254a Remove unneeded cl::ZeroOrMore for cl::opt/cl::list options 2022-06-05 01:07:51 -07:00
Fangrui Song 36c7d79dc4 Remove unneeded cl::ZeroOrMore for cl::opt options
Similar to 557efc9a8b.
This commit handles options where cl::ZeroOrMore is more than one line below
cl::opt.
2022-06-04 00:10:42 -07:00
Fangrui Song 557efc9a8b [llvm] Remove unneeded cl::ZeroOrMore for cl::opt options. NFC
Some cl::ZeroOrMore were added to avoid the `may only occur zero or one times!`
error. More were added due to cargo cult. Since the error has been removed,
cl::ZeroOrMore is unneeded.

Also remove cl::init(false) while touching the lines.
2022-06-03 21:59:05 -07: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
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 39e23bb059 [LegacyPM] Remove HWAsanSanitizerLegacyPass
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 AddressSanitizerLegacyPass...

...,
ModuleAddressSanitizerLegacyPass, and ASanGlobalsMetadataWrapperPass.

MemorySanitizerLegacyPass was removed in D123894.
AddressSanitizerLegacyPass was removed in D124216.

Reviewed By: #sanitizers, vitalybuka

Differential Revision: https://reviews.llvm.org/D124337
2022-04-25 10:21:26 -07:00
Marco Elver b8e49fdcb1 [AddressSanitizer] Allow prefixing memintrinsic calls in kernel mode
Allow receiving memcpy/memset/memmove instrumentation by using __asan or
__hwasan prefixed versions for AddressSanitizer and HWAddressSanitizer
respectively when compiling in kernel mode, by passing params
-asan-kernel-mem-intrinsic-prefix or -hwasan-kernel-mem-intrinsic-prefix.

By default the kernel-specialized versions of both passes drop the
prefixes for calls generated by memintrinsics. This assumes that all
locations that can lower the intrinsics to libcalls can safely be
instrumented. This unfortunately is not the case when implicit calls to
memintrinsics are inserted by the compiler in no_sanitize functions [1].

To solve the issue, normal memcpy/memset/memmove need to be
uninstrumented, and instrumented code should instead use the prefixed
versions. This also aligns with ASan behaviour in user space.

[1] https://lore.kernel.org/lkml/Yj2yYFloadFobRPx@lakrids/

Reviewed By: glider

Differential Revision: https://reviews.llvm.org/D122724
2022-03-31 11:14:42 +02:00
Florian Mayer 078b546555 [HWASan] do not replace lifetime intrinsics with tagged address.
Quote from the LLVM Language Reference
  If ptr is a stack-allocated object and it points to the first byte of the
  object, the object is initially marked as dead. ptr is conservatively
  considered as a non-stack-allocated object if the stack coloring algorithm
  that is used in the optimization pipeline cannot conclude that ptr is a
  stack-allocated object.

By replacing the alloca pointer with the tagged address before this change,
we confused the stack coloring algorithm.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D121835
2022-03-18 10:39:51 -07:00
Florian Mayer dbc918b649 Revert "[HWASan] do not replace lifetime intrinsics with tagged address."
Failed on buildbot:

/home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/build/bin/llc: error: : error: unable to get target for 'aarch64-unknown-linux-android29', see --version and --triple.
FileCheck error: '<stdin>' is empty.
FileCheck command line:  /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/build/bin/FileCheck /home/buildbot/buildbot-root/llvm-project/llvm/test/Instrumentation/HWAddressSanitizer/stack-coloring.ll --check-prefix=COLOR

This reverts commit 208b923e74.
2022-03-18 10:04:48 -07:00
Florian Mayer 208b923e74 [HWASan] do not replace lifetime intrinsics with tagged address.
Quote from the LLVM Language Reference
  If ptr is a stack-allocated object and it points to the first byte of the
  object, the object is initially marked as dead. ptr is conservatively
  considered as a non-stack-allocated object if the stack coloring algorithm
  that is used in the optimization pipeline cannot conclude that ptr is a
  stack-allocated object.

By replacing the alloca pointer with the tagged address before this change,
we confused the stack coloring algorithm.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D121835
2022-03-18 09:45:05 -07:00
serge-sans-paille ed98c1b376 Cleanup includes: DebugInfo & CodeGen
Discourse thread: https://discourse.llvm.org/t/include-what-you-use-include-cleanup
Differential Revision: https://reviews.llvm.org/D121332
2022-03-12 17:26:40 +01:00
Florian Mayer 0f770f4d00 [NFC] [HWASan] document why we tag Size but untag AlignedSize. 2022-03-09 16:18:04 -08:00
Florian Mayer 4bfd8a2c5f [NFC] [MTE] [HWASan] fixed orphaned comments. 2022-03-08 16:42:31 -08:00
Florian Mayer af22478933 [NFC] [MTE] [HWASan] simply code. 2022-03-08 16:36:10 -08:00
Florian Mayer 1d730d80ce [HWASAN] erase lifetime intrinsics if tag is outside.
Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D120437
2022-03-01 14:47:33 -08:00
Florian Mayer c195addb60 [NFC] [MTE] [HWASan] Remove unnecessary member of AllocaInfo
Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D119981
2022-02-16 15:19:30 -08:00
Florian Mayer 59e7de26aa [HWASan] remove replacement of DbgVariableIntrinsics.
This code was dead because we AI->replaceUsesWithIf above. I verified
this doesn't actually get run by applying
https://gist.github.com/fmayer/aea7cbb4700cfe2c9d932591ae1073c3 to the
Android toolchain and building AOSP, without any crash.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D119802
2022-02-15 11:40:58 -08:00
Florian Mayer 8de457eafc [HWASAN] use common alignAndPadAlloca
Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D119614
2022-02-14 15:28:32 -08:00
Florian Mayer 205308de6b [NFC] [MTE] Move alignAndPadAlloca to MemoryTaggingSupport.
Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D119610
2022-02-14 14:54:04 -08:00
Florian Mayer bf2f72fa10 [hwasan] keep debug intrinsicts in AllocaInfo.
Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D119498
2022-02-11 16:01:02 -08:00
Florian Mayer 26dbc47468 Revert "[hwasan] keep debug intrinsicts in AllocaInfo."
This reverts commit 19fdf85f58.
2022-02-11 14:41:24 -08:00
Florian Mayer 19fdf85f58 [hwasan] keep debug intrinsicts in AllocaInfo.
Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D119498
2022-02-11 10:56:53 -08:00
Florian Mayer e7356fb3e2 [nfc] [hwasan] factor out logic to collect info about stack
this is the first step in unifying some of the logic between hwasan and
mte stack tagging. this only moves around code, changes to converge
different implementations of the same logic follow later.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D118947
2022-02-11 10:54:12 -08:00
serge-sans-paille ffe8720aa0 Reduce dependencies on llvm/BinaryFormat/Dwarf.h
This header is very large (3M Lines once expended) and was included in location
where dwarf-specific information were not needed.

More specifically, this commit suppresses the dependencies on
llvm/BinaryFormat/Dwarf.h in two headers: llvm/IR/IRBuilder.h and
llvm/IR/DebugInfoMetadata.h. As these headers (esp. the former) are widely used,
this has a decent impact on number of preprocessed lines generated during
compilation of LLVM, as showcased below.

This is achieved by moving some definitions back to the .cpp file, no
performance impact implied[0].

As a consequence of that patch, downstream user may need to manually some extra
files:

llvm/IR/IRBuilder.h no longer includes llvm/BinaryFormat/Dwarf.h
llvm/IR/DebugInfoMetadata.h no longer includes llvm/BinaryFormat/Dwarf.h

In some situations, codes maybe relying on the fact that
llvm/BinaryFormat/Dwarf.h was including llvm/ADT/Triple.h, this hidden
dependency now needs to be explicit.

$ clang++ -E  -Iinclude -I../llvm/include ../llvm/lib/Transforms/Scalar/*.cpp -std=c++14 -fno-rtti -fno-exceptions | wc -l
after:   10978519
before:  11245451

Related Discourse thread: https://llvm.discourse.group/t/include-what-you-use-include-cleanup
[0] https://llvm-compile-time-tracker.com/compare.php?from=fa7145dfbf94cb93b1c3e610582c495cb806569b&to=995d3e326ee1d9489145e20762c65465a9caeab4&stat=instructions

Differential Revision: https://reviews.llvm.org/D118781
2022-02-04 11:44:03 +01:00
Florian Mayer 374f5f0df4 [hwasan] [nfc] simplify getAllocaSizeInBytes
AllocaInst::getAllocationSize implements essentially the same logic as
our custom function.

Reviewed By: hctim

Differential Revision: https://reviews.llvm.org/D118958
2022-02-03 17:59:24 -08:00
Florian Mayer 8ada962a34 [NFC] [hwasan] use InstIterator
Differential Revision: https://reviews.llvm.org/D118865
2022-02-03 11:10:18 -08:00
Florian Mayer fa75a62cb5 [NFC] pull retvec logic to MemoryTaggingSupport.
we will also need this for aarch64 stack tagging.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D118852
2022-02-02 16:05:52 -08:00
Florian Mayer f7a6c341cb [mte] support more complicated lifetimes (e.g. for exceptions).
Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D118848
2022-02-02 14:39:22 -08:00
Florian Mayer 1d679097da [NFC] remove excessive whitespace. 2022-02-02 13:35:33 -08:00
Florian Mayer 712b31e2d4 [NFC] factor isStandardLifetime out of HWASan
this is so we can use it for aarch64 stack tagging.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D118836
2022-02-02 13:23:55 -08:00
Florian Mayer aefb2e134d [hwasan] work around lifetime issue with setjmp.
setjmp can return twice, but PostDominatorTree is unaware of this. as
such, it overestimates postdominance, leaving some cases (see attached
compiler-rt) where memory does not get untagged on return. this causes
false positives later in the program execution.

this is a crude workaround to unblock use-after-scope for now, in the
longer term PostDominatorTree should bemade aware of returns_twice
function, as this may cause problems elsewhere.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D118647
2022-02-01 12:14:20 -08:00