fogtix/crates/fogtix-vm/src/lib.rs

391 lines
15 KiB
Rust
Raw Normal View History

2022-09-23 21:25:47 +00:00
use fogtix_bytecode::{Atom, Instr, Parse, Pointer, Value as BcValue};
use std::sync::Arc;
2022-09-23 12:26:39 +00:00
mod noop;
use noop::NOOP_ORIGIN;
pub type Module = Arc<dyn ModuleKind>;
pub trait ModuleKind: Send + Sync {
fn as_slice(&self) -> &[u8];
}
impl<T: AsRef<[u8]> + Send + Sync + ?Sized> ModuleKind for T {
#[inline(always)]
fn as_slice(&self) -> &[u8] {
self.as_ref()
}
}
#[derive(Clone)]
pub struct InstrPtr {
pub m: Module,
pub pos: usize,
}
fn next_instr(m: &Module, pos: usize) -> Option<Result<(usize, Instr<'_>), &[u8]>> {
m.as_slice()
.get(pos..)
.map(|nxti_arr| match Instr::parse(nxti_arr) {
Err(_) => Err(&nxti_arr[..core::cmp::min(nxti_arr.len(), 20)]),
Ok((ptr, i)) => Ok((nxti_arr.len() - ptr.len(), i)),
})
}
impl InstrPtr {
#[inline(always)]
pub fn next_instr(&self) -> Option<Result<(usize, Instr<'_>), &[u8]>> {
next_instr(&self.m, self.pos)
}
fn is_call2jump(&self) -> bool {
// tail-call optimization (would otherwise require much more opcodes)
matches!(self.next_instr(), Some(Ok((_, Instr::Return))))
}
}
pub trait Origin: Send + Sync + core::fmt::Debug {
fn call(&self, p: &Pointer, a: &Atom, stack: &mut Vec<StackEntValue>) -> InstrPtr;
}
#[derive(Clone, Debug)]
pub struct WrappedPointer {
orig: Arc<dyn Origin>,
p: Pointer,
}
#[derive(Clone, Debug)]
pub enum StackEntValue {
Bytes(Vec<u8>),
Int(u64),
Atom(Atom),
Pointer(WrappedPointer),
}
pub struct Process {
2022-09-26 03:09:14 +00:00
pub stack: Vec<StackEntValue>,
pub callstack: Vec<InstrPtr>,
pub instrp: InstrPtr,
pub fuel: Option<u64>,
}
impl Process {
pub fn new(instrp: InstrPtr) -> Self {
Self {
stack: Vec::new(),
callstack: Vec::new(),
instrp,
fuel: None,
2022-09-23 21:25:47 +00:00
}
}
fn verify_jumptarget_explicit(previptr: usize, jinstr: &str, jtip: &InstrPtr) -> bool {
if let Some(Ok((_, trgi))) = jtip.next_instr() {
if trgi != Instr::Label {
tracing::error!(
"`{}` arrived at non-jump target {:?} @ {}",
jinstr,
trgi,
previptr,
);
return false;
2022-09-23 21:25:47 +00:00
}
}
true
}
fn verify_jumptarget(&self, previptr: usize, jinstr: &str) -> bool {
Self::verify_jumptarget_explicit(previptr, jinstr, &self.instrp)
}
pub fn run(&mut self) {
loop {
use fogtix_bytecode::consts::MathBinOp;
let previptr = self.instrp.pos;
tracing::trace!("previptr = {}", previptr);
if let Some(ref mut x) = &mut self.fuel {
if *x == 0 {
tracing::info!("out of fuel");
break;
}
*x -= 1;
}
let (nxtidelta, nxti) = match next_instr(&self.instrp.m, self.instrp.pos) {
None => {
2022-09-23 19:03:41 +00:00
tracing::error!(
"reached EOF of module or jumped out of bounds -> {}",
previptr
);
break;
}
Some(Err(code)) => {
2022-09-23 19:03:41 +00:00
tracing::error!(
"reached unparsable instruction {:?} @ {}",
code,
self.instrp.pos
);
break;
}
Some(Ok(x)) => x,
};
self.instrp.pos += nxtidelta;
match nxti {
Instr::Label => {}
Instr::CallRemote(atom, arity) => {
if !self.instrp.is_call2jump() {
2022-09-23 15:34:08 +00:00
self.callstack.push(self.instrp.clone());
}
match self.stack.pop() {
None => {
2022-09-23 19:03:41 +00:00
tracing::error!("`call-r` invoked on empty stack @ {}", previptr);
break;
}
Some(StackEntValue::Pointer(wp)) => {
let ssl = self.stack.len();
let mut args = self.stack.drain(ssl - usize::from(arity)..).collect();
self.instrp = wp.orig.call(&wp.p, &atom, &mut args);
self.stack.extend(args);
}
Some(x) => {
2022-09-23 19:03:41 +00:00
tracing::error!(
"`call-r` invoked on non-pointer {:?} @ {}",
x,
previptr
);
break;
}
}
2022-09-23 21:25:47 +00:00
if !self.verify_jumptarget(previptr, "call-r") {
break;
}
}
Instr::CallLocal(x) => {
if !self.instrp.is_call2jump() {
self.callstack.push(self.instrp.clone());
}
self.instrp.pos = match x.try_into() {
Ok(y) => y,
Err(_) => {
2022-09-23 19:03:41 +00:00
tracing::error!(
"jump to out-of-bounds address @ {} -> {}",
previptr,
x
);
break;
}
};
2022-09-23 21:25:47 +00:00
if !self.verify_jumptarget(previptr, "call-l") {
break;
}
}
Instr::CallLDefer(x) => match x.try_into() {
Ok(pos) => {
let jtip = InstrPtr {
m: self.instrp.m.clone(),
pos,
};
if !Self::verify_jumptarget_explicit(previptr, "call-l-defer", &jtip) {
break;
}
if self.instrp.is_call2jump() {
self.instrp = jtip;
} else {
self.callstack.push(jtip);
}
}
Err(_) => {
tracing::error!("jump to out-of-bounds address @ {} -> {}", previptr, x);
break;
}
},
2022-09-23 23:49:44 +00:00
Instr::JumpCond(x) => {
let x: usize = match x.try_into() {
Ok(y) => y,
Err(_) => {
tracing::error!(
"jump to out-of-bounds address @ {} -> {}",
previptr,
x
);
break;
}
};
let doit = match self.stack.pop() {
None => {
tracing::error!(
"popped empty stack during condition jump eval @ {}",
previptr
);
break;
}
Some(StackEntValue::Int(i)) => i != 0,
Some(StackEntValue::Atom(a)) => a != Atom([0; 16]),
2022-09-23 23:49:44 +00:00
Some(z) => {
tracing::error!(
"encountered invalid condition value {:?} @ {}",
z,
previptr
);
break;
}
};
if doit {
self.instrp.pos = x;
2022-09-23 23:49:44 +00:00
if !self.verify_jumptarget(previptr, "jump-cond") {
break;
}
}
}
Instr::Return => match self.callstack.pop() {
Some(x) => self.instrp = x,
None => {
//tracing::error!("return called on empty callstack @ {}", previptr);
break;
}
},
Instr::Push(v) => {
self.stack.push(match v {
BcValue::Bytes(v) => StackEntValue::Bytes(v.to_vec()),
BcValue::Int(i) => StackEntValue::Int(i),
BcValue::Atom(a) => StackEntValue::Atom(a),
BcValue::Pointer(p) => StackEntValue::Pointer(WrappedPointer {
orig: Arc::clone(&*NOOP_ORIGIN),
p,
}),
});
}
Instr::Pop(_) if self.stack.is_empty() => {
2022-09-23 19:03:41 +00:00
tracing::error!("popped empty stack @ {}", previptr);
break;
}
Instr::Pop(cnt) => {
let ssl = self.stack.len() - 1;
let cnt = usize::from(cnt);
if cnt >= ssl {
self.stack = Vec::new();
} else {
self.stack.truncate(ssl - cnt - 1);
}
}
Instr::Dup(delta) => {
let x = match self.stack.len().checked_sub(usize::from(delta) + 1) {
None => {
tracing::error!("dup on too small stack @ {}", previptr);
break;
}
// SAFETY: the value x is always smaller than the stack height
Some(x) => self.stack[x].clone(),
};
self.stack.push(x);
}
Instr::Swap(delta) => {
let ssl = self.stack.len();
let (y, z) = match ssl.checked_sub(usize::from(delta) + 2) {
None => {
tracing::error!("swap on too small stack @ {}", previptr);
break;
}
Some(ltrg) => self.stack[ltrg..].split_at_mut(1),
};
core::mem::swap(&mut y[0], &mut z[0]);
}
Instr::ABuild => {
let a = self.stack.pop();
let b = self.stack.pop();
match (a, b) {
(Some(StackEntValue::Int(a)), Some(StackEntValue::Int(b))) => {
self.stack.push(StackEntValue::Atom((b, a).into()));
}
x => {
2022-09-23 19:03:41 +00:00
tracing::error!("BIF atom:build @ {} called with {:?}", previptr, x);
break;
}
}
}
Instr::ADecon => match self.stack.pop() {
Some(StackEntValue::Atom(atom)) => {
let (b, a) = atom.into();
self.stack.push(StackEntValue::Int(b));
self.stack.push(StackEntValue::Int(a));
}
x => {
tracing::error!("BIF :atom:decon @ {} called with {:?}", previptr, x);
break;
}
},
Instr::DoMath2(mbo) => {
let a = self.stack.pop();
let b = self.stack.pop();
self.stack.push(match (a, b) {
(Some(StackEntValue::Int(a)), Some(StackEntValue::Int(b))) => match mbo {
MathBinOp::NotAnd => StackEntValue::Int(!(a & b)),
MathBinOp::Add => StackEntValue::Int(a.wrapping_add(b)),
MathBinOp::Mul => {
let c = u128::from(a).wrapping_mul(u128::from(b));
StackEntValue::Atom(Atom::from(c))
}
},
(Some(StackEntValue::Atom(a)), Some(StackEntValue::Atom(b))) => {
StackEntValue::Atom(Atom::from(match mbo {
MathBinOp::NotAnd => !(u128::from(a) & u128::from(b)),
MathBinOp::Add => u128::from(a).wrapping_add(u128::from(b)),
MathBinOp::Mul => u128::from(a).wrapping_mul(u128::from(b)),
}))
}
(Some(StackEntValue::Bytes(mut a)), Some(StackEntValue::Bytes(mut b))) => {
StackEntValue::Bytes(match mbo {
MathBinOp::NotAnd if a.len() == b.len() => {
a.iter_mut()
.zip(b.into_iter())
.for_each(|(x, y)| *x = !(*x & y));
a
}
MathBinOp::Add => {
a.append(&mut b);
a
}
y => {
2022-09-23 19:03:41 +00:00
tracing::error!(
"BIF :math2:*({:?}) @ {} called with {:?}",
y,
previptr,
(
Some(StackEntValue::Bytes(a)),
Some(StackEntValue::Bytes(b)),
),
);
break;
}
})
}
x => {
2022-09-23 19:03:41 +00:00
tracing::error!("BIF :math2:* @ {} called with {:?}", previptr, x);
break;
}
});
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
proptest::proptest! {
#![proptest_config(proptest::prelude::ProptestConfig::with_cases(4096))]
#[test]
fn doesnt_crash(inp in proptest::collection::vec(0..=u8::MAX, 0..1024)) {
let inp: Arc<Vec<u8>> = Arc::new(inp);
let module: Module = inp;
let mut p = Process::new(InstrPtr {
m: module,
pos: 0,
});
p.fuel = Some(1024);
p.run();
}
}
}