From fc0de01c3379dc6b187fbc968bd23225f3d7d27f Mon Sep 17 00:00:00 2001 From: Evan Richter Date: Sun, 8 Nov 2026 19:12:27 -0700 Subject: [PATCH 1/4] [AUTOMATED] Accept SLEIGH spec roots held in memory A library caller can now pass its compiled specs as bytes instead of shipping a specs/ directory. MemorySpecs holds a spec tree as (path, contents) pairs, over a static table or over owned data such as Vec or Arc<[u8]>. SpecRoot names a directory or such a tree. Each public bootstrap gains a *_with_specs form that takes any list of spec roots, and the &[String] functions delegate to it. LanguageDatabase owns the trees it was given, so nothing is registered process-wide. A tree need not follow Ghidra's layout: one language's .ldefs, .sla, .pspec and .cspec, flat, form a complete root. Tests check that memory roots decompile the fauxware fixture as the specs directory does, never fall back to the filesystem, and mix with directory roots. Co-Authored-By: Claude Opus 5.5 --- decompiler/crates/kuna-base/src/filemanage.rs | 5 + decompiler/crates/kuna-console/src/engine.rs | 162 ++++++-- .../crates/kuna-console/tests/integration.rs | 3 + .../kuna-console/tests/verify_memoryspecs.rs | 136 +++++++ .../src/p1_partition/kuna_memoryspecs.rs | 366 ++++++++++++++++++ .../kuna-decomp/src/p1_partition/mod.rs | 1 + .../src/p1_partition/sleigh_arch.rs | 53 ++- 7 files changed, 691 insertions(+), 35 deletions(-) create mode 100644 decompiler/crates/kuna-console/tests/verify_memoryspecs.rs create mode 100644 decompiler/crates/kuna-decomp/src/p1_partition/kuna_memoryspecs.rs diff --git a/decompiler/crates/kuna-base/src/filemanage.rs b/decompiler/crates/kuna-base/src/filemanage.rs index 1f6d13ee03..db9260356b 100644 --- a/decompiler/crates/kuna-base/src/filemanage.rs +++ b/decompiler/crates/kuna-base/src/filemanage.rs @@ -49,6 +49,11 @@ impl FileManage { } } + /// The search path, in the order directories were added (kuna). + pub fn path_list(&self) -> &[String] { + &self.pathlist + } + /// Add current working directory to path (C++ `addCurrentDir`). /// /// C++ calls `getcwd` into a 256-byte buffer and silently adds nothing on diff --git a/decompiler/crates/kuna-console/src/engine.rs b/decompiler/crates/kuna-console/src/engine.rs index 302c332ecd..dd2535ddb1 100644 --- a/decompiler/crates/kuna-console/src/engine.rs +++ b/decompiler/crates/kuna-console/src/engine.rs @@ -46,6 +46,7 @@ use kuna_base::xml::{DocumentStorage, Element}; use kuna_decomp::architecture::Architecture; use kuna_decomp::options::register_option_elements; use kuna_decomp::raw_arch::RawBinaryArchitectureCapability; +use kuna_decomp::kuna_memoryspecs::IntoSpecRoot; use kuna_decomp::sleigh_arch::{register_sleigh_arch_ids, LanguageDatabase, SleighArchitecture}; use kuna_decomp::xml_arch::XmlArchitectureCapability; @@ -3645,11 +3646,12 @@ fn build_registry() -> IdRegistry { /// The Python tools pass `-s ` and set `SLEIGHHOME=`; the bin's /// arg parser records the spec roots and hands them here. The first existing /// root wins (mirroring the C++ `scanForSleighDirectories` over the recorded -/// roots). -fn scan_language_database(spec_roots: &[String], registry: &IdRegistry) -> KunaResult { +/// roots). A root may also be a tree held in memory +/// ([`kuna_decomp::kuna_memoryspecs::MemorySpecs`]). +fn scan_language_database(spec_roots: &[impl IntoSpecRoot], registry: &IdRegistry) -> KunaResult { let mut db = LanguageDatabase::new(); for root in spec_roots { - db.scan_for_sleigh_directories(root); + db.add_spec_root(&root.to_spec_root()); } db.get_descriptions(registry)?; Ok(db) @@ -3691,7 +3693,7 @@ fn build_engine_and_init(sleigh: &mut SleighArchitecture, db: &LanguageDatabase) let resolved_sla = specs .slafile .ok_or_else(|| KunaError::lowlevel("build_spec_file resolved no .sla"))?; - let sla = std::fs::read(&resolved_sla) + let sla = db.read_spec(&resolved_sla) .map_err(|e| KunaError::lowlevel(format!("read sla {resolved_sla}: {e}")))?; // The loader is handed to the translator as a dummy first; the real opened @@ -3723,11 +3725,11 @@ fn build_engine_and_init(sleigh: &mut SleighArchitecture, db: &LanguageDatabase) // A read failure is non-fatal: the architecture falls back to the name-only // default model (proto recovery simply won't fire). if !specs.compilerfile.is_empty() { - if let Ok(cspec) = std::fs::read(&specs.compilerfile) { + if let Ok(cspec) = db.read_spec(&specs.compilerfile) { sleigh .base_mut() .ok_or_else(|| KunaError::lowlevel("no Architecture base after build_translator"))? - .set_cspec_xml(cspec); + .set_cspec_xml(cspec.into_owned()); } } @@ -3738,11 +3740,11 @@ fn build_engine_and_init(sleigh: &mut SleighArchitecture, db: &LanguageDatabase) // without it x86-64 lifts as 16-bit real mode. A read failure is non-fatal // (the engine keeps the zero-default context). if !specs.processorfile.is_empty() { - if let Ok(pspec) = std::fs::read(&specs.processorfile) { + if let Ok(pspec) = db.read_spec(&specs.processorfile) { sleigh .base_mut() .ok_or_else(|| KunaError::lowlevel("no Architecture base after build_translator"))? - .set_pspec_xml(pspec); + .set_pspec_xml(pspec.into_owned()); } } @@ -3780,6 +3782,16 @@ pub fn bootstrap_program( binaryimage: Rc, arch_id: &str, spec_roots: &[String], +) -> KunaResult { + bootstrap_program_with_specs(binaryimage, arch_id, spec_roots) +} + +/// [`bootstrap_program`] over any spec roots: directories, in-memory +/// [`MemorySpecs`](kuna_decomp::kuna_memoryspecs::MemorySpecs) trees, or a mix. +pub fn bootstrap_program_with_specs( + binaryimage: Rc, + arch_id: &str, + spec_roots: &[impl IntoSpecRoot], ) -> KunaResult { let registry = build_registry(); @@ -3944,7 +3956,7 @@ fn input_isa_paints_for_ranges( fn build_sleigh_for_loader( path: &str, target: &str, - spec_roots: &[String], + spec_roots: &[impl IntoSpecRoot], registry: &IdRegistry, arch_type: &str, fallback_arch_id: Option<&[u8]>, @@ -4103,6 +4115,19 @@ pub fn bootstrap_from_raw( entries: &[u64], isa: Option, spec_roots: &[String], +) -> KunaResult { + bootstrap_from_raw_with_specs(path, target, base, entries, isa, spec_roots) +} + +/// [`bootstrap_from_raw`] over any spec roots: directories, in-memory +/// [`MemorySpecs`](kuna_decomp::kuna_memoryspecs::MemorySpecs) trees, or a mix. +pub fn bootstrap_from_raw_with_specs( + path: &str, + target: &str, + base: u64, + entries: &[u64], + isa: Option, + spec_roots: &[impl IntoSpecRoot], ) -> KunaResult { bootstrap_raw_image(path, target, base, entries, isa, spec_roots, None) } @@ -4114,7 +4139,7 @@ fn bootstrap_raw_image( base: u64, entries: &[u64], isa: Option, - spec_roots: &[String], + spec_roots: &[impl IntoSpecRoot], bytes: Option>, ) -> KunaResult { if target.trim().is_empty() { @@ -4253,13 +4278,24 @@ pub fn bootstrap_from_image_with_isa( target: &str, spec_roots: &[String], isa: Option, +) -> KunaResult { + bootstrap_from_image_with_isa_with_specs(path, target, spec_roots, isa) +} + +/// [`bootstrap_from_image_with_isa`] over any spec roots: directories, in-memory +/// [`MemorySpecs`](kuna_decomp::kuna_memoryspecs::MemorySpecs) trees, or a mix. +pub fn bootstrap_from_image_with_isa_with_specs( + path: &str, + target: &str, + spec_roots: &[impl IntoSpecRoot], + isa: Option, ) -> KunaResult { if file_is_te_image(path) { - bootstrap_from_te(path, target, spec_roots, isa) + bootstrap_from_te_with_specs(path, target, spec_roots, isa) } else if let Some(bytes) = file_mz_dos_image(path) { - bootstrap_from_mz_bytes(path, &bytes, target, spec_roots, isa) + bootstrap_from_mz_bytes_with_specs(path, &bytes, target, spec_roots, isa) } else { - bootstrap_from_object_with_isa(path, target, spec_roots, isa) + bootstrap_from_object_with_isa_with_specs(path, target, spec_roots, isa) } } @@ -4299,6 +4335,18 @@ pub fn bootstrap_from_mz_bytes( target: &str, spec_roots: &[String], isa: Option, +) -> KunaResult { + bootstrap_from_mz_bytes_with_specs(path, bytes, target, spec_roots, isa) +} + +/// [`bootstrap_from_mz_bytes`] over any spec roots: directories, in-memory +/// [`MemorySpecs`](kuna_decomp::kuna_memoryspecs::MemorySpecs) trees, or a mix. +pub fn bootstrap_from_mz_bytes_with_specs( + path: &str, + bytes: &[u8], + target: &str, + spec_roots: &[impl IntoSpecRoot], + isa: Option, ) -> KunaResult { let image = MzImage::parse(bytes) .map_err(|error| KunaError::lowlevel(format!("{}; {MZ_RAW_IMAGE_HINT}", error.explain())))?; @@ -4373,6 +4421,16 @@ pub fn bootstrap_from_image( path: &str, target: &str, spec_roots: &[String], +) -> KunaResult { + bootstrap_from_image_with_specs(path, target, spec_roots) +} + +/// [`bootstrap_from_image`] over any spec roots: directories, in-memory +/// [`MemorySpecs`](kuna_decomp::kuna_memoryspecs::MemorySpecs) trees, or a mix. +pub fn bootstrap_from_image_with_specs( + path: &str, + target: &str, + spec_roots: &[impl IntoSpecRoot], ) -> KunaResult { let isa = std::env::var(ARM_ISA_ENV) .ok() @@ -4380,7 +4438,7 @@ pub fn bootstrap_from_image( .transpose() .map_err(KunaError::lowlevel)? .flatten(); - bootstrap_from_image_with_isa(path, target, spec_roots, isa) + bootstrap_from_image_with_isa_with_specs(path, target, spec_roots, isa) } /// Whether the file at `path` opens as a UEFI TE, reading only the bytes the @@ -4414,11 +4472,22 @@ pub fn bootstrap_from_te( target: &str, spec_roots: &[String], isa: Option, +) -> KunaResult { + bootstrap_from_te_with_specs(path, target, spec_roots, isa) +} + +/// [`bootstrap_from_te`] over any spec roots: directories, in-memory +/// [`MemorySpecs`](kuna_decomp::kuna_memoryspecs::MemorySpecs) trees, or a mix. +pub fn bootstrap_from_te_with_specs( + path: &str, + target: &str, + spec_roots: &[impl IntoSpecRoot], + isa: Option, ) -> KunaResult { let bytes = std::fs::read(path).map_err(|error| { KunaError::lowlevel(format!("Unable to open TE image file: {path}: {error}")) })?; - bootstrap_from_te_bytes(path, &bytes, target, spec_roots, isa) + bootstrap_from_te_bytes_with_specs(path, &bytes, target, spec_roots, isa) } /// [`bootstrap_from_te`] over bytes a caller has already read. @@ -4428,6 +4497,18 @@ pub fn bootstrap_from_te_bytes( target: &str, spec_roots: &[String], isa: Option, +) -> KunaResult { + bootstrap_from_te_bytes_with_specs(path, bytes, target, spec_roots, isa) +} + +/// [`bootstrap_from_te_bytes`] over any spec roots: directories, in-memory +/// [`MemorySpecs`](kuna_decomp::kuna_memoryspecs::MemorySpecs) trees, or a mix. +pub fn bootstrap_from_te_bytes_with_specs( + path: &str, + bytes: &[u8], + target: &str, + spec_roots: &[impl IntoSpecRoot], + isa: Option, ) -> KunaResult { let target = kuna_analysis::loadimage_object::explicit_language_target(target).unwrap_or(""); let registry = build_registry(); @@ -4525,6 +4606,16 @@ pub fn bootstrap_from_object( path: &str, target: &str, spec_roots: &[String], +) -> KunaResult { + bootstrap_from_object_with_specs(path, target, spec_roots) +} + +/// [`bootstrap_from_object`] over any spec roots: directories, in-memory +/// [`MemorySpecs`](kuna_decomp::kuna_memoryspecs::MemorySpecs) trees, or a mix. +pub fn bootstrap_from_object_with_specs( + path: &str, + target: &str, + spec_roots: &[impl IntoSpecRoot], ) -> KunaResult { let isa = std::env::var(ARM_ISA_ENV) .ok() @@ -4532,7 +4623,7 @@ pub fn bootstrap_from_object( .transpose() .map_err(KunaError::lowlevel)? .flatten(); - bootstrap_from_object_with_isa(path, target, spec_roots, isa) + bootstrap_from_object_with_isa_with_specs(path, target, spec_roots, isa) } /// The MIPS t9 entry probe: the PIC-setup seed for `entry`, truncated to the @@ -4563,6 +4654,17 @@ pub fn bootstrap_from_object_with_isa( target: &str, spec_roots: &[String], isa: Option, +) -> KunaResult { + bootstrap_from_object_with_isa_with_specs(path, target, spec_roots, isa) +} + +/// [`bootstrap_from_object_with_isa`] over any spec roots: directories, in-memory +/// [`MemorySpecs`](kuna_decomp::kuna_memoryspecs::MemorySpecs) trees, or a mix. +pub fn bootstrap_from_object_with_isa_with_specs( + path: &str, + target: &str, + spec_roots: &[impl IntoSpecRoot], + isa: Option, ) -> KunaResult { let target = kuna_analysis::loadimage_object::explicit_language_target(target).unwrap_or(""); let registry = build_registry(); @@ -6240,11 +6342,17 @@ fn commit_analysis_output( /// `` wrapping one), reading the arch id off the /// `` element. Used by `load file` (path → parse → bootstrap). pub fn bootstrap_from_root(root: &Rc, spec_roots: &[String]) -> KunaResult { + bootstrap_from_root_with_specs(root, spec_roots) +} + +/// [`bootstrap_from_root`] over any spec roots: directories, in-memory +/// [`MemorySpecs`](kuna_decomp::kuna_memoryspecs::MemorySpecs) trees, or a mix. +pub fn bootstrap_from_root_with_specs(root: &Rc, spec_roots: &[impl IntoSpecRoot]) -> KunaResult { let binaryimage = find_binaryimage(root) .ok_or_else(|| KunaError::lowlevel("Could not find binaryimage tag"))?; let arch_id = attr(&binaryimage, "arch") .ok_or_else(|| KunaError::lowlevel(" has no arch attribute"))?; - bootstrap_program(binaryimage, &arch_id, spec_roots) + bootstrap_program_with_specs(binaryimage, &arch_id, spec_roots) } /// The ELF magic (`\x7fELF`), used to route `load file` to the object path. @@ -6342,11 +6450,21 @@ pub fn bootstrap_from_file( path: &str, target: &str, spec_roots: &[String], +) -> KunaResult { + bootstrap_from_file_with_specs(path, target, spec_roots) +} + +/// [`bootstrap_from_file`] over any spec roots: directories, in-memory +/// [`MemorySpecs`](kuna_decomp::kuna_memoryspecs::MemorySpecs) trees, or a mix. +pub fn bootstrap_from_file_with_specs( + path: &str, + target: &str, + spec_roots: &[impl IntoSpecRoot], ) -> KunaResult { if kuna_analysis::loader::kuna_dyldcache::is_dyld_cache_file(Path::new(path)) || kuna_analysis::image_bytes::ImageBytes::map_thin_macho(path).ok().flatten().is_some() { - return bootstrap_from_object(path, target, spec_roots); + return bootstrap_from_object_with_specs(path, target, spec_roots); } let bytes = std::fs::read(path) .map_err(|e| KunaError::lowlevel(format!("Unable to recognize imagefile {path}: {e}")))?; @@ -6357,7 +6475,7 @@ pub fn bootstrap_from_file( .transpose() .map_err(KunaError::lowlevel)? .flatten(); - return bootstrap_from_mz_bytes(path, &bytes, target, spec_roots, isa); + return bootstrap_from_mz_bytes_with_specs(path, &bytes, target, spec_roots, isa); } if is_te_image(&bytes) { let isa = std::env::var(ARM_ISA_ENV) @@ -6366,13 +6484,13 @@ pub fn bootstrap_from_file( .transpose() .map_err(KunaError::lowlevel)? .flatten(); - return bootstrap_from_te_bytes(path, &bytes, target, spec_roots, isa); + return bootstrap_from_te_bytes_with_specs(path, &bytes, target, spec_roots, isa); } if is_object_binary(&bytes) || is_apple_container(&bytes) { // Real object-format binary (ELF / PE / Mach-O / COFF): drive the // object-crate loader. drop(bytes); - return bootstrap_from_object(path, target, spec_roots); + return bootstrap_from_object_with_specs(path, target, spec_roots); } if !bytes .iter() @@ -6393,7 +6511,7 @@ pub fn bootstrap_from_file( if find_binaryimage(&root).is_none() { return Err(KunaError::lowlevel(RAW_IMAGE_INPUT_HINT)); } - bootstrap_from_root(&root, spec_roots) + bootstrap_from_root_with_specs(&root, spec_roots) } /// Iterate the opened [`LoadImageXml`]'s symbol records (name → address) into a diff --git a/decompiler/crates/kuna-console/tests/integration.rs b/decompiler/crates/kuna-console/tests/integration.rs index 4921900c86..b68c29cff4 100644 --- a/decompiler/crates/kuna-console/tests/integration.rs +++ b/decompiler/crates/kuna-console/tests/integration.rs @@ -24,6 +24,9 @@ mod n32_stack; #[path = "setup_commands.rs"] mod setup_commands; +#[path = "verify_memoryspecs.rs"] +mod verify_memoryspecs; + #[path = "verify_aarch64_plt.rs"] mod verify_aarch64_plt; diff --git a/decompiler/crates/kuna-console/tests/verify_memoryspecs.rs b/decompiler/crates/kuna-console/tests/verify_memoryspecs.rs new file mode 100644 index 0000000000..c11006bb18 --- /dev/null +++ b/decompiler/crates/kuna-console/tests/verify_memoryspecs.rs @@ -0,0 +1,136 @@ +//! A `MemorySpecs` root bootstraps and decompiles as the `specs/` directory it +//! was read from does, never falls back to the filesystem, mixes with +//! directory roots, and needs no Ghidra layout: one language's four files, +//! flat, are a root. + +use std::path::{Path, PathBuf}; +use std::sync::OnceLock; + +use kuna_console::engine::{bootstrap_from_object_with_specs, ConsoleProgram}; +use kuna_console::project::{decompile_entry, DecompileOptions}; +use kuna_decomp::kuna_memoryspecs::{MemorySpecs, SpecRoot}; + +const FUNCS: [&str; 3] = ["authenticate", "accepted", "main"]; +const X86_64_SLA: &str = "Ghidra/Processors/x86/data/languages/x86-64.sla"; + +fn repo_root() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("../../..") + .canonicalize() + .unwrap() +} + +fn specs_dir() -> PathBuf { + repo_root().join("specs") +} + +fn fixture() -> String { + let path = repo_root().join("decompiler/crates/kuna-analysis/tests/fixtures/fauxware"); + path.to_str().unwrap().to_string() +} + +fn collect(root: &Path, dir: &Path, out: &mut Vec<(String, Vec)>) { + for entry in std::fs::read_dir(dir).unwrap() { + let path = entry.unwrap().path(); + let name = path.file_name().unwrap().to_str().unwrap(); + if name.starts_with('.') { + continue; + } + if path.is_dir() { + collect(root, &path, out); + } else if !name.ends_with(".slaspec") && !name.ends_with(".sinc") { + let rel = path + .strip_prefix(root) + .unwrap() + .to_str() + .unwrap() + .replace('\\', "/"); + out.push((rel, std::fs::read(&path).unwrap())); + } + } +} + +/// Every file `make specs` leaves under `specs/` except the SLEIGH sources, +/// copied into memory once. +fn embedded() -> &'static [(String, Vec)] { + static FILES: OnceLock)>> = OnceLock::new(); + FILES.get_or_init(|| { + let root = specs_dir(); + let mut files = Vec::new(); + collect(&root, &root, &mut files); + assert!( + files.iter().any(|(rel, _)| *rel == X86_64_SLA), + "{X86_64_SLA} is not built; run `make specs`" + ); + files + }) +} + +fn bootstrap(roots: &[SpecRoot]) -> ConsoleProgram { + let mut prog = bootstrap_from_object_with_specs(&fixture(), "", roots).expect("bootstrap fauxware"); + prog.commit_pending_analysis() + .expect("analysis commit succeeds"); + prog +} + +fn decompile_all(prog: &mut ConsoleProgram) -> Vec { + FUNCS + .iter() + .map(|func| { + let entry = prog.find_entry_by_name(func).expect("function in fixture"); + decompile_entry(prog, entry, &DecompileOptions::default()) + .code + .expect("decompile succeeds") + }) + .collect() +} + +#[test] +fn memory_specs_decompile_as_the_specs_directory_does() { + let memory = MemorySpecs::from_files(embedded().iter().cloned()); + let from_memory = decompile_all(&mut bootstrap(&[memory.into()])); + let from_disk = decompile_all(&mut bootstrap(&[specs_dir().into()])); + assert!( + from_memory[2].contains("authenticate"), + "in:\n{}", + from_memory[2] + ); + assert_eq!(from_memory, from_disk); +} + +#[test] +fn memory_specs_never_fall_back_to_the_filesystem() { + let memory = MemorySpecs::from_files(embedded().iter().filter(|(rel, _)| rel != X86_64_SLA).cloned()); + let err = match bootstrap_from_object_with_specs(&fixture(), "", &[memory]) { + Ok(_) => panic!("bootstrapped without {X86_64_SLA}"), + Err(err) => err.to_string(), + }; + assert!(err.contains(".sla"), "{err}"); +} + +#[test] +fn a_memory_root_mixes_with_path_roots() { + let missing = SpecRoot::Dir(repo_root().join("no-such-specs")); + let memory = MemorySpecs::from_files(embedded().iter().cloned()); + let mut prog = bootstrap(&[missing, memory.into()]); + let entry = prog + .find_entry_by_name("authenticate") + .expect("function in fixture"); + assert!(decompile_entry(&mut prog, entry, &DecompileOptions::default()).code.is_some()); +} + +#[test] +fn one_languages_files_alone_are_a_root() { + let languages = "Ghidra/Processors/x86/data/languages"; + let file = |name: &str| -> (String, Vec) { + let rel = format!("{languages}/{name}"); + let bytes = embedded().iter().find(|(p, _)| *p == rel).expect(&rel).1.clone(); + (name.to_string(), bytes) + }; + let flat = MemorySpecs::from_files( + ["x86.ldefs", "x86-64.sla", "x86-64.pspec", "x86-64-gcc.cspec"].map(file), + ); + let from_flat = decompile_all(&mut bootstrap(&[flat.into()])); + let from_disk = decompile_all(&mut bootstrap(&[specs_dir().into()])); + assert_eq!(from_flat, from_disk); +} diff --git a/decompiler/crates/kuna-decomp/src/p1_partition/kuna_memoryspecs.rs b/decompiler/crates/kuna-decomp/src/p1_partition/kuna_memoryspecs.rs new file mode 100644 index 0000000000..b22f86255f --- /dev/null +++ b/decompiler/crates/kuna-decomp/src/p1_partition/kuna_memoryspecs.rs @@ -0,0 +1,366 @@ +//! (kuna) SLEIGH spec roots that are either directories or trees held in memory. +//! +//! A [`MemorySpecs`] is a flat table of files, each keyed by its `/`-separated +//! path relative to the spec root (`Ghidra/Processors/x86/data/languages/x86-64.sla`): +//! the tree `make specs` leaves on disk, minus whatever the caller chose not to +//! embed. A [`SpecRoot`] names either kind of root. A bootstrap takes a list of +//! anything that converts into one, so the two kinds mix in one list. +//! +//! The table travels by value: [`LanguageDatabase::add_spec_root`] keeps it in +//! the database, which hands each mounted tree an internal root, +//! `kuna-memory://`, unique within that database. Nothing is registered +//! process-wide. The database reaches spec files only through the functions +//! here; each answers a mounted path from its table and passes any other path +//! to [`FileManage`]/`std::fs`. +//! +//! [`LanguageDatabase::add_spec_root`]: crate::sleigh_arch::LanguageDatabase::add_spec_root + +use std::borrow::Cow; +use std::fmt; +use std::fs::File; +use std::io; +use std::path::{Path, PathBuf}; +use std::sync::Arc; + +use kuna_base::filemanage::FileManage; + +const MEMORY_ROOT_SCHEME: &str = "kuna-memory://"; + +type Bytes = Box + Send + Sync>; +type Files = [(Cow<'static, str>, Bytes)]; + +/// A compiled SLEIGH spec tree in memory: (`/`-separated path relative to the +/// spec root, contents) for each file, in any order. Cloning shares the table. +/// +/// The tree need not follow Ghidra's layout: a flat table holding one `.ldefs` +/// and the `.sla`, `.pspec` and `.cspec` it names is a complete root. +#[derive(Clone)] +pub struct MemorySpecs { + files: Arc, +} + +impl MemorySpecs { + /// A tree over a table compiled into the program; no contents are copied. + pub fn new(files: &'static [(&'static str, &'static [u8])]) -> MemorySpecs { + MemorySpecs::from_files(files.iter().copied()) + } + + /// A tree over paths (`&'static str` or `String`) and contents in any + /// container the caller owns or holds for `'static`: `Vec`, + /// `Arc<[u8]>`, `&'static [u8]`, and so on. + pub fn from_files(files: impl IntoIterator) -> MemorySpecs + where + P: Into>, + B: AsRef<[u8]> + Send + Sync + 'static, + { + MemorySpecs { + files: files.into_iter().map(|(p, b)| (p.into(), Box::new(b) as Bytes)).collect(), + } + } + + /// The contents of the file at `rel`, relative to the spec root. + pub fn get(&self, rel: &str) -> Option<&[u8]> { + self.files.iter().find(|(path, _)| path == rel).map(|(_, bytes)| (**bytes).as_ref()) + } +} + +impl fmt::Debug for MemorySpecs { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("MemorySpecs").field("files", &self.files.len()).finish() + } +} + +/// Where a SLEIGH spec tree lives. +#[derive(Clone, Debug)] +pub enum SpecRoot { + /// A directory holding `Ghidra/Processors/...` (or the language directories + /// themselves). + Dir(PathBuf), + /// A tree held in memory. + Memory(MemorySpecs), +} + +impl From for SpecRoot { + fn from(path: String) -> SpecRoot { + SpecRoot::Dir(path.into()) + } +} + +impl From<&String> for SpecRoot { + fn from(path: &String) -> SpecRoot { + SpecRoot::Dir(path.into()) + } +} + +impl From<&str> for SpecRoot { + fn from(path: &str) -> SpecRoot { + SpecRoot::Dir(path.into()) + } +} + +impl From for SpecRoot { + fn from(path: PathBuf) -> SpecRoot { + SpecRoot::Dir(path) + } +} + +impl From<&Path> for SpecRoot { + fn from(path: &Path) -> SpecRoot { + SpecRoot::Dir(path.into()) + } +} + +impl From for SpecRoot { + fn from(specs: MemorySpecs) -> SpecRoot { + SpecRoot::Memory(specs) + } +} + +/// Anything a bootstrap accepts in its spec-root list: a path, a +/// [`MemorySpecs`], or a [`SpecRoot`]. +pub trait IntoSpecRoot { + fn to_spec_root(&self) -> SpecRoot; +} + +impl + Clone> IntoSpecRoot for T { + fn to_spec_root(&self) -> SpecRoot { + self.clone().into() + } +} + +/// The internal root naming the `index`th tree a database mounted. +pub(crate) fn memory_root(index: usize) -> String { + format!("{MEMORY_ROOT_SCHEME}{index}") +} + +/// The mounted table and the path inside it, or `None` for a filesystem path. +/// An unknown mount resolves to an empty table, so it holds nothing. +fn resolve<'a, 'p>(mounts: &'a [MemorySpecs], path: &'p str) -> Option<(&'a Files, &'p str)> { + let rest = path.strip_prefix(MEMORY_ROOT_SCHEME)?; + let (index, inner) = rest.split_once('/').unwrap_or((rest, "")); + let files = index + .parse::() + .ok() + .and_then(|i| mounts.get(i)) + .map_or(&[][..], |m| &*m.files); + Some((files, inner)) +} + +/// The files and subdirectory names directly inside `dir`, a path inside a table. +fn children<'a>(files: &'a Files, dir: &str) -> (Vec<&'a str>, Vec<&'a str>) { + let prefix = dir.trim_end_matches('/'); + let mut leaves = Vec::new(); + let mut subdirs = Vec::new(); + for (path, _) in files { + let rest = if prefix.is_empty() { + &**path + } else { + match path.strip_prefix(prefix).and_then(|r| r.strip_prefix('/')) { + Some(rest) => rest, + None => continue, + } + }; + match rest.split_once('/') { + Some((sub, _)) => subdirs.push(sub), + None => leaves.push(rest), + } + } + leaves.sort_unstable(); + leaves.dedup(); + subdirs.sort_unstable(); + subdirs.dedup(); + (leaves, subdirs) +} + +fn with_separator(dirname: &str) -> String { + let mut dirfinal = dirname.to_string(); + if !dirfinal.ends_with('/') { + dirfinal.push('/'); + } + dirfinal +} + +/// The contents of the spec file at `path`. +pub(crate) fn read<'a>(mounts: &'a [MemorySpecs], path: &str) -> io::Result> { + match resolve(mounts, path) { + Some((files, inner)) => files + .iter() + .find(|(p, _)| p == inner) + .map(|(_, bytes)| Cow::Borrowed((**bytes).as_ref())) + .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, format!("{path}: not in the mounted specs"))), + None => std::fs::read(path).map(Cow::Owned), + } +} + +/// Whether a spec file exists at `path` (C++ `findFile`'s open test). +fn exists(mounts: &[MemorySpecs], path: &str) -> bool { + match resolve(mounts, path) { + Some((files, inner)) => files.iter().any(|(p, _)| p == inner), + None => File::open(path).is_ok(), + } +} + +/// [`FileManage::directory_list`] without dot directories, over either kind of path. +pub(crate) fn directory_list(mounts: &[MemorySpecs], res: &mut Vec, dirname: &str) { + let Some((files, inner)) = resolve(mounts, dirname) else { + FileManage::directory_list(res, dirname, false); + return; + }; + let dirfinal = with_separator(dirname); + for sub in children(files, inner).1 { + if !sub.starts_with('.') { + res.push(format!("{dirfinal}{sub}")); + } + } +} + +/// [`FileManage::match_list_dir`] for a suffix, without dot files, over either +/// kind of path. +pub(crate) fn match_list_dir(mounts: &[MemorySpecs], res: &mut Vec, suffix: &str, dirname: &str) { + let Some((files, inner)) = resolve(mounts, dirname) else { + FileManage::match_list_dir(res, suffix, true, dirname, false); + return; + }; + let dirfinal = with_separator(dirname); + for leaf in children(files, inner).0 { + if suffix.len() <= leaf.len() && !leaf.starts_with('.') && leaf.ends_with(suffix) { + res.push(format!("{dirfinal}{leaf}")); + } + } +} + +/// [`FileManage::scan_directory_recursive`] over either kind of path. +pub(crate) fn scan_directory_recursive( + mounts: &[MemorySpecs], + res: &mut Vec, + matchname: &str, + rootpath: &str, + maxdepth: i32, +) { + if resolve(mounts, rootpath).is_none() { + FileManage::scan_directory_recursive(res, matchname, rootpath, maxdepth); + return; + } + if maxdepth == 0 { + return; + } + let mut subdir = Vec::new(); + directory_list(mounts, &mut subdir, rootpath); + for curpath in &subdir { + let base = curpath.rsplit('/').next().unwrap_or(curpath); + if base == matchname { + res.push(curpath.clone()); + } else { + scan_directory_recursive(mounts, res, matchname, curpath, maxdepth - 1); + } + } +} + +/// [`FileManage::find_file`] over a search list that may hold mounted +/// directories: the first directory holding `name` wins, and `res` is cleared +/// when none does. +pub(crate) fn find_file(mounts: &[MemorySpecs], paths: &FileManage, res: &mut String, name: &str) { + if name.chars().next().is_some_and(FileManage::is_separator) { + *res = name.to_string(); + if exists(mounts, res) { + return; + } + } else { + for path in paths.path_list() { + *res = format!("{path}{name}"); + if exists(mounts, res) { + return; + } + } + } + res.clear(); +} + +#[cfg(test)] +mod tests { + use super::*; + + static TREE: &[(&str, &[u8])] = &[ + ("Ghidra/Processors/toy/data/languages/toy.ldefs", b""), + ("Ghidra/Processors/toy/data/languages/toy.sla", b"sla"), + ("Ghidra/Processors/toy/data/languages/.hidden.ldefs", b""), + ("Ghidra/Processors/toy/data/languages/old/toy_old.ldefs", b"old"), + ("Ghidra/Processors/toy/data/patterns/p.xml", b"p"), + ("README.md", b"readme"), + ]; + + fn mounted() -> (Vec, String) { + (vec![MemorySpecs::new(TREE)], memory_root(0)) + } + + #[test] + fn a_mounted_table_reads_back() { + let (mounts, root) = mounted(); + let sla = format!("{root}/Ghidra/Processors/toy/data/languages/toy.sla"); + assert!(exists(&mounts, &sla)); + assert_eq!(&*read(&mounts, &sla).unwrap(), b"sla"); + assert_eq!(&*read(&mounts, &format!("{root}/README.md")).unwrap(), b"readme"); + let missing = read(&mounts, &format!("{root}/nope.sla")).unwrap_err(); + assert_eq!(missing.kind(), io::ErrorKind::NotFound); + assert!(!exists(&mounts, &format!("{root}/Ghidra/Processors")), "a directory is not a file"); + } + + #[test] + fn owned_and_shared_contents_read_like_static_ones() { + let owned = MemorySpecs::from_files([(String::from("a/b.sla"), vec![1u8, 2, 3])]); + assert_eq!(owned.get("a/b.sla"), Some(&[1u8, 2, 3][..])); + let shared: Arc<[u8]> = Arc::from(&b"pspec"[..]); + let shared = MemorySpecs::from_files([("a/b.pspec", shared)]); + let mounts = [owned, shared]; + assert_eq!(&*read(&mounts, &format!("{}/a/b.sla", memory_root(0))).unwrap(), [1, 2, 3]); + assert_eq!(&*read(&mounts, &format!("{}/a/b.pspec", memory_root(1))).unwrap(), b"pspec"); + } + + #[test] + fn an_unmounted_index_holds_nothing() { + let (mounts, _) = mounted(); + let root = memory_root(1); + assert!(!exists(&mounts, &format!("{root}/README.md"))); + let mut dirs = Vec::new(); + directory_list(&mounts, &mut dirs, &root); + assert!(dirs.is_empty()); + } + + #[test] + fn listings_see_only_direct_children() { + let (mounts, root) = mounted(); + let mut dirs = Vec::new(); + directory_list(&mounts, &mut dirs, &format!("{root}/Ghidra/Processors/toy/data/")); + let languages = format!("{root}/Ghidra/Processors/toy/data/languages"); + assert_eq!(dirs, [languages.clone(), format!("{root}/Ghidra/Processors/toy/data/patterns")]); + + let mut ldefs = Vec::new(); + match_list_dir(&mounts, &mut ldefs, ".ldefs", &languages); + assert_eq!(ldefs, [format!("{languages}/toy.ldefs")]); + } + + #[test] + fn the_sleigh_layout_scan_finds_language_directories() { + let (mounts, root) = mounted(); + let mut ghidra = Vec::new(); + scan_directory_recursive(&mounts, &mut ghidra, "Ghidra", &root, 2); + assert_eq!(ghidra, [format!("{root}/Ghidra")]); + let mut languages = Vec::new(); + scan_directory_recursive(&mounts, &mut languages, "languages", &format!("{root}/Ghidra/Processors/toy"), 2); + assert_eq!(languages, [format!("{root}/Ghidra/Processors/toy/data/languages")]); + } + + #[test] + fn find_file_takes_the_first_directory_in_order() { + let (mounts, root) = mounted(); + let mut paths = FileManage::default(); + paths.add_dir_2_path("/nonexistent/kuna/specs"); + paths.add_dir_2_path(&format!("{root}/Ghidra/Processors/toy/data/languages/old")); + paths.add_dir_2_path(&format!("{root}/Ghidra/Processors/toy/data/languages")); + let mut res = String::new(); + find_file(&mounts, &paths, &mut res, "toy.sla"); + assert_eq!(res, format!("{root}/Ghidra/Processors/toy/data/languages/toy.sla")); + find_file(&mounts, &paths, &mut res, "absent.sla"); + assert!(res.is_empty()); + } +} diff --git a/decompiler/crates/kuna-decomp/src/p1_partition/mod.rs b/decompiler/crates/kuna-decomp/src/p1_partition/mod.rs index f3ec4fc70e..ac1c70f8a1 100644 --- a/decompiler/crates/kuna-decomp/src/p1_partition/mod.rs +++ b/decompiler/crates/kuna-decomp/src/p1_partition/mod.rs @@ -10,6 +10,7 @@ pub mod kuna_formatstring; pub mod kuna_goreg; // (kuna) name+type the goroutine register as the `g` global (docs/multilang.md W4) pub mod kuna_langfamily; // (kuna) the .ldefs processor/size/instruction order FID compares pub mod kuna_litpoolconst; +pub mod kuna_memoryspecs; // (kuna) spec trees supplied from memory pub mod kuna_mzdgroup; // (kuna) an MS-DOS executable's functions run with DS holding its data segment pub mod kuna_octeonid; // (kuna) Octeon language IDs, built by the loader and read by call recovery pub mod sleigh_arch; diff --git a/decompiler/crates/kuna-decomp/src/p1_partition/sleigh_arch.rs b/decompiler/crates/kuna-decomp/src/p1_partition/sleigh_arch.rs index 56d58f75b3..ed65175c7a 100644 --- a/decompiler/crates/kuna-decomp/src/p1_partition/sleigh_arch.rs +++ b/decompiler/crates/kuna-decomp/src/p1_partition/sleigh_arch.rs @@ -63,6 +63,8 @@ //! surface that is itself a later stub. Each is documented inline with //! `// STUB(...)`. +use std::borrow::Cow; + use kuna_base::error::{KunaError, KunaResult}; use kuna_base::filemanage::FileManage; use kuna_base::marshal::{ @@ -78,6 +80,7 @@ use kuna_sleigh::sleigh::Sleigh; use kuna_sleigh::translate::{TruncationTag, ELEM_TRUNCATE_SPACE}; use crate::architecture::Architecture; +use crate::kuna_memoryspecs::{self, MemorySpecs, SpecRoot}; // --------------------------------------------------------------------------- // Marshaling ids (C++ sleigh_arch.cc:21-34 + architecture.cc:40 for adjustvma) @@ -421,6 +424,9 @@ pub struct LanguageDatabase { description: Vec, /// Known directories that contain .ldefs files (C++ `specpaths`). specpaths: FileManage, + /// (kuna) Spec trees held in memory, each reached through the + /// `specpaths` entries under its internal root. + mounts: Vec, } impl LanguageDatabase { @@ -435,6 +441,25 @@ impl LanguageDatabase { &mut self.specpaths } + /// (kuna) Add a spec root of either kind, scanning it like + /// [`Self::scan_for_sleigh_directories`]. + pub fn add_spec_root(&mut self, root: &SpecRoot) { + match root { + SpecRoot::Dir(path) => self.scan_for_sleigh_directories(&path.to_string_lossy()), + SpecRoot::Memory(specs) => { + let rootpath = kuna_memoryspecs::memory_root(self.mounts.len()); + self.mounts.push(specs.clone()); + self.scan_for_sleigh_directories(&rootpath); + } + } + } + + /// (kuna) The contents of a spec file this database resolved, such as a + /// [`BuildSpecFiles`] path, from disk or from a mounted tree. + pub fn read_spec(&self, path: &str) -> std::io::Result> { + kuna_memoryspecs::read(&self.mounts, path) + } + /// All parsed language descriptions, in `.ldefs` document then file order /// (C++ `description`). pub fn descriptions(&self) -> &[LanguageDescription] { @@ -456,8 +481,8 @@ impl LanguageDatabase { errs: &mut String, registry: &IdRegistry, ) { - let bytes = match std::fs::read(specfile) { - Ok(b) => b, + let bytes = match self.read_spec(specfile) { + Ok(b) => b.into_owned(), Err(_) => return, }; @@ -530,7 +555,9 @@ impl LanguageDatabase { return; // Have we already collected before } let mut testspecs: Vec = Vec::new(); - self.specpaths.match_list(&mut testspecs, ".ldefs", true); + for path in self.specpaths.path_list() { + kuna_memoryspecs::match_list_dir(&self.mounts, &mut testspecs, ".ldefs", path); + } for spec in &testspecs { self.load_language_description(spec, errs, registry); } @@ -565,24 +592,24 @@ impl LanguageDatabase { let mut procdir2: Vec = Vec::new(); let mut languagesubdirs: Vec = Vec::new(); - FileManage::scan_directory_recursive(&mut ghidradir, "Ghidra", rootpath, 2); + kuna_memoryspecs::scan_directory_recursive(&self.mounts, &mut ghidradir, "Ghidra", rootpath, 2); for g in &ghidradir { - FileManage::scan_directory_recursive(&mut procdir, "Processors", g, 1); // Processors structure - FileManage::scan_directory_recursive(&mut procdir, "contrib", g, 1); + kuna_memoryspecs::scan_directory_recursive(&self.mounts, &mut procdir, "Processors", g, 1); // Processors structure + kuna_memoryspecs::scan_directory_recursive(&self.mounts, &mut procdir, "contrib", g, 1); } if !procdir.is_empty() { for p in &procdir { - FileManage::directory_list(&mut procdir2, p, false); + kuna_memoryspecs::directory_list(&self.mounts, &mut procdir2, p); } let mut datadirs: Vec = Vec::new(); for p in &procdir2 { - FileManage::scan_directory_recursive(&mut datadirs, "data", p, 1); + kuna_memoryspecs::scan_directory_recursive(&self.mounts, &mut datadirs, "data", p, 1); } let mut languagedirs: Vec = Vec::new(); for d in &datadirs { - FileManage::scan_directory_recursive(&mut languagedirs, "languages", d, 1); + kuna_memoryspecs::scan_directory_recursive(&self.mounts, &mut languagedirs, "languages", d, 1); } for l in &languagedirs { @@ -591,7 +618,7 @@ impl LanguageDatabase { // In the old version we have to go down one more level to get to ldefs for l in &languagedirs { - FileManage::directory_list(&mut languagesubdirs, l, false); + kuna_memoryspecs::directory_list(&self.mounts, &mut languagesubdirs, l); } } // If we haven't matched this directory structure, just use rootpath as the @@ -830,10 +857,10 @@ impl SleighArchitecture { let mut compilerfile = String::new(); let mut slafile = String::new(); - db.specpaths.find_file(&mut processorfile, language.get_processor_spec()); - db.specpaths.find_file(&mut compilerfile, compilertag.get_spec()); + kuna_memoryspecs::find_file(&db.mounts, &db.specpaths, &mut processorfile, language.get_processor_spec()); + kuna_memoryspecs::find_file(&db.mounts, &db.specpaths, &mut compilerfile, compilertag.get_spec()); if !language_reuse { - db.specpaths.find_file(&mut slafile, language.get_sla_file()); + kuna_memoryspecs::find_file(&db.mounts, &db.specpaths, &mut slafile, language.get_sla_file()); if slafile.is_empty() { return Err(KunaError::sleigh(format!( "Could not find .sla file for {}", From ae74f060044be797a5b8124eb78d08b0bacb9c2e Mon Sep 17 00:00:00 2001 From: Evan Richter Date: Mon, 9 Nov 2026 11:29:06 -0700 Subject: [PATCH 2/4] [AUTOMATED] Take load-time settings as explicit LoadOptions Library callers can now load several images at once in one process with different settings. LoadOptions holds every load-time option (ppc32picimports, ifuncfpret, ppcapu, kernelcache, goabi, symbolnamechars, libctypes, calltargettype, macho-arm64e, sourcelang, objcivars, objcsdk, dyldimage, dyldcrossrefs), the fat slice, the .pdb, .fid and FLIRT paths, the Go release, and the load-pass and mapping switches that KUNA_LOAD_OBJC, KUNA_LOAD_STRINGS and KUNA_LOAD_MMAP set. bootstrap_from_object_with_options and bootstrap_from_image_with_options put it in force on the loading thread, and each Architecture keeps its options and re-enters them around analysis, decompilation, name lookup and console commands. An explicit-options load neither reads nor writes the environment; environment_fallbacks() counts reads that fell back to it on a thread. The entry points that take no options load from the environment, publish KUNA_GOVERSION, and build programs that read the environment live. set_kuna_option and reset_wire_defaults write the environment for a later load. Co-Authored-By: Claude Opus 5.5 --- .../kuna-analysis/src/analyzers/fid/mod.rs | 13 +- .../kuna-analysis/src/analyzers/flirt/mod.rs | 13 +- .../kuna-analysis/src/analyzers/objc/mod.rs | 2 +- .../kuna-analysis/src/analyzers/pdb/locate.rs | 26 +- .../src/analyzers/sourcelang/mod.rs | 10 +- .../crates/kuna-analysis/src/image_bytes.rs | 18 +- .../kuna-analysis/src/loader/elf_shdr.rs | 2 +- .../kuna-analysis/src/loader/macho_fat.rs | 42 +- decompiler/crates/kuna-analysis/src/passes.rs | 19 +- decompiler/crates/kuna-console/Cargo.toml | 4 + .../crates/kuna-console/src/assertions.rs | 3 + .../crates/kuna-console/src/decompile_step.rs | 3 + decompiler/crates/kuna-console/src/engine.rs | 137 ++++- .../crates/kuna-console/src/ifacedecomp.rs | 7 + .../crates/kuna-console/src/kuna_console.rs | 11 +- .../kuna-console/tests/verify_loadoptions.rs | 368 ++++++++++++++ .../kuna-decomp/src/infra/architecture.rs | 54 +- .../kuna-decomp/src/infra/decompile_drive.rs | 2 + .../src/p0_knowledge/kuna_dyldcache.rs | 9 +- .../src/p0_knowledge/kuna_goabi.rs | 15 +- .../src/p0_knowledge/kuna_ifuncfpret.rs | 4 +- .../src/p0_knowledge/kuna_kernelcache.rs | 11 +- .../src/p0_knowledge/kuna_libctypes.rs | 12 +- .../src/p0_knowledge/kuna_loadoptions.rs | 474 ++++++++++++++++++ .../src/p0_knowledge/kuna_macho_arm64e.rs | 3 +- .../src/p0_knowledge/kuna_objcivars.rs | 6 +- .../src/p0_knowledge/kuna_objcsdk.rs | 10 +- .../src/p0_knowledge/kuna_option_dispatch.rs | 23 +- .../src/p0_knowledge/kuna_ppc32picimports.rs | 12 +- .../src/p0_knowledge/kuna_ppcapu.rs | 2 +- .../src/p0_knowledge/kuna_sourcelang.rs | 30 +- .../src/p0_knowledge/kuna_symbolnamechars.rs | 8 +- .../kuna-decomp/src/p0_knowledge/mod.rs | 1 + .../src/p5_types/kuna_calltargettype.rs | 2 +- 34 files changed, 1215 insertions(+), 141 deletions(-) create mode 100644 decompiler/crates/kuna-console/tests/verify_loadoptions.rs create mode 100644 decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_loadoptions.rs diff --git a/decompiler/crates/kuna-analysis/src/analyzers/fid/mod.rs b/decompiler/crates/kuna-analysis/src/analyzers/fid/mod.rs index d24ee0c316..39a3ea3081 100644 --- a/decompiler/crates/kuna-analysis/src/analyzers/fid/mod.rs +++ b/decompiler/crates/kuna-analysis/src/analyzers/fid/mod.rs @@ -68,7 +68,7 @@ use crate::fid::skipper::{InstructionSkipper, X86InstructionSkipper}; /// kept out of the per-decompilation `--option` surface in v1 (a real `--fidb /// ` flag is deferred, `docs/history/fid-design.md` §7.2). Absent / empty ⇒ the pass /// is inert (no DB to match against). -pub const FID_DB_ENV: &str = "kuna_fid_db"; +pub const FID_DB_ENV: &str = kuna_decomp::kuna_loadoptions::FID_DB_ENV; /// The FID matching consumer pass — the kuna analog of Ghidra's FID identification /// analyzer (`FidServiceImpl` over the function-id query service), the third @@ -101,10 +101,10 @@ impl AnalysisPass for FidPass { let Some(listing) = ctx.listing else { return out; }; - let Some(path) = std::env::var_os(FID_DB_ENV).filter(|p| !p.is_empty()) else { + let Some(path) = fid_db_path() else { return out; }; - let (report, db) = kuna_fidapply::open(std::path::Path::new(&path), ctx.arch); + let (report, db) = kuna_fidapply::open(&path, ctx.arch); out.fid_databases.push(report); let Some(db) = db else { return out; @@ -130,6 +130,13 @@ impl AnalysisPass for FidPass { } } +/// The `.fid` database the load names: `LoadOptions::fid_db`, else +/// [`FID_DB_ENV`]. `None` when neither names one. +fn fid_db_path() -> Option { + use kuna_decomp::kuna_loadoptions::{env, scoped}; + scoped(|o| o.fid_db.clone()).unwrap_or_else(env::fid_db) +} + /// Fingerprint a discovered function using the same extent and masks as the FID pass. /// The error distinguishes an undersized function from a failed instruction mask. pub fn fingerprint_function( diff --git a/decompiler/crates/kuna-analysis/src/analyzers/flirt/mod.rs b/decompiler/crates/kuna-analysis/src/analyzers/flirt/mod.rs index d0046b8052..dd15cf27f7 100644 --- a/decompiler/crates/kuna-analysis/src/analyzers/flirt/mod.rs +++ b/decompiler/crates/kuna-analysis/src/analyzers/flirt/mod.rs @@ -35,7 +35,7 @@ use crate::pass::{AnalysisCtx, AnalysisOutput, AnalysisPass, FidMatch, Phase}; use flirt_sig::{Endian, Matcher, Module, Signature, FLAG_STARTUP}; /// The environment variable that lists the `.sig` files. -pub const FLIRT_SIGS_ENV: &str = "KUNA_FLIRT_SIGS"; +pub const FLIRT_SIGS_ENV: &str = kuna_decomp::kuna_loadoptions::FLIRT_SIGS_ENV; /// Matching rounds: a module whose reference names a function another module /// names is accepted in the round after that name exists. @@ -75,12 +75,17 @@ impl AnalysisPass for FlirtPass { } } -/// Parse the signature files `KUNA_FLIRT_SIGS` names (files, or directories +/// Parse the signature files the load names (`LoadOptions::flirt_sigs`, else +/// `KUNA_FLIRT_SIGS`: files, or directories /// whose `*.sig` files are all taken) that apply to a binary of `format`: /// startup signatures and those whose header excludes the file type are left /// out. Unreadable or malformed files are skipped. pub fn load_sigs(format: object::BinaryFormat) -> Vec { - let Some(list) = std::env::var_os(FLIRT_SIGS_ENV) else { return Vec::new() }; + use kuna_decomp::kuna_loadoptions::{env, scoped}; + let list = scoped(|o| o.flirt_sigs.clone()).unwrap_or_else(env::flirt_sigs); + if list.is_empty() { + return Vec::new(); + } let ft = match format { object::BinaryFormat::Elf => Some(18), object::BinaryFormat::Pe => Some(11), @@ -89,7 +94,7 @@ pub fn load_sigs(format: object::BinaryFormat) -> Vec { _ => None, }; let mut paths: Vec = Vec::new(); - for p in std::env::split_paths(&list) { + for p in list { match std::fs::read_dir(&p) { Ok(dir) => { let mut sigs: Vec<_> = dir diff --git a/decompiler/crates/kuna-analysis/src/analyzers/objc/mod.rs b/decompiler/crates/kuna-analysis/src/analyzers/objc/mod.rs index 79ef6c1177..efc4676fe6 100644 --- a/decompiler/crates/kuna-analysis/src/analyzers/objc/mod.rs +++ b/decompiler/crates/kuna-analysis/src/analyzers/objc/mod.rs @@ -114,7 +114,7 @@ impl AnalysisPass for ObjcMetadataPass { // The type-mapping context for IMP/ivar prototypes (PR-O2). let (ptr_size, word_size) = ctx.arch.data_org(); let tctx = TypeCtx::new(ctx.arch.types(), ptr_size, word_size); - let sdk = match kuna_decomp::kuna_objcsdk::sdk_path_from_env() { + let sdk = match kuna_decomp::kuna_objcsdk::sdk_path() { Some(root) => kuna_objcsdk::signatures(&root, &kuna_objcsdk::linked_frameworks(ctx.file, ctx.bytes)), None => HashMap::new(), }; diff --git a/decompiler/crates/kuna-analysis/src/analyzers/pdb/locate.rs b/decompiler/crates/kuna-analysis/src/analyzers/pdb/locate.rs index 77a09a74e3..6452cd0db2 100644 --- a/decompiler/crates/kuna-analysis/src/analyzers/pdb/locate.rs +++ b/decompiler/crates/kuna-analysis/src/analyzers/pdb/locate.rs @@ -41,16 +41,20 @@ use crate::pdb::codeview::CodeViewInfo; /// `fid` `kuna_fid_db` env gate (the FID-database external-artifact precedent): /// the `.pdb` source is kept OFF the per-decompilation `--option` surface (so the /// catalog carries a single on|off `pdb` settable, not a path-valued one). -pub const PDB_PATH_ENV: &str = "kuna_pdb_path"; +pub const PDB_PATH_ENV: &str = kuna_decomp::kuna_loadoptions::PDB_PATH_ENV; + +/// The explicitly supplied `.pdb` path: the load's `LoadOptions::pdb_path`, else +/// [`PDB_PATH_ENV`]. `None` when neither names one. +pub fn explicit_pdb_path() -> Option { + use kuna_decomp::kuna_loadoptions::{env, scoped}; + scoped(|o| o.pdb_path.clone()).unwrap_or_else(env::pdb_path) +} /// Resolve the explicitly supplied `.pdb` path from [`PDB_PATH_ENV`]. `None` when -/// the variable is unset or empty. +/// the variable is unset or empty. Reads only the environment; a load in +/// progress reads [`explicit_pdb_path`]. pub fn pdb_path_from_env() -> Option { - let p = std::env::var_os(PDB_PATH_ENV)?; - if p.is_empty() { - return None; - } - Some(PathBuf::from(p)) + kuna_decomp::kuna_loadoptions::env::pdb_path() } /// The ordered `.pdb` candidates for an image, most specific first (see the module @@ -65,7 +69,7 @@ pub fn pdb_candidates(image_path: Option<&Path>, cv: &CodeViewInfo) -> Vec Compiler { - match kuna_decomp::kuna_sourcelang::forced_from_env() { + match kuna_decomp::kuna_sourcelang::forced() { Some(lang) => Compiler::from_forced_lang(lang), None => detect_compiler(file, bytes), } @@ -259,7 +259,7 @@ pub fn effective_compiler(file: &object::File, bytes: &[u8]) -> Compiler { /// [`effective_compiler`] over raw image bytes (the `load file` entry point). pub fn effective_compiler_bytes(bytes: &[u8]) -> Compiler { - match kuna_decomp::kuna_sourcelang::forced_from_env() { + match kuna_decomp::kuna_sourcelang::forced() { Some(lang) => Compiler::from_forced_lang(lang), None => detect_compiler_bytes(bytes), } diff --git a/decompiler/crates/kuna-analysis/src/image_bytes.rs b/decompiler/crates/kuna-analysis/src/image_bytes.rs index 79ea0327f3..945a421085 100644 --- a/decompiler/crates/kuna-analysis/src/image_bytes.rs +++ b/decompiler/crates/kuna-analysis/src/image_bytes.rs @@ -17,10 +17,11 @@ impl ImageBytes { /// Mapping requires the input file to remain unchanged while it is analyzed. /// A kernel collection (`MH_FILESET`) declines: its entry is selected into an /// owned copy. + /// The load's `load_mmap` option (`KUNA_LOAD_MMAP=off` outside a load) + /// selects the owned path. pub fn map_thin_macho(path: &str) -> KunaResult> { - if std::env::var("KUNA_LOAD_MMAP") - .is_ok_and(|value| matches!(value.as_str(), "off" | "0" | "false")) - { + use kuna_decomp::kuna_loadoptions::{env, scoped}; + if !scoped(|o| o.load_mmap).unwrap_or_else(env::load_mmap) { return Ok(None); } let file = File::open(path).map_err(|error| { @@ -87,4 +88,15 @@ mod tests { let fileset = format!("{fixtures}/kernelcache/fileset.macho"); assert!(ImageBytes::map_thin_macho(&fileset).unwrap().is_none(), "MH_FILESET entry selection"); } + + #[test] + fn a_load_without_mmap_keeps_the_owned_path() { + use kuna_decomp::kuna_loadoptions::LoadOptions; + let thin = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/fixtures/macho_objc_arm64"); + let owned = LoadOptions { load_mmap: false, ..LoadOptions::default() }; + let _load = owned.enter(); + assert!(ImageBytes::map_thin_macho(thin).unwrap().is_none()); + let _load = LoadOptions::default().enter(); + assert!(ImageBytes::map_thin_macho(thin).unwrap().is_some()); + } } diff --git a/decompiler/crates/kuna-analysis/src/loader/elf_shdr.rs b/decompiler/crates/kuna-analysis/src/loader/elf_shdr.rs index 56b09e6f77..ad47de8de7 100644 --- a/decompiler/crates/kuna-analysis/src/loader/elf_shdr.rs +++ b/decompiler/crates/kuna-analysis/src/loader/elf_shdr.rs @@ -82,7 +82,7 @@ pub fn read_image_selected( Some(image) => image.map_err(std::io::Error::other)?, None => std::fs::read(path)?, }; - let choice = kuna_decomp::kuna_kernelcache::KernelcacheChoice::from_env(); + let choice = kuna_decomp::kuna_kernelcache::KernelcacheChoice::in_force(); let peeled = super::kuna_kernelcache::peel(bytes, &choice).map_err(std::io::Error::other)?; let bytes = super::macho_fat::peel_fat_image(peeled.bytes, pref); let (bytes, shdr) = tolerate_unusable_section_table(bytes); diff --git a/decompiler/crates/kuna-analysis/src/loader/macho_fat.rs b/decompiler/crates/kuna-analysis/src/loader/macho_fat.rs index 47b05e61e6..409b017f0b 100644 --- a/decompiler/crates/kuna-analysis/src/loader/macho_fat.rs +++ b/decompiler/crates/kuna-analysis/src/loader/macho_fat.rs @@ -123,22 +123,24 @@ pub fn fat_slice_offset(bytes: &[u8], pref: SlicePref) -> Option { Some((slice.as_ptr() as usize - bytes.as_ptr() as usize) as u64) // cast: a sub-slice's index } -/// The environment variable carrying a `--slice ` fat-slice override. -/// Read live (per image read) so a test — and the in-process CLI surfaces — can -/// set it without threading a token; empty/unset selects the default slice. -pub const SLICE_ENV: &str = "KUNA_MACHO_SLICE"; +/// The environment variable carrying a `--slice ` fat-slice override to +/// the `decomp_dbg` subprocess and the in-process CLI surfaces; empty/unset +/// selects the default slice. +pub const SLICE_ENV: &str = kuna_decomp::kuna_loadoptions::MACHO_SLICE_ENV; /// The slice preference a run asks for, in precedence order: an explicit -/// `--slice` token, else [`SLICE_ENV`], else the `--target` token's leading arch +/// `--slice` token, else the load's slice (`LoadOptions::macho_slice`, or +/// [`SLICE_ENV`] outside a load scope), else the `--target` token's leading arch /// stem, else the deterministic default. An explicit slice that names nothing /// recognizable stays the default rather than falling through to `--target`, so /// a typo'd `--slice` cannot be silently answered by the language override. pub fn slice_pref(slice: Option<&str>, target: Option<&str>) -> SlicePref { + use kuna_decomp::kuna_loadoptions::{env, scoped}; let explicit = slice .map(str::trim) .filter(|token| !token.is_empty()) .map(str::to_string) - .or_else(|| std::env::var(SLICE_ENV).ok().filter(|t| !t.trim().is_empty())); + .or_else(|| scoped(|o| o.macho_slice.clone()).unwrap_or_else(env::macho_slice)); if let Some(token) = explicit { return SlicePref::parse(&token); } @@ -234,29 +236,25 @@ mod tests { assert!(select_fat_slice(&[0x7f, b'E', b'L', b'F'], SlicePref::default()).is_none()); } - /// The preference order the CLI and the dispatch share. The environment - /// override is asserted through an explicit token rather than by setting the - /// var, because `std::env` is process-global and cargo runs these in - /// parallel. + /// The preference order the CLI and the dispatch share. A load scope stands + /// in for the environment, so the ambient `KUNA_MACHO_SLICE` cannot reach + /// these assertions. #[test] fn slice_pref_prefers_an_explicit_slice_over_a_target_stem() { + let _load = kuna_decomp::kuna_loadoptions::LoadOptions::default().enter(); assert_eq!( slice_pref(Some("arm64"), Some("x86:LE:64:default:gcc")).0, Some(Architecture::Aarch64), "--slice wins over the --target stem" ); - // The `--target`-only fallbacks read the environment first, so they are - // only meaningful with no ambient override set. - if std::env::var_os(SLICE_ENV).is_none() { - assert_eq!( - slice_pref(None, Some("x86:LE:64:default:gcc")).0, - Some(Architecture::X86_64), - "--target alone still steers the slice" - ); - // A blank token is not an override. - assert_eq!(slice_pref(Some(" "), Some("arm64")).0, Some(Architecture::Aarch64)); - assert_eq!(slice_pref(None, None).0, None); - } + assert_eq!( + slice_pref(None, Some("x86:LE:64:default:gcc")).0, + Some(Architecture::X86_64), + "--target alone still steers the slice" + ); + // A blank token is not an override. + assert_eq!(slice_pref(Some(" "), Some("arm64")).0, Some(Architecture::Aarch64)); + assert_eq!(slice_pref(None, None).0, None); // An unrecognized --slice stays the DEFAULT rather than falling through // to --target: a typo must not be answered by the language override. assert_eq!(slice_pref(Some("riscv-banana"), Some("arm64")).0, None); diff --git a/decompiler/crates/kuna-analysis/src/passes.rs b/decompiler/crates/kuna-analysis/src/passes.rs index 0f5219fbd8..86f0e12e35 100644 --- a/decompiler/crates/kuna-analysis/src/passes.rs +++ b/decompiler/crates/kuna-analysis/src/passes.rs @@ -18,18 +18,17 @@ use crate::loadimage_object::ObjectLoadImage; use crate::pass::{run_analyses, AnalysisCtx, AnalysisOutput, AnalysisPass}; use crate::sourcelang::Compiler; -pub const LOAD_OBJC_ENV: &str = "KUNA_LOAD_OBJC"; -pub const LOAD_STRINGS_ENV: &str = "KUNA_LOAD_STRINGS"; +pub use kuna_decomp::kuna_loadoptions::{LOAD_OBJC_ENV, LOAD_STRINGS_ENV}; +/// Whether the load runs pass `id`: the `objc` and `strings` passes can be +/// skipped by the load's options. fn load_pass_enabled(id: &str) -> bool { - let env = match id { - "objc" => LOAD_OBJC_ENV, - "strings" => LOAD_STRINGS_ENV, - _ => return true, - }; - !std::env::var(env).is_ok_and(|value| { - matches!(value.trim().to_ascii_lowercase().as_str(), "off" | "0" | "false") - }) + use kuna_decomp::kuna_loadoptions::{env, scoped}; + match id { + "objc" => scoped(|o| o.load_objc).unwrap_or_else(env::load_objc), + "strings" => scoped(|o| o.load_strings).unwrap_or_else(env::load_strings), + _ => true, + } } /// The default program-prep passes, in stage order. diff --git a/decompiler/crates/kuna-console/Cargo.toml b/decompiler/crates/kuna-console/Cargo.toml index 0f2ca0f4e3..b544557aaa 100644 --- a/decompiler/crates/kuna-console/Cargo.toml +++ b/decompiler/crates/kuna-console/Cargo.toml @@ -84,6 +84,10 @@ path = "tests/verify_macho_fat.rs" name = "verify_msvcfpconst" path = "tests/verify_msvcfpconst.rs" +[[test]] +name = "verify_loadoptions" +path = "tests/verify_loadoptions.rs" + [[test]] name = "verify_pdatachained" path = "tests/verify_pdatachained.rs" diff --git a/decompiler/crates/kuna-console/src/assertions.rs b/decompiler/crates/kuna-console/src/assertions.rs index dbc88c3028..a48c37278b 100644 --- a/decompiler/crates/kuna-console/src/assertions.rs +++ b/decompiler/crates/kuna-console/src/assertions.rs @@ -340,6 +340,7 @@ pub fn parse_hex_bytes(tok: &str) -> Result, String> { /// `readonly`/`volatile` precedent is the same constraint inverted — a range /// property stated after the symbols over it are mapped is silently inert. pub fn apply_image_scoped(prog: &mut ConsoleProgram) { + let _load = prog.arch().load_scope(); let directives = prog.assertions().to_vec(); for (i, directive) in directives.iter().enumerate() { let Body::Bytes { addr, data } = &directive.body else { @@ -382,6 +383,7 @@ fn overlay_bytes(prog: &mut ConsoleProgram, vma: u64, data: &[u8]) -> Result<(), /// it, which is how "you asked about a function this run did not decompile" /// becomes a report row instead of silence. pub fn apply_program_scoped(prog: &mut ConsoleProgram) { + let _load = prog.arch().load_scope(); let directives = prog.assertions().to_vec(); for (i, directive) in directives.iter().enumerate() { // A QUALIFIED `param`/`return` is a statement about the named function's @@ -1112,6 +1114,7 @@ pub fn apply_symbol_scoped( name: &str, single: bool, ) -> Option> { + let _load = prog.arch().load_scope(); let directives = prog.assertions().to_vec(); let mut applied_any = false; let mut touched: Vec<(usize, kuna_decomp::database::SymbolId, Option>)> = Vec::new(); diff --git a/decompiler/crates/kuna-console/src/decompile_step.rs b/decompiler/crates/kuna-console/src/decompile_step.rs index c260096be1..6f2fa4a549 100644 --- a/decompiler/crates/kuna-console/src/decompile_step.rs +++ b/decompiler/crates/kuna-console/src/decompile_step.rs @@ -146,6 +146,9 @@ pub fn decompile_one_prefollowed( proto_overrides: &[(Address, PrototypePieces)], prefollowed: Option, ) -> DecompileStep { + // The lazy callee materialization between the drives names functions, so + // it reads the program's load-time options too. + let _load = arch.load_scope(); let mut first = decompile_one_prefollowed_once( arch, name, diff --git a/decompiler/crates/kuna-console/src/engine.rs b/decompiler/crates/kuna-console/src/engine.rs index dd2535ddb1..7af51b94d3 100644 --- a/decompiler/crates/kuna-console/src/engine.rs +++ b/decompiler/crates/kuna-console/src/engine.rs @@ -47,6 +47,7 @@ use kuna_decomp::architecture::Architecture; use kuna_decomp::options::register_option_elements; use kuna_decomp::raw_arch::RawBinaryArchitectureCapability; use kuna_decomp::kuna_memoryspecs::IntoSpecRoot; +use kuna_decomp::kuna_loadoptions::LoadOptions; use kuna_decomp::sleigh_arch::{register_sleigh_arch_ids, LanguageDatabase, SleighArchitecture}; use kuna_decomp::xml_arch::XmlArchitectureCapability; @@ -878,6 +879,7 @@ impl ConsoleProgram { /// hook): scan the binaryimage symbols read at load. `None` if no symbol of /// that name exists. pub fn lookup_symbol(&self, name: &str) -> Option
{ + let _load = self.arch().load_scope(); // (kuna `symbolnamebound`) `self.symbols` carries the bounded spelling, // so bound the query too: `load function ` must accept both the // binary's ORIGINAL name and the one the listing renders. Idempotent, and @@ -963,6 +965,7 @@ impl ConsoleProgram { /// Whether any symbol or loader alias is spelled exactly `name` (the /// spelling a name selector would match first). O(1) on the name index. pub(crate) fn has_symbol_spelled(&self, name: &str) -> bool { + let _load = self.arch().load_scope(); let name = &*kuna_decomp::kuna_symbolnamebound::bound_scope_path(name, "::"); (self.symbols.lookup(name).is_some() || self.anonymous_address(name).is_some()) || self.loader_aliases.values().any(|names| names.iter().any(|alias| alias == name)) @@ -1270,6 +1273,7 @@ impl ConsoleProgram { /// to the raw symbol table when it declines, which on a PE import pair is /// the pointer slot rather than the thunk `decompile` would have picked. pub fn find_entry_by_name(&self, want: &str) -> Option { + let _load = self.arch().load_scope(); // (kuna `symbolnamebound`) The enumeration reports the bounded spelling, // so bound the query too -- a caller holding the binary's ORIGINAL name // must still resolve. Idempotent, so the bounded spelling resolves as @@ -1404,6 +1408,7 @@ impl ConsoleProgram { &self, selector: &EntrySelector, ) -> Result { + let _load = self.arch().load_scope(); match selector { EntrySelector::Name(want) => { // (kuna `symbolnamebound`) The enumeration reports the bounded @@ -2264,6 +2269,7 @@ impl ConsoleProgram { /// `::RTTI_*` labels the `rtti` pass emits). The verification hook for /// the gated MSVC-RTTI recovery e2e. pub fn has_symbol_named(&self, full_name: &str) -> bool { + let _load = self.arch().load_scope(); let db = &self.arch().symboltab; let (scope, base) = db.resolve_scope_from_symbol_name(full_name, "::", None); match scope { @@ -2321,6 +2327,7 @@ impl ConsoleProgram { &mut self, entries: &[(String, Address)], ) -> KunaResult { + let _load = self.arch().load_scope(); self.callwide_changed(); self.anonymous_symbols.take(); let taken: std::collections::HashSet = @@ -2366,6 +2373,7 @@ impl ConsoleProgram { /// the table is skipped (the C++ `addFunction` no-ops on an existing match via /// `queryFunction`). pub fn read_loader_symbols(&mut self) -> KunaResult<()> { + let _load = self.arch().load_scope(); self.callwide_changed(); let type_code = self.arch().types().get_type_code()?; let min_size = self.arch().min_funcsymbol_size; @@ -2412,6 +2420,7 @@ impl ConsoleProgram { } fn register_symbol_record(&mut self, name: &str, addr: Address, sole: bool) { + let _load = self.arch().load_scope(); self.callwide_changed(); self.anonymous_symbols.take(); if let Some(prior) = self.anonymous_address(name) { @@ -2588,6 +2597,7 @@ impl ConsoleProgram { name: Option<&str>, size: int4, ) -> KunaResult { + let _load = self.arch().load_scope(); let addr = match addr.get_space() { Some(space) => Address::new( std::rc::Rc::clone(space), @@ -2648,6 +2658,7 @@ impl ConsoleProgram { /// address has a body. Used by an addressed CLI run that needs a symbol for /// prototype directives before selection. pub fn ensure_function_symbol(&mut self, addr: Address) -> KunaResult { + let _load = self.arch().load_scope(); let addr = match addr.get_space() { Some(space) => Address::new( std::rc::Rc::clone(space), @@ -2880,6 +2891,7 @@ impl ConsoleProgram { /// defensively re-gated facts merge into the same `merged` output committed /// below. With Listing off, this whole block is skipped. pub fn commit_pending_analysis(&mut self) -> KunaResult<()> { + let _load = self.arch().load_scope(); if !self.pending_synthetic_entries.is_empty() { // (kuna `rawdiscover`) A headerless image reaches this point holding // only the `--entry` addresses the caller typed, because the @@ -4289,13 +4301,52 @@ pub fn bootstrap_from_image_with_isa_with_specs( target: &str, spec_roots: &[impl IntoSpecRoot], isa: Option, +) -> KunaResult { + bootstrap_image(path, target, spec_roots, isa, &LoadOptions::from_env(), true) +} + +/// [`bootstrap_from_image_with_isa`] with the load-time options explicit, as +/// [`bootstrap_from_object_with_options`] takes them. +pub fn bootstrap_from_image_with_options( + path: &str, + target: &str, + spec_roots: &[impl IntoSpecRoot], + isa: Option, + options: &LoadOptions, +) -> KunaResult { + bootstrap_image(path, target, spec_roots, isa, options, false) +} + +/// The image dispatch behind both image bootstraps; `from_env` as in +/// [`bootstrap_object`]. +fn bootstrap_image( + path: &str, + target: &str, + spec_roots: &[impl IntoSpecRoot], + isa: Option, + options: &LoadOptions, + from_env: bool, ) -> KunaResult { if file_is_te_image(path) { + let _load = enter_load(options, from_env); bootstrap_from_te_with_specs(path, target, spec_roots, isa) } else if let Some(bytes) = file_mz_dos_image(path) { + let _load = enter_load(options, from_env); bootstrap_from_mz_bytes_with_specs(path, &bytes, target, spec_roots, isa) } else { - bootstrap_from_object_with_isa_with_specs(path, target, spec_roots, isa) + bootstrap_object(path, target, spec_roots, isa, options, from_env) + } +} + +/// Put a load's options in force on this thread: `options` for an +/// explicit-options load, the live environment for an entry point that takes +/// none (`options` is then [`LoadOptions::from_env`], read for the bootstrap's +/// own decisions). The program built under the scope keeps that choice. +fn enter_load(options: &LoadOptions, from_env: bool) -> kuna_decomp::kuna_loadoptions::LoadScope { + if from_env { + kuna_decomp::kuna_loadoptions::enter_environment() + } else { + options.enter() } } @@ -4646,9 +4697,9 @@ fn mips_t9_probe( }) } -/// Explicit-input-context form of [`bootstrap_from_object`]. Front-ends use -/// this to avoid process-global state; the interactive console wrapper above -/// retains the environment bridge used by `load file`. +/// Explicit-input-context form of [`bootstrap_from_object`]: the ARM decode +/// mode comes from `isa`, the load-time options from the environment. +/// [`bootstrap_from_object_with_options`] takes both explicitly. pub fn bootstrap_from_object_with_isa( path: &str, target: &str, @@ -4666,6 +4717,41 @@ pub fn bootstrap_from_object_with_isa_with_specs( spec_roots: &[impl IntoSpecRoot], isa: Option, ) -> KunaResult { + bootstrap_object(path, target, spec_roots, isa, &LoadOptions::from_env(), true) +} + +/// [`bootstrap_from_object`] with every setting explicit: the ARM decode mode +/// and the load-time options. Nothing on this path reads or writes the process +/// environment, so threads may load images at once with different options. The +/// program keeps `options`, and its analysis commit and decompiles read them. +pub fn bootstrap_from_object_with_options( + path: &str, + target: &str, + spec_roots: &[impl IntoSpecRoot], + isa: Option, + options: &LoadOptions, +) -> KunaResult { + bootstrap_object(path, target, spec_roots, isa, options, false) +} + +/// The object bootstrap. `from_env` is set by the entry points that take no +/// options: their load and the program it builds read the environment live, and +/// they publish the image's Go toolchain version in +/// [`GOVERSION_ENV`](kuna_decomp::kuna_goreg::GOVERSION_ENV) (cleared for a +/// non-Go image). An explicit-options load leaves the environment alone and +/// never reads it. +fn bootstrap_object( + path: &str, + target: &str, + spec_roots: &[impl IntoSpecRoot], + isa: Option, + options: &LoadOptions, + from_env: bool, +) -> KunaResult { + // The caller's options are in force from the first byte read: a gate read + // outside every scope falls back to the environment, which an explicit + // load must never consult. + let _caller = enter_load(options, from_env); let target = kuna_analysis::loadimage_object::explicit_language_target(target).unwrap_or(""); let registry = build_registry(); @@ -4681,7 +4767,7 @@ pub fn bootstrap_from_object_with_isa_with_specs( }; // (kuna `kernelcache`) IMG4 / IM4P / LZFSE / complzss unwrapping and MH_FILESET // entry selection, ahead of every other normalization. - let choice = kuna_decomp::kuna_kernelcache::KernelcacheChoice::from_env(); + let choice = kuna_decomp::kuna_kernelcache::KernelcacheChoice::in_force(); let peeled = kuna_analysis::loader::kuna_kernelcache::peel(bytes, &choice) .map_err(KunaError::lowlevel)?; if let Some(note) = &peeled.note { @@ -4696,7 +4782,7 @@ pub fn bootstrap_from_object_with_isa_with_specs( // exactly as for a thin Mach-O. The slice preference is `--slice` (env // `KUNA_MACHO_SLICE`) or the `--target` stem, else x86-64 → arm64 → first. // Non-fat input is untouched (the peel returns the bytes verbatim). - let bytes = select_macho_slice(bytes, target); + let bytes = select_macho_slice(bytes, options.macho_slice.as_deref(), target); // (kuna) ELF section-table tolerance: the second normalization at this same // canonical point. An ELF's section table is link-time metadata, but `object` // validates it eagerly, so one corrupt half-word rejected an image whose @@ -4726,6 +4812,27 @@ pub fn bootstrap_from_object_with_isa_with_specs( for note in &load_notes { kuna_base::notes::say_once(path, &format!("[kuna] {note}")); } + // (kuna goreg) The Go toolchain version is a BUILD-TIME fact: the goreg + // shells are interned inside the cspec decode during the sleigh build + // below, upstream of every per-image fact write, and their layouts drift + // by release (go1.23's g.syscallbp, go1.25's dropped gobuf.ret). It travels + // on the load options, which every gate below reads; this scope replaces the + // caller's until the load returns. + // An environment-reading load takes the version from the image alone: a + // stale `KUNA_GOVERSION` must not outlive a non-Go image. + let mut options = options.clone(); + let stated = if from_env { None } else { options.go_version }; + options.go_version = + kuna_analysis::loader::gobuildinfo::go_toolchain_version(&bytes).or(stated); + if from_env { + match options.go_version { + Some((maj, min)) => { + std::env::set_var(kuna_decomp::kuna_goreg::GOVERSION_ENV, format!("{maj}.{min}")) + } + None => std::env::remove_var(kuna_decomp::kuna_goreg::GOVERSION_ENV), + } + } + let _load = enter_load(&options, from_env); let explicit_isa = isa.is_some(); let import_proofs = kuna_analysis::loader::ImportProofScope::enter(&bytes); // LoadImageBfd(filename) + open(): parse the ELF (machine, segments, symbols). @@ -4736,18 +4843,6 @@ pub fn bootstrap_from_object_with_isa_with_specs( // machine → SLEIGH language id), unless an explicit target overrides it; // buildSpecFile -> buildTranslator -> the Architecture::init tail follow. let arch_type = String::from_utf8_lossy(&loader.get_arch_type()).into_owned(); - // (kuna goreg) The Go toolchain version is a BUILD-TIME fact: the goreg - // shells are interned inside the cspec decode during the sleigh build - // below, upstream of every per-image fact write, and their layouts drift - // by release (go1.23's g.syscallbp, go1.25's dropped gobuf.ret). Carry it - // on the loadtime-env tier; a non-Go image clears the variable so a stale - // value from an earlier load in this process cannot leak in. - match kuna_analysis::loader::gobuildinfo::go_toolchain_version(&bytes) { - Some((maj, min)) => { - std::env::set_var(kuna_decomp::kuna_goreg::GOVERSION_ENV, format!("{maj}.{min}")) - } - None => std::env::remove_var(kuna_decomp::kuna_goreg::GOVERSION_ENV), - } let mut sleigh = build_sleigh_for_loader( path, target, @@ -6415,14 +6510,14 @@ fn is_object_binary(bytes: &[u8]) -> bool { /// A fat header that cannot be peeled (unparsable, or no usable slice) is left /// untouched, so the downstream `object::File::parse` produces the existing /// "Unsupported file format" error rather than this silently mis-loading. -fn select_macho_slice(bytes: Vec, target: &str) -> Vec { +fn select_macho_slice(bytes: Vec, slice: Option<&str>, target: &str) -> Vec { use kuna_analysis::loader::macho_fat::{peel_fat_image, slice_pref}; - peel_fat_image(bytes, slice_pref(None, Some(target))) + peel_fat_image(bytes, slice_pref(slice, Some(target))) } /// An IMG4 / IM4P / compressed kernelcache, admitted while `kernelcache` is not off. fn is_apple_container(bytes: &[u8]) -> bool { - kuna_decomp::kuna_kernelcache::KernelcacheChoice::from_env() + kuna_decomp::kuna_kernelcache::KernelcacheChoice::in_force() != kuna_decomp::kuna_kernelcache::KernelcacheChoice::Off && kuna_analysis::loader::kuna_kernelcache::is_container(bytes) } diff --git a/decompiler/crates/kuna-console/src/ifacedecomp.rs b/decompiler/crates/kuna-console/src/ifacedecomp.rs index 60c59a7a60..6f0ececa64 100644 --- a/decompiler/crates/kuna-console/src/ifacedecomp.rs +++ b/decompiler/crates/kuna-console/src/ifacedecomp.rs @@ -4367,6 +4367,13 @@ pub fn register_console_commands(status: &mut IfaceStatus) { /// /// Returns after writing the diagnostic and calling [`IfaceStatus::evaluate_error`]. pub fn execute(status: &mut IfaceStatus) { + // A command runs under the loaded program's load-time options, so a + // program bootstrapped with explicit options keeps them in the console. + let _load = status + .get_data(DECOMPILE_MODULE) + .and_then(|data| data.as_any().downcast_ref::()) + .and_then(|dcp| dcp.conf.as_ref()) + .map(|prog| prog.arch().load_scope()); match status.run_command() { Ok(_) => return, Err(err) => { diff --git a/decompiler/crates/kuna-console/src/kuna_console.rs b/decompiler/crates/kuna-console/src/kuna_console.rs index fdc5fc9de9..b6c434a8c2 100644 --- a/decompiler/crates/kuna-console/src/kuna_console.rs +++ b/decompiler/crates/kuna-console/src/kuna_console.rs @@ -159,16 +159,17 @@ pub fn kuna_live_value(conf: &Architecture, option: &str) -> Option PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("../../..") + .canonicalize() + .unwrap() +} + +fn specs() -> String { + root().join("specs").to_str().unwrap().to_string() +} + +fn fauxware() -> PathBuf { + root().join("decompiler/crates/kuna-analysis/tests/fixtures/fauxware") +} + +/// An x86-64 relocatable object whose `.go.buildinfo` names `version`, which +/// is all Kuna needs to take it for a Go image of that release. +fn go_object(version: &str) -> Vec { + let mut obj = Object::new(BinaryFormat::Elf, Architecture::X86_64, Endianness::Little); + let text = obj.section_id(StandardSection::Text); + let code = [0x48, 0x8d, 0x04, 0x1f, 0xc3]; // lea rax, [rdi+rbx]; ret + let value = obj.append_section_data(text, &code, 16); + obj.add_symbol(Symbol { + name: b"main.add".to_vec(), + value, + size: code.len() as u64, + kind: SymbolKind::Text, + scope: SymbolScope::Linkage, + weak: false, + section: SymbolSection::Section(text), + flags: SymbolFlags::None, + }); + let mut blob = b"\xff Go buildinf:".to_vec(); + blob.extend([8, 2]); // pointer size, inline strings + blob.resize(32, 0); + blob.push(u8::try_from(version.len()).unwrap()); + blob.extend(version.as_bytes()); + let info = obj.add_section( + Vec::new(), + b".go.buildinfo".to_vec(), + SectionKind::ReadOnlyData, + ); + obj.append_section_data(info, &blob, 16); + obj.write().unwrap() +} + +fn load(path: &Path, options: &LoadOptions) -> ConsoleProgram { + let mut prog = + bootstrap_from_object_with_options(path.to_str().unwrap(), "", &[specs()], None, options) + .expect("bootstrap with built processor specs"); + prog.commit_pending_analysis() + .expect("analysis commit succeeds"); + prog +} + +/// The compiler field of the program's language id (`gcc`, `golang`, …). +fn compiler(prog: &ConsoleProgram) -> String { + prog.arch().archid.rsplit(':').next().unwrap().to_string() +} + +fn go_image() -> PathBuf { + let path = common::scratch_file("loadoptions-go", "o"); + std::fs::write(&path, go_object("go1.25.3")).unwrap(); + path +} + +/// What a load decided: its compiler spec, source language and Go release. +type Outcome = (String, SourceLang, Option<(u16, u16)>); + +fn outcome(prog: &ConsoleProgram) -> Outcome { + ( + compiler(prog), + prog.arch().source_lang_detected, + prog.arch().load_options().go_version, + ) +} + +static ENVIRONMENT: Mutex<()> = Mutex::new(()); + +const ENV_VARS: [&str; 7] = [ + "KUNA_GOABI", + "KUNA_SOURCELANG", + "KUNA_KERNELCACHE", + "KUNA_GOVERSION", + "KUNA_LOAD_OBJC", + "KUNA_LOAD_STRINGS", + "KUNA_LOAD_MMAP", +]; + +fn snapshot() -> Vec> { + ENV_VARS.iter().map(std::env::var_os).collect() +} + +#[test] +fn concurrent_loads_keep_their_own_options() { + let go = go_image(); + let fauxware = fauxware(); + let go_off = LoadOptions { + goabi: false, + ..LoadOptions::default() + }; + let forced_c = LoadOptions { + sourcelang: Some(SourceLang::C), + ..LoadOptions::default() + }; + let forced_go = LoadOptions { + sourcelang: Some(SourceLang::Go), + ..LoadOptions::default() + }; + let stated_go = LoadOptions { + go_version: Some((1, 22)), + ..forced_go.clone() + }; + let cases = [ + ( + go.clone(), + LoadOptions::default(), + ("golang".into(), SourceLang::Go, Some((1, 25))), + ), + ( + go.clone(), + go_off, + ("gcc".into(), SourceLang::Go, Some((1, 25))), + ), + ( + go.clone(), + forced_c, + ("gcc".into(), SourceLang::C, Some((1, 25))), + ), + ( + fauxware.clone(), + LoadOptions::default(), + ("gcc".into(), SourceLang::C, None), + ), + ( + fauxware.clone(), + forced_go, + ("golang".into(), SourceLang::Go, None), + ), + ( + fauxware, + stated_go.clone(), + ("golang".into(), SourceLang::Go, Some((1, 22))), + ), + ( + go.clone(), + stated_go, + ("golang".into(), SourceLang::Go, Some((1, 25))), + ), + ]; + for _ in 0..3 { + let start = Arc::new(Barrier::new(cases.len())); + let loads: Vec<_> = cases + .iter() + .cloned() + .map(|(path, options, want)| { + let start = Arc::clone(&start); + std::thread::spawn(move || { + start.wait(); + let prog = load(&path, &options); + assert_eq!(prog.arch().load_options().goabi, options.goabi); + assert_eq!(prog.arch().load_options().sourcelang, options.sourcelang); + (outcome(&prog), want) + }) + }) + .collect(); + for load in loads { + let (got, want) = load.join().unwrap(); + assert_eq!(got, want); + } + } + std::fs::remove_file(go).unwrap(); +} + +/// What the `objc` and `strings` load passes left on two programs `bootstrap` +/// loads: the recovered `-[Greeter greet:]` and the `s_400915` string symbol. +fn load_pass_facts(bootstrap: impl Fn(&str) -> ConsoleProgram) -> (bool, bool) { + let fixtures = root().join("decompiler/crates/kuna-analysis/tests/fixtures"); + let mut objc = bootstrap(fixtures.join("macho_objc_arm64").to_str().unwrap()); + objc.arch_mut().set_kuna_option("objc", "on").unwrap(); + objc.commit_pending_analysis().unwrap(); + let mut strings = bootstrap(fixtures.join("fauxware").to_str().unwrap()); + strings.commit_pending_analysis().unwrap(); + let arch = strings.arch(); + let global = arch.symboltab.get_global_scope().unwrap(); + ( + objc.has_symbol_named("-[Greeter greet:]"), + !arch.symboltab.query_by_name(global, "s_400915").is_empty(), + ) +} + +#[test] +fn explicit_options_neither_read_nor_write_the_environment() { + let _env = ENVIRONMENT.lock().unwrap_or_else(|e| e.into_inner()); + let go = go_image(); + std::env::set_var("KUNA_GOABI", "off"); + std::env::set_var("KUNA_SOURCELANG", "rust"); + std::env::set_var("KUNA_KERNELCACHE", "off"); + std::env::set_var("KUNA_GOVERSION", "1.20"); + std::env::set_var("KUNA_LOAD_OBJC", "off"); + std::env::set_var("KUNA_LOAD_STRINGS", "off"); + std::env::set_var("KUNA_LOAD_MMAP", "off"); + let before = snapshot(); + let prog = load(&go, &LoadOptions::default()); + assert_eq!( + outcome(&prog), + ("golang".into(), SourceLang::Go, Some((1, 25))) + ); + let explicit = load_pass_facts(|path| { + bootstrap_from_object_with_options(path, "", &[specs()], None, &LoadOptions::default()) + .unwrap() + }); + assert_eq!(explicit, (true, true), "an explicit load runs the passes the environment skips"); + assert_eq!( + snapshot(), + before, + "an explicit load leaves the environment alone" + ); + let from_env = load_pass_facts(|path| bootstrap_from_object(path, "", &[specs()]).unwrap()); + assert_eq!(from_env, (false, false), "an environment load skips them"); + for name in ENV_VARS { + std::env::remove_var(name); + } + std::fs::remove_file(go).unwrap(); +} + +/// The language a load of the fat Mach-O fixture chose: its slice's processor. +fn fat_slice(options: &LoadOptions) -> String { + let fat = root().join("decompiler/crates/kuna-analysis/tests/fixtures/macho_fat"); + let prog = load(&fat, options); + let archid = prog.arch().archid.clone(); + archid.split(':').next().unwrap().to_string() +} + +#[test] +fn an_explicit_load_picks_its_own_fat_slice() { + let _env = ENVIRONMENT.lock().unwrap_or_else(|e| e.into_inner()); + std::env::set_var("KUNA_MACHO_SLICE", "arm64"); + let default = fat_slice(&LoadOptions::default()); + let x86 = fat_slice(&LoadOptions { + macho_slice: Some("x86_64".into()), + ..LoadOptions::default() + }); + std::env::set_var("KUNA_MACHO_SLICE", "x86_64"); + let arm = fat_slice(&LoadOptions { + macho_slice: Some("arm64".into()), + ..LoadOptions::default() + }); + std::env::remove_var("KUNA_MACHO_SLICE"); + assert_eq!( + default, "x86", + "no slice named: the default, not the environment's" + ); + assert_eq!(x86, "x86"); + assert_eq!(arm, "AARCH64"); +} + +/// Decompile and print `name` through the console, as a session would. +fn console_decompile(prog: ConsoleProgram, name: &str) -> String { + use kuna_console::ifacedecomp::{ + execute, register_decomp_commands, IfaceDecompData, DECOMPILE_MODULE, + }; + use kuna_console::ifaceterm::ConsoleCommands; + let cmds: Vec = [ + format!("load function {name}"), + "decompile".into(), + "print C".into(), + ] + .into(); + let count = cmds.len(); + let mut status = ConsoleCommands::into_status(cmds); + register_decomp_commands(&mut status); + let data = status.get_data_mut(DECOMPILE_MODULE).unwrap(); + let dcp = data.as_any_mut().downcast_mut::().unwrap(); + dcp.conf = Some(prog); + for _ in 0..count { + execute(&mut status); + } + status.optr.clone() +} + +/// Everything done with an explicitly loaded program -- the load, the commit, +/// listing, lookups, declarations, decompiles on both drivers, printing -- +/// reads the program's own options, never a gate's environment fallback. +#[test] +fn an_explicit_program_never_reads_the_environment() { + use kuna_console::project::{decompile_entry, DecompileOptions}; + use kuna_decomp::kuna_loadoptions::environment_fallbacks; + let go = go_image(); + let fat = root().join("decompiler/crates/kuna-analysis/tests/fixtures/macho_fat"); + let before = environment_fallbacks(); + for path in [fauxware(), fat, go.clone()] { + let mut prog = load(&path, &LoadOptions::default()); + let entries = prog.function_entries_executable(); + assert!(!entries.is_empty(), "{} lists functions", path.display()); + for entry in entries.iter().take(6) { + assert!(prog.find_entry_at(entry.addr.get_offset()).is_some()); + prog.has_symbol_named(&entry.name); + let result = decompile_entry(&mut prog, entry.clone(), &DecompileOptions::default()); + assert!(result.code.is_some_and(|c| !c.is_empty()), "{:?}", result.error); + } + let first = entries[0].clone(); + prog.ensure_function_symbol(first.addr.clone()).unwrap(); + prog.declare_function(first.addr.clone(), Some("outer::inner::renamed"), 0) + .unwrap(); + assert!(prog.has_symbol_named("outer::inner::renamed")); + assert!(prog.find_entry_by_name("outer::inner::renamed").is_some()); + let c = console_decompile(prog, "outer::inner::renamed"); + assert!(c.contains("renamed("), "the console decompiles it:\n{c}"); + } + assert_eq!( + environment_fallbacks(), + before, + "a gate read the environment on an explicit program's thread" + ); + std::fs::remove_file(go).unwrap(); +} + +/// `set_kuna_option` changes the program's own load options, and the change +/// does not reach a later load with explicit options. +#[test] +fn set_kuna_option_changes_its_program_not_later_explicit_loads() { + let _env = ENVIRONMENT.lock().unwrap_or_else(|e| e.into_inner()); + let go = go_image(); + std::env::remove_var("KUNA_GOABI"); + let mut prog = load(&go, &LoadOptions::default()); + prog.arch_mut().set_kuna_option("goabi", "off").unwrap(); + assert!(!prog.arch().load_options().goabi); + assert_eq!(compiler(&load(&go, &LoadOptions::default())), "golang"); + for name in ENV_VARS { + std::env::remove_var(name); + } + std::fs::remove_file(go).unwrap(); +} + +/// A program loaded with explicit options reports its own `kernelcache` +/// choice after the environment changes. +#[test] +fn an_explicit_program_ignores_a_later_environment_change() { + use kuna_console::kuna_console::kuna_live_value; + let _env = ENVIRONMENT.lock().unwrap_or_else(|e| e.into_inner()); + std::env::remove_var("KUNA_KERNELCACHE"); + let explicit = load(&fauxware(), &LoadOptions::default()); + std::env::set_var("KUNA_KERNELCACHE", "off"); + let explicit_value = kuna_live_value(explicit.arch(), "kernelcache").unwrap().into_owned(); + std::env::remove_var("KUNA_KERNELCACHE"); + assert_eq!(explicit_value, "auto", "the explicit program kept its options"); +} diff --git a/decompiler/crates/kuna-decomp/src/infra/architecture.rs b/decompiler/crates/kuna-decomp/src/infra/architecture.rs index bfdda9a670..4c360b96cf 100644 --- a/decompiler/crates/kuna-decomp/src/infra/architecture.rs +++ b/decompiler/crates/kuna-decomp/src/infra/architecture.rs @@ -1549,6 +1549,13 @@ pub struct Architecture { pub analysis_armwalkmode: bool, /// Caller-local PPC32 PIC import proof, applied during flow following. pub analysis_ppc32picimports: crate::kuna_ppc32picimports::Mode, + /// (kuna) The load-time options this program was loaded with: the ones in + /// force when it was built, updated by a load-time `option` command. + /// [`Self::load_scope`] puts them back in force for later analysis. + load_options: Rc, + /// (kuna) Built with no options in force: an entry point that takes none + /// loaded this program, so its gates keep reading the environment live. + load_options_from_env: bool, /// Validated import slots. Never registered as executable FunctionSymbols. pub ppc32_pic_imports: std::collections::BTreeMap, /// (kuna) Gate the x86-64 IFUNC (`R_X86_64_IRELATIVE`) `.plt.sec`/`.iplt` @@ -2239,6 +2246,8 @@ impl Architecture { symbol_snapshots: RefCell::new(SymbolSnapshots::default()), kuna_snapshot_cache: std::env::var_os("KUNA_NO_SYMBOL_SNAPSHOT_CACHE").is_none(), + load_options: crate::kuna_loadoptions::LoadOptions::in_force(), + load_options_from_env: !crate::kuna_loadoptions::options_in_force(), decode_sla: None, decode_truncations: None, @@ -2850,7 +2859,11 @@ impl Architecture { self.analysis_ptrentry = true; // (kuna) pointer-referenced ARM entries (ARM-only, needs listing) self.analysis_poolentry = true; // (kuna) ARM literal-pool inference (ARM-only, needs listing + aif) self.analysis_armwalkmode = true; // (kuna) ARM walk decodes in the mode its control flow carries - self.analysis_ppc32picimports = crate::kuna_ppc32picimports::Mode::loaded(); // (kuna) abi when the loader applied it, else off + self.analysis_ppc32picimports = if self.load_options_from_env { + crate::kuna_ppc32picimports::Mode::loaded() + } else { + crate::kuna_ppc32picimports::Mode::loaded_with(self.load_options.ppc32picimports) + }; // (kuna) abi when the loader applied it, else off self.analysis_goabi = true; // (kuna) option goabi default auto: a Go image takes the vendored golang cspec self.analysis_symbolnamechars = crate::kuna_symbolnamechars::NameChars::Safe; // (kuna) DIV-94: symbol-name sanitizing defaults to `safe` -- the structural set only, a measured no-op on every name a real toolchain emits self.analysis_dwarf = true; @@ -2978,7 +2991,9 @@ impl Architecture { /// `maxlinewidth`/`commentstyle`, which are recorded-no-op printer stubs /// with no state to reset. pub fn reset_wire_defaults(&mut self) { - // The env-backed PPC32 PIC mode is process state, so a wire reset clears it too. + // The load-time PPC32 PIC mode is program and process state, so a wire + // reset clears both. + self.load_options_mut().ppc32picimports = crate::kuna_ppc32picimports::Mode::Off; crate::kuna_ppc32picimports::Mode::Off.set_env(); self.reset_defaults(); self.print.context = crate::printlanguage::PrintContext::new(); @@ -4154,7 +4169,7 @@ impl Architecture { self.infer_ptr_spaces.push(Rc::clone(&spc)); self.symboltab.add_range(scope, Rc::clone(&spc), first, last); let gptr = - crate::kuna_goreg::g_pointer_type(self.types(), crate::kuna_goreg::GoLayout::from_env())?; + crate::kuna_goreg::g_pointer_type(self.types(), self.go_layout())?; let addr = Address::new(spc, first); let (sid, _eref) = self.symboltab.add_symbol_mapped(scope, "g", gptr, &addr, &Address::new_invalid())?; @@ -4638,6 +4653,39 @@ impl Architecture { // ifacedecomp porter confirmed were absent — w9x-arch-engine-glue) // ----------------------------------------------------------------------- + /// (kuna) The load-time options this program was loaded with. + pub fn load_options(&self) -> &crate::kuna_loadoptions::LoadOptions { + &self.load_options + } + + /// (kuna) Change a load-time option for this program's later analysis. + pub fn load_options_mut(&mut self) -> &mut crate::kuna_loadoptions::LoadOptions { + Rc::make_mut(&mut self.load_options) + } + + /// (kuna) Put this program's load-time options in force on this thread until + /// the scope drops, so the gates that run after the load read them. A + /// program loaded with no options defers to the environment instead, so its + /// gates read their variables live. + pub fn load_scope(&self) -> crate::kuna_loadoptions::LoadScope { + if self.load_options_from_env { + crate::kuna_loadoptions::enter_environment() + } else { + crate::kuna_loadoptions::enter(Rc::clone(&self.load_options)) + } + } + + /// (kuna `goreg`) The runtime layouts of the image's Go toolchain release. + fn go_layout(&self) -> crate::kuna_goreg::GoLayout { + if self.load_options_from_env { + return crate::kuna_goreg::GoLayout::from_env(); + } + match self.load_options.go_version { + Some((major, minor)) => crate::kuna_goreg::GoLayout::from_version(major, minor), + None => crate::kuna_goreg::GoLayout::baseline(), + } + } + /// Borrow the data-type factory (C++ `glb->types`). pub fn types(&self) -> &dyn TypeFactory { &*self.types diff --git a/decompiler/crates/kuna-decomp/src/infra/decompile_drive.rs b/decompiler/crates/kuna-decomp/src/infra/decompile_drive.rs index 96fbd584f5..49be97d508 100644 --- a/decompiler/crates/kuna-decomp/src/infra/decompile_drive.rs +++ b/decompiler/crates/kuna-decomp/src/infra/decompile_drive.rs @@ -977,6 +977,7 @@ fn follow_flow_on_fd(arch: &mut Architecture, fd: Funcdata) -> KunaResult KunaResult { + let _load = arch.load_scope(); crate::kuna_mipsunaligned::paint(arch); // C++ Funcdata::followFlow(baddr, eaddr): a function carrying a declared byte // extent restricts flow to it; size 0 keeps the unbounded default the whole @@ -1172,6 +1173,7 @@ fn run_jumptable_pipeline( /// already ported in `action.rs`); this drives it through the engine-side /// [`ActionContext`] so the warning-emission points are observable. fn run_pipeline(arch: &mut Architecture, fd: &mut Funcdata) -> KunaResult { + let _load = arch.load_scope(); // C++ allacts.setCurrent("decompile") derives the filtered decompile root // (idempotent if already current). arch.allacts.set_current("decompile")?; diff --git a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_dyldcache.rs b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_dyldcache.rs index 4815bdaa0a..319db0f11a 100644 --- a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_dyldcache.rs +++ b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_dyldcache.rs @@ -20,7 +20,8 @@ pub const DYLDIMAGE_ENV: &str = "KUNA_DYLD_IMAGE"; pub const DYLDCROSSREFS_ENV: &str = "KUNA_DYLDCROSSREFS"; pub fn dyldcrossrefs_enabled() -> bool { - crate::options::env_toggle(DYLDCROSSREFS_ENV, true) + crate::kuna_loadoptions::scoped(|o| o.dyldcrossrefs) + .unwrap_or_else(crate::kuna_loadoptions::env::dyldcrossrefs) } pub fn set_dyldcrossrefs_env(on: bool) { @@ -36,9 +37,11 @@ pub fn image_name(value: &str) -> Option<&str> { Some(value.trim()).filter(|v| !v.is_empty() && *v != "none") } -/// The image the run selected through [`DYLDIMAGE_ENV`]. +/// The image the load in progress selected (`LoadOptions::dyldimage`, else +/// [`DYLDIMAGE_ENV`]). pub fn selected_image() -> Option { - std::env::var(DYLDIMAGE_ENV).ok().and_then(|v| image_name(&v).map(str::to_string)) + crate::kuna_loadoptions::scoped(|o| o.dyldimage.clone()) + .unwrap_or_else(crate::kuna_loadoptions::env::dyldimage) } /// Apply `option dyldimage|dyldcrossrefs VALUE`, or `None` for another option. diff --git a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_goabi.rs b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_goabi.rs index 9e5104135c..a2f019fef8 100644 --- a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_goabi.rs +++ b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_goabi.rs @@ -19,11 +19,12 @@ //! other architecture and every non-Go image is untouched, and the engine's //! existing default-model retry covers a missing spec. //! -//! ## Why the override is an env-var bridge +//! ## Why the override is a load-time option //! //! The language id is composed inside `load file`, upstream of every -//! per-function `option` command — the same seam as `sourcelang`, and the -//! same [`GOABI_ENV`] bridge pattern. +//! per-function `option` command — the same seam as `sourcelang` — so it +//! reads `LoadOptions::goabi` ([`crate::kuna_loadoptions`]), which the CLI +//! carries in [`GOABI_ENV`]. //! //! ## The `.abi0` binding that is deliberately NOT here //! @@ -38,18 +39,16 @@ //! a Go image takes the cspec default (`abi-internal`), which is also what //! Ghidra does. -use crate::options::env_toggle; - /// Environment variable carrying the `goabi` choice into the load path. /// Absent or any value other than the off-tokens means **enabled** (`auto`). pub const GOABI_ENV: &str = "KUNA_GOABI"; -/// Whether golang-cspec auto-selection is enabled for this process. +/// Whether golang-cspec auto-selection is enabled for the load in progress. pub fn goabi_env_enabled() -> bool { - env_toggle(GOABI_ENV, true) + crate::kuna_loadoptions::scoped(|o| o.goabi).unwrap_or_else(crate::kuna_loadoptions::env::goabi) } -/// Bridge a `set_kuna_option("goabi", val)` toggle to [`GOABI_ENV`] so a later +/// Export a `set_kuna_option("goabi", val)` toggle to [`GOABI_ENV`] so a later /// `load file` in the same process sees the choice. pub fn set_goabi_env(on: bool) { std::env::set_var(GOABI_ENV, if on { "auto" } else { "off" }); diff --git a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_ifuncfpret.rs b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_ifuncfpret.rs index 56b822c12a..bcf12e1fad 100644 --- a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_ifuncfpret.rs +++ b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_ifuncfpret.rs @@ -32,10 +32,10 @@ pub const IFUNCFPRET_ENV: &str = "KUNA_IFUNCFPRET"; /// Whether IFUNC PLT-stub naming is enabled for this process (the loader gate). /// Default **on**: only an explicit off-token in [`IFUNCFPRET_ENV`] disables it. pub fn ifuncfpret_enabled() -> bool { - crate::options::env_toggle(IFUNCFPRET_ENV, true) + crate::kuna_loadoptions::scoped(|o| o.ifuncfpret).unwrap_or_else(crate::kuna_loadoptions::env::ifuncfpret) } -/// Bridge a `set_kuna_option("ifuncfpret", val)` toggle to [`IFUNCFPRET_ENV`] so a +/// Export a `set_kuna_option("ifuncfpret", val)` toggle to [`IFUNCFPRET_ENV`] so a /// later `load file` in the *same* process sees the choice. pub fn set_ifuncfpret_env(on: bool) { std::env::set_var(IFUNCFPRET_ENV, if on { "on" } else { "off" }); diff --git a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_kernelcache.rs b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_kernelcache.rs index 69a25494a6..373107b61c 100644 --- a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_kernelcache.rs +++ b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_kernelcache.rs @@ -39,7 +39,16 @@ impl KernelcacheChoice { } } - /// The choice in force for this process; an unset or unparsable value is `auto`. + /// The choice of the load in progress (`LoadOptions::kernelcache`, else + /// [`KERNELCACHE_ENV`]). + pub fn in_force() -> Self { + crate::kuna_loadoptions::scoped(|o| o.kernelcache.clone()) + .unwrap_or_else(crate::kuna_loadoptions::env::kernelcache) + } + + /// The choice [`KERNELCACHE_ENV`] states; an unset or unparsable value is + /// `auto`. Reads only the environment; a load in progress reads + /// [`Self::in_force`]. pub fn from_env() -> Self { std::env::var(KERNELCACHE_ENV).ok().and_then(|v| Self::parse(&v)).unwrap_or_default() } diff --git a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_libctypes.rs b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_libctypes.rs index 0cb0a3691a..8c148cd56c 100644 --- a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_libctypes.rs +++ b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_libctypes.rs @@ -78,14 +78,8 @@ pub enum LibcTypesLayout { /// validator — `Architecture::set_kuna_option` rejects a bad value at the /// `option` line, where the operator can see it. pub fn libctypes_layout() -> LibcTypesLayout { - match std::env::var(LIBCTYPES_ENV) { - Ok(v) => match v.trim().to_ascii_lowercase().as_str() { - "off" | "0" | "false" => LibcTypesLayout::Off, - "glibc" => LibcTypesLayout::Glibc, - _ => LibcTypesLayout::Opaque, - }, - Err(_) => LibcTypesLayout::Opaque, // unset => default-on, opaque - } + crate::kuna_loadoptions::scoped(|o| o.libctypes) + .unwrap_or_else(crate::kuna_loadoptions::env::libctypes) } /// Whether the named libc aggregate types are enabled for this process. @@ -105,7 +99,7 @@ pub const AGGREGATE_NAMES: &[&str] = &[ "utmpx", ]; -/// Bridge a `set_kuna_option("libctypes", val)` choice to [`LIBCTYPES_ENV`] so a +/// Export a `set_kuna_option("libctypes", val)` choice to [`LIBCTYPES_ENV`] so a /// later `load file` in the *same* process sees it (the interactive /// `option libctypes glibc` before `load file` path; the CLI sets the env var on /// the subprocess directly). diff --git a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_loadoptions.rs b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_loadoptions.rs new file mode 100644 index 0000000000..0578a25aa5 --- /dev/null +++ b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_loadoptions.rs @@ -0,0 +1,474 @@ +//! (kuna) Load-time options: the settings the loader, the analysis tier and the +//! architecture build read before any `option` command can run. +//! +//! A caller fills a [`LoadOptions`] and passes it to a bootstrap that +//! takes one (`kuna_console::engine::bootstrap_from_object_with_options`). The +//! bootstrap enters a [`LoadScope`] on the loading thread, the gates read the +//! options through [`scoped`], and the new `Architecture` keeps a copy, so the +//! analysis commit and every later decompile of that program read the same +//! values. Nothing on that path reads or writes the process environment, so two +//! images can load at once with different settings. +//! +//! The environment is the CLI's channel: the `kuna` CLI exports each +//! `--option` gate onto the `decomp_dbg` subprocess, and the console's +//! `load file` turns it back into options with [`LoadOptions::from_env`]. A +//! gate read outside any scope falls back to its environment variable. + +use std::cell::{Cell, RefCell}; +use std::path::PathBuf; +use std::rc::Rc; + +use crate::kuna_kernelcache::KernelcacheChoice; +use crate::kuna_libctypes::LibcTypesLayout; +use crate::kuna_ppc32picimports::Mode as Ppc32PicMode; +use crate::kuna_sourcelang::SourceLang; +use crate::kuna_symbolnamechars::NameChars; +use crate::options::env_toggle; + +/// The `--slice` fat Mach-O slice override. +pub const MACHO_SLICE_ENV: &str = "KUNA_MACHO_SLICE"; +/// The external `.pdb` file to apply. +pub const PDB_PATH_ENV: &str = "kuna_pdb_path"; +/// The external `.fid` function-ID database to apply. +pub const FID_DB_ENV: &str = "kuna_fid_db"; +/// The FLIRT `.sig` files, or directories of them, to match. +pub const FLIRT_SIGS_ENV: &str = "KUNA_FLIRT_SIGS"; +/// `off` skips the Objective-C metadata pass at load. +pub const LOAD_OBJC_ENV: &str = "KUNA_LOAD_OBJC"; +/// `off` skips the string-literal pass at load. +pub const LOAD_STRINGS_ENV: &str = "KUNA_LOAD_STRINGS"; +/// `off` copies a thin Mach-O into memory instead of mapping it. +pub const LOAD_MMAP_ENV: &str = "KUNA_LOAD_MMAP"; + +/// Every load-time setting, one field per option. [`Default`] is the shipped +/// default of each option, the value an unset environment variable means. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct LoadOptions { + /// `ppc32picimports`; only `abi` acts at load time. + pub ppc32picimports: Ppc32PicMode, + /// `ifuncfpret`: name x86-64 IFUNC PLT stubs. + pub ifuncfpret: bool, + /// `ppcapu`: select the PowerPC APU language for an image that needs it. + pub ppcapu: bool, + /// `kernelcache`: unwrap Apple firmware containers and pick a fileset entry. + pub kernelcache: KernelcacheChoice, + /// `goabi`: select the golang compiler spec for a Go image. + pub goabi: bool, + /// `symbolnamechars`: how symbol-name characters are sanitized. + pub symbolnamechars: NameChars, + /// `libctypes`: the named libc aggregate types. + pub libctypes: LibcTypesLayout, + /// `calltargettype`: give DWARF function pointers their prototypes. + pub calltargettype: bool, + /// `macho-arm64e`: select the Apple-Silicon spec for an arm64e Mach-O. + pub macho_arm64e: bool, + /// `sourcelang`: force the source language; `None` trusts detection. + pub sourcelang: Option, + /// `objcivars`: build Objective-C instance layouts. + pub objcivars: bool, + /// `objcsdk`: the SDK or header directory Objective-C signatures come from. + pub objcsdk: Option, + /// `dyldimage`: the dyld shared cache image to load; `None` takes the default. + pub dyldimage: Option, + /// `dyldcrossrefs`: name references into other dyld cache images. + pub dyldcrossrefs: bool, + /// The fat Mach-O slice to load (`--slice`); `None` takes the default. + pub macho_slice: Option, + /// An external `.pdb` file to apply. + pub pdb_path: Option, + /// An external `.fid` function-ID database to apply. + pub fid_db: Option, + /// FLIRT `.sig` files, or directories whose `*.sig` files are all taken. + pub flirt_sigs: Vec, + /// The Go toolchain release, which picks the `runtime.g` layout. The + /// bootstrap takes it from the image's build info, else from here. + pub go_version: Option<(u16, u16)>, + /// Run the Objective-C metadata pass at load, so `objc` has facts to commit. + pub load_objc: bool, + /// Run the string-literal pass at load, so `strings` has facts to commit. + pub load_strings: bool, + /// Map a thin Mach-O read-only rather than copy it into memory. + pub load_mmap: bool, +} + +impl Default for LoadOptions { + fn default() -> Self { + LoadOptions { + ppc32picimports: Ppc32PicMode::Off, + ifuncfpret: true, + ppcapu: false, + kernelcache: KernelcacheChoice::default(), + goabi: true, + symbolnamechars: NameChars::default(), + libctypes: LibcTypesLayout::Opaque, + calltargettype: true, + macho_arm64e: true, + sourcelang: None, + objcivars: true, + objcsdk: None, + dyldimage: None, + dyldcrossrefs: true, + macho_slice: None, + pdb_path: None, + fid_db: None, + flirt_sigs: Vec::new(), + go_version: None, + load_objc: true, + load_strings: true, + load_mmap: true, + } + } +} + +impl LoadOptions { + /// The options the environment states, in the vocabulary the CLI exports. + /// An unset or unrecognized variable leaves that option at its default. + pub fn from_env() -> Self { + LoadOptions { + ppc32picimports: env::ppc32picimports(), + ifuncfpret: env::ifuncfpret(), + ppcapu: env::ppcapu(), + kernelcache: env::kernelcache(), + goabi: env::goabi(), + symbolnamechars: env::symbolnamechars(), + libctypes: env::libctypes(), + calltargettype: env::calltargettype(), + macho_arm64e: env::macho_arm64e(), + sourcelang: env::sourcelang(), + objcivars: env::objcivars(), + objcsdk: env::objcsdk(), + dyldimage: env::dyldimage(), + dyldcrossrefs: env::dyldcrossrefs(), + macho_slice: env::macho_slice(), + pdb_path: env::pdb_path(), + fid_db: env::fid_db(), + flirt_sigs: env::flirt_sigs(), + go_version: env::go_version(), + load_objc: env::load_objc(), + load_strings: env::load_strings(), + load_mmap: env::load_mmap(), + } + } + + /// The options in force on this thread: the innermost scope's, else the + /// environment's. + pub fn in_force() -> Rc { + CURRENT + .with(|current| current.borrow().clone()) + .unwrap_or_else(|| { + FALLBACKS.with(|count| count.set(count.get() + 1)); + Rc::new(LoadOptions::from_env()) + }) + } + + /// Make these the options in force on this thread until the scope drops. + pub fn enter(&self) -> LoadScope { + enter(Rc::new(self.clone())) + } + + /// Write the named option's value to the environment variable a later + /// `load file` in this process reads. `Architecture::set_kuna_option` + /// already does this for the option it sets; this serves a caller that + /// changed the options through `Architecture::load_options_mut`. A load + /// with explicit options never needs it. A name that is not a load-time + /// option is ignored. + pub fn export_env(&self, option: &str) { + use crate::*; + match option { + "ppc32picimports" => self.ppc32picimports.set_env(), + "ifuncfpret" => kuna_ifuncfpret::set_ifuncfpret_env(self.ifuncfpret), + "ppcapu" => kuna_ppcapu::set_ppcapu_env(self.ppcapu), + "kernelcache" => kuna_kernelcache::set_kernelcache_env(&self.kernelcache), + "goabi" => kuna_goabi::set_goabi_env(self.goabi), + "symbolnamechars" => { + kuna_symbolnamechars::set_symbolnamechars_env(self.symbolnamechars) + } + "libctypes" => kuna_libctypes::set_libctypes_env_layout(self.libctypes), + "calltargettype" => std::env::set_var( + kuna_calltargettype::CALLTARGETTYPE_ENV, + if self.calltargettype { "on" } else { "off" }, + ), + "macho-arm64e" => kuna_macho_arm64e::set_macho_arm64e_env(self.macho_arm64e), + "sourcelang" => kuna_sourcelang::set_sourcelang_env(self.sourcelang), + "objcivars" => kuna_objcivars::set_objcivars_env(self.objcivars), + "objcsdk" => kuna_objcsdk::set_objcsdk_env( + self.objcsdk.as_deref().and_then(|p| p.to_str()), + ), + "dyldimage" => { + kuna_dyldcache::set_dyldimage_env(self.dyldimage.as_deref().unwrap_or("none")) + } + "dyldcrossrefs" => kuna_dyldcache::set_dyldcrossrefs_env(self.dyldcrossrefs), + _ => {} + } + } +} + +thread_local! { + static CURRENT: RefCell>> = const { RefCell::new(None) }; + static FALLBACKS: Cell = const { Cell::new(0) }; +} + +/// Make `options` the options in force on this thread until the scope drops. +pub fn enter(options: Rc) -> LoadScope { + let previous = CURRENT.with(|current| current.replace(Some(options))); + LoadScope { previous } +} + +/// Hand this thread back to the environment until the scope drops: every gate +/// reads its environment variable when it runs. A program built under this +/// scope keeps reading the environment through +/// [`crate::infra::architecture::Architecture::load_scope`]. +pub fn enter_environment() -> LoadScope { + let previous = CURRENT.with(|current| current.replace(None)); + LoadScope { previous } +} + +/// Whether some options are in force on this thread. Unlike [`scoped`], asking +/// does not count as an environment read. +pub(crate) fn options_in_force() -> bool { + CURRENT.with(|current| current.borrow().is_some()) +} + +/// `read` applied to the options in force on this thread, or `None` outside +/// every scope (the caller then reads its environment variable). +pub fn scoped(read: impl FnOnce(&LoadOptions) -> T) -> Option { + let read = CURRENT.with(|current| current.borrow().as_deref().map(read)); + if read.is_none() { + FALLBACKS.with(|count| count.set(count.get() + 1)); + } + read +} + +/// How many reads of the options on this thread found no scope in force and +/// fell back to the environment. A caller that loads with explicit options can +/// check that its calls leave this unchanged. +pub fn environment_fallbacks() -> u64 { + FALLBACKS.with(Cell::get) +} + +/// Restores the enclosing options when dropped. Scopes nest, and each belongs +/// to the thread that entered it. +#[must_use = "the options leave force when the scope drops"] +pub struct LoadScope { + previous: Option>, +} + +impl Drop for LoadScope { + fn drop(&mut self) { + let previous = self.previous.take(); + CURRENT.with(|current| *current.borrow_mut() = previous); + } +} + +/// Each option read from its environment variable, in the vocabulary the CLI +/// exports: the fallback a gate takes outside every scope. +pub mod env { + use super::*; + + fn path(name: &str) -> Option { + std::env::var_os(name) + .filter(|p| !p.is_empty()) + .map(PathBuf::from) + } + + pub fn ppc32picimports() -> Ppc32PicMode { + std::env::var(crate::kuna_ppc32picimports::ENV).map_or(Ppc32PicMode::Off, |value| { + Ppc32PicMode::from_env_value(&value) + }) + } + + pub fn ifuncfpret() -> bool { + env_toggle(crate::kuna_ifuncfpret::IFUNCFPRET_ENV, true) + } + + pub fn ppcapu() -> bool { + env_toggle(crate::kuna_ppcapu::PPCAPU_ENV, false) + } + + pub fn kernelcache() -> KernelcacheChoice { + KernelcacheChoice::from_env() + } + + pub fn goabi() -> bool { + env_toggle(crate::kuna_goabi::GOABI_ENV, true) + } + + pub fn symbolnamechars() -> NameChars { + match std::env::var(crate::kuna_symbolnamechars::SYMBOLNAMECHARS_ENV) { + Ok(v) => NameChars::parse(&v).unwrap_or_default(), + Err(_) => NameChars::default(), + } + } + + pub fn libctypes() -> LibcTypesLayout { + match std::env::var(crate::kuna_libctypes::LIBCTYPES_ENV) { + Ok(v) => match v.trim().to_ascii_lowercase().as_str() { + "off" | "0" | "false" => LibcTypesLayout::Off, + "glibc" => LibcTypesLayout::Glibc, + _ => LibcTypesLayout::Opaque, + }, + Err(_) => LibcTypesLayout::Opaque, + } + } + + pub fn calltargettype() -> bool { + env_toggle(crate::kuna_calltargettype::CALLTARGETTYPE_ENV, true) + } + + pub fn macho_arm64e() -> bool { + env_toggle(crate::kuna_macho_arm64e::MACHO_ARM64E_ENV, true) + } + + pub fn sourcelang() -> Option { + SourceLang::parse_forced(&std::env::var(crate::kuna_sourcelang::SOURCELANG_ENV).ok()?) + } + + pub fn objcivars() -> bool { + match std::env::var(crate::kuna_objcivars::OBJCIVARS_ENV) { + Ok(v) => { + !matches!(v.trim().to_ascii_lowercase().as_str(), "off" | "0" | "false" | "no") + } + Err(_) => true, + } + } + + pub fn objcsdk() -> Option { + crate::kuna_objcsdk::sdk_path_from_env() + } + + pub fn dyldimage() -> Option { + std::env::var(crate::kuna_dyldcache::DYLDIMAGE_ENV) + .ok() + .and_then(|v| crate::kuna_dyldcache::image_name(&v).map(str::to_string)) + } + + pub fn dyldcrossrefs() -> bool { + env_toggle(crate::kuna_dyldcache::DYLDCROSSREFS_ENV, true) + } + + pub fn macho_slice() -> Option { + std::env::var(MACHO_SLICE_ENV) + .ok() + .filter(|t| !t.trim().is_empty()) + } + + pub fn pdb_path() -> Option { + path(PDB_PATH_ENV) + } + + pub fn fid_db() -> Option { + path(FID_DB_ENV) + } + + pub fn flirt_sigs() -> Vec { + std::env::var_os(FLIRT_SIGS_ENV) + .map(|list| std::env::split_paths(&list).collect()) + .unwrap_or_default() + } + + fn load_pass(name: &str) -> bool { + !std::env::var(name).is_ok_and(|value| { + matches!(value.trim().to_ascii_lowercase().as_str(), "off" | "0" | "false") + }) + } + + pub fn load_objc() -> bool { + load_pass(LOAD_OBJC_ENV) + } + + pub fn load_strings() -> bool { + load_pass(LOAD_STRINGS_ENV) + } + + pub fn load_mmap() -> bool { + !std::env::var(LOAD_MMAP_ENV).is_ok_and(|value| matches!(value.as_str(), "off" | "0" | "false")) + } + + /// The `"."` release in `KUNA_GOVERSION`; `None` when it is + /// unset or unparsable. + pub fn go_version() -> Option<(u16, u16)> { + let value = std::env::var(crate::kuna_goreg::GOVERSION_ENV).ok()?; + let mut parts = value.trim().split('.'); + Some((parts.next()?.parse().ok()?, parts.next()?.parse().ok()?)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn scopes_nest_and_restore_per_thread() { + assert_eq!(scoped(|o| o.goabi), None); + let outer = LoadOptions { + goabi: false, + ..LoadOptions::default() + }; + let _outer = outer.enter(); + assert_eq!(scoped(|o| o.goabi), Some(false)); + { + let _inner = LoadOptions::default().enter(); + assert_eq!(scoped(|o| o.goabi), Some(true)); + std::thread::spawn(|| assert_eq!(scoped(|o| o.goabi), None)) + .join() + .unwrap(); + } + assert_eq!(scoped(|o| o.goabi), Some(false)); + assert!(!LoadOptions::in_force().goabi); + } + + #[test] + fn a_scope_restores_the_enclosing_options_after_a_panic() { + let _outer = LoadOptions { + dyldcrossrefs: false, + ..LoadOptions::default() + } + .enter(); + let unwound = std::panic::catch_unwind(|| { + let _inner = LoadOptions::default().enter(); + panic!("load failed"); + }); + assert!(unwound.is_err()); + assert_eq!(scoped(|o| o.dyldcrossrefs), Some(false)); + } + + #[test] + fn gates_read_the_scope_not_the_environment() { + let options = LoadOptions { + ifuncfpret: false, + ppcapu: true, + kernelcache: KernelcacheChoice::Off, + symbolnamechars: NameChars::Ident, + libctypes: LibcTypesLayout::Glibc, + calltargettype: false, + macho_arm64e: false, + sourcelang: Some(SourceLang::Rust), + ppc32picimports: Ppc32PicMode::Abi, + objcivars: false, + objcsdk: Some(PathBuf::from("/sdk")), + dyldimage: Some("libfoo.dylib".into()), + dyldcrossrefs: false, + ..LoadOptions::default() + }; + let _scope = options.enter(); + assert!(!crate::kuna_ifuncfpret::ifuncfpret_enabled()); + assert!(crate::kuna_ppcapu::ppcapu_enabled()); + assert_eq!(KernelcacheChoice::in_force(), KernelcacheChoice::Off); + assert_eq!( + crate::kuna_symbolnamechars::symbolnamechars_mode(), + NameChars::Ident + ); + assert_eq!( + crate::kuna_libctypes::libctypes_layout(), + LibcTypesLayout::Glibc + ); + assert!(!crate::kuna_calltargettype::dwarf_function_types()); + assert!(!crate::kuna_macho_arm64e::macho_arm64e_enabled()); + assert_eq!(crate::kuna_sourcelang::forced(), Some(SourceLang::Rust)); + assert!(crate::kuna_ppc32picimports::abi_enabled()); + assert!(!crate::kuna_objcivars::objcivars_enabled()); + assert_eq!(crate::kuna_objcsdk::sdk_path(), Some(PathBuf::from("/sdk"))); + assert_eq!(crate::kuna_dyldcache::selected_image().as_deref(), Some("libfoo.dylib")); + assert!(!crate::kuna_dyldcache::dyldcrossrefs_enabled()); + } +} diff --git a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_macho_arm64e.rs b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_macho_arm64e.rs index 1272893a4a..2cf61bf44b 100644 --- a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_macho_arm64e.rs +++ b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_macho_arm64e.rs @@ -19,7 +19,8 @@ pub const MACHO_ARM64E_ENV: &str = "KUNA_MACHO_ARM64E"; /// Whether an arm64e Mach-O selects the AppleSilicon spec in this process. pub fn macho_arm64e_enabled() -> bool { - crate::options::env_toggle(MACHO_ARM64E_ENV, true) + crate::kuna_loadoptions::scoped(|o| o.macho_arm64e) + .unwrap_or_else(crate::kuna_loadoptions::env::macho_arm64e) } /// Bridge a `set_kuna_option("macho-arm64e", val)` toggle to diff --git a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_objcivars.rs b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_objcivars.rs index 34b93de23a..afa0f44926 100644 --- a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_objcivars.rs +++ b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_objcivars.rs @@ -12,10 +12,8 @@ pub const OBJCIVARS_ENV: &str = "KUNA_OBJCIVARS"; /// Whether the pass builds the instance layouts. pub fn objcivars_enabled() -> bool { - match std::env::var(OBJCIVARS_ENV) { - Ok(v) => !matches!(v.trim().to_ascii_lowercase().as_str(), "off" | "0" | "false" | "no"), - Err(_) => true, - } + crate::kuna_loadoptions::scoped(|o| o.objcivars) + .unwrap_or_else(crate::kuna_loadoptions::env::objcivars) } /// Bridge a `set_kuna_option("objcivars", val)` choice to [`OBJCIVARS_ENV`] so diff --git a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_objcsdk.rs b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_objcsdk.rs index 8ad3c4cd0e..636ae9a4f3 100644 --- a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_objcsdk.rs +++ b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_objcsdk.rs @@ -13,7 +13,15 @@ use std::path::PathBuf; /// Unset, empty or `off` means no headers are read. pub const OBJCSDK_ENV: &str = "KUNA_OBJCSDK"; -/// The configured header root, if any. +/// The header root the load in progress names (`LoadOptions::objcsdk`, else +/// [`OBJCSDK_ENV`]), if any. +pub fn sdk_path() -> Option { + crate::kuna_loadoptions::scoped(|o| o.objcsdk.clone()) + .unwrap_or_else(crate::kuna_loadoptions::env::objcsdk) +} + +/// The header root [`OBJCSDK_ENV`] names, if any. Reads only the environment; +/// a load in progress reads [`sdk_path`]. pub fn sdk_path_from_env() -> Option { let v = std::env::var(OBJCSDK_ENV).ok()?; parse_option_value(&v).ok().flatten() diff --git a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_option_dispatch.rs b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_option_dispatch.rs index e50b547ab0..6ee528bebf 100644 --- a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_option_dispatch.rs +++ b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_option_dispatch.rs @@ -237,6 +237,7 @@ kuna_options! { self, p1; }, "goabi" => { let on = crate::kuna_goabi::parse_goabi(p1).map_err(KunaError::parse)?; + self.load_options_mut().goabi = on; crate::kuna_goabi::set_goabi_env(on); self.analysis_goabi = on; Ok(format!("Golang compiler-spec selection turned {}", if on { "auto" } else { "off" })) @@ -248,6 +249,7 @@ kuna_options! { self, p1; }, "sourcelang" => { let forced = crate::kuna_sourcelang::parse_option_value(p1).map_err(KunaError::parse)?; + self.load_options_mut().sourcelang = forced; crate::kuna_sourcelang::set_sourcelang_env(forced); self.source_lang_forced = forced; Ok(match forced { @@ -708,6 +710,7 @@ kuna_options! { self, p1; }; self.analysis_libctypes = layout != LibcTypesLayout::Off; self.analysis_libctypes_glibc = layout == LibcTypesLayout::Glibc; + self.load_options_mut().libctypes = layout; crate::kuna_libctypes::set_libctypes_env_layout(layout); Ok(format!( "Named libc aggregate types turned {}", @@ -794,12 +797,14 @@ kuna_options! { self, p1; let mode = crate::kuna_ppc32picimports::Mode::parse(p1) .ok_or_else(|| KunaError::parse("Expected ppc32picimports off|on|abi"))?; self.analysis_ppc32picimports = mode; + self.load_options_mut().ppc32picimports = mode; mode.set_env(); Ok(format!("PPC32 PIC import recovery {}", mode.as_str())) }, "ppcapu" => { let val = on_or_off(p1)?; self.analysis_ppcapu = val; + self.load_options_mut().ppcapu = val; crate::kuna_ppcapu::set_ppcapu_env(val); Ok(format!( "PowerPC APU-note language selection turned {}", @@ -809,6 +814,7 @@ kuna_options! { self, p1; "ifuncfpret" => { let val = on_or_off(p1)?; self.analysis_ifuncfpret = val; + self.load_options_mut().ifuncfpret = val; crate::kuna_ifuncfpret::set_ifuncfpret_env(val); Ok(format!( "IFUNC PLT-stub naming turned {}", @@ -818,10 +824,19 @@ kuna_options! { self, p1; "kernelcache" => { let (choice, msg) = crate::kuna_kernelcache::parse_kernelcache(p1)?; crate::kuna_kernelcache::set_kernelcache_env(&choice); + self.load_options_mut().kernelcache = choice; + Ok(msg) + }, + "dyldimage" => { + self.load_options_mut().dyldimage = + crate::kuna_dyldcache::image_name(p1).map(str::to_string); + Ok(crate::kuna_dyldcache::set_dyldimage(p1)) + }, + "dyldcrossrefs" => { + let msg = crate::kuna_dyldcache::set_dyldcrossrefs(p1)?; + self.load_options_mut().dyldcrossrefs = crate::options::on_or_off(p1)?; Ok(msg) }, - "dyldimage" => Ok(crate::kuna_dyldcache::set_dyldimage(p1)), - "dyldcrossrefs" => crate::kuna_dyldcache::set_dyldcrossrefs(p1), "symbolnamechars" => { let mode = crate::kuna_symbolnamechars::NameChars::parse(p1).ok_or_else(|| { KunaError::lowlevel(format!( @@ -829,6 +844,7 @@ kuna_options! { self, p1; )) })?; self.analysis_symbolnamechars = mode; + self.load_options_mut().symbolnamechars = mode; crate::kuna_symbolnamechars::set_symbolnamechars_env(mode); Ok(format!("symbol-name character sanitizing set to {}", mode.as_str())) }, @@ -932,6 +948,7 @@ kuna_options! { self, p1; "objcivars" => { let val = on_or_off(p1)?; self.analysis_objcivars = val; + self.load_options_mut().objcivars = val; crate::kuna_objcivars::set_objcivars_env(val); Ok(format!("Objective-C instance layouts turned {}", if val { "on" } else { "off" })) }, @@ -939,6 +956,7 @@ kuna_options! { self, p1; "objcsdk" => { let path = crate::kuna_objcsdk::parse_option_value(p1).map_err(KunaError::parse)?; crate::kuna_objcsdk::set_objcsdk_env(path.as_deref().and_then(|p| p.to_str())); + self.load_options_mut().objcsdk = path.clone(); Ok(match path { Some(p) => format!("Objective-C signatures read from {}", p.display()), None => "Objective-C SDK headers off".to_string(), @@ -948,6 +966,7 @@ kuna_options! { self, p1; "macho-arm64e" => { let val = on_or_off(p1)?; self.macho_arm64e = val; + self.load_options_mut().macho_arm64e = val; crate::kuna_macho_arm64e::set_macho_arm64e_env(val); Ok(format!( "Mach-O arm64e Apple-Silicon spec selection turned {}", diff --git a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_ppc32picimports.rs b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_ppc32picimports.rs index f87e0c7126..06a667057b 100644 --- a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_ppc32picimports.rs +++ b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_ppc32picimports.rs @@ -55,17 +55,25 @@ impl Mode { } pub fn from_env() -> Self { - std::env::var(ENV).map_or(Self::Off, |value| Self::from_env_value(&value)) + crate::kuna_loadoptions::env::ppc32picimports() } /// The mode a new `Architecture` starts in. pub fn loaded() -> Self { if abi_enabled() { Self::Abi } else { Self::Off } } + + /// The mode a new `Architecture` loaded with `load` starts in. + pub fn loaded_with(load: Self) -> Self { + if load == Self::Abi { Self::Abi } else { Self::Off } + } } +/// Whether the load in progress asks for `abi`. pub fn abi_enabled() -> bool { - Mode::from_env() == Mode::Abi + crate::kuna_loadoptions::scoped(|o| o.ppc32picimports) + .unwrap_or_else(crate::kuna_loadoptions::env::ppc32picimports) + == Mode::Abi } #[cfg(test)] diff --git a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_ppcapu.rs b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_ppcapu.rs index 1cccdaf7c0..93f22625f3 100644 --- a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_ppcapu.rs +++ b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_ppcapu.rs @@ -13,7 +13,7 @@ pub const PPCAPU_ENV: &str = "KUNA_PPCAPU"; /// Whether APU-based PowerPC language selection is enabled for this process. pub fn ppcapu_enabled() -> bool { - crate::options::env_toggle(PPCAPU_ENV, false) + crate::kuna_loadoptions::scoped(|o| o.ppcapu).unwrap_or_else(crate::kuna_loadoptions::env::ppcapu) } /// Bridge a `set_kuna_option("ppcapu", val)` toggle to [`PPCAPU_ENV`] so a later diff --git a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_sourcelang.rs b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_sourcelang.rs index de3af05507..7e0394dda3 100644 --- a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_sourcelang.rs +++ b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_sourcelang.rs @@ -13,18 +13,18 @@ //! `` bootstrap never runs the analyzer tier, so the fact stays //! [`SourceLang::Unknown`] on the datatest corpus by construction. //! -//! ## Why the override is an env-var bridge, not only an `Architecture` flag +//! ## Why the override is a load-time option, not only an `Architecture` flag //! //! Detection does not just set the engine fact — it selects analysis passes //! (`passes_for(compiler, format)`: the Go pclntab pass, the Rust no-return //! widening) *inside* `load file`, upstream of every per-function `option` -//! command. An override that should reach pass selection therefore travels as -//! a process environment variable ([`SOURCELANG_ENV`]) consulted by -//! `kuna_analysis::sourcelang::effective_compiler` and set by the `kuna` CLI -//! before the file is loaded. -//! The console `option sourcelang` arm still updates the engine fact for the -//! already-loaded program (the per-function rules read the fresh value), and -//! bridges the env so a later `load file` in the same process honours it. +//! command. An override that should reach pass selection therefore travels on +//! the load options (`LoadOptions::sourcelang`, [`crate::kuna_loadoptions`]), +//! read by `kuna_analysis::sourcelang::effective_compiler`; the `kuna` CLI +//! carries it in [`SOURCELANG_ENV`]. The console `option sourcelang` arm still +//! updates the engine fact for the already-loaded program (the per-function +//! rules read the fresh value), and the console exports it so a later +//! `load file` in the same session honours it. //! //! Default **auto** (= trust detection); `c`, `rust`, `go` and `swift` force //! the verdict. A `cpp` value is deliberately absent until something consumes @@ -94,13 +94,19 @@ impl SourceLang { /// verdict for analysis-pass selection and the engine fact alike. pub const SOURCELANG_ENV: &str = "KUNA_SOURCELANG"; -/// The forced source language in this process's environment, if any. +/// The forced source language of the load in progress, if any. +pub fn forced() -> Option { + crate::kuna_loadoptions::scoped(|o| o.sourcelang) + .unwrap_or_else(crate::kuna_loadoptions::env::sourcelang) +} + +/// The forced source language in this process's environment, if any. Reads +/// only the environment; a load in progress reads [`forced`]. pub fn forced_from_env() -> Option { - let v = std::env::var(SOURCELANG_ENV).ok()?; - SourceLang::parse_forced(&v) + crate::kuna_loadoptions::env::sourcelang() } -/// Bridge a `set_kuna_option("sourcelang", val)` choice to [`SOURCELANG_ENV`] +/// Export a `set_kuna_option("sourcelang", val)` choice to [`SOURCELANG_ENV`] /// so a later `load file` in the same process sees it (`auto` clears it). pub fn set_sourcelang_env(forced: Option) { match forced { diff --git a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_symbolnamechars.rs b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_symbolnamechars.rs index ebbcb66724..4a797f2a6a 100644 --- a/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_symbolnamechars.rs +++ b/decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_symbolnamechars.rs @@ -157,13 +157,11 @@ impl NameChars { /// a large image carries a few hundred thousand names and `std::env::var` is not /// free. pub fn symbolnamechars_mode() -> NameChars { - match std::env::var(SYMBOLNAMECHARS_ENV) { - Ok(v) => NameChars::parse(&v).unwrap_or_default(), - Err(_) => NameChars::default(), - } + crate::kuna_loadoptions::scoped(|o| o.symbolnamechars) + .unwrap_or_else(crate::kuna_loadoptions::env::symbolnamechars) } -/// Bridge a `set_kuna_option("symbolnamechars", val)` choice to +/// Export a `set_kuna_option("symbolnamechars", val)` choice to /// [`SYMBOLNAMECHARS_ENV`] so a later `load file` in the *same* process sees it /// (the interactive `option symbolnamechars … ` before `load file` path, and the /// in-process CLI drivers). diff --git a/decompiler/crates/kuna-decomp/src/p0_knowledge/mod.rs b/decompiler/crates/kuna-decomp/src/p0_knowledge/mod.rs index 0c8282ad53..64a20b1db1 100644 --- a/decompiler/crates/kuna-decomp/src/p0_knowledge/mod.rs +++ b/decompiler/crates/kuna-decomp/src/p0_knowledge/mod.rs @@ -55,6 +55,7 @@ pub mod kuna_langfacet; // (kuna) Named libc/POSIX aggregate types in the prototype tables option gate // (load-time, env-bridged). pub mod kuna_libctypes; +pub mod kuna_loadoptions; // (kuna) DWARF variant-part (discriminated-union) import option gate // (load-time, env-bridged) plus the recovered-layout side table. pub mod kuna_dwarfvariants; diff --git a/decompiler/crates/kuna-decomp/src/p5_types/kuna_calltargettype.rs b/decompiler/crates/kuna-decomp/src/p5_types/kuna_calltargettype.rs index 5b09ce2269..25cdf33336 100644 --- a/decompiler/crates/kuna-decomp/src/p5_types/kuna_calltargettype.rs +++ b/decompiler/crates/kuna-decomp/src/p5_types/kuna_calltargettype.rs @@ -107,7 +107,7 @@ pub const CALLTARGETTYPE_ENV: &str = "KUNA_CALLTARGETTYPE"; /// Does the DWARF mapper give a pointer to a `DW_TAG_subroutine_type` its /// prototype rather than a `void` pointee. pub fn dwarf_function_types() -> bool { - crate::options::env_toggle(CALLTARGETTYPE_ENV, true) + crate::kuna_loadoptions::scoped(|o| o.calltargettype).unwrap_or_else(crate::kuna_loadoptions::env::calltargettype) } /// (kuna) `ActionCallTargetType` -- type each indirect call's target from the From 1841233c7ab74c40c4f77e9a51a2c9212710b465 Mon Sep 17 00:00:00 2001 From: Evan Richter Date: Mon, 9 Nov 2026 14:26:47 -0700 Subject: [PATCH 3/4] [AUTOMATED] Let callers reuse one LanguageDatabase across bootstraps A library that loads many binaries can now scan and parse the .ldefs files once. language_database(spec_roots) scans the roots, and bootstrap_from_object_with_db and bootstrap_from_image_with_db take the database in place of the spec roots, alongside LoadOptions. A test loads two images on two threads from one database and shows that a bootstrap over it does not rescan the roots. Co-Authored-By: Claude Opus 5.5 --- decompiler/crates/kuna-console/src/engine.rs | 117 +++++++++++++-- .../crates/kuna-console/tests/integration.rs | 2 + .../tests/verify_language_database.rs | 137 ++++++++++++++++++ 3 files changed, 242 insertions(+), 14 deletions(-) create mode 100644 decompiler/crates/kuna-console/tests/verify_language_database.rs diff --git a/decompiler/crates/kuna-console/src/engine.rs b/decompiler/crates/kuna-console/src/engine.rs index 7af51b94d3..d49a82b142 100644 --- a/decompiler/crates/kuna-console/src/engine.rs +++ b/decompiler/crates/kuna-console/src/engine.rs @@ -32,6 +32,7 @@ mod lazy_inventory; +use std::borrow::Cow; use std::collections::{BTreeMap, BTreeSet, HashMap}; use std::path::Path; use std::rc::Rc; @@ -3669,6 +3670,40 @@ fn scan_language_database(spec_roots: &[impl IntoSpecRoot], registry: &IdRegistr Ok(db) } +/// Scan `spec_roots` into a [`LanguageDatabase`] once, for the `*_with_db` +/// bootstraps ([`bootstrap_from_object_with_db`], +/// [`bootstrap_from_image_with_db`]) to share. Every other bootstrap scans its +/// spec roots again: it lists their `.ldefs` and parses every one, whichever +/// language the image needs. A bootstrap over the database builds what the +/// same bootstrap over the roots builds. +/// +/// A bootstrap only reads the database, and the database is `Send + Sync`, so +/// threads may share one (behind an `Arc` or a `&'static`) and load images at +/// once. It holds the language descriptions and the roots, not the `.sla`, +/// `.pspec` and `.cspec` contents, which each bootstrap reads and decodes for +/// its own language. +pub fn language_database(spec_roots: &[impl IntoSpecRoot]) -> KunaResult { + scan_language_database(spec_roots, &build_registry()) +} + +/// Where a bootstrap finds its languages: spec roots it scans when it first +/// needs a language, or a database a caller scanned once. +trait Languages { + fn database(&self, registry: &IdRegistry) -> KunaResult>; +} + +impl Languages for [R] { + fn database(&self, registry: &IdRegistry) -> KunaResult> { + scan_language_database(self, registry).map(Cow::Owned) + } +} + +impl Languages for LanguageDatabase { + fn database(&self, _registry: &IdRegistry) -> KunaResult> { + Ok(Cow::Borrowed(self)) + } +} + /// Find the `` element inside a parsed document root (which may be a /// bare `` or a `` wrapping one). fn find_binaryimage(root: &Rc) -> Option> { @@ -3968,12 +4003,12 @@ fn input_isa_paints_for_ranges( fn build_sleigh_for_loader( path: &str, target: &str, - spec_roots: &[impl IntoSpecRoot], + languages: &(impl Languages + ?Sized), registry: &IdRegistry, arch_type: &str, fallback_arch_id: Option<&[u8]>, ) -> KunaResult { - let db = scan_language_database(spec_roots, registry)?; + let db = languages.database(registry)?; let mut sleigh = SleighArchitecture::new(path, target); sleigh.resolve_architecture(&db, arch_type)?; if sleigh.language_index() < 0 && target.is_empty() { @@ -4151,7 +4186,7 @@ fn bootstrap_raw_image( base: u64, entries: &[u64], isa: Option, - spec_roots: &[impl IntoSpecRoot], + languages: &(impl Languages + ?Sized), bytes: Option>, ) -> KunaResult { if target.trim().is_empty() { @@ -4168,7 +4203,7 @@ fn bootstrap_raw_image( let registry = build_registry(); let capability = RawBinaryArchitectureCapability::new(); let mut raw = capability.build_architecture(path, target); - let db = scan_language_database(spec_roots, ®istry)?; + let db = languages.database(®istry)?; raw.resolve_architecture(&db)?; if raw.sleigh().language_index() < 0 { return Err(KunaError::lowlevel(format!( @@ -4317,24 +4352,36 @@ pub fn bootstrap_from_image_with_options( bootstrap_image(path, target, spec_roots, isa, options, false) } -/// The image dispatch behind both image bootstraps; `from_env` as in +/// [`bootstrap_from_image_with_options`] over a language database a caller +/// scanned once ([`language_database`]) instead of spec roots it scans again. +pub fn bootstrap_from_image_with_db( + path: &str, + target: &str, + db: &LanguageDatabase, + isa: Option, + options: &LoadOptions, +) -> KunaResult { + bootstrap_image(path, target, db, isa, options, false) +} + +/// The image dispatch behind every image bootstrap; `from_env` as in /// [`bootstrap_object`]. fn bootstrap_image( path: &str, target: &str, - spec_roots: &[impl IntoSpecRoot], + languages: &(impl Languages + ?Sized), isa: Option, options: &LoadOptions, from_env: bool, ) -> KunaResult { if file_is_te_image(path) { let _load = enter_load(options, from_env); - bootstrap_from_te_with_specs(path, target, spec_roots, isa) + bootstrap_te(path, target, languages, isa) } else if let Some(bytes) = file_mz_dos_image(path) { let _load = enter_load(options, from_env); - bootstrap_from_mz_bytes_with_specs(path, &bytes, target, spec_roots, isa) + bootstrap_mz_bytes(path, &bytes, target, languages, isa) } else { - bootstrap_object(path, target, spec_roots, isa, options, from_env) + bootstrap_object(path, target, languages, isa, options, from_env) } } @@ -4398,6 +4445,16 @@ pub fn bootstrap_from_mz_bytes_with_specs( target: &str, spec_roots: &[impl IntoSpecRoot], isa: Option, +) -> KunaResult { + bootstrap_mz_bytes(path, bytes, target, spec_roots, isa) +} + +fn bootstrap_mz_bytes( + path: &str, + bytes: &[u8], + target: &str, + languages: &(impl Languages + ?Sized), + isa: Option, ) -> KunaResult { let image = MzImage::parse(bytes) .map_err(|error| KunaError::lowlevel(format!("{}; {MZ_RAW_IMAGE_HINT}", error.explain())))?; @@ -4437,7 +4494,7 @@ pub fn bootstrap_from_mz_bytes_with_specs( image.base, &[entry], isa, - spec_roots, + languages, Some(image.module.clone()), )?; if let Some(space) = program.analysis_code_space.clone() { @@ -4534,11 +4591,20 @@ pub fn bootstrap_from_te_with_specs( target: &str, spec_roots: &[impl IntoSpecRoot], isa: Option, +) -> KunaResult { + bootstrap_te(path, target, spec_roots, isa) +} + +fn bootstrap_te( + path: &str, + target: &str, + languages: &(impl Languages + ?Sized), + isa: Option, ) -> KunaResult { let bytes = std::fs::read(path).map_err(|error| { KunaError::lowlevel(format!("Unable to open TE image file: {path}: {error}")) })?; - bootstrap_from_te_bytes_with_specs(path, &bytes, target, spec_roots, isa) + bootstrap_te_bytes(path, &bytes, target, languages, isa) } /// [`bootstrap_from_te`] over bytes a caller has already read. @@ -4560,6 +4626,16 @@ pub fn bootstrap_from_te_bytes_with_specs( target: &str, spec_roots: &[impl IntoSpecRoot], isa: Option, +) -> KunaResult { + bootstrap_te_bytes(path, bytes, target, spec_roots, isa) +} + +fn bootstrap_te_bytes( + path: &str, + bytes: &[u8], + target: &str, + languages: &(impl Languages + ?Sized), + isa: Option, ) -> KunaResult { let target = kuna_analysis::loadimage_object::explicit_language_target(target).unwrap_or(""); let registry = build_registry(); @@ -4568,7 +4644,7 @@ pub fn bootstrap_from_te_bytes_with_specs( let mut sleigh = build_sleigh_for_loader( path, target, - spec_roots, + languages, ®istry, &arch_type, loader.fallback_arch_id(), @@ -4734,6 +4810,19 @@ pub fn bootstrap_from_object_with_options( bootstrap_object(path, target, spec_roots, isa, options, false) } +/// [`bootstrap_from_object_with_options`] over a language database a caller +/// scanned once ([`language_database`]) instead of spec roots it scans again. +/// Threads may share the database and load images at once. +pub fn bootstrap_from_object_with_db( + path: &str, + target: &str, + db: &LanguageDatabase, + isa: Option, + options: &LoadOptions, +) -> KunaResult { + bootstrap_object(path, target, db, isa, options, false) +} + /// The object bootstrap. `from_env` is set by the entry points that take no /// options: their load and the program it builds read the environment live, and /// they publish the image's Go toolchain version in @@ -4743,7 +4832,7 @@ pub fn bootstrap_from_object_with_options( fn bootstrap_object( path: &str, target: &str, - spec_roots: &[impl IntoSpecRoot], + languages: &(impl Languages + ?Sized), isa: Option, options: &LoadOptions, from_env: bool, @@ -4846,7 +4935,7 @@ fn bootstrap_object( let mut sleigh = build_sleigh_for_loader( path, target, - spec_roots, + languages, ®istry, &arch_type, loader.fallback_arch_id(), diff --git a/decompiler/crates/kuna-console/tests/integration.rs b/decompiler/crates/kuna-console/tests/integration.rs index b68c29cff4..a28f1f9ac4 100644 --- a/decompiler/crates/kuna-console/tests/integration.rs +++ b/decompiler/crates/kuna-console/tests/integration.rs @@ -24,6 +24,8 @@ mod n32_stack; #[path = "setup_commands.rs"] mod setup_commands; +#[path = "verify_language_database.rs"] +mod verify_language_database; #[path = "verify_memoryspecs.rs"] mod verify_memoryspecs; diff --git a/decompiler/crates/kuna-console/tests/verify_language_database.rs b/decompiler/crates/kuna-console/tests/verify_language_database.rs new file mode 100644 index 0000000000..21de526aa4 --- /dev/null +++ b/decompiler/crates/kuna-console/tests/verify_language_database.rs @@ -0,0 +1,137 @@ +//! A caller can scan its spec roots into one `LanguageDatabase` and hand it to +//! every bootstrap: images of different languages load from the one database, +//! on several threads at once, without scanning the roots again, and decompile +//! as the same bootstraps over the spec roots do. + +use std::path::PathBuf; + +use kuna_base::error::KunaResult; +use kuna_console::engine::{ + bootstrap_from_image_with_db, bootstrap_from_image_with_options, bootstrap_from_object_with_db, + bootstrap_from_object_with_options, language_database, ConsoleProgram, +}; +use kuna_console::project::{decompile_entry, DecompileOptions}; +use kuna_decomp::kuna_loadoptions::LoadOptions; +use kuna_decomp::kuna_memoryspecs::SpecRoot; +use kuna_decomp::sleigh_arch::LanguageDatabase; + +fn repo_root() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../..").canonicalize().unwrap() +} + +fn specs() -> [SpecRoot; 1] { + [repo_root().join("specs").into()] +} + +fn fixture(name: &str) -> String { + let path = repo_root().join("decompiler/crates/kuna-analysis/tests/fixtures").join(name); + path.to_str().unwrap().to_string() +} + +/// Two images in different languages, with the functions each decompiles. +const IMAGES: [(&str, &str, &[&str]); 2] = [ + ("fauxware", "x86:LE:64:default", &["authenticate", "main"]), + ("adrnext_aarch64", "AARCH64:LE:64:v8A", &["audit_calls", "main"]), +]; + +fn decompile(mut prog: ConsoleProgram, funcs: &[&str]) -> (String, Vec) { + prog.commit_pending_analysis().expect("analysis commit succeeds"); + let c = funcs + .iter() + .map(|func| { + let entry = prog.find_entry_by_name(func).expect("function in fixture"); + decompile_entry(&mut prog, entry, &DecompileOptions::default()) + .code + .expect("decompile succeeds") + }) + .collect(); + (prog.arch().archid.clone(), c) +} + +fn each_image( + load: impl Fn(&str) -> KunaResult + Sync, +) -> Vec<(String, Vec)> { + // One thread per image, all reading the same database at once. + std::thread::scope(|scope| { + let threads: Vec<_> = IMAGES + .iter() + .map(|(name, _, funcs)| { + let load = &load; + scope.spawn(move || decompile(load(&fixture(name)).expect("bootstrap"), funcs)) + }) + .collect(); + threads.into_iter().map(|thread| thread.join().unwrap()).collect() + }) +} + +#[test] +fn one_database_serves_images_of_two_languages() { + let db = language_database(&specs()).expect("scan specs"); + let options = LoadOptions::default(); + let shared = each_image(|path| bootstrap_from_object_with_db(path, "", &db, None, &options)); + let rescanned = + each_image(|path| bootstrap_from_object_with_options(path, "", &specs(), None, &options)); + for ((name, language, _), (archid, _)) in IMAGES.iter().zip(&shared) { + assert!(archid.starts_with(language), "{name} loaded as {archid}"); + } + assert!(shared[0].1[1].contains("authenticate"), "{}", shared[0].1[1]); + assert_eq!(shared, rescanned); +} + +#[test] +fn the_image_dispatch_takes_a_database() { + let db = language_database(&specs()).expect("scan specs"); + let options = LoadOptions::default(); + let shared = each_image(|path| bootstrap_from_image_with_db(path, "", &db, None, &options)); + let rescanned = + each_image(|path| bootstrap_from_image_with_options(path, "", &specs(), None, &options)); + assert_eq!(shared, rescanned); +} + +#[test] +fn a_database_without_the_language_refuses_the_image() { + let db = language_database(&[repo_root().join("no-such-specs")]).expect("an empty root scans"); + let path = fixture("fauxware"); + let err = match bootstrap_from_object_with_db(&path, "", &db, None, &LoadOptions::default()) { + Ok(_) => panic!("bootstrapped from an empty database"), + Err(err) => err.to_string(), + }; + assert!(err.contains("No sleigh specification"), "{err}"); +} + +#[test] +fn a_bootstrap_over_a_database_does_not_rescan_the_roots() { + let languages = "Ghidra/Processors/x86/data/languages"; + let root = PathBuf::from(env!("CARGO_TARGET_TMPDIR")) + .join(format!("kuna_language_database_{}", std::process::id())); + let copy = root.join(languages); + std::fs::create_dir_all(©).unwrap(); + for entry in std::fs::read_dir(repo_root().join("specs").join(languages)).unwrap() { + let entry = entry.unwrap(); + if entry.file_type().unwrap().is_file() { + std::fs::copy(entry.path(), copy.join(entry.file_name())).unwrap(); + } + } + let roots: [SpecRoot; 1] = [root.clone().into()]; + let db = language_database(&roots).expect("scan the copy"); + std::fs::remove_file(copy.join("x86.ldefs")).unwrap(); + + let path = fixture("fauxware"); + let options = LoadOptions::default(); + let loaded = bootstrap_from_object_with_db(&path, "", &db, None, &options); + let rescanned = bootstrap_from_object_with_options(&path, "", &roots, None, &options); + std::fs::remove_dir_all(&root).unwrap(); + let archid = loaded.expect("bootstrap over the database").arch().archid.clone(); + assert!(archid.starts_with("x86:LE:64:default"), "{archid}"); + let err = match rescanned { + Ok(_) => panic!("bootstrapped without x86.ldefs"), + Err(err) => err.to_string(), + }; + assert!(err.contains("No sleigh specification"), "{err}"); +} + +#[test] +fn a_database_is_shareable_across_threads() { + fn shareable() {} + shareable::(); +} From 62f4b64ab68e89a831bcf5b8c47d844a0b1b28ab Mon Sep 17 00:00:00 2001 From: Evan Richter Date: Tue, 10 Nov 2026 09:35:51 -0700 Subject: [PATCH 4/4] [AUTOMATED] Load every input through one Bootstrap builder Kuna has 26 bootstrap_* entry points, one per combination of input format, language source (spec roots or a scanned LanguageDatabase), ARM ISA (environment, explicit) and load options (environment, explicit). Each new setting doubled the set. Bootstrap::new(&languages).target(..).arm_isa(..).options(&opts) now holds those settings, and one method per input loads with them: object, image, file, raw, te, te_bytes, mz_bytes, xml_image, xml_root. LanguageSource, a sealed trait, accepts spec roots as a slice, array or Vec, or a LanguageDatabase. The builder is Copy, so one configured builder can load many inputs, on several threads. Without options, a load puts the environment in force for that load on the calling thread, and an unset ISA comes from KUNA_ARM_ISA. With options it reads neither. Explicit options now also reach raw, TE, MZ and XML loads. Each bootstrap_* function becomes a one-line wrapper over the builder. Tests check that explicit options reach every input without the environment, that a builder without options reads the environment and one with options does not, and that one builder loads the same program on several threads. Co-Authored-By: Claude Opus 5.5 --- decompiler/crates/kuna-console/src/engine.rs | 186 +++++++++------ .../crates/kuna-console/src/kuna_bootstrap.rs | 223 ++++++++++++++++++ decompiler/crates/kuna-console/src/lib.rs | 1 + .../crates/kuna-console/tests/integration.rs | 3 + .../kuna-console/tests/verify_bootstrap.rs | 199 ++++++++++++++++ .../kuna-console/tests/verify_loadoptions.rs | 67 +++++- 6 files changed, 601 insertions(+), 78 deletions(-) create mode 100644 decompiler/crates/kuna-console/src/kuna_bootstrap.rs create mode 100644 decompiler/crates/kuna-console/tests/verify_bootstrap.rs diff --git a/decompiler/crates/kuna-console/src/engine.rs b/decompiler/crates/kuna-console/src/engine.rs index d49a82b142..5b4f2269a9 100644 --- a/decompiler/crates/kuna-console/src/engine.rs +++ b/decompiler/crates/kuna-console/src/engine.rs @@ -64,6 +64,7 @@ use kuna_sleigh::loadimage_xml::register_loadimage_xml_ids; use kuna_sleigh::translate::register_translate_ids; use crate::entry_selector::ObjectSectionLocation; +pub use crate::kuna_bootstrap::{Bootstrap, LanguageSource}; pub use crate::entry_selector::{ DataName, EntryLookupError, EntryProvenance, EntrySelector, FunctionEntry, NameSuggestion, ObjectLocation, @@ -3670,8 +3671,8 @@ fn scan_language_database(spec_roots: &[impl IntoSpecRoot], registry: &IdRegistr Ok(db) } -/// Scan `spec_roots` into a [`LanguageDatabase`] once, for the `*_with_db` -/// bootstraps ([`bootstrap_from_object_with_db`], +/// Scan `spec_roots` into a [`LanguageDatabase`] once, for a [`Bootstrap`] +/// or the `*_with_db` bootstraps ([`bootstrap_from_object_with_db`], /// [`bootstrap_from_image_with_db`]) to share. Every other bootstrap scans its /// spec roots again: it lists their `.ldefs` and parses every one, whichever /// language the image needs. A bootstrap over the database builds what the @@ -3686,21 +3687,41 @@ pub fn language_database(spec_roots: &[impl IntoSpecRoot]) -> KunaResult KunaResult>; -} +pub(crate) use languages::Languages; + +mod languages { + use super::*; -impl Languages for [R] { - fn database(&self, registry: &IdRegistry) -> KunaResult> { - scan_language_database(self, registry).map(Cow::Owned) + /// Where a bootstrap finds its languages: spec roots it scans when it + /// first needs a language, or a database a caller scanned once. Public in + /// a private module, so [`crate::engine::LanguageSource`] can name it as a + /// supertrait that callers cannot implement. + pub trait Languages { + fn database(&self, registry: &IdRegistry) -> KunaResult>; + } + + impl Languages for [R] { + fn database(&self, registry: &IdRegistry) -> KunaResult> { + scan_language_database(self, registry).map(Cow::Owned) + } + } + + impl Languages for [R; N] { + fn database(&self, registry: &IdRegistry) -> KunaResult> { + self.as_slice().database(registry) + } + } + + impl Languages for Vec { + fn database(&self, registry: &IdRegistry) -> KunaResult> { + self.as_slice().database(registry) + } } -} -impl Languages for LanguageDatabase { - fn database(&self, _registry: &IdRegistry) -> KunaResult> { - Ok(Cow::Borrowed(self)) + impl Languages for LanguageDatabase { + fn database(&self, _registry: &IdRegistry) -> KunaResult> { + Ok(Cow::Borrowed(self)) + } } } @@ -3830,7 +3851,7 @@ pub fn bootstrap_program( arch_id: &str, spec_roots: &[String], ) -> KunaResult { - bootstrap_program_with_specs(binaryimage, arch_id, spec_roots) + Bootstrap::new(spec_roots).xml_image(binaryimage, arch_id) } /// [`bootstrap_program`] over any spec roots: directories, in-memory @@ -3839,6 +3860,15 @@ pub fn bootstrap_program_with_specs( binaryimage: Rc, arch_id: &str, spec_roots: &[impl IntoSpecRoot], +) -> KunaResult { + Bootstrap::new(spec_roots).xml_image(binaryimage, arch_id) +} + +/// The XML bootstrap behind [`bootstrap_program`]. +pub(crate) fn bootstrap_xml_image( + binaryimage: Rc, + arch_id: &str, + languages: &(impl Languages + ?Sized), ) -> KunaResult { let registry = build_registry(); @@ -3850,7 +3880,7 @@ pub fn bootstrap_program_with_specs( arch.build_loader(Rc::clone(&binaryimage))?; // collectSpecFiles + resolveArchitecture (language-id normalization/index). - let db = scan_language_database(spec_roots, ®istry)?; + let db = languages.database(®istry)?; arch.sleigh_mut().set_archid(arch_id); arch.sleigh_mut().resolve_architecture(&db, arch_id)?; if arch.sleigh().language_index() < 0 { @@ -4163,7 +4193,7 @@ pub fn bootstrap_from_raw( isa: Option, spec_roots: &[String], ) -> KunaResult { - bootstrap_from_raw_with_specs(path, target, base, entries, isa, spec_roots) + Bootstrap::new(spec_roots).target(target).arm_isa(isa).raw(path, base, entries) } /// [`bootstrap_from_raw`] over any spec roots: directories, in-memory @@ -4176,11 +4206,11 @@ pub fn bootstrap_from_raw_with_specs( isa: Option, spec_roots: &[impl IntoSpecRoot], ) -> KunaResult { - bootstrap_raw_image(path, target, base, entries, isa, spec_roots, None) + Bootstrap::new(spec_roots).target(target).arm_isa(isa).raw(path, base, entries) } /// [`bootstrap_from_raw`] over the file, or over `bytes` built in memory. -fn bootstrap_raw_image( +pub(crate) fn bootstrap_raw_image( path: &str, target: &str, base: u64, @@ -4326,7 +4356,7 @@ pub fn bootstrap_from_image_with_isa( spec_roots: &[String], isa: Option, ) -> KunaResult { - bootstrap_from_image_with_isa_with_specs(path, target, spec_roots, isa) + Bootstrap::new(spec_roots).target(target).arm_isa(isa).image(path) } /// [`bootstrap_from_image_with_isa`] over any spec roots: directories, in-memory @@ -4337,7 +4367,7 @@ pub fn bootstrap_from_image_with_isa_with_specs( spec_roots: &[impl IntoSpecRoot], isa: Option, ) -> KunaResult { - bootstrap_image(path, target, spec_roots, isa, &LoadOptions::from_env(), true) + Bootstrap::new(spec_roots).target(target).arm_isa(isa).image(path) } /// [`bootstrap_from_image_with_isa`] with the load-time options explicit, as @@ -4349,7 +4379,7 @@ pub fn bootstrap_from_image_with_options( isa: Option, options: &LoadOptions, ) -> KunaResult { - bootstrap_image(path, target, spec_roots, isa, options, false) + Bootstrap::new(spec_roots).target(target).arm_isa(isa).options(options).image(path) } /// [`bootstrap_from_image_with_options`] over a language database a caller @@ -4361,12 +4391,12 @@ pub fn bootstrap_from_image_with_db( isa: Option, options: &LoadOptions, ) -> KunaResult { - bootstrap_image(path, target, db, isa, options, false) + Bootstrap::new(db).target(target).arm_isa(isa).options(options).image(path) } /// The image dispatch behind every image bootstrap; `from_env` as in /// [`bootstrap_object`]. -fn bootstrap_image( +pub(crate) fn bootstrap_image( path: &str, target: &str, languages: &(impl Languages + ?Sized), @@ -4434,7 +4464,7 @@ pub fn bootstrap_from_mz_bytes( spec_roots: &[String], isa: Option, ) -> KunaResult { - bootstrap_from_mz_bytes_with_specs(path, bytes, target, spec_roots, isa) + Bootstrap::new(spec_roots).target(target).arm_isa(isa).mz_bytes(path, bytes) } /// [`bootstrap_from_mz_bytes`] over any spec roots: directories, in-memory @@ -4446,10 +4476,10 @@ pub fn bootstrap_from_mz_bytes_with_specs( spec_roots: &[impl IntoSpecRoot], isa: Option, ) -> KunaResult { - bootstrap_mz_bytes(path, bytes, target, spec_roots, isa) + Bootstrap::new(spec_roots).target(target).arm_isa(isa).mz_bytes(path, bytes) } -fn bootstrap_mz_bytes( +pub(crate) fn bootstrap_mz_bytes( path: &str, bytes: &[u8], target: &str, @@ -4530,7 +4560,7 @@ pub fn bootstrap_from_image( target: &str, spec_roots: &[String], ) -> KunaResult { - bootstrap_from_image_with_specs(path, target, spec_roots) + Bootstrap::new(spec_roots).target(target).image(path) } /// [`bootstrap_from_image`] over any spec roots: directories, in-memory @@ -4540,13 +4570,7 @@ pub fn bootstrap_from_image_with_specs( target: &str, spec_roots: &[impl IntoSpecRoot], ) -> KunaResult { - let isa = std::env::var(ARM_ISA_ENV) - .ok() - .map(|value| ArmIsa::parse(&value)) - .transpose() - .map_err(KunaError::lowlevel)? - .flatten(); - bootstrap_from_image_with_isa_with_specs(path, target, spec_roots, isa) + Bootstrap::new(spec_roots).target(target).image(path) } /// Whether the file at `path` opens as a UEFI TE, reading only the bytes the @@ -4581,7 +4605,7 @@ pub fn bootstrap_from_te( spec_roots: &[String], isa: Option, ) -> KunaResult { - bootstrap_from_te_with_specs(path, target, spec_roots, isa) + Bootstrap::new(spec_roots).target(target).arm_isa(isa).te(path) } /// [`bootstrap_from_te`] over any spec roots: directories, in-memory @@ -4592,10 +4616,10 @@ pub fn bootstrap_from_te_with_specs( spec_roots: &[impl IntoSpecRoot], isa: Option, ) -> KunaResult { - bootstrap_te(path, target, spec_roots, isa) + Bootstrap::new(spec_roots).target(target).arm_isa(isa).te(path) } -fn bootstrap_te( +pub(crate) fn bootstrap_te( path: &str, target: &str, languages: &(impl Languages + ?Sized), @@ -4615,7 +4639,7 @@ pub fn bootstrap_from_te_bytes( spec_roots: &[String], isa: Option, ) -> KunaResult { - bootstrap_from_te_bytes_with_specs(path, bytes, target, spec_roots, isa) + Bootstrap::new(spec_roots).target(target).arm_isa(isa).te_bytes(path, bytes) } /// [`bootstrap_from_te_bytes`] over any spec roots: directories, in-memory @@ -4627,10 +4651,10 @@ pub fn bootstrap_from_te_bytes_with_specs( spec_roots: &[impl IntoSpecRoot], isa: Option, ) -> KunaResult { - bootstrap_te_bytes(path, bytes, target, spec_roots, isa) + Bootstrap::new(spec_roots).target(target).arm_isa(isa).te_bytes(path, bytes) } -fn bootstrap_te_bytes( +pub(crate) fn bootstrap_te_bytes( path: &str, bytes: &[u8], target: &str, @@ -4734,7 +4758,7 @@ pub fn bootstrap_from_object( target: &str, spec_roots: &[String], ) -> KunaResult { - bootstrap_from_object_with_specs(path, target, spec_roots) + Bootstrap::new(spec_roots).target(target).object(path) } /// [`bootstrap_from_object`] over any spec roots: directories, in-memory @@ -4744,13 +4768,7 @@ pub fn bootstrap_from_object_with_specs( target: &str, spec_roots: &[impl IntoSpecRoot], ) -> KunaResult { - let isa = std::env::var(ARM_ISA_ENV) - .ok() - .map(|value| ArmIsa::parse(&value)) - .transpose() - .map_err(KunaError::lowlevel)? - .flatten(); - bootstrap_from_object_with_isa_with_specs(path, target, spec_roots, isa) + Bootstrap::new(spec_roots).target(target).object(path) } /// The MIPS t9 entry probe: the PIC-setup seed for `entry`, truncated to the @@ -4782,7 +4800,7 @@ pub fn bootstrap_from_object_with_isa( spec_roots: &[String], isa: Option, ) -> KunaResult { - bootstrap_from_object_with_isa_with_specs(path, target, spec_roots, isa) + Bootstrap::new(spec_roots).target(target).arm_isa(isa).object(path) } /// [`bootstrap_from_object_with_isa`] over any spec roots: directories, in-memory @@ -4793,7 +4811,7 @@ pub fn bootstrap_from_object_with_isa_with_specs( spec_roots: &[impl IntoSpecRoot], isa: Option, ) -> KunaResult { - bootstrap_object(path, target, spec_roots, isa, &LoadOptions::from_env(), true) + Bootstrap::new(spec_roots).target(target).arm_isa(isa).object(path) } /// [`bootstrap_from_object`] with every setting explicit: the ARM decode mode @@ -4807,7 +4825,7 @@ pub fn bootstrap_from_object_with_options( isa: Option, options: &LoadOptions, ) -> KunaResult { - bootstrap_object(path, target, spec_roots, isa, options, false) + Bootstrap::new(spec_roots).target(target).arm_isa(isa).options(options).object(path) } /// [`bootstrap_from_object_with_options`] over a language database a caller @@ -4820,7 +4838,7 @@ pub fn bootstrap_from_object_with_db( isa: Option, options: &LoadOptions, ) -> KunaResult { - bootstrap_object(path, target, db, isa, options, false) + Bootstrap::new(db).target(target).arm_isa(isa).options(options).object(path) } /// The object bootstrap. `from_env` is set by the entry points that take no @@ -4829,7 +4847,7 @@ pub fn bootstrap_from_object_with_db( /// [`GOVERSION_ENV`](kuna_decomp::kuna_goreg::GOVERSION_ENV) (cleared for a /// non-Go image). An explicit-options load leaves the environment alone and /// never reads it. -fn bootstrap_object( +pub(crate) fn bootstrap_object( path: &str, target: &str, languages: &(impl Languages + ?Sized), @@ -6526,17 +6544,22 @@ fn commit_analysis_output( /// `` wrapping one), reading the arch id off the /// `` element. Used by `load file` (path → parse → bootstrap). pub fn bootstrap_from_root(root: &Rc, spec_roots: &[String]) -> KunaResult { - bootstrap_from_root_with_specs(root, spec_roots) + Bootstrap::new(spec_roots).xml_root(root) } /// [`bootstrap_from_root`] over any spec roots: directories, in-memory /// [`MemorySpecs`](kuna_decomp::kuna_memoryspecs::MemorySpecs) trees, or a mix. pub fn bootstrap_from_root_with_specs(root: &Rc, spec_roots: &[impl IntoSpecRoot]) -> KunaResult { + Bootstrap::new(spec_roots).xml_root(root) +} + +/// The arch id and `` a parsed XML document root names. +pub(crate) fn xml_root_image(root: &Rc) -> KunaResult<(Rc, String)> { let binaryimage = find_binaryimage(root) .ok_or_else(|| KunaError::lowlevel("Could not find binaryimage tag"))?; let arch_id = attr(&binaryimage, "arch") .ok_or_else(|| KunaError::lowlevel(" has no arch attribute"))?; - bootstrap_program_with_specs(binaryimage, &arch_id, spec_roots) + Ok((binaryimage, arch_id)) } /// The ELF magic (`\x7fELF`), used to route `load file` to the object path. @@ -6635,7 +6658,7 @@ pub fn bootstrap_from_file( target: &str, spec_roots: &[String], ) -> KunaResult { - bootstrap_from_file_with_specs(path, target, spec_roots) + Bootstrap::new(spec_roots).target(target).file(path) } /// [`bootstrap_from_file`] over any spec roots: directories, in-memory @@ -6645,36 +6668,34 @@ pub fn bootstrap_from_file_with_specs( target: &str, spec_roots: &[impl IntoSpecRoot], ) -> KunaResult { + Bootstrap::new(spec_roots).target(target).file(path) +} + +/// What [`bootstrap_from_file`] found at a path, by its leading bytes. +pub(crate) enum FileInput { + Mz(Vec), + Te(Vec), + Object, + Xml(Rc), +} + +/// Read and classify the file [`bootstrap_from_file`] loads. +pub(crate) fn classify_file(path: &str) -> KunaResult { if kuna_analysis::loader::kuna_dyldcache::is_dyld_cache_file(Path::new(path)) || kuna_analysis::image_bytes::ImageBytes::map_thin_macho(path).ok().flatten().is_some() { - return bootstrap_from_object_with_specs(path, target, spec_roots); + return Ok(FileInput::Object); } let bytes = std::fs::read(path) .map_err(|e| KunaError::lowlevel(format!("Unable to recognize imagefile {path}: {e}")))?; if is_mz_dos_image(&bytes) { - let isa = std::env::var(ARM_ISA_ENV) - .ok() - .map(|value| ArmIsa::parse(&value)) - .transpose() - .map_err(KunaError::lowlevel)? - .flatten(); - return bootstrap_from_mz_bytes_with_specs(path, &bytes, target, spec_roots, isa); + return Ok(FileInput::Mz(bytes)); } if is_te_image(&bytes) { - let isa = std::env::var(ARM_ISA_ENV) - .ok() - .map(|value| ArmIsa::parse(&value)) - .transpose() - .map_err(KunaError::lowlevel)? - .flatten(); - return bootstrap_from_te_bytes_with_specs(path, &bytes, target, spec_roots, isa); + return Ok(FileInput::Te(bytes)); } if is_object_binary(&bytes) || is_apple_container(&bytes) { - // Real object-format binary (ELF / PE / Mach-O / COFF): drive the - // object-crate loader. - drop(bytes); - return bootstrap_from_object_with_specs(path, target, spec_roots); + return Ok(FileInput::Object); } if !bytes .iter() @@ -6695,7 +6716,18 @@ pub fn bootstrap_from_file_with_specs( if find_binaryimage(&root).is_none() { return Err(KunaError::lowlevel(RAW_IMAGE_INPUT_HINT)); } - bootstrap_from_root_with_specs(&root, spec_roots) + Ok(FileInput::Xml(root)) +} + +/// The ARM decode mode named by [`ARM_ISA_ENV`], for the entry points that +/// take none. +pub(crate) fn arm_isa_from_env() -> KunaResult> { + std::env::var(ARM_ISA_ENV) + .ok() + .map(|value| ArmIsa::parse(&value)) + .transpose() + .map_err(KunaError::lowlevel) + .map(Option::flatten) } /// Iterate the opened [`LoadImageXml`]'s symbol records (name → address) into a diff --git a/decompiler/crates/kuna-console/src/kuna_bootstrap.rs b/decompiler/crates/kuna-console/src/kuna_bootstrap.rs new file mode 100644 index 0000000000..a6e33c81e0 --- /dev/null +++ b/decompiler/crates/kuna-console/src/kuna_bootstrap.rs @@ -0,0 +1,223 @@ +//! One builder for every bootstrap. A [`Bootstrap`] names where the languages +//! come from, then takes each setting a load can have — the target language, +//! the ARM decode mode, the load-time options — and loads one input with it. +//! Each `bootstrap_*` free function calls it with fixed settings. + +use std::rc::Rc; + +use kuna_base::error::KunaResult; +use kuna_base::xml::Element; +use kuna_decomp::kuna_loadoptions::LoadOptions; + +use crate::engine::{ + arm_isa_from_env, bootstrap_image, bootstrap_mz_bytes, bootstrap_object, + bootstrap_raw_image, bootstrap_te, bootstrap_te_bytes, bootstrap_xml_image, classify_file, + xml_root_image, ArmIsa, ConsoleProgram, FileInput, Languages, +}; + +/// Where a [`Bootstrap`] finds its languages: spec roots (a slice, array or +/// `Vec` of anything [`IntoSpecRoot`](kuna_decomp::kuna_memoryspecs::IntoSpecRoot) +/// takes) it scans at each load, or a +/// [`LanguageDatabase`](kuna_decomp::sleigh_arch::LanguageDatabase) a caller +/// scanned once with [`language_database`](crate::engine::language_database). +/// Kuna implements it; callers cannot. +pub trait LanguageSource: Languages {} + +impl LanguageSource for L {} + +/// The settings for loading a binary into a [`ConsoleProgram`]. +/// +/// ```no_run +/// # use kuna_console::engine::{language_database, ArmIsa, Bootstrap}; +/// # use kuna_decomp::kuna_loadoptions::LoadOptions; +/// let db = language_database(&["specs"])?; +/// let options = LoadOptions::default(); +/// let load = Bootstrap::new(&db).arm_isa(Some(ArmIsa::Thumb)).options(&options); +/// let first = load.object("a.elf")?; +/// let second = load.object("b.elf")?; +/// # Ok::<(), kuna_base::error::KunaError>(()) +/// ``` +/// +/// Each setting has a default: +/// +/// - **target**: empty, so the input chooses its language. A raw image has no +/// header to choose from and needs one. +/// - **options**: none, so the load reads the process environment. An object +/// or image load puts the environment in force on the calling thread for +/// the load, and +/// the program it builds keeps reading the environment live; an object +/// load (or an image load that reaches the object loader) also writes the +/// image's Go version to +/// [`GOVERSION_ENV`](kuna_decomp::kuna_goreg::GOVERSION_ENV), or clears it. +/// The other inputs keep whatever options the calling thread already has in +/// force, so they can differ from thread to thread. With +/// [`Bootstrap::options`], nothing on the load reads or writes the +/// environment, and the program keeps the options for its analysis commit +/// and decompiles. +/// - **arm_isa**: unset. Without options, an unset ISA comes from +/// [`ARM_ISA_ENV`](crate::engine::ARM_ISA_ENV); with options, it is `None` +/// (the image's own evidence decides). +/// +/// The builder only borrows its settings and holds no state of its own, so +/// one builder can load many inputs, on one thread or on several. Each load +/// enters its options on the thread that runs it and leaves them when it +/// returns. +pub struct Bootstrap<'a, L: LanguageSource + ?Sized> { + languages: &'a L, + target: &'a str, + arm_isa: Option>, + options: Option<&'a LoadOptions>, +} + +impl Clone for Bootstrap<'_, L> { + fn clone(&self) -> Self { + *self + } +} + +impl Copy for Bootstrap<'_, L> {} + +impl<'a, L: LanguageSource + ?Sized> Bootstrap<'a, L> { + /// A bootstrap over `languages`, with every other setting at its default. + pub fn new(languages: &'a L) -> Self { + Self { + languages, + target: "", + arm_isa: None, + options: None, + } + } + + /// The SLEIGH language id to load as. Empty or `default` lets the input + /// choose; any other id overrides the language the input names. An XML + /// input carries its own and ignores this. + pub fn target(mut self, target: &'a str) -> Self { + self.target = target; + self + } + + /// The ARM/Thumb decode mode for a 32-bit ARM target: `Some` paints the + /// executable ranges with it, `None` leaves the image's own evidence to + /// decide. A load whose language is not 32-bit ARM fails when this is + /// `Some`. Not read for an XML input. + pub fn arm_isa(mut self, isa: Option) -> Self { + self.arm_isa = Some(isa); + self + } + + /// The load-time options, in place of the environment. + pub fn options(mut self, options: &'a LoadOptions) -> Self { + self.options = Some(options); + self + } + + /// Load an object-format binary: ELF, PE, Mach-O or COFF, as + /// [`bootstrap_from_object`](crate::engine::bootstrap_from_object) does. + pub fn object(&self, path: &str) -> KunaResult { + let isa = self.resolved_isa()?; + match self.options { + Some(options) => { + bootstrap_object(path, self.target, self.languages, isa, options, false) + } + None => bootstrap_object( + path, + self.target, + self.languages, + isa, + &LoadOptions::from_env(), + true, + ), + } + } + + /// Load a real image on disk, choosing the loader by its bytes: a UEFI + /// TE, a DOS `MZ`, or an object-format binary, as + /// [`bootstrap_from_image`](crate::engine::bootstrap_from_image) does. + pub fn image(&self, path: &str) -> KunaResult { + let isa = self.resolved_isa()?; + match self.options { + Some(options) => { + bootstrap_image(path, self.target, self.languages, isa, options, false) + } + None => bootstrap_image( + path, + self.target, + self.languages, + isa, + &LoadOptions::from_env(), + true, + ), + } + } + + /// Load any input `load file` accepts: a real image as [`Bootstrap::image`] + /// takes it, or an XML document holding a ``, as + /// [`bootstrap_from_file`](crate::engine::bootstrap_from_file) does. + pub fn file(&self, path: &str) -> KunaResult { + let input = { + let _load = self.options.map(LoadOptions::enter); + classify_file(path)? + }; + match input { + FileInput::Mz(bytes) => self.mz_bytes(path, &bytes), + FileInput::Te(bytes) => self.te_bytes(path, &bytes), + FileInput::Object => self.object(path), + FileInput::Xml(root) => self.xml_root(&root), + } + } + + /// Load a headerless byte image mapped at `base`, its functions found from + /// `entries`, as [`bootstrap_from_raw`](crate::engine::bootstrap_from_raw) + /// does. Needs a target. + pub fn raw(&self, path: &str, base: u64, entries: &[u64]) -> KunaResult { + let isa = self.resolved_isa()?; + let _load = self.options.map(LoadOptions::enter); + bootstrap_raw_image(path, self.target, base, entries, isa, self.languages, None) + } + + /// Load a UEFI TE image, as [`bootstrap_from_te`](crate::engine::bootstrap_from_te) does. + pub fn te(&self, path: &str) -> KunaResult { + let isa = self.resolved_isa()?; + let _load = self.options.map(LoadOptions::enter); + bootstrap_te(path, self.target, self.languages, isa) + } + + /// Load a UEFI TE image from bytes already read from `path`, as + /// [`bootstrap_from_te_bytes`](crate::engine::bootstrap_from_te_bytes) does. + pub fn te_bytes(&self, path: &str, bytes: &[u8]) -> KunaResult { + let isa = self.resolved_isa()?; + let _load = self.options.map(LoadOptions::enter); + bootstrap_te_bytes(path, bytes, self.target, self.languages, isa) + } + + /// Load a DOS `MZ` executable from bytes already read from `path`, as + /// [`bootstrap_from_mz_bytes`](crate::engine::bootstrap_from_mz_bytes) does. + pub fn mz_bytes(&self, path: &str, bytes: &[u8]) -> KunaResult { + let isa = self.resolved_isa()?; + let _load = self.options.map(LoadOptions::enter); + bootstrap_mz_bytes(path, bytes, self.target, self.languages, isa) + } + + /// Load a `` element as language `arch_id`, as + /// [`bootstrap_program`](crate::engine::bootstrap_program) does. + pub fn xml_image(&self, binaryimage: Rc, arch_id: &str) -> KunaResult { + let _load = self.options.map(LoadOptions::enter); + bootstrap_xml_image(binaryimage, arch_id, self.languages) + } + + /// Load the `` inside a parsed XML document, as language its + /// `arch` attribute names, as + /// [`bootstrap_from_root`](crate::engine::bootstrap_from_root) does. + pub fn xml_root(&self, root: &Rc) -> KunaResult { + let (binaryimage, arch_id) = xml_root_image(root)?; + self.xml_image(binaryimage, &arch_id) + } + + fn resolved_isa(&self) -> KunaResult> { + match (self.arm_isa, self.options) { + (Some(isa), _) => Ok(isa), + (None, Some(_)) => Ok(None), + (None, None) => arm_isa_from_env(), + } + } +} diff --git a/decompiler/crates/kuna-console/src/lib.rs b/decompiler/crates/kuna-console/src/lib.rs index 974066d1ae..8ee246a263 100644 --- a/decompiler/crates/kuna-console/src/lib.rs +++ b/decompiler/crates/kuna-console/src/lib.rs @@ -20,6 +20,7 @@ pub mod interface; pub mod ifaceterm; pub mod ifacedecomp; mod entry_selector; +mod kuna_bootstrap; pub mod engine; pub mod decompile_step; pub mod project; diff --git a/decompiler/crates/kuna-console/tests/integration.rs b/decompiler/crates/kuna-console/tests/integration.rs index a28f1f9ac4..5fd343d7a3 100644 --- a/decompiler/crates/kuna-console/tests/integration.rs +++ b/decompiler/crates/kuna-console/tests/integration.rs @@ -548,3 +548,6 @@ mod verify_rustfmtio; #[path = "verify_rustprobestack.rs"] mod verify_rustprobestack; + +#[path = "verify_bootstrap.rs"] +mod verify_bootstrap; diff --git a/decompiler/crates/kuna-console/tests/verify_bootstrap.rs b/decompiler/crates/kuna-console/tests/verify_bootstrap.rs new file mode 100644 index 0000000000..913f2e9d1b --- /dev/null +++ b/decompiler/crates/kuna-console/tests/verify_bootstrap.rs @@ -0,0 +1,199 @@ +//! The `Bootstrap` builder carries explicit options to each input it loads, +//! and one builder serves several threads. + +use crate::common; + +use std::path::{Path, PathBuf}; +use std::rc::Rc; + +use kuna_analysis::loadimage_te::synthetic::TeImage; +use kuna_base::error::KunaResult; +use kuna_base::xml::{DocumentStorage, Element}; +use kuna_console::engine::{language_database, Bootstrap, ConsoleProgram}; +use kuna_console::project::{decompile_entry, DecompileOptions}; +use kuna_decomp::kuna_loadoptions::{environment_fallbacks, LoadOptions}; + +const X86_64: &str = "x86:LE:64:default"; +/// `lea rax, [rdi+rbx]; ret` +const X86_ADD: [u8; 5] = [0x48, 0x8d, 0x04, 0x1f, 0xc3]; +/// Thumb `movs r0, #7; bx lr`, which decodes as something else in A32. +const THUMB_RETURN_7: [u8; 4] = [0x07, 0x20, 0x70, 0x47]; + +fn repo_root() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("../../..") + .canonicalize() + .unwrap() +} + +fn specs() -> Vec { + vec![repo_root().join("specs").to_string_lossy().into_owned()] +} + +fn fixture(name: &str) -> String { + let path = repo_root() + .join("decompiler/crates/kuna-analysis/tests/fixtures") + .join(name); + path.to_str().unwrap().to_string() +} + +struct Scratch(PathBuf); + +impl Scratch { + fn new(stem: &str, extension: &str, bytes: &[u8]) -> Self { + let path = common::scratch_file(stem, extension); + std::fs::write(&path, bytes).unwrap(); + Self(path) + } + + fn path(&self) -> &str { + self.0.to_str().unwrap() + } +} + +impl Drop for Scratch { + fn drop(&mut self) { + let _ = std::fs::remove_file(&self.0); + } +} + +/// What a load built, as far as a caller can see it: the language, the +/// description, the sections, every function and the C of the first few, or +/// the error. +fn fingerprint(load: KunaResult) -> String { + let mut prog = match load { + Ok(prog) => prog, + Err(err) => return format!("error: {err}"), + }; + prog.commit_pending_analysis() + .expect("analysis commit succeeds"); + let mut out = format!( + "{}\n{}\n{:?}\nsymbols {}\n", + prog.arch().archid, + prog.description(), + prog.sections(), + prog.num_symbols() + ); + let entries = prog.function_entries_canonical(); + for entry in &entries { + out += &format!("{} @ {:#x}\n", entry.name, entry.addr.get_offset()); + } + for entry in entries.iter().take(3) { + let result = decompile_entry(&mut prog, entry.clone(), &DecompileOptions::default()); + match result.code { + Some(code) => out += &code, + None => out += &format!("decompile error: {:?}\n", result.error), + } + } + out +} + +/// Entry `0000:0000` far-calls `0002:0000`, which returns 9 with `RETF`. +fn dos_image() -> Vec { + let mut module = vec![0x90u8; 0x24]; + module[..10].copy_from_slice(&[0x9a, 0x00, 0x00, 0x02, 0x00, 0xb8, 0x00, 0x4c, 0xcd, 0x21]); + module[0x20..0x24].copy_from_slice(&[0xb8, 0x09, 0x00, 0xcb]); + let mut header = vec![0u8; 0x20]; + header[..2].copy_from_slice(b"MZ"); + header[0x1c..0x20].copy_from_slice(&[0x03, 0x00, 0x00, 0x00]); + let total = header.len() + module.len(); + let words: [(usize, u16); 6] = [ + (0x02, (total % 512) as u16), + (0x04, total.div_ceil(512) as u16), + (0x06, 1), + (0x08, (header.len() / 16) as u16), + (0x10, 0x100), + (0x18, 0x1c), + ]; + for (at, value) in words { + header[at..at + 2].copy_from_slice(&value.to_le_bytes()); + } + header.extend_from_slice(&module); + header +} + +const XML_IMAGE: &str = r#"488d041fc3"#; + +fn xml_root() -> Rc { + DocumentStorage::new() + .parse_document(XML_IMAGE.as_bytes()) + .unwrap() + .get_root() + .clone() +} + +type Load<'a> = dyn Fn() -> KunaResult + 'a; + +#[test] +fn explicit_options_reach_every_input_without_the_environment() { + let specs = specs(); + let db = language_database(&specs).unwrap(); + let raw = Scratch::new("bootstrap-scoped-raw", "bin", &X86_ADD); + let te_bytes = TeImage::thumb(&THUMB_RETURN_7).build(); + let te = Scratch::new("bootstrap-scoped-te", "te", &te_bytes); + let mz_bytes = dos_image(); + let mz = Scratch::new("bootstrap-scoped-mz", "exe", &mz_bytes); + let xml = Scratch::new("bootstrap-scoped-xml", "xml", XML_IMAGE.as_bytes()); + let fauxware = fixture("fauxware"); + let options = LoadOptions { + goabi: false, + ..LoadOptions::default() + }; + let load = Bootstrap::new(&db).options(&options); + + let loads: Vec<(&str, Box)> = vec![ + ("object", Box::new(|| load.object(&fauxware))), + ("image", Box::new(|| load.image(te.path()))), + ("file", Box::new(|| load.file(xml.path()))), + ( + "raw", + Box::new(|| load.target(X86_64).raw(raw.path(), 0x1000, &[0x1000])), + ), + ("te", Box::new(|| load.te(te.path()))), + ("te bytes", Box::new(|| load.te_bytes(te.path(), &te_bytes))), + ("mz bytes", Box::new(|| load.mz_bytes(mz.path(), &mz_bytes))), + ("xml root", Box::new(|| load.xml_root(&xml_root()))), + ]; + for (input, bootstrap) in loads { + let before = environment_fallbacks(); + let prog = bootstrap().unwrap_or_else(|err| panic!("{input}: {err}")); + assert_eq!( + environment_fallbacks(), + before, + "{input} read the environment" + ); + assert!( + !prog.arch().load_options().goabi, + "{input} dropped its options" + ); + } +} + +#[test] +fn a_spec_root_array_or_vec_is_a_language_source() { + let path: &Path = &repo_root().join("specs"); + let fauxware = fixture("fauxware"); + let from_array = fingerprint(Bootstrap::new(&[path]).object(&fauxware)); + let from_vec = fingerprint(Bootstrap::new(&vec![path]).object(&fauxware)); + let from_slice = fingerprint(Bootstrap::new(&specs()[..]).object(&fauxware)); + assert_eq!(from_array, from_vec); + assert_eq!(from_array, from_slice); +} + +#[test] +fn one_builder_loads_on_several_threads() { + let specs = specs(); + let db = language_database(&specs).unwrap(); + let fauxware = fixture("fauxware"); + let options = LoadOptions::default(); + let load = Bootstrap::new(&db).options(&options); + let expected = fingerprint(load.object(&fauxware)); + std::thread::scope(|scope| { + let threads: Vec<_> = (0..2) + .map(|_| scope.spawn(|| fingerprint(load.object(&fauxware)))) + .collect(); + for thread in threads { + assert_eq!(thread.join().unwrap(), expected); + } + }); +} diff --git a/decompiler/crates/kuna-console/tests/verify_loadoptions.rs b/decompiler/crates/kuna-console/tests/verify_loadoptions.rs index 67d65642fe..887788478d 100644 --- a/decompiler/crates/kuna-console/tests/verify_loadoptions.rs +++ b/decompiler/crates/kuna-console/tests/verify_loadoptions.rs @@ -9,7 +9,8 @@ use std::path::{Path, PathBuf}; use std::sync::{Arc, Barrier, Mutex}; use kuna_console::engine::{ - bootstrap_from_object, bootstrap_from_object_with_options, ConsoleProgram, + bootstrap_from_object, bootstrap_from_object_with_options, ArmIsa, Bootstrap, + ConsoleProgram, ARM_ISA_ENV, }; use kuna_decomp::kuna_loadoptions::LoadOptions; use kuna_decomp::kuna_sourcelang::SourceLang; @@ -366,3 +367,67 @@ fn an_explicit_program_ignores_a_later_environment_change() { std::env::remove_var("KUNA_KERNELCACHE"); assert_eq!(explicit_value, "auto", "the explicit program kept its options"); } + +/// A `Bootstrap` without options reads the environment: its load options, +/// and an unset ISA from `KUNA_ARM_ISA`. With options, it reads neither, so a bad `KUNA_ARM_ISA` +/// cannot fail it. +#[test] +fn a_bootstrap_without_options_reads_the_environment() { + use kuna_analysis::loadimage_te::synthetic::TeImage; + use kuna_console::project::{decompile_entry, DecompileOptions}; + + fn first_function(load: kuna_base::error::KunaResult) -> String { + let mut prog = load.expect("bootstrap"); + prog.commit_pending_analysis().unwrap(); + let entry = prog.function_entries_executable()[0].clone(); + decompile_entry(&mut prog, entry, &DecompileOptions::default()).code.unwrap() + } + + struct ClearOnDrop; + impl Drop for ClearOnDrop { + fn drop(&mut self) { + std::env::remove_var(ARM_ISA_ENV); + for name in ENV_VARS { + std::env::remove_var(name); + } + } + } + + let _env = ENVIRONMENT.lock().unwrap_or_else(|e| e.into_inner()); + let _clear = ClearOnDrop; + let specs = [specs()]; + let roots = Bootstrap::new(&specs); + let te = common::scratch_file("loadoptions-te", "te"); + std::fs::write(&te, TeImage::arm(&[0x07, 0x20, 0x70, 0x47, 0x1e, 0xff, 0x2f, 0xe1]).build()).unwrap(); + let te = te.to_str().unwrap(); + let options = LoadOptions::default(); + + std::env::set_var(ARM_ISA_ENV, "thumb"); + let from_env = first_function(roots.image(te)); + assert!(from_env.contains("return 7"), "{from_env}"); + assert_eq!(first_function(roots.arm_isa(Some(ArmIsa::Thumb)).image(te)), from_env); + let explicit = first_function(roots.options(&options).image(te)); + assert_eq!(explicit, first_function(roots.arm_isa(None).options(&options).image(te))); + assert_ne!(explicit, from_env, "an explicit load ignored KUNA_ARM_ISA"); + + std::env::set_var(ARM_ISA_ENV, "bogus"); + let refused = |load: kuna_base::error::KunaResult| match load { + Ok(_) => panic!("loaded with KUNA_ARM_ISA=bogus"), + Err(err) => err.to_string(), + }; + let err = refused(roots.image(te)); + assert!(err.contains("invalid --isa"), "{err}"); + assert_eq!(first_function(roots.options(&options).image(te)), explicit); + std::env::remove_var(ARM_ISA_ENV); + std::fs::remove_file(te).unwrap(); + + let go = go_image(); + std::env::set_var("KUNA_GOABI", "off"); + let mut prog = roots.object(go.to_str().unwrap()).unwrap(); + prog.commit_pending_analysis().unwrap(); + assert_eq!(compiler(&prog), "gcc"); + let mut prog = roots.options(&options).object(go.to_str().unwrap()).unwrap(); + prog.commit_pending_analysis().unwrap(); + assert_eq!(compiler(&prog), "golang"); + std::fs::remove_file(go).unwrap(); +}