import _testcapi
compile("pass", "<warmup>", "single")
_testcapi.set_nomemory(64, 65)
try:
compile("pass", "<trigger>", "single")
except MemoryError:
pass
first = bytes(128)
second = bytes(128)
Job 1, './python.exe reproduce_assemble…' terminated by signal SIGSEGV (Address boundary error)
Note, this reproducer only works if the allocation sizes are exactly the same as on my mac! the nomemory values may need tweaking.
The error manifests itself as heap corruption from the double free.
Python 3.16.0a0 (heads/main-dirty:f051c68923b, Jun 8 2026, 16:26:15) [Clang 21.0.0 (clang-2100.1.1.101)]
Crash report
What happened?
Note, this reproducer only works if the allocation sizes are exactly the same as on my mac! the nomemory values may need tweaking.
The chain is:
_PyAssemble_MakeCodeObjectis calledRETURN_IF_ERROR(assemble_init(a, first_lineno));passing the assembler by ref as an argument.assemble_init, when allocating the linetable or the except table.assemble_initcleans up by decreffing all members, BUT the assembler fields are left populated with the old pointers, and the caller has the assembler reference.assemble_emitsees the error code and returns to_PyAssemble_MakeCodeObjectassemble_freeIS called, causing a double free of at leasta->a_bytecode.The error manifests itself as heap corruption from the double free.
CPython versions tested on:
CPython main branch
Operating systems tested on:
macOS
Output from running 'python -VV' on the command line:
Python 3.16.0a0 (heads/main-dirty:f051c68923b, Jun 8 2026, 16:26:15) [Clang 21.0.0 (clang-2100.1.1.101)]
Linked PRs
assemble_initwhen out of memory #151142assertthat may fail incfg_builder_check#151153assemble_initwhen out of memory (GH-151142) #151205assemble_initwhen out of memory (GH-151142) #151206assemble_initwhen out of memory (GH-151142) #151207compiler_mod()when entering the current compilation unit fails #151234assertthat may fail incfg_builder_check(GH-151153) #151313assertthat may fail incfg_builder_check(GH-151153) #151314assertthat may fail incfg_builder_check(GH-151153) #151315