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

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