Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
code_patcher.cc
Go to the documentation of this 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
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
13#if defined(DART_HOST_OS_MACOS) || defined(DART_HOST_OS_MACOS_IOS)
14// On iOS even with debugger attached we must still guarantee that memory
15// is never executable and writable at the same time. On Mac OS X
16// com.apple.security.cs.allow-jit entitelement allows WX memory regions to be
17// created - but we should not rely on this entitelement to be present.
18static constexpr bool kShouldWriteProtectCodeByDefault = true;
19#else
20static constexpr bool kShouldWriteProtectCodeByDefault = false;
21#endif
22
24 write_protect_code,
26 "Write protect jitted code");
27
28#if defined(TARGET_ARCH_IA32)
29WritableInstructionsScope::WritableInstructionsScope(uword address,
30 intptr_t size)
31 : address_(address), size_(size) {
32 if (FLAG_write_protect_code) {
33 VirtualMemory::Protect(reinterpret_cast<void*>(address), size,
35 }
36}
37
38WritableInstructionsScope::~WritableInstructionsScope() {
39 if (FLAG_write_protect_code) {
40 VirtualMemory::Protect(reinterpret_cast<void*>(address_), size_,
41 VirtualMemory::kReadExecute);
42 }
43}
44#endif // defined(TARGET_ARCH_IA32)
45
46bool MatchesPattern(uword end, const int16_t* pattern, intptr_t size) {
47 // When breaking within generated code in GDB, it may overwrite individual
48 // instructions with trap instructions, which can cause this test to fail.
49 //
50 // Ignoring trap instructions would work well enough within GDB alone, but it
51 // doesn't work in RR, because the check for the trap instruction itself will
52 // cause replay to diverge from the original record.
53 if (FLAG_support_rr) return true;
54
55 uint8_t* bytes = reinterpret_cast<uint8_t*>(end - size);
56 for (intptr_t i = 0; i < size; i++) {
57 int16_t val = pattern[i];
58 if ((val >= 0) && (val != bytes[i])) {
59 return false;
60 }
61 }
62 return true;
63}
64
65} // namespace dart
static void Protect(void *address, intptr_t size, Protection mode)
glong glong end
#define DEFINE_FLAG(type, name, default_value, comment)
Definition flags.h:16
uintptr_t uword
Definition globals.h:501
bool MatchesPattern(uword end, const int16_t *pattern, intptr_t size)
static constexpr bool kShouldWriteProtectCodeByDefault