|
| 1 | +// Copyright 2017 Diffblue Limited. All Rights Reserved. |
| 2 | + |
| 3 | +/// \file |
| 4 | +/// Function-level/module-level pass to remove java_new and replace with |
| 5 | +/// malloc & zero-initialize |
| 6 | + |
| 7 | +#include "remove_java_new.h" |
| 8 | + |
| 9 | +#include <util/message.h> |
| 10 | +#include <util/pointer_offset_size.h> |
| 11 | +#include <linking/zero_initializer.h> |
| 12 | + |
| 13 | +#include "class_identifier.h" |
| 14 | + |
| 15 | +static bool remove_java_new( |
| 16 | + goto_programt &goto_program, |
| 17 | + const namespacet &ns, |
| 18 | + message_handlert &message_handler) |
| 19 | +{ |
| 20 | + messaget msg(message_handler); |
| 21 | + |
| 22 | + bool changed=false; |
| 23 | + for(goto_programt::targett target=goto_program.instructions.begin(); |
| 24 | + target!=goto_program.instructions.end(); |
| 25 | + ++target) |
| 26 | + { |
| 27 | + code_assignt *assign=expr_try_dynamic_cast<code_assignt>(target->code); |
| 28 | + if(assign==nullptr) |
| 29 | + continue; |
| 30 | + side_effect_exprt *rhs= |
| 31 | + expr_try_dynamic_cast<side_effect_exprt>(assign->rhs()); |
| 32 | + if(rhs==nullptr) |
| 33 | + continue; |
| 34 | + if(rhs->get_statement()!=ID_java_new) |
| 35 | + continue; |
| 36 | + INVARIANT(rhs->operands().empty(), "java_new does not have operands"); |
| 37 | + INVARIANT(rhs->type().id()==ID_pointer, "java_new returns pointer"); |
| 38 | + |
| 39 | + const exprt &lhs=assign->lhs(); |
| 40 | + INVARIANT( |
| 41 | + !lhs.is_nil(), "remove_java_new without lhs is yet to be implemented"); |
| 42 | + |
| 43 | + typet object_type=rhs->type().subtype(); |
| 44 | + |
| 45 | + // build size expression |
| 46 | + exprt object_size=size_of_expr(object_type, ns); |
| 47 | + CHECK_RETURN(object_size.is_not_nil()); |
| 48 | + |
| 49 | + changed=true; |
| 50 | + |
| 51 | + source_locationt location=rhs->source_location(); |
| 52 | + |
| 53 | + // We produce a malloc side-effect |
| 54 | + side_effect_exprt malloc_expr(ID_allocate); |
| 55 | + malloc_expr.copy_to_operands(object_size); |
| 56 | + // could use true and get rid of the code below |
| 57 | + malloc_expr.copy_to_operands(false_exprt()); |
| 58 | + malloc_expr.type()=rhs->type(); |
| 59 | + *rhs=std::move(malloc_expr); |
| 60 | + |
| 61 | + // zero-initialize the object |
| 62 | + dereference_exprt deref(lhs, object_type); |
| 63 | + exprt zero_object= |
| 64 | + zero_initializer(object_type, location, ns, message_handler); |
| 65 | + set_class_identifier( |
| 66 | + expr_dynamic_cast<struct_exprt>(zero_object), |
| 67 | + ns, |
| 68 | + to_symbol_type(object_type)); |
| 69 | + goto_programt::targett zi_assign=goto_program.insert_after(target); |
| 70 | + zi_assign->make_assignment(); |
| 71 | + zi_assign->code=code_assignt(deref, zero_object); |
| 72 | + zi_assign->source_location=location; |
| 73 | + } |
| 74 | + if(!changed) |
| 75 | + return false; |
| 76 | + goto_program.update(); |
| 77 | + return true; |
| 78 | +} |
| 79 | + |
| 80 | +bool remove_java_new( |
| 81 | + goto_functionst::goto_functiont &goto_function, |
| 82 | + const namespacet &ns, |
| 83 | + message_handlert &message_handler) |
| 84 | +{ |
| 85 | + return remove_java_new(goto_function.body, ns, message_handler); |
| 86 | +} |
| 87 | + |
| 88 | +void remove_java_new( |
| 89 | + goto_functionst &goto_functions, |
| 90 | + const namespacet &ns, |
| 91 | + message_handlert &message_handler) |
| 92 | +{ |
| 93 | + for(auto &named_fn : goto_functions.function_map) |
| 94 | + remove_java_new(named_fn.second, ns, message_handler); |
| 95 | +} |
| 96 | + |
| 97 | +void remove_java_new( |
| 98 | + goto_modelt &goto_model, |
| 99 | + message_handlert &message_handler) |
| 100 | +{ |
| 101 | + remove_java_new( |
| 102 | + goto_model.goto_functions, |
| 103 | + namespacet(goto_model.symbol_table), |
| 104 | + message_handler); |
| 105 | +} |
0 commit comments