Skip to content

Commit a4d9567

Browse files
author
Brian Huffman
committed
Make jvm_field_is support ordinary unqualified field names.
It now performs field resolution on the base field name to determine the complete fieldId including class name and type. This implements part of the feature requested in #902. However, as it stands the feature is not backward-compatible, as *only* unqualified names currently work. The field resolution procedure needs to be updated to allow optional qualified names.
1 parent f330538 commit a4d9567

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

src/SAWScript/Crucible/JVM/Builtins.hs

+15-7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import Control.Lens
4343

4444
import Control.Monad.State
4545
import qualified Control.Monad.State.Strict as Strict
46+
import Control.Monad.Trans.Except (runExceptT)
4647
import qualified Data.BitVector.Sized as BV
4748
import Data.Foldable (for_)
4849
import Data.Function
@@ -434,10 +435,13 @@ setupPrePointsTos mspec cc env pts mem0 = foldM doPointsTo mem0 pts
434435
doPointsTo :: Crucible.SymGlobalState Sym -> JVMPointsTo -> IO (Crucible.SymGlobalState Sym)
435436
doPointsTo mem pt =
436437
case pt of
437-
JVMPointsToField _loc lhs fld rhs ->
438+
JVMPointsToField _loc lhs fid rhs ->
438439
do lhs' <- resolveJVMRefVal lhs
439440
rhs' <- resolveSetupVal cc env tyenv nameEnv rhs
440-
CJ.doFieldStore sym mem lhs' fld (injectJVMVal sym rhs')
441+
-- TODO: Change type of CJ.doFieldStore to take a FieldId instead of a String.
442+
-- Then we won't have to match the definition of 'fieldIdText' here.
443+
let key = J.unClassName (J.fieldIdClass fid) ++ "." ++ J.fieldIdName fid
444+
CJ.doFieldStore sym mem lhs' key (injectJVMVal sym rhs')
441445
JVMPointsToElem _loc lhs idx rhs ->
442446
do lhs' <- resolveJVMRefVal lhs
443447
rhs' <- resolveSetupVal cc env tyenv nameEnv rhs
@@ -874,19 +878,23 @@ jvm_field_is ::
874878
JVMSetupM ()
875879
jvm_field_is _typed _bic _opt ptr fname val =
876880
JVMSetupM $
877-
do loc <- SS.toW4Loc "jvm_field_is" <$> lift getPosition
881+
do pos <- lift getPosition
882+
loc <- SS.toW4Loc "jvm_field_is" <$> lift getPosition
878883
st <- get
879884
let rs = st ^. Setup.csResolvedState
885+
let cc = st ^. Setup.csCrucibleContext
886+
let cb = cc ^. jccCodebase
880887
let path = Left fname
881888
if st ^. Setup.csPrePost == PreState && MS.testResolved ptr [] rs
882889
then fail $ "Multiple points-to preconditions on same pointer (field " ++ fname ++ ")"
883890
else Setup.csResolvedState %= MS.markResolved ptr [path]
884-
-- let env = MS.csAllocations (st ^. Setup.csMethodSpec)
885-
-- nameEnv = MS.csTypeNames (st ^. Setup.csMethodSpec)
886-
-- ptrTy <- typeOfSetupValue cc env nameEnv ptr
891+
let env = MS.csAllocations (st ^. Setup.csMethodSpec)
892+
let nameEnv = MS.csTypeNames (st ^. Setup.csMethodSpec)
893+
ptrTy <- typeOfSetupValue cc env nameEnv ptr
887894
-- valTy <- typeOfSetupValue cc env nameEnv val
888895
--when typed (checkMemTypeCompatibility lhsTy valTy)
889-
Setup.addPointsTo (JVMPointsToField loc ptr fname val)
896+
fid <- either fail pure =<< (liftIO $ runExceptT $ findField cb pos ptrTy fname)
897+
Setup.addPointsTo (JVMPointsToField loc ptr fid val)
890898

891899
jvm_elem_is ::
892900
Bool {- ^ whether to check type compatibility -} ->

src/SAWScript/Crucible/JVM/MethodSpecIR.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ type instance MS.AllocSpec CJ.JVM = (ProgramLoc, Allocation)
122122
type instance MS.PointsTo CJ.JVM = JVMPointsTo
123123

124124
data JVMPointsTo
125-
= JVMPointsToField ProgramLoc (MS.SetupValue CJ.JVM) String (MS.SetupValue CJ.JVM)
125+
= JVMPointsToField ProgramLoc (MS.SetupValue CJ.JVM) J.FieldId (MS.SetupValue CJ.JVM)
126126
| JVMPointsToElem ProgramLoc (MS.SetupValue CJ.JVM) Int (MS.SetupValue CJ.JVM)
127127
| JVMPointsToArray ProgramLoc (MS.SetupValue CJ.JVM) TypedTerm
128128

129129
ppPointsTo :: JVMPointsTo -> PPL.Doc
130130
ppPointsTo =
131131
\case
132-
JVMPointsToField _loc ptr fld val ->
133-
MS.ppSetupValue ptr <> PPL.text "." <> PPL.text fld
132+
JVMPointsToField _loc ptr fid val ->
133+
MS.ppSetupValue ptr <> PPL.text "." <> PPL.text (J.fieldIdName fid)
134134
PPL.<+> PPL.text "points to"
135135
PPL.<+> MS.ppSetupValue val
136136
JVMPointsToElem _loc ptr idx val ->

src/SAWScript/Crucible/JVM/Override.hs

+11-5
Original file line numberDiff line numberDiff line change
@@ -677,12 +677,15 @@ learnPointsTo opts sc cc spec prepost pt = do
677677
globals <- OM (use overrideGlobals)
678678
case pt of
679679

680-
JVMPointsToField loc ptr fname val ->
680+
JVMPointsToField loc ptr fid val ->
681681
do ty <- typeOfSetupValue cc tyenv nameEnv val
682682
(_, ptr') <- resolveSetupValueJVM opts cc sc spec ptr
683683
rval <- asRVal loc ptr'
684-
dyn <- liftIO $ CJ.doFieldLoad sym globals rval fname
685-
v <- liftIO $ projectJVMVal sym ty ("field load " ++ fname ++ ", " ++ show loc) dyn
684+
-- TODO: Change type of CJ.doFieldStore to take a FieldId instead of a String.
685+
-- Then we won't have to match the definition of 'fieldIdText' here.
686+
let key = J.unClassName (J.fieldIdClass fid) ++ "." ++ J.fieldIdName fid
687+
dyn <- liftIO $ CJ.doFieldLoad sym globals rval key
688+
v <- liftIO $ projectJVMVal sym ty ("field load " ++ J.fieldIdName fid ++ ", " ++ show loc) dyn
686689
matchArg opts sc cc spec prepost v ty val
687690

688691
JVMPointsToElem loc ptr idx val ->
@@ -823,12 +826,15 @@ executePointsTo opts sc cc spec pt = do
823826
globals <- OM (use overrideGlobals)
824827
case pt of
825828

826-
JVMPointsToField loc ptr fname val ->
829+
JVMPointsToField loc ptr fid val ->
827830
do (_, val') <- resolveSetupValueJVM opts cc sc spec val
828831
(_, ptr') <- resolveSetupValueJVM opts cc sc spec ptr
829832
rval <- asRVal loc ptr'
830833
let dyn = injectJVMVal sym val'
831-
globals' <- liftIO $ CJ.doFieldStore sym globals rval fname dyn
834+
-- TODO: Change type of CJ.doFieldStore to take a FieldId instead of a String.
835+
-- Then we won't have to match the definition of 'fieldIdText' here.
836+
let key = J.unClassName (J.fieldIdClass fid) ++ "." ++ J.fieldIdName fid
837+
globals' <- liftIO $ CJ.doFieldStore sym globals rval key dyn
832838
OM (overrideGlobals .= globals')
833839

834840
JVMPointsToElem loc ptr idx val ->

0 commit comments

Comments
 (0)