]> git.saurik.com Git - apple/dyld.git/blob - unit-tests/test-cases/text-relocs-perms/main.c
dyld-195.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 <mach-o/getsect.h>
27 #include <mach/mach.h>
28 #include <mach/mach_vm.h>
29 #include "test.h" // PASS(), FAIL(), XPASS(), XFAIL()
30
31
32 extern void foo();
33
34 static vm_prot_t getPermission(void* addr)
35 {
36 mach_vm_address_t address = (mach_vm_address_t)(uintptr_t)addr;
37 kern_return_t result;
38 mach_port_t object_name;
39 vm_region_basic_info_data_64_t info;
40 mach_msg_type_number_t count;
41 mach_vm_size_t size = 4096;
42
43 count = VM_REGION_BASIC_INFO_COUNT_64;
44 result = mach_vm_region(mach_task_self(),
45 &address,
46 &size,
47 VM_REGION_BASIC_INFO_64,
48 (vm_region_info_t)&info,
49 &count,
50 &object_name);
51 //fprintf(stderr, "result=%X, info.protection=%X\n", result, info.protection);
52 if ( result == KERN_SUCCESS )
53 return info.protection;
54 return 0;
55 }
56
57
58 static void* getStubAddr()
59 {
60 unsigned long size;
61 #if __i386__
62 return getsectdata("__IMPORT", "__jump_table", &size);
63 #elif __ppc__
64 void* p = getsectdata("__TEXT", "__picsymbolstub1", &size);
65 if ( p != NULL )
66 return p;
67 return getsectdata("__TEXT", "__symbol_stub1", &size);
68 #elif __ppc64__
69 return getsectdata("__TEXT", "__picsymbolstub1", &size);
70 #elif __x86_64__
71 return getsectdata("__TEXT", "__symbol_stub1", &size);
72 #elif __arm__
73 void* p = getsectdata("__TEXT", "__symbol_stub4", &size);
74 if ( p != NULL )
75 return p;
76 return getsectdata("__TEXT", "__symbolstub1", &size);
77 #else
78 #error unknown arch
79 #endif
80 }
81
82
83 static void checkStubs(void* addr)
84 {
85 vm_prot_t perm = getPermission(addr);
86 if ( (perm == 0) || ((perm & VM_PROT_WRITE) != 0) ) {
87 FAIL("read-only-stubs: bad permissions %d at address %p", perm, addr);
88 exit(0);
89 }
90 }
91
92
93 int main()
94 {
95 foo();
96
97 void* stubAddr = getStubAddr();
98 #if __i386__
99 if ( stubAddr != NULL )
100 #endif
101 {
102 checkStubs(stubAddr);
103 checkStubs(stubAddr);
104 }
105 PASS("text-relocs-perms");
106 return EXIT_SUCCESS;
107 }
108
109