| 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 | 9 |
| 10 namespace dart { | 10 namespace dart { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 // The buffer is not executed. No need to flush. | 22 // The buffer is not executed. No need to flush. |
| 23 } | 23 } |
| 24 | 24 |
| 25 | 25 |
| 26 // The patch code buffer contains the jmp code which will be inserted at | 26 // The patch code buffer contains the jmp code which will be inserted at |
| 27 // entry point. | 27 // entry point. |
| 28 void CodePatcher::PatchEntry(const Code& code) { | 28 void CodePatcher::PatchEntry(const Code& code) { |
| 29 const uword patch_addr = code.GetPcForDeoptId(Isolate::kNoDeoptId, | 29 const uword patch_addr = code.GetPcForDeoptId(Isolate::kNoDeoptId, |
| 30 PcDescriptors::kEntryPatch); | 30 PcDescriptors::kEntryPatch); |
| 31 ASSERT(patch_addr != 0); | 31 ASSERT(patch_addr != 0); |
| 32 JumpPattern jmp_entry(patch_addr); | 32 JumpPattern jmp_entry(patch_addr, code); |
| 33 ASSERT(!jmp_entry.IsValid()); | 33 ASSERT(!jmp_entry.IsValid()); |
| 34 const uword patch_buffer = code.GetPatchCodePc(); | 34 const uword patch_buffer = code.GetPatchCodePc(); |
| 35 ASSERT(patch_buffer != 0); | 35 ASSERT(patch_buffer != 0); |
| 36 JumpPattern jmp_patch(patch_buffer); | 36 JumpPattern jmp_patch(patch_buffer, code); |
| 37 ASSERT(jmp_patch.IsValid()); | 37 ASSERT(jmp_patch.IsValid()); |
| 38 const uword jump_target = jmp_patch.TargetAddress(); | 38 const uword jump_target = jmp_patch.TargetAddress(); |
| 39 SwapCode(jmp_patch.pattern_length_in_bytes(), | 39 SwapCode(jmp_patch.pattern_length_in_bytes(), |
| 40 reinterpret_cast<char*>(patch_addr), | 40 reinterpret_cast<char*>(patch_addr), |
| 41 reinterpret_cast<char*>(patch_buffer)); | 41 reinterpret_cast<char*>(patch_buffer)); |
| 42 jmp_entry.SetTargetAddress(jump_target); | 42 jmp_entry.SetTargetAddress(jump_target); |
| 43 } | 43 } |
| 44 | 44 |
| 45 | 45 |
| 46 // The entry point is a jmp instruction, the patch code buffer contains | 46 // The entry point is a jmp instruction, the patch code buffer contains |
| 47 // original code, the entry point contains the jump instruction. | 47 // original code, the entry point contains the jump instruction. |
| 48 void CodePatcher::RestoreEntry(const Code& code) { | 48 void CodePatcher::RestoreEntry(const Code& code) { |
| 49 const uword patch_addr = code.GetPcForDeoptId(Isolate::kNoDeoptId, | 49 const uword patch_addr = code.GetPcForDeoptId(Isolate::kNoDeoptId, |
| 50 PcDescriptors::kEntryPatch); | 50 PcDescriptors::kEntryPatch); |
| 51 ASSERT(patch_addr != 0); | 51 ASSERT(patch_addr != 0); |
| 52 JumpPattern jmp_entry(patch_addr); | 52 JumpPattern jmp_entry(patch_addr, code); |
| 53 ASSERT(jmp_entry.IsValid()); | 53 ASSERT(jmp_entry.IsValid()); |
| 54 const uword jump_target = jmp_entry.TargetAddress(); | 54 const uword jump_target = jmp_entry.TargetAddress(); |
| 55 const uword patch_buffer = code.GetPatchCodePc(); | 55 const uword patch_buffer = code.GetPatchCodePc(); |
| 56 ASSERT(patch_buffer != 0); | 56 ASSERT(patch_buffer != 0); |
| 57 // 'patch_buffer' contains original entry code. | 57 // 'patch_buffer' contains original entry code. |
| 58 JumpPattern jmp_patch(patch_buffer); | 58 JumpPattern jmp_patch(patch_buffer, code); |
| 59 ASSERT(!jmp_patch.IsValid()); | 59 ASSERT(!jmp_patch.IsValid()); |
| 60 SwapCode(jmp_patch.pattern_length_in_bytes(), | 60 SwapCode(jmp_patch.pattern_length_in_bytes(), |
| 61 reinterpret_cast<char*>(patch_addr), | 61 reinterpret_cast<char*>(patch_addr), |
| 62 reinterpret_cast<char*>(patch_buffer)); | 62 reinterpret_cast<char*>(patch_buffer)); |
| 63 ASSERT(jmp_patch.IsValid()); | 63 ASSERT(jmp_patch.IsValid()); |
| 64 jmp_patch.SetTargetAddress(jump_target); | 64 jmp_patch.SetTargetAddress(jump_target); |
| 65 } | 65 } |
| 66 | 66 |
| 67 | 67 |
| 68 bool CodePatcher::CodeIsPatchable(const Code& code) { | 68 bool CodePatcher::CodeIsPatchable(const Code& code) { |
| 69 const uword patch_addr = code.GetPcForDeoptId(Isolate::kNoDeoptId, | 69 const uword patch_addr = code.GetPcForDeoptId(Isolate::kNoDeoptId, |
| 70 PcDescriptors::kEntryPatch); | 70 PcDescriptors::kEntryPatch); |
| 71 // kEntryPatch may not exist which means the function is not patchable. | 71 // kEntryPatch may not exist which means the function is not patchable. |
| 72 if (patch_addr == 0) { | 72 if (patch_addr == 0) { |
| 73 return true; | 73 return true; |
| 74 } | 74 } |
| 75 JumpPattern jmp_entry(patch_addr); | 75 JumpPattern jmp_entry(patch_addr, code); |
| 76 if (code.Size() < (jmp_entry.pattern_length_in_bytes() * 2)) { | 76 if (code.Size() < (jmp_entry.pattern_length_in_bytes() * 2)) { |
| 77 return false; | 77 return false; |
| 78 } | 78 } |
| 79 const uword limit = patch_addr + jmp_entry.pattern_length_in_bytes(); | 79 const uword limit = patch_addr + jmp_entry.pattern_length_in_bytes(); |
| 80 // Check no object stored between patch_addr .. limit. | 80 // Check no object stored between patch_addr .. limit. |
| 81 for (intptr_t i = 0; i < code.pointer_offsets_length(); i++) { | 81 for (intptr_t i = 0; i < code.pointer_offsets_length(); i++) { |
| 82 const uword obj_start = code.GetPointerOffsetAt(i) + code.EntryPoint(); | 82 const uword obj_start = code.GetPointerOffsetAt(i) + code.EntryPoint(); |
| 83 const uword obj_end = obj_start + kWordSize; | 83 const uword obj_end = obj_start + kWordSize; |
| 84 if ((obj_start < limit) && (obj_end > patch_addr)) { | 84 if ((obj_start < limit) && (obj_end > patch_addr)) { |
| 85 return false; | 85 return false; |
| 86 } | 86 } |
| 87 } | 87 } |
| 88 return true; | 88 return true; |
| 89 } | 89 } |
| 90 | 90 |
| 91 } // namespace dart | 91 } // namespace dart |
| OLD | NEW |