CACAO
patcher.cpp
Go to the documentation of this file.
1 /* src/vm/jit/x86_64/patcher.cpp - x86_64 code patching functions
2 
3  Copyright (C) 1996-2013
4  CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 
6  This file is part of CACAO.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2, or (at
11  your option) any later version.
12 
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  02110-1301, USA.
22 
23 */
24 
25 
26 #include "config.h"
27 
28 #include <stdint.h>
29 
30 #include "vm/types.hpp"
31 
33 #include "vm/jit/x86_64/md.hpp"
34 
35 #include "mm/memory.hpp"
36 
37 #include "native/native.hpp"
38 
39 #include "vm/jit/builtin.hpp"
40 #include "vm/class.hpp"
41 #include "vm/field.hpp"
42 #include "vm/initialize.hpp"
43 #include "vm/options.hpp"
44 #include "vm/resolve.hpp"
45 
47 
48 
49 /* patcher_patch_code **********************************************************
50 
51  Just patches back the original machine code.
52 
53 *******************************************************************************/
54 
56 {
57  *((uint16_t*) pr->mpc) = (uint16_t) pr->mcode;
58  md_icacheflush((void*) pr->mpc, PATCHER_CALL_SIZE);
59 }
60 
61 static int32_t *patch_checked_location(int32_t *p, int32_t val)
62 {
63  assert(*p == 0);
64  // verify that it's aligned
65  assert((((uintptr_t) p) & (4-1)) == 0);
66  *p = val;
67  return p;
68 }
69 
70 static void checked_icache_flush(void *addr, int nbytes, int32_t *check_loc)
71 {
72  assert((int8_t*) addr + nbytes - sizeof(int32_t) >= (int8_t*) check_loc);
73  md_icacheflush(addr, nbytes);
74 }
75 
76 /**
77  * Check if the trap instruction at the given PC is valid.
78  *
79  * @param pc Program counter.
80  *
81  * @return true if valid, false otherwise.
82  */
84 {
85  uint16_t mcode = *((uint16_t*) pc);
86 
87  // Check for the undefined instruction we use.
88  return (mcode == 0x0b0f);
89 }
90 
91 /**
92  * Overwrites the MFENCE instruction at the indicated address with a 3-byte
93  * NOP. The MFENCE instruction is not allowed to cross a (4-byte) word
94  * boundary.
95  *
96  * @param pc Program counter.
97  */
98 static void patch_out_mfence(void *pc)
99 {
100  uint32_t *p = (uint32_t*) (((uintptr_t) pc) & ~3);
101 
102  assert((((uintptr_t) pc) & 3) < 2);
103  if (((uintptr_t) pc) & 1)
104  *p = (*p & 0x000000ff) | 0x001f0f00;
105  else
106  *p = (*p & 0xff000000) | 0x00001f0f;
107 
108  md_icacheflush(p, 4);
109 }
110 
111 /* patcher_resolve_classref_to_classinfo ***************************************
112 
113  ACONST:
114 
115  <patched call position>
116  48 bf a0 f0 92 00 00 00 00 00 mov $0x92f0a0,%rdi
117 
118  MULTIANEWARRAY:
119 
120  <patched call position>
121  48 be 30 40 b2 00 00 00 00 00 mov $0xb24030,%rsi
122  48 89 e2 mov %rsp,%rdx
123  48 b8 7c 96 4b 00 00 00 00 00 mov $0x4b967c,%rax
124  48 ff d0 callq *%rax
125 
126  ARRAYCHECKCAST:
127 
128  <patched call position>
129  48 be b8 3f b2 00 00 00 00 00 mov $0xb23fb8,%rsi
130  48 b8 00 00 00 00 00 00 00 00 mov $0x0,%rax
131  48 ff d0 callq *%rax
132 
133 *******************************************************************************/
134 
136 {
138  uintptr_t* datap = (uintptr_t*) pr->datap;
139 
140  // Resolve the class.
142 
143  if (c == NULL)
144  return false;
145 
146  // Patch the class.
147  *datap = (uintptr_t) c;
148 
149  // Synchronize data cache.
150  md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
151 
152  // Patch back the original code.
153  patcher_patch_code(pr);
154 
155  return true;
156 }
157 
158 
159 /* patcher_resolve_classref_to_vftbl *******************************************
160 
161  CHECKCAST (class):
162  INSTANCEOF (class):
163 
164  <patched call position>
165 
166 *******************************************************************************/
167 
169 {
171  uintptr_t* datap = (uintptr_t*) pr->datap;
172 
173  // Resolve the field.
175 
176  if (c == NULL)
177  return false;
178 
179  // Patch super class' vftbl.
180  *datap = (uintptr_t) c->vftbl;
181 
182  // Synchronize data cache.
183  md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
184 
185  // Patch back the original code.
186  patcher_patch_code(pr);
187 
188  return true;
189 }
190 
191 
192 /* patcher_resolve_classref_to_flags *******************************************
193 
194  CHECKCAST/INSTANCEOF:
195 
196  <patched call position>
197 
198 *******************************************************************************/
199 
201 {
203 /* int32_t* datap = (int32_t*) pr->datap; */
204  uint8_t* ra = (uint8_t*) pr->mpc;
205 
206  // Resolve the field.
208 
209  if (c == NULL)
210  return false;
211 
213  ra += PATCH_ALIGNMENT((uintptr_t) ra, 2, sizeof(int32_t));
214 
215  // Patch class flags.
216 /* *datap = c->flags; */
217  patch_checked_location((int32_t*) (ra + 2), c->flags);
218 
219  // Synchronize data cache.
220 /* md_dcacheflush(datap, sizeof(int32_t)); */
221  md_icacheflush(ra + 2, sizeof(int32_t));
222 
223  // Patch back the original code.
224  patcher_patch_code(pr);
225 
226  return true;
227 }
228 
229 
230 /* patcher_get_putstatic *******************************************************
231 
232  Machine code:
233 
234  <patched call position>
235  4d 8b 15 86 fe ff ff mov -378(%rip),%r10
236  49 8b 32 mov (%r10),%rsi
237 
238 *******************************************************************************/
239 
241 {
243  uintptr_t* datap = (uintptr_t*) pr->datap;
244  uint8_t* ra = (uint8_t*) pr->mpc;
245 
246  // Resolve the field.
247  fieldinfo* fi = resolve_field_eager(uf);
248 
249  if (fi == NULL)
250  return false;
251 
252  ra += PATCHER_CALL_SIZE;
253 
254  // Check if the field's class is initialized/
255  if (!(fi->clazz->state & CLASS_INITIALIZED))
256  if (!initialize_class(fi->clazz))
257  return false;
258 
259  // Patch the field value's address.
260  *datap = (uintptr_t) fi->value;
261 
262  if (pr->disp_mb && !(fi->flags & ACC_VOLATILE))
263  patch_out_mfence(ra + pr->disp_mb - 2);
264 
265  // Synchronize data cache.
266  md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
267 
268  // Patch back the original code.
269  patcher_patch_code(pr);
270 
271  return true;
272 }
273 
274 
275 /* patcher_get_putfield ********************************************************
276 
277  Machine code:
278 
279  <patched call position>
280  45 8b 8f 00 00 00 00 mov 0x0(%r15),%r9d
281 
282 *******************************************************************************/
283 
285 {
286  uint8_t* pc = (uint8_t*) pr->mpc;
288 
289  // Resolve the field.
290  fieldinfo* fi = resolve_field_eager(uf);
291 
292  if (fi == NULL)
293  return false;
294 
295  pc += PATCHER_CALL_SIZE;
296 
297  int disp = -sizeof(int32_t) + pr->patch_align;
298  patch_checked_location((int32_t*) (pc + disp), fi->offset);
299 
300  if (pr->disp_mb && !(fi->flags & ACC_VOLATILE))
301  patch_out_mfence(pc + pr->disp_mb - 2);
302 
303  // Synchronize instruction cache.
304  md_icacheflush(pc, disp + sizeof(int32_t));
305 
306  // Patch back the original code.
307  patcher_patch_code(pr);
308 
309  return true;
310 }
311 
312 
313 /* patcher_putfieldconst *******************************************************
314 
315  Machine code:
316 
317  <patched call position>
318  41 c7 85 00 00 00 00 7b 00 00 00 movl $0x7b,0x0(%r13)
319 
320 *******************************************************************************/
321 
323 {
324  uint8_t* pc = (uint8_t*) pr->mpc;
326 
327  // Resolve the field.
328  fieldinfo* fi = resolve_field_eager(uf);
329 
330  if (fi == NULL)
331  return false;
332 
333  pc += PATCHER_CALL_SIZE;
334 
335  int disp = -2*sizeof(int32_t) + pr->patch_align;
336  patch_checked_location((int32_t*) (pc + disp), fi->offset);
337 
338  if (pr->disp_mb && !(fi->flags & ACC_VOLATILE))
339  patch_out_mfence(pc + pr->disp_mb - 2);
340 
341  // Synchronize instruction cache.
342  md_icacheflush(pc, disp + sizeof(int32_t));
343 
344  // Patch back the original code.
345  patcher_patch_code(pr);
346 
347  return true;
348 }
349 
350 
351 /* patcher_invokestatic_special ************************************************
352 
353  Machine code:
354 
355  <patched call position>
356  49 ba 00 00 00 00 00 00 00 00 mov $0x0,%r10
357  49 ff d2 callq *%r10
358 
359 *******************************************************************************/
360 
362 {
364  uintptr_t* datap = (uintptr_t*) pr->datap;
365 
366  // Resolve the method.
368 
369  if (m == NULL)
370  return false;
371 
372  // Patch stubroutine.
373  *datap = (uintptr_t) m->stubroutine;
374 
375  // Synchronize data cache.
376  md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
377 
378  // Patch back the original code.
379  patcher_patch_code(pr);
380 
381  return true;
382 }
383 
384 
385 /* patcher_invokevirtual *******************************************************
386 
387  Machine code:
388 
389  <patched call position>
390  4c 8b 17 mov (%rdi),%r10
391  49 8b 82 00 00 00 00 mov 0x0(%r10),%rax
392  48 ff d0 callq *%rax
393 
394 *******************************************************************************/
395 
397 {
398  uint8_t* pc = (uint8_t*) pr->mpc;
400 
401  // Resovlve the method.
403 
404  if (m == NULL)
405  return false;
406 
407  pc += PATCHER_CALL_SIZE;
408  pc += PATCH_ALIGNMENT((uintptr_t) pc, 6, sizeof(int32_t));
409 
410  // Patch vftbl index.
411  patch_checked_location((int32_t*) (pc + 6), (int32_t) (OFFSET(vftbl_t, table[0]) + sizeof(methodptr) * m->vftblindex));
412 
413  // Synchronize instruction cache.
414  md_icacheflush(pc + 3 + 3, SIZEOF_VOID_P);
415 
416  // Patch back the original code.
417  patcher_patch_code(pr);
418 
419  return true;
420 }
421 
422 
423 /* patcher_invokeinterface *****************************************************
424 
425  Machine code:
426 
427  <patched call position>
428  4c 8b 17 mov (%rdi),%r10
429  4d 8b 92 00 00 00 00 mov 0x0(%r10),%r10
430  49 8b 82 00 00 00 00 mov 0x0(%r10),%rax
431  48 ff d0 callq *%rax
432 
433 *******************************************************************************/
434 
436 {
437  uint8_t* pc = (uint8_t*) pr->mpc;
439 
440  // Resolve the method.
442 
443  if (m == NULL)
444  return false;
445 
446  pc += PATCHER_CALL_SIZE;
447  pc += PATCH_ALIGNMENT((uintptr_t) pc, 6, sizeof(int32_t));
448 
449  // Patch interfacetable index.
450  patch_checked_location((int32_t*) (pc + 6), (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - sizeof(methodptr) * m->clazz->index));
451 
452  int disp = PATCH_ALIGNMENT((uintptr_t) (pc + 3 + 7), 3, sizeof(int32_t));
453  pc += disp;
454  // Patch method offset.
455  int32_t *loc = patch_checked_location((int32_t*) (pc + 3 + 7 + 3), (int32_t) (sizeof(methodptr) * (m - m->clazz->methods)));
456 
457  // Synchronize instruction cache.
458  checked_icache_flush(pc + 6, SIZEOF_VOID_P + 3 + SIZEOF_VOID_P + disp, loc);
459 
460  // Patch back the original code.
461  patcher_patch_code(pr);
462 
463  return true;
464 }
465 
466 
467 /* patcher_checkcast_interface *************************************************
468 
469  Machine code:
470 
471  <patched call position>
472  45 8b 9a 1c 00 00 00 mov 0x1c(%r10),%r11d
473  41 81 fb 00 00 00 00 cmp $0x0,%r11d
474  0f 8f 08 00 00 00 jg 0x00002aaaaae511d5
475  48 8b 0c 25 03 00 00 00 mov 0x3,%rcx
476  4d 8b 9a 00 00 00 00 mov 0x0(%r10),%r11
477 
478 *******************************************************************************/
479 
481 {
482  uint8_t* pc = (uint8_t*) pr->mpc;
484 
485  // Resolve the class.
487 
488  if (c == NULL)
489  return false;
490 
491  pc += PATCHER_CALL_SIZE;
492  pc += PATCH_ALIGNMENT((uintptr_t) pc, 10, sizeof(int32_t));
493 
494  // Patch super class index.
495  patch_checked_location((int32_t*) (pc + 10), c->index);
496 
497  int disp = PATCH_ALIGNMENT((uintptr_t) (pc + 7 + 7 + 6 + 8), 3, sizeof(int32_t));
498  pc += disp;
499  int32_t *loc = patch_checked_location((int32_t*) (pc + 7 + 7 + 6 + 8 + 3), (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*)));
500 
501  // Synchronize instruction cache.
502  checked_icache_flush(pc + 10, sizeof(int32_t) + 6 + 8 + 3 + sizeof(int32_t) + disp, loc);
503 
504  // Patch back the original code.
505  patcher_patch_code(pr);
506 
507  return true;
508 }
509 
510 
511 /* patcher_instanceof_interface ************************************************
512 
513  Machine code:
514 
515  <patched call position>
516  45 8b 9a 1c 00 00 00 mov 0x1c(%r10),%r11d
517  41 81 fb 00 00 00 00 cmp $0x0,%r11d
518  0f 8e 94 04 00 00 jle 0x00002aaaaab018f8
519  4d 8b 9a 00 00 00 00 mov 0x0(%r10),%r11
520 
521 *******************************************************************************/
522 
524 {
525  uint8_t* pc = (uint8_t*) pr->mpc;
527 
528  // Resolve the class.
530 
531  if (c == NULL)
532  return false;
533 
534  pc += PATCHER_CALL_SIZE;
535  pc += PATCH_ALIGNMENT((uintptr_t) pc, 10, sizeof(int32_t));
536 
537  // Patch super class index.
538  patch_checked_location((int32_t*) (pc + 10), c->index);
539 
540  int disp = PATCH_ALIGNMENT((uintptr_t) (pc + 7 + 7 + 6), 3, sizeof(int32_t));
541  pc += disp;
542  int32_t *loc = patch_checked_location((int32_t*) (pc + 7 + 7 + 6 + 3), (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*)));
543 
544  // Synchronize instruction cache.
545  checked_icache_flush(pc + 10, sizeof(int32_t) + 6 + 3 + sizeof(int32_t) + disp, loc);
546 
547  // Patch back the original code.
548  patcher_patch_code(pr);
549 
550  return true;
551 }
552 
553 
554 /*
555  * These are local overrides for various environment variables in Emacs.
556  * Please do not remove this and leave it at the end of the file, where
557  * Emacs will automagically detect them.
558  * ---------------------------------------------------------------------
559  * Local variables:
560  * mode: c++
561  * indent-tabs-mode: t
562  * c-basic-offset: 4
563  * tab-width: 4
564  * End:
565  * vim:noexpandtab:sw=4:ts=4:
566  */
bool patcher_invokestatic_special(patchref_t *pr)
Definition: patcher.cpp:392
bool patcher_get_putstatic(patchref_t *pr)
Definition: patcher.cpp:264
#define ra
Definition: md-asm.hpp:62
static void checked_icache_flush(void *addr, int nbytes, int32_t *check_loc)
Definition: patcher.cpp:70
bool patcher_resolve_classref_to_classinfo(patchref_t *pr)
Definition: patcher.cpp:148
methodinfo * methods
Definition: class.hpp:113
bool patcher_invokeinterface(patchref_t *pr)
Definition: patcher.cpp:477
#define PATCHER_CALL_SIZE
Definition: codegen.hpp:68
static void md_dcacheflush(void *addr, int nbytes)
Definition: md.hpp:163
u1 * methodptr
Definition: global.hpp:40
u1 * stubroutine
Definition: method.hpp:102
s4 vftblindex
Definition: method.hpp:81
int16_t disp_mb
int32_t offset
Definition: field.hpp:66
classinfo * clazz
Definition: method.hpp:80
void patcher_patch_code(patchref_t *pr)
Definition: patcher.cpp:114
bool patcher_invokevirtual(patchref_t *pr)
Definition: patcher.cpp:433
s4 flags
Definition: field.hpp:59
bool patcher_resolve_classref_to_flags(patchref_t *pr)
Definition: patcher.cpp:225
bool patcher_checkcast_interface(patchref_t *pr)
Definition: patcher.cpp:526
s4 flags
Definition: class.hpp:90
static int32_t * patch_checked_location(int32_t *p, int32_t val)
Definition: patcher.cpp:61
int16_t patch_align
s4 index
Definition: class.hpp:116
bool initialize_class(classinfo *c)
Definition: initialize.cpp:110
bool patcher_putfieldconst(patchref_t *pr)
Definition: patcher.cpp:233
bool patcher_get_putfield(patchref_t *pr)
Definition: patcher.cpp:308
vftbl_t * vftbl
Definition: class.hpp:121
fieldinfo * resolve_field_eager(unresolved_field *ref)
Definition: resolve.cpp:1497
#define pc
Definition: md-asm.hpp:56
methodinfo * resolve_method_eager(unresolved_method *ref)
Definition: resolve.cpp:2236
uintptr_t datap
uint32_t mcode
static void patch_out_mfence(void *pc)
Overwrites the MFENCE instruction at the indicated address with a 3-byte NOP.
Definition: patcher.cpp:98
classinfo * resolve_classref_eager(constant_classref *ref)
Definition: resolve.cpp:961
static void md_icacheflush(void *addr, int nbytes)
Definition: md.hpp:151
uintptr_t mpc
bool patcher_is_valid_trap_instruction_at(void *pc)
Check if the trap instruction at the given PC is valid.
Definition: patcher.cpp:72
#define OFFSET(s, el)
Definition: memory.hpp:90
#define PATCH_ALIGNMENT(addr, offset, size)
Definition: codegen.hpp:56
bool patcher_resolve_classref_to_vftbl(patchref_t *pr)
Definition: patcher.cpp:188
bool patcher_instanceof_interface(patchref_t *pr)
Definition: patcher.cpp:572