os: remove ManagedHandle, refactor to use NativeHandle typename

This commit is contained in:
Michael Scire
2021-10-04 12:33:09 -07:00
parent a774833790
commit 6f76066d24
71 changed files with 473 additions and 397 deletions

View File

@@ -469,7 +469,10 @@ namespace ams::mitm::sysupdater {
return ResultSuccess();
}
Result SystemUpdateService::SetupUpdateImpl(os::ManagedHandle transfer_memory, u64 transfer_memory_size, const ncm::Path &path, bool exfat, ncm::FirmwareVariationId firmware_variation_id) {
Result SystemUpdateService::SetupUpdateImpl(os::NativeHandle transfer_memory, u64 transfer_memory_size, const ncm::Path &path, bool exfat, ncm::FirmwareVariationId firmware_variation_id) {
/* Ensure the transfer memory handle is closed, if we exit before creating the management object. */
auto handle_guard = SCOPE_GUARD { os::CloseNativeHandle(transfer_memory); };
/* Ensure we don't already have an update set up. */
R_UNLESS(!this->setup_update, ns::ResultCardUpdateAlreadySetup());
@@ -481,6 +484,7 @@ namespace ams::mitm::sysupdater {
}
/* Initialize the update task. */
handle_guard.Cancel();
R_TRY(InitializeUpdateTask(transfer_memory, transfer_memory_size, path, exfat, firmware_variation_id));
/* The update is now set up. */
@@ -488,10 +492,10 @@ namespace ams::mitm::sysupdater {
return ResultSuccess();
}
Result SystemUpdateService::InitializeUpdateTask(os::ManagedHandle &transfer_memory_handle, u64 transfer_memory_size, const ncm::Path &path, bool exfat, ncm::FirmwareVariationId firmware_variation_id) {
Result SystemUpdateService::InitializeUpdateTask(os::NativeHandle transfer_memory, u64 transfer_memory_size, const ncm::Path &path, bool exfat, ncm::FirmwareVariationId firmware_variation_id) {
/* Map the transfer memory. */
const size_t tmem_buffer_size = static_cast<size_t>(transfer_memory_size);
this->update_transfer_memory.emplace(tmem_buffer_size, transfer_memory_handle.Get(), true);
this->update_transfer_memory.emplace(tmem_buffer_size, transfer_memory, true);
void *tmem_buffer;
R_TRY(this->update_transfer_memory->Map(std::addressof(tmem_buffer), os::MemoryPermission_None));
@@ -500,9 +504,6 @@ namespace ams::mitm::sysupdater {
this->update_transfer_memory = util::nullopt;
};
/* Now that the memory is mapped, the input handle is managed and can be released. */
transfer_memory_handle.Detach();
/* Adjust the package root. */
ncm::Path package_root;
R_TRY(FormatUserPackagePath(std::addressof(package_root), path));

View File

@@ -64,8 +64,8 @@ namespace ams::mitm::sysupdater {
public:
constexpr SystemUpdateService() : apply_manager(), update_task(), update_transfer_memory(), setup_update(false), requested_update(false) { /* ... */ }
private:
Result SetupUpdateImpl(os::ManagedHandle transfer_memory, u64 transfer_memory_size, const ncm::Path &path, bool exfat, ncm::FirmwareVariationId firmware_variation_id);
Result InitializeUpdateTask(os::ManagedHandle &transfer_memory, u64 transfer_memory_size, const ncm::Path &path, bool exfat, ncm::FirmwareVariationId firmware_variation_id);
Result SetupUpdateImpl(os::NativeHandle transfer_memory, u64 transfer_memory_size, const ncm::Path &path, bool exfat, ncm::FirmwareVariationId firmware_variation_id);
Result InitializeUpdateTask(os::NativeHandle transfer_memory, u64 transfer_memory_size, const ncm::Path &path, bool exfat, ncm::FirmwareVariationId firmware_variation_id);
public:
Result GetUpdateInformation(sf::Out<UpdateInformation> out, const ncm::Path &path);
Result ValidateUpdate(sf::Out<Result> out_validate_result, sf::Out<Result> out_validate_exfat_result, sf::Out<UpdateValidationInfo> out_validate_info, const ncm::Path &path);

View File

@@ -219,7 +219,7 @@ namespace ams::dmnt::cheat::impl {
/* Note: This function *MUST* be called only with the cheat lock held. */
os::ProcessId pid;
bool has_cheat_process = this->cheat_process_debug_handle != svc::InvalidHandle;
has_cheat_process &= R_SUCCEEDED(os::TryGetProcessId(&pid, this->cheat_process_debug_handle));
has_cheat_process &= R_SUCCEEDED(os::GetProcessId(&pid, this->cheat_process_debug_handle));
has_cheat_process &= R_SUCCEEDED(pm::dmnt::GetApplicationProcessId(&pid));
has_cheat_process &= (pid == this->cheat_process_metadata.process_id);

View File

@@ -57,7 +57,9 @@ namespace ams::ldr {
/* Official commands. */
Result LoaderService::CreateProcess(sf::OutMoveHandle proc_h, PinId id, u32 flags, sf::CopyHandle reslimit_h) {
os::ManagedHandle reslimit_holder(reslimit_h.GetValue());
/* Ensure we close the input handle when we're done. */
ON_SCOPE_EXIT { os::CloseNativeHandle(reslimit_h.GetValue()); };
ncm::ProgramLocation loc;
cfg::OverrideStatus override_status;
char path[FS_MAX_PATH];
@@ -69,7 +71,7 @@ namespace ams::ldr {
R_TRY(ResolveContentPath(path, loc));
}
return ldr::CreateProcess(proc_h.GetHandlePointer(), id, loc, override_status, path, flags, reslimit_holder.Get());
return ldr::CreateProcess(proc_h.GetHandlePointer(), id, loc, override_status, path, flags, reslimit_h.GetValue());
}
Result LoaderService::GetProgramInfo(sf::Out<ProgramInfo> out, const ncm::ProgramLocation &loc) {

View File

@@ -70,7 +70,7 @@ namespace ams::ldr {
}
struct ProcessInfo {
os::ManagedHandle process_handle;
os::NativeHandle process_handle;
uintptr_t args_address;
size_t args_size;
uintptr_t nso_address[Nso_Count];
@@ -465,11 +465,12 @@ namespace ams::ldr {
R_TRY(DecideAddressSpaceLayout(out, std::addressof(param), nso_headers, has_nso, arg_info));
/* Actually create process. */
Handle process_handle;
svc::Handle process_handle;
R_TRY(svc::CreateProcess(std::addressof(process_handle), std::addressof(param), static_cast<const u32 *>(meta->aci_kac), meta->aci->kac_size / sizeof(u32)));
/* Set the output handle. */
*out->process_handle.GetPointer() = process_handle;
out->process_handle = process_handle;
return ResultSuccess();
}
@@ -554,8 +555,6 @@ namespace ams::ldr {
}
Result LoadNsosIntoProcessMemory(const ProcessInfo *process_info, const NsoHeader *nso_headers, const bool *has_nso, const args::ArgumentInfo *arg_info) {
const Handle process_handle = process_info->process_handle.Get();
/* Load each NSO. */
for (size_t i = 0; i < Nso_Count; i++) {
if (has_nso[i]) {
@@ -566,7 +565,7 @@ namespace ams::ldr {
uintptr_t map_address = 0;
R_TRY(map::LocateMappableSpace(&map_address, process_info->nso_size[i]));
R_TRY(LoadNsoIntoProcessMemory(process_handle, file, map_address, nso_headers + i, process_info->nso_address[i], process_info->nso_size[i]));
R_TRY(LoadNsoIntoProcessMemory(process_info->process_handle, file, map_address, nso_headers + i, process_info->nso_address[i], process_info->nso_size[i]));
}
}
@@ -577,7 +576,7 @@ namespace ams::ldr {
uintptr_t map_address = 0;
R_TRY(map::LocateMappableSpace(&map_address, process_info->args_size));
map::AutoCloseMap mapper(map_address, process_handle, process_info->args_address, process_info->args_size);
map::AutoCloseMap mapper(map_address, process_info->process_handle, process_info->args_address, process_info->args_size);
R_TRY(mapper.GetResult());
ProgramArguments *args = reinterpret_cast<ProgramArguments *>(map_address);
@@ -588,7 +587,7 @@ namespace ams::ldr {
}
/* Set argument region permissions. */
R_TRY(svcSetProcessMemoryPermission(process_handle, process_info->args_address, process_info->args_size, Perm_Rw));
R_TRY(svcSetProcessMemoryPermission(process_info->process_handle, process_info->args_address, process_info->args_size, Perm_Rw));
}
return ResultSuccess();
@@ -623,13 +622,16 @@ namespace ams::ldr {
ProcessInfo info;
R_TRY(CreateProcessImpl(&info, &meta, nso_headers, has_nso, arg_info, flags, reslimit_h));
/* Ensure we close the process handle, if we fail. */
ON_SCOPE_EXIT { os::CloseNativeHandle(info.process_handle); };
/* Load NSOs into process memory. */
R_TRY(LoadNsosIntoProcessMemory(&info, nso_headers, has_nso, arg_info));
/* Register NSOs with ro manager. */
{
/* Nintendo doesn't validate this get, but we do. */
os::ProcessId process_id = os::GetProcessId(info.process_handle.Get());
os::ProcessId process_id = os::GetProcessId(info.process_handle);
/* Register new process. */
ldr::ro::RegisterProcess(pin_id, process_id, loc.program_id);
@@ -655,7 +657,8 @@ namespace ams::ldr {
SetLaunchedBootProgram(loc.program_id);
/* Move the process handle to output. */
*out = info.process_handle.Move();
*out = info.process_handle;
info.process_handle = os::InvalidNativeHandle;
}
return ResultSuccess();

View File

@@ -24,7 +24,7 @@ namespace ams::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) {
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{});
u64 base_address;
@@ -67,7 +67,7 @@ namespace ams::ro::impl {
return ResultSuccess();
}
Result SetNroPerms(Handle process_handle, u64 base_address, u64 rx_size, u64 ro_size, u64 rw_size) {
Result SetNroPerms(os::NativeHandle process_handle, u64 base_address, u64 rx_size, u64 ro_size, u64 rw_size) {
const u64 rx_offset = 0;
const u64 ro_offset = rx_offset + rx_size;
const u64 rw_offset = ro_offset + ro_size;
@@ -79,7 +79,7 @@ namespace ams::ro::impl {
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) {
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(svcUnmapProcessCodeMemory(process_handle, base_address + code_size + rw_size, bss_heap_address, bss_heap_size));

View File

@@ -20,8 +20,8 @@
namespace ams::ro::impl {
/* Utilities for working with NROs. */
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);
Result SetNroPerms(Handle process_handle, u64 base_address, u64 rx_size, u64 ro_size, u64 rw_size);
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);
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);
Result SetNroPerms(os::NativeHandle process_handle, u64 base_address, u64 rx_size, u64 ro_size, u64 rw_size);
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);
}

View File

@@ -194,7 +194,7 @@ namespace ams::ro::impl {
}
/* Utilities for working with NRRs. */
Result MapAndValidateNrr(NrrHeader **out_header, u64 *out_mapped_code_address, void *out_hash, size_t out_hash_size, Handle process_handle, ncm::ProgramId program_id, u64 nrr_heap_address, u64 nrr_heap_size, NrrKind nrr_kind, bool enforce_nrr_kind) {
Result MapAndValidateNrr(NrrHeader **out_header, u64 *out_mapped_code_address, void *out_hash, size_t out_hash_size, os::NativeHandle process_handle, ncm::ProgramId program_id, u64 nrr_heap_address, u64 nrr_heap_size, NrrKind nrr_kind, bool enforce_nrr_kind) {
map::MappedCodeMemory nrr_mcm(ResultInternalError{});
/* First, map the NRR. */
@@ -223,7 +223,7 @@ namespace ams::ro::impl {
return ResultSuccess();
}
Result UnmapNrr(Handle process_handle, const NrrHeader *header, u64 nrr_heap_address, u64 nrr_heap_size, u64 mapped_code_address) {
Result UnmapNrr(os::NativeHandle 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();

View File

@@ -20,8 +20,8 @@
namespace ams::ro::impl {
/* Utilities for working with NRRs. */
Result MapAndValidateNrr(NrrHeader **out_header, u64 *out_mapped_code_address, void *out_hash, size_t out_hash_size, Handle process_handle, ncm::ProgramId program_id, u64 nrr_heap_address, u64 nrr_heap_size, NrrKind nrr_kind, bool enforce_nrr_kind);
Result UnmapNrr(Handle process_handle, const NrrHeader *header, u64 nrr_heap_address, u64 nrr_heap_size, u64 mapped_code_address);
Result MapAndValidateNrr(NrrHeader **out_header, u64 *out_mapped_code_address, void *out_hash, size_t out_hash_size, os::NativeHandle process_handle, ncm::ProgramId program_id, u64 nrr_heap_address, u64 nrr_heap_size, NrrKind nrr_kind, bool enforce_nrr_kind);
Result UnmapNrr(os::NativeHandle process_handle, const NrrHeader *header, u64 nrr_heap_address, u64 nrr_heap_size, u64 mapped_code_address);
bool ValidateNrrHashTableEntry(const void *signed_area, size_t signed_area_size, size_t hashes_offset, size_t num_hashes, const void *nrr_hash, const u8 *hash_table, const void *desired_hash);

View File

@@ -77,20 +77,17 @@ namespace ams::ro::impl {
bool nrr_in_use[MaxNrrInfos];
NroInfo nro_infos[MaxNroInfos];
NrrInfo nrr_infos[MaxNrrInfos];
Handle process_handle;
os::NativeHandle process_handle;
os::ProcessId process_id;
bool in_use;
ncm::ProgramId GetProgramId(Handle other_process_h) const {
ncm::ProgramId GetProgramId(os::NativeHandle other_process_h) const {
/* Automatically select a handle, allowing for override. */
Handle process_h = this->process_handle;
if (other_process_h != svc::InvalidHandle) {
process_h = other_process_h;
if (other_process_h != os::InvalidNativeHandle) {
return os::GetProgramId(other_process_h);
} else {
return os::GetProgramId(this->process_handle);
}
ncm::ProgramId program_id = ncm::InvalidProgramId;
R_ABORT_UNLESS(svc::GetInfo(std::addressof(program_id.value), svc::InfoType_ProgramId, process_h, 0));
return program_id;
}
Result GetNrrInfoByAddress(NrrInfo **out, u64 nrr_heap_address) {
@@ -296,7 +293,7 @@ namespace ams::ro::impl {
return nullptr;
}
size_t AllocateContext(Handle process_handle, os::ProcessId process_id) {
size_t AllocateContext(os::NativeHandle process_handle, os::ProcessId process_id) {
/* Find a free process context. */
for (size_t i = 0; i < MaxSessions; i++) {
ProcessContext *context = &g_process_contexts[i];
@@ -316,7 +313,7 @@ namespace ams::ro::impl {
void FreeContext(size_t context_id) {
ProcessContext *context = GetContextById(context_id);
if (context != nullptr) {
if (context->process_handle != INVALID_HANDLE) {
if (context->process_handle != os::InvalidNativeHandle) {
for (size_t i = 0; i < MaxNrrInfos; i++) {
if (context->nrr_in_use[i]) {
UnmapNrr(context->process_handle, context->nrr_infos[i].mapped_header, context->nrr_infos[i].nrr_heap_address, context->nrr_infos[i].nrr_heap_size, context->nrr_infos[i].mapped_code_address);
@@ -376,13 +373,16 @@ namespace ams::ro::impl {
}
/* Context utilities. */
Result RegisterProcess(size_t *out_context_id, os::ManagedHandle process_handle, os::ProcessId process_id) {
Result RegisterProcess(size_t *out_context_id, os::NativeHandle process_handle, os::ProcessId process_id) {
/* Ensure we manage process handle correctly. */
auto handle_guard = SCOPE_GUARD { os::CloseNativeHandle(process_handle); };
/* Validate process handle. */
{
os::ProcessId handle_pid = os::InvalidProcessId;
/* Validate handle is a valid process handle. */
R_UNLESS(R_SUCCEEDED(os::TryGetProcessId(&handle_pid, process_handle.Get())), ResultInvalidProcess());
R_UNLESS(R_SUCCEEDED(os::GetProcessId(&handle_pid, process_handle)), ResultInvalidProcess());
/* Validate process id. */
R_UNLESS(handle_pid == process_id, ResultInvalidProcess());
@@ -391,7 +391,10 @@ namespace ams::ro::impl {
/* Check if a process context already exists. */
R_UNLESS(GetContextByProcessId(process_id) == nullptr, ResultInvalidSession());
*out_context_id = AllocateContext(process_handle.Move(), process_id);
/* Allocate a context to manage the process handle. */
handle_guard.Cancel();
*out_context_id = AllocateContext(process_handle, process_id);
return ResultSuccess();
}
@@ -407,13 +410,16 @@ namespace ams::ro::impl {
}
/* Service implementations. */
Result RegisterModuleInfo(size_t context_id, os::ManagedHandle process_handle, u64 nrr_address, u64 nrr_size, NrrKind nrr_kind, bool enforce_nrr_kind) {
Result RegisterModuleInfo(size_t context_id, os::NativeHandle process_handle, u64 nrr_address, u64 nrr_size, NrrKind nrr_kind, bool enforce_nrr_kind) {
/* Ensure we close the process handle when we're done with it. */
ON_SCOPE_EXIT { os::CloseNativeHandle(process_handle); };
/* Get context. */
ProcessContext *context = GetContextById(context_id);
AMS_ABORT_UNLESS(context != nullptr);
/* Get program id. */
const ncm::ProgramId program_id = context->GetProgramId(process_handle.Get());
const ncm::ProgramId program_id = context->GetProgramId(process_handle);
/* Validate address/size. */
R_TRY(ValidateAddressAndNonZeroSize(nrr_address, nrr_size));

View File

@@ -30,12 +30,12 @@ namespace ams::ro::impl {
bool ShouldEaseNroRestriction();
/* Context utilities. */
Result RegisterProcess(size_t *out_context_id, os::ManagedHandle process_handle, os::ProcessId process_id);
Result RegisterProcess(size_t *out_context_id, os::NativeHandle process_handle, os::ProcessId process_id);
Result ValidateProcess(size_t context_id, os::ProcessId process_id);
void UnregisterProcess(size_t context_id);
/* Service implementations. */
Result RegisterModuleInfo(size_t context_id, os::ManagedHandle process_h, u64 nrr_address, u64 nrr_size, NrrKind nrr_kind, bool enforce_nrr_kind);
Result RegisterModuleInfo(size_t context_id, os::NativeHandle process_h, u64 nrr_address, u64 nrr_size, NrrKind nrr_kind, bool enforce_nrr_kind);
Result UnregisterModuleInfo(size_t context_id, u64 nrr_address);
Result MapManualLoadModuleMemory(u64 *out_address, size_t context_id, u64 nro_address, u64 nro_size, u64 bss_address, u64 bss_size);
Result UnmapManualLoadModuleMemory(size_t context_id, u64 nro_address);

View File

@@ -47,7 +47,7 @@ namespace ams::ro {
Result RoService::RegisterModuleInfo(const sf::ClientProcessId &client_pid, u64 nrr_address, u64 nrr_size) {
R_TRY(impl::ValidateProcess(this->context_id, client_pid.GetValue()));
return impl::RegisterModuleInfo(this->context_id, svc::InvalidHandle, nrr_address, nrr_size, NrrKind_User, true);
return impl::RegisterModuleInfo(this->context_id, os::InvalidNativeHandle, nrr_address, nrr_size, NrrKind_User, true);
}
Result RoService::UnregisterModuleInfo(const sf::ClientProcessId &client_pid, u64 nrr_address) {
@@ -56,20 +56,20 @@ namespace ams::ro {
}
Result RoService::RegisterProcessHandle(const sf::ClientProcessId &client_pid, sf::CopyHandle process_h) {
/* Ensure we manage references to the process handle correctly. */
os::ManagedHandle process_handle(process_h.GetValue());
/* Register the process. */
return impl::RegisterProcess(std::addressof(this->context_id), std::move(process_handle), client_pid.GetValue());
return impl::RegisterProcess(std::addressof(this->context_id), process_h.GetValue(), client_pid.GetValue());
}
Result RoService::RegisterProcessModuleInfo(const sf::ClientProcessId &client_pid, u64 nrr_address, u64 nrr_size, sf::CopyHandle process_h) {
/* Ensure we manage references to the process handle correctly. */
os::ManagedHandle process_handle(process_h.GetValue());
/* Validate the process, ensuring we manage the process handle correctly. */
{
auto handle_guard = SCOPE_GUARD { os::CloseNativeHandle(process_h.GetValue()); };
R_TRY(impl::ValidateProcess(this->context_id, client_pid.GetValue()));
handle_guard.Cancel();
}
/* Register the module. */
R_TRY(impl::ValidateProcess(this->context_id, client_pid.GetValue()));
return impl::RegisterModuleInfo(this->context_id, std::move(process_handle), nrr_address, nrr_size, this->nrr_kind, this->nrr_kind == NrrKind_JitPlugin);
return impl::RegisterModuleInfo(this->context_id, process_h.GetValue(), nrr_address, nrr_size, this->nrr_kind, this->nrr_kind == NrrKind_JitPlugin);
}
}