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

Commit210bece

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 parent5be9cff commit210bece

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

@@ -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(MemoryContextparent,
@@ -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)

[8]ページ先頭

©2009-2025 Movatter.jp