Skip to content

Commit d4e03d5

Browse files
committed
[sanitizer_common] Fuchsia support for interceptors
Summary: Actually Fuchsia non-support for interceptors. Fuchsia doesn't use interceptors in the common sense at all. Almost all system library functions don't need interception at all, because the system libraries are just themselves compiled with sanitizers enabled and have specific hook interfaces where needed to inform the sanitizer runtime about thread lifetimes and the like. For the few functions that do get intercepted, they don't use a generic mechanism like dlsym with RTLD_NEXT to find the underlying system library function. Instead, they use specific extra symbol names published by the system library (e.g. __unsanitized_memcpy). Submitted on behalf of Roland McGrath. Reviewers: vitalybuka, alekseyshl, kcc, filcab Reviewed By: filcab Subscribers: kubamracek, phosek, filcab, llvm-commits Tags: #sanitizers Differential Revision: https://reviews.llvm.org/D36028 llvm-svn: 309745
1 parent eacf4e4 commit d4e03d5

File tree

2 files changed

+55
-24
lines changed

2 files changed

+55
-24
lines changed

compiler-rt/lib/interception/interception.h

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#ifndef INTERCEPTION_H
1616
#define INTERCEPTION_H
1717

18-
#if !defined(__linux__) && !defined(__FreeBSD__) && \
19-
!defined(__APPLE__) && !defined(_WIN32)
18+
#if !defined(__linux__) && !defined(__FreeBSD__) && !defined(__APPLE__) && \
19+
!defined(_WIN32) && !defined(__Fuchsia__)
2020
# error "Interception doesn't work on this operating system."
2121
#endif
2222

@@ -139,7 +139,7 @@ const interpose_substitution substitution_##func_name[] \
139139
# define DECLARE_WRAPPER(ret_type, func, ...) \
140140
extern "C" ret_type func(__VA_ARGS__) \
141141
__attribute__((alias("__interceptor_" #func), visibility("default")));
142-
#else
142+
#elif !defined(__Fuchsia__)
143143
# define WRAP(x) __interceptor_ ## x
144144
# define WRAPPER_NAME(x) "__interceptor_" #x
145145
# define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
@@ -148,7 +148,15 @@ const interpose_substitution substitution_##func_name[] \
148148
__attribute__((weak, alias("__interceptor_" #func), visibility("default")));
149149
#endif
150150

151-
#if !defined(__APPLE__)
151+
#if defined(__Fuchsia__)
152+
// There is no general interception at all on Fuchsia.
153+
// Sanitizer runtimes just define functions directly to preempt them,
154+
// and have bespoke ways to access the underlying libc functions.
155+
# include <magenta/sanitizer.h>
156+
# define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
157+
# define REAL(x) __unsanitized_##x
158+
# define DECLARE_REAL(ret_type, func, ...)
159+
#elif !defined(__APPLE__)
152160
# define PTR_TO_REAL(x) real_##x
153161
# define REAL(x) __interception::PTR_TO_REAL(x)
154162
# define FUNC_TYPE(x) x##_f
@@ -166,15 +174,19 @@ const interpose_substitution substitution_##func_name[] \
166174
# define ASSIGN_REAL(x, y)
167175
#endif // __APPLE__
168176

177+
#if !defined(__Fuchsia__)
169178
#define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...) \
170179
DECLARE_REAL(ret_type, func, __VA_ARGS__) \
171180
extern "C" ret_type WRAP(func)(__VA_ARGS__);
181+
#else
182+
#define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...)
183+
#endif
172184

173185
// Generally, you don't need to use DEFINE_REAL by itself, as INTERCEPTOR
174186
// macros does its job. In exceptional cases you may need to call REAL(foo)
175187
// without defining INTERCEPTOR(..., foo, ...). For example, if you override
176188
// foo with an interceptor for other function.
177-
#if !defined(__APPLE__)
189+
#if !defined(__APPLE__) && !defined(__Fuchsia__)
178190
# define DEFINE_REAL(ret_type, func, ...) \
179191
typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
180192
namespace __interception { \
@@ -184,7 +196,18 @@ const interpose_substitution substitution_##func_name[] \
184196
# define DEFINE_REAL(ret_type, func, ...)
185197
#endif
186198

187-
#if !defined(__APPLE__)
199+
#if defined(__Fuchsia__)
200+
201+
// We need to define the __interceptor_func name just to get
202+
// sanitizer_common/scripts/gen_dynamic_list.py to export func.
203+
// But we don't need to export __interceptor_func to get that.
204+
#define INTERCEPTOR(ret_type, func, ...) \
205+
extern "C"[[ gnu::alias(#func), gnu::visibility("hidden") ]] ret_type \
206+
__interceptor_##func(__VA_ARGS__); \
207+
extern "C" INTERCEPTOR_ATTRIBUTE ret_type func(__VA_ARGS__)
208+
209+
#elif !defined(__APPLE__)
210+
188211
#define INTERCEPTOR(ret_type, func, ...) \
189212
DEFINE_REAL(ret_type, func, __VA_ARGS__) \
190213
DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \
@@ -251,7 +274,7 @@ typedef unsigned long uptr; // NOLINT
251274
# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_MAC(func)
252275
# define INTERCEPT_FUNCTION_VER(func, symver) \
253276
INTERCEPT_FUNCTION_VER_MAC(func, symver)
254-
#else // defined(_WIN32)
277+
#elif defined(_WIN32)
255278
# include "interception_win.h"
256279
# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_WIN(func)
257280
# define INTERCEPT_FUNCTION_VER(func, symver) \

compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
# define SI_WINDOWS 1
2929
#endif
3030

31-
#if (SI_POSIX != 0) == (SI_WINDOWS != 0)
31+
#if (SI_POSIX != 0) == (SI_WINDOWS != 0) && !SANITIZER_FUCHSIA
3232
# error "Windows is not POSIX!"
3333
#endif
3434

@@ -80,6 +80,12 @@
8080
# define SI_IOS 0
8181
#endif
8282

83+
#if SANITIZER_FUCHSIA
84+
# define SI_NOT_FUCHSIA 0
85+
#else
86+
# define SI_NOT_FUCHSIA 1
87+
#endif
88+
8389
#if SANITIZER_POSIX && !SANITIZER_MAC
8490
# define SI_POSIX_NOT_MAC 1
8591
#else
@@ -92,23 +98,23 @@
9298
# define SI_LINUX_NOT_FREEBSD 0
9399
#endif
94100

95-
#define SANITIZER_INTERCEPT_STRLEN 1
96-
#define SANITIZER_INTERCEPT_STRNLEN SI_NOT_MAC
97-
#define SANITIZER_INTERCEPT_STRCMP 1
98-
#define SANITIZER_INTERCEPT_STRSTR 1
101+
#define SANITIZER_INTERCEPT_STRLEN SI_NOT_FUCHSIA
102+
#define SANITIZER_INTERCEPT_STRNLEN (SI_NOT_MAC && SI_NOT_FUCHSIA)
103+
#define SANITIZER_INTERCEPT_STRCMP SI_NOT_FUCHSIA
104+
#define SANITIZER_INTERCEPT_STRSTR SI_NOT_FUCHSIA
99105
#define SANITIZER_INTERCEPT_STRCASESTR SI_POSIX
100-
#define SANITIZER_INTERCEPT_STRTOK 1
101-
#define SANITIZER_INTERCEPT_STRCHR 1
102-
#define SANITIZER_INTERCEPT_STRCHRNUL SI_UNIX_NOT_MAC
103-
#define SANITIZER_INTERCEPT_STRRCHR 1
104-
#define SANITIZER_INTERCEPT_STRSPN 1
105-
#define SANITIZER_INTERCEPT_STRPBRK 1
106+
#define SANITIZER_INTERCEPT_STRTOK SI_NOT_FUCHSIA
107+
#define SANITIZER_INTERCEPT_STRCHR SI_NOT_FUCHSIA
108+
#define SANITIZER_INTERCEPT_STRCHRNUL SI_POSIX_NOT_MAC
109+
#define SANITIZER_INTERCEPT_STRRCHR SI_NOT_FUCHSIA
110+
#define SANITIZER_INTERCEPT_STRSPN SI_NOT_FUCHSIA
111+
#define SANITIZER_INTERCEPT_STRPBRK SI_NOT_FUCHSIA
106112
#define SANITIZER_INTERCEPT_TEXTDOMAIN SI_LINUX_NOT_ANDROID
107113
#define SANITIZER_INTERCEPT_STRCASECMP SI_POSIX
108114
#define SANITIZER_INTERCEPT_MEMSET 1
109115
#define SANITIZER_INTERCEPT_MEMMOVE 1
110116
#define SANITIZER_INTERCEPT_MEMCPY 1
111-
#define SANITIZER_INTERCEPT_MEMCMP 1
117+
#define SANITIZER_INTERCEPT_MEMCMP SI_NOT_FUCHSIA
112118
#define SANITIZER_INTERCEPT_STRNDUP SI_POSIX
113119
#define SANITIZER_INTERCEPT___STRNDUP SI_LINUX_NOT_FREEBSD
114120
#if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \
@@ -120,7 +126,7 @@
120126
// memmem on Darwin doesn't exist on 10.6
121127
// FIXME: enable memmem on Windows.
122128
#define SANITIZER_INTERCEPT_MEMMEM (SI_POSIX && !SI_MAC_DEPLOYMENT_BELOW_10_7)
123-
#define SANITIZER_INTERCEPT_MEMCHR 1
129+
#define SANITIZER_INTERCEPT_MEMCHR SI_NOT_FUCHSIA
124130
#define SANITIZER_INTERCEPT_MEMRCHR (SI_FREEBSD || SI_LINUX || SI_NETBSD)
125131

126132
#define SANITIZER_INTERCEPT_READ SI_POSIX
@@ -157,7 +163,7 @@
157163
# define SANITIZER_INTERCEPT_ISOC99_PRINTF SI_LINUX_NOT_ANDROID
158164
#endif
159165

160-
#define SANITIZER_INTERCEPT_FREXP 1
166+
#define SANITIZER_INTERCEPT_FREXP SI_NOT_FUCHSIA
161167
#define SANITIZER_INTERCEPT_FREXPF_FREXPL SI_POSIX
162168

163169
#define SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS SI_POSIX
@@ -372,10 +378,12 @@
372378
(SI_LINUX_NOT_ANDROID || SI_MAC || SI_FREEBSD || SI_NETBSD)
373379

374380
#define SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO \
375-
(!SI_FREEBSD && !SI_MAC && !SI_NETBSD)
381+
(!SI_FREEBSD && !SI_MAC && !SI_NETBSD && SI_NOT_FUCHSIA)
376382
#define SANITIZER_INTERCEPT_MEMALIGN (!SI_FREEBSD && !SI_MAC && !SI_NETBSD)
377-
#define SANITIZER_INTERCEPT_PVALLOC (!SI_FREEBSD && !SI_MAC && !SI_NETBSD)
378-
#define SANITIZER_INTERCEPT_CFREE (!SI_FREEBSD && !SI_MAC && !SI_NETBSD)
383+
#define SANITIZER_INTERCEPT_PVALLOC \
384+
(!SI_FREEBSD && !SI_MAC && !SI_NETBSD && SI_NOT_FUCHSIA)
385+
#define SANITIZER_INTERCEPT_CFREE \
386+
(!SI_FREEBSD && !SI_MAC && !SI_NETBSD && SI_NOT_FUCHSIA)
379387
#define SANITIZER_INTERCEPT_ALIGNED_ALLOC (!SI_MAC)
380388
#define SANITIZER_INTERCEPT_MALLOC_USABLE_SIZE (!SI_MAC)
381389
#define SANITIZER_INTERCEPT_MCHECK_MPROBE SI_LINUX_NOT_ANDROID

0 commit comments

Comments
 (0)