| ... |
... |
@@ -111,6 +111,7 @@ import GHC.Linker.Types |
|
111
|
111
|
import Control.Monad
|
|
112
|
112
|
|
|
113
|
113
|
import Data.Array
|
|
|
114
|
+import Data.Containers.ListUtils (nubOrd)
|
|
114
|
115
|
import Data.ByteString (ByteString)
|
|
115
|
116
|
import qualified Data.Set as Set
|
|
116
|
117
|
import Data.Char (isSpace)
|
| ... |
... |
@@ -172,12 +173,12 @@ uninitialised = panic "Loader not initialised" |
|
172
|
173
|
|
|
173
|
174
|
modifyLoaderState_ :: SafeInterp -> (Interp -> LoaderState -> IO LoaderState) -> IO ()
|
|
174
|
175
|
modifyLoaderState_ interp0 f = withInterp interp0 $ \ interp ->
|
|
175
|
|
- modifyMVar_ (loader_state (interpLoader interp))
|
|
|
176
|
+ modifyMVarMasked_ (loader_state (interpLoader interp))
|
|
176
|
177
|
(fmap pure . f interp . fromMaybe uninitialised)
|
|
177
|
178
|
|
|
178
|
179
|
modifyLoaderState :: SafeInterp -> (Interp -> LoaderState -> IO (LoaderState, a)) -> IO a
|
|
179
|
180
|
modifyLoaderState interp0 f = withInterp interp0 $ \ interp ->
|
|
180
|
|
- modifyMVar (loader_state (interpLoader interp))
|
|
|
181
|
+ modifyMVarMasked (loader_state (interpLoader interp))
|
|
181
|
182
|
(fmapFst pure . f interp . fromMaybe uninitialised)
|
|
182
|
183
|
where fmapFst f = fmap (\(x, y) -> (f x, y))
|
|
183
|
184
|
|
| ... |
... |
@@ -234,27 +235,27 @@ lookupFromLoadedEnv interp name = do |
|
234
|
235
|
loadName :: SafeInterp -> HscEnv -> Name -> IO (ForeignHValue, [LinkableUsage], PkgsLoaded)
|
|
235
|
236
|
loadName interp0 hsc_env name = do
|
|
236
|
237
|
initLoaderState interp0 hsc_env
|
|
237
|
|
- modifyLoaderState interp0 $ \interp pls0 -> do
|
|
238
|
|
- (pls, links, pkgs) <- if not (isExternalName name)
|
|
239
|
|
- then return (pls0, [], emptyUDFM)
|
|
240
|
|
- else do
|
|
241
|
|
- (pls', ok, links, pkgs) <- loadDependencies interp hsc_env pls0 noSrcSpan
|
|
242
|
|
- [nameModule name]
|
|
243
|
|
- if failed ok
|
|
244
|
|
- then throwGhcExceptionIO (ProgramError "")
|
|
245
|
|
- else return (pls', links, pkgs)
|
|
246
|
|
-
|
|
247
|
|
- case lookupNameBytecodeState (bco_loader_state pls) name of
|
|
248
|
|
- Just (_,aa) -> return (pls,(aa, links, pkgs))
|
|
|
238
|
+ (ok, m, links, pkgs) <- modifyLoaderState interp0 $ \interp pls0 -> do
|
|
|
239
|
+ (pls, ok, links, pkgs) <- if not (isExternalName name)
|
|
|
240
|
+ then return (pls0, Succeeded, [], emptyUDFM)
|
|
|
241
|
+ else loadDependencies interp hsc_env pls0 noSrcSpan [nameModule name]
|
|
|
242
|
+
|
|
|
243
|
+ m <- case lookupNameBytecodeState (bco_loader_state pls) name of
|
|
|
244
|
+ _ | failed ok -> return Nothing
|
|
|
245
|
+ Just (_,aa) -> return (Just aa)
|
|
249
|
246
|
Nothing -> assertPpr (isExternalName name) (ppr name) $
|
|
250
|
|
- do let sym_to_find = IClosureSymbol name
|
|
251
|
|
- -- TODO: does not take loader state lock
|
|
252
|
|
- m <- lookupClosure interp sym_to_find
|
|
253
|
|
- r <- case m of
|
|
254
|
|
- Just fhv -> return fhv
|
|
255
|
|
- Nothing -> linkFail "GHC.Linker.Loader.loadName"
|
|
256
|
|
- (ppr sym_to_find)
|
|
257
|
|
- return (pls,(r, links, pkgs))
|
|
|
247
|
+ -- TODO: does not take loader state lock
|
|
|
248
|
+ interpUninterruptibleMask_ interp $
|
|
|
249
|
+ lookupClosure interp sym_to_find
|
|
|
250
|
+ return (pls, (ok, m, links, pkgs))
|
|
|
251
|
+
|
|
|
252
|
+ when (failed ok) $ throwGhcExceptionIO (ProgramError "")
|
|
|
253
|
+ case m of
|
|
|
254
|
+ Just fhv -> return (fhv, links, pkgs)
|
|
|
255
|
+ Nothing -> linkFail "GHC.Linker.Loader.loadName"
|
|
|
256
|
+ (ppr sym_to_find)
|
|
|
257
|
+ where
|
|
|
258
|
+ sym_to_find = IClosureSymbol name
|
|
258
|
259
|
|
|
259
|
260
|
-- | Restrict a 'PkgsLoaded' map to the packages directly needed and their
|
|
260
|
261
|
-- full transitive closure (via 'loaded_pkg_trans_deps').
|
| ... |
... |
@@ -465,7 +466,7 @@ loadCmdLineLibs'' interp hsc_env pls = |
|
465
|
466
|
++ map Framework frameworks
|
|
466
|
467
|
if null cmdline_lib_specs
|
|
467
|
468
|
then return pls
|
|
468
|
|
- else do
|
|
|
469
|
+ else interpUninterruptibleMask_ interp $ do
|
|
469
|
470
|
-- Add directories to library search paths, this only has an effect
|
|
470
|
471
|
-- on Windows. On Unix OSes this function is a NOP.
|
|
471
|
472
|
let all_paths = let paths = takeDirectory (pgm_c dflags)
|
| ... |
... |
@@ -737,16 +738,19 @@ loadDecls interp0 hsc_env span linkable = do |
|
737
|
738
|
initLoaderState interp0 hsc_env
|
|
738
|
739
|
|
|
739
|
740
|
-- Take lock for the actual work.
|
|
740
|
|
- modifyLoaderState interp0 $ \interp pls0 -> do
|
|
|
741
|
+ (ok, ok2, links_needed, units_needed) <- modifyLoaderState interp0 $ \interp pls0 -> do
|
|
741
|
742
|
-- Link the packages and modules required
|
|
742
|
743
|
(pls, ok, links_needed, units_needed) <- loadDependencies interp hsc_env pls0 span needed_mods
|
|
743
|
|
- if failed ok
|
|
744
|
|
- then throwGhcExceptionIO (ProgramError "could not load dependencies for decls")
|
|
745
|
|
- else do
|
|
746
|
|
- (pls2, ok2) <- loadInternalModuleLinkables interp hsc_env pls [linkable]
|
|
747
|
|
- when (failed ok2) $
|
|
748
|
|
- throwGhcExceptionIO (ProgramError "could not load linkable for decls")
|
|
749
|
|
- return (pls2, (links_needed, units_needed))
|
|
|
744
|
+ (pls2, ok2) <- if failed ok
|
|
|
745
|
+ then return (pls, Failed)
|
|
|
746
|
+ else loadInternalModuleLinkables interp hsc_env pls [linkable]
|
|
|
747
|
+ return (pls2, (ok, ok2, links_needed, units_needed))
|
|
|
748
|
+
|
|
|
749
|
+ when (failed ok) $
|
|
|
750
|
+ throwGhcExceptionIO (ProgramError "could not load dependencies for decls")
|
|
|
751
|
+ when (failed ok2) $
|
|
|
752
|
+ throwGhcExceptionIO (ProgramError "could not load linkable for decls")
|
|
|
753
|
+ return (links_needed, units_needed)
|
|
750
|
754
|
where
|
|
751
|
755
|
cbcs = linkableBCOs linkable
|
|
752
|
756
|
|
| ... |
... |
@@ -774,11 +778,11 @@ loadDecls interp0 hsc_env span linkable = do |
|
774
|
778
|
loadModule :: SafeInterp -> HscEnv -> Module -> IO ()
|
|
775
|
779
|
loadModule interp0 hsc_env mod = do
|
|
776
|
780
|
initLoaderState interp0 hsc_env
|
|
777
|
|
- modifyLoaderState_ interp0 $ \interp pls -> do
|
|
|
781
|
+ ok <- modifyLoaderState interp0 $ \interp pls -> do
|
|
778
|
782
|
(pls', ok, _, _) <- loadDependencies interp hsc_env pls noSrcSpan [mod]
|
|
779
|
|
- if failed ok
|
|
780
|
|
- then throwGhcExceptionIO (ProgramError "could not load module")
|
|
781
|
|
- else return pls'
|
|
|
783
|
+ return (pls', ok)
|
|
|
784
|
+ when (failed ok) $
|
|
|
785
|
+ throwGhcExceptionIO (ProgramError "could not load module")
|
|
782
|
786
|
|
|
783
|
787
|
{- **********************************************************************
|
|
784
|
788
|
|
| ... |
... |
@@ -811,7 +815,7 @@ loadInternalModuleLinkables interp hsc_env pls linkables = |
|
811
|
815
|
|
|
812
|
816
|
loadModuleLinkables :: Interp -> HscEnv -> LoaderState -> KeepModuleLinkableDefinitions -> [Linkable] -> IO (LoaderState, SuccessFlag)
|
|
813
|
817
|
loadModuleLinkables interp hsc_env pls keep_spec linkables
|
|
814
|
|
- = mask_ $ do -- don't want to be interrupted by ^C in here
|
|
|
818
|
+ = interpUninterruptibleMask_ interp $ do -- don't want to be interrupted by ^C in here
|
|
815
|
819
|
|
|
816
|
820
|
debugTraceMsg (hsc_logger hsc_env) 3 $
|
|
817
|
821
|
hang (text "Loading module linkables") 2 $ vcat [
|
| ... |
... |
@@ -862,18 +866,19 @@ loadObjects interp hsc_env pls objs = do |
|
862
|
866
|
if interpreterDynamic interp
|
|
863
|
867
|
then do pls2 <- dynLoadObjs interp hsc_env pls1 wanted_objs
|
|
864
|
868
|
return (pls2, Succeeded)
|
|
865
|
|
- else do mapM_ (loadObj interp) wanted_objs
|
|
|
869
|
+ else do loaded <- newIORef []
|
|
|
870
|
+ let load obj = loadObj interp obj >> modifyIORef' loaded (obj :)
|
|
|
871
|
+ undo = readIORef loaded >>= mapM_ (unloadObj interp)
|
|
866
|
872
|
|
|
867
|
873
|
-- Link them all together
|
|
868
|
|
- ok <- resolveObjs interp
|
|
|
874
|
+ ok <- (mapM_ load wanted_objs >> resolveObjs interp)
|
|
|
875
|
+ `onException` undo
|
|
869
|
876
|
|
|
870
|
|
- -- If resolving failed, unload all our
|
|
871
|
|
- -- object modules and carry on
|
|
872
|
877
|
if succeeded ok then
|
|
873
|
878
|
return (pls1, Succeeded)
|
|
874
|
879
|
else do
|
|
875
|
|
- pls2 <- unload_wkr interp pls1
|
|
876
|
|
- return (pls2, Failed)
|
|
|
880
|
+ undo
|
|
|
881
|
+ return (pls, Failed)
|
|
877
|
882
|
|
|
878
|
883
|
|
|
879
|
884
|
-- | Create a shared library containing the given object files
|
| ... |
... |
@@ -1146,10 +1151,6 @@ What we need to avoid is having more in the 'LoaderState' than is actually |
|
1146
|
1151
|
loaded. This can only happen while unloading, which is why purge the symbol cache
|
|
1147
|
1152
|
before the unload.
|
|
1148
|
1153
|
|
|
1149
|
|
-We should also make sure the emptied out loader state returned from unload_wkr
|
|
1150
|
|
-gets actually written to the loader state, the failure to do so is tracked by
|
|
1151
|
|
-#28104.
|
|
1152
|
|
-
|
|
1153
|
1154
|
-}
|
|
1154
|
1155
|
|
|
1155
|
1156
|
-- ---------------------------------------------------------------------------
|
| ... |
... |
@@ -1168,16 +1169,15 @@ unload interp0 hsc_env |
|
1168
|
1169
|
-- Initialise the linker (if it's not been done already)
|
|
1169
|
1170
|
initLoaderState interp0 hsc_env
|
|
1170
|
1171
|
|
|
1171
|
|
- _new_pls <- modifyLoaderState interp0 $ \interp pls -> do
|
|
1172
|
|
- pls1 <- unload_wkr interp pls
|
|
1173
|
|
- return (pls1, pls1)
|
|
1174
|
|
-
|
|
1175
|
|
- return ()
|
|
|
1172
|
+ withInterp interp0 $ \_ -> do
|
|
|
1173
|
+ err <- modifyLoaderState interp0 $ \interp ->
|
|
|
1174
|
+ interpUninterruptibleMask_ interp . unload_wkr interp
|
|
|
1175
|
+ mapM_ throwIO err
|
|
1176
|
1176
|
|
|
1177
|
1177
|
unload_wkr
|
|
1178
|
1178
|
:: Interp
|
|
1179
|
1179
|
-> LoaderState
|
|
1180
|
|
- -> IO LoaderState
|
|
|
1180
|
+ -> IO (LoaderState, Maybe SomeException)
|
|
1181
|
1181
|
-- Does the core unload business
|
|
1182
|
1182
|
-- (the wrapper blocks exceptions and deals with the LS get and put)
|
|
1183
|
1183
|
|
| ... |
... |
@@ -1186,41 +1186,47 @@ unload_wkr interp pls@LoaderState{..} = do |
|
1186
|
1186
|
-- we're unloading some code. -fghci-leak-check with the tests in
|
|
1187
|
1187
|
-- testsuite/ghci can detect space leaks here.
|
|
1188
|
1188
|
|
|
1189
|
|
- let linkables_to_unload = moduleEnvElts objs_loaded ++ moduleEnvElts bcos_loaded
|
|
|
1189
|
+ let objs_to_unload = mapModuleEnv nubOrd $
|
|
|
1190
|
+ plusModuleEnv_C (++) (mapModuleEnv linkableUsageObjs objs_loaded)
|
|
|
1191
|
+ (mapModuleEnv linkableUsageObjs bcos_loaded)
|
|
1190
|
1192
|
|
|
1191
|
1193
|
-- If we unloaded any object files at all, we need to purge the cache
|
|
1192
|
1194
|
-- of lookupSymbol results.
|
|
1193
|
1195
|
-- We do so before actually unloading, so the cache is never stale.
|
|
1194
|
1196
|
--
|
|
1195
|
1197
|
-- See Note [LoaderState may lag the interpreter]
|
|
1196
|
|
- when (not (null (filter (not . null . linkableUsageObjs) linkables_to_unload))) $
|
|
|
1198
|
+ when (any (not . null) (moduleEnvElts objs_to_unload)) $
|
|
1197
|
1199
|
purgeLookupSymbolCache interp
|
|
1198
|
1200
|
|
|
1199
|
|
- mapM_ unloadObjs linkables_to_unload
|
|
|
1201
|
+ (objs_left, err) <- foldM unloadObjs (objs_loaded, Nothing) (moduleEnvToList objs_to_unload)
|
|
1200
|
1202
|
|
|
1201
|
|
- -- FIXME: this emptied state should be committed even on exceptions, see #28104
|
|
1202
|
1203
|
let !new_pls = pls { bco_loader_state = modifyHomePackageBytecodeState bco_loader_state $ \_ -> emptyBytecodeState,
|
|
1203
|
1204
|
-- NB: we don't unload the external package
|
|
1204
|
1205
|
bcos_loaded = emptyModuleEnv,
|
|
1205
|
|
- objs_loaded = emptyModuleEnv }
|
|
|
1206
|
+ objs_loaded = objs_left }
|
|
1206
|
1207
|
|
|
1207
|
|
- return new_pls
|
|
|
1208
|
+ return (new_pls, err)
|
|
1208
|
1209
|
where
|
|
1209
|
|
- unloadObjs :: LinkableUsage -> IO ()
|
|
1210
|
|
- unloadObjs lnk
|
|
1211
|
|
- | interpreterDynamic interp = return ()
|
|
|
1210
|
+ unloadObjs
|
|
|
1211
|
+ :: (LinkableSet LinkableUsage, Maybe SomeException)
|
|
|
1212
|
+ -> (Module, [FilePath])
|
|
|
1213
|
+ -> IO (LinkableSet LinkableUsage, Maybe SomeException)
|
|
|
1214
|
+ unloadObjs acc@(_, Just _) _ = return acc
|
|
|
1215
|
+ unloadObjs (objs_left, Nothing) (mod, objs)
|
|
|
1216
|
+ | interpreterDynamic interp = return (delModuleEnv objs_left mod, Nothing)
|
|
1212
|
1217
|
-- We don't do any cleanup when linking objects with the
|
|
1213
|
1218
|
-- dynamic linker. Doing so introduces extra complexity for
|
|
1214
|
1219
|
-- not much benefit.
|
|
1215
|
1220
|
|
|
1216
|
1221
|
| otherwise
|
|
1217
|
|
- = mapM_ (unloadObj interp) (linkableUsageObjs lnk)
|
|
|
1222
|
+ = do r <- try $ mapM_ (unloadObj interp) objs
|
|
1218
|
1223
|
-- The components of a BCO linkable may contain
|
|
1219
|
1224
|
-- dot-o files (generated from C stubs).
|
|
1220
|
1225
|
--
|
|
1221
|
1226
|
-- But the BCO parts can be unlinked just by
|
|
1222
|
1227
|
-- letting go of them (plus of course depopulating
|
|
1223
|
1228
|
-- the symbol table which is done in the main body)
|
|
|
1229
|
+ return (delModuleEnv objs_left mod, either Just (const Nothing) r)
|
|
1224
|
1230
|
|
|
1225
|
1231
|
showLS :: LibrarySpec -> String
|
|
1226
|
1232
|
showLS (Objects nms) = "(static) [" ++ intercalate ", " nms ++ "]"
|
| ... |
... |
@@ -1315,7 +1321,8 @@ loadPackages' :: Interp -> HscEnv -> [UnitId] -> LoaderState -> IO LoaderState |
|
1315
|
1321
|
loadPackages' interp hsc_env new_pks pls = do
|
|
1316
|
1322
|
(reverse -> pkgs_info_list, pkgs_almost_loaded) <-
|
|
1317
|
1323
|
loadMoreUnits hsc_env new_pks (pkgs_loaded pls)
|
|
1318
|
|
- loadPackage interp hsc_env pkgs_info_list (pls { pkgs_loaded = pkgs_almost_loaded })
|
|
|
1324
|
+ interpUninterruptibleMask_ interp $
|
|
|
1325
|
+ loadPackage interp hsc_env pkgs_info_list (pls { pkgs_loaded = pkgs_almost_loaded })
|
|
1319
|
1326
|
|
|
1320
|
1327
|
|
|
1321
|
1328
|
loadPackage :: Interp -> HscEnv -> [UnitInfo] -> LoaderState -> IO LoaderState
|