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 1ogaP4-0000Vl-Bi for pgsql-hackers@arkaria.postgresql.org; Thu, 06 Oct 2022 23:35:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ogaP2-0005tJ-4V for pgsql-hackers@arkaria.postgresql.org; Thu, 06 Oct 2022 23:35:44 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ogaP1-0005il-Pt for pgsql-hackers@lists.postgresql.org; Thu, 06 Oct 2022 23:35:43 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ogaOp-0007PW-LS for pgsql-hackers@lists.postgresql.org; Thu, 06 Oct 2022 23:35:38 +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 296NZRJJ3023167; Thu, 6 Oct 2022 19:35:27 -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: <2983672.1665097833@sss.pgh.pa.us> References: <555399.1663636988@sss.pgh.pa.us> <1693074.1664825870@sss.pgh.pa.us> <1913788.1664898906@sss.pgh.pa.us> <2869610.1665077908@sss.pgh.pa.us> <2910981.1665080361@sss.pgh.pa.us> <20221006190058.rgk4gxa34d6rg77o@awork3.anarazel.de> <2921309.1665083444@sss.pgh.pa.us> <20221006191454.yviucagfzayak3x6@awork3.anarazel.de> <2922295.1665084196@sss.pgh.pa.us> <2963487.1665086713@sss.pgh.pa.us> <2975022.1665093421@sss.pgh.pa.us> <2983672.1665097833@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Thu, 06 Oct 2022 19:10:33 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <3023142.1665099307.0@sss.pgh.pa.us> Date: Thu, 06 Oct 2022 19:35:27 -0400 Message-ID: <3023166.1665099327@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: <3023142.1665099307.1@sss.pgh.pa.us> I wrote: > So I'm still inclined to leave 001 and 010 both unused, but the > reason why is different than I thought before. Which leaves me with the attached proposed wording. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="more-mcxt-robustness-3.patch"; charset="us-ascii" Content-ID: <3023142.1665099307.2@sss.pgh.pa.us> Content-Description: more-mcxt-robustness-3.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/utils/mmgr/mcxt.c b/src/backend/utils/mmgr/mcxt.c index c80236d285..b1a3c74830 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_UNUSED1_ID].free_p =3D BogusFree, + [MCTX_UNUSED1_ID].realloc =3D BogusRealloc, + [MCTX_UNUSED1_ID].get_chunk_context =3D BogusGetChunkContext, + [MCTX_UNUSED1_ID].get_chunk_space =3D BogusGetChunkSpace, + + [MCTX_UNUSED2_ID].free_p =3D BogusFree, + [MCTX_UNUSED2_ID].realloc =3D BogusRealloc, + [MCTX_UNUSED2_ID].get_chunk_context =3D BogusGetChunkContext, + [MCTX_UNUSED2_ID].get_chunk_space =3D BogusGetChunkSpace, + + [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, }; = /* @@ -125,6 +162,77 @@ static void MemoryContextStatsPrint(MemoryContext con= text, void *passthru, #define MCXT_METHOD(pointer, method) \ mcxt_methods[GetMemoryChunkMethodID(pointer)].method = +/* + * GetMemoryChunkMethodID + * Return the MemoryContextMethodID from the uint64 chunk header which + * directly precedes 'pointer'. + */ +static inline MemoryContextMethodID +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=3D (const void *) MAXALIGN(pointer)); + + header =3D *((const uint64 *) ((const char *) pointer - sizeof(uint64)))= ; + + return (MemoryContextMethodID) (header & MEMORY_CONTEXT_METHODID_MASK); +} + +/* + * GetMemoryChunkHeader + * Return the uint64 chunk header which directly precedes 'pointer'. + * + * This is only used after GetMemoryChunkMethodID, so no need for error c= hecks. + */ +static inline uint64 +GetMemoryChunkHeader(const void *pointer) +{ + return *((const uint64 *) ((const char *) pointer - sizeof(uint64))); +} + +/* + * Support routines to trap use of invalid memory context method IDs + * (from calling pfree or the like on a bogus pointer). As a possible + * aid in debugging, we report the header word along with the pointer + * address (if we got here, there must be an accessible header word). + */ +static void +BogusFree(void *pointer) +{ + elog(ERROR, "pfree called with invalid pointer %p (header 0x%016llx)", + pointer, (long long) GetMemoryChunkHeader(pointer)); +} + +static void * +BogusRealloc(void *pointer, Size size) +{ + elog(ERROR, "repalloc called with invalid pointer %p (header 0x%016llx)"= , + pointer, (long long) GetMemoryChunkHeader(pointer)); + return NULL; /* keep compiler quiet */ +} + +static MemoryContext +BogusGetChunkContext(void *pointer) +{ + elog(ERROR, "GetMemoryChunkContext called with invalid pointer %p (heade= r 0x%016llx)", + pointer, (long long) GetMemoryChunkHeader(pointer)); + return NULL; /* keep compiler quiet */ +} + +static Size +BogusGetChunkSpace(void *pointer) +{ + elog(ERROR, "GetMemoryChunkSpace called with invalid pointer %p (header = 0x%016llx)", + pointer, (long long) GetMemoryChunkHeader(pointer)); + return 0; /* keep compiler quiet */ +} + = /************************************************************************= ***** * EXPORTED ROUTINES * diff --git a/src/include/utils/memutils_internal.h b/src/include/utils/mem= utils_internal.h index 377cee7a84..bc2cbdd506 100644 --- a/src/include/utils/memutils_internal.h +++ b/src/include/utils/memutils_internal.h @@ -74,15 +74,26 @@ extern void SlabCheck(MemoryContext context); * MemoryContextMethodID * A unique identifier for each MemoryContext implementation which * indicates the index into the mcxt_methods[] array. See mcxt.c. - * 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 robust error detection, ensure that MemoryContextMethodID has a va= lue + * for each possible bit-pattern of MEMORY_CONTEXT_METHODID_MASK, and mak= e + * dummy entries for unused IDs in the mcxt_methods[] array. We also try + * to avoid using bit-patterns as valid IDs if they are likely to occur i= n + * garbage data, or if they could falsely match on chunks that are really= from + * malloc not palloc. (We can't tell that for most malloc implementation= s, + * but it happens that glibc stores flag bits in the same place where we = put + * the MemoryContextMethodID, so the possible values are predictable for = it.) */ typedef enum MemoryContextMethodID { + MCTX_UNUSED1_ID, /* 000 occurs in never-used memory */ + MCTX_UNUSED2_ID, /* glibc malloc'd chunks usually match 001 */ + MCTX_UNUSED3_ID, /* glibc malloc'd chunks > 128kB match 010 */ MCTX_ASET_ID, MCTX_GENERATION_ID, MCTX_SLAB_ID, + MCTX_UNUSED4_ID, /* available */ + MCTX_UNUSED5_ID /* 111 occurs in wipe_mem'd memory */ } MemoryContextMethodID; = /* @@ -104,27 +115,4 @@ extern void MemoryContextCreate(MemoryContext node, MemoryContext parent, const char *name); = -/* - * GetMemoryChunkMethodID - * Return the MemoryContextMethodID from the uint64 chunk header which - * directly precedes 'pointer'. - */ -static inline MemoryContextMethodID -GetMemoryChunkMethodID(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))); - - return (MemoryContextMethodID) (header & MEMORY_CONTEXT_METHODID_MASK); -} - #endif /* MEMUTILS_INTERNAL_H */ ------- =_aaaaaaaaaa0--