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

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