Skip to content

Commit

Permalink
wip: (wasm-smith): allow specifying the set of valid imports
Browse files Browse the repository at this point in the history
  • Loading branch information
matklad authored and nagisa committed Apr 1, 2022
1 parent 78819f9 commit 0209fc7
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
94 changes: 94 additions & 0 deletions crates/wasm-smith/src/ast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/// TODO
#[derive(Debug)]
pub struct Import {
/// TODO
pub module: String,
/// TODO
pub name: Option<String>,
/// TODO
pub desc: EntityDesc,
}

/// TODO
#[non_exhaustive]
#[derive(Debug)]
pub enum EntityDesc {
/// TODO
Func(FuncType),
/// TODO
Global(GlobalType),
/// TODO
Table(TableType),
/// TODO
Memory(MemoryType),
/// TODO
Instance(InstanceType),
}

/// A function type.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct FuncType {
/// Types of parameters of the function.
pub params: Vec<ValType>,
/// Types of results of the function.
pub results: Vec<ValType>,
}

/// TODO
#[derive(Clone, Debug, PartialEq)]
pub struct GlobalType {
/// TODO
pub val_type: ValType,
/// TODO
pub mutable: bool,
}

/// Primitive WASM type.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum ValType {
/// Signed 32-bit integer.
I32,
/// Signed 64-bit integer.
I64,
/// 32-bit floating point number.
F32,
/// 64-bit floating point number.
F64,
/// TODO:
V128,
/// TODO:
FuncRef,
/// TODO:
ExternRef,
}

/// TODO:
#[derive(Clone, Debug)]
pub struct MemoryType {
/// TODO:
pub limits: Limits,
}

/// TODO:
#[derive(Clone, Debug)]
pub struct TableType {
/// TODO:
pub limits: Limits,
/// TODO:
pub elem_ty: ValType,
}

/// TODO:
#[derive(Clone, Debug)]
pub struct Limits {
/// TODO:
pub min: u32,
/// TODO:
pub max: Option<u32>,
}

#[derive(Debug)]
pub struct InstanceType {
pub exports: indexmap::IndexMap<String, EntityDesc>,
}
12 changes: 12 additions & 0 deletions crates/wasm-smith/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use crate::InstructionKinds;
use arbitrary::{Arbitrary, Result, Unstructured};

use crate::Import;

/// Configuration for a generated module.
///
/// Don't care to configure your generated modules? Just use
Expand Down Expand Up @@ -59,6 +61,16 @@ pub trait Config: 'static + std::fmt::Debug {
100
}

/// The imports that may be used when generating the module.
///
/// Defaults to `None` which means that any arbitrary import can be generated.
///
/// To only allow imports from a specific set, override this method to return a vec of each
/// available import.
fn available_imports(&self) -> Option<Vec<Import>> {
None
}

/// The minimum number of functions to generate. Defaults to 0. This
/// includes imported functions.
fn min_funcs(&self) -> usize {
Expand Down
8 changes: 8 additions & 0 deletions crates/wasm-smith/test.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(module
(import "env" "tbl" (table (;0;) 1 16 funcref))
(import "vars" "g" (global (;0;) (mut i32)))
(alias 0 "g" (global (;1;)))
(alias 0 "g" (global (;2;)))
(memory (;0;) 28083)
(global (;3;) (mut f64) (f64.const 0x1.586097aea0767p+157 (;=245755863760007340000000000000000000000000000000;)))
(global (;4;) (mut f64) (f64.const -0x1.b1d12363b1833p-495 (;=-0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016566039552511124;))))

0 comments on commit 0209fc7

Please sign in to comment.