Skip to content

fix: prevent corruption of the small allocator state#13548

Merged
Kha merged 1 commit into
leanprover:masterfrom
eric-wieser:fix-small-allocator
May 6, 2026
Merged

fix: prevent corruption of the small allocator state#13548
Kha merged 1 commit into
leanprover:masterfrom
eric-wieser:fix-small-allocator

Conversation

@eric-wieser
Copy link
Copy Markdown
Contributor

@eric-wieser eric-wieser commented Apr 27, 2026

This PR fixes possible corruption when recovering from memory exhaustion.

This code threw an std::bad_alloc in the case of memory exhaustion, but left the small allocator in an inconsistent state when doing so. This would mean any code catching and recovering from the allocation failure would likely find itself with a corrupt small allocator.

While we could try and make this code exception safe in future, simply making it panic is better than the status quo, and is consistent with how we handle most other allocation failures.

changelog-please-rerun-the-changelog-ci

This code threw an `std::bad_alloc` in the case of memory exhaustion, but left the small allocator in an inconsistent state when doing so. This would mean any code catching and recovering from the allocation failure would likely find itself with a corrupt small allocator.

While we could try and make this code exception safe in future, simply making it panic is better than the status quo, and is consistent with how we handle most other allocation failures.
@eric-wieser
Copy link
Copy Markdown
Contributor Author

changelog-language

@github-actions github-actions Bot added changelog-language Language features and metaprograms toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN labels Apr 27, 2026
@mathlib-lean-pr-testing
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 cae4decead7dae180b50b25f2225419a1ed4280e --onto 3c6317b6d77a565b4217532d1190ac6955dba842. You can force Mathlib CI using the force-mathlib-ci label. (2026-04-27 21:13:57)

@leanprover-bot
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 cae4decead7dae180b50b25f2225419a1ed4280e --onto 3fc99eef102549c743c6c63547983319bfae6f01. You can force reference manual CI using the force-manual-ci label. (2026-04-27 21:13:59)

Comment thread src/runtime/alloc.cpp
void heap::alloc_segment() {
LEAN_RUNTIME_STAT_CODE(g_num_segments++);
segment * s = new segment();
LEAN_RUNTIME_STAT_CODE(g_num_segments++);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If new throws, then the number of segments has not yet increased.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah, I was confused in the combination with abort but I guess it doesn't hurt

@eric-wieser eric-wieser requested a review from Kha May 5, 2026 14:39
@Kha Kha added this pull request to the merge queue May 6, 2026
Merged via the queue into leanprover:master with commit 56fe75e May 6, 2026
21 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog-language Language features and metaprograms 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.

3 participants