#include "gin/thread_isolation.h"
#if PA_BUILDFLAG(ENABLE_THREAD_ISOLATION)
#include <sys/mman.h>
#include <sys/utsname.h>
#include <cstddef>
#include "base/check.h"
#include "base/check_op.h"
#include "base/compiler_specific.h"
#include "base/memory/page_size.h"
#include "base/metrics/histogram_functions.h"
#include "base/no_destructor.h"
#include "partition_alloc/thread_isolation/alignment.h"
WEAK_SYMBOL extern int pkey_alloc(unsigned int flags,
unsigned int access_rights);
namespace {
bool KernelHasPkruFix() {
struct utsname uname_buffer;
CHECK_EQ(0, uname(&uname_buffer));
int kernel, major, minor;
if (UNSAFE_BUFFERS(sscanf(uname_buffer.release, "%d.%d.%d", &kernel, &major,
&minor)) != 3) {
return -1;
}
return kernel > 5 || (kernel == 5 && major >= 13) ||
(kernel == 5 && major == 4 && minor >= 182) ||
(kernel == 5 && major == 10 && minor >= 103);
}
int PkeyAlloc(int access_rights) {
if (!pkey_alloc) {
return -1;
}
static bool kernel_has_pkru_fix = KernelHasPkruFix();
if (!kernel_has_pkru_fix) {
return -1;
}
return pkey_alloc(0, access_rights);
}
uint32_t Rdpkru() {
uint32_t pkru;
asm volatile(".byte 0x0f,0x01,0xee\n" : "=a"(pkru) : "c"(0), "d"(0));
return pkru;
}
void Wrpkru(uint32_t pkru) {
asm volatile(".byte 0x0f,0x01,0xef\n" : : "a"(pkru), "c"(0), "d"(0));
}
static constexpr uint32_t kBitsPerPkey = 2;
void PkeyDisableWriteAccess(int pkey) {
#ifdef PKEY_DISABLE_WRITE
uint32_t pkru = Rdpkru();
uint32_t disable_write = static_cast<uint32_t>(PKEY_DISABLE_WRITE)
<< (static_cast<uint32_t>(pkey) * kBitsPerPkey);
Wrpkru(pkru | disable_write);
#endif
}
}
namespace gin {
void ThreadIsolationData::InitializeBeforeThreadCreation() {
bool page_size_mismatch = PA_THREAD_ISOLATED_ALIGN_SZ < base::GetPageSize();
base::UmaHistogramBoolean("V8.CFIPageSizeMismatch", page_size_mismatch);
if (page_size_mismatch) {
return;
}
pkey = PkeyAlloc(0);
if (pkey == -1) {
return;
}
allocator->Initialize(pkey);
PkeyDisableWriteAccess(pkey);
}
bool ThreadIsolationData::Initialized() const {
return pkey != -1;
}
ThreadIsolationData& GetThreadIsolationData() {
static ThreadIsolationData thread_isolation_data;
DCHECK_EQ((reinterpret_cast<size_t>(&thread_isolation_data) %
PA_THREAD_ISOLATED_ALIGN_SZ),
0llu);
return thread_isolation_data;
}
}
#endif