[Git][ghc/ghc][wip/ubsan] hadrian/rts: add support for building with UndefinedBehaviorSanitizer

Cheng Shao (@TerrorJack) gitlab at gitlab.haskell.org
Tue Oct 15 21:07:41 UTC 2024



Cheng Shao pushed to branch wip/ubsan at Glasgow Haskell Compiler / GHC


Commits:
73db80fd by Cheng Shao at 2024-10-15T23:05:46+02:00
hadrian/rts: add support for building with UndefinedBehaviorSanitizer

- - - - -


3 changed files:

- hadrian/src/Flavour.hs
- rts/Interpreter.c
- rts/rts.cabal


Changes:

=====================================
hadrian/src/Flavour.hs
=====================================
@@ -8,6 +8,7 @@ module Flavour
   , splitSections
   , enableThreadSanitizer
   , enableLateCCS
+  , enableUBSan
   , enableDebugInfo, enableTickyGhc
   , viaLlvmBackend
   , enableProfiledGhc
@@ -50,6 +51,7 @@ flavourTransformers = M.fromList
     , "no_split_sections" =: noSplitSections
     , "thread_sanitizer" =: enableThreadSanitizer False
     , "thread_sanitizer_cmm" =: enableThreadSanitizer True
+    , "ubsan"            =: enableUBSan
     , "llvm"             =: viaLlvmBackend
     , "profiled_ghc"     =: enableProfiledGhc
     , "no_dynamic_ghc"   =: disableDynamicGhcPrograms
@@ -239,6 +241,23 @@ enableThreadSanitizer instrumentCmm = addArgs $ notStage0 ? mconcat
         ]
     ]
 
+enableUBSan :: Flavour -> Flavour
+enableUBSan =
+  addArgs $
+    notStage0
+      ? mconcat
+        [ package rts ? builder (Cabal Flags) ? arg "ubsan",
+          builder (Ghc CompileHs) ? arg "-optc-fsanitize=undefined",
+          builder (Ghc CompileCWithGhc) ? arg "-optc-fsanitize=undefined",
+          builder (Ghc CompileCppWithGhc) ? arg "optcxx-fsanitize=undefined",
+          builder (Ghc LinkHs) ? arg "-optc-fsanitize=undefined"
+            <> arg "-optl-fsanitize=undefined"
+            <> arg "-optl-shared-libsan"
+            <> arg "-optl-Wl,-rpath,/usr/lib/llvm-19/lib/clang/19/lib/linux"
+            <> arg "-optl--rtlib=compiler-rt",
+          builder (Cc CompileC) ? arg "-fsanitize=undefined"
+        ]
+
 -- | Use the LLVM backend in stages 1 and later.
 viaLlvmBackend :: Flavour -> Flavour
 viaLlvmBackend = addArgs $ notStage0 ? builder Ghc ? arg "-fllvm"


=====================================
rts/Interpreter.c
=====================================
@@ -326,6 +326,7 @@ static StgWord app_ptrs_itbl[] = {
 HsStablePtr rts_breakpoint_io_action; // points to the IO action which is executed on a breakpoint
                                       // it is set in ghci/GHCi/Run.hs:withBreakAction
 
+__attribute__((no_sanitize("undefined")))
 Capability *
 interpretBCO (Capability* cap)
 {


=====================================
rts/rts.cabal
=====================================
@@ -81,6 +81,8 @@ flag thread-sanitizer
     mechanism supported by GCC and Clang. See Note [ThreadSanitizer]
     in @rts/include/rts/TSANUtils.h at .
   default: False
+flag ubsan
+  default: False
 
 library
     -- rts is a wired in package and
@@ -190,6 +192,9 @@ library
         cc-options: -fsanitize=thread
         ld-options: -fsanitize=thread
 
+      if flag(ubsan)
+        ld-options: -fsanitize=undefined -shared-libsan -Wl,-rpath,/usr/lib/llvm-19/lib/clang/19/lib/linux --rtlib=compiler-rt
+
       if os(linux)
          -- the RTS depends upon libc. while this dependency is generally
          -- implicitly added by `cc`, we must explicitly add it here to ensure



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/73db80fd9f263797019583324f49d1f4eb7d5ce7

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/73db80fd9f263797019583324f49d1f4eb7d5ce7
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/20241015/b9983951/attachment-0001.html>


More information about the ghc-commits mailing list