Skip to content

Commit 68ac566

Browse files
committed
JBMC: Regression tests for synchronized methods
1 parent c0ee316 commit 68ac566

File tree

13 files changed

+183
-0
lines changed

13 files changed

+183
-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,10 @@
1+
CORE
2+
Sync.class
3+
--cp `../../../../scripts/format_classpath.sh . ../../../src/java_bytecode/library/core-models.jar` --lazy-methods --java-threading
4+
^VERIFICATION SUCCESSFUL$
5+
^EXIT=0$
6+
^SIGNAL=0$
7+
--
8+
^warning: ignoring
9+
--
10+
Checks all possible paths to ensure a synchronized method does not end in an illegal monitor state.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
// expected verification success
36+
// --
37+
// KNOWNBUG: synchronization of static synchronized
38+
// methods is not yet supported
39+
public void me4()
40+
{
41+
C t = new C();
42+
synchronized(C.class)
43+
{
44+
C.shared = 5;
45+
t.start();
46+
assert(C.shared == 5);
47+
}
48+
}
49+
}
50+
51+
class B extends Thread
52+
{
53+
public int shared = 0;
54+
55+
@Override
56+
public void run()
57+
{
58+
set();
59+
}
60+
public synchronized void set()
61+
{
62+
shared++;
63+
}
64+
}
65+
66+
class C extends Thread
67+
{
68+
public static int shared = 0;
69+
70+
@Override
71+
public void run()
72+
{
73+
C.static_set();
74+
}
75+
76+
public static synchronized void static_set()
77+
{
78+
C.shared++;
79+
}
80+
}
Binary file not shown.
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 `../../../../scripts/format_classpath.sh . ../../../src/java_bytecode/library/core-models.jar` --show-goto-functions --lazy-methods
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 `../../../../scripts/format_classpath.sh . ../../../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 `../../../../scripts/format_classpath.sh . ../../../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 `../../../../scripts/format_classpath.sh . ../../../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 `../../../../scripts/format_classpath.sh . ../../../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,9 @@
1+
KNOWNBUG
2+
A.class
3+
--function 'A.me4:()V' --java-threading --lazy-methods --cp `../../../../scripts/format_classpath.sh . ../../../src/java_bytecode/library/core-models.jar`
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
--
9+
Synchronization of static synchronized methods is not yet supported.

0 commit comments

Comments
 (0)