1#[allow(
25 unused_imports,
26 clippy::wildcard_imports,
27 reason = "Standard workspace crate prelude"
28)]
29pub(crate) use ctb_utilities::*;
30
31use iced_x86::{Code, Decoder, DecoderOptions};
32use object::read::archive::ArchiveFile;
33use object::{
34 Architecture, File, FileKind, Object, ObjectSection, SectionKind,
35};
36use std::collections::BTreeSet;
37
38pub fn extract_instruction_sets(data: &[u8]) -> Result<Vec<String>> {
46 let mut features = BTreeSet::new();
47 process_data(data, &mut features)?;
48 Ok(features.into_iter().collect())
49}
50
51fn process_data(data: &[u8], features: &mut BTreeSet<String>) -> Result<()> {
52 let kind = FileKind::parse(data).context("Failed to parse file format")?;
53
54 if kind == FileKind::Archive {
55 let archive = ArchiveFile::parse(data)
56 .context("Failed to parse archive header")?;
57 for member in archive.members().flatten() {
58 if let Ok(member_data) = member.data(data) {
59 let _ = process_data(member_data, features);
60 }
61 }
62 } else {
63 let file = File::parse(data).context("Failed to parse object file")?;
64 let bitness = match file.architecture() {
65 Architecture::X86_64 | Architecture::X86_64_X32 => 64_u32,
66 Architecture::I386 => 32_u32,
67 _ => return Ok(()),
68 };
69
70 for section in file.sections() {
71 if section.kind() == SectionKind::Text {
72 if let Ok(section_data) = section.uncompressed_data() {
73 let decoder = Decoder::new(
74 bitness,
75 §ion_data,
76 DecoderOptions::NONE,
77 );
78 for instr in decoder {
79 if instr.code() != Code::INVALID {
80 for cpuid in instr.cpuid_features() {
81 features.insert(format!("{cpuid:?}"));
82 }
83 }
84 }
85 }
86 }
87 }
88 }
89
90 Ok(())
91}
92
93#[cfg(test)]
94#[allow(
95 clippy::panic,
96 clippy::expect_used,
97 clippy::unwrap_used,
98 clippy::unwrap_in_result,
99 clippy::panic_in_result_fn,
100 clippy::indexing_slicing,
101 clippy::arithmetic_side_effects,
102 reason = "Standard repository test boilerplate"
103)]
104mod tests {
105 use super::*;
106
107 fn create_sample_elf() -> Vec<u8> {
108 let mut elf = Vec::new();
109 elf.extend_from_slice(&[
112 0x7f, b'E', b'L', b'F', 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
113 ]);
114 elf.extend_from_slice(&[1, 0]);
116 elf.extend_from_slice(&[62, 0]);
118 elf.extend_from_slice(&[1, 0, 0, 0]);
120 elf.extend_from_slice(&[0; 8]);
122 elf.extend_from_slice(&[0; 8]);
124 elf.extend_from_slice(&[97, 0, 0, 0, 0, 0, 0, 0]);
126 elf.extend_from_slice(&[0, 0, 0, 0]);
128 elf.extend_from_slice(&[64, 0]);
130 elf.extend_from_slice(&[0, 0]);
132 elf.extend_from_slice(&[0, 0]);
134 elf.extend_from_slice(&[64, 0]);
136 elf.extend_from_slice(&[3, 0]);
138 elf.extend_from_slice(&[2, 0]);
140
141 elf.extend_from_slice(&[0x66, 0x0f, 0x58, 0xc1]);
144 elf.extend_from_slice(&[0xc5, 0xf9, 0x58, 0xc1]);
146 elf.extend_from_slice(&[0xc5, 0xdd, 0xfe, 0xc2]);
148 elf.extend_from_slice(&[0x90, 0x90, 0x90, 0x90]);
150
151 elf.extend_from_slice(b"\0.text\0.shstrtab\0");
153
154 elf.extend_from_slice(&[0; 64]);
157
158 elf.extend_from_slice(&[1, 0, 0, 0]); elf.extend_from_slice(&[1, 0, 0, 0]); elf.extend_from_slice(&[6, 0, 0, 0, 0, 0, 0, 0]); elf.extend_from_slice(&[0; 8]); elf.extend_from_slice(&[64, 0, 0, 0, 0, 0, 0, 0]); elf.extend_from_slice(&[16, 0, 0, 0, 0, 0, 0, 0]); elf.extend_from_slice(&[0; 4]); elf.extend_from_slice(&[0; 4]); elf.extend_from_slice(&[1, 0, 0, 0, 0, 0, 0, 0]); elf.extend_from_slice(&[0; 8]); elf.extend_from_slice(&[7, 0, 0, 0]); elf.extend_from_slice(&[3, 0, 0, 0]); elf.extend_from_slice(&[0; 8]); elf.extend_from_slice(&[0; 8]); elf.extend_from_slice(&[80, 0, 0, 0, 0, 0, 0, 0]); elf.extend_from_slice(&[17, 0, 0, 0, 0, 0, 0, 0]); elf.extend_from_slice(&[0; 4]); elf.extend_from_slice(&[0; 4]); elf.extend_from_slice(&[1, 0, 0, 0, 0, 0, 0, 0]); elf.extend_from_slice(&[0; 8]); elf
183 }
184
185 #[crate::ctb_test]
186 fn test_invalid_file_data() {
187 let invalid_data = b"not a valid binary or archive";
188 extract_instruction_sets(invalid_data).unwrap_err();
189 }
190
191 #[crate::ctb_test]
192 fn test_minimal_elf_x86_64() {
193 let elf_bytes = create_sample_elf();
194 let result = extract_instruction_sets(&elf_bytes).unwrap();
195 assert!(result.contains(&"SSE2".to_string()));
196 assert!(result.contains(&"AVX".to_string()));
197 assert!(result.contains(&"AVX2".to_string()));
198 }
199
200 #[crate::ctb_test]
201 fn test_archive_containing_elf() {
202 let elf_bytes = create_sample_elf();
203 let mut ar = Vec::new();
204 ar.extend_from_slice(b"!<arch>\n");
206 ar.extend_from_slice(b"sample.o/ "); ar.extend_from_slice(b"0 "); ar.extend_from_slice(b"0 "); ar.extend_from_slice(b"0 "); ar.extend_from_slice(b"644 "); let size_str = format!("{:<10}", elf_bytes.len());
213 ar.extend_from_slice(size_str.as_bytes()); ar.extend_from_slice(b"`\n"); ar.extend_from_slice(&elf_bytes);
217
218 let result = extract_instruction_sets(&ar).unwrap();
219 assert!(result.contains(&"SSE2".to_string()));
220 assert!(result.contains(&"AVX".to_string()));
221 assert!(result.contains(&"AVX2".to_string()));
222 }
223}