]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/weak-coalesce-inserted-dylibs.dtest/main.cpp
dyld-851.27.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 #include "test_support.h"
14
15 extern void foo();
16
17 // 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
18 static bool calledMainNew = false;
19 static bool calledMainDelete = false;
20 static bool enableTracking = false;
21
22 void* operator new(size_t size)
23 {
24 if (enableTracking)
25 calledMainNew = true;
26 void* ptr = malloc(size);
27 return ptr;
28 }
29
30 void operator delete(void* ptr)
31 {
32 if (enableTracking)
33 calledMainDelete = true;
34 free(ptr);
35 }
36
37 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
38 // First make sure we do use our versions of new and delete.
39 enableTracking = true;
40
41 int* v = new int(1);
42 if (!calledMainNew) {
43 FAIL("Didn't call executable operator new");
44 }
45
46 delete v;
47 if (!calledMainDelete) {
48 FAIL("Didn't call executable operator delete");
49 }
50
51 // Now make foo do the same and make sure we got the new/delete from bar
52 calledMainNew = false;
53 calledMainDelete = false;
54 foo();
55
56 if (calledMainNew) {
57 FAIL("Didn't call bar operator new");
58 }
59
60 if (calledMainDelete) {
61 FAIL("Didn't call bar operator delete");
62 }
63
64 PASS("Success");
65 }
66