public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] Fix handling of null stap semaphores
@ 2020-01-10 20:00 Simon Marchi
  0 siblings, 0 replies; only message in thread
From: Simon Marchi @ 2020-01-10 20:00 UTC (permalink / raw)
  To: gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=7f0ae84c80aae6fe8f7573343fe1ab455d18b5d8

commit 7f0ae84c80aae6fe8f7573343fe1ab455d18b5d8
Author: George Barrett <bob@bob131.so>
Date:   Sat Jan 11 06:30:28 2020 +1100

    Fix handling of null stap semaphores
    
    According to the SystemTap documentation on user-space probes[0], stap
    probe points without semaphores are denoted by setting the semaphore
    address in the probe's note to zero. At present the code does do a
    comparison of the semaphore address against zero, but only after it's
    been relocated; as such it will (almost?) always fail, commonly
    resulting in GDB trying to overwrite the ELF magic located at the
    image's base address.
    
    This commit tests the address as specified in the SDT note rather than
    the relocated value in order to correctly detect absent probe
    semaphores.
    
    [0]: https://sourceware.org/systemtap/wiki/UserSpaceProbeImplementation
    
    gdb/Changelog:
    2020-01-11  George Barrett  <bob@bob131.so>
    
    	* stap-probe.c (stap_modify_semaphore): Don't check for null
    	semaphores.
    	(stap_probe::set_semaphore, stap_probe::clear_semaphore): Check
    	for null semaphores.
    
    gdb/testsuite/ChangeLog:
    2020-01-11  George Barrett  <bob@bob131.so>
    
    	* gdb.base/stap-probe.c (relocation_marker): Add dummy variable
    	to help in finding the image relocation offset.
    	* gdb.base/stap-probe.exp (stap_test): Accept arbitrary compile
    	options in arguments.
    	(stap_test_no_debuginfo): Likewise.
    	(stap-probe-nosem-noopt-pie, stap-probe-nosem-noopt-nopie): Add
    	test variants.
    	(stap_test): Add null semaphore relocation test.

Diff:
---
 gdb/ChangeLog                         |  7 +++++++
 gdb/stap-probe.c                      |  7 ++++---
 gdb/testsuite/ChangeLog               | 11 ++++++++++
 gdb/testsuite/gdb.base/stap-probe.c   |  2 ++
 gdb/testsuite/gdb.base/stap-probe.exp | 38 ++++++++++++++++++++++++++++-------
 5 files changed, 55 insertions(+), 10 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 425bafa..0e5c8a9 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,10 @@
+2020-01-10  George Barrett  <bob@bob131.so>
+
+	* stap-probe.c (stap_modify_semaphore): Don't check for null
+	semaphores.
+	(stap_probe::set_semaphore, stap_probe::clear_semaphore): Check
+	for null semaphores.
+
 2020-01-09  Andrew Burgess  <andrew.burgess@embecosm.com>
 
 	* tui/tui-source.c (tui_source_window::do_scroll_vertical): Update
diff --git a/gdb/stap-probe.c b/gdb/stap-probe.c
index 5e9a043..50f6d51 100644
--- a/gdb/stap-probe.c
+++ b/gdb/stap-probe.c
@@ -1430,9 +1430,6 @@ stap_modify_semaphore (CORE_ADDR address, int set, struct gdbarch *gdbarch)
   struct type *type = builtin_type (gdbarch)->builtin_unsigned_short;
   ULONGEST value;
 
-  if (address == 0)
-    return;
-
   /* Swallow errors.  */
   if (target_read_memory (address, bytes, TYPE_LENGTH (type)) != 0)
     {
@@ -1466,6 +1463,8 @@ stap_modify_semaphore (CORE_ADDR address, int set, struct gdbarch *gdbarch)
 void
 stap_probe::set_semaphore (struct objfile *objfile, struct gdbarch *gdbarch)
 {
+  if (m_sem_addr == 0)
+    return;
   stap_modify_semaphore (relocate_address (m_sem_addr, objfile), 1, gdbarch);
 }
 
@@ -1474,6 +1473,8 @@ stap_probe::set_semaphore (struct objfile *objfile, struct gdbarch *gdbarch)
 void
 stap_probe::clear_semaphore (struct objfile *objfile, struct gdbarch *gdbarch)
 {
+  if (m_sem_addr == 0)
+    return;
   stap_modify_semaphore (relocate_address (m_sem_addr, objfile), 0, gdbarch);
 }
 
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index 84fa9ae..29d6af2 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,5 +1,16 @@
 2020-01-10  George Barrett  <bob@bob131.so>
 
+	* gdb.base/stap-probe.c (relocation_marker): Add dummy variable
+	to help in finding the image relocation offset.
+	* gdb.base/stap-probe.exp (stap_test): Accept arbitrary compile
+	options in arguments.
+	(stap_test_no_debuginfo): Likewise.
+	(stap-probe-nosem-noopt-pie, stap-probe-nosem-noopt-nopie): Add
+	test variants.
+	(stap_test): Add null semaphore relocation test.
+
+2020-01-10  George Barrett  <bob@bob131.so>
+
 	* gdb.base/stap-probe.c: Rename USE_PROBES to USE_SEMAPHORES.
 	* gdb.base/stap-probe.exp: Likewise.
 	(stap_test): Pass argument as an additional flag.
diff --git a/gdb/testsuite/gdb.base/stap-probe.c b/gdb/testsuite/gdb.base/stap-probe.c
index 3d742d1..5cb5494 100644
--- a/gdb/testsuite/gdb.base/stap-probe.c
+++ b/gdb/testsuite/gdb.base/stap-probe.c
@@ -31,6 +31,8 @@ __extension__ unsigned short test_pstr_semaphore __attribute__ ((unused)) __attr
 __extension__ unsigned short test_ps_semaphore __attribute__ ((unused)) __attribute__ ((section (".probes")));
 #else
 
+int relocation_marker __attribute__ ((unused));
+
 #define TEST 1
 #define TEST2 1
 
diff --git a/gdb/testsuite/gdb.base/stap-probe.exp b/gdb/testsuite/gdb.base/stap-probe.exp
index fce5286..398bc89 100644
--- a/gdb/testsuite/gdb.base/stap-probe.exp
+++ b/gdb/testsuite/gdb.base/stap-probe.exp
@@ -18,14 +18,20 @@ standard_testfile
 # Run the tests.  We run the tests two different ways: once with a
 # plain probe, and once with a probe that has an associated semaphore.
 # This returns -1 on failure to compile or start, 0 otherwise.
-proc stap_test {exec_name {arg ""}} {
+proc stap_test {exec_name {args ""}} {
     global testfile hex srcfile
 
     if {[prepare_for_testing "failed to prepare" ${exec_name} $srcfile \
-	   [concat additional_flags=$arg debug]]} {
+	   [concat $args debug]]} {
 	return -1
     }
 
+    set semaphore_addr_var ""
+    if {[string first "-DUSE_SEMAPHORES" $args] == -1} {
+	gdb_test_no_output "set breakpoint always-inserted on"
+	set semaphore_addr_var [get_hexadecimal_valueof "&relocation_marker" "0"]
+    }
+
     if ![runto_main] {
 	return -1
     }
@@ -33,7 +39,7 @@ proc stap_test {exec_name {arg ""}} {
     gdb_test "print \$_probe_argc" "No probe at PC $hex" \
 	"check argument not at probe point"
 
-    if {[string first "-DUSE_SEMAPHORES" $arg] != -1} {
+    if {[string first "-DUSE_SEMAPHORES" $args] != -1} {
 	gdb_test "info probes stap" \
 	    "test *user *$hex *$hex .*"
     } else {
@@ -47,6 +53,20 @@ proc stap_test {exec_name {arg ""}} {
 	fail "run to -pstap test:user"
     }
 
+    if {[string first "-DUSE_SEMAPHORES" $args] == -1} {
+	set relocation_base \
+	   [expr [get_hexadecimal_valueof "&relocation_marker" "0"] - $semaphore_addr_var]
+	if {$relocation_base != 0} {
+	   # Checks that GDB doesn't mistakenly relocate and write to null
+	   # semaphore addresses.  If it were to relocate a zero-valued
+	   # semaphore address and increment the value at that address, we
+	   # would expect to see "\200ELF" here instead.
+	   gdb_test "p (*(char*) $relocation_base)@4" \
+		" = \"\\\\177ELF\"" \
+		"null semaphore relocation"
+	}
+    }
+
     # Test probe arguments.
     gdb_test "print \$_probe_argc" " = 1" \
     "print \$_probe_argc for probe user"
@@ -97,11 +117,11 @@ proc stap_test {exec_name {arg ""}} {
     return 0
 }
 
-proc stap_test_no_debuginfo {exec_name {arg ""}} {
+proc stap_test_no_debuginfo {exec_name {args ""}} {
     global testfile hex
 
     if {[prepare_for_testing "failed to prepare" ${exec_name} ${testfile}.c \
-	   [concat additional_flags=$arg nodebug optimize=-O2]]} {
+	   [concat $args nodebug optimize=-O2]]} {
 	return -1
     }
 
@@ -168,10 +188,14 @@ with_test_prefix "without semaphore, not optimized" {
 	untested "stap probe test failed"
 	  return -1
     }
+
+    foreach_with_prefix pie { "nopie" "pie" } {
+	stap_test "stap-probe-nosem-noopt-$pie" $pie
+    }
 }
 
 with_test_prefix "with semaphore, not optimized" {
-    stap_test "stap-probe-sem-noopt" "-DUSE_SEMAPHORES"
+    stap_test "stap-probe-sem-noopt" additional_flags=-DUSE_SEMAPHORES
 }
 
 with_test_prefix "without semaphore, optimized" {
@@ -179,5 +203,5 @@ with_test_prefix "without semaphore, optimized" {
 }
 
 with_test_prefix "with semaphore, optimized" {
-    stap_test_no_debuginfo "stap-probe-sem-opt" "-DUSE_SEMAPHORES"
+    stap_test_no_debuginfo "stap-probe-sem-opt" additional_flags=-DUSE_SEMAPHORES
 }


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-01-10 20:00 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-10 20:00 [binutils-gdb] Fix handling of null stap semaphores Simon Marchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).