17134 Commits

Author SHA1 Message Date
Fangrui Song
67cbe1b859 Allow -fsanitize=function on all targets
Functions instrumented with -fsanitize=function have two words before
the function label: a signature and a RTTI proxy.
Instrumented call sites check the signature first to skip checks
for uninstrumented callees.

The code is generic and works for all targets supporting C++ RTTI.
Change clangDriver to allow all targets. Add tests for Armv8.5
Branch Target Identification and `-fpatchable-function-entry=`.

Reviewed By: peter.smith

Differential Revision: https://reviews.llvm.org/D148573
2023-05-19 07:59:37 -07:00
Fangrui Song
ad31a2dcad Change -fsanitize=function to place two words before the function entry
The current implementation of -fsanitize=function places two words (the prolog
signature and the RTTI proxy) at the function entry, which makes the feature
incompatible with Intel Indirect Branch Tracking (IBT) that needs an ENDBR instruction
at the function entry. To allow the combination, move the two words before the
function entry, similar to -fsanitize=kcfi.

Armv8.5 Branch Target Identification (BTI) has a similar requirement.

Note: for IBT and BTI, whether a function gets a marker instruction at the entry
generally cannot be assumed (it can be disabled by a function attribute or
stronger LTO optimizations).

It is extremely unlikely for two words preceding a function entry to be
inaccessible. One way to achieve this is by ensuring that a function is
aligned at a page boundary and making the preceding page unmapped or
unreadable. This is not reasonable for application or library code.
(Think: the first text section has crt* code not instrumented by
-fsanitize=function.)

We use 0xc105cafe for all targets. .long 0xc105cafe disassembles to invalid
instructions on all architectures I have tested, except Power where it is
`lfs 8, -13570(5)` (Load Floating-Point with a weird offset, unlikely to be used in real code).

---

For the removed function in AsmPrinter.cpp, remove an assert: `mdconst::extract`
already asserts non-nullness.

For compiler-rt/test/ubsan/TestCases/TypeCheck/Function/function.cpp,
when the function doesn't have prolog/epilog (-O1 and above), after moving the two words,
the address of the function equals the address of ret instruction,
so symbolizing the function will additionally get a non-zero column number.
Adjust the test to allow an optional column number.
```
  .long   3238382334
  .long   .L__llvm_rtti_proxy-_Z1fv
_Z1fv:   // symbolizing here retrieves the line table entry from the second .loc
  .file   0 ...
  .loc    0 1 0
  .cfi_startproc
  .loc    0 2 1 prologue_end
  retq
```

Reviewed By: peter.smith

Differential Revision: https://reviews.llvm.org/D148665
2023-05-19 07:50:29 -07:00
Thurston Dang
ffb368d650 sanitizer_common: fix buildbot by restoring wcslen semantics
Windows buildbot (https://lab.llvm.org/buildbot/#/builders/127/builds/48479)
was broken by my patch https://reviews.llvm.org/D150909 because it
gated wcslen interception to SI_POSIX, instead of always intercepting wcslen.
This changes it back to always intercepting wcslen.
2023-05-19 06:54:29 +00:00
Thurston Dang
016e604f6e sanitizer_common: guard the wcslen interceptor code with SANITIZER_INTERCEPT_WCSLEN
This patch adds the #if SANITIZER_INTERCEPT_ guard for wcslen, similarly to how all the other
functions are guarded. It was the only missing SANITIZER_INTERCEPT_ guard [1].

This missing guard was discovered while investigating the stage2/hwasan check failure of https://reviews.llvm.org/D150708 ("hwasan: lay groundwork for importing subset of sanitizer_common interceptors [NFC]"), that was seen in https://lab.llvm.org/buildbot/#/builders/236/builds/4069. llvm_build_hwasan/unittests/ADT/./ADTTests had crashed with a backtrace of:
...
A disassembly of the binary showed that wcslen interception was present (since it was not guarded by SANITIZER_INTERCEPT_WCSLEN); howver, since INIT_WCSLEN was not called, REAL(wcslen) was null, resulting in the null pointer dereference.

[1] I checked this using "egrep '^#[ ]*define[ ]+(INIT_.*)' sanitizer_common_interceptors.inc | tr -s ' ' | sed -r 's/^# /#/' | cut -d ' ' -f 2 | sort | uniq -c | grep -v '^[ ]*2[ ]'"
The other matches are {INIT_PTHREAD_SETNAME_NP, INIT_QSORT, INIT_SHA2_INTECEPTORS(LEN), INIT_TLS_GET_ADDR, INIT_WAIT4}, which all have good reasons for not having exactly two cases.

Differential Revision: https://reviews.llvm.org/D150909
2023-05-19 06:37:45 +00:00
Vitaly Buka
3cabfe666c [LSAN] Use ThreadArgRetval in LSAN
Fixes false leaks on thread retval.

Reviewed By: thurston

Differential Revision: https://reviews.llvm.org/D150165
2023-05-18 01:11:54 -07:00
Vitaly Buka
6d74cdc7c4 [ASAN] Use ThreadArgRetval in ASAN
Fixes false leaks on thread retval.

Reviewed By: thurston

Differential Revision: https://reviews.llvm.org/D150106
2023-05-18 01:09:37 -07:00
Vitaly Buka
c16fa77c35 [NFC][HWASAN] Fix pthread_attr_getdetachstate use
Luckely of Linux PTHREAD_CREATE_DETACHED is 1.
2023-05-18 01:07:20 -07:00
Thurston Dang
70e0c8ffcb Revert 'hwasan: lay groundwork for importing subset of sanitizer_common interceptors [NFC]'
It was reported in https://reviews.llvm.org/D150708 that my patch has broken
stage2/hwasan check: https://lab.llvm.org/buildbot/#/builders/236/builds/4069

Reverting that patch (and the followup fixes) until I can investigate this further
2023-05-17 20:28:36 +00:00
Fangrui Song
0f9f247361 [gcov][test] Add -dumpdir ./
These tests rely on an unintended behavior that when the driver performs both
compilation and linking phases, the .gcno & .gcda files are placed in PWD. The
behavior will be fixed to respect -o (match -ftime-trace, -gsplit-dwarf, and
GCC).

Add -dumpdir ./ so that the tests will work with or without the behavior change,
and make it easy to compare the coverage behavior with GCC.
2023-05-17 11:54:38 -07:00
Leonard Chan
dc3069dadf [NFC][hwasan][Fuchsia] Instead wrap contents of InitLoadedGlobals with if constexpr (!SANITIZER_FUCHSIA)
This prevents spamming the build log with unused InitLoadedGlobals when building fuchsia runtimes.

Differential Revision: https://reviews.llvm.org/D150737
2023-05-17 18:45:44 +00:00
Jon Roelofs
e9a17453ee
builtins: fix a -Wshorten-64-to-32 in gcc_personality_v0
Differential revision: https://reviews.llvm.org/D150720
2023-05-17 10:59:31 -07:00
Nico Weber
d763c6e5e2 Revert "Reland "[CMake] Bumps minimum version to 3.20.0.""
This reverts commit 65429b9af6a2c99d340ab2dcddd41dab201f399c.

Broke several projects, see https://reviews.llvm.org/D144509#4347562 onwards.

Also reverts follow-up commit "[OpenMP] Compile assembly files as ASM, not C"

This reverts commit 4072c8aee4c89c4457f4f30d01dc9bb4dfa52559.

Also reverts fix attempt  "[cmake] Set CMP0091 to fix Windows builds after the cmake_minimum_required bump"

This reverts commit 7d47dac5f828efd1d378ba44a97559114f00fb64.
2023-05-17 10:53:33 -04:00
Dmitry Vyukov
704eb99383 tsan: fix string comparison in Go build script
Some bots failed with:

buildgo.sh: 173: [: linux: unexpected operator
Unknown platform

https://lab.llvm.org/buildbot/#/builders/247/builds/4603

Reviewed By: dyung

Differential Revision: https://reviews.llvm.org/D150760
2023-05-17 10:36:45 +02:00
Dmitry Vyukov
11081a6a1d tsan: allow to cross-build Go runtime
Allow user to provide target GOOS/GOARCH.
If not provided, use the host GOOS/GOARCH as we do now.
This allows to cross-compile the runtime.
Also provide SKIP_TEST knob for cross-compilation
since the test will most likely not run on host.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D150650
2023-05-17 09:30:51 +02:00
Thurston Dang
bc9526e30d hwasan: fix buildbot breakage (unused functions)
This addresses another buildbot breakage:
https://lab.llvm.org/buildbot/#/builders/37/builds/22166

My patch, https://reviews.llvm.org/D150708 introduced
stubs for common interceptor macros; these had unused
variables and functions. An earlier patch fixed the
unused variables; this patch suppresses unused-functions
warnings.
2023-05-17 06:15:23 +00:00
Jie Fu
000aeb2499 [hwasan] Fix build breakage (-Wunused-function) in hwasan_interceptors.cpp (NFC)
In file included from /data/llvm-project/compiler-rt/lib/hwasan/hwasan_interceptors.cpp:141:
/data/llvm-project/compiler-rt/lib/hwasan/../sanitizer_common/sanitizer_common_interceptors.inc:1044:13: error: unused function 'write_iovec' [-Werror,-Wunused-function]
static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
            ^
/data/llvm-project/compiler-rt/lib/hwasan/../sanitizer_common/sanitizer_common_interceptors.inc:1053:13: error: unused function 'read_iovec' [-Werror,-Wunused-function]
static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
            ^
/data/llvm-project/compiler-rt/lib/hwasan/../sanitizer_common/sanitizer_common_interceptors.inc:10363:13: error: unused function 'InitializeCommonInterceptors' [-Werror,-Wunused-function]
static void InitializeCommonInterceptors() {
            ^
3 errors generated.
2023-05-17 13:00:50 +08:00
Thurston Dang
cae7ef2604 hwasan: fix buildbot breakage (unused variables)
This (hopefully) fixes the buildbot breakage:
https://lab.llvm.org/buildbot/#/builders/77/builds/26793

My patch, https://reviews.llvm.org/D150708 introduced
stubs for common interceptor macros; these had unused
variables. This patch suppresses unused-variable
warnings.
2023-05-17 03:22:09 +00:00
Thurston Dang
02a029f7fb hwasan: lay groundwork for importing subset of sanitizer_common interceptors [NFC]
This patch does the bare minimum to import sanitizer_common_interceptors, but
without actually enabling any interceptors or meaningfully defining the
COMMON_INTERCEPT macros.

This will allow selectively enabling sanitizer_common interceptors (if the
appropriate macros are defined), as suggested by Vitaly in D149701.

Differential Revision: https://reviews.llvm.org/D150708
2023-05-16 21:27:25 +00:00
Fabio D'Urso
0f1a92ba30 [scudo] Deallocate the AllocatorRingBuffer too in unmapTestOnly
The AllocatorRingBuffer is allocated dynamically when Allocator is
initialized. This patch adds a corresponding deinitialization call in
unmapTestOnly, to avoid running out of virtual memory if the tests are run
a large number of times on memory-constrained platforms.

Reviewed By: Chia-hungDuan

Differential Revision: https://reviews.llvm.org/D149266
2023-05-16 20:09:08 +00:00
Vitaly Buka
dcd4c3c3fd Revert "[ASAN] Use ThreadArgRetval in ASAN"
https://bugs.chromium.org/p/chromium/issues/detail?id=1445676

This reverts commit 1030bd181eb74b67b7ea51631ce4becca410c406.
2023-05-16 10:49:45 -07:00
Vitaly Buka
d2b434b4e9 Revert "[LSAN] Use ThreadArgRetval in LSAN"
https://bugs.chromium.org/p/chromium/issues/detail?id=1445676

This reverts commit 20a3c6e84e0955ac20762c35e8c2435017ae967d.
2023-05-16 10:49:45 -07:00
Vitaly Buka
0d846d206e Revert "[NFC][LSAN] Move ThreadCreate into child thread"
https://bugs.chromium.org/p/chromium/issues/detail?id=1445676

This reverts commit 6d7b26ae49b9273d9aea4e53a96901caeb09efe0.
2023-05-16 10:49:45 -07:00
Jon Roelofs
70818f442d
tsan-rt: silence a -Wunused-const-variable 2023-05-16 07:25:21 -07:00
Jon Roelofs
cd4ffbe0ff
lsan-rt: silence a -Wformat-pedantic 2023-05-16 07:24:54 -07:00
Jon Roelofs
54d227312c
asan-rt: silence some more -Wformat-pedantic's 2023-05-16 07:21:00 -07:00
David Candler
f2bb57c194 [builtins][test] Use architecture specific float16 check
The COMPILER_RT_HAS_FLOAT16 cmake check is now set per architecture,
which needs to be reflected when building the tests.

Additionally added armhf to the architecture list.

Reviewed By: dim

Differential Revision: https://reviews.llvm.org/D150281
2023-05-16 14:21:22 +01:00
Alvin Wong
65abcf6c0c [asan][test][win] Move MSVC-specific tests into a subdir
This moves all but one remaining tests which use clang-cl and test
MSVC-specific behaviour into its own subdirectory. `dll_host.cpp` test
is excluded from the move because other tests also depend on its source
file, making it not MSVC-specific.

Differential Revision: https://reviews.llvm.org/D150271
2023-05-16 19:53:49 +08:00
Alvin Wong
51015af773 [asan][test][win] Remove REQUIRES: asan-rtl-heap-interception
This appears to be a leftover from when these tests were first added in
D62927. Because of this, these tests had never run with `check-asan` or
`check-asan-dynamic`.

I've tested locally that these tests do pass on both i686 MSVC and MinGW
targets. They are disabled for 64-bit though, and I believe no LLVM
buildbots are testing for 32-bit Windows targets.

Differential Revision: https://reviews.llvm.org/D150270
2023-05-16 19:53:49 +08:00
Alvin Wong
fe4a5a08b9 [asan][test][win] Port more tests to not use clang-cl on MinGW (4)
This ports some tests that requires dead stripping or ICF.

Differential Revision: https://reviews.llvm.org/D150269
2023-05-16 19:53:49 +08:00
Alvin Wong
6282315f8a [asan][test][win] Port more tests to not use clang-cl on MinGW (3)
This ports tests which requires additional link flags.

Differential Revision: https://reviews.llvm.org/D150268
2023-05-16 19:53:49 +08:00
Alvin Wong
5ad8bbee23 [asan][test][win] Port more tests to not use clang-cl on MinGW (2)
Continuation of D147432 and D147444.

Differential Revision: https://reviews.llvm.org/D150267
2023-05-16 19:53:49 +08:00
Alvin Wong
3237f56840 [asan][win][test] Disable interception_failure_test.cpp for static asan
This test checks that asan does not intercept user-provided libc
functions, but on Windows the static asan runtime does intercept static
copies of libc functions, so this test is invalid for said environment.
It used to fail from a different linker error, but this no longer
happens with newer WinSDK. Refer to comments on
https://reviews.llvm.org/D149549.

Differential Revision: https://reviews.llvm.org/D150349
2023-05-16 19:53:12 +08:00
Jon Roelofs
cd3ff251f2
asan-rt: Silence a few -Wformat=pedantic's in asan_mac.cpp
Differential revision: https://reviews.llvm.org/D150604
2023-05-15 13:10:28 -07:00
Vitaly Buka
a6b9634d0a
[test][sanitizer] Disable create_thread_loop on Android 2023-05-14 01:22:49 -07:00
Thurston Dang
bd1170d2c3 ASan: fix potential use-after-free in backtrace interceptor
Various ASan interceptors may corrupt memory if passed a
pointer to freed memory (https://github.com/google/sanitizers/issues/321).
This patch fixes the issue for the backtrace interceptor,
by calling REAL(backtrace) with a known-good scratch buffer,
and performing an addressability check on the user-provided
buffer prior to writing to it.

Differential Revision: https://reviews.llvm.org/D150496
2023-05-13 23:03:14 +00:00
Mark de Wever
65429b9af6 Reland "[CMake] Bumps minimum version to 3.20.0."
The owner of the last two failing buildbots updated CMake.

This reverts commit e8e8707b4aa6e4cc04c0cffb2de01d2de71165fc.
2023-05-13 11:42:25 +02:00
Thurston Dang
d9377c1ded ASan: unbreak Windows build by limiting backtrace* tests to glibc
My newly added backtrace test (https://reviews.llvm.org/D150491)
broke the Windows buildbot (https://lab.llvm.org/buildbot/#/builders/127/builds/48103)
because they do not have execinfo.h.
I expect the same will happen with the backtrace_symbols test (https://reviews.llvm.org/D150498) as well.

This patch does a quick fix by restricting the test scope to glibc-2.27.
(A tricker fix would take into account SANITIZER_INTERCEPT_BACKTRACE,
which is defined as (SI_FREEBSD || SI_NETBSD || SI_GLIBC || SI_SOLARIS))
2023-05-13 05:56:28 +00:00
Thurston Dang
04fd535409 ASan: add backtrace_symbols test and clarify code is correct
This is another patch for https://github.com/google/sanitizers/issues/321
(sanitizer interceptors can write to freed memory, causing corruption),
in this case for backtrace_symbols.

backtrace_symbols is already correct, hence this patch removes the
TODO note. Additionally, this patch adds a test case for it.

Differential Revision: https://reviews.llvm.org/D150498
2023-05-13 05:26:45 +00:00
Thurston Dang
d3b5ac8b35 ASan: add testcase for backtrace interceptor
It is a known, longstanding issue that some ASan interceptors
may write to freed memory, causing corruption
(https://github.com/google/sanitizers/issues/321). This patch
adds a testcase for the backtrace interceptor (one of the
known cases).

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D150491
2023-05-12 23:02:19 +00:00
Florian Mayer
edd0981e71 [HWASan] unflake test
The short granule logic made this test flaky because with low
probability there would be no tag mismatch by coincidence.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D150484
2023-05-12 15:06:31 -07:00
Vitaly Buka
0245dc94eb [test][sanitizers] Disable new test on Android 2023-05-12 14:09:59 -07:00
Vitaly Buka
d1aee9c0cb [sanitizers] Remove assert from ThreadArgRetval::Finish
Bionic uses pthread_exit to set retval, when GLIBC does not.
This cause double call to Finish. Rather then tracking this difference
on interceptor size, we can just relax precondition. It does not make
a difference.
2023-05-12 10:15:11 -07:00
Vitaly Buka
478739b2d1 Revert "[NFC][xray] Initialize XRayFileHeader"
Revert "[xray] Ignore -Wc++20-extensions in xray_records.h [NFC]"

Not needed. The fix is 3826a74fc7ad589e268b267d9323d4d416c5f6e9.

This reverts commit 231c1d4134513e5a97cfdc874e8bda488ad59699.
This reverts commit 7f191e6d2c9ec4af1819c4e80a5453583cd72f08.
2023-05-11 23:57:54 -07:00
Tomasz Kuchta
5becf548ab [DFSAN] Add support for strnlen
This patch adds a support for the libc strnlen() function in DFSAN

Reviewed by: browneee

Differential Revision: https://reviews.llvm.org/D149459
2023-05-12 06:26:40 +00:00
Fangrui Song
aa6cb0f214 [test] Remove Python<3.3 workaround without shlex.quote
Python>=3.6 has been the requirement since D93097 (2020).
Remove old workarounds.

Remove unused imports from compiler-rt/test/memprof/lit.cfg.py

Reviewed By: serge-sans-paille

Differential Revision: https://reviews.llvm.org/D150410
2023-05-11 23:06:02 -07:00
Vitaly Buka
2394f091df [ASAN][LSAN] Ignore main or uninitialized thead in pthread_exit
Fix crash on CHECK in ThreadArgRetval::Finish().
2023-05-11 22:58:09 -07:00
Jie Fu
231c1d4134 [xray] Ignore -Wc++20-extensions in xray_records.h [NFC]
/data/llvm-project/compiler-rt/lib/xray/../../include/xray/xray_records.h:48:24: error: default member initializer for bit-field is a C++20 extension [
-Werror,-Wc++20-extensions]
  bool ConstantTSC : 1 = false;
                       ^
/data/llvm-project/compiler-rt/lib/xray/../../include/xray/xray_records.h:49:23: error: default member initializer for bit-field is a C++20 extension [
-Werror,-Wc++20-extensions]
  bool NonstopTSC : 1 = false;
                      ^
2 errors generated.
2023-05-12 11:10:08 +08:00
Vitaly Buka
7f191e6d2c [NFC][xray] Initialize XRayFileHeader
Avoids reports with msan -fno-inline.
2023-05-11 18:52:30 -07:00
Eli Kobrin
80ed3c30fc [CMake][fuzzer] Add riscv64 to fuzzer supported arch list
I tried to build libFuzzer for RISC-V and succeeded. All the libFuzzer
targets were successfully built. I tested this on the small hello world code
with a few branches to check the instrumentation; all of them were covered by
libFuzzer on RISC-V arch. So I suppose it makes sense to enable libFuzzer
build for RISC-V.

Reviewed By: phosek, thetruestblue, MaskRay

Differential Revision: https://reviews.llvm.org/D147788
2023-05-11 18:33:44 -07:00
Akira Hatanaka
b653a2823f Declare _availability_version_check as weak_import instead of looking it
up at runtime using dlsym

Calling dlsym with RTLD_DEFAULT can be very slow as all images in the
process are searched for the symbol.

Differential Revision: https://reviews.llvm.org/D150397
2023-05-11 17:22:50 -07:00