#include "mem_map_fuchsia.h"
#include "atomic_helpers.h"
#include "common.h"
#include "string_utils.h"
#if SCUDO_FUCHSIA
#include <zircon/process.h>
#include <zircon/status.h>
#include <zircon/syscalls.h>
namespace scudo {
static void NORETURN dieOnError(zx_status_t Status, const char *FnName,
uptr Size) {
ScopedString Error;
Error.append("SCUDO ERROR: %s failed with size %zuKB (%s)", FnName,
Size >> 10, _zx_status_get_string(Status));
outputRaw(Error.data());
die();
}
static void setVmoName(zx_handle_t Vmo, const char *Name) {
size_t Len = strlen(Name);
DCHECK_LT(Len, ZX_MAX_NAME_LEN);
zx_status_t Status = _zx_object_set_property(Vmo, ZX_PROP_NAME, Name, Len);
CHECK_EQ(Status, ZX_OK);
}
static uptr getRootVmarBase() {
static atomic_uptr CachedResult = {0};
uptr Result = atomic_load(&CachedResult, memory_order_acquire);
if (UNLIKELY(!Result)) {
zx_info_vmar_t VmarInfo;
zx_status_t Status =
_zx_object_get_info(_zx_vmar_root_self(), ZX_INFO_VMAR, &VmarInfo,
sizeof(VmarInfo), nullptr, nullptr);
CHECK_EQ(Status, ZX_OK);
CHECK_NE(VmarInfo.base, 0);
atomic_store(&CachedResult, VmarInfo.base, memory_order_release);
Result = VmarInfo.base;
}
return Result;
}
static zx_handle_t getPlaceholderVmo() {
static atomic_u32 StoredVmo = {ZX_HANDLE_INVALID};
zx_handle_t Vmo = atomic_load(&StoredVmo, memory_order_acquire);
if (UNLIKELY(Vmo == ZX_HANDLE_INVALID)) {
zx_status_t Status = _zx_vmo_create(0, 0, &Vmo);
if (UNLIKELY(Status != ZX_OK))
dieOnError(Status, "zx_vmo_create", 0);
setVmoName(Vmo, "scudo:reserved");
zx_handle_t OldValue = atomic_compare_exchange_strong(
&StoredVmo, ZX_HANDLE_INVALID, Vmo, memory_order_acq_rel);
if (UNLIKELY(OldValue != ZX_HANDLE_INVALID)) {
Status = _zx_handle_close(Vmo);
CHECK_EQ(Status, ZX_OK);
Vmo = OldValue;
}
}
return Vmo;
}
static bool IsNoMemError(zx_status_t Status) {
return Status == ZX_ERR_NO_MEMORY || Status == ZX_ERR_NO_RESOURCES;
}
MemMapFuchsia::MemMapFuchsia(uptr Base, uptr Capacity)
: MapAddr(Base), WindowBase(Base), WindowSize(Capacity) {
zx_status_t Status = _zx_vmo_create(Capacity, 0, &Vmo);
if (UNLIKELY(Status != ZX_OK))
dieOnError(Status, "zx_vmo_create", Capacity);
setVmoName(Vmo, "scudo:dispatched");
}
bool MemMapFuchsia::mapImpl(UNUSED uptr Addr, uptr Size, const char *Name,
uptr Flags) {
const bool AllowNoMem = !!(Flags & MAP_ALLOWNOMEM);
const bool PreCommit = !!(Flags & MAP_PRECOMMIT);
const bool NoAccess = !!(Flags & MAP_NOACCESS);
zx_status_t Status = _zx_vmo_create(Size, 0, &Vmo);
if (UNLIKELY(Status != ZX_OK)) {
if (AllowNoMem && IsNoMemError(Status))
return false;
dieOnError(Status, "zx_vmo_create", Size);
}
if (Name != nullptr)
setVmoName(Vmo, Name);
zx_vm_option_t MapFlags = ZX_VM_ALLOW_FAULTS;
if (!NoAccess)
MapFlags |= ZX_VM_PERM_READ | ZX_VM_PERM_WRITE;
Status =
_zx_vmar_map(_zx_vmar_root_self(), MapFlags, 0, Vmo, 0, Size, &MapAddr);
if (UNLIKELY(Status != ZX_OK)) {
if (AllowNoMem && IsNoMemError(Status)) {
Status = _zx_handle_close(Vmo);
CHECK_EQ(Status, ZX_OK);
MapAddr = 0;
Vmo = ZX_HANDLE_INVALID;
return false;
}
dieOnError(Status, "zx_vmar_map", Size);
}
if (PreCommit) {
Status = _zx_vmar_op_range(_zx_vmar_root_self(), ZX_VMAR_OP_COMMIT, MapAddr,
Size, nullptr, 0);
CHECK_EQ(Status, ZX_OK);
}
WindowBase = MapAddr;
WindowSize = Size;
return true;
}
void MemMapFuchsia::unmapImpl(uptr Addr, uptr Size) {
zx_status_t Status;
if (Size == WindowSize) {
Status = _zx_handle_close(Vmo);
CHECK_EQ(Status, ZX_OK);
Status = _zx_vmar_unmap(_zx_vmar_root_self(), Addr, Size);
CHECK_EQ(Status, ZX_OK);
MapAddr = WindowBase = WindowSize = 0;
Vmo = ZX_HANDLE_INVALID;
} else {
Status = _zx_vmar_unmap(_zx_vmar_root_self(), Addr, Size);
CHECK_EQ(Status, ZX_OK);
Status = _zx_vmo_op_range(Vmo, ZX_VMO_OP_DECOMMIT, Addr - MapAddr, Size,
nullptr, 0);
CHECK_EQ(Status, ZX_OK);
if (Addr == WindowBase)
WindowBase += Size;
WindowSize -= Size;
}
}
bool MemMapFuchsia::remapImpl(uptr Addr, uptr Size, const char *Name,
uptr Flags) {
const bool AllowNoMem = !!(Flags & MAP_ALLOWNOMEM);
const bool PreCommit = !!(Flags & MAP_PRECOMMIT);
const bool NoAccess = !!(Flags & MAP_NOACCESS);
if (Name != nullptr)
setVmoName(Vmo, Name);
uptr MappedAddr;
zx_vm_option_t MapFlags = ZX_VM_ALLOW_FAULTS | ZX_VM_SPECIFIC_OVERWRITE;
if (!NoAccess)
MapFlags |= ZX_VM_PERM_READ | ZX_VM_PERM_WRITE;
zx_status_t Status =
_zx_vmar_map(_zx_vmar_root_self(), MapFlags, Addr - getRootVmarBase(),
Vmo, Addr - MapAddr, Size, &MappedAddr);
if (UNLIKELY(Status != ZX_OK)) {
if (AllowNoMem && IsNoMemError(Status))
return false;
dieOnError(Status, "zx_vmar_map", Size);
}
DCHECK_EQ(Addr, MappedAddr);
if (PreCommit) {
Status = _zx_vmar_op_range(_zx_vmar_root_self(), ZX_VMAR_OP_COMMIT, MapAddr,
Size, nullptr, 0);
CHECK_EQ(Status, ZX_OK);
}
return true;
}
void MemMapFuchsia::releaseAndZeroPagesToOSImpl(uptr From, uptr Size) {
zx_status_t Status = _zx_vmo_op_range(Vmo, ZX_VMO_OP_DECOMMIT, From - MapAddr,
Size, nullptr, 0);
CHECK_EQ(Status, ZX_OK);
}
void MemMapFuchsia::setMemoryPermissionImpl(uptr Addr, uptr Size, uptr Flags) {
const bool NoAccess = !!(Flags & MAP_NOACCESS);
zx_vm_option_t MapFlags = 0;
if (!NoAccess)
MapFlags |= ZX_VM_PERM_READ | ZX_VM_PERM_WRITE;
zx_status_t Status =
_zx_vmar_protect(_zx_vmar_root_self(), MapFlags, Addr, Size);
CHECK_EQ(Status, ZX_OK);
}
bool ReservedMemoryFuchsia::createImpl(UNUSED uptr Addr, uptr Size,
UNUSED const char *Name, uptr Flags) {
const bool AllowNoMem = !!(Flags & MAP_ALLOWNOMEM);
zx_status_t Status = _zx_vmar_map(_zx_vmar_root_self(), ZX_VM_ALLOW_FAULTS, 0,
getPlaceholderVmo(), 0, Size, &Base);
if (UNLIKELY(Status != ZX_OK)) {
if (AllowNoMem && IsNoMemError(Status))
return false;
dieOnError(Status, "zx_vmar_map", Size);
}
Capacity = Size;
return true;
}
void ReservedMemoryFuchsia::releaseImpl() {
zx_status_t Status = _zx_vmar_unmap(_zx_vmar_root_self(), Base, Capacity);
CHECK_EQ(Status, ZX_OK);
}
ReservedMemoryFuchsia::MemMapT ReservedMemoryFuchsia::dispatchImpl(uptr Addr,
uptr Size) {
return ReservedMemoryFuchsia::MemMapT(Addr, Size);
}
}
#endif