Skip to content

Commit 71b32f4

Browse files
Merge pull request #1777 from romainbrenguier/refactor/java-bytecode-instrument-TG-2331
Refactor in java bytecode instrument [TG-2331]
2 parents 83d9272 + 7e176f7 commit 71b32f4

File tree

2 files changed

+37
-48
lines changed

2 files changed

+37
-48
lines changed

src/java_bytecode/java_bytecode_instrument.cpp

+36-47
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@ Date: June 2017
1414
#include <util/fresh_symbol.h>
1515
#include <util/std_code.h>
1616
#include <util/std_expr.h>
17-
#include <util/symbol_table.h>
1817
#include <util/c_types.h>
1918

2019
#include <goto-programs/goto_functions.h>
2120

2221
#include "java_bytecode_convert_class.h"
23-
#include "java_entry_point.h"
24-
#include "java_root_class.h"
25-
#include "java_types.h"
2622
#include "java_utils.h"
2723

2824
class java_bytecode_instrumentt:public messaget
@@ -39,7 +35,7 @@ class java_bytecode_instrumentt:public messaget
3935
{
4036
}
4137

42-
void operator()(exprt &expr);
38+
void operator()(codet &code);
4339

4440
protected:
4541
symbol_table_baset &symbol_table;
@@ -73,29 +69,26 @@ class java_bytecode_instrumentt:public messaget
7369
const exprt &length,
7470
const source_locationt &original_loc);
7571

76-
void instrument_code(exprt &expr);
72+
void instrument_code(codet &code);
7773
void add_expr_instrumentation(code_blockt &block, const exprt &expr);
7874
void prepend_instrumentation(codet &code, code_blockt &instrumentation);
79-
codet instrument_expr(const exprt &expr);
75+
optionalt<codet> instrument_expr(const exprt &expr);
8076
};
8177

82-
83-
const std::vector<irep_idt> exception_needed_classes =
84-
{
78+
const std::vector<std::string> exception_needed_classes = { // NOLINT
8579
"java.lang.ArithmeticException",
8680
"java.lang.ArrayIndexOutOfBoundsException",
8781
"java.lang.ClassCastException",
8882
"java.lang.NegativeArraySizeException",
89-
"java.lang.NullPointerException"
90-
};
83+
"java.lang.NullPointerException"};
9184

9285
/// Creates a class stub for exc_name and generates a
9386
/// conditional GOTO such that exc_name is thrown when
9487
/// cond is met.
95-
/// \par parameters: `cond`: condition to be met in order
88+
/// \param cond: condition to be met in order
9689
/// to throw an exception
97-
/// `original_loc`: source location in the original program
98-
/// `exc_name`: the name of the exception to be thrown
90+
/// \param original_loc: source location in the original program
91+
/// \param exc_name: the name of the exception to be thrown
9992
/// \return Returns the code initialising the throwing the
10093
/// exception
10194
codet java_bytecode_instrumentt::throw_exception(
@@ -178,9 +171,9 @@ codet java_bytecode_instrumentt::check_arithmetic_exception(
178171
/// and throws ArrayIndexOutofBoundsException/generates an assertion
179172
/// if necessary; Exceptions are thrown when the `throw_runtime_exceptions`
180173
/// flag is set. Otherwise, assertions are emitted.
181-
/// \par parameters: `array_struct`: the array being accessed
182-
/// `idx`: index into the array
183-
/// `original_loc: source location in the original code
174+
/// \param array_struct: array being accessed
175+
/// \param idx: index into the array
176+
/// \param original_loc: source location in the original code
184177
/// \return Based on the value of the flag `throw_runtime_exceptions`,
185178
/// it returns code that either throws an ArrayIndexOutPfBoundsException
186179
/// or emits an assertion checking the array access
@@ -223,9 +216,9 @@ codet java_bytecode_instrumentt::check_array_access(
223216
/// ClassCastException/generates an assertion when necessary;
224217
/// Exceptions are thrown when the `throw_runtime_exceptions` flag is set.
225218
/// Otherwise, assertions are emitted.
226-
/// \par parameters: `class1`: the subclass
227-
/// `class2`: the super class
228-
/// `original_loc: source location in the original code
219+
/// \param class1: the subclass
220+
/// \param class2: the super class
221+
/// \param original_loc: source location in the original code
229222
/// \return Based on the value of the flag `throw_runtime_exceptions`,
230223
/// it returns code that either throws an ClassCastException or emits an
231224
/// assertion checking the subtype relation
@@ -272,7 +265,7 @@ codet java_bytecode_instrumentt::check_class_cast(
272265
/// generates an assertion when necessary;
273266
/// Exceptions are thrown when the `throw_runtime_exceptions` flag is set.
274267
/// Otherwise, assertions are emitted.
275-
/// \par parameters: `expr`: the checked expression
268+
/// \param expr: the checked expression
276269
/// `original_loc: source location in the original code
277270
/// \return Based on the value of the flag `throw_runtime_exceptions`,
278271
/// it returns code that either throws an NullPointerException or emits an
@@ -302,8 +295,8 @@ codet java_bytecode_instrumentt::check_null_dereference(
302295
/// generates an assertion when necessary;
303296
/// Exceptions are thrown when the `throw_runtime_exceptions` flag is set.
304297
/// Otherwise, assertions are emitted.
305-
/// \par parameters: `length`: the checked length
306-
/// `original_loc: source location in the original code
298+
/// \param length: the checked length
299+
/// \param original_loc: source location in the original code
307300
/// \return Based on the value of the flag `throw_runtime_exceptions`,
308301
/// it returns code that either throws an NegativeArraySizeException
309302
/// or emits an assertion checking the subtype relation
@@ -335,13 +328,12 @@ void java_bytecode_instrumentt::add_expr_instrumentation(
335328
code_blockt &block,
336329
const exprt &expr)
337330
{
338-
codet expr_instrumentation=instrument_expr(expr);
339-
if(expr_instrumentation!=code_skipt())
331+
if(optionalt<codet> expr_instrumentation = instrument_expr(expr))
340332
{
341-
if(expr_instrumentation.get_statement()==ID_block)
342-
block.append(to_code_block(expr_instrumentation));
333+
if(expr_instrumentation->get_statement() == ID_block)
334+
block.append(to_code_block(*expr_instrumentation));
343335
else
344-
block.move_to_operands(expr_instrumentation);
336+
block.move_to_operands(*expr_instrumentation);
345337
}
346338
}
347339

@@ -365,10 +357,9 @@ void java_bytecode_instrumentt::prepend_instrumentation(
365357

366358
/// Augments `expr` with instrumentation in the form of either
367359
/// assertions or runtime exceptions
368-
/// \par parameters: `expr`: the expression to be instrumented
369-
void java_bytecode_instrumentt::instrument_code(exprt &expr)
360+
/// \param `expr` the expression to be instrumented
361+
void java_bytecode_instrumentt::instrument_code(codet &code)
370362
{
371-
codet &code=to_code(expr);
372363
source_locationt old_source_location=code.source_location();
373364

374365
const irep_idt &statement=code.get_statement();
@@ -480,19 +471,17 @@ void java_bytecode_instrumentt::instrument_code(exprt &expr)
480471

481472
/// Computes the instrumentation for `expr` in the form of
482473
/// either assertions or runtime exceptions.
483-
/// \par parameters: `expr`: the expression for which we compute
474+
/// \param expr: the expression for which we compute
484475
/// instrumentation
485-
/// \return: The instrumentation required for `expr`
486-
codet java_bytecode_instrumentt::instrument_expr(
487-
const exprt &expr)
476+
/// \return: The instrumentation for `expr` if required
477+
optionalt<codet> java_bytecode_instrumentt::instrument_expr(const exprt &expr)
488478
{
489479
code_blockt result;
490480
// First check our operands:
491481
forall_operands(it, expr)
492482
{
493-
codet op_result=instrument_expr(*it);
494-
if(op_result!=code_skipt())
495-
result.move_to_operands(op_result);
483+
if(optionalt<codet> op_result = instrument_expr(*it))
484+
result.move_to_operands(*op_result);
496485
}
497486

498487
// Add any check due at this node:
@@ -524,7 +513,7 @@ codet java_bytecode_instrumentt::instrument_expr(
524513
const irep_idt &statement=side_effect_expr.get_statement();
525514
if(statement==ID_throw)
526515
{
527-
// this corresponds to athrow and so we check that
516+
// this corresponds to a throw and so we check that
528517
// we don't throw null
529518
result.copy_to_operands(
530519
check_null_dereference(
@@ -533,7 +522,7 @@ codet java_bytecode_instrumentt::instrument_expr(
533522
}
534523
else if(statement==ID_java_new_array)
535524
{
536-
// this correpond to new array so we check that
525+
// this corresponds to new array so we check that
537526
// length is >=0
538527
result.copy_to_operands(
539528
check_array_length(
@@ -563,16 +552,16 @@ codet java_bytecode_instrumentt::instrument_expr(
563552
}
564553

565554
if(result==code_blockt())
566-
return code_skipt();
555+
return {};
567556
else
568557
return result;
569558
}
570559

571560
/// Instruments `expr`
572-
/// \par parameters: `expr`: the expression to be instrumented
573-
void java_bytecode_instrumentt::operator()(exprt &expr)
561+
/// \param expr: the expression to be instrumented
562+
void java_bytecode_instrumentt::operator()(codet &code)
574563
{
575-
instrument_code(expr);
564+
instrument_code(code);
576565
}
577566

578567
/// Instruments the code attached to `symbol` with
@@ -601,7 +590,7 @@ void java_bytecode_instrument_symbol(
601590
"java_bytecode_instrument expects a code-typed symbol but was called with"
602591
" " + id2string(symbol.name) + " which has a value with an id of " +
603592
id2string(symbol.value.id()));
604-
instrument(symbol.value);
593+
instrument(to_code(symbol.value));
605594
}
606595

607596
/// Instruments all the code in the symbol_table with
@@ -634,5 +623,5 @@ void java_bytecode_instrument(
634623
// instrument(...) can add symbols to the table, so it's
635624
// not safe to hold a reference to a symbol across a call.
636625
for(const auto &symbol : symbols_to_instrument)
637-
instrument(symbol_table.get_writeable_ref(symbol).value);
626+
instrument(to_code(symbol_table.get_writeable_ref(symbol).value));
638627
}

src/java_bytecode/java_bytecode_instrument.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ void java_bytecode_instrument(
2626
const bool throw_runtime_exceptions,
2727
message_handlert &_message_handler);
2828

29-
extern const std::vector<irep_idt> exception_needed_classes;
29+
extern const std::vector<std::string> exception_needed_classes;
3030

3131
#endif

0 commit comments

Comments
 (0)