dyld-732.8.tar.gz
[apple/dyld.git] / testing / test-cases / _dyld_is_memory_immutable-lock.dtest / main.c
1
2
3 // BUILD: $CC foo.c -bundle -o $BUILD_DIR/lock.bundle
4 // BUILD: $CC main.c -o $BUILD_DIR/immutable-lock.exe -DRUN_DIR="$RUN_DIR"
5
6 // RUN: ./immutable-lock.exe
7
8 #include <stdio.h>
9 #include <dlfcn.h>
10 #include <stdlib.h>
11 #include <pthread.h>
12 #include <mach-o/dyld_priv.h>
13
14
15 static void* work(void* mh)
16 {
17 _dyld_is_memory_immutable(mh, 16);
18
19 return NULL;
20 }
21
22 static void notify(const struct mach_header* mh, intptr_t vmaddr_slide)
23 {
24 if ( mh->flags & 0x80000000 )
25 return;
26
27 // for each image notified that is not in the dyld cache, spin up a thread which calls _dyld_is_memory_immutable()
28 pthread_t workerThread;
29 if ( pthread_create(&workerThread, NULL, &work, (void*)mh) != 0 ) {
30 printf("[FAIL] _dyld_is_memory_immutable-lock, pthread_create\n");
31 exit(0);
32 }
33 void* dummy;
34 pthread_join(workerThread, &dummy);
35 }
36
37
38
39 int main()
40 {
41 printf("[BEGIN] _dyld_is_memory_immutable-lock\n");
42
43 _dyld_register_func_for_add_image(&notify);
44
45 void* h = dlopen(RUN_DIR "/lock.bundle", 0);
46 if ( h == NULL ) {
47 printf("dlopen(lock.bundle) failed with error: %s\n", dlerror());
48 printf("[FAIL] _dyld_is_memory_immutable-lock, lock.bundle not loaded\n");
49 return 0;
50 }
51
52 printf("[PASS] _dyld_is_memory_immutable-lock\n");
53 return 0;
54 }
55