summaryrefslogtreecommitdiffstats
path: root/src/core/memory
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/memory')
-rw-r--r--src/core/memory/cheat_engine.cpp25
-rw-r--r--src/core/memory/cheat_engine.h6
-rw-r--r--src/core/memory/dmnt_cheat_vm.cpp17
-rw-r--r--src/core/memory/dmnt_cheat_vm.h4
4 files changed, 32 insertions, 20 deletions
diff --git a/src/core/memory/cheat_engine.cpp b/src/core/memory/cheat_engine.cpp
index 7a8c14c2b..14d1a3840 100644
--- a/src/core/memory/cheat_engine.cpp
+++ b/src/core/memory/cheat_engine.cpp
@@ -47,12 +47,23 @@ StandardVmCallbacks::StandardVmCallbacks(System& system_, const CheatProcessMeta
StandardVmCallbacks::~StandardVmCallbacks() = default;
-void StandardVmCallbacks::MemoryRead(VAddr address, void* data, u64 size) {
- system.ApplicationMemory().ReadBlock(SanitizeAddress(address), data, size);
+void StandardVmCallbacks::MemoryReadUnsafe(VAddr address, void* data, u64 size) {
+ // Return zero on invalid address
+ if (!IsAddressInRange(address) || !system.ApplicationMemory().IsValidVirtualAddress(address)) {
+ std::memset(data, 0, size);
+ return;
+ }
+
+ system.ApplicationMemory().ReadBlock(address, data, size);
}
-void StandardVmCallbacks::MemoryWrite(VAddr address, const void* data, u64 size) {
- system.ApplicationMemory().WriteBlock(SanitizeAddress(address), data, size);
+void StandardVmCallbacks::MemoryWriteUnsafe(VAddr address, const void* data, u64 size) {
+ // Skip invalid memory write address
+ if (!IsAddressInRange(address) || !system.ApplicationMemory().IsValidVirtualAddress(address)) {
+ return;
+ }
+
+ system.ApplicationMemory().WriteBlock(address, data, size);
}
u64 StandardVmCallbacks::HidKeysDown() {
@@ -82,7 +93,7 @@ void StandardVmCallbacks::CommandLog(std::string_view data) {
data.back() == '\n' ? data.substr(0, data.size() - 1) : data);
}
-VAddr StandardVmCallbacks::SanitizeAddress(VAddr in) const {
+bool StandardVmCallbacks::IsAddressInRange(VAddr in) const {
if ((in < metadata.main_nso_extents.base ||
in >= metadata.main_nso_extents.base + metadata.main_nso_extents.size) &&
(in < metadata.heap_extents.base ||
@@ -97,10 +108,10 @@ VAddr StandardVmCallbacks::SanitizeAddress(VAddr in) const {
"the cheat may be incorrect. However, this may be normal early in execution if "
"the game has not properly set up yet.",
in);
- return 0; ///< Invalid addresses will hard crash
+ return false; ///< Invalid addresses will hard crash
}
- return in;
+ return true;
}
CheatParser::~CheatParser() = default;
diff --git a/src/core/memory/cheat_engine.h b/src/core/memory/cheat_engine.h
index 64a9c486b..619cabaa2 100644
--- a/src/core/memory/cheat_engine.h
+++ b/src/core/memory/cheat_engine.h
@@ -27,14 +27,14 @@ public:
StandardVmCallbacks(System& system_, const CheatProcessMetadata& metadata_);
~StandardVmCallbacks() override;
- void MemoryRead(VAddr address, void* data, u64 size) override;
- void MemoryWrite(VAddr address, const void* data, u64 size) override;
+ void MemoryReadUnsafe(VAddr address, void* data, u64 size) override;
+ void MemoryWriteUnsafe(VAddr address, const void* data, u64 size) override;
u64 HidKeysDown() override;
void DebugLog(u8 id, u64 value) override;
void CommandLog(std::string_view data) override;
private:
- VAddr SanitizeAddress(VAddr address) const;
+ bool IsAddressInRange(VAddr address) const;
const CheatProcessMetadata& metadata;
Core::System& system;
diff --git a/src/core/memory/dmnt_cheat_vm.cpp b/src/core/memory/dmnt_cheat_vm.cpp
index 9424e0f73..8bc81e72d 100644
--- a/src/core/memory/dmnt_cheat_vm.cpp
+++ b/src/core/memory/dmnt_cheat_vm.cpp
@@ -773,7 +773,7 @@ void DmntCheatVm::Execute(const CheatProcessMetadata& metadata) {
case 2:
case 4:
case 8:
- callbacks->MemoryWrite(dst_address, &dst_value, store_static->bit_width);
+ callbacks->MemoryWriteUnsafe(dst_address, &dst_value, store_static->bit_width);
break;
}
} else if (auto begin_cond = std::get_if<BeginConditionalOpcode>(&cur_opcode.opcode)) {
@@ -786,7 +786,7 @@ void DmntCheatVm::Execute(const CheatProcessMetadata& metadata) {
case 2:
case 4:
case 8:
- callbacks->MemoryRead(src_address, &src_value, begin_cond->bit_width);
+ callbacks->MemoryReadUnsafe(src_address, &src_value, begin_cond->bit_width);
break;
}
// Check against condition.
@@ -857,8 +857,8 @@ void DmntCheatVm::Execute(const CheatProcessMetadata& metadata) {
case 2:
case 4:
case 8:
- callbacks->MemoryRead(src_address, &registers[ldr_memory->reg_index],
- ldr_memory->bit_width);
+ callbacks->MemoryReadUnsafe(src_address, &registers[ldr_memory->reg_index],
+ ldr_memory->bit_width);
break;
}
} else if (auto str_static = std::get_if<StoreStaticToAddressOpcode>(&cur_opcode.opcode)) {
@@ -874,7 +874,7 @@ void DmntCheatVm::Execute(const CheatProcessMetadata& metadata) {
case 2:
case 4:
case 8:
- callbacks->MemoryWrite(dst_address, &dst_value, str_static->bit_width);
+ callbacks->MemoryWriteUnsafe(dst_address, &dst_value, str_static->bit_width);
break;
}
// Increment register if relevant.
@@ -1032,7 +1032,7 @@ void DmntCheatVm::Execute(const CheatProcessMetadata& metadata) {
case 2:
case 4:
case 8:
- callbacks->MemoryWrite(dst_address, &dst_value, str_register->bit_width);
+ callbacks->MemoryWriteUnsafe(dst_address, &dst_value, str_register->bit_width);
break;
}
@@ -1111,7 +1111,8 @@ void DmntCheatVm::Execute(const CheatProcessMetadata& metadata) {
case 2:
case 4:
case 8:
- callbacks->MemoryRead(cond_address, &cond_value, begin_reg_cond->bit_width);
+ callbacks->MemoryReadUnsafe(cond_address, &cond_value,
+ begin_reg_cond->bit_width);
break;
}
}
@@ -1253,7 +1254,7 @@ void DmntCheatVm::Execute(const CheatProcessMetadata& metadata) {
case 2:
case 4:
case 8:
- callbacks->MemoryRead(val_address, &log_value, debug_log->bit_width);
+ callbacks->MemoryReadUnsafe(val_address, &log_value, debug_log->bit_width);
break;
}
}
diff --git a/src/core/memory/dmnt_cheat_vm.h b/src/core/memory/dmnt_cheat_vm.h
index 32797dcd7..fed6a24ad 100644
--- a/src/core/memory/dmnt_cheat_vm.h
+++ b/src/core/memory/dmnt_cheat_vm.h
@@ -266,8 +266,8 @@ public:
public:
virtual ~Callbacks();
- virtual void MemoryRead(VAddr address, void* data, u64 size) = 0;
- virtual void MemoryWrite(VAddr address, const void* data, u64 size) = 0;
+ virtual void MemoryReadUnsafe(VAddr address, void* data, u64 size) = 0;
+ virtual void MemoryWriteUnsafe(VAddr address, const void* data, u64 size) = 0;
virtual u64 HidKeysDown() = 0;