Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eWXjH-0000cv-Nv for pgsql-hackers@arkaria.postgresql.org; Wed, 03 Jan 2018 01:20:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eWXjH-0004gs-1D for pgsql-hackers@arkaria.postgresql.org; Wed, 03 Jan 2018 01:20:27 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eWXjG-0004gj-Nu for pgsql-hackers@lists.postgresql.org; Wed, 03 Jan 2018 01:20:26 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eWXjD-0004QQ-Q5 for pgsql-hackers@lists.postgresql.org; Wed, 03 Jan 2018 01:20:25 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w031KJPL017455; Tue, 2 Jan 2018 20:20:19 -0500 From: Tom Lane To: Andres Freund cc: Thomas Munro , pgsql-hackers@lists.postgresql.org Subject: Re: Ensuring hash tuples are properly maxaligned In-reply-to: <20180103010931.6vsirlb5pdapdroq@alap3.anarazel.de> References: <14483.1514938129@sss.pgh.pa.us> <20180103010931.6vsirlb5pdapdroq@alap3.anarazel.de> Comments: In-reply-to Andres Freund message dated "Tue, 02 Jan 2018 17:09:31 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <17453.1514942419.1@sss.pgh.pa.us> Date: Tue, 02 Jan 2018 20:20:19 -0500 Message-ID: <17454.1514942419@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Andres Freund writes: > On 2018-01-02 19:08:49 -0500, Tom Lane wrote: >> Now, the existing definition of the struct seems safe on all >> architectures we support, but it would not take much to break it. >> I think we ought to do what we did recently in the memory-context >> code: insert an explicit padding calculation and a static assertion >> that it's right. Hence, the attached proposed patch (in which >> I also failed to resist the temptation to make the two code paths >> in dense_alloc() look more alike). Any objections? > But note that dsa_pointer can be wider than a regular pointer on > platforms without atomics support. Hm. I did not get that impression from the comments in dsa.h, but if it's true then this approach won't work --- and indeed the hash code would be actively broken in such a case, so it's a problem we must fix. The other idea I was considering was to get rid of the data[] member entirely, define HASH_CHUNK_HEADER_SIZE as MAXALIGN(sizeof(HashMemoryChunkData)), and replace the references to data[] with pointer arithmetic along the lines of ((char *) chunk) + HASH_CHUNK_HEADER_SIZE This would be a bit more invasive, but probably not too ugly if we encapsulated that pointer arithmetic in a macro. And it'd certainly be a bunch more robust against people throwing random fields into the struct. regards, tom lane