Blame SOURCES/gdb-rhbz1842691-corefile-mem-access-14of15.patch

0efd7d
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
0efd7d
From: Keith Seitz <keiths@redhat.com>
0efd7d
Date: Tue, 28 Jul 2020 09:46:44 -0400
0efd7d
Subject: gdb-rhbz1842691-corefile-mem-access-14of15.patch
0efd7d
0efd7d
;; New core file tests with mappings over existing program memory
0efd7d
;; Kevin Buettner, RH BZ 1842961
0efd7d
0efd7d
   Author: Kevin Buettner <kevinb@redhat.com>
0efd7d
   Date:   Wed Jun 17 19:25:47 2020 -0700
0efd7d
0efd7d
    New core file tests with mappings over existing program memory
0efd7d
0efd7d
    This test case was inspired by Pedro's demonstration of a problem
0efd7d
    with my v2 patches.  It can be found here:
0efd7d
0efd7d
        https://sourceware.org/pipermail/gdb-patches/2020-May/168826.html
0efd7d
0efd7d
    In a nutshell, my earlier patches could not handle the case in
0efd7d
    which a read-only mapping created with mmap() was created at
0efd7d
    an address used by other file-backed read-only memory in use by
0efd7d
    the process.
0efd7d
0efd7d
    This problem has been fixed (for Linux, anyway) by the commit "Use
0efd7d
    NT_FILE note section for reading core target memory".
0efd7d
0efd7d
    When I run this test without any of my recent corefile patches,
0efd7d
    I see these failures:
0efd7d
0efd7d
    FAIL: gdb.base/corefile2.exp: kernel core: print/x mbuf_ro[0]@4
0efd7d
    FAIL: gdb.base/corefile2.exp: kernel core: print/x mbuf_ro[pagesize-4]@4
0efd7d
    FAIL: gdb.base/corefile2.exp: kernel core: print/x mbuf_ro[-3]@6
0efd7d
    FAIL: gdb.base/corefile2.exp: kernel core: print/x mbuf_rw[pagesize-3]@6
0efd7d
    FAIL: gdb.base/corefile2.exp: kernel core: print/x mbuf_ro[pagesize-3]@6
0efd7d
    FAIL: gdb.base/corefile2.exp: maint print core-file-backed-mappings
0efd7d
    FAIL: gdb.base/corefile2.exp: gcore core: print/x mbuf_ro[-3]@6
0efd7d
0efd7d
    The ones involving mbuf_ro will almost certainly fail when run on
0efd7d
    non-Linux systems; I've used setup_xfail on those tests to prevent
0efd7d
    them from outright FAILing when not run on Linux.  For a time, I
0efd7d
    had considered skipping these tests altogether when not run on
0efd7d
    Linux, but I changed my mind due to this failure...
0efd7d
0efd7d
    FAIL: gdb.base/corefile2.exp: print/x mbuf_rw[pagesize-3]@6
0efd7d
0efd7d
    I think it *should* pass without my recent corefile patches.  The fact
0efd7d
    that it doesn't is likely due to a bug in GDB.  The following
0efd7d
    interaction with GDB demonstrates the problem:
0efd7d
0efd7d
    (gdb) print/x mbuf_rw[pagesize-3]@6
0efd7d
    $1 = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
0efd7d
    (gdb) print/x mbuf_rw[pagesize]@3
0efd7d
    $2 = {0x6b, 0x6b, 0x6b}
0efd7d
0efd7d
    The last three values in display of $1 should be the same as those
0efd7d
    shown by $2.  Like this...
0efd7d
0efd7d
    (gdb) print/x mbuf_rw[pagesize-3]@6
0efd7d
    $1 = {0x0, 0x0, 0x0, 0x6b, 0x6b, 0x6b}
0efd7d
    (gdb) print/x mbuf_rw[pagesize]@3
0efd7d
    $2 = {0x6b, 0x6b, 0x6b}
0efd7d
0efd7d
    That latter output was obtained with the use of all of my current
0efd7d
    corefile patches.  I see no failures on Linux when running this test
0efd7d
    with my current set of corefile patches.  I tested 3 architectures:
0efd7d
    x86_64, s390x, and aarch64.
0efd7d
0efd7d
    I also tested on FreeBSD 12.1-RELEASE.  I see the following results
0efd7d
    both with and without the current set of core file patches:
0efd7d
0efd7d
        # of expected passes		26
0efd7d
        # of expected failures		8
0efd7d
0efd7d
    Of particular interest is that I did *not* see the problematic mbuf_rw
0efd7d
    failure noted earlier (both with and without the core file patches).
0efd7d
    I still don't have an explanation for why this failure occurred on
0efd7d
    Linux.  Prior to running the tests, I had hypothesized that I'd see
0efd7d
    this failure on FreeBSD too, but testing shows that this is not the
0efd7d
    case.
0efd7d
0efd7d
    Also of importance is that we see no FAILs with this test on FreeBSD
0efd7d
    which indicates that I XFAILed the correct tests.
0efd7d
0efd7d
    This version runs the interesting tests twice, once with a kernel
0efd7d
    created core file and another time with a gcore created core file.
0efd7d
0efd7d
    It also does a very minimal test of the new command "maint print
0efd7d
    core-file-backed-mappings".
0efd7d
0efd7d
    gdb/testsuite/ChangeLog:
0efd7d
0efd7d
    	* gdb.base/corefile2.exp: New file.
0efd7d
    	* gdb.base/coremaker2.exp: New file.
0efd7d
0efd7d
diff --git a/gdb/testsuite/gdb.base/corefile2.exp b/gdb/testsuite/gdb.base/corefile2.exp
0efd7d
new file mode 100644
0efd7d
--- /dev/null
0efd7d
+++ b/gdb/testsuite/gdb.base/corefile2.exp
0efd7d
@@ -0,0 +1,185 @@
0efd7d
+# Copyright 2020 Free Software Foundation, Inc.
0efd7d
+
0efd7d
+# This program is free software; you can redistribute it and/or modify
0efd7d
+# it under the terms of the GNU General Public License as published by
0efd7d
+# the Free Software Foundation; either version 3 of the License, or
0efd7d
+# (at your option) any later version.
0efd7d
+#
0efd7d
+# This program is distributed in the hope that it will be useful,
0efd7d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
0efd7d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0efd7d
+# GNU General Public License for more details.
0efd7d
+#
0efd7d
+# You should have received a copy of the GNU General Public License
0efd7d
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
0efd7d
+
0efd7d
+# Tests of core file memory accesses when mmap() has been used to
0efd7d
+# create a "hole" of zeroes over pre-existing memory regions.  See
0efd7d
+# coremaker2.c for details.
0efd7d
+
0efd7d
+# are we on a target board
0efd7d
+if ![isnative] then {
0efd7d
+    return
0efd7d
+}
0efd7d
+
0efd7d
+# Some of these tests will only work on GNU/Linux due to the
0efd7d
+# fact that Linux core files includes a section describing
0efd7d
+# memory address to file mappings.  We'll use set_up_xfail for the
0efd7d
+# affected tests.  As other targets become supported, the condition
0efd7d
+# can be changed accordingly.
0efd7d
+
0efd7d
+set xfail 0
0efd7d
+if { ![istarget *-linux*] } {
0efd7d
+    set xfail 1
0efd7d
+}
0efd7d
+
0efd7d
+standard_testfile coremaker2.c
0efd7d
+
0efd7d
+if {[build_executable $testfile.exp $testfile $srcfile debug] == -1} {
0efd7d
+    untested "failed to compile"
0efd7d
+    return -1
0efd7d
+}
0efd7d
+
0efd7d
+set corefile [core_find $binfile {}]
0efd7d
+if {$corefile == ""} {
0efd7d
+    return 0
0efd7d
+}
0efd7d
+
0efd7d
+gdb_start
0efd7d
+gdb_reinitialize_dir $srcdir/$subdir
0efd7d
+gdb_load ${binfile}
0efd7d
+
0efd7d
+# Attempt to load the core file.
0efd7d
+
0efd7d
+gdb_test_multiple "core-file $corefile" "core-file command" {
0efd7d
+    -re ".* program is being debugged already.*y or n. $" {
0efd7d
+	# gdb_load may connect us to a gdbserver.
0efd7d
+	send_gdb "y\n"
0efd7d
+	exp_continue
0efd7d
+    }
0efd7d
+    -re "Core was generated by .*corefile.*\r\n\#0  .*\(\).*\r\n$gdb_prompt $" {
0efd7d
+	pass "core-file command"
0efd7d
+    }
0efd7d
+    -re "Core was generated by .*\r\n\#0  .*\(\).*\r\n$gdb_prompt $" {
0efd7d
+	pass "core-file command (with bad program name)"
0efd7d
+    }
0efd7d
+    -re ".*registers from core file: File in wrong format.* $" {
0efd7d
+	fail "core-file command (could not read registers from core file)"
0efd7d
+    }
0efd7d
+}
0efd7d
+
0efd7d
+# Perform the "interesting" tests which check the contents of certain
0efd7d
+# memory regions.
0efd7d
+
0efd7d
+proc do_tests { } {
0efd7d
+    global xfail
0efd7d
+
0efd7d
+    # Check contents of beginning of buf_rw and buf_ro.
0efd7d
+
0efd7d
+    gdb_test {print/x buf_rw[0]@4} {\{0x6b, 0x6b, 0x6b, 0x6b\}}
0efd7d
+    gdb_test {print/x buf_ro[0]@4} {\{0xc5, 0xc5, 0xc5, 0xc5\}}
0efd7d
+
0efd7d
+    # Check for correct contents at beginning of mbuf_rw and mbuf_ro.
0efd7d
+
0efd7d
+    gdb_test {print/x mbuf_rw[0]@4} {\{0x0, 0x0, 0x0, 0x0\}}
0efd7d
+
0efd7d
+    if { $xfail } { setup_xfail "*-*-*" }
0efd7d
+    gdb_test {print/x mbuf_ro[0]@4} {\{0x0, 0x0, 0x0, 0x0\}}
0efd7d
+
0efd7d
+    # Check contents of mbuf_rw and mbuf_ro at the end of these regions.
0efd7d
+
0efd7d
+    gdb_test {print/x mbuf_rw[pagesize-4]@4} {\{0x0, 0x0, 0x0, 0x0\}}
0efd7d
+
0efd7d
+    if { $xfail } { setup_xfail "*-*-*" }
0efd7d
+    gdb_test {print/x mbuf_ro[pagesize-4]@4} {\{0x0, 0x0, 0x0, 0x0\}}
0efd7d
+
0efd7d
+    # Check contents of mbuf_rw and mbuf_ro, right before the hole,
0efd7d
+    # overlapping into the beginning of these mmap'd regions.
0efd7d
+
0efd7d
+    gdb_test {print/x mbuf_rw[-3]@6} {\{0x6b, 0x6b, 0x6b, 0x0, 0x0, 0x0\}}
0efd7d
+
0efd7d
+    if { $xfail } { setup_xfail "*-*-*" }
0efd7d
+    gdb_test {print/x mbuf_ro[-3]@6} {\{0xc5, 0xc5, 0xc5, 0x0, 0x0, 0x0\}}
0efd7d
+
0efd7d
+    # Likewise, at the end of the mbuf_rw and mbuf_ro, with overlap.
0efd7d
+
0efd7d
+    # If this test FAILs, it's probably a genuine bug unrelated to whether
0efd7d
+    # the core file includes a section describing memory address to file
0efd7d
+    # mappings or not.  (So don't xfail it!)
0efd7d
+    gdb_test {print/x mbuf_rw[pagesize-3]@6} {\{0x0, 0x0, 0x0, 0x6b, 0x6b, 0x6b\}}
0efd7d
+
0efd7d
+    if { $xfail } { setup_xfail "*-*-*" }
0efd7d
+    gdb_test {print/x mbuf_ro[pagesize-3]@6} {\{0x0, 0x0, 0x0, 0xc5, 0xc5, 0xc5\}}
0efd7d
+
0efd7d
+    # Check contents of (what should be) buf_rw and buf_ro immediately after
0efd7d
+    # mbuf_rw and mbuf_ro holes.
0efd7d
+
0efd7d
+    gdb_test {print/x mbuf_rw[pagesize]@4} {\{0x6b, 0x6b, 0x6b, 0x6b\}}
0efd7d
+    gdb_test {print/x mbuf_ro[pagesize]@4} {\{0xc5, 0xc5, 0xc5, 0xc5\}}
0efd7d
+
0efd7d
+    # Check contents at ends of buf_rw and buf_rw.
0efd7d
+
0efd7d
+    gdb_test {print/x buf_rw[sizeof(buf_rw)-4]@4} {\{0x6b, 0x6b, 0x6b, 0x6b\}}
0efd7d
+    gdb_test {print/x buf_ro[sizeof(buf_ro)-4]@4} {\{0xc5, 0xc5, 0xc5, 0xc5\}}
0efd7d
+}
0efd7d
+
0efd7d
+# Run tests with kernel-produced core file.
0efd7d
+
0efd7d
+with_test_prefix "kernel core" {
0efd7d
+    do_tests
0efd7d
+}
0efd7d
+
0efd7d
+# Verify that "maint print core-file-backed-mappings" exists and does
0efd7d
+# not crash GDB.  If it produces any output at all, make sure that
0efd7d
+# that output at least mentions binfile.
0efd7d
+
0efd7d
+set test "maint print core-file-backed-mappings"
0efd7d
+gdb_test_multiple $test "" {
0efd7d
+    -re ".*$binfile.*$gdb_prompt $" {
0efd7d
+	pass $test
0efd7d
+    }
0efd7d
+    -re "^$test\[\r\n\]*$gdb_prompt $" {
0efd7d
+	pass "$test (no output)"
0efd7d
+    }
0efd7d
+}
0efd7d
+
0efd7d
+# Restart and run to the abort call.
0efd7d
+
0efd7d
+clean_restart $binfile
0efd7d
+
0efd7d
+if ![runto_main] then {
0efd7d
+    fail "can't run to main"
0efd7d
+    return
0efd7d
+}
0efd7d
+
0efd7d
+gdb_breakpoint [gdb_get_line_number "abort"]
0efd7d
+gdb_continue_to_breakpoint "at abort"
0efd7d
+
0efd7d
+# Do not execute abort call; instead, invoke gcore command to make a
0efd7d
+# gdb-produced core file.
0efd7d
+
0efd7d
+set corefile [standard_output_file gcore.test]
0efd7d
+set core_supported [gdb_gcore_cmd "$corefile" "save a corefile"]
0efd7d
+if {!$core_supported} {
0efd7d
+  return
0efd7d
+}
0efd7d
+
0efd7d
+# maint print-core-file-backed-mappings shouldn't produce any output
0efd7d
+# when not debugging a core file.
0efd7d
+
0efd7d
+gdb_test_no_output "maint print core-file-backed-mappings" \
0efd7d
+    "maint print core-file-backed-mapping with no core file"
0efd7d
+
0efd7d
+clean_restart $binfile
0efd7d
+
0efd7d
+set core_loaded [gdb_core_cmd "$corefile" "re-load generated corefile"]
0efd7d
+if { $core_loaded == -1 } {
0efd7d
+    # No use proceeding from here.
0efd7d
+    return
0efd7d
+}
0efd7d
+
0efd7d
+# Run tests using gcore-produced core file.
0efd7d
+
0efd7d
+with_test_prefix "gcore core" {
0efd7d
+    do_tests
0efd7d
+}
0efd7d
diff --git a/gdb/testsuite/gdb.base/coremaker2.c b/gdb/testsuite/gdb.base/coremaker2.c
0efd7d
new file mode 100644
0efd7d
--- /dev/null
0efd7d
+++ b/gdb/testsuite/gdb.base/coremaker2.c
0efd7d
@@ -0,0 +1,150 @@
0efd7d
+/* Copyright 1992-2020 Free Software Foundation, Inc.
0efd7d
+
0efd7d
+   This file is part of GDB.
0efd7d
+
0efd7d
+   This program is free software; you can redistribute it and/or modify
0efd7d
+   it under the terms of the GNU General Public License as published by
0efd7d
+   the Free Software Foundation; either version 3 of the License, or
0efd7d
+   (at your option) any later version.
0efd7d
+
0efd7d
+   This program is distributed in the hope that it will be useful,
0efd7d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
0efd7d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0efd7d
+   GNU General Public License for more details.
0efd7d
+
0efd7d
+   You should have received a copy of the GNU General Public License
0efd7d
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
0efd7d
+
0efd7d
+/*  This test has two large memory areas buf_rw and buf_ro. 
0efd7d
+
0efd7d
+    buf_rw is written to by the program while buf_ro is initialized at
0efd7d
+    compile / load time.  Thus, when a core file is created, buf_rw's
0efd7d
+    memory should reside in the core file, but buf_ro probably won't be.
0efd7d
+    Instead, the contents of buf_ro are available from the executable.
0efd7d
+
0efd7d
+    Now, for the wrinkle:  We create a one page read-only mapping over
0efd7d
+    both of these areas.  This will create a one page "hole" of all
0efd7d
+    zeros in each area.
0efd7d
+
0efd7d
+    Will GDB be able to correctly read memory from each of the four
0efd7d
+    (or six, if you count the regions on the other side of each hole)
0efd7d
+    memory regions?  */
0efd7d
+
0efd7d
+#include <stdio.h>
0efd7d
+#include <sys/types.h>
0efd7d
+#include <fcntl.h>
0efd7d
+#include <sys/mman.h>
0efd7d
+#include <signal.h>
0efd7d
+#include <stdlib.h>
0efd7d
+#include <unistd.h>
0efd7d
+#include <string.h>
0efd7d
+#include <errno.h>
0efd7d
+
0efd7d
+/* These are globals so that we can find them easily when debugging
0efd7d
+   the core file.  */
0efd7d
+long pagesize;
0efd7d
+unsigned long long addr;
0efd7d
+char *mbuf_ro;
0efd7d
+char *mbuf_rw;
0efd7d
+
0efd7d
+/* 24 KiB buffer.  */
0efd7d
+char buf_rw[24 * 1024];
0efd7d
+
0efd7d
+/* 24 KiB worth of data.  For this test case, we can't allocate a
0efd7d
+   buffer and then fill it; we want GDB to have to read this data
0efd7d
+   from the executable; it should NOT find it in the core file.  */
0efd7d
+
0efd7d
+#define C5_16 \
0efd7d
+  0xc5, 0xc5, 0xc5, 0xc5, \
0efd7d
+  0xc5, 0xc5, 0xc5, 0xc5, \
0efd7d
+  0xc5, 0xc5, 0xc5, 0xc5, \
0efd7d
+  0xc5, 0xc5, 0xc5, 0xc5
0efd7d
+
0efd7d
+#define C5_256 \
0efd7d
+  C5_16, C5_16, C5_16, C5_16, \
0efd7d
+  C5_16, C5_16, C5_16, C5_16, \
0efd7d
+  C5_16, C5_16, C5_16, C5_16, \
0efd7d
+  C5_16, C5_16, C5_16, C5_16
0efd7d
+
0efd7d
+#define C5_1k \
0efd7d
+  C5_256, C5_256, C5_256, C5_256
0efd7d
+
0efd7d
+#define C5_24k \
0efd7d
+  C5_1k, C5_1k, C5_1k, C5_1k, \
0efd7d
+  C5_1k, C5_1k, C5_1k, C5_1k, \
0efd7d
+  C5_1k, C5_1k, C5_1k, C5_1k, \
0efd7d
+  C5_1k, C5_1k, C5_1k, C5_1k, \
0efd7d
+  C5_1k, C5_1k, C5_1k, C5_1k, \
0efd7d
+  C5_1k, C5_1k, C5_1k, C5_1k
0efd7d
+
0efd7d
+const char buf_ro[] = { C5_24k };
0efd7d
+
0efd7d
+int
0efd7d
+main (int argc, char **argv)
0efd7d
+{
0efd7d
+  int i, bitcount;
0efd7d
+
0efd7d
+#ifdef _SC_PAGESIZE
0efd7d
+  pagesize = sysconf (_SC_PAGESIZE);
0efd7d
+#else
0efd7d
+  pagesize = 8192;
0efd7d
+#endif
0efd7d
+
0efd7d
+  /* Verify that pagesize is a power of 2.  */
0efd7d
+  bitcount = 0;
0efd7d
+  for (i = 0; i < 4 * sizeof (pagesize); i++)
0efd7d
+    if (pagesize & (1 << i))
0efd7d
+      bitcount++;
0efd7d
+
0efd7d
+  if (bitcount != 1)
0efd7d
+    {
0efd7d
+      fprintf (stderr, "pagesize is not a power of 2.\n");
0efd7d
+      exit (1);
0efd7d
+    }
0efd7d
+
0efd7d
+  /* Compute an address that should be within buf_ro.  Complain if not.  */
0efd7d
+  addr = ((unsigned long long) buf_ro + pagesize) & ~(pagesize - 1);
0efd7d
+
0efd7d
+  if (addr <= (unsigned long long) buf_ro
0efd7d
+      || addr >= (unsigned long long) buf_ro + sizeof (buf_ro))
0efd7d
+    {
0efd7d
+      fprintf (stderr, "Unable to compute a suitable address within buf_ro.\n");
0efd7d
+      exit (1);
0efd7d
+    }
0efd7d
+
0efd7d
+  mbuf_ro = mmap ((void *) addr, pagesize, PROT_READ,
0efd7d
+               MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
0efd7d
+
0efd7d
+  if (mbuf_ro == MAP_FAILED)
0efd7d
+    {
0efd7d
+      fprintf (stderr, "mmap #1 failed: %s.\n", strerror (errno));
0efd7d
+      exit (1);
0efd7d
+    }
0efd7d
+
0efd7d
+  /* Write (and fill) the R/W region.  */
0efd7d
+  for (i = 0; i < sizeof (buf_rw); i++)
0efd7d
+    buf_rw[i] = 0x6b;
0efd7d
+
0efd7d
+  /* Compute an mmap address within buf_rw.  Complain if it's somewhere
0efd7d
+     else.  */
0efd7d
+  addr = ((unsigned long long) buf_rw + pagesize) & ~(pagesize - 1);
0efd7d
+
0efd7d
+  if (addr <= (unsigned long long) buf_rw
0efd7d
+      || addr >= (unsigned long long) buf_rw + sizeof (buf_rw))
0efd7d
+    {
0efd7d
+      fprintf (stderr, "Unable to compute a suitable address within buf_rw.\n");
0efd7d
+      exit (1);
0efd7d
+    }
0efd7d
+
0efd7d
+  mbuf_rw = mmap ((void *) addr, pagesize, PROT_READ,
0efd7d
+               MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
0efd7d
+
0efd7d
+  if (mbuf_rw == MAP_FAILED)
0efd7d
+    {
0efd7d
+      fprintf (stderr, "mmap #2 failed: %s.\n", strerror (errno));
0efd7d
+      exit (1);
0efd7d
+    }
0efd7d
+
0efd7d
+  /* With correct ulimit, etc. this should cause a core dump.  */
0efd7d
+  abort ();
0efd7d
+}