Skip to content

Commit 4e36916

Browse files
committed
Update initializer and expression types after linking
Non-symbol types may have been updated during linking; those updates had not been propagated to initializers or goto programs.
1 parent 4c34be4 commit 4e36916

File tree

7 files changed

+73
-25
lines changed

7 files changed

+73
-25
lines changed

regression/cbmc/Linking6/main.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <stdio.h>
2+
3+
void set();
4+
5+
char buffer[10];
6+
7+
void init() {
8+
int i;
9+
for (i = 0; i < 10; i++) {buffer[i] = 0;}
10+
}
11+
12+
void print() {
13+
printf("buffer = %s\n",buffer);
14+
}
15+
16+
void main () {
17+
init();
18+
set();
19+
print();
20+
}
21+
22+
23+

regression/cbmc/Linking6/module.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <stdlib.h>
2+
3+
extern char buffer[];
4+
5+
static size_t _debug_tempBufferHead = ((size_t)(&buffer));
6+
7+
void set() {
8+
*(char *)_debug_tempBufferHead = 'a';
9+
}

regression/cbmc/Linking6/test.desc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
module.c --pointer-check
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

src/ansi-c/c_typecheck_base.cpp

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -210,27 +210,6 @@ void c_typecheck_baset::typecheck_new_symbol(symbolt &symbol)
210210
}
211211
else
212212
{
213-
if(symbol.type.id()==ID_array &&
214-
to_array_type(symbol.type).size().is_nil() &&
215-
!symbol.is_type)
216-
{
217-
// Insert a new type symbol for the array.
218-
// We do this because we want a convenient way
219-
// of adjusting the size of the type later on.
220-
221-
type_symbolt new_symbol(symbol.type);
222-
new_symbol.name=id2string(symbol.name)+"$type";
223-
new_symbol.base_name=id2string(symbol.base_name)+"$type";
224-
new_symbol.location=symbol.location;
225-
new_symbol.mode=symbol.mode;
226-
new_symbol.module=symbol.module;
227-
228-
symbol.type=symbol_typet(new_symbol.name);
229-
230-
symbolt *new_sp;
231-
symbol_table.move(new_symbol, new_sp);
232-
}
233-
234213
// check the initializer
235214
do_initializer(symbol);
236215
}

src/goto-programs/read_goto_binary.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ static bool link_functions(
289289
const symbol_tablet &src_symbol_table,
290290
goto_functionst &src_functions,
291291
const rename_symbolt &rename_symbol,
292-
const std::unordered_set<irep_idt, irep_id_hash> &weak_symbols)
292+
const std::unordered_set<irep_idt, irep_id_hash> &weak_symbols,
293+
const replace_symbolt &object_type_updates)
293294
{
294295
namespacet ns(dest_symbol_table);
295296
namespacet src_ns(src_symbol_table);
@@ -381,6 +382,16 @@ static bool link_functions(
381382
Forall_goto_functions(dest_it, dest_functions)
382383
rename_symbols_in_function(dest_it->second, macro_application);
383384

385+
if(!object_type_updates.expr_map.empty())
386+
{
387+
Forall_goto_functions(dest_it, dest_functions)
388+
Forall_goto_program_instructions(iit, dest_it->second.body)
389+
{
390+
object_type_updates(iit->code);
391+
object_type_updates(iit->guard);
392+
}
393+
}
394+
384395
return false;
385396
}
386397

@@ -427,9 +438,14 @@ bool read_object_and_link(
427438
if(linking.typecheck_main())
428439
return true;
429440

430-
if(link_functions(symbol_table, functions,
431-
temp_model.symbol_table, temp_model.goto_functions,
432-
linking.rename_symbol, weak_symbols))
441+
if(link_functions(
442+
symbol_table,
443+
functions,
444+
temp_model.symbol_table,
445+
temp_model.goto_functions,
446+
linking.rename_symbol,
447+
weak_symbols,
448+
linking.object_type_updates))
433449
return true;
434450

435451
return false;

src/linking/linking.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,8 @@ void linkingt::duplicate_object_symbol(
11001100
}
11011101
else if(set_to_new)
11021102
old_symbol.type=new_symbol.type;
1103+
1104+
object_type_updates.insert(old_symbol.name, old_symbol.symbol_expr());
11031105
}
11041106

11051107
// care about initializers
@@ -1508,6 +1510,15 @@ void linkingt::copy_symbols()
15081510
else
15091511
duplicate_non_type_symbol(old_symbol, new_symbol);
15101512
}
1513+
1514+
// Apply type updates to initializers
1515+
Forall_symbols(s_it, main_symbol_table.symbols)
1516+
{
1517+
if(!s_it->second.is_type &&
1518+
!s_it->second.is_macro &&
1519+
s_it->second.value.is_not_nil())
1520+
object_type_updates(s_it->second.value);
1521+
}
15111522
}
15121523

15131524
/*******************************************************************\

src/linking/linking_class.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Author: Daniel Kroening, [email protected]
1111

1212
#include <util/namespace.h>
1313
#include <util/rename_symbol.h>
14+
#include <util/replace_symbol.h>
1415
#include <util/typecheck.h>
1516
#include <util/std_expr.h>
1617

@@ -31,6 +32,7 @@ class linkingt:public typecheckt
3132
virtual void typecheck();
3233

3334
rename_symbolt rename_symbol;
35+
replace_symbolt object_type_updates;
3436

3537
protected:
3638
typedef std::unordered_set<irep_idt, irep_id_hash> id_sett;

0 commit comments

Comments
 (0)