-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Java: make null-pointer and similar checks fatal
These previously used code_assertt, which asserts but then continues -- this changes them to use assert(condition); assume(condition), which ensures subsequent code is not executed in conditions which are in practice impossible. With --java-throw-runtime-exceptions this is irrelevant, as a real exception is raised and the conditional GOTO guarding the throw has the same effect as the assume.
- Loading branch information
Showing
16 changed files
with
221 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
public class Test { | ||
|
||
// In each of these cases a guard is repeated -- either an explicit assertion, | ||
// or a guard implied by Java's semantics, e.g. that an array index is in bounds. | ||
// It should be possible to violate the condition the first time, but not the second, | ||
// as the program would have aborted (without --java-throw-runtime-exceptions) or a | ||
// runtime exception thrown (with --java-throw-runtime-exceptions). | ||
|
||
public static void testAssertion(boolean condition) { | ||
assert(condition); | ||
assert(condition); | ||
} | ||
|
||
public static void testArrayBounds(int[] array, int index) { | ||
if(array == null) | ||
return; | ||
int got = array[index]; | ||
got = array[index]; | ||
} | ||
|
||
public static void testClassCast(boolean unknown) { | ||
A maybe_b = unknown ? new A() : new B(); | ||
B definitely_b = (B)maybe_b; | ||
definitely_b = (B)maybe_b; | ||
} | ||
|
||
public static void testNullDeref(A maybeNull) { | ||
int got = maybeNull.field; | ||
got = maybeNull.field; | ||
} | ||
|
||
public static void testDivByZero(int unknown) { | ||
int div = 1 / unknown; | ||
div = 1 / unknown; | ||
} | ||
|
||
public static void testArrayCreation(int maybeNegative) { | ||
int[] arr = new int[maybeNegative]; | ||
arr = new int[maybeNegative]; | ||
} | ||
|
||
} | ||
|
||
class A { public int field; } | ||
class B extends A {} |
12 changes: 12 additions & 0 deletions
12
regression/cbmc-java/repeated_guards/test_arraybounds.desc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
CORE | ||
Test.class | ||
--function Test.testArrayBounds | ||
^EXIT=10$ | ||
^SIGNAL=0$ | ||
^VERIFICATION FAILED$ | ||
array-index-out-of-bounds-low\.1.*: FAILURE$ | ||
array-index-out-of-bounds-low\.2.*: SUCCESS$ | ||
array-index-out-of-bounds-high\.1.*: FAILURE$ | ||
array-index-out-of-bounds-high\.2.*: SUCCESS$ | ||
-- | ||
^warning: ignoring |
10 changes: 10 additions & 0 deletions
10
regression/cbmc-java/repeated_guards/test_arraycreation.desc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CORE | ||
Test.class | ||
--function Test.testArrayCreation | ||
^EXIT=10$ | ||
^SIGNAL=0$ | ||
^VERIFICATION FAILED$ | ||
array-create-negative-size\.1.*: FAILURE$ | ||
array-create-negative-size\.2.*: SUCCESS$ | ||
-- | ||
^warning: ignoring |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CORE | ||
Test.class | ||
--function Test.testAssertion | ||
^EXIT=10$ | ||
^SIGNAL=0$ | ||
^VERIFICATION FAILED$ | ||
assertion at file Test\.java line 10.*: FAILURE$ | ||
assertion at file Test\.java line 11.*: SUCCESS$ | ||
-- | ||
^warning: ignoring |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CORE | ||
Test.class | ||
--function Test.testClassCast | ||
^EXIT=10$ | ||
^SIGNAL=0$ | ||
^VERIFICATION FAILED$ | ||
bad-dynamic-cast\.1.*: FAILURE$ | ||
bad-dynamic-cast\.2.*: SUCCESS$ | ||
-- | ||
^warning: ignoring |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CORE | ||
Test.class | ||
--function Test.testDivByZero | ||
^EXIT=10$ | ||
^SIGNAL=0$ | ||
^VERIFICATION FAILED$ | ||
integer-divide-by-zero\.1.*: FAILURE$ | ||
integer-divide-by-zero\.2.*: SUCCESS$ | ||
-- | ||
^warning: ignoring |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CORE | ||
Test.class | ||
--function Test.testNullDeref | ||
^EXIT=10$ | ||
^SIGNAL=0$ | ||
^VERIFICATION FAILED$ | ||
testNullDeref:\(LA;\)V\.null-pointer-exception\.1.*: FAILURE$ | ||
testNullDeref:\(LA;\)V\.null-pointer-exception\.2.*: SUCCESS$ | ||
-- | ||
^warning: ignoring |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
#include "assert_or_die.h" | ||
|
||
/// Cprover's code_assertt is non-fatal -- we check the condition and continue | ||
/// to execute the target program. By contrast code_assumet requires that the | ||
/// condition hold for execution to continue. This method therefore creates an | ||
/// "assert or die" construct, as C's `assert` function for example, that | ||
/// checks the condition and then halts if it does not hold. | ||
/// Source level assertions should probably use this, whilst checks that are | ||
/// normally non-fatal at runtime, such as integer overflows, should use | ||
/// code_assertt by itself. | ||
/// \param condition: condition to assert | ||
/// \return code of the form `assert(condition); assume(condition);` | ||
codet create_assert_or_die(const exprt &condition, const source_locationt &loc) | ||
{ | ||
code_blockt result; | ||
result.copy_to_operands(code_assertt(condition)); | ||
result.copy_to_operands(code_assumet(condition)); | ||
for(auto &op : result.operands()) | ||
op.add_source_location() = loc; | ||
result.add_source_location() = loc; | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/*******************************************************************\ | ||
Module: Assert or die | ||
Author: Diffblue Ltd | ||
\*******************************************************************/ | ||
|
||
#ifndef CPROVER_UTIL_ASSERT_OR_DIE_H | ||
#define CPROVER_UTIL_ASSERT_OR_DIE_H | ||
|
||
#include "std_code.h" | ||
|
||
codet create_assert_or_die(const exprt &condition, const source_locationt &loc); | ||
|
||
#endif // CPROVER_UTIL_ASSERT_OR_DIE_H |