From patchwork Sun Nov 17 22:09:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philippe Waroquiers X-Patchwork-Id: 36000 Received: (qmail 30867 invoked by alias); 17 Nov 2019 22:09:30 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 30859 invoked by uid 89); 17 Nov 2019 22:09:29 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.9 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.1 spammy=1567, 836, inf, HContent-Transfer-Encoding:8bit X-HELO: mailsec101.isp.belgacom.be Received: from mailsec101.isp.belgacom.be (HELO mailsec101.isp.belgacom.be) (195.238.20.97) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 17 Nov 2019 22:09:26 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=skynet.be; i=@skynet.be; q=dns/txt; s=securemail; t=1574028567; x=1605564567; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=IuVex3//LaA86nMNgqapVDB935XyAgfXleWp4bIg/OQ=; b=tyn9xzKqgafsFmIT49LiSdBLWuOvOd+xwgF3DUapLfT1Fj+tFS1ycSFl 8pG/+d7XLrpuqrW6uDcveZHSBj9XYA==; IronPort-SDR: 275o3oF+MlC5xE6c5vHpYb5Oqf4cqn9BV09mZOXEHUoeqqW/FZG1psoAKJp7+EauiX1g25ZxKK ZwO1RCc6N/nsC63aeOCyjVVTnNgfRQPD6NN4yjQdZpUpmQYT0BOnmURSu2FGY5edk4WDOWpznN dl/KmQJ0kruJbtfCEHq1Vrgiphj9ZESNNif+IJoAirngKJdIWIw3llGRAwLT4IWUojCyCOb6/d k2kVGnN76hyJ/WkPZDffeFpFfBW8EfG18AQba5sO/GiPUZgN44KKMWjMsN2EFgrk/tBq6q2vGG U0c= Received: from 117.79-136-217.adsl-dyn.isp.belgacom.be (HELO md.home) ([217.136.79.117]) by relay.skynet.be with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 17 Nov 2019 23:09:24 +0100 From: Philippe Waroquiers To: gdb-patches@sourceware.org Cc: Philippe Waroquiers Subject: [RFA] Fix a bunch of python leaks due to missing calls to tp_free in *_dealloc functions. Date: Sun, 17 Nov 2019 23:09:19 +0100 Message-Id: <20191117220919.25298-1-philippe.waroquiers@skynet.be> MIME-Version: 1.0 X-IsSubscribed: yes valgrind reports leaks in many python tests, such as: ==17162== VALGRIND_GDB_ERROR_BEGIN ==17162== 8,208 (5,472 direct, 2,736 indirect) bytes in 57 blocks are definitely lost in loss record 7,551 of 7,679 ==17162== at 0x4835753: malloc (vg_replace_malloc.c:307) ==17162== by 0x6EAFD1: _PyObject_New (object.c:279) ==17162== by 0x4720E6: blpy_iter(_object*) (py-block.c:92) ==17162== by 0x698772: PyObject_GetIter (abstract.c:2577) ==17162== by 0x2343BE: _PyEval_EvalFrameDefault (ceval.c:3159) ==17162== by 0x22E9E2: function_code_fastcall (call.c:283) ==17162== by 0x2340A8: _PyObject_Vectorcall (abstract.h:127) ==17162== by 0x2340A8: call_function (ceval.c:4987) ==17162== by 0x2340A8: _PyEval_EvalFrameDefault (ceval.c:3486) ==17162== by 0x22E9E2: function_code_fastcall (call.c:283) ==17162== by 0x82172B: _PyObject_Vectorcall (abstract.h:127) ==17162== by 0x82172B: method_vectorcall (classobject.c:67) ==17162== by 0x6AF474: _PyObject_Vectorcall (abstract.h:127) ==17162== by 0x6AF474: _PyObject_CallNoArg (abstract.h:153) ==17162== by 0x6AF474: _PyObject_CallFunctionVa (call.c:914) ==17162== by 0x6B0673: callmethod (call.c:1010) ==17162== by 0x6B0673: _PyObject_CallMethod_SizeT (call.c:1103) ==17162== by 0x477DFE: gdb_PyObject_CallMethod<> (python-internal.h:182) ==17162== by 0x477DFE: get_py_iter_from_func(_object*, char const*) (py-framefilter.c:272) ==17162== by 0x4791B4: py_print_args (py-framefilter.c:706) ==17162== by 0x4791B4: py_print_frame(_object*, enum_flags, ext_lang_frame_args, ui_out*, int, htab*) (py-framefilter.c:960) ==17162== by 0x47A130: gdbpy_apply_frame_filter(extension_language_defn const*, frame_info*, enum_flags, ext_lang_frame_args, ui_out*, int, int) (py-framefilter.c:1236) ==17162== by 0x369C39: apply_ext_lang_frame_filter(frame_info*, enum_flags, ext_lang_frame_args, ui_out*, int, int) (extension.c:563) ==17162== by 0x4EC9C9: backtrace_command_1 (stack.c:2031) ==17162== by 0x4EC9C9: backtrace_command(char const*, int) (stack.c:2183) ... Most of the leaks in python tests are due to the fact that many PyObject xxxxx_dealloc functions are missing the line to free self or obj such as: Py_TYPE (self)->tp_free (self); or Py_TYPE (obj)->tp_free (obj); With this patch, the number of python tests leaking decreases from 52 to 12. gdb/ChangeLog YYYY-MM-DD Philippe Waroquiers * python/py-block.c (blpy_dealloc): Call tp_free. (blpy_block_syms_dealloc): Likewise. * python/py-finishbreakpoint.c (bpfinishpy_dealloc): Likewise. * python/py-inferior.c (infpy_dealloc): Likewise. * python/py-lazy-string.c (stpy_dealloc): Likewise. * python/py-linetable.c (ltpy_iterator_dealloc): Likewise. * python/py-symbol.c (sympy_dealloc): Likewise. * python/py-symtab.c (stpy_dealloc): Likewise. * python/py-type.c (typy_iterator_dealloc): Likewise. --- gdb/python/py-block.c | 2 ++ gdb/python/py-finishbreakpoint.c | 1 + gdb/python/py-inferior.c | 1 + gdb/python/py-lazy-string.c | 1 + gdb/python/py-linetable.c | 1 + gdb/python/py-symbol.c | 1 + gdb/python/py-symtab.c | 1 + gdb/python/py-type.c | 1 + 8 files changed, 9 insertions(+) diff --git a/gdb/python/py-block.c b/gdb/python/py-block.c index 4dc47ff169..54c3be9b1a 100644 --- a/gdb/python/py-block.c +++ b/gdb/python/py-block.c @@ -276,6 +276,7 @@ blpy_dealloc (PyObject *obj) if (block->next) block->next->prev = block->prev; block->block = NULL; + Py_TYPE (obj)->tp_free (obj); } /* Given a block, and a block_object that has previously been @@ -370,6 +371,7 @@ blpy_block_syms_dealloc (PyObject *obj) block_syms_iterator_object *iter_obj = (block_syms_iterator_object *) obj; Py_XDECREF (iter_obj->source); + Py_TYPE (obj)->tp_free (obj); } /* Implementation of gdb.Block.is_valid (self) -> Boolean. diff --git a/gdb/python/py-finishbreakpoint.c b/gdb/python/py-finishbreakpoint.c index 7f818213bd..fc20575aea 100644 --- a/gdb/python/py-finishbreakpoint.c +++ b/gdb/python/py-finishbreakpoint.c @@ -83,6 +83,7 @@ bpfinishpy_dealloc (PyObject *self) Py_XDECREF (self_bpfinish->function_value); Py_XDECREF (self_bpfinish->return_type); Py_XDECREF (self_bpfinish->return_value); + Py_TYPE (self)->tp_free (self); } /* Triggered when gdbpy_should_stop is about to execute the `stop' callback diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index 28882221a9..d475263df9 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -856,6 +856,7 @@ infpy_dealloc (PyObject *obj) return; set_inferior_data (inf, infpy_inf_data_key, NULL); + Py_TYPE (obj)->tp_free (obj); } /* Clear the INFERIOR pointer in an Inferior object and clear the diff --git a/gdb/python/py-lazy-string.c b/gdb/python/py-lazy-string.c index 87c6ceaf2a..74f14a94eb 100644 --- a/gdb/python/py-lazy-string.c +++ b/gdb/python/py-lazy-string.c @@ -156,6 +156,7 @@ stpy_dealloc (PyObject *self) lazy_string_object *self_string = (lazy_string_object *) self; xfree (self_string->encoding); + Py_TYPE (self)->tp_free (self); } /* Low level routine to create a object. diff --git a/gdb/python/py-linetable.c b/gdb/python/py-linetable.c index aa43d9e7c7..96a5d3515b 100644 --- a/gdb/python/py-linetable.c +++ b/gdb/python/py-linetable.c @@ -369,6 +369,7 @@ ltpy_iterator_dealloc (PyObject *obj) ltpy_iterator_object *iter_obj = (ltpy_iterator_object *) obj; Py_DECREF (iter_obj->source); + Py_TYPE (obj)->tp_free (obj); } /* Return a reference to the line table iterator. */ diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c index 647c54b0a5..b112ce115f 100644 --- a/gdb/python/py-symbol.c +++ b/gdb/python/py-symbol.c @@ -357,6 +357,7 @@ sympy_dealloc (PyObject *obj) if (sym_obj->next) sym_obj->next->prev = sym_obj->prev; sym_obj->symbol = NULL; + Py_TYPE (obj)->tp_free (obj); } /* Implementation of diff --git a/gdb/python/py-symtab.c b/gdb/python/py-symtab.c index 15af300150..49c6a0ac54 100644 --- a/gdb/python/py-symtab.c +++ b/gdb/python/py-symtab.c @@ -253,6 +253,7 @@ stpy_dealloc (PyObject *obj) if (symtab->next) symtab->next->prev = symtab->prev; symtab->symtab = NULL; + Py_TYPE (obj)->tp_free (obj); } diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c index a2fca97a32..e4350993dd 100644 --- a/gdb/python/py-type.c +++ b/gdb/python/py-type.c @@ -1325,6 +1325,7 @@ typy_iterator_dealloc (PyObject *obj) typy_iterator_object *iter_obj = (typy_iterator_object *) obj; Py_DECREF (iter_obj->source); + Py_TYPE (obj)->tp_free (obj); } /* Create a new Type referring to TYPE. */