Skip to content

Commit df1de9c

Browse files
author
Brian Huffman
committed
Set and use writability permissions on JVM instance fields and arrays.
Adapt to GaloisInc/crucible#735.
1 parent 6c6fa3c commit df1de9c

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

intTests/test_jvm_unsound/test.saw

+3-2
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,6 @@ jvm_verify c "test_b" [set_ov_2] false
8686
fails (jvm_verify c "set" [] false set_spec_1 z3);
8787

8888
// It should be impossible to verify the bogus set_spec_2.
89-
// FIXME: this should fail
90-
jvm_verify c "set" [] false set_spec_2 z3;
89+
fails (jvm_verify c "set" [] false set_spec_2 z3);
90+
91+
print "Done.";

src/SAWScript/Crucible/JVM/Builtins.hs

+24-16
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import Data.Function
5555
import Data.List
5656
import Data.Map (Map)
5757
import qualified Data.Map as Map
58+
import Data.Maybe (fromMaybe)
5859
import Data.Set (Set)
5960
import qualified Data.Set as Set
6061
import qualified Data.Sequence as Seq
@@ -322,6 +323,7 @@ verifyPrestate ::
322323
verifyPrestate cc mspec globals0 =
323324
do let sym = cc^.jccBackend
324325
let jc = cc^.jccJVMContext
326+
let halloc = cc^.jccHandleAllocator
325327
let preallocs = mspec ^. MS.csPreState . MS.csAllocs
326328
let tyenv = MS.csAllocations mspec
327329
let nameEnv = mspec ^. MS.csPreState . MS.csVarTypeNames
@@ -338,8 +340,29 @@ verifyPrestate cc mspec globals0 =
338340
let makeWritable gs fid = CJ.doStaticFieldWritable sym jc gs fid (W4.truePred sym)
339341
globals0' <- liftIO $ foldM makeWritable globals0 updatedStaticFields
340342

343+
-- determine which arrays and instance fields need to be writable
344+
let addUpdates pt (as, es, fs) =
345+
case pt of
346+
JVMPointsToField _ a fid _ -> (as, es, Map.insertWith (++) a [fid] fs)
347+
JVMPointsToStatic{} -> (as, es, fs)
348+
JVMPointsToElem _ a i _ -> (as, Map.insertWith (++) a [i] es, fs)
349+
JVMPointsToArray _ a _ -> (Set.insert a as, es, fs)
350+
let (updatedArrays, updatedElems, updatedFields) =
351+
foldr addUpdates (Set.empty, Map.empty, Map.empty) postPointsTos
352+
341353
-- Allocate objects in memory for each 'jvm_alloc'
342-
(env, globals1) <- runStateT (traverse (doAlloc cc . snd) preallocs) globals0'
354+
let doAlloc a (_loc, alloc) =
355+
case alloc of
356+
AllocObject cname ->
357+
StateT (CJ.doAllocateObject sym halloc jc cname (flip elem fids))
358+
where fids = fromMaybe [] (Map.lookup a updatedFields)
359+
AllocArray len ty ->
360+
StateT (CJ.doAllocateArray sym halloc jc len ty writable)
361+
where
362+
writable
363+
| Set.member a updatedArrays = const True
364+
| otherwise = maybe (const False) (flip elem) (Map.lookup a updatedElems)
365+
(env, globals1) <- runStateT (Map.traverseWithKey doAlloc preallocs) globals0'
343366

344367
globals2 <- setupPrePointsTos mspec cc env (mspec ^. MS.csPreState . MS.csPointsTos) globals1
345368
cs <- setupPrestateConditions mspec cc env (mspec ^. MS.csPreState . MS.csConditions)
@@ -506,21 +529,6 @@ assertEqualVals cc v1 v2 =
506529

507530
--------------------------------------------------------------------------------
508531

509-
doAlloc ::
510-
JVMCrucibleContext ->
511-
Allocation ->
512-
StateT (Crucible.SymGlobalState Sym) IO JVMRefVal
513-
doAlloc cc alloc =
514-
case alloc of
515-
AllocObject cname -> StateT (CJ.doAllocateObject sym halloc jc cname)
516-
AllocArray len ty -> StateT (CJ.doAllocateArray sym halloc jc len ty)
517-
where
518-
sym = cc^.jccBackend
519-
halloc = cc^.jccHandleAllocator
520-
jc = cc^.jccJVMContext
521-
522-
--------------------------------------------------------------------------------
523-
524532
getMethodHandle :: CJ.JVMContext -> JVMMethodId -> IO CJ.JVMHandleInfo
525533
getMethodHandle jc (JVMMethodId mkey cname) =
526534
case Map.lookup (cname, mkey) (CJ.methodHandles jc) of

src/SAWScript/Crucible/JVM/Override.hs

+5-2
Original file line numberDiff line numberDiff line change
@@ -786,10 +786,13 @@ executeAllocation opts cc (var, (loc, alloc)) =
786786
let halloc = cc^.jccHandleAllocator
787787
sym <- Ov.getSymInterface
788788
globals <- OM (use overrideGlobals)
789+
let mut = True -- allocate objects/arrays from post-state as mutable
789790
(ptr, globals') <-
790791
case alloc of
791-
AllocObject cname -> liftIO $ CJ.doAllocateObject sym halloc jc cname globals
792-
AllocArray len elemTy -> liftIO $ CJ.doAllocateArray sym halloc jc len elemTy globals
792+
AllocObject cname ->
793+
liftIO $ CJ.doAllocateObject sym halloc jc cname (const mut) globals
794+
AllocArray len elemTy ->
795+
liftIO $ CJ.doAllocateArray sym halloc jc len elemTy (const mut) globals
793796
OM (overrideGlobals .= globals')
794797
assignVar cc loc var ptr
795798

0 commit comments

Comments
 (0)