Skip to content

Commit

Permalink
Backport bytecodealliance#8323 to the Wasmtime 19 release branch (byt…
Browse files Browse the repository at this point in the history
…ecodealliance#8325)

* Handle out-of-bounds component sections (bytecodealliance#8323)

* Handle out-of-bounds component sections

Fixes bytecodealliance#8322

* Add a test that trancated component binaries don't cause panics

* Add release notes
  • Loading branch information
fitzgen authored Apr 11, 2024
1 parent 9014507 commit 5244937
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
9 changes: 9 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
--------------------------------------------------------------------------------

## 19.0.2

Released 2024-04-10.

* Fix a panic when compiling invalid components.
[#8322](https://github.com/bytecodealliance/wasmtime/issues/8322)

--------------------------------------------------------------------------------

## 19.0.1

Released 2024-04-02.
Expand Down
14 changes: 13 additions & 1 deletion crates/environ/src/component/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
EntityIndex, ModuleEnvironment, ModuleTranslation, ModuleTypesBuilder, PrimaryMap, Tunables,
TypeConvert, WasmHeapType, WasmValType,
};
use anyhow::anyhow;
use anyhow::{bail, Result};
use indexmap::IndexMap;
use std::collections::HashMap;
Expand Down Expand Up @@ -534,7 +535,18 @@ impl<'a, 'data> Translator<'a, 'data> {
self.validator,
self.types.module_types_builder(),
)
.translate(parser, &component[range.start..range.end])?;
.translate(
parser,
component.get(range.start..range.end).ok_or_else(|| {
anyhow!(
"section range {}..{} is out of bounds (bound = {})",
range.start,
range.end,
component.len()
)
.context("wasm component contains an invalid module section")
})?,
)?;
let static_idx = self.static_modules.push(translation);
self.result
.initializers
Expand Down
43 changes: 43 additions & 0 deletions tests/all/component_model/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,46 @@ fn detect_precompiled() -> Result<()> {
);
Ok(())
}

#[test]
#[cfg_attr(miri, ignore)]
fn truncated_component_binaries_dont_panic() -> Result<()> {
let engine = super::engine();

let binary = wat::parse_str(
r#"
(component
(import "a" (core module $m0
(import "" "" (func))
))
(core module $m1
(func (export ""))
)
(core instance $i1 (instantiate (module $m1)))
(func $f (canon lift (core func $i1 "f")))
(component $c1
(import "f" (func))
(core module $m2
(func (export "g"))
)
(core instance $i2 (instantiate $m2))
(func (export "g")
(canon lift (core func $i2 "g"))
)
)
(instance $i3 (instantiate $c1 (with "f" (func $f))))
(func (export "g") (alias export $i3 "g"))
)
"#,
)?;

// Check that if we feed each truncation of the component binary into
// `Component::new` we don't get any panics.
for i in 1..binary.len() - 1 {
let _ = Component::from_binary(&engine, &binary[0..i]);
}

Ok(())
}

0 comments on commit 5244937

Please sign in to comment.