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

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