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

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