cprover
model_argc_argv.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Initialize command line arguments
4 
5 Author: Michael Tautschnig
6 
7 Date: April 2016
8 
9 \*******************************************************************/
10 
13 
14 #include "model_argc_argv.h"
15 
16 #include <sstream>
17 
18 #include <util/cprover_prefix.h>
19 #include <util/invariant.h>
20 #include <util/message.h>
21 #include <util/namespace.h>
22 #include <util/config.h>
23 #include <util/replace_symbol.h>
24 #include <util/symbol_table.h>
25 #include <util/prefix.h>
26 
27 #include <ansi-c/ansi_c_language.h>
28 
32 
40  goto_modelt &goto_model,
41  unsigned max_argc,
43 {
44  messaget message(message_handler);
45  const namespacet ns(goto_model.symbol_table);
46 
47  if(!goto_model.symbol_table.has_symbol(
48  goto_model.goto_functions.entry_point()))
49  {
50  message.error() << "Linking not done, missing "
51  << goto_model.goto_functions.entry_point()
52  << messaget::eom;
53  return true;
54  }
55 
56  const symbolt &main_symbol=
57  ns.lookup(config.main.empty()?ID_main:config.main);
58 
59  if(main_symbol.mode!=ID_C)
60  {
61  message.error() << "argc/argv modelling is C specific"
62  << messaget::eom;
63  return true;
64  }
65 
66  const code_typet::parameterst &parameters=
67  to_code_type(main_symbol.type).parameters();
68  if(parameters.size()!=2 &&
69  parameters.size()!=3)
70  {
71  message.warning() << "main expected to take 2 or 3 arguments,"
72  << " argc/argv instrumentation skipped"
73  << messaget::eom;
74  return false;
75  }
76 
77  // set the size of ARGV storage to 4096, which matches the minimum
78  // guaranteed by POSIX (_POSIX_ARG_MAX):
79  // http://pubs.opengroup.org/onlinepubs/009695399/basedefs/limits.h.html
80  std::ostringstream oss;
81  oss << "int ARGC;\n"
82  << "char *ARGV[1];\n"
83  << "void " << goto_model.goto_functions.entry_point() << "()\n"
84  << "{\n"
85  << " unsigned next=0u;\n"
86  << " " CPROVER_PREFIX "assume(ARGC>=1);\n"
87  << " " CPROVER_PREFIX "assume(ARGC<=" << max_argc << ");\n"
88  << " char arg_string[4096];\n"
89  << " __CPROVER_input(\"arg_string\", &arg_string[0]);\n"
90  << " for(int i=0; i<ARGC && i<" << max_argc << "; ++i)\n"
91  << " {\n"
92  << " unsigned len;\n"
93  << " " CPROVER_PREFIX "assume(len<4096);\n"
94  << " " CPROVER_PREFIX "assume(next+len<4096);\n"
95  << " " CPROVER_PREFIX "assume(arg_string[next+len]==0);\n"
96  << " ARGV[i]=&(arg_string[next]);\n"
97  << " next+=len+1;\n"
98  << " }\n"
99  << "}";
100  std::istringstream iss(oss.str());
101 
102  ansi_c_languaget ansi_c_language;
103  ansi_c_language.set_message_handler(message_handler);
106  ansi_c_language.parse(iss, "");
108 
109  symbol_tablet tmp_symbol_table;
110  ansi_c_language.typecheck(tmp_symbol_table, "<built-in-library>");
111 
112  goto_programt init_instructions;
113  exprt value=nil_exprt();
114  // locate the body of the newly built start function as well as any
115  // additional declarations we might need; the body will then be
116  // converted and inserted into the start function
117  for(const auto &symbol_pair : tmp_symbol_table.symbols)
118  {
119  // add __CPROVER_assume if necessary (it might exist already)
120  if(
121  symbol_pair.first == CPROVER_PREFIX "assume" ||
122  symbol_pair.first == CPROVER_PREFIX "input")
123  goto_model.symbol_table.add(symbol_pair.second);
124  else if(symbol_pair.first == goto_model.goto_functions.entry_point())
125  {
126  value = symbol_pair.second.value;
127 
128  replace_symbolt replace;
129  replace.insert("ARGC", ns.lookup("argc'").symbol_expr());
130  replace.insert("ARGV", ns.lookup("argv'").symbol_expr());
131  replace(value);
132  }
133  else if(
134  has_prefix(
135  id2string(symbol_pair.first),
136  id2string(goto_model.goto_functions.entry_point()) + "::") &&
137  goto_model.symbol_table.add(symbol_pair.second))
138  UNREACHABLE;
139  }
140  POSTCONDITION(value.is_not_nil());
141 
142  goto_convert(
143  to_code(value),
144  goto_model.symbol_table,
145  init_instructions,
147  main_symbol.mode);
148 
149  Forall_goto_program_instructions(it, init_instructions)
150  {
151  it->source_location.set_file("<built-in-library>");
152  it->function=goto_model.goto_functions.entry_point();
153  }
154 
155  goto_functionst::function_mapt::iterator start_entry=
156  goto_model.goto_functions.function_map.find(
157  goto_model.goto_functions.entry_point());
158 
160  start_entry!=goto_model.goto_functions.function_map.end() &&
161  start_entry->second.body_available(),
162  "entry point expected to have a body");
163 
164  goto_programt &start=start_entry->second.body;
165  goto_programt::targett main_call=start.instructions.begin();
166  for(goto_programt::targett end=start.instructions.end();
167  main_call!=end;
168  ++main_call)
169  {
170  if(main_call->is_function_call())
171  {
172  const exprt &func=
173  to_code_function_call(main_call->code).function();
174  if(func.id()==ID_symbol &&
175  to_symbol_expr(func).get_identifier()==main_symbol.name)
176  break;
177  }
178  }
179  POSTCONDITION(main_call!=start.instructions.end());
180 
181  start.insert_before_swap(main_call, init_instructions);
182 
183  // update counters etc.
184  remove_skip(start);
185 
186  return false;
187 }
irep_idt name
The unique identifier.
Definition: symbol.h:43
struct configt::ansi_ct ansi_c
const std::string & id2string(const irep_idt &d)
Definition: irep.h:43
bool is_not_nil() const
Definition: irep.h:103
Initialize command line arguments.
#define CPROVER_PREFIX
irep_idt mode
Language mode.
Definition: symbol.h:52
void goto_convert(const codet &code, symbol_table_baset &symbol_table, goto_programt &dest, message_handlert &message_handler, const irep_idt &mode)
const code_typet & to_code_type(const typet &type)
Cast a generic typet to a code_typet.
Definition: std_types.h:987
const irep_idt & get_identifier() const
Definition: std_expr.h:128
std::vector< parametert > parameterst
Definition: std_types.h:767
function_mapt function_map
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:29
Symbol table entry.This is a symbol in the symbol table, stored in an object of type symbol_tablet...
Definition: symbol.h:30
static mstreamt & eom(mstreamt &m)
Definition: message.h:272
configt config
Definition: config.cpp:23
#define POSTCONDITION(CONDITION)
Definition: invariant.h:237
bool model_argc_argv(goto_modelt &goto_model, unsigned max_argc, message_handlert &message_handler)
Set up argv with up to max_argc pointers into an array of 4096 bytes.
mstreamt & warning() const
Definition: message.h:307
preprocessort preprocessor
Definition: config.h:110
const irep_idt & id() const
Definition: irep.h:189
Symbol Table + CFG.
instructionst::iterator targett
Definition: goto_program.h:397
The NIL expression.
Definition: std_expr.h:4510
std::string main
Definition: config.h:163
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:403
The symbol table.
Definition: symbol_table.h:19
mstreamt & error() const
Definition: message.h:302
TO_BE_DOCUMENTED.
Definition: namespace.h:74
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast a generic exprt to a symbol_exprt.
Definition: std_expr.h:210
virtual void set_message_handler(message_handlert &_message_handler)
Definition: message.h:148
Author: Diffblue Ltd.
const symbolst & symbols
bool has_symbol(const irep_idt &name) const
Check whether a symbol exists in the symbol table.
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:70
typet type
Type of symbol.
Definition: symbol.h:34
static irep_idt entry_point()
exprt & function()
Definition: std_code.h:848
Base class for all expressions.
Definition: expr.h:42
const parameterst & parameters() const
Definition: std_types.h:905
void remove_skip(goto_programt &goto_program, goto_programt::targett begin, goto_programt::targett end)
remove unnecessary skip statements
Definition: remove_skip.cpp:86
#define UNREACHABLE
Definition: invariant.h:250
Program Transformation.
void insert(const irep_idt &identifier, const exprt &expr)
#define Forall_goto_program_instructions(it, program)
Definition: goto_program.h:740
const codet & to_code(const exprt &expr)
Definition: std_code.h:74
bool add(const symbolt &symbol)
Add a new symbol to the symbol table.
goto_programt coverage_criteriont message_handlert & message_handler
Definition: cover.cpp:66
Program Transformation.
#define DATA_INVARIANT(CONDITION, REASON)
Definition: invariant.h:257
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:32
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See namespace_baset::lookup().
Definition: namespace.cpp:130
const code_function_callt & to_code_function_call(const codet &code)
Definition: std_code.h:879