OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "vm/code_patcher.h" | 5 #include "vm/code_patcher.h" |
6 #include "vm/cpu.h" | 6 #include "vm/cpu.h" |
7 #include "vm/instructions.h" | 7 #include "vm/instructions.h" |
8 #include "vm/object.h" | 8 #include "vm/object.h" |
9 #include "vm/virtual_memory.h" | 9 #include "vm/virtual_memory.h" |
10 | 10 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 void CodePatcher::PatchEntry(const Code& code) { | 54 void CodePatcher::PatchEntry(const Code& code) { |
55 ASSERT(!IsEntryPatched(code)); | 55 ASSERT(!IsEntryPatched(code)); |
56 const uword patch_addr = code.GetEntryPatchPc(); | 56 const uword patch_addr = code.GetEntryPatchPc(); |
57 ASSERT(patch_addr != 0); | 57 ASSERT(patch_addr != 0); |
58 JumpPattern jmp_entry(patch_addr, code); | 58 JumpPattern jmp_entry(patch_addr, code); |
59 ASSERT(!jmp_entry.IsValid()); | 59 ASSERT(!jmp_entry.IsValid()); |
60 const uword patch_buffer = code.GetPatchCodePc(); | 60 const uword patch_buffer = code.GetPatchCodePc(); |
61 ASSERT(patch_buffer != 0); | 61 ASSERT(patch_buffer != 0); |
62 JumpPattern jmp_patch(patch_buffer, code); | 62 JumpPattern jmp_patch(patch_buffer, code); |
63 ASSERT(jmp_patch.IsValid()); | 63 ASSERT(jmp_patch.IsValid()); |
64 const uword jump_target = jmp_patch.TargetAddress(); | 64 const Code& jump_target = Code::Handle(jmp_patch.TargetCode()); |
65 intptr_t length = jmp_patch.pattern_length_in_bytes(); | 65 intptr_t length = jmp_patch.pattern_length_in_bytes(); |
66 { | 66 { |
67 WritableInstructionsScope writable_code(patch_addr, length); | 67 WritableInstructionsScope writable_code(patch_addr, length); |
68 WritableInstructionsScope writable_buffer(patch_buffer, length); | 68 WritableInstructionsScope writable_buffer(patch_buffer, length); |
69 SwapCode(jmp_patch.pattern_length_in_bytes(), | 69 SwapCode(jmp_patch.pattern_length_in_bytes(), |
70 reinterpret_cast<char*>(patch_addr), | 70 reinterpret_cast<char*>(patch_addr), |
71 reinterpret_cast<char*>(patch_buffer)); | 71 reinterpret_cast<char*>(patch_buffer)); |
72 jmp_entry.SetTargetAddress(jump_target); | 72 jmp_entry.SetTargetCode(jump_target); |
73 } | 73 } |
74 } | 74 } |
75 | 75 |
76 | 76 |
77 // The entry point is a jmp instruction, the patch code buffer contains | 77 // The entry point is a jmp instruction, the patch code buffer contains |
78 // original code, the entry point contains the jump instruction. | 78 // original code, the entry point contains the jump instruction. |
79 void CodePatcher::RestoreEntry(const Code& code) { | 79 void CodePatcher::RestoreEntry(const Code& code) { |
80 if (!IsEntryPatched(code)) return; | 80 if (!IsEntryPatched(code)) return; |
81 const uword patch_addr = code.GetEntryPatchPc(); | 81 const uword patch_addr = code.GetEntryPatchPc(); |
82 ASSERT(patch_addr != 0); | 82 ASSERT(patch_addr != 0); |
83 JumpPattern jmp_entry(patch_addr, code); | 83 JumpPattern jmp_entry(patch_addr, code); |
84 ASSERT(jmp_entry.IsValid()); | 84 ASSERT(jmp_entry.IsValid()); |
85 const uword jump_target = jmp_entry.TargetAddress(); | 85 const Code& jump_target = Code::Handle(jmp_entry.TargetCode()); |
86 const uword patch_buffer = code.GetPatchCodePc(); | 86 const uword patch_buffer = code.GetPatchCodePc(); |
87 ASSERT(patch_buffer != 0); | 87 ASSERT(patch_buffer != 0); |
88 // 'patch_buffer' contains original entry code. | 88 // 'patch_buffer' contains original entry code. |
89 JumpPattern jmp_patch(patch_buffer, code); | 89 JumpPattern jmp_patch(patch_buffer, code); |
90 ASSERT(!jmp_patch.IsValid()); | 90 ASSERT(!jmp_patch.IsValid()); |
91 intptr_t length = jmp_patch.pattern_length_in_bytes(); | 91 intptr_t length = jmp_patch.pattern_length_in_bytes(); |
92 { | 92 { |
93 WritableInstructionsScope writable_code(patch_addr, length); | 93 WritableInstructionsScope writable_code(patch_addr, length); |
94 WritableInstructionsScope writable_buffer(patch_buffer, length); | 94 WritableInstructionsScope writable_buffer(patch_buffer, length); |
95 SwapCode(jmp_patch.pattern_length_in_bytes(), | 95 SwapCode(jmp_patch.pattern_length_in_bytes(), |
96 reinterpret_cast<char*>(patch_addr), | 96 reinterpret_cast<char*>(patch_addr), |
97 reinterpret_cast<char*>(patch_buffer)); | 97 reinterpret_cast<char*>(patch_buffer)); |
98 ASSERT(jmp_patch.IsValid()); | 98 ASSERT(jmp_patch.IsValid()); |
99 jmp_patch.SetTargetAddress(jump_target); | 99 jmp_patch.SetTargetCode(jump_target); |
100 } | 100 } |
101 } | 101 } |
102 | 102 |
103 | 103 |
104 bool CodePatcher::IsEntryPatched(const Code& code) { | 104 bool CodePatcher::IsEntryPatched(const Code& code) { |
105 const uword patch_addr = code.GetEntryPatchPc(); | 105 const uword patch_addr = code.GetEntryPatchPc(); |
106 if (patch_addr == 0) { | 106 if (patch_addr == 0) { |
107 return false; | 107 return false; |
108 } | 108 } |
109 JumpPattern jmp_entry(patch_addr, code); | 109 JumpPattern jmp_entry(patch_addr, code); |
(...skipping 17 matching lines...) Expand all Loading... |
127 const uword obj_start = code.GetPointerOffsetAt(i) + code.EntryPoint(); | 127 const uword obj_start = code.GetPointerOffsetAt(i) + code.EntryPoint(); |
128 const uword obj_end = obj_start + kWordSize; | 128 const uword obj_end = obj_start + kWordSize; |
129 if ((obj_start < limit) && (obj_end > patch_addr)) { | 129 if ((obj_start < limit) && (obj_end > patch_addr)) { |
130 return false; | 130 return false; |
131 } | 131 } |
132 } | 132 } |
133 return true; | 133 return true; |
134 } | 134 } |
135 | 135 |
136 } // namespace dart | 136 } // namespace dart |
OLD | NEW |