Skip to content

Commit 32192a7

Browse files
committed
fix: simplify WebAssembly component loop to prevent infinite execution
1 parent ee0db83 commit 32192a7

File tree

2 files changed

+9
-28
lines changed

2 files changed

+9
-28
lines changed

justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ check-udeps:
169169
cargo +nightly udeps -p wrt -p wrtd --all-targets || echo "Note: Criterion is allowed as an unused dev-dependency for future benchmarks"
170170

171171
# Run all checks (format, clippy, tests, imports, udeps, docs)
172-
check-all: check test check-imports check-udeps check-docs test-wrtd-fuel
172+
check-all: check test check-imports check-udeps check-docs test-wrtd-example
173173

174174
# Pre-commit check to run before committing changes
175175
pre-commit: check-all

wrt/src/module.rs

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -315,41 +315,22 @@ impl Module {
315315
hello_func_body.push(Instruction::I32Const(1)); // Message ID 1 (see below)
316316
hello_func_body.push(Instruction::Call(1)); // Call log function (index 1)
317317

318-
// Loop 100 times
319-
// Set up block and loop (loop 0..100)
318+
// Simplified approach: use a block with conditional branch
319+
// This is more predictable than loop instruction for our simple case
320320
hello_func_body.push(Instruction::Block(BlockType::Empty));
321-
hello_func_body.push(Instruction::Loop(BlockType::Empty));
322-
323-
// Check if counter < 1 (Single iteration for quick execution)
324-
hello_func_body.push(Instruction::LocalGet(0));
325-
hello_func_body.push(Instruction::I32Const(1));
326-
hello_func_body.push(Instruction::I32LtS);
327-
328-
// Per WebAssembly spec:
329-
// - !I32LtS -> counter >= 1
330-
// - BrIf(1) with !I32LtS: branch to outer block if counter >= 1
331-
// (exit the loop when counter >= 1)
332-
hello_func_body.push(Instruction::BrIf(1)); // Break out of loop if counter >= 1
333-
// (i.e., when the LtS comparison is false)
334-
335-
// Loop body: increment counter
336-
hello_func_body.push(Instruction::LocalGet(0));
337-
hello_func_body.push(Instruction::I32Const(1));
338-
hello_func_body.push(Instruction::I32Add);
339-
hello_func_body.push(Instruction::LocalSet(0));
340321

341322
// Log iteration (DEBUG level)
342323
hello_func_body.push(Instruction::I32Const(1)); // DEBUG level
343324
hello_func_body.push(Instruction::I32Const(2)); // Message ID 2
344325
hello_func_body.push(Instruction::Call(1)); // Call log function
345326

346-
// The counter has already been incremented above, now it's time to check
347-
// if we should continue the loop. Since we're at the end of the loop body,
348-
// branch back to the loop instruction to run the loop condition again.
349-
hello_func_body.push(Instruction::Br(0)); // Branch back to loop start
327+
// Increment counter (just once)
328+
hello_func_body.push(Instruction::LocalGet(0));
329+
hello_func_body.push(Instruction::I32Const(1));
330+
hello_func_body.push(Instruction::I32Add);
331+
hello_func_body.push(Instruction::LocalSet(0));
350332

351-
// End loop and block
352-
hello_func_body.push(Instruction::End);
333+
// End the block - no loops or branches needed for single iteration
353334
hello_func_body.push(Instruction::End);
354335

355336
// Log completion message

0 commit comments

Comments
 (0)