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
3// BSD-style license that can be found in the LICENSE file.
4
5#include "vm/code_patcher.h"
6#include "vm/cpu.h"
7#include "vm/instructions.h"
8#include "vm/object.h"
9#include "vm/virtual_memory.h"
10
11namespace dart {
12
13DEFINE_FLAG(bool, write_protect_code, true, "Write protect jitted code");
14
15#if defined(DUAL_MAPPING_SUPPORTED)
16DEFINE_FLAG(bool, dual_map_code, true, "Dual map jitted code, RW and RX");
17#else
18DEFINE_FLAG(bool, dual_map_code, false, "Dual map jitted code, RW and RX");
19#endif // defined(DUAL_MAPPING_SUPPORTED)
20
21#if defined(TARGET_ARCH_IA32)
22WritableInstructionsScope::WritableInstructionsScope(uword address,
23 intptr_t size)
24 : address_(address), size_(size) {
25 if (FLAG_write_protect_code) {
26 VirtualMemory::Protect(reinterpret_cast<void*>(address), size,
27 VirtualMemory::kReadWrite);
28 }
29}
30
31WritableInstructionsScope::~WritableInstructionsScope() {
32 if (FLAG_write_protect_code) {
33 VirtualMemory::Protect(reinterpret_cast<void*>(address_), size_,
34 VirtualMemory::kReadExecute);
35 }
36}
37#endif // defined(TARGET_ARCH_IA32)
38
39bool MatchesPattern(uword end, const int16_t* pattern, intptr_t size) {
40 // When breaking within generated code in GDB, it may overwrite individual
41 // instructions with trap instructions, which can cause this test to fail.
42 //
43 // Ignoring trap instructions would work well enough within GDB alone, but it
44 // doesn't work in RR, because the check for the trap instrution itself will
45 // cause replay to diverge from the original record.
46 if (FLAG_support_rr) return true;
47
48 uint8_t* bytes = reinterpret_cast<uint8_t*>(end - size);
49 for (intptr_t i = 0; i < size; i++) {
50 int16_t val = pattern[i];
51 if ((val >= 0) && (val != bytes[i])) {
52 return false;
53 }
54 }
55 return true;
56}
57
58} // namespace dart
59