[v3,2/2,gdb/testsuite] Add gdb.dwarf2/dw2-epilogue-begin-2.exp

Message ID 20240405151012.14763-2-tdevries@suse.de
State Superseded
Headers
Series [v3,1/2,gdb/symtab] Fix an out of bounds array access in find_epilogue_using_linetable |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed

Commit Message

Tom de Vries April 5, 2024, 3:10 p.m. UTC
  Test-case gdb.dwarf2/dw2-epilogue-begin.exp has an end_sequence at 0x4004ff:
...
File name                Line number    Starting address    View    Stmt

dw2-epilogue-begin.c              20            0x4004b7               x
dw2-epilogue-begin.c              27            0x4004be               x
dw2-epilogue-begin.c              34            0x4004d0               x
dw2-epilogue-begin.c              37            0x4004de               x
dw2-epilogue-begin.c              38            0x4004ec               x
dw2-epilogue-begin.c              43            0x4004ef               x
dw2-epilogue-begin.c              47            0x4004fa               x
dw2-epilogue-begin.c              50            0x4004ff               x
dw2-epilogue-begin.c               -            0x4004ff
...
which is before the actual exclusive end of the main function at 0x40050d:
...
00000000004004ff <main_epilogue>:
  4004ff:       c6 05 1b 1b 00 00 0a    movb   $0xa,0x1b1b(%rip)
  400506:       b8 00 00 00 00          mov    $0x0,%eax
  40050b:       5d                      pop    %rbp
  40050c:       c3                      ret
...

This triggers the corner case in find_epilogue_using_linetable that
the call to std::lower_bound returns the "not found" case.

However, if we handle the corner case explicitly by returning something
incorrect:
...
+      if (it == linetable->item + linetable->nitems)
+	return {};
...
the test-case still passes.

Fix this by:
- rearranging the test-case to move the watch function to after the main
  function, and
- reworking the test-case into two variants:
  - end_sequence at the watch function end (the correct version), and
  - end_sequence before the watch function end (triggered the corner-case).

Tested on x86_64-linux.
---
 .../gdb.dwarf2/dw2-epilogue-begin-2.exp       |  20 ++
 gdb/testsuite/gdb.dwarf2/dw2-epilogue-begin.c |  28 +--
 .../gdb.dwarf2/dw2-epilogue-begin.exp         | 156 +-------------
 .../gdb.dwarf2/dw2-epilogue-begin.exp.tcl     | 191 ++++++++++++++++++
 4 files changed, 229 insertions(+), 166 deletions(-)
 create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-epilogue-begin-2.exp
 create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-epilogue-begin.exp.tcl
  

Patch

diff --git a/gdb/testsuite/gdb.dwarf2/dw2-epilogue-begin-2.exp b/gdb/testsuite/gdb.dwarf2/dw2-epilogue-begin-2.exp
new file mode 100644
index 00000000000..64cd85644e0
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/dw2-epilogue-begin-2.exp
@@ -0,0 +1,20 @@ 
+# Copyright 2022-2024 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+standard_testfile dw2-epilogue-begin.c dw2-epilogue-begin.S
+
+set early_end_sequence 1
+
+source $srcdir/$subdir/dw2-epilogue-begin.exp.tcl
diff --git a/gdb/testsuite/gdb.dwarf2/dw2-epilogue-begin.c b/gdb/testsuite/gdb.dwarf2/dw2-epilogue-begin.c
index 4ff445cf37d..2fcc4807904 100644
--- a/gdb/testsuite/gdb.dwarf2/dw2-epilogue-begin.c
+++ b/gdb/testsuite/gdb.dwarf2/dw2-epilogue-begin.c
@@ -22,6 +22,20 @@  trivial (void)
 
 char global;
 
+extern void watch (void);
+
+int
+main (void)
+{							/* main prologue */
+  asm ("main_label: .global main_label");
+  global = 0;
+  asm ("main_fun_call: .global main_fun_call");
+  watch ();						/* main function call */
+  asm ("main_epilogue: .global main_epilogue");
+  global = 10;
+  return 0;						/* main end */
+}
+
 void
 watch (void)
 {							/* watch start */
@@ -36,16 +50,6 @@  watch (void)
   asm ("mov $0x2, %rax");
   local = 2;						/* watch reassign */
   asm ("watch_end: .global watch_end");			/* watch end */
-}
-
-int
-main (void)
-{							/* main prologue */
-  asm ("main_label: .global main_label");
-  global = 0;
-  asm ("main_fun_call: .global main_fun_call");
-  watch ();						/* main function call */
-  asm ("main_epilogue: .global main_epilogue");
-  global = 10;
-  return 0;						/* main end */
+  local = 3;
+  asm ("watch_early_end_sequence: .global watch_early_end_sequence");
 }
diff --git a/gdb/testsuite/gdb.dwarf2/dw2-epilogue-begin.exp b/gdb/testsuite/gdb.dwarf2/dw2-epilogue-begin.exp
index f646e23da62..9552dd764dd 100644
--- a/gdb/testsuite/gdb.dwarf2/dw2-epilogue-begin.exp
+++ b/gdb/testsuite/gdb.dwarf2/dw2-epilogue-begin.exp
@@ -13,161 +13,9 @@ 
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-# Check that GDB can honor the epilogue_begin flag the compiler can place
-# in the line-table data.
-# We test 2 things: 1. that a software watchpoint triggered in an epilogue
-# is correctly ignored
-# 2. that GDB can mark the same line as both prologue and epilogue
-
-load_lib dwarf.exp
-
-# This test can only be run on targets which support DWARF-2 and use gas.
-require dwarf2_support
-# restricted to x86 to make it simpler to follow a variable
-require is_x86_64_m64_target
-
 standard_testfile .c .S
 
-set trivial_line [gdb_get_line_number "trivial function"]
-set main_prologue [gdb_get_line_number "main prologue"]
-set main_epilogue [gdb_get_line_number "main end"]
-set watch_start_line [gdb_get_line_number "watch start"]
-
-set asm_file [standard_output_file $srcfile2]
-
-# The producer will be set to clang because at the time of writing
-# we only care about epilogues if the producer is clang.  When the
-# producer is GCC, variables use CFA locations, so watchpoints can
-# continue working even on epilogues.
-Dwarf::assemble $asm_file {
-    global srcdir subdir srcfile srcfile2
-    global trivial_line main_prologue main_epilogue watch_start_line
-    declare_labels lines_label
-
-    get_func_info main
-    get_func_info trivial
-    get_func_info watch
-
-    cu {} {
-	compile_unit {
-	    {language @DW_LANG_C}
-	    {name dw2-prologue-end.c}
-	    {stmt_list ${lines_label} DW_FORM_sec_offset}
-	    {producer "clang version 17.0.1"}
-	} {
-	    declare_labels char_label
-
-	    char_label: base_type {
-		{name char}
-		{encoding @DW_ATE_signed}
-		{byte_size 1 DW_FORM_sdata}
-	    }
-
-	    subprogram {
-		{external 1 flag}
-		{name trivial}
-		{low_pc $trivial_start addr}
-		{high_pc "$trivial_start + $trivial_len" addr}
-	    }
-	    subprogram {
-		{external 1 flag}
-		{name watch}
-		{low_pc $watch_start addr}
-		{high_pc "$watch_start + $watch_len" addr}
-	    } {
-		DW_TAG_variable {
-		    {name local}
-		    {type :$char_label}
-		    {DW_AT_location {DW_OP_reg0} SPECIAL_expr}
-		}
-	    }
-	    subprogram {
-		{external 1 flag}
-		{name main}
-		{low_pc $main_start addr}
-		{high_pc "$main_start + $main_len" addr}
-	    }
-	}
-    }
-
-    lines {version 5} lines_label {
-	set diridx [include_dir "${srcdir}/${subdir}"]
-	file_name "$srcfile" $diridx
-
-	program {
-	    DW_LNS_set_file $diridx
-	    DW_LNE_set_address $trivial_start
-	    line $trivial_line
-	    DW_LNS_set_prologue_end
-	    DW_LNS_set_epilogue_begin
-	    DW_LNS_copy
-
-	    DW_LNE_set_address watch
-	    line $watch_start_line
-	    DW_LNS_copy
-
-	    DW_LNE_set_address watch_start
-	    line [gdb_get_line_number "watch assign"]
-	    DW_LNS_set_prologue_end
-	    DW_LNS_copy
-
-	    DW_LNE_set_address watch_reassign
-	    line [gdb_get_line_number "watch reassign"]
-	    DW_LNS_set_epilogue_begin
-	    DW_LNS_copy
-
-	    DW_LNE_set_address watch_end
-	    line [gdb_get_line_number "watch end"]
-	    DW_LNS_copy
-
-	    DW_LNE_set_address $main_start
-	    line $main_prologue
-	    DW_LNS_set_prologue_end
-	    DW_LNS_copy
-
-	    DW_LNE_set_address main_fun_call
-	    line [gdb_get_line_number "main function call"]
-	    DW_LNS_copy
-
-	    DW_LNE_set_address main_epilogue
-	    line $main_epilogue
-	    DW_LNS_set_epilogue_begin
-	    DW_LNS_copy
-
-	    DW_LNE_end_sequence
-	}
-    }
-}
-
-if { [prepare_for_testing "failed to prepare" ${testfile} \
-	  [list $srcfile $asm_file] {nodebug}] } {
-    return -1
-}
-
-if ![runto_main] {
-    return -1
-}
-
-# Moving to the scope with a local variable.
-gdb_breakpoint $watch_start_line
-gdb_continue_to_breakpoint "continuing to function" ".*"
-gdb_test "next" "local = 2.*" "stepping to epilogue"
-
-# Forcing software watchpoints because hardware ones don't care if we
-# are in the epilogue or not.
-gdb_test_no_output "set can-use-hw-watchpoints 0"
+set early_end_sequence 0
 
-# Test that the software watchpoint will not trigger in this case
-gdb_test "watch local" "\[W|w\]atchpoint .: local" "set watchpoint"
-gdb_test "continue" ".*\[W|w\]atchpoint . deleted.*" \
-    "confirm watchpoint doesn't trigger"
+source $srcdir/$subdir/dw2-epilogue-begin.exp.tcl
 
-# First we test that the trivial function has a line with both a prologue
-# and an epilogue. Do this by finding a line that has 3 Y columns
-set sep "\[ \t\]"
-set hex_number "0x\[0-9a-f\]+"
-gdb_test_multiple "maint info line-table" "test epilogue in linetable" -lbl {
-    -re "\[0-9\]$sep+$trivial_line$sep+$hex_number$sep+$hex_number$sep+Y$sep+Y$sep+Y" {
-	pass $gdb_test_name
-    }
-}
diff --git a/gdb/testsuite/gdb.dwarf2/dw2-epilogue-begin.exp.tcl b/gdb/testsuite/gdb.dwarf2/dw2-epilogue-begin.exp.tcl
new file mode 100644
index 00000000000..18282e884e4
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/dw2-epilogue-begin.exp.tcl
@@ -0,0 +1,191 @@ 
+# Copyright 2022-2024 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Check that GDB can honor the epilogue_begin flag the compiler can place
+# in the line-table data.
+# We test 2 things: 1. that a software watchpoint triggered in an epilogue
+# is correctly ignored
+# 2. that GDB can mark the same line as both prologue and epilogue
+
+load_lib dwarf.exp
+
+# This test can only be run on targets which support DWARF-2 and use gas.
+require dwarf2_support
+# restricted to x86 to make it simpler to follow a variable
+require is_x86_64_m64_target
+
+set trivial_line [gdb_get_line_number "trivial function"]
+set main_prologue [gdb_get_line_number "main prologue"]
+set main_epilogue [gdb_get_line_number "main end"]
+set watch_start_line [gdb_get_line_number "watch start"]
+
+set asm_file [standard_output_file $srcfile2]
+
+# The producer will be set to clang because at the time of writing
+# we only care about epilogues if the producer is clang.  When the
+# producer is GCC, variables use CFA locations, so watchpoints can
+# continue working even on epilogues.
+Dwarf::assemble $asm_file {
+    global srcdir subdir srcfile srcfile2
+    global trivial_line main_prologue main_epilogue watch_start_line
+    declare_labels lines_label
+
+    get_func_info main
+    get_func_info trivial
+    get_func_info watch
+
+    if { $::early_end_sequence == 1 } {
+	set watch_end_sequence watch_early_end_sequence
+    } else {
+	set watch_end_sequence $watch_end
+    }
+
+    cu {} {
+	compile_unit {
+	    {language @DW_LANG_C}
+	    {name dw2-prologue-end.c}
+	    {stmt_list ${lines_label} DW_FORM_sec_offset}
+	    {producer "clang version 17.0.1"}
+	} {
+	    declare_labels char_label
+
+	    char_label: base_type {
+		{name char}
+		{encoding @DW_ATE_signed}
+		{byte_size 1 DW_FORM_sdata}
+	    }
+
+	    subprogram {
+		{external 1 flag}
+		{name trivial}
+		{low_pc $trivial_start addr}
+		{high_pc "$trivial_start + $trivial_len" addr}
+	    }
+	    subprogram {
+		{external 1 flag}
+		{name watch}
+		{low_pc $watch_start addr}
+		{high_pc "$watch_start + $watch_len" addr}
+	    } {
+		DW_TAG_variable {
+		    {name local}
+		    {type :$char_label}
+		    {DW_AT_location {DW_OP_reg0} SPECIAL_expr}
+		}
+	    }
+	    subprogram {
+		{external 1 flag}
+		{name main}
+		{low_pc $main_start addr}
+		{high_pc "$main_start + $main_len" addr}
+	    }
+	}
+    }
+
+    lines {version 5} lines_label {
+	set diridx [include_dir "${srcdir}/${subdir}"]
+	file_name "$srcfile" $diridx
+
+	program {
+	    DW_LNS_set_file $diridx
+
+	    DW_LNE_set_address $trivial_start
+	    line $trivial_line
+	    DW_LNS_set_prologue_end
+	    DW_LNS_set_epilogue_begin
+	    DW_LNS_copy
+
+	    DW_LNE_set_address $trivial_end
+	    DW_LNE_end_sequence
+
+
+	    DW_LNS_set_file $diridx
+
+	    DW_LNE_set_address $main_start
+	    line $main_prologue
+	    DW_LNS_set_prologue_end
+	    DW_LNS_copy
+
+	    DW_LNE_set_address main_fun_call
+	    line [gdb_get_line_number "main function call"]
+	    DW_LNS_copy
+
+	    DW_LNE_set_address main_epilogue
+	    line $main_epilogue
+	    DW_LNS_set_epilogue_begin
+	    DW_LNS_copy
+
+	    DW_LNE_set_address $main_end
+	    DW_LNE_end_sequence
+
+
+	    DW_LNS_set_file $diridx
+
+	    DW_LNE_set_address $watch_start
+	    line $watch_start_line
+	    DW_LNS_copy
+
+	    DW_LNE_set_address watch_start
+	    line [gdb_get_line_number "watch assign"]
+	    DW_LNS_set_prologue_end
+	    DW_LNS_copy
+
+	    DW_LNE_set_address watch_reassign
+	    line [gdb_get_line_number "watch reassign"]
+	    DW_LNS_set_epilogue_begin
+	    DW_LNS_copy
+
+	    DW_LNE_set_address watch_end
+	    line [gdb_get_line_number "watch end"]
+	    DW_LNS_copy
+
+	    DW_LNE_set_address $watch_end_sequence
+	    DW_LNE_end_sequence
+	}
+    }
+}
+
+if { [prepare_for_testing "failed to prepare" ${testfile} \
+	  [list $srcfile $asm_file] {nodebug}] } {
+    return -1
+}
+
+if ![runto_main] {
+    return -1
+}
+
+# Moving to the scope with a local variable.
+gdb_breakpoint $watch_start_line
+gdb_continue_to_breakpoint "continuing to function" ".*"
+gdb_test "next" "local = 2.*" "stepping to epilogue"
+
+# Forcing software watchpoints because hardware ones don't care if we
+# are in the epilogue or not.
+gdb_test_no_output "set can-use-hw-watchpoints 0"
+
+# Test that the software watchpoint will not trigger in this case
+gdb_test "watch local" "\[W|w\]atchpoint .: local" "set watchpoint"
+gdb_test "continue" ".*\[W|w\]atchpoint . deleted.*" \
+    "confirm watchpoint doesn't trigger"
+
+# First we test that the trivial function has a line with both a prologue
+# and an epilogue. Do this by finding a line that has 3 Y columns
+set sep "\[ \t\]"
+set hex_number "0x\[0-9a-f\]+"
+gdb_test_multiple "maint info line-table" "test epilogue in linetable" -lbl {
+    -re "\[0-9\]$sep+$trivial_line$sep+$hex_number$sep+$hex_number$sep+Y$sep+Y$sep+Y" {
+	pass $gdb_test_name
+    }
+}