add_clobber_freed_mem_to_dsm_v2.patch

application/octet-stream

Filename: add_clobber_freed_mem_to_dsm_v2.patch
Type: application/octet-stream
Part: 0
Message: Re: BUG #17619: AllocSizeIsValid violation in parallel hash join

Patch

Format: unified
Series: patch v2
File+
src/backend/storage/ipc/dsm.c 10 0
src/backend/storage/ipc/shm_toc.c 9 1
diff --git a/src/backend/storage/ipc/dsm.c b/src/backend/storage/ipc/dsm.c
index a360325f86..15bad41d31 100644
--- a/src/backend/storage/ipc/dsm.c
+++ b/src/backend/storage/ipc/dsm.c
@@ -43,6 +43,7 @@
 #include "storage/pg_shmem.h"
 #include "utils/freepage.h"
 #include "utils/guc.h"
+#include "utils/memdebug.h"
 #include "utils/memutils.h"
 #include "utils/resowner_private.h"
 
@@ -614,6 +615,11 @@ dsm_create(Size size, int flags)
 	dsm_control->item[nitems].pinned = false;
 	seg->control_slot = nitems;
 	dsm_control->nitems++;
+
+#ifdef CLOBBER_FREED_MEMORY
+	wipe_mem(seg->mapped_address, seg->mapped_size);
+#endif
+
 	LWLockRelease(DynamicSharedMemoryControlLock);
 
 	return seg;
@@ -839,6 +845,10 @@ dsm_detach(dsm_segment *seg)
 			/* A pinned segment should never reach 1. */
 			Assert(!dsm_control->item[control_slot].pinned);
 
+#ifdef CLOBBER_FREED_MEMORY
+			wipe_mem(seg->mapped_address, seg->mapped_size);
+#endif
+
 			/*
 			 * If we fail to destroy the segment here, or are killed before we
 			 * finish doing so, the reference count will remain at 1, which
diff --git a/src/backend/storage/ipc/shm_toc.c b/src/backend/storage/ipc/shm_toc.c
index 0c9ef64428..ca6d60e1dd 100644
--- a/src/backend/storage/ipc/shm_toc.c
+++ b/src/backend/storage/ipc/shm_toc.c
@@ -16,6 +16,7 @@
 #include "port/atomics.h"
 #include "storage/shm_toc.h"
 #include "storage/spin.h"
+#include "utils/memdebug.h"
 
 typedef struct shm_toc_entry
 {
@@ -92,6 +93,7 @@ shm_toc_allocate(shm_toc *toc, Size nbytes)
 	Size		allocated_bytes;
 	Size		nentry;
 	Size		toc_bytes;
+	void		*ret;
 
 	/*
 	 * Make sure request is well-aligned.  XXX: MAXALIGN is not enough,
@@ -121,7 +123,13 @@ shm_toc_allocate(shm_toc *toc, Size nbytes)
 
 	SpinLockRelease(&toc->toc_mutex);
 
-	return ((char *) toc) + (total_bytes - allocated_bytes - nbytes);
+	ret = ((char *) toc) + (total_bytes - allocated_bytes - nbytes);
+
+#ifdef CLOBBER_FREED_MEMORY
+	wipe_mem(ret, nbytes);
+#endif
+
+	return ret;
 }
 
 /*