Skip to content

Commit 012afa8

Browse files
Prefix temporary var names with function id
Temporary variable names must be globally unique and changing the number of temporaries in one function must not change a variable name in another function. Otherwise, this has adverse effects on goto-diff. This commit gets rid of various global temporary variables counters that do not meet above criteria. Instead, we now always use the function id to prefix temporaries to eliminate cross-function effects and increment the numeric suffix only when the entire variable name already exists, using the rename facility.
1 parent ca678aa commit 012afa8

21 files changed

+188
-116
lines changed

src/ansi-c/c_nondet_symbol_factory.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ static const symbolt &c_new_tmp_symbol(
4444
const std::string &prefix="tmp")
4545
{
4646
symbolt &tmp_symbol=
47-
get_fresh_aux_symbol(type, "", prefix, loc, ID_C, symbol_table);
47+
get_fresh_aux_symbol(type, id2string(loc.get_function()),
48+
prefix, loc, ID_C, symbol_table);
4849
tmp_symbol.is_static_lifetime=static_lifetime;
4950

5051
return tmp_symbol;

src/goto-instrument/code_contracts.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ const symbolt &code_contractst::new_tmp_symbol(
250250
{
251251
return get_fresh_aux_symbol(
252252
type,
253-
"",
253+
id2string(source_location.get_function()),
254254
"tmp_cc",
255255
source_location,
256256
irep_idt(),

src/goto-programs/convert_nondet.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Author: Reuben Thomas, [email protected]
1717
#include <java_bytecode/java_object_factory.h> // gen_nondet_init
1818

1919
#include <util/irep_ids.h>
20+
#include <util/fresh_symbol.h>
2021

2122
#include <memory>
2223

@@ -135,11 +136,13 @@ void convert_nondet(
135136
message_handlert &message_handler,
136137
const object_factory_parameterst &object_factory_parameters)
137138
{
139+
object_factory_parameterst parameters = object_factory_parameters;
140+
parameters.function_id = function.get_function_id();
138141
convert_nondet(
139142
function.get_goto_function().body,
140143
function.get_symbol_table(),
141144
message_handler,
142-
object_factory_parameters);
145+
parameters);
143146

144147
function.compute_location_numbers();
145148
}
@@ -158,6 +161,8 @@ void convert_nondet(
158161

159162
if(symbol.mode==ID_java)
160163
{
164+
object_factory_parameterst parameters = object_factory_parameters;
165+
parameters.function_id = f_it.first;
161166
convert_nondet(
162167
f_it.second.body,
163168
symbol_table,

src/goto-programs/goto_convert_class.h

-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class goto_convertt:public messaget
3434
messaget(_message_handler),
3535
symbol_table(_symbol_table),
3636
ns(_symbol_table),
37-
temporary_counter(0),
3837
tmp_symbol_prefix("goto_convertt")
3938
{
4039
}
@@ -46,7 +45,6 @@ class goto_convertt:public messaget
4645
protected:
4746
symbol_table_baset &symbol_table;
4847
namespacet ns;
49-
unsigned temporary_counter;
5048
std::string tmp_symbol_prefix;
5149

5250
void goto_convert_rec(const codet &code, goto_programt &dest);

src/goto-programs/goto_convert_functions.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@ void goto_convert_functionst::convert_function(
147147

148148
// make tmp variables local to function
149149
tmp_symbol_prefix=id2string(symbol.name)+"::$tmp::";
150-
temporary_counter=0;
151-
reset_temporary_counter();
152150

153151
f.type=to_code_type(symbol.type);
154152

src/goto-programs/goto_convert_side_effect.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -379,14 +379,15 @@ void goto_convertt::remove_function_call(
379379

380380
new_base_name+='_';
381381
new_base_name+=id2string(symbol.base_name);
382-
new_base_name+="$"+std::to_string(++temporary_counter);
382+
new_base_name += "$0";
383383

384384
new_symbol.base_name=new_base_name;
385385
new_symbol.mode=symbol.mode;
386386
}
387387

388388
new_symbol.name=tmp_symbol_prefix+id2string(new_symbol.base_name);
389389

390+
// ensure that the name is unique
390391
new_name(new_symbol);
391392

392393
{
@@ -429,10 +430,11 @@ void goto_convertt::remove_cpp_new(
429430

430431
auxiliary_symbolt new_symbol;
431432

432-
new_symbol.base_name="new_ptr$"+std::to_string(++temporary_counter);
433+
new_symbol.base_name = "new_ptr$0";
433434
new_symbol.type=expr.type();
434435
new_symbol.name=tmp_symbol_prefix+id2string(new_symbol.base_name);
435436

437+
// ensure that the name is unique
436438
new_name(new_symbol);
437439

438440
code_declt decl;
@@ -480,11 +482,12 @@ void goto_convertt::remove_malloc(
480482
{
481483
auxiliary_symbolt new_symbol;
482484

483-
new_symbol.base_name="malloc_value$"+std::to_string(++temporary_counter);
485+
new_symbol.base_name = "malloc_value$0";
484486
new_symbol.type=expr.type();
485487
new_symbol.name=tmp_symbol_prefix+id2string(new_symbol.base_name);
486488
new_symbol.location=expr.source_location();
487489

490+
// ensure that the name is unique
488491
new_name(new_symbol);
489492

490493
code_declt decl;

src/goto-programs/remove_function_pointers.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ void remove_function_pointerst::fix_return_type(
236236
symbolt &tmp_symbol=
237237
get_fresh_aux_symbol(
238238
code_type.return_type(),
239-
"remove_function_pointers",
239+
id2string(function_call.source_location().get_function()),
240240
"tmp_return_val",
241241
function_call.source_location(),
242242
irep_idt(),

src/goto-programs/remove_instanceof.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ std::size_t remove_instanceoft::lower_instanceof(
109109
symbolt &newsym=
110110
get_fresh_aux_symbol(
111111
object_clsid.type(),
112-
"instanceof_tmp",
112+
id2string(this_inst->function),
113113
"instanceof_tmp",
114114
source_locationt(),
115115
ID_java,

src/java_bytecode/java_entry_point.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ exprt::operandst java_build_arguments(
203203
bool allow_null=
204204
!assume_init_pointers_not_null && !is_main && !is_this;
205205

206+
object_factory_parameterst parameters = object_factory_parameters;
207+
parameters.function_id = function.name;
208+
206209
// generate code to allocate and non-deterministicaly initialize the
207210
// argument
208211
main_arguments[param_number]=
@@ -212,7 +215,7 @@ exprt::operandst java_build_arguments(
212215
init_code,
213216
allow_null,
214217
symbol_table,
215-
object_factory_parameters,
218+
parameters,
216219
allocation_typet::LOCAL,
217220
function.location,
218221
pointer_type_selector);

src/java_bytecode/java_object_factory.cpp

+55-37
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,6 @@ Author: Daniel Kroening, [email protected]
3535
#include "java_root_class.h"
3636
#include "generic_parameter_specialization_map_keys.h"
3737

38-
static symbolt &new_tmp_symbol(
39-
symbol_table_baset &symbol_table,
40-
const source_locationt &loc,
41-
const typet &type,
42-
const std::string &prefix = "tmp_object_factory")
43-
{
44-
return get_fresh_aux_symbol(
45-
type,
46-
"",
47-
prefix,
48-
loc,
49-
ID_java,
50-
symbol_table);
51-
}
52-
5338
class java_object_factoryt
5439
{
5540
/// Every new variable initialized by the code emitted by the methods of this
@@ -195,6 +180,7 @@ exprt allocate_dynamic_object(
195180
const typet &allocate_type,
196181
symbol_table_baset &symbol_table,
197182
const source_locationt &loc,
183+
const irep_idt &function_id,
198184
code_blockt &output_code,
199185
std::vector<const symbolt *> &symbols_created,
200186
bool cast_needed)
@@ -212,11 +198,13 @@ exprt allocate_dynamic_object(
212198
// create a symbol for the malloc expression so we can initialize
213199
// without having to do it potentially through a double-deref, which
214200
// breaks the to-SSA phase.
215-
symbolt &malloc_sym=new_tmp_symbol(
216-
symbol_table,
217-
loc,
201+
symbolt &malloc_sym = get_fresh_aux_symbol(
218202
pointer_type(allocate_type),
219-
"malloc_site");
203+
id2string(function_id),
204+
"malloc_site",
205+
loc,
206+
ID_java,
207+
symbol_table);
220208
symbols_created.push_back(&malloc_sym);
221209
code_assignt assign=code_assignt(malloc_sym.symbol_expr(), malloc_expr);
222210
assign.add_source_location()=loc;
@@ -253,6 +241,7 @@ exprt allocate_dynamic_object_with_decl(
253241
const exprt &target_expr,
254242
symbol_table_baset &symbol_table,
255243
const source_locationt &loc,
244+
const irep_idt &function_id,
256245
code_blockt &output_code)
257246
{
258247
std::vector<const symbolt *> symbols_created;
@@ -263,6 +252,7 @@ exprt allocate_dynamic_object_with_decl(
263252
allocate_type,
264253
symbol_table,
265254
loc,
255+
function_id,
266256
tmp_block,
267257
symbols_created,
268258
false);
@@ -306,7 +296,13 @@ exprt java_object_factoryt::allocate_object(
306296
case allocation_typet::LOCAL:
307297
case allocation_typet::GLOBAL:
308298
{
309-
symbolt &aux_symbol=new_tmp_symbol(symbol_table, loc, allocate_type);
299+
symbolt &aux_symbol = get_fresh_aux_symbol(
300+
allocate_type,
301+
id2string(object_factory_parameters.function_id),
302+
"tmp_object_factory",
303+
loc,
304+
ID_java,
305+
symbol_table);
310306
if(alloc_type==allocation_typet::GLOBAL)
311307
aux_symbol.is_static_lifetime=true;
312308
symbols_created.push_back(&aux_symbol);
@@ -327,6 +323,7 @@ exprt java_object_factoryt::allocate_object(
327323
allocate_type,
328324
symbol_table,
329325
loc,
326+
object_factory_parameters.function_id,
330327
assignments,
331328
symbols_created,
332329
cast_needed);
@@ -569,6 +566,7 @@ codet initialize_nondet_string_struct(
569566
const exprt &obj,
570567
const std::size_t &max_nondet_string_length,
571568
const source_locationt &loc,
569+
const irep_idt &function_id,
572570
symbol_table_baset &symbol_table,
573571
bool printable)
574572
{
@@ -608,8 +606,13 @@ codet initialize_nondet_string_struct(
608606
{
609607
/// \todo Refactor with make_nondet_string_expr
610608
// length_expr = nondet(int);
611-
const symbolt length_sym =
612-
new_tmp_symbol(symbol_table, loc, java_int_type());
609+
const symbolt length_sym = get_fresh_aux_symbol(
610+
java_int_type(),
611+
id2string(function_id),
612+
"tmp_object_factory",
613+
loc,
614+
ID_java,
615+
symbol_table);
613616
const symbol_exprt length_expr = length_sym.symbol_expr();
614617
const side_effect_expr_nondett nondet_length(length_expr.type());
615618
code.add(code_declt(length_expr));
@@ -646,7 +649,7 @@ codet initialize_nondet_string_struct(
646649
// data_expr = nondet(char[INFINITY]) // we use infinity for variable size
647650
const dereference_exprt data_expr(data_pointer);
648651
const exprt nondet_array =
649-
make_nondet_infinite_char_array(symbol_table, loc, code);
652+
make_nondet_infinite_char_array(symbol_table, loc, function_id, code);
650653
code.add(code_assignt(data_expr, nondet_array));
651654

652655
struct_expr.copy_to_operands(length_expr);
@@ -692,6 +695,7 @@ static bool add_nondet_string_pointer_initialization(
692695
bool printable,
693696
symbol_table_baset &symbol_table,
694697
const source_locationt &loc,
698+
const irep_idt &function_id,
695699
code_blockt &code)
696700
{
697701
const namespacet ns(symbol_table);
@@ -703,13 +707,14 @@ static bool add_nondet_string_pointer_initialization(
703707
return true;
704708

705709
const exprt malloc_site =
706-
allocate_dynamic_object_with_decl(expr, symbol_table, loc, code);
710+
allocate_dynamic_object_with_decl(expr, symbol_table, loc, function_id, code);
707711

708712
code.add(
709713
initialize_nondet_string_struct(
710714
dereference_exprt(malloc_site, struct_type),
711715
max_nondet_string_length,
712716
loc,
717+
function_id,
713718
symbol_table,
714719
printable));
715720
return false;
@@ -862,6 +867,7 @@ void java_object_factoryt::gen_nondet_pointer_init(
862867
object_factory_parameters.string_printable,
863868
symbol_table,
864869
loc,
870+
object_factory_parameters.function_id,
865871
assignments);
866872
}
867873
else
@@ -967,7 +973,13 @@ symbol_exprt java_object_factoryt::gen_nondet_subtype_pointer_init(
967973
const pointer_typet &replacement_pointer,
968974
size_t depth)
969975
{
970-
symbolt new_symbol=new_tmp_symbol(symbol_table, loc, replacement_pointer);
976+
symbolt new_symbol = get_fresh_aux_symbol(
977+
replacement_pointer,
978+
id2string(object_factory_parameters.function_id),
979+
"tmp_object_factory",
980+
loc,
981+
ID_java,
982+
symbol_table);
971983

972984
// Generate a new object into this new symbol
973985
gen_nondet_init(
@@ -1253,11 +1265,13 @@ void java_object_factoryt::allocate_nondet_length_array(
12531265
const exprt &max_length_expr,
12541266
const typet &element_type)
12551267
{
1256-
symbolt &length_sym=new_tmp_symbol(
1257-
symbol_table,
1258-
loc,
1268+
symbolt &length_sym = get_fresh_aux_symbol(
12591269
java_int_type(),
1260-
"nondet_array_length");
1270+
id2string(object_factory_parameters.function_id),
1271+
"nondet_array_length",
1272+
loc,
1273+
ID_java,
1274+
symbol_table);
12611275
symbols_created.push_back(&length_sym);
12621276
const auto &length_sym_expr=length_sym.symbol_expr();
12631277

@@ -1347,11 +1361,13 @@ void java_object_factoryt::gen_nondet_array_init(
13471361

13481362
// Interpose a new symbol, as the goto-symex stage can't handle array indexing
13491363
// via a cast.
1350-
symbolt &array_init_symbol=new_tmp_symbol(
1351-
symbol_table,
1352-
loc,
1364+
symbolt &array_init_symbol = get_fresh_aux_symbol(
13531365
init_array_expr.type(),
1354-
"array_data_init");
1366+
id2string(object_factory_parameters.function_id),
1367+
"array_data_init",
1368+
loc,
1369+
ID_java,
1370+
symbol_table);
13551371
symbols_created.push_back(&array_init_symbol);
13561372
const auto &array_init_symexpr=array_init_symbol.symbol_expr();
13571373
codet data_assign=code_assignt(array_init_symexpr, init_array_expr);
@@ -1360,11 +1376,13 @@ void java_object_factoryt::gen_nondet_array_init(
13601376

13611377
// Emit init loop for(array_init_iter=0; array_init_iter!=array.length;
13621378
// ++array_init_iter) init(array[array_init_iter]);
1363-
symbolt &counter=new_tmp_symbol(
1364-
symbol_table,
1365-
loc,
1379+
symbolt &counter = get_fresh_aux_symbol(
13661380
length_expr.type(),
1367-
"array_init_iter");
1381+
id2string(object_factory_parameters.function_id),
1382+
"array_init_iter",
1383+
loc,
1384+
ID_java,
1385+
symbol_table);
13681386
symbols_created.push_back(&counter);
13691387
exprt counter_expr=counter.symbol_expr();
13701388

0 commit comments

Comments
 (0)