From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 From: Martin Sebor 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-Type: multipart/mixed; boundary="------------0204B200779D8395C9E205F0" 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-List-Received-Date: Fri, 09 Jul 2021 00:55:30 -0000 This is a multi-part message in MIME format. --------------0204B200779D8395C9E205F0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 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 --------------0204B200779D8395C9E205F0 Content-Type: text/x-patch; charset=UTF-8; name="glibc-thread_self.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="glibc-thread_self.diff" 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 \ --------------0204B200779D8395C9E205F0--