| 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 buffer++; | 45 buffer++; |
| 46 } | 46 } |
| 47 CPU::FlushICache(code_address, num_bytes); | 47 CPU::FlushICache(code_address, num_bytes); |
| 48 // The buffer is not executed. No need to flush. | 48 // The buffer is not executed. No need to flush. |
| 49 } | 49 } |
| 50 | 50 |
| 51 | 51 |
| 52 // The patch code buffer contains the jmp code which will be inserted at | 52 // The patch code buffer contains the jmp code which will be inserted at |
| 53 // entry point. | 53 // entry point. |
| 54 void CodePatcher::PatchEntry(const Code& code) { | 54 void CodePatcher::PatchEntry(const Code& code) { |
| 55 ASSERT(!IsEntryPatched(code)); |
| 55 const uword patch_addr = code.GetEntryPatchPc(); | 56 const uword patch_addr = code.GetEntryPatchPc(); |
| 56 ASSERT(patch_addr != 0); | 57 ASSERT(patch_addr != 0); |
| 57 JumpPattern jmp_entry(patch_addr, code); | 58 JumpPattern jmp_entry(patch_addr, code); |
| 58 ASSERT(!jmp_entry.IsValid()); | 59 ASSERT(!jmp_entry.IsValid()); |
| 59 const uword patch_buffer = code.GetPatchCodePc(); | 60 const uword patch_buffer = code.GetPatchCodePc(); |
| 60 ASSERT(patch_buffer != 0); | 61 ASSERT(patch_buffer != 0); |
| 61 JumpPattern jmp_patch(patch_buffer, code); | 62 JumpPattern jmp_patch(patch_buffer, code); |
| 62 ASSERT(jmp_patch.IsValid()); | 63 ASSERT(jmp_patch.IsValid()); |
| 63 const uword jump_target = jmp_patch.TargetAddress(); | 64 const uword jump_target = jmp_patch.TargetAddress(); |
| 64 intptr_t length = jmp_patch.pattern_length_in_bytes(); | 65 intptr_t length = jmp_patch.pattern_length_in_bytes(); |
| 65 { | 66 { |
| 66 WritableInstructionsScope writable_code(patch_addr, length); | 67 WritableInstructionsScope writable_code(patch_addr, length); |
| 67 WritableInstructionsScope writable_buffer(patch_buffer, length); | 68 WritableInstructionsScope writable_buffer(patch_buffer, length); |
| 68 SwapCode(jmp_patch.pattern_length_in_bytes(), | 69 SwapCode(jmp_patch.pattern_length_in_bytes(), |
| 69 reinterpret_cast<char*>(patch_addr), | 70 reinterpret_cast<char*>(patch_addr), |
| 70 reinterpret_cast<char*>(patch_buffer)); | 71 reinterpret_cast<char*>(patch_buffer)); |
| 71 jmp_entry.SetTargetAddress(jump_target); | 72 jmp_entry.SetTargetAddress(jump_target); |
| 72 } | 73 } |
| 73 } | 74 } |
| 74 | 75 |
| 75 | 76 |
| 76 // 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 |
| 77 // original code, the entry point contains the jump instruction. | 78 // original code, the entry point contains the jump instruction. |
| 78 void CodePatcher::RestoreEntry(const Code& code) { | 79 void CodePatcher::RestoreEntry(const Code& code) { |
| 80 if (!IsEntryPatched(code)) return; |
| 79 const uword patch_addr = code.GetEntryPatchPc(); | 81 const uword patch_addr = code.GetEntryPatchPc(); |
| 80 ASSERT(patch_addr != 0); | 82 ASSERT(patch_addr != 0); |
| 81 JumpPattern jmp_entry(patch_addr, code); | 83 JumpPattern jmp_entry(patch_addr, code); |
| 82 ASSERT(jmp_entry.IsValid()); | 84 ASSERT(jmp_entry.IsValid()); |
| 83 const uword jump_target = jmp_entry.TargetAddress(); | 85 const uword jump_target = jmp_entry.TargetAddress(); |
| 84 const uword patch_buffer = code.GetPatchCodePc(); | 86 const uword patch_buffer = code.GetPatchCodePc(); |
| 85 ASSERT(patch_buffer != 0); | 87 ASSERT(patch_buffer != 0); |
| 86 // 'patch_buffer' contains original entry code. | 88 // 'patch_buffer' contains original entry code. |
| 87 JumpPattern jmp_patch(patch_buffer, code); | 89 JumpPattern jmp_patch(patch_buffer, code); |
| 88 ASSERT(!jmp_patch.IsValid()); | 90 ASSERT(!jmp_patch.IsValid()); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 const uword obj_start = code.GetPointerOffsetAt(i) + code.EntryPoint(); | 127 const uword obj_start = code.GetPointerOffsetAt(i) + code.EntryPoint(); |
| 126 const uword obj_end = obj_start + kWordSize; | 128 const uword obj_end = obj_start + kWordSize; |
| 127 if ((obj_start < limit) && (obj_end > patch_addr)) { | 129 if ((obj_start < limit) && (obj_end > patch_addr)) { |
| 128 return false; | 130 return false; |
| 129 } | 131 } |
| 130 } | 132 } |
| 131 return true; | 133 return true; |
| 132 } | 134 } |
| 133 | 135 |
| 134 } // namespace dart | 136 } // namespace dart |
| OLD | NEW |