Skip to content

jit: align due_insn_count with the interpreter on budget exhaustion - #233

Merged
nagisa merged 3 commits into
anza-xyz:mainfrom
latent-9:fix/jit-due-insn-count-off-by-one
Oct 13, 2026
Merged

nagisa merged 3 commits into
anza-xyz:mainfrom
latent-9:fix/jit-due-insn-count-off-by-one

Conversation

@latent-9

@latent-9 latent-9 commented Oct 4, 2026

Copy link
Copy Markdown

Fixes #146

Problem

The budget-exceeded handler saved the instruction meter to the scratch register but did not increment it, while the shared epilogue always decremented it by one. The normal exit path has a matching increment before jumping to the epilogue, but the exceeded path did not, so the JIT reported due_insn_count = budget + 1 while the interpreter reported budget.

The saturating subtraction in consume() masked the difference from instruction_count, but the value passed to ContextObject::consume() was still one greater than intended.

Fix

Increment the instruction meter in the exceeded handler to cancel the epilogue's decrement, matching the interpreter's accounting. One line in the JIT assembly emission.

Testing

  • New test_jit_exceeded_due_insn_count_matches_interpreter checks due_insn_count directly (the saturation hides the difference through instruction_count). Red on the old code (JIT 6, interpreter 5, budget 5), green after.
  • Full suite: 125 passed, 0 failed.

@nagisa

nagisa commented Oct 5, 2026

Copy link
Copy Markdown

To me the broad direction seems about right. I believe interpreter's behaviour here is the more appropriate one. The code path is cold so the addition doesn't hurt anything.

@latent-9

latent-9 commented Oct 8, 2026

Copy link
Copy Markdown
Author

Rebased onto latest main to resolve the conflict with the new tests at the end of tests/execution.rs. Also removed the unused stack/heap bindings in the new test, which were failing the beta toolchain lint job. The CI run is awaiting approval.

Comment thread tests/execution.rs Outdated
));
let due_insn_count_jit = vm.due_insn_count;

assert_eq!(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the nature of the problem I believe this check should rather be a part of test_interpreter_and_jit. It will then cover every single test that uses said macro.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The macro now compares due_insn_count between the engines on every test that uses it. I also tried adding the absolute equality (due == budget on exhaustion) to the macro, but it cannot hold universally: test_err_instruction_count_syscall_capped exhausts after a syscall, and dispatch_syscall resets due_insn_count mid-run, so the due count at exhaustion is the post-syscall tally rather than the budget.

@codecov-commenter

codecov-commenter commented Oct 8, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 67.42%. Comparing base (3f0bddc) to head (240eb84).
⚠️ Report is 10 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #233      +/-   ##
==========================================
+ Coverage   66.87%   67.42%   +0.54%     
==========================================
  Files          26       26              
  Lines       17925    18199     +274     
==========================================
+ Hits        11988    12271     +283     
+ Misses       5937     5928       -9     

☔ 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.

The budget-exceeded handler saved the instruction meter to the scratch
register but did not increment it, while the shared epilogue always
decremented it by one. The normal exit path has a matching increment
before jumping to the epilogue, but the exceeded path did not, so the
JIT reported due_insn_count = budget + 1 while the interpreter reported
budget.

The saturating subtraction in consume() masked the difference from
instruction_count, but the value passed to ContextObject::consume()
was still one greater than intended.

Increment the instruction meter in the exceeded handler to cancel the
epilogue's decrement, matching the interpreter's accounting. The
regression test checks due_insn_count directly, since the saturation
hides the difference through instruction_count.

Fixes anza-xyz#146
@latent-9

latent-9 commented Oct 8, 2026

Copy link
Copy Markdown
Author

Moved the due_insn_count check into the test_interpreter_and_jit macro as suggested, so it now covers every test that uses the macro. The standalone test now only verifies the interpreter baseline (budget-exceeded case). All 147 tests pass.

nagisa
nagisa previously approved these changes Oct 8, 2026
Comment thread tests/execution.rs Outdated
/// `instruction_count`, so the `test_interpreter_and_jit` macro now checks
/// `due_insn_count` directly on every test that uses it.
#[test]
fn test_jit_exceeded_due_insn_count_matches_interpreter() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason this can't be run through test_interpreter_and_jit_asm() like the other tests?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No good reason. Removed it; with the relative check in the macro it would have been redundant with test_tight_infinite_loop_unconditional anyway.

Comment thread tests/execution.rs Outdated
ja -1
exit",
loader,
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this identical to test_tight_infinite_loop_unconditional()?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, it is the same program and budget. Removed it; the macro's due_insn_count comparison covers the alignment on every test.

@nagisa
nagisa added this pull request to the merge queue Oct 13, 2026
Merged via the queue into anza-xyz:main with commit 9327996 Oct 13, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JIT/interpreter off-by-one in due_insn_count on budget exhaustion

4 participants