Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle x86 errors more gracefully #1194

Merged
merged 3 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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