Skip to content

Commit e97ed5c

Browse files
committed
Java frontend: record source locations of read static variables
This enables trace-source-location reports not to vary depending on whether static initializer calls were produced or not (at present the call would result in a statement at the getstatic location, while the variable read itself would disappear; a 'putstatic' instruction by contrast always generates an assignment and so is accounted for by a source location).
1 parent 507c6f8 commit e97ed5c

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

jbmc/src/java_bytecode/java_bytecode_convert_method.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,7 +1619,12 @@ java_bytecode_convert_methodt::convert_instructions(const methodt &method)
16191619
symbol_table.lookup_ref(field_id).symbol_expr());
16201620

16211621
convert_getstatic(
1622-
arg0, symbol_expr, is_assertions_disabled_field, c, results);
1622+
i_it->source_location,
1623+
arg0,
1624+
symbol_expr,
1625+
is_assertions_disabled_field,
1626+
c,
1627+
results);
16231628
}
16241629
else if(bytecode == BC_putfield)
16251630
{
@@ -2624,6 +2629,7 @@ code_blockt java_bytecode_convert_methodt::convert_putfield(
26242629
}
26252630

26262631
void java_bytecode_convert_methodt::convert_getstatic(
2632+
const source_locationt &source_location,
26272633
const exprt &arg0,
26282634
const symbol_exprt &symbol_expr,
26292635
const bool is_assertions_disabled_field,
@@ -2651,7 +2657,9 @@ void java_bytecode_convert_methodt::convert_getstatic(
26512657
needed_lazy_methods->add_needed_class(arg0.get_string(ID_class));
26522658
}
26532659
}
2654-
results[0] = java_bytecode_promotion(symbol_expr);
2660+
symbol_exprt symbol_with_location = symbol_expr;
2661+
symbol_with_location.add_source_location() = source_location;
2662+
results[0] = java_bytecode_promotion(symbol_with_location);
26552663

26562664
// Note this initializer call deliberately inits the class used to make
26572665
// the reference, which may be a child of the class that actually defines

jbmc/src/java_bytecode/java_bytecode_convert_method_class.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ class java_bytecode_convert_methodt:public messaget
432432
exprt::operandst &results) const;
433433

434434
void convert_getstatic(
435+
const source_locationt &source_location,
435436
const exprt &arg0,
436437
const symbol_exprt &symbol_expr,
437438
bool is_assertions_disabled_field,
Binary file not shown.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class ClassReadingStaticField {
2+
3+
static int x;
4+
5+
public static int test() {
6+
7+
return x;
8+
9+
}
10+
11+
}

jbmc/unit/java_bytecode/java_bytecode_convert_method/convert_method.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,45 @@ SCENARIO(
290290
}
291291
}
292292

293+
SCENARIO(
294+
"java_bytecode_convert_method_with_getstatic",
295+
"[core][java_bytecode][java_bytecode_convert_method]")
296+
{
297+
GIVEN("A class that reads a static field.")
298+
{
299+
const symbol_tablet symbol_table = load_java_class(
300+
"ClassReadingStaticField",
301+
"./java_bytecode/java_bytecode_convert_method");
302+
303+
WHEN("Converting the method that reads said field")
304+
{
305+
const auto &method_symbol =
306+
symbol_table.lookup_ref("java::ClassReadingStaticField.test:()I");
307+
308+
THEN(
309+
"There should be a symbol expression attributed to bytecode index "
310+
"0 (the getstatic instruction)")
311+
{
312+
bool found = false;
313+
method_symbol.value.visit_pre([&found](const exprt &subexpr) {
314+
if(
315+
const auto symbol_expr =
316+
expr_try_dynamic_cast<symbol_exprt>(subexpr))
317+
{
318+
if(
319+
symbol_expr->source_location().get_java_bytecode_index() == "0" &&
320+
symbol_expr->get_identifier() ==
321+
"java::ClassReadingStaticField.x")
322+
found = true;
323+
}
324+
});
325+
326+
REQUIRE(found);
327+
}
328+
}
329+
}
330+
}
331+
293332
/// Allow access to private methods so that they can be unit tested
294333
class java_bytecode_convert_method_unit_testt
295334
{

0 commit comments

Comments
 (0)