Skip to content

fix: prevent OutputStackOverflow in intrinsic i32::overflowing_sub#1165

Merged
mooori merged 1 commit into
nextfrom
mooori/fix-intrinsic-i32-overflowing-sub
Jun 10, 2026
Merged

fix: prevent OutputStackOverflow in intrinsic i32::overflowing_sub#1165
mooori merged 1 commit into
nextfrom
mooori/fix-intrinsic-i32-overflowing-sub

Conversation

@mooori

@mooori mooori commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Closes #1163

On top of #1160 to make use of intrinsic testing.

Problem

drop push.MAX # [i32::MAX, a]

The drop would cause stack depth to shrink below the minimum of 16, so the processor implicitly adds a stack slot by adjusting the bottom of the stack [ref].

So after drop push.MAX the stack depth is 17 while in the intrinsic it's assumed to be 16. The extra slot isn't cleaned up, which results in OutputStackOverflow.

Footgun?

MASM authors should probably be aware of this, still it can be a footgun. Could this be:

  • turned into a warning/error produced by a linter?
  • prevented by the processor by trying to recognize such scenarios and remove the extra slot if possible?
    • though this might not always work reliably and would break MASM which builds on this behavior

@mooori mooori marked this pull request as ready for review June 8, 2026 16:59
@mooori mooori requested review from bitwalker and greenhat June 8, 2026 16:59
@mooori mooori linked an issue Jun 8, 2026 that may be closed by this pull request

@greenhat greenhat left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looking good!

Base automatically changed from mooori/intrinsic-testing to next June 10, 2026 11:51
@mooori mooori merged commit 8af373b into next Jun 10, 2026
14 checks passed
@mooori mooori deleted the mooori/fix-intrinsic-i32-overflowing-sub branch June 10, 2026 12:16

@bitwalker bitwalker left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This should not have been merged - this behavior in the processor with regard to the operand stack is known and expected, and is why we emit calls to core::sys::truncate_stack at the end of every program entrypoint - which is what should have been done here.

Let's revert this and adjust the test code instead to call that procedure like we do during codegen

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.

Intrinsic i32::overflowing_sub can create OutputStackOverflow

3 participants