<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/61262>61262</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            TSan false positive with dispatch_apply() over an Objective-C NSArray (on macOS)
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          seanm
      </td>
    </tr>
</table>

<pre>
    On macOS 12.x and 13.x on both x86_64 and arm64 with Xcode this simple code in a toy app:

```objective-c
NSArray* array = @[@1, @2, @3, @4, @5, @6, @7];

dispatch_apply([array count], DISPATCH_APPLY_AUTO, ^(size_t idx) {
        NSNumber* unused = array[idx]; // TSan says race here?!
        (void)unused;
});
```

Causes a TSan warning:

```
WARNING: ThreadSanitizer: data race (pid=33556)
 Read of size 8 at 0x00010b863380 by thread T6:
    #0 __45-[AppDelegate applicationDidFinishLaunching:]_block_invoke <null> (TSanTest:arm64+0x100002d4c)
    #1 __wrap_dispatch_apply_block_invoke <null> (libclang_rt.tsan_osx_dynamic.dylib:arm64+0x7db4c)
    #2 _dispatch_client_callout2 <null> (libdispatch.dylib:arm64e+0x6a10)

 Previous write of size 8 at 0x00010b863380 by main thread:
    #0 __copy_helper_block_e8_32s <null> (TSanTest:arm64+0x100002874)
    #1 _call_copy_helpers_excp <null> (libsystem_blocks.dylib:arm64e+0x2120)
 #2 -[AppDelegate applicationDidFinishLaunching:] <null> (TSanTest:arm64+0x100002c9c)
    #3 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ <null> (CoreFoundation:arm64e+0x7655c)
    #4 <null> <null> (0x0001b16b7e50)

  Location is heap block of size 40 at 0x00010b863360 allocated by main thread:
    #0 malloc <null> (libclang_rt.tsan_osx_dynamic.dylib:arm64+0x54e90)
    #1 _Block_copy <null> (libsystem_blocks.dylib:arm64e+0x1b30)
    #2 -[AppDelegate applicationDidFinishLaunching:] <null> (TSanTest:arm64+0x100002c9c)
    #3 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ <null> (CoreFoundation:arm64e+0x7655c)
    #4 <null> <null> (0x0001b16b7e50)

  Thread T6 (tid=1384939, running) is a GCD worker thread
```

I'm 99% sure there's no bug in my example code above.

I figure this is either:

1. A TSan false positive, hence this ticket
2. a macOS bug (I filed FB9995377 too)

(I'd appreciate if anyone knows which.)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsVk1v4zgS_TX0pRCDIvV58MEfca-BwA5i9-7OiaCkisWJTAoiFdv96weUnKQTp6cbPdcBgjCSWa9evXpxlbRW7TXihEQzEi1GsnOVaScWpT6MclOeJxsNB1lsthCw8QmkLiHg4xMYDblxFZzSWMRh_162hziEo3IV_L8wJYKrlAWrDk2N0L9QGiQ4cwbZNIRPCV0Q-vI7psOPyf_EwqlnvCmGT9bbadvKM2FTkP4PIHwBJKSecUgDwub-iV1OfjnDyxldzvhyJiRaED77PnWpbCNdUQnZNPWZsJREsyFTYTrtfACbw2K1vZ_u5v8R0_v7uz_E9Otu00NGt4SlVn1D4UCVJ8IyIMkrfrberrtDjq2n3-nOYtnz7_FJNPMRPSEgbEnYEnZbqcHKs4VWFggVtkj4krDgFZGw9NmokrBswHurJlkQlr09vij6fa1z2Vm0IIc8R9lqpfc_asXw-L_pw3q1_kL4FHZVi7LcSq2c-oatf1VKJweqhKWNKglfcB5FsWfSh8MDyhLMI3iNIAXpgJ4opQHN05jzlEJ-BtcDwy5-pQIAQBinIEQY3ZBoNm2aBda4lw69fWpVSKeMXqhyqbSy1Z3sdFFdqokWIq9N8SSUfjZPCITPdVfXhN96mr72HVpH-LT3LGEzegoopZSVYfHGfKAQgBDHVjbivU_-LkGt8qKWei9aN3ZWamHsSZRnLQ-qGJfnWuXvUidlfp2WwVvColaonShkXZvOsetsLzffg2OPHsuAvoJfUty3-KxMZ-HYKoc_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