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

Update crucible and macaw submodules, changes to x86 initial stack #1110

Merged
merged 7 commits into from
Mar 11, 2021
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
2 changes: 1 addition & 1 deletion deps/macaw
Submodule macaw updated 59 files
+38 −18 .github/workflows/ci.yaml
+1 −1 deps/asl-translator
+1 −1 deps/crucible
+29 −0 macaw-aarch32-symbolic/macaw-aarch32-symbolic.cabal
+1 −1 macaw-aarch32-symbolic/src/Data/Macaw/AArch32/Symbolic.hs
+180 −0 macaw-aarch32-symbolic/tests/Main.hs
+16 −0 macaw-aarch32-symbolic/tests/README.org
+13 −0 macaw-aarch32-symbolic/tests/fail/Makefile
+16 −0 macaw-aarch32-symbolic/tests/fail/constant.c
+ macaw-aarch32-symbolic/tests/fail/constant.opt.exe
+ macaw-aarch32-symbolic/tests/fail/constant.unopt.exe
+13 −0 macaw-aarch32-symbolic/tests/pass/Makefile
+15 −0 macaw-aarch32-symbolic/tests/pass/identity.c
+ macaw-aarch32-symbolic/tests/pass/identity.opt.exe
+ macaw-aarch32-symbolic/tests/pass/identity.unopt.exe
+13 −0 macaw-aarch32-symbolic/tests/pass/saturate-add.c
+ macaw-aarch32-symbolic/tests/pass/saturate-add.opt.exe
+ macaw-aarch32-symbolic/tests/pass/saturate-add.unopt.exe
+ macaw-arm/tests/arm/test-just-exit-a32.exe
+ macaw-arm/tests/arm/test-just-exit-t32.exe
+31 −1 macaw-ppc-symbolic/macaw-ppc-symbolic.cabal
+2 −1 macaw-ppc-symbolic/src/Data/Macaw/PPC/Symbolic.hs
+2 −1 macaw-ppc-symbolic/src/Data/Macaw/PPC/Symbolic/AtomWrapper.hs
+225 −0 macaw-ppc-symbolic/tests/Main.hs
+16 −0 macaw-ppc-symbolic/tests/README.org
+22 −0 macaw-ppc-symbolic/tests/fail/Makefile
+16 −0 macaw-ppc-symbolic/tests/fail/constant.c
+ macaw-ppc-symbolic/tests/fail/constant.opt64.exe
+ macaw-ppc-symbolic/tests/fail/constant.unopt64.exe
+22 −0 macaw-ppc-symbolic/tests/pass/Makefile
+15 −0 macaw-ppc-symbolic/tests/pass/identity.c
+ macaw-ppc-symbolic/tests/pass/identity.opt64.exe
+ macaw-ppc-symbolic/tests/pass/identity.unopt64.exe
+13 −0 macaw-ppc-symbolic/tests/pass/saturate-add.c
+ macaw-ppc-symbolic/tests/pass/saturate-add.opt64.exe
+ macaw-ppc-symbolic/tests/pass/saturate-add.unopt64.exe
+1 −1 refinement/src/Data/Macaw/Refinement/SymbolicExecution.hs
+2 −0 symbolic/macaw-symbolic.cabal
+2 −1 symbolic/src/Data/Macaw/Symbolic.hs
+16 −1 symbolic/src/Data/Macaw/Symbolic/CrucGen.hs
+56 −5 symbolic/src/Data/Macaw/Symbolic/MemOps.hs
+407 −0 symbolic/src/Data/Macaw/Symbolic/Testing.hs
+5 −0 x86_symbolic/macaw-x86-symbolic.cabal
+145 −119 x86_symbolic/tests/Main.hs
+16 −0 x86_symbolic/tests/README.org
+0 −18 x86_symbolic/tests/add.c
+ x86_symbolic/tests/add_ubuntu64
+ x86_symbolic/tests/add_ubuntu64.o
+13 −0 x86_symbolic/tests/fail/Makefile
+16 −0 x86_symbolic/tests/fail/constant.c
+ x86_symbolic/tests/fail/constant.opt.exe
+ x86_symbolic/tests/fail/constant.unopt.exe
+13 −0 x86_symbolic/tests/pass/Makefile
+15 −0 x86_symbolic/tests/pass/identity.c
+ x86_symbolic/tests/pass/identity.opt.exe
+ x86_symbolic/tests/pass/identity.unopt.exe
+13 −0 x86_symbolic/tests/pass/saturate-add.c
+ x86_symbolic/tests/pass/saturate-add.opt.exe
+ x86_symbolic/tests/pass/saturate-add.unopt.exe
2 changes: 1 addition & 1 deletion deps/what4
43 changes: 31 additions & 12 deletions src/SAWScript/Crucible/LLVM/X86.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Stability : provisional

module SAWScript.Crucible.LLVM.X86
( llvm_verify_x86
, defaultStackBaseAlign
) where

import Control.Lens.TH (makeLenses)
Expand Down Expand Up @@ -78,11 +79,13 @@ import SAWScript.Crucible.LLVM.MethodSpecIR
import SAWScript.Crucible.LLVM.ResolveSetupValue
import qualified SAWScript.Crucible.LLVM.Override as LO

import qualified What4.Config as W4
import qualified What4.Expr as W4
import qualified What4.FunctionName as W4
import qualified What4.Interface as W4
import qualified What4.LabeledPred as W4
import qualified What4.ProgramLoc as W4
import qualified What4.Expr.Builder as W4.B

import qualified Lang.Crucible.Analysis.Postdom as C
import qualified Lang.Crucible.Backend as C
Expand All @@ -98,6 +101,7 @@ import qualified Lang.Crucible.Simulator.RegMap as C
import qualified Lang.Crucible.Simulator.SimError as C
import qualified Lang.Crucible.Simulator.PathSatisfiability as C

import qualified Lang.Crucible.LLVM.Bytes as C.LLVM
import qualified Lang.Crucible.LLVM.DataLayout as C.LLVM
import qualified Lang.Crucible.LLVM.Extension as C.LLVM
import qualified Lang.Crucible.LLVM.Intrinsics as C.LLVM
Expand Down Expand Up @@ -156,6 +160,17 @@ runX86Sim st m = runStateT (unX86Sim m) st
throwX86 :: MonadThrow m => String -> m a
throwX86 = throw . X86Error

defaultStackBaseAlign :: Integer
defaultStackBaseAlign = 16

integerToAlignment ::
(MonadIO m, MonadThrow m) =>
Integer ->
m C.LLVM.Alignment
integerToAlignment i
| Just ba <- C.LLVM.toAlignment (C.LLVM.toBytes i) = pure ba
| otherwise = throwX86 $ mconcat ["Invalid alignment specified: ", show i]

setReg ::
(MonadIO m, MonadThrow m) =>
Register ->
Expand Down Expand Up @@ -235,11 +250,13 @@ llvm_verify_x86 (Some (llvmModule :: LLVMModule x)) path nm globsyms checkSat se
opts <- getOptions
basic_ss <- getBasicSS
sym <- liftIO $ newSAWCoreBackend sc
rw <- getTopLevelRW
cacheTermsSetting <- liftIO $ W4.getOptionSetting W4.B.cacheTerms $ W4.getConfiguration sym
_ <- liftIO $ W4.setOpt cacheTermsSetting $ rwWhat4HashConsingX86 rw
sawst <- liftIO $ sawCoreState sym
halloc <- getHandleAlloc
let mvar = C.LLVM.llvmMemVar . view C.LLVM.transContext $ modTrans llvmModule
sfs <- liftIO $ Macaw.newSymFuns sym
rw <- getTopLevelRW
let cenv = rwCryptol rw
liftIO $ sawRegisterSymFunInterp sawst (Macaw.fnAesEnc sfs) $ cryptolUninterpreted cenv "aesenc"
liftIO $ sawRegisterSymFunInterp sawst (Macaw.fnAesEncLast sfs) $ cryptolUninterpreted cenv "aesenclast"
Expand Down Expand Up @@ -282,7 +299,8 @@ 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
(env, preState) <- liftIO . runX86Sim emptyState $ setupMemory globsyms
balign <- integerToAlignment $ rwStackBaseAlign rw
(env, preState) <- liftIO . runX86Sim emptyState $ setupMemory globsyms balign

let
funcLookup = Macaw.LookupFunctionHandle $ \st _mem regs -> do
Expand All @@ -304,7 +322,7 @@ llvm_verify_x86 (Some (llvmModule :: LLVMModule x)) path nm globsyms checkSat se
pure
( C.cfgHandle funcCFG
, st & C.stateContext . C.functionBindings
%~ C.insertHandleMap (C.cfgHandle funcCFG) (C.UseCFG funcCFG $ C.postdomInfo funcCFG)
%~ C.FnBindings . C.insertHandleMap (C.cfgHandle funcCFG) (C.UseCFG funcCFG $ C.postdomInfo funcCFG) . C.fnBindings
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

)
Nothing -> fail $ mconcat
[ "Unable to find CFG for function at address "
Expand All @@ -322,7 +340,7 @@ llvm_verify_x86 (Some (llvmModule :: LLVMModule x)) path nm globsyms checkSat se
Macaw.macawExtensions (Macaw.x86_64MacawEvalFn sfs) mvar
(mkGlobalMap . Map.singleton 0 $ preState ^. x86GlobalBase)
funcLookup noExtraValidityPred
, C._functionBindings = C.insertHandleMap (C.cfgHandle cfg) (C.UseCFG cfg $ C.postdomInfo cfg) C.emptyHandleMap
, C._functionBindings = C.FnBindings $ C.insertHandleMap (C.cfgHandle cfg) (C.UseCFG cfg $ C.postdomInfo cfg) C.emptyHandleMap
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

, C._cruciblePersonality = Macaw.MacawSimulatorState
, C._profilingMetrics = Map.empty
}
Expand Down Expand Up @@ -541,12 +559,13 @@ initialState sym opts sc cc elf relf ms globs maxAddr = do
setupMemory ::
X86Constraints =>
[(String, Integer)] {- ^ Global variable symbol names and sizes (in bytes) -} ->
C.LLVM.Alignment {- ^ Stack base alignment -} ->
X86Sim (Map MS.AllocIndex Ptr)
setupMemory globsyms = do
setupMemory globsyms balign = do
setupGlobals globsyms

-- Allocate a reasonable amount of stack (4 KiB + 1 qword for IP)
allocateStack 4096
-- Allocate a reasonable amount of stack (4 KiB + 0b10000 for least valid alignment + 1 qword for IP)
allocateStack (4096 + 16) balign

ms <- use x86MethodSpec

Expand Down Expand Up @@ -597,25 +616,25 @@ setupGlobals globsyms = do
allocateStack ::
X86Constraints =>
Integer {- ^ Stack size in bytes -} ->
C.LLVM.Alignment {- ^ Stack base alignment -} ->
X86Sim ()
allocateStack szInt = do
allocateStack szInt balign = do
sym <- use x86Sym
mem <- use x86Mem
regs <- use x86Regs
let align = C.LLVM.exponentToAlignment 4
sz <- liftIO $ W4.bvLit sym knownNat $ BV.mkBV knownNat $ szInt + 8
(base, mem') <- liftIO $ C.LLVM.doMalloc sym C.LLVM.HeapAlloc C.LLVM.Mutable
"stack_alloc" mem sz align
(base, mem') <- liftIO $ C.LLVM.doMalloc sym C.LLVM.HeapAlloc C.LLVM.Mutable "stack_alloc" mem sz balign
sn <- case W4.userSymbol "stack" of
Left err -> throwX86 $ "Invalid symbol for stack: " <> show err
Right sn -> pure sn
fresh <- liftIO $ C.LLVM.LLVMPointer
<$> W4.natLit sym 0
<*> W4.freshConstant sym sn (W4.BaseBVRepr $ knownNat @64)
ptr <- liftIO $ C.LLVM.doPtrAddOffset sym mem' base =<< W4.bvLit sym knownNat (BV.mkBV knownNat szInt)
writeAlign <- integerToAlignment defaultStackBaseAlign
finalMem <- liftIO $ C.LLVM.doStore sym mem' ptr
(C.LLVM.LLVMPointerRepr $ knownNat @64)
(C.LLVM.bitvectorType 8) align fresh
(C.LLVM.bitvectorType 8) writeAlign fresh
x86Mem .= finalMem
finalRegs <- setReg Macaw.RSP ptr regs
x86Regs .= finalRegs
Expand Down
44 changes: 43 additions & 1 deletion src/SAWScript/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,9 @@ buildTopLevelEnv proxy opts =
, rwProfilingFile = Nothing
, rwLaxArith = False
, rwWhat4HashConsing = False
, rwWhat4HashConsingX86 = False
chameco marked this conversation as resolved.
Show resolved Hide resolved
, rwPreservedRegs = []
, rwStackBaseAlign = defaultStackBaseAlign
}
return (bic, ro0, rw0)

Expand Down Expand Up @@ -546,6 +548,16 @@ disable_what4_hash_consing = do
rw <- getTopLevelRW
putTopLevelRW rw { rwWhat4HashConsing = False }

enable_x86_what4_hash_consing :: TopLevel ()
enable_x86_what4_hash_consing = do
rw <- getTopLevelRW
putTopLevelRW rw { rwWhat4HashConsingX86 = True }

disable_x86_what4_hash_consing :: TopLevel ()
disable_x86_what4_hash_consing = do
rw <- getTopLevelRW
putTopLevelRW rw { rwWhat4HashConsingX86 = False }

add_x86_preserved_reg :: String -> TopLevel ()
add_x86_preserved_reg r = do
rw <- getTopLevelRW
Expand All @@ -556,6 +568,16 @@ default_x86_preserved_reg = do
rw <- getTopLevelRW
putTopLevelRW rw { rwPreservedRegs = [] }

set_x86_stack_base_align :: Integer -> TopLevel ()
set_x86_stack_base_align a = do
rw <- getTopLevelRW
putTopLevelRW rw { rwStackBaseAlign = a }

default_x86_stack_base_align :: TopLevel ()
default_x86_stack_base_align = do
rw <- getTopLevelRW
putTopLevelRW rw { rwStackBaseAlign = defaultStackBaseAlign }

include_value :: FilePath -> TopLevel ()
include_value file = do
oldpath <- io $ getCurrentDirectory
Expand Down Expand Up @@ -2387,6 +2409,16 @@ primitives = Map.fromList
Experimental
[ "Legacy alternative name for `llvm_verify_x86`." ]

, prim "enable_x86_what4_hash_consing" "TopLevel ()"
(pureVal enable_x86_what4_hash_consing)
Experimental
[ "Enable hash consing for What4 expressions during x86 verification." ]

, prim "disable_x86_what4_hash_consing" "TopLevel ()"
(pureVal disable_x86_what4_hash_consing)
Current
[ "Disable hash consing for What4 expressions during x86 verification." ]

, prim "add_x86_preserved_reg" "String -> TopLevel ()"
(pureVal add_x86_preserved_reg)
Current
Expand All @@ -2395,7 +2427,17 @@ primitives = Map.fromList
, prim "default_x86_preserved_reg" "TopLevel ()"
(pureVal default_x86_preserved_reg)
Current
[ "Use the default set of callee-saved registers during x86 verification.." ]
[ "Use the default set of callee-saved registers during x86 verification." ]

, prim "set_x86_stack_base_align" "Int -> TopLevel ()"
chameco marked this conversation as resolved.
Show resolved Hide resolved
(pureVal set_x86_stack_base_align)
Experimental
[ "Set the alignment of the stack allocation base to 2^n during x86 verification." ]

, prim "default_x86_stack_base_align" "TopLevel ()"
(pureVal default_x86_stack_base_align)
Experimental
[ "Use the default stack allocation base alignment during x86 verification." ]

, prim "llvm_array_value"
"[SetupValue] -> SetupValue"
Expand Down
5 changes: 5 additions & 0 deletions src/SAWScript/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,13 @@ data TopLevelRW =
, rwCrucibleAssertThenAssume :: Bool
, rwProfilingFile :: Maybe FilePath
, rwLaxArith :: Bool

-- FIXME: These might be better split into "simulator hash-consing" and "tactic hash-consing"
, rwWhat4HashConsing :: Bool
, rwWhat4HashConsingX86 :: Bool

, rwPreservedRegs :: [String]
, rwStackBaseAlign :: Integer
}

newtype TopLevel a =
Expand Down
6 changes: 3 additions & 3 deletions src/SAWScript/X86.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import Lang.Crucible.Simulator.EvalStmt(executeCrucible)
import Lang.Crucible.Simulator.ExecutionTree
(ExecResult(..), SimContext(..), FnState(..)
, ExecState(InitialState)
, FunctionBindings(..)
)
import Lang.Crucible.Simulator.SimError(SimError(..), SimErrorReason)
import Lang.Crucible.Backend
Expand Down Expand Up @@ -475,9 +476,8 @@ doSim opts elf sfs name (globs,overs) st checkPost =
, simHandleAllocator = allocator opts
, printHandle = stdout
, extensionImpl = macawExtensions (x86_64MacawEvalFn sfs) mvar globs (callHandler overs sym) noExtraValidityPred
, _functionBindings =
insertHandleMap (cfgHandle cfg) (UseCFG cfg (postdomInfo cfg)) $
emptyHandleMap
, _functionBindings = FnBindings $
insertHandleMap (cfgHandle cfg) (UseCFG cfg (postdomInfo cfg)) emptyHandleMap
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

, _cruciblePersonality = MacawSimulatorState
, _profilingMetrics = Map.empty
}
Expand Down
2 changes: 1 addition & 1 deletion src/SAWScript/X86Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ setupGlobals opts gs fs s
let halloc = simHandleAllocator (st ^. stateContext)
h <- mkHandle halloc fname
let addBinding = over (stateContext . functionBindings)
(insertHandleMap h (UseOverride o))
(FnBindings . insertHandleMap h (UseOverride o) . fnBindings)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return (h, addBinding st)
)

Expand Down