dyld-732.8.tar.gz
[apple/dyld.git] / testing / test-cases / weak-coalesce-inserted-dylibs.dtest / main.cpp
1
2 // BUILD: $CXX foo.cpp -dynamiclib -install_name $RUN_DIR/libfoo.dylib -o $BUILD_DIR/libfoo.dylib -fno-exceptions
3 // BUILD: $CXX bar.cpp -dynamiclib -install_name $RUN_DIR/libbar.dylib -o $BUILD_DIR/libbar.dylib -fno-exceptions
4 // BUILD: $CXX main.cpp $BUILD_DIR/libfoo.dylib -o $BUILD_DIR/weak-coalesce-inserted-dylibs.exe -fno-exceptions
5 // BUILD: $DYLD_ENV_VARS_ENABLE $BUILD_DIR/weak-coalesce-inserted-dylibs.exe
6
7 // RUN: DYLD_INSERT_LIBRARIES=libbar.dylib ./weak-coalesce-inserted-dylibs.exe
8
9
10 #include <stdio.h>
11 #include <stdlib.h>
12
13 extern void foo();
14
15 // We have our own copy of operator new. Make sure that we call our version, but that foo calls the one from the inserted bar dylib
16 static bool calledMainNew = false;
17 static bool calledMainDelete = false;
18 static bool enableTracking = false;
19
20 void* operator new(size_t size)
21 {
22 if (enableTracking)
23 calledMainNew = true;
24 void* ptr = malloc(size);
25 return ptr;
26 }
27
28 void operator delete(void* ptr)
29 {
30 if (enableTracking)
31 calledMainDelete = true;
32 free(ptr);
33 }
34
35 int main()
36 {
37 printf("[BEGIN] weak-coalesce-inserted-dylibs\n");
38
39 // First make sure we do use our versions of new and delete.
40 enableTracking = true;
41
42 int* v = new int(1);
43 if (!calledMainNew) {
44 printf("[FAIL] weak-coalesce-inserted-dylibs, didn't call executable operator new\n");
45 return 1;
46 }
47
48 delete v;
49 if (!calledMainDelete) {
50 printf("[FAIL] weak-coalesce-inserted-dylibs, didn't call executable operator delete\n");
51 return 1;
52 }
53
54 // Now make foo do the same and make sure we got the new/delete from bar
55 calledMainNew = false;
56 calledMainDelete = false;
57 foo();
58
59 if (calledMainNew) {
60 printf("[FAIL] weak-coalesce-inserted-dylibs, didn't call bar operator new\n");
61 return 1;
62 }
63
64 if (calledMainDelete) {
65 printf("[FAIL] weak-coalesce-inserted-dylibs, didn't call bar operator delete\n");
66 return 1;
67 }
68
69 printf("[PASS] weak-coalesce-inserted-dylibs\n");
70
71 return 0;
72 }
73