Skip to content

Conversation

DanBlackwell
Copy link
Contributor

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);
      |                                ^~~~~~~~~~~~~~~~~~~~~~~~~

@llvmbot
Copy link
Member

llvmbot commented Jul 31, 2025

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Dan Blackwell (DanBlackwell)

Changes

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);
      |                                ^~~~~~~~~~~~~~~~~~~~~~~~~

Full diff: https://github.com/llvm/llvm-project/pull/151517.diff

1 Files Affected:

  • (modified) compiler-rt/lib/asan/asan_mac.cpp (+18-10)
diff --git a/compiler-rt/lib/asan/asan_mac.cpp b/compiler-rt/lib/asan/asan_mac.cpp
index 1f3c79eec125b..99fea6de595d7 100644
--- a/compiler-rt/lib/asan/asan_mac.cpp
+++ b/compiler-rt/lib/asan/asan_mac.cpp
@@ -130,6 +130,7 @@ typedef void* dispatch_queue_t;
 typedef void* dispatch_source_t;
 typedef u64 dispatch_time_t;
 typedef void (*dispatch_function_t)(void *block);
+typedef void (*dispatch_apply_function_t)(void *, size_t);
 typedef void* (*worker_t)(void *block);
 typedef unsigned long dispatch_mach_reason;
 typedef void *dispatch_mach_msg_t;
@@ -149,7 +150,11 @@ typedef void (^dispatch_mach_handler_t)(dispatch_mach_reason reason,
 // A wrapper for the ObjC blocks used to support libdispatch.
 typedef struct {
   void *block;
-  dispatch_function_t func;
+  union {
+    dispatch_function_t dispatch_func;
+    dispatch_apply_function_t dispatch_apply_func;
+    static_assert(sizeof(dispatch_func) == sizeof(dispatch_apply_func));
+  };
   u32 parent_tid;
 } asan_block_context_t;
 
@@ -177,7 +182,7 @@ void asan_dispatch_call_block_and_release(void *block) {
           block, (void*)pthread_self());
   asan_register_worker_thread(context->parent_tid, &stack);
   // Call the original dispatcher for the block.
-  context->func(context->block);
+  context->dispatch_func(context->block);
   asan_free(context, &stack);
 }
 
@@ -193,7 +198,7 @@ asan_block_context_t *alloc_asan_context(void *ctxt, dispatch_function_t func,
   asan_block_context_t *asan_ctxt =
       (asan_block_context_t*) asan_malloc(sizeof(asan_block_context_t), stack);
   asan_ctxt->block = ctxt;
-  asan_ctxt->func = func;
+  asan_ctxt->dispatch_func = func;
   asan_ctxt->parent_tid = GetCurrentTidOrInvalid();
   return asan_ctxt;
 }
@@ -249,14 +254,17 @@ extern "C" void asan_dispatch_apply_f_work(void *context, size_t iteration) {
   GET_STACK_TRACE_THREAD;
   asan_block_context_t *asan_ctxt = (asan_block_context_t *)context;
   asan_register_worker_thread(asan_ctxt->parent_tid, &stack);
-  ((void (*)(void *, size_t))asan_ctxt->func)(asan_ctxt->block, iteration);
+  asan_ctxt->dispatch_apply_func(asan_ctxt->block, iteration);
 }
 
 INTERCEPTOR(void, dispatch_apply_f, size_t iterations, dispatch_queue_t queue,
-            void *ctxt, void (*work)(void *, size_t)) {
+            void *ctxt, dispatch_apply_function_t work) {
   GET_STACK_TRACE_THREAD;
   asan_block_context_t *asan_ctxt =
-      alloc_asan_context(ctxt, (dispatch_function_t)work, &stack);
+      (asan_block_context_t *)asan_malloc(sizeof(asan_block_context_t), &stack);
+  asan_ctxt->block = ctxt;
+  asan_ctxt->dispatch_apply_func = work;
+  asan_ctxt->parent_tid = GetCurrentTidOrInvalid();
   REAL(dispatch_apply_f)(iterations, queue, (void *)asan_ctxt,
                          asan_dispatch_apply_f_work);
 }
@@ -280,10 +288,10 @@ dispatch_mach_t dispatch_mach_create(const char *label, dispatch_queue_t queue,
 #define GET_ASAN_BLOCK(work) \
   void (^asan_block)(void);  \
   int parent_tid = GetCurrentTidOrInvalid(); \
-  asan_block = ^(void) { \
-    GET_STACK_TRACE_THREAD; \
-    asan_register_worker_thread(parent_tid, &stack); \
-    work(); \
+  asan_block = ^(void) {                   \
+        GET_STACK_TRACE_THREAD;                          \
+        asan_register_worker_thread(parent_tid, &stack); \
+        work(); \
   }
 
 INTERCEPTOR(void, dispatch_async,

Copy link

github-actions bot commented Jul 31, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff HEAD~1 HEAD --extensions cpp -- compiler-rt/lib/asan/asan_mac.cpp
View the diff from clang-format here.
diff --git a/compiler-rt/lib/asan/asan_mac.cpp b/compiler-rt/lib/asan/asan_mac.cpp
index a68e362e0..99fea6de5 100644
--- a/compiler-rt/lib/asan/asan_mac.cpp
+++ b/compiler-rt/lib/asan/asan_mac.cpp
@@ -288,10 +288,10 @@ dispatch_mach_t dispatch_mach_create(const char *label, dispatch_queue_t queue,
 #define GET_ASAN_BLOCK(work) \
   void (^asan_block)(void);  \
   int parent_tid = GetCurrentTidOrInvalid(); \
-  asan_block = ^(void) { \
-    GET_STACK_TRACE_THREAD; \
-    asan_register_worker_thread(parent_tid, &stack); \
-    work(); \
+  asan_block = ^(void) {                   \
+        GET_STACK_TRACE_THREAD;                          \
+        asan_register_worker_thread(parent_tid, &stack); \
+        work(); \
   }
 
 INTERCEPTOR(void, dispatch_async,

@DanBlackwell DanBlackwell force-pushed the fix-asan-mac-warnings branch from 5ca1b54 to 1c4e83d Compare July 31, 2025 13:40
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change related to the fix or is it to appease clang format?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was indeed for clang-format. But I didn't touch that code, so I've reset it back now.

@DanBlackwell DanBlackwell force-pushed the fix-asan-mac-warnings branch from 1c4e83d to 0a743fa Compare August 1, 2025 10:36
@thetruestblue
Copy link
Contributor

Thanks for fixing this up after it got past me.

@thetruestblue thetruestblue merged commit e7e7494 into llvm:main Aug 1, 2025
8 of 9 checks passed
thetruestblue pushed a commit to swiftlang/llvm-project that referenced this pull request Sep 14, 2025
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);
      |                                ^~~~~~~~~~~~~~~~~~~~~~~~~
```

(cherry picked from commit e7e7494)
thetruestblue added a commit to swiftlang/llvm-project that referenced this pull request Sep 14, 2025
Author: Dan Blackwell [email protected]
Date: Fri Aug 1 16:33:23 2025 +0100

[TSan] Fix asan_mac.cpp function pointer cast warnings (llvm#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);
          |                                ^~~~~~~~~~~~~~~~~~~~~~~~~
    
(cherry picked from commit e7e7494)

[ASan][Darwin][GCD] Add interceptor for dispatch_apply (llvm#149238)

ASan had a gap in coverage for wqthreads blocks submitted by dispatch_apply

This adds interceptor for dispatch_apply and dispatch_apply_f and adds a test that a failure in a dispatch apply block contains thread and stack info.

rdar://139660648
(cherry picked from commit 5ce04b4)
thetruestblue pushed a commit to swiftlang/llvm-project that referenced this pull request Sep 15, 2025
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);
      |                                ^~~~~~~~~~~~~~~~~~~~~~~~~
```

(cherry picked from commit e7e7494)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants