Skip to content
Merged
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
71 changes: 71 additions & 0 deletions src/ext/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ pub trait SearchExt {
fn search_i8(&self, value: i8, byte_order: Option<ByteOrder>) -> SearchExtResult<Vec<usize>>;
fn search_i16(&self, value: i16, byte_order: Option<ByteOrder>) -> SearchExtResult<Vec<usize>>;
fn search_i32(&self, value: i32, byte_order: Option<ByteOrder>) -> SearchExtResult<Vec<usize>>;
#[cfg(target_pointer_width = "64")]
fn search_i64(&self, value: i64, byte_order: Option<ByteOrder>) -> SearchExtResult<Vec<usize>>;
fn search_f32(&self, value: f32, byte_order: Option<ByteOrder>) -> SearchExtResult<Vec<usize>>;
#[cfg(target_pointer_width = "64")]
fn search_f64(&self, value: f64, byte_order: Option<ByteOrder>) -> SearchExtResult<Vec<usize>>;
}

#[cfg(target_os = "linux")]
Expand Down Expand Up @@ -220,6 +224,27 @@ impl SearchExt for crate::prelude::Process {
}),
)
}

fn search_f32(&self, value: f32, byte_order: Option<ByteOrder>) -> SearchExtResult<Vec<usize>> {
self.search_bytes(
&(match byte_order {
Some(ByteOrder::BigEndian) => value.to_be_bytes(),
Some(ByteOrder::LittleEndian) => value.to_le_bytes(),
None => value.to_ne_bytes(),
}),
)
}

#[cfg(target_pointer_width = "64")]
fn search_f64(&self, value: f64, byte_order: Option<ByteOrder>) -> SearchExtResult<Vec<usize>> {
self.search_bytes(
&(match byte_order {
Some(ByteOrder::BigEndian) => value.to_be_bytes(),
Some(ByteOrder::LittleEndian) => value.to_le_bytes(),
None => value.to_ne_bytes(),
}),
)
}
}

#[derive(Debug, Error)]
Expand Down Expand Up @@ -468,6 +493,7 @@ mod linux_tests {
assert_contains!(addresses, &target_address);
}

#[cfg(target_pointer_width = "64")]
#[rstest]
fn test_search_i64(dummy_process: DummyProcess) {
let process =
Expand All @@ -489,4 +515,49 @@ mod linux_tests {

assert_contains!(addresses, &target_address);
}

#[rstest]
fn test_search_f32(dummy_process: DummyProcess) {
let process =
Process::try_new(dummy_process.pid).expect("Could not get dummy process with id.");
let addresses = process
.search_f32(1.5, None)
.expect("Could not search f32 value.");

let target_address: usize = {
let response =
reqwest::blocking::get(format!("{}/f32val/address", dummy_process.base_url))
.expect("Could not request f32val address.");
response
.text()
.expect("Could not decode the charset of f32val address.")
.parse()
}
.expect("Could not parse address usize.");

assert_contains!(addresses, &target_address);
}

#[cfg(target_pointer_width = "64")]
#[rstest]
fn test_search_f64(dummy_process: DummyProcess) {
let process =
Process::try_new(dummy_process.pid).expect("Could not get dummy process with id.");
let addresses = process
.search_f64(2.5, None)
.expect("Could not search f64 value.");

let target_address: usize = {
let response =
reqwest::blocking::get(format!("{}/f64val/address", dummy_process.base_url))
.expect("Could not request f64val address.");
response
.text()
.expect("Could not decode the charset of f64val address.")
.parse()
}
.expect("Could not parse address usize.");

assert_contains!(addresses, &target_address);
}
}