Skip to content

Commit

Permalink
Merge with upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
dhil committed Mar 8, 2024
2 parents f633e32 + 4361597 commit 542b487
Show file tree
Hide file tree
Showing 278 changed files with 1,735 additions and 1,810 deletions.
34 changes: 25 additions & 9 deletions crates/wasm-smith/src/core/code_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1617,13 +1617,24 @@ fn try_table(
let i = i as u32;

let label_types = ctrl.label_types();

// Empty labels are candidates for a `catch_all` since nothing is
// pushed in that case.
if label_types.is_empty() {
catch_options.push(Box::new(move |_, _| Ok(Catch::All { label: i })));
}

// Labels with just an `externref` are suitable for `catch_all_refs`,
// which first pushes nothing since there's no tag and then pushes
// the caught exception value.
if label_types == [ValType::EXNREF] {
catch_options.push(Box::new(move |_, _| Ok(Catch::AllRef { label: i })));
}

// If there is a tag which exactly matches the types of the label we're
// looking at then that tag can be used as part of a `catch` branch.
// That tag's parameters, which are the except values, are pushed
// for the label.
if builder.allocs.tags.contains_key(label_types) {
let label_types = label_types.to_vec();
catch_options.push(Box::new(move |u, builder| {
Expand All @@ -1634,15 +1645,20 @@ fn try_table(
}));
}

let mut label_types_with_exnref = label_types.to_vec();
label_types_with_exnref.push(ValType::EXNREF);
if builder.allocs.tags.contains_key(&label_types_with_exnref) {
catch_options.push(Box::new(move |u, builder| {
Ok(Catch::OneRef {
tag: *u.choose(&builder.allocs.tags[&label_types_with_exnref])?,
label: i,
})
}));
// And finally the last type of catch label, `catch_ref`. If the label
// ends with `exnref`, then use everything except the last `exnref` to
// see if there's a matching tag. If so then `catch_ref` can be used
// with that tag when branching to this label.
if let Some((&ValType::EXNREF, rest)) = label_types.split_last() {
if builder.allocs.tags.contains_key(rest) {
let rest = rest.to_vec();
catch_options.push(Box::new(move |u, builder| {
Ok(Catch::OneRef {
tag: *u.choose(&builder.allocs.tags[&rest])?,
label: i,
})
}));
}
}
}

Expand Down
14 changes: 8 additions & 6 deletions crates/wasmparser/src/validator/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1473,18 +1473,20 @@ where
Catch::OneRef { tag, label } => {
let tag = self.tag_at(tag)?;
let (ty, kind) = self.jump(label)?;
let params = tag.params().iter().copied();
let types = self.label_types(ty, kind)?;
if params.len() + 1 != types.len() {
let tag_params = tag.params().iter().copied();
let label_types = self.label_types(ty, kind)?;
if tag_params.len() + 1 != label_types.len() {
bail!(
self.offset,
"type mismatch: catch_ref label must have one \
more type than tag types",
);
}
for (expected, actual) in types.zip(params.chain([ValType::EXNREF])) {
self.push_operand(actual)?;
self.pop_operand(Some(expected))?;
for (expected_label_tyep, actual_tag_param) in
label_types.zip(tag_params.chain([ValType::EXNREF]))
{
self.push_operand(actual_tag_param)?;
self.pop_operand(Some(expected_label_tyep))?;
}
}

Expand Down
54 changes: 27 additions & 27 deletions crates/wasmprinter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,13 +779,13 @@ impl Printer {
}
CompositeType::Array(ty) => {
self.start_group("array");
let r = self.print_array_type(ty)?;
let r = self.print_array_type(state, ty)?;
self.end_group(); // `array`
r
}
CompositeType::Struct(ty) => {
self.start_group("struct");
let r = self.print_struct_type(ty)?;
let r = self.print_struct_type(state, ty)?;
self.end_group(); // `struct`
r
}
Expand Down Expand Up @@ -864,15 +864,15 @@ impl Printer {
// a new one if that's the case with a named parameter.
for (i, param) in ty.params().iter().enumerate() {
params.start_local(names_for.unwrap_or(u32::MAX), i as u32, self, state);
self.print_valtype(*param)?;
self.print_valtype(state, *param)?;
params.end_local(&mut self.result);
}
params.finish(&mut self.result);
if !ty.results().is_empty() {
self.result.push_str(" (result");
for result in ty.results().iter() {
self.result.push(' ');
self.print_valtype(*result)?;
self.print_valtype(state, *result)?;
}
self.result.push(')');
}
Expand All @@ -885,26 +885,26 @@ impl Printer {
Ok(0)
}

fn print_field_type(&mut self, ty: &FieldType) -> Result<u32> {
fn print_field_type(&mut self, state: &State, ty: &FieldType) -> Result<u32> {
self.result.push(' ');
if ty.mutable {
self.result.push_str("(mut ");
}
self.print_storage_type(ty.element_type)?;
self.print_storage_type(state, ty.element_type)?;
if ty.mutable {
self.result.push_str(")");
}
Ok(0)
}

fn print_array_type(&mut self, ty: &ArrayType) -> Result<u32> {
self.print_field_type(&ty.0)
fn print_array_type(&mut self, state: &State, ty: &ArrayType) -> Result<u32> {
self.print_field_type(state, &ty.0)
}

fn print_struct_type(&mut self, ty: &StructType) -> Result<u32> {
fn print_struct_type(&mut self, state: &State, ty: &StructType) -> Result<u32> {
for field in ty.fields.iter() {
self.result.push_str(" (field");
self.print_field_type(field)?;
self.print_field_type(state, field)?;
self.result.push(')');
}
Ok(0)
Expand All @@ -922,28 +922,28 @@ impl Printer {
Ok(0)
}

fn print_storage_type(&mut self, ty: StorageType) -> Result<()> {
fn print_storage_type(&mut self, state: &State, ty: StorageType) -> Result<()> {
match ty {
StorageType::I8 => self.result.push_str("i8"),
StorageType::I16 => self.result.push_str("i16"),
StorageType::Val(val_type) => self.print_valtype(val_type)?,
StorageType::Val(val_type) => self.print_valtype(state, val_type)?,
}
Ok(())
}

fn print_valtype(&mut self, ty: ValType) -> Result<()> {
fn print_valtype(&mut self, state: &State, ty: ValType) -> Result<()> {
match ty {
ValType::I32 => self.result.push_str("i32"),
ValType::I64 => self.result.push_str("i64"),
ValType::F32 => self.result.push_str("f32"),
ValType::F64 => self.result.push_str("f64"),
ValType::V128 => self.result.push_str("v128"),
ValType::Ref(rt) => self.print_reftype(rt)?,
ValType::Ref(rt) => self.print_reftype(state, rt)?,
}
Ok(())
}

fn print_reftype(&mut self, ty: RefType) -> Result<()> {
fn print_reftype(&mut self, state: &State, ty: RefType) -> Result<()> {
if ty.is_nullable() {
match ty.as_non_null() {
RefType::FUNC => self.result.push_str("funcref"),
Expand All @@ -959,19 +959,19 @@ impl Printer {
RefType::EXN => self.result.push_str("exnref"),
_ => {
self.result.push_str("(ref null ");
self.print_heaptype(ty.heap_type())?;
self.print_heaptype(state, ty.heap_type())?;
self.result.push_str(")");
}
}
} else {
self.result.push_str("(ref ");
self.print_heaptype(ty.heap_type())?;
self.print_heaptype(state, ty.heap_type())?;
self.result.push_str(")");
}
Ok(())
}

fn print_heaptype(&mut self, ty: HeapType) -> Result<()> {
fn print_heaptype(&mut self, state: &State, ty: HeapType) -> Result<()> {
match ty {
HeapType::Func => self.result.push_str("func"),
HeapType::Extern => self.result.push_str("extern"),
Expand All @@ -986,9 +986,9 @@ impl Printer {
HeapType::Cont => self.result.push_str("cont"),
HeapType::NoCont => self.result.push_str("nocont"),
HeapType::Exn => self.result.push_str("exn"),
HeapType::Concrete(i) => self
.result
.push_str(&format!("{}", i.as_module_index().unwrap())),
HeapType::Concrete(i) => {
self.print_idx(&state.core.type_names, i.as_module_index().unwrap())?;
}
}
Ok(())
}
Expand Down Expand Up @@ -1047,7 +1047,7 @@ impl Printer {
}
self.print_limits(ty.initial, ty.maximum)?;
self.result.push(' ');
self.print_reftype(ty.element_type)?;
self.print_reftype(state, ty.element_type)?;
Ok(())
}

Expand Down Expand Up @@ -1096,10 +1096,10 @@ impl Printer {
}
if ty.mutable {
self.result.push_str("(mut ");
self.print_valtype(ty.content_type)?;
self.print_valtype(state, ty.content_type)?;
self.result.push(')');
} else {
self.print_valtype(ty.content_type)?;
self.print_valtype(state, ty.content_type)?;
}
Ok(())
}
Expand Down Expand Up @@ -1231,7 +1231,7 @@ impl Printer {
first = false;
}
locals.start_local(func_idx, params + local_idx, self, state);
self.print_valtype(ty)?;
self.print_valtype(state, ty)?;
locals.end_local(&mut self.result);
local_idx += 1;
}
Expand Down Expand Up @@ -1492,7 +1492,7 @@ impl Printer {
}
}
ElementItems::Expressions(ty, reader) => {
self.print_reftype(ty)?;
self.print_reftype(state, ty)?;
for expr in reader {
self.result.push(' ');
self.print_const_expr_sugar(state, &expr?, "item")?
Expand Down Expand Up @@ -1966,7 +1966,7 @@ impl Printer {
self.result.push_str(" ");
self.start_group("resource");
self.result.push_str(" (rep ");
self.print_valtype(rep)?;
self.print_valtype(states.last().unwrap(), rep)?;
self.result.push_str(")");
if let Some(dtor) = dtor {
self.result.push_str(" (dtor (func ");
Expand Down
18 changes: 9 additions & 9 deletions crates/wasmprinter/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl<'a, 'b> PrintOperator<'a, 'b> {
BlockType::Empty => {}
BlockType::Type(t) => {
self.push_str(" (result ");
self.printer.print_valtype(t)?;
self.printer.print_valtype(self.state, t)?;
self.push_str(")");
}
BlockType::FuncType(idx) => {
Expand Down Expand Up @@ -257,11 +257,11 @@ impl<'a, 'b> PrintOperator<'a, 'b> {
}

fn from_ref_type(&mut self, ref_ty: RefType) -> Result<()> {
self.printer.print_reftype(ref_ty)
self.printer.print_reftype(self.state, ref_ty)
}

fn to_ref_type(&mut self, ref_ty: RefType) -> Result<()> {
self.printer.print_reftype(ref_ty)
self.printer.print_reftype(self.state, ref_ty)
}

fn data_index(&mut self, idx: u32) -> Result<()> {
Expand Down Expand Up @@ -436,12 +436,12 @@ macro_rules! define_visit {
);
(payload $self:ident TypedSelect $ty:ident) => (
$self.push_str(" (result ");
$self.printer.print_valtype($ty)?;
$self.printer.print_valtype($self.state, $ty)?;
$self.push_str(")")
);
(payload $self:ident RefNull $hty:ident) => (
$self.push_str(" ");
$self.printer.print_heaptype($hty)?;
$self.printer.print_heaptype($self.state, $hty)?;
);
(payload $self:ident TableInit $segment:ident $table:ident) => (
$self.push_str(" ");
Expand Down Expand Up @@ -549,25 +549,25 @@ macro_rules! define_visit {
$self.push_str(" ");
let rty = RefType::new(false, $hty)
.ok_or_else(|| anyhow!("implementation limit: type index too large"))?;
$self.printer.print_reftype(rty)?;
$self.printer.print_reftype($self.state, rty)?;
);
(payload $self:ident RefTestNullable $hty:ident) => (
$self.push_str(" ");
let rty = RefType::new(true, $hty)
.ok_or_else(|| anyhow!("implementation limit: type index too large"))?;
$self.printer.print_reftype(rty)?;
$self.printer.print_reftype($self.state, rty)?;
);
(payload $self:ident RefCastNonNull $hty:ident) => (
$self.push_str(" ");
let rty = RefType::new(false, $hty)
.ok_or_else(|| anyhow!("implementation limit: type index too large"))?;
$self.printer.print_reftype(rty)?;
$self.printer.print_reftype($self.state, rty)?;
);
(payload $self:ident RefCastNullable $hty:ident) => (
$self.push_str(" ");
let rty = RefType::new(true, $hty)
.ok_or_else(|| anyhow!("implementation limit: type index too large"))?;
$self.printer.print_reftype(rty)?;
$self.printer.print_reftype($self.state, rty)?;
);
(payload $self:ident $op:ident $($arg:ident)*) => (
$(
Expand Down
16 changes: 1 addition & 15 deletions crates/wast/src/core/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,19 +761,6 @@ impl Encode for BlockType<'_> {
}
}

impl Encode for FuncBindType<'_> {
fn encode(&self, e: &mut Vec<u8>) {
self.ty.encode(e);
}
}

impl Encode for LetType<'_> {
fn encode(&self, e: &mut Vec<u8>) {
self.block.encode(e);
self.locals.encode(e);
}
}

impl Encode for LaneArg {
fn encode(&self, e: &mut Vec<u8>) {
self.lane.encode(e);
Expand Down Expand Up @@ -1035,8 +1022,7 @@ fn find_names<'a>(
| Instruction::Loop(block)
| Instruction::Try(block)
| Instruction::Barrier(block)
| Instruction::TryTable(TryTable { block, .. })
| Instruction::Let(LetType { block, .. }) => {
| Instruction::TryTable(TryTable { block, .. }) => {
if let Some(name) = get_name(&block.label, &block.label_name) {
label_names.push((label_idx, name));
}
Expand Down
Loading

0 comments on commit 542b487

Please sign in to comment.