Skip to content

Commit 181b63f

Browse files
committed
Add dispatch_apply_f interceptor
1 parent 267f96a commit 181b63f

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

compiler-rt/lib/asan/asan_mac.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ void FlushUnneededASanShadowMemory(uptr p, uptr size) {
104104
// dispatch_group_async_f()
105105
// dispatch_group_async()
106106
// dispatch_apply()
107+
// dispatch_apply_f()
107108
// TODO(glider): libdispatch API contains other functions that we don't support
108109
// yet.
109110
//
@@ -244,7 +245,24 @@ INTERCEPTOR(void, dispatch_group_async_f, dispatch_group_t group,
244245
asan_dispatch_call_block_and_release);
245246
}
246247

247-
#if !defined(MISSING_BLOCKS_SUPPORT)
248+
extern "C" void asan_dispatch_apply_f_block(void *context, size_t iteration) {
249+
GET_STACK_TRACE_THREAD;
250+
asan_block_context_t *asan_ctxt = (asan_block_context_t *)context;
251+
asan_register_worker_thread(asan_ctxt->parent_tid, &stack);
252+
((void (*)(void *, size_t))asan_ctxt->func)(asan_ctxt->block, iteration);
253+
}
254+
255+
INTERCEPTOR(void, dispatch_apply_f, size_t iterations, dispatch_queue_t queue,
256+
void *ctxt, void (*work)(void *, size_t)) {
257+
GET_STACK_TRACE_THREAD;
258+
asan_block_context_t *asan_ctxt =
259+
alloc_asan_context(ctxt, (dispatch_function_t)work, &stack);
260+
261+
REAL(dispatch_apply_f)
262+
(iterations, queue, (void *)asan_ctxt, asan_dispatch_apply_f_block);
263+
}
264+
265+
# if !defined(MISSING_BLOCKS_SUPPORT)
248266
extern "C" {
249267
void dispatch_async(dispatch_queue_t dq, void(^work)(void));
250268
void dispatch_group_async(dispatch_group_t dg, dispatch_queue_t dq,

compiler-rt/test/asan/TestCases/Darwin/dispatch_apply_threadno.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// with an empty stack.
33
// This tests that dispatch_apply blocks can capture valid thread number and stack.
44

5+
// RUN: %clang_asan -DDISPATCH_APPLY_F %s -o %t
6+
// RUN: not %run %t 2>&1 | FileCheck %s
7+
58
// RUN: %clang_asan %s -o %t
69
// RUN: not %run %t 2>&1 | FileCheck %s
710

@@ -17,8 +20,27 @@ __attribute__((noinline)) void test_dispatch_apply() {
1720
});
1821
}
1922

23+
typedef struct {
24+
char *data;
25+
} Context;
26+
27+
void da_func(void *ctx, size_t i) {
28+
Context *c = (Context *)ctx;
29+
access_memory_frame(&c->data[i]);
30+
}
31+
32+
__attribute__((noinline)) void test_dispatch_apply_f() {
33+
Context *ctx = (Context *)malloc(sizeof(Context));
34+
ctx->data = (char *)malloc(4);
35+
dispatch_apply_f(8, dispatch_get_global_queue(0, 0), ctx, da_func);
36+
}
37+
2038
int main(int argc, const char *argv[]) {
39+
#if DISPATCH_APPLY_F
40+
test_dispatch_apply_f();
41+
#else
2142
test_dispatch_apply();
43+
#endif
2244
return 0;
2345
}
2446

0 commit comments

Comments
 (0)