Skip to content

Commit

Permalink
x86: Properly include assumed preconditions in goals (#1095)
Browse files Browse the repository at this point in the history
* x86: Properly assume preconditions

* x86: Add an integration test
  • Loading branch information
chameco committed Apr 18, 2021
1 parent 6c2ce5a commit aac0a02
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 3 deletions.
Binary file added intTests/test_llvm_x86_07/test
Binary file not shown.
10 changes: 10 additions & 0 deletions intTests/test_llvm_x86_07/test.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
section .bss
section .text
global precondtest
precondtest:
mov rax, rdi
ret
global _start
_start:
mov rax, 60
syscall
8 changes: 8 additions & 0 deletions intTests/test_llvm_x86_07/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdint.h>
#include <stdio.h>

extern uint64_t precondtest(uint64_t x);

void test() {
precondtest(1);
};
14 changes: 14 additions & 0 deletions intTests/test_llvm_x86_07/test.saw
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
enable_experimental;

m <- llvm_load_module "test.bc";

let precondtest_setup = do {
x <- crucible_fresh_var "x" (llvm_int 64);
crucible_precond {{ x < 10 }};
llvm_execute_func [crucible_term x];
x' <- crucible_fresh_var "x'" (llvm_int 64);
crucible_return (crucible_term x');
crucible_postcond {{ x' < 10 }};
};

llvm_verify_x86 m "./test" "precondtest" [] false precondtest_setup w4;
7 changes: 7 additions & 0 deletions intTests/test_llvm_x86_07/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh
set -e

# yasm -felf64 test.S
# ld test.o -o test
clang -c -emit-llvm test.c
$SAW test.saw
1 change: 1 addition & 0 deletions src/SAWScript/Crucible/LLVM/Override.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module SAWScript.Crucible.LLVM.Override

, learnCond
, learnSetupCondition
, executeSetupCondition
, matchArg
, assertTermEqualities
, methodSpecHandler
Expand Down
21 changes: 18 additions & 3 deletions src/SAWScript/Crucible/LLVM/X86.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import SAWScript.X86 hiding (Options)
import SAWScript.X86Spec
import SAWScript.Crucible.Common

import qualified SAWScript.Crucible.Common as Common
import qualified SAWScript.Crucible.Common.MethodSpec as MS
import qualified SAWScript.Crucible.Common.Override as O
import qualified SAWScript.Crucible.Common.Setup.Type as Setup
Expand Down Expand Up @@ -437,7 +438,15 @@ llvm_verify_x86 (Some (llvmModule :: LLVMModule x)) path nm globsyms checkSat se

liftIO $ C.executeCrucible execFeatures initial >>= \case
C.FinishedResult{} -> pure ()
C.AbortedResult{} -> printOutLn opts Warn "Warning: function never returns"
C.AbortedResult _ ar -> do
printOutLn opts Warn "Warning: function never returns"
print $ Common.ppAbortedResult
( \gp ->
case C.lookupGlobal mvar $ gp ^. C.gpGlobals of
Nothing -> "LLVM memory global variable not initialized"
Just mem -> C.LLVM.ppMem $ C.LLVM.memImplHeap mem
)
ar
C.TimeoutResult{} -> fail "Execution timed out"

stats <- checkGoals sym opts sc tactic
Expand Down Expand Up @@ -870,7 +879,10 @@ assertPost globals env premem preregs = do
pointsToMatches <- forM (ms ^. MS.csPostState . MS.csPointsTos)
$ assertPointsTo env tyenv nameEnv

let setupConditionMatches = fmap
let setupConditionMatchesPre = fmap -- assume preconditions
(LO.executeSetupCondition opts sc cc ms)
$ ms ^. MS.csPreState . MS.csConditions
let setupConditionMatchesPost = fmap -- assert postconditions
(LO.learnSetupCondition opts sc cc ms MS.PostState)
$ ms ^. MS.csPostState . MS.csConditions

Expand All @@ -889,12 +901,15 @@ assertPost globals env premem preregs = do
. sequence_ $ mconcat
[ returnMatches
, pointsToMatches
, setupConditionMatches
, setupConditionMatchesPre
, setupConditionMatchesPost
, [LO.assertTermEqualities sc cc]
]
st <- case result of
Left err -> throwX86 $ show err
Right (_, st) -> pure st
liftIO . forM_ (view O.osAssumes st) $ \p ->
C.addAssumption sym . C.LabeledPred p $ C.AssumptionReason (st ^. O.osLocation) "precondition"
liftIO . forM_ (view LO.osAsserts st) $ \(W4.LabeledPred p r) ->
C.addAssertion sym $ C.LabeledPred p r

Expand Down

0 comments on commit aac0a02

Please sign in to comment.