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 <util/arith_tools.h>
18 #include <util/c_types.h>
19 #include <util/expr_cast.h>
20 #include <util/expr_initializer.h>
21 #include <util/fresh_symbol.h>
22 #include <util/message.h>
24 
25 class remove_java_newt : public messaget
26 {
27 public:
30  message_handlert &_message_handler)
31  : messaget(_message_handler), symbol_table(symbol_table), ns(symbol_table)
32  {
33  }
34 
35  // Lower java_new for a single function
37 
38  // Lower java_new for a single instruction
41 
42 protected:
45 
47  exprt lhs,
49  goto_programt &,
51 
53  exprt lhs,
55  goto_programt &,
57 };
58 
71  exprt lhs,
73  goto_programt &dest,
75 {
76  PRECONDITION(!lhs.is_nil());
77  PRECONDITION(rhs.operands().empty());
78  PRECONDITION(rhs.type().id() == ID_pointer);
79  source_locationt location = rhs.source_location();
80  typet object_type = rhs.type().subtype();
81 
82  // build size expression
83  exprt object_size = size_of_expr(object_type, ns);
85 
86  // we produce a malloc side-effect, which stays
87  side_effect_exprt malloc_expr(ID_allocate, rhs.type(), location);
88  malloc_expr.copy_to_operands(object_size);
89  // could use true and get rid of the code below
90  malloc_expr.copy_to_operands(false_exprt());
91  target->make_assignment(code_assignt(lhs, malloc_expr));
92  target->source_location = location;
93 
94  // zero-initialize the object
95  dereference_exprt deref(lhs, object_type);
96  exprt zero_object =
97  zero_initializer(object_type, location, ns, get_message_handler());
99  to_struct_expr(zero_object), ns, to_symbol_type(object_type));
100  goto_programt::targett t_i = dest.insert_after(target);
101  t_i->make_assignment(code_assignt(deref, zero_object));
102  t_i->source_location = location;
103 
104  return t_i;
105 }
106 
119  exprt lhs,
120  side_effect_exprt rhs,
121  goto_programt &dest,
122  goto_programt::targett target)
123 {
124  // lower_java_new_array without lhs not implemented
125  PRECONDITION(!lhs.is_nil());
126  PRECONDITION(rhs.operands().size() >= 1); // one per dimension
127  PRECONDITION(rhs.type().id() == ID_pointer);
128 
129  source_locationt location = rhs.source_location();
130  typet object_type = rhs.type().subtype();
131  PRECONDITION(ns.follow(object_type).id() == ID_struct);
132 
133  // build size expression
134  exprt object_size = size_of_expr(object_type, ns);
136 
137  // we produce a malloc side-effect, which stays
138  side_effect_exprt malloc_expr(ID_allocate, rhs.type(), location);
139  malloc_expr.copy_to_operands(object_size);
140  // code use true and get rid of the code below
141  malloc_expr.copy_to_operands(false_exprt());
142 
143  target->make_assignment(code_assignt(lhs, malloc_expr));
144  target->source_location = location;
145  goto_programt::targett next = std::next(target);
146 
147  const struct_typet &struct_type = to_struct_type(ns.follow(object_type));
148 
149  // Ideally we would have a check for `is_valid_java_array(struct_type)` but
150  // `is_valid_java_array is part of the java_bytecode module and we cannot
151  // introduce such dependencies. We do this simple check instead:
152  PRECONDITION(struct_type.components().size() == 3);
153 
154  // Init base class:
155  dereference_exprt deref(lhs, object_type);
156  exprt zero_object =
157  zero_initializer(object_type, location, ns, get_message_handler());
159  to_struct_expr(zero_object), ns, to_symbol_type(object_type));
160  goto_programt::targett t_i = dest.insert_before(next);
161  t_i->make_assignment(code_assignt(deref, zero_object));
162  t_i->source_location = location;
163 
164  // if it's an array, we need to set the length field
165  member_exprt length(
166  deref,
167  struct_type.components()[1].get_name(),
168  struct_type.components()[1].type());
169  goto_programt::targett t_s = dest.insert_before(next);
170  t_s->make_assignment(code_assignt(length, rhs.op0()));
171  t_s->source_location = location;
172 
173  // we also need to allocate space for the data
175  deref,
176  struct_type.components()[2].get_name(),
177  struct_type.components()[2].type());
178 
179  // Allocate a (struct realtype**) instead of a (void**) if possible.
180  const irept &given_element_type = object_type.find(ID_element_type);
181  typet allocate_data_type;
182  if(given_element_type.is_not_nil())
183  {
184  allocate_data_type =
185  pointer_type(static_cast<const typet &>(given_element_type));
186  }
187  else
188  allocate_data_type = data.type();
189 
190  side_effect_exprt data_java_new_expr(
191  ID_java_new_array_data, allocate_data_type, location);
192 
193  // The instruction may specify a (hopefully small) upper bound on the
194  // array size, in which case we allocate a fixed-length array that may
195  // be larger than the `length` member rather than use a true variable-
196  // length array, which produces a more complex formula in the current
197  // backend.
198  const irept size_bound = rhs.find(ID_length_upper_bound);
199  if(size_bound.is_nil())
200  data_java_new_expr.set(ID_size, rhs.op0());
201  else
202  data_java_new_expr.set(ID_size, size_bound);
203 
204  // Must directly assign the new array to a temporary
205  // because goto-symex will notice `x=side_effect_exprt` but not
206  // `x=typecast_exprt(side_effect_exprt(...))`
207  symbol_exprt new_array_data_symbol = get_fresh_aux_symbol(
208  data_java_new_expr.type(),
209  id2string(target->function),
210  "tmp_new_data_array",
211  location,
212  ID_java,
213  symbol_table)
214  .symbol_expr();
215  code_declt array_decl(new_array_data_symbol);
216  array_decl.add_source_location() = location;
217  goto_programt::targett t_array_decl = dest.insert_before(next);
218  t_array_decl->make_decl(array_decl);
219  t_array_decl->source_location = location;
220  goto_programt::targett t_p2 = dest.insert_before(next);
221  t_p2->make_assignment(
222  code_assignt(new_array_data_symbol, data_java_new_expr));
223  t_p2->source_location = location;
224 
225  goto_programt::targett t_p = dest.insert_before(next);
226  exprt cast_java_new = new_array_data_symbol;
227  if(cast_java_new.type() != data.type())
228  cast_java_new = typecast_exprt(cast_java_new, data.type());
229  t_p->make_assignment(code_assignt(data, cast_java_new));
230  t_p->source_location = location;
231 
232  // zero-initialize the data
233  if(!rhs.get_bool(ID_skip_initialize))
234  {
235  exprt zero_element = zero_initializer(
236  data.type().subtype(), location, ns, get_message_handler());
237  codet array_set(ID_array_set);
238  array_set.copy_to_operands(new_array_data_symbol, zero_element);
239  goto_programt::targett t_d = dest.insert_before(next);
240  t_d->make_other(array_set);
241  t_d->source_location = location;
242  }
243 
244  // multi-dimensional?
245 
246  if(rhs.operands().size() >= 2)
247  {
248  // produce
249  // for(int i=0; i<size; i++) tmp[i]=java_new(dim-1);
250  // This will be converted recursively.
251 
252  goto_programt tmp;
253 
255  length.type(),
256  id2string(target->function),
257  "tmp_index",
258  location,
259  ID_java,
260  symbol_table)
261  .symbol_expr();
262  code_declt decl(tmp_i);
263  decl.add_source_location() = location;
264  goto_programt::targett t_decl = tmp.insert_before(tmp.instructions.begin());
265  t_decl->make_decl(decl);
266  t_decl->source_location = location;
267 
268  code_fort for_loop;
269 
270  side_effect_exprt sub_java_new = rhs;
271  sub_java_new.operands().erase(sub_java_new.operands().begin());
272 
273  // we already know that rhs has pointer type
274  typet sub_type =
275  static_cast<const typet &>(rhs.type().subtype().find(ID_element_type));
276  CHECK_RETURN(sub_type.id() == ID_pointer);
277  sub_java_new.type() = sub_type;
278 
279  side_effect_exprt inc(ID_assign, typet(), location);
280  inc.operands().resize(2);
281  inc.op0() = tmp_i;
282  inc.op1() = plus_exprt(tmp_i, from_integer(1, tmp_i.type()));
283 
284  dereference_exprt deref_expr(
285  plus_exprt(data, tmp_i), data.type().subtype());
286 
287  code_blockt for_body;
289  sub_type,
290  id2string(target->function),
291  "subarray_init",
292  location,
293  ID_java,
294  symbol_table)
295  .symbol_expr();
296  code_declt init_decl(init_sym);
297  init_decl.add_source_location() = location;
298  for_body.move_to_operands(init_decl);
299 
300  code_assignt init_subarray(init_sym, sub_java_new);
301  code_assignt assign_subarray(
302  deref_expr, typecast_exprt(init_sym, deref_expr.type()));
303  for_body.move_to_operands(init_subarray);
304  for_body.move_to_operands(assign_subarray);
305 
306  for_loop.init() = code_assignt(tmp_i, from_integer(0, tmp_i.type()));
307  for_loop.cond() = binary_relation_exprt(tmp_i, ID_lt, rhs.op0());
308  for_loop.iter() = inc;
309  for_loop.body() = for_body;
310 
311  goto_convert(for_loop, symbol_table, tmp, get_message_handler(), ID_java);
312 
313  // lower new side effects recursively
314  lower_java_new(tmp);
315 
316  dest.destructive_insert(next, tmp);
317  }
318 
319  return std::prev(next);
320 }
321 
329  goto_programt::targett target)
330 {
331  const auto maybe_code_assign =
332  expr_try_dynamic_cast<code_assignt>(target->code);
333  if(!maybe_code_assign)
334  return target;
335 
336  const exprt &lhs = maybe_code_assign->lhs();
337  const exprt &rhs = maybe_code_assign->rhs();
338 
339  if(rhs.id() == ID_side_effect && rhs.get(ID_statement) == ID_java_new)
340  {
341  return lower_java_new(lhs, to_side_effect_expr(rhs), goto_program, target);
342  }
343 
344  if(rhs.id() == ID_side_effect && rhs.get(ID_statement) == ID_java_new_array)
345  {
346  return lower_java_new_array(
347  lhs, to_side_effect_expr(rhs), goto_program, target);
348  }
349 
350  return target;
351 }
352 
359 {
360  bool changed = false;
361  for(goto_programt::instructionst::iterator target =
362  goto_program.instructions.begin();
363  target != goto_program.instructions.end();
364  ++target)
365  {
366  goto_programt::targett new_target = lower_java_new(goto_program, target);
367  changed = changed || new_target == target;
368  target = new_target;
369  }
370  if(!changed)
371  return false;
373  return true;
374 }
375 
384  goto_programt::targett target,
386  symbol_table_baset &symbol_table,
388 {
389  remove_java_newt rem(symbol_table, message_handler);
390  rem.lower_java_new(goto_program, target);
391 }
392 
401  symbol_table_baset &symbol_table,
403 {
404  remove_java_newt rem(symbol_table, message_handler);
405  rem.lower_java_new(function.body);
406 }
407 
415  goto_functionst &goto_functions,
416  symbol_table_baset &symbol_table,
418 {
419  remove_java_newt rem(symbol_table, message_handler);
420  bool changed = false;
421  for(auto &f : goto_functions.function_map)
422  changed = rem.lower_java_new(f.second.body) || changed;
423  if(changed)
424  goto_functions.compute_location_numbers();
425 }
426 
434 {
436  goto_model.goto_functions, goto_model.symbol_table, message_handler);
437 }
Remove Java New Operators.
The type of an expression.
Definition: type.h:22
exprt size_of_expr(const typet &type, const namespacet &ns)
void update()
Update all indices.
semantic type conversion
Definition: std_expr.h:2111
bool is_nil() const
Definition: irep.h:172
A generic base class for relations, i.e., binary predicates.
Definition: std_expr.h:752
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
bool is_not_nil() const
Definition: irep.h:173
pointer_typet pointer_type(const typet &subtype)
Definition: c_types.cpp:243
const exprt & init() const
Definition: std_code.h:736
targett insert_before(const_targett target)
Insertion before the given target.
Definition: goto_program.h:473
exprt & op0()
Definition: expr.h:72
const codet & body() const
Definition: std_code.h:766
void goto_convert(const codet &code, symbol_table_baset &symbol_table, goto_programt &dest, message_handlert &message_handler, const irep_idt &mode)
void copy_to_operands(const exprt &expr)
Definition: expr.cpp:55
Fresh auxiliary symbol creation.
const symbol_typet & to_symbol_type(const typet &type)
Cast a generic typet to a symbol_typet.
Definition: std_types.h:139
const componentst & components() const
Definition: std_types.h:245
function_mapt function_map
typet & type()
Definition: expr.h:56
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:29
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:266
Structure type.
Definition: std_types.h:297
bool get_bool(const irep_namet &name) const
Definition: irep.cpp:240
void set_class_identifier(struct_exprt &expr, const namespacet &ns, const symbol_typet &class_type)
If expr has its components filled in then sets the @class_identifier member of the struct...
side_effect_exprt & to_side_effect_expr(exprt &expr)
Definition: std_code.h:1326
Extract member of struct or union.
Definition: std_expr.h:3869
Templated functions to cast to specific exprt-derived classes.
Expression Initialization.
exprt object_size(const exprt &pointer)
const irep_idt & id() const
Definition: irep.h:259
class symbol_exprt symbol_expr() const
produces a symbol_exprt for a symbol
Definition: symbol.cpp:111
void compute_location_numbers()
instructionst::iterator targett
Definition: goto_program.h:397
symbol_table_baset & symbol_table
A declaration of a local variable.
Definition: std_code.h:254
const exprt & cond() const
Definition: std_code.h:746
Operator to dereference a pointer.
Definition: std_expr.h:3282
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:403
exprt & op1()
Definition: expr.h:75
const irep_idt & get(const irep_namet &name) const
Definition: irep.cpp:213
TO_BE_DOCUMENTED.
Definition: namespace.h:74
::goto_functiont goto_functiont
#define PRECONDITION(CONDITION)
Definition: invariant.h:242
targett insert_after(const_targett target)
Insertion after the given target.
Definition: goto_program.h:480
void destructive_insert(const_targett target, goto_programt &p)
Inserts the given program at the given location.
Definition: goto_program.h:495
Base class for tree-like data structures with sharing.
Definition: irep.h:156
The plus expression.
Definition: std_expr.h:893
remove_java_newt(symbol_table_baset &symbol_table, message_handlert &_message_handler)
const typet & follow(const typet &) const
Definition: namespace.cpp:55
const struct_typet & to_struct_type(const typet &type)
Cast a generic typet to a struct_typet.
Definition: std_types.h:318
const struct_exprt & to_struct_expr(const exprt &expr)
Cast a generic exprt to a struct_exprt.
Definition: std_expr.h:1838
The boolean constant false.
Definition: std_expr.h:4497
exprt zero_initializer(const typet &type, const source_locationt &source_location, const namespacet &ns, message_handlert &message_handler)
Pointer Logic.
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:70
message_handlert & get_message_handler()
Definition: message.h:153
symbolt & get_fresh_aux_symbol(const typet &type, const std::string &name_prefix, const std::string &basename_prefix, const source_locationt &source_location, const irep_idt &symbol_mode, symbol_table_baset &symbol_table)
Installs a fresh-named symbol with the requested name pattern.
Base class for all expressions.
Definition: expr.h:42
The symbol table base class interface.
const source_locationt & source_location() const
Definition: expr.h:125
const exprt & iter() const
Definition: std_code.h:756
Program Transformation.
void remove_java_new(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.
goto_programt & goto_program
Definition: cover.cpp:63
source_locationt & add_source_location()
Definition: expr.h:130
Sequential composition.
Definition: std_code.h:89
bool lower_java_new(goto_programt &)
Replace every java_new or java_new_array by a malloc side-effect and zero initialization.
goto_programt::targett lower_java_new_array(exprt lhs, side_effect_exprt rhs, goto_programt &, goto_programt::targett)
Replaces the instruction lhs = new java_array_type by the following code: lhs = ALLOCATE(java_type) l...
Expression to hold a symbol (variable)
Definition: std_expr.h:90
Extract class identifier.
goto_programt coverage_criteriont message_handlert & message_handler
Definition: cover.cpp:66
A statement in a programming language.
Definition: std_code.h:21
A ‘for’ instruction.
Definition: std_code.h:727
const typet & subtype() const
Definition: type.h:33
An expression containing a side effect.
Definition: std_code.h:1271
operandst & operands()
Definition: expr.h:66
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
const irept & find(const irep_namet &name) const
Definition: irep.cpp:285
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:32
Assignment.
Definition: std_code.h:196
Definition: kdev_t.h:24
void set(const irep_namet &name, const irep_idt &value)
Definition: irep.h:286
auto expr_try_dynamic_cast(TExpr &base) -> typename detail::expr_try_dynamic_cast_return_typet< T, TExpr >::type
Try to cast a reference to a generic exprt to a specific derived class.
Definition: expr_cast.h:91