dyld-732.8.tar.gz
[apple/dyld.git] / testing / test-cases / dlsym-in-interposed-malloc.dtest / interposer.c
1
2 #include <dlfcn.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <mach-o/dyld-interposing.h>
7
8 static bool inMalloc = false;
9 static bool forceSystemMalloc = false;
10
11 void* mymalloc(size_t size)
12 {
13 // We are in our own printf, so we need to fall back to the default malloc
14 if (forceSystemMalloc) {
15 return malloc(size);
16 }
17
18 if (inMalloc) {
19 // Recursion! This shouldn't happen.
20 forceSystemMalloc = true;
21 printf("[FAIL] dlsym-in-interposed-malloc mymalloc() is recursive\n");
22 exit(1);
23 }
24
25 inMalloc = true;
26
27 // ASan calls dlsym before libdyld has created an image list. Make sure that succeeds
28 void* sym = dlsym(RTLD_DEFAULT, "malloc");
29 if (sym == NULL) {
30 forceSystemMalloc = true;
31 printf("[FAIL] dlsym-in-interposed-malloc dlsym failed\n");
32 exit(1);
33 }
34
35 if (sym != mymalloc) {
36 forceSystemMalloc = true;
37 printf("[FAIL] dlsym-in-interposed-malloc dlsym result %p != mymalloc %p\n", sym, &mymalloc);
38 exit(1);
39 }
40 void* result = malloc(size);
41
42 inMalloc = false;
43
44 return result;
45 }
46
47 DYLD_INTERPOSE(mymalloc, malloc)