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