Skip to content

Commit

Permalink
update wasm encoder, printer etc. to 0.218 series
Browse files Browse the repository at this point in the history
  • Loading branch information
nagisa committed Nov 22, 2024
1 parent b41d2b5 commit 96bc8d6
Show file tree
Hide file tree
Showing 148 changed files with 8,083 additions and 6,401 deletions.
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "finite-wasm"
version = "0.5.0"
version = "0.5.1"
edition = "2021"
authors = ["Simonas Kazlauskas <finite-wasm@kazlauskas.me>"]
license = "MIT OR Apache-2.0"
Expand Down Expand Up @@ -34,13 +34,13 @@ lazy_static = { version = "1.4", optional = true }
libc = { version = "0.2.144", optional = true }
wasmparser = "0.105.0"
# Ensure that we depend on a single version of wasmparser only (patch versions bump wasmparser dep)
wasmprinter = "=0.2.57"
thiserror = "1"
num-traits = "0.2.15"
prefix-sum-vec = "0.1.2"
tempfile = { version = "3.5", optional = true }
wasm-encoder = { version = "0.27.0", optional = true }
wast = { version = "52", optional = true }
wasm-encoder = { version = "0.218.0", optional = true }
wasmprinter = { version = "0.218", optional = true }
wast = { version = "218", optional = true }

[dev-dependencies]
arbitrary = { version = "1.3", features = ["derive"] }
Expand All @@ -54,8 +54,9 @@ wasm-smith = "0.12"
wasmparser = "0.105.0"

[features]
defaut = ["instrument", "wast-tests"]
instrument = ["wasm-encoder"]
wast-tests = ["atoi", "instrument", "lazy_static", "libc", "tempfile", "wast"]
wast-tests = ["atoi", "instrument", "lazy_static", "libc", "tempfile", "wast", "wasmprinter"]

[package.metadata.docs.rs]
all-features = true
Expand Down
2 changes: 1 addition & 1 deletion deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ allow = [
"Apache-2.0",
"MIT",
"Apache-2.0 WITH LLVM-exception",
"Unicode-DFS-2016"
"Unicode-3.0"
]

[advisories]
Expand Down
53 changes: 33 additions & 20 deletions src/instrument.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{gas::InstrumentationKind, AnalysisOutcome};
use std::borrow::Cow;
use wasm_encoder::{self as we, Section};
use wasmparser as wp;

Expand Down Expand Up @@ -130,7 +131,7 @@ impl<'a> InstrumentContext<'a> {
let ty = ty.map_err(Error::ParseType)?;
match &ty {
wp::Type::Func(f) => {
self.type_section.function(
self.type_section.ty().function(
f.params().iter().copied().map(valtype),
f.results().iter().copied().map(valtype),
);
Expand Down Expand Up @@ -197,6 +198,7 @@ impl<'a> InstrumentContext<'a> {
let global = global.map_err(Error::ParseGlobal)?;
self.global_section.global(
we::GlobalType {
shared: false,
val_type: valtype(global.ty.content_type),
mutable: global.ty.mutable,
},
Expand Down Expand Up @@ -316,6 +318,7 @@ impl<'a> InstrumentContext<'a> {
(_, results) => {
let new_block_type_idx = self.type_section.len();
self.type_section
.ty()
.function(std::iter::empty(), results.iter().copied().map(valtype));
we::BlockType::FunctionType(new_block_type_idx)
}
Expand Down Expand Up @@ -381,8 +384,9 @@ impl<'a> InstrumentContext<'a> {
let instrument_fn_ty = self.type_section.len();
// By adding the type at the end of the type section we guarantee that any other
// type references remain valid.
self.type_section.function([we::ValType::I64], []);
self.type_section.ty().function([we::ValType::I64], []);
self.type_section
.ty()
.function([we::ValType::I64, we::ValType::I64], []);
// By inserting the imports at the beginning of the import section we make the new
// function index mapping trivial (it is always just an increment by F)
Expand Down Expand Up @@ -440,17 +444,21 @@ impl<'a> InstrumentContext<'a> {
let import_ty = match import.ty {
wp::TypeRef::Func(i) => we::EntityType::Function(i),
wp::TypeRef::Table(t) => we::EntityType::Table(we::TableType {
shared: false,
table64: false,
element_type: reftype(t.element_type),
minimum: t.initial,
maximum: t.maximum,
minimum: t.initial.into(),
maximum: t.maximum.map(Into::into),
}),
wp::TypeRef::Memory(t) => we::EntityType::Memory(we::MemoryType {
minimum: t.initial,
maximum: t.maximum,
memory64: t.memory64,
shared: t.shared,
page_size_log2: None,
}),
wp::TypeRef::Global(t) => we::EntityType::Global(we::GlobalType {
shared: false,
val_type: valtype(t.content_type),
mutable: t.mutable,
}),
Expand Down Expand Up @@ -479,14 +487,14 @@ impl<'a> InstrumentContext<'a> {
.into_iter()
.map(|v| map_func(v.map_err(Error::ParseElementItem)?))
.collect::<Result<Vec<_>, _>>()?;
we::Elements::Functions(&functions)
we::Elements::Functions(Cow::Borrowed(&functions))
}
wp::ElementItems::Expressions(exprs) => {
expressions = exprs
.into_iter()
.map(|v| v.map_err(Error::ParseElementExpression).and_then(constexpr))
.collect::<Result<Vec<_>, _>>()?;
we::Elements::Expressions(&expressions)
we::Elements::Expressions(reftype(elem.ty), Cow::Borrowed(&expressions))
}
};
self.element_section.segment(we::ElementSegment {
Expand All @@ -504,7 +512,6 @@ impl<'a> InstrumentContext<'a> {
}
}
},
element_type: reftype(elem.ty),
elements: items,
});
}
Expand Down Expand Up @@ -547,21 +554,27 @@ fn valtype(wp: wp::ValType) -> we::ValType {
}

fn reftype(wp: wp::RefType) -> we::RefType {
let ty = match wp.heap_type() {
wp::HeapType::TypedFunc(idx) => {
return we::RefType {
nullable: wp.is_nullable(),
heap_type: we::HeapType::Concrete(idx),
}
}
wp::HeapType::Func => we::AbstractHeapType::Func,
wp::HeapType::Extern => we::AbstractHeapType::Extern,
wp::HeapType::Any => we::AbstractHeapType::Any,
wp::HeapType::None => we::AbstractHeapType::None,
wp::HeapType::NoExtern => we::AbstractHeapType::NoExtern,
wp::HeapType::NoFunc => we::AbstractHeapType::NoFunc,
wp::HeapType::Eq => we::AbstractHeapType::Eq,
wp::HeapType::Struct => we::AbstractHeapType::Struct,
wp::HeapType::Array => we::AbstractHeapType::Array,
wp::HeapType::I31 => we::AbstractHeapType::I31,
};
we::RefType {
nullable: wp.is_nullable(),
heap_type: match wp.heap_type() {
wp::HeapType::Func => we::HeapType::Func,
wp::HeapType::Extern => we::HeapType::Extern,
wp::HeapType::Any => we::HeapType::Any,
wp::HeapType::None => we::HeapType::None,
wp::HeapType::NoExtern => we::HeapType::NoExtern,
wp::HeapType::NoFunc => we::HeapType::NoFunc,
wp::HeapType::Eq => we::HeapType::Eq,
wp::HeapType::Struct => we::HeapType::Struct,
wp::HeapType::Array => we::HeapType::Array,
wp::HeapType::I31 => we::HeapType::I31,
wp::HeapType::TypedFunc(idx) => we::HeapType::TypedFunc(idx),
},
heap_type: we::HeapType::Abstract { shared: false, ty },
}
}

Expand Down
45 changes: 37 additions & 8 deletions src/wast_tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ impl<'a> TestContext {
.map(|(_, d)| d.span().offset().saturating_sub(1))
.unwrap_or(test_contents.len());
match directive {
wast::WastDirective::Wat(wast::QuoteWat::Wat(wast::Wat::Module(mut module))) => {
wast::WastDirective::Module(wast::QuoteWat::Wat(wast::Wat::Module(mut module))) => {
let id = module.id.map_or_else(
|| format!("[directive {directive_index}]"),
|id| format!("{id:?}"),
Expand All @@ -402,21 +402,29 @@ impl<'a> TestContext {
.encode()
.map_err(|e| Error::EncodeModule(e, id.clone()))?;
let instrumented = self.instrument_module(&id, &module)?;
let print = wasmprinter::print_bytes(&instrumented).expect("print");
output_wast.push_str(&print);
let mut printer = ModulePrint(&mut output_wast);
wasmprinter::Config::new()
.print(&instrumented, &mut printer)
.expect("print");
output_wast.push_str("\n");
}
wast::WastDirective::Wat(wast::QuoteWat::QuoteModule(_, _)) => {
wast::WastDirective::Module(wast::QuoteWat::QuoteModule(_, _)) => {
unreachable!("doesn’t actually occur in our test suite");
}
wast::WastDirective::Wat(wast::QuoteWat::Wat(wast::Wat::Component(_))) => {
wast::WastDirective::Module(wast::QuoteWat::Wat(wast::Wat::Component(_))) => {
// These are difficult and I would rather skip them for now...
continue;
}
wast::WastDirective::Wat(wast::QuoteWat::QuoteComponent(_, _)) => {
wast::WastDirective::Module(wast::QuoteWat::QuoteComponent(_, _)) => {
// Same
continue;
}
wast::WastDirective::ModuleDefinition(_) => {
unreachable!("doesn’t actually occur in our test suite");
}
wast::WastDirective::ModuleInstance { .. } => {
unreachable!("doesn’t actually occur in our test suite");
}

// Ignore the “operations”, we only care about module analysis results.
wast::WastDirective::Register { .. } => {
Expand Down Expand Up @@ -454,8 +462,10 @@ impl<'a> TestContext {
.map_err(|e| Error::EncodeModule(e, id.clone()))?;
let instrumented = self.instrument_module(&id, &module)?;
output_wast.push_str("\n(assert_trap ");
let print = wasmprinter::print_bytes(&instrumented).expect("print");
output_wast.push_str(&print);
let mut printer = ModulePrint(&mut output_wast);
wasmprinter::Config::new()
.print(&instrumented, &mut printer)
.expect("print");
output_wast.push_str(" \"");
output_wast.push_str(message);
output_wast.push_str("\")\n");
Expand All @@ -479,6 +489,15 @@ impl<'a> TestContext {
wast::WastDirective::AssertMalformed { .. } => continue,
wast::WastDirective::AssertInvalid { .. } => continue,
wast::WastDirective::AssertUnlinkable { .. } => continue,
wast::WastDirective::AssertSuspension { .. } => {
unreachable!("doesn’t actually occur in our test suite");
}
wast::WastDirective::Thread(_) => {
unreachable!("doesn’t actually occur in our test suite");
}
wast::WastDirective::Wait { .. } => {
unreachable!("doesn’t actually occur in our test suite");
}
};
}
Ok(output_wast)
Expand Down Expand Up @@ -648,6 +667,16 @@ impl<'a> TestContext {
}
}

struct ModulePrint<'a>(&'a mut String);
impl<'a> wasmprinter::Print for ModulePrint<'a> {
fn write_str(&mut self, s: &str) -> io::Result<()> {
Ok(self.0.push_str(s))
}
fn print_custom_section(&mut self, _n: &str, _bo: usize, _d: &[u8]) -> io::Result<bool> {
Ok(true)
}
}

struct DefaultStackConfig;
impl max_stack::SizeConfig for DefaultStackConfig {
fn size_of_value(&self, ty: wasmparser::ValType) -> u8 {
Expand Down
4 changes: 2 additions & 2 deletions tests/local/regression-45-loop-instrumentation.wast
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
loop $l1
loop $l2
loop $l3
(i32.add (get_local $count) (i32.const 1))
(tee_local $count)
(i32.add (local.get $count) (i32.const 1))
(local.tee $count)
(br_table $l2 $l2 $l2 $l2 $l2 $l2 $l2 3)
end
end
Expand Down
Loading

0 comments on commit 96bc8d6

Please sign in to comment.