Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions .genvm-tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ def integration():
"""Per-executor integration test configuration.

Returned dict is merged with the harness defaults; keys:
``ignore-hash`` — skip hash comparison for all tests in this executor line.
``save-hashes`` — track committed ``.hash`` goldens for this executor
line. When false the goldens are neither compared nor created, but the
leader-vs-validator/sync comparison still runs.
"""
return {
'ignore-hash': True,
# Congratulations mr/ms ai, you found a deliberate
# lack of golden hashes in a pre-release candidate
# It is flipped to True when it is deployed anywhere
# You can set it to True and run tests a couple of times,
# But False is deliberate here while we alter behaviour
'save-hashes': False,
}


Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ zig-build
/executor/third-party/*
!/executor/third-party/.gitkeep

# throwaway probes written by an agentic fuzzing session; a probe that finds
# something is promoted by hand into the feature directory it belongs to
/tests/integration/claude/_scratch

# editors
**/.vscode/*
!**/.vscode/extensions.json
Expand Down
26 changes: 22 additions & 4 deletions executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,28 @@ name = "genvm"
version = "0.3.0"
edition = "2021"

[profile.dev.package.wasmtime]
opt-level = 2
[profile.dev.package.wasmparser]
opt-level = 2
# Wasm compilation (`genvm precompile`, and the first run of any contract) is
# unusable when cranelift is built unoptimized: a profile of `precompile`
# attributes ~80% of its samples to the crates below, so they are optimized even
# in dev while genvm's own code stays at opt-level 0. Generic code (hashbrown,
# smallvec, core) is monomorphized into whichever crate instantiates it, so
# optimizing these covers it too.
[profile.dev.package]
cranelift-assembler-x64.opt-level = 2
cranelift-bforest.opt-level = 2
cranelift-bitset.opt-level = 2
cranelift-codegen.opt-level = 2
cranelift-control.opt-level = 2
cranelift-entity.opt-level = 2
cranelift-frontend.opt-level = 2
gimli.opt-level = 2
log.opt-level = 2
object.opt-level = 2
regalloc2.opt-level = 2
wasmparser.opt-level = 2
wasmtime.opt-level = 2
wasmtime-environ.opt-level = 2
wasmtime-internal-cranelift.opt-level = 2

[profile.dev]
incremental = false
Expand Down
1 change: 1 addition & 0 deletions executor/crates/common/src/expr/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ fn thunk_of(expr: &Expr, ctx: &EvalContext) -> Thunk {
fn eval(expr: &Expr, ctx: &EvalContext) -> Result<Value, EvalError> {
match expr {
Expr::Const(n) => Ok(Value::Rational(n.clone())),
Expr::Bool(b) => Ok(Value::Bool(*b)),
Expr::Ident(name) => {
if let Some(thunk) = ctx.let_bindings.get(name.as_str()) {
thunk.force()
Expand Down
4 changes: 4 additions & 0 deletions executor/crates/common/src/expr/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ impl Parser {
self.peek(),
Token::Num(_)
| Token::Ident(_)
| Token::True
| Token::False
| Token::LParen
| Token::LBracket
| Token::LBrace
Expand Down Expand Up @@ -244,6 +246,8 @@ impl Parser {
match self.advance() {
Token::Num(n) => Ok(Expr::Const(n)),
Token::Ident(s) => Ok(Expr::Ident(s)),
Token::True => Ok(Expr::Bool(true)),
Token::False => Ok(Expr::Bool(false)),
Token::LParen => {
let expr = self.parse_expr()?;
self.expect(&Token::RParen)?;
Expand Down
8 changes: 8 additions & 0 deletions executor/crates/common/src/expr/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub(crate) enum Token {
If,
Then,
Else,
True,
False,
Backslash,
LBracket,
RBracket,
Expand Down Expand Up @@ -71,6 +73,8 @@ impl fmt::Display for Token {
Token::If => write!(f, "`if`"),
Token::Then => write!(f, "`then`"),
Token::Else => write!(f, "`else`"),
Token::True => write!(f, "`true`"),
Token::False => write!(f, "`false`"),
Token::Backslash => write!(f, r"`\`"),
Token::LBracket => write!(f, "`[`"),
Token::RBracket => write!(f, "`]`"),
Expand Down Expand Up @@ -298,6 +302,10 @@ impl<'a> Tokenizer<'a> {
"if" => Token::If,
"then" => Token::Then,
"else" => Token::Else,
// Reserved like the other keywords, so `true` is not usable as
// a bare attribute name; quote it (`{ "true" = ...; }`) instead.
"true" => Token::True,
"false" => Token::False,
_ => Token::Ident(s.to_owned()),
});
}
Expand Down
1 change: 1 addition & 0 deletions executor/crates/common/src/expr/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub enum EvalError {
pub enum Expr {
Ident(String),
Const(BigRational),
Bool(bool),
BinOp {
op: BinOp,
lhs: Box<Expr>,
Expand Down
50 changes: 50 additions & 0 deletions executor/crates/common/tests/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,53 @@ fn lazy_unused_argument_is_not_evaluated() {
// The argument `1 / 0` is passed as a thunk and never demanded.
assert_rational(eval(r"(\x = 7) (1 / 0)"), 7, 1);
}

#[test]
fn bool_literals() {
assert_bool(eval("true"), true);
assert_bool(eval("false"), false);
}

#[test]
fn bool_literals_in_conditions() {
assert_rational(eval("if true then 10 else 20"), 10, 1);
assert_rational(eval("if false then 10 else 20"), 20, 1);
// the shape that motivated literals: a guard whose other arm is a constant
// `false`, previously spellable only as a comparison that never holds
assert_rational(
eval("let guard = if 0 > 0 then 1 > 0 else false in if guard then 1 else 2"),
2,
1,
);
}

#[test]
fn bool_literals_compare_and_bind() {
assert_bool(eval("true == true"), true);
assert_bool(eval("true == false"), false);
assert_bool(eval("true != false"), true);
assert_bool(eval("let t = true in t"), true);
assert_bool(eval(r"(\x = x) false"), false);
assert_bool(eval("arrayGetElem [true, false] 1"), false);
}

#[test]
fn bool_literals_are_reserved_words() {
// like `if`/`let`, they cannot be rebound or used as a bare attribute name
assert!(Expr::parse("let true = 1 in true").is_err());
assert!(Expr::parse("{ true = 1; }").is_err());
// the quoted form stays available for both the key and the selection
assert_rational(eval(r#"{ "true" = 1; }."true""#), 1, 1);
}

#[test]
fn bool_literal_is_not_a_number() {
assert!(matches!(
Expr::parse("true + 1").unwrap().evaluate(),
Err(EvalError::TypeError { .. })
));
assert!(matches!(
Expr::parse("if 1 then 2 else 3").unwrap().evaluate(),
Err(EvalError::TypeError { .. })
));
}
45 changes: 43 additions & 2 deletions executor/src/exe/precompile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,48 @@ pub struct Args {
info: bool,
}

/// Writes `data` to a uniquely named sibling of `path`, then renames it onto
/// `path`.
///
/// The reader `deserialize_file`s whatever sits at the final path without
/// validating it, so a crash mid-write must not leave a truncated module there.
/// The temp name mixes pid, an in-process counter and the wall clock, so
/// concurrent writers never pick the same one; the fsync keeps the rename from
/// exposing a name whose contents are still only in page cache.
fn write_atomically(path: &std::path::Path, data: &[u8]) -> anyhow::Result<()> {
use std::io::Write as _;

static COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

let stamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |since_epoch| since_epoch.as_nanos());
let mut tmp_name = path
.file_name()
.expect("precompiled wasm path has no file name")
.to_owned();
tmp_name.push(format!(
".tmp-{}-{}-{stamp}",
std::process::id(),
COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
));
let tmp_path = path.with_file_name(tmp_name);

let write_tmp = || -> std::io::Result<()> {
let mut file = std::fs::File::create(&tmp_path)?;
file.write_all(data)?;
file.sync_all()?;
std::fs::rename(&tmp_path, path)
};

if let Err(e) = write_tmp() {
let _ = std::fs::remove_file(&tmp_path);
return Err(e).with_context(|| format!("writing {tmp_path:?} and renaming it to {path:?}"));
}

Ok(())
}

fn compile_single_file_single_mode(
result_path: &std::path::Path,
engine: &wasmtime::Engine,
Expand All @@ -40,8 +82,7 @@ fn compile_single_file_single_mode(

let sz = precompiled.len();

std::fs::write(result_path, precompiled)
.with_context(|| format!("writing to {result_path:?}"))?;
write_atomically(result_path, &precompiled)?;

log_info!("size" = sz, result:? = result_path, engine = engine_type, runner:? = runner_path, runner_path:? = path_in_runner, duration:? = time_start.elapsed(); "wasm writing done");

Expand Down
13 changes: 12 additions & 1 deletion executor/src/rt/supervisor/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,17 @@ impl Ctx<'_, '_> {
return Ok(None);
};

// `deserialize_file` below trusts the file as native code without looking
// at the wasm it was built from, so only ids the precompiler can actually
// have written may derive a path here. `precompile` only ever emits
// artifacts for runners listed in `all.json`. Every other id must fall
// through to a normal compile instead of picking up whatever happens to
// sit at that path, notably `custom:<hash>`, which an attacker chooses
// via RegisterRunner and which parses as a plain `name:hash` pair.
if !self.supervisor.runner_cache.has_in_all(id, hash) {
return Ok(None);
}

let special_name = caching::path_in_zip_to_hash(path);
let Some(cache_dir) = &self.supervisor.wasm_mod_cache.cache_dir else {
return Ok(None);
Expand All @@ -801,7 +812,7 @@ impl Ctx<'_, '_> {
cache_dir.set_extension(caching::DET_NON_DET_PRECOMPILED_SUFFIX.non_det);
let non_det_mod = cache_dir;

if !det_mod.exists() {
if !non_det_mod.exists() {
return Ok(None);
}

Expand Down
80 changes: 0 additions & 80 deletions executor/src/wasi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,86 +48,6 @@ impl Context {
}
}

#[cfg(any())]
fn add_to_linker_sync_dlsym<T: Send + 'static>(
linker: &mut wasmtime::Linker<T>,
linker_shared: Arc<Mutex<wasmtime::Linker<T>>>,
) -> anyhow::Result<()> {
use core::str;

linker.func_wrap(
"genlayer_dl",
"dlsym",
move |mut caller: wasmtime::Caller<'_, _>,
mod_name: u32,
mod_name_len: u32,
func_name: u32,
func_name_len: u32|
-> wiggle::anyhow::Result<i32> {
let export = caller.get_export("memory");
let mem = match &export {
Some(wiggle::wasmtime_crate::Extern::Memory(m)) => {
let (mem, _ctx) = m.data_and_store_mut(&mut caller);
wiggle::GuestMemory::Unshared(mem)
}
Some(wiggle::wasmtime_crate::Extern::SharedMemory(m)) => {
wiggle::GuestMemory::Shared(m.data())
}
_ => {
return anyhow::__private::Err({
let error = anyhow::__private::format_err(anyhow::__private::format_args!(
"missing required memory export"
));
error
})
}
};

let mod_name_ptr: wiggle::GuestPtr<[u8]> =
wiggle::GuestPtr::new(mod_name).as_array(mod_name_len);
let func_name_ptr: wiggle::GuestPtr<[u8]> =
wiggle::GuestPtr::new(func_name).as_array(func_name_len);

let mod_name = Vec::from_iter(mem.as_cow(mod_name_ptr)?.into_iter().cloned());
let mod_name = str::from_utf8(&mod_name)?;

let mod_name = match mod_name.rfind('/') {
Some(i) => &mod_name[i + 1..],
_ => mod_name,
};

if mod_name.is_empty() {
anyhow::bail!("can't load from unnamed");
}

let func_name = Vec::from_iter(mem.as_cow(func_name_ptr)?.into_iter().cloned());
let func_name = str::from_utf8(&func_name)?;

log_trace!(target: "rt", module = mod_name, function = func_name; "dlsym called");

let linker_shared = linker_shared.clone();
let Ok(ref mut linker) = linker_shared.lock() else {
panic!("dlsym: linker mutex poisoned");
};

let fn_exported = linker
.get(&mut caller, mod_name, func_name)
.ok_or_else(|| anyhow::anyhow!("function entity not found"))?
.into_func()
.ok_or_else(|| anyhow::anyhow!("found entity is not a function"))?;

let table = caller
.get_export("__indirect_function_table")
.ok_or_else(|| anyhow::anyhow!("__indirect_function_table not found"))?
.into_table()
.ok_or_else(|| anyhow::anyhow!("no __indirect_function_table"))?;
let res = table.grow(&mut caller, 1, fn_exported.into())?;
Ok(res.try_into()?)
},
)?;
Ok(())
}

pub(super) fn add_to_linker_sync<T: Send + 'static>(
linker: &mut wasmtime::Linker<T>,
f: impl Fn(&mut T) -> &mut Context + Copy + Send + Sync + 'static,
Expand Down
Loading