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

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