@@ -41,6 +41,7 @@ module SAWScript.Crucible.JVM.Builtins
41
41
42
42
import Control.Lens
43
43
44
+ import qualified Control.Monad.Catch as X
44
45
import Control.Monad.State
45
46
import qualified Control.Monad.State.Strict as Strict
46
47
import Control.Monad.Trans.Except (runExceptT )
@@ -792,6 +793,54 @@ setupDynamicClassTable sym jc = foldM addClass Map.empty (Map.assocs (CJ.classTa
792
793
--------------------------------------------------------------------------------
793
794
-- Setup builtins
794
795
796
+ data JVMSetupError
797
+ = JVMFreshVarInvalidType JavaType
798
+ | JVMFieldMultiple SetupValue String -- reference and field name
799
+ | JVMFieldFailure String -- TODO: switch to a more structured type
800
+ | JVMFieldTypeMismatch String J. Type J. Type -- field name, expected, found
801
+ | JVMElemNonArray J. Type
802
+ | JVMElemInvalidIndex SetupValue Int Int -- reference, length, index
803
+ | JVMElemTypeMismatch Int J. Type J. Type -- index, expected, found
804
+ | JVMElemMultiple SetupValue Int -- reference and array index
805
+ | JVMArrayMultiple SetupValue
806
+
807
+ instance X. Exception JVMSetupError
808
+
809
+ instance Show JVMSetupError where
810
+ show err =
811
+ case err of
812
+ JVMFreshVarInvalidType jty ->
813
+ " jvm_fresh_var: Invalid type: " ++ show jty
814
+ JVMFieldMultiple _ptr fname ->
815
+ " jvm_field_is: Multiple specifications for the same instance field (" ++ fname ++ " )"
816
+ JVMFieldFailure msg ->
817
+ " jvm_field_is: JVM field resolution failed:\n " ++ msg
818
+ JVMFieldTypeMismatch fname expected found ->
819
+ -- FIXME: use a pretty printing function for J.Type instead of show
820
+ unlines
821
+ [ " jvm_field_is: Incompatible types for field " ++ show fname
822
+ , " Expected type: " ++ show expected
823
+ , " Given type: " ++ show found
824
+ ]
825
+ JVMElemNonArray jty ->
826
+ " jvm_elem_is: Not an array type: " ++ show jty
827
+ JVMElemInvalidIndex _ptr len idx ->
828
+ unlines
829
+ [ " jvm_elem_is: Array index out of bounds"
830
+ , " Array length: " ++ show len
831
+ , " Given index: " ++ show idx
832
+ ]
833
+ JVMElemTypeMismatch idx expected found ->
834
+ unlines
835
+ [ " jvm_elem_is: Incompatible types for array index " ++ show idx
836
+ , " Expected type: " ++ show expected
837
+ , " Given type: " ++ show found
838
+ ]
839
+ JVMElemMultiple _ptr idx ->
840
+ " jvm_elem_is: Multiple specifications for the same array index (" ++ show idx ++ " )"
841
+ JVMArrayMultiple _ptr ->
842
+ " jvm_array_is: Multiple specifications for the same array reference"
843
+
795
844
-- | Returns Cryptol type of actual type if it is an array or
796
845
-- primitive type.
797
846
cryptolTypeOfActual :: JavaType -> Maybe Cryptol. Type
@@ -837,7 +886,7 @@ jvm_fresh_var bic _opts name jty =
837
886
JVMSetupM $
838
887
do let sc = biSharedContext bic
839
888
case cryptolTypeOfActual jty of
840
- Nothing -> fail $ " Unsupported type in jvm_fresh_var: " ++ show jty
889
+ Nothing -> X. throwM $ JVMFreshVarInvalidType jty
841
890
Just cty -> Setup. freshVariable sc name cty
842
891
843
892
jvm_alloc_object ::
@@ -884,19 +933,15 @@ jvm_field_is _typed _bic _opt ptr fname val =
884
933
let cb = cc ^. jccCodebase
885
934
let path = Left fname
886
935
if st ^. Setup. csPrePost == PreState && MS. testResolved ptr [] rs
887
- then fail $ " Multiple points-to preconditions on same pointer (field " ++ fname ++ " ) "
936
+ then X. throwM $ JVMFieldMultiple ptr fname
888
937
else Setup. csResolvedState %= MS. markResolved ptr [path]
889
938
let env = MS. csAllocations (st ^. Setup. csMethodSpec)
890
939
let nameEnv = MS. csTypeNames (st ^. Setup. csMethodSpec)
891
940
ptrTy <- typeOfSetupValue cc env nameEnv ptr
892
941
valTy <- typeOfSetupValue cc env nameEnv val
893
- fid <- either fail pure =<< (liftIO $ runExceptT $ findField cb pos ptrTy fname)
942
+ fid <- either ( X. throwM . JVMFieldFailure ) pure =<< (liftIO $ runExceptT $ findField cb pos ptrTy fname)
894
943
unless (registerCompatible (J. fieldIdType fid) valTy) $
895
- fail $ unlines
896
- [ " Incompatible types for field " ++ fname
897
- , " Expected: " ++ show (J. fieldIdType fid)
898
- , " but given value of type: " ++ show valTy
899
- ]
944
+ X. throwM $ JVMFieldTypeMismatch fname (J. fieldIdType fid) valTy
900
945
Setup. addPointsTo (JVMPointsToField loc ptr fid val)
901
946
902
947
jvm_elem_is ::
@@ -915,7 +960,7 @@ jvm_elem_is _typed _bic _opt ptr idx val =
915
960
let cc = st ^. Setup. csCrucibleContext
916
961
let path = Right idx
917
962
if st ^. Setup. csPrePost == PreState && MS. testResolved ptr [path] rs
918
- then fail " Multiple points-to preconditions on same pointer "
963
+ then X. throwM $ JVMElemMultiple ptr idx
919
964
else Setup. csResolvedState %= MS. markResolved ptr [path]
920
965
let env = MS. csAllocations (st ^. Setup. csMethodSpec)
921
966
let nameEnv = MS. csTypeNames (st ^. Setup. csMethodSpec)
@@ -924,13 +969,9 @@ jvm_elem_is _typed _bic _opt ptr idx val =
924
969
elTy <-
925
970
case ptrTy of
926
971
J. ArrayType elTy -> pure elTy
927
- _ -> fail $ " Not an array type: " ++ show ptrTy
972
+ _ -> X. throwM $ JVMElemNonArray ptrTy
928
973
unless (registerCompatible elTy valTy) $
929
- fail $ unlines
930
- [ " Incompatible types for array element"
931
- , " Expected: " ++ show elTy
932
- , " but given value of type: " ++ show valTy
933
- ]
974
+ X. throwM $ JVMElemTypeMismatch idx elTy valTy
934
975
Setup. addPointsTo (JVMPointsToElem loc ptr idx val)
935
976
936
977
jvm_array_is ::
@@ -946,7 +987,7 @@ jvm_array_is _typed _bic _opt ptr val =
946
987
st <- get
947
988
let rs = st ^. Setup. csResolvedState
948
989
if st ^. Setup. csPrePost == PreState && MS. testResolved ptr [] rs
949
- then fail " Multiple points-to preconditions on same pointer "
990
+ then X. throwM $ JVMArrayMultiple ptr
950
991
else Setup. csResolvedState %= MS. markResolved ptr []
951
992
Setup. addPointsTo (JVMPointsToArray loc ptr val)
952
993
0 commit comments