Skip to content

Commit e7e7494

Browse files
authored
[TSan] Fix asan_mac.cpp function pointer cast warnings (#151517)
Fixes these compiler warnings: ``` .../llvm-project/compiler-rt/lib/asan/asan_mac.cpp:252:4: warning: cast from 'dispatch_function_t' (aka 'void (*)(void *)') to 'void (*)(void *, size_t)' (aka 'void (*)(void *, unsigned long)') converts to incompatible function type [-Wcast-function-type-mismatch] 252 | ((void (*)(void *, size_t))asan_ctxt->func)(asan_ctxt->block, iteration); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .../llvm-project/compiler-rt/lib/asan/asan_mac.cpp:259:32: warning: cast from 'void (*)(void *, size_t)' (aka 'void (*)(void *, unsigned long)') to 'dispatch_function_t' (aka 'void (*)(void *)') converts to incompatible function type [-Wcast-function-type-mismatch] 259 | alloc_asan_context(ctxt, (dispatch_function_t)work, &stack); | ^~~~~~~~~~~~~~~~~~~~~~~~~ ```
1 parent 89c1da6 commit e7e7494

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

compiler-rt/lib/asan/asan_mac.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ typedef void* dispatch_queue_t;
130130
typedef void* dispatch_source_t;
131131
typedef u64 dispatch_time_t;
132132
typedef void (*dispatch_function_t)(void *block);
133+
typedef void (*dispatch_apply_function_t)(void *, size_t);
133134
typedef void* (*worker_t)(void *block);
134135
typedef unsigned long dispatch_mach_reason;
135136
typedef void *dispatch_mach_msg_t;
@@ -149,7 +150,11 @@ typedef void (^dispatch_mach_handler_t)(dispatch_mach_reason reason,
149150
// A wrapper for the ObjC blocks used to support libdispatch.
150151
typedef struct {
151152
void *block;
152-
dispatch_function_t func;
153+
union {
154+
dispatch_function_t dispatch_func;
155+
dispatch_apply_function_t dispatch_apply_func;
156+
static_assert(sizeof(dispatch_func) == sizeof(dispatch_apply_func));
157+
};
153158
u32 parent_tid;
154159
} asan_block_context_t;
155160

@@ -177,7 +182,7 @@ void asan_dispatch_call_block_and_release(void *block) {
177182
block, (void*)pthread_self());
178183
asan_register_worker_thread(context->parent_tid, &stack);
179184
// Call the original dispatcher for the block.
180-
context->func(context->block);
185+
context->dispatch_func(context->block);
181186
asan_free(context, &stack);
182187
}
183188

@@ -193,7 +198,7 @@ asan_block_context_t *alloc_asan_context(void *ctxt, dispatch_function_t func,
193198
asan_block_context_t *asan_ctxt =
194199
(asan_block_context_t*) asan_malloc(sizeof(asan_block_context_t), stack);
195200
asan_ctxt->block = ctxt;
196-
asan_ctxt->func = func;
201+
asan_ctxt->dispatch_func = func;
197202
asan_ctxt->parent_tid = GetCurrentTidOrInvalid();
198203
return asan_ctxt;
199204
}
@@ -249,14 +254,17 @@ extern "C" void asan_dispatch_apply_f_work(void *context, size_t iteration) {
249254
GET_STACK_TRACE_THREAD;
250255
asan_block_context_t *asan_ctxt = (asan_block_context_t *)context;
251256
asan_register_worker_thread(asan_ctxt->parent_tid, &stack);
252-
((void (*)(void *, size_t))asan_ctxt->func)(asan_ctxt->block, iteration);
257+
asan_ctxt->dispatch_apply_func(asan_ctxt->block, iteration);
253258
}
254259

255260
INTERCEPTOR(void, dispatch_apply_f, size_t iterations, dispatch_queue_t queue,
256-
void *ctxt, void (*work)(void *, size_t)) {
261+
void *ctxt, dispatch_apply_function_t work) {
257262
GET_STACK_TRACE_THREAD;
258263
asan_block_context_t *asan_ctxt =
259-
alloc_asan_context(ctxt, (dispatch_function_t)work, &stack);
264+
(asan_block_context_t *)asan_malloc(sizeof(asan_block_context_t), &stack);
265+
asan_ctxt->block = ctxt;
266+
asan_ctxt->dispatch_apply_func = work;
267+
asan_ctxt->parent_tid = GetCurrentTidOrInvalid();
260268
REAL(dispatch_apply_f)(iterations, queue, (void *)asan_ctxt,
261269
asan_dispatch_apply_f_work);
262270
}

0 commit comments

Comments
 (0)