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

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