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

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