Skip to content

Commit 4761070

Browse files
committed
Add owning class functions as interface to the owner of a symbol
Without the functions added by this commit, the only way to access the owner of a symbol for static fields and java methods is through `symbol.type.get(ID_C_class)`. When this technique is used by downstream repositories, they are vulnerable to being broken by upstream changes to how this information is stored in JBMC. The intension of this commit is that the two new functions added will be the standard interface to this symbol -> owner link. This means that future changes to how this information is stored can be made without breaking downstream repositories by changing these two functions.
1 parent 535bde0 commit 4761070

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

jbmc/src/java_bytecode/java_utils.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,3 +464,14 @@ symbolt &fresh_java_symbol(
464464
return get_fresh_aux_symbol(
465465
type, name_prefix, basename_prefix, source_location, ID_java, symbol_table);
466466
}
467+
468+
optionalt<irep_idt> owning_class(const symbolt &symbol)
469+
{
470+
const irep_idt &class_id = symbol.type.get(ID_C_class);
471+
return class_id.empty() ? optionalt<irep_idt>{} : class_id;
472+
}
473+
474+
void set_owning_class(symbolt &symbol, const irep_idt &owning_class)
475+
{
476+
symbol.type.set(ID_C_class, owning_class);
477+
}

jbmc/src/java_bytecode/java_utils.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Author: Daniel Kroening, [email protected]
1414
#include <unordered_set>
1515

1616
#include <util/message.h>
17+
#include <util/optional.h>
1718
#include <util/std_expr.h>
1819
#include <util/symbol_table.h>
1920

@@ -115,4 +116,14 @@ symbolt &fresh_java_symbol(
115116
const irep_idt &function_name,
116117
symbol_table_baset &symbol_table);
117118

119+
/// Gets the identifier of the class which owns a given \p symbol. If the symbol
120+
/// is not owned by a class the an empty optional is returned. This is used for
121+
/// method symbols and static field symbols to link them back to the class which
122+
/// owns them.
123+
optionalt<irep_idt> owning_class(const symbolt &symbol);
124+
125+
/// Sets the identifier of the class which owns a given \p symbol to \p
126+
/// owning_class.
127+
void set_owning_class(symbolt &symbol, const irep_idt &owning_class);
128+
118129
#endif // CPROVER_JAVA_BYTECODE_JAVA_UTILS_H

jbmc/unit/java_bytecode/java_utils_test.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,28 @@ SCENARIO("Test pretty printing auxiliary function", "[core][java_util_test]")
368368
}
369369
}
370370
}
371+
372+
SCENARIO("Test symbol ownership.", "[core][java_util_test]")
373+
{
374+
WHEN("We have a new symbol.")
375+
{
376+
symbolt symbol;
377+
378+
THEN("The symbol has no owner.")
379+
{
380+
REQUIRE(!owning_class(symbol).has_value());
381+
}
382+
}
383+
384+
WHEN("The owning class of a symbol is set.")
385+
{
386+
const auto owning_class = "java::java.lang.object";
387+
symbolt symbol;
388+
set_owning_class(symbol, owning_class);
389+
390+
THEN("Getting the owning class of a symbol returns the class set.")
391+
{
392+
REQUIRE(id2string(*::owning_class(symbol)) == owning_class);
393+
}
394+
}
395+
}

0 commit comments

Comments
 (0)