mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-05-28 21:54:10 -04:00
ro/os: use os primitives for MapProcessCodeMemory
This commit is contained in:
parent
f5052b4bca
commit
c2c0a2e169
15 changed files with 297 additions and 400 deletions
|
@ -15,65 +15,38 @@
|
|||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#include "ro_nro_utils.hpp"
|
||||
#include "ro_map_utils.hpp"
|
||||
|
||||
namespace ams::ro::impl {
|
||||
|
||||
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) {
|
||||
/* 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());
|
||||
namespace {
|
||||
|
||||
/* 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;
|
||||
ALWAYS_INLINE size_t SetupNroProcessMemoryRegions(os::ProcessMemoryRegion *regions, u64 nro_heap_address, u64 nro_heap_size, u64 bss_heap_address, u64 bss_heap_size) {
|
||||
/* Reset region count. */
|
||||
size_t num_regions = 0;
|
||||
|
||||
/* 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;
|
||||
/* We always want a region for the nro. */
|
||||
regions[num_regions++] = { nro_heap_address, nro_heap_size };
|
||||
|
||||
/* 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;
|
||||
}
|
||||
}
|
||||
|
||||
/* We succeeded, so save the code memory. */
|
||||
nro_mcm = std::move(tmp_nro_mcm);
|
||||
break;
|
||||
/* If we have bss, create a region for bss. */
|
||||
if (bss_heap_size > 0) {
|
||||
regions[num_regions++] = { bss_heap_address, bss_heap_size };
|
||||
}
|
||||
|
||||
R_UNLESS(i != RetrySearchCount, ro::ResultOutOfAddressSpace());
|
||||
return num_regions;
|
||||
}
|
||||
|
||||
/* Cancel the automatic closing of our mappings. */
|
||||
nro_mcm.Cancel();
|
||||
bss_mcm.Cancel();
|
||||
}
|
||||
|
||||
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) {
|
||||
/* Set up the process memory regions. */
|
||||
os::ProcessMemoryRegion regions[2];
|
||||
const size_t num_regions = SetupNroProcessMemoryRegions(regions, nro_heap_address, nro_heap_size, bss_heap_address, bss_heap_size);
|
||||
|
||||
/* Re-map the nro/bss as code memory in the destination process. */
|
||||
R_TRY_CATCH(os::MapProcessCodeMemory(out_base_address, process_handle, regions, num_regions)) {
|
||||
R_CONVERT(os::ResultOutOfAddressSpace, ro::ResultOutOfAddressSpace())
|
||||
} R_END_TRY_CATCH;
|
||||
|
||||
*out_base_address = base_address;
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
|
@ -82,28 +55,20 @@ namespace ams::ro::impl {
|
|||
const u64 ro_offset = rx_offset + rx_size;
|
||||
const u64 rw_offset = ro_offset + ro_size;
|
||||
|
||||
R_TRY(svc::SetProcessMemoryPermission(process_handle, base_address + rx_offset, rx_size, svc::MemoryPermission_ReadExecute));
|
||||
R_TRY(svc::SetProcessMemoryPermission(process_handle, base_address + ro_offset, ro_size, svc::MemoryPermission_Read));
|
||||
R_TRY(svc::SetProcessMemoryPermission(process_handle, base_address + rw_offset, rw_size, svc::MemoryPermission_ReadWrite));
|
||||
R_TRY(os::SetProcessMemoryPermission(process_handle, base_address + rx_offset, rx_size, os::MemoryPermission_ReadExecute));
|
||||
R_TRY(os::SetProcessMemoryPermission(process_handle, base_address + ro_offset, ro_size, os::MemoryPermission_ReadOnly));
|
||||
R_TRY(os::SetProcessMemoryPermission(process_handle, base_address + rw_offset, rw_size, os::MemoryPermission_ReadWrite));
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result UnmapNro(os::NativeHandle process_handle, u64 base_address, u64 nro_heap_address, u64 bss_heap_address, u64 bss_heap_size, u64 code_size, u64 rw_size) {
|
||||
/* First, unmap bss. */
|
||||
if (bss_heap_size > 0) {
|
||||
R_TRY(svc::UnmapProcessCodeMemory(process_handle, base_address + code_size + rw_size, bss_heap_address, bss_heap_size));
|
||||
}
|
||||
Result UnmapNro(os::NativeHandle process_handle, u64 base_address, u64 nro_heap_address, u64 nro_heap_size, u64 bss_heap_address, u64 bss_heap_size) {
|
||||
/* Set up the process memory regions. */
|
||||
os::ProcessMemoryRegion regions[2];
|
||||
const size_t num_regions = SetupNroProcessMemoryRegions(regions, nro_heap_address, nro_heap_size, bss_heap_address, bss_heap_size);
|
||||
|
||||
/* Next, unmap .rwdata */
|
||||
if (rw_size > 0) {
|
||||
R_TRY(svc::UnmapProcessCodeMemory(process_handle, base_address + code_size, nro_heap_address + code_size, rw_size));
|
||||
}
|
||||
|
||||
/* Finally, unmap .text + .rodata. */
|
||||
R_TRY(svc::UnmapProcessCodeMemory(process_handle, base_address, nro_heap_address, code_size));
|
||||
|
||||
R_SUCCEED();
|
||||
/* Unmap the nro/bss. */
|
||||
R_RETURN(os::UnmapProcessCodeMemory(process_handle, base_address, regions, num_regions));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue