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

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