xref: /aosp_15_r20/external/compiler-rt/test/tsan/Darwin/objc-race.mm (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot// RUN: %clang_tsan %s -o %t -framework Foundation
2*7c3d14c8STreehugger Robot// RUN: %deflake %run %t 2>&1 | FileCheck %s
3*7c3d14c8STreehugger Robot
4*7c3d14c8STreehugger Robot#import <Foundation/Foundation.h>
5*7c3d14c8STreehugger Robot
6*7c3d14c8STreehugger Robot#import "../test.h"
7*7c3d14c8STreehugger Robot
8*7c3d14c8STreehugger Robot@interface MyClass : NSObject {
9*7c3d14c8STreehugger Robot  long instance_variable;
10*7c3d14c8STreehugger Robot}
11*7c3d14c8STreehugger Robot- (void)method:(long)value;
12*7c3d14c8STreehugger Robot@end
13*7c3d14c8STreehugger Robot
14*7c3d14c8STreehugger Robot@implementation MyClass
15*7c3d14c8STreehugger Robot
16*7c3d14c8STreehugger Robot- (void)method:(long)value {
17*7c3d14c8STreehugger Robot  self->instance_variable = value;
18*7c3d14c8STreehugger Robot}
19*7c3d14c8STreehugger Robot
20*7c3d14c8STreehugger Robot@end
21*7c3d14c8STreehugger Robot
22*7c3d14c8STreehugger Robotint main() {
23*7c3d14c8STreehugger Robot  NSLog(@"Hello world.");
24*7c3d14c8STreehugger Robot  barrier_init(&barrier, 2);
25*7c3d14c8STreehugger Robot
26*7c3d14c8STreehugger Robot  MyClass *my_object = [MyClass new];
27*7c3d14c8STreehugger Robot  [my_object method:42];
28*7c3d14c8STreehugger Robot
29*7c3d14c8STreehugger Robot  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
30*7c3d14c8STreehugger Robot    [my_object method:43];
31*7c3d14c8STreehugger Robot    barrier_wait(&barrier);
32*7c3d14c8STreehugger Robot  });
33*7c3d14c8STreehugger Robot
34*7c3d14c8STreehugger Robot  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
35*7c3d14c8STreehugger Robot    barrier_wait(&barrier);
36*7c3d14c8STreehugger Robot    [my_object method:44];
37*7c3d14c8STreehugger Robot
38*7c3d14c8STreehugger Robot    dispatch_sync(dispatch_get_main_queue(), ^{
39*7c3d14c8STreehugger Robot      CFRunLoopStop(CFRunLoopGetCurrent());
40*7c3d14c8STreehugger Robot    });
41*7c3d14c8STreehugger Robot  });
42*7c3d14c8STreehugger Robot
43*7c3d14c8STreehugger Robot  CFRunLoopRun();
44*7c3d14c8STreehugger Robot  NSLog(@"Done.");
45*7c3d14c8STreehugger Robot  return 0;
46*7c3d14c8STreehugger Robot}
47*7c3d14c8STreehugger Robot
48*7c3d14c8STreehugger Robot// CHECK: Hello world.
49*7c3d14c8STreehugger Robot// CHECK: WARNING: ThreadSanitizer: data race
50*7c3d14c8STreehugger Robot// CHECK:   Write of size 8
51*7c3d14c8STreehugger Robot// CHECK:     #0 -[MyClass method:]
52*7c3d14c8STreehugger Robot// CHECK:   Previous write of size 8
53*7c3d14c8STreehugger Robot// CHECK:     #0 -[MyClass method:]
54*7c3d14c8STreehugger Robot// CHECK:   Location is heap block
55*7c3d14c8STreehugger Robot// CHECK: Done.
56