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

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