Results: Implement namespaced, type-safe results.

Because I was working on multiple things at once, this commit also:
- Adds wrappers for/linker flags to wrap CXX exceptions to make them
  abort. This saves ~0x8000 of memory in every system module.
- Broadly replaces lines of the pattern if (cond) { return ResultX; }
  with R_UNLESS(!cond, ResultX());.
- Reworks the R_TRY_CATCH macros (and the result macros in general).
This commit is contained in:
Michael Scire
2019-10-24 01:40:44 -07:00
committed by SciresM
parent 15773e4755
commit 4059dc6187
169 changed files with 2172 additions and 1868 deletions

View File

@@ -27,21 +27,21 @@ namespace sts::ro::impl {
}
Result MapNro(u64 *out_base_address, Handle process_handle, u64 nro_heap_address, u64 nro_heap_size, u64 bss_heap_address, u64 bss_heap_size) {
map::MappedCodeMemory nro_mcm(ResultRoInternalError);
map::MappedCodeMemory bss_mcm(ResultRoInternalError);
map::MappedCodeMemory nro_mcm(ResultInternalError{});
map::MappedCodeMemory bss_mcm(ResultInternalError{});
u64 base_address;
/* Map the NRO, and map the BSS immediately after it. */
size_t i;
for (i = 0; i < MaxMapRetries; i++) {
map::MappedCodeMemory tmp_nro_mcm(ResultRoInternalError);
map::MappedCodeMemory tmp_nro_mcm(ResultInternalError{});
R_TRY(map::MapCodeMemoryInProcess(tmp_nro_mcm, process_handle, nro_heap_address, nro_heap_size));
base_address = tmp_nro_mcm.GetDstAddress();
if (bss_heap_size > 0) {
map::MappedCodeMemory tmp_bss_mcm(process_handle, base_address + nro_heap_size, bss_heap_address, bss_heap_size);
R_TRY_CATCH(tmp_bss_mcm.GetResult()) {
R_CATCH(ResultKernelInvalidMemoryState) {
R_CATCH(svc::ResultInvalidCurrentMemoryState) {
continue;
}
} R_END_TRY_CATCH;
@@ -59,16 +59,14 @@ namespace sts::ro::impl {
nro_mcm = std::move(tmp_nro_mcm);
break;
}
if (i == MaxMapRetries) {
return ResultRoInsufficientAddressSpace;
}
R_UNLESS(i < MaxMapRetries, ResultOutOfAddressSpace());
/* Invalidation here actually prevents them from unmapping at scope exit. */
nro_mcm.Invalidate();
bss_mcm.Invalidate();
*out_base_address = base_address;
return ResultSuccess;
return ResultSuccess();
}
Result SetNroPerms(Handle process_handle, u64 base_address, u64 rx_size, u64 ro_size, u64 rw_size) {
@@ -80,7 +78,7 @@ namespace sts::ro::impl {
R_TRY(svcSetProcessMemoryPermission(process_handle, base_address + ro_offset, ro_size, Perm_R ));
R_TRY(svcSetProcessMemoryPermission(process_handle, base_address + rw_offset, rw_size, Perm_Rw));
return ResultSuccess;
return ResultSuccess();
}
Result UnmapNro(Handle process_handle, u64 base_address, u64 nro_heap_address, u64 bss_heap_address, u64 bss_heap_size, u64 code_size, u64 rw_size) {
@@ -97,7 +95,7 @@ namespace sts::ro::impl {
/* Finally, unmap .text + .rodata. */
R_TRY(svcUnmapProcessCodeMemory(process_handle, base_address, nro_heap_address, code_size));
return ResultSuccess;
return ResultSuccess();
}
}

View File

@@ -28,33 +28,23 @@ namespace sts::ro::impl {
/* TODO: Implement RSA-2048 PSS..... */
/* TODO: Check PSS fixed-key signature. */
if (false) {
return ResultRoNotAuthorized;
}
R_UNLESS(true, ResultNotAuthorized());
/* Check TitleID pattern is valid. */
if (!header->IsTitleIdValid()) {
return ResultRoNotAuthorized;
}
R_UNLESS(header->IsTitleIdValid(), ResultNotAuthorized());
/* TODO: Check PSS signature over hashes. */
if (false) {
return ResultRoNotAuthorized;
}
R_UNLESS(true, ResultNotAuthorized());
return ResultSuccess;
return ResultSuccess();
}
Result ValidateNrr(const NrrHeader *header, u64 size, ncm::TitleId title_id, ModuleType expected_type, bool enforce_type) {
/* Check magic. */
if (!header->IsMagicValid()) {
return ResultRoInvalidNrr;
}
R_UNLESS(header->IsMagicValid(), ResultInvalidNrr());
/* Check size. */
if (header->GetSize() != size) {
return ResultRoInvalidSize;
}
R_UNLESS(header->GetSize() == size, ResultInvalidSize());
/* Only perform checks if we must. */
const bool ease_nro_restriction = ShouldEaseNroRestriction();
@@ -63,41 +53,33 @@ namespace sts::ro::impl {
R_TRY(ValidateNrrSignature(header));
/* Check title id. */
if (title_id != header->GetTitleId()) {
return ResultRoInvalidNrr;
}
R_UNLESS(header->GetTitleId() == title_id, ResultInvalidNrr());
/* Check type. */
if (hos::GetVersion() >= hos::Version_700 && enforce_type) {
if (expected_type != header->GetType()) {
return ResultRoInvalidNrrType;
}
R_UNLESS(header->GetType() == expected_type, ResultInvalidNrrType());
}
}
return ResultSuccess;
return ResultSuccess();
}
}
/* Utilities for working with NRRs. */
Result MapAndValidateNrr(NrrHeader **out_header, u64 *out_mapped_code_address, Handle process_handle, ncm::TitleId title_id, u64 nrr_heap_address, u64 nrr_heap_size, ModuleType expected_type, bool enforce_type) {
map::MappedCodeMemory nrr_mcm(ResultRoInternalError);
map::MappedCodeMemory nrr_mcm(ResultInternalError{});
/* First, map the NRR. */
R_TRY(map::MapCodeMemoryInProcess(nrr_mcm, process_handle, nrr_heap_address, nrr_heap_size));
const u64 code_address = nrr_mcm.GetDstAddress();
uintptr_t map_address;
if (R_FAILED(map::LocateMappableSpace(&map_address, nrr_heap_size))) {
return ResultRoInsufficientAddressSpace;
}
R_UNLESS(R_SUCCEEDED(map::LocateMappableSpace(&map_address, nrr_heap_size)), ResultOutOfAddressSpace());
/* Nintendo...does not check the return value of this map. We will check, instead of aborting if it fails. */
map::AutoCloseMap nrr_map(map_address, process_handle, code_address, nrr_heap_size);
if (!nrr_map.IsSuccess()) {
return nrr_map.GetResult();
}
R_TRY(nrr_map.GetResult());
NrrHeader *nrr_header = reinterpret_cast<NrrHeader *>(map_address);
R_TRY(ValidateNrr(nrr_header, nrr_heap_size, title_id, expected_type, enforce_type));
@@ -108,13 +90,13 @@ namespace sts::ro::impl {
*out_header = nrr_header;
*out_mapped_code_address = code_address;
return ResultSuccess;
return ResultSuccess();
}
Result UnmapNrr(Handle process_handle, const NrrHeader *header, u64 nrr_heap_address, u64 nrr_heap_size, u64 mapped_code_address) {
R_TRY(svcUnmapProcessMemory(reinterpret_cast<void *>(const_cast<NrrHeader *>(header)), process_handle, mapped_code_address, nrr_heap_size));
R_TRY(svcUnmapProcessCodeMemory(process_handle, mapped_code_address, nrr_heap_address, nrr_heap_size));
return ResultSuccess;
return ResultSuccess();
}
}

View File

@@ -105,10 +105,10 @@ namespace sts::ro::impl {
if (out != nullptr) {
*out = &this->nrr_infos[i];
}
return ResultSuccess;
return ResultSuccess();
}
}
return ResultRoNotRegistered;
return ResultNotRegistered();
}
Result GetFreeNrrInfo(NrrInfo **out) {
@@ -117,10 +117,10 @@ namespace sts::ro::impl {
if (out != nullptr) {
*out = &this->nrr_infos[i];
}
return ResultSuccess;
return ResultSuccess();
}
}
return ResultRoTooManyNrr;
return ResultTooManyNrr();
}
Result GetNroInfoByAddress(NroInfo **out, u64 nro_address) {
@@ -129,10 +129,10 @@ namespace sts::ro::impl {
if (out != nullptr) {
*out = &this->nro_infos[i];
}
return ResultSuccess;
return ResultSuccess();
}
}
return ResultRoNotLoaded;
return ResultNotLoaded();
}
Result GetNroInfoByModuleId(NroInfo **out, const ModuleId *module_id) {
@@ -141,10 +141,10 @@ namespace sts::ro::impl {
if (out != nullptr) {
*out = &this->nro_infos[i];
}
return ResultSuccess;
return ResultSuccess();
}
}
return ResultRoNotLoaded;
return ResultNotLoaded();
}
Result GetFreeNroInfo(NroInfo **out) {
@@ -153,10 +153,10 @@ namespace sts::ro::impl {
if (out != nullptr) {
*out = &this->nro_infos[i];
}
return ResultSuccess;
return ResultSuccess();
}
}
return ResultRoTooManyNro;
return ResultTooManyNro();
}
Result ValidateHasNroHash(const NroHeader *nro_header) const {
@@ -168,34 +168,27 @@ namespace sts::ro::impl {
if (this->nrr_infos[i].in_use) {
const NrrHeader *nrr_header = this->nrr_infos[i].header;
const Sha256Hash *nro_hashes = reinterpret_cast<const Sha256Hash *>(nrr_header->GetHashes());
if (std::binary_search(nro_hashes, nro_hashes + nrr_header->GetNumHashes(), hash)) {
return ResultSuccess;
}
R_UNLESS(!std::binary_search(nro_hashes, nro_hashes + nrr_header->GetNumHashes(), hash), ResultSuccess());
}
}
return ResultRoNotAuthorized;
return ResultNotAuthorized();
}
Result ValidateNro(ModuleId *out_module_id, u64 *out_rx_size, u64 *out_ro_size, u64 *out_rw_size, u64 base_address, u64 expected_nro_size, u64 expected_bss_size) {
/* Find space to map the NRO. */
uintptr_t map_address;
if (R_FAILED(map::LocateMappableSpace(&map_address, expected_nro_size))) {
return ResultRoInsufficientAddressSpace;
}
R_UNLESS(R_SUCCEEDED(map::LocateMappableSpace(&map_address, expected_nro_size)), ResultOutOfAddressSpace());
/* Actually map the NRO. */
map::AutoCloseMap nro_map(map_address, this->process_handle, base_address, expected_nro_size);
if (!nro_map.IsSuccess()) {
return nro_map.GetResult();
}
R_TRY(nro_map.GetResult());
/* Validate header. */
const NroHeader *header = reinterpret_cast<const NroHeader *>(map_address);
if (!header->IsMagicValid()) {
return ResultRoInvalidNro;
}
R_UNLESS(header->IsMagicValid(), ResultInvalidNro());
/* Read sizes from header. */
const u64 nro_size = header->GetSize();
const u64 text_ofs = header->GetTextOffset();
const u64 text_size = header->GetTextSize();
@@ -204,27 +197,33 @@ namespace sts::ro::impl {
const u64 rw_ofs = header->GetRwOffset();
const u64 rw_size = header->GetRwSize();
const u64 bss_size = header->GetBssSize();
if (nro_size != expected_nro_size || bss_size != expected_bss_size) {
return ResultRoInvalidNro;
}
if ((text_size & 0xFFF) || (ro_size & 0xFFF) || (rw_size & 0xFFF) || (bss_size & 0xFFF)) {
return ResultRoInvalidNro;
}
if (text_ofs > ro_ofs || ro_ofs > rw_ofs) {
return ResultRoInvalidNro;
}
if (text_ofs != 0 || text_ofs + text_size != ro_ofs || ro_ofs + ro_size != rw_ofs || rw_ofs + rw_size != nro_size) {
return ResultRoInvalidNro;
}
/* Validate sizes meet expected. */
R_UNLESS(nro_size == expected_nro_size, ResultInvalidNro());
R_UNLESS(bss_size == expected_bss_size, ResultInvalidNro());
/* Validate all sizes are aligned. */
R_UNLESS(util::IsAligned(text_size, 0x1000), ResultInvalidNro());
R_UNLESS(util::IsAligned(ro_size, 0x1000), ResultInvalidNro());
R_UNLESS(util::IsAligned(rw_size, 0x1000), ResultInvalidNro());
R_UNLESS(util::IsAligned(bss_size, 0x1000), ResultInvalidNro());
/* Validate sections are in order. */
R_UNLESS(text_ofs <= ro_ofs, ResultInvalidNro());
R_UNLESS(ro_ofs <= rw_ofs, ResultInvalidNro());
/* Validate sections are sequential and contiguous. */
R_UNLESS(text_ofs == 0, ResultInvalidNro());
R_UNLESS(text_ofs + text_size == ro_ofs, ResultInvalidNro());
R_UNLESS(ro_ofs + ro_size == rw_ofs, ResultInvalidNro());
R_UNLESS(rw_ofs + rw_size == nro_size, ResultInvalidNro());
/* Verify NRO hash. */
R_TRY(this->ValidateHasNroHash(header));
/* Check if NRO has already been loaded. */
const ModuleId *module_id = header->GetModuleId();
if (R_SUCCEEDED(this->GetNroInfoByModuleId(nullptr, module_id))) {
return ResultRoAlreadyLoaded;
}
R_UNLESS(R_FAILED(this->GetNroInfoByModuleId(nullptr, module_id)), ResultAlreadyLoaded());
/* Apply patches to NRO. */
LocateAndApplyIpsPatchesToModule(module_id, reinterpret_cast<u8 *>(map_address), nro_size);
@@ -234,7 +233,7 @@ namespace sts::ro::impl {
*out_rx_size = text_size;
*out_ro_size = ro_size;
*out_rw_size = rw_size;
return ResultSuccess;
return ResultSuccess();
}
};
@@ -295,6 +294,14 @@ namespace sts::ro::impl {
}
}
constexpr inline Result ValidateAddressAndSize(u64 address, u64 size) {
R_UNLESS(util::IsAligned(address, 0x1000), ResultInvalidAddress());
R_UNLESS(size != 0, ResultInvalidSize());
R_UNLESS(util::IsAligned(size, 0x1000), ResultInvalidSize());
R_UNLESS(address < address + size, ResultInvalidSize());
return ResultSuccess();
}
}
/* Access utilities. */
@@ -334,31 +341,24 @@ namespace sts::ro::impl {
os::ProcessId handle_pid = os::InvalidProcessId;
/* Validate handle is a valid process handle. */
if (R_FAILED(os::TryGetProcessId(&handle_pid, process_handle))) {
return ResultRoInvalidProcess;
}
R_UNLESS(R_SUCCEEDED(os::TryGetProcessId(&handle_pid, process_handle)), ResultInvalidProcess());
/* Validate process id. */
if (handle_pid != process_id) {
return ResultRoInvalidProcess;
}
R_UNLESS(handle_pid == process_id, ResultInvalidProcess());
}
/* Check if a process context already exists. */
if (GetContextByProcessId(process_id) != nullptr) {
return ResultRoInvalidSession;
}
R_UNLESS(GetContextByProcessId(process_id) == nullptr, ResultInvalidSession());
*out_context_id = AllocateContext(process_handle, process_id);
return ResultSuccess;
return ResultSuccess();
}
Result ValidateProcess(size_t context_id, os::ProcessId process_id) {
const ProcessContext *ctx = GetContextById(context_id);
if (ctx == nullptr || ctx->process_id != process_id) {
return ResultRoInvalidProcess;
}
return ResultSuccess;
R_UNLESS(ctx != nullptr, ResultInvalidProcess());
R_UNLESS(ctx->process_id == process_id, ResultInvalidProcess());
return ResultSuccess();
}
void UnregisterProcess(size_t context_id) {
@@ -375,12 +375,7 @@ namespace sts::ro::impl {
const ncm::TitleId title_id = context->GetTitleId(process_h);
/* Validate address/size. */
if (nrr_address & 0xFFF) {
return ResultRoInvalidAddress;
}
if (nrr_size == 0 || (nrr_size & 0xFFF) || !(nrr_address < nrr_address + nrr_size)) {
return ResultRoInvalidSize;
}
R_TRY(ValidateAddressAndSize(nrr_address, nrr_size));
/* Check we have space for a new NRR. */
NrrInfo *nrr_info = nullptr;
@@ -398,7 +393,7 @@ namespace sts::ro::impl {
nrr_info->nrr_heap_size = nrr_size;
nrr_info->mapped_code_address = mapped_code_address;
return ResultSuccess;
return ResultSuccess();
}
Result UnloadNrr(size_t context_id, u64 nrr_address) {
@@ -407,9 +402,7 @@ namespace sts::ro::impl {
STS_ASSERT(context != nullptr);
/* Validate address. */
if (nrr_address & 0xFFF) {
return ResultRoInvalidAddress;
}
R_UNLESS(util::IsAligned(nrr_address, 0x1000), ResultInvalidAddress());
/* Check the NRR is loaded. */
NrrInfo *nrr_info = nullptr;
@@ -431,23 +424,12 @@ namespace sts::ro::impl {
STS_ASSERT(context != nullptr);
/* Validate address/size. */
if (nro_address & 0xFFF) {
return ResultRoInvalidAddress;
}
if (nro_size == 0 || (nro_size & 0xFFF) || !(nro_address < nro_address + nro_size)) {
return ResultRoInvalidSize;
}
if (bss_address & 0xFFF) {
return ResultRoInvalidAddress;
}
if ((bss_size & 0xFFF) || (bss_size > 0 && !(bss_address < bss_address + bss_size))) {
return ResultRoInvalidSize;
}
R_TRY(ValidateAddressAndSize(nro_address, nro_size));
R_TRY(ValidateAddressAndSize(bss_address, bss_size));
const u64 total_size = nro_size + bss_size;
if (total_size < nro_size || total_size < bss_size) {
return ResultRoInvalidSize;
}
R_UNLESS(total_size >= nro_size, ResultInvalidSize());
R_UNLESS(total_size >= bss_size, ResultInvalidSize());
/* Check we have space for a new NRO. */
NroInfo *nro_info = nullptr;
@@ -462,20 +444,24 @@ namespace sts::ro::impl {
/* Validate the NRO (parsing region extents). */
u64 rx_size = 0, ro_size = 0, rw_size = 0;
R_TRY_CLEANUP(context->ValidateNro(&nro_info->module_id, &rx_size, &ro_size, &rw_size, nro_info->base_address, nro_size, bss_size), {
UnmapNro(context->process_handle, nro_info->base_address, nro_address, bss_address, bss_size, nro_size, 0);
});
{
auto unmap_guard = SCOPE_GUARD { UnmapNro(context->process_handle, nro_info->base_address, nro_address, bss_address, bss_size, nro_size, 0); };
R_TRY(context->ValidateNro(&nro_info->module_id, &rx_size, &ro_size, &rw_size, nro_info->base_address, nro_size, bss_size));
unmap_guard.Cancel();
}
/* Set NRO perms. */
R_TRY_CLEANUP(SetNroPerms(context->process_handle, nro_info->base_address, rx_size, ro_size, rw_size + bss_size), {
UnmapNro(context->process_handle, nro_info->base_address, nro_address, bss_address, bss_size, rx_size + ro_size, rw_size);
});
{
auto unmap_guard = SCOPE_GUARD { UnmapNro(context->process_handle, nro_info->base_address, nro_address, bss_address, bss_size, rx_size + ro_size, rw_size); };
R_TRY(SetNroPerms(context->process_handle, nro_info->base_address, rx_size, ro_size, rw_size + bss_size));
unmap_guard.Cancel();
}
nro_info->code_size = rx_size + ro_size;
nro_info->rw_size = rw_size;
nro_info->in_use = true;
*out_address = nro_info->base_address;
return ResultSuccess;
return ResultSuccess();
}
Result UnloadNro(size_t context_id, u64 nro_address) {
@@ -484,9 +470,7 @@ namespace sts::ro::impl {
STS_ASSERT(context != nullptr);
/* Validate address. */
if (nro_address & 0xFFF) {
return ResultRoInvalidAddress;
}
R_UNLESS(util::IsAligned(nro_address, 0x1000), ResultInvalidAddress());
/* Check the NRO is loaded. */
NroInfo *nro_info = nullptr;
@@ -522,7 +506,7 @@ namespace sts::ro::impl {
}
*out_count = static_cast<u32>(count);
return ResultSuccess;
return ResultSuccess();
}
}