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

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