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

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