From patchwork Fri Apr 1 10:50:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Wielaard X-Patchwork-Id: 52566 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 3E6B83945C1F for ; Fri, 1 Apr 2022 10:50:59 +0000 (GMT) X-Original-To: elfutils-devel@sourceware.org Delivered-To: elfutils-devel@sourceware.org Received: from gnu.wildebeest.org (gnu.wildebeest.org [45.83.234.184]) by sourceware.org (Postfix) with ESMTPS id 270993858D28 for ; Fri, 1 Apr 2022 10:50:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 270993858D28 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org Received: from tarox.wildebeest.org (83-87-18-245.cable.dynamic.v4.ziggo.nl [83.87.18.245]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id B954B30007D1; Fri, 1 Apr 2022 12:50:50 +0200 (CEST) Received: by tarox.wildebeest.org (Postfix, from userid 1000) id 6E35E413CD0E; Fri, 1 Apr 2022 12:50:50 +0200 (CEST) From: Mark Wielaard To: elfutils-devel@sourceware.org Subject: [PATCH] libelf: Return already gotten Elf_Data from elf_getdata_rawchunk Date: Fri, 1 Apr 2022 12:50:39 +0200 Message-Id: <20220401105039.21734-1-mark@klomp.org> X-Mailer: git-send-email 2.18.4 X-Spam-Status: No, score=-9.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, 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: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , Cc: Mark Wielaard Errors-To: elfutils-devel-bounces+patchwork=sourceware.org@sourceware.org Sender: "Elfutils-devel" elf_getdata_rawchunk keeps a list of Elf_Data_Chunk to track which Elf_Data structures have already been requested. This allows elf_end to clean up all internal data structures and the Elf_Data d_buf if it was malloced. But it didn't check if a chunk was already requested earlier. This meant that if for example dwelf_elf_gnu_build_id was called multiple times to lookup a build-id from the phdrs a new Elf_Data_Chunk was created. This could slowly leak memory. So also keep track of the offset from which the size and type of the rawdata was requested so we can return existing data if it is requested multiple times. Note that the current cache is a simple linked list but the chain is normally not that long. It is normally used to get chunks from the phdrs, and there are normally less than 10. Signed-off-by: Mark Wielaard --- libelf/ChangeLog | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/libelf/ChangeLog b/libelf/ChangeLog index 7fd6202b..73917f2b 100644 --- a/libelf/ChangeLog +++ b/libelf/ChangeLog @@ -1,3 +1,10 @@ +2022-04-01 Mark Wielaard + + * libelfP.h (struct Elf_Data_Chunk): Add an int64_t offset field. + * elf_getdata_rawchunk.c (elf_getdata_rawchunk): Check whether the + requested chunk, offset, size and type, was already handed out. + Set new Elf_Data_Chunk offset field. + 2022-03-24 Mark Wielaard * elf.h: Update from glibc.