]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/shared_cache_range.dtest/main.c
693de548b8b85acb089e6c71f17d38f4a932838e
[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
13 int main()
14 {
15 printf("[BEGIN] shared_cache_range\n");
16
17 // see if image containing malloc is in the dyld cache
18 Dl_info info;
19 if ( dladdr(&malloc, &info) == 0 ) {
20 printf("[FAIL] shared_cache_range: dladdr(&malloc, xx) failed");
21 return 0;
22 }
23 const struct mach_header* mh = (struct mach_header*)info.dli_fbase;
24 printf("image with malloc=%p\n", mh);
25 if ( mh == NULL ) {
26 printf("[FAIL] shared_cache_range: dladdr(&malloc, xx) => dli_fbase==NULL");
27 return 0;
28 }
29 bool haveSharedCache = (mh->flags & 0x80000000);
30 printf("haveSharedCache=%d\n", haveSharedCache);
31
32 size_t cacheLen;
33 const void* cacheStart = _dyld_get_shared_cache_range(&cacheLen);
34
35 if ( haveSharedCache ) {
36 if ( cacheStart == NULL ) {
37 printf("[FAIL] _dyld_get_shared_cache_range() returned NULL even though we have a cache\n");
38 return 0;
39 }
40 printf("shared cache start=%p, len=0x%0lX\n", cacheStart, cacheLen);
41 const void* cacheEnd = (char*)cacheStart + cacheLen;
42
43 // verify malloc is in shared cache
44 if ( ((void*)&malloc < cacheStart) || ((void*)&malloc > cacheEnd) ) {
45 printf("[FAIL] shared_cache_range: malloc is outside range of cache\n");
46 return 0;
47 }
48 }
49 else {
50 if ( cacheStart != NULL ) {
51 printf("[FAIL] _dyld_get_shared_cache_range() returned non-NULL even though we don't have a cache\n");
52 return 0;
53 }
54 }
55
56 printf("[PASS] shared_cache_range\n");
57 return 0;
58 }
59