]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/interpose-malloc.dtest/main.c
9ef1842b2a1c7e808a5be4ee2871d1c26570e2ed
[apple/dyld.git] / testing / test-cases / interpose-malloc.dtest / main.c
1 // BUILD: $CC foo.c -dynamiclib -o $BUILD_DIR/libfoo.dylib -install_name $RUN_DIR/libfoo.dylib
2 // BUILD: $CC main.c $BUILD_DIR/libfoo.dylib -o $BUILD_DIR/interpose-malloc.exe
3 // BUILD: $DYLD_ENV_VARS_ENABLE $BUILD_DIR/interpose-malloc.exe
4 // BUILD: $CC interposer.c -dynamiclib -o $BUILD_DIR/libmyalloc.dylib -install_name libmyalloc.dylib
5
6 // RUN: DYLD_INSERT_LIBRARIES=libmyalloc.dylib ./interpose-malloc.exe
7
8
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 extern void* myalloc1(size_t);
15 extern void* myalloc2(size_t);
16
17 int main()
18 {
19 printf("[BEGIN] interpose-malloc\n");
20
21 char* p1 = malloc(10);
22 if ( strncmp(p1+10, "##########", 10) != 0 ) {
23 printf("[FAIL] interpose-malloc malloc() from main executable not interposed\n");
24 return 0;
25 }
26
27 void* p2 = myalloc1(6);
28 if ( strncmp(p2+6, "######", 6) != 0 ) {
29 printf("[FAIL] interpose-malloc myalloc1() from libfoo.dylib not interposed\n");
30 return 0;
31 }
32
33 void* p3 = myalloc2(10);
34 if ( strncmp(p3+10, "##########", 10) != 0 ) {
35 printf("[FAIL] interpose-malloc myalloc2() from libfoo.dylib not interposed\n");
36 return 0;
37 }
38
39 void* p4 = strdup("hello");
40 if ( strncmp(p4+6, "#######", 6) != 0 ) {
41 printf("[FAIL] interpose-malloc malloc() from strdup not interposed\n");
42 return 0;
43 }
44
45 //printf("%p %p %p %p\n", p1, p2, p3, p4);
46 printf("[PASS] interpose-malloc\n");
47 return 0;
48 }