]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46430: Fix memory leak in interned strings of deep-frozen modules (GH-31549)
authorKumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Thu, 24 Feb 2022 16:54:06 +0000 (22:24 +0530)
committerGitHub <noreply@github.com>
Thu, 24 Feb 2022 16:54:06 +0000 (17:54 +0100)
Include/internal/pycore_pylifecycle.h
Misc/NEWS.d/next/Core and Builtins/2022-02-24-07-33-29.bpo-46430.c91TAg.rst [new file with mode: 0644]
Programs/_bootstrap_python.c
Programs/_freeze_module.c
Python/pylifecycle.c
Tools/scripts/deepfreeze.py

index 439bc5b470bb266baa08b52adf4e9da8eff4e777..deac6ee3d3f41b90550d6e05c6fbe4c3e521b1c8 100644 (file)
@@ -65,7 +65,7 @@ extern PyStatus _Py_HashRandomization_Init(const PyConfig *);
 extern PyStatus _PyImportZip_Init(PyThreadState *tstate);
 extern PyStatus _PyGC_Init(PyInterpreterState *interp);
 extern PyStatus _PyAtExit_Init(PyInterpreterState *interp);
-
+extern void _Py_Deepfreeze_Init(void);
 
 /* Various internal finalizers */
 
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-02-24-07-33-29.bpo-46430.c91TAg.rst b/Misc/NEWS.d/next/Core and Builtins/2022-02-24-07-33-29.bpo-46430.c91TAg.rst
new file mode 100644 (file)
index 0000000..0ae128b
--- /dev/null
@@ -0,0 +1 @@
+Fix memory leak in interned strings of deep-frozen modules.
index f4d0167b62e7123c30f900fd9d702469f63be312..75d455ca179835f83ced328044ce93b29ec0053f 100644 (file)
 #include "Python/frozen_modules/importlib._bootstrap_external.h"
 /* End includes */
 
-/* Empty finalizer for deepfrozen modules*/
+/* Empty initializer for deepfrozen modules */
+void _Py_Deepfreeze_Init(void)
+{
+}
+/* Empty finalizer for deepfrozen modules */
 void
-_Py_Deepfreeze_Fini(void) 
+_Py_Deepfreeze_Fini(void)
 {
 }
 
index 99a1d4dfc261f3914537dbefdcb0c46f7e06e499..d5a236a0c635c6b88dd7c4edbf9905eee90f520b 100644 (file)
 #include <unistd.h>
 #endif
 
+/* Empty initializer for deepfrozen modules */
+void _Py_Deepfreeze_Init(void)
+{
+}
 /* Empty finalizer for deepfrozen modules */
 void
 _Py_Deepfreeze_Fini(void)
index b6310c9aeb808b5eea8d5e4071eb365f5c23f9d7..9dd769a0e87b10fb788d1f076c9a2d0203a8d763 100644 (file)
@@ -754,7 +754,6 @@ pycore_init_types(PyInterpreterState *interp)
     if (_PyStatus_EXCEPTION(status)) {
         return status;
     }
-
     return _PyStatus_OK();
 }
 
@@ -827,7 +826,10 @@ pycore_interp_init(PyThreadState *tstate)
     if (_PyStatus_EXCEPTION(status)) {
         return status;
     }
-
+    // Intern strings in deep-frozen modules first so that others
+    // can use it instead of creating a heap allocated string.
+    _Py_Deepfreeze_Init();
+    
     status = pycore_init_types(interp);
     if (_PyStatus_EXCEPTION(status)) {
         goto done;
index 0edf3af71d9934d1057243d329a3d1e8ddcedc98..b62be3713feb8b76a3f62da8e26af6e3ae8d61e5 100644 (file)
@@ -110,6 +110,7 @@ class Printer:
         self.hits, self.misses = 0, 0
         self.patchups: list[str] = []
         self.deallocs: list[str] = []
+        self.interns: list[str] = []
         self.write('#include "Python.h"')
         self.write('#include "internal/pycore_gc.h"')
         self.write('#include "internal/pycore_code.h"')
@@ -279,7 +280,7 @@ class Printer:
             self.write(f".co_cellvars = {co_cellvars},")
             self.write(f".co_freevars = {co_freevars},")
         self.deallocs.append(f"_PyStaticCode_Dealloc(&{name});")
-        self.patchups.append(f"_PyStaticCode_InternStrings(&{name});")
+        self.interns.append(f"_PyStaticCode_InternStrings(&{name});")
         return f"& {name}.ob_base"
 
     def generate_tuple(self, name: str, t: Tuple[object, ...]) -> str:
@@ -446,6 +447,9 @@ def generate(args: list[str], output: TextIO) -> None:
     with printer.block(f"void\n_Py_Deepfreeze_Fini(void)"):
             for p in printer.deallocs:
                 printer.write(p)
+    with printer.block(f"void\n_Py_Deepfreeze_Init(void)"):
+            for p in printer.interns:
+                printer.write(p)
     if verbose:
         print(f"Cache hits: {printer.hits}, misses: {printer.misses}")