From ed86c44a498ba493c123d91cc95121bf4645ddc5 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Sat, 20 Apr 2019 18:15:39 -0700 Subject: [PATCH] loader: refactor to use LoaderModuleInfo --- .../loader/source/ldr_debug_monitor.cpp | 6 ++-- .../loader/source/ldr_debug_monitor.hpp | 7 ++-- .../loader/source/ldr_process_creation.cpp | 2 +- .../loader/source/ldr_registration.cpp | 32 +++++++++--------- .../loader/source/ldr_registration.hpp | 33 ++++++++----------- 5 files changed, 36 insertions(+), 44 deletions(-) diff --git a/stratosphere/loader/source/ldr_debug_monitor.cpp b/stratosphere/loader/source/ldr_debug_monitor.cpp index 2ab52d4c8..cbb10faa3 100644 --- a/stratosphere/loader/source/ldr_debug_monitor.cpp +++ b/stratosphere/loader/source/ldr_debug_monitor.cpp @@ -31,9 +31,9 @@ void DebugMonitorService::ClearLaunchQueue() { LaunchQueue::Clear(); } -Result DebugMonitorService::GetNsoInfo(Out count, OutPointerWithClientSize out, u64 pid) { +Result DebugMonitorService::GetProcessModuleInfo(Out count, OutPointerWithClientSize out, u64 pid) { /* Zero out the output memory. */ - std::fill(out.pointer, out.pointer + out.num_elements, Registration::NsoInfo{}); + std::memset(out.pointer, 0, out.num_elements * sizeof(LoaderModuleInfo)); /* Actually return the nso infos. */ - return Registration::GetNsoInfosForProcessId(out.pointer, out.num_elements, pid, count.GetPointer()); + return Registration::GetProcessModuleInfo(out.pointer, out.num_elements, pid, count.GetPointer()); } diff --git a/stratosphere/loader/source/ldr_debug_monitor.hpp b/stratosphere/loader/source/ldr_debug_monitor.hpp index d61c419c8..2e87c1521 100644 --- a/stratosphere/loader/source/ldr_debug_monitor.hpp +++ b/stratosphere/loader/source/ldr_debug_monitor.hpp @@ -18,12 +18,11 @@ #include #include -#include "ldr_registration.hpp" enum DebugMonitorServiceCmd { Dmnt_Cmd_AddTitleToLaunchQueue = 0, Dmnt_Cmd_ClearLaunchQueue = 1, - Dmnt_Cmd_GetNsoInfo = 2 + Dmnt_Cmd_GetProcessModuleInfo = 2 }; class DebugMonitorService final : public IServiceObject { @@ -31,11 +30,11 @@ class DebugMonitorService final : public IServiceObject { /* Actual commands. */ Result AddTitleToLaunchQueue(u64 tid, InPointer args, u32 args_size); void ClearLaunchQueue(); - Result GetNsoInfo(Out count, OutPointerWithClientSize out, u64 pid); + Result GetProcessModuleInfo(Out count, OutPointerWithClientSize out, u64 pid); public: DEFINE_SERVICE_DISPATCH_TABLE { MakeServiceCommandMeta(), MakeServiceCommandMeta(), - MakeServiceCommandMeta(), + MakeServiceCommandMeta(), }; }; diff --git a/stratosphere/loader/source/ldr_process_creation.cpp b/stratosphere/loader/source/ldr_process_creation.cpp index 840bd910b..cf86d97fe 100644 --- a/stratosphere/loader/source/ldr_process_creation.cpp +++ b/stratosphere/loader/source/ldr_process_creation.cpp @@ -214,7 +214,7 @@ Result ProcessCreation::CreateProcess(Handle *out_process_h, u64 index, char *nc Registration::SetProcessIdTidAndIs64BitAddressSpace(index, process_id, npdm_info.aci0->title_id, is_64_bit_addspace); for (unsigned int i = 0; i < NSO_NUM_MAX; i++) { if (NsoUtils::IsNsoPresent(i)) { - Registration::AddNsoInfo(index, nso_extents.nso_addresses[i], nso_extents.nso_sizes[i], NsoUtils::GetNsoBuildId(i)); + Registration::AddModuleInfo(index, nso_extents.nso_addresses[i], nso_extents.nso_sizes[i], NsoUtils::GetNsoBuildId(i)); } } diff --git a/stratosphere/loader/source/ldr_registration.cpp b/stratosphere/loader/source/ldr_registration.cpp index db37baae1..3d01cad43 100644 --- a/stratosphere/loader/source/ldr_registration.cpp +++ b/stratosphere/loader/source/ldr_registration.cpp @@ -34,7 +34,7 @@ Registration::Process *Registration::GetFreeProcess() { } Registration::Process *Registration::GetProcess(u64 index) { - for (unsigned int i = 0; i < REGISTRATION_LIST_MAX; i++) { + for (unsigned int i = 0; i < Registration::MaxProcesses; i++) { if (g_registration_list.processes[i].in_use && g_registration_list.processes[i].index == index) { return &g_registration_list.processes[i]; } @@ -43,7 +43,7 @@ Registration::Process *Registration::GetProcess(u64 index) { } Registration::Process *Registration::GetProcessByProcessId(u64 pid) { - for (unsigned int i = 0; i < REGISTRATION_LIST_MAX; i++) { + for (unsigned int i = 0; i < Registration::MaxProcesses; i++) { if (g_registration_list.processes[i].in_use && g_registration_list.processes[i].process_id == pid) { return &g_registration_list.processes[i]; } @@ -52,7 +52,7 @@ Registration::Process *Registration::GetProcessByProcessId(u64 pid) { } Registration::Process *Registration::GetProcessByRoService(void *service) { - for (unsigned int i = 0; i < REGISTRATION_LIST_MAX; i++) { + for (unsigned int i = 0; i < Registration::MaxProcesses; i++) { if (g_registration_list.processes[i].in_use && g_registration_list.processes[i].owner_ro_service == service) { return &g_registration_list.processes[i]; } @@ -109,17 +109,17 @@ void Registration::SetProcessIdTidAndIs64BitAddressSpace(u64 index, u64 process_ target_process->is_64_bit_addspace = is_64_bit_addspace; } -void Registration::AddNsoInfo(u64 index, u64 base_address, u64 size, const unsigned char *build_id) { +void Registration::AddModuleInfo(u64 index, u64 base_address, u64 size, const unsigned char *build_id) { Registration::Process *target_process = GetProcess(index); if (target_process == NULL) { return; } - auto nso_info_it = std::find_if_not(target_process->nso_infos.begin(), target_process->nso_infos.end(), std::mem_fn(&Registration::NsoInfoHolder::in_use)); - if (nso_info_it != target_process->nso_infos.end()) { + auto nso_info_it = std::find_if_not(target_process->module_infos.begin(), target_process->module_infos.end(), std::mem_fn(&Registration::ModuleInfoHolder::in_use)); + if (nso_info_it != target_process->module_infos.end()) { nso_info_it->info.base_address = base_address; nso_info_it->info.size = size; - std::copy(build_id, build_id + sizeof(nso_info_it->info.build_id), nso_info_it->info.build_id); + memcpy(nso_info_it->info.build_id, build_id, sizeof(nso_info_it->info.build_id)); nso_info_it->in_use = true; } } @@ -129,7 +129,7 @@ void Registration::CloseRoService(void *service, Handle process_h) { if (target_process == NULL) { return; } - for (unsigned int i = 0; i < NRR_INFO_MAX; i++) { + for (unsigned int i = 0; i < Registration::MaxNrrInfos; i++) { if (target_process->nrr_infos[i].IsActive() && target_process->nrr_infos[i].process_handle == process_h) { target_process->nrr_infos[i].Close(); } @@ -159,7 +159,7 @@ Result Registration::RemoveNrrInfo(u64 index, u64 base_address) { return ResultLoaderProcessNotRegistered; } - for (unsigned int i = 0; i < NRR_INFO_MAX; i++) { + for (unsigned int i = 0; i < Registration::MaxNrrInfos; i++) { if (target_process->nrr_infos[i].IsActive() && target_process->nrr_infos[i].base_address == base_address) { target_process->nrr_infos[i].Close(); return ResultSuccess; @@ -176,7 +176,7 @@ bool Registration::IsNroHashPresent(u64 index, u8 *nro_hash) { return false; } - for (unsigned int i = 0; i < NRR_INFO_MAX; i++) { + for (unsigned int i = 0; i < Registration::MaxNrrInfos; i++) { if (target_process->nrr_infos[i].IsActive()) { NroUtils::NrrHeader *nrr = (NroUtils::NrrHeader *)target_process->nrr_infos[i].mapped_address; /* Binary search. */ @@ -205,7 +205,7 @@ bool Registration::IsNroAlreadyLoaded(u64 index, u8 *build_id) { return true; } - for (unsigned int i = 0; i < NRO_INFO_MAX; i++) { + for (unsigned int i = 0; i < Registration::MaxNroInfos; i++) { if (target_process->nro_infos[i].in_use && std::equal(build_id, build_id + 0x20, target_process->nro_infos[i].build_id)) { return true; } @@ -241,7 +241,7 @@ Result Registration::RemoveNroInfo(u64 index, Handle process_h, u64 nro_heap_add return ResultLoaderProcessNotRegistered; } - for (unsigned int i = 0; i < NRO_INFO_MAX; i++) { + for (unsigned int i = 0; i < Registration::MaxNroInfos; i++) { if (target_process->nro_infos[i].in_use && target_process->nro_infos[i].nro_heap_address == nro_heap_address) { NroInfo *info = &target_process->nro_infos[i]; Result rc = svcUnmapProcessCodeMemory(process_h, info->base_address + info->text_size + info->ro_size + info->rw_size, info->bss_heap_address, info->bss_heap_size); @@ -258,16 +258,16 @@ Result Registration::RemoveNroInfo(u64 index, Handle process_h, u64 nro_heap_add return ResultLoaderNotLoaded; } -Result Registration::GetNsoInfosForProcessId(Registration::NsoInfo *out, u32 max_out, u64 process_id, u32 *num_written) { +Result Registration::GetProcessModuleInfo(LoaderModuleInfo *out, u32 max_out, u64 process_id, u32 *num_written) { Registration::Process *target_process = GetProcessByProcessId(process_id); if (target_process == NULL) { return ResultLoaderProcessNotRegistered; } u32 cur = 0; - for (unsigned int i = 0; i < NSO_INFO_MAX && cur < max_out; i++) { - if (target_process->nso_infos[i].in_use) { - out[cur++] = target_process->nso_infos[i].info; + for (unsigned int i = 0; i < Registration::MaxModuleInfos && cur < max_out; i++) { + if (target_process->module_infos[i].in_use) { + out[cur++] = target_process->module_infos[i].info; } } diff --git a/stratosphere/loader/source/ldr_registration.hpp b/stratosphere/loader/source/ldr_registration.hpp index a3b6ba318..27f458ee7 100644 --- a/stratosphere/loader/source/ldr_registration.hpp +++ b/stratosphere/loader/source/ldr_registration.hpp @@ -20,23 +20,16 @@ #include "ldr_map.hpp" -#define REGISTRATION_LIST_MAX (0x40) - -#define NSO_INFO_MAX (0x20) -#define NRR_INFO_MAX (0x40) -#define NRO_INFO_MAX (0x40) - class Registration { public: - struct NsoInfo { - u64 base_address; - u64 size; - unsigned char build_id[0x20]; - }; - - struct NsoInfoHolder { + static constexpr size_t MaxProcesses = 0x40; + static constexpr size_t MaxModuleInfos = 0x20; + static constexpr size_t MaxNrrInfos = 0x40; + static constexpr size_t MaxNroInfos = 0x40; + public: + struct ModuleInfoHolder { bool in_use; - NsoInfo info; + LoaderModuleInfo info; }; struct NroInfo { @@ -65,14 +58,14 @@ class Registration { u64 process_id; u64 title_id; Registration::TidSid tid_sid; - std::array nso_infos; - std::array nro_infos; - std::array nrr_infos; + std::array module_infos; + std::array nro_infos; + std::array nrr_infos; void *owner_ro_service; }; struct List { - std::array processes; + std::array processes; u64 num_processes; }; @@ -84,7 +77,7 @@ class Registration { static bool RegisterTidSid(const TidSid *tid_sid, u64 *out_index); static bool UnregisterIndex(u64 index); static void SetProcessIdTidAndIs64BitAddressSpace(u64 index, u64 process_id, u64 tid, bool is_64_bit_addspace); - static void AddNsoInfo(u64 index, u64 base_address, u64 size, const unsigned char *build_id); + static void AddModuleInfo(u64 index, u64 base_address, u64 size, const unsigned char *build_id); static void CloseRoService(void *service, Handle process_h); static Result AddNrrInfo(u64 index, MappedCodeMemory *nrr_info); static Result RemoveNrrInfo(u64 index, u64 base_address); @@ -92,7 +85,7 @@ class Registration { static bool IsNroAlreadyLoaded(u64 index, u8 *build_id); static void AddNroToProcess(u64 index, MappedCodeMemory *nro, MappedCodeMemory *bss, u32 text_size, u32 ro_size, u32 rw_size, u8 *build_id); static Result RemoveNroInfo(u64 index, Handle process_h, u64 base_address); - static Result GetNsoInfosForProcessId(NsoInfo *out, u32 max_out, u64 process_id, u32 *num_written); + static Result GetProcessModuleInfo(LoaderModuleInfo *out, u32 max_out, u64 process_id, u32 *num_written); /* Atmosphere MitM Extension. */ static void AssociatePidTidForMitM(u64 index);