summaryrefslogtreecommitdiff
path: root/chromium/v8/src/common/code-memory-access.cc
blob: aaad4e000ce115afef66851e3602bf97261315cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2022 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/common/code-memory-access-inl.h"

namespace v8 {
namespace internal {

#if V8_HAS_PTHREAD_JIT_WRITE_PROTECT || V8_HAS_PKU_JIT_WRITE_PROTECT
thread_local int RwxMemoryWriteScope::code_space_write_nesting_level_ = 0;
#endif  // V8_HAS_PTHREAD_JIT_WRITE_PROTECT || V8_HAS_PKU_JIT_WRITE_PROTECT

#if V8_HAS_PKU_JIT_WRITE_PROTECT
int RwxMemoryWriteScope::memory_protection_key_ =
    base::MemoryProtectionKey::kNoMemoryProtectionKey;

#if DEBUG
thread_local bool
    RwxMemoryWriteScope::is_key_permissions_initialized_for_current_thread_ =
        false;
bool RwxMemoryWriteScope::pkey_initialized = false;

bool RwxMemoryWriteScope::is_key_permissions_initialized_for_current_thread() {
  return is_key_permissions_initialized_for_current_thread_;
}
#endif  // DEBUG

void RwxMemoryWriteScope::InitializeMemoryProtectionKey() {
  // Flip {pkey_initialized} (in debug mode) and check the new value.
  DCHECK_EQ(true, pkey_initialized = !pkey_initialized);
  memory_protection_key_ = base::MemoryProtectionKey::AllocateKey();
  DCHECK(memory_protection_key_ > 0 ||
         memory_protection_key_ ==
             base::MemoryProtectionKey::kNoMemoryProtectionKey);
}

bool RwxMemoryWriteScope::IsPKUWritable() {
  DCHECK(pkey_initialized);
  return base::MemoryProtectionKey::GetKeyPermission(memory_protection_key_) ==
         base::MemoryProtectionKey::kNoRestrictions;
}

ResetPKUPermissionsForThreadSpawning::ResetPKUPermissionsForThreadSpawning() {
  if (!RwxMemoryWriteScope::IsSupported()) return;
  was_writable_ = RwxMemoryWriteScope::IsPKUWritable();
  if (was_writable_) {
    base::MemoryProtectionKey::SetPermissionsForKey(
        RwxMemoryWriteScope::memory_protection_key(),
        base::MemoryProtectionKey::kDisableWrite);
  }
}

ResetPKUPermissionsForThreadSpawning::~ResetPKUPermissionsForThreadSpawning() {
  if (!RwxMemoryWriteScope::IsSupported()) return;
  if (was_writable_) {
    base::MemoryProtectionKey::SetPermissionsForKey(
        RwxMemoryWriteScope::memory_protection_key(),
        base::MemoryProtectionKey::kNoRestrictions);
  }
}

void RwxMemoryWriteScope::SetDefaultPermissionsForNewThread() {
  // TODO(v8:13023): consider initializing the permissions only once per thread
  // if the SetPermissionsForKey() call is too heavy.
  if (RwxMemoryWriteScope::IsSupported() &&
      base::MemoryProtectionKey::GetKeyPermission(
          RwxMemoryWriteScope::memory_protection_key()) ==
          base::MemoryProtectionKey::kDisableAccess) {
    base::MemoryProtectionKey::SetPermissionsForKey(
        RwxMemoryWriteScope::memory_protection_key(),
        base::MemoryProtectionKey::kDisableWrite);
  }
#if DEBUG
  is_key_permissions_initialized_for_current_thread_ = true;
#endif
}

void RwxMemoryWriteScope::SetDefaultPermissionsForSignalHandler() {
  DCHECK(pkey_initialized);
  DCHECK(is_key_permissions_initialized_for_current_thread_);
  if (!RwxMemoryWriteScope::IsSupported()) return;
  base::MemoryProtectionKey::SetPermissionsForKey(
      memory_protection_key_, base::MemoryProtectionKey::kDisableWrite);
}
#else  // !V8_HAS_PKU_JIT_WRITE_PROTECT

void RwxMemoryWriteScope::SetDefaultPermissionsForNewThread() {}

#if DEBUG
bool RwxMemoryWriteScope::is_key_permissions_initialized_for_current_thread() {
  return true;
}
#endif  // DEBUG
#endif  // V8_HAS_PKU_JIT_WRITE_PROTECT

RwxMemoryWriteScopeForTesting::RwxMemoryWriteScopeForTesting()
    : RwxMemoryWriteScope("For Testing") {}

RwxMemoryWriteScopeForTesting::~RwxMemoryWriteScopeForTesting() {}

}  // namespace internal
}  // namespace v8