]> git.saurik.com Git - apple/dyld.git/blame - unit-tests/test-cases/read-only-stubs/foo.c
dyld-97.1.tar.gz
[apple/dyld.git] / unit-tests / test-cases / read-only-stubs / foo.c
CommitLineData
bac542e6
A
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-o/ldsyms.h>
28#include <mach/mach.h>
29#include <mach/mach_vm.h>
30
31#include "test.h" // PASS(), FAIL(), XPASS(), XFAIL()
32
33extern void bar();
34extern int barData;
35
36
37static 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 if ( result == KERN_SUCCESS )
55 return info.protection;
56 return 0;
57}
58
59
60static void* getStubAddr()
61{
62#if __LP64__
63 uint64_t size;
64#else
65 uint32_t size;
66#endif
67 uintptr_t slide = (uintptr_t)&_mh_dylib_header; // assume dylib is zero-base so slide == load address
68#if __i386__
69 return getsectdatafromheader(&_mh_dylib_header, "__IMPORT", "__jump_table", &size) + slide;
70#elif __ppc__
71 return getsectdatafromheader(&_mh_dylib_header, "TEXT", "__picsymbolstub1", &size) + slide;
72#elif __ppc64__
73 return getsectdatafromheader_64(&_mh_dylib_header, "__TEXT", "__picsymbolstub1", &size) + slide;
74#elif __x86_64__
75 return getsectdatafromheader_64(&_mh_dylib_header, "__TEXT", "__symbol_stub1", &size) + slide;
76#else
77 #error unknown arch
78#endif
79}
80
81
82static void checkStubs(void* addr)
83{
84 vm_prot_t perm = getPermission(addr);
85 if ( (perm == 0) || ((perm & VM_PROT_WRITE) != 0) ) {
86 FAIL("read-only-stubs: bad permissions %d at address %p", perm, addr);
87 exit(0);
88 }
89}
90
91int fooData = 1;
92
93void foo()
94{
95 void* stubAddr = getStubAddr();
96 checkStubs(stubAddr);
97 barData = 0;
98 bar();
99 checkStubs(stubAddr);
100}
101
102