From 23d06bd6e6312c1676cbed5561a9933848f13a87 Mon Sep 17 00:00:00 2001
From: Cheng Shao <terrorjack@type.dance>
Date: Wed, 28 Feb 2024 19:28:46 +0000
Subject: [PATCH] rts: assert pointer is indeed heap allocated in Bdescr()

This commit adds an assertion to Bdescr() to assert the pointer is
indeed heap allocated. This is useful to rule out RTS bugs that
attempt to access non-existent block descriptor of a static closure, #24492
being one such example.

(cherry picked from commit d19441d7981778ab9463557d812d9d7cf586f9e7)
---
 rts/include/rts/storage/Block.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/rts/include/rts/storage/Block.h b/rts/include/rts/storage/Block.h
index a3b75cf7cd2..2054ca0bc9a 100644
--- a/rts/include/rts/storage/Block.h
+++ b/rts/include/rts/storage/Block.h
@@ -10,6 +10,10 @@
 
 #include "ghcconfig.h"
 
+#if !defined(CMINUSMINUS)
+#include "rts/storage/HeapAlloc.h"
+#endif
+
 /* The actual block and megablock-size constants are defined in
  * rts/include/Constants.h, all constants here are derived from these.
  */
@@ -190,6 +194,7 @@ typedef struct bdescr_ {
 EXTERN_INLINE bdescr *Bdescr(StgPtr p);
 EXTERN_INLINE bdescr *Bdescr(StgPtr p)
 {
+  ASSERT(HEAP_ALLOCED_GC(p));
   return (bdescr *)
     ((((W_)p &  MBLOCK_MASK & ~BLOCK_MASK) >> (BLOCK_SHIFT-BDESCR_SHIFT))
      | ((W_)p & ~MBLOCK_MASK)
-- 
GitLab