Skip to content

Commit

Permalink
Fix type interning during Wasm-to-CLIF translation (#9374)
Browse files Browse the repository at this point in the history
We were handling the case where we had fewer rec groups than types, in the case
where a rec group defines multiple types, but were failing to handle the case
when we have more rec groups than types due to empty rec groups.

The fix is to bound the loop by the number of types, not the number of rec
groups.
  • Loading branch information
fitzgen authored Oct 4, 2024
1 parent b4fe60a commit 79da845
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
11 changes: 7 additions & 4 deletions crates/environ/src/compile/module_environ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ impl<'a, 'data> ModuleEnvironment<'a, 'data> {
Payload::TypeSection(types) => {
self.validator.type_section(&types)?;

let count = types.count();
let count = self.validator.types(0).unwrap().core_type_count();
log::trace!("interning {count} Wasm types");

let capacity = usize::try_from(count).unwrap();
self.result.module.types.reserve(capacity);
self.types.reserve_wasm_signatures(capacity);
Expand All @@ -254,15 +256,16 @@ impl<'a, 'data> ModuleEnvironment<'a, 'data> {
// groups, we need copy the duplicates over (shallowly) as well,
// so that our types index space doesn't have holes.
let mut type_index = 0;
for _ in 0..count {
while type_index < count {
let validator_types = self.validator.types(0).unwrap();

// Get the rec group for the current type index, which is
// always the first type defined in a rec group.
log::trace!("looking up wasmparser type for index {type_index}");
let core_type_id = validator_types.core_type_at(type_index).unwrap_sub();
log::trace!(
"about to intern rec group for {core_type_id:?} = {:?}",
validator_types[core_type_id]
" --> {core_type_id:?} = {:?}",
validator_types[core_type_id],
);
let rec_group_id = validator_types.rec_group_id_of(core_type_id);
debug_assert_eq!(
Expand Down
18 changes: 18 additions & 0 deletions tests/misc_testsuite/gc/more-rec-groups-than-types.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
;; Test that we properly handle empty rec groups and when we have more rec
;; groups defined in the type section than actual types (and therefore the
;; length that the type section reports is greater than the length of the types
;; index space).

(module
(rec)
(rec)
(rec)
(rec)
(rec)
(rec)
(rec)
(rec)
(rec)
(rec)
(type (func (param i32) (result i32)))
)

0 comments on commit 79da845

Please sign in to comment.