]>
Commit | Line | Data |
---|---|---|
bac542e6 A |
1 | /* |
2 | * Copyright (c) 2006-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 <string.h> // memcpy | |
25 | #include <stdlib.h> // exit(), EXIT_SUCCESS | |
26 | #include <dlfcn.h> | |
27 | #include <libkern/OSCacheControl.h> // sys_icache_invalidate | |
28 | #include <sys/mman.h> // for mprotext | |
412ebb8e | 29 | #include <mach/mach.h> |
bac542e6 A |
30 | |
31 | #include "test.h" // PASS(), FAIL(), XPASS(), XFAIL() | |
32 | ||
33 | ||
34 | ||
35 | void* calldlopen(const char* path, int mode, void* (*dlopen_proc)(const char* path, int mode)) | |
36 | { | |
37 | return (*dlopen_proc)(path, mode); | |
38 | } | |
39 | ||
412ebb8e A |
40 | #if __thumb__ |
41 | #define START_OF_FUNC(x) ((void*)((long)x & (-2))) | |
42 | #define ADDR_FROM_BLOCK(x) ((void*)((long)x | 1)) | |
43 | #else | |
44 | #define START_OF_FUNC(x) (x) | |
45 | #define ADDR_FROM_BLOCK(x) (x) | |
46 | #endif | |
47 | ||
bac542e6 A |
48 | // |
49 | // try calling dlopen() from code not owned by dyld | |
50 | // | |
51 | int main() | |
52 | { | |
412ebb8e A |
53 | // now try to create a page where foo() was |
54 | vm_address_t addr = 0; | |
55 | kern_return_t r = vm_allocate(mach_task_self(), &addr, 4096, VM_FLAGS_ANYWHERE); | |
56 | if ( r != KERN_SUCCESS ) { | |
57 | FAIL("vm_allocate returned %d", r); | |
58 | return 0; | |
59 | } | |
60 | void* codeBlock = (void*)(addr); | |
61 | memcpy(codeBlock, START_OF_FUNC(calldlopen), 4096); | |
bac542e6 A |
62 | sys_icache_invalidate(codeBlock, 4096); |
63 | mprotect(codeBlock, 4096, PROT_READ | PROT_EXEC); | |
64 | //fprintf(stderr, "codeBlock=%p\n", codeBlock); | |
65 | ||
412ebb8e | 66 | void* (*caller)(const char* path, int mode, void* (*dlopen_proc)(const char* path, int mode)) = ADDR_FROM_BLOCK(codeBlock); |
bac542e6 A |
67 | |
68 | void* handle = (*caller)("foo.bundle", RTLD_LAZY, &dlopen); | |
69 | if ( handle == NULL ) { | |
70 | FAIL("dlopen(\"%s\") failed with: %s", "foo.bundle", dlerror()); | |
71 | exit(0); | |
72 | } | |
73 | ||
74 | void* sym = dlsym(handle, "foo"); | |
75 | if ( sym == NULL ) { | |
76 | FAIL("dlsym(handle, \"foo\") failed"); | |
77 | exit(0); | |
78 | } | |
79 | ||
80 | int result = dlclose(handle); | |
81 | if ( result != 0 ) { | |
82 | FAIL("dlclose(handle) returned %d", result); | |
83 | exit(0); | |
84 | } | |
85 | ||
86 | PASS("dlopen-from-anonymous-code"); | |
87 | return EXIT_SUCCESS; | |
88 | } |