From 49c2986e5a71f48f2408d507573ac2c39bdd0b68 Mon Sep 17 00:00:00 2001
From: Teo Camarasu <teo.camarasu@tracsis.com>
Date: Wed, 28 Feb 2024 16:26:48 +0000
Subject: [PATCH] rts: avoid checking bdescr of value outside of Haskell heap

In nonmovingTidyWeaks we want to check if the key of a weak pointer
lives in the non-moving heap. We do this by checking the flags of the
block the key lives in. But we need to be careful with values that live
outside the Haskell heap, since they will lack a block descriptor and
looking for one may lead to a segfault. In this case we should just
accept that it isn't on the non-moving heap.

Resolves #24492

(cherry picked from commit 74b24a9b0084459b8aa426a502956bd332b4d0fb)
---
 rts/sm/NonMovingMark.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/rts/sm/NonMovingMark.c b/rts/sm/NonMovingMark.c
index 68939dc6e7e..ba5603d89f9 100644
--- a/rts/sm/NonMovingMark.c
+++ b/rts/sm/NonMovingMark.c
@@ -2008,7 +2008,7 @@ bool nonmovingTidyWeaks (struct MarkQueue_ *queue)
 
         // See Note [Weak pointer processing and the non-moving GC] in
         // MarkWeak.c
-        bool key_in_nonmoving = Bdescr((StgPtr) w->key)->flags & BF_NONMOVING;
+        bool key_in_nonmoving = HEAP_ALLOCED_GC(w->key) && Bdescr((StgPtr) w->key)->flags & BF_NONMOVING;
         if (!key_in_nonmoving || nonmovingIsNowAlive(w->key)) {
             nonmovingMarkLiveWeak(queue, w);
             did_work = true;
-- 
GitLab