0001-Mark-buffers-as-defined-to-Valgrind-consistently.patch

application/octet-stream

Filename: 0001-Mark-buffers-as-defined-to-Valgrind-consistently.patch
Type: application/octet-stream
Part: 0
Message: Re: Using Valgrind to detect faulty buffer accesses (no pin or buffer content lock held)

Patch

Format: format-patch
Series: patch 0001
Subject: Mark buffers as defined to Valgrind consistently.
File+
src/backend/storage/buffer/bufmgr.c 6 4
From 07d03aaf68eaa6e149e9cb522f16e08255615689 Mon Sep 17 00:00:00 2001
From: Peter Geoghegan <pg@bowt.ie>
Date: Sat, 18 Jul 2020 11:19:45 -0700
Subject: [PATCH 1/2] Mark buffers as defined to Valgrind consistently.

Make PinBuffer() mark buffers as defined to Valgrind unconditionally,
including when the buffer header spinlock must be acquired.  This
theoretically creates a risk that we'll mark buffers defined even when
external callers don't end up with a buffer pin.  That seems perfectly
acceptable, though, since in general we make no guarantees about buffers
that are unsafe to access being reliably marked as unsafe.

Oversight in commit 1e0dfd16, which added valgrind buffer access
instrumentation.
---
 src/backend/storage/buffer/bufmgr.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 8ef073b382..83d91b14fb 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1636,11 +1636,13 @@ PinBuffer(BufferDesc *buf, BufferAccessStrategy strategy)
 				result = (buf_state & BM_VALID) != 0;
 
 				/*
-				 * If we successfully acquired our first pin on this buffer
-				 * within this backend, mark buffer contents defined
+				 * Assume that we acquired a buffer pin for the purposes of
+				 * Valgrind buffer client checks (even in !result case) to
+				 * keep things simple.  Buffers that are unsafe to access are
+				 * not generally guaranteed to be marked undefined in any
+				 * case.
 				 */
-				if (result)
-					VALGRIND_MAKE_MEM_DEFINED(BufHdrGetBlock(buf), BLCKSZ);
+				VALGRIND_MAKE_MEM_DEFINED(BufHdrGetBlock(buf), BLCKSZ);
 				break;
 			}
 		}
-- 
2.25.1