365 lines
15 KiB
Rust
365 lines
15 KiB
Rust
//! Native execution of registered leaf regions. Each mapping owns an immutable
|
|
//! copy of its checked native bytes; replacing a registration invalidates it.
|
|
|
|
#[cfg(feature = "std")]
|
|
pub mod checked_memory;
|
|
|
|
pub(crate) mod context;
|
|
#[cfg(feature = "std")]
|
|
pub(crate) mod floating_environment;
|
|
#[cfg(all(host_memory, target_arch = "x86_64"))]
|
|
mod native_image;
|
|
#[cfg(all(host_memory, target_arch = "x86_64"))]
|
|
pub(crate) use native_image::ReachedCall as ObservedNativeCall;
|
|
|
|
#[cfg(feature = "std")]
|
|
use crate::dispatch::{LeafDispatcher, ResolvedLeaf};
|
|
#[cfg(feature = "std")]
|
|
use crate::{Result, error::Io};
|
|
#[cfg(feature = "std")]
|
|
use alloc::rc::Rc;
|
|
#[cfg(feature = "std")]
|
|
use core::marker::PhantomData;
|
|
#[cfg(feature = "std")]
|
|
use std::io::Read;
|
|
#[cfg(feature = "std")]
|
|
use stela_compiler::GuestState;
|
|
#[cfg(feature = "std")]
|
|
use stela_contracts::require;
|
|
#[cfg(feature = "std")]
|
|
use stela_formats::sha256;
|
|
|
|
// Bounds a cached artifact so it cannot force an unbounded allocation or
|
|
// mapping.
|
|
pub(crate) const MAX_NATIVE_BYTES: usize = 64 * 1024 * 1024;
|
|
|
|
/// An owned RX copy of one registered leaf's code. It is neither Send nor Sync.
|
|
#[cfg(feature = "std")]
|
|
#[derive(Debug)]
|
|
pub struct ExecutableLeaf {
|
|
resolved: ResolvedLeaf,
|
|
mapping: platform::Mapping,
|
|
_serialized: PhantomData<Rc<()>>,
|
|
}
|
|
|
|
#[cfg(feature = "std")]
|
|
impl ExecutableLeaf {
|
|
/// Read and hash generated bytes against the immutable resolved manifest,
|
|
/// check the current compiler/ABI/CPU identity, and create an RX mapping.
|
|
///
|
|
/// The persisted manifest is not reread: external cache edits cannot change
|
|
/// the identity or expected hash held by the resolved handle. An already
|
|
/// loaded mapping remains independent of later disk changes.
|
|
pub fn load(dispatcher: &LeafDispatcher, resolved: &ResolvedLeaf) -> Result<Self> {
|
|
require!(
|
|
dispatcher.is_current(resolved),
|
|
"stale or foreign registered leaf"
|
|
);
|
|
let manifest = &resolved.prepared().manifest;
|
|
let identity = &manifest.identity;
|
|
require!(
|
|
*identity == resolved.identity().translation,
|
|
"resolved translation identity mismatch"
|
|
);
|
|
require!(
|
|
identity.schema_version == 1,
|
|
"unsupported translation schema"
|
|
);
|
|
require!(
|
|
identity.compiler_source_sha256 == crate::compiler_source_sha256(),
|
|
"generated code uses a different compiler"
|
|
);
|
|
require!(
|
|
identity.abi
|
|
== stela_compiler::block::BlockProfile::new(stela_compiler::block::Features::LEAF)
|
|
.abi(),
|
|
"unsupported generated-code ABI"
|
|
);
|
|
let target = identity.target_cpu.parse::<stela_compiler::TargetCpu>()?;
|
|
if !crate::platform::cpu::admitted(target) {
|
|
return Err(crate::Error::HostLacks(target));
|
|
}
|
|
require!(
|
|
!manifest.verified,
|
|
"cache metadata is not verification evidence"
|
|
);
|
|
require!(
|
|
manifest.native_bytes > 0 && manifest.native_bytes <= MAX_NATIVE_BYTES,
|
|
"generated-code size exceeds execution bound"
|
|
);
|
|
platform::ensure_supported()?;
|
|
let path = resolved.prepared().directory.join("native.bin");
|
|
let mut bytes = Vec::new();
|
|
std::fs::File::open(&path)
|
|
.io("open a generated artifact")?
|
|
.take((MAX_NATIVE_BYTES + 1) as u64)
|
|
.read_to_end(&mut bytes)
|
|
.io("read a generated artifact")?;
|
|
require!(
|
|
bytes.len() == manifest.native_bytes && sha256(&bytes) == manifest.native_sha256,
|
|
"generated artifact differs from resolved immutable manifest"
|
|
);
|
|
let mapping = platform::Mapping::new(&bytes)?;
|
|
// The immutable dispatcher borrow spans validation and publication.
|
|
require!(
|
|
dispatcher.is_current(resolved),
|
|
"registered leaf became stale"
|
|
);
|
|
Ok(Self {
|
|
resolved: resolved.clone(),
|
|
mapping,
|
|
_serialized: PhantomData,
|
|
})
|
|
}
|
|
|
|
pub fn resolved(&self) -> &ResolvedLeaf {
|
|
&self.resolved
|
|
}
|
|
|
|
pub fn native_bytes(&self) -> usize {
|
|
self.resolved.prepared().manifest.native_bytes
|
|
}
|
|
|
|
/// Execute this leaf against the ABI's memory-backed guest register state.
|
|
/// Registration is checked immediately before the native call, while this
|
|
/// immutable dispatcher borrow excludes serialized replacement/removal.
|
|
///
|
|
/// # Safety
|
|
///
|
|
/// The caller must trust the registered source and generated bytes, and must
|
|
/// establish that this leaf returns in finite time. Every executed guest
|
|
/// load/store must address live ordinary RAM at the identical native virtual
|
|
/// address, with sufficient bounds, permissions and alignment for the direct
|
|
/// RAM profile. RAM must remain live for the entire call. Guest memory cannot
|
|
/// alias `state`, executable bytes, this object, the dispatcher, any live Rust
|
|
/// references, or host metadata/stack. Guest writes require exclusive access
|
|
/// and cannot race other threads. SP-based accesses must satisfy the enabled
|
|
/// ARM alignment checks. No MMIO, PAC/tagged-pointer assumptions, atomicity,
|
|
/// concurrent code mutation, external calls or guest exception delivery are
|
|
/// supported. A guest fault can terminate the host process; this API does not
|
|
/// install a signal handler or sandbox untrusted code.
|
|
pub unsafe fn execute(
|
|
&self,
|
|
dispatcher: &LeafDispatcher,
|
|
state: &mut GuestState,
|
|
) -> Result<()> {
|
|
require!(
|
|
dispatcher.is_current(&self.resolved),
|
|
"refusing stale or foreign native execution"
|
|
);
|
|
// SAFETY: the caller upholds the direct-RAM and termination contract;
|
|
// the mapping is owned, immutable RX with the checked ABI.
|
|
unsafe { self.mapping.call(state) }
|
|
}
|
|
}
|
|
|
|
pub mod platform;
|
|
|
|
#[cfg(all(
|
|
test,
|
|
target_arch = "x86_64",
|
|
any(target_os = "macos", target_os = "linux")
|
|
))]
|
|
mod tests {
|
|
use super::*;
|
|
use anyhow::Result;
|
|
use stela_compiler::GuestAddress;
|
|
|
|
fn source(value: u32) -> Vec<u8> {
|
|
[0xd2800000u32 | (value << 5), 0xd65f03c0]
|
|
.into_iter()
|
|
.flat_map(u32::to_le_bytes)
|
|
.collect()
|
|
}
|
|
|
|
#[test]
|
|
fn executes_first_resolved_native_leaf_and_rejects_replaced_or_removed_handles() -> Result<()> {
|
|
let cache = tempfile::tempdir()?;
|
|
let mut dispatcher = LeafDispatcher::new(1, cache.path().into());
|
|
let entry = GuestAddress(0x1000);
|
|
dispatcher.register(entry, source(42))?;
|
|
let first = dispatcher.resolve(entry)?;
|
|
let executable = ExecutableLeaf::load(&dispatcher, &first)?;
|
|
assert_eq!(
|
|
executable.native_bytes(),
|
|
first.prepared().manifest.native_bytes
|
|
);
|
|
assert_eq!(executable.resolved().identity(), first.identity());
|
|
let mut state = GuestState::default();
|
|
// SAFETY: register-only MOV/RET with no guest memory.
|
|
unsafe { executable.execute(&dispatcher, &mut state)? };
|
|
assert_eq!(state.x[0], 42);
|
|
dispatcher.register(entry, source(43))?;
|
|
assert!(ExecutableLeaf::load(&dispatcher, &first).is_err());
|
|
state.x[0] = 777;
|
|
// SAFETY: the test leaf touches only `state`, a live local, and returns.
|
|
assert!(unsafe { executable.execute(&dispatcher, &mut state) }.is_err());
|
|
assert_eq!(state.x[0], 777);
|
|
let replacement = ExecutableLeaf::load(&dispatcher, &dispatcher.resolve(entry)?)?;
|
|
// SAFETY: the test leaf touches only `state`, a live local, and returns.
|
|
unsafe { replacement.execute(&dispatcher, &mut state)? };
|
|
assert_eq!(state.x[0], 43);
|
|
assert!(dispatcher.unregister(entry));
|
|
state.x[0] = 888;
|
|
// SAFETY: the test leaf touches only `state`, a live local, and returns.
|
|
assert!(unsafe { replacement.execute(&dispatcher, &mut state) }.is_err());
|
|
assert_eq!(state.x[0], 888);
|
|
dispatcher.register(entry, source(42))?;
|
|
// SAFETY: the test leaf touches only `state`, a live local, and returns.
|
|
assert!(unsafe { executable.execute(&dispatcher, &mut state) }.is_err());
|
|
let reloaded = ExecutableLeaf::load(&dispatcher, &dispatcher.resolve(entry)?)?;
|
|
// SAFETY: the test leaf touches only `state`, a live local, and returns.
|
|
unsafe { reloaded.execute(&dispatcher, &mut state)? };
|
|
assert_eq!(state.x[0], 42);
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn rejects_foreign_registry_and_changed_disk_bytes_but_owns_loaded_snapshot() -> Result<()> {
|
|
let cache = tempfile::tempdir()?;
|
|
let mut dispatcher = LeafDispatcher::new(1, cache.path().into());
|
|
let mut other = LeafDispatcher::new(1, cache.path().into());
|
|
let entry = GuestAddress(0x1000);
|
|
dispatcher.register(entry, source(42))?;
|
|
other.register(entry, source(42))?;
|
|
let resolved = dispatcher.resolve(entry)?;
|
|
let executable = ExecutableLeaf::load(&dispatcher, &resolved)?;
|
|
assert!(ExecutableLeaf::load(&other, &resolved).is_err());
|
|
let mut state = GuestState::default();
|
|
// SAFETY: the test leaf touches only `state`, a live local, and returns.
|
|
assert!(unsafe { executable.execute(&other, &mut state) }.is_err());
|
|
std::fs::write(resolved.prepared().directory.join("native.bin"), b"corrupt")?;
|
|
assert!(ExecutableLeaf::load(&dispatcher, &resolved).is_err());
|
|
// Own RX bytes are independent of the now-corrupt disk artifact.
|
|
// SAFETY: the test leaf touches only `state`, a live local, and returns.
|
|
unsafe { executable.execute(&dispatcher, &mut state)? };
|
|
assert_eq!(state.x[0], 42);
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn registered_direct_ram_leaf_writes_owned_memory_without_state_aliasing() -> Result<()> {
|
|
let cache = tempfile::tempdir()?;
|
|
let mut dispatcher = LeafDispatcher::new(1, cache.path().into());
|
|
let entry = GuestAddress(0x2000);
|
|
// STR X1,[X0]; LDR X2,[X0]; RET. Compiler semantics have their own
|
|
// ARM-vs-native differential suite; this checks executor integration.
|
|
let source = [0xf9000001u32, 0xf9400002, 0xd65f03c0]
|
|
.into_iter()
|
|
.flat_map(u32::to_le_bytes)
|
|
.collect();
|
|
dispatcher.register(entry, source)?;
|
|
let executable = ExecutableLeaf::load(&dispatcher, &dispatcher.resolve(entry)?)?;
|
|
let mut ram = vec![0u64; 2];
|
|
let mut state = GuestState::default();
|
|
state.x[0] = ram.as_mut_ptr() as u64;
|
|
state.x[1] = 0xfedc_ba98_7654_3210;
|
|
state.nzcv = 0xa000_0000;
|
|
// SAFETY: the code makes one aligned in-bounds store and load to separately
|
|
// owned RAM. No Rust reference to that RAM is held during the call, and it
|
|
// aliases neither the state nor the mapping.
|
|
unsafe { executable.execute(&dispatcher, &mut state)? };
|
|
assert_eq!(ram, [0xfedc_ba98_7654_3210, 0]);
|
|
assert_eq!(state.x[2], state.x[1]);
|
|
assert_eq!(state.nzcv, 0xa000_0000);
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn unsupported_source_never_becomes_an_executable_object() -> Result<()> {
|
|
let cache = tempfile::tempdir()?;
|
|
let mut dispatcher = LeafDispatcher::new(1, cache.path().into());
|
|
dispatcher.register(GuestAddress(0x1000), vec![0, 0, 0, 0])?;
|
|
assert!(dispatcher.resolve(GuestAddress(0x1000)).is_err());
|
|
assert_eq!(std::fs::read_dir(cache.path())?.count(), 0);
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(target_os = "linux")]
|
|
#[test]
|
|
fn linux_mapping_is_read_execute_and_not_write() -> Result<()> {
|
|
let cache = tempfile::tempdir()?;
|
|
let mut dispatcher = LeafDispatcher::new(1, cache.path().into());
|
|
let entry = GuestAddress(0x1000);
|
|
dispatcher.register(entry, source(42))?;
|
|
let executable = ExecutableLeaf::load(&dispatcher, &dispatcher.resolve(entry)?)?;
|
|
let address = executable.mapping.address();
|
|
let maps = std::fs::read_to_string("/proc/self/maps")?;
|
|
let permissions = maps.lines().find_map(|line| {
|
|
let mut columns = line.split_whitespace();
|
|
let (low, high) = columns.next()?.split_once('-')?;
|
|
let low = usize::from_str_radix(low, 16).ok()?;
|
|
let high = usize::from_str_radix(high, 16).ok()?;
|
|
(low <= address && address < high).then(|| columns.next().unwrap().to_owned())
|
|
});
|
|
assert_eq!(permissions.as_deref(), Some("r-xp"));
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(target_os = "macos")]
|
|
#[test]
|
|
fn macos_mapping_is_read_execute_and_not_write() -> Result<()> {
|
|
let cache = tempfile::tempdir()?;
|
|
let mut dispatcher = LeafDispatcher::new(1, cache.path().into());
|
|
let entry = GuestAddress(0x1000);
|
|
dispatcher.register(entry, source(42))?;
|
|
let executable = ExecutableLeaf::load(&dispatcher, &dispatcher.resolve(entry)?)?;
|
|
let mut address = executable.mapping.address() as u64;
|
|
let mut size = 0u64;
|
|
// Installed Darwin SDK mach/vm_region.h: BASIC_INFO_64 flavor 9,
|
|
// 4-byte packed structure occupies nine integer_t words. Only the
|
|
// first word (current protection) is needed; do not reinterpret u64
|
|
// members through potentially misaligned Rust references.
|
|
let mut info = [0i32; 9];
|
|
let mut count = 9u32;
|
|
unsafe extern "C" {
|
|
static mach_task_self_: u32;
|
|
fn mach_vm_region(
|
|
task: u32,
|
|
address: *mut u64,
|
|
size: *mut u64,
|
|
flavor: i32,
|
|
info: *mut i32,
|
|
count: *mut u32,
|
|
object: *mut u32,
|
|
) -> i32;
|
|
fn mach_port_deallocate(task: u32, name: u32) -> i32;
|
|
}
|
|
let mut object = 0u32;
|
|
// SAFETY: Query the current process with initialized output buffers and
|
|
// the documented VM_REGION_BASIC_INFO_64 flavor/count.
|
|
let result = unsafe {
|
|
mach_vm_region(
|
|
mach_task_self_,
|
|
&mut address,
|
|
&mut size,
|
|
9,
|
|
info.as_mut_ptr(),
|
|
&mut count,
|
|
&mut object,
|
|
)
|
|
};
|
|
if object != 0 {
|
|
// SAFETY: `object` is a send right this function obtained above and releases once.
|
|
unsafe { mach_port_deallocate(mach_task_self_, object) };
|
|
}
|
|
assert_eq!(result, libc::KERN_SUCCESS);
|
|
assert_eq!(info[0], libc::VM_PROT_READ | libc::VM_PROT_EXECUTE);
|
|
assert!(address <= executable.mapping.address() as u64);
|
|
assert!(
|
|
executable.mapping.address() as u64 + executable.native_bytes() as u64
|
|
<= address + size
|
|
);
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
/// Bounded native fault capture used by hardware-reference validation.
|
|
#[cfg(all(
|
|
feature = "std",
|
|
any(test, feature = "test-support"),
|
|
target_os = "linux",
|
|
target_arch = "x86_64"
|
|
))]
|
|
pub mod reference;
|