1 2 #include <assert.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <pthread.h> 6 #include <unistd.h> 7 8 static char* s_mem; 9 10 /* wait a second, so as to guarantee that the parent access 11 the malloc'd area, then free it. */ 12 static void* thread_func(void* arg) 13 { 14 sleep(1); 15 free(s_mem); 16 return NULL; 17 } 18 19 int main(int argc, char** argv) 20 { 21 pthread_t tid; 22 int quiet; 23 24 fprintf(stderr, "Start.\n"); 25 26 quiet = argc > 1; 27 28 s_mem = malloc(10); 29 if (0 && !quiet) 30 fprintf(stderr, "Pointer to allocated memory: %p\n", s_mem); 31 assert(s_mem); 32 pthread_create(&tid, NULL, thread_func, NULL); 33 34 /* Write, which isn't coordinated with the free ==> a race 35 should be reported. */ 36 char c = s_mem[5]; 37 __asm__ __volatile__("" : : "r"((long)c) ); 38 39 pthread_join(tid, NULL); 40 fprintf(stderr, "Done.\n"); 41 return 0; 42 } 43