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 5 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
14 changes: 9 additions & 5 deletions src/emulator/gba/mednafen.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{file_format::pe, signature::Signature, Address, Address32, Address64, Error, Process};
use crate::{
file_format::pe,
signature::{Signature, SignatureScanner},
Address, Address32, Address64, Error, Process,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct State {
Expand All @@ -20,7 +24,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(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 +40,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(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 +60,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(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(game, main_module_range)?;
game.read::<Address32>(ptr + 1).ok()?.into()
};

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

pub const fn new() -> Self {

Check warning on line 102 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
7 changes: 5 additions & 2 deletions src/emulator/gba/nocashgba.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{signature::Signature, Address, Address32, Process};
use crate::{
signature::{Signature, SignatureScanner},
Address, Address32, Process,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct State {
Expand All @@ -16,7 +19,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(game, main_module_range)? + 0x2)
.ok()?
.into();

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

pub const fn new() -> Self {

Check warning on line 55 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
22 changes: 13 additions & 9 deletions src/emulator/gba/retroarch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{file_format::pe, signature::Signature, Address, Address32, Address64, Process};
use crate::{
file_format::pe,
signature::{Signature, SignatureScanner},
Address, Address32, Address64, Process,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct State {
Expand Down Expand Up @@ -47,7 +51,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(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 +65,7 @@
};

let iwram_pointer = {
let ptr: Address = SIG2.scan_process_range(game, module_range)? + 3;
let ptr: Address = SIG2.scan(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 +89,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(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(game, module_range)?;
game.read::<Address32>(ptr + 1).ok()?.into()
};

Expand All @@ -114,24 +118,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(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(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(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(game, (self.core_base, module_size))? + 9;
base_addr + game.read::<i32>(offset).ok()?
};

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

pub const fn new() -> Self {

Check warning on line 149 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
26 changes: 15 additions & 11 deletions src/emulator/gba/vba.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{file_format::pe, signature::Signature, Address, Address32, Address64, Error, Process};
use crate::{
file_format::pe,
signature::{Signature, SignatureScanner},
Address, Address32, Address64, Error, Process,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct State {
Expand All @@ -25,7 +29,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(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 +43,7 @@
};

self.cached_iwram_pointer = {
let ptr: Address = SIG2.scan_process_range(game, main_module_range)? + 3;
let ptr: Address = SIG2.scan(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 +62,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(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(game, main_module_range)? + 3;
let ptr = ptr + 0x4 + game.read::<i32>(ptr).ok()?;
game.read::<Address64>(ptr).ok()?.into()
}
Expand All @@ -76,11 +80,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(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(game, main_module_range)?;
game.read::<Address32>(ptr + 1).ok()?.into()
};

Expand All @@ -91,8 +95,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(game, main_module_range)
.or_else(|| SIG_OLD.scan(game, main_module_range))?;

game.read::<Address32>(ptr + 2).ok()?.into()
};
Expand All @@ -101,15 +105,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(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(game, main_module_range)? + 2;
game.read::<Address32>(ptr).ok()?.into()
};

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

pub const fn new() -> Self {

Check warning on line 156 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
8 changes: 6 additions & 2 deletions src/emulator/genesis/blastem.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{runtime::MemoryRangeFlags, signature::Signature, Address, Address32, Endian, Process};
use crate::{
runtime::MemoryRangeFlags,
signature::{Signature, SignatureScanner},
Address, Address32, Endian, Process,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct State;
Expand All @@ -18,7 +22,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(game, m.range().ok()?))?
+ 11;

let wram = game.read::<Address32>(scanned_address).ok()?;
Expand Down
7 changes: 5 additions & 2 deletions src/emulator/genesis/fusion.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{signature::Signature, Address, Address32, Endian, Process};
use crate::{
signature::{Signature, SignatureScanner},
Address, Address32, Endian, Process,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct State {
Expand All @@ -14,7 +17,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(game, main_module)? + 1;

let addr = ptr + game.read::<u8>(ptr).ok()? as u64 + 3;
let addr = game.read::<Address32>(addr).ok()?;
Expand Down
7 changes: 5 additions & 2 deletions src/emulator/genesis/gens.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{signature::Signature, Address, Address32, Endian, Process};
use crate::{
signature::{Signature, SignatureScanner},
Address, Address32, Endian, Process,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct State;
Expand All @@ -12,7 +15,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(game, main_module)? + 11;

*endian = if game.read::<u8>(ptr + 4).ok()? == 0x86 {
Endian::Big
Expand Down
30 changes: 12 additions & 18 deletions src/emulator/genesis/retroarch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{
file_format::pe, signature::Signature, Address, Address32, Endian, MemoryRangeFlags, Process,
file_format::pe,
signature::{Signature, SignatureScanner},
Address, Address32, Endian, MemoryRangeFlags, Process,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -45,7 +47,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(game, m.range().ok()?))?
+ 11;

let wram = game.read::<Address32>(scanned_address).ok()?;
Expand All @@ -58,21 +60,17 @@ 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(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(game, (core_address, game.get_module_size(core_name).ok()?))? + 1;

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

Expand All @@ -85,21 +83,17 @@ 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(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(game, (core_address, game.get_module_size(core_name).ok()?))? + 1;

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

Expand Down
9 changes: 6 additions & 3 deletions src/emulator/genesis/segaclassics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{signature::Signature, Address, Address32, Endian, Process};
use crate::{
signature::{Signature, SignatureScanner},
Address, Address32, Endian, Process,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct State {
Expand All @@ -13,14 +16,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(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(game, main_module)? + 8
};

ptr = game.read::<Address32>(ptr).ok()?.into();
Expand Down
8 changes: 6 additions & 2 deletions src/emulator/ps1/duckstation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{file_format::pe, signature::Signature, Address, Address64, Process};
use crate::{
file_format::pe,
signature::{Signature, SignatureScanner},
Address, Address64, Process,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct State {
Expand All @@ -24,7 +28,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(game, main_module_range)? + 3;
self.addr = addr + 0x4 + game.read::<i32>(addr).ok()?;
}

Expand Down
Loading
Loading