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

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