strat: remove map namespace, svc: add address space defs

This commit is contained in:
Michael Scire 2021-10-05 12:22:34 -07:00
parent 69777cf792
commit 719ead824e
17 changed files with 643 additions and 494 deletions

View file

@ -15,53 +15,63 @@
*/
#include <stratosphere.hpp>
#include "ro_nro_utils.hpp"
#include "ro_map_utils.hpp"
namespace ams::ro::impl {
namespace {
constexpr size_t MaxMapRetries = 0x200;
}
Result MapNro(u64 *out_base_address, os::NativeHandle process_handle, u64 nro_heap_address, u64 nro_heap_size, u64 bss_heap_address, u64 bss_heap_size) {
map::MappedCodeMemory nro_mcm(ResultInternalError{});
map::MappedCodeMemory bss_mcm(ResultInternalError{});
/* Re-map the NRO/BSS as code memory in the destination process. */
MappedCodeMemory nro_mcm;
MappedCodeMemory bss_mcm;
ProcessRegionInfo region_info(process_handle);
u64 base_address;
{
const u64 memory_size = nro_heap_size + bss_heap_size;
int i;
for (i = 0; i < RetrySearchCount; ++i) {
/* Get a random address for the nro. */
base_address = region_info.GetAslrRegion(memory_size);
R_UNLESS(base_address != 0, ro::ResultOutOfAddressSpace());
/* Map the NRO, and map the BSS immediately after it. */
size_t i;
for (i = 0; i < MaxMapRetries; i++) {
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(svc::ResultInvalidCurrentMemory) {
continue;
}
/* Map the NRO, retrying if random address was invalid. */
MappedCodeMemory tmp_nro_mcm(process_handle, base_address, nro_heap_address, nro_heap_size);
R_TRY_CATCH(tmp_nro_mcm.GetResult()) {
R_CATCH(svc::ResultInvalidCurrentMemory) { continue; }
} R_END_TRY_CATCH;
if (!map::CanAddGuardRegionsInProcess(process_handle, base_address, nro_heap_size + bss_heap_size)) {
continue;
/* Handle bss. */
if (bss_heap_size > 0) {
/* Map BSS, retrying if random address was invalid. */
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(svc::ResultInvalidCurrentMemory) { continue; }
} R_END_TRY_CATCH;
/* Check that we can have guard spaces. */
if (!region_info.CanEmplaceGuardSpaces(process_handle, base_address, memory_size)) {
continue;
}
/* We succeeded, so save the bss memory. */
bss_mcm = std::move(tmp_bss_mcm);
} else {
/* Check that we can have guard spaces. */
if (!region_info.CanEmplaceGuardSpaces(process_handle, base_address, memory_size)) {
continue;
}
}
bss_mcm = std::move(tmp_bss_mcm);
} else {
if (!map::CanAddGuardRegionsInProcess(process_handle, base_address, nro_heap_size)) {
continue;
}
/* We succeeded, so save the code memory. */
nro_mcm = std::move(tmp_nro_mcm);
break;
}
nro_mcm = std::move(tmp_nro_mcm);
break;
}
R_UNLESS(i < MaxMapRetries, ResultOutOfAddressSpace());
/* Invalidation here actually prevents them from unmapping at scope exit. */
nro_mcm.Invalidate();
bss_mcm.Invalidate();
R_UNLESS(i != RetrySearchCount, ro::ResultOutOfAddressSpace());
}
/* Cancel the automatic closing of our mappings. */
nro_mcm.Cancel();
bss_mcm.Cancel();
*out_base_address = base_address;
return ResultSuccess();