From: Tom Lane <tgl@sss.pgh.pa.us>
To: David Rowley <dgrowleyml@gmail.com>
Cc: Andres Freund <andres@anarazel.de>
Cc: Julien Rouhaud <rjuju123@gmail.com>
Cc: Robert Haas <robertmhaas@gmail.com>
Cc: Yura Sokolov <y.sokolov@postgrespro.ru>
Cc: PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>
Subject: Re: Reducing the chunk header sizes on all memory context types
Date: Thu, 06 Oct 2022 14:19:21 -0400
Message-ID: <2910981.1665080361@sss.pgh.pa.us> (raw)
In-Reply-To: <2869610.1665077908@sss.pgh.pa.us>
References: <CAApHDvr7=b9wCFNOmL6-jkFZM1WzOAo6R+B=BMOPA75xAOgPEw@mail.gmail.com>
<3021143.1662432194@sss.pgh.pa.us>
<CAApHDvq9yis1F7p5Vu-T4MUpAaNiiA7gDdDaJj5VqBKMexysyA@mail.gmail.com>
<CAApHDvo-pS0MnyKGMua_TeFCy8QZzSzU0po_8pa7SMxqDhy6Ww@mail.gmail.com>
<20220907130552.sfjri7jublfxyyi4@jrouhaud>
<CAApHDvpwMuezpKGOxhAmeVSGQ+WUf2w_bRvUVHeFU8zoQCaoqQ@mail.gmail.com>
<CAApHDvoeqxV3HKWADuQdkHCc-WRRjOeiRMoXehaXSHStz3-WiA@mail.gmail.com>
<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>
<CAApHDvprSZjZgmpaz55WZU-KrvqigncmPvEsqqt7UO=t7+N_OQ@mail.gmail.com>
<CAApHDvrsqN_qM7JaiKgR3Sf5YmnYvgf6kcFUp=w1Ot06nZAfUw@mail.gmail.com>
<CAApHDvrNLEkNLhtsp6uZ9+Hdc7LkF_LC3eT32+nedu0teCWccQ@mail.gmail.com>
<555399.1663636988@sss.pgh.pa.us>
<CAApHDvoBxz7RiJiyGzi-wN4c-vLg9TGQ8yTmFW1j6eUJPsP4DA@mail.gmail.com>
<CAApHDvqE9TRO43CyBMaCmPwt4N=6Ei7! ! f=zo6sk0rFZaYXOk_8Q@mail.gmail.com>
<1693074.1664825870@sss.pgh.pa.us>
<1913788.1664898906@sss.pgh.pa.us>
<CAApHDvoOWpxq0UucD0W11=99GopDwozTr=+bt2AmZFgHi87gmg@mail.gmail.com>
<2869610.1665077908@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
Attachments:more-mcxt-robustness-1.patch (text/x-diff, 4.9K) downloaddiff --git a/src/backend/utils/mmgr/mcxt.c b/src/backend/utils/mmgr/mcxt.cindex 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[] = {
[MCTX_SLAB_ID].get_chunk_context = SlabGetChunkContext,
[MCTX_SLAB_ID].get_chunk_space = SlabGetChunkSpace,
[MCTX_SLAB_ID].is_empty = SlabIsEmpty,
- [MCTX_SLAB_ID].stats = SlabStats+ [MCTX_SLAB_ID].stats = SlabStats,
#ifdef MEMORY_CONTEXT_CHECKING
- ,[MCTX_SLAB_ID].check = SlabCheck+ [MCTX_SLAB_ID].check = SlabCheck,
#endif
++ /*+ * Unused (as yet) IDs should have dummy entries here. This allows us to+ * 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 = BogusFree,+ [MCTX_UNUSED3_ID].realloc = BogusRealloc,+ [MCTX_UNUSED3_ID].get_chunk_context = BogusGetChunkContext,+ [MCTX_UNUSED3_ID].get_chunk_space = BogusGetChunkSpace,++ [MCTX_UNUSED4_ID].free_p = BogusFree,+ [MCTX_UNUSED4_ID].realloc = BogusRealloc,+ [MCTX_UNUSED4_ID].get_chunk_context = BogusGetChunkContext,+ [MCTX_UNUSED4_ID].get_chunk_space = BogusGetChunkSpace,++ [MCTX_UNUSED5_ID].free_p = BogusFree,+ [MCTX_UNUSED5_ID].realloc = BogusRealloc,+ [MCTX_UNUSED5_ID].get_chunk_context = BogusGetChunkContext,+ [MCTX_UNUSED5_ID].get_chunk_space = BogusGetChunkSpace,++ [MCTX_UNUSED6_ID].free_p = BogusFree,+ [MCTX_UNUSED6_ID].realloc = BogusRealloc,+ [MCTX_UNUSED6_ID].get_chunk_context = BogusGetChunkContext,+ [MCTX_UNUSED6_ID].get_chunk_space = BogusGetChunkSpace,++ [MCTX_UNUSED7_ID].free_p = BogusFree,+ [MCTX_UNUSED7_ID].realloc = BogusRealloc,+ [MCTX_UNUSED7_ID].get_chunk_context = BogusGetChunkContext,+ [MCTX_UNUSED7_ID].get_chunk_space = BogusGetChunkSpace,
};
/*
@@ -125,6 +162,34 @@ static void MemoryContextStatsPrint(MemoryContext context, 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", pointer);+}++static Size+BogusGetChunkSpace(void *pointer)+{+ elog(ERROR, "GetMemoryChunkSpace called with invalid pointer %p", pointer);+}+
/*****************************************************************************
* EXPORTED ROUTINES *
diff --git a/src/include/utils/memutils_internal.h b/src/include/utils/memutils_internal.hindex 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 != NULL);- Assert(pointer == (void *) MAXALIGN(pointer));-- header = *((uint64 *) ((char *) pointer - sizeof(uint64)));-+ header = *((const uint64 *) ((const char *) pointer - sizeof(uint64)));
return (MemoryContextMethodID) (header & MEMORY_CONTEXT_METHODID_MASK);
}
=
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: tgl@sss.pgh.pa.us, dgrowleyml@gmail.com, andres@anarazel.de, rjuju123@gmail.com, robertmhaas@gmail.com, y.sokolov@postgrespro.ru
Subject: Re: Reducing the chunk header sizes on all memory context types
In-Reply-To: <2910981.1665080361@sss.pgh.pa.us>
References: <CAApHDvr7=b9wCFNOmL6-jkFZM1WzOAo6R+B=BMOPA75xAOgPEw@mail.gmail.com> <20220907130552.sfjri7jublfxyyi4@jrouhaud> <CAApHDvpwMuezpKGOxhAmeVSGQ+WUf2w_bRvUVHeFU8zoQCaoqQ@mail.gmail.com> <CAApHDvoeqxV3HKWADuQdkHCc-WRRjOeiRMoXehaXSHStz3-WiA@mail.gmail.com> <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> <CAApHDvprSZjZgmpaz55WZU-KrvqigncmPvEsqqt7UO=t7+N_OQ@mail.gmail.com> <CAApHDvrsqN_qM7JaiKgR3Sf5YmnYvgf6kcFUp=w1Ot06nZAfUw@mail.gmail.com> <CAApHDvrNLEkNLhtsp6uZ9+Hdc7LkF_LC3eT32+nedu0teCWccQ@mail.gmail.com> <555399.1663636988@sss.pgh.pa.us> <CAApHDvoBxz7RiJiyGzi-wN4c-vLg9TGQ8yTmFW1j6eUJPsP4DA@mail.gmail.com> <1693074.1664825870@sss.pgh.pa.us> <1913788.1664898906@sss.pgh.pa.us> <CAApHDvoOWpxq0UucD0W11=99GopDwozTr=+bt2AmZFgHi87gmg@mail.gmail.com> <2869610.1665077908@sss.pgh.pa.us> <2910981.1665080361@sss.pgh.pa.us>
* 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