dyld-625.13.tar.gz
[apple/dyld.git] / testing / test-cases / shared_cache_range.dtest / main.c
1
2 // BUILD: $CC main.c -o $BUILD_DIR/shared_cache_range.exe
3
4 // RUN: ./shared_cache_range.exe
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <mach-o/dyld_priv.h>
10 #include <dlfcn.h>
11
12 #if __has_feature(ptrauth_calls)
13 #include <ptrauth.h>
14 #endif
15
16 static const void *stripPointer(const void *ptr) {
17 #if __has_feature(ptrauth_calls)
18 return __builtin_ptrauth_strip(ptr, ptrauth_key_asia);
19 #else
20 return ptr;
21 #endif
22 }
23
24
25 int main()
26 {
27 printf("[BEGIN] shared_cache_range\n");
28
29 // see if image containing malloc is in the dyld cache
30 Dl_info info;
31 if ( dladdr(&malloc, &info) == 0 ) {
32 printf("[FAIL] shared_cache_range: dladdr(&malloc, xx) failed");
33 return 0;
34 }
35 const struct mach_header* mh = (struct mach_header*)info.dli_fbase;
36 printf("image with malloc=%p\n", mh);
37 if ( mh == NULL ) {
38 printf("[FAIL] shared_cache_range: dladdr(&malloc, xx) => dli_fbase==NULL");
39 return 0;
40 }
41 bool haveSharedCache = (mh->flags & 0x80000000);
42 printf("haveSharedCache=%d\n", haveSharedCache);
43
44 size_t cacheLen;
45 const void* cacheStart = _dyld_get_shared_cache_range(&cacheLen);
46
47 if ( haveSharedCache ) {
48 if ( cacheStart == NULL ) {
49 printf("[FAIL] _dyld_get_shared_cache_range() returned NULL even though we have a cache\n");
50 return 0;
51 }
52 printf("shared cache start=%p, len=0x%0lX\n", cacheStart, cacheLen);
53 const void* cacheEnd = (char*)cacheStart + cacheLen;
54
55 // verify malloc is in shared cache
56 if ( (stripPointer((void*)&malloc) < cacheStart) || (stripPointer((void*)&malloc) > cacheEnd) ) {
57 printf("[FAIL] shared_cache_range: malloc is outside range of cache\n");
58 return 0;
59 }
60 }
61 else {
62 if ( cacheStart != NULL ) {
63 printf("[FAIL] _dyld_get_shared_cache_range() returned non-NULL even though we don't have a cache\n");
64 return 0;
65 }
66 }
67
68 printf("[PASS] shared_cache_range\n");
69 return 0;
70 }
71