From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1726) id C4923383B7AC; Mon, 25 Jul 2022 18:27:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C4923383B7AC Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Andrew Burgess To: gdb-cvs@sourceware.org Subject: [binutils-gdb] gdb/python: fix invalid use disassemble_info::stream X-Act-Checkin: binutils-gdb X-Git-Author: Andrew Burgess X-Git-Refname: refs/heads/master X-Git-Oldrev: 554128418b4f1328bb956d5926f24bfd56fd45ea X-Git-Newrev: 23aa2befce75966acd388b810e139922857533fa Message-Id: <20220725182702.C4923383B7AC@sourceware.org> Date: Mon, 25 Jul 2022 18:27:02 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 25 Jul 2022 18:27:02 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D23aa2befce75= 966acd388b810e139922857533fa commit 23aa2befce75966acd388b810e139922857533fa Author: Andrew Burgess Date: Wed Jul 20 13:57:08 2022 +0100 gdb/python: fix invalid use disassemble_info::stream =20 After this commit: =20 commit 81384924cdcc9eb2676dd9084b76845d7d0e0759 Date: Tue Apr 5 11:06:16 2022 +0100 =20 gdb: have gdb_disassemble_info carry 'this' in its stream pointer =20 The disassemble_info::stream field will no longer be a ui_file*. That commit failed to update one location in py-disasm.c though. =20 While running some tests using the Python disassembler API, I triggered a call to gdbpy_disassembler::print_address_func, and, as I had compiled GDB with the undefined behaviour sanitizer, GDB crashed as the code currently (incorrectly) casts the stream field to be a ui_file*. =20 In this commit I fix this error. =20 In order to test this case I had to tweak the existing test case a little. I also spotted some debug printf statements in py-disasm.py, which I have removed. Diff: --- gdb/python/py-disasm.c | 2 +- gdb/testsuite/gdb.python/py-disasm.c | 8 +++++++- gdb/testsuite/gdb.python/py-disasm.exp | 22 ++++++++++++++-------- gdb/testsuite/gdb.python/py-disasm.py | 3 --- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/gdb/python/py-disasm.c b/gdb/python/py-disasm.c index 4c78ca350c2..c37452fcf72 100644 --- a/gdb/python/py-disasm.c +++ b/gdb/python/py-disasm.c @@ -626,7 +626,7 @@ gdbpy_disassembler::print_address_func (bfd_vma addr, { gdbpy_disassembler *dis =3D static_cast (info->application_data); - print_address (dis->arch (), addr, (struct ui_file *) info->stream); + print_address (dis->arch (), addr, dis->stream ()); } =20 /* constructor. */ diff --git a/gdb/testsuite/gdb.python/py-disasm.c b/gdb/testsuite/gdb.pytho= n/py-disasm.c index ee0bb157f4d..e5c4d2f1d0e 100644 --- a/gdb/testsuite/gdb.python/py-disasm.c +++ b/gdb/testsuite/gdb.python/py-disasm.c @@ -16,10 +16,16 @@ along with this program. If not, see . = */ =20 int -main () +test () { asm ("nop"); asm ("nop"); /* Break here. */ asm ("nop"); return 0; } + +int +main () +{ + return test (); +} diff --git a/gdb/testsuite/gdb.python/py-disasm.exp b/gdb/testsuite/gdb.pyt= hon/py-disasm.exp index 1b9cd4465ac..1f94d3e60f3 100644 --- a/gdb/testsuite/gdb.python/py-disasm.exp +++ b/gdb/testsuite/gdb.python/py-disasm.exp @@ -110,8 +110,8 @@ foreach plan $test_plans { gdb_test_no_output "python add_global_disassembler($global_disassembl= er_name)" } =20 - # Disassemble main, and check the disassembler output. - gdb_test "disassemble main" $expected_pattern + # Disassemble test, and check the disassembler output. + gdb_test "disassemble test" $expected_pattern } } =20 @@ -138,21 +138,21 @@ with_test_prefix "DisassemblerResult errors" { with_test_prefix "GLOBAL tagging disassembler" { py_remove_all_disassemblers gdb_test_no_output "python gdb.disassembler.register_disassembler(Tagg= ingDisassembler(\"GLOBAL\"), None)" - gdb_test "disassemble main" "${base_pattern}\\s+## tag =3D GLOBAL\r\n.= *" + gdb_test "disassemble test" "${base_pattern}\\s+## tag =3D GLOBAL\r\n.= *" } =20 # Now register an architecture specific disassembler, and check it # overrides the global disassembler. with_test_prefix "LOCAL tagging disassembler" { gdb_test_no_output "python gdb.disassembler.register_disassembler(Tagg= ingDisassembler(\"LOCAL\"), \"${curr_arch}\")" - gdb_test "disassemble main" "${base_pattern}\\s+## tag =3D LOCAL\r\n.*" + gdb_test "disassemble test" "${base_pattern}\\s+## tag =3D LOCAL\r\n.*" } =20 # Now remove the architecture specific disassembler, and check that # the global disassembler kicks back in. with_test_prefix "GLOBAL tagging disassembler again" { gdb_test_no_output "python gdb.disassembler.register_disassembler(None= , \"${curr_arch}\")" - gdb_test "disassemble main" "${base_pattern}\\s+## tag =3D GLOBAL\r\n.= *" + gdb_test "disassemble test" "${base_pattern}\\s+## tag =3D GLOBAL\r\n.= *" } =20 # Check that a DisassembleInfo becomes invalid after the call into the @@ -160,7 +160,7 @@ with_test_prefix "GLOBAL tagging disassembler again" { with_test_prefix "DisassembleInfo becomes invalid" { py_remove_all_disassemblers gdb_test_no_output "python add_global_disassembler(GlobalCachingDisass= embler)" - gdb_test "disassemble main" "${base_pattern}\\s+## CACHED\r\n.*" + gdb_test "disassemble test" "${base_pattern}\\s+## CACHED\r\n.*" gdb_test "python GlobalCachingDisassembler.check()" "PASS" } =20 @@ -168,10 +168,10 @@ with_test_prefix "DisassembleInfo becomes invalid" { with_test_prefix "memory source api" { py_remove_all_disassemblers gdb_test_no_output "python analyzing_disassembler =3D add_global_disas= sembler(AnalyzingDisassembler)" - gdb_test "disassemble main" "${base_pattern}\r\n.*" + gdb_test "disassemble test" "${base_pattern}\r\n.*" gdb_test "python analyzing_disassembler.find_replacement_candidate()" \ "Replace from $hex to $hex with NOP" - gdb_test "disassemble main" "${base_pattern}\r\n.*" \ + gdb_test "disassemble test" "${base_pattern}\r\n.*" \ "second disassembler pass" gdb_test "python analyzing_disassembler.check()" \ "PASS" @@ -196,6 +196,12 @@ with_test_prefix "maint info python-disassemblers" { "\[^\r\n\]+BuiltinDisassembler\\s+\\(Matches current architecture\\)= " \ "GLOBAL\\s+BuiltinDisassembler"] \ "list disassemblers, multiple disassemblers registered" + + # Check that disassembling main (with the BuiltinDisassembler in + # place) doesn't cause GDB to crash. The hope is that + # disassembling main will result in a call to print_address, which + # is where the problem was. + gdb_test "disassemble main" ".*" } =20 # Check the attempt to create a "new" DisassembleInfo object fails. diff --git a/gdb/testsuite/gdb.python/py-disasm.py b/gdb/testsuite/gdb.pyth= on/py-disasm.py index ff7ffdb97d9..3b9008b1c54 100644 --- a/gdb/testsuite/gdb.python/py-disasm.py +++ b/gdb/testsuite/gdb.python/py-disasm.py @@ -584,7 +584,6 @@ class AnalyzingDisassembler(Disassembler): if self._nop_index is None and result.string =3D=3D "nop": self._nop_index =3D len(self._pass_1_length) # The offset in the following read_memory call defaults to 0. - print("APB: Reading nop bytes") self._nop_bytes =3D info.read_memory(result.length) =20 # Record information about each instruction that is disassembled. @@ -661,8 +660,6 @@ class AnalyzingDisassembler(Disassembler): def check(self): """Call this after the second disassembler pass to validate the ou= tput.""" if self._check !=3D self._pass_2_insn: - print("APB, Check : %s" % self._check) - print("APB, Result: %s" % self._pass_2_insn) raise gdb.GdbError("mismatch") print("PASS")