Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

7.4 KB
Raw Blame
#![cfg(arc_try_new)]

use wasmtime::component::{Component, Linker, Val};
use wasmtime::{Config, Engine, Result, Store};
use wasmtime_fuzzing::oom::OomTest;

#[tokio::test]
async fn component_instance_pre_instantiate_async() -> Result<()> {
    let mut config = Config::new();
    config.concurrency_support(false);
    let engine = Engine::new(&config)?;
    let component = Component::new(
        &engine,
        r#"
            (component
                (core module $m
                    (func (export "id") (param i32) (result i32) (local.get 0))
                )
                (core instance $i (instantiate $m))
                (func (export "id") (param "x" s32) (result s32)
                    (canon lift (core func $i "id"))
                )
            )
        "#,
    )?;
    let linker = Linker::<()>::new(&engine);
    let instance_pre = linker.instantiate_pre(&component)?;

    OomTest::new()
        .test_async(|| async {
            let mut store = Store::try_new(&engine, ())?;
            let _instance = instance_pre.instantiate_async(&mut store).await?;
            Ok(())
        })
        .await
}

#[tokio::test]
async fn component_func_call_async() -> Result<()> {
    let mut config = Config::new();
    config.concurrency_support(false);
    let engine = Engine::new(&config)?;
    let component = Component::new(
        &engine,
        r#"
            (component
                (core module $m
                    (func (export "id") (param i32) (result i32) (local.get 0))
                )
                (core instance $i (instantiate $m))
                (func (export "id") (param "x" s32) (result s32)
                    (canon lift (core func $i "id"))
                )
            )
        "#,
    )?;
    let linker = Linker::<()>::new(&engine);
    let instance_pre = linker.instantiate_pre(&component)?;

    OomTest::new()
        .test_async(|| async {
            let mut store = Store::try_new(&engine, ())?;
            let instance = instance_pre.instantiate_async(&mut store).await?;
            let func = instance.get_func(&mut store, "id").unwrap();
            let mut results = [Val::S32(0)];
            func.call_async(&mut store, &[Val::S32(42)], &mut results)
                .await?;
            assert_eq!(results[0], Val::S32(42));
            Ok(())
        })
        .await
}

#[tokio::test]
async fn component_typed_func_call_async() -> Result<()> {
    let mut config = Config::new();
    config.concurrency_support(false);
    let engine = Engine::new(&config)?;
    let component = Component::new(
        &engine,
        r#"
            (component
                (core module $m
                    (func (export "id") (param i32) (result i32) (local.get 0))
                )
                (core instance $i (instantiate $m))
                (func (export "id") (param "x" s32) (result s32)
                    (canon lift (core func $i "id"))
                )
            )
        "#,
    )?;
    let linker = Linker::<()>::new(&engine);
    let instance_pre = linker.instantiate_pre(&component)?;

    OomTest::new()
        .test_async(|| async {
            let mut store = Store::try_new(&engine, ())?;
            let instance = instance_pre.instantiate_async(&mut store).await?;
            let id = instance.get_typed_func::<(i32,), (i32,)>(&mut store, "id")?;
            let result = id.call_async(&mut store, (42,)).await?;
            assert_eq!(result, (42,));
            Ok(())
        })
        .await
}

#[test]
fn component_instance_pre_instantiate() -> Result<()> {
    let mut config = Config::new();
    config.concurrency_support(false);
    let engine = Engine::new(&config)?;
    let component = Component::new(
        &engine,
        r#"
            (component
                (core module $m
                    (func (export "id") (param i32) (result i32) (local.get 0))
                )
                (core instance $i (instantiate $m))
                (func (export "id") (param "x" s32) (result s32)
                    (canon lift (core func $i "id"))
                )
            )
        "#,
    )?;
    let linker = Linker::<()>::new(&engine);
    let instance_pre = linker.instantiate_pre(&component)?;

    OomTest::new().test(|| {
        let mut store = Store::try_new(&engine, ())?;
        let _instance = instance_pre.instantiate(&mut store)?;
        Ok(())
    })
}

#[test]
fn component_func_call() -> Result<()> {
    let mut config = Config::new();
    config.concurrency_support(false);
    let engine = Engine::new(&config)?;
    let component = Component::new(
        &engine,
        r#"
            (component
                (core module $m
                    (func (export "id") (param i32) (result i32) (local.get 0))
                )
                (core instance $i (instantiate $m))
                (func (export "id") (param "x" s32) (result s32)
                    (canon lift (core func $i "id"))
                )
            )
        "#,
    )?;
    let linker = Linker::<()>::new(&engine);
    let instance_pre = linker.instantiate_pre(&component)?;

    OomTest::new().test(|| {
        let mut store = Store::try_new(&engine, ())?;
        let instance = instance_pre.instantiate(&mut store)?;
        let func = instance.get_func(&mut store, "id").unwrap();
        let mut results = [Val::S32(0)];
        func.call(&mut store, &[Val::S32(42)], &mut results)?;
        assert_eq!(results[0], Val::S32(42));
        Ok(())
    })
}

#[test]
fn component_typed_func_call() -> Result<()> {
    let mut config = Config::new();
    config.concurrency_support(false);
    let engine = Engine::new(&config)?;
    let component = Component::new(
        &engine,
        r#"
            (component
                (core module $m
                    (func (export "id") (param i32) (result i32) (local.get 0))
                )
                (core instance $i (instantiate $m))
                (func (export "id") (param "x" s32) (result s32)
                    (canon lift (core func $i "id"))
                )
            )
        "#,
    )?;
    let linker = Linker::<()>::new(&engine);
    let instance_pre = linker.instantiate_pre(&component)?;

    OomTest::new().test(|| {
        let mut store = Store::try_new(&engine, ())?;
        let instance = instance_pre.instantiate(&mut store)?;
        let id = instance.get_typed_func::<(i32,), (i32,)>(&mut store, "id")?;
        let result = id.call(&mut store, (42,))?;
        assert_eq!(result, (42,));
        Ok(())
    })
}

#[test]
fn component_func_typed() -> Result<()> {
    let mut config = Config::new();
    config.concurrency_support(false);
    let engine = Engine::new(&config)?;
    let component = Component::new(
        &engine,
        r#"
            (component
                (core module $m
                    (func (export "id") (param i32) (result i32) (local.get 0))
                )
                (core instance $i (instantiate $m))
                (func (export "id") (param "x" s32) (result s32)
                    (canon lift (core func $i "id"))
                )
            )
        "#,
    )?;
    let linker = Linker::<()>::new(&engine);
    let instance_pre = linker.instantiate_pre(&component)?;

    OomTest::new().test(|| {
        let mut store = Store::try_new(&engine, ())?;
        let instance = instance_pre.instantiate(&mut store)?;
        let func = instance.get_func(&mut store, "id").unwrap();
        let _typed = func.typed::<(i32,), (i32,)>(&store)?;
        Ok(())
    })
}