Skip to content

Commit e00d56b

Browse files
committed
JBMC: Regression tests for synchronized methods
1 parent 0f1dd43 commit e00d56b

File tree

11 files changed

+146
-0
lines changed

11 files changed

+146
-0
lines changed
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.lang.RuntimeException;
2+
import org.cprover.CProver;
3+
4+
public class Sync
5+
{
6+
public int field;
7+
8+
public static void main(String[] args)
9+
{
10+
final Sync o = new Sync();
11+
o.field = 0;
12+
// test regular synchronized method (monitorexit on return)
13+
o.f();
14+
// test synchronized method with throw (monitorexit on catch)
15+
try
16+
{
17+
o.g();
18+
}
19+
catch (RuntimeException e)
20+
{
21+
o.field++;
22+
}
23+
// Make sure both functions were executed and the second threw
24+
// The object should remain unlocked
25+
if ((o.field !=3) || (CProver.getMonitorCount(o) !=0))
26+
assert(false);
27+
}
28+
29+
public synchronized void f()
30+
{
31+
// Check that we acquired the lock
32+
if (CProver.getMonitorCount(this) !=1)
33+
assert(false);
34+
field++;
35+
}
36+
37+
public synchronized void g()
38+
{
39+
field++;
40+
// Check that we acquired the lock
41+
if (CProver.getMonitorCount(this) !=1)
42+
assert(false);
43+
throw new RuntimeException();
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
KNOWNBUG
2+
Sync.class
3+
--cp ../../../src/java_bytecode/library/core-models.jar:. --lazy-methods --java-threading
4+
5+
^VERIFICATION SUCCESSFUL$
6+
^EXIT=0$
7+
^SIGNAL=0$
8+
--
9+
^warning: ignoring
10+
--
11+
Checks all possible paths to ensure a synchronized method does not end in an illegal monitor state.
12+
13+
This is currently not working as explicit throws have been removed from the underlying model due to performance considerations.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
public class A
2+
{
3+
static int g;
4+
5+
public synchronized void me1()
6+
{
7+
g = 0;
8+
}
9+
10+
// expected verification success
11+
// --
12+
// base-case, no synchronization
13+
public void me2()
14+
{
15+
B t = new B();
16+
t.shared = 5;
17+
t.start();
18+
assert(t.shared == 5 || t.shared == 6);
19+
}
20+
21+
// expected verification success
22+
// --
23+
// locking mechanism
24+
public void me3()
25+
{
26+
B t = new B();
27+
synchronized(t)
28+
{
29+
t.shared = 5;
30+
t.start();
31+
assert(t.shared == 5);
32+
}
33+
}
34+
}
35+
36+
class B extends Thread
37+
{
38+
public int shared = 0;
39+
40+
@Override
41+
public void run()
42+
{
43+
set();
44+
}
45+
public synchronized void set()
46+
{
47+
shared++;
48+
}
49+
}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
A.class
3+
--function "A.me1:()V" --java-threading --cp ../../../src/java_bytecode/library/core-models.jar:. --show-goto-functions
4+
ATOMIC_BEGIN
5+
ATOMIC_END
6+
--
7+
--
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
A.class
3+
--function "A.me1:()V" --java-threading --lazy-methods --cp ../../../src/java_bytecode/library/core-models.jar:. --show-goto-functions
4+
ATOMIC_BEGIN
5+
ATOMIC_END
6+
--
7+
--
8+
Making sure that monitorEnter and monitorExit are not removed by lazy-methods
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
A.class
3+
--function "A.me1:()V" --java-threading --lazy-methods --cp ../../../src/java_bytecode/library/core-models.jar:.
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL
7+
--
8+
--
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
A.class
3+
--function "A.me3:()V" --java-threading --lazy-methods --cp ../../../src/java_bytecode/library/core-models.jar:.
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
--
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
A.class
3+
--function "A.me2:()V" --java-threading --lazy-methods --cp ../../../src/java_bytecode/library/core-models.jar:.
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
--

0 commit comments

Comments
 (0)