Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c4e861b

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 974299a commit c4e861b

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

@@ -169,7 +171,7 @@ static const MemoryContextMethods SlabMethods = {
169171
* chunkSize: allocation chunk size
170172
*
171173
* The chunkSize may not exceed:
172-
* MAXALIGN_DOWN(SIZE_MAX) - MAXALIGN(sizeof(SlabBlock)) - sizeof(SlabChunk)
174+
* MAXALIGN_DOWN(SIZE_MAX) - MAXALIGN(Slab_BLOCKHDRSZ) - sizeof(SlabChunk)
173175
*/
174176
MemoryContext
175177
SlabContextCreate(MemoryContext parent,
@@ -199,12 +201,12 @@ SlabContextCreate(MemoryContext parent,
199201
fullChunkSize = sizeof(SlabChunk) + MAXALIGN(chunkSize);
200202

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

206208
/* Compute maximum number of chunks per block */
207-
chunksPerBlock = (blockSize - sizeof(SlabBlock)) / fullChunkSize;
209+
chunksPerBlock = (blockSize - Slab_BLOCKHDRSZ) / fullChunkSize;
208210

209211
/* The freelist starts with 0, ends with chunksPerBlock. */
210212
freelistSize = sizeof(dlist_head) * (chunksPerBlock + 1);
@@ -772,7 +774,7 @@ SlabCheck(MemoryContext context)
772774

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

0 commit comments

Comments
 (0)