113 Commits

Author SHA1 Message Date
Brad Smith
d1fd97737e
[compiler-rt][sanitizer] add Haiku support (#134772)
Co-authored-by: Jérôme Duval <jerome.duval@gmail.com>
2025-04-11 16:21:00 -04:00
mojyack
801b519dfd
[sanitizer_common] Fix build on ppc64+musl (#120036)
In powerpc64-unknown-linux-musl, signal.h does not include asm/ptrace.h,
which causes "member access into incomplete type 'struct pt_regs'"
errors. Include the header explicitly to fix this.

Also in sanitizer_linux_libcdep.cpp, there is a usage of TlsPreTcbSize
which is not defined in such a platform. Guard the branch with macro.
2025-04-10 14:23:26 -07:00
Brad Smith
9fdac840ec
[sanitizer_common] Fix building with NetBSD 10.99.12 or newer (#134708)
16543c4905

__lwp_getprivate_fast() was moved to a new arch-specific header file.

Closes: #125566

Co-authored-by: Thomas Klausner <wiz@gatalith.at>
2025-04-07 15:56:58 -04:00
Brad Smith
8f5a3ec649
[sanitizer_common] Use HW_NCPUONLINE sysctl on NetBSD in GetNumberOfCPUs() (#134704) 2025-04-07 15:15:12 -04:00
Brad Smith
540402c05b
[sanitizer_common] Remove workarounds for older Android (#124881)
Anything supported has CPU_COUNT and sched_getaffinity().
2025-01-29 10:27:19 -05:00
Brad Smith
e0cd57decb
[compiler-rt] Remove support and workarounds for Android 4 and older (#124056) 2025-01-23 13:00:39 -05:00
Dimitry Andric
867a1d621d
[sanitizer_common] mark __elf_aux_vector as weak on FreeBSD (#118110)
At some point FreeBSD introduced libsys as a wrapper between syscalls
and libc, and then linking sanitized programs started failing with:

```text
  # c++ -fsanitize=address main.cc
  ld: error: undefined symbol: __elf_aux_vector
  >>> referenced by sanitizer_linux_libcdep.cpp:950 (/usr/src/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp:950)
  >>>               sanitizer_linux_libcdep.o:(__sanitizer::ReExec()) in archive /usr/lib/clang/17/lib/freebsd/libclang_rt.asan-x86_64.a
  c++: error: linker command failed with exit code 1 (use -v to see invocation)
```

Mark `__elf_aux_vector` as weak in the internal sanitizer declaration,
so the linker will accept it at link time. The dynamic linker will then
take care of the symbol at run time.
2024-12-03 00:31:19 +01:00
Vitaly Buka
b856c9fc6a
[sanitizer] Extract SANITIZER_FREEBSD version of ThreadDescriptorSizeFallback (#109743)
This should fix SANITIZER_FREEBSD and simplify
SANITIZER_GLIBC version.

Also the PR make readers aware of problematic
`ThreadDescriptorSizeFallback` for SANITIZER_FREEBSD.
Maybe it will encourage FreeBSD maintainers to
improve the functions, or prove that it's not needed at
all.
2024-09-25 14:24:01 -07:00
Rainer Orth
f322f4a55e
[sanitizer_common] Provide dummy ThreadDescriptorSize on Solaris (#109285)
Since 2c69a09bee94acca859a1adf5b04d01dc13f7295, the Solaris build is
broken like
```
Undefined			first referenced
 symbol  			    in file
_ZN11__sanitizer20ThreadDescriptorSizeEv projects/compiler-rt/lib/sanitizer_common/CMakeFiles/RTSanitizerCommonLibc.i386.dir/sanitizer_linux_libcdep.cpp.o
```
The `ThreadDescriptorSize` reference is from
`sanitizer_linux_libcdep.cpp` (`GetTls`), l.590. This isn't actually
needed on non-glibc targets AFAICS, so this patch provides a dummy to
restore the build.

Tested on `sparcv9-sun-solaris2.11`, `amd64-pc-solaris2.11`, and
`x86_64-pc-linux-gnu`.
2024-09-20 09:35:28 +02:00
Vitaly Buka
999313debe
[sanitizer] Switch from lazy ThreadDescriptorSize (#108923)
`ThreadDescriptorSize` uses `dlsym` which may use
malloc in unexpected time.

It's relatively easy to init size from the main init.
2024-09-18 16:45:43 -07:00
Vitaly Buka
d1544da533
[NFC][sanitizer] Move InitTlsSize (#108922)
Move after ThreadDescriptorSizeFallback to be able to use it.
2024-09-18 16:29:18 -07:00
Vitaly Buka
a0bb2e21c1
[NFC][sanitizer] Move InitTlsSize into InitializePlatformEarly (#108921) 2024-09-18 16:19:35 -07:00
Vitaly Buka
5bb1ce8ea6
[NFC][sanitizer] Make GetLibcVersion void (#108915)
It can't fail if guarded with SANITIZER_GLIBC.
2024-09-17 13:12:57 -07:00
Vitaly Buka
2c69a09bee
[NFC][sanitizer] Move ThreadDescriptorSize into GLIBC/FREEBSD block (#108913) 2024-09-17 10:50:14 -07:00
Vitaly Buka
3d87e21ac8
[NFC][sanitizer] Consolidate version checking branches of ThreadDescriptorSizeFallback (#108912) 2024-09-17 09:23:51 -07:00
Vitaly Buka
e5bc842a9c
[NFC][sanitizer] Remove #elif to simplify ThreadDescriptorSizeFallback (#108911) 2024-09-17 09:22:03 -07:00
Vitaly Buka
8982f9854f
[NFC][sanitizer] Remove else if from ThreadDescriptorSizeFallback (#108909) 2024-09-16 19:20:20 -07:00
Vitaly Buka
fab60a6bae
[NFC][sanitizer] Simplify ThreadDescriptorSizeFallback using early return (#108908) 2024-09-16 19:15:25 -07:00
Vitaly Buka
ad06e9562a
Reapply "[NFC][sanitizer] Switch to gnu_get_libc_version (#108724)" (#108885)
In #108724 `#ifdef` was used instead of `#if`.

This reverts commit 68e4518598d63efa02230f400e50263baccbb8e4.
2024-09-16 14:54:01 -07:00
Vitaly Buka
f13b7d0b02
Reland "[sanitizer] Fix partially initialized static TLS range" (#108883)
Reland llvm/llvm-project#108685

Arguments order was wrong on Windows and Darwin.
2024-09-16 14:05:00 -07:00
Vitaly Buka
9a1d0744ed
Revert "[sanitizer] Fix partially initialized static TLS range" (#108881)
Reverts llvm/llvm-project#108685

Breaks Darwin and Windows
https://lab.llvm.org/buildbot/#/builders/107/builds/2930
https://ci.swift.org/view/all/job/llvm.org/view/LLDB/job/as-lldb-cmake/11684/
2024-09-16 13:51:23 -07:00
Vitaly Buka
b7c9ebe4ec
[sanitizer] Fix partially initialized static TLS range (#108685)
Fixes asan, msan crash on check added in #108684.
The #108684 includes reproducer of the issue.

Change interface of `GetThreadStackAndTls` to
set `tls_begin` and `tls_end` at the same time.
2024-09-16 12:48:24 -07:00
Thurston Dang
68e4518598 Revert "[NFC][sanitizer] Switch to gnu_get_libc_version (#108724)"
This reverts commit 69f3244da76586be393d1e97b01660c6f03d666c.

Reason: buildbot breakage because Android doesn't have <gnu/libc-version.h>
https://lab.llvm.org/buildbot/#/builders/186/builds/2381

(It's probably easy to fix but I don't readily have an Android device to test.)
2024-09-16 19:11:27 +00:00
Rainer Orth
9ec1f65814 Revert "[NFC][sanitizer] Simplify ifdef"
Breaks the [Solaris/sparcv9](https://lab.llvm.org/buildbot/#/builders/13/builds/2219) and [Solaris/amd64](https://lab.llvm.org/staging/#/builders/120/builds/1770) builds.

This reverts commit c21909a530f438bcc942c11e6d7b875bb28a028a.
2024-09-16 20:14:54 +02:00
Vitaly Buka
d3014e1f3b
[NFC][sanitizer] Use RTLD_DEFAULT for _dl_get_tls_static_info (#108723)
We don't intercept this one, no reason to use RTLD_NEXT.

Co-authored-by: Sam Elliott <quic_aelliott@quicinc.com>
2024-09-16 11:00:13 -07:00
Vitaly Buka
69f3244da7
[NFC][sanitizer] Switch to gnu_get_libc_version (#108724)
`gnu_get_libc_version` unlike `confstr` is not
intercepted. We should be able to use this
function earier.

Looks like we use `confstr` staring from
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60038
but there is no specific reason to refer it over
`gnu_get_libc_version`.
2024-09-16 09:53:19 -07:00
Vitaly Buka
c21909a530 [NFC][sanitizer] Simplify ifdef 2024-09-14 19:12:42 -07:00
Thurston Dang
694b132177
[sanitizer_common] Fix edge case for stack mapping parsing (#98381)
On some systems (e.g., at least two AArch64 Linux instances), the
process map can have:
```
    fffffffdf000-1000000000000 ... [stack]
```
instead of:
```
    fffffffdf000- ffffffffffff
```
The stack top value is larger than `GetMaxUserVirtualAddress()`, which
violates the precondition that shadow memory calculations expect. This
patch fixes the issue by saturating off-by-one values (and also adds
checks for more flagrant violations).

This fixes an issue that was observed with DFSan on AArch64 Linux (with
high-entropy ASLR, resulting in ASLR being disabled on some runs):
```
==11057==ERROR: DataflowSanitizer failed to allocate 0x1600000800000 (387028101365760) bytes at address 4fffff800000 (errno: 12)
```

(https://lab.llvm.org/staging/#/builders/90/builds/552/steps/9/logs/stdio)
This was trying to allocate a shadow at `[0x4fffff800000, 0x4fffff800000
+ 0x1600000800000] = [0x4fffff800000, 0x1b00000000000]`. Notice that the
end of the shadow region - an invalid value - is equal to
`MEM_TO_SHADOW(0x1000000000000)`, where `MEM_TO_SHADOW` is defined as
`(mem ^ 0xB00000000000ULL)`.
2024-07-10 13:49:11 -07:00
Florian Mayer
c6049e67ef Reapply "[HWASan] [compiler-rt] support non-4k pages on Android" (#95853)
Updated MapDynamicShadow callsite in asan_win.
2024-06-17 15:20:57 -07:00
Florian Mayer
1adf0fae05
Revert "[HWASan] [compiler-rt] support non-4k pages on Android" (#95853)
Reverts llvm/llvm-project#95069

Broke windows bot
2024-06-17 14:38:26 -07:00
Florian Mayer
5b04b6fe3f
[HWASan] [compiler-rt] support non-4k pages on Android (#95069) 2024-06-17 13:21:34 -07:00
Alexander Richardson
92a870888c
[compiler-rt] Map internal_sigaction to __sys_sigaction on FreeBSD (#84441)
This function is called during very early startup and which can result
in a crash on FreeBSD. The sigaction() function in libc is indirected
via a table so that it can be interposed by the threading library
rather than calling the syscall directly. In the crash I was observing
this table had not yet been relocated, so we ended up jumping to an
invalid address. To avoid this problem we can call __sys_sigaction,
which calls the syscall directly and in FreeBSD 15 is part of libsys
rather than libc, so does not depend on libc being fully initialized.
2024-06-07 00:22:07 +02:00
David CARLIER
529b5705db
Revert "[compiler-rt] simplifying ::ReExec for freebsd. (#79711)" (#82933)
This reverts commit 691b12a2dcc12fa43517d23f2a9b6039616eebc8.
2024-02-25 21:10:01 +00:00
David CARLIER
691b12a2dc
[compiler-rt] simplifying ::ReExec for freebsd. (#79711)
taking the getauxval route since elf_aux_info is available since FBSD
12.
2024-02-16 23:59:38 +00:00
Brad Smith
450be89136
[compiler-rt] Remove a few workarounds for FreeBSD 9.x (#76263)
Support for FreeBSD 11.x was dropped so garbage collect a few FreeBSD
9.x workarounds and make 12.x the oldest supported releases.
2023-12-29 05:10:13 -05:00
Dimitry Andric
f45453ab64
[sanitizer][nfc] Reformat sanitizer_linux sources (#73573) 2023-11-27 14:17:03 -08:00
Dimitry Andric
7440e4ed85
[sanitizer] Add re-execution on FreeBSD when ASLR is detected (#73439)
In the FreeBSD base system, re-executing the main binary when ASLR is
detected was implemented in the following commits:

* freebsd/freebsd-src@7cafe89f9c
* freebsd/freebsd-src@96fe7c8ab0
* freebsd/freebsd-src@930a7c2ac6
* freebsd/freebsd-src@0a736f0a6a
* freebsd/freebsd-src@4c9a0adad1

Squash all these to bring them into upstream compiler-rt.

When ASLR is detected to be enabled, this first force-disables ASLR for
the current process, then calls ReExec(). The ReExec() function gets a
FreeBSD specific implementation for finding the path of the executed
program, via the ELF auxiliary vector. This is done without calling into
the regular elf_aux_info(3) function, as that makes use of several
already-intercepted functions.
2023-11-27 22:43:33 +01:00
Vitaly Buka
a8d8c58b39 [NFC][sanitizer] Remove temp buffer 2023-09-15 23:03:20 -07:00
Vitaly Buka
5b7dfa968f
[NFC][sanitizer] Rename InternalScopedString::append to AppendF (#66558)
Prepare to introduce trivial InternalScopedString::Append(const char*).
2023-09-15 17:06:20 -07:00
Vitaly Buka
281195a22f [NFC][sanitizer] Rename internal function 2023-05-08 16:26:28 -07:00
Frederic Cambus
8510cf9fc1 [compiler-rt] Add missing #else clause to fix the build on NetBSD.
An #elif SANITIZER_SOLARIS clause was removed in D120048, but it also
removed the #else clause for the error fallback, causing the build to
fail on NetBSD.

Differential Revision: https://reviews.llvm.org/D146316
2023-03-19 09:46:45 +01:00
Youling Tang
2b15c63fb7 [compiler-rt] Fix build errors when using gcc on LoongArch
- GCC does not recognize $fcsr0, uses $r0 instead.
- GCC does not implement __builtin_thread_pointer, which can be
  obtained directly through $tp.

Reviewed By: SixWeining

Differential Revision: https://reviews.llvm.org/D140545
2022-12-26 09:33:54 +08:00
Youling Tang
b89b42b31c [tsan] Add tsan support for loongarch64
This patch enabled tsan for loongarch64 with 47-bit VMA layout. All
tests are passing.

Also adds assembly routines to enable setjmp/longjmp for loongarch64
on linux.

Reviewed By: dvyukov, SixWeining, #sanitizers

Differential Revision: https://reviews.llvm.org/D138489
2022-12-08 10:08:49 +08:00
Rainer Orth
3776db9a4f [sanitizer_common] Support Solaris < 11.4 in GetStaticTlsBoundary
This patch, on top of D120048 <https://reviews.llvm.org/D120048>, supports
GetTls on Solaris 11.3 and Illumos that lack `dlpi_tls_modid`.  It's the
same method originally used in D91605 <https://reviews.llvm.org/D91605>,
but integrated into `GetStaticTlsBoundary`.

Tested on `amd64-pc-solaris2.11`, `sparcv9-sun-solaris2.11`, and
`x86_64-pc-linux-gnu`.

Differential Revision: https://reviews.llvm.org/D120059
2022-07-21 09:18:10 +02:00
Xi Ruoyao
36b52c38aa [sanitizer] Compare against the alignment of the latter range trying to find consecutive TLS blocks in GetStaticTlsBoundary
On a mips64el-linux-gnu system, the dynamic linker arranges TLS blocks
like:

    [0] 0xfff7fe9680..0xfff7fe9684, align = 0x4
    [1] 0xfff7fe9688..0xfff7fe96a8, align = 0x8
    [2] 0xfff7fe96c0..0xfff7fe9e60, align = 0x40
    [3] 0xfff7fe9e60..0xfff7fe9ef8, align = 0x8

Note that the dynamic linker can only put [1] at 0xfff7fe9688, not
0xfff7fe9684 or it will be misaligned.  But we were comparing the
distance between two blocks with the alignment of the previous range,
causing GetStaticTlsBoundary fail to merge the consecutive blocks.

Compare against the alignment of the latter range to fix the issue.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D129112
2022-07-17 00:45:15 -07:00
H.J. Lu
b226894d47 [sanitizer] Correct GetTls for x32
Since x32 pointer size is 4 bytes, the self pointer offset in TCB:

struct
{
  void *tcb;
  dtv_t *dtv;
  void *self;
  ...

should be 8, not 16.

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

Differential Revision: https://reviews.llvm.org/D125025
2022-05-05 13:55:19 -07:00
Vitaly Buka
c36fbe05a7 Revert "[sanitizer] Don't run malloc hooks for stacktraces"
Msan crashes on clang-s390x-linux bot

This reverts commit d3531fc7f0053a7fe68317521ee6491da6e36733.
2022-04-13 23:52:36 -07:00
Vitaly Buka
d3531fc7f0 [sanitizer] Don't run malloc hooks for stacktraces
Usually when we generated stacktraces the process is in error state, so
running hooks may crash the process and prevent meaningfull error report.

Symbolizer, unwinder and pthread are potential source of mallocs.

https://b.corp.google.com/issues/228110771

Reviewed By: kda

Differential Revision: https://reviews.llvm.org/D123566
2022-04-13 16:33:53 -07:00
Vitaly Buka
5d4df59de1 Revert "[sanitizer] Don't run malloc hooks for stacktraces"
Breaks android and iOS bots.
https://green.lab.llvm.org/green/job/clang-san-iossim/5229/consoleFull#711521816a1ca8a51-895e-46c6-af87-ce24fa4cd561
https://lab.llvm.org/buildbot/#/builders/77/builds/16456

This reverts commit 6345d7f2a829faea56ad522a7d5180043f862a5c.
2022-04-13 13:13:33 -07:00
Vitaly Buka
6345d7f2a8 [sanitizer] Don't run malloc hooks for stacktraces
Usually when we generated stacktraces the process is in error state, so
running hooks may crash the process and prevent meaningfull error report.

Symbolizer, unwinder and pthread are potential source of mallocs.

https://b.corp.google.com/issues/228110771

Reviewed By: kda

Differential Revision: https://reviews.llvm.org/D123566
2022-04-13 11:44:38 -07:00