Commit Graph

12174 Commits

Author SHA1 Message Date
Leonard Chan
bcc4470bad [compiler-rt][hwasan] Let CheckAddressSized eventually call HandleTagMismatch on Fuchsia
Any hwasan tag checking done through runtime calls like __hwasan_mem* or
__hwasan_load/store* currently raise a sigtrap on a tag mismatch. Hwasan
dumps as much information it knows on the tag mismatch by placing
important values in specific registers before the brk and encoding the
access information in the optional argument supplied to the brk. If the
platform hwasan runs on uses signal handlers, then users can see the
typical pretty hwasan error report, but Fuchsia doesn't use signal
handlers, so it's left up to the platform exception handler to print all
this encoded information.

This patch attempts to enter the regular error reporting path via
HandleTagMismatch if a new macro CAN_GET_REGISTERS is set. For now this
is only defined for Fuchsia + aarch64, but can be expanded for other
platforms.

Differential Revision: https://reviews.llvm.org/D139377
2022-12-07 23:09:53 +00:00
Mitch Phillips
c414bbefe4 Make -fsanitize=scudo use scudo_standalone. Delete check-scudo.
Leaves the implementation and tests files in-place for right now, but
deletes the ability to build the old sanitizer-common based scudo. This
has been on life-support for a long time, and the newer scudo_standalone
is much better supported and maintained.

Also patches up some GWP-ASan wording, primarily related to the fact
that -fsanitize=scudo now is scudo_standalone, and therefore the way to
reference the GWP-ASan options through the environment variable has
changed.

Future follow-up patches will delete the original scudo, and migrate all
its tests over to be part of the scudo_standalone test suite.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D138157
2022-12-02 11:29:41 -08:00
Ties Stuij
aadec60ad9 [compiler-rt] support armv4t
The main thing that needed changing was excluding functionality that
isn't supported on armv4t. So excluding Arm specific builtin assembly files.

In the process some files were renamed and the source was annotated where
appropriate, so it's a bit easier to follow what group of files are meant for
what purpose.

Reviewed By: peter.smith, nickdesaulniers

Differential Revision: https://reviews.llvm.org/D138725
2022-12-01 21:24:53 +00:00
Kirill Stoimenov
f7c7161137 [HWASAN] Modified __hwasan::Thread::unique_id_ to be u32 instead of u64.
This is needed for LSAN integration work. LSAN uses context to store thread_id << 32 + stack_id. In order to fit both we would need thread_id to be 32 bit.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D139060
2022-12-01 00:44:22 +00:00
Roy Sundahl
ad6c8092dc [asan][aarch64] Don't use 64 bit allocator for Apple ios family
The recent change (https://reviews.llvm.org/D137136) to unconditionally
choose the 64 bit allocator on aarch64 breaks Apple iOS family of devices
which purposely use a smaller address space than is used with macOS.

rdar://102527313

Reviewed By: thetruestblue, yln

Differential Revision: https://reviews.llvm.org/D139030
2022-11-30 13:16:59 -08:00
Gabor Buella
e49a6c3d7f [MSAN] add interceptor for stpncpy
Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D138386
2022-11-30 12:21:39 +01:00
Dmitry Vyukov
099997540f tsan: fix epoll_pwait2 interceptor
epoll_pwait2 is new and may not be present in libc and/or kernel.
Since we effectively add it to libc (as will be probed by the program
using dlsym or a weak function pointer) we need to handle the case
when it's not present in the actual libc.

Reviewed By: melver

Differential Revision: https://reviews.llvm.org/D138929
2022-11-30 10:37:11 +01:00
Petr Hosek
12e20a446e [scudo] Do not consider releasing unallocated pages
We already know that there are no free blocks above Region->AllocatedUser.
This results in a smaller RegionPageMap and faster releaseFreeMemoryToOS.

Patch By: fabio-d
Differential Revision: https://reviews.llvm.org/D138794
2022-11-30 02:34:46 +00:00
Thurston Dang
b726df1b3a [msan] Increase size of app/shadow/origin mappings on aarch64
msan's app memory mappings for aarch64 are constrained by
the MEM_TO_SHADOW constant to 64GB or less, and some app
memory mappings (in kMemoryLayout) are even smaller in
practice. This will lead to a crash with the error message
"MemorySanitizer can not mmap the shadow memory" if the
executable's memory mappings (e.g., libraries) extend
beyond msan's app memory mappings.

This patch makes the app/shadow/origin memory mappings
considerably larger, along with corresponding changes to
the MEM_TO_SHADOW and SHADOW_TO_ORIGIN constants.

Note that this deprecates compatibility with 39- and 42-bit
VMAs.

Differential Revision: https://reviews.llvm.org/D137666
2022-11-29 20:57:17 +00:00
Vitaly Buka
0d07922d92 [NFC][asan] Simplify parameters check 2022-11-29 10:56:17 -08:00
Vitaly Buka
cc27860165 [NFC][asan] Cleanup comments
D132090 is too verbose.
Don't explain obvious.
Add simpler explanation of rounding reasoning.

Differential Revision: https://reviews.llvm.org/D138773
2022-11-29 10:56:17 -08:00
Vitaly Buka
2b026fedca [NFC][asan] Handle non-intersecting case early
Differential Revision: https://reviews.llvm.org/D138772
2022-11-29 10:56:17 -08:00
Vitaly Buka
e1657e3229 [asan] Add unaligned double ended container support
Differential Revision: https://reviews.llvm.org/D138771
2022-11-29 10:56:17 -08:00
Vitaly Buka
ca3c58f0d1 [lsan] Add logs for debugging 2022-11-28 22:59:38 -08:00
Teresa Johnson
311801a017 Revert "[MemProf] Defer profile file setup until dump time"
This reverts commit ea1826ee57.

This change is breaking the ability of tests to override the profile
output file. Need to add a mechanism to do that before resubmitting.
2022-11-28 06:29:24 -08:00
Vitaly Buka
cef078a89d [NFC][asan] Reimplement with FindBadAddress 2022-11-27 23:00:42 -08:00
Vitaly Buka
450ec77037 [NFC][asan] Move a few precondition checks 2022-11-27 23:00:42 -08:00
Vitaly Buka
8db7768ab8 [NFC][asan] Extract FixUnalignedStorage
And simplify slightly: adjust for unaligned container
only if aligned case poisons different bytes.
2022-11-27 15:26:24 -08:00
Vitaly Buka
4883c91a73 [NFC][asan] Extract kMaxRangeToCheck into a function 2022-11-27 15:26:24 -08:00
Vitaly Buka
b6c5875146 [NFC][asan] Rename variables in __sanitizer_annotate_contiguous_container 2022-11-27 15:26:23 -08:00
Dmitry Vyukov
fbcdf4a4fb tsan: intercept epoll_pwait2
It's a new syscall similar to epoll_pwait.
Add a similar interceptor for it and add synchronization
annotations in epoll_wait* syscall wrappers.
Testing this is problematic b/c it's not present in glibc
and the syscall itself may not be supported by the kernel.

Reviewed By: melver

Differential Revision: https://reviews.llvm.org/D138574
2022-11-24 07:27:09 +01:00
Snehasish Kumar
cf5c53727b [memprof] Fix RAII usage of SpinMutexLock, remove unused headers.
For the RAII lock usage we need to create a local var. There were some headers which clang-tidy identified as unused.

Reviewed By: tejohnson

Differential Revision: https://reviews.llvm.org/D138593
2022-11-23 19:16:13 +00:00
Hans Wennborg
907baeec49 Revert "Make -fsanitize=scudo use scudo_standalone. Delete check-scudo."
It broke the build, see comments on code review.

> Leaves the implementation and tests files in-place for right now, but
> deletes the ability to build the old sanitizer-common based scudo. This
> has been on life-support for a long time, and the newer scudo_standalone
> is much better supported and maintained.
>
> Also patches up some GWP-ASan wording, primarily related to the fact
> that -fsanitize=scudo now is scudo_standalone, and therefore the way to
> reference the GWP-ASan options through the environment variable has
> changed.
>
> Future follow-up patches will delete the original scudo, and migrate all
> its tests over to be part of the scudo_standalone test suite.
>
> Reviewed By: vitalybuka
>
> Differential Revision: https://reviews.llvm.org/D138157

This reverts commit ab1a5991fe.
2022-11-23 16:07:07 +01:00
Fangrui Song
06c74b5e73 Revert D138095 Use InernalAlloc in DemangleCXXABI
Broke 2/3 tests on macOS which seem to be related to
`free(demangled_name)` in DemangleCXXABI.
2022-11-22 16:29:24 -08:00
Mitch Phillips
ab1a5991fe Make -fsanitize=scudo use scudo_standalone. Delete check-scudo.
Leaves the implementation and tests files in-place for right now, but
deletes the ability to build the old sanitizer-common based scudo. This
has been on life-support for a long time, and the newer scudo_standalone
is much better supported and maintained.

Also patches up some GWP-ASan wording, primarily related to the fact
that -fsanitize=scudo now is scudo_standalone, and therefore the way to
reference the GWP-ASan options through the environment variable has
changed.

Future follow-up patches will delete the original scudo, and migrate all
its tests over to be part of the scudo_standalone test suite.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D138157
2022-11-22 12:08:30 -08:00
Kirill Stoimenov
a2ef44a5d6 [Sanitizer][NFC] Rearranged prototype definitions in lsan_common.h to group them by implementation file.
Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D138335
2022-11-22 17:23:56 +00:00
Youling Tang
dcefbce281 [Sanitizer] Fix the implementation of internal_fstat on LoongArch
If `pathname` is an empty string and the AT_EMPTY_PATH flag is specified in `flags`,
statx `pathname` argument is of type `const char *restrict`, so it should be `""`
instead of `0`.

Reviewed By: SixWeining, xen0n, xry111, lixing-star

Differential Revision: https://reviews.llvm.org/D138414
2022-11-22 22:16:11 +08:00
Evgenii Stepanov
8add770417 Revert "[scudo] Detect double free when running with MTE."
Mysterious failures on the x86_64-linux-qemu, to be debugged.

This reverts commit 1dd54691b2.
2022-11-21 16:57:14 -08:00
Advenam Tacet
1c5ad6d2c0 [1a/3][ASan][compiler-rt] API for double ended containers
This revision is a part of a series of patches extending
AddressSanitizer C++ container overflow detection capabilities by adding
annotations, similar to those existing in std::vector, to std::string
and std::deque collections. These changes allow ASan to detect cases
when the instrumented program accesses memory which is internally
allocated by the collection but is still not in-use (accesses before or
after the stored elements for std::deque, or between the size and
capacity bounds for std::string).

The motivation for the research and those changes was a bug, found by
Trail of Bits, in a real code where an out-of-bounds read could happen
as two strings were compared via a std::equals function that took
iter1_begin, iter1_end, iter2_begin iterators (with a custom comparison
function). When object iter1 was longer than iter2, read out-of-bounds
on iter2 could happen. Container sanitization would detect it.

This revision adds a new compiler-rt ASan sanitization API function
sanitizer_annotate_double_ended_contiguous_container necessary to
sanitize/annotate double ended contiguous containers. Note that that
function annotates a single contiguous memory buffer (for example the
std::deque's internal chunk). Such containers have the beginning of
allocated memory block, beginning of the container in-use data, end of
the container's in-use data and the end of the allocated memory block.
This also adds a new API function to verify if a double ended contiguous
container is correctly annotated
(__sanitizer_verify_double_ended_contiguous_container).

Since we do not modify the ASan's shadow memory encoding values, the
capability of sanitizing/annotating a prefix of the internal contiguous
memory buffer is limited – up to SHADOW_GRANULARITY-1 bytes may not be
poisoned before the container's in-use data. This can cause false
negatives (situations when ASan will not detect memory corruption in
those areas).

On the other hand, API function interfaces are designed to work even if
this caveat would not exist. Therefore implementations using those
functions will poison every byte correctly, if only ASan (and
compiler-rt) is extended to support it. In other words, if ASan was
modified to support annotating/poisoning of objects lying on addresses
unaligned to SHADOW_GRANULARITY (so e.g. prefixes of those blocks),
which would require changing its shadow memory encoding, this would not
require any changes in the libcxx std::string/deque code which is added
in further commits of this patch series.

If you have any questions, please email:
advenam.tacet@trailofbits.com
disconnect3d@trailofbits.com

Differential Revision: https://reviews.llvm.org/D132090
2022-11-21 16:38:52 -08:00
Fangrui Song
db7c82231c Restore global descriptor demangling after D138095 "[asan] Keep Itanium mangled names in global metadata"
This amends commit 00be3578e0 to demangle symbol
names in global descriptors. We keep the mangled name for the `__odr_gen_asan_*`
variables and the runtime __cxa_demangle call site change (which fixed possible
leaks for other scenarios: non-fatal diagnostics).

compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cpp uses
an undefined weak `__cxa_demangle` which does not pull in an archive definition.
A -static-libstdc++ executable link does not get demangled names.

Unfortunately this means we cannot rely on runtime demangling.
See compiler-rt/test/asan/TestCases/global-demangle.cpp
2022-11-21 20:51:52 +00:00
Fangrui Song
00be3578e0 [asan] Keep Itanium mangled names in global metadata
The runtime calls `MaybeDemangleGlobalName` for error reporting and
`__cxxabiv1::__cxa_demangle` is called if available, so demanging Itanium
mangled names in global metadata is unnecessary and wastes data size.

Add `MaybeDemangleGlobalName` in ODR violation detection to support demangled
names in a suppressions file. `MaybeDemangleGlobalName` may call
`DemangleCXXABI` and leak memory. Use an internal allocation to prevent lsan
leak (in case there is no fatal asan error).

The debug feature `report_globals=2` prints information for all instrumented
global variables. `MaybeDemangleGlobalName` would be slow, so don't do that.
The output looks like `Added Global[0x56448f092d60]: beg=0x56448fa66d60 size=4/32 name=_ZL13test_global_2`
and I think the mangled name is fine.

Other mangled schemes e.g. Windows (see win-string-literal.ll) remain the
current behavior.

Reviewed By: hctim

Differential Revision: https://reviews.llvm.org/D138095
2022-11-19 01:06:26 +00:00
Teresa Johnson
ea1826ee57 [MemProf] Defer profile file setup until dump time
With all of the writing of the memprof profile consolidated into one
place, there is no need to set up the profile file (which creates the
file and also redirects all printing from the runtime to it) until we
are ready to dump the profile.

This allows errors and other messages to be dumped to stderr instead of
the profile file, which by default is in a binary format. Additionally,
reset the output file to stderr after dumping the profile so that any
requested memprof allocator statistics are printed to stderr.

Differential Revision: https://reviews.llvm.org/D138175
2022-11-18 15:09:02 -08:00
Evgenii Stepanov
1dd54691b2 [scudo] Detect double free when running with MTE.
Try to trigger an MTE fault on double/invalid free by touching the first
byte of the allocation with the provided pointer.

Differential Revision: https://reviews.llvm.org/D137352
2022-11-18 13:18:00 -08:00
Vitaly Buka
d13dd10da0 [NFC][asan] Simplify __sanitizer_annotate_contiguous_container 2022-11-18 00:56:33 -08:00
usama hameed
7052164f98 [UBSan] Implementation NonUniqueARMRTTIBit __type_name has higher bits set on arm64
Differential Revision: https://reviews.llvm.org/D137518
2022-11-17 13:46:59 -08:00
Evgenii Stepanov
48dbf35302 [scudo] Extend basic test to cover zero size allocs.
Differential Revision: https://reviews.llvm.org/D137354
2022-11-17 11:22:25 -08:00
Vitaly Buka
605355f0dc [NFC][asan] Reformat macro 2022-11-16 22:45:08 -08:00
Vitaly Buka
38b5a37082 [NFC][asan] format __sanitizer_annotate_contiguous_container 2022-11-16 22:45:08 -08:00
Youling Tang
4ae3e13c7d [sanitizer] Add ptrace interceptor support for LoongArch
Add ptrace interceptor support for LoongArch, `ptrace.cpp` has been
tested and passed.

Reviewed By: SixWeining

Differential Revision: https://reviews.llvm.org/D137228
2022-11-17 11:57:32 +08:00
Petr Hosek
9c2700dfa5 [CMake][compiler-rt] Don't load LLVM config in the runtimes build
LLVM runtimes build already loads the LLVM config and sets all
appropriate variables, no need to do it again.

Differential Revision: https://reviews.llvm.org/D137870
2022-11-15 09:01:46 +00:00
Kuba Mracek
32bada2eda [lsan] Fix stack buffer overwrite in SuspendedThreadsListMac::GetRegistersAndSP
The call to the thread_get_state syscall (that fetches the register values for a thread) on arm64 is mistakenly claiming that the buffer to receive the register state is larger that its actual size on the stack -- the struct on the stack is arm_thread_state64_t, but the MACHINE_THREAD_STATE + MACHINE_THREAD_STATE_COUNT refer to the "unified arm state" struct (which is larger).

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

Differential Revision: https://reviews.llvm.org/D137292
2022-11-12 10:17:52 -08:00
Lang Hames
e7707464a3 [ORC-RT][MachO] Unlock JDStatesMutex during push-initializers to avoid deadlock.
During __orc_rt_macho_jit_dlopen the ORC runtime will make a request to the JIT
to push any new initializers. Since this call may add new JD-state to the
runtime (and is expected to in general) we need to unlock the JDStatesMutex
during this operation (and similarly when running initializers and atexits, as
these may call trigger push-initializers recursively).

No testcase yet: I haven't been able to reproduce the deadlock when running
llvm-jitlink in in-process mode, and we don't support out-of-process mode in
regression tests yet.
2022-11-11 16:01:25 -08:00
XingLi
1e2c20fc13 [compiler-rt] Mark $t* as clobbered for Linux/LoongArch syscalls
Linux/LoongArch doesn't preserve temporary registers across syscalls,
so we have to explicitly mark them as clobbered to avoid trashing local variables.

Reviewed By: xry111, xen0n, tangyouling, SixWeining

Differential Revision: https://reviews.llvm.org/D137396
2022-11-11 21:24:07 +08:00
Simon Pilgrim
261b3f71c0 [X86] Add missing Zen3 model subtypes
This patch adds support for detecting all current Zen/Zen3+ submodels

Based off a mixture of https://github.com/torvalds/linux/blob/master/drivers/hwmon/k10temp.c#L436 and InstLatx64 https://github.com/InstLatx64/InstLatx64/tree/master/AuthenticAMD CPUID dumps and confirmed by @GGanesh

Differential Revision: https://reviews.llvm.org/D137695
2022-11-10 10:36:09 +00:00
Youling Tang
8993f3eed6 [sanitizer] Add symbolizer support for loongarch64
Reviewed By: xry111

Differential Revision: https://reviews.llvm.org/D137383
2022-11-10 13:38:09 +08:00
Youling Tang
91bc4abfdb [sanitizer][test] Fix FastUnwindTest on LoongArch
Fixes the `FastUnwindTest` unit test for LoongArch.
This change is similar to RISCV D90574.

The following test cases pass after applying the patch:
```
$ ./runtimes/runtimes-bins/compiler-rt/lib/sanitizer_common/tests/Sanitizer-loongarch64-Test
...
[  FAILED  ] FastUnwindTest.Basic
[  FAILED  ] FastUnwindTest.FramePointerLoop
[  FAILED  ] FastUnwindTest.MisalignedFramePointer
[  FAILED  ] FastUnwindTest.FPBelowPrevFP
[  FAILED  ] FastUnwindTest.CloseToZeroFrame
```

Reviewed By: SixWeining, xen0n, MaskRay

Differential Revision: https://reviews.llvm.org/D137314
2022-11-10 13:35:52 +08:00
Youling Tang
14cd113e69 [sanitizer] Add the settings of Read and Write flags in SignalContext for LoongArch
The bit-30 in this `__flags` means the address error is due to memory load, and the
bit-31 means the address error is due to memory store. (see SC_ADDRERR_RD
and SC_ADDRERR_WR in kernel arch/loongarch/include/uapi/asm/sigcontext.h).

`illegal_write_test.cpp` and `illegal_read_test.cpp` have been tested and passed.

Reviewed By: SixWeining, xen0n, XiaodongLoong

Differential Revision: https://reviews.llvm.org/D137231
2022-11-10 13:34:22 +08:00
Youling Tang
eae8d93dc2 [sanitizer] Add GetMaxVirtualAddress() support for LoongArch
Add support for getting the maximum virtual address, LoongArch has multiple
address space layouts, the default maximum virtual address of the current
user space is 47 bits. (from TASK_SIZE in the kernel for loongarch64).

Reviewed By: SixWeining

Differential Revision: https://reviews.llvm.org/D137219
2022-11-10 13:32:30 +08:00
Lang Hames
8fda8901e2 [ORC-RT] Add a SymbolStringPool class to the ORC runtime.
This is a counterpart to llvm::orc::SymbolStringPool. It holds uniqued,
ref-counted strings; and can be used to avoid redundant storage of strings,
and speed up comparison of strings held in the pool (these become pointer
comparisons).
2022-11-09 21:26:18 -08:00
Lang Hames
873584b6cb [ORC-RT][MachO] Add RecordSectionsTracker utility to track metadata sections.
Newly added sections can be processed by calling processNewSections. Calling
reset moves all sections back to the "new" state for reprocessing (expected to
be used by dlclose).
2022-11-09 21:26:17 -08:00