From patchwork Mon Jul 19 14:33:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 44388 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 34D73395100D for ; Mon, 19 Jul 2021 14:35:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 34D73395100D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1626705334; bh=AnWO7G/4wHiuy9iKUehfCFJ7WTZDENm35V2y/B8/evw=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=ij2W2EyCKWRS2HAkvZE2cOUJ+tduh3MMWqyl5oi57gMBZWxeGXW3OGqKIRJbnNxIX GUIQIKizFmK0r1qTplE1rYqWMtwBclyvdv2z4cfk8QhstUQMoY3MDGJDFj9glX/DzE VURfsoqZw94c4go7InMVlEfkar7y2h7kMBamu0I4= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by sourceware.org (Postfix) with ESMTPS id 182C63951C2A for ; Mon, 19 Jul 2021 14:33:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 182C63951C2A Received: by mail-pl1-x636.google.com with SMTP id c15so9660560pls.13 for ; Mon, 19 Jul 2021 07:33:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=AnWO7G/4wHiuy9iKUehfCFJ7WTZDENm35V2y/B8/evw=; b=StB+Zf7cIxweeEpdyd6lMK8cFy/NkJPKSQI1ypsuhynUL6fsRepJC9yGUGbfIfgMFV jmBJs7yioDa/qoHt8XF9QZAOs7Wyov9qK7lZK9im2IzC066GqFgaVPLV8bb5ZdiUdwcu HIcuBhFx1MPipyUPh2+B8t7qcTOgUwaWDLI4T53MOLaEpfB0opc/+l4JPIEWDb2oSiHK WMGWO9uwt93QYqugYjKKxkuCuLIrj2agMN70g2f4bvCnSsrbAVMWKrkYsqqmKuoJWDUS nC3S/awSUgsXgTqZVpv2cpjDH6Id5PP6EgcxxpVxn6BA5PpTmn8IRNQUOKlo2qZmOP3Q X78A== X-Gm-Message-State: AOAM530SoC4ifuviULLSZT0w+QMNCPdQrTtMGbgjb/Qzu4b/qMzZYJan lIjNgo/0bOUJufpm5USUe2C+cPiOabLM8w== X-Google-Smtp-Source: ABdhPJyzCPDxpDyGhhXTMQFotTXyWkgWrIKktrkj9tk7gWqDfnlMijD+pveDTRja1uhR98By8A7YjA== X-Received: by 2002:a17:902:7246:b029:129:dcf5:b583 with SMTP id c6-20020a1709027246b0290129dcf5b583mr19410778pll.73.1626705211928; Mon, 19 Jul 2021 07:33:31 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:1133:c8a4:b05d:5ba9:3665]) by smtp.gmail.com with ESMTPSA id y13sm21461214pgp.16.2021.07.19.07.33.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Jul 2021 07:33:31 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v2 2/6] elf: Avoid unnecessary slowdown from profiling with audit (BZ#15533) Date: Mon, 19 Jul 2021 11:33:05 -0300 Message-Id: <20210719143309.2848878-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210719143309.2848878-1-adhemerval.zanella@linaro.org> References: <20210719143309.2848878-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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: 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: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Reply-To: Adhemerval Zanella Cc: Alexander Monakov , John Mellor-Crummey Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" The rtld-audit interfaces introduces a slowdown due to enabling profiling instrumentation (as if LD_AUDIT implied LD_PROFILE). However, instrumenting is only necessary if one of audit libraries provides PLT (la_plt{enter,exit} symbols). Otherwise, the slowdown can be avoided. The following patch adjusts the logic that enables profiling to iterate over all audit modules and check if any of those provides a PLT hook. Co-authored-by: Alexander Monakov --- NEWS | 4 ++++ elf/Makefile | 6 +++++- elf/dl-reloc.c | 12 +++++++++++- elf/rtld.c | 8 +------- elf/tst-audit18.c | 39 +++++++++++++++++++++++++++++++++++++++ elf/tst-auditmod18.c | 24 ++++++++++++++++++++++++ include/link.h | 2 ++ 7 files changed, 86 insertions(+), 9 deletions(-) create mode 100644 elf/tst-audit18.c create mode 100644 elf/tst-auditmod18.c diff --git a/NEWS b/NEWS index 13ffe627da..8fde312ec6 100644 --- a/NEWS +++ b/NEWS @@ -76,6 +76,10 @@ Major new features: equal to a giver integer. This function is a GNU extension, although Solaris also provides a similar function. +* The audit libraries will avoid unnecessary slowdown if it is not required + either PLT tracking or symbol binding profiling (enabled with LA_FLG_BINDFROM + or LA_FLG_BINDTO from la_objopen() callback). + Deprecated and removed features, and other changes affecting compatibility: * The function pthread_mutex_consistent_np has been deprecated; programs diff --git a/elf/Makefile b/elf/Makefile index 3216d67bb4..9e2d766c03 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -219,7 +219,7 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \ tst-dlopen-self tst-auditmany tst-initfinilazyfail tst-dlopenfail \ tst-dlopenfail-2 \ tst-filterobj tst-filterobj-dlopen tst-auxobj tst-auxobj-dlopen \ - tst-audit14 tst-audit15 tst-audit16 tst-audit17 \ + tst-audit14 tst-audit15 tst-audit16 tst-audit17 tst-audit18 \ tst-single_threaded tst-single_threaded-pthread \ tst-tls-ie tst-tls-ie-dlmopen argv0test \ tst-glibc-hwcaps tst-glibc-hwcaps-prepend tst-glibc-hwcaps-mask \ @@ -301,6 +301,7 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \ tst-unique1mod1 tst-unique1mod2 \ tst-unique2mod1 tst-unique2mod2 \ tst-auditmod9a tst-auditmod9b \ + tst-auditmod18 \ $(if $(CXX),tst-unique3lib tst-unique3lib2 tst-unique4lib \ tst-nodelete-uniquemod tst-nodelete-rtldmod \ tst-nodelete-zmod \ @@ -1489,6 +1490,9 @@ $(objpfx)tst-auditmod17.so: $(objpfx)tst-auditmod17.os CFLAGS-.os += $(call elide-stack-protector,.os,tst-auditmod17) tst-audit17-ENV = LD_AUDIT=$(objpfx)tst-auditmod17.so +$(objpfx)tst-audit18.out: $(objpfx)tst-auditmod18.so +tst-audit18-ENV = LD_AUDIT=$(objpfx)tst-auditmod18.so + # tst-sonamemove links against an older implementation of the library. LDFLAGS-tst-sonamemove-linkmod1.so = \ -Wl,--version-script=tst-sonamemove-linkmod1.map \ diff --git a/elf/dl-reloc.c b/elf/dl-reloc.c index e13a672ade..2abcfc996f 100644 --- a/elf/dl-reloc.c +++ b/elf/dl-reloc.c @@ -181,7 +181,17 @@ _dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[], #ifdef SHARED /* If we are auditing, install the same handlers we need for profiling. */ if ((reloc_mode & __RTLD_AUDIT) == 0) - consider_profiling |= GLRO(dl_audit) != NULL; + { + struct audit_ifaces *afct = GLRO(dl_audit); + for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt) + { + /* Profiling is needed only if PLT hooks are provided. */ + if (afct->ARCH_LA_PLTENTER != NULL + || afct->ARCH_LA_PLTEXIT != NULL) + consider_profiling = 1; + afct = afct->next; + } + } #elif defined PROF /* Never use dynamic linker profiling for gprof profiling code. */ # define consider_profiling 0 diff --git a/elf/rtld.c b/elf/rtld.c index d733359eaf..374bf86a69 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -1013,13 +1013,7 @@ ERROR: audit interface '%s' requires version %d (maximum supported version %d); "la_objsearch\0" "la_objopen\0" "la_preinit\0" -#if __ELF_NATIVE_CLASS == 32 - "la_symbind32\0" -#elif __ELF_NATIVE_CLASS == 64 - "la_symbind64\0" -#else -# error "__ELF_NATIVE_CLASS must be defined" -#endif + LA_SYMBIND "\0" #define STRING(s) __STRING (s) "la_" STRING (ARCH_LA_PLTENTER) "\0" "la_" STRING (ARCH_LA_PLTEXIT) "\0" diff --git a/elf/tst-audit18.c b/elf/tst-audit18.c new file mode 100644 index 0000000000..36b781f9be --- /dev/null +++ b/elf/tst-audit18.c @@ -0,0 +1,39 @@ +/* Check if DT_AUDIT a module without la_plt{enter,exit} symbols does not incur + in profiling (BZ#15533). + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +/* We interpose the profile resolver and if it is called it means profiling is + enabled. */ +void +_dl_runtime_profile (ElfW(Word) addr) +{ + volatile int *p = NULL; + *p = 0; +} + +static int +do_test (void) +{ + printf ("..."); + return 0; +} + +#include diff --git a/elf/tst-auditmod18.c b/elf/tst-auditmod18.c new file mode 100644 index 0000000000..6d1b55223e --- /dev/null +++ b/elf/tst-auditmod18.c @@ -0,0 +1,24 @@ +/* Check if DT_ADIT a module without la_plt{enter,exit} symbols does not incur + in profiling (BZ#15533). + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +unsigned int +la_version (unsigned int version) +{ + return version; +} diff --git a/include/link.h b/include/link.h index 4af16cb596..ebd0f511e2 100644 --- a/include/link.h +++ b/include/link.h @@ -355,8 +355,10 @@ struct auditstate #if __ELF_NATIVE_CLASS == 32 # define symbind symbind32 +# define LA_SYMBIND "la_symbind32" #elif __ELF_NATIVE_CLASS == 64 # define symbind symbind64 +# define LA_SYMBIND "la_symbind64" #else # error "__ELF_NATIVE_CLASS must be defined" #endif