From patchwork Mon Sep 30 16:02:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1991075 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Qin5oZdv; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XHQpj5Bgwz1xtM for ; Tue, 1 Oct 2024 02:03:24 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0B3BC38650F1 for ; Mon, 30 Sep 2024 16:03:22 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id ED9963861035 for ; Mon, 30 Sep 2024 16:02:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org ED9963861035 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org ED9963861035 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727712176; cv=none; b=avNDHNVJrBI5IRKBy0Ck48vUvECFoUsneGjfKRH23nEF05s+Spio0ZNVfFMQWABP0wzkp3ragmHHgdzs7t8wRjeeb66R1m3yqj+mKBz9M97nsZ/gHlD/7am2lS5o+7rLqczzabtHwhi9iSqoRbakQuC0wZVOJQysdsAo410MTU4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727712176; c=relaxed/simple; bh=vjgOT/Owgxd5c9Jkre9dMku5b7X8SQSwJD81O5nZaBA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Atwk5+kkUbEPcH35V0GH0wBd2mpSmFv24e5gPUkwu/SFlgzgr7P377EX4PVBXjSyAUSGNmLBFX+BUYGjyp3yfdhgZxxDDtgibx6V33w26bsu/u/3jX5XM/I+E74zDjngctrqIaDhVzTyQoMMTuZFN2EznKg2IU53WrdZbTHW+iY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1727712173; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=iTUBXDuDr7J2ghvJpLe6Ede7eZBoN1igMxjCUrg3vjw=; b=Qin5oZdvZwSG05zcv8IMaEpJmB3OchP0OOgSt/hPJmXhH005LFr9oy4dk+93DVJOhnb620 v9UfuKby3xvEz+7Vlo7fO6y6Pd8NcWba3acOVu5UglfQXsOR/7/EGMtVfR5CfM1RN09YZY koM1zgGNl7dOjDzaXelAL6nN6VXEvUg= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-288-Ql63eNAuOgaxXntlIeH-Lw-1; Mon, 30 Sep 2024 12:02:52 -0400 X-MC-Unique: Ql63eNAuOgaxXntlIeH-Lw-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (unknown [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 2E27B193EF55 for ; Mon, 30 Sep 2024 16:02:51 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.17.150]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 983661944CF6; Mon, 30 Sep 2024 16:02:50 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed: r15-3971] diagnostics: fix memory leak in SARIF selftests Date: Mon, 30 Sep 2024 12:02:48 -0400 Message-ID: <20240930160248.2857030-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 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_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org "make selftest-valgrind" was complaining about leaks of artifact objects in SARIF's selftest::test_make_location_object: -fself-test: 7638695 pass(es) in 89.999249 seconds ==3306525== ==3306525== HEAP SUMMARY: ==3306525== in use at exit: 1,215,639 bytes in 2,808 blocks ==3306525== total heap usage: 2,860,898 allocs, 2,858,090 frees, 1,336,446,579 bytes allocated ==3306525== ==3306525== 11,728 (1,536 direct, 10,192 indirect) bytes in 16 blocks are definitely lost in loss record 353 of 375 ==3306525== at 0x514FE7D: operator new(unsigned long) (vg_replace_malloc.c:342) ==3306525== by 0x36E5FD2: sarif_builder::get_or_create_artifact(char const*, diagnostic_artifact_role, bool) (diagnostic-format-sarif.cc:2884) ==3306525== by 0x36E3D57: sarif_builder::maybe_make_physical_location_object(unsigned int, diagnostic_artifact_role, int, content_renderer const*) (diagnostic-format-sarif.cc:2097) ==3306525== by 0x36E34CE: sarif_builder::make_location_object(sarif_location_manager&, rich_location const&, logical_location const*, diagnostic_artifact_role) (diagnostic-format-sarif.cc:1922) ==3306525== by 0x36E72C6: selftest::test_make_location_object(selftest::line_table_case const&) (diagnostic-format-sarif.cc:3500) ==3306525== by 0x375609B: selftest::for_each_line_table_case(void (*)(selftest::line_table_case const&)) (input.cc:3898) ==3306525== by 0x36E9668: selftest::diagnostic_format_sarif_cc_tests() (diagnostic-format-sarif.cc:3910) ==3306525== by 0x3592A11: selftest::run_tests() (selftest-run-tests.cc:100) ==3306525== by 0x17DBEF3: toplev::run_self_tests() (toplev.cc:2268) ==3306525== by 0x17DC2BF: toplev::main(int, char**) (toplev.cc:2376) ==3306525== by 0x36A1919: main (main.cc:39) ==3306525== ==3306525== 12,400 (1,536 direct, 10,864 indirect) bytes in 16 blocks are definitely lost in loss record 355 of 375 ==3306525== at 0x514FE7D: operator new(unsigned long) (vg_replace_malloc.c:342) ==3306525== by 0x36E5FD2: sarif_builder::get_or_create_artifact(char const*, diagnostic_artifact_role, bool) (diagnostic-format-sarif.cc:2884) ==3306525== by 0x36E2323: sarif_builder::sarif_builder(diagnostic_context&, line_maps const*, char const*, bool) (diagnostic-format-sarif.cc:1500) ==3306525== by 0x36E70AA: selftest::test_make_location_object(selftest::line_table_case const&) (diagnostic-format-sarif.cc:3469) ==3306525== by 0x375609B: selftest::for_each_line_table_case(void (*)(selftest::line_table_case const&)) (input.cc:3898) ==3306525== by 0x36E9668: selftest::diagnostic_format_sarif_cc_tests() (diagnostic-format-sarif.cc:3910) ==3306525== by 0x3592A11: selftest::run_tests() (selftest-run-tests.cc:100) ==3306525== by 0x17DBEF3: toplev::run_self_tests() (toplev.cc:2268) ==3306525== by 0x17DC2BF: toplev::main(int, char**) (toplev.cc:2376) ==3306525== by 0x36A1919: main (main.cc:39) ==3306525== ==3306525== LEAK SUMMARY: ==3306525== definitely lost: 3,072 bytes in 32 blocks ==3306525== indirectly lost: 21,056 bytes in 368 blocks ==3306525== possibly lost: 0 bytes in 0 blocks ==3306525== still reachable: 1,191,511 bytes in 2,408 blocks ==3306525== suppressed: 0 bytes in 0 blocks ==3306525== Reachable blocks (those to which a pointer was found) are not shown. ==3306525== To see them, rerun with: --leak-check=full --show-leak-kinds=all ==3306525== ==3306525== For lists of detected and suppressed errors, rerun with: -s ==3306525== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0) Fixed thusly. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r15-3971-gab6c7a329d4958. gcc/ChangeLog: * diagnostic-format-sarif.cc (sarif_builder::~sarif_builder): New, deleting any remaining artifact objects. (sarif_builder::make_run_object): Empty the artifact map. * ordered-hash-map.h (ordered_hash_map::empty): New. Signed-off-by: David Malcolm --- gcc/diagnostic-format-sarif.cc | 14 ++++++++++++++ gcc/ordered-hash-map.h | 2 ++ 2 files changed, 16 insertions(+) diff --git a/gcc/diagnostic-format-sarif.cc b/gcc/diagnostic-format-sarif.cc index 6cd18cef6c89..7b11dfd89a31 100644 --- a/gcc/diagnostic-format-sarif.cc +++ b/gcc/diagnostic-format-sarif.cc @@ -649,6 +649,7 @@ public: const line_maps *line_maps, const char *main_input_filename_, bool formatted); + ~sarif_builder (); void on_report_diagnostic (const diagnostic_info &diagnostic, diagnostic_t orig_diag_kind); @@ -1500,6 +1501,18 @@ sarif_builder::sarif_builder (diagnostic_context &context, false); } +sarif_builder::~sarif_builder () +{ + /* Normally m_filename_to_artifact_map will have been emptied as part + of make_run_object, but this isn't run by all the selftests. + Ensure the artifact objects are cleaned up for such cases. */ + for (auto iter : m_filename_to_artifact_map) + { + sarif_artifact *artifact_obj = iter.second; + delete artifact_obj; + } +} + /* Implementation of "on_report_diagnostic" for SARIF output. */ void @@ -2697,6 +2710,7 @@ make_run_object (std::unique_ptr invocation_obj, artifacts_arr->append (artifact_obj); } run_obj->set ("artifacts", std::move (artifacts_arr)); + m_filename_to_artifact_map.empty (); /* "results" property (SARIF v2.1.0 section 3.14.23). */ run_obj->set ("results", std::move (results)); diff --git a/gcc/ordered-hash-map.h b/gcc/ordered-hash-map.h index aa9c86de3989..cb410f29b58b 100644 --- a/gcc/ordered-hash-map.h +++ b/gcc/ordered-hash-map.h @@ -112,6 +112,8 @@ public: size_t elements () const { return m_map.elements (); } + void empty () { m_map.empty(); } + class iterator { public: