Blame SOURCES/gdb-rhbz1007614-memleak-infpy_read_memory-test.patch

a8223e
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
a8223e
From: Fedora GDB patches <invalid@email.com>
a8223e
Date: Fri, 27 Oct 2017 21:07:50 +0200
a8223e
Subject: gdb-rhbz1007614-memleak-infpy_read_memory-test.patch
a8223e
a8223e
;; Fix 'memory leak in infpy_read_memory()' (RH BZ 1007614)
a8223e
;;=fedoratest
a8223e
a8223e
Original message by Tom Tromey:
a8223e
a8223e
  <https://sourceware.org/ml/gdb-patches/2012-03/msg00955.html>
a8223e
  Message-ID: <871uoc1va3.fsf@fleche.redhat.com>
a8223e
a8223e
Comment from Sergio Durigan Junior:
a8223e
a8223e
  In order to correctly test this patch, I wrote a testcase based on Jan
a8223e
  Kratochvil's <gdb/testsuite/gdb.base/gcore-excessive-memory.exp>.  The
a8223e
  testcase, which can be seen below, tests GDB in order to see if the
a8223e
  amount of memory being leaked is minimal, as requested in the bugzilla.
a8223e
  It is hard to define what "minimum" is, so I ran the testcase on all
a8223e
  supported RHEL architectures and came up with an average.
a8223e
a8223e
commit cc0265cdda9dc7e8665e8bfcf5b4477489daf27c
a8223e
Author: Tom Tromey <tromey@redhat.com>
a8223e
Date:   Wed Mar 28 17:38:08 2012 +0000
a8223e
a8223e
    	* python/py-inferior.c (infpy_read_memory): Remove cleanups and
a8223e
    	explicitly free 'buffer' on exit paths.  Decref 'membuf_object'
a8223e
    	before returning.
a8223e
a8223e
diff --git a/gdb/testsuite/gdb.python/py-gdb-rhbz1007614-memleak-infpy_read_memory.c b/gdb/testsuite/gdb.python/py-gdb-rhbz1007614-memleak-infpy_read_memory.c
a8223e
new file mode 100644
a8223e
--- /dev/null
a8223e
+++ b/gdb/testsuite/gdb.python/py-gdb-rhbz1007614-memleak-infpy_read_memory.c
a8223e
@@ -0,0 +1,27 @@
a8223e
+/* This testcase is part of GDB, the GNU debugger.
a8223e
+
a8223e
+   Copyright 2014 Free Software Foundation, Inc.
a8223e
+
a8223e
+   This program is free software; you can redistribute it and/or modify
a8223e
+   it under the terms of the GNU General Public License as published by
a8223e
+   the Free Software Foundation; either version 3 of the License, or
a8223e
+   (at your option) any later version.
a8223e
+
a8223e
+   This program is distributed in the hope that it will be useful,
a8223e
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
a8223e
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a8223e
+   GNU General Public License for more details.
a8223e
+
a8223e
+   You should have received a copy of the GNU General Public License
a8223e
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
a8223e
+
a8223e
+static struct x
a8223e
+  {
a8223e
+    char unsigned u[4096];
a8223e
+  } x, *px = &x;
a8223e
+
a8223e
+int
a8223e
+main (int argc, char *argv[])
a8223e
+{
a8223e
+  return 0;
a8223e
+}
a8223e
diff --git a/gdb/testsuite/gdb.python/py-gdb-rhbz1007614-memleak-infpy_read_memory.exp b/gdb/testsuite/gdb.python/py-gdb-rhbz1007614-memleak-infpy_read_memory.exp
a8223e
new file mode 100644
a8223e
--- /dev/null
a8223e
+++ b/gdb/testsuite/gdb.python/py-gdb-rhbz1007614-memleak-infpy_read_memory.exp
a8223e
@@ -0,0 +1,68 @@
a8223e
+# Copyright 2014 Free Software Foundation, Inc.
a8223e
+
a8223e
+# This program is free software; you can redistribute it and/or modify
a8223e
+# it under the terms of the GNU General Public License as published by
a8223e
+# the Free Software Foundation; either version 3 of the License, or
a8223e
+# (at your option) any later version.
a8223e
+#
a8223e
+# This program is distributed in the hope that it will be useful,
a8223e
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
a8223e
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a8223e
+# GNU General Public License for more details.
a8223e
+#
a8223e
+# You should have received a copy of the GNU General Public License
a8223e
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
a8223e
+
a8223e
+set testfile py-gdb-rhbz1007614-memleak-infpy_read_memory
a8223e
+set srcfile ${testfile}.c
a8223e
+set binfile [standard_output_file ${testfile}]
a8223e
+
a8223e
+if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } {
a8223e
+    return -1
a8223e
+}
a8223e
+
a8223e
+if { [skip_python_tests] } { continue }
a8223e
+
a8223e
+set pid_of_gdb [exp_pid -i [board_info host fileid]]
a8223e
+
a8223e
+proc memory_v_pages_get {} {
a8223e
+    global pid_of_gdb
a8223e
+    set fd [open "/proc/$pid_of_gdb/statm"]
a8223e
+    gets $fd line
a8223e
+    close $fd
a8223e
+    # number of pages of virtual memory
a8223e
+    scan $line "%d" drs
a8223e
+    return $drs
a8223e
+}
a8223e
+
a8223e
+if { ![runto_main] } {
a8223e
+    untested $testfile.exp
a8223e
+    return -1
a8223e
+}
a8223e
+
a8223e
+set remote_python_file [remote_download host ${srcdir}/${subdir}/${testfile}.py]
a8223e
+
a8223e
+gdb_test "source ${remote_python_file}" ""
a8223e
+
a8223e
+gdb_test "hello-world" ""
a8223e
+
a8223e
+set kbytes_before [memory_v_pages_get]
a8223e
+verbose -log "kbytes_before = $kbytes_before"
a8223e
+
a8223e
+gdb_test "hello-world" ""
a8223e
+
a8223e
+set kbytes_after [memory_v_pages_get]
a8223e
+verbose -log "kbytes_after = $kbytes_after"
a8223e
+
a8223e
+set kbytes_diff [expr $kbytes_after - $kbytes_before]
a8223e
+verbose -log "kbytes_diff = $kbytes_diff"
a8223e
+
a8223e
+# The value "1000" was calculated by running a few GDB sessions with this
a8223e
+# testcase, and seeing how much (in average) the memory consumption
a8223e
+# increased after the "hello-world" command issued above.  The average
a8223e
+# was around 500 bytes, so I chose 1000 as a high estimate.
a8223e
+if { $kbytes_diff > 1000 } {
a8223e
+    fail "there is a memory leak on GDB (RHBZ 1007614)"
a8223e
+} else {
a8223e
+    pass "there is not a memory leak on GDB (RHBZ 1007614)"
a8223e
+}
a8223e
diff --git a/gdb/testsuite/gdb.python/py-gdb-rhbz1007614-memleak-infpy_read_memory.py b/gdb/testsuite/gdb.python/py-gdb-rhbz1007614-memleak-infpy_read_memory.py
a8223e
new file mode 100644
a8223e
--- /dev/null
a8223e
+++ b/gdb/testsuite/gdb.python/py-gdb-rhbz1007614-memleak-infpy_read_memory.py
a8223e
@@ -0,0 +1,30 @@
a8223e
+# Copyright (C) 2014 Free Software Foundation, Inc.
a8223e
+
a8223e
+# This program is free software; you can redistribute it and/or modify
a8223e
+# it under the terms of the GNU General Public License as published by
a8223e
+# the Free Software Foundation; either version 3 of the License, or
a8223e
+# (at your option) any later version.
a8223e
+#
a8223e
+# This program is distributed in the hope that it will be useful,
a8223e
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
a8223e
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a8223e
+# GNU General Public License for more details.
a8223e
+#
a8223e
+# You should have received a copy of the GNU General Public License
a8223e
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
a8223e
+
a8223e
+class HelloWorld (gdb.Command):
a8223e
+    """Greet the whole world."""
a8223e
+
a8223e
+    def __init__ (self):
a8223e
+        super (HelloWorld, self).__init__ ("hello-world",
a8223e
+                                           gdb.COMMAND_OBSCURE)
a8223e
+
a8223e
+    def invoke (self, arg, from_tty):
a8223e
+        px = gdb.parse_and_eval("px")
a8223e
+        core = gdb.inferiors()[0]
a8223e
+        for i in range(256 * 1024):
a8223e
+            chunk = core.read_memory(px, 4096)
a8223e
+        print "Hello, World!"
a8223e
+
a8223e
+HelloWorld ()