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

Commitfc41542

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 parentbb3f42a commitfc41542

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
@@ -1074,13 +1074,13 @@ ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable)
10741074
uint32hashcode=locallock->hashcode;
10751075
LWLock*partitionLock=LockHashPartitionLock(hashcode);
10761076
PROC_QUEUE*waitQueue=&(lock->waitProcs);
1077+
SHM_QUEUE*waitQueuePos;
10771078
LOCKMASKmyHeldLocks=MyProc->heldLocks;
10781079
TimestampTzstandbyWaitStart=0;
10791080
boolearly_deadlock= false;
10801081
boolallow_autovacuum_cancel= true;
10811082
boollogged_recovery_conflict= false;
10821083
ProcWaitStatusmyWaitStatus;
1083-
PGPROC*proc;
10841084
PGPROC*leader=MyProc->lockGroupLeader;
10851085
inti;
10861086

@@ -1128,21 +1128,24 @@ ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable)
11281128
* we are only considering the part of the wait queue before my insertion
11291129
* point.
11301130
*/
1131-
if (myHeldLocks!=0)
1131+
if (myHeldLocks!=0&&waitQueue->size>0)
11321132
{
11331133
LOCKMASKaheadRequests=0;
1134+
SHM_QUEUE*proc_node;
11341135

1135-
proc= (PGPROC*)waitQueue->links.next;
1136+
proc_node=waitQueue->links.next;
11361137
for (i=0;i<waitQueue->size;i++)
11371138
{
1139+
PGPROC*proc= (PGPROC*)proc_node;
1140+
11381141
/*
11391142
* If we're part of the same locking group as this waiter, its
11401143
* locks neither conflict with ours nor contribute to
11411144
* aheadRequests.
11421145
*/
11431146
if (leader!=NULL&&leader==proc->lockGroupLeader)
11441147
{
1145-
proc= (PGPROC*)proc->links.next;
1148+
proc_node=proc->links.next;
11461149
continue;
11471150
}
11481151
/* Must he wait for me? */
@@ -1177,24 +1180,25 @@ ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable)
11771180
}
11781181
/* Nope, so advance to next waiter */
11791182
aheadRequests |=LOCKBIT_ON(proc->waitLockMode);
1180-
proc= (PGPROC*)proc->links.next;
1183+
proc_node=proc->links.next;
11811184
}
11821185

11831186
/*
1184-
* If wefall out of loop normally, proc points towaitQueue head, so
1185-
* we will insert at tail of queue as desired.
1187+
* If weiterated through the whole queue, cur points tothe waitQueue
1188+
*head, sowe will insert at tail of queue as desired.
11861189
*/
1190+
waitQueuePos=proc_node;
11871191
}
11881192
else
11891193
{
11901194
/* I hold no locks, so I can't push in front of anyone. */
1191-
proc=(PGPROC*)&(waitQueue->links);
1195+
waitQueuePos=&waitQueue->links;
11921196
}
11931197

11941198
/*
1195-
* Insert self into queue,ahead ofthegiven proc (or at tail of queue).
1199+
* Insert self into queue,attheposition determined above.
11961200
*/
1197-
SHMQueueInsertBefore(&(proc->links),&(MyProc->links));
1201+
SHMQueueInsertBefore(waitQueuePos,&MyProc->links);
11981202
waitQueue->size++;
11991203

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp