summaryrefslogtreecommitdiff
path: root/vendor/object/tests/read
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-07-02 18:36:06 -0600
committermo khan <mo@mokhan.ca>2025-07-02 18:36:06 -0600
commit8cdfa445d6629ffef4cb84967ff7017654045bc2 (patch)
tree22f0b0907c024c78d26a731e2e1f5219407d8102 /vendor/object/tests/read
parent4351c74c7c5f97156bc94d3a8549b9940ac80e3f (diff)
chore: add vendor directory
Diffstat (limited to 'vendor/object/tests/read')
-rw-r--r--vendor/object/tests/read/coff.rs23
-rw-r--r--vendor/object/tests/read/elf.rs47
-rw-r--r--vendor/object/tests/read/macho.rs49
-rw-r--r--vendor/object/tests/read/mod.rs5
4 files changed, 124 insertions, 0 deletions
diff --git a/vendor/object/tests/read/coff.rs b/vendor/object/tests/read/coff.rs
new file mode 100644
index 00000000..959e317a
--- /dev/null
+++ b/vendor/object/tests/read/coff.rs
@@ -0,0 +1,23 @@
+use object::{pe, read, Object, ObjectSection};
+use std::fs;
+use std::path::PathBuf;
+
+#[cfg(feature = "coff")]
+#[test]
+fn coff_extended_relocations() {
+ let path_to_obj: PathBuf = ["testfiles", "coff", "relocs_overflow.o"].iter().collect();
+ let contents = fs::read(path_to_obj).expect("Could not read relocs_overflow.o");
+ let file =
+ read::coff::CoffFile::<_>::parse(&contents[..]).expect("Could not parse relocs_overflow.o");
+ let code_section = file
+ .section_by_name(".text")
+ .expect("Could not find .text section in relocs_overflow.o");
+ match code_section.flags() {
+ object::SectionFlags::Coff { characteristics } => {
+ assert!(characteristics & pe::IMAGE_SCN_LNK_NRELOC_OVFL != 0)
+ }
+ _ => panic!("Invalid section flags flavour."),
+ };
+ let relocations = code_section.relocations().collect::<Vec<_>>();
+ assert_eq!(relocations.len(), 65536);
+}
diff --git a/vendor/object/tests/read/elf.rs b/vendor/object/tests/read/elf.rs
new file mode 100644
index 00000000..e42cd516
--- /dev/null
+++ b/vendor/object/tests/read/elf.rs
@@ -0,0 +1,47 @@
+#[cfg(feature = "std")]
+use std::path::{Path, PathBuf};
+
+#[cfg(feature = "std")]
+fn get_buildid(path: &Path) -> Result<Option<Vec<u8>>, object::read::Error> {
+ use object::Object;
+ let file = std::fs::File::open(path).unwrap();
+ let reader = object::read::ReadCache::new(file);
+ let object = object::read::File::parse(&reader)?;
+ object
+ .build_id()
+ .map(|option| option.map(ToOwned::to_owned))
+}
+
+#[cfg(feature = "std")]
+#[test]
+/// Regression test: used to attempt to allocate 5644418395173552131 bytes
+fn get_buildid_bad_elf() {
+ let path: PathBuf = [
+ "testfiles",
+ "elf",
+ "yara-fuzzing",
+ "crash-7dc27920ae1cb85333e7f2735a45014488134673",
+ ]
+ .iter()
+ .collect();
+ let _ = get_buildid(&path);
+}
+
+#[cfg(feature = "std")]
+#[test]
+fn get_buildid_less_bad_elf() {
+ let path: PathBuf = [
+ "testfiles",
+ "elf",
+ "yara-fuzzing",
+ "crash-f1fd008da535b110853885221ebfaac3f262a1c1e280f10929f7b353c44996c8",
+ ]
+ .iter()
+ .collect();
+ let buildid = get_buildid(&path).unwrap().unwrap();
+ // ground truth obtained from GNU binutils's readelf
+ assert_eq!(
+ buildid,
+ b"\xf9\xc0\xc6\x05\xd3\x76\xbb\xa5\x7e\x02\xf5\x74\x50\x9d\x16\xcc\xe9\x9c\x1b\xf1"
+ );
+}
diff --git a/vendor/object/tests/read/macho.rs b/vendor/object/tests/read/macho.rs
new file mode 100644
index 00000000..59f314be
--- /dev/null
+++ b/vendor/object/tests/read/macho.rs
@@ -0,0 +1,49 @@
+#[cfg(feature = "std")]
+use object::{Object, ObjectSection as _};
+
+// Test that we can read compressed sections in Mach-O files as produced
+// by the Go compiler.
+#[cfg(feature = "std")]
+#[test]
+fn test_go_macho() {
+ let macho_testfiles = std::path::Path::new("testfiles/macho");
+
+ // Section names we expect to find, whether they should be
+ // compressed, and the actual name of the section in the file.
+ const EXPECTED: &[(&str, bool, &str)] = &[
+ (".debug_abbrev", true, "__zdebug_abbrev"),
+ (".debug_gdb_scripts", false, "__debug_gdb_scri"),
+ (".debug_ranges", true, "__zdebug_ranges"),
+ ("__data", false, "__data"),
+ ];
+
+ for file in &["go-aarch64", "go-x86_64"] {
+ let path = macho_testfiles.join(file);
+ let file = std::fs::File::open(path).unwrap();
+ let reader = object::read::ReadCache::new(file);
+ let object = object::read::File::parse(&reader).unwrap();
+ for &(name, compressed, actual_name) in EXPECTED {
+ let section = object.section_by_name(name).unwrap();
+ assert_eq!(section.name(), Ok(actual_name));
+ let compressed_file_range = section.compressed_file_range().unwrap();
+ let size = section.size();
+ if compressed {
+ assert_eq!(
+ compressed_file_range.format,
+ object::CompressionFormat::Zlib
+ );
+ assert_eq!(compressed_file_range.compressed_size, size - 12);
+ assert!(
+ compressed_file_range.uncompressed_size > compressed_file_range.compressed_size,
+ "decompressed size is greater than compressed size"
+ );
+ } else {
+ assert_eq!(
+ compressed_file_range.format,
+ object::CompressionFormat::None
+ );
+ assert_eq!(compressed_file_range.compressed_size, size);
+ }
+ }
+ }
+}
diff --git a/vendor/object/tests/read/mod.rs b/vendor/object/tests/read/mod.rs
new file mode 100644
index 00000000..48e005ee
--- /dev/null
+++ b/vendor/object/tests/read/mod.rs
@@ -0,0 +1,5 @@
+#![cfg(feature = "read")]
+
+mod coff;
+mod elf;
+mod macho;