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

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