Skip to content

Commit 4e6aec2

Browse files
author
Remi Delmas
committed
CONTRACTS: only throw proper exception objects
1 parent 266f8aa commit 4e6aec2

File tree

5 files changed

+49
-52
lines changed

5 files changed

+49
-52
lines changed

src/goto-instrument/contracts/dynamic-frames/dfcc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ void dfcct::check_transform_goto_model_preconditions(
148148
// "goto model, expected '"
149149
// << harness_id << "', aborting contracts transformations."
150150
// << messaget::eom;
151-
// throw 0;
151+
// throw invalid_input_exceptiont(msg);
152152
// }
153153

154154
// if(config.main.value() != harness_id)
@@ -157,7 +157,7 @@ void dfcct::check_transform_goto_model_preconditions(
157157
// << config.main.value()
158158
// << "' differs from given harness function '" << harness_id
159159
// << "', aborting contracts transformations." << messaget::eom;
160-
// throw 0;
160+
// throw invalid_input_exceptiont(msg);
161161
// }
162162

163163
// check there is at most one function to check

src/goto-instrument/contracts/dynamic-frames/dfcc_dsl_contract_handler.cpp

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,12 @@ const symbolt *dfcc_dsl_contract_handlert::get_pure_contract_symbol_ptr(
139139
new_symbol.module = contract_symbol.module;
140140
new_symbol.location = contract_symbol.location;
141141
auto entry = goto_model.symbol_table.insert(std::move(new_symbol));
142-
if(!entry.second)
143-
{
144-
log.error().source_location = contract_symbol.location;
145-
log.error() << "contract '" << contract_symbol.display_name()
146-
<< "' already set at " << entry.first.location.as_string()
147-
<< messaget::eom;
148-
throw 0;
149-
}
150-
// this lookup must work, and no need to check for signature compatibility
142+
INVARIANT(
143+
entry.second,
144+
"contract '" + id2string(contract_symbol.display_name()) +
145+
"' already set at " + id2string(entry.first.location.as_string()));
146+
// this lookup will work and set the pointer
147+
// no need to check for signature compatibility
151148
ns.lookup(pure_contract_id, pure_contract_symbol);
152149
}
153150
return pure_contract_symbol;
@@ -157,12 +154,11 @@ const symbolt &dfcc_dsl_contract_handlert::get_pure_contract_symbol(
157154
const irep_idt &contract_id)
158155
{
159156
auto result = get_pure_contract_symbol_ptr(contract_id);
160-
if(result != nullptr)
161-
return *result;
162-
163-
log.error() << "dfcc_dsl_contract_handlert: pure contract symbol for "
164-
<< contract_id << " was not found, aborting" << messaget::eom;
165-
throw 0;
157+
INVARIANT(
158+
result != nullptr,
159+
"pure contract symbol for " + id2string(contract_id) +
160+
" could not be found");
161+
return *result;
166162
}
167163

168164
const bool dfcc_dsl_contract_handlert::pure_contract_symbol_exists(
@@ -187,30 +183,29 @@ void dfcc_dsl_contract_handlert::check_signature_compat(
187183
std::ostringstream err_msg;
188184
err_msg << "dfcc_dsl_contract_handlert: function '" << contract_id
189185
<< "' and the corresponding pure contract symbol '"
190-
<< pure_contract_id
191-
<< "' have incompatible type signatures:" << std::endl;
186+
<< pure_contract_id << "' have incompatible type signatures:\n";
192187

193188
err_msg << "- contract return type "
194-
<< from_type(ns, contract_id, contract_type.return_type())
195-
<< std::endl;
189+
<< from_type(ns, contract_id, contract_type.return_type()) << "\n";
196190

197191
for(const auto &param : contract_type.parameters())
198192
{
199193
err_msg << "- contract param type "
200-
<< from_type(ns, contract_id, param.type()) << std::endl;
194+
<< from_type(ns, contract_id, param.type()) << "\n";
201195
}
202196

203197
err_msg << "- pure contract return type "
204198
<< from_type(ns, pure_contract_id, pure_contract_type.return_type())
205-
<< std::endl;
199+
<< "\n";
206200

207201
for(const auto &param : pure_contract_type.parameters())
208202
{
209203
err_msg << "- pure contract param type "
210-
<< from_type(ns, pure_contract_id, param.type()) << std::endl;
204+
<< from_type(ns, pure_contract_id, param.type()) << "\n";
211205
}
212206

213-
err_msg << "aborting." << std::endl;
207+
err_msg << "aborting."
208+
<< "\n";
214209
throw invalid_source_file_exceptiont(
215210
err_msg.str(), contract_type.source_location());
216211
}

src/goto-instrument/contracts/dynamic-frames/dfcc_library.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,13 @@ void dfcc_libraryt::load(std::set<irep_idt> &to_instrument)
312312
{
313313
const auto &found =
314314
goto_model.goto_functions.function_map.find(pair.second);
315-
if(!(found != goto_model.goto_functions.function_map.end() &&
316-
found->second.body_available()))
317-
{
318-
log.error() << "dfcc_libraryt: GOTO body of dfcc function " << pair.second
319-
<< " not found" << messaget::eom;
320-
throw 0;
321-
}
315+
316+
INVARIANT(
317+
found != goto_model.goto_functions.function_map.end() &&
318+
found->second.body_available(),
319+
"body of DFCC library function " + id2string(pair.second) +
320+
" could not be found");
321+
322322
dfcc_fun_symbol[pair.first] = ns.lookup(pair.second);
323323
}
324324

src/goto-instrument/contracts/dynamic-frames/dfcc_spec_functions.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,11 @@ void dfcc_spec_functionst::generate_havoc_function(
184184
{
185185
if(ins_it->call_function().id() != ID_symbol)
186186
{
187-
log.error().source_location = ins_it->source_location();
188-
log.error() << "dfcc_spec_functionst::generate_havoc_function: "
189-
"function pointer calls not supported"
190-
<< messaget::eom;
191-
throw 0;
187+
throw invalid_source_file_exceptiont(
188+
"Function pointer call '" +
189+
from_expr(ns, function_id, ins_it->call_function()) +
190+
"' in function '" + id2string(function_id) + "' is not supported",
191+
ins_it->source_location());
192192
}
193193

194194
const irep_idt &callee_id =
@@ -328,11 +328,11 @@ void dfcc_spec_functionst::to_spec_assigns_function(
328328
{
329329
if(ins_it->call_function().id() != ID_symbol)
330330
{
331-
log.error().source_location = ins_it->source_location();
332-
log.error()
333-
<< "dfcc_spec_functionst::function pointer calls not supported"
334-
<< messaget::eom;
335-
throw 0;
331+
throw invalid_source_file_exceptiont(
332+
"Function pointer call '" +
333+
from_expr(ns, function_id, ins_it->call_function()) +
334+
"' in function '" + id2string(function_id) + "' is not supported",
335+
ins_it->source_location());
336336
}
337337

338338
const irep_idt &callee_id =
@@ -410,9 +410,11 @@ void dfcc_spec_functionst::to_spec_frees_function(
410410
{
411411
if(ins_it->call_function().id() != ID_symbol)
412412
{
413-
log.error().source_location = ins_it->source_location();
414-
log.error() << "function pointer calls not supported" << messaget::eom;
415-
throw 0;
413+
throw invalid_source_file_exceptiont(
414+
"Function pointer call '" +
415+
from_expr(ns, function_id, ins_it->call_function()) +
416+
"' in function '" + id2string(function_id) + "' is not supported",
417+
ins_it->source_location());
416418
}
417419

418420
const irep_idt &callee_id =

src/goto-instrument/contracts/dynamic-frames/dfcc_swap_and_wrap.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,17 @@ void dfcc_swap_and_wrapt::swap_and_wrap(
7979
return;
8080

8181
// already swapped with something else, abort
82-
log.error() << " multiple attempts to swap and wrap function '"
83-
<< function_id << "':" << messaget::eom;
8482
auto mode1 =
8583
(pair.second == dfcc_contract_modet::REPLACE) ? "REPLACE" : "CHECK";
86-
log.error() << "with '" << pair.first << "' in mode " << mode1
87-
<< messaget::eom;
8884
auto mode2 =
8985
(contract_mode == dfcc_contract_modet::REPLACE) ? "REPLACE" : "CHECK)";
90-
log.error() << "with '" << contract_id << "' in mode " << mode2
91-
<< messaget::eom;
92-
throw 0;
86+
87+
std::ostringstream err_msg;
88+
err_msg << "DFCC: multiple attempts to swap and wrap function '"
89+
<< function_id << "':\n";
90+
err_msg << "- with '" << pair.first << "' in mode " << mode1 << "\n";
91+
err_msg << "- with '" << contract_id << "' in mode " << mode2 << "\n";
92+
throw invalid_input_exceptiont(err_msg.str());
9393
}
9494

9595
cache.insert({function_id, {contract_id, contract_mode}});

0 commit comments

Comments
 (0)