Skip to content

Commit 7bfa18e

Browse files
committed
Return an error on invalid section
1 parent 63f8b73 commit 7bfa18e

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

Diff for: crates/externref-xform/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//! goal at least is to have valid wasm modules coming in that don't use
1616
//! `externref` and valid wasm modules going out which use `externref` at the fringes.
1717
18-
use anyhow::{anyhow, bail, Error};
18+
use anyhow::{anyhow, bail, Context as _, Error};
1919
use std::cmp;
2020
use std::collections::{BTreeMap, HashMap, HashSet};
2121
use std::mem;
@@ -102,7 +102,8 @@ impl Context {
102102
/// we're appending entries.
103103
pub fn prepare(&mut self, module: &mut Module) -> Result<(), Error> {
104104
// Insert reference types to the target features section.
105-
wasm_bindgen_wasm_conventions::insert_target_feature(module, "reference-types");
105+
wasm_bindgen_wasm_conventions::insert_target_feature(module, "reference-types")
106+
.context("failed to parse `target_features` custom section")?;
106107

107108
// Figure out what the maximum index of functions pointers are. We'll
108109
// be adding new entries to the function table later (maybe) so

Diff for: crates/multi-value-xform/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@
9292
9393
#![deny(missing_docs, missing_debug_implementations)]
9494

95+
use anyhow::Context;
96+
9597
/// Run the transformation.
9698
///
9799
/// See the module-level docs for details on the transformation.
@@ -118,7 +120,8 @@ pub fn run(
118120
to_xform: &[(walrus::FunctionId, usize, Vec<walrus::ValType>)],
119121
) -> Result<Vec<walrus::FunctionId>, anyhow::Error> {
120122
// Insert multi-value to the target features section.
121-
wasm_bindgen_wasm_conventions::insert_target_feature(module, "multivalue");
123+
wasm_bindgen_wasm_conventions::insert_target_feature(module, "multivalue")
124+
.context("failed to parse `target_features` custom section")?;
122125

123126
let mut wrappers = Vec::new();
124127
for (func, return_pointer_index, results) in to_xform {

Diff for: crates/wasm-conventions/src/lib.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
use std::io::Cursor;
1010

11-
use anyhow::{anyhow, bail, Result};
11+
use anyhow::{anyhow, bail, Context, Result};
1212
use walrus::{
1313
ir::Value, ElementId, FunctionBuilder, FunctionId, FunctionKind, GlobalId, GlobalKind,
1414
InitExpr, MemoryId, Module, RawCustomSection, ValType,
@@ -152,9 +152,9 @@ pub fn get_or_insert_start_builder(module: &mut Module) -> &mut FunctionBuilder
152152
.builder_mut()
153153
}
154154

155-
pub fn insert_target_feature(module: &mut Module, new_feature: &str) {
155+
pub fn insert_target_feature(module: &mut Module, new_feature: &str) -> Result<()> {
156156
// Taken from <https://github.com/bytecodealliance/wasm-tools/blob/f1898f46bb9d96f0f09682415cb6ccfd6a4dca79/crates/wasmparser/src/limits.rs#L27>.
157-
assert!(new_feature.len() <= 100_000);
157+
anyhow::ensure!(new_feature.len() <= 100_000, "feature name too long");
158158

159159
// Try to find an existing section.
160160
let section = module
@@ -164,19 +164,22 @@ pub fn insert_target_feature(module: &mut Module, new_feature: &str) {
164164

165165
// If one exists, check if the target feature is already present.
166166
let section = if let Some((_, section)) = section {
167-
let section: &mut RawCustomSection = section.as_any_mut().downcast_mut().unwrap();
167+
let section: &mut RawCustomSection = section
168+
.as_any_mut()
169+
.downcast_mut()
170+
.context("failed to read section")?;
168171
let mut reader = BinaryReader::new(&section.data);
169172
// The first integer contains the target feature count.
170-
let count = reader.read_var_u32().unwrap();
173+
let count = reader.read_var_u32()?;
171174

172175
// Try to find if the target feature is already present.
173176
for _ in 0..count {
174177
// First byte is the prefix.
175178
let prefix_index = reader.current_position();
176-
let prefix = reader.read_u8().unwrap() as u8;
179+
let prefix = reader.read_u8()? as u8;
177180
// Read the feature.
178-
let length = reader.read_var_u32().unwrap();
179-
let feature = reader.read_bytes(length as usize).unwrap();
181+
let length = reader.read_var_u32()?;
182+
let feature = reader.read_bytes(length as usize)?;
180183

181184
// If we found the target feature, we are done here.
182185
if feature == new_feature.as_bytes() {
@@ -185,7 +188,7 @@ pub fn insert_target_feature(module: &mut Module, new_feature: &str) {
185188
section.data[prefix_index] = b'+';
186189
}
187190

188-
return;
191+
return Ok(());
189192
}
190193
}
191194

@@ -214,4 +217,6 @@ pub fn insert_target_feature(module: &mut Module, new_feature: &str) {
214217
leb128::write::unsigned(&mut section.data, new_feature.len() as u64).unwrap();
215218
// Lastly the target feature string is inserted.
216219
section.data.extend(new_feature.as_bytes());
220+
221+
Ok(())
217222
}

0 commit comments

Comments
 (0)