From patchwork Wed Jan 1 12:34:14 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 103832 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 9BE1F3858415 for ; Wed, 1 Jan 2025 12:37:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9BE1F3858415 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=iy2YEROe X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by sourceware.org (Postfix) with ESMTPS id 51D8C3858401 for ; Wed, 1 Jan 2025 12:34:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 51D8C3858401 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 51D8C3858401 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::102a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1735734859; cv=none; b=xxcZIo7E9VsWTnrqq/Fh4ZwM8sHY7YNnaq6Ru1jZ9E9E/viPznRv+vr6zfANdTbkG2qJB3U/YJDq2fuDSOlqiDyZOzLbJJVaZdocFjIk9RbgO4v6KNSyxuqvaddFsFh3Ae566rARSlJNrZOVPnib+YmktBw4bc678MwUXk1qyqo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1735734859; c=relaxed/simple; bh=rusuOF8thIsc2rpSVWs8H2d7ycIJrefJR1e0EyWzjoY=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=J6ICrXCCHXEPc13rXxiL6vGFyqaAPaDyN0Fscm61YIsHiVeuSqQyUDn1qASOhHPUXFlIHsysBxVmv18W6VQKBeh8+0/EO+og/ki2REv1JIauhPuuu5kmO9/T8ninug5x3zqXphGwW/igUgniRaIG5dUh5t8deT9Kla+NkZBAG4E= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 51D8C3858401 Received: by mail-pj1-x102a.google.com with SMTP id 98e67ed59e1d1-2efe25558ddso11121669a91.2 for ; Wed, 01 Jan 2025 04:34:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735734858; x=1736339658; darn=sourceware.org; h=content-disposition:mime-version:message-id:subject:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=NKReeQ9c3ANjQWBk8n7EJ8ZkzTUAmHUnS8qzjDCJ9EQ=; b=iy2YEROeR65BCjbkak1UltzSDeC4KurlWrXJVHPmAqaYc0AVFDrmpaX7T0SJnBLP8o G6NHb1sug86yy3LmyL3sLn3cTdGbzj/5J2l6oBpk9ajXeOxxPfIJuAAIm5e7HvP2uWe/ VYK7xMCgk+hIvZX2n9VVz6VPziUgVC2AolJ7O8fu+nDpdwbdiTWkfm7sCdy9NgRnCcv7 DrgMd9muXx1a3me9WJwv5QwjYiomTSFekl/pnEQgboqtIf1EqsfXzMeDn7l+sxswLTqz ee/ihvtQOy7aFFuXS9N4tppdy65NZd+uhde8eEu0MxUDdKD1J3ruUeuzNMcUqTQURhgS JM6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735734858; x=1736339658; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NKReeQ9c3ANjQWBk8n7EJ8ZkzTUAmHUnS8qzjDCJ9EQ=; b=Z9pg1T5E1thfbhzYx2Y1xu90P2zT3XpARVzrllaJkFny+hkDZc9dTLAayBf5xRKAqf MYesbpLyMJACkZjTHCHQ7iG3+UW4MTR8VoBn6ghBwoZWCEIJUTxfUPd2GgLHdIwelT6R 1vTZIwbqkhQW1grEti+VZH7SuAfV460kGrObIYKmgS6Ds11/tNqsPgH4nauAir8nmF9n DqmGsYa87MaPYtkoQ+uRlz//I9LHRwF3crONA7u/rp7qovEyMoDUZXk/Z6b6W32lC3ES 9Dn4Eqgg4/1W8wtuBVIX352qfO2pwDqR/+6N4R+fLbpyrpfmaULxc8M5usq4jiVSYlT9 Iy3A== X-Gm-Message-State: AOJu0Yzmunq5b9xaCLU2RQLFMhCQhiEeSs84cTuj6LeqwlBLb7I4ekyb 5w4SyHFzyWtw4qn0weUCdZy03lzfYyRCFYqOts5Aa0V2h7EWlSIlShU4+Q== X-Gm-Gg: ASbGncuz/MikejVs5T4zY6Oj1mV/RGHjJy2IFeCFl+VQ4yMGxLwt53EuGC0lm2oSjDa 2GoQLVpjwO9bpxaeNETkhL9DFc5qyufMmfVXUbEVdkUlvjXIEy6+SXDdiCnIFrzUMuJLzgdAScP a7XOBQgKL3Uw2S8Ovjqc+dUHJaHJISO+kOv0/b99VbfJA1hSVi2bs9gJsA8yJKER4pQj/6aNvOW erl5bDM5Pi/HBCGNPY/sot1gg/MEsh31aJ6J3QxWwzzbYW2Ovsaio2/U/SGt1y0t6e3mA== X-Google-Smtp-Source: AGHT+IEmAYgx4ZijCxtEfHX38oLsDDVBsCNGsvMCe7lixfLppcugo6xr7qQmqHJ6PR1ggUPDmQdMww== X-Received: by 2002:a17:90b:2649:b0:2ee:dd9b:e402 with SMTP id 98e67ed59e1d1-2f452e0e152mr77795902a91.12.1735734858077; Wed, 01 Jan 2025 04:34:18 -0800 (PST) Received: from squeak.grove.modra.org ([2406:3400:51d:8cc0:4d08:cebd:d73f:b794]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2f2db97a6b5sm8481254a91.1.2025.01.01.04.34.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jan 2025 04:34:17 -0800 (PST) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id B9EF5114260D; Wed, 1 Jan 2025 23:04:14 +1030 (ACDT) Date: Wed, 1 Jan 2025 23:04:14 +1030 From: Alan Modra To: binutils@sourceware.org Subject: gas dw2gencfi memory leaks Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3032.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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: binutils@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: binutils-bounces~patchwork=sourceware.org@sourceware.org Some of these could have remained as malloc'd memory, but that would require quite a bit of code to traverse frch_cfi_data for example, and would rely on matching cfi directives (ie. valid input). Just put them on the notes obstack instead. * dw2gencfi.c (alloc_fde_entry): Use notes_calloc. (alloc_cfi_insn_data): Likewise. (cfi_end_fde): Don't free frch_cfi_data. (cfi_add_label): Use notes_strdup. (cfi_add_CFA_remember_state): Use notes_alloc. (cfi_add_CFA_restore_state): Don't free. (dot_cfi_escape): Use notes_alloc. (cfi_finish): Free cies after each pass, not before. Clear out static vars too. diff --git a/gas/dw2gencfi.c b/gas/dw2gencfi.c index 2ca71977117..fbeb697af09 100644 --- a/gas/dw2gencfi.c +++ b/gas/dw2gencfi.c @@ -406,9 +406,9 @@ static struct cie_entry *cie_root; static struct fde_entry * alloc_fde_entry (void) { - struct fde_entry *fde = XCNEW (struct fde_entry); + struct fde_entry *fde = notes_calloc (1, sizeof (*fde)); - frchain_now->frch_cfi_data = XCNEW (struct frch_cfi_data); + frchain_now->frch_cfi_data = notes_calloc (1, sizeof (struct frch_cfi_data)); frchain_now->frch_cfi_data->cur_fde_data = fde; *last_fde_data = fde; last_fde_data = &fde->next; @@ -440,7 +440,7 @@ static struct fde_entry *last_fde; static struct cfi_insn_data * alloc_cfi_insn_data (void) { - struct cfi_insn_data *insn = XCNEW (struct cfi_insn_data); + struct cfi_insn_data *insn = notes_calloc (1, sizeof (*insn)); struct fde_entry *cur_fde_data = frchain_now->frch_cfi_data->cur_fde_data; *cur_fde_data->last = insn; @@ -465,7 +465,6 @@ void cfi_end_fde (symbolS *label) { frchain_now->frch_cfi_data->cur_fde_data->end_address = label; - free (frchain_now->frch_cfi_data); frchain_now->frch_cfi_data = NULL; } @@ -559,12 +558,10 @@ cfi_add_advance_loc (symbolS *label) void cfi_add_label (const char *name) { - unsigned int len = strlen (name) + 1; struct cfi_insn_data *insn = alloc_cfi_insn_data (); insn->insn = CFI_label; - obstack_grow (¬es, name, len); - insn->u.sym_name = (char *) obstack_finish (¬es); + insn->u.sym_name = notes_strdup (name); } /* Add a DW_CFA_offset record to the CFI data. */ @@ -658,7 +655,7 @@ cfi_add_CFA_remember_state (void) cfi_add_CFA_insn (DW_CFA_remember_state); - p = XNEW (struct cfa_save_data); + p = notes_alloc (sizeof (*p)); p->cfa_offset = frchain_now->frch_cfi_data->cur_cfa_offset; p->next = frchain_now->frch_cfi_data->cfa_save_stack; frchain_now->frch_cfi_data->cfa_save_stack = p; @@ -676,7 +673,6 @@ cfi_add_CFA_restore_state (void) { frchain_now->frch_cfi_data->cur_cfa_offset = p->cfa_offset; frchain_now->frch_cfi_data->cfa_save_stack = p->next; - free (p); } else as_bad (_("CFI state restore without previous remember")); @@ -951,7 +947,7 @@ dot_cfi_escape (int ignored ATTRIBUTE_UNUSED) tail = &head; do { - e = XNEW (struct cfi_escape_data); + e = notes_alloc (sizeof (*e)); do_parse_cons_expression (&e->exp, 1); *tail = e; tail = &e->next; @@ -2333,13 +2329,6 @@ cfi_finish (void) ccseg = NULL; seek_next_seg = 0; - for (cie = cie_root; cie; cie = cie_next) - { - cie_next = cie->next; - free ((void *) cie); - } - cie_root = NULL; - for (fde = all_fde_data; fde ; fde = fde->next) { if ((fde->sections & CFI_EMIT_eh_frame) == 0 @@ -2396,6 +2385,13 @@ cfi_finish (void) output_fde (fde, cie, true, first, fde->next == NULL ? EH_FRAME_ALIGNMENT : 2); } + + for (cie = cie_root; cie; cie = cie_next) + { + cie_next = cie->next; + free (cie); + } + cie_root = NULL; } while (EH_FRAME_LINKONCE && seek_next_seg == 2); @@ -2527,13 +2523,6 @@ cfi_finish (void) ccseg = NULL; seek_next_seg = 0; - for (cie = cie_root; cie; cie = cie_next) - { - cie_next = cie->next; - free ((void *) cie); - } - cie_root = NULL; - for (fde = all_fde_data; fde ; fde = fde->next) { if ((fde->sections & CFI_EMIT_debug_frame) == 0) @@ -2572,6 +2561,13 @@ cfi_finish (void) cie = select_cie_for_fde (fde, false, &first, alignment); output_fde (fde, cie, false, first, alignment); } + + for (cie = cie_root; cie; cie = cie_next) + { + cie_next = cie->next; + free (cie); + } + cie_root = NULL; } while (SUPPORT_FRAME_LINKONCE && seek_next_seg == 2); @@ -2579,8 +2575,17 @@ cfi_finish (void) for (fde = all_fde_data; fde ; fde = fde->next) SET_HANDLED (fde, 0); } + all_fde_data = NULL; + last_fde_data = &all_fde_data; + cfi_sections_set = false; + cfi_sections = CFI_EMIT_eh_frame; + all_cfi_sections = 0; + last_fde = NULL; if (dwcfi_hash) - htab_delete (dwcfi_hash); + { + htab_delete (dwcfi_hash); + dwcfi_hash = NULL; + } } #else /* TARGET_USE_CFIPOP */