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

Commitc13667b

Browse files
committed
Fix mislabeling of PROC_QUEUE->links as PGPROC, fixing UBSan on 32bit
ProcSleep() used a PGPROC* variable to point to PROC_QUEUE->links.next,because that does "the right thing" with SHMQueueInsertBefore(). While thatlargely works, it's certainly not correct and unnecessary - we can just useSHM_QUEUE* to point to the insertion point.Noticed when testing a 32bit of postgres with undefined behaviorsanitizer. UBSan noticed that sometimes the supposed PGPROC wasn'tsufficiently aligned (required since46d6e5f, ensured indirectly, viaShmemAllocRaw() guaranteeing cacheline alignment).For now fix this by using a SHM_QUEUE* for the insertion point. Subsequentlywe should replace all the use of PROC_QUEUE and SHM_QUEUE with ilist.h, butthat's a larger change that we don't want to backpatch.Backpatch to all supported versions - it's useful to be able to run postgresunder UBSan.Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>Discussion:https://postgr.es/m/20221117014230.op5kmgypdv2dtqsf@awork3.anarazel.deBackpatch: 11-
1 parentd3d3888 commitc13667b

File tree

1 file changed

+14
-10
lines changed
  • src/backend/storage/lmgr

1 file changed

+14
-10
lines changed

‎src/backend/storage/lmgr/proc.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,11 +1067,11 @@ ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable)
10671067
uint32hashcode=locallock->hashcode;
10681068
LWLock*partitionLock=LockHashPartitionLock(hashcode);
10691069
PROC_QUEUE*waitQueue=&(lock->waitProcs);
1070+
SHM_QUEUE*waitQueuePos;
10701071
LOCKMASKmyHeldLocks=MyProc->heldLocks;
10711072
boolearly_deadlock= false;
10721073
boolallow_autovacuum_cancel= true;
10731074
intmyWaitStatus;
1074-
PGPROC*proc;
10751075
PGPROC*leader=MyProc->lockGroupLeader;
10761076
inti;
10771077

@@ -1119,21 +1119,24 @@ ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable)
11191119
* we are only considering the part of the wait queue before my insertion
11201120
* point.
11211121
*/
1122-
if (myHeldLocks!=0)
1122+
if (myHeldLocks!=0&&waitQueue->size>0)
11231123
{
11241124
LOCKMASKaheadRequests=0;
1125+
SHM_QUEUE*proc_node;
11251126

1126-
proc= (PGPROC*)waitQueue->links.next;
1127+
proc_node=waitQueue->links.next;
11271128
for (i=0;i<waitQueue->size;i++)
11281129
{
1130+
PGPROC*proc= (PGPROC*)proc_node;
1131+
11291132
/*
11301133
* If we're part of the same locking group as this waiter, its
11311134
* locks neither conflict with ours nor contribute to
11321135
* aheadRequests.
11331136
*/
11341137
if (leader!=NULL&&leader==proc->lockGroupLeader)
11351138
{
1136-
proc= (PGPROC*)proc->links.next;
1139+
proc_node=proc->links.next;
11371140
continue;
11381141
}
11391142
/* Must he wait for me? */
@@ -1168,24 +1171,25 @@ ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable)
11681171
}
11691172
/* Nope, so advance to next waiter */
11701173
aheadRequests |=LOCKBIT_ON(proc->waitLockMode);
1171-
proc= (PGPROC*)proc->links.next;
1174+
proc_node=proc->links.next;
11721175
}
11731176

11741177
/*
1175-
* If wefall out of loop normally, proc points towaitQueue head, so
1176-
* we will insert at tail of queue as desired.
1178+
* If weiterated through the whole queue, cur points tothe waitQueue
1179+
*head, sowe will insert at tail of queue as desired.
11771180
*/
1181+
waitQueuePos=proc_node;
11781182
}
11791183
else
11801184
{
11811185
/* I hold no locks, so I can't push in front of anyone. */
1182-
proc=(PGPROC*)&(waitQueue->links);
1186+
waitQueuePos=&waitQueue->links;
11831187
}
11841188

11851189
/*
1186-
* Insert self into queue,ahead ofthegiven proc (or at tail of queue).
1190+
* Insert self into queue,attheposition determined above.
11871191
*/
1188-
SHMQueueInsertBefore(&(proc->links),&(MyProc->links));
1192+
SHMQueueInsertBefore(waitQueuePos,&MyProc->links);
11891193
waitQueue->size++;
11901194

11911195
lock->waitMask |=LOCKBIT_ON(lockmode);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp