Skip to content

Commit 210bece

Browse files
committed
Fix some possibly latent bugs in slab.c
Primarily, this fixes an incorrect calculation in SlabCheck which was looking in the wrong byte for the sentinel check. The reason that we've never noticed this before in the form of a failing sentinel check is because the pre-check to this always fails because all current core users of slab contexts have a chunk size which is already MAXALIGNed, therefore there's never any space for the sentinel byte. It is possible that an extension needs to use a slab context and if they do with a chunk size that's not MAXALIGNed, then they'll likely get errors about overwritten sentinel bytes. Additionally, this patch changes various calculations which are being done based on the sizeof(SlabBlock). Currently, sizeof(SlabBlock) is a multiple of 8, therefore sizeof(SlabBlock) is the same as MAXALIGN(sizeof(SlabBlock)), however, if we were to ever have to add any fields to that struct as part of a bug fix, then SlabAlloc could end up returning a non-MAXALIGNed pointer. To be safe, let's ensure we always MAXALIGN sizeof(SlabBlock) before using it in any calculations. This patch has already been applied to master in d5ee4db. Diagnosed-by: Tomas Vondra, Tom Lane Author: Tomas Vondra, David Rowley Discussion: https://postgr.es/m/CAA4eK1%2B1JyW5TiL%3DyV-3Uq1CrfnTyn0Xrk5uArt31Z%3D8rgPhXQ%40mail.gmail.com Backpatch-through: 10
1 parent 5be9cff commit 210bece

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/backend/utils/mmgr/slab.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
#include "utils/memdebug.h"
5757
#include "utils/memutils.h"
5858

59+
#define Slab_BLOCKHDRSZ MAXALIGN(sizeof(SlabBlock))
60+
5961
/*
6062
* SlabContext is a specialized implementation of MemoryContext.
6163
*/
@@ -116,10 +118,10 @@ typedef struct SlabChunk
116118
#define SlabChunkGetPointer(chk) \
117119
((void *)(((char *)(chk)) + sizeof(SlabChunk)))
118120
#define SlabBlockGetChunk(slab, block, idx) \
119-
((SlabChunk *) ((char *) (block) + sizeof(SlabBlock) \
121+
((SlabChunk *) ((char *) (block) + Slab_BLOCKHDRSZ \
120122
+ (idx * slab->fullChunkSize)))
121123
#define SlabBlockStart(block) \
122-
((char *) block + sizeof(SlabBlock))
124+
((char *) block + Slab_BLOCKHDRSZ)
123125
#define SlabChunkIndex(slab, block, chunk) \
124126
(((char *) chunk - SlabBlockStart(block)) / slab->fullChunkSize)
125127

@@ -168,7 +170,7 @@ static const MemoryContextMethods SlabMethods = {
168170
* chunkSize: allocation chunk size
169171
*
170172
* The chunkSize may not exceed:
171-
* MAXALIGN_DOWN(SIZE_MAX) - MAXALIGN(sizeof(SlabBlock)) - sizeof(SlabChunk)
173+
* MAXALIGN_DOWN(SIZE_MAX) - MAXALIGN(Slab_BLOCKHDRSZ) - sizeof(SlabChunk)
172174
*/
173175
MemoryContext
174176
SlabContextCreate(MemoryContext parent,
@@ -198,12 +200,12 @@ SlabContextCreate(MemoryContext parent,
198200
fullChunkSize = sizeof(SlabChunk) + MAXALIGN(chunkSize);
199201

200202
/* Make sure the block can store at least one chunk. */
201-
if (blockSize < fullChunkSize + sizeof(SlabBlock))
203+
if (blockSize < fullChunkSize + Slab_BLOCKHDRSZ)
202204
elog(ERROR, "block size %zu for slab is too small for %zu chunks",
203205
blockSize, chunkSize);
204206

205207
/* Compute maximum number of chunks per block */
206-
chunksPerBlock = (blockSize - sizeof(SlabBlock)) / fullChunkSize;
208+
chunksPerBlock = (blockSize - Slab_BLOCKHDRSZ) / fullChunkSize;
207209

208210
/* The freelist starts with 0, ends with chunksPerBlock. */
209211
freelistSize = sizeof(dlist_head) * (chunksPerBlock + 1);
@@ -769,7 +771,7 @@ SlabCheck(MemoryContext context)
769771

770772
/* there might be sentinel (thanks to alignment) */
771773
if (slab->chunkSize < (slab->fullChunkSize - sizeof(SlabChunk)))
772-
if (!sentinel_ok(chunk, slab->chunkSize))
774+
if (!sentinel_ok(chunk, sizeof(SlabChunk) + slab->chunkSize))
773775
elog(WARNING, "problem in slab %s: detected write past chunk end in block %p, chunk %p",
774776
name, block, chunk);
775777
}

0 commit comments

Comments
 (0)