Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterating through addresses matching a signature #103

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/emulator/gba/mednafen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
if self.is_64_bit {
self.cached_ewram_pointer = {
const SIG: Signature<13> = Signature::new("48 8B 05 ?? ?? ?? ?? 81 E1 FF FF 03 00");
let ptr: Address = SIG.scan_process_range(game, main_module_range)? + 3;
let ptr: Address = SIG.scan_once(game, main_module_range)? + 3;
let mut addr: Address = ptr + 0x4 + game.read::<i32>(ptr).ok()?;

if game.read::<u8>(ptr + 10).ok()? == 0x48 {
Expand All @@ -36,7 +36,7 @@
self.cached_iwram_pointer = {
const SIG2: Signature<13> =
Signature::new("48 8B 05 ?? ?? ?? ?? 81 E1 FF 7F 00 00");
let ptr: Address = SIG2.scan_process_range(game, main_module_range)? + 3;
let ptr: Address = SIG2.scan_once(game, main_module_range)? + 3;
let mut addr: Address = ptr + 0x4 + game.read::<i32>(ptr).ok()?;

if game.read::<u8>(ptr + 10).ok()? == 0x48 {
Expand All @@ -56,13 +56,13 @@
} else {
self.cached_ewram_pointer = {
const SIG: Signature<11> = Signature::new("A1 ?? ?? ?? ?? 81 ?? FF FF 03 00");
let ptr = SIG.scan_process_range(game, main_module_range)?;
let ptr = SIG.scan_once(game, main_module_range)?;
game.read::<Address32>(ptr + 1).ok()?.into()
};

self.cached_iwram_pointer = {
const SIG2: Signature<11> = Signature::new("A1 ?? ?? ?? ?? 81 ?? FF 7F 00 00");
let ptr = SIG2.scan_process_range(game, main_module_range)?;
let ptr = SIG2.scan_once(game, main_module_range)?;
game.read::<Address32>(ptr + 1).ok()?.into()
};

Expand Down Expand Up @@ -95,7 +95,7 @@
true
}

pub const fn new() -> Self {

Check warning on line 98 in src/emulator/gba/mednafen.rs

View workflow job for this annotation

GitHub Actions / Check clippy lints

you should consider adding a `Default` implementation for `State`
Self {
cached_ewram_pointer: Address::NULL,
cached_iwram_pointer: Address::NULL,
Expand Down
2 changes: 1 addition & 1 deletion src/emulator/gba/nocashgba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
.find_map(|(name, _)| game.get_module_range(name).ok())?;

self.base_addr = game
.read::<Address32>(SIG.scan_process_range(game, main_module_range)? + 0x2)
.read::<Address32>(SIG.scan_once(game, main_module_range)? + 0x2)
.ok()?
.into();

Expand Down Expand Up @@ -49,7 +49,7 @@
true
}

pub const fn new() -> Self {

Check warning on line 52 in src/emulator/gba/nocashgba.rs

View workflow job for this annotation

GitHub Actions / Check clippy lints

you should consider adding a `Default` implementation for `State`
Self {
base_addr: Address::NULL,
}
Expand Down
16 changes: 8 additions & 8 deletions src/emulator/gba/retroarch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
const SIG2: Signature<13> = Signature::new("48 8B 05 ?? ?? ?? ?? 81 E1 FF 7F 00 00");

let ewram_pointer = {
let ptr: Address = SIG.scan_process_range(game, module_range)? + 3;
let ptr: Address = SIG.scan_once(game, module_range)? + 3;
let mut addr: Address = ptr + 0x4 + game.read::<i32>(ptr).ok()?;

if game.read::<u8>(ptr + 10).ok()? == 0x48 {
Expand All @@ -61,7 +61,7 @@
};

let iwram_pointer = {
let ptr: Address = SIG2.scan_process_range(game, module_range)? + 3;
let ptr: Address = SIG2.scan_once(game, module_range)? + 3;
let mut addr: Address = ptr + 0x4 + game.read::<i32>(ptr).ok()?;

if game.read::<u8>(ptr + 10).ok()? == 0x48 {
Expand All @@ -85,12 +85,12 @@
} else {
let ewram_pointer: Address = {
const SIG: Signature<11> = Signature::new("A1 ?? ?? ?? ?? 81 ?? FF FF 03 00");
let ptr = SIG.scan_process_range(game, module_range)?;
let ptr = SIG.scan_once(game, module_range)?;
game.read::<Address32>(ptr + 1).ok()?.into()
};
let iwram_pointer: Address = {
const SIG2: Signature<11> = Signature::new("A1 ?? ?? ?? ?? 81 ?? FF 7F 00 00");
let ptr = SIG2.scan_process_range(game, module_range)?;
let ptr = SIG2.scan_once(game, module_range)?;
game.read::<Address32>(ptr + 1).ok()?.into()
};

Expand All @@ -114,24 +114,24 @@
let base_addr: Address = match is_64_bit {
true => {
const SIG: Signature<10> = Signature::new("48 8B 15 ?? ?? ?? ?? 8B 42 40");
let ptr = SIG.scan_process_range(game, (self.core_base, module_size))? + 3;
let ptr = SIG.scan_once(game, (self.core_base, module_size))? + 3;
let ptr: Address = ptr + 0x4 + game.read::<i32>(ptr).ok()?;
game.read::<Address64>(ptr).ok()?.into()
}
false => {
const SIG: Signature<11> = Signature::new("A3 ?? ?? ?? ?? F7 C5 02 00 00 00");
let ptr = SIG.scan_process_range(game, (self.core_base, module_size))? + 1;
let ptr = SIG.scan_once(game, (self.core_base, module_size))? + 1;
game.read::<Address32>(ptr).ok()?.into()
}
};

let ewram = {
let offset = SIG_EWRAM.scan_process_range(game, (self.core_base, module_size))? + 8;
let offset = SIG_EWRAM.scan_once(game, (self.core_base, module_size))? + 8;
base_addr + game.read::<i32>(offset).ok()?
};

let iwram = {
let offset = SIG_IWRAM.scan_process_range(game, (self.core_base, module_size))? + 9;
let offset = SIG_IWRAM.scan_once(game, (self.core_base, module_size))? + 9;
base_addr + game.read::<i32>(offset).ok()?
};

Expand All @@ -142,7 +142,7 @@
game.read::<u8>(self.core_base).is_ok()
}

pub const fn new() -> Self {

Check warning on line 145 in src/emulator/gba/retroarch.rs

View workflow job for this annotation

GitHub Actions / Check clippy lints

you should consider adding a `Default` implementation for `State`
Self {
core_base: Address::NULL,
}
Expand Down
20 changes: 10 additions & 10 deletions src/emulator/gba/vba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
const SIG2: Signature<13> = Signature::new("48 8B 05 ?? ?? ?? ?? 81 E3 FF 7F 00 00");

self.cached_ewram_pointer = {
let ptr: Address = SIG.scan_process_range(game, main_module_range)? + 3;
let ptr: Address = SIG.scan_once(game, main_module_range)? + 3;
let mut addr: Address = ptr + 0x4 + game.read::<i32>(ptr).ok()?;

if game.read::<u8>(ptr + 10).ok()? == 0x48 {
Expand All @@ -39,7 +39,7 @@
};

self.cached_iwram_pointer = {
let ptr: Address = SIG2.scan_process_range(game, main_module_range)? + 3;
let ptr: Address = SIG2.scan_once(game, main_module_range)? + 3;
let mut addr: Address = ptr + 0x4 + game.read::<i32>(ptr).ok()?;

if game.read::<u8>(ptr + 10).ok()? == 0x48 {
Expand All @@ -58,11 +58,11 @@
const SIG_RUNNING2: Signature<16> =
Signature::new("48 8B 15 ?? ?? ?? ?? 31 C0 8B 12 85 D2 74 ?? 48");

if let Some(ptr) = SIG_RUNNING.scan_process_range(game, main_module_range) {
if let Some(ptr) = SIG_RUNNING.scan_once(game, main_module_range) {
let ptr = ptr + 2;
ptr + 0x4 + game.read::<i32>(ptr).ok()? + 0x1
} else {
let ptr = SIG_RUNNING2.scan_process_range(game, main_module_range)? + 3;
let ptr = SIG_RUNNING2.scan_once(game, main_module_range)? + 3;
let ptr = ptr + 0x4 + game.read::<i32>(ptr).ok()?;
game.read::<Address64>(ptr).ok()?.into()
}
Expand All @@ -76,11 +76,11 @@
const SIG: Signature<11> = Signature::new("A1 ?? ?? ?? ?? 81 ?? FF FF 03 00");
const SIG_OLD: Signature<12> = Signature::new("81 E6 FF FF 03 00 8B 15 ?? ?? ?? ??");

if let Some(ptr) = SIG.scan_process_range(game, main_module_range) {
if let Some(ptr) = SIG.scan_once(game, main_module_range) {
self.cached_ewram_pointer = game.read::<Address32>(ptr + 1).ok()?.into();
self.cached_iwram_pointer = {
const SIG2: Signature<11> = Signature::new("A1 ?? ?? ?? ?? 81 ?? FF 7F 00 00");
let ptr = SIG2.scan_process_range(game, main_module_range)?;
let ptr = SIG2.scan_once(game, main_module_range)?;
game.read::<Address32>(ptr + 1).ok()?.into()
};

Expand All @@ -91,8 +91,8 @@
Signature::new("8B 15 ?? ?? ?? ?? 31 C0 85 D2 74 ?? 0F");

let ptr = SIG
.scan_process_range(game, main_module_range)
.or_else(|| SIG_OLD.scan_process_range(game, main_module_range))?;
.scan_once(game, main_module_range)
.or_else(|| SIG_OLD.scan_once(game, main_module_range))?;

game.read::<Address32>(ptr + 2).ok()?.into()
};
Expand All @@ -101,15 +101,15 @@
let iwram = game.read::<Address32>(self.cached_iwram_pointer).ok()?;

Some([ewram.into(), iwram.into()])
} else if let Some(ptr) = SIG_OLD.scan_process_range(game, main_module_range) {
} else if let Some(ptr) = SIG_OLD.scan_once(game, main_module_range) {
// This code is for very old versions of VisualBoyAdvance (1.8.0-beta 3)
self.cached_ewram_pointer = game.read::<Address32>(ptr + 8).ok()?.into();
self.cached_iwram_pointer = self.cached_ewram_pointer.add_signed(0x4);

self.is_emulating = {
const SIG_RUNNING: Signature<11> =
Signature::new("8B 0D ?? ?? ?? ?? 85 C9 74 ?? 8A");
let ptr = SIG_RUNNING.scan_process_range(game, main_module_range)? + 2;
let ptr = SIG_RUNNING.scan_once(game, main_module_range)? + 2;
game.read::<Address32>(ptr).ok()?.into()
};

Expand Down Expand Up @@ -149,7 +149,7 @@
true
}

pub const fn new() -> Self {

Check warning on line 152 in src/emulator/gba/vba.rs

View workflow job for this annotation

GitHub Actions / Check clippy lints

you should consider adding a `Default` implementation for `State`
Self {
cached_ewram_pointer: Address::NULL,
cached_iwram_pointer: Address::NULL,
Expand Down
2 changes: 1 addition & 1 deletion src/emulator/genesis/blastem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl State {
.contains(MemoryRangeFlags::WRITE)
&& m.size().unwrap_or_default() == 0x101000
})
.find_map(|m| SIG.scan_process_range(game, m.range().ok()?))?
.find_map(|m| SIG.scan_once(game, m.range().ok()?))?
+ 11;

let wram = game.read::<Address32>(scanned_address).ok()?;
Expand Down
2 changes: 1 addition & 1 deletion src/emulator/genesis/fusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl State {
.filter(|(_, state)| matches!(state, super::State::Fusion(_)))
.find_map(|(name, _)| game.get_module_range(name).ok())?;

let ptr = SIG.scan_process_range(game, main_module)? + 1;
let ptr = SIG.scan_once(game, main_module)? + 1;

let addr = ptr + game.read::<u8>(ptr).ok()? as u64 + 3;
let addr = game.read::<Address32>(addr).ok()?;
Expand Down
2 changes: 1 addition & 1 deletion src/emulator/genesis/gens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl State {
.filter(|(_, state)| matches!(state, super::State::Gens(_)))
.find_map(|(name, _)| game.get_module_range(name).ok())?;

let ptr = SIG.scan_process_range(game, main_module)? + 11;
let ptr = SIG.scan_once(game, main_module)? + 11;

*endian = if game.read::<u8>(ptr + 4).ok()? == 0x86 {
Endian::Big
Expand Down
30 changes: 13 additions & 17 deletions src/emulator/genesis/retroarch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl State {
.contains(MemoryRangeFlags::WRITE)
&& m.size().unwrap_or_default() == 0x101000
})
.find_map(|m| SIG.scan_process_range(game, m.range().ok()?))?
.find_map(|m| SIG.scan_once(game, m.range().ok()?))?
+ 11;

let wram = game.read::<Address32>(scanned_address).ok()?;
Expand All @@ -58,21 +58,19 @@ impl State {
if is_x86_64 {
const SIG_64: Signature<10> = Signature::new("48 8D 0D ?? ?? ?? ?? 4C 8B 2D");

let addr = SIG_64.scan_process_range(
game,
(core_address, game.get_module_size(core_name).ok()?),
)? + 3;
let addr = SIG_64
.scan_once(game, (core_address, game.get_module_size(core_name).ok()?))?
+ 3;

let wram = addr + 0x4 + game.read::<i32>(addr).ok()?;

Some(wram)
} else {
const SIG_32: Signature<7> = Signature::new("A3 ?? ?? ?? ?? 29 F9");

let ptr = SIG_32.scan_process_range(
game,
(core_address, game.get_module_size(core_name).ok()?),
)? + 1;
let ptr = SIG_32
.scan_once(game, (core_address, game.get_module_size(core_name).ok()?))?
+ 1;

let wram = game.read::<Address32>(ptr).ok()?;

Expand All @@ -85,21 +83,19 @@ impl State {
if is_x86_64 {
const SIG_64: Signature<9> = Signature::new("48 8D 0D ?? ?? ?? ?? 41 B8");

let addr = SIG_64.scan_process_range(
game,
(core_address, game.get_module_size(core_name).ok()?),
)? + 3;
let addr = SIG_64
.scan_once(game, (core_address, game.get_module_size(core_name).ok()?))?
+ 3;

let wram = addr + 0x4 + game.read::<i32>(addr).ok()?;

Some(wram)
} else {
const SIG_32: Signature<8> = Signature::new("B9 ?? ?? ?? ?? C1 EF 10");

let ptr = SIG_32.scan_process_range(
game,
(core_address, game.get_module_size(core_name).ok()?),
)? + 1;
let ptr = SIG_32
.scan_once(game, (core_address, game.get_module_size(core_name).ok()?))?
+ 1;

let wram = game.read::<Address32>(ptr).ok()?;

Expand Down
4 changes: 2 additions & 2 deletions src/emulator/genesis/segaclassics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ impl State {
const GENESISWRAPPERDLL: &str = "GenesisEmuWrapper.dll";

let mut ptr = if let Ok(module) = game.get_module_range(GENESISWRAPPERDLL) {
SIG_GAMEROOM.scan_process_range(game, module)? + 2
SIG_GAMEROOM.scan_once(game, module)? + 2
} else {
let main_module = super::PROCESS_NAMES
.iter()
.filter(|(_, state)| matches!(state, super::State::SegaClassics(_)))
.find_map(|(name, _)| game.get_module_range(name).ok())?;

SIG_SEGACLASSICS.scan_process_range(game, main_module)? + 8
SIG_SEGACLASSICS.scan_once(game, main_module)? + 8
};

ptr = game.read::<Address32>(ptr).ok()?.into();
Expand Down
2 changes: 1 addition & 1 deletion src/emulator/ps1/duckstation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl State {
self.addr = debug_symbol.address;
} else {
// For older versions of Duckstation, we fall back to regular sigscanning
let addr = SIG.scan_process_range(game, main_module_range)? + 3;
let addr = SIG.scan_once(game, main_module_range)? + 3;
self.addr = addr + 0x4 + game.read::<i32>(addr).ok()?;
}

Expand Down
2 changes: 1 addition & 1 deletion src/emulator/ps1/epsxe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl State {
.filter(|(_, state)| matches!(state, super::State::Epsxe(_)))
.find_map(|(name, _)| game.get_module_range(name).ok())?;

let ptr = SIG.scan_process_range(game, main_module_range)? + 5;
let ptr = SIG.scan_once(game, main_module_range)? + 5;

Some(game.read::<Address32>(ptr).ok()?.into())
}
Expand Down
4 changes: 2 additions & 2 deletions src/emulator/ps1/mednafen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ impl State {
pe::MachineType::read(game, main_module_range.0) == Some(pe::MachineType::X86_64);

let ptr = match is_64_bit {
true => SIG_64.scan_process_range(game, main_module_range)?,
false => SIG_32.scan_process_range(game, main_module_range)?,
true => SIG_64.scan_once(game, main_module_range)?,
false => SIG_32.scan_once(game, main_module_range)?,
} + 0x5;

Some(game.read::<Address32>(ptr).ok()?.into())
Expand Down
6 changes: 3 additions & 3 deletions src/emulator/ps1/pcsx_redux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ impl State {
);
const SIG_OFFSET: Signature<9> = Signature::new("89 D1 C1 E9 10 48 8B ?? ??");

self.addr_base = SIG_BASE.scan_process_range(game, main_module_range)? + 2;
self.addr_base = SIG_BASE.scan_once(game, main_module_range)? + 2;
self.addr = game.read::<Address64>(self.addr_base).ok()?.into();

let offset = SIG_OFFSET.scan_process_range(game, main_module_range)? + 8;
let offset = SIG_OFFSET.scan_once(game, main_module_range)? + 8;
let offset = game.read::<u8>(offset).ok()? as u64;

let addr = game.read::<Address64>(self.addr + offset).ok()?;
Expand All @@ -45,7 +45,7 @@ impl State {
.unwrap_or_default()
.contains(MemoryRangeFlags::WRITE)
})
.find_map(|m| SIG.scan_process_range(game, m.range().ok()?))?
.find_map(|m| SIG.scan_once(game, m.range().ok()?))?
+ 2;

self.addr = game.read::<Address32>(self.addr_base).ok()?.into();
Expand Down
9 changes: 4 additions & 5 deletions src/emulator/ps1/psxfin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ impl State {
.filter(|(_, state)| matches!(state, super::State::PsxFin(_)))
.find_map(|(name, _)| game.get_module_range(name).ok())?;

let mut ptr: Address32 = if let Some(sig) = SIG.scan_process_range(game, main_module_range)
{
let mut ptr: Address32 = if let Some(sig) = SIG.scan_once(game, main_module_range) {
game.read(sig + 2).ok()?
} else if let Some(sig) = SIG_0.scan_process_range(game, main_module_range) {
} else if let Some(sig) = SIG_0.scan_once(game, main_module_range) {
game.read(sig + 1).ok()?
} else if let Some(sig) = SIG_1.scan_process_range(game, main_module_range) {
} else if let Some(sig) = SIG_1.scan_once(game, main_module_range) {
game.read(sig + 1).ok()?
} else if let Some(sig) = SIG_2.scan_process_range(game, main_module_range) {
} else if let Some(sig) = SIG_2.scan_once(game, main_module_range) {
game.read(sig + 1).ok()?
} else {
return None;
Expand Down
Loading
Loading