Message ID | 20221220130634.9693-1-hexiaole1994@126.com |
---|---|
State | New |
Headers |
Return-Path: <libabigail-bounces+patchwork=sourceware.org@sourceware.org> 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 6E2FE38582A3 for <patchwork@sourceware.org>; Tue, 20 Dec 2022 13:08:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6E2FE38582A3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1671541738; bh=Z1yqtvbSCV+4vaW7H7AcIbu23Af9CFaVGPKpazd3JIs=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Help:List-Subscribe:From:Reply-To:From; b=Kam0BNklkcjkTp3MbpFyUS3XMwXocY7rwLDU2QUAfK9ELRe2WeEcgpGCcthFPklgX 9/Z7ti0HM6bFOoet3CyP7hLg3FPYyYSMj2u3Jff19a+d7qsC0vPkMtNigK/Wtqd52i VvzuFPddyr8GC5TXoCXhiS/m+ijMcDLUXlOE3MsU= X-Original-To: libabigail@sourceware.org Delivered-To: libabigail@sourceware.org Received: from m126.mail.126.com (m126.mail.126.com [220.181.12.36]) by sourceware.org (Postfix) with ESMTP id EEEE03858421 for <libabigail@sourceware.org>; Tue, 20 Dec 2022 13:08:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org EEEE03858421 Received: from localhost.localdomain (unknown [111.32.120.106]) by zwqz-smtp-mta-g2-0 (Coremail) with SMTP id _____wDHnpJus6Fj1bMWAA--.27965S2; Tue, 20 Dec 2022 21:07:01 +0800 (CST) To: libabigail@sourceware.org Cc: Xiaole He <hexiaole1994@126.com>, Xiaole He <hexiaole@kylinos.cn> Subject: [PATCH] elf-reader: reclaim fd and mem before break Date: Tue, 20 Dec 2022 21:06:34 +0800 Message-Id: <20221220130634.9693-1-hexiaole1994@126.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: _____wDHnpJus6Fj1bMWAA--.27965S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7CFykWw4fXFW8uF4DKr18uFg_yoW8Wr1Dpa y3uw1UKr4UJr1fCry3C3yUXF9Iqayjqa1Uur9I9343tr9xJr9I9rWfJrWfGFWjqFZ7W34Y qr43XrWxAa48Aw7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0pEs2-bUUUUU= X-Originating-IP: [111.32.120.106] X-CM-SenderInfo: 5kh0xt5rohimizu6ij2wof0z/1tbiThzdBmIxk21uXgAAsU X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, 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: libabigail@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Mailing list of the Libabigail project <libabigail.sourceware.org> List-Unsubscribe: <https://sourceware.org/mailman/options/libabigail>, <mailto:libabigail-request@sourceware.org?subject=unsubscribe> List-Archive: <https://sourceware.org/pipermail/libabigail/> List-Help: <mailto:libabigail-request@sourceware.org?subject=help> List-Subscribe: <https://sourceware.org/mailman/listinfo/libabigail>, <mailto:libabigail-request@sourceware.org?subject=subscribe> From: Xiaole He via Libabigail <libabigail@sourceware.org> Reply-To: Xiaole He <hexiaole1994@126.com> Errors-To: libabigail-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libabigail" <libabigail-bounces+patchwork=sourceware.org@sourceware.org> |
Series |
elf-reader: reclaim fd and mem before break
|
|
Commit Message
Xiaole He
Dec. 20, 2022, 1:06 p.m. UTC
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 = open(file_path.c_str(), O_RDONLY)) == -1)
10 continue;
11
12 ...
13 Elf *hdl;
14 if ((hdl = elf_begin(fd, ELF_C_READ, nullptr)) == nullptr)
15 ...
16
17 alt_ctf_section =
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.
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'.
This patch fix above problem.
* src/abg-elf-reader.cc (locate_alt_ctf_debug_info):
reclaim fd and mem before break.
Signed-off-by: Xiaole He <hexiaole@kylinos.cn>
---
src/abg-elf-reader.cc | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Comments
Hello Xiaole, Xiaole He via Libabigail <libabigail@sourceware.org> a écrit: > 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 = open(file_path.c_str(), O_RDONLY)) == -1) > 10 continue; > 11 > 12 ... > 13 Elf *hdl; > 14 if ((hdl = elf_begin(fd, ELF_C_READ, nullptr)) == nullptr) > 15 ... > 16 > 17 alt_ctf_section = > 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 <hexiaole@kylinos.cn> > --- > 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 = > elf_helpers::find_section(hdl, ".ctf", SHT_PROGBITS); > - break; > > 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 = 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; } } 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 <hexiaole@kylinos.cn> 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 <hexiaole@kylinos.cn> Signed-off-by: Dodji Seketeli <dodji@redhat.com> --- 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 = 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 = elf_helpers::find_section(hdl, ".ctf", SHT_PROGBITS); - break; elf_end(hdl); close(fd); + + if (alt_ctf_section) + break; } }
Copy that, thank you for reviewing and amending, looks great! At 2022-12-21 19:01:44, "Dodji Seketeli" <dodji@seketeli.org> wrote: >Hello Xiaole, > >Xiaole He via Libabigail <libabigail@sourceware.org> a écrit: > >> 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 = open(file_path.c_str(), O_RDONLY)) == -1) >> 10 continue; >> 11 >> 12 ... >> 13 Elf *hdl; >> 14 if ((hdl = elf_begin(fd, ELF_C_READ, nullptr)) == nullptr) >> 15 ... >> 16 >> 17 alt_ctf_section = >> 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 <hexiaole@kylinos.cn> >> --- >> 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 = >> elf_helpers::find_section(hdl, ".ctf", SHT_PROGBITS); >> - break; >> >> 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 = > 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; > } > } > >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 <hexiaole@kylinos.cn> >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 <hexiaole@kylinos.cn> >Signed-off-by: Dodji Seketeli <dodji@redhat.com> >--- > 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 = > 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 = > elf_helpers::find_section(hdl, ".ctf", SHT_PROGBITS); >- break; > > elf_end(hdl); > close(fd); >+ >+ if (alt_ctf_section) >+ break; > } > } > >-- >2.31.1 > > >-- > Dodji
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 = elf_helpers::find_section(hdl, ".ctf", SHT_PROGBITS); - break; elf_end(hdl); close(fd); + break; } }