[Git][ghc/ghc][wip/andreask/lsp-crash] Enable -fkeep-cafs for both static and dynamic builds.

Andreas Klebinger (@AndreasK) gitlab at gitlab.haskell.org
Mon Dec 19 15:52:04 UTC 2022



Andreas Klebinger pushed to branch wip/andreask/lsp-crash at Glasgow Haskell Compiler / GHC


Commits:
069a6e3f by Andreas Klebinger at 2022-12-19T16:50:35+01:00
Enable -fkeep-cafs for both static and dynamic builds.

We used to use keepCAFsForGHCi.c to force -fkeep-cafs by using a
__attribute__((constructor)) function.

Besides being an odd choice this broke for static builds where
the constructor function was never called. Yuk.

So instead we now just tell cabal to compile the relevant files with
-fkeep-cafs.

-------------------------
Metric Increase:
    T21839r
-------------------------

- - - - -


7 changed files:

- − compiler/cbits/keepCAFsForGHCi.c
- compiler/ghc.cabal.in
- docs/users_guide/9.6.1-notes.rst
- ghc/ghc-bin.cabal.in
- hadrian/src/Packages.hs
- − utils/iserv/cbits/iservmain.c
- utils/iserv/iserv.cabal.in


Changes:

=====================================
compiler/cbits/keepCAFsForGHCi.c deleted
=====================================
@@ -1,15 +0,0 @@
-#include <Rts.h>
-
-// This file is only included in the dynamic library.
-// It contains an __attribute__((constructor)) function (run prior to main())
-// which sets the keepCAFs flag in the RTS, before any Haskell code is run.
-// This is required so that GHCi can use dynamic libraries instead of HSxyz.o
-// files.
-
-static void keepCAFsForGHCi(void) __attribute__((constructor));
-
-static void keepCAFsForGHCi(void)
-{
-    keepCAFs = 1;
-}
-


=====================================
compiler/ghc.cabal.in
=====================================
@@ -150,7 +150,6 @@ Library
     c-sources:
         cbits/cutils.c
         cbits/genSym.c
-        cbits/keepCAFsForGHCi.c
 
     hs-source-dirs:
         .


=====================================
docs/users_guide/9.6.1-notes.rst
=====================================
@@ -146,9 +146,9 @@ Runtime system
 
 - Updated to `Unicode 15.0.0 <https://www.unicode.org/versions/Unicode15.0.0/>`_.
 
-- Add standard Unicode case predicates :base-ref:`Data.Char.isUpperCase` and 
-  :base-ref:`Data.Char.isLowerCase`. These predicates use the standard Unicode 
-  case properties and are more intuitive than :base-ref:`Data.Char.isUpper` and 
+- Add standard Unicode case predicates :base-ref:`Data.Char.isUpperCase` and
+  :base-ref:`Data.Char.isLowerCase`. These predicates use the standard Unicode
+  case properties and are more intuitive than :base-ref:`Data.Char.isUpper` and
   :base-ref:`Data.Char.isLower`.
 
 ``ghc-prim`` library
@@ -160,6 +160,15 @@ Runtime system
 - Add `Foreign.C.Types.ConstPtr` was added to encode ``const``-qualified pointer return
   types in foreign declarations when using ``CApiFFI`` extension.
 
+- The ghc library no longer implicitly enables `-fkeep-cafs` when linked against
+  dynamically. If an application requires cafs to be kept alive passing
+  `-fkeep-cafs` explicitly can restore the old behaviour.
+  This only affects code which loads haskell libraries at runtime. This applies
+  for example to custom GHCi frontends or compiler frontends which support
+  template-haskell.
+  Statically linked applications are not affected as `-fkeep-cafs` was never
+  implicitly enabled for applications statically linking against the ghc library.
+
 ``ghc-heap`` library
 ~~~~~~~~~~~~~~~~~~~~
 


=====================================
ghc/ghc-bin.cabal.in
=====================================
@@ -52,6 +52,8 @@ Executable ghc
                  -Wnoncanonical-monoid-instances
                  -rtsopts=all
                  "-with-rtsopts=-K512M -H -I5 -T"
+                 -fkeep-cafs
+    -- See Note [-fkeep-cafs for GHC]
 
     if flag(internal-interpreter)
         -- NB: this is never built by the bootstrapping GHC+libraries
@@ -96,3 +98,13 @@ Executable ghc
         NoImplicitPrelude
       , ScopedTypeVariables
       , BangPatterns
+
+Note [-fkeep-cafs for GHC]
+~~~~~~~~~~~~~~~~~~~~~~~~~~
+When loading object files for TH or ghci we currently don't properly track
+references to CAFs. This means we need to enable -fkeep-cafs to avoid crashes
+resulting from reusing cafs that usually would have been gced already.
+For dynamic builds this was initially fixed in a roundabout way by enabling
+keep-cafs in a constructor function executed when loading the ghc library.
+However #22417 showed that this caused segfaults in static builds, where such
+constructors have no effect. So instead we now explicitly compile with -fkeep-cafs.


=====================================
hadrian/src/Packages.hs
=====================================
@@ -210,7 +210,7 @@ timeoutPath = "testsuite/timeout/install-inplace/bin/timeout" <.> exe
 -- TODO: Can we extract this information from Cabal files?
 -- | Some program packages should not be linked with Haskell main function.
 nonHsMainPackage :: Package -> Bool
-nonHsMainPackage = (`elem` [hp2ps, iserv, touchy, unlit, ghciWrapper])
+nonHsMainPackage = (`elem` [hp2ps, touchy, unlit, ghciWrapper])
 
 -- TODO: Combine this with 'programName'.
 -- | Path to the @autogen@ directory generated by 'buildAutogenFiles'.


=====================================
utils/iserv/cbits/iservmain.c deleted
=====================================
@@ -1,29 +0,0 @@
-#include <ghcversion.h>
-#if MIN_VERSION_GLASGOW_HASKELL(9,3,0,0)
-#  include <rts/PosixSource.h>
-#else // PosixSource.h not yet exposed, hacky inline for now.
-#  include <ghcplatform.h>
-#  if defined(solaris2_HOST_OS)
-#  define _POSIX_C_SOURCE 200112L
-#  define _XOPEN_SOURCE   600
-#  else
-#  define _POSIX_C_SOURCE 200809L
-#  define _XOPEN_SOURCE   700
-#  endif
-#endif
-#include <Rts.h>
-
-#include <HsFFI.h>
-
-int main (int argc, char *argv[])
-{
-    RtsConfig conf = defaultRtsConfig;
-
-    // We never know what symbols GHC will look up in the future, so
-    // we must retain CAFs for running interpreted code.
-    conf.keep_cafs = 1;
-
-    conf.rts_opts_enabled = RtsOptsAll;
-    extern StgClosure ZCMain_main_closure;
-    hs_main(argc, argv, &ZCMain_main_closure, conf);
-}


=====================================
utils/iserv/iserv.cabal.in
=====================================
@@ -28,9 +28,9 @@ cabal-version: >=1.10
 
 Executable iserv
     Default-Language: Haskell2010
-    ghc-options: -no-hs-main
+    -- See Note [-fkeep-cafs for GHC] in ghc.cabal.in
+    ghc-options: -fkeep-cafs -rtsopts
     Main-Is: Main.hs
-    C-Sources: cbits/iservmain.c
     Hs-Source-Dirs: src
     include-dirs: .
     Build-Depends: array      >= 0.5 && < 0.6,



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/069a6e3fef70562f99b6f89fefbfd4aecd5b0b59

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/069a6e3fef70562f99b6f89fefbfd4aecd5b0b59
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/20221219/a500ca69/attachment-0001.html>


More information about the ghc-commits mailing list