Rust Quickstart
This chapter walks through four complete, runnable Rust workflows using Architectura's public APIs:
- Decoding, lifting, and simplifying a single x86-64 instruction (
add rax, rbx). - Discovering and lifting a reachable control-flow graph (
lift_reachable_stateless/Function). - Executing an instruction concretely against the independent reference oracle (
archsem-ref). - One-call CFG lifting for WebAssembly (
archsem-wasm) and EVM (archsem-evm).
[!IMPORTANT] MSRV & Feature Flags: Architectura requires Rust 1.88+ (Edition 2024).
archsem-x86keeps its decoder adapter behind theicedfeature (features = ["iced"]).archsem-arm64keeps its decoder adapter behind thefarm64feature (features = ["farm64"]).- Bytecode crates (
archsem-wasm,archsem-evm,archsem-cil,archsem-lua) enable their default frontend features out of the box.
1. Lift and Simplify a Single x86-64 Instruction
Add archsem-core, archsem-ir, archsem-simplify, and archsem-x86 (with iced) to Cargo.toml:
[dependencies]
archsem-core = "0.1"
archsem-ir = "0.1"
archsem-simplify = "0.1"
archsem-x86 = { version = "0.1", features = ["iced"] }
The bytes [0x48, 0x01, 0xd8] encode add rax, rbx in 64-bit mode:
use std::error::Error; use archsem_core::{ lookup, BinaryMode, DecodeContext, Decoder, EnabledFeatures, LiftCtx, NoDecodeState, NoState, ResourceLimits, }; use archsem_ir::RtlEmitter; use archsem_simplify::{simplify_fragment, SimplifyOutcome}; use archsem_x86::decode::{sem_index, IcedX86Decoder}; use archsem_x86::{profile_of, LONG64, X86_MANIFEST, X86_SEMANTICS}; fn main() -> Result<(), Box<dyn Error>> { let bytes = [0x48, 0x01, 0xd8]; // add rax, rbx let va = 0x401000; // 1. Construct a validated DecodeContext for 64-bit x86. let decode_ctx = DecodeContext::for_manifest( &X86_MANIFEST, LONG64, EnabledFeatures::empty(), NoDecodeState, &ResourceLimits::STANDARD, )?; // 2. Decode bytes into an IcedInsn and resolve its semantic table entry. let decoded = IcedX86Decoder.decode(&bytes, va, &decode_ctx)?.instruction; let index = sem_index(decoded.instruction()).expect("ADD has a semantic row"); let entry = lookup(X86_SEMANTICS, index).expect("table index in bounds"); // 3. Lift into neutral RTL via RtlEmitter. let mut rtl = RtlEmitter::new(); let mut arch_state = NoState; let control = { let mut lift_ctx = LiftCtx { emitter: &mut rtl, arch: &X86_MANIFEST, mode: LONG64, profile: profile_of(LONG64).expect("LONG64 profile"), insn_va: decoded.address(), next_va: decoded.next_address(), image: None, binary_mode: BinaryMode::Image, arch_state: &mut arch_state, }; archsem_x86::lift(&mut lift_ctx, &decoded, entry)? }; let mut fragment = rtl.finish(control); fragment.validate()?; let before = fragment.nodes.len(); // 4. Run effect-aware integer simplification. let report = simplify_fragment(&mut fragment)?; assert_eq!(report.outcome, SimplifyOutcome::Complete); println!( "lifted {} ({} -> {} RTL nodes)", entry.mnemonic, before, fragment.nodes.len() ); Ok(()) }
fragment.nodes contains typed SSA definitions (Const, ReadReg, BinOp, Compare, Select, etc.) and ordered side effects (WriteReg, Load, Store, TrapIf, ...), while fragment.control preserves the terminal SemanticControl<ValueId>.
2. Reachable CFG Lifting (lift_reachable_stateless)
To discover and lift a complete function CFG from an in-memory image, implement archsem_core::ImageReader and pass it to archsem_ir::lift_reachable_stateless:
use std::error::Error; use archsem_core::{ lookup, DecodeContext, EnabledFeatures, ImageReader, InsnView, LiftError, NoDecodeState, ResourceLimits, UnsupportedSemanticsReason, }; use archsem_ir::{lift_reachable_stateless, CfgLiftConfig, Completion}; use archsem_x86::decode::{sem_index, IcedX86Decoder}; use archsem_x86::{LONG64, X86_MANIFEST, X86_SEMANTICS}; struct SliceImage<'a> { base: u64, bytes: &'a [u8], } impl ImageReader for SliceImage<'_> { fn read_bytes(&self, va: u64, out: &mut [u8]) -> bool { let Some(offset) = va.checked_sub(self.base).and_then(|o| usize::try_from(o).ok()) else { return false; }; let Some(end) = offset.checked_add(out.len()) else { return false; }; let Some(slice) = self.bytes.get(offset..end) else { return false; }; out.copy_from_slice(slice); true } } fn main() -> Result<(), Box<dyn Error>> { // 0x1000: test edi, edi ; je +5 ; mov eax, 1 ; ret let code = [ 0x85, 0xff, 0x74, 0x05, 0xb8, 0x01, 0x00, 0x00, 0x00, 0xc3, ]; let base = 0x1000; let image = SliceImage { base, bytes: &code }; let decode_ctx = DecodeContext::for_manifest( &X86_MANIFEST, LONG64, EnabledFeatures::empty(), NoDecodeState, &ResourceLimits::STANDARD, )?; let config = CfgLiftConfig { address_range: Some(base..base + code.len() as u64), ..CfgLiftConfig::default() }; let outcome = lift_reachable_stateless( &IcedX86Decoder, &image, &X86_MANIFEST, base, decode_ctx, &config, |insn, ctx| { let idx = sem_index(insn.instruction()).ok_or_else(|| { LiftError::unsupported("unknown", UnsupportedSemanticsReason::ExplicitlyUnsupported) })?; let entry = lookup(X86_SEMANTICS, idx).ok_or_else(|| { LiftError::unsupported("unknown", UnsupportedSemanticsReason::ExplicitlyUnsupported) })?; archsem_x86::lift(ctx, insn, entry) }, ) .map_err(|failure| failure.error)?; assert_eq!(outcome.function.completion(), Completion::Complete); println!( "discovered {} blocks ({} instructions)", outcome.stats.blocks, outcome.stats.instructions ); Ok(()) }
Once lifted, you can run function-level normalization (outcome.function.optimize(...)), build register SSA (outcome.function.register_ssa(&X86_MANIFEST)), or replay into a backend (outcome.function.replay_register_ssa(...)).
3. Concrete Reference Execution (archsem-ref)
archsem-ref provides an independent, bounded concrete evaluator. You drive it using ConcreteEnvironment and ReferenceEmitter:
use std::error::Error; use archsem_core::{ lookup, BinaryMode, DecodeContext, Decoder, EnabledFeatures, LiftCtx, NoDecodeState, NoState, ResourceLimits, }; use archsem_ref::{ BitVector, ConcreteEnvironment, ReferenceCompletion, ReferenceEmitter, ReferenceEmitterConfig, }; use archsem_x86::decode::{sem_index, IcedX86Decoder}; use archsem_x86::{profile_of, regmap, LONG64, X86_MANIFEST, X86_SEMANTICS}; fn main() -> Result<(), Box<dyn Error>> { let bytes = [0x48, 0x01, 0xd8]; // add rax, rbx let decode_ctx = DecodeContext::for_manifest( &X86_MANIFEST, LONG64, EnabledFeatures::empty(), NoDecodeState, &ResourceLimits::STANDARD, )?; let decoded = IcedX86Decoder.decode(&bytes, 0x1000, &decode_ctx)?.instruction; let entry = lookup(X86_SEMANTICS, sem_index(decoded.instruction()).unwrap()).unwrap(); // Seed initial register state. let mut env = ConcreteEnvironment::new(); let rax = regmap::gpr(regmap::RAX, 64); let rbx = regmap::gpr(regmap::RBX, 64); env.set_register(rax, BitVector::from_u64(10, 64)?)?; env.set_register(rbx, BitVector::from_u64(32, 64)?)?; // Lift directly into ReferenceEmitter. let mut emitter = ReferenceEmitter::new(&mut env, ReferenceEmitterConfig::default()); let mut arch_state = NoState; let vm_result = { let mut lift_ctx = LiftCtx { emitter: &mut emitter, arch: &X86_MANIFEST, mode: LONG64, profile: profile_of(LONG64).unwrap(), insn_va: decoded.address(), next_va: decoded.next_address(), image: None, binary_mode: BinaryMode::Image, arch_state: &mut arch_state, }; archsem_x86::lift(&mut lift_ctx, &decoded, entry) }; let execution = emitter.finish_vm(vm_result); assert!(matches!(execution.completion, Ok(ReferenceCompletion::Control(_)))); let final_rax = env.register(rax).expect("RAX written"); assert_eq!(final_rax.to_u64(), Some(42)); println!("RAX = {}", final_rax.to_u64().unwrap()); Ok(()) }
4. One-Call Bytecode CFG Lifting (Wasm & EVM)
Unlike native byte streams, bytecode frontends expose one-call whole-function or whole-contract CFG lifting helpers.
WebAssembly (archsem-wasm)
#![allow(unused)] fn main() { use archsem_core::ResourceLimits; use archsem_ir::Completion; use archsem_wasm::{lift_function_cfg, WasmModule}; fn lift_wasm_fn(wasm_bytes: &[u8], fn_index: u32) -> Result<(), Box<dyn std::error::Error>> { let module = WasmModule::parse(wasm_bytes)?; let lifted = lift_function_cfg(&module, fn_index, &ResourceLimits::STANDARD)?; assert_eq!(lifted.outcome.function.completion(), Completion::Complete); println!("Wasm function {fn_index}: {} blocks", lifted.outcome.function.blocks().len()); Ok(()) } }
Ethereum Virtual Machine (archsem-evm)
#![allow(unused)] fn main() { use archsem_core::ResourceLimits; use archsem_evm::{lift_code_cfg, EvmCode, EvmFork}; use archsem_ir::Completion; fn lift_evm_contract(bytecode: &[u8]) -> Result<(), Box<dyn std::error::Error>> { let code = EvmCode::parse(bytecode, EvmFork::default())?; let lifted = lift_code_cfg(&code, &ResourceLimits::STANDARD)?; assert_eq!(lifted.outcome.function.completion(), Completion::Complete); println!("EVM contract: {} blocks", lifted.outcome.function.blocks().len()); Ok(()) } }
For additional runnable target-neutral Rust examples (direct emission, ActionRecorder -> RtlEmitter -> simplify -> replay, bounded symbolic execution, proof obligations, and runtime-loaded JSON programs), see the archsem-examples crate (cargo run -p archsem-examples --example <name>).