Skip to content

Commit bc6d2d7

Browse files
committed
Image::add_files_cached() API to simplify the creation of a cached image
1 parent 69e8167 commit bc6d2d7

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _This changelog documents only changes relevant to users, internal changes might
88

99
- This [CHANGELOG](./CHANGELOG.md) 🎉
1010
- Explicit [MSRV](Cargo.toml)
11+
- New `Image::add_files_cached()` API to simplify the creation of a cached image for simple use cases.
1112

1213
### Changed
1314

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "libipt"
3-
version = "0.3.0-beta.2"
3+
version = "0.3.0-beta.3"
44
authors = [
55
"sum_catnip <catnip@catnip.fyi>",
66
"Marcondiro <cavenatimarco+libiptrs@gmail.com>",
@@ -17,6 +17,6 @@ rust-version = "1.82.0"
1717
libipt_master = ["libipt-sys/libipt_master"]
1818

1919
[dependencies]
20-
libipt-sys = { version = "0.2.1-beta.3", git = "https://github.com/sum-catnip/libipt-sys.git" }
20+
libipt-sys = { version = "0.2.1", git = "https://github.com/sum-catnip/libipt-sys.git" }
2121
bitflags = "2.4.1"
2222
num_enum = "0.7.1"

src/image/mod.rs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ impl Drop for BoxedCallback {
9797
}
9898
}
9999

100+
/// Info of a binary's section that can be used to populate an `Image`
101+
#[derive(Debug, Clone, PartialEq, Eq)]
102+
pub struct SectionInfo {
103+
/// Path of the binary
104+
pub filename: String,
105+
/// Offset of the section in the file
106+
pub offset: u64,
107+
/// Size of the section
108+
pub size: u64,
109+
/// Start virtual address of the section once loaded in memory
110+
pub virtual_address: u64,
111+
}
112+
100113
/// An Image defines the memory image that was traced as a collection
101114
/// of file sections and the virtual addresses at which those sections were loaded.
102115
#[derive(Debug)]
@@ -109,7 +122,7 @@ pub struct Image {
109122
callback: Option<BoxedCallback>,
110123
caches: Vec<Rc<SectionCache>>,
111124
// `HashSet` might grow and move the content around, we cannot use `Asid` directly since we
112-
// share a pointer with libipt and it must be valid for the entire Image (section) filetime.
125+
// share a pointer with libipt, and it must be valid for the entire Image (section) lifetime.
113126
asids: HashSet<Rc<Asid>>,
114127
}
115128

@@ -321,6 +334,31 @@ impl Image {
321334
})?;
322335
Ok(())
323336
}
337+
338+
/// Add multiple file sections to the traced memory image, backed by a cache.
339+
///
340+
/// This is the same as creating a `SectionCache` and subsequently calling `add_cached()` for
341+
/// each section.
342+
pub fn add_files_cached(
343+
&mut self,
344+
sections_info: &[SectionInfo],
345+
asid: Option<&Asid>,
346+
) -> Result<(), PtError> {
347+
let mut image_cache = SectionCache::new(None)?;
348+
349+
let mut isids = Vec::with_capacity(sections_info.len());
350+
for s in sections_info {
351+
let isid = image_cache.add_file(&s.filename, s.offset, s.size, s.virtual_address)?;
352+
isids.push(isid);
353+
}
354+
355+
let rc_cache = Rc::new(image_cache);
356+
for isid in isids {
357+
self.add_cached(rc_cache.clone(), isid, asid)?;
358+
}
359+
360+
Ok(())
361+
}
324362
}
325363

326364
impl Drop for Image {
@@ -511,4 +549,22 @@ mod test {
511549
assert_eq!(img.remove_by_asid(&Asid::new(Some(i), Some(i))).unwrap(), 1);
512550
}
513551
}
552+
553+
#[test]
554+
fn img_add_files_cached() {
555+
let file: PathBuf = [env!("CARGO_MANIFEST_DIR"), "testfiles", "garbage.txt"]
556+
.iter()
557+
.collect();
558+
559+
let section = SectionInfo {
560+
filename: file.to_string_lossy().to_string(),
561+
offset: 5,
562+
size: 15,
563+
virtual_address: 0x1337,
564+
};
565+
let mut i = img_with_file();
566+
let asid = Asid::new(Some(3), Some(4));
567+
i.add_files_cached(&[section], Some(&asid)).unwrap();
568+
assert_eq!(i.remove_by_asid(&asid).unwrap(), 1);
569+
}
514570
}

0 commit comments

Comments
 (0)