cprover
remove_java_new.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Remove Java New Operators
4 
5 Author: Peter Schrammel
6 
7 \*******************************************************************/
8 
11 
12 #include "remove_java_new.h"
13 
16 
17 #include "java_utils.h"
18 #include <util/arith_tools.h>
19 #include <util/c_types.h>
20 #include <util/expr_cast.h>
21 #include <util/expr_initializer.h>
22 #include <util/message.h>
24 
26 {
27 public:
30  {
31  }
32 
33  // Lower java_new for a single function
34  bool lower_java_new(
35  const irep_idt &function_identifier,
36  goto_programt &,
38 
39  // Lower java_new for a single instruction
41  const irep_idt &function_identifier,
42  goto_programt &,
45 
46 protected:
49 
51  const irep_idt &function_identifier,
52  const exprt &lhs,
53  const side_effect_exprt &rhs,
54  goto_programt &,
56 
58  const irep_idt &function_identifier,
59  const exprt &lhs,
60  const side_effect_exprt &rhs,
61  goto_programt &,
64 };
65 
79  const irep_idt &function_identifier,
80  const exprt &lhs,
81  const side_effect_exprt &rhs,
82  goto_programt &dest,
84 {
85  PRECONDITION(!lhs.is_nil());
86  PRECONDITION(rhs.operands().empty());
87  PRECONDITION(rhs.type().id() == ID_pointer);
88  source_locationt location = rhs.source_location();
89  typet object_type = rhs.type().subtype();
90 
91  // build size expression
92  const auto object_size = size_of_expr(object_type, ns);
93  CHECK_RETURN(object_size.has_value());
94 
95  // we produce a malloc side-effect, which stays
96  side_effect_exprt malloc_expr(ID_allocate, rhs.type(), location);
97  malloc_expr.copy_to_operands(*object_size);
98  // could use true and get rid of the code below
99  malloc_expr.copy_to_operands(false_exprt());
100  *target =
101  goto_programt::make_assignment(code_assignt(lhs, malloc_expr), location);
102 
103  // zero-initialize the object
104  dereference_exprt deref(lhs, object_type);
105  auto zero_object = zero_initializer(object_type, location, ns);
106  CHECK_RETURN(zero_object.has_value());
108  to_struct_expr(*zero_object), ns, to_struct_tag_type(object_type));
109  return dest.insert_after(
110  target,
112  code_assignt(deref, *zero_object), location));
113 }
114 
129  const irep_idt &function_identifier,
130  const exprt &lhs,
131  const side_effect_exprt &rhs,
132  goto_programt &dest,
133  goto_programt::targett target,
134  message_handlert &message_handler)
135 {
136  // lower_java_new_array without lhs not implemented
137  PRECONDITION(!lhs.is_nil());
138  PRECONDITION(rhs.operands().size() >= 1); // one per dimension
139  PRECONDITION(rhs.type().id() == ID_pointer);
140 
141  source_locationt location = rhs.source_location();
142  struct_tag_typet object_type = to_struct_tag_type(rhs.type().subtype());
143  PRECONDITION(ns.follow(object_type).id() == ID_struct);
144 
145  // build size expression
146  const auto object_size = size_of_expr(object_type, ns);
147  CHECK_RETURN(object_size.has_value());
148 
149  // we produce a malloc side-effect, which stays
150  side_effect_exprt malloc_expr(ID_allocate, rhs.type(), location);
151  malloc_expr.copy_to_operands(*object_size);
152  // code use true and get rid of the code below
153  malloc_expr.copy_to_operands(false_exprt());
154 
155  *target =
156  goto_programt::make_assignment(code_assignt(lhs, malloc_expr), location);
157  goto_programt::targett next = std::next(target);
158 
159  const struct_typet &struct_type = to_struct_type(ns.follow(object_type));
160 
161  PRECONDITION(is_valid_java_array(struct_type));
162 
163  // Init base class:
164  dereference_exprt deref(lhs, object_type);
165  auto zero_object = zero_initializer(object_type, location, ns);
166  CHECK_RETURN(zero_object.has_value());
167  set_class_identifier(to_struct_expr(*zero_object), ns, object_type);
168  dest.insert_before(
169  next,
171  code_assignt(deref, *zero_object), location));
172 
173  // If it's a reference array we need to set the dimension and element type
174  // fields. Primitive array types don't have these fields; if the element type
175  // is a void pointer then the element type is statically unknown and the
176  // caller must set these up itself. This happens in array[reference].clone(),
177  // where the type info must be copied over from the input array)
178  const auto underlying_type_and_dimension =
180 
181  bool target_type_is_reference_array =
182  underlying_type_and_dimension.second >= 1 &&
183  can_cast_type<java_reference_typet>(underlying_type_and_dimension.first);
184 
185  if(target_type_is_reference_array)
186  {
187  exprt object_array_dimension = get_array_dimension_field(lhs);
188  dest.insert_before(
189  next,
191  object_array_dimension,
192  from_integer(
193  underlying_type_and_dimension.second, object_array_dimension.type()),
194  location)));
195 
196  exprt object_array_element_type = get_array_element_type_field(lhs);
197  dest.insert_before(
198  next,
200  object_array_element_type,
202  to_struct_tag_type(underlying_type_and_dimension.first.subtype())
203  .get_identifier(),
204  string_typet()),
205  location)));
206  }
207 
208  // if it's an array, we need to set the length field
209  member_exprt length(
210  deref,
211  struct_type.components()[1].get_name(),
212  struct_type.components()[1].type());
213  dest.insert_before(
214  next,
216  code_assignt(length, to_multi_ary_expr(rhs).op0()), location));
217 
218  // we also need to allocate space for the data
220  deref,
221  struct_type.components()[2].get_name(),
222  struct_type.components()[2].type());
223 
224  // Allocate a (struct realtype**) instead of a (void**) if possible.
225  const irept &given_element_type = object_type.find(ID_element_type);
226  typet allocate_data_type;
227  if(given_element_type.is_not_nil())
228  {
229  allocate_data_type =
230  pointer_type(static_cast<const typet &>(given_element_type));
231  }
232  else
233  allocate_data_type = data.type();
234 
235  side_effect_exprt data_java_new_expr(
236  ID_java_new_array_data, allocate_data_type, location);
237 
238  // The instruction may specify a (hopefully small) upper bound on the
239  // array size, in which case we allocate a fixed-length array that may
240  // be larger than the `length` member rather than use a true variable-
241  // length array, which produces a more complex formula in the current
242  // backend.
243  const irept size_bound = rhs.find(ID_length_upper_bound);
244  if(size_bound.is_nil())
245  data_java_new_expr.set(ID_size, to_multi_ary_expr(rhs).op0());
246  else
247  data_java_new_expr.set(ID_size, size_bound);
248 
249  // Must directly assign the new array to a temporary
250  // because goto-symex will notice `x=side_effect_exprt` but not
251  // `x=typecast_exprt(side_effect_exprt(...))`
252  symbol_exprt new_array_data_symbol = fresh_java_symbol(
253  data_java_new_expr.type(),
254  "tmp_new_data_array",
255  location,
256  function_identifier,
257  symbol_table)
258  .symbol_expr();
259  code_declt array_decl(new_array_data_symbol);
260  array_decl.add_source_location() = location;
261  dest.insert_before(next, goto_programt::make_decl(array_decl, location));
262  dest.insert_before(
263  next,
265  code_assignt(new_array_data_symbol, data_java_new_expr), location));
266 
267  exprt cast_java_new = new_array_data_symbol;
268  if(cast_java_new.type() != data.type())
269  cast_java_new = typecast_exprt(cast_java_new, data.type());
270  dest.insert_before(
271  next,
273  code_assignt(data, cast_java_new), location));
274 
275  // zero-initialize the data
276  if(!rhs.get_bool(ID_skip_initialize))
277  {
278  const auto zero_element =
279  zero_initializer(data.type().subtype(), location, ns);
280  CHECK_RETURN(zero_element.has_value());
281  codet array_set(ID_array_set);
282  array_set.copy_to_operands(new_array_data_symbol, *zero_element);
283  dest.insert_before(next, goto_programt::make_other(array_set, location));
284  }
285 
286  // multi-dimensional?
287 
288  if(rhs.operands().size() >= 2)
289  {
290  // produce
291  // for(int i=0; i<size; i++) tmp[i]=java_new(dim-1);
292  // This will be converted recursively.
293 
294  goto_programt tmp;
295 
296  symbol_exprt tmp_i =
298  length.type(), "tmp_index", location, function_identifier, symbol_table)
299  .symbol_expr();
300  code_declt decl(tmp_i);
301  decl.add_source_location() = location;
302  tmp.insert_before(
303  tmp.instructions.begin(), goto_programt::make_decl(decl, location));
304 
305  side_effect_exprt sub_java_new = rhs;
306  sub_java_new.operands().erase(sub_java_new.operands().begin());
307 
308  // we already know that rhs has pointer type
309  typet sub_type =
310  static_cast<const typet &>(rhs.type().subtype().find(ID_element_type));
311  CHECK_RETURN(sub_type.id() == ID_pointer);
312  sub_java_new.type() = sub_type;
313 
314  plus_exprt(tmp_i, from_integer(1, tmp_i.type()));
315  dereference_exprt deref_expr(
316  plus_exprt(data, tmp_i), data.type().subtype());
317 
318  code_blockt for_body;
319  symbol_exprt init_sym =
321  sub_type, "subarray_init", location, function_identifier, symbol_table)
322  .symbol_expr();
323  code_declt init_decl(init_sym);
324  init_decl.add_source_location() = location;
325  for_body.add(std::move(init_decl));
326 
327  code_assignt init_subarray(init_sym, sub_java_new);
328  for_body.add(std::move(init_subarray));
329  code_assignt assign_subarray(
330  deref_expr, typecast_exprt(init_sym, deref_expr.type()));
331  for_body.add(std::move(assign_subarray));
332 
333  const code_fort for_loop = code_fort::from_index_bounds(
334  from_integer(0, tmp_i.type()),
335  to_multi_ary_expr(rhs).op0(),
336  tmp_i,
337  std::move(for_body),
338  location);
339 
340  goto_convert(for_loop, symbol_table, tmp, message_handler, ID_java);
341 
342  // lower new side effects recursively
343  lower_java_new(function_identifier, tmp, message_handler);
344 
345  dest.destructive_insert(next, tmp);
346  }
347 
348  return std::prev(next);
349 }
350 
359  const irep_idt &function_identifier,
360  goto_programt &goto_program,
361  goto_programt::targett target,
362  message_handlert &message_handler)
363 {
364  if(!target->is_assign())
365  return target;
366 
367  if(as_const(*target).get_assign().rhs().id() == ID_side_effect)
368  {
369  // we make a copy, as we intend to destroy the assignment
370  // inside lower_java_new and lower_java_new_array
371  const auto code_assign = target->get_assign();
372 
373  const exprt &lhs = code_assign.lhs();
374  const exprt &rhs = code_assign.rhs();
375 
376  const auto &side_effect_expr = to_side_effect_expr(rhs);
377  const auto &statement = side_effect_expr.get_statement();
378 
379  if(statement == ID_java_new)
380  {
381  return lower_java_new(
382  function_identifier, lhs, side_effect_expr, goto_program, target);
383  }
384  else if(statement == ID_java_new_array)
385  {
386  return lower_java_new_array(
387  function_identifier,
388  lhs,
389  side_effect_expr,
390  goto_program,
391  target,
392  message_handler);
393  }
394  }
395 
396  return target;
397 }
398 
407  const irep_idt &function_identifier,
408  goto_programt &goto_program,
409  message_handlert &message_handler)
410 {
411  bool changed = false;
412  for(goto_programt::instructionst::iterator target =
413  goto_program.instructions.begin();
414  target != goto_program.instructions.end();
415  ++target)
416  {
418  function_identifier, goto_program, target, message_handler);
419  changed = changed || new_target == target;
420  target = new_target;
421  }
422  if(!changed)
423  return false;
424  goto_program.update();
425  return true;
426 }
427 
437  const irep_idt &function_identifier,
438  goto_programt::targett target,
439  goto_programt &goto_program,
440  symbol_table_baset &symbol_table,
441  message_handlert &message_handler)
442 {
443  remove_java_newt rem{symbol_table};
444  rem.lower_java_new(
445  function_identifier, goto_program, target, message_handler);
446 }
447 
456  const irep_idt &function_identifier,
458  symbol_table_baset &symbol_table,
459  message_handlert &message_handler)
460 {
461  remove_java_newt rem{symbol_table};
462  rem.lower_java_new(function_identifier, function.body, message_handler);
463 }
464 
472  goto_functionst &goto_functions,
473  symbol_table_baset &symbol_table,
474  message_handlert &message_handler)
475 {
476  remove_java_newt rem{symbol_table};
477  bool changed = false;
478  for(auto &f : goto_functions.function_map)
479  changed =
480  rem.lower_java_new(f.first, f.second.body, message_handler) || changed;
481  if(changed)
482  goto_functions.compute_location_numbers();
483 }
484 
491 void remove_java_new(goto_modelt &goto_model, message_handlert &message_handler)
492 {
494  goto_model.goto_functions, goto_model.symbol_table, message_handler);
495 }
exprt::copy_to_operands
void copy_to_operands(const exprt &expr)
Copy the given argument to the end of exprt's operands.
Definition: expr.h:133
tag_typet::get_identifier
const irep_idt & get_identifier() const
Definition: std_types.h:459
struct_union_typet::components
const componentst & components() const
Definition: std_types.h:142
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
pointer_offset_size.h
Pointer Logic.
can_cast_type< java_reference_typet >
bool can_cast_type< java_reference_typet >(const typet &type)
Definition: java_types.h:626
code_blockt
A codet representing sequential composition of program statements.
Definition: std_code.h:170
typet::subtype
const typet & subtype() const
Definition: type.h:47
arith_tools.h
to_struct_type
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition: std_types.h:303
get_array_dimension_field
exprt get_array_dimension_field(const exprt &pointer)
Definition: java_types.cpp:205
remove_java_newt::lower_java_new_array
goto_programt::targett lower_java_new_array(const irep_idt &function_identifier, const exprt &lhs, const side_effect_exprt &rhs, goto_programt &, goto_programt::targett, message_handlert &)
Replaces the instruction lhs = new java_array_type by the following code: lhs = ALLOCATE(java_type) l...
Definition: remove_java_new.cpp:128
code_fort
codet representation of a for statement.
Definition: std_code.h:1052
CHECK_RETURN
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:496
typet
The type of an expression, extends irept.
Definition: type.h:28
dereference_exprt
Operator to dereference a pointer.
Definition: pointer_expr.h:256
data
Definition: kdev_t.h:24
irept::find
const irept & find(const irep_namet &name) const
Definition: irep.cpp:103
goto_programt::update
void update()
Update all indices.
Definition: goto_program.cpp:540
code_declt
A codet representing the declaration of a local variable.
Definition: std_code.h:402
plus_exprt
The plus expression Associativity is not specified.
Definition: std_expr.h:831
goto_functionst::compute_location_numbers
void compute_location_numbers()
Definition: goto_functions.cpp:18
is_valid_java_array
bool is_valid_java_array(const struct_typet &type)
Programmatic documentation of the structure of a Java array (of either primitives or references) type...
Definition: java_types.cpp:834
exprt
Base class for all expressions.
Definition: expr.h:54
goto_modelt
Definition: goto_model.h:26
struct_tag_typet
A struct tag type, i.e., struct_typet with an identifier.
Definition: std_types.h:498
remove_java_newt::remove_java_newt
remove_java_newt(symbol_table_baset &symbol_table)
Definition: remove_java_new.cpp:28
to_side_effect_expr
side_effect_exprt & to_side_effect_expr(exprt &expr)
Definition: std_code.h:1954
goto_convert.h
Program Transformation.
goto_functionst::function_map
function_mapt function_map
Definition: goto_functions.h:27
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:81
goto_programt::make_decl
static instructiont make_decl(const symbol_exprt &symbol, const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:891
remove_java_newt::symbol_table
symbol_table_baset & symbol_table
Definition: remove_java_new.cpp:47
goto_programt::make_assignment
static instructiont make_assignment(const code_assignt &_code, const source_locationt &l=source_locationt::nil())
Create an assignment instruction.
Definition: goto_program.h:995
zero_initializer
optionalt< exprt > zero_initializer(const typet &type, const source_locationt &source_location, const namespacet &ns)
Create the equivalent of zero for type type.
Definition: expr_initializer.cpp:318
goto_convert
void goto_convert(const codet &code, symbol_table_baset &symbol_table, goto_programt &dest, message_handlert &message_handler, const irep_idt &mode)
Definition: goto_convert.cpp:1863
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:92
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:82
irept::get_bool
bool get_bool(const irep_namet &name) const
Definition: irep.cpp:64
irept::is_not_nil
bool is_not_nil() const
Definition: irep.h:391
goto_programt::insert_before
targett insert_before(const_targett target)
Insertion before the instruction pointed-to by the given instruction iterator target.
Definition: goto_program.h:610
remove_java_newt::ns
namespacet ns
Definition: remove_java_new.cpp:48
set_class_identifier
void set_class_identifier(struct_exprt &expr, const namespacet &ns, const struct_tag_typet &class_type)
If expr has its components filled in then sets the @class_identifier member of the struct.
Definition: class_identifier.cpp:83
expr_initializer.h
Expression Initialization.
as_const
const T & as_const(T &value)
Return a reference to the same object but ensures the type is const.
Definition: as_const.h:14
goto_programt::destructive_insert
void destructive_insert(const_targett target, goto_programt &p)
Inserts the given program p before target.
Definition: goto_program.h:648
PRECONDITION
#define PRECONDITION(CONDITION)
Definition: invariant.h:464
symbol_table_baset
The symbol table base class interface.
Definition: symbol_table_base.h:22
symbolt::symbol_expr
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
Definition: symbol.cpp:122
pointer_type
pointer_typet pointer_type(const typet &subtype)
Definition: c_types.cpp:243
object_size
exprt object_size(const exprt &pointer)
Definition: pointer_predicates.cpp:32
irept::is_nil
bool is_nil() const
Definition: irep.h:387
irept::id
const irep_idt & id() const
Definition: irep.h:407
message_handlert
Definition: message.h:28
to_struct_tag_type
const struct_tag_typet & to_struct_tag_type(const typet &type)
Cast a typet to a struct_tag_typet.
Definition: std_types.h:523
code_blockt::add
void add(const codet &code)
Definition: std_code.h:208
false_exprt
The Boolean constant false.
Definition: std_expr.h:2726
remove_java_new
void remove_java_new(const irep_idt &function_identifier, goto_programt::targett target, goto_programt &goto_program, symbol_table_baset &symbol_table, message_handlert &message_handler)
Replace every java_new or java_new_array by a malloc side-effect and zero initialization.
Definition: remove_java_new.cpp:436
fresh_java_symbol
symbolt & fresh_java_symbol(const typet &type, const std::string &basename_prefix, const source_locationt &source_location, const irep_idt &function_name, symbol_table_baset &symbol_table)
Definition: java_utils.cpp:558
source_locationt
Definition: source_location.h:20
goto_functionst::goto_functiont
::goto_functiont goto_functiont
Definition: goto_functions.h:25
member_exprt
Extract member of struct or union.
Definition: std_expr.h:2528
goto_programt::instructions
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:556
goto_functionst
A collection of goto functions.
Definition: goto_functions.h:23
struct_typet
Structure type, corresponds to C style structs.
Definition: std_types.h:226
goto_modelt::goto_functions
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:33
namespace_baset::follow
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:51
irept::set
void set(const irep_namet &name, const irep_idt &value)
Definition: irep.h:431
from_integer
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
Definition: arith_tools.cpp:99
goto_programt::make_other
static instructiont make_other(const codet &_code, const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:884
string_typet
String type.
Definition: std_types.h:1669
expr_cast.h
Templated functions to cast to specific exprt-derived classes.
goto_programt
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:74
class_identifier.h
Extract class identifier.
code_fort::from_index_bounds
static code_fort from_index_bounds(exprt start_index, exprt end_index, symbol_exprt loop_index, codet body, source_locationt location)
Produce a code_fort representing:
Definition: std_code.cpp:213
goto_programt::insert_after
targett insert_after(const_targett target)
Insertion after the instruction pointed-to by the given instruction iterator target.
Definition: goto_program.h:626
java_array_dimension_and_element_type
std::pair< typet, std::size_t > java_array_dimension_and_element_type(const struct_tag_typet &type)
Returns the underlying element type and array dimensionality of Java struct type.
Definition: java_types.cpp:188
size_of_expr
optionalt< exprt > size_of_expr(const typet &type, const namespacet &ns)
Definition: pointer_offset_size.cpp:278
irept
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition: irep.h:383
remove_java_new.h
Remove Java New Operators.
exprt::operands
operandst & operands()
Definition: expr.h:96
exprt::add_source_location
source_locationt & add_source_location()
Definition: expr.h:239
typecast_exprt
Semantic type conversion.
Definition: std_expr.h:1781
get_array_element_type_field
exprt get_array_element_type_field(const exprt &pointer)
Definition: java_types.cpp:217
code_assignt
A codet representing an assignment in the program.
Definition: std_code.h:295
message.h
java_utils.h
constant_exprt
A constant literal expression.
Definition: std_expr.h:2668
to_multi_ary_expr
const multi_ary_exprt & to_multi_ary_expr(const exprt &expr)
Cast an exprt to a multi_ary_exprt.
Definition: std_expr.h:816
exprt::source_location
const source_locationt & source_location() const
Definition: expr.h:234
goto_modelt::symbol_table
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:30
c_types.h
remove_java_newt
Definition: remove_java_new.cpp:26
goto_programt::targett
instructionst::iterator targett
Definition: goto_program.h:550
side_effect_exprt
An expression containing a side effect.
Definition: std_code.h:1898
to_struct_expr
const struct_exprt & to_struct_expr(const exprt &expr)
Cast an exprt to a struct_exprt.
Definition: std_expr.h:1606
remove_java_newt::lower_java_new
bool lower_java_new(const irep_idt &function_identifier, goto_programt &, message_handlert &)
Replace every java_new or java_new_array by a malloc side-effect and zero initialization.
Definition: remove_java_new.cpp:406
codet
Data structure for representing an arbitrary statement in a program.
Definition: std_code.h:35