From patchwork Fri Nov 30 21:53:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Marchi X-Patchwork-Id: 30485 Received: (qmail 39357 invoked by alias); 30 Nov 2018 21:53:34 -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 39343 invoked by uid 89); 30 Nov 2018 21:53:33 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, MIME_BASE64_BLANKS autolearn=ham version=3.3.2 spammy=sk:vg_repl, UD:vg_replace_malloc.c, 1888, throws X-HELO: sessmg22.ericsson.net Received: from sessmg22.ericsson.net (HELO sessmg22.ericsson.net) (193.180.251.58) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 30 Nov 2018 21:53:29 +0000 DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/simple; q=dns/txt; i=@ericsson.com; t=1543614806; x=1546206806; h=From:Sender:Reply-To:Subject:Date:Message-ID:To:Cc:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=YMqVzeKf5k2u/BXaUYUVnQyYoB2BG4sL5m0wJGJZnnU=; b=VIdSS8+6qO8mA/QfQ23J3UPihyH9rBIIchCu2kyWFPxS5ruiLsgLc+ZuK4BsDFZQ gihgCi+KrxmKYSW2L/IGg+rfh9rnu1RaukZCTdKBf290XQ0YD93CZ/4ZashQ6sVe j3C5Q76G5NkwQ4jH5ROrOLAh4yxRlXYT5UwZIwQHQvw=; Received: from ESESSMB504.ericsson.se (Unknown_Domain [153.88.183.122]) by sessmg22.ericsson.net (Symantec Mail Security) with SMTP id B1.B8.10055.651B10C5; Fri, 30 Nov 2018 22:53:26 +0100 (CET) Received: from ESESSMR502.ericsson.se (153.88.183.110) by ESESSMB504.ericsson.se (153.88.183.165) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1466.3; Fri, 30 Nov 2018 22:53:26 +0100 Received: from ESESSMB503.ericsson.se (153.88.183.164) by ESESSMR502.ericsson.se (153.88.183.110) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1466.3; Fri, 30 Nov 2018 22:53:26 +0100 Received: from NAM04-SN1-obe.outbound.protection.outlook.com (153.88.183.157) by ESESSMB503.ericsson.se (153.88.183.164) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1466.3 via Frontend Transport; Fri, 30 Nov 2018 22:53:25 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ericsson.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=YMqVzeKf5k2u/BXaUYUVnQyYoB2BG4sL5m0wJGJZnnU=; b=jSHfWC7c3MAY2BprgRER7c8XqPc9v/1Kqsp9B0pkxfYFLaaHT2h4+30drNbGkP6fqr7momdxLG1xulaDJdm5u9e4T9PuVT1JHeclOhE1PSx23bRoaf2DNF30djW+7mTp/vPvwAoXoE/xRNYfuo/9CYhUDN/KGquBZk6w64AySek= Received: from BYAPR15MB2390.namprd15.prod.outlook.com (52.135.198.30) by BYAPR15MB3239.namprd15.prod.outlook.com (20.179.57.30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1382.22; Fri, 30 Nov 2018 21:53:21 +0000 Received: from BYAPR15MB2390.namprd15.prod.outlook.com ([fe80::2978:b8d7:86fd:94ed]) by BYAPR15MB2390.namprd15.prod.outlook.com ([fe80::2978:b8d7:86fd:94ed%3]) with mapi id 15.20.1361.019; Fri, 30 Nov 2018 21:53:21 +0000 From: Simon Marchi To: Philippe Waroquiers , Simon Marchi , "gdb-patches@sourceware.org" Subject: Re: [RFAv2] Fix leak in linespec parser. Date: Fri, 30 Nov 2018 21:53:21 +0000 Message-ID: References: <20181125230451.6036-1-philippe.waroquiers@skynet.be> <56a8ee8f-a5b3-f34f-6179-abc9c791dd13@simark.ca> <1543359126.4149.5.camel@skynet.be> In-Reply-To: <1543359126.4149.5.camel@skynet.be> authentication-results: spf=none (sender IP is ) smtp.mailfrom=simon.marchi@ericsson.com; received-spf: None (protection.outlook.com: ericsson.com does not designate permitted sender hosts) Content-ID: <0817BE0C9CB3BA43959FB2E14C8479ED@namprd15.prod.outlook.com> MIME-Version: 1.0 Return-Path: simon.marchi@ericsson.com X-IsSubscribed: yes On 2018-11-27 5:52 p.m., Philippe Waroquiers wrote: > On Sun, 2018-11-25 at 20:42 -0500, Simon Marchi wrote: >> Hi Philippe, >> >> This looks good, but I think we could simplify that a bit by returning >> std::vector objects directly, not dealing with new/delete. We would move >> returned data in the existing vector. Would the patch below work? I think >> everything is set up correctly move-semantic-wise so that there would be no >> copy of vector data. > > Hello Simon, > > I tested the patch, which effectively solves the leak. > For what concerns the move-semantic-wise: I cannot really comment on that, > as my c++ knowledge is very limited (only working with c++ on GDB at home, > never really learned it). > But valgrind does not show any increase in total nr of blocks allocated: > We see in the below that the unpatched GDB has 7 blocks leaked, and does > the same total nr of block allocations than the patched GDB, so that looks all ok. > > The patched GDB allocates in total 2206 bytes more, probably irrelevant > difference caused by environment/file name differences/hash table differing/... > > So, FWIW, the patch looks good to me ... Ok thanks, this is what I pushed: From 4717cec4fe4cb3a086fb13161603112e8ded787e Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Fri, 30 Nov 2018 16:49:35 -0500 Subject: [PATCH] Fix leak in linespec parser Valgrind reports this leak: ==798== VALGRIND_GDB_ERROR_BEGIN ==798== 32 (24 direct, 8 indirect) bytes in 1 blocks are definitely lost in loss record 447 of 3,143 ==798== at 0x4C2C48C: operator new(unsigned long) (vg_replace_malloc.c:334) ==798== by 0x51D401: linespec_parser_new(ls_parser*, int, language_defn const*, program_space*, symtab*, int, linespec_result*) (linespec.c:2756) ==798== by 0x524BF7: decode_line_full(event_location const*, int, program_space*, symtab*, int, linespec_result*, char const*, char const*) (linespec.c:3271) ==798== by 0x3E8893: parse_breakpoint_sals(event_location const*, linespec_result*) (breakpoint.c:9067) ==798== by 0x3E4E7F: create_breakpoint(gdbarch*, event_location const*, char const*, int, char const*, int, int, bptype, int, auto_boolean, breakpoint_ops const*, int, int, int, unsigned int) (breakpoint.c:9248) ==798== by 0x3E55F5: break_command_1(char const*, int, int) (breakpoint.c:9434) ==798== by 0x40BA68: cmd_func(cmd_list_element*, char const*, int) (cli-decode.c:1888) ==798== by 0x665300: execute_command(char const*, int) (top.c:630) ... linespec_parser_new allocates a std::vector at line 2756, and stores the pointer to this vector in PARSER_RESULT (parser)->file_symtabs. At 3 different places in linespec.c, another std::vector is assigned to a linespec->file_symtabs, without first deleting the current value. The leak is fixed by assigning the vector itself instead of the pointer. Everything should be moved, so there is no significant data copy involved. Tested on debian/amd64, + a bunch of tests re-run under valgrind (including the test that throws an error). gdb/ChangeLog: * linespec.c (symtab_vector_up): Remove. (symtabs_from_filename): Change return type to std::vector. (collect_symtabs_from_filename): Likewise. (create_sals_line_offset): Assign return value of collect_symtabs_from_filename to *ls->file_symtabs. (convert_explicit_location_to_linespec): Remove call to release. (parse_linespec): Likewise. (symtab_collector) : Remove initialization of m_symtabs. : Change return type to std::vector. : Adjust. --- gdb/ChangeLog | 15 +++++++++++++++ gdb/linespec.c | 38 +++++++++++++++----------------------- 2 files changed, 30 insertions(+), 23 deletions(-) -- 2.19.2 diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 348eb65ec71..778eebc1b10 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,18 @@ +2018-11-30 Philippe Waroquiers + Simon Marchi + + * linespec.c (symtab_vector_up): Remove. + (symtabs_from_filename): Change return type to std::vector. + (collect_symtabs_from_filename): Likewise. + (create_sals_line_offset): Assign return value of + collect_symtabs_from_filename to *ls->file_symtabs. + (convert_explicit_location_to_linespec): Remove call to release. + (parse_linespec): Likewise. + (symtab_collector) : Remove initialization of + m_symtabs. + : Change return type to std::vector. + : Adjust. + 2018-11-30 John Baldwin * fbsd-nat.c [__FreeBSD_version >= 700009] (USE_SIGINFO): Macro diff --git a/gdb/linespec.c b/gdb/linespec.c index 00f59f9c286..e534cf2e81e 100644 --- a/gdb/linespec.c +++ b/gdb/linespec.c @@ -77,10 +77,6 @@ enum class linespec_complete_what KEYWORD, }; -/* Typedef for unique_ptrs of vectors of symtabs. */ - -typedef std::unique_ptr> symtab_vector_up; - /* An address entry is used to ensure that any given location is only added to the result a single time. It holds an address and the program space from which the address came. */ @@ -357,7 +353,7 @@ static std::vector decode_objc (struct linespec_state *self, linespec_p ls, const char *arg); -static symtab_vector_up symtabs_from_filename +static std::vector symtabs_from_filename (const char *, struct program_space *pspace); static std::vector *find_label_symbols @@ -389,7 +385,7 @@ static void add_all_symbol_names_from_pspace (struct collect_info *info, struct program_space *pspace, const std::vector &names, enum search_domain search_domain); -static symtab_vector_up +static std::vector collect_symtabs_from_filename (const char *file, struct program_space *pspace); @@ -2117,9 +2113,8 @@ create_sals_line_offset (struct linespec_state *self, set_default_source_symtab_and_line (); initialize_defaults (&self->default_symtab, &self->default_line); fullname = symtab_to_fullname (self->default_symtab); - symtab_vector_up r = - collect_symtabs_from_filename (fullname, self->search_pspace); - ls->file_symtabs = r.release (); + *ls->file_symtabs + = collect_symtabs_from_filename (fullname, self->search_pspace); use_default = 1; } @@ -2401,9 +2396,8 @@ convert_explicit_location_to_linespec (struct linespec_state *self, { TRY { - result->file_symtabs - = symtabs_from_filename (source_filename, - self->search_pspace).release (); + *result->file_symtabs + = symtabs_from_filename (source_filename, self->search_pspace); } CATCH (except, RETURN_MASK_ERROR) { @@ -2627,10 +2621,9 @@ parse_linespec (linespec_parser *parser, const char *arg, /* Check if the input is a filename. */ TRY { - symtab_vector_up r + *PARSER_RESULT (parser)->file_symtabs = symtabs_from_filename (user_filename.get (), PARSER_STATE (parser)->search_pspace); - PARSER_RESULT (parser)->file_symtabs = r.release (); } CATCH (ex, RETURN_MASK_ERROR) { @@ -3790,7 +3783,6 @@ class symtab_collector { public: symtab_collector () - : m_symtabs (new std::vector ()) { m_symtab_table = htab_create (1, htab_hash_pointer, htab_eq_pointer, NULL); @@ -3805,15 +3797,15 @@ public: /* Callable as a symbol_found_callback_ftype callback. */ bool operator () (symtab *sym); - /* Releases ownership of the collected symtabs and returns them. */ - symtab_vector_up release_symtabs () + /* Return an rvalue reference to the collected symtabs. */ + std::vector &&release_symtabs () { return std::move (m_symtabs); } private: /* The result vector of symtabs. */ - symtab_vector_up m_symtabs; + std::vector m_symtabs; /* This is used to ensure the symtabs are unique. */ htab_t m_symtab_table; @@ -3828,7 +3820,7 @@ symtab_collector::operator () (struct symtab *symtab) if (!*slot) { *slot = symtab; - m_symtabs->push_back (symtab); + m_symtabs.push_back (symtab); } return false; @@ -3840,7 +3832,7 @@ symtab_collector::operator () (struct symtab *symtab) SEARCH_PSPACE is not NULL, the search is restricted to just that program space. */ -static symtab_vector_up +static std::vector collect_symtabs_from_filename (const char *file, struct program_space *search_pspace) { @@ -3872,14 +3864,14 @@ collect_symtabs_from_filename (const char *file, /* Return all the symtabs associated to the FILENAME. If SEARCH_PSPACE is not NULL, the search is restricted to just that program space. */ -static symtab_vector_up +static std::vector symtabs_from_filename (const char *filename, struct program_space *search_pspace) { - symtab_vector_up result + std::vector result = collect_symtabs_from_filename (filename, search_pspace); - if (result->empty ()) + if (result.empty ()) { if (!have_full_symbols () && !have_partial_symbols ()) throw_error (NOT_FOUND_ERROR,