Skip to content

Commit 683fa63

Browse files
committed
Fix FreeList::add_capacity when we still don't have enough capacity for first allocation
When we haven't added enough capacity such that it is greater than or equal to our alignment, we were triggering a debug assertion and would get integer overflow and a corrupted free list in release builds. Note that, due to our GC heap sandboxing, this couldn't lead to anything worse than a panic/abort.
1 parent 6ba6e13 commit 683fa63

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

crates/wasmtime/src/runtime/vm/gc/enabled/free_list.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,13 @@ impl FreeList {
106106
// list.
107107
let new_cap = u32::try_from(self.capacity).unwrap_or(u32::MAX);
108108
let new_cap = round_u32_down_to_pow2(new_cap, ALIGN_U32);
109-
debug_assert!(new_cap >= index.get());
109+
110+
// If we haven't added enough capacity for our first allocation yet,
111+
// then just return and wait for more capacity.
112+
if index.get() > new_cap {
113+
return;
114+
}
115+
110116
let size = new_cap - index.get();
111117
debug_assert_eq!(size % ALIGN_U32, 0);
112118
if size == 0 {
@@ -914,4 +920,30 @@ mod tests {
914920
in the free list, when possible"
915921
);
916922
}
923+
924+
#[test]
925+
fn add_capacity_not_enough_for_first_alloc() {
926+
let layout = Layout::from_size_align(ALIGN_USIZE, ALIGN_USIZE).unwrap();
927+
928+
let mut free_list = FreeList::new(0);
929+
assert!(free_list.alloc(layout).unwrap().is_none(), "no capacity");
930+
931+
for _ in 1..2 * ALIGN_USIZE {
932+
free_list.add_capacity(1);
933+
assert!(
934+
free_list.alloc(layout).unwrap().is_none(),
935+
"not enough capacity"
936+
);
937+
}
938+
939+
free_list.add_capacity(1);
940+
free_list
941+
.alloc(layout)
942+
.unwrap()
943+
.expect("now we have enough capacity for one");
944+
assert!(
945+
free_list.alloc(layout).unwrap().is_none(),
946+
"but not enough capacity for two"
947+
);
948+
}
917949
}

0 commit comments

Comments
 (0)