Skip to content

Commit

Permalink
Merge pull request #1267 from GaloisInc/persist-solver2
Browse files Browse the repository at this point in the history
Adapt to cryptol PR #1128 "persist-solver2"
  • Loading branch information
mergify[bot] authored May 19, 2021
2 parents cbd9223 + 567c6ed commit 2d1daa1
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 36 deletions.
6 changes: 3 additions & 3 deletions cryptol-saw-core/css/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Cryptol.Utils.Logger (quietLogger)
import qualified Verifier.SAW.Cryptol as C
import Verifier.SAW.SharedTerm
import qualified Verifier.SAW.Cryptol.Prelude as C
import Verifier.SAW.CryptolEnv (schemaNoUser)
import Verifier.SAW.CryptolEnv (schemaNoUser, meSolverConfig)


import qualified Data.ABC as ABC
Expand Down Expand Up @@ -92,7 +92,7 @@ cssMain css [inputModule,name] | cssMode css == NormalMode = do
modEnv <- CM.initialModuleEnv
let minp = CM.ModuleInput True (pure defaultEvalOpts) BS.readFile modEnv
(e,warn) <-
SMT.withSolver (CME.meSolverConfig modEnv) $ \s ->
SMT.withSolver (meSolverConfig modEnv) $ \s ->
CM.loadModuleByPath inputModule (minp s)
mapM_ (print . pp) warn
case e of
Expand Down Expand Up @@ -133,7 +133,7 @@ extractCryptol sc modEnv input = do
Right x -> return x
let minp = CM.ModuleInput True (pure defaultEvalOpts) BS.readFile modEnv
(exprResult, exprWarnings) <-
SMT.withSolver (CME.meSolverConfig modEnv) $ \s ->
SMT.withSolver (meSolverConfig modEnv) $ \s ->
CM.checkExpr pexpr (minp s)
mapM_ (print . pp) exprWarnings
((_, expr, schema), _modEnv') <-
Expand Down
17 changes: 15 additions & 2 deletions cryptol-saw-core/src/Verifier/SAW/CryptolEnv.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module Verifier.SAW.CryptolEnv
, InputText(..)
, lookupIn
, resolveIdentifier
, meSolverConfig
)
where

Expand Down Expand Up @@ -466,6 +467,18 @@ bindInteger (ident, n) env =

--------------------------------------------------------------------------------

-- FIXME: This definition was copied from a local declaration inside
-- function 'defaultRW' in module 'Cryptol.REPL.Monad'. The cryptol
-- package should probably export it so we don't have to copy it.
meSolverConfig :: ME.ModuleEnv -> TM.SolverConfig
meSolverConfig env =
TM.SolverConfig
{ TM.solverPath = "z3"
, TM.solverArgs = [ "-smt2", "-in" ]
, TM.solverVerbose = 0
, TM.solverPreludePath = ME.meSearchPath env
}

resolveIdentifier ::
(?fileReader :: FilePath -> IO ByteString) =>
CryptolEnv -> Text -> IO (Maybe T.Name)
Expand All @@ -480,7 +493,7 @@ resolveIdentifier env nm =
nameEnv = getNamingEnv env

doResolve pnm =
SMT.withSolver (ME.meSolverConfig modEnv) $ \s ->
SMT.withSolver (meSolverConfig modEnv) $ \s ->
do let minp = MM.ModuleInput True (pure defaultEvalOpts) ?fileReader modEnv
(res, _ws) <- MM.runModuleM (minp s) $
MM.interactive (MB.rename interactiveName nameEnv (MR.renameVar pnm))
Expand Down Expand Up @@ -646,7 +659,7 @@ liftModuleM ::
ME.ModuleEnv -> MM.ModuleM a -> IO (a, ME.ModuleEnv)
liftModuleM env m =
do let minp = MM.ModuleInput True (pure defaultEvalOpts) ?fileReader env
SMT.withSolver (ME.meSolverConfig env) $ \s ->
SMT.withSolver (meSolverConfig env) $ \s ->
MM.runModuleM (minp s) m >>= moduleCmdResult

defaultEvalOpts :: E.EvalOpts
Expand Down
49 changes: 25 additions & 24 deletions saw-remote-api/src/SAWServer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import Data.Parameterized.Some ( Some(..) )
import Data.Text (Text)
import qualified Data.Text as T
import qualified Crypto.Hash as Hash
import qualified Crypto.Hash.Conduit as Hash
--import qualified Crypto.Hash.Conduit as Hash
import System.Directory (getCurrentDirectory)
import System.IO.Silently (silence)

Expand All @@ -38,7 +38,7 @@ import qualified Data.AIG as AIG
import qualified Lang.Crucible.FunctionHandle as Crucible (HandleAllocator, newHandleAllocator)
import qualified Lang.Crucible.JVM as CJ
import qualified Lang.JVM.Codebase as JSS
import qualified Verifier.SAW.CryptolEnv as CryptolEnv
--import qualified Verifier.SAW.CryptolEnv as CryptolEnv
import Verifier.SAW.Module (emptyModule)
import Verifier.SAW.SharedTerm (mkSharedContext, scLoadModule)
import Verifier.SAW.Term.Functor (mkModuleName)
Expand All @@ -59,7 +59,7 @@ import Verifier.SAW.Rewriter (Simpset)
import qualified Cryptol.Utils.Ident as Cryptol

import qualified Argo
import qualified CryptolServer (validateServerState, ServerState(..))
--import qualified CryptolServer (validateServerState, ServerState(..))
import SAWServer.Exceptions
( serverValNotFound,
notAnLLVMModule,
Expand Down Expand Up @@ -230,27 +230,28 @@ initialState readFileFn =
-- recompute a cached result, the cached result may be used even if it is
-- associated with stale filesystem state. See the discussion of this issue at:
-- https://github.com/GaloisInc/argo/pull/70#discussion_r412462908
validateSAWState :: SAWState -> IO Bool
validateSAWState sawState =
checkAll
[ CryptolServer.validateServerState cryptolState
, checkAll $ map (uncurry checkHash) (M.assocs (view trackedFiles sawState))
]
where
checkAll [] = pure True
checkAll (c : cs) =
do result <- c
if result
then checkAll cs
else pure False

checkHash path hash =
do currentHash <- Hash.hashFile path
pure (currentHash == hash)

cryptolState =
CryptolServer.ServerState Nothing
(CryptolEnv.eModuleEnv . rwCryptol . view sawTopLevelRW $ sawState)

-- validateSAWState :: SAWState -> IO Bool
-- validateSAWState sawState =
-- checkAll
-- [ CryptolServer.validateServerState cryptolState
-- , checkAll $ map (uncurry checkHash) (M.assocs (view trackedFiles sawState))
-- ]
-- where
-- checkAll [] = pure True
-- checkAll (c : cs) =
-- do result <- c
-- if result
-- then checkAll cs
-- else pure False

-- checkHash path hash =
-- do currentHash <- Hash.hashFile path
-- pure (currentHash == hash)

-- cryptolState =
-- CryptolServer.ServerState Nothing
-- (CryptolEnv.eModuleEnv . rwCryptol . view sawTopLevelRW $ sawState)


newtype SAWEnv =
Expand Down
3 changes: 2 additions & 1 deletion saw-remote-api/src/SAWServer/CryptolExpression.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Data.Maybe (fromMaybe)
import Cryptol.Eval (EvalOpts(..))
import Cryptol.ModuleSystem (ModuleError, ModuleInput(..), ModuleRes, ModuleWarning)
import Cryptol.ModuleSystem.Base (genInferInput, getPrimMap, noPat, rename)
import Cryptol.ModuleSystem.Env (ModuleEnv, meSolverConfig)
import Cryptol.ModuleSystem.Env (ModuleEnv)
import Cryptol.ModuleSystem.Interface (noIfaceParams)
import Cryptol.ModuleSystem.Monad (ModuleM, interactive, runModuleM, setNameSeeds, setSupply, typeCheckWarnings, typeCheckingFailed)
import qualified Cryptol.ModuleSystem.Renamer as MR
Expand All @@ -35,6 +35,7 @@ import SAWScript.Value (biSharedContext, TopLevelRW(..))
import Verifier.SAW.CryptolEnv
( getAllIfaceDecls,
getNamingEnv,
meSolverConfig,
translateExpr,
CryptolEnv(eExtraTypes, eExtraTSyns, eModuleEnv) )
import Verifier.SAW.SharedTerm (SharedContext)
Expand Down
4 changes: 3 additions & 1 deletion src/SAWScript/AutoMatch/Cryptol.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import Data.List hiding (sort)
import Data.Maybe
import Data.Ord

import Verifier.SAW.CryptolEnv (meSolverConfig)

import Cryptol.Eval (EvalOpts(..))
import qualified Cryptol.ModuleSystem as M
import Cryptol.ModuleSystem.Name
Expand All @@ -33,7 +35,7 @@ getDeclsCryptol path = do
modEnv <- M.initialModuleEnv
let minp = M.ModuleInput True (pure evalOpts) BS.readFile modEnv
(result, warnings) <-
SMT.withSolver (M.meSolverConfig modEnv) $ \s ->
SMT.withSolver (meSolverConfig modEnv) $ \s ->
M.loadModuleByPath path (minp s)
return $ do
forM_ warnings $ liftF . flip Warning () . pretty
Expand Down
4 changes: 2 additions & 2 deletions src/SAWScript/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ import qualified Data.SBV.Dynamic as SBV
import qualified Data.AIG as AIG

-- cryptol
import qualified Cryptol.ModuleSystem.Env as C (meSolverConfig, meSearchPath)
import qualified Cryptol.ModuleSystem.Env as C (meSearchPath)
import qualified Cryptol.TypeCheck as C (SolverConfig)
import qualified Cryptol.TypeCheck.AST as C
import qualified Cryptol.TypeCheck.PP as C (ppWithNames, pp, text, (<+>))
Expand Down Expand Up @@ -1147,7 +1147,7 @@ eval_int :: TypedTerm -> TopLevel Integer
eval_int t = do
sc <- getSharedContext
cenv <- fmap rwCryptol getTopLevelRW
let cfg = C.meSolverConfig (CEnv.eModuleEnv cenv)
let cfg = CEnv.meSolverConfig (CEnv.eModuleEnv cenv)
unless (null (getAllExts (ttTerm t))) $
fail "term contains symbolic variables"
opts <- getOptions
Expand Down
3 changes: 1 addition & 2 deletions src/SAWScript/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ import SAWScript.Crucible.LLVM.Skeleton.Builtins
import qualified SAWScript.Crucible.LLVM.MethodSpecIR as CIR

-- Cryptol
import Cryptol.ModuleSystem.Env (meSolverConfig)
import qualified Cryptol.Eval as V (PPOpts(..))
import qualified Cryptol.Backend.Monad as V (runEval)
import qualified Cryptol.Eval.Value as V (defaultPPOpts, ppValue)
Expand Down Expand Up @@ -610,7 +609,7 @@ print_value (VString s) = printOutLnTop Info (Text.unpack s)
print_value (VTerm t) = do
sc <- getSharedContext
cenv <- fmap rwCryptol getTopLevelRW
let cfg = meSolverConfig (CEnv.eModuleEnv cenv)
let cfg = CEnv.meSolverConfig (CEnv.eModuleEnv cenv)
unless (null (getAllExts (ttTerm t))) $
fail "term contains symbolic variables"
sawopts <- getOptions
Expand Down

0 comments on commit 2d1daa1

Please sign in to comment.