Skip to content

Commit 7e4cdd5

Browse files
Add function pointer restriction regression tests
1 parent 3bef3a9 commit 7e4cdd5

File tree

25 files changed

+662
-0
lines changed

25 files changed

+662
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <assert.h>
2+
3+
typedef int (*fptr_t)(int);
4+
5+
fptr_t get_f(void);
6+
fptr_t get_g(void);
7+
8+
void use_fg(int choice, fptr_t fptr, fptr_t gptr)
9+
{
10+
assert((choice ? fptr : gptr)(10) == 10 + choice);
11+
}
12+
13+
// this is just here to distinguish the behavior from FP removal, which'd include g
14+
int g_always_false_cond = 0;
15+
16+
int main(void)
17+
{
18+
use_fg(0, get_f(), get_g());
19+
use_fg(1, get_f(), get_g());
20+
}
21+
22+
int f(int x)
23+
{
24+
return x + 1;
25+
}
26+
27+
int g(int x)
28+
{
29+
return x;
30+
}
31+
32+
int h(int x)
33+
{
34+
return x / 2;
35+
}
36+
37+
fptr_t get_f(void)
38+
{
39+
if(!g_always_false_cond)
40+
{
41+
return f;
42+
}
43+
else
44+
{
45+
return h;
46+
}
47+
}
48+
49+
fptr_t get_g(void)
50+
{
51+
if(!g_always_false_cond)
52+
{
53+
return g;
54+
}
55+
else
56+
{
57+
return h;
58+
}
59+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
test.c
3+
--restrict-function-pointer "use_fg.function_pointer_call.1/f,g"
4+
\[use_fg.assertion.2\] line \d+ assertion \(choice \? fptr : gptr\)\(10\) == 10 \+ choice: SUCCESS
5+
\[use_fg.assertion.1\] line \d+ dereferenced function pointer at use_fg.function_pointer_call.1 must be one of \[(f|g), (f|g)\]: SUCCESS
6+
^EXIT=0$
7+
^SIGNAL=0$
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"use_fg.function_pointer_call.1": ["f"]
3+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <assert.h>
2+
3+
typedef int (*fptr_t)(int);
4+
5+
fptr_t get_f(void);
6+
7+
void use_f(fptr_t fptr)
8+
{
9+
assert(fptr(10) >= 10);
10+
}
11+
12+
void select_f(void);
13+
void select_g(void);
14+
void select_h(void);
15+
16+
int main(void)
17+
{
18+
select_f();
19+
use_f(get_f());
20+
select_g();
21+
use_f(get_f());
22+
}
23+
24+
int f(int x)
25+
{
26+
return x + 1;
27+
}
28+
int g(int x)
29+
{
30+
return x;
31+
}
32+
int h(int x)
33+
{
34+
return x - 1;
35+
}
36+
37+
int g_select_function = 0;
38+
39+
void select_f(void)
40+
{
41+
g_select_function = 0;
42+
}
43+
void select_g(void)
44+
{
45+
g_select_function = 1;
46+
}
47+
void select_h(void)
48+
{
49+
g_select_function = 2;
50+
}
51+
52+
fptr_t get_f(void)
53+
{
54+
if(g_select_function == 0)
55+
{
56+
return f;
57+
}
58+
else if(g_select_function == 1)
59+
{
60+
return g;
61+
}
62+
else
63+
{
64+
return h;
65+
}
66+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
test.c
3+
--restrict-function-pointer use_f.function_pointer_call.1/f,g --show-goto-functions
4+
f\(10\)
5+
g\(10\)
6+
^EXIT=0$
7+
^SIGNAL=0$
8+
--
9+
h\(10\)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <assert.h>
2+
3+
typedef int (*fptr_t)(int);
4+
5+
fptr_t get_f(void);
6+
7+
void use_f(fptr_t fptr)
8+
{
9+
assert(fptr(10) >= 10);
10+
}
11+
12+
void select_f(void);
13+
void select_g(void);
14+
void select_h(void);
15+
16+
int main(void)
17+
{
18+
select_f();
19+
use_f(get_f());
20+
select_g();
21+
use_f(get_f());
22+
select_h();
23+
use_f(get_f());
24+
}
25+
26+
int f(int x)
27+
{
28+
return x + 1;
29+
}
30+
int g(int x)
31+
{
32+
return x;
33+
}
34+
int h(int x)
35+
{
36+
return x - 1;
37+
}
38+
39+
int g_select_function = 0;
40+
41+
void select_f(void)
42+
{
43+
g_select_function = 0;
44+
}
45+
void select_g(void)
46+
{
47+
g_select_function = 1;
48+
}
49+
void select_h(void)
50+
{
51+
g_select_function = 2;
52+
}
53+
54+
fptr_t get_f(void)
55+
{
56+
if(g_select_function == 0)
57+
{
58+
return f;
59+
}
60+
else if(g_select_function == 1)
61+
{
62+
return g;
63+
}
64+
else
65+
{
66+
return h;
67+
}
68+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CORE
2+
test.c
3+
--restrict-function-pointer use_f.function_pointer_call.1/f,g
4+
\[use_f\.assertion\.1\] line \d+ dereferenced function pointer at use_f.function_pointer_call.1 must be one of \[(f|g), (f|g)\]: FAILURE
5+
^EXIT=10$
6+
^SIGNAL=0$
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"use_f.function_pointer_call.1": ["f"]
3+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <assert.h>
2+
3+
typedef int (*fptr_t)(int);
4+
5+
fptr_t get_f(void);
6+
7+
void use_f(fptr_t fptr)
8+
{
9+
assert(fptr(10) >= 10);
10+
}
11+
12+
void select_f(void);
13+
void select_g(void);
14+
void select_h(void);
15+
16+
int main(void)
17+
{
18+
select_f();
19+
use_f(get_f());
20+
select_g();
21+
use_f(get_f());
22+
select_h();
23+
use_f(get_f());
24+
}
25+
26+
int f(int x)
27+
{
28+
return x + 1;
29+
}
30+
int g(int x)
31+
{
32+
return x;
33+
}
34+
int h(int x)
35+
{
36+
return x - 1;
37+
}
38+
39+
int g_select_function = 0;
40+
41+
void select_f(void)
42+
{
43+
g_select_function = 0;
44+
}
45+
void select_g(void)
46+
{
47+
g_select_function = 1;
48+
}
49+
void select_h(void)
50+
{
51+
g_select_function = 2;
52+
}
53+
54+
fptr_t get_f(void)
55+
{
56+
if(g_select_function == 0)
57+
{
58+
return f;
59+
}
60+
else if(g_select_function == 1)
61+
{
62+
return g;
63+
}
64+
else
65+
{
66+
return h;
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CORE
2+
test.c
3+
--function-pointer-restrictions-file restrictions.json --restrict-function-pointer use_f.function_pointer_call.1/g
4+
\[use_f\.assertion\.1\] line \d+ dereferenced function pointer at use_f.function_pointer_call.1 must be one of \[(f|g), (f|g)\]: FAILURE
5+
^EXIT=10$
6+
^SIGNAL=0$

0 commit comments

Comments
 (0)