From patchwork Tue Jun 22 13:27:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 43957 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 684B5389443E for ; Tue, 22 Jun 2021 13:29:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 684B5389443E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1624368565; bh=VvVDun/L8O1UNnRnGcYoUB5WFiSA7hAhA+ikII/617U=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=RQpJE0Qf60r3ZfmpNPDMiLOKERT2yX5bHcld2U/svGeVlrZO3GEHLQUrRoMOow2MZ HgZ2EYlbnEKIu3swSVR3dE1QvvYdXPv4pE9lbtS3XdZ56aT9wa85NSduCwGsvZyxF5 gg07Xe56DBe3DFuDWP3jOMrP2kGIrgt4CeSDNqNg= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.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 EACEE3896820 for ; Tue, 22 Jun 2021 13:27:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org EACEE3896820 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-112-vo1Jyja6O7G8ZdcZcrTzyQ-1; Tue, 22 Jun 2021 09:27:53 -0400 X-MC-Unique: vo1Jyja6O7G8ZdcZcrTzyQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 41B6419067E0; Tue, 22 Jun 2021 13:27:52 +0000 (UTC) Received: from oldenburg.str.redhat.com (ovpn-112-211.ams2.redhat.com [10.36.112.211]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3C19960854; Tue, 22 Jun 2021 13:27:51 +0000 (UTC) To: libc-alpha@sourceware.org Subject: [PATCH] elf: Fix glibc-hwcaps priorities with cache flags mismatches [BZ #27046] Date: Tue, 22 Jun 2021 15:27:49 +0200 Message-ID: <87wnqmc9m2.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.6 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_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Florian Weimer via Libc-alpha From: Florian Weimer Reply-To: Florian Weimer Cc: schwab@linux-m68k.org Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" If lib->flags (in the cache) did not match GLRO (dl_correct_cache_id), searching for further glibc-hwcaps entries did not happen, and it was possible that the best glibc-hwcaps was not found. By accident, this causes a test failure for elf/tst-glibc-hwcaps-prepend-cache on armv7l. This commit changes the cache lookup logic to continue searching if (a) no match has been found, (b) a named glibc-hwcaps match has been found(), or (c) non-glibc-hwcaps match has been found and the entry flags and cache default flags do not match. _DL_CACHE_DEFAULT_ID is used instead of GLRO (dl_correct_cache_id) because the latter is only written once on i386 if loading of libc.so.5 libraries is selected, so GLRO (dl_correct_cache_id) should probably removed in a future change. Tested on i686-linux-gnu, x86_64-linux-gnu, and on an armv7l system that exposed the issue. Most of the diff is due to whitespace changes. Reviewed-by: Szabolcs Nagy --- elf/dl-cache.c | 134 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 65 insertions(+), 69 deletions(-) diff --git a/elf/dl-cache.c b/elf/dl-cache.c index 32f3bef5ea..2b8da8650d 100644 --- a/elf/dl-cache.c +++ b/elf/dl-cache.c @@ -269,81 +269,77 @@ search_cache (const char *string_table, uint32_t string_table_size, if (_dl_cache_check_flags (flags) && _dl_cache_verify_ptr (lib->value, string_table_size)) { - if (best == NULL || flags == GLRO (dl_correct_cache_id)) + /* Named/extension hwcaps get slightly different + treatment: We keep searching for a better + match. */ + bool named_hwcap = false; + + if (entry_size >= sizeof (struct file_entry_new)) { - /* Named/extension hwcaps get slightly different - treatment: We keep searching for a better - match. */ - bool named_hwcap = false; + /* The entry is large enough to include + HWCAP data. Check it. */ + struct file_entry_new *libnew + = (struct file_entry_new *) lib; - if (entry_size >= sizeof (struct file_entry_new)) +#ifdef SHARED + named_hwcap = dl_cache_hwcap_extension (libnew); + if (named_hwcap + && !dl_cache_hwcap_isa_level_compatible (libnew)) + continue; +#endif + + /* The entries with named/extension hwcaps have + been exhausted (they are listed before all + other entries). Return the best match + encountered so far if there is one. */ + if (!named_hwcap && best != NULL) + break; + + if ((libnew->hwcap & hwcap_exclude) && !named_hwcap) + continue; + if (GLRO (dl_osversion) + && libnew->osversion > GLRO (dl_osversion)) + continue; + if (_DL_PLATFORMS_COUNT + && (libnew->hwcap & _DL_HWCAP_PLATFORM) != 0 + && ((libnew->hwcap & _DL_HWCAP_PLATFORM) + != platform)) + continue; + +#ifdef SHARED + /* For named hwcaps, determine the priority and + see if beats what has been found so far. */ + if (named_hwcap) { - /* The entry is large enough to include - HWCAP data. Check it. */ - struct file_entry_new *libnew - = (struct file_entry_new *) lib; - -#ifdef SHARED - named_hwcap = dl_cache_hwcap_extension (libnew); - if (named_hwcap - && !dl_cache_hwcap_isa_level_compatible (libnew)) + uint32_t entry_priority + = glibc_hwcaps_priority (libnew->hwcap); + if (entry_priority == 0) + /* Not usable at all. Skip. */ + continue; + else if (best == NULL + || entry_priority < best_priority) + /* This entry is of higher priority + than the previous one, or it is the + first entry. */ + best_priority = entry_priority; + else + /* An entry has already been found, + but it is a better match. */ continue; -#endif - - /* The entries with named/extension hwcaps - have been exhausted. Return the best - match encountered so far if there is - one. */ - if (!named_hwcap && best != NULL) - break; - - if ((libnew->hwcap & hwcap_exclude) && !named_hwcap) - continue; - if (GLRO (dl_osversion) - && libnew->osversion > GLRO (dl_osversion)) - continue; - if (_DL_PLATFORMS_COUNT - && (libnew->hwcap & _DL_HWCAP_PLATFORM) != 0 - && ((libnew->hwcap & _DL_HWCAP_PLATFORM) - != platform)) - continue; - -#ifdef SHARED - /* For named hwcaps, determine the priority - and see if beats what has been found so - far. */ - if (named_hwcap) - { - uint32_t entry_priority - = glibc_hwcaps_priority (libnew->hwcap); - if (entry_priority == 0) - /* Not usable at all. Skip. */ - continue; - else if (best == NULL - || entry_priority < best_priority) - /* This entry is of higher priority - than the previous one, or it is the - first entry. */ - best_priority = entry_priority; - else - /* An entry has already been found, - but it is a better match. */ - continue; - } -#endif /* SHARED */ } - - best = string_table + lib->value; - - if (flags == GLRO (dl_correct_cache_id) - && !named_hwcap) - /* We've found an exact match for the shared - object and no general `ELF' release. Stop - searching, but not if a named (extension) - hwcap is used. In this case, an entry with - a higher priority may come up later. */ - break; +#endif /* SHARED */ } + + best = string_table + lib->value; + + if (!named_hwcap && flags == _DL_CACHE_DEFAULT_ID) + /* With named hwcaps, we need to keep searching to + see if we find a better match. A better match + is also possible if the flags of the current + entry do not match the expected cache flags. + But if the flags match, no better entry will be + found. */ + break; } } while (++middle <= right);