Skip to content

Commit 82dabf3

Browse files
Test we don't call static initialisers unnecessarily
Previously we couldn't properly track when we had definitely already called them.
1 parent 5c636ce commit 82dabf3

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class ClassWithStaticInit {
2+
public static int x;
3+
4+
static {
5+
x = 42;
6+
}
7+
8+
public static int getStaticValue() {
9+
return x;
10+
}
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class Test {
2+
public static void test(boolean b) {
3+
int x;
4+
if (b) {
5+
x = ClassWithStaticInit.getStaticValue();
6+
}
7+
x = ClassWithStaticInit.getStaticValue();
8+
x = ClassWithStaticInit.getStaticValue();
9+
x = ClassWithStaticInit.getStaticValue();
10+
x = ClassWithStaticInit.getStaticValue();
11+
x = ClassWithStaticInit.getStaticValue();
12+
x = ClassWithStaticInit.getStaticValue();
13+
x = ClassWithStaticInit.getStaticValue();
14+
x = ClassWithStaticInit.getStaticValue();
15+
assert(false);
16+
}
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
CORE
2+
Test.class
3+
--function Test.test --show-vcc
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
java::ClassWithStaticInit::clinit_already_run#10
8+
^warning: ignoring
9+
--
10+
After the second call to ClassWithStaticInit.getStaticValue(), symex should know
11+
that java::ClassWithStaticInit::clinit_already_run is true, and hence the static
12+
initialiser doesn't need to be run again. It's quite hard to check this in a
13+
test, so we check that the generation of
14+
java::ClassWithStaticInit::clinit_already_run doesn't get too high despite many
15+
calls to ClassWithStaticInit.getStaticValue(). Hopefully this isn't brittle to
16+
small changes in how often generations are updated.

0 commit comments

Comments
 (0)