Skip to content

Commit a1f9c8b

Browse files
committed
Pipe condition metadata through the X86 verification modes.
1 parent 75ebba3 commit a1f9c8b

File tree

2 files changed

+47
-20
lines changed

2 files changed

+47
-20
lines changed

src/SAWScript/Crucible/LLVM/X86.hs

+18-11
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import Control.Monad.Catch (MonadThrow)
4141

4242
import qualified Data.BitVector.Sized as BV
4343
import Data.Foldable (foldlM)
44+
import Data.IORef
4445
import qualified Data.List.NonEmpty as NE
4546
import qualified Data.Vector as Vector
4647
import qualified Data.Text as Text
@@ -79,6 +80,7 @@ import SAWScript.Options
7980
import SAWScript.X86 hiding (Options)
8081
import SAWScript.X86Spec
8182
import SAWScript.Crucible.Common
83+
import SAWScript.Crucible.Common.Override (MetadataMap)
8284

8385
import qualified SAWScript.Crucible.Common as Common
8486
import qualified SAWScript.Crucible.Common.MethodSpec as MS
@@ -350,6 +352,7 @@ llvm_verify_x86_common (Some (llvmModule :: LLVMModule x)) path nm globsyms chec
350352
basic_ss <- getBasicSS
351353
rw <- getTopLevelRW
352354
sym <- liftIO $ newSAWCoreExprBuilder sc
355+
mdMap <- liftIO $ newIORef mempty
353356
SomeOnlineBackend bak <- liftIO $
354357
newSAWCoreBackendWithTimeout pathSatSolver sym $ rwCrucibleTimeout rw
355358
cacheTermsSetting <- liftIO $ W4.getOptionSetting W4.B.cacheTerms $ W4.getConfiguration sym
@@ -487,7 +490,8 @@ llvm_verify_x86_common (Some (llvmModule :: LLVMModule x)) path nm globsyms chec
487490
}
488491
liftIO $ printOutLn opts Info
489492
"Examining specification to determine postconditions"
490-
liftIO . void . runX86Sim finalState $ assertPost globals' env (preState ^. x86Mem) (preState ^. x86Regs)
493+
liftIO . void . runX86Sim finalState $
494+
assertPost globals' env (preState ^. x86Mem) (preState ^. x86Regs) mdMap
491495
pure $ C.regValue r
492496

493497
liftIO $ printOutLn opts Info "Simulating function"
@@ -521,7 +525,7 @@ llvm_verify_x86_common (Some (llvmModule :: LLVMModule x)) path nm globsyms chec
521525
ar
522526
C.TimeoutResult{} -> fail "Execution timed out"
523527

524-
(stats,thms) <- checkGoals bak opts nm sc tactic
528+
(stats,thms) <- checkGoals bak opts nm sc tactic mdMap
525529

526530
end <- io getCurrentTime
527531
let diff = diffUTCTime end start
@@ -1020,8 +1024,9 @@ assertPost ::
10201024
Map MS.AllocIndex Ptr ->
10211025
Mem {- ^ The state of memory before simulation -} ->
10221026
Regs {- ^ The state of the registers before simulation -} ->
1027+
IORef MetadataMap {- ^ metadata map -} ->
10231028
X86Sim ()
1024-
assertPost globals env premem preregs = do
1029+
assertPost globals env premem preregs mdMap = do
10251030
SomeOnlineBackend bak <- use x86Backend
10261031
sym <- use x86Sym
10271032
opts <- use x86Options
@@ -1109,9 +1114,10 @@ assertPost globals env premem preregs = do
11091114
Right (_, st) -> pure st
11101115
liftIO . forM_ (view O.osAssumes st) $ \(_md, asum) ->
11111116
C.addAssumption bak $ C.GenericAssumption (st ^. O.osLocation) "precondition" asum
1112-
liftIO . forM_ (view LO.osAsserts st) $ \(_md, W4.LabeledPred p r) ->
1113-
-- TODO, use assertion metadata
1114-
C.addAssertion bak $ C.LabeledPred p r
1117+
liftIO . forM_ (view LO.osAsserts st) $ \(md, W4.LabeledPred p r) ->
1118+
do (ann,p') <- W4.annotateTerm sym p
1119+
modifyIORef mdMap (Map.insert ann md)
1120+
C.addAssertion bak (W4.LabeledPred p' r)
11151121

11161122
-- | Assert that a points-to postcondition holds.
11171123
assertPointsTo ::
@@ -1160,9 +1166,10 @@ checkGoals ::
11601166
String ->
11611167
SharedContext ->
11621168
ProofScript () ->
1169+
IORef MetadataMap {- ^ metadata map -} ->
11631170
TopLevel (SolverStats, Set TheoremNonce)
1164-
checkGoals bak opts nm sc tactic = do
1165-
gs <- liftIO $ getGoals (SomeBackend bak)
1171+
checkGoals bak opts nm sc tactic mdMap = do
1172+
gs <- liftIO $ getGoals (SomeBackend bak) mdMap
11661173
liftIO . printOutLn opts Info $ mconcat
11671174
[ "Simulation finished, running solver on "
11681175
, show $ length gs
@@ -1172,12 +1179,12 @@ checkGoals bak opts nm sc tactic = do
11721179
term <- liftIO $ gGoal sc g
11731180
let proofgoal = ProofGoal
11741181
{ goalNum = n
1175-
, goalType = "vc"
1182+
, goalType = MS.conditionType (gMd g)
11761183
, goalName = nm
1177-
, goalLoc = show $ gLoc g
1184+
, goalLoc = show $ MS.conditionLoc (gMd g)
11781185
, goalDesc = show $ gMessage g
11791186
, goalProp = term
1180-
, goalTags = mempty -- TODO! propagate tags
1187+
, goalTags = MS.conditionTags (gMd g)
11811188
}
11821189
res <- runProofScript tactic proofgoal (Just (gLoc g)) $ Text.unwords
11831190
["X86 verification condition", Text.pack (show n), Text.pack (show (gMessage g))]

src/SAWScript/X86.hs

+29-9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module SAWScript.X86
1212
, Fun(..)
1313
, Goal(..)
1414
, gGoal
15+
, gLoc
1516
, getGoals
1617
, X86Error(..)
1718
, X86Unsupported(..)
@@ -35,11 +36,12 @@ import qualified Data.BitVector.Sized as BV
3536
import Data.ByteString (ByteString)
3637
import qualified Data.ByteString as BS
3738
import qualified Data.ByteString.Char8 as BSC
39+
import Data.IORef
3840
import qualified Data.Map as Map
3941
import qualified Data.Text as Text
4042
import Data.Text.Encoding(decodeUtf8)
4143
import System.IO(hFlush,stdout)
42-
import Data.Maybe(mapMaybe)
44+
import Data.Maybe(mapMaybe, fromMaybe)
4345

4446
-- import Text.PrettyPrint.ANSI.Leijen(pretty)
4547

@@ -135,6 +137,8 @@ import Verifier.SAW.Cryptol.Prelude(scLoadPreludeModule,scLoadCryptolModule)
135137
-- SAWScript
136138
import SAWScript.X86Spec hiding (Prop)
137139
import SAWScript.Proof(boolToProp, Prop)
140+
import SAWScript.Crucible.Common.MethodSpec (ConditionMetadata(..))
141+
import SAWScript.Crucible.Common.Override (MetadataMap)
138142
import SAWScript.Crucible.Common
139143
( newSAWCoreBackend, newSAWCoreExprBuilder
140144
, sawCoreState, SomeOnlineBackend(..)
@@ -405,6 +409,9 @@ translate opts elf fun =
405409
do let name = funName fun
406410
sayLn ("Translating function: " ++ BSC.unpack name)
407411

412+
-- TODO? do we need to pass in the mdMap into more places in this mode?
413+
mdMap <- newIORef mempty
414+
408415
let ?memOpts = Crucible.defaultMemOptions
409416
let ?recordLLVMAnnotation = \_ _ _ -> return ()
410417

@@ -426,7 +433,7 @@ translate opts elf fun =
426433

427434
addr <- doSim opts elf sfs name globs st checkPost
428435

429-
gs <- getGoals bak
436+
gs <- getGoals bak mdMap
430437
sc <- saw_ctx <$> sawCoreState sym
431438
return (sc, addr, gs)
432439

@@ -549,10 +556,13 @@ makeCFG opts elf name addr =
549556
data Goal = Goal
550557
{ gAssumes :: [ Term ] -- ^ Assuming these
551558
, gShows :: Term -- ^ We need to show this
552-
, gLoc :: ProgramLoc -- ^ The goal came from here
559+
, gMd :: ConditionMetadata -- ^ Metadata about the goal
553560
, gMessage :: SimErrorReason -- ^ We should say this if the proof fails
554561
}
555562

563+
gLoc :: Goal -> ProgramLoc
564+
gLoc = conditionLoc . gMd
565+
556566
-- | The proposition that needs proving (i.e., assumptions imply conclusion)
557567
gGoal :: SharedContext -> Goal -> IO Prop
558568
gGoal sc g0 = boolToProp sc [] =<< go (gAssumes g)
@@ -575,21 +585,31 @@ gGoal sc g0 = boolToProp sc [] =<< go (gAssumes g)
575585
[] -> return (gShows g)
576586
a : as -> scImplies sc a =<< go as
577587

578-
getGoals :: SomeBackend Sym -> IO [Goal]
579-
getGoals (SomeBackend bak) =
580-
do obls <- maybe [] goalsToList <$> getProofObligations bak
588+
getGoals :: SomeBackend Sym -> IORef MetadataMap -> IO [Goal]
589+
getGoals (SomeBackend bak) mdMap =
590+
do finalMdMap <- readIORef mdMap
591+
obls <- maybe [] goalsToList <$> getProofObligations bak
581592
st <- sawCoreState sym
582-
mapM (toGoal st) obls
593+
mapM (toGoal st finalMdMap) obls
583594
where
584595
sym = backendGetSym bak
585596

586-
toGoal st (ProofGoal asmps g) =
597+
toGoal st finalMdMap (ProofGoal asmps g) =
587598
do a1 <- toSC sym st =<< assumptionsPred sym asmps
588599
p <- toSC sym st (g ^. labeledPred)
589600
let SimError loc msg = g^.labeledPredMsg
601+
let defaultMd = ConditionMetadata
602+
{ conditionLoc = loc
603+
, conditionTags = mempty
604+
, conditionType = "safety assertion"
605+
, conditionContext = ""
606+
}
607+
let md = fromMaybe defaultMd $
608+
do ann <- W4.getAnnotation sym (g^.labeledPred)
609+
Map.lookup ann finalMdMap
590610
return Goal { gAssumes = [a1]
591611
, gShows = p
592-
, gLoc = loc
612+
, gMd = md
593613
, gMessage = msg
594614
}
595615

0 commit comments

Comments
 (0)