diff --git a/Cargo.lock b/Cargo.lock index 6892e2c0..120fe7a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,15 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "addr2line" -version = "0.21.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" -dependencies = [ - "gimli 0.28.1", -] - [[package]] name = "addr2line" version = "0.22.0" @@ -222,17 +213,6 @@ dependencies = [ "syn 2.0.74", ] -[[package]] -name = "async-trait" -version = "0.1.81" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e0c28dcc82d7c8ead5cb13beb15405b57b8546e93215673ff8ca0349a028107" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.74", -] - [[package]] name = "autocfg" version = "1.3.0" @@ -245,7 +225,7 @@ version = "0.3.73" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a" dependencies = [ - "addr2line 0.22.0", + "addr2line", "cc", "cfg-if", "libc", @@ -266,12 +246,6 @@ version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" -[[package]] -name = "base64" -version = "0.21.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" - [[package]] name = "base64" version = "0.22.1" @@ -1103,16 +1077,6 @@ dependencies = [ "subtle", ] -[[package]] -name = "directories-next" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "339ee130d97a610ea5a5872d2bbb130fdf68884ff09d3028b81bec8a1ac23bbc" -dependencies = [ - "cfg-if", - "dirs-sys-next", -] - [[package]] name = "dirs" version = "4.0.0" @@ -1133,17 +1097,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "dirs-sys-next" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d" -dependencies = [ - "libc", - "redox_users", - "winapi", -] - [[package]] name = "displaydoc" version = "0.2.5" @@ -1421,28 +1374,6 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" -[[package]] -name = "fxhash" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c" -dependencies = [ - "byteorder", -] - -[[package]] -name = "fxprof-processed-profile" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "27d12c0aed7f1e24276a241aadc4cb8ea9f83000f34bc062b7cc2d51e3b0fabd" -dependencies = [ - "bitflags 2.6.0", - "debugid", - "fxhash", - "serde", - "serde_json", -] - [[package]] name = "generic-array" version = "0.14.7" @@ -1823,26 +1754,6 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" -[[package]] -name = "ittapi" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b996fe614c41395cdaedf3cf408a9534851090959d90d54a535f675550b64b1" -dependencies = [ - "anyhow", - "ittapi-sys", - "log", -] - -[[package]] -name = "ittapi-sys" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52f5385394064fa2c886205dba02598013ce83d3e92d33dbdc0c52fe0e7bf4fc" -dependencies = [ - "cc", -] - [[package]] name = "jobserver" version = "0.1.32" @@ -4013,15 +3924,6 @@ dependencies = [ "leb128", ] -[[package]] -name = "wasm-encoder" -version = "0.215.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fb56df3e06b8e6b77e37d2969a50ba51281029a9aeb3855e76b7f49b6418847" -dependencies = [ - "leb128", -] - [[package]] name = "wasmparser" version = "0.209.1" @@ -4066,18 +3968,12 @@ version = "22.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "786d8b5e7a4d54917c5ebe555b9667337e5f93383f49bddaaeec2eba68093b45" dependencies = [ - "addr2line 0.21.0", "anyhow", - "async-trait", "bumpalo", "cc", "cfg-if", - "encoding_rs", - "fxprof-processed-profile", - "gimli 0.28.1", "hashbrown 0.14.5", "indexmap 2.4.0", - "ittapi", "libc", "libm", "log", @@ -4091,28 +3987,19 @@ dependencies = [ "psm", "rayon", "rustix", - "semver 1.0.23", "serde", "serde_derive", - "serde_json", "smallvec", "sptr", "target-lexicon", - "wasm-encoder 0.209.1", "wasmparser 0.209.1", "wasmtime-asm-macros", - "wasmtime-cache", "wasmtime-component-macro", - "wasmtime-component-util", "wasmtime-cranelift", "wasmtime-environ", - "wasmtime-fiber", - "wasmtime-jit-debug", "wasmtime-jit-icache-coherence", "wasmtime-slab", "wasmtime-versioned-export-macros", - "wasmtime-winch", - "wat", "windows-sys 0.52.0", ] @@ -4125,26 +4012,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "wasmtime-cache" -version = "22.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "916610f9ae9a6c22deb25bba2e6247ba9f00b093d30620875203b91328a1adfa" -dependencies = [ - "anyhow", - "base64 0.21.7", - "directories-next", - "log", - "postcard", - "rustix", - "serde", - "serde_derive", - "sha2 0.10.8", - "toml", - "windows-sys 0.52.0", - "zstd", -] - [[package]] name = "wasmtime-component-macro" version = "22.0.0" @@ -4197,51 +4064,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f444e900e848b884d8a8a2949b6f5b92af642a3e663ff8fbe78731143a55be61" dependencies = [ "anyhow", - "cpp_demangle", "cranelift-entity", "gimli 0.28.1", "indexmap 2.4.0", "log", "object", "postcard", - "rustc-demangle", "serde", "serde_derive", "target-lexicon", "wasm-encoder 0.209.1", "wasmparser 0.209.1", "wasmprinter", - "wasmtime-component-util", "wasmtime-types", ] -[[package]] -name = "wasmtime-fiber" -version = "22.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ded58eb2d1bf0dcd2182d0ccd7055c4b10b50d711514f1d73f61515d0fa829d" -dependencies = [ - "anyhow", - "cc", - "cfg-if", - "rustix", - "wasmtime-asm-macros", - "wasmtime-versioned-export-macros", - "windows-sys 0.52.0", -] - -[[package]] -name = "wasmtime-jit-debug" -version = "22.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9bc54198c6720f098210a85efb3ba8c078d1de4d373cdb6778850a66ae088d11" -dependencies = [ - "object", - "once_cell", - "rustix", - "wasmtime-versioned-export-macros", -] - [[package]] name = "wasmtime-jit-icache-coherence" version = "22.0.0" @@ -4284,23 +4121,6 @@ dependencies = [ "syn 2.0.74", ] -[[package]] -name = "wasmtime-winch" -version = "22.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed4db238a0241df2d15f79ad17b3a37a27f2ea6cb885894d81b42ae107544466" -dependencies = [ - "anyhow", - "cranelift-codegen", - "gimli 0.28.1", - "object", - "target-lexicon", - "wasmparser 0.209.1", - "wasmtime-cranelift", - "wasmtime-environ", - "winch-codegen", -] - [[package]] name = "wasmtime-wit-bindgen" version = "22.0.0" @@ -4313,28 +4133,6 @@ dependencies = [ "wit-parser", ] -[[package]] -name = "wast" -version = "215.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ff1d00d893593249e60720be04a7c1f42f1c4dc3806a2869f4e66ab61eb54cb" -dependencies = [ - "bumpalo", - "leb128", - "memchr", - "unicode-width", - "wasm-encoder 0.215.0", -] - -[[package]] -name = "wat" -version = "1.215.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "670bf4d9c8cf76ae242d70ded47c546525b6dafaa6871f9bcb065344bf2b4e3d" -dependencies = [ - "wast", -] - [[package]] name = "wezterm-bidi" version = "0.2.3" @@ -4446,23 +4244,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" -[[package]] -name = "winch-codegen" -version = "0.20.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85c6915884e731b2db0d8cf08cb64474cb69221a161675fd3c135f91febc3daa" -dependencies = [ - "anyhow", - "cranelift-codegen", - "gimli 0.28.1", - "regalloc2", - "smallvec", - "target-lexicon", - "wasmparser 0.209.1", - "wasmtime-cranelift", - "wasmtime-environ", -] - [[package]] name = "windows-core" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index 6d3d6cbc..e6e76dc5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -97,7 +97,7 @@ thiserror = "1.0.61" tlsh-fixed = "0.1.1" uuid = "1.4.1" walrus = "0.21.1" -wasmtime = "22.0.0" +wasmtime = { version = "22.0.0", default-features = false } x509-parser = "0.16.0" yaml-rust = "0.4.5" yansi = "1.0.1" diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 567c7584..12c97e56 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -212,7 +212,7 @@ thiserror = { workspace = true } tlsh-fixed = { workspace = true, optional = true } uuid = { workspace = true, optional = true, features = ["v4"] } walrus = { workspace = true } -wasmtime = { workspace = true, features = ["cranelift", "parallel-compilation"] } +wasmtime = { workspace = true, default-features = false, features = ["cranelift", "parallel-compilation", "runtime"] } x509-parser = { workspace = true, optional = true } yansi = { workspace = true } yara-x-macros = { workspace = true } diff --git a/lib/src/compiler/emit.rs b/lib/src/compiler/emit.rs index bdae9421..44f65bbd 100644 --- a/lib/src/compiler/emit.rs +++ b/lib/src/compiler/emit.rs @@ -132,24 +132,33 @@ macro_rules! emit_shift_op { // // eval lhs // eval rhs - // move rhs to tmp while leaving it in the stack (local_tee) - // push result form shift operation - // push 0 - // push rhs (from tmp) + // move rhs to tmp_b + // move lhs to tmp_a + // push rhs // push 64 // is rhs less than 64? - // if true ┐ - // push result form shift operation │ select - // else │ - // push 0 ┘ + // if true + // push lhs + // push rhs + // else + // push 0 // - $instr.local_tee($ctx.wasm_symbols.i64_tmp); - $instr.binop(BinaryOp::$int_op); - $instr.i64_const(0); - $instr.local_get($ctx.wasm_symbols.i64_tmp); + $instr.local_set($ctx.wasm_symbols.i64_tmp_b); + $instr.local_set($ctx.wasm_symbols.i64_tmp_a); + $instr.local_get($ctx.wasm_symbols.i64_tmp_b); $instr.i64_const(64); $instr.binop(BinaryOp::I64LtS); - $instr.select(Some(I64)); + $instr.if_else( + I64, + |then_| { + then_.local_get($ctx.wasm_symbols.i64_tmp_a); + then_.local_get($ctx.wasm_symbols.i64_tmp_b); + then_.binop(BinaryOp::$int_op); + }, + |else_| { + else_.i64_const(0); + }, + ) } _ => unreachable!(), }; @@ -1574,7 +1583,7 @@ fn emit_for_in_range( // Store lower_bound in temp variable, without removing // it from the stack. - instr.local_tee(ctx.wasm_symbols.i64_tmp); + instr.local_tee(ctx.wasm_symbols.i64_tmp_a); // Compute upper_bound - lower_bound + 1. instr.binop(BinaryOp::I64Sub); @@ -1602,7 +1611,7 @@ fn emit_for_in_range( // Store lower_bound in `next_item`. set_var(ctx, instr, next_item, |ctx, instr| { - instr.local_get(ctx.wasm_symbols.i64_tmp); + instr.local_get(ctx.wasm_symbols.i64_tmp_a); }); }, // Before each iteration. @@ -2342,9 +2351,9 @@ fn set_vars( | Type::Struct | Type::Array | Type::Map => { - instr.local_set(ctx.wasm_symbols.i64_tmp); + instr.local_set(ctx.wasm_symbols.i64_tmp_a); instr.i32_const(var.index * Var::mem_size()); - instr.local_get(ctx.wasm_symbols.i64_tmp); + instr.local_get(ctx.wasm_symbols.i64_tmp_a); instr.store( ctx.wasm_symbols.main_memory, StoreKind::I64 { atomic: false }, @@ -2779,7 +2788,7 @@ fn throw_undef(ctx: &mut EmitContext, instr: &mut InstrSeqBuilder) { fn throw_undef_if_zero(ctx: &mut EmitContext, instr: &mut InstrSeqBuilder) { // Save the top of the stack into temp variable, but leave a copy in the // stack. - let tmp = ctx.wasm_symbols.i64_tmp; + let tmp = ctx.wasm_symbols.i64_tmp_a; instr.local_tee(tmp); // Is top of the stack zero? The comparison removes the value from the // stack. diff --git a/lib/src/wasm/builder.rs b/lib/src/wasm/builder.rs index afb6e76d..d240a5af 100644 --- a/lib/src/wasm/builder.rs +++ b/lib/src/wasm/builder.rs @@ -173,7 +173,8 @@ impl WasmModuleBuilder { filesize, pattern_search_done, timeout_occurred, - i64_tmp: module.locals.add(I64), + i64_tmp_a: module.locals.add(I64), + i64_tmp_b: module.locals.add(I64), i32_tmp: module.locals.add(I32), f64_tmp: module.locals.add(F64), }; diff --git a/lib/src/wasm/mod.rs b/lib/src/wasm/mod.rs index 45eb3f41..5e701978 100644 --- a/lib/src/wasm/mod.rs +++ b/lib/src/wasm/mod.rs @@ -699,7 +699,8 @@ pub(crate) struct WasmSymbols { pub timeout_occurred: walrus::GlobalId, /// Local variables used for temporary storage. - pub i64_tmp: walrus::LocalId, + pub i64_tmp_a: walrus::LocalId, + pub i64_tmp_b: walrus::LocalId, pub i32_tmp: walrus::LocalId, pub f64_tmp: walrus::LocalId, } @@ -720,6 +721,7 @@ lazy_static! { config.cranelift_opt_level(wasmtime::OptLevel::SpeedAndSize); config.epoch_interruption(true); + config.wasm_bulk_memory(false); config }; diff --git a/lib/src/wasm/string.rs b/lib/src/wasm/string.rs index bc09f3bd..17f4221f 100644 --- a/lib/src/wasm/string.rs +++ b/lib/src/wasm/string.rs @@ -36,7 +36,7 @@ pub(crate) type RuntimeStringWasm = i64; /// String types handled by YARA's WASM runtime. /// -/// At runtime, when the the WASM code generated for rule conditions is +/// At runtime, when the WASM code generated for rule conditions is /// being executed, text strings can adopt multiple forms. The difference /// between them resides in the place in which the string's data is stored. ///