Skip to content

Java frontend: record source locations of read static variables #5144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions jbmc/src/java_bytecode/java_bytecode_convert_method.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1644,7 +1644,12 @@ java_bytecode_convert_methodt::convert_instructions(const methodt &method)
symbol_table.lookup_ref(field_id).symbol_expr());

convert_getstatic(
arg0, symbol_expr, is_assertions_disabled_field, c, results);
i_it->source_location,
arg0,
symbol_expr,
is_assertions_disabled_field,
c,
results);
}
else if(bytecode == BC_putfield)
{
Expand Down Expand Up @@ -2649,6 +2654,7 @@ code_blockt java_bytecode_convert_methodt::convert_putfield(
}

void java_bytecode_convert_methodt::convert_getstatic(
const source_locationt &source_location,
const exprt &arg0,
const symbol_exprt &symbol_expr,
const bool is_assertions_disabled_field,
Expand Down Expand Up @@ -2676,7 +2682,9 @@ void java_bytecode_convert_methodt::convert_getstatic(
needed_lazy_methods->add_needed_class(arg0.get_string(ID_class));
}
}
results[0] = java_bytecode_promotion(symbol_expr);
symbol_exprt symbol_with_location = symbol_expr;
symbol_with_location.add_source_location() = source_location;
results[0] = java_bytecode_promotion(symbol_with_location);

// Note this initializer call deliberately inits the class used to make
// the reference, which may be a child of the class that actually defines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ class java_bytecode_convert_methodt:public messaget
exprt::operandst &results) const;

void convert_getstatic(
const source_locationt &source_location,
const exprt &arg0,
const symbol_exprt &symbol_expr,
bool is_assertions_disabled_field,
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class ClassReadingStaticField {

static int x;

public static int test() {

return x;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,45 @@ SCENARIO(
}
}

SCENARIO(
"java_bytecode_convert_method_with_getstatic",
"[core][java_bytecode][java_bytecode_convert_method]")
{
GIVEN("A class that reads a static field.")
{
const symbol_tablet symbol_table = load_java_class(
"ClassReadingStaticField",
"./java_bytecode/java_bytecode_convert_method");

WHEN("Converting the method that reads said field")
{
const auto &method_symbol =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⛏️ if the symbol isn't found, using lookup_ref makes the output on CI not very clear, better to use lookup and then REQUIRE(method_symbol != nullptr)

symbol_table.lookup_ref("java::ClassReadingStaticField.test:()I");

THEN(
"There should be a symbol expression attributed to bytecode index "
"0 (the getstatic instruction)")
{
bool found = false;
method_symbol.value.visit_pre([&found](const exprt &subexpr) {
if(
const auto symbol_expr =
expr_try_dynamic_cast<symbol_exprt>(subexpr))
{
if(
symbol_expr->source_location().get_java_bytecode_index() == "0" &&
symbol_expr->get_identifier() ==
"java::ClassReadingStaticField.x")
found = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⛏️ multi line condition so please add braces around the body

}
});

REQUIRE(found);
}
}
}
}

/// Allow access to private methods so that they can be unit tested
class java_bytecode_convert_method_unit_testt
{
Expand Down