From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 7209A385500C for ; Thu, 29 Jul 2021 12:16:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7209A385500C 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-474-_DKymBUiPk2J9od0wscKVA-1; Thu, 29 Jul 2021 08:16:25 -0400 X-MC-Unique: _DKymBUiPk2J9od0wscKVA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id EE9FF107ACF5; Thu, 29 Jul 2021 12:16:24 +0000 (UTC) Received: from oldenburg.str.redhat.com (ovpn-112-7.ams2.redhat.com [10.36.112.7]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3A74260BF1; Thu, 29 Jul 2021 12:16:24 +0000 (UTC) From: Florian Weimer To: gcc@gcc.gnu.org Cc: libc-alpha@sourceware.org Subject: Named address spaces on x86 GNU/Linux Date: Thu, 29 Jul 2021 14:16:22 +0200 Message-ID: <87czr12u3t.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.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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: gcc@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 29 Jul 2021 12:16:28 -0000 The x86-64 architecture supports two instruction prefixes, SEGFS and SEGGS that apply an additional offset to memory operands. The offset lives in a special register that is accessible to the kernel only (historically). On GNU/Linux, SEGFS is used to implement the thread pointer, to avoid dedicating a general-purpose register to it. At address zero with the SEGFS prefix, the offset itself is stored so that userspace can read it without having to call into the kernel. So the SEGFS null pointer is a valid address, and so are some bytes after it (depending on TCB layout, some of which is specified by the ABI or is part of the de-facto ABI used by GCC). GCC 12 has started warning on __seg_fs null pointer arithmetic. In glibc, we use this construct: *(struct pthread *__seg_fs *) offsetof (struct pthread, header.self) And this is now causing build failures due to -Werror. It's been suggested that I should prove that these warnings are invalid under the N1275 document referenced in the GCC manual. However, I think N1275 is not actually implemented by GCC on x86-64. The address spaces are treated as disjoint by the front end. That is, this int * f (int __seg_fs *q) { return (int *) q; } results in | warning: cast to generic address space pointer from disjoint __seg_fs | address space pointer But I would argue that this is not correct under the N1275 rules because the address spaces are overlapping in practice. I assume the CPU wraps around address arithmetic, which would make them completely equivalent. The optimizers appear to treat the address spaces as overlapping, as expected. In this, int f(int *p, int __seg_fs *q) { *p = 1; *q = 2; return *p; } the read in the return statement is not optimized away. I have trouble deriving from N1275 if pointer casts are supposed to apply offsets or perform some other sort of address translation to produce a pointer to the same object. The GCC implementation does not do this, it preserves the representation. With a shifted address space, the rule for mapping null pointers to null pointers does not make sense and is actually not helpful in the GNU/Linux case (because a copy of the thread pointer is stored at address zero, as mentioned above). I can't shake the impression that N1275 is about something else and not the offsets that the x86-64 architecture can apply to addresses. Thanks, Florian