From patchwork Fri Jul 9 00:55:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 44288 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 768D93AAA0B2 for ; Fri, 9 Jul 2021 00:55:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 768D93AAA0B2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1625792151; bh=ZqDs2zNRBhCexZ6gnclbdDONFEIKJCFdbXAvi/3C6qI=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=OA2tJmj3ETmLbbLfB4x17H8YIL+/wnysY6iVUq6X1hEoObC3o6iXV/JIWMpiHPFLB uYY7X2/X+4e0CNLmbGTcQ3OTYLXxP1IOhBo/+oUi/Fl3dgnPp6dgTovY249Q5CgGHE ASAmvPNhxm6ckwdyTdLueW3RfTlJuyDMibaW3Bt4= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x834.google.com (mail-qt1-x834.google.com [IPv6:2607:f8b0:4864:20::834]) by sourceware.org (Postfix) with ESMTPS id BC31E3857C66 for ; Fri, 9 Jul 2021 00:55:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BC31E3857C66 Received: by mail-qt1-x834.google.com with SMTP id z18so3037635qtq.8 for ; Thu, 08 Jul 2021 17:55:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language; bh=ZqDs2zNRBhCexZ6gnclbdDONFEIKJCFdbXAvi/3C6qI=; b=LS/IpheEf67/EV5Q019zoTmyJxs9FdDHsSj03OQeDpB3fiLpMAHUAQqSCOH7F1S8fD HKznk+ZW6dZrFWHEuZ/dnKsmTFj9LKHjGNGoBLXMnLhd5snytGrn4xw1bTJWaEmALhn+ ZzEWT2XBYukfc4Y5v1WohTScZq3ni9teP0hJq7VIkNAyjAy0JpMLS3MZVRph8yWwjnfR nxyGjed/nkUJKstMnbdNxhGXrsjs6COgrJoX6uIUdLGuflvK355EDIfCBW4xsDpmVpEy 8FKXkUCSn6gPz8ZMfm9da5hALnHwACX0H1vWYw2X91YXx3fCiI05d1UqPIinhkqodZMA CnkA== X-Gm-Message-State: AOAM533TwLFXYDF134MJ3qKKbQTfB+fgdJ1kom38z+6EUGxEGoj8kufu FSDBta2PeScc6CZatKDHszivRlNqD+8= X-Google-Smtp-Source: ABdhPJzx9T7ktRrjAoLRe5DqObswfUbe5g8gtZCs2gmrySzT+/Z7UoT0oZDwzjrzm61snGlWs59h4g== X-Received: by 2002:ac8:5d47:: with SMTP id g7mr30561688qtx.351.1625792128249; Thu, 08 Jul 2021 17:55:28 -0700 (PDT) Received: from [192.168.0.41] (75-166-102-22.hlrn.qwest.net. [75.166.102.22]) by smtp.gmail.com with ESMTPSA id 7sm1576362qtw.30.2021.07.08.17.55.27 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 08 Jul 2021 17:55:27 -0700 (PDT) To: GNU C Library Subject: [RFC patch] avoid warning on accesses to hardwired address Message-ID: <59b05c40-d0a8-233a-27c5-104b24bdc9b4@gmail.com> Date: Thu, 8 Jul 2021 18:55:26 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 Content-Language: en-US X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, 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: Martin Sebor via Libc-alpha From: Martin Sebor Reply-To: Martin Sebor Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Thanks to recent code refactoring in GCC 12, -Warray-bounds has started to diagnose accesses to constant addresses just like many other flow based warnings do (e.g., -Wstringop-overflow). The warnings are meant to help detect accesses resulting from invalid arithmetic on null pointers. There may be a better way to detect them but GCC doesn't have the detection yet. This warning change has in turn exposed Glibc's uses of this trick in the implementation of the THREAD_SELF macro. I have tried a few approaches to avoid the warning but none worked or seemed satisfactory: 1) Using #pragma GCC diagnostic doesn't work in macros. 2) Using _Pragma doesn't work there either due to a GCC limitation. 3) Declaring the pointer volatile works but prevents accesses to it from being eliminated when the result isn't used (something Glibc apparently cares about). 4) Defining a simple static inline function to wrap the code and the #pragmas doesn't work because the header is #included in files where struct pthread isn't defined. 5) Introducing a global variable with the address and initializing it in some .c file seems too heavy-weight. 6) Falling back on the pre-GCC 6 asm would be safer but seems like a step back. Finally I have come up with the attached solution that combines (4) and (1). If (6) is preferable I'm happy to go that route. If there are other alternatives I'd be glad to consider them as well. Thanks Martin diff --git a/sysdeps/x86_64/nptl/tls.h b/sysdeps/x86_64/nptl/tls.h index a78c4f4d01..a5bd245aa9 100644 --- a/sysdeps/x86_64/nptl/tls.h +++ b/sysdeps/x86_64/nptl/tls.h @@ -180,8 +180,20 @@ _Static_assert (offsetof (tcbhead_t, __glibc_unused2) == 0x80, assignments like pthread_descr self = thread_self(); do not get optimized away. */ -# if __GNUC_PREREQ (6, 0) -# define THREAD_SELF \ + +# if __GNUC_PREREQ (12, 0) +/* Hide access to a hardwided address to avoid GCC -Warray-bounds. */ +static inline void *__thread_self (size_t __off) +{ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Warray-bounds" + return *(void *__seg_fs *) __off; +#pragma GCC diagnostic pop +} +# define THREAD_SELF \ + ((struct pthread *)__thread_self (offsetof (struct pthread, header.self))) +# elif __GNUC_PREREQ (6, 0) +# define THREAD_SELF \ (*(struct pthread *__seg_fs *) offsetof (struct pthread, header.self)) # else # define THREAD_SELF \