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

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