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