pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andres Freund <andres@anarazel.de>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Thomas Munro <thomas.munro@enterprisedb.com>
Cc: pgsql-hackers@lists.postgresql.org
Subject: Re: Ensuring hash tuples are properly maxaligned
Date: Tue, 2 Jan 2018 17:09:31 -0800
Message-ID: <20180103010931.6vsirlb5pdapdroq@alap3.anarazel.de> (raw)
In-Reply-To: <14483.1514938129@sss.pgh.pa.us>
References: <14483.1514938129@sss.pgh.pa.us>

Hi,

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?

Generally no objections.

> +	/*
> +	 * It's required that data[] start at a maxaligned offset.  This padding
> +	 * calculation is correct as long as int is not wider than size_t and
> +	 * dsa_pointer is not wider than a regular pointer, but there's a static
> +	 * assertion to check things in nodeHash.c.
> +	 */

But note that dsa_pointer can be wider than a regular pointer on
platforms without atomics support.


> +#define HASHMEMORYCHUNKDATA_RAWSIZE (SIZEOF_SIZE_T * 3 + SIZEOF_VOID_P)
> +
> +	/* ensure proper alignment by adding padding if needed */
> +#if (HASHMEMORYCHUNKDATA_RAWSIZE % MAXIMUM_ALIGNOF) != 0
> +	char		padding[MAXIMUM_ALIGNOF - HASHMEMORYCHUNKDATA_RAWSIZE % MAXIMUM_ALIGNOF];
> +#endif
> +
>  	char		data[FLEXIBLE_ARRAY_MEMBER];	/* buffer allocated at the end */
>  }			HashMemoryChunkData;

Wonder if this specific case wouldn't be easier handled with a union?

Greetings,

Andres Freund




view thread (8+ messages)  latest in thread

Message-ID: <20180103010931.6vsirlb5pdapdroq@alap3.anarazel.de>
Permalink:  ../20180103010931.6vsirlb5pdapdroq@alap3.anarazel.de/
Also on:    postgresql.org/message-id/20180103010931.6vsirlb5pdapdroq@alap3.anarazel.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: andres@anarazel.de, tgl@sss.pgh.pa.us, thomas.munro@enterprisedb.com, pgsql-hackers@lists.postgresql.org
  Subject: Re: Ensuring hash tuples are properly maxaligned
  In-Reply-To: <20180103010931.6vsirlb5pdapdroq@alap3.anarazel.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox