Zubin pushed to branch wip/linkable-hashes at Glasgow Haskell Compiler / GHC

Commits:

8 changed files:

Changes:

  • compiler/GHC/ByteCode/Serialize.hs
    ... ... @@ -29,6 +29,7 @@ import GHC.Prelude
    29 29
     import GHC.ByteCode.Binary
    
    30 30
     import GHC.ByteCode.Recomp.Binary (computeFingerprint)
    
    31 31
     import GHC.ByteCode.Types
    
    32
    +import GHC.Data.Maybe (rightToMaybe)
    
    32 33
     import GHC.Driver.DynFlags
    
    33 34
     import GHC.Driver.Env
    
    34 35
     import GHC.Iface.Binary
    
    ... ... @@ -36,6 +37,7 @@ import GHC.Iface.Recomp.Binary (putNameLiterally)
    36 37
     import GHC.Linker.Types
    
    37 38
     import GHC.Unit.Types
    
    38 39
     import GHC.Utils.Binary
    
    40
    +import GHC.Utils.Exception (tryIO)
    
    39 41
     import GHC.Utils.Fingerprint (Fingerprint(..), fingerprintFingerprints)
    
    40 42
     import GHC.Utils.Logger
    
    41 43
     import GHC.Utils.Panic
    
    ... ... @@ -148,8 +150,9 @@ readBytecodeLib hsc_env path = do
    148 150
       res <- getWithUserData (hsc_NC hsc_env) bh
    
    149 151
       pure res
    
    150 152
     
    
    153
    +-- | 'Nothing' if the file can't be read or its header is not valid.
    
    151 154
     -- See Note [Hash of bytecode libs]
    
    152
    -readBytecodeLibInputsHash :: FilePath -> IO Fingerprint
    
    155
    +readBytecodeLibInputsHash :: FilePath -> IO (Maybe Fingerprint)
    
    153 156
     readBytecodeLibInputsHash = readPersistentBytecodeHash BytecodeLibraryFile
    
    154 157
     
    
    155 158
     -- | Convert an 'OnDiskModuleByteCode' to an 'ModuleByteCode'.
    
    ... ... @@ -237,7 +240,8 @@ readBinByteCode hsc_env f = do
    237 240
       decodeOnDiskModuleByteCode hsc_env odbco
    
    238 241
     
    
    239 242
     -- | Read the hash of a bytecode file, without reading the rest of the file.
    
    240
    -readBinByteCodeHash :: FilePath -> IO Fingerprint
    
    243
    +-- 'Nothing' if the file can't be read or its header is not valid.
    
    244
    +readBinByteCodeHash :: FilePath -> IO (Maybe Fingerprint)
    
    241 245
     readBinByteCodeHash = readPersistentBytecodeHash ModuleByteCodeFile
    
    242 246
     
    
    243 247
     readOnDiskModuleByteCode :: HscEnv -> FilePath -> IO OnDiskModuleByteCode
    
    ... ... @@ -318,44 +322,46 @@ writePersistentBytecodeHeader file_kind (Fingerprint w1 w2) bh = do
    318 322
       put_ bh (FixedLengthEncoding w1)
    
    319 323
       put_ bh (FixedLengthEncoding w2)
    
    320 324
     
    
    325
    +-- | Read only the header of a persistent bytecode file.
    
    326
    +getPersistentBytecodeHeader :: PersistentBytecodeFile -> ReadBinHandle -> IO (Either String Fingerprint)
    
    327
    +getPersistentBytecodeHeader file_kind bh = do
    
    328
    +  magic <- get bh
    
    329
    +  if unFixedLength magic /= unFixedLength expected_magic
    
    330
    +    then mismatch "magic" (show $ unFixedLength expected_magic) (show $ unFixedLength magic)
    
    331
    +    else do
    
    332
    +      version <- get bh
    
    333
    +      if version /= persistentBytecodeVersion
    
    334
    +        then mismatch "version"
    
    335
    +               (showPersistentBytecodeVersion persistentBytecodeVersion)
    
    336
    +               (showPersistentBytecodeVersion version)
    
    337
    +        else do
    
    338
    +          w1 <- get bh
    
    339
    +          w2 <- get bh
    
    340
    +          pure $ Right (Fingerprint (unFixedLength w1) (unFixedLength w2))
    
    341
    +  where
    
    342
    +    expected_magic = persistentBytecodeMagic file_kind
    
    343
    +    mismatch what expected actual =
    
    344
    +      pure $ Left $ what ++ " (expected " ++ expected ++ ", got " ++ actual ++ ")"
    
    345
    +
    
    321 346
     -- | Validate the header of a persistent bytecode file, and return its hash.
    
    322 347
     readPersistentBytecodeHeader :: PersistentBytecodeFile -> FilePath -> ReadBinHandle -> IO Fingerprint
    
    323 348
     readPersistentBytecodeHeader file_kind path bh = do
    
    324
    -  let mismatch what expected actual =
    
    325
    -        headerMismatch file_kind path $
    
    326
    -          what ++ " (expected " ++ expected ++ ", got " ++ actual ++ ")"
    
    327
    -
    
    328
    -  magic <- get bh
    
    329
    -  let expected_magic = persistentBytecodeMagic file_kind
    
    330
    -  if unFixedLength magic == unFixedLength expected_magic
    
    331
    -    then pure ()
    
    332
    -    else mismatch "magic" (show $ unFixedLength expected_magic) (show $ unFixedLength magic)
    
    333
    -
    
    334
    -  version <- get bh
    
    335
    -  if version == persistentBytecodeVersion
    
    336
    -    then pure ()
    
    337
    -    else mismatch "version"
    
    338
    -           (showPersistentBytecodeVersion persistentBytecodeVersion)
    
    339
    -           (showPersistentBytecodeVersion version)
    
    340
    -
    
    341
    -  w1 <- get bh
    
    342
    -  w2 <- get bh
    
    343
    -  pure (Fingerprint (unFixedLength w1) (unFixedLength w2))
    
    344
    -
    
    345
    -headerMismatch :: PersistentBytecodeFile -> FilePath -> String -> IO a
    
    346
    -headerMismatch file_kind path what =
    
    347
    -  throwGhcExceptionIO $ ProgramError $
    
    348
    -    persistentBytecodeFileDescription file_kind ++ " header mismatch in " ++ path ++
    
    349
    -    ": " ++ what
    
    349
    +  r <- getPersistentBytecodeHeader file_kind bh
    
    350
    +  case r of
    
    351
    +    Right hash -> pure hash
    
    352
    +    Left what -> throwGhcExceptionIO $ ProgramError $
    
    353
    +      persistentBytecodeFileDescription file_kind ++ " header mismatch in " ++ path ++
    
    354
    +      ": " ++ what
    
    350 355
     
    
    351 356
     -- | Read the hash from the header of a persistent bytecode file, without
    
    352
    --- reading the rest of the file.
    
    353
    -readPersistentBytecodeHash :: PersistentBytecodeFile -> FilePath -> IO Fingerprint
    
    357
    +-- reading the rest of the file. 'Nothing' if the file can't be read or its
    
    358
    +-- header is not valid.
    
    359
    +readPersistentBytecodeHash :: PersistentBytecodeFile -> FilePath -> IO (Maybe Fingerprint)
    
    354 360
     readPersistentBytecodeHash file_kind path = do
    
    355
    -  mb_bh <- readBinMemN persistentBytecodeHeaderSize path
    
    356
    -  case mb_bh of
    
    357
    -    Just bh -> readPersistentBytecodeHeader file_kind path bh
    
    358
    -    Nothing -> headerMismatch file_kind path "file too short"
    
    361
    +  r <- tryIO $ readBinMemN persistentBytecodeHeaderSize path
    
    362
    +  case r of
    
    363
    +    Right (Just bh) -> rightToMaybe <$> getPersistentBytecodeHeader file_kind bh
    
    364
    +    _ -> pure Nothing
    
    359 365
     
    
    360 366
     persistentBytecodeFileDescription :: PersistentBytecodeFile -> String
    
    361 367
     persistentBytecodeFileDescription ModuleByteCodeFile = "bytecode file"
    

  • compiler/GHC/Driver/Main/Passes.hs
    ... ... @@ -743,7 +743,7 @@ checkByteCodeInMemory hsc_env mod_sum mb_old_linkable =
    743 743
                          Just _ -> do
    
    744 744
                            disk_hash <- ByteCode.readBinByteCodeHash
    
    745 745
                                           (ml_bytecode_file (ms_location mod_sum))
    
    746
    -                       pure (disk_hash == linkableHash old_linkable)
    
    746
    +                       pure (disk_hash == Just (linkableHash old_linkable))
    
    747 747
                   else pure True
    
    748 748
           if ok
    
    749 749
             then return (UpToDateItem old_linkable)
    
    ... ... @@ -760,12 +760,17 @@ checkByteCodeFromObject hsc_env mod_sum = do
    760 760
       case (,) <$> obj_date <*> if_date of
    
    761 761
         Just (obj_date, if_date)
    
    762 762
           | obj_date >= if_date -> do
    
    763
    -          -- Don't force this if we reuse the linkable already loaded into memory, but we have to check
    
    764
    -          -- that the one we have on disk would be suitable as well.
    
    765
    -          linkable <- unsafeInterleaveIO $ do
    
    766
    -            bco <- ByteCode.readBinByteCode hsc_env obj_fn
    
    767
    -            return $ mkOnlyModuleByteCodeLinkable bco
    
    768
    -          return $ UpToDateItem linkable
    
    763
    +          -- A file without a valid header is treated as missing
    
    764
    +          mb_disk_hash <- ByteCode.readBinByteCodeHash obj_fn
    
    765
    +          case mb_disk_hash of
    
    766
    +            Nothing -> return $ outOfDateItemBecause MissingBytecode Nothing
    
    767
    +            Just _ -> do
    
    768
    +              -- Don't force this if we reuse the linkable already loaded into memory, but we have to check
    
    769
    +              -- that the one we have on disk would be suitable as well.
    
    770
    +              linkable <- unsafeInterleaveIO $ do
    
    771
    +                bco <- ByteCode.readBinByteCode hsc_env obj_fn
    
    772
    +                return $ mkOnlyModuleByteCodeLinkable bco
    
    773
    +              return $ UpToDateItem linkable
    
    769 774
         _ -> return $ outOfDateItemBecause MissingBytecode Nothing
    
    770 775
     
    
    771 776
     -- | 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 =
    550 550
         Nothing  -> return $ NeedsRecompile MustCompile
    
    551 551
         Just _ -> do
    
    552 552
             -- See Note [Hash of bytecode libs] in GHC.ByteCode.Serialize
    
    553
    -        e_recorded <- tryIO (readBytecodeLibInputsHash exe_file)
    
    553
    +        recorded <- readBytecodeLibInputsHash exe_file
    
    554 554
             let current = bytecodeLibInputsHash (map linkableHash linkables)
    
    555
    -        case e_recorded of
    
    556
    -          Right recorded | recorded == current -> return UpToDate
    
    557
    -          _ -> return $ needsRecompileBecause ObjectsChanged
    
    555
    +        if recorded == Just current
    
    556
    +          then return UpToDate
    
    557
    +          else return $ needsRecompileBecause ObjectsChanged
    
    558 558
     
    
    559 559
     checkNativeLibraryLinkingNeeded :: Bool -> Logger -> DynFlags -> UnitEnv -> [Linkable] -> [UnitId] -> IO RecompileRequired
    
    560 560
     checkNativeLibraryLinkingNeeded staticLink _ dflags unit_env linkables pkg_deps = do
    

  • testsuite/tests/driver/bytecode-object/Makefile
    ... ... @@ -170,3 +170,14 @@ bytecode_object26:
    170 170
     # The module should not be recompiled.
    
    171 171
     bytecode_object27:
    
    172 172
     	@echo ":reload" | "$(TEST_HC)" $(TEST_HC_OPTS_INTERACTIVE) -v1 -fno-hide-source-paths  -fbyte-code -fwrite-byte-code -fwrite-interface BytecodeTest.hs
    
    173
    +
    
    174
    +# Test corrupting the header of the .gbc file and reloading in GHCi
    
    175
    +# The module should be recompiled.
    
    176
    +bytecode_object28:
    
    177
    +	@cat bytecode_object28.script | "$(TEST_HC)" $(TEST_HC_OPTS_INTERACTIVE) -v1 -fno-hide-source-paths  -fbyte-code -fwrite-byte-code -fwrite-interface BytecodeTest.hs
    
    178
    +
    
    179
    +# Test that a bytecode library with a corrupt header is linked again
    
    180
    +bytecode_object29:
    
    181
    +	"$(TEST_HC)" $(TEST_HC_OPTS) -bytecodelib -o linked.bytecode BytecodeTest.hs -fwrite-byte-code -fbyte-code -fwrite-interface
    
    182
    +	@printf 'bad!' | dd of=linked.bytecode bs=1 count=4 conv=notrunc 2>/dev/null
    
    183
    +	"$(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
    28 28
     test('bytecode_object25', [bytecode_opts, req_interp, extra_files(['BytecodeForeign.hs', 'BytecodeForeign.c'])], makefile_test, ['bytecode_object25'])
    
    29 29
     test('bytecode_object26', [bytecode_opts], makefile_test, ['bytecode_object26'])
    
    30 30
     test('bytecode_object27', [bytecode_opts, req_interp, filter_stdout_lines(r'.*Compiling.*') ], makefile_test, ['bytecode_object27'])
    
    31
    +test('bytecode_object28', [bytecode_opts, req_interp, filter_stdout_lines(r'.*Compiling.*') ], makefile_test, ['bytecode_object28'])
    
    32
    +test('bytecode_object29', bytecode_opts, makefile_test, ['bytecode_object29'])

  • testsuite/tests/driver/bytecode-object/bytecode_object28.script
    1
    +:!printf 'bad!' | dd of=BytecodeTest.gbc bs=1 count=4 conv=notrunc 2>/dev/null
    
    2
    +:reload
    
    3
    +:quit

  • testsuite/tests/driver/bytecode-object/bytecode_object28.stdout
    1
    +[1 of 1] Compiling BytecodeTest     ( BytecodeTest.hs, BytecodeTest.gbc )[main]
    
    2
    +ghci> ghci> [1 of 1] Compiling BytecodeTest     ( BytecodeTest.hs, BytecodeTest.gbc )[main] [Missing bytecode]

  • testsuite/tests/driver/bytecode-object/bytecode_object29.stdout
    1
    +[1 of 2] Compiling BytecodeTest     ( BytecodeTest.hs, BytecodeTest.gbc )
    
    2
    +[2 of 2] Linking linked.bytecode
    
    3
    +[2 of 2] Linking linked.bytecode [Objects changed]