Skip to content

Commit

Permalink
Merge pull request #1194 from GaloisInc/at-x86-error-handling
Browse files Browse the repository at this point in the history
Handle x86 errors more gracefully
  • Loading branch information
mergify[bot] authored Apr 19, 2021
2 parents 0814e0b + 45ca064 commit 309ae98
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/SAWScript/Crucible/LLVM/X86.hs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ llvm_verify_x86 (Some (llvmModule :: LLVMModule x)) path nm globsyms checkSat se
liftIO $ sawRegisterSymFunInterp sawst (Macaw.fnShaMaj sfs) $ cryptolUninterpreted cenv "Maj"

let preserved = Set.fromList . catMaybes $ stringToReg . Text.toLower . Text.pack <$> rwPreservedRegs rw
(C.SomeCFG cfg, elf, relf, addr, cfgs) <- liftIO $ buildCFG opts halloc preserved path nm
(C.SomeCFG cfg, elf, relf, addr, cfgs) <- io $ buildCFG opts halloc preserved path nm
addrInt <- if Macaw.segmentBase (Macaw.segoffSegment addr) == 0
then pure . toInteger $ Macaw.segmentOffset (Macaw.segoffSegment addr) + Macaw.segoffOffset addr
else fail $ mconcat ["Address of \"", nm, "\" is not an absolute address"]
Expand Down Expand Up @@ -352,9 +352,9 @@ llvm_verify_x86 (Some (llvmModule :: LLVMModule x)) path nm globsyms checkSat se

let ?lc = modTrans llvmModule ^. C.LLVM.transContext . C.LLVM.llvmTypeCtx

emptyState <- liftIO $ initialState sym opts sc cc elf relf methodSpec globsyms maxAddr
emptyState <- io $ initialState sym opts sc cc elf relf methodSpec globsyms maxAddr
balign <- integerToAlignment $ rwStackBaseAlign rw
(env, preState) <- liftIO . runX86Sim emptyState $ setupMemory globsyms balign
(env, preState) <- io . runX86Sim emptyState $ setupMemory globsyms balign

let
funcLookup = Macaw.LookupFunctionHandle $ \st _mem regs -> do
Expand Down
15 changes: 13 additions & 2 deletions src/SAWScript/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import Control.Applicative (Applicative)
#endif
import Control.Lens
import Control.Monad.Fail (MonadFail(..))
import Control.Monad.Catch (MonadThrow(..), MonadMask(..), MonadCatch(..))
import Control.Monad.Catch (MonadThrow(..), MonadMask(..),
MonadCatch(..), catches, Handler(..))
import Control.Monad.Except (ExceptT(..), runExceptT, MonadError(..))
import Control.Monad.Reader (MonadReader)
import qualified Control.Exception as X
Expand Down Expand Up @@ -75,6 +76,7 @@ import SAWScript.Options (Options(printOutFn),printOutLn,Verbosity)
import SAWScript.Proof
import SAWScript.Prover.SolverStats
import SAWScript.Crucible.LLVM.Skeleton
import SAWScript.X86 (X86Unsupported(..), X86Error(..))

import Verifier.SAW.Name (toShortName)
import Verifier.SAW.CryptolEnv as CEnv
Expand Down Expand Up @@ -423,7 +425,16 @@ runTopLevel :: TopLevel a -> TopLevelRO -> TopLevelRW -> IO (a, TopLevelRW)
runTopLevel (TopLevel m) ro rw = runStateT (runReaderT m ro) rw

io :: IO a -> TopLevel a
io = liftIO
io f = liftIO f
`catches`
[ Handler (\(ex :: X86Unsupported) -> handleX86Unsupported ex)
, Handler (\(ex :: X86Error) -> handleX86Error ex)
]
where
handleX86Unsupported (X86Unsupported s) =
throwTopLevel $ "Unsupported x86 feature: " ++ s
handleX86Error (X86Error s) =
throwTopLevel $ "Error in x86 code: " ++ s

throwTopLevel :: String -> TopLevel a
throwTopLevel msg = do
Expand Down
7 changes: 5 additions & 2 deletions src/SAWScript/X86.hs
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,11 @@ getElf path =
Right (Elf.SomeElf hdr)
| Elf.ELFCLASS64 <- Elf.headerClass (Elf.header hdr) -> pure hdr
| otherwise -> unsupported "32-bit ELF format"
Left _ -> malformed "Invalid ELF header"

Left (off, msg) -> malformed $ mconcat [ "Invalid ELF header at offset "
, show off
, ": "
, msg
]


-- | Extract a Macaw "memory" from an ELF file and resolve symbols.
Expand Down

0 comments on commit 309ae98

Please sign in to comment.