2 * Copyright (c) 2007 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
23 #include <stdio.h> // fprintf(), NULL
24 #include <stdlib.h> // exit(), EXIT_SUCCESS
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()
33 #if __MAC_OS_X_VERSION_MIN_REQUIRED
37 static vm_prot_t
getPermission(void* addr
)
39 mach_vm_address_t address
= (mach_vm_address_t
)(uintptr_t)addr
;
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;
46 count
= VM_REGION_BASIC_INFO_COUNT_64
;
47 result
= mach_vm_region(mach_task_self(),
50 VM_REGION_BASIC_INFO_64
,
51 (vm_region_info_t
)&info
,
54 //fprintf(stderr, "result=%X, info.protection=%X\n", result, info.protection);
55 if ( result
== KERN_SUCCESS
)
56 return info
.protection
;
61 static void* getStubAddr()
65 return getsectdata("__IMPORT", "__jump_table", &size
);
67 void* p
= getsectdata("__TEXT", "__picsymbolstub1", &size
);
70 return getsectdata("__TEXT", "__symbol_stub1", &size
);
72 return getsectdata("__TEXT", "__picsymbolstub1", &size
);
74 return getsectdata("__TEXT", "__symbol_stub1", &size
);
76 void* p
= getsectdata("__TEXT", "__symbol_stub4", &size
);
79 return getsectdata("__TEXT", "__symbolstub1", &size
);
86 static void checkStubs(void* addr
)
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
);
100 void* stubAddr
= getStubAddr();
102 if ( stubAddr
!= NULL
)
105 checkStubs(stubAddr
);
106 checkStubs(stubAddr
);
108 PASS("text-relocs-perms");
116 // iOS does not have text relocs
117 PASS("text-relocs-perm");