From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [217.70.183.198]) by sourceware.org (Postfix) with ESMTPS id 830103858D1E for ; Wed, 21 Dec 2022 11:01:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 830103858D1E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=seketeli.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=seketeli.org Received: (Authenticated sender: dodji@seketeli.org) by mail.gandi.net (Postfix) with ESMTPSA id 35AD2C0010; Wed, 21 Dec 2022 11:01:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=seketeli.org; s=gm1; t=1671620506; 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: in-reply-to:in-reply-to:references:references; bh=N25zcaCofEKFP/5deg0DCfHBhkPIrA+UD6Rhgr64clQ=; b=RpxVqypjbYcly+oy1rl13mwT++WLG+OyCnRjxmsUwxNbPVc7lNdE8pEvpQmOWW2LPuOt8y i3zff4NXEqPXdn0mAjVl2SNejr/HaVd56PLb/tMBAscNIUlQw0L2nFjijo35PHN4uJvCqm 75jQNel8nmlbjAX6BVQxDOnXNiVemfOZ0gU3kPgIalSiiy+BydW86NoqvOLet4t4u4NOuw TpIIYgqgOIQp2C/04nPiEvb20mzhPQxP7wc2UAyAhPeBIQtfL0oAIdj27xmUMIyJ3hPICO U6CW6QNa4TxEjz58eKIYrK1537xwJKjJmfyWoJnCosZy9MgtlyksU9jdqONqDA== Received: by localhost (Postfix, from userid 1000) id 467FDB5649; Wed, 21 Dec 2022 12:01:44 +0100 (CET) From: Dodji Seketeli To: Xiaole He via Libabigail Cc: Xiaole He , Xiaole He Subject: Re: [PATCH] elf-reader: reclaim fd and mem before break Organization: Me, myself and I References: <20221220130634.9693-1-hexiaole1994@126.com> X-Operating-System: CentOS Stream release 9 X-URL: http://www.seketeli.net/~dodji Date: Wed, 21 Dec 2022 12:01:44 +0100 In-Reply-To: <20221220130634.9693-1-hexiaole1994@126.com> (Xiaole He via Libabigail's message of "Tue, 20 Dec 2022 21:06:34 +0800") Message-ID: <87wn6lc95z.fsf@seketeli.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,JMQ_SPF_NEUTRAL,RCVD_IN_DNSWL_LOW,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 List-Id: Hello Xiaole, Xiaole He via Libabigail a =C3=A9crit: > In 'src/abg-elf-reader.cc': > > /* src/abg-elf-reader.cc begin */ > 1 void > 2 locate_alt_ctf_debug_info() > 3 { > 4 ... > 5 for (const auto& path : rdr.debug_info_root_paths()) > 6 { > 7 ... > 8 int fd; > 9 if ((fd =3D open(file_path.c_str(), O_RDONLY)) =3D=3D -1) > 10 continue; > 11 > 12 ... > 13 Elf *hdl; > 14 if ((hdl =3D elf_begin(fd, ELF_C_READ, nullptr)) =3D=3D nullpt= r) > 15 ... > 16 > 17 alt_ctf_section =3D > 18 elf_helpers::find_section(hdl, ".ctf", SHT_PROGBITS); > 19 break; > 20 > 21 elf_end(hdl); > 22 close(fd); > 23 } > 24 ... > 25 } > /* src/abg-elf-reader.cc end */ > > The file descriptor 'fd' and the memory that 'hdl' pointed to can have > a chance where they were only created but nerver been destroyed when > above code reach the line 19. Thus cause the leakage of file descriptor > and memory. Good catch. > This leakage problem had already occured on our system, and the problem > finally cause process can not open any more file and complaint > 'Errno 24: Too many open files'. Of course. Sorry about that. > > This patch fix above problem. > > * src/abg-elf-reader.cc (locate_alt_ctf_debug_info): > reclaim fd and mem before break. Thanks. > > Signed-off-by: Xiaole He > --- > src/abg-elf-reader.cc | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/src/abg-elf-reader.cc b/src/abg-elf-reader.cc > index c07f0655..979f0aae 100644 > --- a/src/abg-elf-reader.cc > +++ b/src/abg-elf-reader.cc > @@ -453,10 +453,10 @@ struct reader::priv > // unlikely .ctf was designed to be present in stripped file > alt_ctf_section =3D > elf_helpers::find_section(hdl, ".ctf", SHT_PROGBITS); > - break; >=20=20 > elf_end(hdl); > close(fd); > + break; Right, so I have amended the patch somewhat to break out of the loop only if alt_ctf_section has been found. Otherwise, the loop keeps going until all the debug info paths have been explored. Also, if an alt_ctf_section section is already available, locate_alt_ctf_debug_info returns early. The diff of my changes (compared to your patch) is: diff --git a/src/abg-elf-reader.cc b/src/abg-elf-reader.cc index 979f0aae..656418e3 100644 --- a/src/abg-elf-reader.cc +++ b/src/abg-elf-reader.cc @@ -420,6 +420,9 @@ struct reader::priv void locate_alt_ctf_debug_info() { + if (alt_ctf_section) + return; + Elf_Scn *section =3D elf_helpers::find_section(elf_handle, ".gnu_debuglink", @@ -456,7 +459,9 @@ struct reader::priv elf_end(hdl); close(fd); - break; + + if (alt_ctf_section) + break; } } =20 The complete patch I am applying to the master branch is the one below. Many thanks! Cheers, >From 83bbc679e509047f171fa4db9faa0d05cd26a258 Mon Sep 17 00:00:00 2001 From: Xiaole He Date: Tue, 20 Dec 2022 21:06:34 +0800 Subject: [PATCH] elf-reader: reclaim fd and mem before break In elf::reader::priv::locate_alt_ctf_debug_info from src/abg-elf-reader.cc, the resources held by the hdl and fd variables aren't necessary released because the control-flow gets out of the loop too early. This patch fixes the problem. * src/abg-elf-reader.cc (elf::reader::priv::locate_alt_ctf_debug_info): Reclaim fd and mem before break. Also, do not try to locate the debug info it's already been located. Signed-off-by: Xiaole He Signed-off-by: Dodji Seketeli --- src/abg-elf-reader.cc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/abg-elf-reader.cc b/src/abg-elf-reader.cc index c07f0655..656418e3 100644 --- a/src/abg-elf-reader.cc +++ b/src/abg-elf-reader.cc @@ -420,6 +420,9 @@ struct reader::priv void locate_alt_ctf_debug_info() { + if (alt_ctf_section) + return; + Elf_Scn *section =3D elf_helpers::find_section(elf_handle, ".gnu_debuglink", @@ -453,10 +456,12 @@ struct reader::priv // unlikely .ctf was designed to be present in stripped file alt_ctf_section =3D elf_helpers::find_section(hdl, ".ctf", SHT_PROGBITS); - break; =20 elf_end(hdl); close(fd); + + if (alt_ctf_section) + break; } } =20 --=20 2.31.1 --=20 Dodji