Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6ec8961

Browse files
committed
Fix some possibly latent bugs in slab.c
Primarily, this fixes an incorrect calculation in SlabCheck which waslooking in the wrong byte for the sentinel check. The reason that we'venever noticed this before in the form of a failing sentinel check isbecause the pre-check to this always fails because all current core usersof slab contexts have a chunk size which is already MAXALIGNed, thereforethere's never any space for the sentinel byte. It is possible that anextension needs to use a slab context and if they do with a chunk sizethat's not MAXALIGNed, then they'll likely get errors about overwrittensentinel bytes.Additionally, this patch changes various calculations which are being donebased on the sizeof(SlabBlock). Currently, sizeof(SlabBlock) is amultiple of 8, therefore sizeof(SlabBlock) is the same asMAXALIGN(sizeof(SlabBlock)), however, if we were to ever have to add anyfields to that struct as part of a bug fix, then SlabAlloc could end upreturning a non-MAXALIGNed pointer. To be safe, let's ensure we alwaysMAXALIGN sizeof(SlabBlock) before using it in any calculations.This patch has already been applied to master ind5ee4db.Diagnosed-by: Tomas Vondra, Tom LaneAuthor: Tomas Vondra, David RowleyDiscussion:https://postgr.es/m/CAA4eK1%2B1JyW5TiL%3DyV-3Uq1CrfnTyn0Xrk5uArt31Z%3D8rgPhXQ%40mail.gmail.comBackpatch-through: 10
1 parent8f32ac5 commit6ec8961

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+
#defineSlab_BLOCKHDRSZMAXALIGN(sizeof(SlabBlock))
60+
5961
/*
6062
* SlabContext is a specialized implementation of MemoryContext.
6163
*/
@@ -116,10 +118,10 @@ typedef struct SlabChunk
116118
#defineSlabChunkGetPointer(chk)\
117119
((void *)(((char *)(chk)) + sizeof(SlabChunk)))
118120
#defineSlabBlockGetChunk(slab,block,idx) \
119-
((SlabChunk *) ((char *) (block) +sizeof(SlabBlock)\
121+
((SlabChunk *) ((char *) (block) +Slab_BLOCKHDRSZ\
120122
+ (idx * slab->fullChunkSize)))
121123
#defineSlabBlockStart(block)\
122-
((char *) block +sizeof(SlabBlock))
124+
((char *) block +Slab_BLOCKHDRSZ)
123125
#defineSlabChunkIndex(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(MemoryContextparent,
@@ -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)

[8]ページ先頭

©2009-2025 Movatter.jp