Skip to content

gh-151112: Fix crash in compiler_mod() when entering the current compilation unit fails#151234

Merged
StanFromIreland merged 2 commits into
python:mainfrom
StanFromIreland:compiler_mod
Jun 11, 2026
Merged

gh-151112: Fix crash in compiler_mod() when entering the current compilation unit fails#151234
StanFromIreland merged 2 commits into
python:mainfrom
StanFromIreland:compiler_mod

Conversation

@StanFromIreland

@StanFromIreland StanFromIreland commented Jun 10, 2026

Copy link
Copy Markdown
Member

_PyCodegen_EnterAnonymousScope can fail to enter the module's compilation unit (e.g. when OOM, like the in repro), so c->u is left NULL. compiler_mod's cleanup then calls _PyCompile_ExitScope, which derefs that NULL unit in compiler_unit_free and segfaults.

The reproducers start/stop for nomemory will probably have to be adjusted, but for me I can reproduce with:

import _testcapi

compile("pass", "<warmup>", "single")
_testcapi.set_nomemory(31, 32)
try:
    compile("pass", "<trigger>", "single")
except MemoryError:
    pass
first = bytes(128)
second = bytes(128)

Skipping news, the other PR has one which I think can cover this too, these are both quite rare.

Comment thread Python/compile.c
co = _PyCompile_OptimizeAndAssemble(c, addNone);
finally:
_PyCompile_ExitScope(c);
if (c->u != NULL) {

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.

Are we sure c->u was NULL when we entered compiler_mod? Can we assert that at the top of the function?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, in _PyAST_Compile we initialise c:

cpython/Python/compile.c

Lines 1527 to 1532 in 9fd1a12

compiler *c = new_compiler(mod, filename, pflags, optimize, arena, module);
if (c == NULL) {
return NULL;
}
PyCodeObject *co = compiler_mod(c, mod);

But new_compiler doesn't set c->u.

@StanFromIreland StanFromIreland added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Jun 10, 2026
@StanFromIreland StanFromIreland merged commit 937d89c into python:main Jun 11, 2026
61 checks passed
@StanFromIreland StanFromIreland deleted the compiler_mod branch June 11, 2026 14:52
@miss-islington-app

Copy link
Copy Markdown

Thanks @StanFromIreland for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Sorry, @StanFromIreland, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 937d89c4d9b7c5fda6730a1127db118d881d13cb 3.13

@bedevere-app

bedevere-app Bot commented Jun 11, 2026

Copy link
Copy Markdown

GH-151350 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jun 11, 2026
@bedevere-app

bedevere-app Bot commented Jun 11, 2026

Copy link
Copy Markdown

GH-151351 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Jun 11, 2026
@StanFromIreland StanFromIreland removed the needs backport to 3.13 bugs and security fixes label Jun 11, 2026
StanFromIreland added a commit that referenced this pull request Jun 11, 2026
…rent compilation unit fails (GH-151234) (#151351)

(cherry picked from commit 937d89c)

Co-authored-by: Stan Ulbrych <stan@python.org>
StanFromIreland added a commit that referenced this pull request Jun 11, 2026
…rent compilation unit fails (GH-151234) (#151350)

(cherry picked from commit 937d89c)

Co-authored-by: Stan Ulbrych <stan@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants