Skip to content

Commit 06da450

Browse files
authored
Merge pull request #6925 from apple/rsundahl/109373128
Discussion: Darwin Sanitizers Stable ABI
2 parents 3ee004c + 1dc8909 commit 06da450

File tree

16 files changed

+960
-1
lines changed

16 files changed

+960
-1
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,6 +1756,10 @@ def fsanitize_address_outline_instrumentation : Flag<["-"], "fsanitize-address-o
17561756
def fno_sanitize_address_outline_instrumentation : Flag<["-"], "fno-sanitize-address-outline-instrumentation">,
17571757
Group<f_clang_Group>,
17581758
HelpText<"Use default code inlining logic for the address sanitizer">;
1759+
defm sanitize_stable_abi
1760+
: OptInCC1FFlag<"sanitize-stable-abi", "Stable ", "Conventional ",
1761+
"ABI instrumentation for sanitizer runtime. Default: Conventional">;
1762+
17591763
def fsanitize_memtag_mode_EQ : Joined<["-"], "fsanitize-memtag-mode=">,
17601764
Group<f_clang_Group>,
17611765
HelpText<"Set default MTE mode to 'sync' (default) or 'async'">;

clang/include/clang/Driver/SanitizerArgs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class SanitizerArgs {
4040
bool CfiCanonicalJumpTables = false;
4141
int AsanFieldPadding = 0;
4242
bool SharedRuntime = false;
43+
bool StableABI = false;
4344
bool AsanUseAfterScope = true;
4445
bool AsanPoisonCustomArrayCookie = false;
4546
bool AsanGlobalsDeadStripping = false;

clang/lib/Driver/SanitizerArgs.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,9 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
900900
}
901901
}
902902

903+
StableABI = Args.hasFlag(options::OPT_fsanitize_stable_abi,
904+
options::OPT_fno_sanitize_stable_abi, false);
905+
903906
AsanUseAfterScope = Args.hasFlag(
904907
options::OPT_fsanitize_address_use_after_scope,
905908
options::OPT_fno_sanitize_address_use_after_scope, AsanUseAfterScope);
@@ -1254,6 +1257,16 @@ void SanitizerArgs::addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
12541257
CmdArgs.push_back("-asan-instrumentation-with-call-threshold=0");
12551258
}
12561259

1260+
// When emitting Stable ABI instrumentation, force outlining calls and avoid
1261+
// inlining shadow memory poisoning. While this is a big performance burden
1262+
// for now it allows full abstraction from implementation details.
1263+
if (StableABI) {
1264+
CmdArgs.push_back("-mllvm");
1265+
CmdArgs.push_back("-asan-instrumentation-with-call-threshold=0");
1266+
CmdArgs.push_back("-mllvm");
1267+
CmdArgs.push_back("-asan-max-inline-poisoning-size=0");
1268+
}
1269+
12571270
// Only pass the option to the frontend if the user requested,
12581271
// otherwise the frontend will just use the codegen default.
12591272
if (AsanDtorKind != llvm::AsanDtorKind::Invalid) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %clang --target=arm64-apple-darwin -fsanitize-stable-abi %s -### 2>&1 | \
2+
// RUN: FileCheck %s --check-prefix=CHECK-ASAN-STABLE-WARN
3+
// CHECK-ASAN-STABLE-WARN: warning: argument unused during compilation: '-fsanitize-stable-abi'
4+
// RUN: %clang --target=arm64-apple-darwin -fsanitize=address -fsanitize-stable-abi %s -### 2>&1 | \
5+
// RUN: FileCheck %s --check-prefix=CHECK-ASAN-STABLE-OK
6+
// RUN: %clang --target=arm64-apple-darwin -fsanitize=address -fno-sanitize-stable-abi -fsanitize-stable-abi %s -### 2>&1 | \
7+
// RUN: FileCheck %s --check-prefix=CHECK-ASAN-STABLE-OK
8+
// CHECK-ASAN-STABLE-OK: "-mllvm" "-asan-instrumentation-with-call-threshold=0"
9+
// CHECK-ASAN-STABLE-OK: "-mllvm" "-asan-max-inline-poisoning-size=0"
10+
// RUN: %clang --target=arm64-apple-darwin -fsanitize=address -fno-sanitize-stable-abi %s -### 2>&1 | \
11+
// RUN: FileCheck %s --check-prefix=CHECK-NO-ASAN-STABLE-OK
12+
// RUN: %clang --target=arm64-apple-darwin -fsanitize=address -fsanitize-stable-abi -fno-sanitize-stable-abi %s -### 2>&1 | \
13+
// RUN: FileCheck %s --check-prefix=CHECK-NO-ASAN-STABLE-OK
14+
// CHECK-NO-ASAN-STABLE-OK-NOT: "-mllvm" "-asan-instrumentation-with-call-threshold=0"
15+
// CHECK-NO-ASAN-STABLE-OK-NOT: "-mllvm" "-asan-max-inline-poisoning-size=0"

compiler-rt/cmake/config-ix.cmake

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ if(COMPILER_RT_SUPPORTED_ARCH)
701701
endif()
702702
message(STATUS "Compiler-RT supported architectures: ${COMPILER_RT_SUPPORTED_ARCH}")
703703

704-
set(ALL_SANITIZERS asan;dfsan;msan;hwasan;tsan;safestack;cfi;scudo;ubsan_minimal;gwp_asan)
704+
set(ALL_SANITIZERS asan;dfsan;msan;hwasan;tsan;safestack;cfi;scudo;ubsan_minimal;gwp_asan;asan_abi)
705705
set(COMPILER_RT_SANITIZERS_TO_BUILD all CACHE STRING
706706
"sanitizers to build if supported on the target (all;${ALL_SANITIZERS})")
707707
list_replace(COMPILER_RT_SANITIZERS_TO_BUILD all "${ALL_SANITIZERS}")
@@ -721,6 +721,11 @@ else()
721721
set(COMPILER_RT_HAS_INTERCEPTION FALSE)
722722
endif()
723723

724+
if (COMPILER_RT_HAS_SANITIZER_COMMON AND ASAN_SUPPORTED_ARCH AND APPLE)
725+
set(COMPILER_RT_HAS_ASAN_ABI TRUE)
726+
else()
727+
set(COMPILER_RT_HAS_ASAN_ABI FALSE)
728+
endif()
724729
if (COMPILER_RT_HAS_SANITIZER_COMMON AND ASAN_SUPPORTED_ARCH)
725730
set(COMPILER_RT_HAS_ASAN TRUE)
726731
else()

compiler-rt/docs/ASanABI.rst

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
.. _BuildingCompilerRT:
2+
3+
============================
4+
Darwin Sanitizers Stable ABI
5+
============================
6+
7+
Some OSes like Darwin want to include the AddressSanitizer runtime by establishing a stable ASan ABI. lib/asan_abi contains a secondary stable ABI for Darwin use and potentially others. The Stable ABI has minimal impact on the community, prioritizing stability over performance.
8+
9+
The Stable ABI is isolated by a “shim” layer which maps the unstable ABI to the stable ABI. It consists of a static library (libclang_rt.asan_abi_osx.a) that contains simple mappings of the existing ASan ABI to the smaller Stable ABI. After linking with the static shim library, only calls to the Stable ABI remain.
10+
11+
Sample content of the shim:
12+
13+
.. code-block:: c
14+
15+
void __asan_load1(uptr p) { __asan_abi_loadn(p, 1, true); }
16+
void __asan_load2(uptr p) { __asan_abi_loadn(p, 2, true); }
17+
void __asan_noabort_load16(uptr p) { __asan_abi_loadn(p, 16, false); }
18+
void __asan_poison_cxx_array_cookie(uptr p) { __asan_abi_pac(p); }
19+
20+
The shim library is only used when ``-fsanitize-stable-abi`` is specified in the Clang driver and the emitted instrumentation favors runtime calls over inline expansion.
21+
22+
Maintenance
23+
-----------
24+
25+
The maintenance burden on the sanitizer developer community should be negligible. Stable ABI tests should always pass for non-Darwin platforms. Changes to the existing ABI requiring changes to the shim should been infrequent as the existing ASan ABI has long been relatively stable anyway. Rarely, when a change that impacts the contract between LLVM and the shim occurs, some simple responses should suffice. Among such foreseeable changes are: 1) changes to a function signature, 2) additions of new functions, or 3) deprecation of an existing function.
26+
27+
Following are some examples of reasonable responses to such changes:
28+
29+
* An existing ABI function is changed to return the input parameter on success or NULL on failure. In this scenario, a reasonable change to the shim would be to modify the function signature appropriately and to simply guess at a common-sense implementation.
30+
31+
.. code-block:: c
32+
33+
uptr __asan_load1(uptr p) { __asan_abi_loadn(p, 1, true); return p; }
34+
35+
* An additional function is added for performance reasons. It has a very similar function signature to other similarly named functions and logically is an extension of that same pattern. In this case it would make sense to apply the same logic as the existing entry points:
36+
37+
.. code-block:: c
38+
39+
void __asan_load128(uptr p) { __asan_abi_loadn(p, 128, true); }
40+
41+
* An entry point is added to the existing ABI for which there is no obvious stable ABI implementation: In this case, doing nothing in a no-op stub would be acceptable, assuming existing features of ASan can still work without an actual implementation of this new function.
42+
43+
.. code-block:: c
44+
45+
void __asan_prefetch(uptr p) { }
46+
47+
* An entrypoint in the existing ABI is deprecated and/or deleted:
48+
49+
.. code-block:: c
50+
51+
(Delete the entrypoint from the shim.)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Build for the ASAN Stable ABI runtime support library.
2+
set(ASAN_ABI_SOURCES
3+
asan_abi_shim.cpp
4+
)
5+
6+
set(ASAN_ABI_HEADERS
7+
../asan/asan_interface_internal.h
8+
asan_abi.h
9+
)
10+
11+
include_directories(..)
12+
13+
add_compiler_rt_component(asan_abi)
14+
15+
if (APPLE)
16+
# TODO: set in config-ix.cmake
17+
set(ASAN_ABI_SUPPORTED_OS osx)
18+
set(ASAN_ABI_SUPPORTED_ARCHS ${X86_64} ${ARM64})
19+
# Compile Stable API sources into an object library.
20+
add_compiler_rt_object_libraries(RTASAN_ABI
21+
OS ${ASAN_ABI_SUPPORTED_OS}
22+
ARCHS ${ASAN_ABI_SUPPORTED_ARCHS}
23+
SOURCES ${ASAN_ABI_SOURCES}
24+
ADDITIONAL_HEADERS ${ASAN_ABI_HEADERS}
25+
CFLAGS ${SANITIZER_COMMON_CFLAGS})
26+
27+
add_compiler_rt_runtime(clang_rt.asan_abi
28+
STATIC
29+
OS ${ASAN_ABI_SUPPORTED_OS}
30+
ARCHS ${ASAN_ABI_SUPPORTED_ARCHS}
31+
OBJECT_LIBS RTASAN_ABI
32+
CFLAGS ${SANITIZER_COMMON_CFLAGS}
33+
LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS}
34+
PARENT_TARGET asan_abi)
35+
endif()

compiler-rt/lib/asan_abi/asan_abi.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//===-asan_abi.cpp - ASan Stable ABI---------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "asan_abi.h"
10+
11+
extern "C" {
12+
// Functions concerning instrumented global variables:
13+
void __asan_abi_register_image_globals(void) {}
14+
void __asan_abi_unregister_image_globals(void) {}
15+
16+
// Functions concerning dynamic library initialization
17+
void __asan_abi_before_dynamic_init(const char *module_name) {}
18+
void __asan_abi_after_dynamic_init(void) {}
19+
20+
// Functions concerning block memory destinations
21+
void *__asan_abi_memcpy(void *d, const void *s, size_t n) { return NULL; }
22+
void *__asan_abi_memmove(void *d, const void *s, size_t n) { return NULL; }
23+
void *__asan_abi_memset(void *p, int c, size_t n) { return NULL; }
24+
25+
// Functions concerning RTL startup and initialization
26+
void __asan_abi_init(void) {}
27+
void __asan_abi_handle_no_return(void) {}
28+
29+
// Functions concerning memory load and store reporting
30+
void __asan_abi_report_load_n(void *p, size_t n, bool abort) {}
31+
void __asan_abi_report_exp_load_n(void *p, size_t n, int exp, bool abort) {}
32+
void __asan_abi_report_store_n(void *p, size_t n, bool abort) {}
33+
void __asan_abi_report_exp_store_n(void *p, size_t n, int exp, bool abort) {}
34+
35+
// Functions concerning memory load and store
36+
void __asan_abi_load_n(void *p, size_t n, bool abort) {}
37+
void __asan_abi_exp_load_n(void *p, size_t n, int exp, bool abort) {}
38+
void __asan_abi_store_n(void *p, size_t n, bool abort) {}
39+
void __asan_abi_exp_store_n(void *p, size_t n, int exp, bool abort) {}
40+
41+
// Functions concerning query about whether memory is poisoned
42+
int __asan_abi_address_is_poisoned(void const volatile *p) { return 0; }
43+
void *__asan_abi_region_is_poisoned(void const volatile *p, size_t size) {
44+
return NULL;
45+
}
46+
47+
// Functions concerning the poisoning of memory
48+
void __asan_abi_poison_memory_region(void const volatile *p, size_t n) {}
49+
void __asan_abi_unpoison_memory_region(void const volatile *p, size_t n) {}
50+
51+
// Functions concerning the partial poisoning of memory
52+
void __asan_abi_set_shadow_xx_n(void *p, unsigned char xx, size_t n) {}
53+
54+
// Functions concerning stack poisoning
55+
void __asan_abi_poison_stack_memory(void *p, size_t n) {}
56+
void __asan_abi_unpoison_stack_memory(void *p, size_t n) {}
57+
58+
// Functions concerning redzone poisoning
59+
void __asan_abi_poison_intra_object_redzone(void *p, size_t size) {}
60+
void __asan_abi_unpoison_intra_object_redzone(void *p, size_t size) {}
61+
62+
// Functions concerning array cookie poisoning
63+
void __asan_abi_poison_cxx_array_cookie(void *p) {}
64+
void *__asan_abi_load_cxx_array_cookie(void **p) { return NULL; }
65+
66+
// Functions concerning fake stacks
67+
void *__asan_abi_get_current_fake_stack(void) { return NULL; }
68+
void *__asan_abi_addr_is_in_fake_stack(void *fake_stack, void *addr, void **beg,
69+
void **end) {
70+
return NULL;
71+
}
72+
73+
// Functions concerning poisoning and unpoisoning fake stack alloca
74+
void __asan_abi_alloca_poison(void *addr, size_t size) {}
75+
void __asan_abi_allocas_unpoison(void *top, void *bottom) {}
76+
77+
// Functions concerning fake stack malloc
78+
void *__asan_abi_stack_malloc_n(size_t scale, size_t size) { return NULL; }
79+
void *__asan_abi_stack_malloc_always_n(size_t scale, size_t size) {
80+
return NULL;
81+
}
82+
83+
// Functions concerning fake stack free
84+
void __asan_abi_stack_free_n(int scale, void *p, size_t n) {}
85+
}

compiler-rt/lib/asan_abi/asan_abi.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//===-asan_abi.h - ASan Stable ABI Interface-------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef ASAN_ABI_H
10+
#define ASAN_ABI_H
11+
12+
#include <stdbool.h>
13+
#include <stddef.h>
14+
#include <sys/types.h>
15+
16+
extern "C" {
17+
// Functions concerning instrumented global variables:
18+
void __asan_abi_register_image_globals();
19+
void __asan_abi_unregister_image_globals();
20+
21+
// Functions concerning dynamic library initialization
22+
void __asan_abi_before_dynamic_init(const char *module_name);
23+
void __asan_abi_after_dynamic_init();
24+
25+
// Functions concerning block memory destinations
26+
void *__asan_abi_memcpy(void *d, const void *s, size_t n);
27+
void *__asan_abi_memmove(void *d, const void *s, size_t n);
28+
void *__asan_abi_memset(void *p, int c, size_t n);
29+
30+
// Functions concerning RTL startup and initialization
31+
void __asan_abi_init();
32+
void __asan_abi_handle_no_return();
33+
34+
// Functions concerning memory load and store reporting
35+
void __asan_abi_report_load_n(void *p, size_t n, bool abort);
36+
void __asan_abi_report_exp_load_n(void *p, size_t n, int exp, bool abort);
37+
void __asan_abi_report_store_n(void *p, size_t n, bool abort);
38+
void __asan_abi_report_exp_store_n(void *p, size_t n, int exp, bool abort);
39+
40+
// Functions concerning memory load and store
41+
void __asan_abi_load_n(void *p, size_t n, bool abort);
42+
void __asan_abi_exp_load_n(void *p, size_t n, int exp, bool abort);
43+
void __asan_abi_store_n(void *p, size_t n, bool abort);
44+
void __asan_abi_exp_store_n(void *p, size_t n, int exp, bool abort);
45+
46+
// Functions concerning query about whether memory is poisoned
47+
int __asan_abi_address_is_poisoned(void const volatile *p);
48+
void *__asan_abi_region_is_poisoned(void const volatile *p, size_t size);
49+
50+
// Functions concerning the poisoning of memory
51+
void __asan_abi_unpoison_memory_region(void const volatile *p, size_t n);
52+
void __asan_abi_poison_memory_region(void const volatile *p, size_t n);
53+
54+
// Functions concerning the partial poisoning of memory
55+
void __asan_abi_set_shadow_xx_n(void *p, unsigned char xx, size_t n);
56+
57+
// Functions concerning stack poisoning
58+
void __asan_abi_poison_stack_memory(void *p, size_t n);
59+
void __asan_abi_unpoison_stack_memory(void *p, size_t n);
60+
61+
// Functions concerning redzone poisoning
62+
void __asan_abi_poison_intra_object_redzone(void *p, size_t size);
63+
void __asan_abi_unpoison_intra_object_redzone(void *p, size_t size);
64+
65+
// Functions concerning array cookie poisoning
66+
void __asan_abi_poison_cxx_array_cookie(void *p);
67+
void *__asan_abi_load_cxx_array_cookie(void **p);
68+
69+
// Functions concerning fake stacks
70+
void *__asan_abi_get_current_fake_stack();
71+
void *__asan_abi_addr_is_in_fake_stack(void *fake_stack, void *addr, void **beg,
72+
void **end);
73+
// Functions concerning poisoning and unpoisoning fake stack alloca
74+
void __asan_abi_alloca_poison(void *addr, size_t size);
75+
void __asan_abi_allocas_unpoison(void *top, void *bottom);
76+
77+
// Functions concerning fake stack malloc
78+
void *__asan_abi_stack_malloc_n(size_t scale, size_t size);
79+
void *__asan_abi_stack_malloc_always_n(size_t scale, size_t size);
80+
81+
// Functions concerning fake stack free
82+
void __asan_abi_stack_free_n(int scale, void *p, size_t n);
83+
}
84+
#endif // ASAN_ABI_H

0 commit comments

Comments
 (0)