Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1c2Jxy-0003Pk-SW for pgsql-hackers@arkaria.postgresql.org; Thu, 03 Nov 2016 15:30:10 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1c2Jxy-00071M-FB for pgsql-hackers@arkaria.postgresql.org; Thu, 03 Nov 2016 15:30:10 +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 1c2JwY-0004S9-Uo for pgsql-hackers@postgresql.org; Thu, 03 Nov 2016 15:28:43 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1c2JwR-0002yD-US for pgsql-hackers@postgresql.org; Thu, 03 Nov 2016 15:28:41 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.nyi.internal (Postfix) with ESMTP id 146D220C37; Thu, 3 Nov 2016 11:28:35 -0400 (EDT) Received: from frontend2 ([10.202.2.161]) by compute2.internal (MEProxy); Thu, 03 Nov 2016 11:28:35 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc:x-sasl-enc; s=smtpout; bh=XpPTTcGZBU+MCu YRjXo2j4bwo6Q=; b=WjUY2Wi02d/cGHGXQnjQvRZpmlnOMk0DL4Y1iX7/syIQ/C rLfpMZm2VhGwrGW/uWpnGF9izHiMZy1JIyA1l1rICQPiZfT/Yk1CzgWCqEY9nUMx qouVfYMnNIHU7fjHfygNkiZFvws1HyCvDpaP5Qcc/Ur2hdQqp0mwySp5111SM= X-ME-Sender: X-Sasl-enc: k0txFR4/jMbW4PhCjte5fg+0rHPu3yEdneb3DJ7Mu/RA 1478186914 Received: from april.local (c-73-13-66-39.hsd1.pa.comcast.net [73.13.66.39]) by mail.messagingengine.com (Postfix) with ESMTPA id A2E34CC0C3; Thu, 3 Nov 2016 11:28:34 -0400 (EDT) Subject: Re: pageinspect: Hash index support To: Tom Lane References: <5d64a536-cc90-ce96-b68f-5b6bd3206476@redhat.com> <80a00b5c-56d3-dffb-8dba-edf5d2167b12@2ndquadrant.com> <16d89e7e-cb1a-cb5d-e944-6f10c324a312@redhat.com> <836487ee-8d4e-cd49-54ac-f4793ddba5ae@redhat.com> <8a751498-6ee9-b943-abf3-b5641851d7de@2ndquadrant.com> <62ee2eba-3a5b-06d3-e224-cfe00b395be7@redhat.com> <66afc31d-e97a-41c3-da4c-bfa02fa2d54e@redhat.com> <02dc6cef-a80c-c976-8ac0-518ff9b22dd8@2n! dquadrant.com> <8921.1478109287@sss.pgh.pa.us> Cc: Jesper Pedersen , Michael Paquier , Ashutosh Sharma , Jeff Janes , Alvaro Herrera , PostgreSQL Hackers From: Peter Eisentraut Organization: 2ndQuadrant Message-ID: <7351c7bb-db87-be4a-0b86-7f7aad0bed41@2ndquadrant.com> Date: Thu, 3 Nov 2016 11:28:33 -0400 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: <8921.1478109287@sss.pgh.pa.us> Content-Type: multipart/mixed; boundary="------------49C01783DFF74C1295E7DFA2" X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org This is a multi-part message in MIME format. --------------49C01783DFF74C1295E7DFA2 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit On 11/2/16 1:54 PM, Tom Lane wrote: > I think the right thing is likely to be to copy the presented bytea > into a palloc'd (and therefore properly aligned) buffer. And not > just in this one function. Does the attached look reasonable? -- Peter Eisentraut http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --------------49C01783DFF74C1295E7DFA2 Content-Type: text/x-patch; name="0001-pageinspect-Fix-unaligned-struct-access-in-GIN-funct.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-pageinspect-Fix-unaligned-struct-access-in-GIN-funct.pa"; filename*1="tch" From 086c603016de5018a8baddc88a8932472e0fcd1e Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Thu, 3 Nov 2016 12:00:00 -0400 Subject: [PATCH] pageinspect: Fix unaligned struct access in GIN functions The raw page data that is passed into the functions will not be aligned at 8-byte boundaries. Casting that to a struct and accessing int64 fields will result in unaligned access. On most platforms, you get away with it, but it will result on a crash on pickier platforms such as ia64 and sparc64. --- contrib/pageinspect/ginfuncs.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/contrib/pageinspect/ginfuncs.c b/contrib/pageinspect/ginfuncs.c index 145e2ce..660d236 100644 --- a/contrib/pageinspect/ginfuncs.c +++ b/contrib/pageinspect/ginfuncs.c @@ -51,7 +51,10 @@ gin_metapage_info(PG_FUNCTION_ARGS) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("input page too small (%d bytes)", raw_page_size))); - page = VARDATA(raw_page); + + /* make a copy so that the page is properly aligned for struct access */ + page = palloc(raw_page_size); + memcpy(page, VARDATA(raw_page), raw_page_size); opaq = (GinPageOpaque) PageGetSpecialPointer(page); if (opaq->flags != GIN_META) @@ -115,7 +118,10 @@ gin_page_opaque_info(PG_FUNCTION_ARGS) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("input page too small (%d bytes)", raw_page_size))); - page = VARDATA(raw_page); + + /* make a copy so that the page is properly aligned for struct access */ + page = palloc(raw_page_size); + memcpy(page, VARDATA(raw_page), raw_page_size); opaq = (GinPageOpaque) PageGetSpecialPointer(page); @@ -195,7 +201,10 @@ gin_leafpage_items(PG_FUNCTION_ARGS) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("input page too small (%d bytes)", raw_page_size))); - page = VARDATA(raw_page); + + /* make a copy so that the page is properly aligned for struct access */ + page = palloc(raw_page_size); + memcpy(page, VARDATA(raw_page), raw_page_size); if (PageGetSpecialSize(page) != MAXALIGN(sizeof(GinPageOpaqueData))) ereport(ERROR, -- 2.10.2 --------------49C01783DFF74C1295E7DFA2 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers --------------49C01783DFF74C1295E7DFA2--