2011-11-30 09:07:02 +08:00
|
|
|
// Mac OS X 10.6 or higher only.
|
|
|
|
#include <dispatch/dispatch.h>
|
2013-09-24 20:42:25 +08:00
|
|
|
#include <pthread.h> // for pthread_yield_np()
|
2011-11-30 09:07:02 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#import <CoreFoundation/CFBase.h>
|
|
|
|
#import <Foundation/NSObject.h>
|
2012-06-21 04:28:39 +08:00
|
|
|
#import <Foundation/NSURL.h>
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-06-22 00:08:11 +08:00
|
|
|
// This is a (void*)(void*) function so it can be passed to pthread_create.
|
|
|
|
void *CFAllocatorDefaultDoubleFree(void *unused) {
|
2013-09-24 20:42:25 +08:00
|
|
|
void *mem = CFAllocatorAllocate(kCFAllocatorDefault, 5, 0);
|
2011-11-30 09:07:02 +08:00
|
|
|
CFAllocatorDeallocate(kCFAllocatorDefault, mem);
|
|
|
|
CFAllocatorDeallocate(kCFAllocatorDefault, mem);
|
2012-06-28 20:19:52 +08:00
|
|
|
return 0;
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CFAllocatorSystemDefaultDoubleFree() {
|
2013-09-24 20:42:25 +08:00
|
|
|
void *mem = CFAllocatorAllocate(kCFAllocatorSystemDefault, 5, 0);
|
2011-11-30 09:07:02 +08:00
|
|
|
CFAllocatorDeallocate(kCFAllocatorSystemDefault, mem);
|
|
|
|
CFAllocatorDeallocate(kCFAllocatorSystemDefault, mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CFAllocatorMallocDoubleFree() {
|
2013-09-24 20:42:25 +08:00
|
|
|
void *mem = CFAllocatorAllocate(kCFAllocatorMalloc, 5, 0);
|
2011-11-30 09:07:02 +08:00
|
|
|
CFAllocatorDeallocate(kCFAllocatorMalloc, mem);
|
|
|
|
CFAllocatorDeallocate(kCFAllocatorMalloc, mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CFAllocatorMallocZoneDoubleFree() {
|
2013-09-24 20:42:25 +08:00
|
|
|
void *mem = CFAllocatorAllocate(kCFAllocatorMallocZone, 5, 0);
|
2011-11-30 09:07:02 +08:00
|
|
|
CFAllocatorDeallocate(kCFAllocatorMallocZone, mem);
|
|
|
|
CFAllocatorDeallocate(kCFAllocatorMallocZone, mem);
|
|
|
|
}
|
|
|
|
|
2012-01-31 07:23:26 +08:00
|
|
|
__attribute__((noinline))
|
|
|
|
void access_memory(char *a) {
|
|
|
|
*a = 0;
|
|
|
|
}
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
// Test the +load instrumentation.
|
|
|
|
// Because the +load methods are invoked before anything else is initialized,
|
|
|
|
// it makes little sense to wrap the code below into a gTest test case.
|
|
|
|
// If AddressSanitizer doesn't instrument the +load method below correctly,
|
|
|
|
// everything will just crash.
|
|
|
|
|
|
|
|
char kStartupStr[] =
|
|
|
|
"If your test didn't crash, AddressSanitizer is instrumenting "
|
|
|
|
"the +load methods correctly.";
|
|
|
|
|
|
|
|
@interface LoadSomething : NSObject {
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation LoadSomething
|
|
|
|
|
|
|
|
+(void) load {
|
2012-12-21 13:17:13 +08:00
|
|
|
for (size_t i = 0; i < strlen(kStartupStr); i++) {
|
2012-01-31 07:55:46 +08:00
|
|
|
access_memory(&kStartupStr[i]); // make sure no optimizations occur.
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
// Don't print anything here not to interfere with the death tests.
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
void worker_do_alloc(int size) {
|
2012-12-21 13:17:13 +08:00
|
|
|
char * volatile mem = (char * volatile)malloc(size);
|
2011-11-30 09:07:02 +08:00
|
|
|
mem[0] = 0; // Ok
|
|
|
|
free(mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
void worker_do_crash(int size) {
|
2012-12-21 13:17:13 +08:00
|
|
|
char * volatile mem = (char * volatile)malloc(size);
|
2012-01-31 07:23:26 +08:00
|
|
|
access_memory(&mem[size]); // BOOM
|
2011-11-30 09:07:02 +08:00
|
|
|
free(mem);
|
|
|
|
}
|
|
|
|
|
2013-09-24 20:42:25 +08:00
|
|
|
// Used by the GCD tests to avoid a race between the worker thread reporting a
|
|
|
|
// memory error and the main thread which may exit with exit code 0 before
|
|
|
|
// that.
|
|
|
|
void wait_forever() {
|
|
|
|
volatile bool infinite = true;
|
|
|
|
while (infinite) pthread_yield_np();
|
|
|
|
}
|
|
|
|
|
2011-11-30 09:07:02 +08:00
|
|
|
// Tests for the Grand Central Dispatch. See
|
|
|
|
// http://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html
|
|
|
|
// for the reference.
|
|
|
|
void TestGCDDispatchAsync() {
|
|
|
|
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
|
|
|
|
dispatch_block_t block = ^{ worker_do_crash(1024); };
|
|
|
|
// dispatch_async() runs the task on a worker thread that does not go through
|
|
|
|
// pthread_create(). We need to verify that AddressSanitizer notices that the
|
|
|
|
// thread has started.
|
|
|
|
dispatch_async(queue, block);
|
2013-09-24 20:42:25 +08:00
|
|
|
wait_forever();
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestGCDDispatchSync() {
|
|
|
|
dispatch_queue_t queue = dispatch_get_global_queue(2, 0);
|
|
|
|
dispatch_block_t block = ^{ worker_do_crash(1024); };
|
|
|
|
// dispatch_sync() runs the task on a worker thread that does not go through
|
|
|
|
// pthread_create(). We need to verify that AddressSanitizer notices that the
|
|
|
|
// thread has started.
|
|
|
|
dispatch_sync(queue, block);
|
2013-09-24 20:42:25 +08:00
|
|
|
wait_forever();
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// libdispatch spawns a rather small number of threads and reuses them. We need
|
|
|
|
// to make sure AddressSanitizer handles the reusing correctly.
|
|
|
|
void TestGCDReuseWqthreadsAsync() {
|
|
|
|
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
|
|
|
|
dispatch_block_t block_alloc = ^{ worker_do_alloc(1024); };
|
|
|
|
dispatch_block_t block_crash = ^{ worker_do_crash(1024); };
|
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
dispatch_async(queue, block_alloc);
|
|
|
|
}
|
|
|
|
dispatch_async(queue, block_crash);
|
2013-09-24 20:42:25 +08:00
|
|
|
wait_forever();
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to trigger abnormal behaviour of dispatch_sync() being unhandled by us.
|
|
|
|
void TestGCDReuseWqthreadsSync() {
|
|
|
|
dispatch_queue_t queue[4];
|
|
|
|
queue[0] = dispatch_get_global_queue(2, 0);
|
|
|
|
queue[1] = dispatch_get_global_queue(0, 0);
|
|
|
|
queue[2] = dispatch_get_global_queue(-2, 0);
|
|
|
|
queue[3] = dispatch_queue_create("my_queue", NULL);
|
|
|
|
dispatch_block_t block_alloc = ^{ worker_do_alloc(1024); };
|
|
|
|
dispatch_block_t block_crash = ^{ worker_do_crash(1024); };
|
|
|
|
for (int i = 0; i < 1000; i++) {
|
|
|
|
dispatch_sync(queue[i % 4], block_alloc);
|
|
|
|
}
|
|
|
|
dispatch_sync(queue[3], block_crash);
|
2013-09-24 20:42:25 +08:00
|
|
|
wait_forever();
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestGCDDispatchAfter() {
|
|
|
|
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
|
|
|
|
dispatch_block_t block_crash = ^{ worker_do_crash(1024); };
|
|
|
|
// Schedule the event one second from the current time.
|
|
|
|
dispatch_time_t milestone =
|
|
|
|
dispatch_time(DISPATCH_TIME_NOW, 1LL * NSEC_PER_SEC);
|
|
|
|
dispatch_after(milestone, queue, block_crash);
|
2013-09-24 20:42:25 +08:00
|
|
|
wait_forever();
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void worker_do_deallocate(void *ptr) {
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallFreeOnWorkqueue(void *tsd) {
|
|
|
|
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
|
|
|
|
dispatch_block_t block_dealloc = ^{ worker_do_deallocate(tsd); };
|
|
|
|
dispatch_async(queue, block_dealloc);
|
|
|
|
// Do not wait for the worker to free the memory -- nobody is going to touch
|
|
|
|
// it.
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestGCDSourceEvent() {
|
|
|
|
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
|
|
|
|
dispatch_source_t timer =
|
|
|
|
dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
|
|
|
|
// Schedule the timer one second from the current time.
|
|
|
|
dispatch_time_t milestone =
|
|
|
|
dispatch_time(DISPATCH_TIME_NOW, 1LL * NSEC_PER_SEC);
|
|
|
|
|
|
|
|
dispatch_source_set_timer(timer, milestone, DISPATCH_TIME_FOREVER, 0);
|
2012-12-21 13:17:13 +08:00
|
|
|
char * volatile mem = (char * volatile)malloc(10);
|
2011-11-30 09:07:02 +08:00
|
|
|
dispatch_source_set_event_handler(timer, ^{
|
2012-01-31 07:23:26 +08:00
|
|
|
access_memory(&mem[10]);
|
2011-11-30 09:07:02 +08:00
|
|
|
});
|
|
|
|
dispatch_resume(timer);
|
2013-09-24 20:42:25 +08:00
|
|
|
wait_forever();
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestGCDSourceCancel() {
|
|
|
|
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
|
|
|
|
dispatch_source_t timer =
|
|
|
|
dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
|
|
|
|
// Schedule the timer one second from the current time.
|
|
|
|
dispatch_time_t milestone =
|
|
|
|
dispatch_time(DISPATCH_TIME_NOW, 1LL * NSEC_PER_SEC);
|
|
|
|
|
|
|
|
dispatch_source_set_timer(timer, milestone, DISPATCH_TIME_FOREVER, 0);
|
2012-12-21 13:17:13 +08:00
|
|
|
char * volatile mem = (char * volatile)malloc(10);
|
2011-11-30 09:07:02 +08:00
|
|
|
// Both dispatch_source_set_cancel_handler() and
|
|
|
|
// dispatch_source_set_event_handler() use dispatch_barrier_async_f().
|
|
|
|
// It's tricky to test dispatch_source_set_cancel_handler() separately,
|
|
|
|
// so we test both here.
|
|
|
|
dispatch_source_set_event_handler(timer, ^{
|
|
|
|
dispatch_source_cancel(timer);
|
|
|
|
});
|
|
|
|
dispatch_source_set_cancel_handler(timer, ^{
|
2012-01-31 07:23:26 +08:00
|
|
|
access_memory(&mem[10]);
|
2011-11-30 09:07:02 +08:00
|
|
|
});
|
|
|
|
dispatch_resume(timer);
|
2013-09-24 20:42:25 +08:00
|
|
|
wait_forever();
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestGCDGroupAsync() {
|
|
|
|
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
|
|
|
|
dispatch_group_t group = dispatch_group_create();
|
2012-12-21 13:17:13 +08:00
|
|
|
char * volatile mem = (char * volatile)malloc(10);
|
2011-11-30 09:07:02 +08:00
|
|
|
dispatch_group_async(group, queue, ^{
|
2012-01-31 07:23:26 +08:00
|
|
|
access_memory(&mem[10]);
|
2011-11-30 09:07:02 +08:00
|
|
|
});
|
|
|
|
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
|
2013-09-24 20:42:25 +08:00
|
|
|
wait_forever();
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
2012-03-31 01:31:15 +08:00
|
|
|
|
|
|
|
@interface FixedArray : NSObject {
|
|
|
|
int items[10];
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation FixedArray
|
|
|
|
-(int) access: (int)index {
|
|
|
|
return items[index];
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
void TestOOBNSObjects() {
|
|
|
|
id anObject = [FixedArray new];
|
|
|
|
[anObject access:1];
|
|
|
|
[anObject access:11];
|
|
|
|
[anObject release];
|
|
|
|
}
|
2012-06-21 04:28:39 +08:00
|
|
|
|
|
|
|
void TestNSURLDeallocation() {
|
|
|
|
NSURL *base =
|
|
|
|
[[NSURL alloc] initWithString:@"file://localhost/Users/glider/Library/"];
|
|
|
|
volatile NSURL *u =
|
|
|
|
[[NSURL alloc] initWithString:@"Saved Application State"
|
|
|
|
relativeToURL:base];
|
|
|
|
[u release];
|
|
|
|
}
|