From patchwork Mon Oct 23 23:19:08 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Gulick X-Patchwork-Id: 23772 Received: (qmail 22187 invoked by alias); 23 Oct 2017 23:19:21 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 22177 invoked by uid 89); 23 Oct 2017 23:19:20 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.3 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 spammy=Immediately, Close, TRY X-HELO: NAM03-CO1-obe.outbound.protection.outlook.com Received: from mail-co1nam03on0079.outbound.protection.outlook.com (HELO NAM03-CO1-obe.outbound.protection.outlook.com) (104.47.40.79) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 23 Oct 2017 23:19:16 +0000 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Mike.Gulick@mathworks.com; Received: from [172.28.194.135] (144.212.3.4) by BLUPR0501MB2033.namprd05.prod.outlook.com (10.164.23.15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.178.3; Mon, 23 Oct 2017 23:19:13 +0000 Subject: Re: [RFC][PATCH] fix gdb segv when objfile can't be opened To: Simon Marchi Cc: gdb-patches@sourceware.org References: <59E8B251.4050100@mathworks.com> <8c08307a-94ad-92b8-9c8b-c713cad541fd@mathworks.com> <56b1cb34b33613ca4496abfcd28f135a@polymtl.ca> From: Mike Gulick Message-ID: Date: Mon, 23 Oct 2017 19:19:08 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: <56b1cb34b33613ca4496abfcd28f135a@polymtl.ca> X-ClientProxiedBy: DM5PR16CA0041.namprd16.prod.outlook.com (10.172.42.155) To BLUPR0501MB2033.namprd05.prod.outlook.com (10.164.23.15) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 33024e03-6691-4ead-2445-08d51a6c7940 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(4534020)(4602075)(4627075)(201703031133081)(201702281549075)(2017052603199); SRVR:BLUPR0501MB2033; X-Microsoft-Exchange-Diagnostics: 1; BLUPR0501MB2033; 3:kmqxYeci6CEczipHuzzht7uMSQgg/3V461hNFIttmfu4na3cMgoCpzUQflVbwcyLOOewxdwlJsZD4YrYly+i7wXiaDM25sIOes9mMVQR8kPoKsAqH7cOtX7aD3gnUweTugH4/HLdhseCDGYziag1T9Sa9I6D+rEXix8Mlovo+HR5qxFrzSnjq6gM1L+4mGHmDRkSxAjAIwER1SI5MTHJgtICXqOL4CU9jyuEvNKvKB7T6k3OIcvSbkntDFiICPwH; 25:VVhFBs88P2GE7HN/TlEgKyTxLI2seZ+6keNrNs00Gwf8pK9Zu4Uv5D3b9vqy7izfA7cA/3GuPT2IyuVtzZ+/4VKpdv1UfnsaJ8fJIAxzI7XrwouJCQKqct/n3mPBt9WP9mPuR9JcrmoYe3jBVGc26zggKeAEhJud38Bcqn7JNZqyUFQ40M6tVF+E/pEQWADksovpVR16eUy/NHvYw3DYRoIRZ9UFn9z9L+JYOP2qGOtR+ora5r4p2zp1mmUP+lW/3WpW21rzvXQKEUgRSz6eauAe6t6gytumCdQe1TmsiCKMVoDq48Is6KYxYAyCydy455HVPa32vQLrcCxzlo76QA==; 31:G6e/V/eJTsk1pOqIkqGLvaTycS89S5jDLDnHnScHjj/m8YuxmDP/TYzhqICEmuEsO53a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BERXYoLYoFZZQetDdO/ZkQ6SpVUFkJXjQFAAs2Fpb2HA= X-Exchange-Antispam-Report-Test: UriScan:(250305191791016)(22074186197030); X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(2401047)(8121501046)(5005006)(93006095)(93001095)(3002001)(3231020)(10201501046)(100000703101)(100105400095)(6041248)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123560025)(20161123558100)(20161123562025)(20161123564025)(20161123555025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095); SRVR:BLUPR0501MB2033; BCL:0; PCL:0; RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095); SRVR:BLUPR0501MB2033; X-Forefront-PRVS: 046985391D X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(6049001)(6009001)(376002)(346002)(199003)(189002)(16576012)(31686004)(316002)(58126008)(6666003)(6916009)(101416001)(2950100002)(77096006)(16526018)(33646002)(105586002)(106356001)(90366009)(6486002)(25786009)(6246003)(23676002)(305945005)(229853002)(2906002)(68736007)(81166006)(7736002)(8676002)(81156014)(8936002)(575784001)(86362001)(65826007)(478600001)(4326008)(5660300001)(72206003)(31696002)(53546010)(47776003)(66066001)(65956001)(64126003)(36756003)(65806001)(50466002)(230700001)(54356999)(83506002)(6306002)(3846002)(6116002)(97736004)(189998001)(76176999)(53936002)(50986999)(2004002)(473944003)(414714003); DIR:OUT; SFP:1101; SCL:1; SRVR:BLUPR0501MB2033; H:[172.28.194.135]; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; Received-SPF: None (protection.outlook.com: mathworks.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtCTFVQUjA1MDFNQjIwMzM7MjM6YktRZWtLSzlJcGtKbTh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MfiQNHizqoMBAwvvp5JefJPO9gkW/n4VAg2s5 SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: mathworks.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Oct 2017 23:19:13.2705 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 33024e03-6691-4ead-2445-08d51a6c7940 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 99dd3a11-4348-4468-9bdd-e5072b1dc1e6 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLUPR0501MB2033 Here is a new version of the patch. It changes the following from the original patch: 1) It adds a testsuite which locks down: a) The stack frame is printed when the breakpoint is hit b) 'next' runs without causing a segv 2) No longer set descriptor->size in gdb_bfd_map_section when an error is hit. It doesn't look like this is needed as gdb_bfd_map_section is the only place that consumes that descriptor->size field. 3) Update the description in gdb_bfd.h to indicate that if there is an error mapping the section, a warning is generated, *SIZE is set to 0, and NULL is returned. I considered catching the error in the caller of gdb_bfd_map_section, which is dwarf2_read_section in dwarf2read.c. It didn't seem to make any more sense to catch the error here than it would directly in gdb_bfd_map_section. If anything, it would make more sense to catch the error in the caller of dwarf2_read_section, which in this case is dwarf2_get_section_info. However there are many callers of dwarf2_read_section. It doesn't make sense to force all of them to add a TRY/CATCH around calling dwarf2_read_section. Please let me know thoughts/feedback. Also see the note in the test case about not being able to make 'shlib=...' work when compiling a shared library that links against another shared library. The workaround I used is kind of ugly. Thanks, Mike --- From d59bb69db0f10f8cb7d91568907b505f05424df7 Mon Sep 17 00:00:00 2001 From: Mike Gulick Date: Wed, 18 Oct 2017 16:04:27 -0400 Subject: [PATCH v2] fix gdb segv when objfile can't be opened This fixes PR 16577. This patch changes gdb_bfd_map_section to issue a warning rather than an error if it is unable to read the object file, and sets the size of the section/frame that it attempted to read to 0 on error. The description of gdb_bfd_map_section states that it will try to read or map the contents of the section SECT, and if successful, the section data is returned and *SIZE is set to the size of the section data. This function was throwing an error and leaving *SIZE as-is. Setting the section size to 0 indicates to dwarf2_build_frame_info that there is no data to read, otherwise it will try to read from an invalid frame pointer. Changing the error to a warning allows this to be handled gracefully. Additionally, the error was clobbering the breakpoint output indicating the current frame (function name, arguments, source file, and line number). E.g. Thread 3 "foo" hit Breakpoint 1, BFD: reopening /tmp/jna-1013829440/jna2973250704389291330.tmp: No such file or directory BFD: reopening /tmp/jna-1013829440/jna2973250704389291330.tmp: No such file or directory (gdb) While the "BFD: reopening ..." messages will still appear interspersed in the breakpoint output, the current frame info is now displayed: Thread 3 "foo" hit Breakpoint 1, BFD: reopening /tmp/jna-1013829440/jna1875755897659885075.tmp: No such file or directory BFD: reopening /tmp/jna-1013829440/jna1875755897659885075.tmp: No such file or directory warning: Can't read data for section '.eh_frame' in file '/tmp/jna-1013829440/jna1875755897659885075.tmp' do_something () at file.cpp:80 80 { (gdb) --- gdb/gdb_bfd.c | 11 ++- gdb/gdb_bfd.h | 17 +++-- gdb/testsuite/gdb.base/solib-vanish-lib1.c | 22 ++++++ gdb/testsuite/gdb.base/solib-vanish-lib2.c | 20 ++++++ gdb/testsuite/gdb.base/solib-vanish-main.c | 62 +++++++++++++++++ gdb/testsuite/gdb.base/solib-vanish.exp | 108 +++++++++++++++++++++++++++++ 6 files changed, 228 insertions(+), 12 deletions(-) create mode 100644 gdb/testsuite/gdb.base/solib-vanish-lib1.c create mode 100644 gdb/testsuite/gdb.base/solib-vanish-lib2.c create mode 100644 gdb/testsuite/gdb.base/solib-vanish-main.c create mode 100644 gdb/testsuite/gdb.base/solib-vanish.exp diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c index cc02740..b33de07 100644 --- a/gdb/gdb_bfd.c +++ b/gdb/gdb_bfd.c @@ -702,9 +702,14 @@ gdb_bfd_map_section (asection *sectp, bfd_size_type *size) data = NULL; if (!bfd_get_full_section_contents (abfd, sectp, &data)) - error (_("Can't read data for section '%s' in file '%s'"), - bfd_get_section_name (abfd, sectp), - bfd_get_filename (abfd)); + { + warning (_("Can't read data for section '%s' in file '%s'"), + bfd_get_section_name (abfd, sectp), + bfd_get_filename (abfd)); + /* Section is invalid -- set size to 0 and return NULL */ + *size = 0; + return (const gdb_byte *) NULL; + } descriptor->data = data; done: diff --git a/gdb/gdb_bfd.h b/gdb/gdb_bfd.h index b1ff857..e427781 100644 --- a/gdb/gdb_bfd.h +++ b/gdb/gdb_bfd.h @@ -115,15 +115,14 @@ void gdb_bfd_mark_parent (bfd *child, bfd *parent); void gdb_bfd_record_inclusion (bfd *includer, bfd *includee); -/* Try to read or map the contents of the section SECT. If - successful, the section data is returned and *SIZE is set to the - size of the section data; this may not be the same as the size - according to bfd_get_section_size if the section was compressed. - The returned section data is associated with the BFD and will be - destroyed when the BFD is destroyed. There is no other way to free - it; for temporary uses of section data, see - bfd_malloc_and_get_section. SECT may not have relocations. This - function will throw on error. */ +/* Try to read or map the contents of the section SECT. If successful, the + section data is returned and *SIZE is set to the size of the section data; + this may not be the same as the size according to bfd_get_section_size if the + section was compressed. The returned section data is associated with the BFD + and will be destroyed when the BFD is destroyed. There is no other way to + free it; for temporary uses of section data, see bfd_malloc_and_get_section. + SECT may not have relocations. If there is an error reading the section, + this issues a warning, sets *SIZE to 0, and returns NULL. */ const gdb_byte *gdb_bfd_map_section (asection *section, bfd_size_type *size); diff --git a/gdb/testsuite/gdb.base/solib-vanish-lib1.c b/gdb/testsuite/gdb.base/solib-vanish-lib1.c new file mode 100644 index 0000000..f8e8182 --- /dev/null +++ b/gdb/testsuite/gdb.base/solib-vanish-lib1.c @@ -0,0 +1,22 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2017 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 . */ + +extern int bar (int); + +int foo (int x) { + return bar (x); +} diff --git a/gdb/testsuite/gdb.base/solib-vanish-lib2.c b/gdb/testsuite/gdb.base/solib-vanish-lib2.c new file mode 100644 index 0000000..7799d92 --- /dev/null +++ b/gdb/testsuite/gdb.base/solib-vanish-lib2.c @@ -0,0 +1,20 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2017 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 . */ + +int bar (int y) { + return y + 1; /* break here */ +} diff --git a/gdb/testsuite/gdb.base/solib-vanish-main.c b/gdb/testsuite/gdb.base/solib-vanish-main.c new file mode 100644 index 0000000..0e21e9c --- /dev/null +++ b/gdb/testsuite/gdb.base/solib-vanish-main.c @@ -0,0 +1,62 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2017 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 . */ + +#include +#include +#include + +#ifndef VANISH_LIB +#define VANISH_LIB "./solib-vanish-lib1.so" +#endif + +int main() +{ + void *handle; + int (*foo)(int); + char *error; + + /* Open library */ + handle = dlopen(VANISH_LIB, RTLD_NOW); + if (!handle) { + fprintf(stderr, "%s\n", dlerror()); + exit(EXIT_FAILURE); + } + + /* Clear any existing error */ + dlerror(); + + /* Remove library */ + if (remove(VANISH_LIB) == -1) { + perror("remove " VANISH_LIB); + exit(EXIT_FAILURE); + } + + /* Get function pointer */ + foo = dlsym(handle, "foo"); + error = dlerror(); + if (error != NULL) { + fprintf(stderr, "%s\n", error); + exit(EXIT_FAILURE); + } + + /* Call function */ + (*foo)(1); + + /* Close and exit */ + dlclose(handle); + exit(EXIT_SUCCESS); +} diff --git a/gdb/testsuite/gdb.base/solib-vanish.exp b/gdb/testsuite/gdb.base/solib-vanish.exp new file mode 100644 index 0000000..de92d8d --- /dev/null +++ b/gdb/testsuite/gdb.base/solib-vanish.exp @@ -0,0 +1,108 @@ +# Copyright 2017 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 . +# + +# This test case verifies that GDB gracefully handles a shared library file +# vanishing after being dlopen'ed. This consists of three nested function calls: +# +# main() -> foo() -> bar() +# +# where: +# - foo exists in solib-vanish-lib1.so, which is dlopen'ed by main() +# - bar exists in solib-vanish-lib2.so, which is dynamically linked into +# solib-vanish-lib1.so +# +# Immediately after dlopen'ing solib-vanish-lib1.so, the so file is deleted. +# The main executable and solib-vanish-lib2.so are still accessible. +# +# If a breakpoint is set on bar(), gdb throws an error when this breakpoint is +# hit: +# +# (gdb) r +# Starting program: /local/gdb/git/pr_16577_repro/simple/solib-vanish-main +# +# Breakpoint 1, BFD: reopening ./solib-vanish-lib1.so: No such file or directory +# +# BFD: reopening ./solib-vanish-lib1.so: No such file or directory +# +# (gdb) +# +# Notice that this does not print the current frame, i.e.: +# bar (y=1) at solib-vanish-lib2.c:19 +# 19 return y + 1; /* break here */ +# (gdb) +# +# The current gdb git tip segfaults if we then try to step: +# (gdb) n +# Segmentation fault + +# This test verifies that: +# 1) GDB does not segfault when stepping +# 2) The stack frame is printed + +if { [skip_shlib_tests] } { + return 0 +} + +# Library 2 +set lib2name "solib-vanish-lib2" +set srcfile_lib2 ${srcdir}/${subdir}/${lib2name}.c +set binfile_lib2 [standard_output_file ${lib2name}.so] +set lib2_flags {debug} + +# Library 1 +set lib1name "solib-vanish-lib1" +set srcfile_lib1 ${srcdir}/${subdir}/${lib1name}.c +set binfile_lib1 [standard_output_file ${lib1name}.so] +# I can't make this work for some reason: +#set lib1_flags [list debug shlib=${binfile_lib2}] +set lib1_flags [list debug ldflags=-l:${lib2name}.so ldflags=-Wl,-rpath=[standard_output_file ""] libdir=[standard_output_file ""]] + +# Main program +set testfile "solib-vanish-main" +set srcfile ${srcdir}/${subdir}/${testfile}.c +set executable ${testfile} +set binfile [standard_output_file ${executable}] +set bin_flags [list debug shlib_load additional_flags=-DVANISH_LIB=\"${binfile_lib1}\"] + +if { [gdb_compile_shlib ${srcfile_lib2} ${binfile_lib2} $lib2_flags] != "" + || [gdb_compile_shlib ${srcfile_lib1} ${binfile_lib1} $lib1_flags] != "" + || [gdb_compile ${srcfile} ${binfile} executable $bin_flags] != "" } { + untested "failed to compile" + return -1 +} + +clean_restart $testfile + +if { ![runto_main] } { + fail "can't run to main" + return +} + +delete_breakpoints + +set lib2_lineno [gdb_get_line_number "break here" ${srcfile_lib2}] + +gdb_breakpoint "${lib2name}.c:${lib2_lineno}" {allow-pending} + +gdb_test "continue" \ + "Breakpoint .*at .*/${lib2name}.c:${lib2_lineno}.*break here.*" \ + "breakpoint prints location" + +# This should not segfault +gdb_test "next" \ + "" \ + "next succeeds" +