Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge with upstream #43

Merged
merged 5 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 75 additions & 14 deletions crates/wasm-encoder/src/core/code.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{encode_section, Encode, HeapType, Section, SectionId, ValType};
use crate::{encode_section, Encode, HeapType, RefType, Section, SectionId, ValType};
use std::borrow::Cow;

/// An encoder for the code section.
Expand Down Expand Up @@ -538,10 +538,22 @@ pub enum Instruction<'a> {
// GC types instructions.
StructNew(u32),
StructNewDefault(u32),
StructGet(u32, u32),
StructGetS(u32, u32),
StructGetU(u32, u32),
StructSet(u32, u32),
StructGet {
struct_type_index: u32,
field_index: u32,
},
StructGetS {
struct_type_index: u32,
field_index: u32,
},
StructGetU {
struct_type_index: u32,
field_index: u32,
},
StructSet {
struct_type_index: u32,
field_index: u32,
},

ArrayNew(u32),
ArrayNewDefault(u32),
Expand Down Expand Up @@ -575,11 +587,20 @@ pub enum Instruction<'a> {
array_type_index: u32,
array_elem_index: u32,
},

RefTestNonNull(HeapType),
RefTestNullable(HeapType),
RefCastNonNull(HeapType),
RefCastNullable(HeapType),
BrOnCast {
relative_depth: u32,
from_ref_type: RefType,
to_ref_type: RefType,
},
BrOnCastFail {
relative_depth: u32,
from_ref_type: RefType,
to_ref_type: RefType,
},
AnyConvertExtern,
ExternConvertAny,

Expand Down Expand Up @@ -1440,28 +1461,40 @@ impl Encode for Instruction<'_> {
sink.push(0x01);
type_index.encode(sink);
}
Instruction::StructGet(type_index, field_index) => {
Instruction::StructGet {
struct_type_index,
field_index,
} => {
sink.push(0xfb);
sink.push(0x02);
type_index.encode(sink);
struct_type_index.encode(sink);
field_index.encode(sink);
}
Instruction::StructGetS(type_index, field_index) => {
Instruction::StructGetS {
struct_type_index,
field_index,
} => {
sink.push(0xfb);
sink.push(0x03);
type_index.encode(sink);
struct_type_index.encode(sink);
field_index.encode(sink);
}
Instruction::StructGetU(type_index, field_index) => {
Instruction::StructGetU {
struct_type_index,
field_index,
} => {
sink.push(0xfb);
sink.push(0x04);
type_index.encode(sink);
struct_type_index.encode(sink);
field_index.encode(sink);
}
Instruction::StructSet(type_index, field_index) => {
Instruction::StructSet {
struct_type_index,
field_index,
} => {
sink.push(0xfb);
sink.push(0x05);
type_index.encode(sink);
struct_type_index.encode(sink);
field_index.encode(sink);
}
Instruction::ArrayNew(type_index) => {
Expand Down Expand Up @@ -1577,6 +1610,34 @@ impl Encode for Instruction<'_> {
sink.push(0x17);
heap_type.encode(sink);
}
Instruction::BrOnCast {
relative_depth,
from_ref_type,
to_ref_type,
} => {
sink.push(0xfb);
sink.push(0x18);
let cast_flags =
(from_ref_type.nullable as u8) | ((to_ref_type.nullable as u8) << 1);
relative_depth.encode(sink);
sink.push(cast_flags);
from_ref_type.heap_type.encode(sink);
to_ref_type.heap_type.encode(sink);
}
Instruction::BrOnCastFail {
relative_depth,
from_ref_type,
to_ref_type,
} => {
sink.push(0xfb);
sink.push(0x19);
let cast_flags =
(from_ref_type.nullable as u8) | ((to_ref_type.nullable as u8) << 1);
relative_depth.encode(sink);
sink.push(cast_flags);
from_ref_type.heap_type.encode(sink);
to_ref_type.heap_type.encode(sink);
}
Instruction::AnyConvertExtern => {
sink.push(0xfb);
sink.push(0x1a);
Expand Down
6 changes: 6 additions & 0 deletions crates/wasm-mutate/src/mutators/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@ pub fn op(t: &mut dyn Translator, op: &Operator<'_>) -> Result<Instruction<'stat
(map $arg:ident flags) => (());
(map $arg:ident ty) => (t.translate_ty($arg)?);
(map $arg:ident hty) => (t.translate_heapty($arg)?);
(map $arg:ident from_ref_type) => (t.translate_refty($arg)?);
(map $arg:ident to_ref_type) => (t.translate_refty($arg)?);
(map $arg:ident memarg) => (t.translate_memarg($arg)?);
(map $arg:ident local_index) => (*$arg);
(map $arg:ident value) => ($arg);
Expand All @@ -378,6 +380,9 @@ pub fn op(t: &mut dyn Translator, op: &Operator<'_>) -> Result<Instruction<'stat
.into()
);
(map $arg:ident array_size) => (*$arg);
(map $arg:ident field_index) => (*$arg);
(map $arg:ident from_type_nullable) => (*$arg);
(map $arg:ident to_type_nullable) => (*$arg);

// This case takes the arguments of a wasmparser instruction and creates
// a wasm-encoder instruction. There are a few special cases for where
Expand All @@ -404,6 +409,7 @@ pub fn op(t: &mut dyn Translator, op: &Operator<'_>) -> Result<Instruction<'stat
});
(build MemoryGrow $mem:ident $_:ident) => (I::MemoryGrow($mem));
(build MemorySize $mem:ident $_:ident) => (I::MemorySize($mem));
(build StructNew $type_index:ident) => (I::StructNew($type_index));
(build ArrayGet $type_index:ident) => (I::ArrayGet($type_index));
(build ArrayGetS $type_index:ident) => (I::ArrayGetS($type_index));
(build ArrayGetU $type_index:ident) => (I::ArrayGetU($type_index));
Expand Down
72 changes: 71 additions & 1 deletion crates/wasmparser/src/binary_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,7 @@ impl<'a> BinaryReader<'a> {
0xd0 => visitor.visit_ref_null(self.read()?),
0xd1 => visitor.visit_ref_is_null(),
0xd2 => visitor.visit_ref_func(self.read_var_u32()?),
0xd3 => visitor.visit_ref_eq(),
0xd4 => visitor.visit_ref_as_non_null(),
0xd5 => visitor.visit_br_on_null(self.read_var_u32()?),
0xd6 => visitor.visit_br_on_non_null(self.read_var_u32()?),
Expand Down Expand Up @@ -1027,11 +1028,34 @@ impl<'a> BinaryReader<'a> {
{
let code = self.read_var_u32()?;
Ok(match code {
0x0 => {
let type_index = self.read_var_u32()?;
visitor.visit_struct_new(type_index)
}
0x01 => {
let type_index = self.read_var_u32()?;
visitor.visit_struct_new_default(type_index)
}

0x02 => {
let type_index = self.read_var_u32()?;
let field_index = self.read_var_u32()?;
visitor.visit_struct_get(type_index, field_index)
}
0x03 => {
let type_index = self.read_var_u32()?;
let field_index = self.read_var_u32()?;
visitor.visit_struct_get_s(type_index, field_index)
}
0x04 => {
let type_index = self.read_var_u32()?;
let field_index = self.read_var_u32()?;
visitor.visit_struct_get_u(type_index, field_index)
}
0x05 => {
let type_index = self.read_var_u32()?;
let field_index = self.read_var_u32()?;
visitor.visit_struct_set(type_index, field_index)
}
0x06 => {
let type_index = self.read_var_u32()?;
visitor.visit_array_new(type_index)
Expand Down Expand Up @@ -1095,6 +1119,52 @@ impl<'a> BinaryReader<'a> {
0x15 => visitor.visit_ref_test_nullable(self.read()?),
0x16 => visitor.visit_ref_cast_non_null(self.read()?),
0x17 => visitor.visit_ref_cast_nullable(self.read()?),
0x18 => {
let pos = self.original_position();
let cast_flags = self.read_u8()?;
let relative_depth = self.read_var_u32()?;
let (from_type_nullable, to_type_nullable) = match cast_flags {
0b00 => (false, false),
0b01 => (true, false),
0b10 => (false, true),
0b11 => (true, true),
_ => bail!(pos, "invalid cast flags: {cast_flags:08b}"),
};
let from_heap_type = self.read()?;
let from_ref_type =
RefType::new(from_type_nullable, from_heap_type).ok_or_else(|| {
format_err!(pos, "implementation error: type index too large")
})?;
let to_heap_type = self.read()?;
let to_ref_type =
RefType::new(to_type_nullable, to_heap_type).ok_or_else(|| {
format_err!(pos, "implementation error: type index too large")
})?;
visitor.visit_br_on_cast(relative_depth, from_ref_type, to_ref_type)
}
0x19 => {
let pos = self.original_position();
let cast_flags = self.read_u8()?;
let relative_depth = self.read_var_u32()?;
let (from_type_nullable, to_type_nullable) = match cast_flags {
0 => (false, false),
1 => (true, false),
2 => (false, true),
3 => (true, true),
_ => bail!(pos, "invalid cast flags: {cast_flags:08b}"),
};
let from_heap_type = self.read()?;
let from_ref_type =
RefType::new(from_type_nullable, from_heap_type).ok_or_else(|| {
format_err!(pos, "implementation error: type index too large")
})?;
let to_heap_type = self.read()?;
let to_ref_type =
RefType::new(to_type_nullable, to_heap_type).ok_or_else(|| {
format_err!(pos, "implementation error: type index too large")
})?;
visitor.visit_br_on_cast_fail(relative_depth, from_ref_type, to_ref_type)
}

0x1a => visitor.visit_any_convert_extern(),
0x1b => visitor.visit_extern_convert_any(),
Expand Down
16 changes: 16 additions & 0 deletions crates/wasmparser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ macro_rules! for_each_operator {
@reference_types RefNull { hty: $crate::HeapType } => visit_ref_null
@reference_types RefIsNull => visit_ref_is_null
@reference_types RefFunc { function_index: u32 } => visit_ref_func
@gc RefEq => visit_ref_eq
@mvp I32Eqz => visit_i32_eqz
@mvp I32Eq => visit_i32_eq
@mvp I32Ne => visit_i32_ne
Expand Down Expand Up @@ -316,7 +317,12 @@ macro_rules! for_each_operator {
// 0xFB prefixed operators
// Garbage Collection
// http://github.com/WebAssembly/gc
@gc StructNew { struct_type_index: u32 } => visit_struct_new
@gc StructNewDefault { struct_type_index: u32 } => visit_struct_new_default
@gc StructGet { struct_type_index: u32, field_index: u32 } => visit_struct_get
@gc StructGetS { struct_type_index: u32, field_index: u32 } => visit_struct_get_s
@gc StructGetU { struct_type_index: u32, field_index: u32 } => visit_struct_get_u
@gc StructSet { struct_type_index: u32, field_index: u32 } => visit_struct_set
@gc ArrayNew { array_type_index: u32 } => visit_array_new
@gc ArrayNewDefault { array_type_index: u32 } => visit_array_new_default
@gc ArrayNewFixed { array_type_index: u32, array_size: u32 } => visit_array_new_fixed
Expand All @@ -335,6 +341,16 @@ macro_rules! for_each_operator {
@gc RefTestNullable { hty: $crate::HeapType } => visit_ref_test_nullable
@gc RefCastNonNull { hty: $crate::HeapType } => visit_ref_cast_non_null
@gc RefCastNullable { hty: $crate::HeapType } => visit_ref_cast_nullable
@gc BrOnCast {
relative_depth: u32,
from_ref_type: $crate::RefType,
to_ref_type: $crate::RefType
} => visit_br_on_cast
@gc BrOnCastFail {
relative_depth: u32,
from_ref_type: $crate::RefType,
to_ref_type: $crate::RefType
} => visit_br_on_cast_fail
@gc AnyConvertExtern => visit_any_convert_extern
@gc ExternConvertAny => visit_extern_convert_any
@gc RefI31 => visit_ref_i31
Expand Down
2 changes: 2 additions & 0 deletions crates/wasmparser/src/readers/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod functions;
mod globals;
mod imports;
mod init;
mod linking;
mod memories;
mod names;
mod operators;
Expand All @@ -28,6 +29,7 @@ pub use self::functions::*;
pub use self::globals::*;
pub use self::imports::*;
pub use self::init::*;
pub use self::linking::*;
pub use self::memories::*;
pub use self::names::*;
pub use self::operators::*;
Expand Down
15 changes: 0 additions & 15 deletions crates/wasmparser/src/readers/core/dylink0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,6 @@ const WASM_DYLINK_NEEDED: u8 = 2;
const WASM_DYLINK_EXPORT_INFO: u8 = 3;
const WASM_DYLINK_IMPORT_INFO: u8 = 4;

#[allow(missing_docs)]
pub const WASM_SYM_BINDING_WEAK: u32 = 1 << 0;
#[allow(missing_docs)]
pub const WASM_SYM_BINDING_LOCAL: u32 = 1 << 1;
#[allow(missing_docs)]
pub const WASM_SYM_VISIBILITY_HIDDEN: u32 = 1 << 2;
#[allow(missing_docs)]
pub const WASM_SYM_UNDEFINED: u32 = 1 << 4;
#[allow(missing_docs)]
pub const WASM_SYM_EXPORTED: u32 = 1 << 5;
#[allow(missing_docs)]
pub const WASM_SYM_EXPLICIT_NAME: u32 = 1 << 6;
#[allow(missing_docs)]
pub const WASM_SYM_NO_STRIP: u32 = 1 << 7;

/// Represents a `WASM_DYLINK_MEM_INFO` field
#[derive(Debug, Copy, Clone)]
pub struct MemInfo {
Expand Down
Loading