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 
17 
18 #include "java_types.h"
19 #include "java_utils.h"
20 
21 #include <util/arith_tools.h>
22 #include <util/c_types.h>
23 #include <util/expr_initializer.h>
25 
27 {
28 public:
31  {
32  }
33 
34  // Lower java_new for a single function
35  bool lower_java_new(
36  const irep_idt &function_identifier,
37  goto_programt &,
39 
40  // Lower java_new for a single instruction
42  const irep_idt &function_identifier,
43  goto_programt &,
46 
47 protected:
50 
52  const irep_idt &function_identifier,
53  const exprt &lhs,
54  const side_effect_exprt &rhs,
55  goto_programt &,
57 
59  const irep_idt &function_identifier,
60  const exprt &lhs,
61  const side_effect_exprt &rhs,
62  goto_programt &,
65 };
66 
80  const irep_idt &function_identifier,
81  const exprt &lhs,
82  const side_effect_exprt &rhs,
83  goto_programt &dest,
85 {
86  PRECONDITION(!lhs.is_nil());
87  PRECONDITION(rhs.operands().empty());
88  PRECONDITION(rhs.type().id() == ID_pointer);
89  source_locationt location = rhs.source_location();
90  typet object_type = rhs.type().subtype();
91 
92  // build size expression
93  const auto object_size = size_of_expr(object_type, ns);
94  CHECK_RETURN(object_size.has_value());
95 
96  // we produce a malloc side-effect, which stays
97  side_effect_exprt malloc_expr(ID_allocate, rhs.type(), location);
98  malloc_expr.copy_to_operands(*object_size);
99  // could use true and get rid of the code below
100  malloc_expr.copy_to_operands(false_exprt());
101  *target =
102  goto_programt::make_assignment(code_assignt(lhs, malloc_expr), location);
103 
104  // zero-initialize the object
105  dereference_exprt deref(lhs, object_type);
106  auto zero_object = zero_initializer(object_type, location, ns);
107  CHECK_RETURN(zero_object.has_value());
109  to_struct_expr(*zero_object), ns, to_struct_tag_type(object_type));
110  return dest.insert_after(
111  target,
113  code_assignt(deref, *zero_object), location));
114 }
115 
130  const irep_idt &function_identifier,
131  const exprt &lhs,
132  const side_effect_exprt &rhs,
133  goto_programt &dest,
134  goto_programt::targett target,
135  message_handlert &message_handler)
136 {
137  // lower_java_new_array without lhs not implemented
138  PRECONDITION(!lhs.is_nil());
139  PRECONDITION(rhs.operands().size() >= 1); // one per dimension
140  PRECONDITION(rhs.type().id() == ID_pointer);
141 
142  source_locationt location = rhs.source_location();
143  struct_tag_typet object_type = to_struct_tag_type(rhs.type().subtype());
144  PRECONDITION(ns.follow(object_type).id() == ID_struct);
145 
146  // build size expression
147  const auto object_size = size_of_expr(object_type, ns);
148  CHECK_RETURN(object_size.has_value());
149 
150  // we produce a malloc side-effect, which stays
151  side_effect_exprt malloc_expr(ID_allocate, rhs.type(), location);
152  malloc_expr.copy_to_operands(*object_size);
153  // code use true and get rid of the code below
154  malloc_expr.copy_to_operands(false_exprt());
155 
156  *target =
157  goto_programt::make_assignment(code_assignt(lhs, malloc_expr), location);
158  goto_programt::targett next = std::next(target);
159 
160  const struct_typet &struct_type = to_struct_type(ns.follow(object_type));
161 
162  PRECONDITION(is_valid_java_array(struct_type));
163 
164  // Init base class:
165  dereference_exprt deref(lhs, object_type);
166  auto zero_object = zero_initializer(object_type, location, ns);
167  CHECK_RETURN(zero_object.has_value());
168  set_class_identifier(to_struct_expr(*zero_object), ns, object_type);
169  dest.insert_before(
170  next,
172  code_assignt(deref, *zero_object), location));
173 
174  // If it's a reference array we need to set the dimension and element type
175  // fields. Primitive array types don't have these fields; if the element type
176  // is a void pointer then the element type is statically unknown and the
177  // caller must set these up itself. This happens in array[reference].clone(),
178  // where the type info must be copied over from the input array)
179  const auto underlying_type_and_dimension =
181 
182  bool target_type_is_reference_array =
183  underlying_type_and_dimension.second >= 1 &&
184  can_cast_type<java_reference_typet>(underlying_type_and_dimension.first);
185 
186  if(target_type_is_reference_array)
187  {
188  exprt object_array_dimension = get_array_dimension_field(lhs);
189  dest.insert_before(
190  next,
192  object_array_dimension,
193  from_integer(
194  underlying_type_and_dimension.second, object_array_dimension.type()),
195  location)));
196 
197  exprt object_array_element_type = get_array_element_type_field(lhs);
198  dest.insert_before(
199  next,
201  object_array_element_type,
203  to_struct_tag_type(underlying_type_and_dimension.first.subtype())
204  .get_identifier(),
205  string_typet()),
206  location)));
207  }
208 
209  // if it's an array, we need to set the length field
210  member_exprt length(
211  deref,
212  struct_type.components()[1].get_name(),
213  struct_type.components()[1].type());
214  dest.insert_before(
215  next,
217  code_assignt(length, to_multi_ary_expr(rhs).op0()), location));
218 
219  // we also need to allocate space for the data
221  deref,
222  struct_type.components()[2].get_name(),
223  struct_type.components()[2].type());
224 
225  // Allocate a (struct realtype**) instead of a (void**) if possible.
226  const irept &given_element_type = object_type.find(ID_element_type);
227  typet allocate_data_type;
228  if(given_element_type.is_not_nil())
229  {
230  allocate_data_type =
231  pointer_type(static_cast<const typet &>(given_element_type));
232  }
233  else
234  allocate_data_type = data.type();
235 
236  side_effect_exprt data_java_new_expr(
237  ID_java_new_array_data, allocate_data_type, location);
238 
239  // The instruction may specify a (hopefully small) upper bound on the
240  // array size, in which case we allocate a fixed-length array that may
241  // be larger than the `length` member rather than use a true variable-
242  // length array, which produces a more complex formula in the current
243  // backend.
244  const irept size_bound = rhs.find(ID_length_upper_bound);
245  if(size_bound.is_nil())
246  data_java_new_expr.set(ID_size, to_multi_ary_expr(rhs).op0());
247  else
248  data_java_new_expr.set(ID_size, size_bound);
249 
250  // Must directly assign the new array to a temporary
251  // because goto-symex will notice `x=side_effect_exprt` but not
252  // `x=typecast_exprt(side_effect_exprt(...))`
253  symbol_exprt new_array_data_symbol = fresh_java_symbol(
254  data_java_new_expr.type(),
255  "tmp_new_data_array",
256  location,
257  function_identifier,
258  symbol_table)
259  .symbol_expr();
260  code_declt array_decl(new_array_data_symbol);
261  array_decl.add_source_location() = location;
262  dest.insert_before(next, goto_programt::make_decl(array_decl, location));
263  dest.insert_before(
264  next,
266  code_assignt(new_array_data_symbol, data_java_new_expr), location));
267 
268  exprt cast_java_new = new_array_data_symbol;
269  if(cast_java_new.type() != data.type())
270  cast_java_new = typecast_exprt(cast_java_new, data.type());
271  dest.insert_before(
272  next,
274  code_assignt(data, cast_java_new), location));
275 
276  // zero-initialize the data
277  if(!rhs.get_bool(ID_skip_initialize))
278  {
279  const auto zero_element =
280  zero_initializer(data.type().subtype(), location, ns);
281  CHECK_RETURN(zero_element.has_value());
282  codet array_set(ID_array_set);
283  array_set.copy_to_operands(new_array_data_symbol, *zero_element);
284  dest.insert_before(next, goto_programt::make_other(array_set, location));
285  }
286 
287  // multi-dimensional?
288 
289  if(rhs.operands().size() >= 2)
290  {
291  // produce
292  // for(int i=0; i<size; i++) tmp[i]=java_new(dim-1);
293  // This will be converted recursively.
294 
295  goto_programt tmp;
296 
297  symbol_exprt tmp_i =
299  length.type(), "tmp_index", location, function_identifier, symbol_table)
300  .symbol_expr();
301  code_declt decl(tmp_i);
302  decl.add_source_location() = location;
303  tmp.insert_before(
304  tmp.instructions.begin(), goto_programt::make_decl(decl, location));
305 
306  side_effect_exprt sub_java_new = rhs;
307  sub_java_new.operands().erase(sub_java_new.operands().begin());
308 
309  // we already know that rhs has pointer type
310  typet sub_type =
311  static_cast<const typet &>(rhs.type().subtype().find(ID_element_type));
312  CHECK_RETURN(sub_type.id() == ID_pointer);
313  sub_java_new.type() = sub_type;
314 
315  plus_exprt(tmp_i, from_integer(1, tmp_i.type()));
316  dereference_exprt deref_expr(
317  plus_exprt(data, tmp_i), data.type().subtype());
318 
319  code_blockt for_body;
320  symbol_exprt init_sym =
322  sub_type, "subarray_init", location, function_identifier, symbol_table)
323  .symbol_expr();
324  code_declt init_decl(init_sym);
325  init_decl.add_source_location() = location;
326  for_body.add(std::move(init_decl));
327 
328  code_assignt init_subarray(init_sym, sub_java_new);
329  for_body.add(std::move(init_subarray));
330  code_assignt assign_subarray(
331  deref_expr, typecast_exprt(init_sym, deref_expr.type()));
332  for_body.add(std::move(assign_subarray));
333 
334  const code_fort for_loop = code_fort::from_index_bounds(
335  from_integer(0, tmp_i.type()),
336  to_multi_ary_expr(rhs).op0(),
337  tmp_i,
338  std::move(for_body),
339  location);
340 
341  goto_convert(for_loop, symbol_table, tmp, message_handler, ID_java);
342 
343  // lower new side effects recursively
344  lower_java_new(function_identifier, tmp, message_handler);
345 
346  dest.destructive_insert(next, tmp);
347  }
348 
349  return std::prev(next);
350 }
351 
360  const irep_idt &function_identifier,
361  goto_programt &goto_program,
362  goto_programt::targett target,
363  message_handlert &message_handler)
364 {
365  if(!target->is_assign())
366  return target;
367 
368  if(as_const(*target).assign_rhs().id() == ID_side_effect)
369  {
370  // we make a copy, as we intend to destroy the assignment
371  // inside lower_java_new and lower_java_new_array
372  exprt lhs = target->assign_lhs();
373  exprt rhs = target->assign_rhs();
374 
375  const auto &side_effect_expr = to_side_effect_expr(rhs);
376  const auto &statement = side_effect_expr.get_statement();
377 
378  if(statement == ID_java_new)
379  {
380  return lower_java_new(
381  function_identifier, lhs, side_effect_expr, goto_program, target);
382  }
383  else if(statement == ID_java_new_array)
384  {
385  return lower_java_new_array(
386  function_identifier,
387  lhs,
388  side_effect_expr,
389  goto_program,
390  target,
391  message_handler);
392  }
393  }
394 
395  return target;
396 }
397 
406  const irep_idt &function_identifier,
407  goto_programt &goto_program,
408  message_handlert &message_handler)
409 {
410  bool changed = false;
411  for(goto_programt::instructionst::iterator target =
412  goto_program.instructions.begin();
413  target != goto_program.instructions.end();
414  ++target)
415  {
417  function_identifier, goto_program, target, message_handler);
418  changed = changed || new_target == target;
419  target = new_target;
420  }
421  if(!changed)
422  return false;
423  goto_program.update();
424  return true;
425 }
426 
436  const irep_idt &function_identifier,
437  goto_programt::targett target,
438  goto_programt &goto_program,
439  symbol_table_baset &symbol_table,
440  message_handlert &message_handler)
441 {
442  remove_java_newt rem{symbol_table};
443  rem.lower_java_new(
444  function_identifier, goto_program, target, message_handler);
445 }
446 
455  const irep_idt &function_identifier,
457  symbol_table_baset &symbol_table,
458  message_handlert &message_handler)
459 {
460  remove_java_newt rem{symbol_table};
461  rem.lower_java_new(function_identifier, function.body, message_handler);
462 }
463 
471  goto_functionst &goto_functions,
472  symbol_table_baset &symbol_table,
473  message_handlert &message_handler)
474 {
475  remove_java_newt rem{symbol_table};
476  bool changed = false;
477  for(auto &f : goto_functions.function_map)
478  changed =
479  rem.lower_java_new(f.first, f.second.body, message_handler) || changed;
480  if(changed)
481  goto_functions.compute_location_numbers();
482 }
483 
490 void remove_java_new(goto_modelt &goto_model, message_handlert &message_handler)
491 {
493  goto_model.goto_functions, goto_model.symbol_table, message_handler);
494 }
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:129
tag_typet::get_identifier
const irep_idt & get_identifier() const
Definition: std_types.h:410
struct_union_typet::components
const componentst & components() const
Definition: std_types.h:147
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:624
code_blockt
A codet representing sequential composition of program statements.
Definition: std_code.h:168
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:308
get_array_dimension_field
exprt get_array_dimension_field(const exprt &pointer)
Definition: java_types.cpp:204
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:129
code_fort
codet representation of a for statement.
Definition: std_code.h:1050
CHECK_RETURN
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:495
typet
The type of an expression, extends irept.
Definition: type.h:28
dereference_exprt
Operator to dereference a pointer.
Definition: pointer_expr.h:628
data
Definition: kdev_t.h:24
irept::find
const irept & find(const irep_namet &name) const
Definition: irep.cpp:106
goto_programt::update
void update()
Update all indices.
Definition: goto_program.cpp:576
code_declt
A codet representing the declaration of a local variable.
Definition: std_code.h:400
goto_model.h
Symbol Table + CFG.
plus_exprt
The plus expression Associativity is not specified.
Definition: std_expr.h:914
goto_functionst::compute_location_numbers
void compute_location_numbers()
Definition: goto_functions.cpp:20
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:833
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:449
remove_java_newt::remove_java_newt
remove_java_newt(symbol_table_baset &symbol_table)
Definition: remove_java_new.cpp:29
to_side_effect_expr
side_effect_exprt & to_side_effect_expr(exprt &expr)
Definition: std_code.h:1952
goto_convert.h
Program Transformation.
goto_functionst::function_map
function_mapt function_map
Definition: goto_functions.h:29
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:80
goto_programt::make_decl
static instructiont make_decl(const symbol_exprt &symbol, const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:978
remove_java_newt::symbol_table
symbol_table_baset & symbol_table
Definition: remove_java_new.cpp:48
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:1082
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:317
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:1876
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:91
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:58
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:706
remove_java_newt::ns
namespacet ns
Definition: remove_java_new.cpp:49
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:744
PRECONDITION
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
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:121
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:33
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:474
code_blockt::add
void add(const codet &code)
Definition: std_code.h:206
false_exprt
The Boolean constant false.
Definition: std_expr.h:2811
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:435
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:19
goto_functionst::goto_functiont
::goto_functiont goto_functiont
Definition: goto_functions.h:27
member_exprt
Extract member of struct or union.
Definition: std_expr.h:2613
goto_programt::instructions
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:652
goto_functionst
A collection of goto functions.
Definition: goto_functions.h:25
struct_typet
Structure type, corresponds to C style structs.
Definition: std_types.h:231
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:49
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:971
string_typet
String type.
Definition: std_types.h:880
goto_programt
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:71
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:214
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:722
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:187
size_of_expr
optionalt< exprt > size_of_expr(const typet &type, const namespacet &ns)
Definition: pointer_offset_size.cpp:279
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:92
java_types.h
exprt::add_source_location
source_locationt & add_source_location()
Definition: expr.h:235
typecast_exprt
Semantic type conversion.
Definition: std_expr.h:1866
get_array_element_type_field
exprt get_array_element_type_field(const exprt &pointer)
Definition: java_types.cpp:216
code_assignt
A codet representing an assignment in the program.
Definition: std_code.h:293
java_utils.h
constant_exprt
A constant literal expression.
Definition: std_expr.h:2753
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:899
exprt::source_location
const source_locationt & source_location() const
Definition: expr.h:230
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:27
goto_programt::targett
instructionst::iterator targett
Definition: goto_program.h:646
side_effect_exprt
An expression containing a side effect.
Definition: std_code.h:1896
to_struct_expr
const struct_exprt & to_struct_expr(const exprt &expr)
Cast an exprt to a struct_exprt.
Definition: std_expr.h:1691
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:405
codet
Data structure for representing an arbitrary statement in a program.
Definition: std_code.h:33