Skip to content

Commit

Permalink
Implement type checking for jvm_return.
Browse files Browse the repository at this point in the history
This fixes more remaining known false positives of #938.
  • Loading branch information
Brian Huffman committed Dec 3, 2020
1 parent b4b90c5 commit a47c2f3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
10 changes: 5 additions & 5 deletions intTests/test_jvm_setup_errors/test.saw
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ check_fails test "set"
};

print "jvm_return with non-monomorphic type";
KNOWN_FALSE_POSITIVE test "get"
check_fails test "get"
do {
this <- jvm_alloc_object "Test";
val <- jvm_fresh_var "val" java_long;
Expand All @@ -439,7 +439,7 @@ KNOWN_FALSE_POSITIVE test "get"
};

print "jvm_return with non-jvm type";
KNOWN_FALSE_POSITIVE test "get"
check_fails test "get"
do {
this <- jvm_alloc_object "Test";
val <- jvm_fresh_var "val" java_long;
Expand All @@ -449,7 +449,7 @@ KNOWN_FALSE_POSITIVE test "get"
};

print "jvm_return with wrong base type";
KNOWN_FALSE_POSITIVE test "get"
check_fails test "get"
do {
this <- jvm_alloc_object "Test";
val <- jvm_fresh_var "val" java_long;
Expand All @@ -459,7 +459,7 @@ KNOWN_FALSE_POSITIVE test "get"
};

print "jvm_return with reference type when base type was expected";
KNOWN_FALSE_POSITIVE test "get"
check_fails test "get"
do {
this <- jvm_alloc_object "Test";
val <- jvm_fresh_var "val" java_long;
Expand All @@ -478,7 +478,7 @@ KNOWN_FALSE_POSITIVE test "get"
};

print "jvm_return when none was expected";
KNOWN_FALSE_POSITIVE test "set"
check_fails test "set"
do {
this <- jvm_alloc_object "Test";
x <- jvm_fresh_var "x" java_long;
Expand Down
29 changes: 28 additions & 1 deletion src/SAWScript/Crucible/JVM/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,8 @@ data JVMSetupError
| JVMArrayMultiple SetupValue
| JVMArgTypeMismatch Int J.Type J.Type -- argument position, expected, found
| JVMArgNumberWrong Int Int -- number expected, number found
| JVMReturnUnexpected J.Type -- found
| JVMReturnTypeMismatch J.Type J.Type -- expected, found

instance X.Exception JVMSetupError

Expand Down Expand Up @@ -886,6 +888,17 @@ instance Show JVMSetupError where
, "Expected: " ++ show expected
, "Given: " ++ show found
]
JVMReturnUnexpected found ->
unlines
[ "jvm_return: Unexpected return value for void method"
, "Given type: " ++ show found
]
JVMReturnTypeMismatch expected found ->
unlines
[ "jvm_return: Return type mismatch"
, "Expected type: " ++ show expected
, "Given type: " ++ show found
]

-- | Returns Cryptol type of actual type if it is an array or
-- primitive type.
Expand Down Expand Up @@ -1102,7 +1115,21 @@ jvm_execute_func bic opts args =
Setup.crucible_execute_func bic opts args

jvm_return :: BuiltinContext -> Options -> SetupValue -> JVMSetupM ()
jvm_return bic opts retVal = JVMSetupM $ Setup.crucible_return bic opts retVal
jvm_return bic opts retVal =
JVMSetupM $
do st <- get
let cc = st ^. Setup.csCrucibleContext
let mspec = st ^. Setup.csMethodSpec
let env = MS.csAllocations mspec
let nameEnv = MS.csTypeNames mspec
valTy <- typeOfSetupValue cc env nameEnv retVal
case mspec ^. MS.csRet of
Nothing ->
X.throwM (JVMReturnUnexpected valTy)
Just retTy ->
unless (registerCompatible retTy valTy) $
X.throwM (JVMReturnTypeMismatch retTy valTy)
Setup.crucible_return bic opts retVal

--------------------------------------------------------------------------------

Expand Down

0 comments on commit a47c2f3

Please sign in to comment.