v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
memory-protection-key.h
Go to the documentation of this file.
1// Copyright 2021 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_BASE_PLATFORM_MEMORY_PROTECTION_KEY_H_
6#define V8_BASE_PLATFORM_MEMORY_PROTECTION_KEY_H_
7
9
10#if V8_HAS_PKU_JIT_WRITE_PROTECT
11
12#include "include/v8-platform.h"
14
15namespace v8 {
16namespace base {
17
18// ----------------------------------------------------------------------------
19// MemoryProtectionKey
20//
21// This class has static methods for the different platform specific
22// functions related to memory protection key support.
23
24// TODO(sroettger): Consider adding this to {base::PageAllocator} (higher-level,
25// exported API) once the API is more stable and we have converged on a better
26// design (e.g., typed class wrapper around int memory protection key).
27class V8_BASE_EXPORT MemoryProtectionKey {
28 public:
29 // Sentinel value if there is no PKU support or allocation of a key failed.
30 // This is also the return value on an error of pkey_alloc() and has the
31 // benefit that calling pkey_mprotect() with -1 behaves the same as regular
32 // mprotect().
33 static constexpr int kNoMemoryProtectionKey = -1;
34
35 // The default ProtectionKey can be used to remove pkey assignments.
36 static constexpr int kDefaultProtectionKey = 0;
37
38 // Permissions for memory protection keys on top of the page's permissions.
39 // NOTE: Since there is no executable bit, the executable permission cannot be
40 // withdrawn by memory protection keys.
41 enum Permission {
42 kNoRestrictions = 0,
43 kDisableAccess = 1,
44 kDisableWrite = 2,
45 };
46
47// If sys/mman.h has PKEY support (on newer Linux distributions), ensure that
48// our definitions of the permissions is consistent with the ones in glibc.
49#if defined(PKEY_DISABLE_ACCESS)
50 static_assert(kDisableAccess == PKEY_DISABLE_ACCESS);
51 static_assert(kDisableWrite == PKEY_DISABLE_WRITE);
52#endif
53
54 // Call exactly once per process to determine if PKU is supported on this
55 // platform and initialize global data structures.
56 static bool HasMemoryProtectionKeySupport();
57
58 // Allocates a new key. Returns -1 on error.
59 static int AllocateKey();
60
61 // Associates a memory protection {key} with the given {region}.
62 // If {key} is {kNoMemoryProtectionKey} this behaves like "plain"
63 // {SetPermissions()} and associates the default key to the region. That is,
64 // explicitly calling with {kNoMemoryProtectionKey} can be used to
65 // disassociate any protection key from a region. This also means "plain"
66 // {SetPermissions()} disassociates the key from a region, making the key's
67 // access restrictions irrelevant/inactive for that region. Returns true if
68 // changing permissions and key was successful. (Returns a bool to be
69 // consistent with {SetPermissions()}). The {page_permissions} are the
70 // permissions of the page, not the key. For changing the permissions of the
71 // key, use {SetPermissionsForKey()} instead.
72 static bool SetPermissionsAndKey(
73 base::AddressRegion region,
74 v8::PageAllocator::Permission page_permissions, int key);
75
76 // Set the key's permissions. {key} must be valid, i.e. not
77 // {kNoMemoryProtectionKey}.
78 static void SetPermissionsForKey(int key, Permission permissions);
79
80 // Get the permissions of the protection key {key} for the current thread.
81 static Permission GetKeyPermission(int key);
82};
83
84} // namespace base
85} // namespace v8
86
87#endif // V8_HAS_PKU_JIT_WRITE_PROTECT
88
89#endif // V8_BASE_PLATFORM_MEMORY_PROTECTION_KEY_H_
#define V8_BASE_EXPORT
Definition base-export.h:26
std::unique_ptr< ValueMirror > key