Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9e1b441

Browse files
committed
Allocate static pages in memory strictly MAXALIGNed to avoid overflow
due to adding padding bytes by Postgres data access alignment macros. This was the source of rare but dangerous segfault on 32-bit FreeBSD but no system was safe as static alignment is completely system/compiler free choice. This problem was hidden by the added completely unrelated variable trace_sort way before the relevant part of the code. It just shifted the alignment of all variables with bigger address values to acceptable but haven't solved the problem at large.
1 parent 7930600 commit 9e1b441

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/rumdatapage.c

+24-4
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,14 @@ dataPlaceToPage(RumBtree btree, Page page, OffsetNumber off)
853853
ItemPointerData iptr = {{0, 0}, 0};
854854
RumItem copyItem;
855855
bool copyItemEmpty = true;
856-
char pageCopy[BLCKSZ];
856+
/*
857+
* Must have pageCopy MAXALIGNed to use PG macros to access data in
858+
* it. Should not rely on compiler alignment preferences to avoid
859+
* pageCopy overflow related to PG in-memory page items alignment
860+
* inside rumDataPageLeafRead() or elsewhere.
861+
*/
862+
char pageCopyStorage[BLCKSZ + MAXIMUM_ALIGNOF];
863+
char *pageCopy = (char *) MAXALIGN(pageCopyStorage);
857864
int maxoff = RumPageGetOpaque(page)->maxoff;
858865
int freespace,
859866
insertCount = 0;
@@ -1055,7 +1062,14 @@ dataSplitPageLeaf(RumBtree btree, Buffer lbuf, Buffer rbuf,
10551062
RumItem item;
10561063
int totalCount = 0;
10571064
int maxItemIndex = btree->curitem;
1058-
static char lpageCopy[BLCKSZ];
1065+
/*
1066+
* Must have lpageCopy MAXALIGNed to use PG macros to access data in
1067+
* it. Should not rely on compiler alignment preferences to avoid
1068+
* lpageCopy overflow related to PG in-memory page items alignment
1069+
* inside rumDataPageLeafRead() etc.
1070+
*/
1071+
static char lpageCopyStorage[BLCKSZ + MAXIMUM_ALIGNOF];
1072+
char *lpageCopy = (char *) MAXALIGN(lpageCopyStorage);
10591073

10601074
memset(&item, 0, sizeof(item));
10611075
dataPrepareData(btree, newlPage, off);
@@ -1233,8 +1247,14 @@ dataSplitPageInternal(RumBtree btree, Buffer lbuf, Buffer rbuf,
12331247
OffsetNumber maxoff = RumPageGetOpaque(newlPage)->maxoff;
12341248
Size pageSize = PageGetPageSize(newlPage);
12351249
Size freeSpace;
1236-
1237-
static char vector[2 * BLCKSZ];
1250+
/*
1251+
* Must have vector MAXALIGNed to use PG macros to access data in
1252+
* it. Should not rely on compiler alignment preferences to avoid
1253+
* vector overflow related to PG in-memory page items alignment
1254+
* inside rumDataPageLeafRead() etc.
1255+
*/
1256+
static char vectorStorage[2 * BLCKSZ + MAXIMUM_ALIGNOF];
1257+
char *vector = (char *) MAXALIGN(vectorStorage);
12381258

12391259
RumInitPage(rPage, RumPageGetOpaque(newlPage)->flags, pageSize);
12401260
freeSpace = RumDataPageGetFreeSpace(rPage);

src/rumentrypage.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,14 @@ entrySplitPage(RumBtree btree, Buffer lbuf, Buffer rbuf,
428428
Page page;
429429
Page newlPage = PageGetTempPageCopy(lPage);
430430
Size pageSize = PageGetPageSize(newlPage);
431-
432-
static char tupstore[2 * BLCKSZ];
431+
/*
432+
* Must have tupstore MAXALIGNed to use PG macros to access data in
433+
* it. Should not rely on compiler alignment preferences to avoid
434+
* tupstore overflow related to PG in-memory page items alignment
435+
* inside rumDataPageLeafRead() or elsewhere.
436+
*/
437+
static char tupstoreStorage[2 * BLCKSZ + MAXIMUM_ALIGNOF];
438+
char *tupstore = (char *) MAXALIGN(tupstoreStorage);
433439

434440
entryPreparePage(btree, newlPage, off);
435441

0 commit comments

Comments
 (0)