-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #460 from mgudemann/java_unwind_enum_static
Java unwind enum static
- Loading branch information
Showing
10 changed files
with
204 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ Author: Daniel Kroening, [email protected] | |
#include <goto-programs/remove_complex.h> | ||
#include <goto-programs/remove_asm.h> | ||
#include <goto-programs/remove_unused_functions.h> | ||
#include <goto-programs/remove_static_init_loops.h> | ||
#include <goto-programs/goto_inline.h> | ||
#include <goto-programs/show_properties.h> | ||
#include <goto-programs/set_properties.h> | ||
|
@@ -221,6 +222,10 @@ void cbmc_parse_optionst::get_command_line_options(optionst &options) | |
// all checks supported by goto_check | ||
GOTO_CHECK_PARSE_OPTIONS(cmdline, options); | ||
|
||
// unwind loops in java enum static initialization | ||
if(cmdline.isset("java-unwind-enum-static")) | ||
options.set_option("java-unwind-enum-static", true); | ||
|
||
// check assertions | ||
if(cmdline.isset("no-assertions")) | ||
options.set_option("assertions", false); | ||
|
@@ -541,6 +546,9 @@ int cbmc_parse_optionst::doit() | |
if(set_properties(goto_functions)) | ||
return 7; // should contemplate EX_USAGE from sysexits.h | ||
|
||
if(options.get_bool_option("java-unwind-enum-static")) | ||
remove_static_init_loops(symbol_table, goto_functions, options); | ||
|
||
// do actual BMC | ||
return do_bmc(bmc, goto_functions); | ||
} | ||
|
@@ -1126,6 +1134,10 @@ void cbmc_parse_optionst::help() | |
"Java Bytecode frontend options:\n" | ||
" --classpath dir/jar set the classpath\n" | ||
" --main-class class-name set the name of the main class\n" | ||
// NOLINTNEXTLINE(whitespace/line_length) | ||
" --java-max-vla-length limit the length of user-code-created arrays\n" | ||
// NOLINTNEXTLINE(whitespace/line_length) | ||
" --java-unwind-enum-static try to unwind loops in static initialization of enums\n" | ||
"\n" | ||
"Semantic transformations:\n" | ||
" --nondet-static add nondeterministic initialization of variables with static lifetime\n" // NOLINT(*) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/*******************************************************************\ | ||
Module: Unwind loops in static initializers | ||
Author: Daniel Kroening, [email protected] | ||
\*******************************************************************/ | ||
|
||
#include <util/message.h> | ||
#include <util/string2int.h> | ||
|
||
#include "remove_static_init_loops.h" | ||
|
||
class remove_static_init_loopst | ||
{ | ||
public: | ||
explicit remove_static_init_loopst( | ||
const symbol_tablet &_symbol_table): | ||
symbol_table(_symbol_table) | ||
{} | ||
|
||
void unwind_enum_static( | ||
const goto_functionst &goto_functions, | ||
optionst &options); | ||
protected: | ||
const symbol_tablet &symbol_table; | ||
}; | ||
|
||
/*******************************************************************\ | ||
Function: unwind_enum_static | ||
Inputs: goto_functions and options | ||
Outputs: side effect is adding <clinit> loops to unwindset | ||
Purpose: unwind static initialization loops of Java enums as far as | ||
the enum has elements, thus flattening them completely | ||
\*******************************************************************/ | ||
|
||
void remove_static_init_loopst::unwind_enum_static( | ||
const goto_functionst &goto_functions, | ||
optionst &options) | ||
{ | ||
size_t unwind_max=0; | ||
forall_goto_functions(f, goto_functions) | ||
{ | ||
auto &p=f->second.body; | ||
for(const auto &ins : p.instructions) | ||
{ | ||
// is this a loop? | ||
if(ins.is_backwards_goto()) | ||
{ | ||
size_t loop_id=ins.loop_number; | ||
const std::string java_clinit="<clinit>:()V"; | ||
const std::string &fname=id2string(ins.function); | ||
size_t class_prefix_length=fname.find_last_of('.'); | ||
assert( | ||
class_prefix_length!=std::string::npos && | ||
"could not identify class name"); | ||
const std::string &classname=fname.substr(0, class_prefix_length); | ||
const symbolt &class_symbol=symbol_table.lookup(classname); | ||
const class_typet &class_type=to_class_type(class_symbol.type); | ||
size_t unwinds=class_type.get_size_t(ID_java_enum_static_unwind); | ||
|
||
unwind_max=std::max(unwind_max, unwinds); | ||
if(fname.length()>java_clinit.length()) | ||
{ | ||
// extend unwindset with unwinds for <clinit> of enum | ||
if(fname.compare( | ||
fname.length()-java_clinit.length(), | ||
java_clinit.length(), | ||
java_clinit)==0 && unwinds>0) | ||
{ | ||
const std::string &set=options.get_option("unwindset"); | ||
std::string newset; | ||
if(set!="") | ||
newset=","; | ||
newset+= | ||
id2string(ins.function)+"."+std::to_string(loop_id)+":"+ | ||
std::to_string(unwinds); | ||
options.set_option("unwindset", set+newset); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
const std::string &vla_length=options.get_option("java-max-vla-length"); | ||
if(!vla_length.empty()) | ||
{ | ||
size_t max_vla_length=safe_string2unsigned(vla_length); | ||
if(max_vla_length<unwind_max) | ||
throw "cannot unwind <clinit> due to insufficient max vla length"; | ||
} | ||
} | ||
|
||
/*******************************************************************\ | ||
Function: remove_static_init_loops | ||
Inputs: symbol table, goto_functions and options | ||
Outputs: side effect is adding <clinit> loops to unwindset | ||
Purpose: this is the entry point for the removal of loops in static | ||
initialization code of Java enums | ||
\*******************************************************************/ | ||
|
||
void remove_static_init_loops( | ||
const symbol_tablet &symbol_table, | ||
const goto_functionst &goto_functions, | ||
optionst &options) | ||
{ | ||
remove_static_init_loopst remove_loops(symbol_table); | ||
remove_loops.unwind_enum_static(goto_functions, options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/*******************************************************************\ | ||
Module: Unwind loops in static initializers | ||
Author: Daniel Kroening, [email protected] | ||
\*******************************************************************/ | ||
|
||
#include <goto-programs/goto_functions.h> | ||
|
||
#include <util/options.h> | ||
#include <util/symbol_table.h> | ||
|
||
#ifndef CPROVER_GOTO_PROGRAMS_REMOVE_STATIC_INIT_LOOPS_H | ||
#define CPROVER_GOTO_PROGRAMS_REMOVE_STATIC_INIT_LOOPS_H | ||
|
||
void remove_static_init_loops( | ||
const symbol_tablet &, | ||
const goto_functionst &, | ||
optionst &); | ||
|
||
#endif // CPROVER_GOTO_PROGRAMS_REMOVE_STATIC_INIT_LOOPS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -736,3 +736,4 @@ bswap | |
java_bytecode_index | ||
java_instanceof | ||
java_super_method_call | ||
java_enum_static_unwind |