refactor(loader): move Mach-O loading out of the CLI into crates/stela-loader
This commit is contained in:
1 parent
5ad9b331cb
commit
11de7f40bc
19 files changed
+61
-39
No files matched your search
Generated
+17
-9
@@ -695,12 +695,6 @@ version = "0.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4050469837a6ff301cd14c1f8f24f88549e6d548f24f64e2148eb0f72cebc51f"
|
||||
|
||||
[[package]]
|
||||
name = "lzfse_rust"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5edeac3ec657925dececc672bdd58fa2212291886ddcb3e08aa46924c78340c6"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.8.3"
|
||||
@@ -1184,8 +1178,6 @@ dependencies = [
|
||||
"anyhow",
|
||||
"clap",
|
||||
"hex",
|
||||
"lzfse_rust",
|
||||
"object",
|
||||
"plist",
|
||||
"reqwest",
|
||||
"serde",
|
||||
@@ -1194,9 +1186,9 @@ dependencies = [
|
||||
"sha2",
|
||||
"stela-compiler",
|
||||
"stela-core",
|
||||
"stela-loader",
|
||||
"stela-runtime",
|
||||
"tempfile",
|
||||
"walkdir",
|
||||
"zip",
|
||||
]
|
||||
|
||||
@@ -1222,6 +1214,22 @@ dependencies = [
|
||||
"tempfile",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "stela-loader"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"hex",
|
||||
"object",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"stela-compiler",
|
||||
"stela-core",
|
||||
"stela-runtime",
|
||||
"tempfile",
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "stela-runtime"
|
||||
version = "0.1.0"
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
members = [
|
||||
"crates/stela-cli",
|
||||
"crates/stela-core",
|
||||
"crates/stela-loader",
|
||||
"crates/stela-compiler",
|
||||
"crates/stela-runtime",
|
||||
"crates/stela-state",
|
||||
@@ -30,6 +31,7 @@ stela-state = { path = "crates/stela-state" }
|
||||
stela-core = { path = "crates/stela-core" }
|
||||
stela-compiler = { path = "crates/stela-compiler" }
|
||||
stela-runtime = { path = "crates/stela-runtime" }
|
||||
stela-loader = { path = "crates/stela-loader" }
|
||||
|
||||
[workspace.lints.rust]
|
||||
unsafe_op_in_unsafe_fn = "deny"
|
||||
|
||||
@@ -19,12 +19,10 @@ tempfile.workspace = true
|
||||
stela-core.workspace = true
|
||||
stela-compiler.workspace = true
|
||||
stela-runtime.workspace = true
|
||||
stela-loader.workspace = true
|
||||
plist = "1"
|
||||
reqwest = { version = "0.12", default-features = false, features = ["blocking", "rustls-tls"] }
|
||||
sha1 = "0.10"
|
||||
object = { version = "0.37", features = ["read"] }
|
||||
walkdir = "2"
|
||||
lzfse_rust = "0.2"
|
||||
zip = { version = "4", default-features = false, features = ["deflate"] }
|
||||
|
||||
[lints]
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
//! A bounded original-dyld startup probe, not an application launcher.
|
||||
//! Reviewed startup prefixes or the original first continuous-clock call run.
|
||||
//! Main/dyld, stack and read-only clock mappings stay owned together.
|
||||
use crate::macho::{load_plan, mapped_image};
|
||||
use anyhow::{Context, Result, ensure};
|
||||
use std::path::{Path, PathBuf};
|
||||
use stela_compiler::{GuestAddress, GuestState};
|
||||
use stela_loader::{load_plan, mapped_image};
|
||||
use stela_runtime::{
|
||||
blocks::{BlockRuntime, StopReason, ThreadState},
|
||||
memory::OwnedMemory,
|
||||
@@ -126,10 +126,10 @@ pub fn probe(
|
||||
options: Options,
|
||||
) -> Result<serde_json::Value> {
|
||||
let dyld_data = std::fs::read(dyld_path)?;
|
||||
let (dyld_offset, dyld_bytes) = crate::macho::select_slice(&dyld_data, dyld_path, None)?;
|
||||
let (dyld_offset, dyld_bytes) = stela_loader::select_slice(&dyld_data, dyld_path, None)?;
|
||||
let dyld = load_plan::plan_dynamic_linker(dyld_bytes)?;
|
||||
let main_data = std::fs::read(executable)?;
|
||||
let (main_offset, main_bytes) = crate::macho::select_slice(&main_data, executable, None)?;
|
||||
let (main_offset, main_bytes) = stela_loader::select_slice(&main_data, executable, None)?;
|
||||
let main = load_plan::plan(main_bytes)?;
|
||||
ensure!(
|
||||
main.dylinker.as_deref() == Some(dyld.install_name.as_str()),
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
//! Bounded native execution of raw code, mapped images and original dyld.
|
||||
use crate::{args::*, compile::block_profile, dyld_entry, input::*, macho};
|
||||
use crate::{args::*, compile::block_profile, dyld_entry, input::*};
|
||||
use anyhow::{Context, Result, ensure};
|
||||
use clap::Args;
|
||||
use std::path::PathBuf;
|
||||
use stela_core::read_json;
|
||||
use stela_loader as macho;
|
||||
|
||||
#[derive(Args)]
|
||||
pub(crate) struct RunLinkedImages {
|
||||
|
||||
@@ -2,7 +2,6 @@ use super::*;
|
||||
use crate::Cli;
|
||||
use clap::Parser;
|
||||
|
||||
|
||||
#[test]
|
||||
fn research_state_json_retains_vector_bits_and_fp_status() -> Result<()> {
|
||||
let mut state = stela_compiler::GuestState::default();
|
||||
|
||||
@@ -9,7 +9,6 @@ mod execute;
|
||||
mod images;
|
||||
mod input;
|
||||
mod loader;
|
||||
mod macho;
|
||||
mod restore;
|
||||
|
||||
#[derive(Parser)]
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
//! Mach-O planning, dependency discovery, inventories and progress ledgers.
|
||||
use crate::{args::*, input::pauth_all_keys_context, macho};
|
||||
use crate::{args::*, input::pauth_all_keys_context};
|
||||
use anyhow::{Context, Result, ensure};
|
||||
use clap::{Args, Subcommand};
|
||||
use std::path::PathBuf;
|
||||
use stela_core::{Progress, Stage, read_json, write_json};
|
||||
use stela_loader as macho;
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub(crate) enum ProgressCommand {
|
||||
@@ -188,7 +189,7 @@ impl Inventory {
|
||||
!output.exists(),
|
||||
"inventory already exists; use a new output path for each scan"
|
||||
);
|
||||
let inventory = crate::macho::scan(&root, &build)?;
|
||||
let inventory = stela_loader::scan(&root, &build)?;
|
||||
write_json(&output, &inventory)?;
|
||||
eprintln!(
|
||||
"{} slices, {} scan errors, {} excluded containers; {}",
|
||||
|
||||
@@ -7,10 +7,7 @@ use std::{
|
||||
path::Path,
|
||||
};
|
||||
|
||||
pub fn manifest(
|
||||
image: &Path,
|
||||
model: Option<&str>,
|
||||
) -> Result<super::manifest::RestoreManifest> {
|
||||
pub fn manifest(image: &Path, model: Option<&str>) -> Result<super::manifest::RestoreManifest> {
|
||||
let mut archive = zip::ZipArchive::new(File::open(image)?)?;
|
||||
let member = archive.by_name("BuildManifest.plist")?;
|
||||
let mut bytes = vec![];
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
[package]
|
||||
name = "stela-loader"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
hex.workspace = true
|
||||
object = { version = "0.37", features = ["read"] }
|
||||
serde = { workspace = true, features = ["std"] }
|
||||
serde_json.workspace = true
|
||||
stela-compiler.workspace = true
|
||||
stela-core.workspace = true
|
||||
stela-runtime.workspace = true
|
||||
walkdir = "2"
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
+1
-3
@@ -847,9 +847,7 @@ fn decode_impl(
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::macho::load_plan::{
|
||||
Dependency, EntryPoint, FileRange, GuestRange, Payload, SegmentPlan,
|
||||
};
|
||||
use crate::load_plan::{Dependency, EntryPoint, FileRange, GuestRange, Payload, SegmentPlan};
|
||||
|
||||
fn pointer(
|
||||
raw: u64,
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1,4 +1,5 @@
|
||||
//! Inventory standalone Mach-O slices. Containers are explicitly uncounted.
|
||||
//! Apple Mach-O loading: slice selection, load plans, chained fixups, exports,
|
||||
//! dependency discovery and owned image mappings for native execution.
|
||||
pub mod chained_fixups;
|
||||
pub mod dependency_graph;
|
||||
pub mod dependency_paths;
|
||||
File renamed without changes.
File renamed without changes.
+6
-10
@@ -46,19 +46,19 @@ pub struct MappedImage {
|
||||
}
|
||||
|
||||
/// Slide ordinary image addresses without truncating a negative delta or overflow.
|
||||
pub(crate) fn relocated(address: u64, preferred_base: u64, load_base: u64) -> Result<u64> {
|
||||
pub fn relocated(address: u64, preferred_base: u64, load_base: u64) -> Result<u64> {
|
||||
u64::try_from(i128::from(address) + i128::from(load_base) - i128::from(preferred_base))
|
||||
.context("slid image address overflow")
|
||||
}
|
||||
|
||||
pub(crate) struct PreparedImage {
|
||||
pub struct PreparedImage {
|
||||
pub specs: Vec<MemoryRegionSpec>,
|
||||
pub code: Vec<(u64, Vec<u8>)>,
|
||||
pub unreserved_guards: Vec<UnreservedGuard>,
|
||||
pub data_limits: Vec<DataProtectionLimit>,
|
||||
}
|
||||
|
||||
pub(crate) fn preferred_base(plan: &LoadPlan) -> Result<u64> {
|
||||
pub fn preferred_base(plan: &LoadPlan) -> Result<u64> {
|
||||
Ok(plan
|
||||
.segments
|
||||
.iter()
|
||||
@@ -69,7 +69,7 @@ pub(crate) fn preferred_base(plan: &LoadPlan) -> Result<u64> {
|
||||
}
|
||||
|
||||
/// Preflight all writes on private copies before allocating any guest memory.
|
||||
pub(crate) fn prepare_segments(
|
||||
pub fn prepare_segments(
|
||||
data: &[u8],
|
||||
plan: &LoadPlan,
|
||||
load_base: u64,
|
||||
@@ -80,11 +80,7 @@ pub(crate) fn prepare_segments(
|
||||
|
||||
/// Kernel-style original bytes/initprot: original dyld owns relocation and its
|
||||
/// later SG_READ_ONLY lockdown. Never mix this route with pre-applied fixups.
|
||||
pub(crate) fn prepare_raw_segments(
|
||||
data: &[u8],
|
||||
plan: &LoadPlan,
|
||||
load_base: u64,
|
||||
) -> Result<PreparedImage> {
|
||||
pub fn prepare_raw_segments(data: &[u8], plan: &LoadPlan, load_base: u64) -> Result<PreparedImage> {
|
||||
prepare_segments_inner(data, plan, load_base, &[], false)
|
||||
}
|
||||
fn prepare_segments_inner(
|
||||
@@ -223,7 +219,7 @@ fn prepare_segments_inner(
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn add_stack(
|
||||
pub fn add_stack(
|
||||
specs: &mut Vec<MemoryRegionSpec>,
|
||||
stack: Option<StackSpec>,
|
||||
) -> Result<Option<StackMapping>> {
|
||||
+1
-1
@@ -103,7 +103,7 @@ mod tests {
|
||||
#[test]
|
||||
fn preserves_original_fields_raw_identity_and_process_type_path() -> Result<()> {
|
||||
let raw = command();
|
||||
let thread: crate::macho::load_plan::Arm64UnixThread = unix_thread(&raw)?;
|
||||
let thread: crate::load_plan::Arm64UnixThread = unix_thread(&raw)?;
|
||||
for index in 0..ARM64_GPR_COUNT {
|
||||
assert_eq!(
|
||||
thread.x[index],
|
||||
Reference in new issue
Block a user