Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-46476: Fix memory leak in code objects generated by deepfreeze #30853

Merged
merged 25 commits into from Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Include/internal/pycore_pylifecycle.h
Expand Up @@ -83,6 +83,7 @@ extern void _PyWarnings_Fini(PyInterpreterState *interp);
extern void _PyAST_Fini(PyInterpreterState *interp);
extern void _PyAtExit_Fini(PyInterpreterState *interp);
extern void _PyThread_FiniType(PyInterpreterState *interp);
extern void _Py_Deepfreeze_Fini(void);

extern PyStatus _PyGILState_Init(_PyRuntimeState *runtime);
extern PyStatus _PyGILState_SetTstate(PyThreadState *tstate);
Expand Down
@@ -0,0 +1 @@
Fix memory leak in code objects generated by deepfreeze. Patch by Kumar Aditya.
7 changes: 7 additions & 0 deletions Programs/_bootstrap_python.c
Expand Up @@ -14,6 +14,12 @@
#include "Python/frozen_modules/importlib._bootstrap_external.h"
/* End includes */

/* Empty finalizer for deepfrozen modules*/
void
_Py_Deepfreeze_Fini(void)
{
}

/* Note that a negative size indicates a package. */

static const struct _frozen bootstrap_modules[] = {
Expand Down Expand Up @@ -103,3 +109,4 @@ main(int argc, char **argv)
}
Py_ExitStatusException(status);
}

7 changes: 7 additions & 0 deletions Programs/_freeze_module.c
Expand Up @@ -22,6 +22,12 @@
#include <unistd.h>
#endif

/* Empty finalizer for deepfrozen modules */
void
_Py_Deepfreeze_Fini(void)
{
}

/* To avoid a circular dependency on frozen.o, we create our own structure
of frozen modules instead, left deliberately blank so as to avoid
unintentional import of a stale version of _frozen_importlib. */
Expand Down Expand Up @@ -235,3 +241,4 @@ main(int argc, char *argv[])
Py_Finalize();
return 1;
}

1 change: 1 addition & 0 deletions Python/pylifecycle.c
Expand Up @@ -1723,6 +1723,7 @@ finalize_interp_clear(PyThreadState *tstate)
_Py_HashRandomization_Fini();
_PyArg_Fini();
_Py_ClearFileSystemEncoding();
_Py_Deepfreeze_Fini();
}

finalize_interp_types(tstate->interp);
Expand Down
14 changes: 14 additions & 0 deletions Tools/scripts/deepfreeze.py
Expand Up @@ -109,6 +109,7 @@ def __init__(self, file: TextIO) -> None:
self.cache: Dict[tuple[type, object, str], str] = {}
self.hits, self.misses = 0, 0
self.patchups: list[str] = []
self.deallocs: list[str] = []
self.write('#include "Python.h"')
self.write('#include "internal/pycore_gc.h"')
self.write('#include "internal/pycore_code.h"')
Expand Down Expand Up @@ -277,6 +278,7 @@ def generate_code(self, name: str, code: types.CodeType) -> str:
self.write(f".co_varnames = {co_varnames},")
self.write(f".co_cellvars = {co_cellvars},")
self.write(f".co_freevars = {co_freevars},")
self.deallocs.append(f"dealloc_codeobject(&{name}, (_Py_CODEUNIT *) {removesuffix(co_code, '.ob_base.ob_base')}.ob_sval);")
return f"& {name}.ob_base"

def generate_tuple(self, name: str, t: Tuple[object, ...]) -> str:
Expand Down Expand Up @@ -440,6 +442,18 @@ def generate(args: list[str], output: TextIO) -> None:
else:
code = compile(fd.read(), f"<frozen {modname}>", "exec")
printer.generate_file(modname, code)
with printer.block("static void\ndealloc_codeobject(PyCodeObject *co, _Py_CODEUNIT *firstinstr)"):
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved
printer.write("PyMem_Free(co->co_quickened);")
printer.write("co->co_quickened = NULL;")
printer.write("PyMem_Free(co->co_extra);")
printer.write("co->co_extra = NULL;")
printer.write("co->co_firstinstr = firstinstr;")
with printer.block("if (co->co_weakreflist != NULL)"):
printer.write("PyObject_ClearWeakRefs((PyObject *)co);")
printer.write("co->co_weakreflist = NULL;")
with printer.block(f"void\n_Py_Deepfreeze_Fini(void)"):
for p in printer.deallocs:
printer.write(p)
if verbose:
print(f"Cache hits: {printer.hits}, misses: {printer.misses}")

Expand Down