Skip to content

Commit 07bafd2

Browse files
author
Daniel Kroening
committed
move CPROVER built-in functions into the factory
1 parent 7f3ba30 commit 07bafd2

7 files changed

+125
-102
lines changed

src/ansi-c/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ add_custom_target(library_check
5858

5959
make_inc(arm_builtin_headers)
6060
make_inc(clang_builtin_headers)
61+
make_inc(cprover_builtin_headers)
6162
make_inc(cw_builtin_headers)
6263
make_inc(gcc_builtin_headers_alpha)
6364
make_inc(gcc_builtin_headers_arm)
@@ -74,11 +75,13 @@ make_inc(gcc_builtin_headers_power)
7475
make_inc(gcc_builtin_headers_tm)
7576
make_inc(gcc_builtin_headers_types)
7677
make_inc(gcc_builtin_headers_ubsan)
78+
make_inc(windows_builtin_headers)
7779

7880
set(extra_dependencies
7981
${CMAKE_CURRENT_BINARY_DIR}/arm_builtin_headers.inc
8082
${CMAKE_CURRENT_BINARY_DIR}/clang_builtin_headers.inc
8183
${CMAKE_CURRENT_BINARY_DIR}/cprover_library.inc
84+
${CMAKE_CURRENT_BINARY_DIR}/cprover_builtin_headers.inc
8285
${CMAKE_CURRENT_BINARY_DIR}/cw_builtin_headers.inc
8386
${CMAKE_CURRENT_BINARY_DIR}/gcc_builtin_headers_alpha.inc
8487
${CMAKE_CURRENT_BINARY_DIR}/gcc_builtin_headers_arm.inc
@@ -95,6 +98,7 @@ set(extra_dependencies
9598
${CMAKE_CURRENT_BINARY_DIR}/gcc_builtin_headers_tm.inc
9699
${CMAKE_CURRENT_BINARY_DIR}/gcc_builtin_headers_types.inc
97100
${CMAKE_CURRENT_BINARY_DIR}/gcc_builtin_headers_ubsan.inc
101+
${CMAKE_CURRENT_BINARY_DIR}/windows_builtin_headers.inc
98102
${CMAKE_CURRENT_BINARY_DIR}/library-check.stamp
99103
)
100104

src/ansi-c/Makefile

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ include ../common
4747
BUILTIN_FILES = \
4848
arm_builtin_headers.inc \
4949
clang_builtin_headers.inc \
50+
cprover_builtin_headers.inc \
5051
cw_builtin_headers.inc \
5152
gcc_builtin_headers_alpha.inc \
5253
gcc_builtin_headers_arm.inc \
@@ -62,7 +63,8 @@ BUILTIN_FILES = \
6263
gcc_builtin_headers_power.inc \
6364
gcc_builtin_headers_tm.inc \
6465
gcc_builtin_headers_types.inc \
65-
gcc_builtin_headers_ubsan.inc
66+
gcc_builtin_headers_ubsan.inc \
67+
windows_builtin_headers.inc
6668

6769
CLEANFILES = ansi-c$(LIBEXT) \
6870
ansi_c_y.tab.h ansi_c_y.tab.cpp ansi_c_lex.yy.cpp ansi_c_y.tab.cpp.output \

src/ansi-c/ansi_c_internal_additions.cpp

+15-101
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,15 @@ const char clang_builtin_headers[]=
9494
#include "clang_builtin_headers.inc"
9595
; // NOLINT(whitespace/semicolon)
9696

97+
const char cprover_builtin_headers[]=
98+
"# 1 \"cprover_builtin_headers.h\"\n"
99+
#include "cprover_builtin_headers.inc"
100+
; // NOLINT(whitespace/semicolon)
101+
97102
const char windows_builtin_headers[]=
98-
"int __noop();\n"
99-
"int __assume(int);\n";
103+
"# 1 \"windows_builtin_headers.h\"\n"
104+
#include "windows_builtin_headers.inc"
105+
; // NOLINT(whitespace/semicolon)
100106

101107
static std::string architecture_string(const std::string &value, const char *s)
102108
{
@@ -114,68 +120,28 @@ static std::string architecture_string(int value, const char *s)
114120

115121
void ansi_c_internal_additions(std::string &code)
116122
{
123+
// do the built-in types and variables
117124
code+=
118125
"# 1 \"<built-in-additions>\"\n"
119126
"typedef __typeof__(sizeof(int)) __CPROVER_size_t;\n"
120-
"void __CPROVER_assume(__CPROVER_bool assumption);\n"
121-
"void __VERIFIER_assume(__CPROVER_bool assumption);\n"
122-
// NOLINTNEXTLINE(whitespace/line_length)
123-
"void __CPROVER_assert(__CPROVER_bool assertion, const char *description);\n"
124-
// NOLINTNEXTLINE(whitespace/line_length)
125-
"void __CPROVER_precondition(__CPROVER_bool precondition, const char *description);\n"
126-
"void __CPROVER_havoc_object(void *);\n"
127-
"__CPROVER_bool __CPROVER_equal();\n"
128-
"__CPROVER_bool __CPROVER_same_object(const void *, const void *);\n"
129-
"__CPROVER_bool __CPROVER_invalid_pointer(const void *);\n"
130-
"__CPROVER_bool __CPROVER_is_zero_string(const void *);\n"
131-
"__CPROVER_size_t __CPROVER_zero_string_length(const void *);\n"
132-
"__CPROVER_size_t __CPROVER_buffer_size(const void *);\n"
133-
134-
"__CPROVER_bool __CPROVER_get_flag(const void *, const char *);\n"
135-
"void __CPROVER_set_must(const void *, const char *);\n"
136-
"void __CPROVER_clear_must(const void *, const char *);\n"
137-
"void __CPROVER_set_may(const void *, const char *);\n"
138-
"void __CPROVER_clear_may(const void *, const char *);\n"
139-
"void __CPROVER_cleanup(const void *, const void *);\n"
140-
"__CPROVER_bool __CPROVER_get_must(const void *, const char *);\n"
141-
"__CPROVER_bool __CPROVER_get_may(const void *, const char *);\n"
142-
143127
"const unsigned __CPROVER_constant_infinity_uint;\n"
144128
"typedef void __CPROVER_integer;\n"
145129
"typedef void __CPROVER_rational;\n"
146-
"void __CPROVER_initialize(void);\n"
147-
"void __CPROVER_input(const char *id, ...);\n"
148-
"void __CPROVER_output(const char *id, ...);\n"
149-
"void __CPROVER_cover(__CPROVER_bool condition);\n"
150-
151-
// concurrency-related
152-
"void __CPROVER_atomic_begin();\n"
153-
"void __CPROVER_atomic_end();\n"
154-
"void __CPROVER_fence(const char *kind, ...);\n"
155130
"__CPROVER_thread_local unsigned long __CPROVER_thread_id=0;\n"
156131
// NOLINTNEXTLINE(whitespace/line_length)
157132
"__CPROVER_bool __CPROVER_threads_exited[__CPROVER_constant_infinity_uint];\n"
158133
"unsigned long __CPROVER_next_thread_id=0;\n"
159-
160-
// traces
161-
"void CBMC_trace(int lvl, const char *event, ...);\n"
162-
163-
// pointers
164-
"unsigned __CPROVER_POINTER_OBJECT(const void *p);\n"
165-
"signed __CPROVER_POINTER_OFFSET(const void *p);\n"
166-
"__CPROVER_bool __CPROVER_DYNAMIC_OBJECT(const void *p);\n"
167-
"extern unsigned char __CPROVER_memory[__CPROVER_constant_infinity_uint];\n"
168134
// NOLINTNEXTLINE(whitespace/line_length)
169-
"void __CPROVER_allocated_memory(__CPROVER_size_t address, __CPROVER_size_t extent);\n"
135+
"extern unsigned char __CPROVER_memory[__CPROVER_constant_infinity_uint];\n"
170136

171137
// malloc
172-
"void *__CPROVER_allocate(__CPROVER_size_t size, __CPROVER_bool zero);\n"
173138
"const void *__CPROVER_deallocated=0;\n"
174139
"const void *__CPROVER_dead_object=0;\n"
175140
"const void *__CPROVER_malloc_object=0;\n"
176141
"__CPROVER_size_t __CPROVER_malloc_size;\n"
177142
"__CPROVER_bool __CPROVER_malloc_is_new_array=0;\n" // for C++
178143
"const void *__CPROVER_memory_leak=0;\n"
144+
"void *__CPROVER_allocate(__CPROVER_size_t size, __CPROVER_bool zero);\n"
179145

180146
// this is ANSI-C
181147
// NOLINTNEXTLINE(whitespace/line_length)
@@ -188,63 +154,8 @@ void ansi_c_internal_additions(std::string &code)
188154
"extern __CPROVER_thread_local const char __PRETTY_FUNCTION__[__CPROVER_constant_infinity_uint];\n"
189155

190156
// float stuff
191-
"__CPROVER_bool __CPROVER_isnanf(float f);\n"
192-
"__CPROVER_bool __CPROVER_isnand(double f);\n"
193-
"__CPROVER_bool __CPROVER_isnanld(long double f);\n"
194-
"__CPROVER_bool __CPROVER_isfinitef(float f);\n"
195-
"__CPROVER_bool __CPROVER_isfinited(double f);\n"
196-
"__CPROVER_bool __CPROVER_isfiniteld(long double f);\n"
197-
"__CPROVER_bool __CPROVER_isinff(float f);\n"
198-
"__CPROVER_bool __CPROVER_isinfd(double f);\n"
199-
"__CPROVER_bool __CPROVER_isinfld(long double f);\n"
200-
"__CPROVER_bool __CPROVER_isnormalf(float f);\n"
201-
"__CPROVER_bool __CPROVER_isnormald(double f);\n"
202-
"__CPROVER_bool __CPROVER_isnormalld(long double f);\n"
203-
"__CPROVER_bool __CPROVER_signf(float f);\n"
204-
"__CPROVER_bool __CPROVER_signd(double f);\n"
205-
"__CPROVER_bool __CPROVER_signld(long double f);\n"
206-
"double __CPROVER_inf(void);\n"
207-
"float __CPROVER_inff(void);\n"
208-
"long double __CPROVER_infl(void);\n"
209157
"int __CPROVER_thread_local __CPROVER_rounding_mode="+
210158
std::to_string(config.ansi_c.rounding_mode)+";\n"
211-
"int __CPROVER_isgreaterf(float f, float g);\n"
212-
"int __CPROVER_isgreaterd(double f, double g);\n"
213-
"int __CPROVER_isgreaterequalf(float f, float g);\n"
214-
"int __CPROVER_isgreaterequald(double f, double g);\n"
215-
"int __CPROVER_islessf(float f, float g);\n"
216-
"int __CPROVER_islessd(double f, double g);\n"
217-
"int __CPROVER_islessequalf(float f, float g);\n"
218-
"int __CPROVER_islessequald(double f, double g);\n"
219-
"int __CPROVER_islessgreaterf(float f, float g);\n"
220-
"int __CPROVER_islessgreaterd(double f, double g);\n"
221-
"int __CPROVER_isunorderedf(float f, float g);\n"
222-
"int __CPROVER_isunorderedd(double f, double g);\n"
223-
224-
// absolute value
225-
"int __CPROVER_abs(int x);\n"
226-
"long int __CPROVER_labs(long int x);\n"
227-
"long int __CPROVER_llabs(long long int x);\n"
228-
"double __CPROVER_fabs(double x);\n"
229-
"long double __CPROVER_fabsl(long double x);\n"
230-
"float __CPROVER_fabsf(float x);\n"
231-
232-
// arrays
233-
// NOLINTNEXTLINE(whitespace/line_length)
234-
"__CPROVER_bool __CPROVER_array_equal(const void *array1, const void *array2);\n"
235-
// overwrite all of *dest (possibly using nondet values), even
236-
// if *src is smaller
237-
"void __CPROVER_array_copy(const void *dest, const void *src);\n"
238-
// replace at most size-of-*src bytes in *dest
239-
"void __CPROVER_array_replace(const void *dest, const void *src);\n"
240-
"void __CPROVER_array_set(const void *dest, ...);\n"
241-
242-
// k-induction
243-
"void __CPROVER_k_induction_hint(unsigned min, unsigned max, "
244-
"unsigned step, unsigned loop_free);\n"
245-
246-
// format string-related
247-
"int __CPROVER_scanf(const char *, ...);\n"
248159

249160
// pipes, write, read, close
250161
"struct __CPROVER_pipet {\n"
@@ -258,7 +169,10 @@ void ansi_c_internal_additions(std::string &code)
258169
// offset to make sure we don't collide with other fds
259170
"extern const int __CPROVER_pipe_offset;\n"
260171
"unsigned __CPROVER_pipe_count=0;\n"
261-
172+
"\n"
173+
// This function needs to be declared, or otherwise can't be called
174+
// by the entry-point construction.
175+
"void __CPROVER_initialize(void);\n"
262176
"\n";
263177

264178
// GCC junk stuff, also for CLANG and ARM

src/ansi-c/ansi_c_internal_additions.h

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ void ansi_c_internal_additions(std::string &code);
1616
void ansi_c_architecture_strings(std::string &code);
1717

1818
extern const char clang_builtin_headers[];
19+
extern const char cprover_builtin_headers[];
1920
extern const char gcc_builtin_headers_types[];
2021
extern const char gcc_builtin_headers_generic[];
2122
extern const char gcc_builtin_headers_math[];

src/ansi-c/builtin_factory.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ bool builtin_factory(
108108
ansi_c_internal_additions(code);
109109
s << code;
110110

111+
// our own extensions
112+
if(find_pattern(pattern, cprover_builtin_headers, s))
113+
return convert(identifier, s, symbol_table, mh);
114+
111115
// this is Visual C/C++ only
112116
if(config.ansi_c.os==configt::ansi_ct::ost::OS_WIN)
113117
{

src/ansi-c/cprover_builtin_headers.h

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
void __CPROVER_assume(__CPROVER_bool assumption);
2+
void __VERIFIER_assume(__CPROVER_bool assumption);
3+
void __CPROVER_assert(__CPROVER_bool assertion, const char *description);
4+
void __CPROVER_precondition(__CPROVER_bool precondition, const char *description);
5+
void __CPROVER_havoc_object(void *);
6+
__CPROVER_bool __CPROVER_equal();
7+
__CPROVER_bool __CPROVER_same_object(const void *, const void *);
8+
__CPROVER_bool __CPROVER_invalid_pointer(const void *);
9+
__CPROVER_bool __CPROVER_is_zero_string(const void *);
10+
__CPROVER_size_t __CPROVER_zero_string_length(const void *);
11+
__CPROVER_size_t __CPROVER_buffer_size(const void *);
12+
13+
// bitvector analysis
14+
__CPROVER_bool __CPROVER_get_flag(const void *, const char *);
15+
void __CPROVER_set_must(const void *, const char *);
16+
void __CPROVER_clear_must(const void *, const char *);
17+
void __CPROVER_set_may(const void *, const char *);
18+
void __CPROVER_clear_may(const void *, const char *);
19+
void __CPROVER_cleanup(const void *, const void *);
20+
__CPROVER_bool __CPROVER_get_must(const void *, const char *);
21+
__CPROVER_bool __CPROVER_get_may(const void *, const char *);
22+
23+
void __CPROVER_input(const char *id, ...);
24+
void __CPROVER_output(const char *id, ...);
25+
void __CPROVER_cover(__CPROVER_bool condition);
26+
27+
// concurrency-related
28+
void __CPROVER_atomic_begin();
29+
void __CPROVER_atomic_end();
30+
void __CPROVER_fence(const char *kind, ...);
31+
32+
// traces
33+
void CBMC_trace(int lvl, const char *event, ...);
34+
35+
// pointers
36+
unsigned __CPROVER_POINTER_OBJECT(const void *p);
37+
signed __CPROVER_POINTER_OFFSET(const void *p);
38+
__CPROVER_bool __CPROVER_DYNAMIC_OBJECT(const void *p);
39+
void __CPROVER_allocated_memory(__CPROVER_size_t address, __CPROVER_size_t extent);
40+
41+
// float stuff
42+
__CPROVER_bool __CPROVER_isnanf(float f);
43+
__CPROVER_bool __CPROVER_isnand(double f);
44+
__CPROVER_bool __CPROVER_isnanld(long double f);
45+
__CPROVER_bool __CPROVER_isfinitef(float f);
46+
__CPROVER_bool __CPROVER_isfinited(double f);
47+
__CPROVER_bool __CPROVER_isfiniteld(long double f);
48+
__CPROVER_bool __CPROVER_isinff(float f);
49+
__CPROVER_bool __CPROVER_isinfd(double f);
50+
__CPROVER_bool __CPROVER_isinfld(long double f);
51+
__CPROVER_bool __CPROVER_isnormalf(float f);
52+
__CPROVER_bool __CPROVER_isnormald(double f);
53+
__CPROVER_bool __CPROVER_isnormalld(long double f);
54+
__CPROVER_bool __CPROVER_signf(float f);
55+
__CPROVER_bool __CPROVER_signd(double f);
56+
__CPROVER_bool __CPROVER_signld(long double f);
57+
double __CPROVER_inf(void);
58+
float __CPROVER_inff(void);
59+
long double __CPROVER_infl(void);
60+
int __CPROVER_isgreaterf(float f, float g);
61+
int __CPROVER_isgreaterd(double f, double g);
62+
int __CPROVER_isgreaterequalf(float f, float g);
63+
int __CPROVER_isgreaterequald(double f, double g);
64+
int __CPROVER_islessf(float f, float g);
65+
int __CPROVER_islessd(double f, double g);
66+
int __CPROVER_islessequalf(float f, float g);
67+
int __CPROVER_islessequald(double f, double g);
68+
int __CPROVER_islessgreaterf(float f, float g);
69+
int __CPROVER_islessgreaterd(double f, double g);
70+
int __CPROVER_isunorderedf(float f, float g);
71+
int __CPROVER_isunorderedd(double f, double g);
72+
73+
// absolute value
74+
int __CPROVER_abs(int x);
75+
long int __CPROVER_labs(long int x);
76+
long int __CPROVER_llabs(long long int x);
77+
double __CPROVER_fabs(double x);
78+
long double __CPROVER_fabsl(long double x);
79+
float __CPROVER_fabsf(float x);
80+
81+
// arrays
82+
__CPROVER_bool __CPROVER_array_equal(const void *array1, const void *array2);
83+
// overwrite all of *dest (possibly using nondet values), even
84+
// if *src is smaller
85+
void __CPROVER_array_copy(const void *dest, const void *src);
86+
// replace at most size-of-*src bytes in *dest
87+
void __CPROVER_array_replace(const void *dest, const void *src);
88+
void __CPROVER_array_set(const void *dest, ...);
89+
90+
// k-induction
91+
void __CPROVER_k_induction_hint(unsigned min, unsigned max,
92+
unsigned step, unsigned loop_free);
93+
94+
// format string-related
95+
int __CPROVER_scanf(const char *, ...);
96+

src/ansi-c/windows_builtin_headers.h

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
int __noop();
2+
int __assume(int);

0 commit comments

Comments
 (0)