diff --git a/stratosphere/dmnt/source/dmnt_cheat_manager.cpp b/stratosphere/dmnt/source/dmnt_cheat_manager.cpp index 14077a702..2f74cb291 100644 --- a/stratosphere/dmnt/source/dmnt_cheat_manager.cpp +++ b/stratosphere/dmnt/source/dmnt_cheat_manager.cpp @@ -139,26 +139,24 @@ Result DmntCheatManager::ReadCheatProcessMemoryForVm(u64 proc_addr, void *out_da Result DmntCheatManager::WriteCheatProcessMemoryForVm(u64 proc_addr, const void *data, size_t size) { if (HasActiveCheatProcess()) { - Result rc = svcWriteDebugProcessMemory(g_cheat_process_debug_hnd, data, proc_addr, size); + R_TRY(svcWriteDebugProcessMemory(g_cheat_process_debug_hnd, data, proc_addr, size)); /* We might have a frozen address. Update it if we do! */ - if (R_SUCCEEDED(rc)) { - for (auto & [address, value] : g_frozen_addresses_map) { - /* Map is in order, so break here. */ - if (address >= proc_addr + size) { - break; - } + for (auto & [address, value] : g_frozen_addresses_map) { + /* Map is in order, so break here. */ + if (address >= proc_addr + size) { + break; + } - /* Check if we need to write. */ - if (proc_addr <= address) { - const size_t offset = (address - proc_addr); - const size_t size_to_copy = size - offset; - memcpy(&value.value, (void *)((uintptr_t)data + offset), size_to_copy < sizeof(value.value) ? size_to_copy : sizeof(value.value)); - } + /* Check if we need to write. */ + if (proc_addr <= address) { + const size_t offset = (address - proc_addr); + const size_t size_to_copy = size - offset; + memcpy(&value.value, (void *)((uintptr_t)data + offset), size_to_copy < sizeof(value.value) ? size_to_copy : sizeof(value.value)); } } - return rc; + return ResultSuccess; } return ResultDmntCheatNotAttached; @@ -775,12 +773,9 @@ Result DmntCheatManager::EnableFrozenAddress(u64 *out_value, u64 address, u64 wi return ResultDmntCheatAddressAlreadyFrozen; } - Result rc; FrozenAddressValue value = {0}; value.width = width; - if (R_FAILED((rc = ReadCheatProcessMemoryForVm(address, &value.value, width)))) { - return rc; - } + R_TRY(ReadCheatProcessMemoryForVm(address, &value.value, width)); g_frozen_addresses_map[address] = value; *out_value = value.value; @@ -835,7 +830,6 @@ static void StartDebugProcess(u64 pid) { Result DmntCheatManager::ForceOpenCheatProcess() { std::scoped_lock attach_lk(g_attach_lock); - Result rc; /* Acquire the cheat lock for long enough to close the process if needed. */ { @@ -857,19 +851,17 @@ Result DmntCheatManager::ForceOpenCheatProcess() { std::scoped_lock lk(g_cheat_lock); /* Get the current application process ID. */ - if (R_FAILED((rc = pmdmntGetApplicationPid(&g_cheat_process_metadata.process_id)))) { - return rc; - } - ON_SCOPE_EXIT { if (R_FAILED(rc)) { g_cheat_process_metadata.process_id = 0; } }; + R_TRY(pmdmntGetApplicationPid(&g_cheat_process_metadata.process_id)); + auto proc_guard = SCOPE_EXIT { + g_cheat_process_metadata.process_id = 0; + }; /* Get process handle, use it to learn memory extents. */ { Handle proc_h = 0; ON_SCOPE_EXIT { if (proc_h != 0) { svcCloseHandle(proc_h); } }; - if (R_FAILED((rc = pmdmntAtmosphereGetProcessInfo(&proc_h, &g_cheat_process_metadata.title_id, nullptr, g_cheat_process_metadata.process_id)))) { - return rc; - } + R_TRY(pmdmntAtmosphereGetProcessInfo(&proc_h, &g_cheat_process_metadata.title_id, nullptr, g_cheat_process_metadata.process_id)); /* Get memory extents. */ PopulateMemoryExtents(&g_cheat_process_metadata.heap_extents, proc_h, 4, 5); @@ -886,9 +878,7 @@ Result DmntCheatManager::ForceOpenCheatProcess() { { LoaderModuleInfo proc_modules[2]; u32 num_modules; - if (R_FAILED((rc = ldrDmntGetModuleInfos(g_cheat_process_metadata.process_id, proc_modules, sizeof(proc_modules)/sizeof(proc_modules[0]), &num_modules)))) { - return rc; - } + R_TRY(ldrDmntGetModuleInfos(g_cheat_process_metadata.process_id, proc_modules, sizeof(proc_modules)/sizeof(proc_modules[0]), &num_modules)); /* All applications must have two modules. */ /* However, this is a force-open, so we will accept one module. */ @@ -899,8 +889,7 @@ Result DmntCheatManager::ForceOpenCheatProcess() { } else if (num_modules == 1) { proc_module = &proc_modules[0]; } else { - rc = ResultDmntCheatNotAttached; - return rc; + return ResultDmntCheatNotAttached; } g_cheat_process_metadata.main_nso_extents.base = proc_module->base_address; @@ -916,16 +905,18 @@ Result DmntCheatManager::ForceOpenCheatProcess() { LoadCheatToggles(g_cheat_process_metadata.title_id); /* Open a debug handle. */ - if (R_FAILED((rc = svcDebugActiveProcess(&g_cheat_process_debug_hnd, g_cheat_process_metadata.process_id)))) { - return rc; - } + R_TRY(svcDebugActiveProcess(&g_cheat_process_debug_hnd, g_cheat_process_metadata.process_id)); + + /* Cancel process guard. */ + proc_guard.Cancel(); + /* Start debug events thread. */ StartDebugEventsThread(); /* Signal to our fans. */ g_cheat_process_event->Signal(); - return rc; + return ResultSuccess; } void DmntCheatManager::OnNewApplicationLaunch() { diff --git a/stratosphere/dmnt/source/dmnt_cheat_service.cpp b/stratosphere/dmnt/source/dmnt_cheat_service.cpp index b6741d4aa..54f339bda 100644 --- a/stratosphere/dmnt/source/dmnt_cheat_service.cpp +++ b/stratosphere/dmnt/source/dmnt_cheat_service.cpp @@ -13,7 +13,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - + #include #include "dmnt_cheat_service.hpp" #include "dmnt_cheat_manager.hpp" @@ -35,13 +35,11 @@ Result DmntCheatService::GetCheatProcessMetadata(Out out_m } Result DmntCheatService::ForceOpenCheatProcess() { - Result rc = DmntCheatManager::ForceOpenCheatProcess(); - - if (R_FAILED(rc)) { - rc = ResultDmntCheatNotAttached; + if (R_FAILED(DmntCheatManager::ForceOpenCheatProcess())) { + return ResultDmntCheatNotAttached; } - - return rc; + + return ResultSuccess; } /* ========================================================================================= */ @@ -56,7 +54,7 @@ Result DmntCheatService::GetCheatProcessMappings(OutBuffer mappings, if (mappings.buffer == nullptr) { return ResultDmntCheatNullBuffer; } - + return DmntCheatManager::GetCheatProcessMappings(mappings.buffer, mappings.num_elements, out_count.GetPointer(), offset); } @@ -64,12 +62,12 @@ Result DmntCheatService::ReadCheatProcessMemory(OutBuffer buffer, u64 addres if (buffer.buffer == nullptr) { return ResultDmntCheatNullBuffer; } - + u64 sz = out_size; if (buffer.num_elements < sz) { sz = buffer.num_elements; } - + return DmntCheatManager::ReadCheatProcessMemory(address, buffer.buffer, sz); } @@ -77,12 +75,12 @@ Result DmntCheatService::WriteCheatProcessMemory(InBuffer buffer, u64 addres if (buffer.buffer == nullptr) { return ResultDmntCheatNullBuffer; } - + u64 sz = in_size; if (buffer.num_elements < sz) { sz = buffer.num_elements; } - + return DmntCheatManager::WriteCheatProcessMemory(address, buffer.buffer, sz); } @@ -102,7 +100,7 @@ Result DmntCheatService::GetCheats(OutBuffer cheats, Out out_co if (cheats.buffer == nullptr) { return ResultDmntCheatNullBuffer; } - + return DmntCheatManager::GetCheats(cheats.buffer, cheats.num_elements, out_count.GetPointer(), offset); } @@ -110,11 +108,11 @@ Result DmntCheatService::GetCheatById(OutBuffer cheat, u32 cheat_id) if (cheat.buffer == nullptr) { return ResultDmntCheatNullBuffer; } - + if (cheat.num_elements < 1) { return ResultDmntCheatInvalidBuffer; } - + return DmntCheatManager::GetCheatById(cheat.buffer, cheat_id); } @@ -126,11 +124,11 @@ Result DmntCheatService::AddCheat(InBuffer cheat, Out out_ if (cheat.buffer == nullptr) { return ResultDmntCheatNullBuffer; } - + if (cheat.num_elements < 1) { return ResultDmntCheatInvalidBuffer; } - + return DmntCheatManager::AddCheat(out_cheat_id.GetPointer(), cheat.buffer, enabled); } @@ -150,7 +148,7 @@ Result DmntCheatService::GetFrozenAddresses(OutBuffer frz_ad if (frz_addrs.buffer == nullptr) { return ResultDmntCheatNullBuffer; } - + return DmntCheatManager::GetFrozenAddresses(frz_addrs.buffer, frz_addrs.num_elements, out_count.GetPointer(), offset); } @@ -168,7 +166,7 @@ Result DmntCheatService::EnableFrozenAddress(Out out_value, u64 address, u6 default: return ResultDmntCheatInvalidFreezeWidth; } - + return DmntCheatManager::EnableFrozenAddress(out_value.GetPointer(), address, width); } diff --git a/stratosphere/dmnt/source/dmnt_service_debug.cpp b/stratosphere/dmnt/source/dmnt_service_debug.cpp index 4065ed585..91ba8ca6c 100644 --- a/stratosphere/dmnt/source/dmnt_service_debug.cpp +++ b/stratosphere/dmnt/source/dmnt_service_debug.cpp @@ -13,7 +13,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - + #include #include "dmnt_service.hpp" @@ -39,11 +39,13 @@ Result DebugMonitorService::GetProcessId(Out out_pid, Handle hnd) { } Result DebugMonitorService::GetProcessHandle(Out out_hnd, u64 pid) { - Result rc = svcDebugActiveProcess(out_hnd.GetPointer(), pid); - if (rc == ResultKernelAlreadyExists) { - rc = ResultDebugAlreadyAttached; - } - return rc; + R_TRY_CATCH(svcDebugActiveProcess(out_hnd.GetPointer(), pid)) { + R_CATCH(ResultKernelAlreadyExists) { + return ResultDebugAlreadyAttached; + } + } R_END_TRY_CATCH; + + return ResultSuccess; } Result DebugMonitorService::WaitSynchronization(Handle hnd, u64 ns) { diff --git a/stratosphere/dmnt/source/dmnt_service_target_io.cpp b/stratosphere/dmnt/source/dmnt_service_target_io.cpp index 7f130f303..ad7d915fe 100644 --- a/stratosphere/dmnt/source/dmnt_service_target_io.cpp +++ b/stratosphere/dmnt/source/dmnt_service_target_io.cpp @@ -13,7 +13,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - + #include #include #include "dmnt_service.hpp" @@ -40,17 +40,15 @@ static Result EnsureSdInitialized() { if (g_sd_initialized) { return ResultSuccess; } - - Result rc = fsMountSdcard(&g_sd_fs); - if (R_SUCCEEDED(rc)) { - g_sd_initialized = true; - } - return rc; + + R_TRY(fsMountSdcard(&g_sd_fs)); + g_sd_initialized = true; + return ResultSuccess; } -static u64 GetFileHandle(FsFile f) { +static u64 GetNewFileHandle(FsFile f) { std::scoped_lock lk(g_file_handle_lock); - + u64 fd = g_cur_fd++; g_file_handles[fd] = f; return fd; @@ -58,45 +56,49 @@ static u64 GetFileHandle(FsFile f) { static Result GetFileByHandle(FsFile *out, u64 handle) { std::scoped_lock lk(g_file_handle_lock); + if (g_file_handles.find(handle) != g_file_handles.end()) { *out = g_file_handles[handle]; return ResultSuccess; } + return ResultFsInvalidArgument; } static Result CloseFileByHandle(u64 handle) { std::scoped_lock lk(g_file_handle_lock); + if (g_file_handles.find(handle) != g_file_handles.end()) { fsFileClose(&g_file_handles[handle]); g_file_handles.erase(handle); return ResultSuccess; } + return ResultFsInvalidArgument; } static void FixPath(char *dst, size_t dst_size, InBuffer &path) { dst[dst_size - 1] = 0; strncpy(dst, "/", dst_size - 1); - + const char *src = path.buffer; size_t src_idx = 0; size_t dst_idx = 1; while (src_idx < path.num_elements && (src[src_idx] == '/' || src[src_idx] == '\\')) { src_idx++; } - + while (src_idx < path.num_elements && dst_idx < dst_size - 1 && src[src_idx] != 0) { if (src[src_idx] == '\\') { dst[dst_idx] = '/'; } else { dst[dst_idx] = src[src_idx]; } - + src_idx++; dst_idx++; } - + if (dst_idx < dst_size) { dst[dst_idx] = 0; } @@ -107,47 +109,39 @@ Result DebugMonitorService::TargetIO_FileOpen(OutBuffer out_hnd, InBuffer hnd) { @@ -155,7 +149,7 @@ Result DebugMonitorService::TargetIO_FileClose(InBuffer hnd) { /* Serialization error. */ return ResultKernelConnectionClosed; } - + return CloseFileByHandle(hnd[0]); } @@ -164,17 +158,15 @@ Result DebugMonitorService::TargetIO_FileRead(InBuffer hnd, OutBuffer(read)); - return rc; + return ResultSuccess; } Result DebugMonitorService::TargetIO_FileWrite(InBuffer hnd, InBuffer data, Out out_written, u64 offset) { @@ -182,19 +174,14 @@ Result DebugMonitorService::TargetIO_FileWrite(InBuffer hnd, InBuffer path, InBuffer attributes) { @@ -208,39 +195,33 @@ Result DebugMonitorService::TargetIO_FileGetInformation(InBuffer path, Out /* Serialization error. */ return ResultKernelConnectionClosed; } - - Result rc = EnsureSdInitialized(); - if (R_FAILED(rc)) { - return rc; - } - + + R_TRY(EnsureSdInitialized()); + char fs_path[FS_MAX_PATH]; FixPath(fs_path, sizeof(fs_path), path); - + for (size_t i = 0; i < out_info.num_elements; i++) { out_info[i] = 0; } is_directory.SetValue(0); - + FsFile f; - rc = fsFsOpenFile(&g_sd_fs, fs_path, FS_OPEN_READ, &f); - if (R_SUCCEEDED(rc)) { + if (R_SUCCEEDED(fsFsOpenFile(&g_sd_fs, fs_path, FS_OPEN_READ, &f))) { ON_SCOPE_EXIT { fsFileClose(&f); }; - + /* N doesn't check this return code. */ fsFileGetSize(&f, &out_info[0]); - + /* TODO: N does not call fsFsGetFileTimestampRaw here, but we possibly could. */ } else { FsDir dir; - rc = fsFsOpenDirectory(&g_sd_fs, fs_path, FS_DIROPEN_FILE | FS_DIROPEN_DIRECTORY, &dir); - if (R_SUCCEEDED(rc)) { - fsDirClose(&dir); - is_directory.SetValue(1); - } + R_TRY(fsFsOpenDirectory(&g_sd_fs, fs_path, FS_DIROPEN_FILE | FS_DIROPEN_DIRECTORY, &dir)); + fsDirClose(&dir); + is_directory.SetValue(1); } - - return rc; + + return ResultSuccess; } Result DebugMonitorService::TargetIO_FileSetTime(InBuffer path, u64 create, u64 access, u64 modify) { @@ -250,7 +231,7 @@ Result DebugMonitorService::TargetIO_FileSetTime(InBuffer path, u64 create Result DebugMonitorService::TargetIO_FileSetSize(InBuffer input, u64 size) { /* Why does this function take in a path and not a file handle? */ - + /* We will try to be better than N, here. N only treats input as a path. */ if (input.num_elements == sizeof(u64)) { FsFile f; @@ -258,47 +239,35 @@ Result DebugMonitorService::TargetIO_FileSetSize(InBuffer input, u64 size) return fsFileSetSize(&f, size); } } - - Result rc = EnsureSdInitialized(); - if (R_FAILED(rc)) { - return rc; - } - + + R_TRY(EnsureSdInitialized()); + char fs_path[FS_MAX_PATH]; FixPath(fs_path, sizeof(fs_path), input); - + FsFile f; - rc = fsFsOpenFile(&g_sd_fs, fs_path, FS_OPEN_WRITE, &f); - if (R_SUCCEEDED(rc)) { - rc = fsFileSetSize(&f, size); - fsFileClose(&f); - } - - return rc; + R_TRY(fsFsOpenFile(&g_sd_fs, fs_path, FS_OPEN_WRITE, &f)); + ON_SCOPE_EXIT { fsFileClose(&f); }; + + return fsFileSetSize(&f, size); } Result DebugMonitorService::TargetIO_FileDelete(InBuffer path) { - Result rc = EnsureSdInitialized(); - if (R_FAILED(rc)) { - return rc; - } - + R_TRY(EnsureSdInitialized()); + char fs_path[FS_MAX_PATH]; FixPath(fs_path, sizeof(fs_path), path); - + return fsFsDeleteFile(&g_sd_fs, fs_path); } Result DebugMonitorService::TargetIO_FileMove(InBuffer path0, InBuffer path1) { - Result rc = EnsureSdInitialized(); - if (R_FAILED(rc)) { - return rc; - } - + R_TRY(EnsureSdInitialized()); + char fs_path0[FS_MAX_PATH]; char fs_path1[FS_MAX_PATH]; FixPath(fs_path0, sizeof(fs_path0), path0); FixPath(fs_path1, sizeof(fs_path1), path1); - + return fsFsRenameFile(&g_sd_fs, fs_path0, fs_path1); }