Debian Codegraph @

rpython/rlib/rvmprof/src/shared/trampoline.c

raw symbols SCIP
1 #include "trampoline.h"
2
3 #include "vmprof.h"
4 #include "machine.h"
5
6 #define _GNU_SOURCE 1
7 #include <string.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <sys/mman.h>
11 #include <dlfcn.h>
12 #include <errno.h>
13 #include <stdint.h>
14 #include <unistd.h>
15
16 #if __APPLE__
17 #include <mach-o/dyld.h>
18 #endif
19
20 #define PAGE_ALIGNED(a,size) (void*)(((uintptr_t)a) & ~(size - 1))
21
22 /*
23 * The trampoline works the following way:
24 *
25 * `eval` is the traditional PyEval_EvalFrameEx (for 2.7)
26 * `page` is allocated and used as memory block to execute
27 * the first few instructions from eval
28 * `vmprof_eval` is a function just saving the
29 * frame in rbx
30 *
31 * +--- eval ----------+
32 * +----| jmp vmprof_eval | <-- patched, original bits moved to page
33 * | +->| asm instr 1 |
34 * | | | asm instr 2 |
35 * | | | ... |
36 * | | +-------------------+
37 * | |
38 * | | +--- page ----------+<-+
39 * | | | push rbp | <-- copied from PyEval_Loop
40 * | | | mov rsp -> rbp | |
41 * | | | ... | |
42 * | | | ... | |
43 * | +--| jmp eval+copied | |
44 * | +-------------------+ |
45 * | |
46 * +--->+--- vmprof_eval ---+ |
47 * | ... | |
48 * | push rbx | |
49 * | mov rdi -> rbx | <-- save the frame, custom method
50 * | call eval |--+
51 * | ... |
52 * | retq |
53 * +-------------------+
54 */
55
56 static
57 int g_patched = 0;
58
59 static char * g_trampoline = NULL;
60 // the machine code size copied over from the callee
61 static int g_trampoline_length;
62
63 int _jmp_to(char * a, uintptr_t addr) {
64 #ifdef X86_64
65 // moveabsq <addr>, <reg>
66 a[0] = 0x48; // REX.W
67 a[1] = 0xba; // %rdx
68 a[2] = addr & 0xff;
69 a[3] = (addr >> 8) & 0xff;
70 a[4] = (addr >> 16) & 0xff;
71 a[5] = (addr >> 24) & 0xff;
72 a[6] = (addr >> 32) & 0xff;
73 a[7] = (addr >> 40) & 0xff;
74 a[8] = (addr >> 48) & 0xff;
75 a[9] = (addr >> 56) & 0xff;
76
77 // jmp %edx
78 a[10] = 0xff;
79 a[11] = 0xe2;
80 return 12;
81 #elif defined(X86_32)
82 // mov <addr>, %edx
83 a[0] = 0xba;
84 a[1] = addr & 0xff;
85 a[2] = (addr >> 8) & 0xff;
86 a[3] = (addr >> 16) & 0xff;
87 a[4] = (addr >> 24) & 0xff;
88 // jmp %edx
89 a[5] = 0xff;
90 a[6] = 0xe2;
91 return 7;
92 #endif
93 return 0;
94 }
95
96 #ifdef X86_32
97 int patch_relative_call(void * base, char * rel_call, char *rel_call_end, int bytes_after) {
98 if (bytes_after != 0) {
99 return 0;
100 }
101
102 char * r = rel_call+1;
103
104 int off = r[0] |
105 ((r[1] & 0xff) << 8) |
106 ((r[2] & 0xff) << 16) |
107 ((r[3] & 0xff) << 24);
108 // instruction pointer is just after the whole instruction
109 intptr_t addr = (intptr_t)base + 5 + off;
110
111 rel_call[0] = 0xb8;
112 rel_call[1] = addr & 0xff;
113 rel_call[2] = (addr >> 8) & 0xff;
114 rel_call[3] = (addr >> 16) & 0xff;
115 rel_call[4] = (addr >> 24) & 0xff;
116 // jmp %edx
117 rel_call[5] = 0xff;
118 rel_call[6] = 0xd0;
119
120 return 2;
121 }
122 #endif
123
124 #include "libudis86/udis86.h"
125 unsigned int vmp_machine_code_instr_length(char* pc)
126 {
127 struct ud u;
128 ud_init(&u);
129 ud_set_input_buffer(&u, (uint8_t*)pc, 12);
130 ud_set_mode(&u, vmp_machine_bits());
131 return ud_decode(&u);
132 }
133
134 // a hilarious typo, tramp -> trump :)
135 int _redirect_trampoline_and_back(char * eval, char * trump, char * vmprof_eval) {
136
137 char * trump_first_byte = trump;
138 #ifdef X86_64
139 int needed_bytes = 12;
140 #elif defined(X86_32)
141 int needed_bytes = 8;
142 int relative_call_at_pos = -1;
143 #else
144 # error "platform not supported"
145 #endif
146 int bytes = 0;
147 int off = 0;
148 char * ptr = eval;
149
150 // 1) copy the instructions that should be redone in the trampoline
151 while (bytes < needed_bytes) {
152 unsigned int res = vmp_machine_code_instr_length(ptr);
153 if (res == 0) {
154 fprintf(stderr, "could not determine length of instr for trampoline\n");
155 fprintf(stderr, " %x %x %x %x %x\n", ptr[0], ptr[1],
156 ptr[2], ptr[3], ptr[4]);
157 return 1;
158 }
159 #ifdef X86_32
160 if (ptr[0] == '\xe8') {
161 // occur on 32bit linux
162 relative_call_at_pos = bytes;
163 }
164 #endif
165 bytes += res;
166 ptr += res;
167 }
168 g_trampoline_length = bytes;
169
170 // 2) initiate the first few instructions of the eval loop
171 {
172 (void)memcpy(trump, eval, bytes);
173 #ifdef X86_32
174 if (relative_call_at_pos != -1) {
175 off = patch_relative_call(eval+relative_call_at_pos, trump+relative_call_at_pos,
176 trump+relative_call_at_pos+5, bytes-relative_call_at_pos-5);
177 }
178 #endif
179 _jmp_to(trump+bytes+off, (uintptr_t)eval+bytes);
180 }
181
182 // 3) overwrite the first few bytes of callee to jump to tramp
183 // callee must call back
184 _jmp_to(eval, (uintptr_t)vmprof_eval);
185
186 return 0;
187 }
188
189
190 int vmp_patch_callee_trampoline(void * callee_addr, void * vmprof_eval, void ** vmprof_eval_target)
191 {
192 int result;
193 int pagesize;
194
195 if (g_trampoline != NULL) {
196 //fprintf(stderr, "trampoline already patched\n");
197 return 0; // already patched
198 }
199
200 pagesize = sysconf(_SC_PAGESIZE);
201 errno = 0;
202
203 result = mprotect(PAGE_ALIGNED(callee_addr, pagesize), pagesize*2, PROT_READ|PROT_WRITE);
204 if (result != 0) {
205 fprintf(stderr, "read|write protecting callee_addr\n");
206 return -1;
207 }
208 // create a new page and set it all of it writable
209 char * page = (char*)mmap(NULL, pagesize, PROT_READ|PROT_WRITE|PROT_EXEC,
210 MAP_ANON | MAP_PRIVATE, 0, 0);
211 if (page == NULL) {
212 fprintf(stderr, "could not allocate page for trampoline\n");
213 return -1;
214 }
215
216 char * a = (char*)callee_addr;
217 if (_redirect_trampoline_and_back(a, page, vmprof_eval) != 0) {
218 fprintf(stderr, "could not redirect eval->vmprof_eval->trampoline->eval+off\n");
219 return -1;
220 }
221
222 result = mprotect(PAGE_ALIGNED(callee_addr, pagesize), pagesize*2, PROT_READ|PROT_EXEC);
223 if (result != 0) {
224 fprintf(stderr, "read|exec protecting callee addr\n");
225 return -1;
226 }
227 // revert, the page should not be writable any more now!
228 result = mprotect((void*)page, pagesize, PROT_READ|PROT_EXEC);
229 if (result != 0) {
230 fprintf(stderr, "read|exec protecting tramp\n");
231 return -1;
232 }
233
234 g_trampoline = page;
235 *vmprof_eval_target = page;
236
237 return 0;
238 }
239
240 int vmp_unpatch_callee_trampoline(void * callee_addr)
241 {
242 return 0; // currently the trampoline is not removed
243 }