v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
default-thread-isolated-allocator.cc
Go to the documentation of this file.
1// Copyright 2023 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
6
7#if V8_HAS_PKU_JIT_WRITE_PROTECT
8
9#if !V8_OS_LINUX
10#error pkey support in this file is only implemented on Linux
11#endif
12
13#include <sys/mman.h>
14#include <sys/syscall.h>
15#include <sys/utsname.h>
16#include <unistd.h>
17#endif
18
19#if V8_HAS_PKU_JIT_WRITE_PROTECT
20
21extern int pkey_alloc(unsigned int flags, unsigned int access_rights) V8_WEAK;
22extern int pkey_free(int pkey) V8_WEAK;
23
24namespace {
25
26bool KernelHasPkruFix() {
27 // PKU was broken on Linux kernels before 5.13 (see
28 // https://lore.kernel.org/all/20210623121456.399107624@linutronix.de/).
29 // A fix is also included in the 5.4.182 and 5.10.103 versions ("x86/fpu:
30 // Correct pkru/xstate inconsistency" by Brian Geffon <bgeffon@google.com>).
31 // Thus check the kernel version we are running on, and bail out if does not
32 // contain the fix.
33 struct utsname uname_buffer;
34 CHECK_EQ(0, uname(&uname_buffer));
35 int kernel, major, minor;
36 // Conservatively return if the release does not match the format we expect.
37 if (sscanf(uname_buffer.release, "%d.%d.%d", &kernel, &major, &minor) != 3) {
38 return false;
39 }
40
41 return kernel > 5 || (kernel == 5 && major >= 13) || // anything >= 5.13
42 (kernel == 5 && major == 4 && minor >= 182) || // 5.4 >= 5.4.182
43 (kernel == 5 && major == 10 && minor >= 103); // 5.10 >= 5.10.103
44}
45
46int PkeyAlloc() {
47#ifdef PKEY_DISABLE_WRITE
48 if (!pkey_alloc) return -1;
49
50 static bool kernel_has_pkru_fix = KernelHasPkruFix();
51 if (!kernel_has_pkru_fix) return -1;
52
53 return pkey_alloc(0, PKEY_DISABLE_WRITE);
54#else // PKEY_DISABLE_WRITE
55 return -1;
56#endif
57}
58
59int PkeyFree(int pkey) {
60 DCHECK(pkey_free);
61 return pkey_free(pkey);
62}
63
64} // namespace
65
66#endif // V8_HAS_PKU_JIT_WRITE_PROTECT
67
68namespace v8::platform {
69
71#if V8_HAS_PKU_JIT_WRITE_PROTECT
72 : pkey_(PkeyAlloc())
73#endif
74{
75}
76
78#if V8_HAS_PKU_JIT_WRITE_PROTECT
79 if (pkey_ != -1) {
80 PkeyFree(pkey_);
81 }
82#endif
83}
84
85// TODO(sroettger): this should return thread isolated (e.g. pkey-tagged) memory
86// for testing.
88 return malloc(size);
89}
90
91void DefaultThreadIsolatedAllocator::Free(void* object) { free(object); }
92
94 const {
95#if V8_HAS_PKU_JIT_WRITE_PROTECT
96 return Type::kPkey;
97#else
99#endif
100}
101
103#if V8_HAS_PKU_JIT_WRITE_PROTECT
104 return pkey_;
105#else
106 UNREACHABLE();
107#endif
108}
109
111#if V8_HAS_PKU_JIT_WRITE_PROTECT
112 return pkey_ != -1;
113#else
114 return false;
115#endif
116}
117
118} // namespace v8::platform
#define UNREACHABLE()
Definition logging.h:67
#define CHECK_EQ(lhs, rhs)
#define DCHECK(condition)
Definition logging.h:482
#define V8_WEAK
Definition v8config.h:679