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

Commitf249f10

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 parentdc565a4 commitf249f10

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
@@ -57,6 +57,8 @@
5757
#include"lib/ilist.h"
5858

5959

60+
#defineSlab_BLOCKHDRSZMAXALIGN(sizeof(SlabBlock))
61+
6062
/*
6163
* SlabContext is a specialized implementation of MemoryContext.
6264
*/
@@ -117,10 +119,10 @@ typedef struct SlabChunk
117119
#defineSlabChunkGetPointer(chk)\
118120
((void *)(((char *)(chk)) + sizeof(SlabChunk)))
119121
#defineSlabBlockGetChunk(slab,block,idx) \
120-
((SlabChunk *) ((char *) (block) +sizeof(SlabBlock)\
122+
((SlabChunk *) ((char *) (block) +Slab_BLOCKHDRSZ\
121123
+ (idx * slab->fullChunkSize)))
122124
#defineSlabBlockStart(block)\
123-
((char *) block +sizeof(SlabBlock))
125+
((char *) block +Slab_BLOCKHDRSZ)
124126
#defineSlabChunkIndex(slab,block,chunk)\
125127
(((char *) chunk - SlabBlockStart(block)) / slab->fullChunkSize)
126128

@@ -185,7 +187,7 @@ static const MemoryContextMethods SlabMethods = {
185187
* chunkSize: allocation chunk size
186188
*
187189
* The chunkSize may not exceed:
188-
*MAXALIGN_DOWN(SIZE_MAX) - MAXALIGN(sizeof(SlabBlock)) -SLAB_CHUNKHDRSZ
190+
*MAXALIGN_DOWN(SIZE_MAX) - MAXALIGN(Slab_BLOCKHDRSZ) -sizeof(SlabChunk)
189191
*/
190192
MemoryContext
191193
SlabContextCreate(MemoryContextparent,
@@ -215,12 +217,12 @@ SlabContextCreate(MemoryContext parent,
215217
fullChunkSize=sizeof(SlabChunk)+MAXALIGN(chunkSize);
216218

217219
/* Make sure the block can store at least one chunk. */
218-
if (blockSize<fullChunkSize+sizeof(SlabBlock))
220+
if (blockSize<fullChunkSize+Slab_BLOCKHDRSZ)
219221
elog(ERROR,"block size %zu for slab is too small for %zu chunks",
220222
blockSize,chunkSize);
221223

222224
/* Compute maximum number of chunks per block */
223-
chunksPerBlock= (blockSize-sizeof(SlabBlock)) /fullChunkSize;
225+
chunksPerBlock= (blockSize-Slab_BLOCKHDRSZ) /fullChunkSize;
224226

225227
/* The freelist starts with 0, ends with chunksPerBlock. */
226228
freelistSize=sizeof(dlist_head)* (chunksPerBlock+1);
@@ -781,7 +783,7 @@ SlabCheck(MemoryContext context)
781783

782784
/* there might be sentinel (thanks to alignment) */
783785
if (slab->chunkSize< (slab->fullChunkSize-sizeof(SlabChunk)))
784-
if (!sentinel_ok(chunk,slab->chunkSize))
786+
if (!sentinel_ok(chunk,sizeof(SlabChunk)+slab->chunkSize))
785787
elog(WARNING,"problem in slab %s: detected write past chunk end in block %p, chunk %p",
786788
name,block,chunk);
787789
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp