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

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