dyld-750.5.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 #include "test_support.h"
17
18 static const void *stripPointer(const void *ptr) {
19 #if __has_feature(ptrauth_calls)
20 return __builtin_ptrauth_strip(ptr, ptrauth_key_asia);
21 #else
22 return ptr;
23 #endif
24 }
25
26
27 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
28 // see if image containing malloc is in the dyld cache
29 Dl_info info;
30 if ( dladdr(&malloc, &info) == 0 ) {
31 FAIL("shared_cache_range: dladdr(&malloc, xx) fail");
32 }
33 const struct mach_header* mh = (struct mach_header*)info.dli_fbase;
34 LOG("image with malloc=%p", mh);
35 if ( mh == NULL ) {
36 FAIL("shared_cache_range: dladdr(&malloc, xx) => dli_fbase==NULL");
37 }
38 bool haveSharedCache = (mh->flags & 0x80000000);
39 LOG("haveSharedCache=%d", haveSharedCache);
40
41 size_t cacheLen;
42 const void* cacheStart = _dyld_get_shared_cache_range(&cacheLen);
43
44 if ( haveSharedCache ) {
45 if ( cacheStart == NULL ) {
46 FAIL("_dyld_get_shared_cache_range() returned NULL even though we have a cache");
47 }
48 LOG("shared cache start=%p, len=0x%0lX", cacheStart, cacheLen);
49 const void* cacheEnd = (char*)cacheStart + cacheLen;
50
51 // verify malloc is in shared cache
52 if ( (stripPointer((void*)&malloc) < cacheStart) || (stripPointer((void*)&malloc) > cacheEnd) ) {
53 FAIL("shared_cache_range: malloc is outside range of cache");
54 }
55 }
56 else {
57 if ( cacheStart != NULL ) {
58 FAIL("returned non-NULL even though we don't have a cache");
59 }
60 }
61
62 PASS("Success");
63 }
64