Skip to content

Commit 3d9e3a3

Browse files
committed
Java bytecode front-end: fix static field is_static_lifetime flag
Ensure static fields accessed via getstatic bytecode are properly marked with is_static_lifetime=true in the symbol table. This fixes an issue where static fields from missing classes were not properly initialized. Fixes: #851
1 parent 4fe3ade commit 3d9e3a3

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed
513 Bytes
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class a {
2+
public static void fun() {
3+
assert b.x > 0;
4+
}
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
a.class
3+
--function a.fun --show-symbol-table
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
^warning: ignoring

jbmc/src/java_bytecode/java_bytecode_typecheck.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ std::string java_bytecode_typecheckt::to_string(const typet &type)
2626
void java_bytecode_typecheckt::typecheck_non_type_symbol(symbolt &symbol)
2727
{
2828
PRECONDITION(!symbol.is_type);
29+
30+
// Static fields should be marked with is_static_lifetime.
31+
// In case the symbol was created without this flag (e.g., through stubbing),
32+
// ensure it's set correctly based on whether this is a static field.
33+
// Static fields have ID_C_field set on their type.
34+
if(symbol.type.find(ID_C_field).is_not_nil() && !symbol.is_static_lifetime)
35+
{
36+
symbol.is_static_lifetime = true;
37+
}
38+
2939
typecheck_type(symbol.type);
3040
typecheck_expr(symbol.value);
3141
}

jbmc/src/java_bytecode/java_bytecode_typecheck_expr.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,19 @@ void java_bytecode_typecheckt::typecheck_expr_symbol(symbol_exprt &expr)
7171

7272
// the java_bytecode_convert_class and java_bytecode_convert_method made sure
7373
// "identifier" exists in the symbol table
74-
const symbolt &symbol = symbol_table.lookup_ref(identifier);
74+
symbolt &symbol = symbol_table.get_writeable_ref(identifier);
7575

7676
INVARIANT(!symbol.is_type, "symbol identifier should not be a type");
7777

78+
// Static fields should be marked with is_static_lifetime.
79+
// In case the symbol was created without this flag (e.g., through stubbing),
80+
// ensure it's set correctly based on whether this is a static field.
81+
// Static fields have ID_C_field set on their type.
82+
if(symbol.type.find(ID_C_field).is_not_nil() && !symbol.is_static_lifetime)
83+
{
84+
symbol.is_static_lifetime = true;
85+
}
86+
7887
// type the expression
7988
expr.type() = symbol.type;
8089
}

0 commit comments

Comments
 (0)