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

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