From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Shelley Vohr <shelley.vohr@gmail.com>
Date: Mon, 27 Nov 2023 17:23:29 +0100
Subject: build: only create cppgc heap on non-32 bit platforms

Node.js crashes during heap marking when using cppgc on 32 bit platforms. Disable for now.

Crash occurs in the following stack:
* MarkingVisitorBase<ConcreteVisitor>::VisitEmbedderTracingSubClassWithEmbedderTracing
* MarkingWorklists::Local::PushExtractedWrapper
* CppMarkingState::MarkAndPush
* CppMarkingState::MarkAndPush
* WrappableInfo::From
* Crash

See https://gist.github.com/codebytere/275ec8923253fd6559b3d36115f7b31b for more.

This should be fixed and re-enabled on all platforms.

diff --git a/BUILD.gn b/BUILD.gn
index 24c122008e0fc009833cf9189ebf43883207c754..1f0263b06969e099a5b3aaf09d36a66c43dd80fc 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -178,6 +178,10 @@ config("node_internal_config") {
   }
   defines += [ "NODE_ARCH=\"$node_arch\"" ]
 
+  if (target_cpu == "x86" || target_cpu == "arm") {
+    defines += [ "NODE_RUNNING_32_BIT" ]
+  }
+
   if (target_os == "win") {
     node_platform = "win32"
   } else if (target_os == "mac") {
diff --git a/src/env.cc b/src/env.cc
index 1d8df40c3446ac9c72c5f0ad24edf0b8a96cf16b..25b81dee18aeeb1bd0452ba0b66085d451723709 100644
--- a/src/env.cc
+++ b/src/env.cc
@@ -542,7 +542,8 @@ IsolateData::IsolateData(Isolate* isolate,
     // for embedder ID, V8 could accidentally enable cppgc on them. So
     // safe guard against this.
     DCHECK_NE(descriptor.wrappable_type_index, BaseObject::kSlot);
-  } else {
+  } else  {
+#if !defined(NODE_RUNNING_32_BIT)
     cpp_heap_ = CppHeap::Create(
         platform,
         CppHeapCreateParams{
@@ -550,6 +551,7 @@ IsolateData::IsolateData(Isolate* isolate,
             WrapperDescriptor(
                 BaseObject::kEmbedderType, BaseObject::kSlot, cppgc_id)});
     isolate->AttachCppHeap(cpp_heap_.get());
+#endif
   }
   // We do not care about overflow since we just want this to be different
   // from the cppgc id.
