From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mo4-p00-ob.smtp.rzone.de (mo4-p00-ob.smtp.rzone.de [81.169.146.221]) by sourceware.org (Postfix) with ESMTPS id 7CB5B3857835; Fri, 11 Mar 2022 16:31:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7CB5B3857835 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nieper-wisskirchen.de Authentication-Results: sourceware.org; spf=none smtp.mailfrom=nieper-wisskirchen.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1647016280; s=strato-dkim-0002; d=nieper-wisskirchen.de; h=Cc:To:Subject:Message-ID:Date:From:In-Reply-To:References:Cc:Date: From:Subject:Sender; bh=rW6MYd5q/ymCvaylIYaZZdHLT2EQThn+uMZZQDHKoiY=; b=TPSIayBnPFpYWn/uuE659wJyWtCSNfngVknesodRIZVD3165w/QQMX9EsdPdX9JzMA Wupcvi59/DetzdgMjsJRZRV/jV4QfCZaDNMIbdLYkOrBP87JWDcjB4jIw7LkM9G1CN/t DbFbhr37oeR3qMBfDY8uiX6rhA6sQBRLQgcpDBTKWZUPZ7epD5l72G1VtXd0YmB2WXkX Zsp2sfzdu2nf+H9zFkt6XNcUqyYLZ1sJ9/xsQUS4Hc7Z284gCiy1IhwyzI8XH9cq7WD4 QNFA/xyRAKqlmZs5eYzOQkGF43TnmA2+S2R5QUGnUNLa6+qf56BhEgUh0dr/R63RCNfi FAmw== Authentication-Results: strato.com; dkim=none X-RZG-AUTH: ":IW0WdmCmcvpIrP2+VJuPtIhjJvc4Ig+QdhX22iZVwSDOx4Kp3cYsBVGy6CZgmO/guIaJVMJ565O6HQ==" X-RZG-CLASS-ID: mo00 Received: from mail-yw1-f169.google.com by smtp.strato.de (RZmta 47.40.1 AUTH) with ESMTPSA id K42a82y2BGVJ6M2 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Fri, 11 Mar 2022 17:31:19 +0100 (CET) Received: by mail-yw1-f169.google.com with SMTP id 00721157ae682-2db569555d6so98898717b3.12; Fri, 11 Mar 2022 08:31:19 -0800 (PST) X-Gm-Message-State: AOAM531wd20/ztEQkD3ou+jFHYbqhZf7w9qz+YhJA5yCeBXMOFJ8Lfwm w/s/kJ9y7NECIjoBL0KCO7NYyMYEZ9uyZoyfLnA= X-Google-Smtp-Source: ABdhPJwrOjXreXWDuL7RKSitIxG7Ii1PH0ibXzlgeuhzDgrwT2Nwct821sfWk4U9ZiTE623haO1nACXYcfNZ/SUydUo= X-Received: by 2002:a81:488d:0:b0:2db:dde2:9e89 with SMTP id v135-20020a81488d000000b002dbdde29e89mr9199698ywa.119.1647016278806; Fri, 11 Mar 2022 08:31:18 -0800 (PST) MIME-Version: 1.0 References: <20211219213010.17113-1-marc@nieper-wisskirchen.de> <2a54fac9b37d87afb009b8eb339d5ad6927454dd.camel@redhat.com> In-Reply-To: From: =?UTF-8?Q?Marc_Nieper=2DWi=C3=9Fkirchen?= Date: Fri, 11 Mar 2022 17:31:06 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] gcc: pass-manager: Fix memory leak. [PR jit/63854] To: Jeff Law Cc: David Malcolm , gcc-patches@gcc.gnu.org, =?UTF-8?Q?Marc_Nieper=2DWi=C3=9Fkirchen_via_Jit?= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-4.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: jit@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Jit mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 11 Mar 2022 16:31:23 -0000 Hi Jeff and David, any news on this fix? Thanks, Marc Am Mo., 31. Jan. 2022 um 12:42 Uhr schrieb Marc Nieper-Wi=C3=9Fkirchen : > > Attached to this email is the patch updated to the recent renaming from *= .c to *.cc. > > > Am So., 23. Jan. 2022 um 14:18 Uhr schrieb Marc Nieper-Wi=C3=9Fkirchen : >> >> Am Sa., 15. Jan. 2022 um 14:56 Uhr schrieb Marc Nieper-Wi=C3=9Fkirchen >> : >> > >> > Jeff, David, do you need any more input from my side? >> > >> > -- Marc >> > >> > Am Sa., 8. Jan. 2022 um 17:32 Uhr schrieb Jeff Law : >> > > >> > > >> > > >> > > On 1/6/2022 6:53 AM, David Malcolm via Gcc-patches wrote: >> > > > On Sun, 2021-12-19 at 22:30 +0100, Marc Nieper-Wi=C3=9Fkirchen wro= te: >> > > >> This patch fixes a memory leak in the pass manager. In the existi= ng >> > > >> code, >> > > >> the m_name_to_pass_map is allocated in >> > > >> pass_manager::register_pass_name, but >> > > >> never deallocated. This is fixed by adding a deletion in >> > > >> pass_manager::~pass_manager. Moreover the string keys in >> > > >> m_name_to_pass_map are >> > > >> all dynamically allocated. To free them, this patch adds a new h= ash >> > > >> trait for >> > > >> string hashes that are to be freed when the corresponding hash en= try >> > > >> is removed. >> > > >> >> > > >> This fix is particularly relevant for using GCC as a library thro= ugh >> > > >> libgccjit. >> > > >> The memory leak also occurs when libgccjit is instructed to use a= n >> > > >> external >> > > >> driver. >> > > >> >> > > >> Before the patch, compiling the hello world example of libgccjit = with >> > > >> the external driver under Valgrind shows a loss of 12,611 (48 dir= ect) >> > > >> bytes. After the patch, no memory leaks are reported anymore. >> > > >> (Memory leaks occurring when using the internal driver are mostly= in >> > > >> the driver code in gcc/gcc.c and have to be fixed separately.) >> > > >> >> > > >> The patch has been tested by fully bootstrapping the compiler wit= h >> > > >> the >> > > >> frontends C, C++, Fortran, LTO, ObjC, JIT and running the test su= ite >> > > >> under a x86_64-pc-linux-gnu host. >> > > > Thanks for the patch. >> > > > >> > > > It looks correct to me, given that pass_manager::register_pass_nam= e >> > > > does an xstrdup and puts the result in the map. >> > > > >> > > > That said: >> > > > - I'm not officially a reviewer for this part of gcc (though I pro= bably >> > > > touched this code last) >> > > > - is it cleaner to instead change m_name_to_pass_map's key type fr= om >> > > > const char * to char *, to convey that the map "owns" the name? T= hat >> > > > way we probably wouldn't need struct typed_const_free_remove, and = (I >> > > > hope) works better with the type system. >> > > > >> > > > Dave >> > > > >> > > >> gcc/ChangeLog: >> > > >> >> > > >> PR jit/63854 >> > > >> * hash-traits.h (struct typed_const_free_remove): New. >> > > >> (struct free_string_hash): New. >> > > >> * pass_manager.h: Use free_string_hash. >> > > >> * passes.c (pass_manager::register_pass_name): Use >> > > >> free_string_hash. >> > > >> (pass_manager::~pass_manager): Delete allocated >> > > >> m_name_to_pass_map. >> > > My concern (and what I hadn't had time to dig into) was we initially >> > > used nofree_string_hash -- I wanted to make sure there wasn't any pa= th >> > > where the name came from the stack (can't be free'd), was saved >> > > elsewhere (danging pointer) and the like. ie, why were we using >> > > nofree_string_hash to begin with? I've never really mucked around w= ith >> > > these bits, so the analysis side kept falling off the daily todo lis= t. >> >> The only occurrences of m_name_to_pass_map are in pass-manager.h >> (where it is defined as a private field of the class pass_manager) and >> in passes.cc. There is just one instance where a name is added to the >> map in passes.cc, namely through the put method. There, the name has >> been xstrdup'ed. >> >> The name (as a const char *) escapes the pass map in >> pass_manager::create_pass_tab through the call to >> m_name_pass_map->traverse. This inserts the name into the pass_tab, >> which is a static vec of const char *s. The pass_tab does not escape >> the translation unit of passes.c. It is used in dump_one_pass where >> the name is used as an argument to fprintf. The important point is >> that it is not freed and not further copied. >> >> > > >> > > If/once you're comfortable with the patch David, then go ahead and a= pply >> > > it on Marc's behalf. >> > > >> > > jeff >> > >