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

0efd7d
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
0efd7d
From: Keith Seitz <keiths@redhat.com>
0efd7d
Date: Mon, 27 Jul 2020 19:21:54 -0400
0efd7d
Subject: gdb-rhbz1842691-corefile-mem-access-9of15.patch
0efd7d
0efd7d
;; Add test for accessing read-only mmapped data in a core file
0efd7d
;; Kevin Buettner, RH BZ 1842691
0efd7d
0efd7d
   Author: Kevin Buettner <kevinb@redhat.com>
0efd7d
   Date:   Tue Jun 16 11:39:22 2020 -0700
0efd7d
0efd7d
    Add test for accessing read-only mmapped data in a core file
0efd7d
0efd7d
    This test passes when run using a GDB with my corefile patches.  When
0efd7d
    run against a GDB without my patches, I see the following failures,
0efd7d
    the first of which is due to the test added by this commit:
0efd7d
0efd7d
    FAIL: gdb.base/corefile.exp: accessing read-only mmapped data in core file (
0efd7d
    FAIL: gdb.base/corefile.exp: accessing anonymous, unwritten-to mmap data
0efd7d
0efd7d
    gdb/testsuite/ChangeLog:
0efd7d
0efd7d
        * gdb.base/corefile.exp: Add test "accessing read-only mmapped
0efd7d
        data in core file".
0efd7d
        * gdb.base/coremaker.c (buf2ro): New global.
0efd7d
        (mmapdata): Add a read-only mmap mapping.
0efd7d
0efd7d
diff --git a/gdb/testsuite/gdb.base/corefile.exp b/gdb/testsuite/gdb.base/corefile.exp
0efd7d
--- a/gdb/testsuite/gdb.base/corefile.exp
0efd7d
+++ b/gdb/testsuite/gdb.base/corefile.exp
0efd7d
@@ -34,7 +34,10 @@ if {[build_executable $testfile.exp $testfile $srcfile debug] == -1} {
0efd7d
     return -1
0efd7d
 }
0efd7d
 
0efd7d
-set corefile [core_find $binfile {coremmap.data}]
0efd7d
+# Do not delete coremap.data when calling core_find.  This file is
0efd7d
+# required for GDB to find mmap'd data in the "accessing read-only
0efd7d
+# mmapped data in core file" test.
0efd7d
+set corefile [core_find $binfile {}]
0efd7d
 if {$corefile == ""} {
0efd7d
     return 0
0efd7d
 }
0efd7d
@@ -175,6 +178,19 @@ gdb_test_multiple "x/8bd buf2" "$test" {
0efd7d
     }
0efd7d
 }
0efd7d
 
0efd7d
+set test "accessing read-only mmapped data in core file"
0efd7d
+gdb_test_multiple "x/8bd buf2ro" "$test" {
0efd7d
+    -re ".*:.*0.*1.*2.*3.*4.*5.*6.*7.*$gdb_prompt $" {
0efd7d
+	pass "$test"
0efd7d
+    }
0efd7d
+    -re "0x\[f\]*:.*Cannot access memory at address 0x\[f\]*.*$gdb_prompt $" {
0efd7d
+	fail "$test (mapping failed at runtime)"
0efd7d
+    }
0efd7d
+    -re "0x.*:.*Cannot access memory at address 0x.*$gdb_prompt $" {
0efd7d
+	fail "$test (mapping address not found in core file)"
0efd7d
+    }
0efd7d
+}
0efd7d
+
0efd7d
 # Test ability to read anonymous and, more importantly, unwritten-to
0efd7d
 # mmap'd data.
0efd7d
 
0efd7d
diff --git a/gdb/testsuite/gdb.base/coremaker.c b/gdb/testsuite/gdb.base/coremaker.c
0efd7d
--- a/gdb/testsuite/gdb.base/coremaker.c
0efd7d
+++ b/gdb/testsuite/gdb.base/coremaker.c
0efd7d
@@ -38,6 +38,7 @@
0efd7d
 
0efd7d
 char *buf1;
0efd7d
 char *buf2;
0efd7d
+char *buf2ro;
0efd7d
 char *buf3;
0efd7d
 
0efd7d
 int coremaker_data = 1;	/* In Data section */
0efd7d
@@ -90,16 +91,25 @@ mmapdata ()
0efd7d
       return;
0efd7d
     }
0efd7d
 
0efd7d
+  /* Map in another copy, read-only.  We won't write to this copy so it
0efd7d
+     will likely not end up in the core file.  */
0efd7d
+  buf2ro = (char *) mmap (0, MAPSIZE, PROT_READ, MAP_PRIVATE, fd, 0);
0efd7d
+  if (buf2ro == (char *) -1)
0efd7d
+    {
0efd7d
+      perror ("mmap failed");
0efd7d
+      return;
0efd7d
+    }
0efd7d
+
0efd7d
   /* Verify that the original data and the mapped data are identical.
0efd7d
      If not, we'd rather fail now than when trying to access the mapped
0efd7d
      data from the core file. */
0efd7d
 
0efd7d
   for (j = 0; j < MAPSIZE; ++j)
0efd7d
     {
0efd7d
-      if (buf1[j] != buf2[j])
0efd7d
+      if (buf1[j] != buf2[j] || buf1[j] != buf2ro[j])
0efd7d
 	{
0efd7d
 	  fprintf (stderr, "mapped data is incorrect");
0efd7d
-	  buf2 = (char *) -1;
0efd7d
+	  buf2 = buf2ro = (char *) -1;
0efd7d
 	  return;
0efd7d
 	}
0efd7d
     }