]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/_dyld_is_memory_immutable.dtest/main.c
a162cfa5b5f144b03e1c9160175ae909ded59807
[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 <string.h>
12 #include <mach-o/dyld.h>
13 #include <mach-o/dyld_priv.h>
14
15 #if __has_feature(ptrauth_calls)
16 #include <ptrauth.h>
17 #endif
18
19 static const void* stripPointer(const void* ptr)
20 {
21 #if __has_feature(ptrauth_calls)
22 return __builtin_ptrauth_strip(ptr, ptrauth_key_asia);
23 #else
24 return ptr;
25 #endif
26 }
27
28
29 typedef const char* (*BarProc)(void);
30
31 extern uint32_t _cpu_capabilities;
32 extern const char* foo();
33
34 const char* myStr = "myStr";
35
36 int myInt;
37
38
39 int main()
40 {
41 printf("[BEGIN] _dyld_is_memory_immutable\n");
42
43 if ( !_dyld_is_memory_immutable(myStr, 6) ) {
44 printf("[FAIL] _dyld_is_memory_immutable() returned false for string in main executable\n");
45 return 0;
46 }
47
48 if ( _dyld_is_memory_immutable(strdup("hello"), 6) ) {
49 printf("[FAIL] _dyld_is_memory_immutable() returned true for result from strdup()\n");
50 return 0;
51 }
52
53 if ( _dyld_is_memory_immutable(&myInt, 4) ) {
54 printf("[FAIL] _dyld_is_memory_immutable() returned true for global variabe in main executable\n");
55 return 0;
56 }
57
58 if ( !_dyld_is_memory_immutable(foo(), 4) ) {
59 printf("[FAIL] _dyld_is_memory_immutable() returned false for string in statically linked dylib\n");
60 return 0;
61 }
62
63 if ( !_dyld_is_memory_immutable(stripPointer((void*)&strcpy), 4) ) {
64 printf("[FAIL] _dyld_is_memory_immutable() returned false for strcpy function in dyld shared cache\n");
65 return 0;
66 }
67
68 if ( _dyld_is_memory_immutable(&_cpu_capabilities, 4) ) {
69 printf("[FAIL] _dyld_is_memory_immutable() returned true for global variable in shared cache\n");
70 return 0;
71 }
72
73 void* handle = dlopen(RUN_DIR "/libbar.dylib", RTLD_FIRST);
74 if ( handle == NULL ) {
75 printf("[FAIL] dlopen(libbar.dylib) failed");
76 return 0;
77 }
78
79 BarProc proc = dlsym(handle, "bar");
80 if ( proc == NULL ) {
81 printf("[FAIL] dlsym(bar) failed\n");
82 return 0;
83 }
84 const char* barStr = (*proc)();
85 if ( _dyld_is_memory_immutable(barStr, 4) ) {
86 printf("[FAIL] _dyld_is_memory_immutable() returned true for string in unloadable dylib\n");
87 return 0;
88 }
89
90
91 printf("[PASS] _dyld_is_memory_immutable\n");
92 return 0;
93 }
94