Skip to content

Commit 4912fdd

Browse files
author
Brian Huffman
committed
Check disjointness between allocations and LLVM globals.
1 parent bdacfcc commit 4912fdd

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/SAWScript/Crucible/LLVM/Builtins.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,7 @@ verifyPoststate opts sc cc mspec env0 globals ret =
11311131
io $
11321132
runOverrideMatcher sym globals env0 terms0 initialFree poststateLoc $
11331133
do matchResult
1134-
learnCond opts sc cc mspec PostState (mspec ^. MS.csPostState)
1134+
learnCond opts sc cc mspec PostState (mspec ^. MS.csGlobalAllocs) (mspec ^. MS.csPostState)
11351135

11361136
st <-
11371137
case matchPost of

src/SAWScript/Crucible/LLVM/Override.hs

+41-4
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ methodSpecHandler_prestate opts sc cc args cs =
592592

593593
sequence_ [ matchArg opts sc cc cs PreState x y z | (x, y, z) <- xs]
594594

595-
learnCond opts sc cc cs PreState (cs ^. MS.csPreState)
595+
learnCond opts sc cc cs PreState (cs ^. MS.csGlobalAllocs) (cs ^. MS.csPreState)
596596

597597

598598
-- | Use a method spec to override the behavior of a function.
@@ -619,14 +619,15 @@ learnCond :: (?lc :: Crucible.TypeContext, Crucible.HasPtrWidth (Crucible.ArchWi
619619
-> LLVMCrucibleContext arch
620620
-> MS.CrucibleMethodSpecIR (LLVM arch)
621621
-> PrePost
622+
-> [MS.AllocGlobal (LLVM arch)]
622623
-> MS.StateSpec (LLVM arch)
623624
-> OverrideMatcher (LLVM arch) md ()
624-
learnCond opts sc cc cs prepost ss =
625+
learnCond opts sc cc cs prepost globals ss =
625626
do let loc = cs ^. MS.csLoc
626627
matchPointsTos opts sc cc cs prepost (ss ^. MS.csPointsTos)
627628
traverse_ (learnSetupCondition opts sc cc cs prepost) (ss ^. MS.csConditions)
628629
enforcePointerValidity cc loc ss
629-
enforceDisjointness loc ss
630+
enforceDisjointness cc loc globals ss
630631
enforceCompleteSubstitution loc ss
631632

632633

@@ -743,12 +744,15 @@ enforcePointerValidity cc loc ss =
743744
-- allowed to alias other read-only allocations, however.
744745
enforceDisjointness ::
745746
(?lc :: Crucible.TypeContext, Crucible.HasPtrWidth (Crucible.ArchWidth arch)) =>
747+
LLVMCrucibleContext arch ->
746748
W4.ProgramLoc ->
749+
[MS.AllocGlobal (LLVM arch)] ->
747750
MS.StateSpec (LLVM arch) ->
748751
OverrideMatcher (LLVM arch) md ()
749-
enforceDisjointness loc ss =
752+
enforceDisjointness cc loc globals ss =
750753
do sym <- Ov.getSymInterface
751754
sub <- OM (use setupValueSub)
755+
mem <- readGlobal $ Crucible.llvmMemVar $ ccLLVMContext cc
752756
let (allocsRW, allocsRO) = Map.partition (view isMut) (view MS.csAllocs ss)
753757
memsRW = Map.elems $ Map.intersectionWith (,) allocsRW sub
754758
memsRO = Map.elems $ Map.intersectionWith (,) allocsRO sub
@@ -761,6 +765,19 @@ enforceDisjointness loc ss =
761765
, q <- ps ++ memsRO
762766
]
763767

768+
-- Ensure that all RW and RO regions are disjoint from mutable
769+
-- global regions.
770+
let resolveAllocGlobal g@(LLVMAllocGlobal _ nm) =
771+
do ptr <- liftIO $ Crucible.doResolveGlobal sym mem nm
772+
pure (g, ptr)
773+
globals' <- traverse resolveAllocGlobal globals
774+
sequence_
775+
[ enforceDisjointAllocGlobal sym loc p q
776+
| p <- memsRW ++ memsRO
777+
, q <- globals'
778+
]
779+
780+
-- | Assert that two LLVM allocations are disjoint from each other.
764781
enforceDisjointAllocSpec ::
765782
(Crucible.HasPtrWidth (Crucible.ArchWidth arch)) =>
766783
Sym -> W4.ProgramLoc ->
@@ -788,6 +805,26 @@ enforceDisjointAllocSpec sym loc
788805
addAssert c $ Crucible.SimError loc $
789806
Crucible.AssertFailureSimError msg ""
790807

808+
-- | Assert that an LLVM allocation is disjoint from a global region.
809+
enforceDisjointAllocGlobal ::
810+
Sym -> W4.ProgramLoc ->
811+
(LLVMAllocSpec, LLVMPtr (Crucible.ArchWidth arch)) ->
812+
(LLVMAllocGlobal arch, LLVMPtr (Crucible.ArchWidth arch)) ->
813+
OverrideMatcher (LLVM arch) md ()
814+
enforceDisjointAllocGlobal sym loc
815+
(LLVMAllocSpec _pmut _pty _palign psz _ploc, p)
816+
(LLVMAllocGlobal _qloc (L.Symbol qname), q) =
817+
do let Crucible.LLVMPointer pblk _ = p
818+
let Crucible.LLVMPointer qblk _ = q
819+
c <- liftIO $ W4.notPred sym =<< W4.natEq sym pblk qblk
820+
let msg =
821+
"Memory regions not disjoint: "
822+
++ "(base=" ++ show (Crucible.ppPtr p) ++ ", size=" ++ show psz ++ ")"
823+
++ " and "
824+
++ "global " ++ show qname ++ " (base=" ++ show (Crucible.ppPtr q) ++ ")"
825+
addAssert c $ Crucible.SimError loc $
826+
Crucible.AssertFailureSimError msg ""
827+
791828
------------------------------------------------------------------------
792829

793830
-- | For each points-to statement read the memory value through the

0 commit comments

Comments
 (0)