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

Separate backend #1555

Merged
merged 4 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ packages:
deps/crucible/crucible-concurrency
deps/crucible/crucible-jvm
deps/crucible/crucible-llvm
deps/crucible/crucible-saw
deps/crucible/crucible-symio
deps/crucible/crucible-syntax
deps/crucible/crux
Expand Down
4 changes: 2 additions & 2 deletions crux-mir-comp/src/Mir/Compositional.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import Mir.Compositional.Clobber (clobberGlobalsOverride)


compositionalOverrides ::
forall sym p t st fs args ret blocks rtp a r .
forall sym bak p t st fs args ret blocks rtp a r .
(IsSymInterface sym, sym ~ W4.ExprBuilder t st fs) =>
Maybe (SomeOnlineSolver sym) ->
Maybe (SomeOnlineSolver sym bak) ->
CollectionState ->
Text ->
CFG MIR blocks args ret ->
Expand Down
77 changes: 43 additions & 34 deletions crux-mir-comp/src/Mir/Compositional/Builder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,10 @@ builderNew :: forall sym p t st fs rtp.
DefId ->
OverrideSim (p sym) sym MIR rtp
EmptyCtx MethodSpecBuilderType (MethodSpecBuilder sym t)
builderNew cs defId = do
sym <- getSymInterface
snapFrame <- liftIO $ pushAssumptionFrame sym
builderNew cs defId =
ovrWithBackend $ \bak -> do
let sym = backendGetSym bak
snapFrame <- liftIO $ pushAssumptionFrame bak

let tyArg = cs ^? collection . M.intrinsics . ix defId .
M.intrInst . M.inSubsts . _Wrapped . ix 0
Expand Down Expand Up @@ -227,8 +228,10 @@ addArg :: forall sym p t st fs rtp args ret tp.
(IsSymInterface sym, sym ~ W4.ExprBuilder t st fs) =>
TypeRepr tp -> MirReferenceMux sym tp -> MethodSpecBuilder sym t ->
OverrideSim (p sym) sym MIR rtp args ret (MethodSpecBuilder sym t)
addArg tpr argRef msb = execBuilderT msb $ do
sym <- lift $ getSymInterface
addArg tpr argRef msb =
ovrWithBackend $ \bak ->
execBuilderT msb $ do
let sym = backendGetSym bak
loc <- liftIO $ W4.getCurrentProgramLoc sym

let idx = Map.size $ msb ^. msbSpec . MS.csArgBindings
Expand All @@ -238,7 +241,7 @@ addArg tpr argRef msb = execBuilderT msb $ do
let shp = tyToShapeEq col ty tpr

rv <- lift $ readMirRefSim tpr argRef
sv <- regToSetup sym Pre (\_tpr expr -> SAW.mkTypedTerm sc =<< eval expr) shp rv
sv <- regToSetup bak Pre (\_tpr expr -> SAW.mkTypedTerm sc =<< eval expr) shp rv

void $ forNewRefs Pre $ \fr -> do
let len = fr ^. frAllocSpec . maLen
Expand All @@ -248,15 +251,15 @@ addArg tpr argRef msb = execBuilderT msb $ do
ref' <- lift $ mirRef_offsetSim (fr ^. frType) (fr ^. frRef) iSym
rv <- lift $ readMirRefSim (fr ^. frType) ref'
let shp = tyToShapeEq col (fr ^. frMirType) (fr ^. frType)
sv <- regToSetup sym Pre (\_tpr expr -> SAW.mkTypedTerm sc =<< eval expr) shp rv
sv <- regToSetup bak Pre (\_tpr expr -> SAW.mkTypedTerm sc =<< eval expr) shp rv

-- Clobber the current value
rv' <- case fr ^. frAllocSpec . maMutbl of
M.Mut -> lift $ clobberSymbolic sym loc "clobberArg" shp rv
M.Immut -> lift $ clobberImmutSymbolic sym loc "clobberArg" shp rv
lift $ writeMirRefSim (fr ^. frType) ref' rv'
-- Gather fresh vars created by the clobber operation
sv' <- regToSetup sym Post (\_tpr expr -> SAW.mkTypedTerm sc =<< eval expr) shp rv'
sv' <- regToSetup bak Post (\_tpr expr -> SAW.mkTypedTerm sc =<< eval expr) shp rv'

return (sv, sv')

Expand All @@ -277,16 +280,17 @@ setReturn :: forall sym t st fs p rtp args ret tp.
(IsSymInterface sym, sym ~ W4.ExprBuilder t st fs) =>
TypeRepr tp -> MirReferenceMux sym tp -> MethodSpecBuilder sym t ->
OverrideSim p sym MIR rtp args ret (MethodSpecBuilder sym t)
setReturn tpr argRef msb = execBuilderT msb $ do
sym <- lift $ getSymInterface

setReturn tpr argRef msb =
ovrWithBackend $ \bak ->
execBuilderT msb $ do
let sym = backendGetSym bak
let ty = case msb ^. msbSpec . MS.csRet of
Just x -> x
Nothing -> M.TyTuple []
let shp = tyToShapeEq col ty tpr

rv <- lift $ readMirRefSim tpr argRef
sv <- regToSetup sym Post (\_tpr expr -> SAW.mkTypedTerm sc =<< eval expr) shp rv
sv <- regToSetup bak Post (\_tpr expr -> SAW.mkTypedTerm sc =<< eval expr) shp rv

void $ forNewRefs Post $ \fr -> do
let len = fr ^. frAllocSpec . maLen
Expand All @@ -296,7 +300,7 @@ setReturn tpr argRef msb = execBuilderT msb $ do
ref' <- lift $ mirRef_offsetSim (fr ^. frType) (fr ^. frRef) iSym
rv <- lift $ readMirRefSim (fr ^. frType) ref'
let shp = tyToShapeEq col (fr ^. frMirType) (fr ^. frType)
regToSetup sym Post (\_tpr expr -> SAW.mkTypedTerm sc =<< eval expr) shp rv
regToSetup bak Post (\_tpr expr -> SAW.mkTypedTerm sc =<< eval expr) shp rv

msbSpec . MS.csPostState . MS.csPointsTos %= (MirPointsTo (fr ^. frAlloc) svs :)

Expand All @@ -315,14 +319,15 @@ gatherAssumes :: forall sym t st fs p rtp args ret.
(IsSymInterface sym, sym ~ W4.ExprBuilder t st fs) =>
MethodSpecBuilder sym t ->
OverrideSim p sym MIR rtp args ret (MethodSpecBuilder sym t)
gatherAssumes msb = do
sym <- getSymInterface
gatherAssumes msb =
ovrWithBackend $ \bak -> do
let sym = backendGetSym bak

-- Find all vars that are mentioned in the arguments.
let vars = msb ^. msbPre . seVars

-- Find all assumptions that mention a relevant variable.
assumes <- liftIO $ collectAssumptions sym
assumes <- liftIO $ collectAssumptions bak
flatAssumes <- liftIO $ flattenAssumptions sym assumes
optAssumes' <- liftIO $ relevantPreds sym vars $
map (\a -> (assumptionPred a, show $ ppAssumption PP.viaShow a)) $ flatAssumes
Expand Down Expand Up @@ -355,14 +360,15 @@ gatherAsserts :: forall sym t st fs p rtp args ret.
(IsSymInterface sym, sym ~ W4.ExprBuilder t st fs) =>
MethodSpecBuilder sym t ->
OverrideSim p sym MIR rtp args ret (MethodSpecBuilder sym t)
gatherAsserts msb = do
sym <- getSymInterface
gatherAsserts msb =
ovrWithBackend $ \bak -> do
let sym = backendGetSym bak

-- Find all vars that are mentioned in the arguments or return value.
let vars = (msb ^. msbPre . seVars) `Set.union` (msb ^. msbPost . seVars)

-- Find all assertions that mention a relevant variable.
goals <- liftIO $ maybe [] goalsToList <$> getProofObligations sym
goals <- liftIO $ maybe [] goalsToList <$> getProofObligations bak
let asserts = map proofGoal goals
optAsserts' <- liftIO $ relevantPreds sym vars $
map (\a -> (a ^. W4.labeledPred, a ^. W4.labeledPredMsg)) asserts
Expand Down Expand Up @@ -464,7 +470,7 @@ gatherVars sym vals = do
-- `pred` that mentions at least one variable in `vars` along with some
-- `badVar` not in `vars`.
relevantPreds :: forall sym t st fs a.
(IsSymInterface sym, IsBoolSolver sym, sym ~ W4.ExprBuilder t st fs) =>
(IsSymInterface sym, sym ~ W4.ExprBuilder t st fs) =>
sym ->
Set (Some (W4.ExprBoundVar t)) ->
[(W4.Pred sym, a)] ->
Expand Down Expand Up @@ -502,14 +508,15 @@ finish :: forall sym t st fs p rtp args ret.
(IsSymInterface sym, sym ~ W4.ExprBuilder t st fs) =>
MethodSpecBuilder sym t ->
OverrideSim p sym MIR rtp args ret (M.MethodSpec sym)
finish msb = do
sym <- getSymInterface
let ng = W4.exprCounter sym
finish msb =
ovrWithBackend $ \bak -> do
let sym = backendGetSym bak
let ng = sym ^. W4.exprCounter

-- TODO: also undo any changes to Crucible global variables / refcells
-- (correct spec functions shouldn't make such changes, but it would be
-- nice to warn the user if they accidentally do)
_ <- liftIO $ popAssumptionFrameAndObligations sym (msb ^. msbSnapshotFrame)
_ <- liftIO $ popAssumptionFrameAndObligations bak (msb ^. msbSnapshotFrame)

let preVars = msb ^. msbPre . seVars
let postVars = msb ^. msbPost . seVars
Expand Down Expand Up @@ -628,14 +635,16 @@ substMethodSpec sc sm ms = do
-- RegValue will be converted into fresh variables in the MethodSpec (in either
-- the pre or post state, depending on the pre/post flag `p`), and any
-- MirReferences will be converted into MethodSpec allocations/pointers.
regToSetup :: forall sym t st fs tp p rtp args ret.
(IsSymInterface sym, sym ~ W4.ExprBuilder t st fs, HasCallStack) =>
sym -> PrePost ->
regToSetup :: forall sym bak t st fs tp p rtp args ret.
(IsSymBackend sym bak, sym ~ W4.ExprBuilder t st fs, HasCallStack) =>
bak -> PrePost ->
(forall tp'. BaseTypeRepr tp' -> W4.Expr t tp' -> IO SAW.TypedTerm) ->
TypeShape tp -> RegValue sym tp ->
BuilderT sym t (OverrideSim p sym MIR rtp args ret) (MS.SetupValue MIR)
regToSetup sym p eval shp rv = go shp rv
regToSetup bak p eval shp rv = go shp rv
where
sym = backendGetSym bak

go :: forall tp. TypeShape tp -> RegValue sym tp ->
BuilderT sym t (OverrideSim p sym MIR rtp args ret) (MS.SetupValue MIR)
go (UnitShape _) () = return $ MS.SetupStruct () False []
Expand Down Expand Up @@ -686,7 +695,7 @@ regToSetup sym p eval shp rv = go shp rv
let mutbl = case refTy of
M.TyRef _ M.Immut -> M.Immut
_ -> M.Mut
alloc <- refToAlloc sym p mutbl ty' tpr startRef len
alloc <- refToAlloc bak p mutbl ty' tpr startRef len
let offsetSv idx sv = if idx == 0 then sv else MS.SetupElem () sv idx
return $ offsetSv idx $ MS.SetupVar alloc

Expand All @@ -704,11 +713,11 @@ regToSetup sym p eval shp rv = go shp rv
OptField shp -> liftIO (readMaybeType sym "field" (shapeType shp) rv) >>= go shp
loop flds rvs (sv : svs)

refToAlloc :: forall sym t st fs tp p rtp args ret.
(IsSymInterface sym, sym ~ W4.ExprBuilder t st fs) =>
sym -> PrePost -> M.Mutability -> M.Ty -> TypeRepr tp -> MirReferenceMux sym tp -> Int ->
refToAlloc :: forall sym bak t st fs tp p rtp args ret.
(IsSymBackend sym bak, sym ~ W4.ExprBuilder t st fs) =>
bak -> PrePost -> M.Mutability -> M.Ty -> TypeRepr tp -> MirReferenceMux sym tp -> Int ->
BuilderT sym t (OverrideSim p sym MIR rtp args ret) MS.AllocIndex
refToAlloc sym p mutbl ty tpr ref len = do
refToAlloc bak p mutbl ty tpr ref len = do
refs <- use $ msbPrePost p . seRefs
mAlloc <- liftIO $ lookupAlloc ref (toList refs)
case mAlloc of
Expand All @@ -734,7 +743,7 @@ refToAlloc sym p mutbl ty tpr ref len = do
lookupAlloc _ref [] = return Nothing
lookupAlloc ref (Some fr : frs) = case testEquality tpr (fr ^. frType) of
Just Refl -> do
eq <- mirRef_eqIO sym ref (fr ^. frRef)
eq <- mirRef_eqIO bak ref (fr ^. frRef)
case W4.asConstantPred eq of
Just True -> return $ Just $ fr ^. frAlloc
Just False -> lookupAlloc ref frs
Expand Down
3 changes: 2 additions & 1 deletion crux-mir-comp/src/Mir/Compositional/Clobber.hs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ freshSymbolic sym loc nameStr shp = go shp
let nameSymbol = W4.safeSymbol nameStr
expr <- liftIO $ W4.freshConstant sym nameSymbol btpr
let ev = CreateVariableEvent loc nameStr btpr expr
liftIO $ addAssumptions sym (singleEvent ev)
ovrWithBackend $ \bak ->
liftIO $ addAssumptions bak (singleEvent ev)
return expr
go (ArrayShape (M.TyArray _ len) _ shp) =
MirVector_Vector <$> V.replicateM len (go shp)
Expand Down
28 changes: 15 additions & 13 deletions crux-mir-comp/src/Mir/Compositional/Override.hs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ runSpec :: forall sym p t st fs args ret rtp.
(IsSymInterface sym, sym ~ W4.ExprBuilder t st fs) =>
CollectionState -> FnHandle args ret -> MIRMethodSpec ->
OverrideSim (p sym) sym MIR rtp args ret (RegValue sym ret)
runSpec cs mh ms = do
let col = cs ^. collection
runSpec cs mh ms = ovrWithBackend $ \bak ->
do let col = cs ^. collection
sym <- getSymInterface
RegMap argVals <- getOverrideArgs
let argVals' = Map.fromList $ zip [0..] $ MS.assignmentToList argVals
Expand Down Expand Up @@ -168,7 +168,7 @@ runSpec cs mh ms = do
Some btpr <- liftIO $ termToType sym sc (SAW.ecType ec)
expr <- liftIO $ W4.freshConstant sym nameSymbol btpr
let ev = CreateVariableEvent loc nameStr btpr expr
liftIO $ addAssumptions sym (singleEvent ev)
liftIO $ addAssumptions bak (singleEvent ev)
term <- liftIO $ eval expr
return (SAW.ecVarIndex ec, term)

Expand Down Expand Up @@ -239,7 +239,7 @@ runSpec cs mh ms = do
show alloc ++ " (info: " ++ show info ++ ")"

-- All references in `allocSub` must point to disjoint memory regions.
liftIO $ checkDisjoint sym (Map.toList allocSub)
liftIO $ checkDisjoint bak (Map.toList allocSub)

-- TODO: see if we need any other assertions from LLVM OverrideMatcher

Expand All @@ -266,10 +266,9 @@ runSpec cs mh ms = do
Right x -> return x

forM_ (os ^. MS.osAsserts) $ \lp ->
liftIO $ addAssertion sym lp

liftIO $ addAssertion bak lp
forM_ (os ^. MS.osAssumes) $ \p ->
liftIO $ addAssumption sym (GenericAssumption loc "methodspec postcondition" p)
liftIO $ addAssumption bak (GenericAssumption loc "methodspec postcondition" p)

let preAllocMap = os ^. MS.setupValueSub

Expand Down Expand Up @@ -465,7 +464,8 @@ matchArg sym sc eval allocSpecs shp rv sv = go shp rv sv
Nothing -> return ()
Just (Some ptr)
| Just Refl <- testEquality tpr (ptr ^. mpType) -> do
eq <- liftIO $ mirRef_eqIO sym ref' (ptr ^. mpRef)
eq <- lift $ ovrWithBackend $ \bak ->
liftIO $ mirRef_eqIO bak ref' (ptr ^. mpRef)
let loc = mkProgramLoc "matchArg" InternalPos
MS.addAssert eq $
SimError loc (AssertFailureSimError ("mismatch on " ++ show alloc) "")
Expand Down Expand Up @@ -555,16 +555,18 @@ condTerm _ (MS.SetupCond_Ghost _ _ _ _) = do


checkDisjoint ::
(IsSymInterface sym, sym ~ W4.ExprBuilder t st fs) =>
sym ->
(sym ~ W4.ExprBuilder t st fs, IsSymBackend sym bak) =>
bak ->
[(MS.AllocIndex, Some (MirPointer sym))] ->
IO ()
checkDisjoint sym refs = go refs
checkDisjoint bak refs = go refs
where
sym = backendGetSym bak

go [] = return ()
go ((alloc, Some ptr) : rest) = do
forM_ rest $ \(alloc', Some ptr') -> do
disjoint <- W4.notPred sym =<< mirRef_overlapsIO sym (ptr ^. mpRef) (ptr' ^. mpRef)
assert sym disjoint $ GenericSimError $
disjoint <- W4.notPred sym =<< mirRef_overlapsIO bak (ptr ^. mpRef) (ptr' ^. mpRef)
assert bak disjoint $ GenericSimError $
"references " ++ show alloc ++ " and " ++ show alloc' ++ " must not overlap"
go rest
4 changes: 2 additions & 2 deletions crux-mir-comp/src/Mir/Cryptol.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ import Mir.Compositional.Convert


cryptolOverrides ::
forall sym p t st fs args ret blocks rtp a r .
forall sym bak p t st fs args ret blocks rtp a r .
(IsSymInterface sym, sym ~ W4.ExprBuilder t st fs) =>
Maybe (SomeOnlineSolver sym) ->
Maybe (SomeOnlineSolver sym bak) ->
CollectionState ->
Text ->
CFG MIR blocks args ret ->
Expand Down
2 changes: 1 addition & 1 deletion deps/crucible
Submodule crucible updated 217 files
2 changes: 1 addition & 1 deletion deps/macaw
Submodule macaw updated 70 files
+2 −2 .github/update-freeze
+2 −2 .github/workflows/ci.yaml
+23 −0 README.md
+2 −4 base/macaw-base.cabal
+2 −2 base/src/Data/Macaw/Analysis/RegisterUse.hs
+0 −4 base/src/Data/Macaw/Architecture/Info.hs
+1 −1 base/src/Data/Macaw/CFG/Rewriter.hs
+1 −7 base/src/Data/Macaw/DebugLogging.hs
+87 −74 cabal.project.freeze.ghc-8.10.7
+77 −64 cabal.project.freeze.ghc-8.8.4
+101 −89 cabal.project.freeze.ghc-9.0.2
+1 −1 deps/asl-translator
+1 −1 deps/crucible
+1 −1 deps/llvm-pretty
+1 −1 deps/llvm-pretty-bc-parser
+1 −1 deps/semmc
+1 −1 deps/what4
+28 −16 macaw-aarch32-symbolic/src/Data/Macaw/AArch32/Symbolic.hs
+30 −53 macaw-aarch32-symbolic/src/Data/Macaw/AArch32/Symbolic/Functions.hs
+7 −3 macaw-aarch32-symbolic/tests/Main.hs
+2 −0 macaw-aarch32-symbolic/tests/README.org
+11 −2 macaw-aarch32/ChangeLog.md
+0 −1 macaw-aarch32/macaw-aarch32.cabal
+31 −31 macaw-aarch32/src/Data/Macaw/ARM/ARMReg.hs
+322 −23 macaw-aarch32/src/Data/Macaw/ARM/Arch.hs
+0 −2 macaw-aarch32/src/Data/Macaw/ARM/Disassemble.hs
+45 −30 macaw-aarch32/src/Data/Macaw/ARM/Eval.hs
+0 −2 macaw-aarch32/src/Data/Macaw/ARM/Identify.hs
+3 −3 macaw-aarch32/src/Data/Macaw/ARM/Semantics/ARMSemantics.hs
+2 −4 macaw-aarch32/src/Data/Macaw/ARM/Semantics/TH.hs
+3 −3 macaw-aarch32/src/Data/Macaw/ARM/Semantics/ThumbSemantics.hs
+0 −195 macaw-aarch32/src/Data/Macaw/ARM/Simplify.hs
+9 −1 macaw-aarch32/tests/arm/Makefile
+ macaw-aarch32/tests/arm/syscall-a32.exe
+4 −0 macaw-aarch32/tests/arm/syscall-a32.mcw.expected
+30 −0 macaw-aarch32/tests/arm/syscall.s
+74 −76 macaw-ppc-symbolic/src/Data/Macaw/PPC/Symbolic/Functions.hs
+7 −3 macaw-ppc-symbolic/tests/Main.hs
+2 −0 macaw-ppc-symbolic/tests/README.org
+2 −1 macaw-ppc/src/Data/Macaw/PPC/Arch.hs
+3 −3 macaw-ppc/src/Data/Macaw/PPC/Semantics/PPC32.hs
+3 −3 macaw-ppc/src/Data/Macaw/PPC/Semantics/PPC64.hs
+6 −6 macaw-semmc/macaw-semmc.cabal
+0 −4 macaw-semmc/src/Data/Macaw/SemMC/Generator.hs
+16 −1 macaw-semmc/src/Data/Macaw/SemMC/Simplify.hs
+3 −3 macaw-semmc/src/Data/Macaw/SemMC/TH.hs
+7 −5 macaw-semmc/src/Data/Macaw/SemMC/TH/Monad.hs
+1 −1 refinement/macaw-refinement.cabal
+1 −1 refinement/src/Data/Macaw/Refinement/Logging.hs
+15 −6 refinement/src/Data/Macaw/Refinement/Solver.hs
+51 −42 refinement/src/Data/Macaw/Refinement/SymbolicExecution.hs
+7 −3 refinement/tests/RefinementTests.hs
+3 −0 symbolic/ChangeLog.md
+4 −6 symbolic/macaw-symbolic.cabal
+31 −30 symbolic/src/Data/Macaw/Symbolic.hs
+12 −11 symbolic/src/Data/Macaw/Symbolic/Bitcast.hs
+181 −158 symbolic/src/Data/Macaw/Symbolic/CrucGen.hs
+193 −96 symbolic/src/Data/Macaw/Symbolic/MemOps.hs
+108 −33 symbolic/src/Data/Macaw/Symbolic/Memory.hs
+70 −25 symbolic/src/Data/Macaw/Symbolic/Testing.hs
+2 −3 x86/macaw-x86.cabal
+0 −4 x86/src/Data/Macaw/X86/Flexdis.hs
+0 −7 x86/src/Data/Macaw/X86/Generator.hs
+2 −3 x86_symbolic/macaw-x86-symbolic.cabal
+181 −115 x86_symbolic/src/Data/Macaw/X86/Crucible.hs
+9 −3 x86_symbolic/tests/Main.hs
+2 −0 x86_symbolic/tests/README.org
+39 −0 x86_symbolic/tests/fail/T260.c
+ x86_symbolic/tests/fail/T260.opt.exe
+ x86_symbolic/tests/fail/T260.unopt.exe
Loading