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

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