From 1eee2e28ff51db0b8c2393d68f716c6c7941e779 Mon Sep 17 00:00:00 2001
From: Cheng Shao <astrohavoc@gmail.com>
Date: Tue, 8 Mar 2022 09:41:19 +0000
Subject: [PATCH] CmmToC: use __builtin versions of memcpyish functions to fix
 type mismatch

Our memcpyish primop's type signatures doesn't match the C type
signatures. It's not a problem for typical archs, since their C ABI
permits dropping the result, but it doesn't work for wasm. The
previous logic would cast the memcpyish function pointer to an
incorrect type and perform an indirect call, which results in a
runtime trap on wasm.

The most straightforward fix is: don't emit EFF_ for memcpyish
functions. Since we don't want to include extra headers in .hc to
bring in their prototypes, we can just use the __builtin versions.
---
 compiler/GHC/CmmToC.hs | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/compiler/GHC/CmmToC.hs b/compiler/GHC/CmmToC.hs
index 9fe20cc0988f..a8fa7ef6cb75 100644
--- a/compiler/GHC/CmmToC.hs
+++ b/compiler/GHC/CmmToC.hs
@@ -269,7 +269,6 @@ pprStmt platform stmt =
         hargs    = zip args arg_hints
 
         need_cdecl
-          | Just _align <- machOpMemcpyishAlign op = True
           | MO_ResumeThread  <- op                 = True
           | MO_SuspendThread <- op                 = True
           | otherwise                              = False
@@ -931,10 +930,10 @@ pprCallishMachOp_for_C mop
         MO_F32_Fabs     -> text "fabsf"
         MO_ReadBarrier  -> text "load_load_barrier"
         MO_WriteBarrier -> text "write_barrier"
-        MO_Memcpy _     -> text "memcpy"
-        MO_Memset _     -> text "memset"
-        MO_Memmove _    -> text "memmove"
-        MO_Memcmp _     -> text "memcmp"
+        MO_Memcpy _     -> text "__builtin_memcpy"
+        MO_Memset _     -> text "__builtin_memset"
+        MO_Memmove _    -> text "__builtin_memmove"
+        MO_Memcmp _     -> text "__builtin_memcmp"
 
         MO_SuspendThread -> text "suspendThread"
         MO_ResumeThread  -> text "resumeThread"
-- 
GitLab