Fix FullScreen crash in Webapp
[platform/framework/web/chromium-efl.git] / gin / thread_isolation.cc
1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "gin/thread_isolation.h"
6
7 #if BUILDFLAG(ENABLE_THREAD_ISOLATION)
8
9 #include <sys/mman.h>
10 #include <sys/utsname.h>
11 #include <cstddef>
12
13 #include "base/allocator/partition_allocator/src/partition_alloc/thread_isolation/alignment.h"
14 #include "base/check.h"
15 #include "base/check_op.h"
16 #include "base/memory/page_size.h"
17 #include "base/metrics/histogram_functions.h"
18 #include "base/no_destructor.h"
19 #include "third_party/abseil-cpp/absl/base/attributes.h"
20
21 extern int pkey_alloc(unsigned int flags,
22                       unsigned int access_rights) ABSL_ATTRIBUTE_WEAK;
23
24 namespace {
25
26 bool 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 -1;
39   }
40   return kernel > 5 || (kernel == 5 && major >= 13) ||   // anything >= 5.13
41          (kernel == 5 && major == 4 && minor >= 182) ||  // 5.4 >= 5.4.182
42          (kernel == 5 && major == 10 && minor >= 103);   // 5.10 >= 5.10.103
43 }
44
45 int PkeyAlloc(int access_rights) {
46   if (!pkey_alloc) {
47     return -1;
48   }
49
50   static bool kernel_has_pkru_fix = KernelHasPkruFix();
51   if (!kernel_has_pkru_fix) {
52     return -1;
53   }
54
55   return pkey_alloc(0, access_rights);
56 }
57
58 uint32_t Rdpkru() {
59   uint32_t pkru;
60   asm volatile(".byte 0x0f,0x01,0xee\n" : "=a"(pkru) : "c"(0), "d"(0));
61   return pkru;
62 }
63
64 void Wrpkru(uint32_t pkru) {
65   asm volatile(".byte 0x0f,0x01,0xef\n" : : "a"(pkru), "c"(0), "d"(0));
66 }
67
68 static constexpr uint32_t kBitsPerPkey = 2;
69
70 void PkeyDisableWriteAccess(int pkey) {
71 #ifdef PKEY_DISABLE_WRITE
72   uint32_t pkru = Rdpkru();
73   uint32_t disable_write = static_cast<uint32_t>(PKEY_DISABLE_WRITE)
74                            << (static_cast<uint32_t>(pkey) * kBitsPerPkey);
75   Wrpkru(pkru | disable_write);
76 #endif
77 }
78
79 }  // namespace
80
81 namespace gin {
82
83 void ThreadIsolationData::InitializeBeforeThreadCreation() {
84   bool page_size_mismatch = PA_THREAD_ISOLATED_ALIGN_SZ < base::GetPageSize();
85   base::UmaHistogramBoolean("V8.CFIPageSizeMismatch", page_size_mismatch);
86   if (page_size_mismatch) {
87     // We write-protect global variables and need to align and pad them to (a
88     // multiple of) the OS page size. But since page size is not a compile time
89     // constant, check at runtime that our value was large enough.
90     return;
91   }
92
93   pkey = PkeyAlloc(0);
94   if (pkey == -1) {
95     return;
96   }
97   allocator->Initialize(pkey);
98   PkeyDisableWriteAccess(pkey);
99 }
100
101 bool ThreadIsolationData::Initialized() const {
102   return pkey != -1;
103 }
104
105 ThreadIsolationData& GetThreadIsolationData() {
106   static ThreadIsolationData thread_isolation_data;
107   DCHECK_EQ((reinterpret_cast<size_t>(&thread_isolation_data) %
108              PA_THREAD_ISOLATED_ALIGN_SZ),
109             0llu);
110   return thread_isolation_data;
111 }
112
113 }  // namespace gin
114
115 #endif  // BUILDFLAG(ENABLE_THREAD_ISOLATION)