Skip to content

Commit 251c043

Browse files
committed
fix: update branch offsets before evaluating compiled function frame size (#1699)
* add test case and improve triggering debug assert * fix issue-1698 the comment describes the underlying issue. * remove test because of its massive input file size
1 parent 2c93485 commit 251c043

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

crates/wasmi/src/engine/executor/stack/values.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,12 @@ impl FrameParams {
374374

375375
/// Zero-initialize the remaining locals and parameters.
376376
pub fn init_zeroes(mut self) {
377-
debug_assert!(self.range.start <= self.range.end);
377+
debug_assert!(
378+
self.range.start <= self.range.end,
379+
"failed to zero-initialize `FrameParams`: start = {:?}, end = {:?}",
380+
self.range.start,
381+
self.range.end,
382+
);
378383
while !core::ptr::eq(self.range.start, self.range.end) {
379384
// Safety: We do not write out-of-buffer due to the above condition.
380385
unsafe { self.init_next(UntypedVal::from(0_u64)) }

crates/wasmi/src/engine/translator/func/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,16 @@ impl WasmTranslator<'_> for FuncTranslator {
187187
mut self,
188188
finalize: impl FnOnce(CompiledFuncEntity),
189189
) -> Result<Self::Allocations, Error> {
190+
// Note: `update_branch_offsets` might change `frame_size` so we need to compute it prior.
191+
//
192+
// Context:
193+
// This only happens if the function has so many instructions that some conditional branch
194+
// operators need to be encoded as their fallbacks which requires to allocate more function
195+
// local constant values, thus increasing the size of the function frame.
196+
self.update_branch_offsets()?;
190197
let Some(frame_size) = self.frame_size() else {
191198
return Err(Error::from(TranslationError::AllocatedTooManyRegisters));
192199
};
193-
self.update_branch_offsets()?;
194200
finalize(CompiledFuncEntity::new(
195201
frame_size,
196202
self.instrs.drain(),

0 commit comments

Comments
 (0)