From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Status: No, score=-4.2 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 18BCC1F8C6 for ; Thu, 29 Jul 2021 12:18:33 +0000 (UTC) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C96D53851C16 for ; Thu, 29 Jul 2021 12:18:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C96D53851C16 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1627561111; bh=LXCAkeQF7EMSOnXjeR1neMr4cDxu0nJ3lOf958wOFXs=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=WQBqwJfvZVjm58ayK4npU7+9WmdAGuzf5iHsV5uTcwhaO5peF/wj1jooAVpRe3nR4 CcOwj8PjiOFNJMKzWmnbruHElbnElaDZuWPodITfAIU5tYqr8JLe7FxYQ7JjfC28qk kCuLp5swPJtRgXHhhn+zJXBwuLIfbea44HJl97uM= 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 75363385480E for ; Thu, 29 Jul 2021 12:16:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 75363385480E 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) To: gcc@gcc.gnu.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-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: , From: Florian Weimer via Libc-alpha Reply-To: Florian Weimer Cc: libc-alpha@sourceware.org Errors-To: libc-alpha-bounces+e=80x24.org@sourceware.org Sender: "Libc-alpha" 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