]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/_dyld_is_memory_immutable.dtest/main.c
dyld-519.2.1.tar.gz
[apple/dyld.git] / testing / test-cases / _dyld_is_memory_immutable.dtest / main.c
1
2 // BUILD: $CC foo.c -dynamiclib -install_name $RUN_DIR/libfoo.dylib -o $BUILD_DIR/libfoo.dylib
3 // BUILD: $CC bar.c -dynamiclib -install_name $RUN_DIR/libbar.dylib -o $BUILD_DIR/libbar.dylib
4 // BUILD: $CC main.c -DRUN_DIR="$RUN_DIR" $BUILD_DIR/libfoo.dylib -o $BUILD_DIR/dyld_immutable_test.exe
5
6 // RUN: ./dyld_immutable_test.exe
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <dlfcn.h>
11 #include <mach-o/dyld.h>
12 #include <mach-o/dyld_priv.h>
13
14 typedef const char* (*BarProc)(void);
15
16 extern uint32_t _cpu_capabilities;
17 extern const char* foo();
18
19 const char* myStr = "myStr";
20
21 int myInt;
22
23
24 int main()
25 {
26 printf("[BEGIN] _dyld_is_memory_immutable\n");
27
28 if ( !_dyld_is_memory_immutable(myStr, 6) ) {
29 printf("[FAIL] _dyld_is_memory_immutable() returned false for string in main executable\n");
30 return 0;
31 }
32
33 if ( _dyld_is_memory_immutable(strdup("hello"), 6) ) {
34 printf("[FAIL] _dyld_is_memory_immutable() returned true for result from strdup()\n");
35 return 0;
36 }
37
38 if ( _dyld_is_memory_immutable(&myInt, 4) ) {
39 printf("[FAIL] _dyld_is_memory_immutable() returned true for global variabe in main executable\n");
40 return 0;
41 }
42
43 if ( !_dyld_is_memory_immutable(foo(), 4) ) {
44 printf("[FAIL] _dyld_is_memory_immutable() returned false for string in statically linked dylib\n");
45 return 0;
46 }
47
48 if ( !_dyld_is_memory_immutable(&strcpy, 4) ) {
49 printf("[FAIL] _dyld_is_memory_immutable() returned false for function in dyld shared cache\n");
50 return 0;
51 }
52
53 if ( _dyld_is_memory_immutable(&_cpu_capabilities, 4) ) {
54 printf("[FAIL] _dyld_is_memory_immutable() returned true for global variable in shared cache\n");
55 return 0;
56 }
57
58 void* handle = dlopen(RUN_DIR "/libbar.dylib", RTLD_FIRST);
59 if ( handle == NULL ) {
60 printf("[FAIL] dlopen(libbar.dylib) failed");
61 return 0;
62 }
63
64 BarProc proc = dlsym(handle, "bar");
65 if ( proc == NULL ) {
66 printf("[FAIL] dlsym(bar) failed\n");
67 return 0;
68 }
69 const char* barStr = (*proc)();
70 if ( _dyld_is_memory_immutable(barStr, 4) ) {
71 printf("[FAIL] _dyld_is_memory_immutable() returned true for string in unloadable dylib\n");
72 return 0;
73 }
74
75
76 printf("[PASS] _dyld_is_memory_immutable\n");
77 return 0;
78 }
79