strat: prefer os::NativeHandle to Handle/svc::Handle where sensible

This commit is contained in:
Michael Scire
2021-10-04 17:12:32 -07:00
parent 597d521f52
commit d0041a33ac
50 changed files with 193 additions and 190 deletions

View File

@@ -40,7 +40,7 @@ namespace ams::dmnt {
ContinueMode_Step,
};
private:
svc::Handle m_debug_handle{svc::InvalidHandle};
os::NativeHandle m_debug_handle{os::InvalidNativeHandle};
s32 m_thread_count{0};
bool m_is_valid{false};
bool m_is_64_bit{false};
@@ -73,7 +73,7 @@ namespace ams::dmnt {
}
~DebugProcess() { this->Detach(); }
svc::Handle GetHandle() const { return m_debug_handle; }
os::NativeHandle GetHandle() const { return m_debug_handle; }
bool IsValid() const { return m_is_valid; }
bool Is64Bit() const { return m_is_64_bit; }
bool Is64BitAddressSpace() const { return m_is_64_bit_address_space; }

View File

@@ -88,7 +88,7 @@ namespace ams::dmnt::cheat::impl {
os::Event debug_events_event; /* Autoclear. */
os::ThreadType detect_thread, debug_events_thread;
os::SystemEvent cheat_process_event;
Handle cheat_process_debug_handle = svc::InvalidHandle;
os::NativeHandle cheat_process_debug_handle = os::InvalidNativeHandle;
CheatProcessMetadata cheat_process_metadata = {};
os::ThreadType vm_thread;
@@ -175,7 +175,7 @@ namespace ams::dmnt::cheat::impl {
}
void CloseActiveCheatProcess() {
if (this->cheat_process_debug_handle != svc::InvalidHandle) {
if (this->cheat_process_debug_handle != os::InvalidNativeHandle) {
/* We don't need to do any unsafe brekaing. */
this->broken_unsafe = false;
this->unsafe_break_event.Signal();
@@ -185,7 +185,7 @@ namespace ams::dmnt::cheat::impl {
/* Close resources. */
R_ABORT_UNLESS(svc::CloseHandle(this->cheat_process_debug_handle));
this->cheat_process_debug_handle = svc::InvalidHandle;
this->cheat_process_debug_handle = os::InvalidNativeHandle;
/* Save cheat toggles. */
if (this->always_save_cheat_toggles || this->should_save_cheat_toggles) {
@@ -218,7 +218,7 @@ namespace ams::dmnt::cheat::impl {
bool HasActiveCheatProcess() {
/* 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;
bool has_cheat_process = this->cheat_process_debug_handle != os::InvalidNativeHandle;
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);
@@ -235,12 +235,12 @@ namespace ams::dmnt::cheat::impl {
return ResultSuccess();
}
Handle GetCheatProcessHandle() const {
os::NativeHandle GetCheatProcessHandle() const {
return this->cheat_process_debug_handle;
}
Handle HookToCreateApplicationProcess() const {
Handle h = svc::InvalidHandle;
os::NativeHandle HookToCreateApplicationProcess() const {
os::NativeHandle h;
R_ABORT_UNLESS(pm::dmnt::HookToCreateApplicationProcess(&h));
return h;
}
@@ -284,7 +284,7 @@ namespace ams::dmnt::cheat::impl {
return this->HasActiveCheatProcess();
}
Handle GetCheatProcessEventHandle() const {
os::NativeHandle GetCheatProcessEventHandle() const {
return this->cheat_process_event.GetReadableHandle();
}
@@ -704,7 +704,7 @@ namespace ams::dmnt::cheat::impl {
this_ptr->cheat_lock.Unlock();
this_ptr->unsafe_break_event.Wait();
this_ptr->cheat_lock.Lock();
if (this_ptr->GetCheatProcessHandle() != svc::InvalidHandle) {
if (this_ptr->GetCheatProcessHandle() != os::InvalidNativeHandle) {
continue;
} else {
break;
@@ -1193,7 +1193,7 @@ namespace ams::dmnt::cheat::impl {
return GetReference(g_cheat_process_manager).GetHasActiveCheatProcess();
}
Handle GetCheatProcessEventHandle() {
os::NativeHandle GetCheatProcessEventHandle() {
return GetReference(g_cheat_process_manager).GetCheatProcessEventHandle();
}

View File

@@ -21,7 +21,7 @@ namespace ams::dmnt::cheat::impl {
void InitializeCheatManager();
bool GetHasActiveCheatProcess();
Handle GetCheatProcessEventHandle();
os::NativeHandle GetCheatProcessEventHandle();
Result GetCheatProcessMetadata(CheatProcessMetadata *out);
Result ForceOpenCheatProcess();
Result PauseCheatProcess();

View File

@@ -126,7 +126,7 @@ namespace ams::dmnt::cheat::impl {
}
}
Result ContinueCheatProcess(Handle cheat_dbg_hnd) {
Result ContinueCheatProcess(os::NativeHandle cheat_dbg_hnd) {
/* Loop getting all debug events. */
svc::DebugEventInfo d;
size_t target_core = NumCores - 1;
@@ -151,7 +151,7 @@ namespace ams::dmnt::cheat::impl {
util::ConstructAt(g_events_manager);
}
Result ContinueCheatProcess(Handle cheat_dbg_hnd) {
Result ContinueCheatProcess(os::NativeHandle cheat_dbg_hnd) {
return GetReference(g_events_manager).ContinueCheatProcess(cheat_dbg_hnd);
}

View File

@@ -20,6 +20,6 @@ namespace ams::dmnt::cheat::impl {
void InitializeDebugEventsManager();
Result ContinueCheatProcess(Handle cheat_dbg_hnd);
Result ContinueCheatProcess(os::NativeHandle cheat_dbg_hnd);
}

View File

@@ -24,7 +24,7 @@ namespace ams::fatal::srv {
FatalConfig g_config;
/* Event creator. */
Handle GetFatalDirtyEventReadableHandle() {
os::NativeHandle GetFatalDirtyEventReadableHandle() {
Event evt;
R_ABORT_UNLESS(setsysAcquireFatalDirtyFlagEventHandle(&evt));
return evt.revent;

View File

@@ -37,7 +37,7 @@ namespace ams::pm::resource {
/* Globals. */
constinit os::SdkMutex g_resource_limit_lock;
constinit Handle g_resource_limit_handles[ResourceLimitGroup_Count];
constinit os::NativeHandle g_resource_limit_handles[ResourceLimitGroup_Count];
constinit spl::MemoryArrangement g_memory_arrangement = spl::MemoryArrangement_Standard;
constinit u64 g_system_memory_boost_size = 0;
constinit u64 g_extra_application_threads_available = 0;
@@ -136,7 +136,7 @@ namespace ams::pm::resource {
}
void WaitResourceAvailable(ResourceLimitGroup group) {
const Handle reslimit_hnd = GetResourceLimitHandle(group);
const auto reslimit_hnd = GetResourceLimitHandle(group);
for (size_t i = 0; i < svc::LimitableResource_Count; i++) {
const auto resource = LimitableResources[i];
@@ -336,11 +336,11 @@ namespace ams::pm::resource {
return ResultSuccess();
}
Handle GetResourceLimitHandle(ResourceLimitGroup group) {
os::NativeHandle GetResourceLimitHandle(ResourceLimitGroup group) {
return g_resource_limit_handles[group];
}
Handle GetResourceLimitHandle(const ldr::ProgramInfo *info) {
os::NativeHandle GetResourceLimitHandle(const ldr::ProgramInfo *info) {
return GetResourceLimitHandle(GetResourceLimitGroup(info));
}
@@ -358,7 +358,7 @@ namespace ams::pm::resource {
AMS_ABORT_UNLESS(group < ResourceLimitGroup_Count);
AMS_ABORT_UNLESS(resource < svc::LimitableResource_Count);
const Handle reslimit_hnd = GetResourceLimitHandle(group);
const auto reslimit_hnd = GetResourceLimitHandle(group);
R_TRY(svc::GetResourceLimitCurrentValue(out_cur, reslimit_hnd, resource));
R_TRY(svc::GetResourceLimitLimitValue(out_lim, reslimit_hnd, resource));

View File

@@ -22,9 +22,11 @@ namespace ams::pm::resource {
Result InitializeResourceManager();
Result BoostSystemMemoryResourceLimit(u64 boost_size);
Result BoostApplicationThreadResourceLimit();
Handle GetResourceLimitHandle(ResourceLimitGroup group);
Handle GetResourceLimitHandle(const ldr::ProgramInfo *info);
void WaitResourceAvailable(const ldr::ProgramInfo *info);
os::NativeHandle GetResourceLimitHandle(ResourceLimitGroup group);
os::NativeHandle GetResourceLimitHandle(const ldr::ProgramInfo *info);
void WaitResourceAvailable(const ldr::ProgramInfo *info);
Result GetResourceLimitValues(s64 *out_cur, s64 *out_lim, ResourceLimitGroup group, svc::LimitableResource resource);

View File

@@ -43,7 +43,8 @@ namespace ams::pm {
}
Result InformationService::AtmosphereGetProcessInfo(sf::Out<ncm::ProgramLocation> out_loc, sf::Out<cfg::OverrideStatus> out_status, os::ProcessId process_id) {
Handle dummy_handle;
/* NOTE: We don't need to worry about closing this handle, because it's an in-process copy, not a newly allocated handle. */
os::NativeHandle dummy_handle;
return impl::AtmosphereGetProcessInfo(&dummy_handle, out_loc.GetPointer(), out_status.GetPointer(), process_id);
}

View File

@@ -53,10 +53,10 @@ namespace ams::sm::impl {
os::ProcessId owner_process_id;
os::ProcessId mitm_process_id;
os::ProcessId mitm_waiting_ack_process_id;
svc::Handle mitm_port_h;
svc::Handle mitm_query_h;
svc::Handle port_h;
svc::Handle mitm_fwd_sess_h;
os::NativeHandle mitm_port_h;
os::NativeHandle mitm_query_h;
os::NativeHandle port_h;
os::NativeHandle mitm_fwd_sess_h;
s32 max_sessions;
bool is_light;
bool mitm_waiting_ack;
@@ -67,10 +67,10 @@ namespace ams::sm::impl {
.owner_process_id = os::InvalidProcessId,
.mitm_process_id = os::InvalidProcessId,
.mitm_waiting_ack_process_id = os::InvalidProcessId,
.mitm_port_h = svc::InvalidHandle,
.mitm_query_h = svc::InvalidHandle,
.port_h = svc::InvalidHandle,
.mitm_fwd_sess_h = svc::InvalidHandle,
.mitm_port_h = os::InvalidNativeHandle,
.mitm_query_h = os::InvalidNativeHandle,
.port_h = os::InvalidNativeHandle,
.mitm_fwd_sess_h = os::InvalidNativeHandle,
.max_sessions = 0,
.is_light = false,
.mitm_waiting_ack = false,
@@ -361,7 +361,7 @@ namespace ams::sm::impl {
program_id == ncm::SystemProgramId::Creport;
}
Result GetMitmServiceHandleImpl(svc::Handle *out, ServiceInfo *service_info, const MitmProcessInfo &client_info) {
Result GetMitmServiceHandleImpl(os::NativeHandle *out, ServiceInfo *service_info, const MitmProcessInfo &client_info) {
/* Send command to query if we should mitm. */
bool should_mitm;
{
@@ -376,15 +376,15 @@ namespace ams::sm::impl {
/* Create both handles. */
{
/* Get the forward handle. */
svc::Handle fwd_hnd;
os::NativeHandle fwd_hnd;
R_TRY(svc::ConnectToPort(std::addressof(fwd_hnd), service_info->port_h));
/* Ensure that the forward handle is closed, if we fail to get the mitm handle. */
auto fwd_guard = SCOPE_GUARD { R_ABORT_UNLESS(svc::CloseHandle(fwd_hnd)); };
auto fwd_guard = SCOPE_GUARD { os::CloseNativeHandle(fwd_hnd); };
/* Get the mitm handle. */
/* This should be guaranteed to succeed, since we got a forward handle. */
svc::Handle hnd;
os::NativeHandle hnd;
R_ABORT_UNLESS(svc::ConnectToPort(std::addressof(hnd), service_info->mitm_port_h));
/* We got both handles, so we no longer need to clean up the forward handle. */
@@ -401,9 +401,9 @@ namespace ams::sm::impl {
return ResultSuccess();
}
Result GetServiceHandleImpl(svc::Handle *out, ServiceInfo *service_info, os::ProcessId process_id) {
Result GetServiceHandleImpl(os::NativeHandle *out, ServiceInfo *service_info, os::ProcessId process_id) {
/* Clear handle output. */
*out = svc::InvalidHandle;
*out = os::InvalidNativeHandle;
/* Check if we should return a mitm handle. */
if (IsValidProcessId(service_info->mitm_process_id) && service_info->mitm_process_id != process_id) {
@@ -420,7 +420,7 @@ namespace ams::sm::impl {
return svc::ConnectToPort(out, service_info->port_h);
}
Result RegisterServiceImpl(svc::Handle *out, os::ProcessId process_id, ServiceName service, size_t max_sessions, bool is_light) {
Result RegisterServiceImpl(os::NativeHandle *out, os::ProcessId process_id, ServiceName service, size_t max_sessions, bool is_light) {
/* Validate service name. */
R_TRY(ValidateServiceName(service));
@@ -432,8 +432,8 @@ namespace ams::sm::impl {
R_UNLESS(free_service != nullptr, sm::ResultOutOfServices());
/* Create the new service. */
*out = svc::InvalidHandle;
svc::Handle server_hnd = svc::InvalidHandle;
*out = os::InvalidNativeHandle;
os::NativeHandle server_hnd = os::InvalidNativeHandle;
R_TRY(svc::CreatePort(out, std::addressof(server_hnd), max_sessions, is_light, reinterpret_cast<uintptr_t>(free_service->name.name)));
/* Save info. */
@@ -451,10 +451,10 @@ namespace ams::sm::impl {
void UnregisterServiceImpl(ServiceInfo *service_info) {
/* Close all valid handles. */
if (service_info->port_h != svc::InvalidHandle) { R_ABORT_UNLESS(svc::CloseHandle(service_info->port_h)); }
if (service_info->mitm_port_h != svc::InvalidHandle) { R_ABORT_UNLESS(svc::CloseHandle(service_info->mitm_port_h)); }
if (service_info->mitm_query_h != svc::InvalidHandle) { R_ABORT_UNLESS(svc::CloseHandle(service_info->mitm_query_h)); }
if (service_info->mitm_fwd_sess_h != svc::InvalidHandle) { R_ABORT_UNLESS(svc::CloseHandle(service_info->mitm_fwd_sess_h)); }
os::CloseNativeHandle(service_info->port_h);
os::CloseNativeHandle(service_info->mitm_port_h);
os::CloseNativeHandle(service_info->mitm_query_h);
os::CloseNativeHandle(service_info->mitm_fwd_sess_h);
/* Reset the info's state. */
*service_info = InvalidServiceInfo;
@@ -547,7 +547,7 @@ namespace ams::sm::impl {
return StartRegisterRetry(service);
}
Result GetServiceHandle(svc::Handle *out, os::ProcessId process_id, ServiceName service) {
Result GetServiceHandle(os::NativeHandle *out, os::ProcessId process_id, ServiceName service) {
/* Acquire exclusive access to global state. */
std::scoped_lock lk(g_mutex);
@@ -583,7 +583,7 @@ namespace ams::sm::impl {
return ResultSuccess();
}
Result RegisterService(svc::Handle *out, os::ProcessId process_id, ServiceName service, size_t max_sessions, bool is_light) {
Result RegisterService(os::NativeHandle *out, os::ProcessId process_id, ServiceName service, size_t max_sessions, bool is_light) {
/* Acquire exclusive access to global state. */
std::scoped_lock lk(g_mutex);
@@ -604,7 +604,7 @@ namespace ams::sm::impl {
return RegisterServiceImpl(out, process_id, service, max_sessions, is_light);
}
Result RegisterServiceForSelf(svc::Handle *out, ServiceName service, size_t max_sessions) {
Result RegisterServiceForSelf(os::NativeHandle *out, ServiceName service, size_t max_sessions) {
/* Acquire exclusive access to global state. */
std::scoped_lock lk(g_mutex);
@@ -665,7 +665,7 @@ namespace ams::sm::impl {
return StartRegisterRetry(service);
}
Result InstallMitm(svc::Handle *out, svc::Handle *out_query, os::ProcessId process_id, ServiceName service) {
Result InstallMitm(os::NativeHandle *out, os::NativeHandle *out_query, os::ProcessId process_id, ServiceName service) {
/* Acquire exclusive access to global state. */
std::scoped_lock lk(g_mutex);
@@ -689,8 +689,8 @@ namespace ams::sm::impl {
R_UNLESS(!IsValidProcessId(service_info->mitm_process_id), sm::ResultAlreadyRegistered());
/* Always clear output. */
*out = svc::InvalidHandle;
*out_query = svc::InvalidHandle;
*out = os::InvalidNativeHandle;
*out_query = os::InvalidNativeHandle;
/* If we don't have a future mitm declaration, add one. */
/* Client will clear this when ready to process. */
@@ -704,14 +704,14 @@ namespace ams::sm::impl {
/* Create mitm handles. */
{
/* Get the port handles. */
svc::Handle hnd, port_hnd;
os::NativeHandle hnd, port_hnd;
R_TRY(svc::CreatePort(std::addressof(hnd), std::addressof(port_hnd), service_info->max_sessions, service_info->is_light, reinterpret_cast<uintptr_t>(service_info->name.name)));
/* Ensure that we clean up the port handles, if something goes wrong creating the query sessions. */
auto port_guard = SCOPE_GUARD { R_ABORT_UNLESS(svc::CloseHandle(hnd)); R_ABORT_UNLESS(svc::CloseHandle(port_hnd)); };
auto port_guard = SCOPE_GUARD { os::CloseNativeHandle(hnd); os::CloseNativeHandle(port_hnd); };
/* Create the session for our query service. */
svc::Handle qry_hnd, mitm_qry_hnd;
os::NativeHandle qry_hnd, mitm_qry_hnd;
R_TRY(svc::CreateSession(std::addressof(qry_hnd), std::addressof(mitm_qry_hnd), false, 0));
/* We created the query service session, so we no longer need to clean up the port handles. */
@@ -755,12 +755,12 @@ namespace ams::sm::impl {
/* Uninstall the mitm. */
{
/* Close mitm handles. */
R_ABORT_UNLESS(svc::CloseHandle(service_info->mitm_port_h));
R_ABORT_UNLESS(svc::CloseHandle(service_info->mitm_query_h));
os::CloseNativeHandle(service_info->mitm_port_h);
os::CloseNativeHandle(service_info->mitm_query_h);
/* Reset mitm members. */
service_info->mitm_port_h = svc::InvalidHandle;
service_info->mitm_query_h = svc::InvalidHandle;
service_info->mitm_port_h = os::InvalidNativeHandle;
service_info->mitm_query_h = os::InvalidNativeHandle;
service_info->mitm_process_id = os::InvalidProcessId;
}
@@ -824,7 +824,7 @@ namespace ams::sm::impl {
return ResultSuccess();
}
Result AcknowledgeMitmSession(MitmProcessInfo *out_info, svc::Handle *out_hnd, os::ProcessId process_id, ServiceName service) {
Result AcknowledgeMitmSession(MitmProcessInfo *out_info, os::NativeHandle *out_hnd, os::ProcessId process_id, ServiceName service) {
/* Acquire exclusive access to global state. */
std::scoped_lock lk(g_mutex);
@@ -852,7 +852,7 @@ namespace ams::sm::impl {
/* Set the output handle. */
*out_hnd = service_info->mitm_fwd_sess_h;
service_info->mitm_fwd_sess_h = svc::InvalidHandle;
service_info->mitm_fwd_sess_h = os::InvalidNativeHandle;
/* Clear acknowledgement-related fields. */
service_info->mitm_waiting_ack = false;

View File

@@ -29,19 +29,19 @@ namespace ams::sm::impl {
/* Service management. */
Result HasService(bool *out, ServiceName service);
Result WaitService(ServiceName service);
Result GetServiceHandle(svc::Handle *out, os::ProcessId process_id, ServiceName service);
Result RegisterService(svc::Handle *out, os::ProcessId process_id, ServiceName service, size_t max_sessions, bool is_light);
Result RegisterServiceForSelf(svc::Handle *out, ServiceName service, size_t max_sessions);
Result GetServiceHandle(os::NativeHandle *out, os::ProcessId process_id, ServiceName service);
Result RegisterService(os::NativeHandle *out, os::ProcessId process_id, ServiceName service, size_t max_sessions, bool is_light);
Result RegisterServiceForSelf(os::NativeHandle *out, ServiceName service, size_t max_sessions);
Result UnregisterService(os::ProcessId process_id, ServiceName service);
/* Mitm extensions. */
Result HasMitm(bool *out, ServiceName service);
Result WaitMitm(ServiceName service);
Result InstallMitm(svc::Handle *out, svc::Handle *out_query, os::ProcessId process_id, ServiceName service);
Result InstallMitm(os::NativeHandle *out, os::NativeHandle *out_query, os::ProcessId process_id, ServiceName service);
Result UninstallMitm(os::ProcessId process_id, ServiceName service);
Result DeclareFutureMitm(os::ProcessId process_id, ServiceName service);
Result ClearFutureMitm(os::ProcessId process_id, ServiceName service);
Result AcknowledgeMitmSession(MitmProcessInfo *out_info, svc::Handle *out_hnd, os::ProcessId process_id, ServiceName service);
Result AcknowledgeMitmSession(MitmProcessInfo *out_info, os::NativeHandle *out_hnd, os::ProcessId process_id, ServiceName service);
/* Deferral extension (works around FS bug). */
Result EndInitialDefers();

View File

@@ -143,7 +143,7 @@ namespace ams::sm {
g_server_manager.Initialize();
/* Create the handles for our ports. */
svc::Handle user_port_handle = svc::InvalidHandle, manager_port_handle = svc::InvalidHandle;
os::NativeHandle user_port_handle, manager_port_handle;
{
/* Create the user port handle. */
R_ABORT_UNLESS(svc::ManageNamedPort(std::addressof(user_port_handle), "sm:", MaxSessionsUser));

View File

@@ -222,13 +222,13 @@ namespace ams::spl::impl {
class DeviceAddressSpaceMapHelper {
private:
Handle das_hnd;
os::NativeHandle das_hnd;
u64 dst_addr;
u64 src_addr;
size_t size;
svc::MemoryPermission perm;
public:
DeviceAddressSpaceMapHelper(Handle h, u64 dst, u64 src, size_t sz, svc::MemoryPermission p) : das_hnd(h), dst_addr(dst), src_addr(src), size(sz), perm(p) {
DeviceAddressSpaceMapHelper(os::NativeHandle h, u64 dst, u64 src, size_t sz, svc::MemoryPermission p) : das_hnd(h), dst_addr(dst), src_addr(src), size(sz), perm(p) {
R_ABORT_UNLESS(svc::MapDeviceAddressSpaceAligned(this->das_hnd, dd::GetCurrentProcessHandle(), this->src_addr, this->size, this->dst_addr, this->perm));
}
~DeviceAddressSpaceMapHelper() {
@@ -241,7 +241,7 @@ namespace ams::spl::impl {
constinit os::InterruptEventType g_se_event;
constinit os::SystemEventType g_se_keyslot_available_event;
constinit Handle g_se_das_hnd;
constinit os::NativeHandle g_se_das_hnd = os::InvalidNativeHandle;
constinit u32 g_se_mapped_work_buffer_addr;
alignas(os::MemoryPageSize) constinit u8 g_work_buffer[2 * WorkBufferSizeMax];
@@ -937,7 +937,7 @@ namespace ams::spl::impl {
return ResultSuccess();
}
Handle GetAesKeySlotAvailableEventHandle() {
os::NativeHandle GetAesKeySlotAvailableEventHandle() {
return os::GetReadableHandleOfSystemEvent(std::addressof(g_se_keyslot_available_event));
}

View File

@@ -68,6 +68,6 @@ namespace ams::spl::impl {
/* Helper. */
Result DeallocateAllAesKeySlots(const void *owner);
Handle GetAesKeySlotAvailableEventHandle();
os::NativeHandle GetAesKeySlotAvailableEventHandle();
}