Skip to content

Commit

Permalink
Merge pull request bytecodealliance#21 from dhil/typed-conts
Browse files Browse the repository at this point in the history
Rebase on func-ref-2 & add stubs to make `cargo check` succeed.
  • Loading branch information
dhil authored Sep 30, 2022
2 parents 1900f58 + 05172d9 commit a2bc2f0
Show file tree
Hide file tree
Showing 17 changed files with 152 additions and 65 deletions.
14 changes: 11 additions & 3 deletions crates/wasm-compose/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,22 @@ impl<'a> TypeEncoder<'a> {
wasmparser::ValType::F32 => ValType::F32,
wasmparser::ValType::F64 => ValType::F64,
wasmparser::ValType::V128 => ValType::V128,
wasmparser::ValType::FuncRef => ValType::FuncRef,
wasmparser::ValType::ExternRef => ValType::ExternRef,
wasmparser::ValType::Ref(ty) => Self::ref_type(ty),
wasmparser::ValType::Bot => unimplemented!(),
}
}

fn ref_type(ty: wasmparser::RefType) -> ValType {
match ty {
wasmparser::FUNC_REF => ValType::FuncRef,
wasmparser::EXTERN_REF => ValType::ExternRef,
_ => unimplemented!(),
}
}

fn table_type(ty: wasmparser::TableType) -> TableType {
TableType {
element_type: Self::val_type(ty.element_type),
element_type: Self::ref_type(ty.element_type),
minimum: ty.initial,
maximum: ty.maximum,
}
Expand Down
14 changes: 10 additions & 4 deletions crates/wasm-encoder/src/core/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,9 @@ pub enum Instruction<'a> {
BrOnNonNull(u32),
Return,
Call(u32),
CallRef,
CallRef(ValType),
CallIndirect { ty: u32, table: u32 },
ReturnCallRef,
ReturnCallRef(ValType),
Throw(u32),
Rethrow(u32),

Expand Down Expand Up @@ -916,13 +916,19 @@ impl Encode for Instruction<'_> {
sink.push(0x10);
f.encode(sink);
}
Instruction::CallRef => sink.push(0x14),
Instruction::CallRef(ty) => {
sink.push(0x14);
ty.encode(sink);
}
Instruction::CallIndirect { ty, table } => {
sink.push(0x11);
ty.encode(sink);
table.encode(sink);
}
Instruction::ReturnCallRef => sink.push(0x15),
Instruction::ReturnCallRef(ty) => {
sink.push(0x15);
ty.encode(sink);
}
Instruction::Delegate(l) => {
sink.push(0x18);
l.encode(sink);
Expand Down
27 changes: 23 additions & 4 deletions crates/wasm-mutate/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,18 @@ impl From<wasmparser::ValType> for PrimitiveTypeInfo {
wasmparser::ValType::F32 => PrimitiveTypeInfo::F32,
wasmparser::ValType::F64 => PrimitiveTypeInfo::F64,
wasmparser::ValType::V128 => PrimitiveTypeInfo::V128,
wasmparser::ValType::FuncRef => PrimitiveTypeInfo::FuncRef,
wasmparser::ValType::ExternRef => PrimitiveTypeInfo::ExternRef,
wasmparser::ValType::Ref(t) => t.into(),
wasmparser::ValType::Bot => unreachable!(),
}
}
}

impl From<wasmparser::RefType> for PrimitiveTypeInfo {
fn from(value: wasmparser::RefType) -> Self {
match value {
wasmparser::FUNC_REF => PrimitiveTypeInfo::FuncRef,
wasmparser::EXTERN_REF => PrimitiveTypeInfo::ExternRef,
_ => unimplemented!(),
}
}
}
Expand All @@ -57,6 +67,7 @@ impl TryFrom<wasmparser::Type> for TypeInfo {
.map(|&t| PrimitiveTypeInfo::from(t))
.collect(),
})),
wasmparser::Type::Cont(_) => unimplemented!(),
}
}
}
Expand All @@ -68,8 +79,16 @@ pub fn map_type(tpe: wasmparser::ValType) -> Result<ValType> {
wasmparser::ValType::F32 => Ok(ValType::F32),
wasmparser::ValType::F64 => Ok(ValType::F64),
wasmparser::ValType::V128 => Ok(ValType::V128),
wasmparser::ValType::FuncRef => Ok(ValType::FuncRef),
wasmparser::ValType::ExternRef => Ok(ValType::ExternRef),
wasmparser::ValType::Ref(t) => map_ref_type(t),
wasmparser::ValType::Bot => unimplemented!(),
}
}

pub fn map_ref_type(tpe: wasmparser::RefType) -> Result<ValType> {
match tpe {
wasmparser::FUNC_REF => Ok(ValType::FuncRef),
wasmparser::EXTERN_REF => Ok(ValType::ExternRef),
_ => unimplemented!(),
}
}

Expand Down
13 changes: 11 additions & 2 deletions crates/wasm-mutate/src/mutators/add_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ impl Mutator for AddTypeMutator {
.collect::<Result<Vec<_>, _>>()?;
types.function(params, results);
}
wasmparser::Type::Cont(_) => unimplemented!(),
}
}
// And then add our new type.
Expand All @@ -94,8 +95,16 @@ fn translate_type(ty: &wasmparser::ValType) -> Result<wasm_encoder::ValType> {
wasmparser::ValType::F32 => wasm_encoder::ValType::F32,
wasmparser::ValType::F64 => wasm_encoder::ValType::F64,
wasmparser::ValType::V128 => wasm_encoder::ValType::V128,
wasmparser::ValType::FuncRef => wasm_encoder::ValType::FuncRef,
wasmparser::ValType::ExternRef => wasm_encoder::ValType::ExternRef,
wasmparser::ValType::Ref(rt) => translate_ref_type(rt)?,
wasmparser::ValType::Bot => unreachable!(),
})
}

fn translate_ref_type(rt: &wasmparser::RefType) -> Result<wasm_encoder::ValType> {
Ok(match *rt {
wasmparser::FUNC_REF => wasm_encoder::ValType::FuncRef,
wasmparser::EXTERN_REF => wasm_encoder::ValType::ExternRef,
_ => unimplemented!(),
})
}

Expand Down
6 changes: 4 additions & 2 deletions crates/wasm-mutate/src/mutators/modify_const_exprs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ impl<'cfg, 'wasm> Translator for InitTranslator<'cfg, 'wasm> {
} else {
f64::from_bits(self.config.rng().gen())
}),
T::FuncRef => CE::ref_null(wasm_encoder::ValType::FuncRef),
T::ExternRef => CE::ref_null(wasm_encoder::ValType::ExternRef),
T::Ref(wasmparser::FUNC_REF) => CE::ref_null(wasm_encoder::ValType::FuncRef),
T::Ref(wasmparser::EXTERN_REF) => CE::ref_null(wasm_encoder::ValType::ExternRef),
T::Ref(_) => unimplemented!(),
T::Bot => unreachable!(),
}
} else {
// FIXME: implement non-reducing mutations for constant expressions.
Expand Down
4 changes: 2 additions & 2 deletions crates/wasm-mutate/src/mutators/peephole/dfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,12 +762,12 @@ impl<'a> DFGBuilder {
}

Operator::RefNull {
ty: wasmparser::ValType::ExternRef,
ty: wasmparser::HeapType::Extern,
} => {
self.push_node(Lang::RefNull(RefType::Extern), idx);
}
Operator::RefNull {
ty: wasmparser::ValType::FuncRef,
ty: wasmparser::HeapType::Func,
} => {
self.push_node(Lang::RefNull(RefType::Func), idx);
}
Expand Down
44 changes: 30 additions & 14 deletions crates/wasm-mutate/src/mutators/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ pub trait Translator {
ty(self.as_obj(), t)
}

fn translate_refty(&mut self, t: &wasmparser::RefType) -> Result<ValType> {
refty(self.as_obj(), t)
}

fn translate_heapty(&mut self, t: &wasmparser::HeapType) -> Result<ValType> {
heapty(self.as_obj(), t)
}

fn translate_global(&mut self, g: Global, s: &mut GlobalSection) -> Result<()> {
global(self.as_obj(), g, s)
}
Expand Down Expand Up @@ -130,6 +138,7 @@ pub fn type_def(t: &mut dyn Translator, ty: Type, s: &mut TypeSection) -> Result
);
Ok(())
}
Type::Cont(_) => unimplemented!(),
}
}

Expand All @@ -138,7 +147,7 @@ pub fn table_type(
ty: &wasmparser::TableType,
) -> Result<wasm_encoder::TableType> {
Ok(wasm_encoder::TableType {
element_type: t.translate_ty(&ty.element_type)?,
element_type: t.translate_refty(&ty.element_type)?,
minimum: ty.initial,
maximum: ty.maximum,
})
Expand Down Expand Up @@ -174,14 +183,18 @@ pub fn tag_type(t: &mut dyn Translator, ty: &wasmparser::TagType) -> Result<wasm
}

pub fn ty(_t: &mut dyn Translator, ty: &wasmparser::ValType) -> Result<ValType> {
crate::module::map_type(*ty)
}

pub fn refty(_t: &mut dyn Translator, ty: &wasmparser::RefType) -> Result<ValType> {
crate::module::map_ref_type(*ty)
}

pub fn heapty(_t: &mut dyn Translator, ty: &wasmparser::HeapType) -> Result<ValType> {
match ty {
wasmparser::ValType::I32 => Ok(ValType::I32),
wasmparser::ValType::I64 => Ok(ValType::I64),
wasmparser::ValType::F32 => Ok(ValType::F32),
wasmparser::ValType::F64 => Ok(ValType::F64),
wasmparser::ValType::V128 => Ok(ValType::V128),
wasmparser::ValType::FuncRef => Ok(ValType::FuncRef),
wasmparser::ValType::ExternRef => Ok(ValType::ExternRef),
wasmparser::HeapType::Func => Ok(ValType::FuncRef),
wasmparser::HeapType::Extern => Ok(ValType::ExternRef),
_ => unimplemented!(),
}
}

Expand All @@ -208,7 +221,7 @@ pub fn const_expr(
match op {
Operator::RefFunc { .. }
| Operator::RefNull {
ty: wasmparser::ValType::FuncRef,
ty: wasmparser::HeapType::Func,
..
}
| Operator::GlobalGet { .. } => {}
Expand Down Expand Up @@ -247,7 +260,7 @@ pub fn element(
ElementKind::Passive => ElementMode::Passive,
ElementKind::Declared => ElementMode::Declared,
};
let element_type = t.translate_ty(&element.ty)?;
let element_type = t.translate_refty(&element.ty)?;
let mut functions = Vec::new();
let mut exprs = Vec::new();
let mut reader = element.items.get_items_reader()?;
Expand All @@ -259,7 +272,7 @@ pub fn element(
ElementItem::Expr(expr) => {
exprs.push(t.translate_const_expr(
&expr,
&element.ty,
&wasmparser::ValType::Ref(element.ty),
ConstExprKind::ElementFunction,
)?);
}
Expand Down Expand Up @@ -312,7 +325,7 @@ pub fn op(t: &mut dyn Translator, op: &Operator<'_>) -> Result<Instruction<'stat

O::Return => I::Return,
O::Call { function_index } => I::Call(t.remap(Item::Function, *function_index)?),
O::CallRef => I::CallRef,
O::CallRef { ty } => I::CallRef(t.translate_heapty(ty)?),
O::CallIndirect {
index,
table_index,
Expand All @@ -321,7 +334,7 @@ pub fn op(t: &mut dyn Translator, op: &Operator<'_>) -> Result<Instruction<'stat
ty: t.remap(Item::Type, *index)?,
table: t.remap(Item::Table, *table_index)?,
},
O::ReturnCallRef => I::ReturnCallRef,
O::ReturnCallRef { ty } => I::ReturnCallRef(t.translate_heapty(ty)?),
O::Delegate { relative_depth } => I::Delegate(*relative_depth),
O::CatchAll => I::CatchAll,
O::Drop => I::Drop,
Expand Down Expand Up @@ -367,7 +380,7 @@ pub fn op(t: &mut dyn Translator, op: &Operator<'_>) -> Result<Instruction<'stat
O::F32Const { value } => I::F32Const(f32::from_bits(value.bits())),
O::F64Const { value } => I::F64Const(f64::from_bits(value.bits())),

O::RefNull { ty } => I::RefNull(t.translate_ty(ty)?),
O::RefNull { ty } => I::RefNull(t.translate_heapty(ty)?),
O::RefIsNull => I::RefIsNull,
O::RefFunc { function_index } => I::RefFunc(t.remap(Item::Function, *function_index)?),
O::RefAsNonNull => I::RefAsNonNull,
Expand Down Expand Up @@ -933,6 +946,9 @@ pub fn op(t: &mut dyn Translator, op: &Operator<'_>) -> Result<Instruction<'stat
| O::ReturnCall { .. }
| O::ReturnCallIndirect { .. }
| O::AtomicFence { .. } => return Err(Error::no_mutations_applicable()),

// Typed continuations
| O::ContNew { .. } | O::ContBind { .. } | O::Suspend { .. } | O::Resume { .. } | O::ResumeThrow { .. } | O::Barrier { .. } => unimplemented!(),
})
}

Expand Down
2 changes: 2 additions & 0 deletions crates/wasm-shrink/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ impl ShrinkRun {
saturating_float_to_int: true,
sign_extension: true,
component_model: false,
function_references: false,
typed_continuations: false,

// We'll never enable this here.
deterministic_only: false,
Expand Down
4 changes: 2 additions & 2 deletions crates/wasm-smith/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1799,9 +1799,9 @@ fn inverse_scalar_canonical_abi_for(
.cloned(),
ValType::F32 => Ok(ComponentValType::Primitive(PrimitiveValType::Float32)),
ValType::F64 => Ok(ComponentValType::Primitive(PrimitiveValType::Float64)),
ValType::V128 | ValType::FuncRef | ValType::ExternRef => {
ValType::V128 | ValType::FuncRef | ValType::ExternRef | ValType::Ref(_) => {
unreachable!("not used in canonical ABI")
}
},
};

let mut param_names = HashSet::default();
Expand Down
15 changes: 13 additions & 2 deletions crates/wasm-smith/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ impl Module {
new_types.push(Type::Func(Rc::clone(&func_type)));
new_index
}
Some((wasmparser::Type::Cont(_), _)) => unimplemented!(),
};
match &new_types[serialized_sig_idx - first_type_index] {
Type::Func(f) => Some((serialized_sig_idx as u32, Rc::clone(f))),
Expand Down Expand Up @@ -648,7 +649,7 @@ impl Module {

wasmparser::TypeRef::Table(table_ty) => {
let table_ty = TableType {
element_type: convert_type(table_ty.element_type),
element_type: convert_reftype(table_ty.element_type),
minimum: table_ty.initial,
maximum: table_ty.maximum,
};
Expand Down Expand Up @@ -880,7 +881,8 @@ impl Module {
} else {
ConstExpr::ref_null(ValType::FuncRef)
}
}
},
ValType::Ref(_) => unimplemented!(),
}))
}));

Expand Down Expand Up @@ -1567,6 +1569,15 @@ fn convert_type(parsed_type: wasmparser::ValType) -> ValType {
}
}

/// Convert a wasmparser's `ValType` to a `wasm_encoder::ValType`.
fn convert_reftype(parsed_type: wasmparser::RefType) -> ValType {
match parsed_type {
wasmparser::FUNC_REF => ValType::FuncRef,
wasmparser::EXTERN_REF => ValType::ExternRef,
_ => unimplemented!(),
}
}

impl EntityType {
fn size(&self) -> u32 {
match self {
Expand Down
2 changes: 2 additions & 0 deletions crates/wasm-smith/src/core/code_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,7 @@ fn arbitrary_val(ty: ValType, u: &mut Unstructured<'_>) -> Instruction {
ValType::V128 => Instruction::V128Const(u.arbitrary().unwrap_or(0)),
ValType::ExternRef => Instruction::RefNull(ValType::ExternRef),
ValType::FuncRef => Instruction::RefNull(ValType::FuncRef),
ValType::Ref(_) => unimplemented!(),
}
}

Expand Down Expand Up @@ -1562,6 +1563,7 @@ fn select(_: &mut Unstructured, _: &Module, builder: &mut CodeBuilder) -> Result
}
Some(ValType::I32) | Some(ValType::I64) | Some(ValType::F32) | Some(ValType::F64)
| Some(ValType::V128) | None => Ok(Instruction::Select),
Some(ValType::Ref(_)) => unimplemented!(),
}
}

Expand Down
3 changes: 2 additions & 1 deletion crates/wasm-smith/src/core/notrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ fn dummy_value_inst<'a>(ty: ValType) -> Instruction<'a> {
ValType::F64 => Instruction::F64Const(0.0),
ValType::V128 => Instruction::V128Const(0),
ValType::FuncRef | ValType::ExternRef => Instruction::RefNull(ty),
ValType::Ref(_) => unimplemented!(),
}
}

Expand Down Expand Up @@ -808,6 +809,6 @@ fn size_of_type_in_memory(ty: ValType) -> u64 {
ValType::F32 => 4,
ValType::F64 => 8,
ValType::V128 => 16,
ValType::FuncRef | ValType::ExternRef => panic!("not a memory type"),
ValType::Ref(_) | ValType::FuncRef | ValType::ExternRef => panic!("not a memory type"),
}
}
8 changes: 6 additions & 2 deletions crates/wasmparser/src/binary_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1689,8 +1689,12 @@ impl<'a> BinaryReader<'a> {
index: self.read_var_u32()?,
table_index: self.read_var_u32()?,
},
0x14 => Operator::CallRef,
0x15 => Operator::ReturnCallRef,
0x14 => Operator::CallRef {
ty: self.read_heap_type()?,
},
0x15 => Operator::ReturnCallRef {
ty: self.read_heap_type()?,
},
0x18 => Operator::Delegate {
relative_depth: self.read_var_u32()?,
},
Expand Down
Loading

0 comments on commit a2bc2f0

Please sign in to comment.