[Git][ghc/ghc][wip/marge_bot_batch_merge_job] 2 commits: Revert "compiler: start deprecating cmmToRawCmmHook"

Marge Bot (@marge-bot) gitlab at gitlab.haskell.org
Thu Oct 17 17:22:02 UTC 2024



Marge Bot pushed to branch wip/marge_bot_batch_merge_job at Glasgow Haskell Compiler / GHC


Commits:
9dc9be7e by Cheng Shao at 2024-10-17T13:21:53-04:00
Revert "compiler: start deprecating cmmToRawCmmHook"

This reverts commit 1c064ef1f3e1aa2afc996e962ad53effa99ec5f4. Turns
out the GHC-WPC project does use it to observe Cmm in the pipeline,
see #25363.

- - - - -
b9b59b69 by Cheng Shao at 2024-10-17T13:21:53-04:00
rts: fix pointer overflow undefined behavior in bytecode interpreter

This patch fixes an unnoticed undefined behavior in the bytecode
interpreter. It can be caught by building `rts/Interpreter.c` with
`-fsanitize=pointer-overflow`, the warning message is something like:

```
rts/Interpreter.c:1369:13: runtime error: addition of unsigned offset to 0x004200197660 overflowed to 0x004200197658
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior rts/Interpreter.c:1369:13
rts/Interpreter.c:1265:13: runtime error: addition of unsigned offset to 0x004200197660 overflowed to 0x004200197658
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior rts/Interpreter.c:1265:13
rts/Interpreter.c:1645:13: runtime error: addition of unsigned offset to 0x0042000b22f8 overflowed to 0x0042000b22f0
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior rts/Interpreter.c:1645:13
```

Whenever we do something like `SpW(-1)`, the negative argument is
implicitly converted to an unsigned integer type and causes pointer
arithmetic overflow. It happens to be harmless for most targets since
overflowing would wrap the result to desired value, but it's still
coincidental and undefined behavior. Furthermore, it causes real
damage to the wasm backend, given clang-20 will emit invalid wasm code
that crashes at run-time for this kind of C code! (see
https://github.com/llvm/llvm-project/issues/108770)

The fix here is adding some explicit casts to ensure we always use the
signed `ptrdiff_t` type as right hand operand of pointer arithmetic.

- - - - -


3 changed files:

- compiler/GHC/Driver/Hooks.hs
- compiler/GHC/Driver/Main.hs
- rts/Interpreter.c


Changes:

=====================================
compiler/GHC/Driver/Hooks.hs
=====================================
@@ -154,8 +154,6 @@ data Hooks = Hooks
                                  -> IO (CgStream RawCmmGroup a)))
   }
 
-{-# DEPRECATED cmmToRawCmmHook "cmmToRawCmmHook is being deprecated. If you do use it in your project, please raise a GHC issue!" #-}
-
 class HasHooks m where
     getHooks :: m Hooks
 


=====================================
compiler/GHC/Driver/Main.hs
=====================================
@@ -5,9 +5,6 @@
 
 {-# OPTIONS_GHC -fprof-auto-top #-}
 
--- Remove this after cmmToRawCmmHook removal
-{-# OPTIONS_GHC -Wno-deprecations #-}
-
 -------------------------------------------------------------------------------
 --
 -- | Main API for compiling plain Haskell source code.


=====================================
rts/Interpreter.c
=====================================
@@ -157,11 +157,11 @@ tag functions as tag inference currently doesn't rely on those being properly ta
    cap->r.rRet = (retcode);                             \
    return cap;
 
-#define Sp_plusB(n)  ((void *)(((StgWord8*)Sp) + (n)))
-#define Sp_minusB(n) ((void *)(((StgWord8*)Sp) - (n)))
+#define Sp_plusB(n)  ((void *)((StgWord8*)Sp + (ptrdiff_t)(n)))
+#define Sp_minusB(n) ((void *)((StgWord8*)Sp - (ptrdiff_t)(n)))
 
-#define Sp_plusW(n)  (Sp_plusB((n) * sizeof(W_)))
-#define Sp_minusW(n) (Sp_minusB((n) * sizeof(W_)))
+#define Sp_plusW(n)  (Sp_plusB((ptrdiff_t)(n) * (ptrdiff_t)sizeof(W_)))
+#define Sp_minusW(n) (Sp_minusB((ptrdiff_t)(n) * (ptrdiff_t)sizeof(W_)))
 
 #define Sp_addB(n)   (Sp = Sp_plusB(n))
 #define Sp_subB(n)   (Sp = Sp_minusB(n))



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/d7d1da7d919d7f52867977edb8a721a306fb1cec...b9b59b69a905bcbc7175e2c654614da47d8e6f3b

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/d7d1da7d919d7f52867977edb8a721a306fb1cec...b9b59b69a905bcbc7175e2c654614da47d8e6f3b
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20241017/8c47d961/attachment-0001.html>


More information about the ghc-commits mailing list