Add shared WebAssembly inspection and validation reports (#2044)

* wasm: Craft Anything

Port Craft Anything f64ae3bf3c8863a982af1304e06de20653803968.

* wasm: Compare WebAssembly exported tag parameter types (R0.5) (#441)

Port Craft Anything fa4aae2e79198ece843a876512172bfb10d902e3.

* wasm: Count all local declaration groups in a WebAssembly function (R3.12) (#1125)

Port Craft Anything ad4d57d4ae5993e157a280b4237a0aa8a7a7fdb7.

* wasm: Read WebAssembly debug URLs as length-prefixed UTF-8 names (R3.12) (#1126)

Port Craft Anything 07a5f8686f63bff6d0efe19af56bf129e0fee9f7.

* wasm: Compare WebAssembly body counts with all declared functions (R3.12) (#1129)

Port Craft Anything 58402ab226ce1a417d250e789dce9a0613158567.

* wasm: Bound WebAssembly metadata across repeated custom sections (R3.12) (#1390)

Port Craft Anything 37ec5557f07eff39050b0bd448a4a52d155d130e.

* wasm: Bound WebAssembly strings expanded through type and name references (R3.12) (#1540)

Port Craft Anything ada52c6cce7979c386460e80e69c95cf4b82dbda.

* wasm: Report top-level WebAssembly component sections (R3.12)

Port Craft Anything 7d60ac9f896fc91cb623bd1666c4424e6ce944f9.

* wasm: retain component_layout regression tests

Source crates/formats/tests/wasm_component_layout.rs at 7d60ac9f896fc91cb623bd1666c4424e6ce944f9; imports adapted only.

* wasm: retain custom_inventory_budget regression tests

Source crates/formats/tests/wasm_custom_inventory_budget.rs at 37ec5557f07eff39050b0bd448a4a52d155d130e; imports adapted only.

* wasm: retain reference_bytes regression tests

Source crates/formats/tests/wasm_reference_bytes.rs at ada52c6cce7979c386460e80e69c95cf4b82dbda; imports adapted only.

* Expose shared WebAssembly inspection and retained hostile-input tests

* Split WASM section inspection and reuse regression encoders

---------

Co-authored-by: Brandon Thomas <echelon@gmail.com>
Co-authored-by: Afloat <132809511+Afloat16@users.noreply.github.com>
Co-authored-by: Afloat <ShuffleMonkey@163.com>
Co-authored-by: RRFZ <247457381+Plastic-time@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-11-17 14:13:51 -05:00
1 parent dcda149a8a
commit 5039e49ab6
10 files changed
+1518 -1

No files matched your search

+4
View File
@@ -141,6 +141,10 @@ Sanitization, animation fitting and numeric output policy remain caller-owned.
`crates/jsonc` is extracted from Craft Anything `crates/formats/src/jsonc.rs` at `e5c9c94106747ed44b9a3fb0decce867c2285f93` (MIT OR Apache-2.0). The original implementation and hardening by BFlat, newline preservation by NetterSilver, and subsequent RRFZ/Afloat correctness fixes retain separate primary-author port commits with immutable source revisions. Shared API wiring and optional input-byte admission are subsequent changes. Compatibility parsing and all existing formatting/editing tests are retained; application file I/O and policy are not imported.
## WebAssembly inspection
`crates/wasm` extracts Craft Anything `crates/formats/src/wasm.rs` at `5979f9b13f16dcf1df3c7ae7f8e7735f452866f2`, with its checked byte-reader subset and original synthetic WASM/testkit fixtures (MIT OR Apache-2.0). Eight core-history ports preserve Brandon Thomas, Afloat and RRFZ as primary authors; component, repeated-custom-section and referenced-string regression imports retain source authors and commit references. Fixtures come from `crates/testkit/src/formats.rs` and deterministic mutation helpers from `crates/testkit/src/lib.rs`; no third-party binaries. Shared error naming and workspace wiring are subsequent changes. Existing inspection limits and report behavior are unchanged; this crate validates/inspects/renders, never executes modules.
## Packet capture and protocol inspection
`crates/pcap` extracts Craft Anything `crates/formats/src/pcap.rs`, its checked byte reader primitives and synthetic packet fixtures/regressions at `5979f9b13f16dcf1df3c7ae7f8e7735f452866f2` (MIT OR Apache-2.0). Brandon Thomas introduced the implementation, byte reader and fixtures in `f64ae3bf3c8863a982af1304e06de20653803968`. Subsequent fixes by phatloi0010, GGWA, BFlat, Afloat, NetterSilver, Max Mittelstädt and RRFZ retain separate primary-author source-history commits with immutable source revisions. Shared error/API wiring and chrono-based timestamp formatting with exhaustive day-boundary parity tests are subsequent Dylan M. Taylor changes. No packet reassembly, decryption, live capture or app analysis policy is imported.
Generated
+33 -1
View File
@@ -3366,6 +3366,16 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-wasm"
version = "0.1.0"
dependencies = [
"serde",
"thiserror 2.0.21",
"wasmparser 0.261.0",
"wasmprinter",
]
[[package]]
name = "craft-web-api"
version = "0.1.0"
@@ -12049,7 +12059,7 @@ dependencies = [
"wasmi_collections",
"wasmi_core",
"wasmi_ir",
"wasmparser",
"wasmparser 0.228.0",
]
[[package]]
@@ -12089,6 +12099,28 @@ dependencies = [
"indexmap",
]
[[package]]
name = "wasmparser"
version = "0.261.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f20f20e44f7e8aeb6744823ea9d869ede51e51be4fdaedede2852282e54d2d8"
dependencies = [
"bitflags 2.13.2",
"indexmap",
"semver",
]
[[package]]
name = "wasmprinter"
version = "0.261.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86a21c9e3325655ba02de3e645bc8357a8b3651a2e3ada25b6dbc37560deb9b7"
dependencies = [
"anyhow",
"termcolor",
"wasmparser 0.261.0",
]
[[package]]
name = "wayland-backend"
version = "0.3.17"
+1
View File
@@ -29,6 +29,7 @@ acadrust = "=0.6.6"
log = "0.4"
# internal: add each crate here as `craft-<name> = { path = "crates/<name>" }`
craft-plist = { path = "crates/plist" }
craft-wasm = { path = "crates/wasm" }
craft-platform = { path = "crates/platform" }
craft-aep = { path = "crates/aep" }
craft-affinity = { path = "crates/affinity" }
+15
View File
@@ -0,0 +1,15 @@
[package]
name = "craft-wasm"
description = "Bounded WebAssembly module/component inspection, validation reports and WAT rendering; never executes code"
version.workspace = true
edition.workspace = true
license.workspace = true
rust-version.workspace = true
repository.workspace = true
[lints]
workspace = true
[dependencies]
serde = { workspace = true }
thiserror = { workspace = true }
wasmparser = { version = "0.261", default-features = false, features = ["std", "validate", "features", "component-model", "simd"] }
wasmprinter = { version = "0.261", default-features = false, features = ["component-model"] }
+32
View File
@@ -0,0 +1,32 @@
// Checked reader subset retained from Craft Anything; MIT OR Apache-2.0.
pub(crate) struct Reader<'a> {
data: &'a [u8],
}
impl<'a> Reader<'a> {
pub fn new(data: &'a [u8]) -> Self {
Self { data }
}
pub fn u8(&self, off: usize) -> Option<u8> {
self.data.get(off).copied()
}
pub fn slice(&self, off: usize, len: usize) -> Option<&'a [u8]> {
self.data.get(off..off.checked_add(len)?)
}
/// Unsigned LEB128 at `off`: (value, bytes consumed). At most 10 bytes.
pub fn uleb128(&self, off: usize) -> Option<(u64, usize)> {
let mut result = 0u64;
for i in 0..10usize {
let b = self.u8(off.checked_add(i)?)?;
let payload = u64::from(b & 0x7f);
let shift = 7 * i as u32;
if shift > 0 && payload.checked_shl(shift)? >> shift != payload {
return None;
}
result |= payload << shift;
if b & 0x80 == 0 {
return Some((result, i + 1));
}
}
None
}
}
+973
View File
@@ -0,0 +1,973 @@
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
#![forbid(unsafe_code)]
//! WebAssembly modules: validation, sections, imports, exports, functions with their signatures
//! and names, memories, tables, globals, tags, custom sections (names, producers, target features,
//! source map URL) and a text (WAT) rendering. Built on the pure-Rust `wasmparser` and
//! `wasmprinter` crates; never executes anything.
use crate::bytes::Reader;
use WasmError as FormatError;
use serde::Serialize;
use std::collections::{BTreeMap, btree_map::Entry};
use wasmparser::{CompositeInnerType, Encoding, ExternalKind, FuncType, KnownCustom, Name, Parser, Payload, TypeRef, Validator, WasmFeatures};
/// Largest module we read (256 MiB).
pub const MAX_INPUT: usize = 256 << 20;
/// Most entries kept per list.
pub const MAX_ITEMS: usize = 100_000;
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct WasmSection {
pub id: u8,
pub name: String,
pub offset: u64,
pub size: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct WasmImport {
pub module: String,
pub name: String,
/// func, table, memory, global or tag.
pub kind: &'static str,
pub type_index: Option<u32>,
/// Function signature or a short description of the imported item.
pub signature: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct WasmExport {
pub name: String,
pub kind: &'static str,
pub index: u32,
/// Name of the exported function from the name section, when known.
pub target_name: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct WasmFunction {
/// Index in the function index space (imports first).
pub index: u32,
pub type_index: u32,
pub signature: String,
pub body_offset: u64,
pub body_size: u64,
pub locals: u64,
pub name: Option<String>,
pub exported_as: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct WasmMemory {
pub index: u32,
pub initial: u64,
pub maximum: Option<u64>,
pub shared: bool,
pub memory64: bool,
pub imported: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct WasmTable {
pub index: u32,
pub element_type: String,
pub initial: u64,
pub maximum: Option<u64>,
pub imported: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct WasmGlobal {
pub index: u32,
pub value_type: String,
pub mutable: bool,
pub imported: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct WasmTag {
/// Index in the tag index space (imports first).
pub index: u32,
pub type_index: u32,
pub signature: String,
pub imported: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct WasmCustom {
pub name: String,
pub offset: u64,
pub size: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ProducerValue {
pub name: String,
pub version: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ProducerField {
pub field: String,
pub values: Vec<ProducerValue>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct WasmValidation {
pub valid: bool,
pub error: Option<String>,
pub offset: Option<u64>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct WasmReport {
/// `module` or `component`.
pub encoding: &'static str,
pub version: u16,
pub size: usize,
pub validation: WasmValidation,
/// Set when the structure could not be read to the end.
pub parse_error: Option<String>,
pub sections: Vec<WasmSection>,
pub types: usize,
pub imports: Vec<WasmImport>,
pub imported_functions: u32,
pub exports: Vec<WasmExport>,
/// Functions defined in the module (imports are listed under `imports`).
pub functions: Vec<WasmFunction>,
pub memories: Vec<WasmMemory>,
pub tables: Vec<WasmTable>,
pub globals: Vec<WasmGlobal>,
pub tags: Vec<WasmTag>,
pub start_function: Option<u32>,
pub data_segments: u32,
pub element_segments: u32,
pub data_count: Option<u32>,
pub custom_sections: Vec<WasmCustom>,
pub module_name: Option<String>,
pub producers: Vec<ProducerField>,
pub target_features: Vec<String>,
pub source_mapping_url: Option<String>,
pub external_debug_info: Option<String>,
pub truncated: bool,
pub warnings: Vec<String>,
}
/// True when the bytes start with the WebAssembly magic.
pub fn is_wasm(data: &[u8]) -> bool {
data.starts_with(b"\0asm")
}
fn sig(f: &FuncType) -> String {
let p: Vec<String> = f.params().iter().map(|v| v.to_string()).collect();
let r: Vec<String> = f.results().iter().map(|v| v.to_string()).collect();
format!("({}) -> ({})", p.join(", "), r.join(", "))
}
struct ReferencedStrings {
remaining: usize,
exhausted: bool,
}
impl ReferencedStrings {
fn claim(&mut self, bytes: usize) -> bool {
if self.exhausted || bytes > self.remaining {
self.exhausted = true;
return false;
}
self.remaining -= bytes;
true
}
fn copy(&mut self, value: &str) -> Option<String> {
self.claim(value.len()).then(|| value.to_owned())
}
}
fn type_sig(types: &[Option<FuncType>], t: u32, cache: &mut BTreeMap<u32, String>, strings: &mut ReferencedStrings) -> Option<String> {
if strings.exhausted {
return None;
}
let signature = match cache.entry(t) {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => {
let value = match types.get(t as usize) {
Some(Some(f)) => sig(f),
Some(None) => format!("type {t} (not a function type)"),
None => format!("type {t} (missing)"),
};
if !strings.claim(value.len()) {
return None;
}
entry.insert(value)
}
};
strings.copy(signature)
}
fn section_name(id: u8) -> &'static str {
match id {
0 => "custom",
1 => "type",
2 => "import",
3 => "function",
4 => "table",
5 => "memory",
6 => "global",
7 => "export",
8 => "start",
9 => "element",
10 => "code",
11 => "data",
12 => "datacount",
13 => "tag",
_ => "unknown",
}
}
/// Read the outer component layout without interpreting the contents of embedded binaries.
fn component_sections(reader: &mut wasmparser::BinaryReader<'_>, rep: &mut WasmReport) -> Result<(), wasmparser::BinaryReaderError> {
while !reader.eof() {
if rep.sections.len() == MAX_ITEMS {
rep.truncated = true;
break;
}
let id = reader.read_u8()?;
let body = reader.read_reader()?;
let range = body.range();
let name = match id {
0 => format!("custom:{}", wasmparser::CustomSectionReader::new(body)?.name()),
1 => "core module".into(),
2 => "core instance".into(),
3 => "core type".into(),
4 => "component".into(),
5 => "instance".into(),
6 => "alias".into(),
7 => "type".into(),
8 => "canonical".into(),
9 => "start".into(),
10 => "import".into(),
11 => "export".into(),
_ => "unknown".into(),
};
rep.sections.push(WasmSection { id, name, offset: range.start, size: range.end.saturating_sub(range.start) });
}
Ok(())
}
fn kind_name(k: ExternalKind) -> &'static str {
match k {
ExternalKind::Func | ExternalKind::FuncExact => "func",
ExternalKind::Table => "table",
ExternalKind::Memory => "memory",
ExternalKind::Global => "global",
ExternalKind::Tag => "tag",
}
}
/// Validate with every feature enabled; the result never fails the caller.
pub fn validate(data: &[u8]) -> WasmValidation {
match Validator::new_with_features(WasmFeatures::all()).validate_all(data) {
Ok(_) => WasmValidation { valid: true, error: None, offset: None },
Err(e) => WasmValidation { valid: false, error: Some(e.message().to_string()), offset: Some(e.offset()) },
}
}
/// A length-prefixed UTF-8 string at the start of a custom section payload.
fn leading_string(data: &[u8]) -> Option<String> {
// inspect bounds the enclosing module by MAX_INPUT, including any inline source map.
wasmparser::BinaryReader::new(data, 0).read_unlimited_string().ok().map(str::to_owned)
}
fn target_features(data: &[u8]) -> Vec<String> {
let r = Reader::new(data);
let mut out = Vec::new();
let Some((n, mut p)) = r.uleb128(0) else { return out };
for _ in 0..n.min(1024) {
let Some(prefix) = r.u8(p) else { break };
let Some((len, used)) = r.uleb128(p + 1) else { break };
let Some(s) = usize::try_from(len).ok().and_then(|l| r.slice(p + 1 + used, l)) else { break };
out.push(format!("{}{}", char::from(prefix), String::from_utf8_lossy(s)));
p = p + 1 + used + s.len();
}
out
}
fn cap(len: usize, truncated: &mut bool) -> bool {
if len >= MAX_ITEMS {
*truncated = true;
false
} else {
true
}
}
fn read_types(
r: wasmparser::TypeSectionReader<'_>,
types: &mut Vec<Option<FuncType>>,
rep: &mut WasmReport,
) -> Result<(), wasmparser::BinaryReaderError> {
for rg in r {
for st in rg?.into_types() {
if types.len() >= MAX_ITEMS {
rep.truncated = true;
break;
}
types.push(match st.composite_type.inner {
CompositeInnerType::Func(f) => Some(f),
_ => None,
});
}
}
rep.types = types.len();
Ok(())
}
fn read_custom(c: wasmparser::CustomSectionReader<'_>, rep: &mut WasmReport, fn_names: &mut BTreeMap<u32, String>, producer_values: &mut usize) {
if cap(rep.custom_sections.len(), &mut rep.truncated) {
let range = c.range();
rep.custom_sections.push(WasmCustom { name: c.name().to_string(), offset: range.start, size: range.end.saturating_sub(range.start) });
}
match c.name() {
"sourceMappingURL" => rep.source_mapping_url = leading_string(c.data()),
"external_debug_info" => rep.external_debug_info = leading_string(c.data()),
"target_features" => rep.target_features = target_features(c.data()),
_ => {}
}
match c.as_known() {
KnownCustom::Name(names) => {
// A damaged name section is reported but does not stop the rest.
for (i, sub) in names.into_iter().take(65).enumerate() {
if i == 64 {
rep.truncated = true;
break;
}
let Ok(sub) = sub else {
rep.warnings.push("the name section is damaged".into());
break;
};
match sub {
Name::Module { name, .. } => rep.module_name = Some(name.to_string()),
Name::Function(map) => {
for (i, n) in map.into_iter().take(MAX_ITEMS + 1).enumerate() {
if i == MAX_ITEMS {
rep.truncated = true;
break;
}
match n {
Ok(n) => {
if let Some(name) = fn_names.get_mut(&n.index) {
// Repeated sections may replace an already retained name at the limit.
*name = n.name.to_string();
} else if cap(fn_names.len(), &mut rep.truncated) {
fn_names.insert(n.index, n.name.to_string());
}
}
Err(_) => {
rep.warnings.push("a function name entry is damaged".into());
break;
}
}
}
}
_ => {}
}
}
}
KnownCustom::Producers(p) => {
for (i, f) in p.into_iter().take(65).enumerate() {
if i == 64 || !cap(rep.producers.len(), &mut rep.truncated) {
rep.truncated = true;
break;
}
let Ok(f) = f else {
rep.warnings.push("the producers section is damaged".into());
break;
};
let mut values = Vec::new();
for (i, value) in f.values.into_iter().take(65).enumerate() {
if i == 64 || !cap(*producer_values, &mut rep.truncated) {
rep.truncated = true;
break;
}
if let Ok(value) = value {
values.push(ProducerValue { name: value.name.to_string(), version: value.version.to_string() });
*producer_values += 1;
}
}
rep.producers.push(ProducerField { field: f.name.to_string(), values });
}
}
_ => {}
}
}
fn read_code(
body: wasmparser::FunctionBody<'_>,
types: &[Option<FuncType>],
func_types: &[u32],
code_index: &mut u32,
rep: &mut WasmReport,
type_signatures: &mut BTreeMap<u32, String>,
strings: &mut ReferencedStrings,
) -> Result<(), wasmparser::BinaryReaderError> {
let ti = func_types.get(*code_index as usize).copied();
let mut locals = 0u64;
let lr = body.get_locals_reader()?;
for l in lr {
let (n, _) = l?;
locals = locals.saturating_add(u64::from(n));
}
let range = body.range();
if cap(rep.functions.len(), &mut rep.truncated) {
let signature = match ti {
Some(t) => type_sig(types, t, type_signatures, strings),
None => strings.copy("no matching function section entry"),
};
if let Some(signature) = signature {
rep.functions.push(WasmFunction {
index: rep.imported_functions.saturating_add(*code_index),
type_index: ti.unwrap_or(u32::MAX),
signature,
body_offset: range.start,
body_size: range.end.saturating_sub(range.start),
locals,
name: None,
exported_as: Vec::new(),
});
}
}
*code_index = code_index.saturating_add(1);
Ok(())
}
fn read_functions(
r: wasmparser::FunctionSectionReader<'_>,
func_types: &mut Vec<u32>,
declared_functions: &mut u32,
rep: &mut WasmReport,
) -> Result<(), wasmparser::BinaryReaderError> {
*declared_functions = r.count();
for t in r {
if func_types.len() >= MAX_ITEMS {
rep.truncated = true;
break;
}
func_types.push(t?);
}
Ok(())
}
/// Read a module. Fails only when the bytes are not WebAssembly at all or too large; structural
/// damage is reported in `parse_error` and `validation`.
pub fn inspect(data: &[u8]) -> Result<WasmReport, FormatError> {
if data.len() > MAX_INPUT {
return Err(FormatError::TooLarge(format!("{} bytes; the limit is {MAX_INPUT}", data.len())));
}
if !is_wasm(data) {
return Err(FormatError::Malformed("no WebAssembly magic (\\0asm)".into()));
}
let mut rep = WasmReport {
encoding: "module",
version: 0,
size: data.len(),
validation: validate(data),
parse_error: None,
sections: Vec::new(),
types: 0,
imports: Vec::new(),
imported_functions: 0,
exports: Vec::new(),
functions: Vec::new(),
memories: Vec::new(),
tables: Vec::new(),
globals: Vec::new(),
tags: Vec::new(),
start_function: None,
data_segments: 0,
element_segments: 0,
data_count: None,
custom_sections: Vec::new(),
module_name: None,
producers: Vec::new(),
target_features: Vec::new(),
source_mapping_url: None,
external_debug_info: None,
truncated: false,
warnings: Vec::new(),
};
let mut types: Vec<Option<FuncType>> = Vec::new();
let mut func_types: Vec<u32> = Vec::new();
let mut fn_names: BTreeMap<u32, String> = BTreeMap::new();
let mut producer_values = 0usize;
let mut imported_tables = 0u32;
let mut imported_memories = 0u32;
let mut imported_globals = 0u32;
let mut imported_tags = 0u32;
let mut declared_functions = 0;
let mut code_index = 0u32;
// Type and name references can expand a small module into much larger display strings.
// Charge both the signature cache and every retained copy before allocating that copy.
let reference_budget = (64 << 20).max(data.len().saturating_mul(3));
let mut strings = ReferencedStrings { remaining: reference_budget, exhausted: false };
let mut type_signatures = BTreeMap::new();
let result: Result<(), wasmparser::BinaryReaderError> = (|| {
for payload in Parser::new(0).parse_all(data) {
let payload = payload?;
if let Some((id, range)) = payload.as_section()
&& cap(rep.sections.len(), &mut rep.truncated)
{
let name = match &payload {
Payload::CustomSection(c) => format!("custom:{}", c.name()),
_ => section_name(id).to_string(),
};
rep.sections.push(WasmSection { id, name, offset: range.start, size: range.end.saturating_sub(range.start) });
}
match payload {
Payload::Version { num, encoding, range } => {
rep.version = num;
if encoding == Encoding::Component {
rep.encoding = "component";
rep.warnings.push("this is a component, not a core module; only validation and the top-level layout are reported".into());
if let Some(body) = usize::try_from(range.end).ok().and_then(|end| data.get(end..)) {
component_sections(&mut wasmparser::BinaryReader::new(body, range.end), &mut rep)?;
}
break;
}
}
Payload::TypeSection(r) => {
read_types(r, &mut types, &mut rep)?;
}
Payload::ImportSection(r) => {
for imp in r.into_imports() {
let imp = imp?;
let (kind, type_index, signature) = match imp.ty {
TypeRef::Func(t) | TypeRef::FuncExact(t) => {
rep.imported_functions = rep.imported_functions.saturating_add(1);
let signature =
if rep.imports.len() < MAX_ITEMS { type_sig(&types, t, &mut type_signatures, &mut strings) } else { None };
("func", Some(t), signature)
}
TypeRef::Table(t) => {
if cap(rep.tables.len(), &mut rep.truncated) {
rep.tables.push(WasmTable {
index: imported_tables,
element_type: format!("{:?}", t.element_type),
initial: t.initial,
maximum: t.maximum,
imported: true,
});
}
imported_tables = imported_tables.saturating_add(1);
("table", None, Some(format!("table {} min {}", format!("{:?}", t.element_type).to_lowercase(), t.initial)))
}
TypeRef::Memory(m) => {
if cap(rep.memories.len(), &mut rep.truncated) {
rep.memories.push(WasmMemory {
index: imported_memories,
initial: m.initial,
maximum: m.maximum,
shared: m.shared,
memory64: m.memory64,
imported: true,
});
}
imported_memories = imported_memories.saturating_add(1);
("memory", None, Some(format!("memory min {} pages", m.initial)))
}
TypeRef::Global(g) => {
if cap(rep.globals.len(), &mut rep.truncated) {
rep.globals.push(WasmGlobal {
index: imported_globals,
value_type: g.content_type.to_string(),
mutable: g.mutable,
imported: true,
});
}
imported_globals = imported_globals.saturating_add(1);
("global", None, Some(format!("{}{}", if g.mutable { "mut " } else { "" }, g.content_type)))
}
TypeRef::Tag(t) => {
let keep_tag = cap(rep.tags.len(), &mut rep.truncated);
let signature = if rep.imports.len() < MAX_ITEMS {
type_sig(&types, t.func_type_idx, &mut type_signatures, &mut strings)
} else {
None
};
if keep_tag {
let tag_signature = match &signature {
Some(value) => strings.copy(value),
None => type_sig(&types, t.func_type_idx, &mut type_signatures, &mut strings),
};
if let Some(signature) = tag_signature {
rep.tags.push(WasmTag { index: imported_tags, type_index: t.func_type_idx, signature, imported: true });
}
}
// The import list and tag list are capped independently.
imported_tags = imported_tags.saturating_add(1);
("tag", Some(t.func_type_idx), signature)
}
};
if cap(rep.imports.len(), &mut rep.truncated)
&& let Some(signature) = signature
{
rep.imports.push(WasmImport { module: imp.module.to_string(), name: imp.name.to_string(), kind, type_index, signature });
}
}
}
Payload::FunctionSection(r) => {
read_functions(r, &mut func_types, &mut declared_functions, &mut rep)?;
}
Payload::TableSection(r) => {
for (i, t) in r.into_iter().enumerate() {
let t = t?;
if !cap(rep.tables.len(), &mut rep.truncated) {
break;
}
rep.tables.push(WasmTable {
index: imported_tables.saturating_add(u32::try_from(i).unwrap_or(u32::MAX)),
element_type: format!("{:?}", t.ty.element_type),
initial: t.ty.initial,
maximum: t.ty.maximum,
imported: false,
});
}
}
Payload::MemorySection(r) => {
for (i, m) in r.into_iter().enumerate() {
let m = m?;
if !cap(rep.memories.len(), &mut rep.truncated) {
break;
}
rep.memories.push(WasmMemory {
index: imported_memories.saturating_add(u32::try_from(i).unwrap_or(u32::MAX)),
initial: m.initial,
maximum: m.maximum,
shared: m.shared,
memory64: m.memory64,
imported: false,
});
}
}
Payload::TagSection(r) => {
for (i, t) in r.into_iter().enumerate() {
let t = t?;
if !cap(rep.tags.len(), &mut rep.truncated) {
break;
}
if let Some(signature) = type_sig(&types, t.func_type_idx, &mut type_signatures, &mut strings) {
rep.tags.push(WasmTag {
index: imported_tags.saturating_add(u32::try_from(i).unwrap_or(u32::MAX)),
type_index: t.func_type_idx,
signature,
imported: false,
});
}
}
}
Payload::GlobalSection(r) => {
for (i, g) in r.into_iter().enumerate() {
let g = g?;
if !cap(rep.globals.len(), &mut rep.truncated) {
break;
}
rep.globals.push(WasmGlobal {
index: imported_globals.saturating_add(u32::try_from(i).unwrap_or(u32::MAX)),
value_type: g.ty.content_type.to_string(),
mutable: g.ty.mutable,
imported: false,
});
}
}
Payload::ExportSection(r) => {
for e in r {
let e = e?;
if !cap(rep.exports.len(), &mut rep.truncated) {
break;
}
rep.exports.push(WasmExport { name: e.name.to_string(), kind: kind_name(e.kind), index: e.index, target_name: None });
}
}
Payload::StartSection { func, .. } => rep.start_function = Some(func),
Payload::ElementSection(r) => rep.element_segments = r.count(),
Payload::DataCountSection { count, .. } => rep.data_count = Some(count),
Payload::DataSection(r) => rep.data_segments = r.count(),
Payload::CodeSectionEntry(body) => {
read_code(body, &types, &func_types, &mut code_index, &mut rep, &mut type_signatures, &mut strings)?;
}
Payload::CustomSection(c) => {
read_custom(c, &mut rep, &mut fn_names, &mut producer_values);
}
Payload::End(_) => {}
_ => {}
}
}
Ok(())
})();
if let Err(e) = result {
rep.parse_error = Some(format!("{} (at byte {})", e.message(), e.offset()));
}
// Attach names and exports to functions.
let mut exported: BTreeMap<u32, Vec<String>> = BTreeMap::new();
for e in rep.exports.iter_mut() {
if e.kind == "func" {
e.target_name = fn_names.get(&e.index).and_then(|name| strings.copy(name));
exported.entry(e.index).or_default().push(e.name.clone());
}
}
for f in rep.functions.iter_mut() {
f.name = fn_names.get(&f.index).and_then(|name| strings.copy(name));
f.exported_as = exported.remove(&f.index).unwrap_or_default();
}
if declared_functions != code_index && rep.parse_error.is_none() {
rep.warnings.push(format!("{declared_functions} function declarations but {code_index} bodies"));
}
if strings.exhausted {
rep.truncated = true;
rep.warnings
.push(format!("referenced type signatures and function names exceed the {reference_budget}-byte budget; some metadata is omitted"));
}
Ok(rep)
}
struct CappedText {
out: String,
cap: usize,
truncated: bool,
}
impl wasmprinter::Print for CappedText {
fn write_str(&mut self, s: &str) -> std::io::Result<()> {
if self.out.len().saturating_add(s.len()) > self.cap {
let room = self.cap.saturating_sub(self.out.len());
let mut end = room.min(s.len());
while end > 0 && !s.is_char_boundary(end) {
end -= 1;
}
self.out.push_str(s.get(..end).unwrap_or(""));
self.truncated = true;
return Err(std::io::Error::other("output cap reached"));
}
self.out.push_str(s);
Ok(())
}
}
/// The text format of a module, at most `cap` bytes: (text, truncated). Only call this on
/// modules that passed [`validate`]; damaged modules are not rendered.
pub fn to_wat(data: &[u8], cap: usize) -> Result<(String, bool), String> {
let mut p = CappedText { out: String::new(), cap, truncated: false };
let mut cfg = wasmprinter::Config::new();
cfg.name_unnamed(false);
match cfg.print(data, &mut p) {
Ok(()) => Ok((p.out, false)),
Err(_) if p.truncated => Ok((p.out, true)),
Err(e) => Err(e.to_string()),
}
}
/// What a JavaScript glue file says about the module.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct GlueScan {
/// String literals ending in `.wasm`.
pub wasm_references: Vec<String>,
/// Imports (`module.name`) whose module and field names both appear in the glue.
pub matched_imports: Vec<String>,
pub mentions_instantiate: bool,
}
/// Scan glue JavaScript for `.wasm` literals and the import names the module expects.
pub fn scan_glue(text: &str, imports: &[WasmImport]) -> GlueScan {
let mut refs = Vec::new();
let b = text.as_bytes();
let mut i = 0usize;
while let Some(p) = text.get(i..).and_then(|t| t.find(".wasm")) {
let at = i + p;
let end = at + 5;
if let Some(&q) = b.get(end)
&& matches!(q, b'"' | b'\'' | b'`')
{
let start = b.get(..at).and_then(|h| h.iter().rposition(|&c| c == q)).map(|s| s + 1);
if let Some(s) = start
&& at - s <= 256
&& let Some(lit) = text.get(s..end)
&& !lit.contains(['\n', ' '])
&& !refs.iter().any(|r: &String| r == lit)
&& refs.len() < 256
{
refs.push(lit.to_string());
}
}
i = end;
}
let matched = imports
.iter()
.filter(|im| text.contains(im.module.as_str()) && text.contains(im.name.as_str()))
.take(MAX_ITEMS)
.map(|im| format!("{}.{}", im.module, im.name))
.collect();
GlueScan { wasm_references: refs, matched_imports: matched, mentions_instantiate: text.contains("WebAssembly.instantiate") }
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_support::formats as fx;
/// The output inventory limit applies to retained functions, not the local groups within one retained function.
#[test]
fn local_counts_include_groups_after_ten_thousand() {
// One valid () -> () function containing 10,001 runs of one i32 local.
let mut bytes = b"\0asm\x01\0\0\0\x01\x04\x01\x60\0\0\x03\x02\x01\0".to_vec();
// section size = 1 + 3 + 20005, body size = 2 + 20002 + 1.
bytes.extend([10, 0xa9, 0x9c, 1, 1, 0xa5, 0x9c, 1, 0x91, 0x4e]);
for _ in 0..10_001 {
bytes.extend([1, 0x7f]);
}
bytes.push(0x0b);
let report = inspect(&bytes).unwrap();
assert!(report.validation.valid, "{:?}", report.validation);
assert_eq!(report.functions[0].locals, 10_001);
}
#[test]
fn debug_links_need_valid_wasm_names() {
assert_eq!(leading_string(b"\x04test").as_deref(), Some("test"));
assert!(leading_string(&[1, 0xff]).is_none());
assert!(leading_string(b"\x81\x80\x80\x80\x80\0x").is_none());
assert!(leading_string(b"\x04abc").is_none());
}
#[test]
fn inspect_preserves_long_source_mapping_urls() {
let url = format!("data:application/json;base64,{}", "A".repeat(100_000));
let report = inspect(&fx::wasm_module_with_source_mapping_url(&url)).unwrap();
assert_eq!(report.source_mapping_url.as_deref(), Some(url.as_str()));
}
#[test]
fn large_function_tables_do_not_report_missing_declarations() {
fn leb(mut value: usize) -> Vec<u8> {
let mut out = Vec::new();
while value >= 128 {
out.push((value as u8 & 0x7f) | 0x80);
value >>= 7;
}
out.push(value as u8);
out
}
let count = MAX_ITEMS + 1;
let mut bytes = b"\0asm\x01\0\0\0\x01\x04\x01\x60\0\0".to_vec();
let mut declarations = leb(count);
declarations.resize(declarations.len() + count, 0);
let mut bodies = leb(count);
for _ in 0..count {
bodies.extend([2, 0, 0x0b]);
}
for (id, section) in [(3, declarations), (10, bodies)] {
bytes.push(id);
bytes.extend(leb(section.len()));
bytes.extend(section);
}
let report = inspect(&bytes).unwrap();
assert!(report.validation.valid && report.truncated);
assert_eq!(report.functions.len(), MAX_ITEMS);
assert!(!report.warnings.iter().any(|s| s.contains("function declarations")), "{:?}", report.warnings);
}
#[test]
fn reads_the_sample_module() {
let m = fx::wasm_module();
let r = inspect(&m).unwrap();
assert!(r.validation.valid, "{:?}", r.validation);
assert!(r.parse_error.is_none());
assert_eq!(r.types, 3);
assert_eq!(r.imports.len(), 1);
assert_eq!(r.imports[0].signature, "(i32) -> ()");
assert_eq!(r.imported_functions, 1);
assert_eq!(r.functions.len(), 2);
assert_eq!(r.functions[0].name.as_deref(), Some("add"));
assert_eq!(r.functions[0].signature, "(i32, i32) -> (i32)");
assert_eq!(r.functions[0].exported_as, vec!["add"]);
assert_eq!(r.exports.len(), 3);
assert_eq!(r.memories.len(), 1);
assert_eq!(r.globals.len(), 1);
assert!(r.globals[0].mutable);
assert_eq!(r.data_segments, 1);
assert_eq!(r.module_name.as_deref(), Some("demo"));
assert_eq!(r.producers.len(), 2);
assert_eq!(r.producers[1].values[0].name, "rustc");
assert_eq!(r.source_mapping_url.as_deref(), Some("demo.wasm.map"));
assert_eq!(r.target_features, vec!["+mutable-globals"]);
let (wat, trunc) = to_wat(&m, 1 << 20).unwrap();
assert!(!trunc);
assert!(wat.contains("(module $demo"), "{wat}");
assert!(wat.contains("i32.add"));
let (short, trunc) = to_wat(&m, 40).unwrap();
assert!(trunc && short.len() <= 40);
let g = scan_glue(fx::WASM_GLUE, &r.imports);
assert_eq!(g.wasm_references, vec!["./demo.wasm"]);
assert_eq!(g.matched_imports, vec!["env.log"]);
assert!(g.mentions_instantiate);
}
#[test]
fn damaged_modules_are_reported_not_fatal() {
let m = fx::wasm_module();
let mut bad = m.clone();
let len = bad.len();
bad.truncate(len - 10);
let r = inspect(&bad).unwrap();
assert!(!r.validation.valid);
assert!(r.parse_error.is_some());
assert!(inspect(b"not wasm").is_err());
}
#[test]
fn hostile_inputs_do_not_panic() {
let m = fx::wasm_module();
for x in crate::test_support::mutations(&m) {
if let Ok(r) = inspect(&x)
&& r.validation.valid
{
let _ = to_wat(&x, 4096);
}
}
for n in 0..m.len() {
let _ = inspect(&m[..n]);
}
for seed in 0..100 {
let mut n = crate::test_support::noise(seed, 256);
n[..8].copy_from_slice(b"\0asm\x01\0\0\0");
let _ = inspect(&n);
let _ = scan_glue(&String::from_utf8_lossy(&n), &[]);
}
}
}
mod bytes;
#[cfg(test)]
mod test_support;
/// Admission errors; structural damage stays in the inspection report.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum WasmError {
#[error("the file is malformed ({0})")]
Malformed(String),
#[error("too large: {0}")]
TooLarge(String),
}
#[cfg(test)]
#[path = "regressions/component_layout.rs"]
mod component_layout;
#[cfg(test)]
#[path = "regressions/custom_inventory_budget.rs"]
mod custom_inventory_budget;
#[cfg(test)]
#[path = "regressions/reference_bytes.rs"]
mod reference_bytes;
@@ -0,0 +1,42 @@
use crate::{MAX_ITEMS, inspect};
const COMPONENT: &[u8] = b"\0asm\x0d\0\x01\0";
/// Only outer sections appear, including sections after embedded binaries.
#[test]
fn lists_component_sections_without_descending_into_embedded_modules() {
let mut bytes = COMPONENT.to_vec();
bytes.extend(b"\x00\x04\x03one");
bytes.extend(b"\x01\x0b\0asm\x01\0\0\0\x01\x01\0");
bytes.extend(b"\x04\x0e\0asm\x0d\0\x01\0\x00\x04\x03two");
bytes.extend(b"\x00\x06\x05three");
let report = inspect(&bytes).unwrap();
assert!(report.validation.valid, "{:?}", report.validation);
let layout: Vec<_> = report.sections.iter().map(|s| (s.id, s.name.as_str(), s.offset, s.size)).collect();
assert_eq!(layout, [(0, "custom:one", 10, 4), (1, "core module", 16, 11), (4, "component", 29, 14), (0, "custom:three", 45, 6)]);
assert!(report.functions.is_empty());
assert!(report.parse_error.is_none());
}
/// A short section reports damage without losing the preceding layout.
#[test]
fn truncated_component_keeps_its_complete_prefix() {
let mut bytes = COMPONENT.to_vec();
bytes.extend(b"\x00\x04\x03one\x01\x08\0asm");
let report = inspect(&bytes).unwrap();
assert_eq!(report.sections.len(), 1);
assert!(report.parse_error.is_some());
}
/// Component layouts use the same cap as core module layouts.
#[test]
fn component_section_limit_is_disclosed() {
let mut bytes = COMPONENT.to_vec();
for _ in 0..=MAX_ITEMS {
bytes.extend(b"\x00\x02\x01x");
}
let report = inspect(&bytes).unwrap();
assert!(report.validation.valid);
assert_eq!(report.sections.len(), MAX_ITEMS);
assert!(report.truncated);
}
@@ -0,0 +1,115 @@
use crate::test_support::formats::wasm_module;
use crate::{MAX_ITEMS, inspect};
use crate::test_support::{leb, string};
fn custom(module: &mut Vec<u8>, name: &str, data: &[u8]) {
let mut body = Vec::new();
string(&mut body, name);
body.extend(data);
module.push(0);
module.extend(leb(body.len()));
module.extend(body);
}
fn names(module: &mut Vec<u8>, entries: &[(usize, &str)]) {
let mut map = leb(entries.len());
for (index, name) in entries {
map.extend(leb(*index));
string(&mut map, name);
}
let mut body = vec![1];
body.extend(leb(map.len()));
body.extend(map);
custom(module, "name", &body);
}
fn producers(fields: usize, values: usize) -> Vec<u8> {
let mut body = leb(fields);
for _ in 0..fields {
string(&mut body, "language");
body.extend(leb(values));
for _ in 0..values {
string(&mut body, "fixture");
string(&mut body, "1");
}
}
body
}
#[test]
fn repeated_name_sections_share_a_limit_but_can_update_retained_functions() {
let mut bytes = b"\0asm\x01\0\0\0\x01\x04\x01\x60\0\0\x03\x02\x01\0\x0a\x04\x01\x02\0\x0b".to_vec();
let entries: Vec<_> = (0..MAX_ITEMS).map(|index| (index, "kept")).collect();
names(&mut bytes, &entries);
let complete = inspect(&bytes).unwrap();
assert!(complete.validation.valid);
assert!(!complete.truncated);
assert_eq!(complete.functions[0].name.as_deref(), Some("kept"));
names(&mut bytes, &[(0, "updated"), (MAX_ITEMS, "omitted")]);
let report = inspect(&bytes).unwrap();
assert!(report.validation.valid);
assert!(report.parse_error.is_none());
assert!(report.truncated);
assert_eq!(report.functions[0].name.as_deref(), Some("updated"));
}
#[test]
fn producer_values_are_bounded_across_repeated_sections() {
let mut bytes = b"\0asm\x01\0\0\0".to_vec();
let section = producers(1, 64);
for _ in 0..MAX_ITEMS / 64 + 1 {
custom(&mut bytes, "producers", &section);
}
let report = inspect(&bytes).unwrap();
assert!(report.validation.valid);
assert!(report.parse_error.is_none());
assert!(report.truncated);
assert_eq!(report.producers.iter().map(|field| field.values.len()).sum::<usize>(), MAX_ITEMS);
assert_eq!(report.producers[0].values[0].name, "fixture");
}
#[test]
fn empty_producer_fields_still_spend_the_global_field_limit() {
let mut bytes = b"\0asm\x01\0\0\0".to_vec();
let section = producers(64, 0);
for _ in 0..MAX_ITEMS / 64 + 1 {
custom(&mut bytes, "producers", &section);
}
let report = inspect(&bytes).unwrap();
assert!(report.validation.valid);
assert_eq!(report.producers.len(), MAX_ITEMS);
assert!(report.truncated);
}
#[test]
fn existing_per_section_limits_are_reported_as_truncation() {
for (fields, values) in [(65, 0), (1, 65)] {
let mut bytes = b"\0asm\x01\0\0\0".to_vec();
custom(&mut bytes, "producers", &producers(fields, values));
let report = inspect(&bytes).unwrap();
assert!(report.truncated, "{fields} fields with {values} values");
}
let mut bytes = b"\0asm\x01\0\0\0".to_vec();
let mut subsections = vec![0, 2, 1, b'm'];
// Name subsection IDs must increase, including unknown extension IDs.
for id in 20..84 {
subsections.extend([id, 0]);
}
custom(&mut bytes, "name", &subsections);
let report = inspect(&bytes).unwrap();
assert!(report.truncated);
assert_eq!(report.module_name.as_deref(), Some("m"));
}
#[test]
fn ordinary_names_and_producers_remain_complete() {
let report = inspect(&wasm_module()).unwrap();
assert!(report.validation.valid);
assert!(!report.truncated);
assert_eq!(report.functions[0].name.as_deref(), Some("add"));
assert_eq!(report.module_name.as_deref(), Some("demo"));
assert_eq!(report.producers.len(), 2);
assert_eq!(report.producers[1].values[0].name, "rustc");
assert_eq!(report.exports.iter().find(|export| export.name == "add").unwrap().target_name.as_deref(), Some("add"));
}
@@ -0,0 +1,140 @@
use crate::{WasmReport, inspect};
use crate::test_support::{leb, string};
fn section(module: &mut Vec<u8>, id: u8, body: &[u8]) {
module.push(id);
module.extend(leb(body.len()));
module.extend(body);
}
fn shared_type(functions: usize, imports: usize, imported_tags: usize, defined_tags: usize, params: usize) -> Vec<u8> {
let mut module = b"\0asm\x01\0\0\0".to_vec();
let mut ty = vec![1, 0x60];
ty.extend(leb(params));
ty.extend(std::iter::repeat_n(0x7f, params));
ty.push(0);
section(&mut module, 1, &ty);
if imports + imported_tags > 0 {
let mut body = leb(imports + imported_tags);
for i in 0..imports + imported_tags {
string(&mut body, "env");
string(&mut body, "item");
if i < imports {
body.extend([0, 0]);
} else {
body.extend([4, 0, 0]);
}
}
section(&mut module, 2, &body);
}
if functions > 0 {
let mut body = leb(functions);
body.extend(std::iter::repeat_n(0, functions));
section(&mut module, 3, &body);
}
if defined_tags > 0 {
let mut body = leb(defined_tags);
for _ in 0..defined_tags {
body.extend([0, 0]);
}
section(&mut module, 13, &body);
}
if functions > 0 {
let mut body = leb(functions);
for _ in 0..functions {
body.extend([2, 0, 0x0b]);
}
section(&mut module, 10, &body);
}
module
}
fn derived_bytes(report: &WasmReport) -> usize {
report.imports.iter().map(|item| item.signature.len()).sum::<usize>()
+ report.tags.iter().map(|item| item.signature.len()).sum::<usize>()
+ report.functions.iter().map(|item| item.signature.len() + item.name.as_ref().map_or(0, String::len)).sum::<usize>()
+ report.exports.iter().map(|item| item.target_name.as_ref().map_or(0, String::len)).sum::<usize>()
}
fn bounded(module: &[u8]) -> WasmReport {
assert!(module.len() < 1 << 20);
let report = inspect(module).unwrap();
assert!(report.parse_error.is_none());
assert!(derived_bytes(&report) <= 64 << 20, "{} input bytes retain {} derived string bytes", module.len(), derived_bytes(&report));
assert!(report.truncated);
assert!(report.warnings.iter().any(|warning| warning.contains("byte budget")), "{:?}", report.warnings);
report
}
#[test]
fn repeated_function_signatures_have_a_byte_budget() {
let report = bounded(&shared_type(16_384, 0, 0, 0, 1000));
assert!(report.validation.valid, "{:?}", report.validation);
assert!(!report.functions.is_empty());
assert!(report.functions.len() < 16_384);
assert!(
report.functions.iter().enumerate().all(|(index, function)| function.index as usize == index && function.signature.starts_with("(i32, i32"))
);
assert!(!report.warnings.iter().any(|warning| warning.contains("function declarations")));
}
#[test]
fn repeated_import_signatures_stay_bounded_even_when_validation_fails() {
let report = bounded(&shared_type(0, 16_384, 0, 0, 1000));
assert!(!report.validation.valid);
assert!(report.validation.error.as_deref().unwrap().contains("effective type size"));
assert_eq!(report.imported_functions, 16_384);
assert!(!report.imports.is_empty());
assert!(report.imports.len() < 16_384);
}
#[test]
fn imported_and_defined_tags_share_the_same_budget_as_functions() {
for (functions, imports, imported_tags, defined_tags) in [(0, 0, 8192, 0), (0, 0, 0, 16_384), (4096, 4096, 4096, 4096)] {
let report = bounded(&shared_type(functions, imports, imported_tags, defined_tags, 1000));
assert_eq!(report.imported_functions as usize, imports);
assert!(report.tags.len() < imported_tags + defined_tags);
assert!(report.tags.iter().all(|tag| tag.signature.starts_with("(i32, i32")));
assert!(report.tags.iter().filter(|tag| !tag.imported).all(|tag| tag.index as usize >= imported_tags));
}
}
#[test]
fn exported_function_names_cannot_multiply_a_small_name_section() {
let mut module = shared_type(0, 1, 0, 0, 0);
let name = "λ".repeat(32_768);
let mut exports = leb(1280);
for index in 0..1280 {
string(&mut exports, &format!("export_{index}"));
exports.extend([0, 0]);
}
section(&mut module, 7, &exports);
let mut map = vec![1, 0];
string(&mut map, &name);
let mut names = vec![4, b'n', b'a', b'm', b'e', 1];
names.extend(leb(map.len()));
names.extend(map);
section(&mut module, 0, &names);
let report = bounded(&module);
assert!(report.validation.valid, "{:?}", report.validation);
assert_eq!(report.exports.len(), 1280);
assert!(report.exports.iter().any(|export| export.target_name.is_none()));
assert!(report.exports.iter().filter_map(|export| export.target_name.as_deref()).all(|value| value == name));
}
#[test]
fn ordinary_shared_signatures_and_names_remain_complete() {
let report = inspect(&shared_type(2, 2, 2, 2, 3)).unwrap();
assert!(report.validation.valid, "{:?}", report.validation);
assert!(!report.truncated, "{:?}", report.warnings);
assert_eq!(report.imported_functions, 2);
assert_eq!(report.functions.len(), 2);
assert_eq!(report.tags.len(), 4);
assert!(report.imports.iter().all(|item| item.signature == "(i32, i32, i32) -> ()"));
assert!(report.tags.iter().all(|tag| tag.signature == "(i32, i32, i32) -> ()"));
assert_eq!(report.functions.iter().map(|function| function.index).collect::<Vec<_>>(), [2, 3]);
assert_eq!(report.tags.iter().map(|tag| tag.index).collect::<Vec<_>>(), [0, 1, 2, 3]);
assert!(report.tags[..2].iter().all(|tag| tag.imported));
assert!(report.tags[2..].iter().all(|tag| !tag.imported));
}
+163
View File
@@ -0,0 +1,163 @@
// Synthetic fixtures and mutations from Craft Anything testkit, same source revision.
/// Deterministic pseudo-random bytes (xorshift) for fuzz-like loops.
pub fn noise(seed: u64, len: usize) -> Vec<u8> {
let mut x = seed.wrapping_mul(0x9e37_79b9_7f4a_7c15) | 1;
(0..len)
.map(|_| {
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
x as u8
})
.collect()
}
/// Every way to damage a sample used by robustness tests: truncations at many lengths and
/// single-byte corruptions spread across the file.
pub fn mutations(sample: &[u8]) -> Vec<Vec<u8>> {
let mut out = Vec::new();
let n = sample.len();
let step = (n / 64).max(1);
let mut len = 0;
while len < n {
out.push(sample[..len].to_vec());
len += step;
}
for (i, seed) in (0..n).step_by(step).zip(1u64..) {
let mut m = sample.to_vec();
let noise = noise(seed, 8);
for (j, v) in noise.iter().enumerate() {
if let Some(b) = m.get_mut(i + j) {
*b = *v;
}
}
out.push(m);
let mut m = sample.to_vec();
if let Some(b) = m.get_mut(i) {
*b = 0xff;
}
out.push(m);
}
out
}
pub mod formats {
fn leb(mut v: u32, out: &mut Vec<u8>) {
loop {
let b = (v & 0x7f) as u8;
v >>= 7;
if v == 0 {
out.push(b);
return;
}
out.push(b | 0x80);
}
}
fn name(s: &str, out: &mut Vec<u8>) {
leb(s.len() as u32, out);
out.extend_from_slice(s.as_bytes());
}
fn section(id: u8, body: &[u8], out: &mut Vec<u8>) {
out.push(id);
leb(body.len() as u32, out);
out.extend_from_slice(body);
}
fn custom(sname: &str, body: &[u8], out: &mut Vec<u8>) {
let mut c = Vec::new();
name(sname, &mut c);
c.extend_from_slice(body);
section(0, &c, out);
}
/// A small valid module: imports `env.log(i32)`, defines `add(i32,i32)->i32` and `_start`,
/// one memory, one mutable global, one data segment, exports `add`, `memory` and `_start`, and
/// carries `name`, `producers` and `sourceMappingURL` custom sections.
pub fn wasm_module() -> Vec<u8> {
wasm_module_with_source_mapping_url("demo.wasm.map")
}
/// The sample module with a caller-provided source map URL.
pub fn wasm_module_with_source_mapping_url(url: &str) -> Vec<u8> {
let mut m = b"\0asm\x01\0\0\0".to_vec();
section(1, &[3, 0x60, 2, 0x7f, 0x7f, 1, 0x7f, 0x60, 0, 0, 0x60, 1, 0x7f, 0], &mut m);
let mut imp = vec![1];
name("env", &mut imp);
name("log", &mut imp);
imp.extend_from_slice(&[0, 2]);
section(2, &imp, &mut m);
section(3, &[2, 0, 1], &mut m);
section(5, &[1, 0, 1], &mut m);
section(6, &[1, 0x7f, 1, 0x41, 42, 0x0b], &mut m);
let mut exp = vec![3];
name("add", &mut exp);
exp.extend_from_slice(&[0, 1]);
name("memory", &mut exp);
exp.extend_from_slice(&[2, 0]);
name("_start", &mut exp);
exp.extend_from_slice(&[0, 2]);
section(7, &exp, &mut m);
let add = [0u8, 0x20, 0, 0x20, 1, 0x6a, 0x0b];
let start = [0u8, 0x41, 7, 0x10, 0, 0x0b];
let mut code = vec![2, add.len() as u8];
code.extend_from_slice(&add);
code.push(start.len() as u8);
code.extend_from_slice(&start);
section(10, &code, &mut m);
section(11, &[1, 0, 0x41, 0, 0x0b, 2, b'h', b'i'], &mut m);
// name section
let mut n = Vec::new();
let mut module_name = Vec::new();
name("demo", &mut module_name);
n.push(0);
leb(module_name.len() as u32, &mut n);
n.extend_from_slice(&module_name);
let mut fnames = vec![3];
for (i, f) in ["log", "add", "_start"].iter().enumerate() {
leb(i as u32, &mut fnames);
name(f, &mut fnames);
}
n.push(1);
leb(fnames.len() as u32, &mut n);
n.extend_from_slice(&fnames);
custom("name", &n, &mut m);
let mut p = vec![2];
name("language", &mut p);
p.push(1);
name("Rust", &mut p);
name("", &mut p);
name("processed-by", &mut p);
p.push(1);
name("rustc", &mut p);
name("1.80.0", &mut p);
custom("producers", &p, &mut m);
let mut s = Vec::new();
name(url, &mut s);
custom("sourceMappingURL", &s, &mut m);
let mut tf = vec![1, b'+'];
name("mutable-globals", &mut tf);
custom("target_features", &tf, &mut m);
m
}
/// JavaScript glue that loads the module.
pub const WASM_GLUE: &str =
"const imports = { env: { log: (x) => console.log(x) } };\nfetch(\"./demo.wasm\").then(r => WebAssembly.instantiateStreaming(r, imports));\n";
}
pub fn leb(mut value: usize) -> Vec<u8> {
let mut out = Vec::new();
while value >= 128 {
out.push((value as u8 & 0x7f) | 0x80);
value >>= 7;
}
out.push(value as u8);
out
}
pub fn string(out: &mut Vec<u8>, value: &str) {
out.extend(leb(value.len()));
out.extend(value.as_bytes());
}