wasm-rquickjs-cli 0.4.14

Tool for wrapping JavaScript modules as WebAssembly components using the QuickJS engine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
use camino::Utf8Path;
use camino_tempfile::Utf8TempDir;
use wasm_rquickjs::{OptimizeOptions, optimize_component, optimize_component_with_options};

mod p3_build;

// Initialization mutates the last byte, so verifying the optimized component
// checks that snapshot extraction retained memory, not just that Wizer returned.
fn write_component(path: &Utf8Path, pages: u32) -> anyhow::Result<()> {
    let last_byte = pages * 65536 - 1;
    let wasm = wat::parse_str(format!(
        r#"(component
            (core module $m
                (memory (export "memory") {pages})
                (func (export "init")
                    i32.const {last_byte} i32.const 77 i32.store8)
                (func (export "read") (result i32)
                    i32.const {last_byte} i32.load8_u))
            (core instance $i (instantiate $m))
            (func (export "wizer-initialize") (canon lift (core func $i "init")))
            (func (export "read") (result u32) (canon lift (core func $i "read"))))"#
    ))?;
    std::fs::write(path, wasm)?;
    Ok(())
}

async fn assert_snapshot(path: &Utf8Path) -> anyhow::Result<()> {
    let mut config = wasmtime::Config::new();
    config.wasm_component_model(true);
    let engine = wasmtime::Engine::new(&config)?;
    let component = wasmtime::component::Component::from_file(&engine, path)?;
    let mut store = wasmtime::Store::new(&engine, ());
    let linker = wasmtime::component::Linker::new(&engine);
    let instance = linker.instantiate_async(&mut store, &component).await?;
    let read = instance.get_typed_func::<(), (u32,)>(&mut store, "read")?;
    assert_eq!(read.call_async(&mut store, ()).await?.0, 77);
    Ok(())
}

#[tokio::test]
async fn optimizer_default_options_preserve_existing_behavior() -> anyhow::Result<()> {
    assert_eq!(OptimizeOptions::default().hostcall_fuel, None);
    let temp = Utf8TempDir::new()?;
    let input = temp.path().join("input.wasm");
    let legacy = temp.path().join("legacy.wasm");
    let explicit = temp.path().join("explicit.wasm");
    write_component(&input, 3)?;
    optimize_component(&input, &legacy, "wizer-initialize").await?;
    optimize_component_with_options(
        &input,
        &explicit,
        "wizer-initialize",
        &OptimizeOptions::default(),
    )
    .await?;
    assert_eq!(std::fs::read(&legacy)?, std::fs::read(&explicit)?);
    assert_snapshot(&legacy).await?;
    Ok(())
}

#[tokio::test]
async fn optimizer_determinism_requires_matching_restore_capability() -> anyhow::Result<()> {
    let temp = Utf8TempDir::new()?;
    let input = temp.path().join("input.wasm");
    let output = temp.path().join("output.wasm");
    for (marker, init_func, deterministic) in [
        ("", "wizer-initialize", false),
        (
            r#"(func (export "wasm-rquickjs-wizer-restore-v1") (canon lift (core func $i "marker")))"#,
            "wizer-initialize",
            true,
        ),
        // Same spelling, wrong signature: not a restore capability.
        (
            r#"(func (export "wasm-rquickjs-wizer-restore-v1") (result u64) (canon lift (core func $i "read")))"#,
            "wizer-initialize",
            false,
        ),
        (
            r#"(func (export "wasm-rquickjs-wizer-restore-v1") (canon lift (core func $i "marker")))"#,
            "other-initialize",
            false,
        ),
    ] {
        std::fs::write(
            &input,
            wat::parse_str(format!(
                r#"(component
            (import "wasi:random/random@0.2.0" (instance $random
                (export "get-random-u64" (func (result u64)))))
            (alias export $random "get-random-u64" (func $random-u64))
            (core func $random-u64 (canon lower (func $random-u64)))
            (core instance $host (export "random" (func $random-u64)))
            (core module $m
                (import "host" "random" (func $random (result i64)))
                (memory (export "memory") 1)
                (func (export "marker"))
                (func (export "init") i32.const 0 call $random i64.store)
                (func (export "read") (result i64) i32.const 0 i64.load))
            (core instance $i (instantiate $m (with "host" (instance $host))))
            (func (export "wizer-initialize") (canon lift (core func $i "init")))
            (func (export "other-initialize") (canon lift (core func $i "init")))
            (func (export "read") (result u64) (canon lift (core func $i "read")))
            {marker})"#
            ))?,
        )?;
        let mut snapshots = Vec::new();
        for _ in 0..2 {
            optimize_component(&input, &output, init_func).await?;
            snapshots.push(std::fs::read(&output)?);
        }
        assert_eq!(
            snapshots[0] == snapshots[1],
            deterministic,
            "only a supported capability paired with its initializer enables deterministic entropy"
        );
    }
    Ok(())
}

async fn run_probe(path: &Utf8Path, deny_entropy: bool) -> anyhow::Result<Option<String>> {
    use wasmtime::component::{Component, Linker, ResourceTable};
    use wasmtime::{Config, Engine, Store, StoreContextMut};

    struct Host {
        table: ResourceTable,
        wasi: wasmtime_wasi::WasiCtx,
        http: wasmtime_wasi_http::WasiHttpCtx,
        entropy_requested: bool,
        #[cfg(feature = "use-golem-wasmtime")]
        io_ctx: wasmtime_wasi::IoCtx,
    }
    impl wasmtime_wasi::WasiView for Host {
        fn ctx(&mut self) -> wasmtime_wasi::WasiCtxView<'_> {
            wasmtime_wasi::WasiCtxView {
                ctx: &mut self.wasi,
                table: &mut self.table,
                #[cfg(feature = "use-golem-wasmtime")]
                io_ctx: &mut self.io_ctx,
            }
        }
    }
    impl wasmtime_wasi_http::p3::WasiHttpView for Host {
        fn http(&mut self) -> wasmtime_wasi_http::p3::WasiHttpCtxView<'_> {
            wasmtime_wasi_http::p3::WasiHttpCtxView {
                ctx: &mut self.http,
                table: &mut self.table,
                hooks: wasmtime_wasi_http::p3::default_hooks(),
            }
        }
    }
    let mut config = Config::new();
    config
        .wasm_component_model(true)
        .wasm_component_model_async(true);
    if std::env::var("WASM_RQUICKJS_TEST_WASMTIME_CACHE").is_ok_and(|value| value == "1") {
        config.cache(Some(wasmtime::Cache::new(wasmtime::CacheConfig::new())?));
    }
    let engine = Engine::new(&config)?;
    let component = Component::from_file(&engine, path)?;
    let mut builder = wasmtime_wasi::WasiCtxBuilder::new();
    builder.env("GOL787_RUNTIME", "live").arg(path.as_str());
    #[cfg(not(feature = "use-golem-wasmtime"))]
    let wasi = builder.build();
    #[cfg(feature = "use-golem-wasmtime")]
    let (wasi, io_ctx) = builder.build();
    let mut store = Store::new(
        &engine,
        Host {
            table: ResourceTable::new(),
            wasi,
            http: wasmtime_wasi_http::WasiHttpCtx::new(),
            entropy_requested: false,
            #[cfg(feature = "use-golem-wasmtime")]
            io_ctx,
        },
    );
    let mut linker = Linker::new(&engine);
    // Populate trapping HTTP/logging definitions before installing live WASI;
    // define_unknown_imports_as_traps replaces entire instance definitions.
    linker.define_unknown_imports_as_traps(&component)?;
    linker.allow_shadowing(true);
    wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
    wasmtime_wasi::p3::add_to_linker(&mut linker)?;
    // Unknown-import stubs are synchronous; P3 HTTP imports require the real
    // async signatures even though this probe never performs HTTP requests.
    wasmtime_wasi_http::p3::add_to_linker(&mut linker)?;
    for (name, _) in component.component_type().imports(&engine) {
        if deny_entropy && name.starts_with("wasi:random/random@") {
            let mut random = linker.instance(name)?;
            random.func_wrap(
                "get-random-u64",
                |mut ctx: StoreContextMut<'_, Host>, (): ()| {
                    ctx.data_mut().entropy_requested = true;
                    Err::<(u64,), _>(wasmtime::Error::msg("runtime entropy denied"))
                },
            )?;
            random.func_wrap(
                "get-random-bytes",
                |mut ctx: StoreContextMut<'_, Host>, (_len,): (u64,)| {
                    ctx.data_mut().entropy_requested = true;
                    Err::<(Vec<u8>,), _>(wasmtime::Error::msg("runtime entropy denied"))
                },
            )?;
        }
    }
    let instance = linker.instantiate_async(&mut store, &component).await?;
    if !deny_entropy {
        let run = instance.get_typed_func::<(), (String,)>(&mut store, "run")?;
        let (json,) = store
            .run_concurrent(async move |accessor| run.call_concurrent(accessor, ()).await)
            .await??;
        return Ok(Some(json));
    }
    let ready = instance.get_typed_func::<(), (String,)>(&mut store, "ready")?;
    let result = ready.call_async(&mut store, ()).await;
    assert!(result.is_err(), "restore ignored unavailable entropy");
    assert!(
        store.data().entropy_requested,
        "failure was not an entropy request: {result:?}"
    );
    assert!(
        ready.call_async(&mut store, ()).await.is_err(),
        "retry after a failed restore exposed application JS"
    );
    Ok(None)
}

#[test]
fn optimizer_restores_live_services_after_deterministic_preinit() -> anyhow::Result<()> {
    use std::process::Command;
    use wasm_rquickjs::{EmbeddingMode, GenerationTarget, JsModuleSpec};

    let temp = Utf8TempDir::new()?;
    let runtime = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()?;
    for (target, tier) in [
        (GenerationTarget::WasiP2, "p2,encoding"),
        (GenerationTarget::WasiP2, "p2,crypto,encoding"),
        (GenerationTarget::WasiP3, "p3,encoding"),
        (GenerationTarget::WasiP3, "p3,crypto,encoding"),
    ] {
        let root = temp.path().join(tier.replace(',', "-"));
        std::fs::create_dir_all(root.join("wit"))?;
        let async_keyword = if target == GenerationTarget::WasiP3 {
            "async "
        } else {
            ""
        };
        std::fs::write(
            root.join("wit/world.wit"),
            format!(
                "package test:preinit; world preinit-probe {{ export run: {async_keyword}func() -> string; export ready: {async_keyword}func() -> string; }}"
            ),
        )?;
        wasm_rquickjs::generate_wrapper_crate_with_target(
            &root.join("wit"),
            &[JsModuleSpec {
                name: "module".into(),
                mode: EmbeddingMode::BinarySlot,
            }],
            &root.join("out"),
            None,
            target,
        )?;
        let cache = p3_build::BuildCache::new()?;
        let mut build = Command::new("cargo");
        cache.configure(&mut build);
        let result = build
            .args([
                "build",
                "--target",
                "wasm32-wasip2",
                "--no-default-features",
                "--features",
                tier,
                "--manifest-path",
            ])
            .arg(root.join("out/Cargo.toml"))
            .output()?;
        assert!(
            result.status.success(),
            "{tier}: {}",
            String::from_utf8_lossy(&result.stderr)
        );
        let mut metadata = Command::new("cargo");
        cache.configure(&mut metadata);
        let result = metadata
            .args([
                "metadata",
                "--no-deps",
                "--format-version",
                "1",
                "--manifest-path",
            ])
            .arg(root.join("out/Cargo.toml"))
            .output()?;
        assert!(result.status.success());
        let metadata: serde_json::Value = serde_json::from_slice(&result.stdout)?;
        let wasm = Utf8Path::new(metadata["target_directory"].as_str().unwrap())
            .join("wasm32-wasip2/debug/preinit_probe.wasm");
        let template = cache.finish(wasm.as_std_path(), root.as_std_path())?;
        let template = Utf8Path::from_path(&template).unwrap();
        let js = root.join("main.js");
        std::fs::write(
            &js,
            r#"
            import { performance } from 'node:perf_hooks';
            import { runInNewContext } from 'node:vm';
            const random = Math.random;
            const vmRandom = runInNewContext('Math.random');
            const now = performance.now.bind(performance);
            const uuid = crypto.randomUUID.bind(crypto);
            const frozen = { time: Date.now(), random: random(),
                bytes: [...crypto.getRandomValues(new Uint8Array(32))],
                origin: performance.timeOrigin, uptime: process.uptime() };
            export function ready() { return 'ready'; }
            export function run() {
                const monotonic = process.hrtime.bigint();
                const perfStart = now();
                const start = Date.now();
                while (Date.now() - start < 20) {}
                return JSON.stringify({ frozen, time: Date.now(),
                    origin: performance.timeOrigin, jsonOrigin: performance.toJSON().timeOrigin,
                    elapsed: now() - perfStart, uptime: process.uptime(),
                    monotonicElapsed: Number(process.hrtime.bigint() - monotonic),
                    random: [random(), random()], vmRandom: [vmRandom(), vmRandom()],
                    bytes: [...crypto.getRandomValues(new Uint8Array(32))], uuid: uuid(),
                    env: process.env.GOL787_RUNTIME, argv: process.argv });
            }
        "#,
        )?;
        let input = root.join("input.wasm");
        let source = std::fs::read_to_string(&js)?;
        wasm_rquickjs::inject_js_into_component(template, &input, &[&source])?;
        let mut previous = None;
        let optimized = root.join("optimized.wasm");
        for _ in 0..3 {
            // Independent optimizer processes: no host RNG/clock state is reused.
            let result = Command::new(env!("CARGO_BIN_EXE_wasm-rquickjs"))
                .args(["optimize", "--input"])
                .arg(&input)
                .arg("--output")
                .arg(&optimized)
                .output()?;
            assert!(
                result.status.success(),
                "{tier}: {}",
                String::from_utf8_lossy(&result.stderr)
            );
            assert!(!String::from_utf8_lossy(&result.stderr).contains("preserving legacy"));
            let bytes = std::fs::read(&optimized)?;
            if let Some(previous) = &previous {
                assert!(previous == &bytes, "{tier}: independent builds differ");
            }
            previous = Some(bytes);
        }
        eprintln!(
            "{tier}: 3 independent optimizations byte-identical; BLAKE3={}",
            blake3::hash(previous.as_ref().unwrap())
        );
        let mut previous: Option<serde_json::Value> = None;
        for component in [&optimized, &optimized, &input] {
            let before = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)?
                .as_millis() as u64;
            let json = runtime.block_on(run_probe(component, false))?.unwrap();
            let value: serde_json::Value = serde_json::from_str(&json)?;
            let after = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)?
                .as_millis() as u64;
            assert!((before..=after).contains(&value["time"].as_u64().unwrap()));
            assert!((before..=after).contains(&value["origin"].as_u64().unwrap()));
            assert_eq!(value["origin"], value["jsonOrigin"]);
            assert!(value["elapsed"].as_f64().unwrap() >= 15.0);
            assert!(value["monotonicElapsed"].as_u64().unwrap() >= 15_000_000);
            assert!((0.015..10.0).contains(&value["uptime"].as_f64().unwrap()));
            assert_eq!(value["env"], "live");
            assert!(value["argv"].as_array().unwrap().iter().any(|arg| {
                arg.as_str()
                    .is_some_and(|s| s.ends_with(component.file_name().unwrap()))
            }));
            for key in ["random", "vmRandom"] {
                let numbers = value[key].as_array().unwrap();
                assert_ne!(numbers[0], numbers[1]);
                assert!(
                    numbers
                        .iter()
                        .all(|n| (0.0..1.0).contains(&n.as_f64().unwrap()))
                );
            }
            if component == &optimized {
                assert_eq!(value["frozen"]["time"], 0);
                assert_eq!(value["frozen"]["origin"], 0);
                if let Some(previous) = &previous {
                    assert_eq!(
                        value["frozen"], previous["frozen"],
                        "application top-level values remain snapshotted"
                    );
                    for key in ["bytes", "random", "vmRandom", "uuid"] {
                        assert_ne!(
                            value[key], previous[key],
                            "{tier}: restored instances reused {key}"
                        );
                    }
                }
                assert_ne!(value["bytes"], value["frozen"]["bytes"]);
                previous = Some(value.clone());
            } else {
                assert!((before..=after).contains(&value["frozen"]["time"].as_u64().unwrap()));
            }
            eprintln!("{tier} {}: {json}", component.file_name().unwrap());
        }
        // This export makes no random calls. Without restore-time reseeding it
        // would succeed, silently leaving the build-time stream available.
        runtime.block_on(run_probe(&optimized, true))?;
        eprintln!("{tier}: entropy-denied restoration trapped before ready()");
    }
    Ok(())
}

#[cfg(feature = "use-golem-wasmtime")]
#[tokio::test]
async fn optimizer_copy_budget_controls_snapshot_extraction() -> anyhow::Result<()> {
    let temp = Utf8TempDir::new()?;
    let input = temp.path().join("input.wasm");
    let output = temp.path().join("output.wasm");
    // Initialization itself performs no hostcalls; exhaustion is in extraction.
    // Zero must fail even for a snapshot that succeeds with the engine default.
    // 129 MiB exceeds the fork's default 128 MiB copy budget.
    for (pages, fuel) in [
        (3, Some(0)),
        (129 * 16, None),
        (129 * 16, Some(128 * 1024 * 1024)),
    ] {
        write_component(&input, pages)?;
        let input = input.clone();
        let task_output = output.clone();
        let failure = tokio::spawn(async move {
            optimize_component_with_options(
                &input,
                &task_output,
                "wizer-initialize",
                &OptimizeOptions {
                    hostcall_fuel: fuel,
                },
            )
            .await
        })
        .await
        .expect_err("Wizer currently panics when snapshot extraction exhausts fuel");
        assert!(failure.is_panic());
        let panic = failure.into_panic();
        let message = panic.downcast_ref::<String>().expect("panic message");
        assert!(message.contains("fuel allocated for hostcalls has been exhausted"));
        assert!(!output.exists());
    }
    optimize_component_with_options(
        &input,
        &output,
        "wizer-initialize",
        &OptimizeOptions {
            hostcall_fuel: Some(512 * 1024 * 1024),
        },
    )
    .await?;
    assert_snapshot(&output).await?;
    Ok(())
}

#[cfg(not(feature = "use-golem-wasmtime"))]
#[tokio::test]
async fn optimizer_rejects_unsupported_copy_budget_before_io() {
    for fuel in [0, 512 * 1024 * 1024] {
        let error = optimize_component_with_options(
            Utf8Path::new("nonexistent-input.wasm"),
            Utf8Path::new("nonexistent-output.wasm"),
            "wizer-initialize",
            &OptimizeOptions {
                hostcall_fuel: Some(fuel),
            },
        )
        .await
        .unwrap_err();
        assert!(error.to_string().contains("requires use-golem-wasmtime"));
    }
}