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

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