Skip to content

Commit c419218

Browse files
author
thk123
committed
Don't try to follow non-const symbols
If a symbol is not a constant variable then its value may change. As a result, we cannot reason about its value and hence we resort to the old mechanism of all type compatible functions. The only exception is when the symbol is the function itself which is implictly const As we now require the symbol to be const, updated the tests to reflect this. Adding tests for demonstrating what was the problem with non constant pointers
1 parent f46cfa5 commit c419218

File tree

10 files changed

+109
-6
lines changed

10 files changed

+109
-6
lines changed

regression/goto-analyzer/fp-removal1/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const void_fp fp_all[] = {f1, f2 ,f3, f4, f5 ,f6, f7, f8, f9};
1818

1919
void func()
2020
{
21-
void_fp fp = f2;
21+
const void_fp fp = f2;
2222
fp();
2323
}
2424

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <stdio.h>
2+
3+
void f1 (void) { printf("%i", 1); }
4+
void f2 (void) { printf("%i", 2); }
5+
void f3 (void) { printf("%i", 3); }
6+
void f4 (void) { printf("%i", 4); }
7+
void f5 (void) { printf("%i", 5); }
8+
void f6 (void) { printf("%i", 6); }
9+
void f7 (void) { printf("%i", 7); }
10+
void f8 (void) { printf("%i", 8); }
11+
void f9 (void) { printf("%i", 9); }
12+
13+
typedef void(*void_fp)(void);
14+
15+
// There is a basic check that excludes all functions that aren't used anywhere
16+
// This ensures that check can't work in this example
17+
const void_fp fp_all[] = {f1, f2 ,f3, f4, f5 ,f6, f7, f8, f9};
18+
19+
void func()
20+
{
21+
void_fp fp = f2;
22+
fp = f3;
23+
fp();
24+
}
25+
26+
void main(){
27+
func();
28+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
CORE
2+
main.c
3+
--show-goto-functions
4+
5+
^Removing function pointers and virtual functions$
6+
^\s*IF fp == f1 THEN GOTO 1$
7+
^\s*IF fp == f2 THEN GOTO 2$
8+
^\s*IF fp == f3 THEN GOTO 3$
9+
^\s*IF fp == f4 THEN GOTO 4$
10+
^\s*IF fp == f5 THEN GOTO 5$
11+
^\s*IF fp == f6 THEN GOTO 6$
12+
^\s*IF fp == f7 THEN GOTO 7$
13+
^\s*IF fp == f8 THEN GOTO 8$
14+
^\s*IF fp == f9 THEN GOTO 9$
15+
^SIGNAL=0$
16+
--
17+
^warning: ignoring
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <stdio.h>
2+
3+
void f1 (void) { printf("%i", 1); }
4+
void f2 (void) { printf("%i", 2); }
5+
void f3 (void) { printf("%i", 3); }
6+
void f4 (void) { printf("%i", 4); }
7+
void f5 (void) { printf("%i", 5); }
8+
void f6 (void) { printf("%i", 6); }
9+
void f7 (void) { printf("%i", 7); }
10+
void f8 (void) { printf("%i", 8); }
11+
void f9 (void) { printf("%i", 9); }
12+
13+
typedef void(*void_fp)(void);
14+
15+
// There is a basic check that excludes all functions that aren't used anywhere
16+
// This ensures that check can't work in this example
17+
const void_fp fp_all[] = {f1, f2 ,f3, f4, f5 ,f6, f7, f8, f9};
18+
19+
void func()
20+
{
21+
const void_fp fp = f2;
22+
const void_fp fp3 = f4;
23+
void_fp fp2 = fp;
24+
fp2 = fp3;
25+
fp2();
26+
}
27+
28+
void main()
29+
{
30+
func();
31+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
CORE
2+
main.c
3+
--show-goto-functions
4+
^Removing function pointers and virtual functions$
5+
^\s*IF fp2 == f1 THEN GOTO 1$
6+
^\s*IF fp2 == f2 THEN GOTO 2$
7+
^\s*IF fp2 == f3 THEN GOTO 3$
8+
^\s*IF fp2 == f4 THEN GOTO 4$
9+
^\s*IF fp2 == f5 THEN GOTO 5$
10+
^\s*IF fp2 == f6 THEN GOTO 6$
11+
^\s*IF fp2 == f7 THEN GOTO 7$
12+
^\s*IF fp2 == f8 THEN GOTO 8$
13+
^\s*IF fp2 == f9 THEN GOTO 9$
14+
^SIGNAL=0$
15+
--
16+
^warning: ignoring

regression/goto-analyzer/fp-removal2/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const void_fp fp_all[] = {f1, f2 ,f3, f4, f5 ,f6, f7, f8, f9};
1919
void func()
2020
{
2121
void_fp fp = f2;
22-
void_fp fp2 = fp;
22+
const void_fp fp2 = fp;
2323
fp2();
2424
}
2525

regression/goto-analyzer/fp-removal3/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const void_fp fp_all[] = {f1, f2 ,f3, f4, f5 ,f6, f7, f8, f9};
2020

2121
void func()
2222
{
23-
void_fp fp = fp_tbl[1];
23+
const void_fp fp = fp_tbl[1];
2424
fp();
2525
}
2626

regression/goto-analyzer/fp-removal4/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const void_fp fp_all[] = {f1, f2 ,f3, f4, f5 ,f6, f7, f8, f9};
2121
void func()
2222
{
2323
int x = 1;
24-
void_fp fp = fp_tbl[x];
24+
const void_fp fp = fp_tbl[x];
2525
fp();
2626
}
2727

regression/goto-analyzer/fp-removal5/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const void_fp fp_tbl[] = {f2, f3 ,f4};
1717
const void_fp fp_all[] = {f1, f2 ,f3, f4, f5 ,f6, f7, f8, f9};
1818

1919
void func(int i){
20-
void_fp fp = fp_tbl[i];
20+
const void_fp fp = fp_tbl[i];
2121
fp();
2222
}
2323

src/goto-programs/remove_function_pointers.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,8 +706,19 @@ void remove_function_pointerst::remove_function_pointer(
706706
to_code_function_call(target->code).function()=precise_call;
707707
return;
708708
}
709+
710+
const c_qualifierst pointer_qualifers(pointer.type());
711+
709712
found_functions=found_functions||try_get_from_address_of(pointer, functions);
710-
found_functions=found_functions||try_get_call_from_symbol(pointer, functions);
713+
714+
// If it is a symbol (except in the case where the symbol is the function
715+
// symbol itself) then the symbol must be const or else can be reassigned.
716+
if(pointer_qualifers.is_constant)
717+
{
718+
found_functions=
719+
found_functions||try_get_call_from_symbol(pointer, functions);
720+
}
721+
711722
found_functions=found_functions||try_get_call_from_index(pointer, functions);
712723

713724
if(functions.size()==1)

0 commit comments

Comments
 (0)