From: Simon Marchi <simon.marchi@ericsson.com>
To: <gdb-patches@sourceware.org>
Cc: Simon Marchi <simon.marchi@ericsson.com>
Subject: [pushed master+8.0] Add missing incref when creating Inferior Python object
Date: Thu, 27 Apr 2017 21:13:00 -0000 [thread overview]
Message-ID: <20170427211155.29204-1-simon.marchi@ericsson.com> (raw)
In-Reply-To: <81b15bf924ea176fc601dea7be679200@polymtl.ca>
I have pushed this patch (by itself) to master and 8.0. It just needed a
little refresh, here's the pushed version.
The test py-inferior.exp fails when using a debug build of Python 3.6. I don't
see it failing with my system's default Python, but it might be related to the
different memory allocation scheme used when doing a build with pydebug.
The issue is that we are missing a Py_INCREF in
inferior_to_inferior_object. The PyObject_New function initializes the
object with a refcount of 1. If we assume that this refcount
corresponds to the reference we are returning, then we are missing an
incref for the reference in the inferior data.
The counterpart for the incref that corresponds to the reference in the
inferior data is in py_free_inferior, in the form the gdbpy_ref instance.
Here's how I can get it to crash (with some debug output):
$ ./gdb -nx -ex "set debug python 1"
(gdb) add-inferior
Added inferior 2
(gdb) python infs = gdb.inferiors()
Creating Python Inferior object inf = 1
Creating Python Inferior object inf = 2
(gdb) remove-inferiors 2
py_free_inferior inf = 2
infpy_dealloc inf = <unknown>
(gdb) python infs = None
Fatal Python error: Objects/tupleobject.c:243 object at 0x7f9cf1a568d8 has negative ref count -1
Current thread 0x00007f9cf1b68780 (most recent call first):
File "<string>", line 1 in <module>
[1] 408 abort (core dumped) ./gdb -nx -ex "set debug python 1"
After having created the inferiors object, their refcount is 1 (which
comes from PyObject_New), but it should be two. The gdb inferior object
has a reference and the "infs" list has a reference.
When invoking remove-inferiors, py_free_inferior gets called. It does
the decref that corresponds to the reference that the gdb inferior
object kept. At this moment, the refcount drops to 0 and the object
gets deallocated, even though the "infs" list still has a reference.
When we set "infs" to None, Python tries to decref the already zero
refcount and the assert triggers.
With this patch, it looks better:
(gdb) add-inferior
Added inferior 2
(gdb) python infs = gdb.inferiors()
Creating Python Inferior object inf = 1
Creating Python Inferior object inf = 2
(gdb) remove-inferiors 2
py_free_inferior inf = 2
(gdb) python infs = None
infpy_dealloc inf = <unknown>
gdb/ChangeLog:
* python/py-inferior.c (inferior_to_inferior_object): Increment reference
count when creating the object.
---
gdb/ChangeLog | 5 +++++
gdb/python/py-inferior.c | 7 +++++--
2 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 2b265a1301..e356e0b7ea 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,8 @@
+2017-04-27 Simon Marchi <simon.marchi@ericsson.com>
+
+ * python/py-inferior.c (inferior_to_inferior_object): Increment reference
+ count when creating the object.
+
2017-04-25 Yao Qi <yao.qi@linaro.org>
* aarch64-tdep.c (aarch64_gdbarch_init): Don't call
diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c
index 3d2cb1ddbe..f6a24a090f 100644
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -223,11 +223,14 @@ inferior_to_inferior_object (struct inferior *inferior)
inf_obj->threads = NULL;
inf_obj->nthreads = 0;
+ /* PyObject_New initializes the new object with a refcount of 1. This
+ counts for the reference we are keeping in the inferior data. */
set_inferior_data (inferior, infpy_inf_data_key, inf_obj);
}
- else
- Py_INCREF ((PyObject *)inf_obj);
+
+ /* We are returning a new reference. */
+ Py_INCREF ((PyObject *)inf_obj);
return (PyObject *) inf_obj;
}
--
2.11.0
next prev parent reply other threads:[~2017-04-27 21:13 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-01-23 22:43 [PATCH 0/5] Improve Python Inferior reference handling + fix a bug Simon Marchi
2017-01-23 22:43 ` [PATCH 4/5] Make Python inferior-related internal functions return a gdbpy_inf_ref Simon Marchi
2017-01-24 16:15 ` Simon Marchi
2017-02-09 12:30 ` Pedro Alves
2017-02-09 16:39 ` Simon Marchi
2017-01-23 22:43 ` [PATCH 1/5] Introduce specialized versions of gdbpy_ref Simon Marchi
2017-01-24 15:54 ` Tom Tromey
2017-01-24 16:18 ` Simon Marchi
2017-02-09 11:58 ` Pedro Alves
2017-02-09 16:18 ` Simon Marchi
2017-01-23 22:43 ` [PATCH 2/5] Add Python Inferior object debug traces Simon Marchi
2017-01-23 22:43 ` [PATCH 5/5] Add missing incref when creating Inferior Python object Simon Marchi
2017-02-25 18:41 ` Simon Marchi
2017-04-27 21:13 ` Simon Marchi [this message]
2017-01-23 22:43 ` [PATCH 3/5] Make Python inferior-related internal functions return inferior_object* Simon Marchi
2017-01-24 0:03 ` Pedro Alves
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170427211155.29204-1-simon.marchi@ericsson.com \
--to=simon.marchi@ericsson.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox