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

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