From 1804f12f5915dd51a0893074c581e691ffad341a Mon Sep 17 00:00:00 2001
From: Cheng Shao <terrorjack@type.dance>
Date: Thu, 26 Sep 2024 22:30:28 +0000
Subject: [PATCH] ghci: use plain malloc for mkConInfoTable on non-TNTC
 platforms

This patch avoids using mmap() to allocate executable memory for
mkConInfoTable on platforms without tables-next-to-code, see added
comment for explanation.

(cherry picked from commit 839ac52e94f8ecf878e522dba0575466af248267)
(cherry picked from commit e54e3335a4ea1636a56b96fd60a7f7dfad97de84)
(cherry picked from commit 23cab8a10de6e92f6b4f66c76db8ae6e98d08ac5)
---
 libraries/ghci/GHCi/InfoTable.hsc | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/libraries/ghci/GHCi/InfoTable.hsc b/libraries/ghci/GHCi/InfoTable.hsc
index ce5aee21fbc..7db0fdd77f9 100644
--- a/libraries/ghci/GHCi/InfoTable.hsc
+++ b/libraries/ghci/GHCi/InfoTable.hsc
@@ -315,7 +315,19 @@ newExecConItbl tables_next_to_code obj con_desc = do
             -- with a 32-bit offset relative to the info table, so if we
             -- allocated the string separately it might be out of range.
 
-    ex_ptr <- fillExecBuffer (sz + fromIntegral lcon_desc) $ \wr_ptr ex_ptr -> do
+        -- Just use plain malloc on platforms without TNTC, since we
+        -- don't need to allocate executable memory anyway. This is
+        -- much faster than mmap(), and is crucial for wasm since it
+        -- doesn't support mmap() at all, not to mention executable
+        -- memory.
+        fill_exec_buffer = if tables_next_to_code
+          then fillExecBuffer
+          else \n cont -> do
+            p <- mallocBytes $ fromIntegral n
+            cont p p
+            pure p
+
+    ex_ptr <- fill_exec_buffer (sz + fromIntegral lcon_desc) $ \wr_ptr ex_ptr -> do
         let cinfo = StgConInfoTable { conDesc = ex_ptr `plusPtr` fromIntegral sz
                                     , infoTable = obj }
         pokeConItbl tables_next_to_code wr_ptr ex_ptr cinfo
-- 
GitLab