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"
35 
36 #include "mm/memory.hpp"
37 
38 #include "native/native.hpp"
39 
40 #include "vm/jit/builtin.hpp"
41 #include "vm/class.hpp"
42 #include "vm/field.hpp"
43 #include "vm/initialize.hpp"
44 #include "vm/options.hpp"
45 #include "vm/resolve.hpp"
46 
47 
48 /* patcher_patch_code **********************************************************
49 
50  Just patches back the original machine code.
51 
52 *******************************************************************************/
53 
55 {
56  *((uint16_t*) pr->mpc) = (uint16_t) pr->mcode;
57  md_icacheflush((void*) pr->mpc, PATCHER_CALL_SIZE);
58 }
59 
60 static int32_t *patch_checked_location(int32_t *p, int32_t val)
61 {
62  assert(*p == 0);
63  // verify that it's aligned
64  assert((((uintptr_t) p) & (4-1)) == 0);
65  *p = val;
66  return p;
67 }
68 
69 static void checked_icache_flush(void *addr, int nbytes, int32_t *check_loc)
70 {
71  assert((int8_t*) addr + nbytes - sizeof(int32_t) >= (int8_t*) check_loc);
72  md_icacheflush(addr, nbytes);
73 }
74 
75 /**
76  * Check if the trap instruction at the given PC is valid.
77  *
78  * @param pc Program counter.
79  *
80  * @return true if valid, false otherwise.
81  */
83 {
84  uint16_t mcode = *((uint16_t*) pc);
85 
86  // Check for the undefined instruction we use.
87  return (mcode == 0x0b0f);
88 }
89 
90 /**
91  * Overwrites the MFENCE instruction at the indicated address with a 3-byte
92  * NOP. The MFENCE instruction is not allowed to cross a (4-byte) word
93  * boundary.
94  *
95  * @param pc Program counter.
96  */
97 static void patch_out_mfence(void *pc)
98 {
99  uint32_t *p = (uint32_t*) (((uintptr_t) pc) & ~3);
100 
101  assert((((uintptr_t) pc) & 3) < 2);
102  if (((uintptr_t) pc) & 1)
103  *p = (*p & 0x000000ff) | 0x001f0f00;
104  else
105  *p = (*p & 0xff000000) | 0x00001f0f;
106 
107  md_icacheflush(p, 4);
108 }
109 
110 /* patcher_resolve_classref_to_classinfo ***************************************
111 
112  ACONST:
113 
114  <patched call position>
115  48 bf a0 f0 92 00 00 00 00 00 mov $0x92f0a0,%rdi
116 
117  MULTIANEWARRAY:
118 
119  <patched call position>
120  48 be 30 40 b2 00 00 00 00 00 mov $0xb24030,%rsi
121  48 89 e2 mov %rsp,%rdx
122  48 b8 7c 96 4b 00 00 00 00 00 mov $0x4b967c,%rax
123  48 ff d0 callq *%rax
124 
125  ARRAYCHECKCAST:
126 
127  <patched call position>
128  48 be b8 3f b2 00 00 00 00 00 mov $0xb23fb8,%rsi
129  48 b8 00 00 00 00 00 00 00 00 mov $0x0,%rax
130  48 ff d0 callq *%rax
131 
132 *******************************************************************************/
133 
135 {
137  uintptr_t* datap = (uintptr_t*) pr->datap;
138 
139  // Resolve the class.
141 
142  if (c == NULL)
143  return false;
144 
145  // Patch the class.
146  *datap = (uintptr_t) c;
147 
148  // Synchronize data cache.
149  md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
150 
151  // Patch back the original code.
152  patcher_patch_code(pr);
153 
154  return true;
155 }
156 
157 
158 /* patcher_resolve_classref_to_vftbl *******************************************
159 
160  CHECKCAST (class):
161  INSTANCEOF (class):
162 
163  <patched call position>
164 
165 *******************************************************************************/
166 
168 {
170  uintptr_t* datap = (uintptr_t*) pr->datap;
171 
172  // Resolve the field.
174 
175  if (c == NULL)
176  return false;
177 
178  // Patch super class' vftbl.
179  *datap = (uintptr_t) c->vftbl;
180 
181  // Synchronize data cache.
182  md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
183 
184  // Patch back the original code.
185  patcher_patch_code(pr);
186 
187  return true;
188 }
189 
190 
191 /* patcher_resolve_classref_to_flags *******************************************
192 
193  CHECKCAST/INSTANCEOF:
194 
195  <patched call position>
196 
197 *******************************************************************************/
198 
200 {
202 /* int32_t* datap = (int32_t*) pr->datap; */
203  uint8_t* ra = (uint8_t*) pr->mpc;
204 
205  // Resolve the field.
207 
208  if (c == NULL)
209  return false;
210 
212  ra += PATCH_ALIGNMENT((uintptr_t) ra, 2, sizeof(int32_t));
213 
214  // Patch class flags.
215 /* *datap = c->flags; */
216  patch_checked_location((int32_t*) (ra + 2), c->flags);
217 
218  // Synchronize data cache.
219 /* md_dcacheflush(datap, sizeof(int32_t)); */
220  md_icacheflush(ra + 2, sizeof(int32_t));
221 
222  // Patch back the original code.
223  patcher_patch_code(pr);
224 
225  return true;
226 }
227 
228 
229 /* patcher_get_putstatic *******************************************************
230 
231  Machine code:
232 
233  <patched call position>
234  4d 8b 15 86 fe ff ff mov -378(%rip),%r10
235  49 8b 32 mov (%r10),%rsi
236 
237 *******************************************************************************/
238 
240 {
242  uintptr_t* datap = (uintptr_t*) pr->datap;
243  uint8_t* ra = (uint8_t*) pr->mpc;
244 
245  // Resolve the field.
246  fieldinfo* fi = resolve_field_eager(uf);
247 
248  if (fi == NULL)
249  return false;
250 
251  ra += PATCHER_CALL_SIZE;
252 
253  // Check if the field's class is initialized/
254  if (!(fi->clazz->state & CLASS_INITIALIZED))
255  if (!initialize_class(fi->clazz))
256  return false;
257 
258  // Patch the field value's address.
259  *datap = (uintptr_t) fi->value;
260 
261  if (pr->disp_mb && !(fi->flags & ACC_VOLATILE))
262  patch_out_mfence(ra + pr->disp_mb - 2);
263 
264  // Synchronize data cache.
265  md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
266 
267  // Patch back the original code.
268  patcher_patch_code(pr);
269 
270  return true;
271 }
272 
273 
274 /* patcher_get_putfield ********************************************************
275 
276  Machine code:
277 
278  <patched call position>
279  45 8b 8f 00 00 00 00 mov 0x0(%r15),%r9d
280 
281 *******************************************************************************/
282 
284 {
285  uint8_t* pc = (uint8_t*) pr->mpc;
287 
288  // Resolve the field.
289  fieldinfo* fi = resolve_field_eager(uf);
290 
291  if (fi == NULL)
292  return false;
293 
294  pc += PATCHER_CALL_SIZE;
295 
296  int disp = -sizeof(int32_t) + pr->patch_align;
297  patch_checked_location((int32_t*) (pc + disp), fi->offset);
298 
299  if (pr->disp_mb && !(fi->flags & ACC_VOLATILE))
300  patch_out_mfence(pc + pr->disp_mb - 2);
301 
302  // Synchronize instruction cache.
303  md_icacheflush(pc, disp + sizeof(int32_t));
304 
305  // Patch back the original code.
306  patcher_patch_code(pr);
307 
308  return true;
309 }
310 
311 
312 /* patcher_putfieldconst *******************************************************
313 
314  Machine code:
315 
316  <patched call position>
317  41 c7 85 00 00 00 00 7b 00 00 00 movl $0x7b,0x0(%r13)
318 
319 *******************************************************************************/
320 
322 {
323  uint8_t* pc = (uint8_t*) pr->mpc;
325 
326  // Resolve the field.
327  fieldinfo* fi = resolve_field_eager(uf);
328 
329  if (fi == NULL)
330  return false;
331 
332  pc += PATCHER_CALL_SIZE;
333 
334  int disp = -2*sizeof(int32_t) + pr->patch_align;
335  patch_checked_location((int32_t*) (pc + disp), fi->offset);
336 
337  if (pr->disp_mb && !(fi->flags & ACC_VOLATILE))
338  patch_out_mfence(pc + pr->disp_mb - 2);
339 
340  // Synchronize instruction cache.
341  md_icacheflush(pc, disp + sizeof(int32_t));
342 
343  // Patch back the original code.
344  patcher_patch_code(pr);
345 
346  return true;
347 }
348 
349 
350 /* patcher_invokestatic_special ************************************************
351 
352  Machine code:
353 
354  <patched call position>
355  49 ba 00 00 00 00 00 00 00 00 mov $0x0,%r10
356  49 ff d2 callq *%r10
357 
358 *******************************************************************************/
359 
361 {
363  uintptr_t* datap = (uintptr_t*) pr->datap;
364 
365  // Resolve the method.
367 
368  if (m == NULL)
369  return false;
370 
371  // Patch stubroutine.
372  *datap = (uintptr_t) m->stubroutine;
373 
374  // Synchronize data cache.
375  md_dcacheflush((void*) pr->datap, SIZEOF_VOID_P);
376 
377  // Patch back the original code.
378  patcher_patch_code(pr);
379 
380  return true;
381 }
382 
383 
384 /* patcher_invokevirtual *******************************************************
385 
386  Machine code:
387 
388  <patched call position>
389  4c 8b 17 mov (%rdi),%r10
390  49 8b 82 00 00 00 00 mov 0x0(%r10),%rax
391  48 ff d0 callq *%rax
392 
393 *******************************************************************************/
394 
396 {
397  uint8_t* pc = (uint8_t*) pr->mpc;
399 
400  // Resovlve the method.
402 
403  if (m == NULL)
404  return false;
405 
406  pc += PATCHER_CALL_SIZE;
407  pc += PATCH_ALIGNMENT((uintptr_t) pc, 6, sizeof(int32_t));
408 
409  // Patch vftbl index.
410  patch_checked_location((int32_t*) (pc + 6), (int32_t) (OFFSET(vftbl_t, table[0]) + sizeof(methodptr) * m->vftblindex));
411 
412  // Synchronize instruction cache.
413  md_icacheflush(pc + 3 + 3, SIZEOF_VOID_P);
414 
415  // Patch back the original code.
416  patcher_patch_code(pr);
417 
418  return true;
419 }
420 
421 
422 /* patcher_invokeinterface *****************************************************
423 
424  Machine code:
425 
426  <patched call position>
427  4c 8b 17 mov (%rdi),%r10
428  4d 8b 92 00 00 00 00 mov 0x0(%r10),%r10
429  49 8b 82 00 00 00 00 mov 0x0(%r10),%rax
430  48 ff d0 callq *%rax
431 
432 *******************************************************************************/
433 
435 {
436  uint8_t* pc = (uint8_t*) pr->mpc;
438 
439  // Resolve the method.
441 
442  if (m == NULL)
443  return false;
444 
445  pc += PATCHER_CALL_SIZE;
446  pc += PATCH_ALIGNMENT((uintptr_t) pc, 6, sizeof(int32_t));
447 
448  // Patch interfacetable index.
449  patch_checked_location((int32_t*) (pc + 6), (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - sizeof(methodptr) * m->clazz->index));
450 
451  int disp = PATCH_ALIGNMENT((uintptr_t) (pc + 3 + 7), 3, sizeof(int32_t));
452  pc += disp;
453  // Patch method offset.
454  int32_t *loc = patch_checked_location((int32_t*) (pc + 3 + 7 + 3), (int32_t) (sizeof(methodptr) * (m - m->clazz->methods)));
455 
456  // Synchronize instruction cache.
457  checked_icache_flush(pc + 6, SIZEOF_VOID_P + 3 + SIZEOF_VOID_P + disp, loc);
458 
459  // Patch back the original code.
460  patcher_patch_code(pr);
461 
462  return true;
463 }
464 
465 
466 /* patcher_checkcast_interface *************************************************
467 
468  Machine code:
469 
470  <patched call position>
471  45 8b 9a 1c 00 00 00 mov 0x1c(%r10),%r11d
472  41 81 fb 00 00 00 00 cmp $0x0,%r11d
473  0f 8f 08 00 00 00 jg 0x00002aaaaae511d5
474  48 8b 0c 25 03 00 00 00 mov 0x3,%rcx
475  4d 8b 9a 00 00 00 00 mov 0x0(%r10),%r11
476 
477 *******************************************************************************/
478 
480 {
481  uint8_t* pc = (uint8_t*) pr->mpc;
483 
484  // Resolve the class.
486 
487  if (c == NULL)
488  return false;
489 
490  pc += PATCHER_CALL_SIZE;
491  pc += PATCH_ALIGNMENT((uintptr_t) pc, 10, sizeof(int32_t));
492 
493  // Patch super class index.
494  patch_checked_location((int32_t*) (pc + 10), c->index);
495 
496  int disp = PATCH_ALIGNMENT((uintptr_t) (pc + 7 + 7 + 6 + 8), 3, sizeof(int32_t));
497  pc += disp;
498  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*)));
499 
500  // Synchronize instruction cache.
501  checked_icache_flush(pc + 10, sizeof(int32_t) + 6 + 8 + 3 + sizeof(int32_t) + disp, loc);
502 
503  // Patch back the original code.
504  patcher_patch_code(pr);
505 
506  return true;
507 }
508 
509 
510 /* patcher_instanceof_interface ************************************************
511 
512  Machine code:
513 
514  <patched call position>
515  45 8b 9a 1c 00 00 00 mov 0x1c(%r10),%r11d
516  41 81 fb 00 00 00 00 cmp $0x0,%r11d
517  0f 8e 94 04 00 00 jle 0x00002aaaaab018f8
518  4d 8b 9a 00 00 00 00 mov 0x0(%r10),%r11
519 
520 *******************************************************************************/
521 
523 {
524  uint8_t* pc = (uint8_t*) pr->mpc;
526 
527  // Resolve the class.
529 
530  if (c == NULL)
531  return false;
532 
533  pc += PATCHER_CALL_SIZE;
534  pc += PATCH_ALIGNMENT((uintptr_t) pc, 10, sizeof(int32_t));
535 
536  // Patch super class index.
537  patch_checked_location((int32_t*) (pc + 10), c->index);
538 
539  int disp = PATCH_ALIGNMENT((uintptr_t) (pc + 7 + 7 + 6), 3, sizeof(int32_t));
540  pc += disp;
541  int32_t *loc = patch_checked_location((int32_t*) (pc + 7 + 7 + 6 + 3), (int32_t) (OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*)));
542 
543  // Synchronize instruction cache.
544  checked_icache_flush(pc + 10, sizeof(int32_t) + 6 + 3 + sizeof(int32_t) + disp, loc);
545 
546  // Patch back the original code.
547  patcher_patch_code(pr);
548 
549  return true;
550 }
551 
552 /* patcher_function_list *******************************************************
553 
554  This is a list which maps patcher function pointers to the according
555  names of the patcher functions. It is only usefull for debugging
556  purposes.
557 
558 *******************************************************************************/
559 
560 
561 #if !defined(NDEBUG)
563  { PATCHER_initialize_class, "initialize_class" },
564 #ifdef ENABLE_VERIFIER
565  { PATCHER_resolve_class, "resolve_class" },
566 #endif /* ENABLE_VERIFIER */
567  { PATCHER_resolve_classref_to_classinfo, "resolve_classref_to_classinfo"},
568  { PATCHER_resolve_classref_to_vftbl, "resolve_classref_to_vftbl"},
569  { PATCHER_resolve_classref_to_flags, "resolve_classref_to_flags"},
570  { PATCHER_resolve_native_function, "resolve_native_function" },
571  { PATCHER_invokestatic_special, "invokestatic_special" },
572  { PATCHER_invokevirtual, "invokevirtual" },
573  { PATCHER_invokeinterface, "invokeinterface" },
574  { PATCHER_breakpoint, "breakpoint" },
575  { PATCHER_checkcast_interface, "checkcast_interface" },
576  { PATCHER_instanceof_interface, "instanceof_interface" },
577  { PATCHER_get_putstatic, "get_putstatic" },
578  { PATCHER_get_putfield, "get_putfield" },
579  { PATCHER_putfieldconst, "putfieldconst" },
580  { NULL, "-UNKNOWN PATCHER FUNCTION-" }
581 };
582 #endif
583 
584 
585 /*
586  * These are local overrides for various environment variables in Emacs.
587  * Please do not remove this and leave it at the end of the file, where
588  * Emacs will automagically detect them.
589  * ---------------------------------------------------------------------
590  * Local variables:
591  * mode: c++
592  * indent-tabs-mode: t
593  * c-basic-offset: 4
594  * tab-width: 4
595  * End:
596  * vim:noexpandtab:sw=4:ts=4:
597  */
#define PATCHER_resolve_classref_to_flags
bool patcher_invokestatic_special(patchref_t *pr)
Definition: patcher.cpp:392
bool patcher_get_putstatic(patchref_t *pr)
Definition: patcher.cpp:264
#define PATCHER_invokeinterface
#define ra
Definition: md-asm.hpp:62
static void checked_icache_flush(void *addr, int nbytes, int32_t *check_loc)
Definition: patcher.cpp:69
patcher_function_list
bool patcher_resolve_classref_to_classinfo(patchref_t *pr)
Definition: patcher.cpp:148
methodinfo * methods
Definition: class.hpp:113
#define PATCHER_resolve_classref_to_vftbl
bool patcher_invokeinterface(patchref_t *pr)
Definition: patcher.cpp:477
#define PATCHER_CALL_SIZE
Definition: codegen.hpp:68
#define PATCHER_get_putfield
#define PATCHER_resolve_class
#define PATCHER_putfieldconst
Definition: patcher.hpp:41
static void md_dcacheflush(void *addr, int nbytes)
Definition: md.hpp:167
u1 * methodptr
Definition: global.hpp:40
#define PATCHER_breakpoint
patcher_function_list_t patcher_function_list[]
Definition: patcher.cpp:607
#define PATCHER_get_putstatic
#define PATCHER_instanceof_interface
#define PATCHER_resolve_native_function
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
#define OFFSET(s, el)
Definition: memory.hpp:90
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:60
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:229
#define PATCHER_checkcast_interface
bool patcher_get_putfield(patchref_t *pr)
Definition: patcher.cpp:308
#define PATCHER_invokestatic_special
vftbl_t * vftbl
Definition: class.hpp:121
fieldinfo * resolve_field_eager(unresolved_field *ref)
Definition: resolve.cpp:1497
#define PATCHER_invokevirtual
#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:97
#define PATCHER_initialize_class
classinfo * resolve_classref_eager(constant_classref *ref)
Definition: resolve.cpp:961
#define PATCHER_resolve_classref_to_classinfo
static void md_icacheflush(void *addr, int nbytes)
Definition: md.hpp:155
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 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