]>
git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/read-only-data.dtest/main.c
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
7 // RUN: ./read-only-data.exe
13 #include <mach/mach.h>
14 #include <mach/vm_region.h>
16 #include <mach-o/dyld.h>
17 #include <mach-o/dyld_priv.h>
18 #include <mach-o/getsect.h>
20 #include "test_support.h"
22 extern bool isReadOnly(const void* addr
);
23 extern bool testLib();
25 const void* const funcs
[] = { &malloc
, &free
, &strcmp
, &printf
};
27 typedef bool (*TestFunc
)(void);
29 static void notify(const struct mach_header
* mh
, intptr_t slide
)
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
);
38 uint8_t* p
= getsectiondata((struct mach_header_64
*)mh
, "__DATA_CONST", "__const", &size
);
40 uint8_t* p
= getsectiondata(mh
, "__DATA_CONST", "__const", &size
);
42 if ( (p
!= NULL
) && inTestDir
&& !isReadOnly(p
) ) {
43 FAIL("read-only-data __DATA_CONST,__const section not read-only in %p %s", mh
, path
);
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");
55 // test __DATA_CONST in linked dylib is read-only
57 FAIL("librotest.dylib not read-only");
60 _dyld_register_func_for_add_image(¬ify
);
62 // test __DATA_CONST in dlopen'ed bundle is read-only
63 void* h
= dlopen(RUN_DIR
"/test.bundle", 0);
65 FAIL("test.bundle not loaded");
67 TestFunc tb
= (TestFunc
)dlsym(h
, "testBundle");
69 FAIL("test.bundle not read-only");