llvm-project/compiler-rt/lib/asan
Fangrui Song 7afdc89c20 [sanitizer] Define SANITIZER_GLIBC to refine SANITIZER_LINUX feature detection and support musl
Several `#if SANITIZER_LINUX && !SANITIZER_ANDROID` guards are replaced
with the more appropriate `#if SANITIZER_GLIBC` (the headers are glibc
extensions, not specific to Linux (i.e. if we ever support GNU/kFreeBSD
or Hurd, the guards may automatically work)).

Several `#if SANITIZER_LINUX && !SANITIZER_ANDROID` guards are refined
with `#if SANITIZER_GLIBC` (the definitions are available on Linux glibc,
but may not be available on other libc (e.g. musl) implementations).

This patch makes `ninja asan cfi lsan msan stats tsan ubsan xray` build on a musl based Linux distribution (apk install musl-libintl)
Notes about disabled interceptors for musl:

* `SANITIZER_INTERCEPT_GLOB`: musl does not implement `GLOB_ALTDIRFUNC` (GNU extension)
* Some ioctl structs and functions operating on them.
* `SANITIZER_INTERCEPT___PRINTF_CHK`: `_FORTIFY_SOURCE` functions are GNU extension
* `SANITIZER_INTERCEPT___STRNDUP`: `dlsym(RTLD_NEXT, "__strndup")` errors so a diagnostic is formed. The diagnostic uses `write` which hasn't been intercepted => SIGSEGV
* `SANITIZER_INTERCEPT_*64`: the `_LARGEFILE64_SOURCE` functions are glibc specific. musl does something like `#define pread64 pread`
* Disabled `msg_iovlen msg_controllen cmsg_len` checks: musl is conforming while many implementations (Linux/FreeBSD/NetBSD/Solaris) are non-conforming. Since we pick the glibc definition, exclude the checks for musl (incompatible sizes but compatible offsets)

Pass through LIBCXX_HAS_MUSL_LIBC to make check-msan/check-tsan able to build libc++ (https://bugs.llvm.org/show_bug.cgi?id=48618).

Many sanitizer features are available now.

```
% ninja check-asan
(known issues:
* ASAN_OPTIONS=fast_unwind_on_malloc=0 odr-violations hangs
)
...
Testing Time: 53.69s
  Unsupported      : 185
  Passed           : 512
  Expectedly Failed:   1
  Failed           :  12

% ninja check-ubsan check-ubsan-minimal check-memprof # all passed

% ninja check-cfi
( all cross-dso/)
...
Testing Time: 8.68s
  Unsupported      : 264
  Passed           :  80
  Expectedly Failed:   8
  Failed           :  32

% ninja check-lsan
(With GetTls (D93972), 10 failures)
Testing Time: 4.09s
  Unsupported:  7
  Passed     : 65
  Failed     : 22

% ninja check-msan
(Many are due to functions not marked unsupported.)
Testing Time: 23.09s
  Unsupported      :   6
  Passed           : 764
  Expectedly Failed:   2
  Failed           :  58

% ninja check-tsan
Testing Time: 23.21s
  Unsupported      :  86
  Passed           : 295
  Expectedly Failed:   1
  Failed           :  25
```

Used `ASAN_OPTIONS=verbosity=2` to verify there is no unneeded interceptor.

Partly based on Jari Ronkainen's https://reviews.llvm.org/D63785#1921014

Note: we need to place `_FILE_OFFSET_BITS` above `#include "sanitizer_platform.h"` to avoid `#define __USE_FILE_OFFSET64 1` in 32-bit ARM `features.h`

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D93848
2021-01-06 10:55:40 -08:00
..
scripts [RISCV][ASAN] mark asan as supported for RISCV64 and enable tests 2020-10-05 10:38:30 +03:00
tests [sanitizer] Define SANITIZER_GLIBC to refine SANITIZER_LINUX feature detection and support musl 2021-01-06 10:55:40 -08:00
.clang-format [sanitizer] Remove unneeded blank lines 2018-05-09 00:44:26 +00:00
CMakeLists.txt [CMake] Remove cxx-headers from runtime deps 2020-10-30 20:03:38 -07:00
README.txt
asan.syms.extra
asan_activation.cpp compiler-rt: Rename .cc file in lib/asan to .cpp 2019-08-01 13:43:28 +00:00
asan_activation.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
asan_activation_flags.inc Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
asan_allocator.cpp Revert "[Asan] Accept __lsan_ignore_object for redzone pointer" 2020-09-16 00:34:43 -07:00
asan_allocator.h [RISCV][ASAN] support code for architecture-specific parts of asan 2020-10-04 15:21:48 +03:00
asan_blacklist.txt
asan_debugging.cpp Update compiler-rt cpplint.py 2019-09-11 21:33:06 +00:00
asan_descriptions.cpp compiler-rt: Rename .cc file in lib/asan to .cpp 2019-08-01 13:43:28 +00:00
asan_descriptions.h Update compiler-rt cpplint.py 2019-09-11 21:33:06 +00:00
asan_errors.cpp [compiler-rt] Use GetNextInstructionPc in signal handlers 2019-10-02 21:20:37 +00:00
asan_errors.h Reland "[ASan] Do not misrepresent high value address dereferences as null dereferences" 2019-10-10 17:19:58 +00:00
asan_fake_stack.cpp compiler-rt: Rename .cc file in lib/asan to .cpp 2019-08-01 13:43:28 +00:00
asan_fake_stack.h compiler-rt: Rename .cc file in lib/asan to .cpp 2019-08-01 13:43:28 +00:00
asan_flags.cpp [sanitizers] Remove unneeded MaybeCall*DefaultOptions() and nullptr checks 2020-09-08 10:12:05 -07:00
asan_flags.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
asan_flags.inc [compiler-rt] Better lint output for .inc files 2019-09-12 01:35:09 +00:00
asan_fuchsia.cpp [asan][fuchsia] set current thread before reading thread state 2020-10-24 14:23:09 -07:00
asan_globals.cpp Remove NOLINTs from compiler-rt 2019-09-11 23:19:48 +00:00
asan_globals_win.cpp Remove NOLINTs from compiler-rt 2019-09-11 23:19:48 +00:00
asan_init_version.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
asan_interceptors.cpp [compiler-rt] Fix lint check failure on comments 2019-09-22 15:31:03 +00:00
asan_interceptors.h [sanitizer] Define SANITIZER_GLIBC to refine SANITIZER_LINUX feature detection and support musl 2021-01-06 10:55:40 -08:00
asan_interceptors_memintrinsics.cpp Remove NOLINTs from compiler-rt 2019-09-11 23:19:48 +00:00
asan_interceptors_memintrinsics.h Remove a few straggler ".cc"s in compiler-rt/lib 2019-08-01 17:53:25 +00:00
asan_interceptors_vfork.S [RISCV][ASAN] implementation for vfork interceptor for riscv64 2020-09-22 22:23:29 -07:00
asan_interface.inc [asan] Provide an interface to update an allocation stack trace. 2019-10-31 13:54:46 -07:00
asan_interface_internal.h [sanitizers] Remove unneeded MaybeCall*DefaultOptions() and nullptr checks 2020-09-08 10:12:05 -07:00
asan_internal.h Revert "[lsan] On Fuchsia, don't use atexit hook for leak checks" 2020-09-23 21:10:56 +02:00
asan_linux.cpp [asan][memprof] Declare _DYNAMIC and fix -Wparentheses 2020-12-27 20:28:59 -08:00
asan_lock.h
asan_mac.cpp [compiler-rt][asan] decommit shadow memory for unmaps in fuchsia. 2020-07-21 11:07:47 -07:00
asan_malloc_linux.cpp [compiler-rt] Replace INLINE with inline 2020-09-17 16:24:20 +02:00
asan_malloc_local.h [compiler-rt] Replace INLINE with inline 2020-09-17 16:24:20 +02:00
asan_malloc_mac.cpp compiler-rt: Rename .cc file in lib/asan to .cpp 2019-08-01 13:43:28 +00:00
asan_malloc_win.cpp Remove unused variables, as suggested by @mcgov. 2019-11-04 14:55:51 -05:00
asan_mapping.h [RISCV][ASAN] support code for architecture-specific parts of asan 2020-10-04 15:21:48 +03:00
asan_mapping_myriad.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
asan_mapping_sparc64.h [asan] Dedup MemToShadowSize 2020-07-17 02:40:08 -07:00
asan_memory_profile.cpp compiler-rt: Rename .cc file in lib/asan to .cpp 2019-08-01 13:43:28 +00:00
asan_new_delete.cpp Remove NOLINTs from compiler-rt 2019-09-11 23:19:48 +00:00
asan_poisoning.cpp [compiler-rt][asan] decommit shadow memory for unmaps in fuchsia. 2020-07-21 11:07:47 -07:00
asan_poisoning.h [ASan] Use __sanitizer_fill_shadow for FastPoisonShadow on Fuchsia 2019-07-11 07:30:11 +00:00
asan_posix.cpp Revert "[lsan] On Fuchsia, don't use atexit hook for leak checks" 2020-09-23 21:10:56 +02:00
asan_preinit.cpp compiler-rt: Rename .cc file in lib/asan to .cpp 2019-08-01 13:43:28 +00:00
asan_premap_shadow.cpp [compiler-rt][asan][hwasan] Refactor shadow setup into sanitizer_common (NFCI) 2020-07-16 11:47:05 -07:00
asan_premap_shadow.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
asan_report.cpp [sanitizer] Convert PrintModuleMap to DumpProcessMap 2020-10-21 12:46:49 -07:00
asan_report.h [sanitizer] Implement reallocarray. 2019-05-01 17:33:01 +00:00
asan_rtems.cpp [compiler-rt][asan] decommit shadow memory for unmaps in fuchsia. 2020-07-21 11:07:47 -07:00
asan_rtl.cpp [sanitizer] Convert PrintModuleMap to DumpProcessMap 2020-10-21 12:46:49 -07:00
asan_scariness_score.h [compiler-rt] Remove some cpplint filters 2019-09-12 02:20:36 +00:00
asan_shadow_setup.cpp [RISCV][ASAN] support code for architecture-specific parts of asan 2020-10-04 15:21:48 +03:00
asan_stack.cpp compiler-rt: Rename .cc file in lib/asan to .cpp 2019-08-01 13:43:28 +00:00
asan_stack.h [NFC][Asan] Remove unused macro 2020-10-11 22:29:51 -07:00
asan_stats.cpp Remove NOLINTs from compiler-rt 2019-09-11 23:19:48 +00:00
asan_stats.h Revert r369472 and r369441 2019-08-21 05:06:21 +00:00
asan_suppressions.cpp Remove NOLINTs from compiler-rt 2019-09-11 23:19:48 +00:00
asan_suppressions.h compiler-rt: Rename .cc file in lib/asan to .cpp 2019-08-01 13:43:28 +00:00
asan_thread.cpp [asan] Fix stack-use-after-free checks on non-main thread on Fuchsia 2020-10-24 14:29:32 -07:00
asan_thread.h [sanitizer] Make destructors protected 2020-11-02 18:00:43 -08:00
asan_win.cpp Revert "[lsan] On Fuchsia, don't use atexit hook for leak checks" 2020-09-23 21:10:56 +02:00
asan_win_dll_thunk.cpp Remove NOLINTs from compiler-rt 2019-09-11 23:19:48 +00:00
asan_win_dynamic_runtime_thunk.cpp Remove NOLINTs from compiler-rt 2019-09-11 23:19:48 +00:00
asan_win_weak_interception.cpp compiler-rt: Rename .cc file in lib/asan to .cpp 2019-08-01 13:43:28 +00:00
weak_symbols.txt

README.txt

AddressSanitizer RT
================================
This directory contains sources of the AddressSanitizer (ASan) runtime library.

Directory structure:
README.txt       : This file.
Makefile.mk      : File for make-based build.
CMakeLists.txt   : File for cmake-based build.
asan_*.{cc,h}    : Sources of the asan runtime library.
scripts/*        : Helper scripts.
tests/*          : ASan unit tests.

Also ASan runtime needs the following libraries:
lib/interception/      : Machinery used to intercept function calls.
lib/sanitizer_common/  : Code shared between various sanitizers.

ASan runtime currently also embeds part of LeakSanitizer runtime for
leak detection (lib/lsan/lsan_common.{cc,h}).

ASan runtime can only be built by CMake. You can run ASan tests
from the root of your CMake build tree:

make check-asan

For more instructions see:
https://github.com/google/sanitizers/wiki/AddressSanitizerHowToBuild