]> git.saurik.com Git - apple/dyld.git/blob - unit-tests/test-cases/text-relocs-perms/main.c
dyld-750.5.tar.gz
[apple/dyld.git] / unit-tests / test-cases / text-relocs-perms / main.c
1 /*
2 * Copyright (c) 2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 #include <stdio.h> // fprintf(), NULL
24 #include <stdlib.h> // exit(), EXIT_SUCCESS
25 #include <unistd.h>
26 #include <Availability.h>
27 #include <mach-o/getsect.h>
28 #include <mach/mach.h>
29 #include <mach/mach_vm.h>
30 #include "test.h" // PASS(), FAIL(), XPASS(), XFAIL()
31
32
33 #if __MAC_OS_X_VERSION_MIN_REQUIRED
34
35 extern void foo();
36
37 static vm_prot_t getPermission(void* addr)
38 {
39 mach_vm_address_t address = (mach_vm_address_t)(uintptr_t)addr;
40 kern_return_t result;
41 mach_port_t object_name;
42 vm_region_basic_info_data_64_t info;
43 mach_msg_type_number_t count;
44 mach_vm_size_t size = 4096;
45
46 count = VM_REGION_BASIC_INFO_COUNT_64;
47 result = mach_vm_region(mach_task_self(),
48 &address,
49 &size,
50 VM_REGION_BASIC_INFO_64,
51 (vm_region_info_t)&info,
52 &count,
53 &object_name);
54 //fprintf(stderr, "result=%X, info.protection=%X\n", result, info.protection);
55 if ( result == KERN_SUCCESS )
56 return info.protection;
57 return 0;
58 }
59
60
61 static void* getStubAddr()
62 {
63 unsigned long size;
64 #if __i386__
65 return getsectdata("__IMPORT", "__jump_table", &size);
66 #elif __ppc__
67 void* p = getsectdata("__TEXT", "__picsymbolstub1", &size);
68 if ( p != NULL )
69 return p;
70 return getsectdata("__TEXT", "__symbol_stub1", &size);
71 #elif __ppc64__
72 return getsectdata("__TEXT", "__picsymbolstub1", &size);
73 #elif __x86_64__
74 return getsectdata("__TEXT", "__symbol_stub1", &size);
75 #elif __arm__
76 void* p = getsectdata("__TEXT", "__symbol_stub4", &size);
77 if ( p != NULL )
78 return p;
79 return getsectdata("__TEXT", "__symbolstub1", &size);
80 #else
81 #error unknown arch
82 #endif
83 }
84
85
86 static void checkStubs(void* addr)
87 {
88 vm_prot_t perm = getPermission(addr);
89 if ( (perm == 0) || ((perm & VM_PROT_WRITE) != 0) ) {
90 FAIL("read-only-stubs: bad permissions %d at address %p", perm, addr);
91 exit(0);
92 }
93 }
94
95
96 int main()
97 {
98 foo();
99
100 void* stubAddr = getStubAddr();
101 #if __i386__
102 if ( stubAddr != NULL )
103 #endif
104 {
105 checkStubs(stubAddr);
106 checkStubs(stubAddr);
107 }
108 PASS("text-relocs-perms");
109 return EXIT_SUCCESS;
110 }
111
112 #else
113
114 int main()
115 {
116 // iOS does not have text relocs
117 PASS("text-relocs-perm");
118 return EXIT_SUCCESS;
119 }
120
121 #endif
122