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