From 8abee1bdaa0095a23a36dbdd55c9a256e0d96d87 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Mon, 16 Sep 2019 02:14:23 -0700 Subject: [PATCH] dmnt: fix some bugs in init --- stratosphere/dmnt/dmnt.json | 1 + .../dmnt/source/cheat/impl/dmnt_cheat_api.cpp | 8 +++++--- .../impl/dmnt_cheat_debug_events_manager.cpp | 19 +++++++++++-------- stratosphere/dmnt/source/dmnt_main.cpp | 4 +++- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/stratosphere/dmnt/dmnt.json b/stratosphere/dmnt/dmnt.json index 2ddf2cec8..52c1533c2 100644 --- a/stratosphere/dmnt/dmnt.json +++ b/stratosphere/dmnt/dmnt.json @@ -16,6 +16,7 @@ }, "service_access": [ "pm:dmnt", + "pm:info", "ldr:dmnt", "ro:dmnt", "ns:dev", diff --git a/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_api.cpp b/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_api.cpp index c315b8dec..82b7ea86c 100644 --- a/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_api.cpp +++ b/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_api.cpp @@ -116,6 +116,9 @@ namespace sts::dmnt::cheat::impl { void CloseActiveCheatProcess() { if (this->cheat_process_debug_handle != INVALID_HANDLE) { + /* Knock out the debug events thread. */ + R_ASSERT(this->debug_events_thread.CancelSynchronization()); + /* Close resources. */ R_ASSERT(svcCloseHandle(this->cheat_process_debug_handle)); this->cheat_process_debug_handle = INVALID_HANDLE; @@ -146,6 +149,8 @@ namespace sts::dmnt::cheat::impl { u64 tmp; bool has_cheat_process = this->cheat_process_debug_handle != INVALID_HANDLE; has_cheat_process &= R_SUCCEEDED(svcGetProcessId(&tmp, this->cheat_process_debug_handle)); + has_cheat_process &= R_SUCCEEDED(pm::dmnt::GetApplicationProcessId(&tmp)); + has_cheat_process &= (tmp == this->cheat_process_metadata.process_id); if (!has_cheat_process) { this->CloseActiveCheatProcess(); @@ -610,9 +615,6 @@ namespace sts::dmnt::cheat::impl { this->CloseActiveCheatProcess(); } - /* Knock out the debug events thread. */ - R_ASSERT(this->debug_events_thread.CancelSynchronization()); - /* Get the application process's ID. */ R_ASSERT_IF_NEW_PROCESS(pm::dmnt::GetApplicationProcessId(&this->cheat_process_metadata.process_id)); auto proc_guard = SCOPE_GUARD { diff --git a/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_debug_events_manager.cpp b/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_debug_events_manager.cpp index bf490010f..d7a2107e3 100644 --- a/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_debug_events_manager.cpp +++ b/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_debug_events_manager.cpp @@ -33,7 +33,7 @@ namespace sts::dmnt::cheat::impl { static void PerCoreThreadFunction(void *_this) { /* This thread will wait on the appropriate message queue. */ DebugEventsManager *this_ptr = reinterpret_cast(_this); - const u32 current_core = svcGetCurrentProcessorNumber(); + const size_t current_core = svcGetCurrentProcessorNumber(); while (true) { /* Receive handle. */ Handle debug_handle = this_ptr->WaitReceiveHandle(current_core); @@ -46,9 +46,9 @@ namespace sts::dmnt::cheat::impl { } } - u32 GetTargetCore(const svc::DebugEventInfo &dbg_event, Handle debug_handle) { + size_t GetTargetCore(const svc::DebugEventInfo &dbg_event, Handle debug_handle) { /* If we don't need to continue on a specific core, use the system core. */ - u32 target_core = NumCores - 1; + size_t target_core = NumCores - 1; /* Retrieve correct core for new thread event. */ if (dbg_event.type == svc::DebugEventType::AttachThread) { @@ -61,11 +61,11 @@ namespace sts::dmnt::cheat::impl { return target_core; } - void SendHandle(const svc::DebugEventInfo &dbg_event, Handle debug_handle) { - this->message_queues[GetTargetCore(dbg_event, debug_handle)].Send(static_cast(debug_handle)); + void SendHandle(size_t target_core, Handle debug_handle) { + this->message_queues[target_core].Send(static_cast(debug_handle)); } - Handle WaitReceiveHandle(u32 core_id) { + Handle WaitReceiveHandle(size_t core_id) { uintptr_t x = 0; this->message_queues[core_id].Receive(&x); return static_cast(x); @@ -105,12 +105,15 @@ namespace sts::dmnt::cheat::impl { void ContinueCheatProcess(Handle cheat_dbg_hnd) { /* Loop getting all debug events. */ svc::DebugEventInfo d; + size_t target_core = NumCores - 1; while (R_SUCCEEDED(svcGetDebugEvent(reinterpret_cast(&d), cheat_dbg_hnd))) { - /* ... */ + if (d.type == svc::DebugEventType::AttachThread) { + target_core = GetTargetCore(d, cheat_dbg_hnd); + } } /* Send handle to correct core, wait for continue to finish. */ - this->SendHandle(d, cheat_dbg_hnd); + this->SendHandle(target_core, cheat_dbg_hnd); this->WaitContinued(); } }; diff --git a/stratosphere/dmnt/source/dmnt_main.cpp b/stratosphere/dmnt/source/dmnt_main.cpp index ad1cfff66..8b39a1789 100644 --- a/stratosphere/dmnt/source/dmnt_main.cpp +++ b/stratosphere/dmnt/source/dmnt_main.cpp @@ -31,7 +31,7 @@ extern "C" { u32 __nx_applet_type = AppletType_None; - #define INNER_HEAP_SIZE 0x80000 + #define INNER_HEAP_SIZE 0xC0000 size_t nx_inner_heap_size = INNER_HEAP_SIZE; char nx_inner_heap[INNER_HEAP_SIZE]; @@ -60,6 +60,7 @@ void __appInit(void) { DoWithSmSession([&]() { R_ASSERT(pmdmntInitialize()); + R_ASSERT(pminfoInitialize()); R_ASSERT(ldrDmntInitialize()); /* TODO: We provide this on every sysver via ro. Do we need a shim? */ if (GetRuntimeFirmwareVersion() >= FirmwareVersion_300) { @@ -89,6 +90,7 @@ void __appExit(void) { nsdevExit(); roDmntExit(); ldrDmntExit(); + pminfoExit(); pmdmntExit(); }