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