]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/read-only-data.dtest/main.c
dyld-851.27.tar.gz
[apple/dyld.git] / testing / test-cases / read-only-data.dtest / main.c
1
2 // BUILD: $CC foo.c -dynamiclib -o $BUILD_DIR/librotest.dylib -install_name $RUN_DIR/librotest.dylib -Wl,-data_const
3 // BUILD: $CC main.c -o $BUILD_DIR/read-only-data.exe $BUILD_DIR/librotest.dylib -Wl,-data_const -DRUN_DIR="$RUN_DIR"
4 // BUILD: $CC foo.c -bundle -o $BUILD_DIR/test.bundle -DBUNDLE=1 -Wl,-data_const
5
6
7 // RUN: ./read-only-data.exe
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdbool.h>
12 #include <stdlib.h>
13 #include <mach/mach.h>
14 #include <mach/vm_region.h>
15 #include <dlfcn.h>
16 #include <mach-o/dyld.h>
17 #include <mach-o/dyld_priv.h>
18 #include <mach-o/getsect.h>
19
20 #include "test_support.h"
21
22 extern bool isReadOnly(const void* addr);
23 extern bool testLib();
24
25 const void* const funcs[] = { &malloc, &free, &strcmp, &printf };
26
27 typedef bool (*TestFunc)(void);
28
29 static void notify(const struct mach_header* mh, intptr_t slide)
30 {
31 // only look at images not in dyld shared cache
32 if ( (mh->flags & 0x80000000) == 0 ) {
33 LOG("mh=%p flags=0x%08X", mh, mh->flags);
34 const char* path = dyld_image_path_containing_address(mh);
35 bool inTestDir = (strstr(path, RUN_DIR) != NULL);
36 unsigned long size;
37 #if __LP64__
38 uint8_t* p = getsectiondata((struct mach_header_64*)mh, "__DATA_CONST", "__const", &size);
39 #else
40 uint8_t* p = getsectiondata(mh, "__DATA_CONST", "__const", &size);
41 #endif
42 if ( (p != NULL) && inTestDir && !isReadOnly(p) ) {
43 FAIL("read-only-data __DATA_CONST,__const section not read-only in %p %s", mh, path);
44 }
45 }
46 }
47
48
49 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
50 // test __DATA_CONST in main is read-only
51 if ( !isReadOnly(&funcs[2]) ) {
52 FAIL("main executable not read-only");
53 }
54
55 // test __DATA_CONST in linked dylib is read-only
56 if ( !testLib() ) {
57 FAIL("librotest.dylib not read-only");
58 }
59
60 _dyld_register_func_for_add_image(&notify);
61
62 // test __DATA_CONST in dlopen'ed bundle is read-only
63 void* h = dlopen(RUN_DIR "/test.bundle", 0);
64 if ( h == NULL ) {
65 FAIL("test.bundle not loaded");
66 }
67 TestFunc tb = (TestFunc)dlsym(h, "testBundle");
68 if ( !tb() ) {
69 FAIL("test.bundle not read-only");
70 }
71
72 PASS("Success");
73 }
74