From patchwork Tue Jul 14 09:08:23 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Coplan X-Patchwork-Id: 1328751 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=armh.onmicrosoft.com header.i=@armh.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-armh-onmicrosoft-com header.b=onF/4Bbu; dkim=pass (1024-bit key) header.d=armh.onmicrosoft.com header.i=@armh.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-armh-onmicrosoft-com header.b=onF/4Bbu; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4B5ZST23vFz9sQt for ; Tue, 14 Jul 2020 19:08:40 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C549E3850424; Tue, 14 Jul 2020 09:08:36 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from EUR04-VI1-obe.outbound.protection.outlook.com (mail-eopbgr80073.outbound.protection.outlook.com [40.107.8.73]) by sourceware.org (Postfix) with ESMTPS id 017DF3858D35; Tue, 14 Jul 2020 09:08:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 017DF3858D35 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=Alex.Coplan@arm.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=SfPN/yf5FPk59vztcjWo47qNPdnuBdWmNyHSxcJzgs8=; b=onF/4BbuA7K0w6sGu4BoKJgtzrUS4IWayVD5pCQskwFPXMmN8JDzAAW7l/NxmfOrU4Dny0jpAXhaUFExCrQ/UfFYo3/tAaLonNgdNbkU2wsZRVZEfdHYljyq+9v3/QFNR2MXk/YBDDN6HC+IqHRf0AIWhL+7FyPE0R8sOggHLhU= Received: from AM6P191CA0028.EURP191.PROD.OUTLOOK.COM (2603:10a6:209:8b::41) by AM0PR08MB3585.eurprd08.prod.outlook.com (2603:10a6:208:d9::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3174.20; Tue, 14 Jul 2020 09:08:31 +0000 Received: from AM5EUR03FT042.eop-EUR03.prod.protection.outlook.com (2603:10a6:209:8b:cafe::44) by AM6P191CA0028.outlook.office365.com (2603:10a6:209:8b::41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3174.21 via Frontend Transport; Tue, 14 Jul 2020 09:08:31 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; gcc.gnu.org; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;gcc.gnu.org; dmarc=bestguesspass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by AM5EUR03FT042.mail.protection.outlook.com (10.152.17.168) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3174.21 via Frontend Transport; Tue, 14 Jul 2020 09:08:31 +0000 Received: ("Tessian outbound 1dc58800d5dd:v62"); Tue, 14 Jul 2020 09:08:31 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 0c56d9ac4828f5cb X-CR-MTA-TID: 64aa7808 Received: from 69c74ffed87e.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id B2F539D0-FF54-4EF6-9CE0-AD7CC40550DC.1; Tue, 14 Jul 2020 09:08:25 +0000 Received: from EUR05-AM6-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 69c74ffed87e.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Tue, 14 Jul 2020 09:08:25 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WFuppwdIdcMq2YlzVTwBUgPV/fZXMazhj1hsPbybVMRh6dzhNQQBjU2hVWqwB8BwK6TSnryJvovLrtJpAKERe2sr3MZ61oK4DwlcUVgj9SUL6SuGch68ZNl+xjx31cPQTXcEDjtNMCHBHZAxMbMIgzwK0+QwNJnscX8Rvg1nCsWEtLabCuQ0g6FuKWM42EAKT4p0JtfGz6xYXHY4OBv2atfkhxSZUEpp5/D8P5UZ/XXVEg2zJluQbN4vSsoErWVM9VApBqdm64OFYcBbjLStp2PLDXBxjfvBnMQDSvb1cgvCnuJ4gV/HouHvuKBFry/6vvcymx8DinWHLw70GcSptg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=SfPN/yf5FPk59vztcjWo47qNPdnuBdWmNyHSxcJzgs8=; b=ScSlsFNDjQzq5Id1Du+xWu9DadoCofbTMqx9RwaKmdxaHjOEnCS/LK4etGmn8k2qckQm9hJ3wqgQ06iYwazI4/ue0y0ORDjA6kbOjkCsc3WlZYxRCarPN7Nb22Gfw+S7G9EUR/Q0d+WOb2bPmU10SmdPsIBdHJSX7lmRyJrUpu9px9IOaPSHkYoRVDaRZ/ovbslirODMRccQPH2qSw6iImU2x9cIni8ISsMY3x7RDWfxDP2SBMTc2L3jBCZdiltkFD2pkm39w/G8hxJT4D8D3HGl4PBf/jiOuR8LpSiio/Xc6ZJg/zb17snLD7pMcwUdmM2FdAGZAJBImkaBKbttpQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=arm.com; dmarc=pass action=none header.from=arm.com; dkim=pass header.d=arm.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=SfPN/yf5FPk59vztcjWo47qNPdnuBdWmNyHSxcJzgs8=; b=onF/4BbuA7K0w6sGu4BoKJgtzrUS4IWayVD5pCQskwFPXMmN8JDzAAW7l/NxmfOrU4Dny0jpAXhaUFExCrQ/UfFYo3/tAaLonNgdNbkU2wsZRVZEfdHYljyq+9v3/QFNR2MXk/YBDDN6HC+IqHRf0AIWhL+7FyPE0R8sOggHLhU= Received: from VI1PR08MB4029.eurprd08.prod.outlook.com (2603:10a6:803:ec::14) by VI1PR08MB3663.eurprd08.prod.outlook.com (2603:10a6:803:85::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3174.20; Tue, 14 Jul 2020 09:08:23 +0000 Received: from VI1PR08MB4029.eurprd08.prod.outlook.com ([fe80::b8ed:5041:249f:6770]) by VI1PR08MB4029.eurprd08.prod.outlook.com ([fe80::b8ed:5041:249f:6770%2]) with mapi id 15.20.3174.026; Tue, 14 Jul 2020 09:08:23 +0000 From: Alex Coplan To: "gcc-patches@gcc.gnu.org" , "jit@gcc.gnu.org" Subject: [PATCH] driver: Fix several memory leaks Thread-Topic: [PATCH] driver: Fix several memory leaks Thread-Index: AdZZvinvo2PdI08aTL+A2RwtDLJPQg== Date: Tue, 14 Jul 2020 09:08:23 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: yes X-MS-TNEF-Correlator: Authentication-Results-Original: gcc.gnu.org; dkim=none (message not signed) header.d=none;gcc.gnu.org; dmarc=none action=none header.from=arm.com; x-originating-ip: [80.1.50.43] x-ms-publictraffictype: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: cd0905f0-402c-41a6-eb6a-08d827d57aad x-ms-traffictypediagnostic: VI1PR08MB3663:|AM0PR08MB3585: X-Microsoft-Antispam-PRVS: x-checkrecipientrouted: true nodisclaimer: true x-ms-oob-tlc-oobclassifiers: OLM:4941;OLM:4941; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: bs7a6NYiVeTJLK9+6nHwcBrqNsCVmwzXu8nO/RlUx0AX8igL9H4NgWAEExotCnT9gqq2/hD0Nh12YuCVnxJg6ftXABgnt3O7n177RGBA4b9cd30bTIdNuSCX00oG0Wdm1r4wRNgMujHkmQE6qh5r7IT/Bdwm84NGM390jKj+5OhDxmymjcRVnaryR+XMEUrydLAidXG/qE+sOxNnsWPWHLZJJZ1fbOLLv8+i6NIRtbZgE2hbPOLEkFPaVOanYTa8tVjbMdmbm7GhCsxST5oTY7d2Zz9WKuuQC038nmSaP5Y7P2K9ZE4fkdruKfDD1ARgqt6L9UJkDj3m9yo4RqKC/g== X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:VI1PR08MB4029.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFTY:; SFS:(4636009)(366004)(346002)(376002)(39860400002)(136003)(396003)(66446008)(66946007)(76116006)(66616009)(66476007)(66556008)(64756008)(2906002)(450100002)(86362001)(8936002)(8676002)(478600001)(71200400001)(83380400001)(7696005)(33656002)(9686003)(6506007)(53546011)(52536014)(316002)(186003)(55016002)(99936003)(5660300002)(26005)(110136005); DIR:OUT; SFP:1101; x-ms-exchange-antispam-messagedata: FDHzAG1Sn/r9P608VOWIWvGzIr1hIgg9PZLXGrc16+8WXnaLdGiu59zeD94s1/QNVi5nX9JmEU1essqI5lPnKPk/AQSOwezFJEEuBpAaIbqLyNDXy5uqSrYrxaiW/k4x60DH/gelTDG/LCbtHdmDokf4LrAVL5bOLtuxE5r7WhtRXZYW+7JZkdeEWhmxF4Rmu6sP4ihjdGXXgSjeAFi81O83g2+5zGuwzEaYEO7+hr9ZJzxUC2xexLdJblFgcX5smX/+0QaP5WiAs+16oz78vLEc4vnqjvFM+dlMvF+UlUoQ+/pQZ6cgYm8a+PiAujReSORWXUNILiFub2f/unK4VTIvl8l2jGBO3lf891dX4p/h8+vvtW+2KbPE37qJL+GMpl5eIPai4aS6LQSJfPe4S9fSR15XRyGzNTAWj/8z2BthdPGc49g2hhFC/aVkIgY2VZTRoLXj5uZW6UVHObVMJ1ODJYYhh7MykDl3eZsSeuI= x-ms-exchange-transport-forked: True MIME-Version: 1.0 X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR08MB3663 Original-Authentication-Results: gcc.gnu.org; dkim=none (message not signed) header.d=none;gcc.gnu.org; dmarc=none action=none header.from=arm.com; X-EOPAttributedMessage: 0 X-MS-Exchange-Transport-CrossTenantHeadersStripped: AM5EUR03FT042.eop-EUR03.prod.protection.outlook.com X-Forefront-Antispam-Report: CIP:63.35.35.123; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:64aa7808-outbound-1.mta.getcheckrecipient.com; PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com; CAT:NONE; SFTY:; SFS:(4636009)(136003)(376002)(346002)(396003)(39860400002)(46966005)(8936002)(70586007)(33656002)(26005)(45080400002)(82310400002)(53546011)(52536014)(6506007)(66616009)(70206006)(21480400003)(83380400001)(33964004)(356005)(81166007)(186003)(235185007)(36906005)(316002)(5660300002)(47076004)(55016002)(9686003)(450100002)(336012)(2906002)(99936003)(478600001)(7696005)(8676002)(110136005)(82740400003)(86362001); DIR:OUT; SFP:1101; X-MS-Office365-Filtering-Correlation-Id-Prvs: d8d101fd-6f26-4a3b-6c35-08d827d57655 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YP7EYWakC9bv4iZmZpNumHHsM8qIFRHCeK4NKAgG7AAZZjwxf7iTlCVBfIDoUek/obntmqXxxlMBRMHfChec8kW0ZWEhQohWJq+JjmKD2wqTR0lyOrcjDnQVjhqYSeBjk9YEMrHmo2fWSvtQJnSJ95iEFOGzlnMuxRwFb1q40cP7iDch+w4oeTa3VYHdIN9G8AhvNgspnmTi3oxUb8Ex21OEk43kzbHvqpBRjP4pm56Ekci2w5ks3DlFT0tHGOFuRkmuaQrpSKgSLRqvIHhIUp56aHCkQbU0S1VELY5LX5e8BsBsHovZfIN+QN5NU7xduzeHiaCZQvprp1D996vZj0vfiMbMJRK9+j9BmbM04Sad2xxyYYcywg/X32FdXR7Y2nw70w5QSdfltg1o1FNmKg== X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Jul 2020 09:08:31.1831 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: cd0905f0-402c-41a6-eb6a-08d827d57aad X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d; Ip=[63.35.35.123]; Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-AuthSource: AM5EUR03FT042.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR08MB3585 X-Spam-Status: No, score=-16.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Updating the subject since this is really just a driver change (and therefore needs a review from those who can approve patches there). Thanks, Alex -----Original Message----- From: Jit On Behalf Of Alex Coplan Sent: 09 July 2020 21:13 To: gcc-patches@gcc.gnu.org; jit@gcc.gnu.org Cc: nd Subject: [PATCH] libgccjit: Fix several memory leaks in the driver Hello, This patch fixes several memory leaks in the driver, all of which relate to the handling of static specs. We introduce functions set_static_spec_{shared,owned}() which are used to enforce proper memory management when updating the strings in the static_specs table. This is achieved by making use of the alloc_p field in the table entries. Similarly to set_spec(), each time we update an entry, we check whether alloc_p is set, and free the old value if so. We then set alloc_p correctly based on whether we "own" this memory or whether we're just taking a pointer to a shared string which we shouldn't free. The following table shows the number of leaks found by AddressSanitizer when running a minimal libgccjit program on AArch64. The test program does the whole libgccjit compilation cycle in a loop (including acquiring and releasing the context), and the table below shows the number of leaks for different iterations of that loop. +--------------+-----+-----+------+---------------+ | # of runs > | 1 | 2 | 3 | Leaks per run | +--------------+-----+-----+------+---------------+ | Before patch | 463 | 940 | 1417 | 477 | +--------------+-----+-----+------+---------------+ | After patch | 416 | 846 | 1276 | 430 | +--------------+-----+-----+------+---------------+ Ensuring that we minimize "leaks per run" (ultimately eliminating all of them) is important in order for long-running applications to be able to make use of in-process libgccjit. Testing: * Bootstrap and regtest on aarch64-linxu-gnu, x86_64-linux-gnu. * Bootstrap and regtest on aarch64-linux-gnu with bootstrap-asan config. * Smoke test of libgccjit, ran regressions on a --disable-bootstrap build on aarch64-linux-gnu. OK for master? Thanks, Alex --- gcc/ChangeLog: 2020-07-09 Alex Coplan * gcc.c (set_static_spec): New. (set_static_spec_owned): New. (set_static_spec_shared): New. (driver::maybe_putenv_COLLECT_LTO_WRAPPER): Use set_static_spec_owned() to take ownership of lto_wrapper_file such that it gets freed in driver::finalize. (driver::maybe_run_linker): Use set_static_spec_shared() to ensure that we don't try and free() the static string "ld", also ensuring that any previously-allocated string in linker_name_spec is freed. Likewise with argv0. (driver::finalize): Use set_static_spec_shared() when resetting specs that previously had allocated strings; remove if(0) around call to free(). diff --git a/gcc/gcc.c b/gcc/gcc.c index c0eb3c10cfd..f839971d42d 100644 --- a/gcc/gcc.c +++ b/gcc/gcc.c @@ -1908,6 +1908,47 @@ init_spec (void) specs = sl; } + +static void +set_static_spec (const char **spec, const char *value, bool alloc_p) +{ + struct spec_list *sl = NULL; + + for (unsigned i = 0; i < ARRAY_SIZE (static_specs); i++) + { + if (static_specs[i].ptr_spec == spec) + { + sl = static_specs + i; + break; + } + } + + gcc_assert (sl); + + if (sl->alloc_p) + { + const char *old = *spec; + free (const_cast (old)); + } + + *spec = value; + sl->alloc_p = alloc_p; +} + +/* Update a static spec to a new string, taking ownership of that + string's memory. */ +static void set_static_spec_owned (const char **spec, const char *val) +{ + return set_static_spec (spec, val, true); +} + +/* Update a static spec to point to a new value, but don't take + ownership of (i.e. don't free) that string. */ +static void set_static_spec_shared (const char **spec, const char *val) +{ + return set_static_spec (spec, val, false); +} + /* Change the value of spec NAME to SPEC. If SPEC is empty, then the spec is removed; If the spec starts with a + then SPEC is added to the end of the @@ -8333,7 +8374,7 @@ driver::maybe_putenv_COLLECT_LTO_WRAPPER () const if (lto_wrapper_file) { lto_wrapper_file = convert_white_space (lto_wrapper_file); - lto_wrapper_spec = lto_wrapper_file; + set_static_spec_owned (<o_wrapper_spec, lto_wrapper_file); obstack_init (&collect_obstack); obstack_grow (&collect_obstack, "COLLECT_LTO_WRAPPER=", sizeof ("COLLECT_LTO_WRAPPER=") - 1); @@ -8840,7 +8881,7 @@ driver::maybe_run_linker (const char *argv0) const { char *s = find_a_file (&exec_prefixes, "collect2", X_OK, false); if (s == NULL) - linker_name_spec = "ld"; + set_static_spec_shared (&linker_name_spec, "ld"); } #if HAVE_LTO_PLUGIN > 0 @@ -8864,7 +8905,7 @@ driver::maybe_run_linker (const char *argv0) const linker_plugin_file_spec = convert_white_space (temp_spec); } #endif - lto_gcc_spec = argv0; + set_static_spec_shared (<o_gcc_spec, argv0); } /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables @@ -10806,9 +10847,9 @@ driver::finalize () just_machine_suffix = 0; gcc_exec_prefix = 0; gcc_libexec_prefix = 0; - md_exec_prefix = MD_EXEC_PREFIX; - md_startfile_prefix = MD_STARTFILE_PREFIX; - md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1; + set_static_spec_shared (&md_exec_prefix, MD_EXEC_PREFIX); + set_static_spec_shared (&md_startfile_prefix, MD_STARTFILE_PREFIX); + set_static_spec_shared (&md_startfile_prefix_1, MD_STARTFILE_PREFIX_1); multilib_dir = 0; multilib_os_dir = 0; multiarch_dir = 0; @@ -10832,8 +10873,7 @@ driver::finalize () spec_list *sl = &static_specs[i]; if (sl->alloc_p) { - if (0) - free (const_cast (*(sl->ptr_spec))); + free (const_cast (*(sl->ptr_spec))); sl->alloc_p = false; } *(sl->ptr_spec) = sl->default_ptr;