Zubin pushed to branch wip/linkable-hashes at Glasgow Haskell Compiler / GHC Commits: f3f5e98c by Zubin Duggal at 2026-11-20T14:15:50+05:30 bytecode: Treat persistent bytecode files with a bad header as stale - - - - - 8 changed files: - compiler/GHC/ByteCode/Serialize.hs - compiler/GHC/Driver/Main/Passes.hs - compiler/GHC/Driver/Pipeline.hs - testsuite/tests/driver/bytecode-object/Makefile - testsuite/tests/driver/bytecode-object/all.T - + testsuite/tests/driver/bytecode-object/bytecode_object28.script - + testsuite/tests/driver/bytecode-object/bytecode_object28.stdout - + testsuite/tests/driver/bytecode-object/bytecode_object29.stdout Changes: ===================================== compiler/GHC/ByteCode/Serialize.hs ===================================== @@ -29,6 +29,7 @@ import GHC.Prelude import GHC.ByteCode.Binary import GHC.ByteCode.Recomp.Binary (computeFingerprint) import GHC.ByteCode.Types +import GHC.Data.Maybe (rightToMaybe) import GHC.Driver.DynFlags import GHC.Driver.Env import GHC.Iface.Binary @@ -36,6 +37,7 @@ import GHC.Iface.Recomp.Binary (putNameLiterally) import GHC.Linker.Types import GHC.Unit.Types import GHC.Utils.Binary +import GHC.Utils.Exception (tryIO) import GHC.Utils.Fingerprint (Fingerprint(..), fingerprintFingerprints) import GHC.Utils.Logger import GHC.Utils.Panic @@ -148,8 +150,9 @@ readBytecodeLib hsc_env path = do res <- getWithUserData (hsc_NC hsc_env) bh pure res +-- | 'Nothing' if the file can't be read or its header is not valid. -- See Note [Hash of bytecode libs] -readBytecodeLibInputsHash :: FilePath -> IO Fingerprint +readBytecodeLibInputsHash :: FilePath -> IO (Maybe Fingerprint) readBytecodeLibInputsHash = readPersistentBytecodeHash BytecodeLibraryFile -- | Convert an 'OnDiskModuleByteCode' to an 'ModuleByteCode'. @@ -237,7 +240,8 @@ readBinByteCode hsc_env f = do decodeOnDiskModuleByteCode hsc_env odbco -- | Read the hash of a bytecode file, without reading the rest of the file. -readBinByteCodeHash :: FilePath -> IO Fingerprint +-- 'Nothing' if the file can't be read or its header is not valid. +readBinByteCodeHash :: FilePath -> IO (Maybe Fingerprint) readBinByteCodeHash = readPersistentBytecodeHash ModuleByteCodeFile readOnDiskModuleByteCode :: HscEnv -> FilePath -> IO OnDiskModuleByteCode @@ -318,44 +322,46 @@ writePersistentBytecodeHeader file_kind (Fingerprint w1 w2) bh = do put_ bh (FixedLengthEncoding w1) put_ bh (FixedLengthEncoding w2) +-- | Read only the header of a persistent bytecode file. +getPersistentBytecodeHeader :: PersistentBytecodeFile -> ReadBinHandle -> IO (Either String Fingerprint) +getPersistentBytecodeHeader file_kind bh = do + magic <- get bh + if unFixedLength magic /= unFixedLength expected_magic + then mismatch "magic" (show $ unFixedLength expected_magic) (show $ unFixedLength magic) + else do + version <- get bh + if version /= persistentBytecodeVersion + then mismatch "version" + (showPersistentBytecodeVersion persistentBytecodeVersion) + (showPersistentBytecodeVersion version) + else do + w1 <- get bh + w2 <- get bh + pure $ Right (Fingerprint (unFixedLength w1) (unFixedLength w2)) + where + expected_magic = persistentBytecodeMagic file_kind + mismatch what expected actual = + pure $ Left $ what ++ " (expected " ++ expected ++ ", got " ++ actual ++ ")" + -- | Validate the header of a persistent bytecode file, and return its hash. readPersistentBytecodeHeader :: PersistentBytecodeFile -> FilePath -> ReadBinHandle -> IO Fingerprint readPersistentBytecodeHeader file_kind path bh = do - let mismatch what expected actual = - headerMismatch file_kind path $ - what ++ " (expected " ++ expected ++ ", got " ++ actual ++ ")" - - magic <- get bh - let expected_magic = persistentBytecodeMagic file_kind - if unFixedLength magic == unFixedLength expected_magic - then pure () - else mismatch "magic" (show $ unFixedLength expected_magic) (show $ unFixedLength magic) - - version <- get bh - if version == persistentBytecodeVersion - then pure () - else mismatch "version" - (showPersistentBytecodeVersion persistentBytecodeVersion) - (showPersistentBytecodeVersion version) - - w1 <- get bh - w2 <- get bh - pure (Fingerprint (unFixedLength w1) (unFixedLength w2)) - -headerMismatch :: PersistentBytecodeFile -> FilePath -> String -> IO a -headerMismatch file_kind path what = - throwGhcExceptionIO $ ProgramError $ - persistentBytecodeFileDescription file_kind ++ " header mismatch in " ++ path ++ - ": " ++ what + r <- getPersistentBytecodeHeader file_kind bh + case r of + Right hash -> pure hash + Left what -> throwGhcExceptionIO $ ProgramError $ + persistentBytecodeFileDescription file_kind ++ " header mismatch in " ++ path ++ + ": " ++ what -- | Read the hash from the header of a persistent bytecode file, without --- reading the rest of the file. -readPersistentBytecodeHash :: PersistentBytecodeFile -> FilePath -> IO Fingerprint +-- reading the rest of the file. 'Nothing' if the file can't be read or its +-- header is not valid. +readPersistentBytecodeHash :: PersistentBytecodeFile -> FilePath -> IO (Maybe Fingerprint) readPersistentBytecodeHash file_kind path = do - mb_bh <- readBinMemN persistentBytecodeHeaderSize path - case mb_bh of - Just bh -> readPersistentBytecodeHeader file_kind path bh - Nothing -> headerMismatch file_kind path "file too short" + r <- tryIO $ readBinMemN persistentBytecodeHeaderSize path + case r of + Right (Just bh) -> rightToMaybe <$> getPersistentBytecodeHeader file_kind bh + _ -> pure Nothing persistentBytecodeFileDescription :: PersistentBytecodeFile -> String persistentBytecodeFileDescription ModuleByteCodeFile = "bytecode file" ===================================== compiler/GHC/Driver/Main/Passes.hs ===================================== @@ -743,7 +743,7 @@ checkByteCodeInMemory hsc_env mod_sum mb_old_linkable = Just _ -> do disk_hash <- ByteCode.readBinByteCodeHash (ml_bytecode_file (ms_location mod_sum)) - pure (disk_hash == linkableHash old_linkable) + pure (disk_hash == Just (linkableHash old_linkable)) else pure True if ok then return (UpToDateItem old_linkable) @@ -760,12 +760,17 @@ checkByteCodeFromObject hsc_env mod_sum = do case (,) <$> obj_date <*> if_date of Just (obj_date, if_date) | obj_date >= if_date -> do - -- Don't force this if we reuse the linkable already loaded into memory, but we have to check - -- that the one we have on disk would be suitable as well. - linkable <- unsafeInterleaveIO $ do - bco <- ByteCode.readBinByteCode hsc_env obj_fn - return $ mkOnlyModuleByteCodeLinkable bco - return $ UpToDateItem linkable + -- A file without a valid header is treated as missing + mb_disk_hash <- ByteCode.readBinByteCodeHash obj_fn + case mb_disk_hash of + Nothing -> return $ outOfDateItemBecause MissingBytecode Nothing + Just _ -> do + -- Don't force this if we reuse the linkable already loaded into memory, but we have to check + -- that the one we have on disk would be suitable as well. + linkable <- unsafeInterleaveIO $ do + bco <- ByteCode.readBinByteCode hsc_env obj_fn + return $ mkOnlyModuleByteCodeLinkable bco + return $ UpToDateItem linkable _ -> return $ outOfDateItemBecause MissingBytecode Nothing -- | Attempt to load bytecode from whole core bindings in the interface if they exist. ===================================== compiler/GHC/Driver/Pipeline.hs ===================================== @@ -550,11 +550,11 @@ checkBytecodeLibraryLinkingNeeded _logger dflags unit_env linkables _pkg_deps = Nothing -> return $ NeedsRecompile MustCompile Just _ -> do -- See Note [Hash of bytecode libs] in GHC.ByteCode.Serialize - e_recorded <- tryIO (readBytecodeLibInputsHash exe_file) + recorded <- readBytecodeLibInputsHash exe_file let current = bytecodeLibInputsHash (map linkableHash linkables) - case e_recorded of - Right recorded | recorded == current -> return UpToDate - _ -> return $ needsRecompileBecause ObjectsChanged + if recorded == Just current + then return UpToDate + else return $ needsRecompileBecause ObjectsChanged checkNativeLibraryLinkingNeeded :: Bool -> Logger -> DynFlags -> UnitEnv -> [Linkable] -> [UnitId] -> IO RecompileRequired checkNativeLibraryLinkingNeeded staticLink _ dflags unit_env linkables pkg_deps = do ===================================== testsuite/tests/driver/bytecode-object/Makefile ===================================== @@ -170,3 +170,14 @@ bytecode_object26: # The module should not be recompiled. bytecode_object27: @echo ":reload" | "$(TEST_HC)" $(TEST_HC_OPTS_INTERACTIVE) -v1 -fno-hide-source-paths -fbyte-code -fwrite-byte-code -fwrite-interface BytecodeTest.hs + +# Test corrupting the header of the .gbc file and reloading in GHCi +# The module should be recompiled. +bytecode_object28: + @cat bytecode_object28.script | "$(TEST_HC)" $(TEST_HC_OPTS_INTERACTIVE) -v1 -fno-hide-source-paths -fbyte-code -fwrite-byte-code -fwrite-interface BytecodeTest.hs + +# Test that a bytecode library with a corrupt header is linked again +bytecode_object29: + "$(TEST_HC)" $(TEST_HC_OPTS) -bytecodelib -o linked.bytecode BytecodeTest.hs -fwrite-byte-code -fbyte-code -fwrite-interface + @printf 'bad!' | dd of=linked.bytecode bs=1 count=4 conv=notrunc 2>/dev/null + "$(TEST_HC)" $(TEST_HC_OPTS) -bytecodelib -o linked.bytecode BytecodeTest.hs -fwrite-byte-code -fbyte-code -fwrite-interface ===================================== testsuite/tests/driver/bytecode-object/all.T ===================================== @@ -28,3 +28,5 @@ test('bytecode_object24', bytecode_opts + [copy_files], makefile_test, ['bytecod test('bytecode_object25', [bytecode_opts, req_interp, extra_files(['BytecodeForeign.hs', 'BytecodeForeign.c'])], makefile_test, ['bytecode_object25']) test('bytecode_object26', [bytecode_opts], makefile_test, ['bytecode_object26']) test('bytecode_object27', [bytecode_opts, req_interp, filter_stdout_lines(r'.*Compiling.*') ], makefile_test, ['bytecode_object27']) +test('bytecode_object28', [bytecode_opts, req_interp, filter_stdout_lines(r'.*Compiling.*') ], makefile_test, ['bytecode_object28']) +test('bytecode_object29', bytecode_opts, makefile_test, ['bytecode_object29']) ===================================== testsuite/tests/driver/bytecode-object/bytecode_object28.script ===================================== @@ -0,0 +1,3 @@ +:!printf 'bad!' | dd of=BytecodeTest.gbc bs=1 count=4 conv=notrunc 2>/dev/null +:reload +:quit ===================================== testsuite/tests/driver/bytecode-object/bytecode_object28.stdout ===================================== @@ -0,0 +1,2 @@ +[1 of 1] Compiling BytecodeTest ( BytecodeTest.hs, BytecodeTest.gbc )[main] +ghci> ghci> [1 of 1] Compiling BytecodeTest ( BytecodeTest.hs, BytecodeTest.gbc )[main] [Missing bytecode] ===================================== testsuite/tests/driver/bytecode-object/bytecode_object29.stdout ===================================== @@ -0,0 +1,3 @@ +[1 of 2] Compiling BytecodeTest ( BytecodeTest.hs, BytecodeTest.gbc ) +[2 of 2] Linking linked.bytecode +[2 of 2] Linking linked.bytecode [Objects changed] View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/f3f5e98cc9cc5377bec357ec65bd442e... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/f3f5e98cc9cc5377bec357ec65bd442e... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help