From patchwork Thu May 18 08:37:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 69586 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AF16F38323C6 for ; Thu, 18 May 2023 08:38:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AF16F38323C6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684399119; bh=lC/29mmi17kMRfxM3n+j3MZm1qGWkHs9niIlWpv3cPA=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=kyKXsyoyQyzJXQuekah/S+qvR0HUJNNfBuVSqp5zCUrZX2ra3JTulUV6/MKHyhplR jZJLc0TYNIQ5AfTKyqBGbZj/FEeWF0nk4g2/bDFalTf6XNrhgCHReDbCgP52oM2Lrx 2u0BugPWSAFEEZ1uGuxQRC9dCIxJQKPxgZkX+QW4= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 94A353857704 for ; Thu, 18 May 2023 08:38:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 94A353857704 Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-308-6hP_f6DiPEql_RUyeggCKA-1; Thu, 18 May 2023 04:38:06 -0400 X-MC-Unique: 6hP_f6DiPEql_RUyeggCKA-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-3f518f2900eso11872515e9.2 for ; Thu, 18 May 2023 01:38:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684399085; x=1686991085; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lC/29mmi17kMRfxM3n+j3MZm1qGWkHs9niIlWpv3cPA=; b=hmIZLZ5sxBAaB0uUMY2mKn2jxPrUh38ZgAL8pGKu9cqBJ3jXFE6QzphDUJAJVJy3ak yazaTzPR6cMIchMnTVT7yVNdLf8irT4ridCxKFlVdlA+mKKJLPVVpbQ+eNiH7V1fj83y QaA54LMsjKcUVv+/f/CyVOUY8hZncbPJV/ytGDeQt6Ys9oQP9kWoAk173LmJD6rtkgyG ILZIFI9wAyXgsOGLlw2SUFEkVtPguhLoYM9mCf2rtehAVQesnFAxr9kg5ThXjSz9goA9 L4LesyX1CXQnRy6IbfVlIErCgy+edt60NJgrgw//KPZHakEizV3vpzZNhpYVc/FNCcx/ ZssQ== X-Gm-Message-State: AC+VfDyWStu86a1VMQaUUDlSNwYIJMwdnIVnTXKT2eWRLM7/QlYuOM7H sQ3C0Kgqj1ltNzKh3kET4ydQyXyUZXpf7qBomtZpkGFZ6ZoWb9i4GdKKAH3oAlv909zqz5scGbN gNYCrut3HE6KeM3bOUWki/QaD67FEU4jXc2b4jgfzWRzoOWLoLj7FRFU7u1kKktVK+PnU5047lO 7IhaIWtQ== X-Received: by 2002:adf:f0c8:0:b0:306:462a:b78d with SMTP id x8-20020adff0c8000000b00306462ab78dmr1009233wro.53.1684399085504; Thu, 18 May 2023 01:38:05 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4BPG36ha4OYTaHZ0xjIeauWel304rG8rcFFF/PffSBhOCmYwDMdx584/O5Nc4vDh8+Jo2EFQ== X-Received: by 2002:adf:f0c8:0:b0:306:462a:b78d with SMTP id x8-20020adff0c8000000b00306462ab78dmr1009212wro.53.1684399084993; Thu, 18 May 2023 01:38:04 -0700 (PDT) Received: from localhost (11.72.115.87.dyn.plus.net. [87.115.72.11]) by smtp.gmail.com with ESMTPSA id k6-20020a5d5246000000b003090cb7a9e6sm1429017wrc.31.2023.05.18.01.38.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 01:38:04 -0700 (PDT) To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 1/2] gdb/testsuite: test for a function with no line table Date: Thu, 18 May 2023 09:37:54 +0100 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Andrew Burgess via Gdb-patches From: Andrew Burgess Reply-To: Andrew Burgess Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" This commit adds a test for the following commit: commit e86e87f77fd5d8afb3e714f1d9e09e0ff5b4e6ff Date: Tue Nov 28 16:23:32 2006 +0000 * symtab.c (find_pc_sect_line): Do not return a line before the start of a symtab. We have been carrying a test for that commit in the Fedora GDB tree since that commit was added to GDB. I don't know why the test wasn't added along with the original commit, but as was written, the test is pretty gross, it uses objcopy to pull the .text section from an object file, which was then injected into another source file within a .asm statement... ... these days we can just make use of the DWARF assembler to achieve the same results, so I've rewritten the test and think it is worth adding this to upstream GDB. The original patch was about about how we find the best symtab and line table entry, and what to do when GDB can't find a good match. The new test creates a CU with two functions, only one of which is covered by the line table. With the above patch reverted GDB returns an invalid address. With the above patch reverted I did run the testsuite to see what other tests might already be exercising this functionality, and I found two tests: gdb.dwarf2/dw2-step-out-of-function-no-stmt.exp gdb.dwarf2/dw2-vendor-extended-opcode.exp These are pretty similar, they either create minimal, or no line table for one of the functions in the source file, and as a consequence GDB returns an unexpected address at some point during the test. However, both of those tests are really focused on other issues, so I think this new test does add some value. Plus the new test is not large, so it's not a huge cost to also run this new test. --- gdb/testsuite/gdb.dwarf2/missing-line-table.c | 32 +++++ .../gdb.dwarf2/missing-line-table.exp | 122 ++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 gdb/testsuite/gdb.dwarf2/missing-line-table.c create mode 100644 gdb/testsuite/gdb.dwarf2/missing-line-table.exp diff --git a/gdb/testsuite/gdb.dwarf2/missing-line-table.c b/gdb/testsuite/gdb.dwarf2/missing-line-table.c new file mode 100644 index 00000000000..656fa0687ef --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/missing-line-table.c @@ -0,0 +1,32 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2023 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 . */ + +void +foo (void) +{ + asm ("foo_label: .globl foo_label"); +} + +int +main() +{ /* L1 */ + asm ("main_label: .globl main_label"); + foo (); /* L2 */ + + asm ("main_label_2: .globl main_label_2"); + return 0; /* L3 */ +} diff --git a/gdb/testsuite/gdb.dwarf2/missing-line-table.exp b/gdb/testsuite/gdb.dwarf2/missing-line-table.exp new file mode 100644 index 00000000000..a66d853726d --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/missing-line-table.exp @@ -0,0 +1,122 @@ +# Copyright 2023 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 . + +# Create a compilation unit containing two functions (the CU's +# low/high pc range will include both functions), but define a line +# table that only covers one of the functions. +# +# Use GDB to try and place a breakpoint in both functions. GDB should +# correctly find some address within both functions at which to place +# a breakpoint. + +load_lib dwarf.exp + +# This test can only be run on targets which support the DWARF +# assembler. +require dwarf2_support + +standard_testfile .c -dw.S + +get_func_info main +get_func_info foo + +set asm_file [standard_output_file $srcfile2] +Dwarf::assemble $asm_file { + declare_labels Llines + + cu {} { + compile_unit { + {language @DW_LANG_C} + {name missing-line-table.c} + {stmt_list $Llines DW_FORM_sec_offset} + {DW_AT_low_pc $::foo_start DW_FORM_addr} + {DW_AT_high_pc $::main_end DW_FORM_addr} + } { + subprogram { + {external 1 flag} + {MACRO_AT_func {main}} + } + subprogram { + {external 1 flag} + {MACRO_AT_func {foo}} + } + } + } + + lines {version 2} Llines { + include_dir "${::srcdir}/${::subdir}" + file_name "$::srcfile" 1 + + program { + DW_LNE_set_address main + line [gdb_get_line_number "L1"] + DW_LNS_copy + + DW_LNE_set_address main_label + line [gdb_get_line_number "L2"] + DW_LNS_copy + + DW_LNE_set_address main_label_2 + line [gdb_get_line_number "L3"] + DW_LNS_copy + + DW_LNE_set_address "$::main_start + $::main_len" + DW_LNE_end_sequence + } + } +} + +if { [prepare_for_testing "failed to prepare" ${testfile} \ + [list $srcfile $asm_file] {nodebug}] } { + return -1 +} + +# Place a breakpoint on FUNC. Check that the address at which the +# breakpoint is placed lies within FUNC which is starts at address +# START, and address END is the first address after FUNC. +proc check_breakpoint { func start end } { + with_test_prefix "func=$func" { + set addr "*unknown*" + gdb_test_multiple "break $func" "place breakpoint" { + -re -wrap "Breakpoint $::decimal at ($::hex)(?:: \[^\r\n\]+)?" { + set addr $expect_out(1,string) + pass $gdb_test_name + } + } + + verbose -log "breakpoint at: $addr" + verbose -log "$func start: $start, $func end: $end" + + gdb_assert {$addr >= $start && $addr < $end} \ + "check breakpoint address is within function" + } +} + +# The get_func_info calls declare the function start/end in terms of a +# program label and offsets, but we now need these as actual +# addresses. As the program is running, this can be done easily by +# asking GDB to evaluate each in turn. +foreach func {foo main} { + foreach part {start end} { + set ${func}_${part} \ + [get_hexadecimal_valueof [set ${func}_${part}] "??" \ + "get value of ${func}_${part}"] + } +} + +# Place breakpoints within 'foo' and 'main', and check that the +# breakpoint is actually placed within the function. +check_breakpoint "foo" $foo_start $foo_end +check_breakpoint "main" $main_start $main_end From patchwork Thu May 18 08:37:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 69587 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 767A93857724 for ; Thu, 18 May 2023 08:38:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 767A93857724 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684399128; bh=B63TIwJIoB/VB//C80GAuVFiiJoWscvD2ylyUyL5TXM=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=Ajd3HSxs1F1Y3hl1wUz55MrhNebhGuGwL4A3LJDbgn85XAYrpGNJ8K0kYaR1EyIzC kaT/2BZwxj+sBVOIgPZzm/Q6EcxxH2yhhC5QVHWMhDXCZ6nVGFTYjRsS4OgOOkqXln 7pHjoWviByZJs7HdJ2FQ5djK4431ryzptz4Fjp/4= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 45D6638560AA for ; Thu, 18 May 2023 08:38:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 45D6638560AA Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-186-NViED8oaOwCmIv3GJ0Yx6w-1; Thu, 18 May 2023 04:38:08 -0400 X-MC-Unique: NViED8oaOwCmIv3GJ0Yx6w-1 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-30643d4a430so669230f8f.0 for ; Thu, 18 May 2023 01:38:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684399087; x=1686991087; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=B63TIwJIoB/VB//C80GAuVFiiJoWscvD2ylyUyL5TXM=; b=LE0j5OGLL6qA5bPQkvNtHsulv5z0QxJGgGpcBcOrCofu6VeGLpL8JL+fV20an5JiZ+ AR7MrXbP2ry5qev2sMMEM1GXpt4oIhllHy6HOVVCYniWJsXZa1gOlgh3a/bLQy7sOr64 ZIjiDjjIOd64RYzsWagBQWgO/3ZqK10MEeaTbUofLIIYowGJLh3rG4F+flSgMEUoinrx mcn4aDwNjBXRO+8g8/t2B+7gA22VEX4W1xifUHkGtU13Qw0GtyiCVrUHFYOH+ykU6TyE T18ezcyTH6KPVOYhMnzaYu/B3WzP0eWNc2KRxx9/ZDKa0DkrQY7OUbS3XSvcu09XGSUN Ab1g== X-Gm-Message-State: AC+VfDyViIZXJzDE73Z49Kh+ICpuwUV4NJtWknab4+DPtR8DZU2hXxVB v54lxYbH5U5fHYrDXUU7iuO33Sk7o2iK4A5YJQkGRSquPJ01WAZPepMHRRckevAYEz6ho5mUDtX LQ/JhPvt8mK5PvEQcGS7sr+my1Umtt1QVQQIKYhkAUUuGu2O6dqtt+gxvGzCtny2p/nMoQDH3sV AaLMrvAw== X-Received: by 2002:adf:e50f:0:b0:309:4da8:7726 with SMTP id j15-20020adfe50f000000b003094da87726mr645664wrm.44.1684399087124; Thu, 18 May 2023 01:38:07 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6RA3S39STr0kty538tl9ed8DDB3RaWLj5VhcW5J8qtiQzFpft59cIN/fWIMlX2+JpW1HwFbA== X-Received: by 2002:adf:e50f:0:b0:309:4da8:7726 with SMTP id j15-20020adfe50f000000b003094da87726mr645640wrm.44.1684399086698; Thu, 18 May 2023 01:38:06 -0700 (PDT) Received: from localhost (11.72.115.87.dyn.plus.net. [87.115.72.11]) by smtp.gmail.com with ESMTPSA id f4-20020a5d5684000000b0030903d44dbcsm1399095wrv.33.2023.05.18.01.38.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 01:38:06 -0700 (PDT) To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 2/2] gdb: safety checks in skip_prologue_using_sal Date: Thu, 18 May 2023 09:37:55 +0100 Message-Id: <74b814d3c7621df2d09945579829194e33a4ccf4.1684398918.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Andrew Burgess via Gdb-patches From: Andrew Burgess Reply-To: Andrew Burgess Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" While working on the previous patch I reverted this commit: commit e86e87f77fd5d8afb3e714f1d9e09e0ff5b4e6ff Date: Tue Nov 28 16:23:32 2006 +0000 * symtab.c (find_pc_sect_line): Do not return a line before the start of a symtab. When I re-ran the testsuite I saw some GDB crashes in the tests: gdb.dwarf2/dw2-line-number-zero.exp gdb.dwarf2/dw2-lines.exp gdb.dwarf2/dw2-vendor-extended-opcode.exp GDB was reading beyond the end of an array in the function skip_prologue_using_sal. Now, without the above commit reverted I don't believe that this should ever happen. Reverting the above commit effectively breaks GDB's symtab_and_line lookup, we try to find a result for an address, and return the wrong symtab and line-table. In skip_prologue_using_sal we then walk the line table looking for an appropriate entry, except we never find one, and GDB just keeps going, wandering off the end of the array. However, I think adding extra protection to prevent walking off the end of the array is pretty cheap, and if something does go wrong in the future then this should prevent a random crash. Obviously, I have no reproducer for this, as I said, I don't think this should impact GDB at all, this is just adding a little extra caution. --- gdb/symtab.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/gdb/symtab.c b/gdb/symtab.c index 4f28667b1b3..5e1b9d91879 100644 --- a/gdb/symtab.c +++ b/gdb/symtab.c @@ -3953,15 +3953,17 @@ skip_prologue_using_sal (struct gdbarch *gdbarch, CORE_ADDR func_addr) struct objfile *objfile = prologue_sal.symtab->compunit ()->objfile (); const linetable *linetable = prologue_sal.symtab->linetable (); + gdb_assert (linetable->nitems > 0); int idx = 0; /* Skip any earlier lines, and any end-of-sequence marker from a previous function. */ - while (linetable->item[idx].pc (objfile) != prologue_sal.pc - || linetable->item[idx].line == 0) + while (idx + 1 < linetable->nitems + && (linetable->item[idx].pc (objfile) != prologue_sal.pc + || linetable->item[idx].line == 0)) idx++; - if (idx+1 < linetable->nitems + if (idx + 1 < linetable->nitems && linetable->item[idx+1].line != 0 && linetable->item[idx+1].pc (objfile) == start_pc) return start_pc;