]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/interpose-then-dlopen.dtest/main.c
dyld-733.6.tar.gz
[apple/dyld.git] / testing / test-cases / interpose-then-dlopen.dtest / main.c
1 // BUILD: $CC fooimpl.c -dynamiclib -o $BUILD_DIR/libfooimpl.dylib -install_name $RUN_DIR/libfooimpl.dylib
2 // BUILD: $CC foo.c -dynamiclib $BUILD_DIR/libfooimpl.dylib -o $BUILD_DIR/libfoo.dylib -Wl,-interposable_list,interposable.txt -install_name $RUN_DIR/libfoo.dylib
3 // BUILD: $CC bar.c -bundle $BUILD_DIR/libfooimpl.dylib -o $BUILD_DIR/libbar.bundle -Wl,-interposable_list,interposable.txt
4 // BUILD: $CC main.c -DRUN_DIR="$RUN_DIR" -Wl,-interposable_list,interposable.txt -o $BUILD_DIR/interpose-then-dlopen.exe
5 // BUILD: $DYLD_ENV_VARS_ENABLE $BUILD_DIR/interpose-then-dlopen.exe
6 // BUILD: $CC interposer.c -dynamiclib $BUILD_DIR/libfooimpl.dylib -o $BUILD_DIR/libinterposer.dylib -install_name libinterposer.dylib
7
8 // RUN: DYLD_INSERT_LIBRARIES=libinterposer.dylib ./interpose-then-dlopen.exe
9
10
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <dlfcn.h>
16
17 // Note, libinterposer.dylib interposes interposableFoo
18 extern int interposableFoo();
19
20 // Note, libfoo interposes interposableBar
21 extern int interposableBar();
22
23 extern int callFunc();
24
25
26 static int tryImage(const char* path, int expectedFoo, int expectedBar)
27 {
28 void* handle = dlopen(path, RTLD_LAZY);
29 if ( handle == NULL ) {
30 printf("dlerror(): %s\n", dlerror());
31 printf("[FAIL] interpose-then-dlopen %s\n", path);
32 return 1;
33 }
34
35 __typeof(&callFunc) callFooSym = (__typeof(&callFunc))dlsym(handle, "callFoo");
36 if ( callFooSym == NULL ) {
37 printf("dlerror(): %s\n", dlerror());
38 printf("[FAIL] interpose-then-dlopen %s\n", path);
39 return 1;
40 }
41
42 int fooResult = callFooSym();
43 if ( fooResult != expectedFoo ) {
44 printf("[FAIL] interpose-then-dlopen callFoo() from %s not interposed as it returned %d\n", path, fooResult);
45 return 1;
46 }
47
48 __typeof(&callFunc) callBarSym = (__typeof(&callFunc))dlsym(handle, "callBar");
49 if ( callBarSym == NULL ) {
50 printf("dlerror(): %s\n", dlerror());
51 printf("[FAIL] interpose-then-dlopen %s\n", path);
52 return 1;
53 }
54
55 int barResult = callBarSym();
56 if ( barResult != expectedBar ) {
57 printf("[FAIL] interpose-then-dlopen callBar() from %s not interposed as it returned %d\n", path, barResult);
58 return 1;
59 }
60
61 return 0;
62 }
63
64 int main()
65 {
66 printf("[BEGIN] interpose-then-dlopen\n");
67
68 if (tryImage(RUN_DIR "/libfoo.dylib", 4, 2))
69 return 0;
70
71 if (tryImage(RUN_DIR "/libbar.bundle", 4, 100))
72 return 0;
73
74 //printf("%p %p %p %p\n", p1, p2, p3, p4);
75 printf("[PASS] interpose-then-dlopen\n");
76 return 0;
77 }