dyld-750.5.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 #include "test_support.h"
9
10 static bool inMalloc = false;
11 static bool forceSystemMalloc = false;
12
13 void* mymalloc(size_t size)
14 {
15 // We are in our own printf, so we need to fall back to the default malloc
16 if (forceSystemMalloc) {
17 return malloc(size);
18 }
19
20 if (inMalloc) {
21 // Recursion! This shouldn't happen.
22 forceSystemMalloc = true;
23 FAIL("mymalloc() is recursive");
24 }
25
26 inMalloc = true;
27
28 // ASan calls dlsym before libdyld has created an image list. Make sure that succeeds
29 void* sym = dlsym(RTLD_DEFAULT, "malloc");
30 if (sym == NULL) {
31 forceSystemMalloc = true;
32 FAIL("dlsym failed");
33 }
34
35 if (sym != mymalloc) {
36 forceSystemMalloc = true;
37 FAIL("dlsym result %p != mymalloc %p", sym, &mymalloc);
38 }
39 void* result = malloc(size);
40
41 inMalloc = false;
42
43 return result;
44 }
45
46 DYLD_INTERPOSE(mymalloc, malloc)