diff options
author | Andrew Burgess <aburgess@redhat.com> | 2024-09-25 15:44:56 +0100 |
---|---|---|
committer | Andrew Burgess <aburgess@redhat.com> | 2024-09-26 11:22:49 +0100 |
commit | f4b29c2f6109a3d83facd50fe6b7c2faf1962f39 (patch) | |
tree | 40f110da3c55cc80cafbb9ef01cec27fc01fa02a /gdb/testsuite/gdb.python/py-read-memory-leak.c | |
parent | 24979a6d5af83cb197d4acef57b13b55a328a43d (diff) | |
download | binutils-f4b29c2f6109a3d83facd50fe6b7c2faf1962f39.zip binutils-f4b29c2f6109a3d83facd50fe6b7c2faf1962f39.tar.gz binutils-f4b29c2f6109a3d83facd50fe6b7c2faf1962f39.tar.bz2 |
gdb/testsuite: test for memory leaks in gdb.Inferior.read_memory()
For a long time Fedora GDB has carried an out of tree patch which
checks for memory leaks in gdb.Inferior.read_memory(). At one point
in the distant past GDB did have a memory leak in this code, but this
was first fixed in commit:
commit 655e820cf9a039ee55325d9e1f8423796d592b4b
Date: Wed Mar 28 17:38:07 2012 +0000
* python/py-inferior.c (infpy_read_memory): Remove cleanups and
explicitly free 'buffer' on exit paths. Decref 'membuf_object'
before returning.
And the code has changed a lot since then, but the leak is still
fixed. Unfortunately, this commit didn't have any associated tests.
The original Fedora test wasn't really suitable for upstream, it was
reading /proc/PID/... to figure out if there was a leak or not.
However, we already have gdb.python/py-inferior-leak.exp in upstream
GDB, which makes use of the Python tracemalloc module to check for
memory leaks in a corner of the Python API, so I figured it wouldn't
hurt to rewrite the test in the same style.
And so here is a test for a bug which was closed 12 years ago. This
detects if the gdb.Inferior.read_memory() call leaks any memory.
I've tested this by hacking gdbpy_buffer_to_membuf, replacing the last
line which currently looks like this:
return PyMemoryView_FromObject ((PyObject *) membuf_obj.get ());
and instead doing:
return PyMemoryView_FromObject ((PyObject *) membuf_obj.release ());
The use of "release" here will mean we no longer decrement the
reference count on membuf_obj before returning from the function. As
a consequence the membuf_obj will not be garbage collected. With this
hack in place the new test will fail.
The Python script in the new test is mostly a copy&paste from
py-inferior-leak.py with the core changed to do a memory read instead
of inferior creation. I did consider rewriting both tests into a
single file, maybe, py-memory-leak.py, which would make it easier to
add additional similar tests in the future. For now I've held off
doing that, but if this gets merged then I _might_ revisit this idea.
If folk feel that this new test should only be accepted if I do this
rewrite then let me know and I can get that done.
On copyright date ranges: The .exp and .py scripts are new enough for
this commit that I've dated them 2024. The .c source script is lifted
directly from the old Fedora patch, so I've retained the original 2014
start date for that file only.
Approved-By: Tom Tromey <tom@tromey.com>
Diffstat (limited to 'gdb/testsuite/gdb.python/py-read-memory-leak.c')
-rw-r--r-- | gdb/testsuite/gdb.python/py-read-memory-leak.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/gdb/testsuite/gdb.python/py-read-memory-leak.c b/gdb/testsuite/gdb.python/py-read-memory-leak.c new file mode 100644 index 0000000..75035cd --- /dev/null +++ b/gdb/testsuite/gdb.python/py-read-memory-leak.c @@ -0,0 +1,27 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2014-2024 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +static struct x +{ + char unsigned u[4096]; +} x, *px = &x; + +int +main (void) +{ + return 0; +} |