1 // Mac OS X 10.6 or higher only.
2 #include <dispatch/dispatch.h>
3 #include <pthread.h> // for pthread_yield_np()
9 #import <CoreFoundation/CFBase.h>
10 #import <Foundation/NSObject.h>
11 #import <Foundation/NSURL.h>
13 // This is a (void*)(void*) function so it can be passed to pthread_create.
14 void *CFAllocatorDefaultDoubleFree(void *unused) {
15 void *mem = CFAllocatorAllocate(kCFAllocatorDefault, 5, 0);
16 CFAllocatorDeallocate(kCFAllocatorDefault, mem);
17 CFAllocatorDeallocate(kCFAllocatorDefault, mem);
21 void CFAllocatorSystemDefaultDoubleFree() {
22 void *mem = CFAllocatorAllocate(kCFAllocatorSystemDefault, 5, 0);
23 CFAllocatorDeallocate(kCFAllocatorSystemDefault, mem);
24 CFAllocatorDeallocate(kCFAllocatorSystemDefault, mem);
27 void CFAllocatorMallocDoubleFree() {
28 void *mem = CFAllocatorAllocate(kCFAllocatorMalloc, 5, 0);
29 CFAllocatorDeallocate(kCFAllocatorMalloc, mem);
30 CFAllocatorDeallocate(kCFAllocatorMalloc, mem);
33 void CFAllocatorMallocZoneDoubleFree() {
34 void *mem = CFAllocatorAllocate(kCFAllocatorMallocZone, 5, 0);
35 CFAllocatorDeallocate(kCFAllocatorMallocZone, mem);
36 CFAllocatorDeallocate(kCFAllocatorMallocZone, mem);
39 __attribute__((noinline))
40 void access_memory(char *a) {
44 // Test the +load instrumentation.
45 // Because the +load methods are invoked before anything else is initialized,
46 // it makes little sense to wrap the code below into a gTest test case.
47 // If AddressSanitizer doesn't instrument the +load method below correctly,
48 // everything will just crash.
51 "If your test didn't crash, AddressSanitizer is instrumenting "
52 "the +load methods correctly.";
54 @interface LoadSomething : NSObject {
58 @implementation LoadSomething
61 for (size_t i = 0; i < strlen(kStartupStr); i++) {
62 access_memory(&kStartupStr[i]); // make sure no optimizations occur.
64 // Don't print anything here not to interfere with the death tests.
69 void worker_do_alloc(int size) {
70 char * volatile mem = (char * volatile)malloc(size);
75 void worker_do_crash(int size) {
76 char * volatile mem = (char * volatile)malloc(size);
77 access_memory(&mem[size]); // BOOM
81 // Used by the GCD tests to avoid a race between the worker thread reporting a
82 // memory error and the main thread which may exit with exit code 0 before
85 volatile bool infinite = true;
86 while (infinite) pthread_yield_np();
89 // Tests for the Grand Central Dispatch. See
90 // http://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html
92 void TestGCDDispatchAsync() {
93 dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
94 dispatch_block_t block = ^{ worker_do_crash(1024); };
95 // dispatch_async() runs the task on a worker thread that does not go through
96 // pthread_create(). We need to verify that AddressSanitizer notices that the
97 // thread has started.
98 dispatch_async(queue, block);
102 void TestGCDDispatchSync() {
103 dispatch_queue_t queue = dispatch_get_global_queue(2, 0);
104 dispatch_block_t block = ^{ worker_do_crash(1024); };
105 // dispatch_sync() runs the task on a worker thread that does not go through
106 // pthread_create(). We need to verify that AddressSanitizer notices that the
107 // thread has started.
108 dispatch_sync(queue, block);
112 // libdispatch spawns a rather small number of threads and reuses them. We need
113 // to make sure AddressSanitizer handles the reusing correctly.
114 void TestGCDReuseWqthreadsAsync() {
115 dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
116 dispatch_block_t block_alloc = ^{ worker_do_alloc(1024); };
117 dispatch_block_t block_crash = ^{ worker_do_crash(1024); };
118 for (int i = 0; i < 100; i++) {
119 dispatch_async(queue, block_alloc);
121 dispatch_async(queue, block_crash);
125 // Try to trigger abnormal behaviour of dispatch_sync() being unhandled by us.
126 void TestGCDReuseWqthreadsSync() {
127 dispatch_queue_t queue[4];
128 queue[0] = dispatch_get_global_queue(2, 0);
129 queue[1] = dispatch_get_global_queue(0, 0);
130 queue[2] = dispatch_get_global_queue(-2, 0);
131 queue[3] = dispatch_queue_create("my_queue", NULL);
132 dispatch_block_t block_alloc = ^{ worker_do_alloc(1024); };
133 dispatch_block_t block_crash = ^{ worker_do_crash(1024); };
134 for (int i = 0; i < 1000; i++) {
135 dispatch_sync(queue[i % 4], block_alloc);
137 dispatch_sync(queue[3], block_crash);
141 void TestGCDDispatchAfter() {
142 dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
143 dispatch_block_t block_crash = ^{ worker_do_crash(1024); };
144 // Schedule the event one second from the current time.
145 dispatch_time_t milestone =
146 dispatch_time(DISPATCH_TIME_NOW, 1LL * NSEC_PER_SEC);
147 dispatch_after(milestone, queue, block_crash);
151 void worker_do_deallocate(void *ptr) {
155 void CallFreeOnWorkqueue(void *tsd) {
156 dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
157 dispatch_block_t block_dealloc = ^{ worker_do_deallocate(tsd); };
158 dispatch_async(queue, block_dealloc);
159 // Do not wait for the worker to free the memory -- nobody is going to touch
163 void TestGCDSourceEvent() {
164 dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
165 dispatch_source_t timer =
166 dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
167 // Schedule the timer one second from the current time.
168 dispatch_time_t milestone =
169 dispatch_time(DISPATCH_TIME_NOW, 1LL * NSEC_PER_SEC);
171 dispatch_source_set_timer(timer, milestone, DISPATCH_TIME_FOREVER, 0);
172 char * volatile mem = (char * volatile)malloc(10);
173 dispatch_source_set_event_handler(timer, ^{
174 access_memory(&mem[10]);
176 dispatch_resume(timer);
180 void TestGCDSourceCancel() {
181 dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
182 dispatch_source_t timer =
183 dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
184 // Schedule the timer one second from the current time.
185 dispatch_time_t milestone =
186 dispatch_time(DISPATCH_TIME_NOW, 1LL * NSEC_PER_SEC);
188 dispatch_source_set_timer(timer, milestone, DISPATCH_TIME_FOREVER, 0);
189 char * volatile mem = (char * volatile)malloc(10);
190 // Both dispatch_source_set_cancel_handler() and
191 // dispatch_source_set_event_handler() use dispatch_barrier_async_f().
192 // It's tricky to test dispatch_source_set_cancel_handler() separately,
193 // so we test both here.
194 dispatch_source_set_event_handler(timer, ^{
195 dispatch_source_cancel(timer);
197 dispatch_source_set_cancel_handler(timer, ^{
198 access_memory(&mem[10]);
200 dispatch_resume(timer);
204 void TestGCDGroupAsync() {
205 dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
206 dispatch_group_t group = dispatch_group_create();
207 char * volatile mem = (char * volatile)malloc(10);
208 dispatch_group_async(group, queue, ^{
209 access_memory(&mem[10]);
211 dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
215 @interface FixedArray : NSObject {
220 @implementation FixedArray
221 -(int) access: (int)index {
226 void TestOOBNSObjects() {
227 id anObject = [FixedArray new];
229 [anObject access:11];
233 void TestNSURLDeallocation() {
235 [[NSURL alloc] initWithString:@"file://localhost/Users/glider/Library/"];
237 [[NSURL alloc] initWithString:@"Saved Application State"