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

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