#ifndef BASE_ALLOCATOR_PARTITION_ALLOCATOR_PKEY_H_
#define BASE_ALLOCATOR_PARTITION_ALLOCATOR_PKEY_H_
#include "base/allocator/partition_allocator/partition_alloc_buildflags.h"
#if BUILDFLAG(ENABLE_PKEYS)
#include "base/allocator/partition_allocator/page_allocator_constants.h"
#include "base/allocator/partition_allocator/partition_alloc_base/component_export.h"
#include "base/allocator/partition_allocator/partition_alloc_base/debug/debugging_buildflags.h"
#include <cstddef>
#include <cstdint>
#if !BUILDFLAG(HAS_64_BIT_POINTERS)
#error "pkey support requires 64 bit pointers"
#endif
#define PA_PKEY_ALIGN_SZ SystemPageSize()
#define PA_PKEY_ALIGN_OFFSET_MASK (PA_PKEY_ALIGN_SZ - 1)
#define PA_PKEY_ALIGN_BASE_MASK (~PA_PKEY_ALIGN_OFFSET_MASK)
#define PA_PKEY_ALIGN alignas(PA_PKEY_ALIGN_SZ)
#define PA_PKEY_FILL_PAGE_SZ(size) \
((PA_PKEY_ALIGN_SZ - (size & PA_PKEY_ALIGN_OFFSET_MASK)) % PA_PKEY_ALIGN_SZ)
#define PA_PKEY_ARRAY_PAD_SZ(Type, count) \
PA_PKEY_FILL_PAGE_SZ(sizeof(Type) * (count - 1))
namespace partition_alloc::internal {
constexpr int kDefaultPkey = 0;
constexpr int kInvalidPkey = -1;
bool CPUHasPkeySupport();
[[nodiscard]] int PkeyMprotectIfEnabled(void* addr,
size_t len,
int prot,
int pkey);
[[nodiscard]] int PkeyMprotect(void* addr, size_t len, int prot, int pkey);
void TagGlobalsWithPkey(int pkey);
int PkeyAlloc(int access_rights);
void PkeyFree(int pkey);
uint32_t Rdpkru();
void Wrpkru(uint32_t pkru);
struct PkeySettings {
bool enabled = false;
char pad_[PA_PKEY_FILL_PAGE_SZ(sizeof(enabled))] = {};
static PkeySettings settings PA_PKEY_ALIGN PA_CONSTINIT;
};
#if BUILDFLAG(PA_DCHECK_IS_ON)
class PA_COMPONENT_EXPORT(PARTITION_ALLOC) LiftPkeyRestrictionsScope {
public:
static constexpr uint32_t kDefaultPkeyValue = 0x55555554;
static constexpr uint32_t kAllowAllPkeyValue = 0x0;
LiftPkeyRestrictionsScope();
~LiftPkeyRestrictionsScope();
private:
uint32_t saved_pkey_value_;
};
#endif
}
#else
#define PA_PKEY_ALIGN
#define PA_PKEY_FILL_PAGE_SZ(size) 0
#define PA_PKEY_ARRAY_PAD_SZ(Type, size) 0
#endif
#endif