dmnt: update for R_TRY

This commit is contained in:
Michael Scire 2019-06-19 22:19:53 -07:00
parent 241b8f4627
commit d7a3645f7f
4 changed files with 133 additions and 173 deletions

View file

@ -139,10 +139,9 @@ Result DmntCheatManager::ReadCheatProcessMemoryForVm(u64 proc_addr, void *out_da
Result DmntCheatManager::WriteCheatProcessMemoryForVm(u64 proc_addr, const void *data, size_t size) { Result DmntCheatManager::WriteCheatProcessMemoryForVm(u64 proc_addr, const void *data, size_t size) {
if (HasActiveCheatProcess()) { 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! */ /* We might have a frozen address. Update it if we do! */
if (R_SUCCEEDED(rc)) {
for (auto & [address, value] : g_frozen_addresses_map) { for (auto & [address, value] : g_frozen_addresses_map) {
/* Map is in order, so break here. */ /* Map is in order, so break here. */
if (address >= proc_addr + size) { if (address >= proc_addr + size) {
@ -156,9 +155,8 @@ Result DmntCheatManager::WriteCheatProcessMemoryForVm(u64 proc_addr, const void
memcpy(&value.value, (void *)((uintptr_t)data + offset), size_to_copy < sizeof(value.value) ? size_to_copy : sizeof(value.value)); 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; return ResultDmntCheatNotAttached;
@ -775,12 +773,9 @@ Result DmntCheatManager::EnableFrozenAddress(u64 *out_value, u64 address, u64 wi
return ResultDmntCheatAddressAlreadyFrozen; return ResultDmntCheatAddressAlreadyFrozen;
} }
Result rc;
FrozenAddressValue value = {0}; FrozenAddressValue value = {0};
value.width = width; value.width = width;
if (R_FAILED((rc = ReadCheatProcessMemoryForVm(address, &value.value, width)))) { R_TRY(ReadCheatProcessMemoryForVm(address, &value.value, width));
return rc;
}
g_frozen_addresses_map[address] = value; g_frozen_addresses_map[address] = value;
*out_value = value.value; *out_value = value.value;
@ -835,7 +830,6 @@ static void StartDebugProcess(u64 pid) {
Result DmntCheatManager::ForceOpenCheatProcess() { Result DmntCheatManager::ForceOpenCheatProcess() {
std::scoped_lock<HosMutex> attach_lk(g_attach_lock); std::scoped_lock<HosMutex> attach_lk(g_attach_lock);
Result rc;
/* Acquire the cheat lock for long enough to close the process if needed. */ /* Acquire the cheat lock for long enough to close the process if needed. */
{ {
@ -857,19 +851,17 @@ Result DmntCheatManager::ForceOpenCheatProcess() {
std::scoped_lock<HosMutex> lk(g_cheat_lock); std::scoped_lock<HosMutex> lk(g_cheat_lock);
/* Get the current application process ID. */ /* Get the current application process ID. */
if (R_FAILED((rc = pmdmntGetApplicationPid(&g_cheat_process_metadata.process_id)))) { R_TRY(pmdmntGetApplicationPid(&g_cheat_process_metadata.process_id));
return rc; auto proc_guard = SCOPE_EXIT {
} g_cheat_process_metadata.process_id = 0;
ON_SCOPE_EXIT { if (R_FAILED(rc)) { g_cheat_process_metadata.process_id = 0; } }; };
/* Get process handle, use it to learn memory extents. */ /* Get process handle, use it to learn memory extents. */
{ {
Handle proc_h = 0; Handle proc_h = 0;
ON_SCOPE_EXIT { if (proc_h != 0) { svcCloseHandle(proc_h); } }; 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)))) { R_TRY(pmdmntAtmosphereGetProcessInfo(&proc_h, &g_cheat_process_metadata.title_id, nullptr, g_cheat_process_metadata.process_id));
return rc;
}
/* Get memory extents. */ /* Get memory extents. */
PopulateMemoryExtents(&g_cheat_process_metadata.heap_extents, proc_h, 4, 5); PopulateMemoryExtents(&g_cheat_process_metadata.heap_extents, proc_h, 4, 5);
@ -886,9 +878,7 @@ Result DmntCheatManager::ForceOpenCheatProcess() {
{ {
LoaderModuleInfo proc_modules[2]; LoaderModuleInfo proc_modules[2];
u32 num_modules; 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)))) { R_TRY(ldrDmntGetModuleInfos(g_cheat_process_metadata.process_id, proc_modules, sizeof(proc_modules)/sizeof(proc_modules[0]), &num_modules));
return rc;
}
/* All applications must have two modules. */ /* All applications must have two modules. */
/* However, this is a force-open, so we will accept one module. */ /* However, this is a force-open, so we will accept one module. */
@ -899,8 +889,7 @@ Result DmntCheatManager::ForceOpenCheatProcess() {
} else if (num_modules == 1) { } else if (num_modules == 1) {
proc_module = &proc_modules[0]; proc_module = &proc_modules[0];
} else { } else {
rc = ResultDmntCheatNotAttached; return ResultDmntCheatNotAttached;
return rc;
} }
g_cheat_process_metadata.main_nso_extents.base = proc_module->base_address; 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); LoadCheatToggles(g_cheat_process_metadata.title_id);
/* Open a debug handle. */ /* Open a debug handle. */
if (R_FAILED((rc = svcDebugActiveProcess(&g_cheat_process_debug_hnd, g_cheat_process_metadata.process_id)))) { R_TRY(svcDebugActiveProcess(&g_cheat_process_debug_hnd, g_cheat_process_metadata.process_id));
return rc;
} /* Cancel process guard. */
proc_guard.Cancel();
/* Start debug events thread. */ /* Start debug events thread. */
StartDebugEventsThread(); StartDebugEventsThread();
/* Signal to our fans. */ /* Signal to our fans. */
g_cheat_process_event->Signal(); g_cheat_process_event->Signal();
return rc; return ResultSuccess;
} }
void DmntCheatManager::OnNewApplicationLaunch() { void DmntCheatManager::OnNewApplicationLaunch() {

View file

@ -35,13 +35,11 @@ Result DmntCheatService::GetCheatProcessMetadata(Out<CheatProcessMetadata> out_m
} }
Result DmntCheatService::ForceOpenCheatProcess() { Result DmntCheatService::ForceOpenCheatProcess() {
Result rc = DmntCheatManager::ForceOpenCheatProcess(); if (R_FAILED(DmntCheatManager::ForceOpenCheatProcess())) {
return ResultDmntCheatNotAttached;
if (R_FAILED(rc)) {
rc = ResultDmntCheatNotAttached;
} }
return rc; return ResultSuccess;
} }
/* ========================================================================================= */ /* ========================================================================================= */

View file

@ -39,11 +39,13 @@ Result DebugMonitorService::GetProcessId(Out<u64> out_pid, Handle hnd) {
} }
Result DebugMonitorService::GetProcessHandle(Out<Handle> out_hnd, u64 pid) { Result DebugMonitorService::GetProcessHandle(Out<Handle> out_hnd, u64 pid) {
Result rc = svcDebugActiveProcess(out_hnd.GetPointer(), pid); R_TRY_CATCH(svcDebugActiveProcess(out_hnd.GetPointer(), pid)) {
if (rc == ResultKernelAlreadyExists) { R_CATCH(ResultKernelAlreadyExists) {
rc = ResultDebugAlreadyAttached; return ResultDebugAlreadyAttached;
} }
return rc; } R_END_TRY_CATCH;
return ResultSuccess;
} }
Result DebugMonitorService::WaitSynchronization(Handle hnd, u64 ns) { Result DebugMonitorService::WaitSynchronization(Handle hnd, u64 ns) {

View file

@ -41,14 +41,12 @@ static Result EnsureSdInitialized() {
return ResultSuccess; return ResultSuccess;
} }
Result rc = fsMountSdcard(&g_sd_fs); R_TRY(fsMountSdcard(&g_sd_fs));
if (R_SUCCEEDED(rc)) {
g_sd_initialized = true; g_sd_initialized = true;
} return ResultSuccess;
return rc;
} }
static u64 GetFileHandle(FsFile f) { static u64 GetNewFileHandle(FsFile f) {
std::scoped_lock<HosMutex> lk(g_file_handle_lock); std::scoped_lock<HosMutex> lk(g_file_handle_lock);
u64 fd = g_cur_fd++; u64 fd = g_cur_fd++;
@ -58,20 +56,24 @@ static u64 GetFileHandle(FsFile f) {
static Result GetFileByHandle(FsFile *out, u64 handle) { static Result GetFileByHandle(FsFile *out, u64 handle) {
std::scoped_lock<HosMutex> lk(g_file_handle_lock); std::scoped_lock<HosMutex> lk(g_file_handle_lock);
if (g_file_handles.find(handle) != g_file_handles.end()) { if (g_file_handles.find(handle) != g_file_handles.end()) {
*out = g_file_handles[handle]; *out = g_file_handles[handle];
return ResultSuccess; return ResultSuccess;
} }
return ResultFsInvalidArgument; return ResultFsInvalidArgument;
} }
static Result CloseFileByHandle(u64 handle) { static Result CloseFileByHandle(u64 handle) {
std::scoped_lock<HosMutex> lk(g_file_handle_lock); std::scoped_lock<HosMutex> lk(g_file_handle_lock);
if (g_file_handles.find(handle) != g_file_handles.end()) { if (g_file_handles.find(handle) != g_file_handles.end()) {
fsFileClose(&g_file_handles[handle]); fsFileClose(&g_file_handles[handle]);
g_file_handles.erase(handle); g_file_handles.erase(handle);
return ResultSuccess; return ResultSuccess;
} }
return ResultFsInvalidArgument; return ResultFsInvalidArgument;
} }
@ -108,46 +110,38 @@ Result DebugMonitorService::TargetIO_FileOpen(OutBuffer<u64> out_hnd, InBuffer<c
return ResultKernelConnectionClosed; return ResultKernelConnectionClosed;
} }
Result rc = EnsureSdInitialized(); R_TRY(EnsureSdInitialized());
if (R_FAILED(rc)) {
return rc;
}
char fs_path[FS_MAX_PATH]; char fs_path[FS_MAX_PATH];
FixPath(fs_path, sizeof(fs_path), path); FixPath(fs_path, sizeof(fs_path), path);
/* Create file as required by mode. */
if (create_mode == TIOCreateOption_CreateAlways) { if (create_mode == TIOCreateOption_CreateAlways) {
fsFsDeleteFile(&g_sd_fs, fs_path); fsFsDeleteFile(&g_sd_fs, fs_path);
rc = fsFsCreateFile(&g_sd_fs, fs_path, 0, 0); R_TRY(fsFsCreateFile(&g_sd_fs, fs_path, 0, 0));
} else if (create_mode == TIOCreateOption_CreateNew) { } else if (create_mode == TIOCreateOption_CreateNew) {
rc = fsFsCreateFile(&g_sd_fs, fs_path, 0, 0); R_TRY(fsFsCreateFile(&g_sd_fs, fs_path, 0, 0));
} } else if (create_mode == TIOCreateOption_OpenAlways) {
if (R_FAILED(rc)) {
return rc;
}
FsFile f;
rc = fsFsOpenFile(&g_sd_fs, fs_path, open_mode, &f);
if (R_FAILED(rc)) {
if (create_mode == TIOCreateOption_OpenAlways) {
fsFsCreateFile(&g_sd_fs, fs_path, 0, 0); fsFsCreateFile(&g_sd_fs, fs_path, 0, 0);
rc = fsFsOpenFile(&g_sd_fs, fs_path, open_mode, &f);
}
} }
if (R_SUCCEEDED(rc)) { /* Open the file, guard to prevent failure to close. */
if (create_mode == TIOCreateOption_ResetSize) { FsFile f;
rc = fsFileSetSize(&f, 0); R_TRY(fsFsOpenFile(&g_sd_fs, fs_path, open_mode, &f));
} auto file_guard = SCOPE_GUARD {
if (R_SUCCEEDED(rc)) {
out_hnd[0] = GetFileHandle(f);
} else {
fsFileClose(&f); fsFileClose(&f);
} };
/* Set size if needed. */
if (create_mode == TIOCreateOption_ResetSize) {
R_TRY(fsFileSetSize(&f, 0));
} }
return rc; /* Cancel guard, output file handle. */
file_guard.Cancel();
out_hnd[0] = GetNewFileHandle(f);
return ResultSuccess;
} }
Result DebugMonitorService::TargetIO_FileClose(InBuffer<u64> hnd) { Result DebugMonitorService::TargetIO_FileClose(InBuffer<u64> hnd) {
@ -166,15 +160,13 @@ Result DebugMonitorService::TargetIO_FileRead(InBuffer<u64> hnd, OutBuffer<u8, B
} }
FsFile f; FsFile f;
Result rc = GetFileByHandle(&f, hnd[0]);
if (R_FAILED(rc)) {
return rc;
}
size_t read = 0; size_t read = 0;
rc = fsFileRead(&f, offset, out_data.buffer, out_data.num_elements, FS_READOPTION_NONE, &read);
R_TRY(GetFileByHandle(&f, hnd[0]));
R_TRY(fsFileRead(&f, offset, out_data.buffer, out_data.num_elements, FS_READOPTION_NONE, &read));
out_read.SetValue(static_cast<u32>(read)); out_read.SetValue(static_cast<u32>(read));
return rc; return ResultSuccess;
} }
Result DebugMonitorService::TargetIO_FileWrite(InBuffer<u64> hnd, InBuffer<u8, BufferType_Type1> data, Out<u32> out_written, u64 offset) { Result DebugMonitorService::TargetIO_FileWrite(InBuffer<u64> hnd, InBuffer<u8, BufferType_Type1> data, Out<u32> out_written, u64 offset) {
@ -184,17 +176,12 @@ Result DebugMonitorService::TargetIO_FileWrite(InBuffer<u64> hnd, InBuffer<u8, B
} }
FsFile f; FsFile f;
Result rc = GetFileByHandle(&f, hnd[0]);
if (R_FAILED(rc)) {
return rc;
}
rc = fsFileWrite(&f, offset, data.buffer, data.num_elements, FS_WRITEOPTION_NONE); R_TRY(GetFileByHandle(&f, hnd[0]));
if (R_SUCCEEDED(rc)) { R_TRY(fsFileWrite(&f, offset, data.buffer, data.num_elements, FS_WRITEOPTION_NONE));
out_written.SetValue(data.num_elements); out_written.SetValue(data.num_elements);
} return ResultSuccess;
return rc;
} }
Result DebugMonitorService::TargetIO_FileSetAttributes(InBuffer<char> path, InBuffer<u8> attributes) { Result DebugMonitorService::TargetIO_FileSetAttributes(InBuffer<char> path, InBuffer<u8> attributes) {
@ -209,10 +196,7 @@ Result DebugMonitorService::TargetIO_FileGetInformation(InBuffer<char> path, Out
return ResultKernelConnectionClosed; return ResultKernelConnectionClosed;
} }
Result rc = EnsureSdInitialized(); R_TRY(EnsureSdInitialized());
if (R_FAILED(rc)) {
return rc;
}
char fs_path[FS_MAX_PATH]; char fs_path[FS_MAX_PATH];
FixPath(fs_path, sizeof(fs_path), path); FixPath(fs_path, sizeof(fs_path), path);
@ -223,8 +207,7 @@ Result DebugMonitorService::TargetIO_FileGetInformation(InBuffer<char> path, Out
is_directory.SetValue(0); is_directory.SetValue(0);
FsFile f; FsFile f;
rc = fsFsOpenFile(&g_sd_fs, fs_path, FS_OPEN_READ, &f); if (R_SUCCEEDED(fsFsOpenFile(&g_sd_fs, fs_path, FS_OPEN_READ, &f))) {
if (R_SUCCEEDED(rc)) {
ON_SCOPE_EXIT { fsFileClose(&f); }; ON_SCOPE_EXIT { fsFileClose(&f); };
/* N doesn't check this return code. */ /* N doesn't check this return code. */
@ -233,14 +216,12 @@ Result DebugMonitorService::TargetIO_FileGetInformation(InBuffer<char> path, Out
/* TODO: N does not call fsFsGetFileTimestampRaw here, but we possibly could. */ /* TODO: N does not call fsFsGetFileTimestampRaw here, but we possibly could. */
} else { } else {
FsDir dir; FsDir dir;
rc = fsFsOpenDirectory(&g_sd_fs, fs_path, FS_DIROPEN_FILE | FS_DIROPEN_DIRECTORY, &dir); R_TRY(fsFsOpenDirectory(&g_sd_fs, fs_path, FS_DIROPEN_FILE | FS_DIROPEN_DIRECTORY, &dir));
if (R_SUCCEEDED(rc)) {
fsDirClose(&dir); fsDirClose(&dir);
is_directory.SetValue(1); is_directory.SetValue(1);
} }
}
return rc; return ResultSuccess;
} }
Result DebugMonitorService::TargetIO_FileSetTime(InBuffer<char> path, u64 create, u64 access, u64 modify) { Result DebugMonitorService::TargetIO_FileSetTime(InBuffer<char> path, u64 create, u64 access, u64 modify) {
@ -259,29 +240,20 @@ Result DebugMonitorService::TargetIO_FileSetSize(InBuffer<char> input, u64 size)
} }
} }
Result rc = EnsureSdInitialized(); R_TRY(EnsureSdInitialized());
if (R_FAILED(rc)) {
return rc;
}
char fs_path[FS_MAX_PATH]; char fs_path[FS_MAX_PATH];
FixPath(fs_path, sizeof(fs_path), input); FixPath(fs_path, sizeof(fs_path), input);
FsFile f; FsFile f;
rc = fsFsOpenFile(&g_sd_fs, fs_path, FS_OPEN_WRITE, &f); R_TRY(fsFsOpenFile(&g_sd_fs, fs_path, FS_OPEN_WRITE, &f));
if (R_SUCCEEDED(rc)) { ON_SCOPE_EXIT { fsFileClose(&f); };
rc = fsFileSetSize(&f, size);
fsFileClose(&f);
}
return rc; return fsFileSetSize(&f, size);
} }
Result DebugMonitorService::TargetIO_FileDelete(InBuffer<char> path) { Result DebugMonitorService::TargetIO_FileDelete(InBuffer<char> path) {
Result rc = EnsureSdInitialized(); R_TRY(EnsureSdInitialized());
if (R_FAILED(rc)) {
return rc;
}
char fs_path[FS_MAX_PATH]; char fs_path[FS_MAX_PATH];
FixPath(fs_path, sizeof(fs_path), path); FixPath(fs_path, sizeof(fs_path), path);
@ -290,10 +262,7 @@ Result DebugMonitorService::TargetIO_FileDelete(InBuffer<char> path) {
} }
Result DebugMonitorService::TargetIO_FileMove(InBuffer<char> path0, InBuffer<char> path1) { Result DebugMonitorService::TargetIO_FileMove(InBuffer<char> path0, InBuffer<char> path1) {
Result rc = EnsureSdInitialized(); R_TRY(EnsureSdInitialized());
if (R_FAILED(rc)) {
return rc;
}
char fs_path0[FS_MAX_PATH]; char fs_path0[FS_MAX_PATH];
char fs_path1[FS_MAX_PATH]; char fs_path1[FS_MAX_PATH];