Skip to content

Commit 5bc512a

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 d955e28 commit 5bc512a

File tree

5 files changed

+40
-0
lines changed

5 files changed

+40
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class ClassWithStaticInit {
2+
public static int x;
3+
4+
static { x = 42; }
5+
6+
public static int getStaticValue() { return x; }
7+
}
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#[1-9][0-9]
8+
^warning: ignoring
9+
--
10+
After the second call to ClassWithStaticInit.getStaticValue(), symex should have
11+
java::ClassWithStaticInit::clinit_already_run in its constant propagator with
12+
the value true, and hence the static initialiser won't need to be run again.
13+
It's quite hard to check this in a test, so we check that the generation of
14+
java::ClassWithStaticInit::clinit_already_run doesn't get into double digits
15+
despite many calls to ClassWithStaticInit.getStaticValue(). Hopefully this isn't
16+
brittle to small changes in how often generations are updated.

0 commit comments

Comments
 (0)