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

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