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

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