Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ogVT4-0001p0-MU for pgsql-hackers@arkaria.postgresql.org; Thu, 06 Oct 2022 18:19:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ogVT2-0000Gg-Sx for pgsql-hackers@arkaria.postgresql.org; Thu, 06 Oct 2022 18:19:32 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ogVT2-0000Fp-Fc for pgsql-hackers@lists.postgresql.org; Thu, 06 Oct 2022 18:19:32 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ogVSv-0004y3-Or for pgsql-hackers@lists.postgresql.org; Thu, 06 Oct 2022 18:19:31 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 296IJL2h2910982; Thu, 6 Oct 2022 14:19:21 -0400 From: Tom Lane To: David Rowley cc: Andres Freund , Julien Rouhaud , Robert Haas , Yura Sokolov , PostgreSQL Developers Subject: Re: Reducing the chunk header sizes on all memory context types In-reply-to: <2869610.1665077908@sss.pgh.pa.us> References: <3021143.1662432194@sss.pgh.pa.us> <20220907130552.sfjri7jublfxyyi4@jrouhaud> <3666758.1662563307@sss.pgh.pa.us> <20220908164409.ptv66iogw2kvnwj5@awork3.anarazel.de> <4098200.1662660636@sss.pgh.pa.us> <20220908221523.c2nsmn7cvsdqvxvn@awork3.anarazel.de> <4159947.1662677609@sss.pgh.pa.us> <555399.1663636988@sss.pgh.pa.us> <1693074.1664825870@sss.pgh.pa.us> <1913788.1664898906@sss.pgh.pa.us> <2869610.1665077908@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Thu, 06 Oct 2022 13:38:28 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <2910929.1665080324.0@sss.pgh.pa.us> Date: Thu, 06 Oct 2022 14:19:21 -0400 Message-ID: <2910981.1665080361@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2910929.1665080324.1@sss.pgh.pa.us> One more thing: based on what I saw in working with my pending guc.c changes, the assertions in GetMemoryChunkMethodID are largely useless for detecting bogus pointers. I think we should do something more like the attached, which will result in a clean failure if the method ID bits are invalid. I'm a little tempted also to rearrange the MemoryContextMethodID enum so that likely bit patterns like 000 are not valid IDs. While I didn't change it here, I wonder why GetMemoryChunkMethodID is publicly exposed at all. AFAICS it could be static inside mcxt.c. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="more-mcxt-robustness-1.patch"; charset="us-ascii" Content-ID: <2910929.1665080324.2@sss.pgh.pa.us> Content-Description: more-mcxt-robustness-1.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/utils/mmgr/mcxt.c b/src/backend/utils/mmgr/mcxt.c index c80236d285..e82d9b21ba 100644 --- a/src/backend/utils/mmgr/mcxt.c +++ b/src/backend/utils/mmgr/mcxt.c @@ -32,6 +32,11 @@ #include "utils/memutils_internal.h" = = +static void BogusFree(void *pointer); +static void *BogusRealloc(void *pointer, Size size); +static MemoryContext BogusGetChunkContext(void *pointer); +static Size BogusGetChunkSpace(void *pointer); + /************************************************************************= ***** * GLOBAL MEMORY * ************************************************************************= *****/ @@ -74,10 +79,42 @@ static const MemoryContextMethods mcxt_methods[] =3D { [MCTX_SLAB_ID].get_chunk_context =3D SlabGetChunkContext, [MCTX_SLAB_ID].get_chunk_space =3D SlabGetChunkSpace, [MCTX_SLAB_ID].is_empty =3D SlabIsEmpty, - [MCTX_SLAB_ID].stats =3D SlabStats + [MCTX_SLAB_ID].stats =3D SlabStats, #ifdef MEMORY_CONTEXT_CHECKING - ,[MCTX_SLAB_ID].check =3D SlabCheck + [MCTX_SLAB_ID].check =3D SlabCheck, #endif + + /* + * Unused (as yet) IDs should have dummy entries here. This allows us t= o + * fail cleanly if a bogus pointer is passed to pfree or the like. It + * seems sufficient to provide routines for the methods that might get + * invoked from inspection of a chunk (see MCXT_METHOD calls below). + */ + + [MCTX_UNUSED3_ID].free_p =3D BogusFree, + [MCTX_UNUSED3_ID].realloc =3D BogusRealloc, + [MCTX_UNUSED3_ID].get_chunk_context =3D BogusGetChunkContext, + [MCTX_UNUSED3_ID].get_chunk_space =3D BogusGetChunkSpace, + + [MCTX_UNUSED4_ID].free_p =3D BogusFree, + [MCTX_UNUSED4_ID].realloc =3D BogusRealloc, + [MCTX_UNUSED4_ID].get_chunk_context =3D BogusGetChunkContext, + [MCTX_UNUSED4_ID].get_chunk_space =3D BogusGetChunkSpace, + + [MCTX_UNUSED5_ID].free_p =3D BogusFree, + [MCTX_UNUSED5_ID].realloc =3D BogusRealloc, + [MCTX_UNUSED5_ID].get_chunk_context =3D BogusGetChunkContext, + [MCTX_UNUSED5_ID].get_chunk_space =3D BogusGetChunkSpace, + + [MCTX_UNUSED6_ID].free_p =3D BogusFree, + [MCTX_UNUSED6_ID].realloc =3D BogusRealloc, + [MCTX_UNUSED6_ID].get_chunk_context =3D BogusGetChunkContext, + [MCTX_UNUSED6_ID].get_chunk_space =3D BogusGetChunkSpace, + + [MCTX_UNUSED7_ID].free_p =3D BogusFree, + [MCTX_UNUSED7_ID].realloc =3D BogusRealloc, + [MCTX_UNUSED7_ID].get_chunk_context =3D BogusGetChunkContext, + [MCTX_UNUSED7_ID].get_chunk_space =3D BogusGetChunkSpace, }; = /* @@ -125,6 +162,34 @@ static void MemoryContextStatsPrint(MemoryContext con= text, void *passthru, #define MCXT_METHOD(pointer, method) \ mcxt_methods[GetMemoryChunkMethodID(pointer)].method = +/* + * Support routines to trap use of invalid memory context method IDs + * (from calling pfree or the like on a bogus pointer). + */ +static void +BogusFree(void *pointer) +{ + elog(ERROR, "pfree called with invalid pointer %p", pointer); +} + +static void * +BogusRealloc(void *pointer, Size size) +{ + elog(ERROR, "repalloc called with invalid pointer %p", pointer); +} + +static MemoryContext +BogusGetChunkContext(void *pointer) +{ + elog(ERROR, "GetMemoryChunkContext called with invalid pointer %p", poin= ter); +} + +static Size +BogusGetChunkSpace(void *pointer) +{ + elog(ERROR, "GetMemoryChunkSpace called with invalid pointer %p", pointe= r); +} + = /************************************************************************= ***** * EXPORTED ROUTINES * diff --git a/src/include/utils/memutils_internal.h b/src/include/utils/mem= utils_internal.h index 377cee7a84..797409ee94 100644 --- a/src/include/utils/memutils_internal.h +++ b/src/include/utils/memutils_internal.h @@ -77,12 +77,20 @@ extern void SlabCheck(MemoryContext context); * The maximum value for this enum is constrained by * MEMORY_CONTEXT_METHODID_MASK. If an enum value higher than that is * required then MEMORY_CONTEXT_METHODID_BITS will need to be increased. + * For robustness, ensure that MemoryContextMethodID has a value for + * each possible bit-pattern of MEMORY_CONTEXT_METHODID_MASK, and make + * dummy entries for unused IDs in the mcxt_methods[] array. */ typedef enum MemoryContextMethodID { MCTX_ASET_ID, MCTX_GENERATION_ID, MCTX_SLAB_ID, + MCTX_UNUSED3_ID, + MCTX_UNUSED4_ID, + MCTX_UNUSED5_ID, + MCTX_UNUSED6_ID, + MCTX_UNUSED7_ID } MemoryContextMethodID; = /* @@ -110,20 +118,11 @@ extern void MemoryContextCreate(MemoryContext node, * directly precedes 'pointer'. */ static inline MemoryContextMethodID -GetMemoryChunkMethodID(void *pointer) +GetMemoryChunkMethodID(const void *pointer) { uint64 header; = - /* - * Try to detect bogus pointers handed to us, poorly though we can. - * Presumably, a pointer that isn't MAXALIGNED isn't pointing at an - * allocated chunk. - */ - Assert(pointer !=3D NULL); - Assert(pointer =3D=3D (void *) MAXALIGN(pointer)); - - header =3D *((uint64 *) ((char *) pointer - sizeof(uint64))); - + header =3D *((const uint64 *) ((const char *) pointer - sizeof(uint64)))= ; return (MemoryContextMethodID) (header & MEMORY_CONTEXT_METHODID_MASK); } = ------- =_aaaaaaaaaa0--