Skip to content

fix: use the correct calling convention when over-applying a closure in lean_apply_m - #14969

Draft
Kha wants to merge 1 commit into
masterfrom
runtime-apply-m-fix
Draft

fix: use the correct calling convention when over-applying a closure in lean_apply_m#14969
Kha wants to merge 1 commit into
masterfrom
runtime-apply-m-fix

Conversation

@Kha

@Kha Kha commented Aug 29, 2026

Copy link
Copy Markdown
Member

This PR fixes a crash when more than 16 arguments are applied at once to a closure whose arity is at most 16. Deeply nested monad stacks can produce such applications, and the result was memory corruption rather than a clean call.

lean_apply_m handles applications of more than 16 arguments. Its over-application branch invoked the closure through FNN, which passes arguments as an array. That convention is only correct for closures whose arity exceeds LEAN_CLOSURE_MAX_ARGS; below that the generated code takes its arguments separately, so the callee received the argument array in its first parameter and register garbage in the rest. The fixed-arity lean_apply_N functions already guard this, lean_apply_15 even asserting arity > 16 immediately before its FNN call. The over-application branch now applies the first arity - fixed arguments via lean_apply_n, which dispatches on the count and consumes the closure, and continues with the remainder.

@github-actions github-actions Bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Aug 29, 2026
@mathlib-lean-pr-testing

mathlib-lean-pr-testing Bot commented Aug 29, 2026

Copy link
Copy Markdown

Mathlib CI status (docs):

  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 696e847a6c079637186e492f37e22dedf4faf679 --onto e991a05e359a25988f49bff3ab8af986e959b866. You can force Mathlib CI using the force-mathlib-ci label. (2026-08-29 16:47:56)
  • ✅ Mathlib branch lean-pr-testing-14969 has successfully built against this PR. (2026-08-29 21:36:52) View Log

@leanprover-bot

leanprover-bot commented Aug 29, 2026

Copy link
Copy Markdown
Collaborator

Reference manual CI status:

  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase 696e847a6c079637186e492f37e22dedf4faf679 --onto e991a05e359a25988f49bff3ab8af986e959b866. You can force reference manual CI using the force-manual-ci label. (2026-08-29 16:47:58)
  • ✅ Reference manual branch lean-pr-testing-14969 has successfully built against this PR. (2026-08-29 20:11:49) View Log
  • 🟡 Reference manual branch lean-pr-testing-14969 build against this PR didn't complete normally. (2026-08-29 20:12:23) View Log

…in `lean_apply_m`

This PR fixes a crash when more than 16 arguments are applied at once to a closure whose arity is at most 16. Deeply nested monad stacks can produce such applications, and the result was memory corruption rather than a clean call.

`lean_apply_m` handles applications of more than 16 arguments. Its over-application branch invoked the closure through `FNN`, which passes arguments as an array. That convention is only correct for closures whose arity exceeds `LEAN_CLOSURE_MAX_ARGS`; below that the generated code takes its arguments separately, so the callee received the argument array in its first parameter and register garbage in the rest. The fixed-arity `lean_apply_N` functions already guard this, `lean_apply_15` even asserting `arity > 16` immediately before its `FNN` call. The over-application branch now applies the first `arity - fixed` arguments via `lean_apply_n`, which dispatches on the count and consumes the closure, and continues with the remainder.
@Kha
Kha force-pushed the runtime-apply-m-fix branch from ae5d019 to df1b1f0 Compare August 29, 2026 19:50
@github-actions github-actions Bot added the mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN label Aug 29, 2026
@leanprover-bot leanprover-bot added the builds-manual CI has verified that the Lean Language Reference builds against this PR label Aug 29, 2026
@mathlib-lean-pr-testing mathlib-lean-pr-testing Bot added the builds-mathlib CI has verified that Mathlib builds against this PR label Aug 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

builds-manual CI has verified that the Lean Language Reference builds against this PR builds-mathlib CI has verified that Mathlib builds against this PR mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants