Fix mislabeling of PROC_QUEUE->links as PGPROC, fixing UBSan on 32bit
authorAndres Freund
Thu, 17 Nov 2022 04:00:59 +0000 (20:00 -0800)
committerAndres Freund
Sat, 19 Nov 2022 20:33:14 +0000 (12:33 -0800)
commitfc4154286e0e47d748d19183fd05be794a019be8
tree639fb84d3255c9d27a53e60fcb8be51c9d95f57c
parentbb3f42aae382106b91d60b861333a08ff6007e9c
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 that
largely works, it's certainly not correct and unnecessary - we can just use
SHM_QUEUE* to point to the insertion point.

Noticed when testing a 32bit of postgres with undefined behavior
sanitizer. UBSan noticed that sometimes the supposed PGPROC wasn't
sufficiently aligned (required since 46d6e5f5679, ensured indirectly, via
ShmemAllocRaw() guaranteeing cacheline alignment).

For now fix this by using a SHM_QUEUE* for the insertion point. Subsequently
we should replace all the use of PROC_QUEUE and SHM_QUEUE with ilist.h, but
that's a larger change that we don't want to backpatch.

Backpatch to all supported versions - it's useful to be able to run postgres
under UBSan.

Reviewed-by: Tom Lane
Discussion: https://postgr.es/m/20221117014230[email protected]
Backpatch: 11-
src/backend/storage/lmgr/proc.c