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

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