Skip to content

Commit 83ff33c

Browse files
committed
Return an error on invalid section
1 parent 63f8b73 commit 83ff33c

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

crates/externref-xform/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
.expect("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

crates/multi-value-xform/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ pub fn run(
118118
to_xform: &[(walrus::FunctionId, usize, Vec<walrus::ValType>)],
119119
) -> Result<Vec<walrus::FunctionId>, anyhow::Error> {
120120
// Insert multi-value to the target features section.
121-
wasm_bindgen_wasm_conventions::insert_target_feature(module, "multivalue");
121+
wasm_bindgen_wasm_conventions::insert_target_feature(module, "multivalue")
122+
.expect("failed to parse `target_features` custom section");
122123

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

crates/wasm-conventions/src/lib.rs

Lines changed: 13 additions & 8 deletions
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,7 +152,7 @@ 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>.
157157
assert!(new_feature.len() <= 100_000);
158158

@@ -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)