[Git][ghc/ghc][wip/haskell-nix-patches/musl64/ghc-9.6-missing-symbols-deadbeef] wrk: done, just need a better name and good commit

doyougnu (@doyougnu) gitlab at gitlab.haskell.org
Thu Sep 12 15:50:59 UTC 2024



doyougnu pushed to branch wip/haskell-nix-patches/musl64/ghc-9.6-missing-symbols-deadbeef at Glasgow Haskell Compiler / GHC


Commits:
501963a9 by doyougnu at 2024-09-12T11:50:24-04:00
wrk: done, just need a better name and good commit

- - - - -


8 changed files:

- rts/Linker.c
- testsuite/tests/ghci/linking/Makefile
- testsuite/tests/rts/flags/T25240/FFI.hs → testsuite/tests/ghci/linking/T25240/FFI.hs
- + testsuite/tests/ghci/linking/T25240/Makefile
- + testsuite/tests/ghci/linking/T25240/T25240.hs
- testsuite/tests/rts/flags/T25240/T25240.hs → testsuite/tests/ghci/linking/T25240/T25240f.hs
- testsuite/tests/rts/flags/T25240/TH.hs → testsuite/tests/ghci/linking/T25240/TH.hs
- testsuite/tests/rts/flags/T25240/all.T → testsuite/tests/ghci/linking/T25240/all.T


Changes:

=====================================
rts/Linker.c
=====================================
@@ -946,16 +946,17 @@ SymbolAddr* lookupSymbol( SymbolName* lbl )
     // lookupDependentSymbol directly.
     SymbolAddr* r = lookupDependentSymbol(lbl, NULL, NULL);
     if (!r) {
-        errorBelch("^^ Could not load '%s', dependency unresolved. "
-                   "See top entry above.\n", lbl);
-        IF_DEBUG(linker, printLoadedObjects());
-        fflush(stderr);
+        if (!RtsFlags.MiscFlags.linkUnknownSymbols) {
+          errorBelch("^^ Could not load '%s', dependency unresolved. "
+                     "See top entry above.\n",
+                     lbl);
+          IF_DEBUG(linker, printLoadedObjects());
+          fflush(stderr);
+        }
 
         // if --link-unknown-symbols is passed into the RTS we allow the linker
         // to optimistically continue
-        if (RtsFlags.MiscFlags.linkUnknownSymbols){
-            r = (void*) 0xDEADBEEF;
-        }
+        r = (void*) 0xDEADBEEF;
 
     }
 


=====================================
testsuite/tests/ghci/linking/Makefile
=====================================
@@ -145,3 +145,7 @@ T15729:
 big-obj:
 	'$(TEST_CC)' -c -Wa,-mbig-obj big-obj-c.c -o big-obj-c.o
 	echo "main" | '$(TEST_HC)' $(TEST_HC_OPTS_INTERACTIVE) big-obj-c.o big-obj.hs
+
+.PHONY: T25240
+T25240:
+	echo "main" | "$(TEST_HC)" $(TEST_HC_OPTS_INTERACTIVE) FFI.hs TH.hs +RTS --link-unknown-objects -RTS


=====================================
testsuite/tests/rts/flags/T25240/FFI.hs → testsuite/tests/ghci/linking/T25240/FFI.hs
=====================================
@@ -3,6 +3,8 @@
 {-# LANGUAGE TemplateHaskell #-}
 {-# LANGUAGE UnliftedFFITypes #-}
 
+{-# OPTIONS_GHC -fno-warn-missing-methods #-}
+
 module FFI
   ( D(..), D2(..), tabulate
   ) where


=====================================
testsuite/tests/ghci/linking/T25240/Makefile
=====================================
@@ -0,0 +1,11 @@
+TOP=../../../..
+include $(TOP)/mk/boilerplate.mk
+include $(TOP)/mk/test.mk
+
+.PHONY: T25240
+T25240:
+	"$(TEST_HC)" $(TEST_HC_OPTS_INTERACTIVE) FFI.hs TH.hs T25240.hs +RTS --link-unknown-symbols -RTS
+
+.PHONY: T25240f
+T25240f:
+	"$(TEST_HC)" $(TEST_HC_OPTS_INTERACTIVE) FFI.hs TH.hs T25240f.hs


=====================================
testsuite/tests/ghci/linking/T25240/T25240.hs
=====================================
@@ -0,0 +1,33 @@
+
+{-
+Here we have two modules, FFI and TH.
+
+In the FFI module, we define a data type D and a function to be used in Template
+Haskell, tabulate. Completely unrelatedly, we also include a Num D instance
+which makes use of a foreign import prim declaration, foreign import prim
+"prim_func". In the TH module, we use tabulate and get a linking error:
+
+FFI.o: unknown symbol `prim_func'
+
+
+This suggests we are trying to run some code at splice time involving the Num D
+instance, even though Num D is not involved in any way. In particular, if we
+change the use of prim_func to something like error "blah", then the error never
+gets thrown, which shows we don't run that code at splice time. Note that it is
+important to use a typeclass to trigger this bug. If we instead use the FFI
+import in a top-level binding, there are no problems. It doesn't matter that the
+class is Num or some other (possibly user-defined) class.
+
+The essential bug is that we have a symbol at link time which comes from dead
+code and yet we try to resolve it anyway. The fix is to pass the flag
+--link-unknown-symbols to the RTS which will assign the symbol a magic number
+0xDEADBEEF and allow the linker to continue. See MR!13012 for the implementation
+of that flag
+-}
+
+module DeadBeef where
+
+import TH
+
+main :: IO ()
+main = print th_bug


=====================================
testsuite/tests/rts/flags/T25240/T25240.hs → testsuite/tests/ghci/linking/T25240/T25240f.hs
=====================================
@@ -1,4 +1,7 @@
 
+-- duplicate of T25240 to test the failure case
+module DeadBeef where
+
 import TH
 
 main :: IO ()


=====================================
testsuite/tests/rts/flags/T25240/TH.hs → testsuite/tests/ghci/linking/T25240/TH.hs
=====================================


=====================================
testsuite/tests/rts/flags/T25240/all.T → testsuite/tests/ghci/linking/T25240/all.T
=====================================
@@ -1,2 +1,5 @@
 test('T25240', [req_rts_linker, extra_files(['FFI.hs', 'TH.hs'])],
-    multimod_compile, ['T25240', '+RTS --link-unknown-symbols -RTS'])
+    makefile_test, ['T25240'])
+
+test('T25240f', [req_rts_linker, expect_fail, extra_files(['FFI.hs', 'TH.hs'])],
+    makefile_test, ['T25240f'])



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/501963a972af8f4266ea15da89946e8c943ccef0

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/501963a972af8f4266ea15da89946e8c943ccef0
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/20240912/6d87044d/attachment-0001.html>


More information about the ghc-commits mailing list