Skip to content

Commit aac0a02

Browse files
authored
x86: Properly include assumed preconditions in goals (#1095)
* x86: Properly assume preconditions * x86: Add an integration test
1 parent 6c2ce5a commit aac0a02

File tree

7 files changed

+58
-3
lines changed

7 files changed

+58
-3
lines changed

intTests/test_llvm_x86_07/test

4.59 KB
Binary file not shown.

intTests/test_llvm_x86_07/test.S

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
section .bss
2+
section .text
3+
global precondtest
4+
precondtest:
5+
mov rax, rdi
6+
ret
7+
global _start
8+
_start:
9+
mov rax, 60
10+
syscall

intTests/test_llvm_x86_07/test.c

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <stdint.h>
2+
#include <stdio.h>
3+
4+
extern uint64_t precondtest(uint64_t x);
5+
6+
void test() {
7+
precondtest(1);
8+
};

intTests/test_llvm_x86_07/test.saw

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
enable_experimental;
2+
3+
m <- llvm_load_module "test.bc";
4+
5+
let precondtest_setup = do {
6+
x <- crucible_fresh_var "x" (llvm_int 64);
7+
crucible_precond {{ x < 10 }};
8+
llvm_execute_func [crucible_term x];
9+
x' <- crucible_fresh_var "x'" (llvm_int 64);
10+
crucible_return (crucible_term x');
11+
crucible_postcond {{ x' < 10 }};
12+
};
13+
14+
llvm_verify_x86 m "./test" "precondtest" [] false precondtest_setup w4;

intTests/test_llvm_x86_07/test.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
set -e
3+
4+
# yasm -felf64 test.S
5+
# ld test.o -o test
6+
clang -c -emit-llvm test.c
7+
$SAW test.saw

src/SAWScript/Crucible/LLVM/Override.hs

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module SAWScript.Crucible.LLVM.Override
3838

3939
, learnCond
4040
, learnSetupCondition
41+
, executeSetupCondition
4142
, matchArg
4243
, assertTermEqualities
4344
, methodSpecHandler

src/SAWScript/Crucible/LLVM/X86.hs

+18-3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ import SAWScript.X86 hiding (Options)
7272
import SAWScript.X86Spec
7373
import SAWScript.Crucible.Common
7474

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

438439
liftIO $ C.executeCrucible execFeatures initial >>= \case
439440
C.FinishedResult{} -> pure ()
440-
C.AbortedResult{} -> printOutLn opts Warn "Warning: function never returns"
441+
C.AbortedResult _ ar -> do
442+
printOutLn opts Warn "Warning: function never returns"
443+
print $ Common.ppAbortedResult
444+
( \gp ->
445+
case C.lookupGlobal mvar $ gp ^. C.gpGlobals of
446+
Nothing -> "LLVM memory global variable not initialized"
447+
Just mem -> C.LLVM.ppMem $ C.LLVM.memImplHeap mem
448+
)
449+
ar
441450
C.TimeoutResult{} -> fail "Execution timed out"
442451

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

873-
let setupConditionMatches = fmap
882+
let setupConditionMatchesPre = fmap -- assume preconditions
883+
(LO.executeSetupCondition opts sc cc ms)
884+
$ ms ^. MS.csPreState . MS.csConditions
885+
let setupConditionMatchesPost = fmap -- assert postconditions
874886
(LO.learnSetupCondition opts sc cc ms MS.PostState)
875887
$ ms ^. MS.csPostState . MS.csConditions
876888

@@ -889,12 +901,15 @@ assertPost globals env premem preregs = do
889901
. sequence_ $ mconcat
890902
[ returnMatches
891903
, pointsToMatches
892-
, setupConditionMatches
904+
, setupConditionMatchesPre
905+
, setupConditionMatchesPost
893906
, [LO.assertTermEqualities sc cc]
894907
]
895908
st <- case result of
896909
Left err -> throwX86 $ show err
897910
Right (_, st) -> pure st
911+
liftIO . forM_ (view O.osAssumes st) $ \p ->
912+
C.addAssumption sym . C.LabeledPred p $ C.AssumptionReason (st ^. O.osLocation) "precondition"
898913
liftIO . forM_ (view LO.osAsserts st) $ \(W4.LabeledPred p r) ->
899914
C.addAssertion sym $ C.LabeledPred p r
900915

0 commit comments

Comments
 (0)