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

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