Debian Codegraph @

third_party/rust/object/tests/read/elf.rs

raw
1 #[cfg(feature = "std")]
2 use std::path::{Path, PathBuf};
3
4 #[cfg(feature = "std")]
5 fn get_buildid(path: &Path) -> Result<Option<Vec<u8>>, object::read::Error> {
6 use object::Object;
7 let file = std::fs::File::open(path).unwrap();
8 let reader = object::read::ReadCache::new(file);
9 let object = object::read::File::parse(&reader)?;
10 object
11 .build_id()
12 .map(|option| option.map(ToOwned::to_owned))
13 }
14
15 #[cfg(feature = "std")]
16 #[test]
17 /// Regression test: used to attempt to allocate 5644418395173552131 bytes
18 fn get_buildid_bad_elf() {
19 let path: PathBuf = [
20 "testfiles",
21 "elf",
22 "yara-fuzzing",
23 "crash-7dc27920ae1cb85333e7f2735a45014488134673",
24 ]
25 .iter()
26 .collect();
27 let _ = get_buildid(&path);
28 }
29
30 #[cfg(feature = "std")]
31 #[test]
32 fn get_buildid_less_bad_elf() {
33 let path: PathBuf = [
34 "testfiles",
35 "elf",
36 "yara-fuzzing",
37 "crash-f1fd008da535b110853885221ebfaac3f262a1c1e280f10929f7b353c44996c8",
38 ]
39 .iter()
40 .collect();
41 let buildid = get_buildid(&path).unwrap().unwrap();
42 // ground truth obtained from GNU binutils's readelf
43 assert_eq!(
44 buildid,
45 b"\xf9\xc0\xc6\x05\xd3\x76\xbb\xa5\x7e\x02\xf5\x74\x50\x9d\x16\xcc\xe9\x9c\x1b\xf1"
46 );
47 }