Commit Graph

13207 Commits

Author SHA1 Message Date
Jon Roelofs
7939ce39da [builtins] Move cfi start's after the symbol name [NFC]
... in preparation for diagnosing improperly nested .cfi regions.

See https://reviews.llvm.org/D155245
2023-11-17 14:33:20 -08:00
Thurston Dang
b1338d1e3a [tsan] Shrink RiscV64 48-bit LowApp region slightly to speed up TSan RestoreAddr (#72316)
The RiscV64 48-bit mappings introduced in
46cb8d9a32 necessitated changing
RestoreAddr to use 4-bits as the indicator. This roughly halves the
speed of RestoreAddr, because it is now brute-force testing addresses in
1TB increments, rather than 2TB increments. Crucially, this slowdown
applies to TSan on all platforms, not just RiscV64 48-bit.

This patch slightly shrinks the RiscV64 48-bit LowApp region mapping
(from 5TB to 4TB); we hope that 4TB ought to be enough for anybody,
especially since there is no ASLR applied to the binary in this region.
This allows restoring RestoreAddr to use 3-bits as the indicator again,
thus speeding up TSan on all platforms.

Co-authored-by: Thurston Dang <thurston@google.com>
2023-11-17 09:27:32 -08:00
Lang Hames
d97981c98a [ORC-RT] Add missing cstdint include.
This should have been included in b2bbe8cc1c. Adding it should fix the bot
failures in https://lab.llvm.org/buildbot/#/builders/85/builds/20288
2023-11-16 16:45:29 -08:00
Lang Hames
b2bbe8cc1c [ORC-RT] Add bitmask-enum and bit_ceil utilities to the ORC runtime.
bitmask_enum.h is essentially a copy of llvm/ADT/BitmaskEnum.h, with some minor
cleanup and renaming.

The bit_ceil function is a placeholder for std::bit_ceil, which we can use once
compiler-rt can use c++20.

These utilities will be used to simplify bitfield enum usage in upcoming
ORC-RT patches.
2023-11-16 16:14:15 -08:00
Nikita Popov
89361007aa [hwasan] Move __hwasan_thread_enter/__hwasan_thread_exit out of namespace (#72123)
Due to a GCC bug (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25940),
GCC doesn't consider extern "C" functions with the same name but
different namespace to be the same. As such, the default visibility
attribute (on a declaration outside the namespace) doesn't get applied
to the definition in the namespace and the symbol is not exported.

This came up as an ABI diff when switching between gcc and clang for
compiling compiler-rt.
2023-11-15 09:38:24 +01:00
Andres Villegas
3dc098d392 [NFC, sanitizer_symbolizer] Split Fuchsia and Markup. (#72305)
This PR separates parts of the symbolizer markup
implementation that are Fuchsia OS specific. This
is in preparation of enabling symbolizer markup
in other OSs.
2023-11-14 16:39:04 -08:00
Florian Mayer
8aed91631d [NFC] remove static asserts 2023-11-14 15:27:40 -08:00
Florian Mayer
d1168df2a4 [scudo] change allocation buffer size with env var (#71703)
We don't allow SCUDO_OPTIONS to be preserved across SELinux transitions,
so introducing a more constrained one that we can preserve.
2023-11-14 14:59:25 -08:00
Florian Mayer
a66dc461ac [scudo] allocation_ring_buffer_size <= 0 disables buffer (#71791)
Prevent a null pointer exception for allocation_ring_buffer_size < 0.
2023-11-14 14:58:05 -08:00
Zequan Wu
0358825906 [Profile] Remove __llvm_profile_has_correlation() (#71996)
As discussed in
https://github.com/llvm/llvm-project/pull/70856#issuecomment-1791465183
and
https://github.com/llvm/llvm-project/pull/70856#issuecomment-1806281746,
it's better not to do runtime check for VARIANT_MASK_DBG_CORRELATE bit
in __llvm_profile_raw_version when deciding if profile data/name
sections should be dropped or not.
2023-11-14 14:03:10 -05:00
Alex Richardson
dc298fecb7 [builtins] Build with -Wbuiltin-declaration-mismatch if supported
GCC is able to check that the signatures of the builtins are as expected
and this shows some incorrect signatures on ld80 platforms (i.e. x86).
The *tf* functions should take 128-bit arguments but until the latest fixes
they used 80-bit long double.

Differential Revision: https://reviews.llvm.org/D153814
2023-11-13 11:06:22 -08:00
Mariusz Borsa
c2205ab309 [Sanitizers][Darwin] Pass offset to __asan_set_shadow_xx (#71745)
Normally, when __asan_option_detect_stack_use_after_return option is
set,
the instrumentation passed the adress of the shadow memory bytes to be
set, for detecting problems with local variables.
This can be a problem when the -fsanitize-stable-abi option is in
effect,
since the ABI implementation doesn't have means to communicate the
current shadow memory base address
back to its users.

This change addresses it simply by setting
__asan_shadow_memory_dynamic_address to zero. It means
that __asan_set_shadow_xx will be now called with the offset relative to
the current shadow memory
base, and the ABI implementation needs to adapt accordingly.

The other change here is to set
__asan_option_detect_stack_use_after_return to nonzer by default,
which is needed for instrumentation to take paths using the
__asan_shadow_memory_dynamic_address
and __asan_set_shadow_xx calls.

Co-authored-by: Mariusz Borsa <m_borsa@apple.com>
2023-11-12 14:44:43 -08:00
Fangrui Song
ee7d41d179 [asan] Report executable/DSO name for report_globals=2 and odr-violation checking (#71879)
For an odr-violation error due to a source file linked into two DSOs, or
one DSO and the main executable, it can be difficult to identify the DSO
name. Let's print the module name in the error report.

```
echo 'extern long var; int main() { return var; }' > a.cc
echo 'long var;' > b.cc
clang++ -fpic -fsanitize=address -shared b.cc -o b.so
clang++ -fsanitize=address a.cc b.cc ./b.so -o a
```

w/o this patch:
```
==1375386==ERROR: AddressSanitizer: odr-violation (0x56067cb06240):
  [1] size=8 'var' b.cc
  [2] size=8 'var' b.cc
...
```
w/ this patch:
```
==1375386==ERROR: AddressSanitizer: odr-violation (0x56067cb06240):
  [1] size=8 'var' b.cc in /tmp/c/a
  [2] size=8 'var' b.cc in ./b.so
```

In addition, update the `report_globals=2` message to include the module
name
```
==1451005==Added Global[0x7fcfe59ae040]: beg=0x7fcfe59ae140 size=8/32 name=var source=b.cc module=./b.so dyn_init=0 odr_indicator=0x55754f939260
```
2023-11-10 13:12:27 -08:00
Zachary Johnson
5d6304f017 [NFC][asan] Change asan_init and asan_init_is_running; add setters/getters
For #71833
2023-11-09 13:57:46 -08:00
Zachary Johnson
08771c4e0e [ASan] Clang-format for #71833 2023-11-09 13:57:46 -08:00
Evgenii Stepanov
180b99c302 [scudo] Relax MemtagTag.SelectRandomTag. (#68048)
As it turns out, PRNGs have varying quality.
Relax the test to accept less-then-perfect tag distribution.
2023-11-09 13:14:20 -08:00
Thurston Dang
a34f3772e8 [sanitizer_common] Fix build breakage by guarding #include <dlfcn.h>
My change
(0be4c6b948) broke the Windows buildbot
(https://lab.llvm.org/buildbot/#/builders/127/builds/57976/steps/4/logs/stdio)

This fixes forward by adding a guard
2023-11-09 21:00:33 +00:00
Thurston Dang
0be4c6b948 [sanitizer_common] Add experimental flag to tweak dlopen(<main program>) (#71715)
This introduces an experimental flag 'test_only_replace_dlopen_main_program'. When enabled, this will replace dlopen(main program,...) with dlopen(NULL,...), which is the correct way to get a handle to the main program.

This can be useful when ASan is statically linked, since dladdr((void*)pthread_join) or similar will return the path to the main program.

Note that dlopen(main program,...) never ends well:
- PIE in recent glibc versions (glibc bugzilla 24323), or non-PIE: return an error
- PIE in current GRTE and older glibc: attempt to load the main program again, leading to reinitializing ASan and failing to remap the shadow memory.

---------

Co-authored-by: Thurston Dang <thurston@google.com>
2023-11-09 12:53:06 -08:00
ChiaHungDuan
048ece4413 [scudo] Calling initCache() in init() of SizeClassAllocatorLocalCache (#71427)
initCacheMaybe() will init all the size class arrays at once and it
doesn't have much work to do even if it supports partial initialization.
This avoids the call to initCacheMaybe in each allocate()/deallocate().
2023-11-09 01:31:58 +08:00
Florian Mayer
1a4754c8c8 [NFC] turn comment into static_assert (#71504) 2023-11-08 01:23:55 -08:00
Kenny Yu
1146d96096 [TSAN] Add __tsan_check_no_mutexes_held helper (#71568)
This adds a new helper that can be called from application code to
ensure that no mutexes are held on specific code paths. This is useful
for multiple scenarios, including ensuring no locks are held:

- at thread exit
- in peformance-critical code
- when a coroutine is suspended (can cause deadlocks)

See this discourse thread for more discussion:

https://discourse.llvm.org/t/add-threadsanitizer-check-to-prevent-coroutine-suspending-while-holding-a-lock-potential-deadlock/74051

This resubmits and fixes #69372 (was reverted because of build
breakage).
This also includes the followup change #71471 (to fix a land race).
2023-11-08 08:02:14 +01:00
Hans Wennborg
27156dd575 Revert "[TSAN] Add __tsan_check_no_mutexes_held helper (#69372)"
The new lit test fails, see comment on the PR. This also reverts
the follow-up commit, see below.

> This adds a new helper that can be called from application code to
> ensure that no mutexes are held on specific code paths. This is useful
> for multiple scenarios, including ensuring no locks are held:
>
> - at thread exit
> - in peformance-critical code
> - when a coroutine is suspended (can cause deadlocks)
>
> See this discourse thread for more discussion:
>
> https://discourse.llvm.org/t/add-threadsanitizer-check-to-prevent-coroutine-suspending-while-holding-a-lock-potential-deadlock/74051

This reverts commit bd841111f3.
This reverts commit 16a395b74d.
2023-11-07 16:08:01 +01:00
Kirill Stoimenov
4d9f3ca77c [HWASAN] Add memset interceptor (#71244)
Co-authored-by: Vitaly Buka <vitalybuka@google.com>
2023-11-07 00:01:04 -08:00
Christopher Ferris
e15fcd7ba4 [scudo] Use the releaseAndZeroPagesToOS function. (#71256)
All of the code assumes that when the pages are released, the entry is
zero'd, so use the correct function. On most systems, this does not
change anything.
2023-11-06 12:46:37 -08:00
Heejin Ahn
d859403037 [sanitizer] Fix pthread_exit interceptor's return type (#71253)
`pthread_exit`'s return type is void.
2023-11-05 22:41:37 -08:00
Jonathan Wakely
c670cdb968 [sanitizers] Do not define __has_feature in sanitizer/common_interface_defs.h (#66628)
Public headers intended for user code should not define `__has_feature`,
because this can break preprocessor checks done later in user code, e.g.
if they test `#ifdef __has_feature` to check for real support in the
compiler.

Replace the only use in the public header with a check for it being
supported before trying to use it. Define the fallback definition in the
internal headers, so that other internal sanitizer headers can continue
to use it as preferred.

This resolves a bug reported to GCC as https://gcc.gnu.org/PR109882
2023-11-05 15:22:56 -08:00
Kirill Stoimenov
3cf9bf343d [HWASAN] Enable memcpy and memmove interceptors (#71217) 2023-11-03 14:17:45 -07:00
Kenny Yu
bd841111f3 [TSAN] Add __tsan_check_no_mutexes_held helper (#69372)
This adds a new helper that can be called from application code to
ensure that no mutexes are held on specific code paths. This is useful
for multiple scenarios, including ensuring no locks are held:

- at thread exit
- in peformance-critical code
- when a coroutine is suspended (can cause deadlocks)

See this discourse thread for more discussion:

https://discourse.llvm.org/t/add-threadsanitizer-check-to-prevent-coroutine-suspending-while-holding-a-lock-potential-deadlock/74051
2023-11-03 06:53:52 +01:00
Ryan Prichard
3747cde5e8 [Fuzzer] Enable custom libc++ for Android (#70407)
The Android LLVM build system builds the arm64 fuzzer lib without
HWASan, but then applications that enable HWASan can generated an object
file with a HWASan-ified version of some libc++ symbols (e.g.
`std::__1::piecewise_construct`). The linker can choose the HWASan-ified
definition, but then it cannot resolve the relocation from
libclang_rt.fuzzer-aarch64-android.a to this symbol because the high
bits of the address are unexpectedly set. This produces an error:

```
relocation R_AARCH64_ADR_PREL_PG_HI21 out of range
```

Fix this problem by linking a custom isolated libc++ into Android's
fuzzer library.

We need to pass through ANDROID_NATIVE_API_LEVEL so that the libc++ for
32-bit Android (API < 24) uses LLVM_FORCE_SMALLFILE_FOR_ANDROID.
2023-11-02 14:07:39 -07:00
Zequan Wu
7fa9930847 Rename hasCorrelation to __llvm_profile_has_correlation 2023-11-02 14:45:59 -04:00
Thurston Dang
7d039effc4 [tsan] Increase size of shadow mappings for C/C++ on linux/x86_64 (#70517)
The current TSan mappings for C/C++ on linux/x86_64 have 0.5TB
for low app mem, 1.5TB (1.17TB usable) for mid app mem and
1.5TB for high app mem.
This can get a bit cramped if the apps are huge, and/or (in the
case of mid/high app mem) with significant ASLR entropy
(default ASLR setting of 28-bits = 1TB).

This patch increases the mapping sizes to 2TB, 5TB, and 6TB for
the low, mid and high app regions respectively. This is compatible
with up to 30-bits of ASLR entropy. It is difficult to make the
mappings any larger, given the 44-bit pointer compression.

It also moves the heap region to avoid HeapEnd() overlapping with
the newly enlarged high app region.

For convenience, we now use kShadowAdd instead of kShadowXor for
this set of mappings. This should be roughly equivalent in
runtime performance.
2023-11-02 09:49:19 -07:00
Zequan Wu
56e205a89c [Profile] Fix debug info correlation test failure on mac. 2023-11-02 11:30:26 -04:00
Davide Italiano
d06596516f [build_symbolizer] Fix typo in 2c81d70747ac81b37b6c7639fe7afa328e8f5e79 2023-11-01 18:40:11 -07:00
Davide Italiano
4de92601c8 [build_symbolizer] Introduce ZLIB_SRC to specify an on-disk location for (#70994)
zlib.

Not everyone wants to checkout from `git`. Tested with and without the
env var.
2023-11-01 15:39:01 -07:00
Zequan Wu
d871456659 [Profile] Remove inline for hasCorrelation. 2023-11-01 15:48:50 -04:00
Zequan Wu
3c97c8b6fc [Profile] Refactor profile correlation. (#70856)
Refactor some code from https://github.com/llvm/llvm-project/pull/69493.

#70712 was reverted due to linking failures. So, `-debug-info-correlate` remains unchanged and no new flag added.
2023-11-01 14:16:43 -04:00
Zequan Wu
db7a1ed9a2 Revert "[Profile] Refactor profile correlation. (#70712)"
This reverts commit 4b383d0af9.
2023-10-31 10:53:45 -04:00
Zequan Wu
4b383d0af9 [Profile] Refactor profile correlation. (#70712)
Refactor some code from https://github.com/llvm/llvm-project/pull/69493.

Rebase of https://github.com/llvm/llvm-project/pull/69656 on top of main
as it was messed up.
2023-10-31 10:41:01 -04:00
David Truby
dbb4f90252 [compiler-rt] Don't link builtins against the CRT on Windows (#70675)
compiler-rt/builtins doesn't depend on anything from the CRT but
currently links against it and embeds a `/defaultlib:msvcrt` in the
`.lib` file, forcing anyone linking against it to also link against that
specific CRT. This isn't necessary as the end user can just choose which
CRT they want to use independently.
2023-10-31 12:51:38 +00:00
Vitaly Buka
3e5187ea83 Revert "[HWASAN] Enable memcpy, memmove and memset interceptors (#70387)"
Breaks build bots, details in #70387.

This reverts commit 91cdd7d615.
2023-10-30 21:09:21 -07:00
Kirill Stoimenov
91cdd7d615 [HWASAN] Enable memcpy, memmove and memset interceptors (#70387) 2023-10-30 15:01:21 -07:00
Alan Phipps
f95b2f1acf Reland "[InstrProf][compiler-rt] Enable MC/DC Support in LLVM Source-based Code Coverage (1/3)"
Part 1 of 3. This includes the LLVM back-end processing and profile
reading/writing components. compiler-rt changes are included.

Differential Revision: https://reviews.llvm.org/D138846
2023-10-30 11:15:02 -05:00
Sander de Smalen
e79f0506cf [compiler-rt] Don't use 'vg' in CFI directives for SME ABI routines
This broke some builds where GNU assembler doesn't support 'vg'.
2023-10-30 08:27:10 +00:00
Alexander Shaposhnikov
d4b8572f11 [compiler-rt] Fix src_rep_t_clz and clz_in_sig_frac
This is a follow-up to 910a4bf5b.

1. __builtin_clz takes unsigned int, thus for uint16_t
src_rep_t_clz can't use it directly but should subtract 16
(leading 16 bits of the promoted argument are zero).

2. Fix (and simplify) clz_in_sig_frac.

Test plan: ninja check-compiler-rt
(extendhfsf2_test.c and extenddftf2_test.c)
2023-10-28 10:20:57 +00:00
ChiaHungDuan
4e8d6c4f82 [scudo] Pass the max number of blocks to popBlocks (#70243)
Make the cache have the fully control on how many blocks to be popped
(At before, it depended the number of blocks stored in the
TransferBatch)
2023-10-26 15:16:57 -07:00
Alex Richardson
b745ce9525 [builtins] Revert accidental change to PPC implementation in 05a4212cc7
This commit was supposed to only change the generic implementation.
Should fix the build bot errors.
2023-10-24 17:28:49 -07:00
Alexander Richardson
05a4212cc7 [builtins] Avoid using long double in generic sources (#69754)
Use of long double can be error-prone since it could be one of 80-bit
extended precision float, IEEE 128-bit float, or IBM 128-bit float.
Instead use an explicit xf_float typedef for the remaining cases where
long double is being used in the implementation. This patch does not
touch
the PPC specializations which still use long double.
2023-10-25 01:15:47 +01:00
Leonard Chan
bac3808a12 Reapply "[compiler-rt] Allow Fuchsia to use 64-bit allocator for RISCV (#68343)"
This reverts commit 37432c154f.

The tunings for the lsan allocator for Fuchsia on RISCV should be
adjusted.
2023-10-24 20:45:08 +00:00
Rainer Orth
ad7611dafe [builtins] Fix floattitf.c etc. compilation on Solaris/SPARC (#70058)
69660ccf2a broke the [Solaris/sparcv9
buildbot](https://lab.llvm.org/staging/#/builders/12/builds/264):
`compiler-rt/lib/builtins/int_to_fp.h` unconditionally uses `*int128_t`
which don't exist on 32-bit SPARC.

As suggested in https://github.com/llvm/llvm-project/pull/67540, this
patch fixes this by moving the `CRT_HAS_TF_MODE` guard up which does the
necessary checks.

Tested on `sparcv9-sun-solaris2.11`.
2023-10-24 19:14:33 +02:00
Alexander Richardson
d2ce3e9621 [builtins] Support building the 128-bit float functions on ld80 platforms (#68132)
GCC provides these functions (e.g. __addtf3, etc.) in libgcc on x86_64.
Since Clang supports float128, we can also enable the existing code by
using float128 for fp_t if either __FLOAT128__ or __SIZEOF_FLOAT128__ is
defined instead of only supporting these builtins for platforms with
128-bit IEEE long doubles.
This commit defines a new tf_float typedef that matches a float with
attribute((mode(TF)) on each given architecture.

There are more tests that could be enabled for x86, but to keep the diff
smaller, I restricted test changes to ones that started failing as part
of this refactoring.

This change has been tested on x86 (natively) and
aarch64,powerpc64,riscv64 and sparc64 via qemu-user.

This supersedes https://reviews.llvm.org/D98261 and should also cover
the changes from https://github.com/llvm/llvm-project/pull/68041.
2023-10-24 17:32:01 +01:00