Skip to content

Commit 020cdaf

Browse files
committed
[Asan][Darwin][GCD] Add interceptor for dispatch_mach_create_f
When enabling DriverKit, Address Sanitizer was unable to intercept thread creation directly for dispatch workerthreads. Because of this calls to GetStackTraceFromID failed and ASan was unable to capture a meaningful stack trace. This patch adds an interceptor for a dispatch function as a proxy that is "close enough" to thread creation so that ASan is able to meaningfully capture and register the dispatched thread. Note: I propose not adding a test for this change. Because this change is only meaningful in such a narrow usecase on Darwin and is incredibly difficult to add a meaningful test. Differential Revision: https://reviews.llvm.org/D154753
1 parent b16372c commit 020cdaf

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

compiler-rt/lib/asan/asan_mac.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,18 @@ typedef void* dispatch_source_t;
130130
typedef u64 dispatch_time_t;
131131
typedef void (*dispatch_function_t)(void *block);
132132
typedef void* (*worker_t)(void *block);
133+
typedef unsigned long dispatch_mach_reason;
134+
typedef void *dispatch_mach_msg_t;
135+
typedef int mach_error_t;
136+
typedef void *dispatch_mach_t;
137+
138+
typedef void (*dispatch_mach_handler_function_t)(void *context,
139+
dispatch_mach_reason reason,
140+
dispatch_mach_msg_t message,
141+
mach_error_t error);
142+
typedef void (^dispatch_mach_handler_t)(dispatch_mach_reason reason,
143+
dispatch_mach_msg_t message,
144+
mach_error_t error);
133145

134146
// A wrapper for the ObjC blocks used to support libdispatch.
135147
typedef struct {
@@ -241,6 +253,8 @@ void dispatch_after(dispatch_time_t when, dispatch_queue_t queue,
241253
void dispatch_source_set_cancel_handler(dispatch_source_t ds,
242254
void(^work)(void));
243255
void dispatch_source_set_event_handler(dispatch_source_t ds, void(^work)(void));
256+
dispatch_mach_t dispatch_mach_create(const char *label, dispatch_queue_t queue,
257+
dispatch_mach_handler_t handler);
244258
}
245259

246260
#define GET_ASAN_BLOCK(work) \
@@ -290,6 +304,34 @@ INTERCEPTOR(void, dispatch_source_set_event_handler,
290304
GET_ASAN_BLOCK(work);
291305
REAL(dispatch_source_set_event_handler)(ds, asan_block);
292306
}
307+
308+
INTERCEPTOR(void *, dispatch_mach_create, const char *label,
309+
dispatch_queue_t dq, dispatch_mach_handler_t handler) {
310+
int parent_tid = GetCurrentTidOrInvalid();
311+
return REAL(dispatch_mach_create)(
312+
label, dq,
313+
^(dispatch_mach_reason reason, dispatch_mach_msg_t message,
314+
mach_error_t error) {
315+
GET_STACK_TRACE_THREAD;
316+
asan_register_worker_thread(parent_tid, &stack);
317+
handler(reason, message, error);
318+
});
319+
}
320+
321+
INTERCEPTOR(void *, dispatch_mach_create_f, const char *label,
322+
dispatch_queue_t dq, void *ctxt,
323+
dispatch_mach_handler_function_t handler) {
324+
int parent_tid = GetCurrentTidOrInvalid();
325+
return REAL(dispatch_mach_create)(
326+
label, dq,
327+
^(dispatch_mach_reason reason, dispatch_mach_msg_t message,
328+
mach_error_t error) {
329+
GET_STACK_TRACE_THREAD;
330+
asan_register_worker_thread(parent_tid, &stack);
331+
handler(ctxt, reason, message, error);
332+
});
333+
}
334+
293335
#endif
294336

295337
#endif // SANITIZER_APPLE

0 commit comments

Comments
 (0)