Skip to content

Commit 816b844

Browse files
committed
Now CBMC supports the wrap-entry-point-in-while flag correctly.
1 parent 57af54c commit 816b844

File tree

9 files changed

+77
-9
lines changed

9 files changed

+77
-9
lines changed

src/ansi-c/ansi_c_entry_point.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Author: Daniel Kroening, [email protected]
2525
#include <linking/static_lifetime_init.h>
2626

2727
#include "ansi_c_entry_point.h"
28+
#include "ansi_c_language.h"
2829
#include "c_nondet_symbol_factory.h"
2930

3031
exprt::operandst build_function_environment(
@@ -121,7 +122,8 @@ void record_function_outputs(
121122
bool ansi_c_entry_point(
122123
symbol_tablet &symbol_table,
123124
const std::string &standard_main,
124-
message_handlert &message_handler)
125+
message_handlert &message_handler,
126+
bool wrap_entry_point)
125127
{
126128
// check if entry point is already there
127129
if(symbol_table.symbols.find(goto_functionst::entry_point())!=
@@ -444,7 +446,12 @@ bool ansi_c_entry_point(
444446
message_handler);
445447
}
446448

447-
init_code.move_to_operands(call_main);
449+
if (wrap_entry_point) {
450+
auto wrapped_main = wrap_entry_point_in_while(call_main);
451+
init_code.move_to_operands(wrapped_main);
452+
} else {
453+
init_code.move_to_operands(call_main);
454+
}
448455

449456
// TODO: add read/modified (recursively in call graph) globals as INPUT/OUTPUT
450457

@@ -471,3 +478,16 @@ bool ansi_c_entry_point(
471478

472479
return false;
473480
}
481+
482+
// Build and return a while(true) statement nesting the function call
483+
// passed as a parameter.
484+
code_whilet wrap_entry_point_in_while(code_function_callt &call_main)
485+
{
486+
exprt true_expr;
487+
code_whilet while_expr;
488+
true_expr.make_true();
489+
while_expr.cond() = true_expr;
490+
while_expr.body() = call_main;
491+
492+
return while_expr;
493+
}

src/ansi-c/ansi_c_entry_point.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@ Author: Daniel Kroening, [email protected]
1212

1313
#include <util/symbol_table.h>
1414
#include <util/message.h>
15+
#include <util/std_code.h>
1516

1617
bool ansi_c_entry_point(
1718
symbol_tablet &symbol_table,
1819
const std::string &standard_main,
19-
message_handlert &message_handler);
20+
message_handlert &message_handler,
21+
bool wrap_entry_point);
22+
23+
code_whilet wrap_entry_point_in_while(
24+
code_function_callt &call_main);
2025

2126
#endif // CPROVER_ANSI_C_ANSI_C_ENTRY_POINT_H

src/ansi-c/ansi_c_language.cpp

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

1414
#include <util/config.h>
1515
#include <util/get_base_name.h>
16+
#include <util/cmdline.h>
1617

1718
#include <linking/linking.h>
1819
#include <linking/remove_internal_symbols.h>
@@ -26,6 +27,14 @@ Author: Daniel Kroening, [email protected]
2627
#include "ansi_c_internal_additions.h"
2728
#include "type2name.h"
2829

30+
void ansi_c_languaget::get_language_options(const cmdlinet &cmd)
31+
{
32+
if (cmd.isset("wrap-entry-point-in-while"))
33+
{
34+
wrap_entry_point = true;
35+
}
36+
}
37+
2938
std::set<std::string> ansi_c_languaget::extensions() const
3039
{
3140
return { "c", "i" };
@@ -126,7 +135,8 @@ bool ansi_c_languaget::typecheck(
126135

127136
bool ansi_c_languaget::final(symbol_tablet &symbol_table)
128137
{
129-
if(ansi_c_entry_point(symbol_table, "main", get_message_handler()))
138+
if(ansi_c_entry_point(symbol_table, "main", get_message_handler(),
139+
wrap_entry_point_in_while()))
130140
return true;
131141

132142
return false;
@@ -211,7 +221,6 @@ bool ansi_c_languaget::to_expr(
211221
expr.type().id()==ID_empty &&
212222
expr.operands().size()==1)
213223
expr=expr.op0();
214-
215224
return result;
216225
}
217226

src/ansi-c/ansi_c_language.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ Author: Daniel Kroening, [email protected]
2222
class ansi_c_languaget:public languaget
2323
{
2424
public:
25+
virtual void get_language_options(
26+
const cmdlinet &) override;
27+
2528
bool preprocess(
2629
std::istream &instream,
2730
const std::string &path,
@@ -67,6 +70,9 @@ class ansi_c_languaget:public languaget
6770
languaget *new_language() override
6871
{ return new ansi_c_languaget; }
6972

73+
bool wrap_entry_point_in_while()
74+
{ return wrap_entry_point; }
75+
7076
std::string id() const override { return "C"; }
7177
std::string description() const override { return "ANSI-C 99"; }
7278
std::set<std::string> extensions() const override;
@@ -76,6 +82,9 @@ class ansi_c_languaget:public languaget
7682
protected:
7783
ansi_c_parse_treet parse_tree;
7884
std::string parse_path;
85+
86+
private:
87+
bool wrap_entry_point;
7988
};
8089

8190
languaget *new_ansi_c_language();

src/cbmc/cbmc_parse_options.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,16 @@ Author: Daniel Kroening, [email protected]
5757

5858
#include <langapi/mode.h>
5959

60+
#include <ansi-c/ansi_c_language.h>
61+
6062
#include "cbmc_solvers.h"
6163
#include "cbmc_parse_options.h"
6264
#include "bmc.h"
6365
#include "version.h"
6466
#include "xml_interface.h"
6567

68+
69+
6670
cbmc_parse_optionst::cbmc_parse_optionst(int argc, const char **argv):
6771
parse_options_baset(CBMC_OPTIONS, argc, argv),
6872
xml_interfacet(cmdline),
@@ -181,6 +185,9 @@ void cbmc_parse_optionst::get_command_line_options(optionst &options)
181185
cmdline.get_value("localize-faults-method"));
182186
}
183187

188+
if (cmdline.isset("wrap-entry-point-in-while"))
189+
options.set_option("wrap-entry-point-in-while", true);
190+
184191
if(cmdline.isset("unwind"))
185192
options.set_option("unwind", cmdline.get_value("unwind"));
186193

@@ -597,7 +604,6 @@ int cbmc_parse_optionst::get_goto_program(
597604
}
598605

599606
language->set_message_handler(get_message_handler());
600-
601607
status() << "Parsing " << filename << eom;
602608

603609
if(language->parse(infile, filename))

src/cbmc/cbmc_parse_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class optionst;
6363
"(java-cp-include-files):" \
6464
"(localize-faults)(localize-faults-method):" \
6565
"(lazy-methods)" \
66+
"(wrap-entry-point-in-while)" \
6667
"(test-invariant-failure)" \
6768
"(fixedbv)(floatbv)(all-claims)(all-properties)" // legacy, and will eventually disappear // NOLINT(whitespace/line_length)
6869

src/cpp/cpp_language.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Author: Daniel Kroening, [email protected]
1616
#include <util/config.h>
1717
#include <util/replace_symbol.h>
1818
#include <util/get_base_name.h>
19-
19+
#include <util/cmdline.h>
2020
#include <linking/linking.h>
2121

2222
#include <ansi-c/ansi_c_entry_point.h>
@@ -29,6 +29,14 @@ Author: Daniel Kroening, [email protected]
2929
#include "cpp_typecheck.h"
3030
#include "cpp_type2name.h"
3131

32+
void cpp_languaget::get_language_options(const cmdlinet &cmd)
33+
{
34+
if (cmd.isset("wrap-entry-point-in-while"))
35+
{
36+
wrap_entry_point = true;
37+
}
38+
}
39+
3240
std::set<std::string> cpp_languaget::extensions() const
3341
{
3442
std::set<std::string> s;
@@ -135,7 +143,8 @@ bool cpp_languaget::typecheck(
135143

136144
bool cpp_languaget::final(symbol_tablet &symbol_table)
137145
{
138-
if(ansi_c_entry_point(symbol_table, "main", get_message_handler()))
146+
if(ansi_c_entry_point(symbol_table, "main", get_message_handler(),
147+
wrap_entry_point_in_while()))
139148
return true;
140149

141150
return false;

src/cpp/cpp_language.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Author: Daniel Kroening, [email protected]
2424
class cpp_languaget:public languaget
2525
{
2626
public:
27+
virtual void get_language_options(
28+
const cmdlinet &cmd) override;
29+
2730
bool preprocess(
2831
std::istream &instream,
2932
const std::string &path,
@@ -85,6 +88,9 @@ class cpp_languaget:public languaget
8588

8689
void modules_provided(std::set<std::string> &modules) override;
8790

91+
bool wrap_entry_point_in_while()
92+
{ return wrap_entry_point; }
93+
8894
protected:
8995
cpp_parse_treet cpp_parse_tree;
9096
std::string parse_path;
@@ -95,6 +101,9 @@ class cpp_languaget:public languaget
95101
{
96102
return "main";
97103
}
104+
105+
private:
106+
bool wrap_entry_point;
98107
};
99108

100109
languaget *new_cpp_language();

src/goto-cc/compile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ bool compilet::link()
368368
symbol_table.remove(goto_functionst::entry_point());
369369
compiled_functions.function_map.erase(goto_functionst::entry_point());
370370

371-
if(ansi_c_entry_point(symbol_table, "main", get_message_handler()))
371+
if(ansi_c_entry_point(symbol_table, "main", get_message_handler(), false))
372372
return true;
373373

374374
// entry_point may (should) add some more functions.

0 commit comments

Comments
 (0)