Results: Implement namespaced, type-safe results.

Because I was working on multiple things at once, this commit also:
- Adds wrappers for/linker flags to wrap CXX exceptions to make them
  abort. This saves ~0x8000 of memory in every system module.
- Broadly replaces lines of the pattern if (cond) { return ResultX; }
  with R_UNLESS(!cond, ResultX());.
- Reworks the R_TRY_CATCH macros (and the result macros in general).
This commit is contained in:
Michael Scire
2019-10-24 01:40:44 -07:00
committed by SciresM
parent 15773e4755
commit 4059dc6187
169 changed files with 2172 additions and 1868 deletions

View File

@@ -37,11 +37,8 @@ namespace sts::dmnt::cheat {
}
Result CheatService::ForceOpenCheatProcess() {
if (R_FAILED(dmnt::cheat::impl::ForceOpenCheatProcess())) {
return ResultDmntCheatNotAttached;
}
return ResultSuccess;
R_UNLESS(R_SUCCEEDED(dmnt::cheat::impl::ForceOpenCheatProcess()), ResultCheatNotAttached());
return ResultSuccess();
}
/* ========================================================================================= */
@@ -53,26 +50,17 @@ namespace sts::dmnt::cheat {
}
Result CheatService::GetCheatProcessMappings(const sf::OutArray<MemoryInfo> &mappings, sf::Out<u64> out_count, u64 offset) {
if (mappings.GetPointer() == nullptr) {
return ResultDmntCheatNullBuffer;
}
R_UNLESS(mappings.GetPointer() != nullptr, ResultCheatNullBuffer());
return dmnt::cheat::impl::GetCheatProcessMappings(mappings.GetPointer(), mappings.GetSize(), out_count.GetPointer(), offset);
}
Result CheatService::ReadCheatProcessMemory(const sf::OutBuffer &buffer, u64 address, u64 out_size) {
if (buffer.GetPointer() == nullptr) {
return ResultDmntCheatNullBuffer;
}
R_UNLESS(buffer.GetPointer() != nullptr, ResultCheatNullBuffer());
return dmnt::cheat::impl::ReadCheatProcessMemory(address, buffer.GetPointer(), std::min(out_size, buffer.GetSize()));
}
Result CheatService::WriteCheatProcessMemory(const sf::InBuffer &buffer, u64 address, u64 in_size) {
if (buffer.GetPointer() == nullptr) {
return ResultDmntCheatNullBuffer;
}
R_UNLESS(buffer.GetPointer() != nullptr, ResultCheatNullBuffer());
return dmnt::cheat::impl::WriteCheatProcessMemory(address, buffer.GetPointer(), std::min(in_size, buffer.GetSize()));
}
@@ -89,10 +77,7 @@ namespace sts::dmnt::cheat {
}
Result CheatService::GetCheats(const sf::OutArray<CheatEntry> &cheats, sf::Out<u64> out_count, u64 offset) {
if (cheats.GetPointer() == nullptr) {
return ResultDmntCheatNullBuffer;
}
R_UNLESS(cheats.GetPointer() != nullptr, ResultCheatNullBuffer());
return dmnt::cheat::impl::GetCheats(cheats.GetPointer(), cheats.GetSize(), out_count.GetPointer(), offset);
}
@@ -121,10 +106,7 @@ namespace sts::dmnt::cheat {
}
Result CheatService::GetFrozenAddresses(const sf::OutArray<FrozenAddressEntry> &addresses, sf::Out<u64> out_count, u64 offset) {
if (addresses.GetPointer() == nullptr) {
return ResultDmntCheatNullBuffer;
}
R_UNLESS(addresses.GetPointer() != nullptr, ResultCheatNullBuffer());
return dmnt::cheat::impl::GetFrozenAddresses(addresses.GetPointer(), addresses.GetSize(), out_count.GetPointer(), offset);
}
@@ -133,16 +115,10 @@ namespace sts::dmnt::cheat {
}
Result CheatService::EnableFrozenAddress(sf::Out<u64> out_value, u64 address, u64 width) {
switch (width) {
case 1:
case 2:
case 4:
case 8:
break;
default:
return ResultDmntCheatInvalidFreezeWidth;
}
/* Width needs to be a power of two <= 8. */
R_UNLESS(width > 0, ResultFrozenAddressInvalidWidth());
R_UNLESS(width <= sizeof(u64), ResultFrozenAddressInvalidWidth());
R_UNLESS((width & (width - 1)) == 0, ResultFrozenAddressInvalidWidth());
return dmnt::cheat::impl::EnableFrozenAddress(out_value.GetPointer(), address, width);
}

View File

@@ -169,10 +169,8 @@ namespace sts::dmnt::cheat::impl {
}
Result EnsureCheatProcess() {
if (!this->HasActiveCheatProcess()) {
return ResultDmntCheatNotAttached;
}
return ResultSuccess;
R_UNLESS(this->HasActiveCheatProcess(), ResultCheatNotAttached());
return ResultSuccess();
}
Handle GetCheatProcessHandle() const {
@@ -235,7 +233,7 @@ namespace sts::dmnt::cheat::impl {
R_TRY(this->EnsureCheatProcess());
std::memcpy(out, &this->cheat_process_metadata, sizeof(*out));
return ResultSuccess;
return ResultSuccess();
}
Result ForceOpenCheatProcess() {
@@ -263,7 +261,7 @@ namespace sts::dmnt::cheat::impl {
}
}
return ResultSuccess;
return ResultSuccess();
}
Result GetCheatProcessMappingCount(u64 *out_count) {
@@ -288,7 +286,7 @@ namespace sts::dmnt::cheat::impl {
} while (address != 0);
*out_count = count;
return ResultSuccess;
return ResultSuccess();
}
Result GetCheatProcessMappings(MemoryInfo *mappings, size_t max_count, u64 *out_count, u64 offset) {
@@ -316,7 +314,7 @@ namespace sts::dmnt::cheat::impl {
} while (address != 0 && written_count < max_count);
*out_count = written_count;
return ResultSuccess;
return ResultSuccess();
}
Result ReadCheatProcessMemory(u64 proc_addr, void *out_data, size_t size) {
@@ -357,7 +355,7 @@ namespace sts::dmnt::cheat::impl {
}
*out_count = count;
return ResultSuccess;
return ResultSuccess();
}
Result GetCheats(CheatEntry *out_cheats, size_t max_count, u64 *out_count, u64 offset) {
@@ -376,7 +374,7 @@ namespace sts::dmnt::cheat::impl {
}
*out_count = count;
return ResultSuccess;
return ResultSuccess();
}
Result GetCheatById(CheatEntry *out_cheat, u32 cheat_id) {
@@ -385,12 +383,11 @@ namespace sts::dmnt::cheat::impl {
R_TRY(this->EnsureCheatProcess());
const CheatEntry *entry = this->GetCheatEntryById(cheat_id);
if (entry == nullptr || entry->definition.num_opcodes == 0) {
return ResultDmntCheatUnknownChtId;
}
R_UNLESS(entry != nullptr, ResultCheatUnknownId());
R_UNLESS(entry->definition.num_opcodes != 0, ResultCheatUnknownId());
*out_cheat = *entry;
return ResultSuccess;
return ResultSuccess();
}
Result ToggleCheat(u32 cheat_id) {
@@ -399,20 +396,17 @@ namespace sts::dmnt::cheat::impl {
R_TRY(this->EnsureCheatProcess());
CheatEntry *entry = this->GetCheatEntryById(cheat_id);
if (entry == nullptr || entry->definition.num_opcodes == 0) {
return ResultDmntCheatUnknownChtId;
}
R_UNLESS(entry != nullptr, ResultCheatUnknownId());
R_UNLESS(entry->definition.num_opcodes != 0, ResultCheatUnknownId());
if (cheat_id == 0) {
return ResultDmntCheatCannotDisableMasterCheat;
}
R_UNLESS(cheat_id != 0, ResultCheatCannotDisable());
entry->enabled = !entry->enabled;
/* Trigger a VM reload. */
this->SetNeedsReloadVm(true);
return ResultSuccess;
return ResultSuccess();
}
Result AddCheat(u32 *out_id, const CheatDefinition &def, bool enabled) {
@@ -420,14 +414,11 @@ namespace sts::dmnt::cheat::impl {
R_TRY(this->EnsureCheatProcess());
if (def.num_opcodes == 0 || def.num_opcodes > util::size(def.opcodes)) {
return ResultDmntCheatInvalidCheat;
}
R_UNLESS(def.num_opcodes != 0, ResultCheatInvalid());
R_UNLESS(def.num_opcodes <= util::size(def.opcodes), ResultCheatInvalid());
CheatEntry *new_entry = this->GetFreeCheatEntry();
if (new_entry == nullptr) {
return ResultDmntCheatOutOfCheats;
}
R_UNLESS(new_entry != nullptr, ResultCheatOutOfResource());
new_entry->enabled = enabled;
new_entry->definition = def;
@@ -435,24 +426,21 @@ namespace sts::dmnt::cheat::impl {
/* Trigger a VM reload. */
this->SetNeedsReloadVm(true);
return ResultSuccess;
return ResultSuccess();
}
Result RemoveCheat(u32 cheat_id) {
std::scoped_lock lk(this->cheat_lock);
R_TRY(this->EnsureCheatProcess());
if (cheat_id >= MaxCheatCount) {
return ResultDmntCheatUnknownChtId;
}
R_UNLESS(cheat_id < MaxCheatCount, ResultCheatUnknownId());
this->ResetCheatEntry(cheat_id);
/* Trigger a VM reload. */
this->SetNeedsReloadVm(true);
return ResultSuccess;
return ResultSuccess();
}
Result GetFrozenAddressCount(u64 *out_count) {
@@ -461,7 +449,7 @@ namespace sts::dmnt::cheat::impl {
R_TRY(this->EnsureCheatProcess());
*out_count = this->frozen_addresses_map.size();
return ResultSuccess;
return ResultSuccess();
}
Result GetFrozenAddresses(FrozenAddressEntry *frz_addrs, size_t max_count, u64 *out_count, u64 offset) {
@@ -484,7 +472,7 @@ namespace sts::dmnt::cheat::impl {
}
*out_count = written_count;
return ResultSuccess;
return ResultSuccess();
}
Result GetFrozenAddress(FrozenAddressEntry *frz_addr, u64 address) {
@@ -493,13 +481,11 @@ namespace sts::dmnt::cheat::impl {
R_TRY(this->EnsureCheatProcess());
const auto it = this->frozen_addresses_map.find(address);
if (it == this->frozen_addresses_map.end()) {
return ResultDmntCheatAddressNotFrozen;
}
R_UNLESS(it != this->frozen_addresses_map.end(), ResultFrozenAddressNotFound());
frz_addr->address = it->first;
frz_addr->value = it->second;
return ResultSuccess;
return ResultSuccess();
}
Result EnableFrozenAddress(u64 *out_value, u64 address, u64 width) {
@@ -507,14 +493,10 @@ namespace sts::dmnt::cheat::impl {
R_TRY(this->EnsureCheatProcess());
if (this->frozen_addresses_map.size() >= MaxFrozenAddressCount) {
return ResultDmntCheatTooManyFrozenAddresses;
}
R_UNLESS(this->frozen_addresses_map.size() < MaxFrozenAddressCount, ResultFrozenAddressOutOfResource());
const auto it = this->frozen_addresses_map.find(address);
if (it != this->frozen_addresses_map.end()) {
return ResultDmntCheatAddressAlreadyFrozen;
}
R_UNLESS(it == this->frozen_addresses_map.end(), ResultFrozenAddressAlreadyExists());
FrozenAddressValue value = {};
value.width = width;
@@ -522,7 +504,7 @@ namespace sts::dmnt::cheat::impl {
this->frozen_addresses_map[address] = value;
*out_value = value.value;
return ResultSuccess;
return ResultSuccess();
}
Result DisableFrozenAddress(u64 address) {
@@ -531,12 +513,10 @@ namespace sts::dmnt::cheat::impl {
R_TRY(this->EnsureCheatProcess());
const auto it = this->frozen_addresses_map.find(address);
if (it == this->frozen_addresses_map.end()) {
return ResultDmntCheatAddressNotFrozen;
}
R_UNLESS(it != this->frozen_addresses_map.end(), ResultFrozenAddressNotFound());
this->frozen_addresses_map.erase(it);
return ResultSuccess;
return ResultSuccess();
}
};
@@ -626,9 +606,7 @@ namespace sts::dmnt::cheat::impl {
{
if (this->HasActiveCheatProcess()) {
/* When forcing attach, we're done. */
if (!on_process_launch) {
return ResultSuccess;
}
R_UNLESS(on_process_launch, ResultSuccess());
}
/* Detach from the current process, if it's open. */
@@ -667,9 +645,7 @@ namespace sts::dmnt::cheat::impl {
/* If new process launch, we may not want to actually attach. */
if (on_process_launch) {
if (!cfg::IsCheatEnableKeyHeld(this->cheat_process_metadata.title_id)) {
return ResultDmntCheatNotAttached;
}
R_UNLESS(cfg::IsCheatEnableKeyHeld(this->cheat_process_metadata.title_id), ResultCheatNotAttached());
}
/* Get module information from loader. */
@@ -689,7 +665,7 @@ namespace sts::dmnt::cheat::impl {
} else if (num_modules == 1 && !on_process_launch) {
proc_module = &proc_modules[0];
} else {
return ResultDmntCheatNotAttached;
return ResultCheatNotAttached();
}
this->cheat_process_metadata.main_nso_extents.base = proc_module->base_address;
@@ -701,9 +677,7 @@ namespace sts::dmnt::cheat::impl {
if (!this->LoadCheats(this->cheat_process_metadata.title_id, this->cheat_process_metadata.main_nso_build_id) ||
!this->LoadCheatToggles(this->cheat_process_metadata.title_id)) {
/* If new process launch, require success. */
if (on_process_launch) {
return ResultDmntCheatNotAttached;
}
R_UNLESS(!on_process_launch, ResultCheatNotAttached());
}
/* Open a debug handle. */
@@ -723,7 +697,7 @@ namespace sts::dmnt::cheat::impl {
/* Signal to our fans. */
this->cheat_process_event.Signal();
return ResultSuccess;
return ResultSuccess();
}
#undef R_ASSERT_IF_NEW_PROCESS

View File

@@ -631,7 +631,7 @@ namespace sts::dmnt::cheat::impl {
/* However, I don't actually believe it is possible for this to happen. */
/* I guess we'll throw a fatal error here, so as to encourage me to fix the VM */
/* in the event that someone triggers it? I don't know how you'd do that. */
fatalSimple(ResultDmntCheatVmInvalidCondDepth);
R_ASSERT(ResultVirtualMachineInvalidConditionDepth());
}
}

View File

@@ -49,6 +49,12 @@ namespace sts::ams {
}
namespace sts::result {
bool CallFatalOnResultAssertion = true;
}
using namespace sts;
void __libnx_initheap(void) {

View File

@@ -41,12 +41,10 @@ namespace sts::dmnt {
Result DebugMonitorService::GetProcessHandle(sf::Out<Handle> out_hnd, os::ProcessId pid) {
R_TRY_CATCH(svcDebugActiveProcess(out_hnd.GetPointer(), static_cast<u64>(pid))) {
R_CATCH(ResultKernelAlreadyExists) {
return ResultDebugAlreadyAttached;
}
R_CONVERT(svc::ResultBusy, dbg::ResultAlreadyAttached());
} R_END_TRY_CATCH;
return ResultSuccess;
return ResultSuccess();
}
Result DebugMonitorService::WaitSynchronization(Handle hnd, u64 ns) {

View File

@@ -51,13 +51,11 @@ namespace sts::dmnt {
Result EnsureSdInitialized() {
std::scoped_lock lk(g_sd_lock);
if (g_sd_initialized) {
return ResultSuccess;
}
R_UNLESS(!g_sd_initialized, ResultSuccess());
R_TRY(fsOpenSdCardFileSystem(&g_sd_fs));
g_sd_initialized = true;
return ResultSuccess;
return ResultSuccess();
}
TargetIOFileHandle GetNewFileHandle(FsFile f) {
@@ -73,10 +71,10 @@ namespace sts::dmnt {
if (g_file_handles.find(handle) != g_file_handles.end()) {
*out = g_file_handles[handle];
return ResultSuccess;
return ResultSuccess();
}
return ResultFsInvalidArgument;
return fs::ResultInvalidArgument();
}
Result CloseFileByHandle(TargetIOFileHandle handle) {
@@ -85,10 +83,10 @@ namespace sts::dmnt {
if (g_file_handles.find(handle) != g_file_handles.end()) {
fsFileClose(&g_file_handles[handle]);
g_file_handles.erase(handle);
return ResultSuccess;
return ResultSuccess();
}
return ResultFsInvalidArgument;
return fs::ResultInvalidArgument();
}
void FixPath(char *dst, size_t dst_size, const sf::InBuffer &path) {
@@ -139,9 +137,7 @@ namespace sts::dmnt {
/* Open the file, guard to prevent failure to close. */
FsFile f;
R_TRY(fsFsOpenFile(&g_sd_fs, fs_path, open_mode, &f));
auto file_guard = SCOPE_GUARD {
fsFileClose(&f);
};
auto file_guard = SCOPE_GUARD { fsFileClose(&f); };
/* Set size if needed. */
if (create_mode == TIOCreateOption_ResetSize) {
@@ -152,7 +148,7 @@ namespace sts::dmnt {
file_guard.Cancel();
out_hnd.SetValue(GetNewFileHandle(f));
return ResultSuccess;
return ResultSuccess();
}
Result DebugMonitorService::TargetIO_FileClose(TargetIOFileHandle hnd) {
@@ -167,7 +163,7 @@ namespace sts::dmnt {
R_TRY(fsFileRead(&f, offset, out_data.GetPointer(), out_data.GetSize(), FsReadOption_None, &read));
out_read.SetValue(static_cast<u32>(read));
return ResultSuccess;
return ResultSuccess();
}
Result DebugMonitorService::TargetIO_FileWrite(TargetIOFileHandle hnd, const sf::InNonSecureBuffer &data, sf::Out<u32> out_written, u64 offset) {
@@ -177,13 +173,13 @@ namespace sts::dmnt {
R_TRY(fsFileWrite(&f, offset, data.GetPointer(), data.GetSize(), FsWriteOption_None));
out_written.SetValue(data.GetSize());
return ResultSuccess;
return ResultSuccess();
}
Result DebugMonitorService::TargetIO_FileSetAttributes(const sf::InBuffer &path, const sf::InBuffer &attributes) {
/* I don't really know why this command exists, Horizon doesn't allow you to set any attributes. */
/* N just returns ResultSuccess unconditionally here. */
return ResultSuccess;
return ResultSuccess();
}
Result DebugMonitorService::TargetIO_FileGetInformation(const sf::InBuffer &path, const sf::OutArray<u64> &out_info, sf::Out<int> is_directory) {
@@ -214,12 +210,12 @@ namespace sts::dmnt {
is_directory.SetValue(1);
}
return ResultSuccess;
return ResultSuccess();
}
Result DebugMonitorService::TargetIO_FileSetTime(const sf::InBuffer &path, u64 create, u64 access, u64 modify) {
/* This is another function that doesn't really need to exist, because Horizon doesn't let you set anything. */
return ResultSuccess;
return ResultSuccess();
}
Result DebugMonitorService::TargetIO_FileSetSize(const sf::InBuffer &input, u64 size) {
@@ -229,9 +225,7 @@ namespace sts::dmnt {
/* We will try to be better than N, here. N only treats input as a path. */
FsFile f;
if (input.GetSize() == sizeof(TargetIOFileHandle)) {
if (R_SUCCEEDED(GetFileByHandle(&f, *reinterpret_cast<const TargetIOFileHandle *>(input.GetPointer())))) {
return fsFileSetSize(&f, size);
}
R_UNLESS(R_FAILED(GetFileByHandle(&f, *reinterpret_cast<const TargetIOFileHandle *>(input.GetPointer()))), fsFileSetSize(&f, size));
}
char fs_path[FS_MAX_PATH];