Skip to content

fix(vm): make loop iteration limit consistent across loop kinds - #5464

Open
mansiverma897993 wants to merge 1 commit into
boa-dev:mainfrom
mansiverma897993:fix/loop-iteration-limit-5461
Open

fix(vm): make loop iteration limit consistent across loop kinds#5464
mansiverma897993 wants to merge 1 commit into
boa-dev:mainfrom
mansiverma897993:fix/loop-iteration-limit-5461

Conversation

@mansiverma897993

Copy link
Copy Markdown
Contributor

This Pull Request fixes/closes #5461.

With set_loop_iteration_limit(10), a for loop body ran 12 times and a while loop body ran 11 times before the RuntimeLimit error was raised. Two bugs compounded:

  • IncrementLoopIteration only raised the error when the counter was strictly greater than the limit, which allows one extra iteration for every loop kind.
  • for loops jumped over their IncrementLoopIteration opcode on the first pass (the initial jump into the loop lands after it, since it must skip the per-iteration binding copy and the final expression), so the first iteration was never counted.

It changes the following:

  • IncrementLoopIteration now raises the limit error when the counter reaches the limit instead of one past it.
  • The bytecompiler emits IncrementLoopIteration after the condition/done check and right before the loop body for all loop kinds (for, while, do-while, for-in, for-of). This means the limit counts actual body executions: a limit of N allows exactly N body executions for every loop kind, and loops that finish within the limit (e.g. for (let i = 0; i < 10; ++i) with limit 10, or a for-of over a 10-element array) never raise a limit error.
  • Adds a regression test asserting that all five loop kinds stop after exactly limit body executions and agree with each other, and regenerates the affected insta-bytecode snapshots.

With `set_loop_iteration_limit(10)`, a `for` loop body ran 12 times and a
`while` loop body 11 times before the limit error was raised. Two bugs
compounded:

- `IncrementLoopIteration` only errored when the counter was strictly
  greater than the limit, allowing one extra iteration.
- `for` loops jumped over the `IncrementLoopIteration` opcode on the
  first iteration, so it was never counted.

Emit `IncrementLoopIteration` after the condition/done check and before
the body in all loop kinds (for, while, do-while, for-in, for-of), and
error once the counter reaches the limit. A limit of N now allows
exactly N body executions for every loop kind, and loops that finish
within the limit never raise a limit error.

Fixes boa-dev#5461
@mansiverma897993
mansiverma897993 requested a review from a team as a code owner July 28, 2026 11:25
@github-actions github-actions Bot added Waiting On Review Waiting on reviews from the maintainers C-Tests Issues and PRs related to the tests. labels Jul 28, 2026
@github-actions github-actions Bot added this to the v1.0.0 milestone Jul 28, 2026
@github-actions github-actions Bot added the C-VM Issues and PRs related to the Boa Virtual Machine. label Jul 28, 2026
@github-actions

Copy link
Copy Markdown

Test262 conformance changes

Test result main count PR count difference
Total 53,125 53,125 0
Passed 51,073 51,073 0
Ignored 1,482 1,482 0
Failed 570 570 0
Panics 0 0 0
Conformance 96.14% 96.14% 0.00%

Tested main commit: 4fc75c6ae9d85f2b8065c6716f88e9b35318438c
Tested PR commit: a4dde97a3665062e6dee57357109c72f2fbc65e4
Compare commits: 4fc75c6...a4dde97

@codecov

codecov Bot commented Jul 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 62.88%. Comparing base (6ddc2b4) to head (a4dde97).
⚠️ Report is 1012 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main    #5464       +/-   ##
===========================================
+ Coverage   47.24%   62.88%   +15.63%     
===========================================
  Files         476      530       +54     
  Lines       46892    59102    +12210     
===========================================
+ Hits        22154    37166    +15012     
+ Misses      24738    21936     -2802     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-Tests Issues and PRs related to the tests. C-VM Issues and PRs related to the Boa Virtual Machine. Waiting On Review Waiting on reviews from the maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

for loops run one more iteration than while loops under the same loop iteration limit

1 participant