Skip to content

RFC: handling of fatal errors #1280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions src/cbmc/bv_cbmc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ Author: Daniel Kroening, [email protected]

#include <util/arith_tools.h>
#include <util/replace_expr.h>
#include <util/exception_utils.h>

bvt bv_cbmct::convert_waitfor(const exprt &expr)
{
if(expr.operands().size()!=4)
{
error().source_location=expr.find_source_location();
error() << "waitfor expected to have four operands" << eom;
throw 0;
}
throw input_src_exceptiont(
expr.find_source_location(),
"waitfor expected to have four operands");

exprt new_cycle;
const exprt &old_cycle=expr.op0();
Expand All @@ -30,11 +29,9 @@ bvt bv_cbmct::convert_waitfor(const exprt &expr)

mp_integer bound_value;
if(to_integer(bound, bound_value))
{
error().source_location=expr.find_source_location();
error() << "waitfor bound must be a constant" << eom;
throw 0;
}
throw input_src_exceptiont(
expr.find_source_location(),
"waitfor bound must be a constant");

{
// constraint: new_cycle>=old_cycle
Expand Down Expand Up @@ -135,11 +132,9 @@ bvt bv_cbmct::convert_waitfor(const exprt &expr)
bvt bv_cbmct::convert_waitfor_symbol(const exprt &expr)
{
if(expr.operands().size()!=1)
{
error().source_location=expr.find_source_location();
error() << "waitfor_symbol expected to have one operand" << eom;
throw 0;
}
throw input_src_exceptiont(
expr.find_source_location(),
"waitfor_symbol expected to have one operand");

exprt result;
const exprt &bound=expr.op0();
Expand Down
5 changes: 3 additions & 2 deletions src/cbmc/cbmc_parse_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Author: Daniel Kroening, [email protected]
#include "xml_interface.h"

cbmc_parse_optionst::cbmc_parse_optionst(int argc, const char **argv):
parse_options_baset(CBMC_OPTIONS, argc, argv),
parse_optionst(CBMC_OPTIONS, argc, argv, ui_message_handler),
xml_interfacet(cmdline),
language_uit(cmdline, ui_message_handler),
ui_message_handler(cmdline, "CBMC " CBMC_VERSION)
Expand All @@ -80,7 +80,8 @@ ::cbmc_parse_optionst::cbmc_parse_optionst(
int argc,
const char **argv,
const std::string &extra_options):
parse_options_baset(CBMC_OPTIONS+extra_options, argc, argv),
parse_optionst(
CBMC_OPTIONS+extra_options, argc, argv, ui_message_handler),
xml_interfacet(cmdline),
language_uit(cmdline, ui_message_handler),
ui_message_handler(cmdline, "CBMC " CBMC_VERSION)
Expand Down
2 changes: 1 addition & 1 deletion src/cbmc/cbmc_parse_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class optionst;
"(fixedbv)(floatbv)(all-claims)(all-properties)" // legacy, and will eventually disappear // NOLINT(whitespace/line_length)

class cbmc_parse_optionst:
public parse_options_baset,
public parse_optionst,
public xml_interfacet,
public language_uit
{
Expand Down
32 changes: 8 additions & 24 deletions src/cbmc/cbmc_solvers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Author: Daniel Kroening, [email protected]
#include <fstream>

#include <util/unicode.h>
#include <util/exception_utils.h>

#include <solvers/sat/satcheck.h>
#include <solvers/refinement/bv_refinement.h>
Expand Down Expand Up @@ -205,10 +206,7 @@ cbmc_solverst::solvert* cbmc_solverst::get_smt1(smt1_dect::solvert solver)
if(filename=="")
{
if(solver==smt1_dect::solvert::GENERIC)
{
error() << "please use --outfile" << eom;
throw 0;
}
throw ui_exceptiont("please use --outfile");

smt1_dect *smt1_dec=
new smt1_dect(
Expand Down Expand Up @@ -244,10 +242,7 @@ cbmc_solverst::solvert* cbmc_solverst::get_smt1(smt1_dect::solvert solver)
#endif

if(!out)
{
error() << "failed to open " << filename << eom;
throw 0;
}
throw system_exceptiont("failed to open "+filename);

smt1_convt *smt1_conv=
new smt1_convt(
Expand All @@ -273,10 +268,7 @@ cbmc_solverst::solvert* cbmc_solverst::get_smt2(smt2_dect::solvert solver)
if(filename=="")
{
if(solver==smt2_dect::solvert::GENERIC)
{
error() << "please use --outfile" << eom;
throw 0;
}
throw ui_exceptiont("please use --outfile");

smt2_dect *smt2_dec=
new smt2_dect(
Expand Down Expand Up @@ -318,10 +310,7 @@ cbmc_solverst::solvert* cbmc_solverst::get_smt2(smt2_dect::solvert solver)
#endif

if(!*out)
{
error() << "failed to open " << filename << eom;
throw 0;
}
throw system_exceptiont("failed to open " +filename);

smt2_convt *smt2_conv=
new smt2_convt(
Expand All @@ -344,19 +333,14 @@ cbmc_solverst::solvert* cbmc_solverst::get_smt2(smt2_dect::solvert solver)
void cbmc_solverst::no_beautification()
{
if(options.get_bool_option("beautify"))
{
error() << "sorry, this solver does not support beautification" << eom;
throw 0;
}
throw ui_exceptiont("sorry, this solver does not support beautification");
}

void cbmc_solverst::no_incremental_check()
{
if(options.get_bool_option("all-properties") ||
options.get_option("cover")!="" ||
options.get_option("incremental-check")!="")
{
error() << "sorry, this solver does not support incremental solving" << eom;
throw 0;
}
throw ui_exceptiont(
"sorry, this solver does not support incremental solving");
}
3 changes: 2 additions & 1 deletion src/cbmc/show_vcc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Author: Daniel Kroening, [email protected]

#include <util/json.h>
#include <util/json_expr.h>
#include <util/exception_utils.h>

void bmct::show_vcc_plain(std::ostream &out)
{
Expand Down Expand Up @@ -150,7 +151,7 @@ void bmct::show_vcc()
{
of.open(filename);
if(!of)
throw "failed to open file "+filename;
throw system_exceptiont("failed to open file "+filename);
}

std::ostream &out=have_file?of:std::cout;
Expand Down
51 changes: 51 additions & 0 deletions src/util/exception_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*******************************************************************\

Module: Exception helper utilities

Author: Peter Schrammel, [email protected]

\*******************************************************************/

#ifndef CPROVER_UTIL_EXCEPTION_UTILS_H
#define CPROVER_UTIL_EXCEPTION_UTILS_H

#include "invariant.h"

/// A logic error to be used for OS-related errors (I/O etc)
class system_exceptiont: public std::logic_error
{
public:
system_exceptiont(
const std::string &_reason):
std::logic_error(_reason)
{
}
};

/// A logic error to be used for user interface errors
class ui_exceptiont: public std::logic_error
{
public:
ui_exceptiont(
const std::string &_reason):
std::logic_error(_reason)
{
}
};

/// A user interface exception with source locations
class input_src_exceptiont: public ui_exceptiont
{
public:
input_src_exceptiont(
const source_locationt &_source_location,
const std::string &_reason):
ui_exceptiont(_reason),
source_location(_source_location)
{
}

source_locationt source_location;
};

#endif // CPROVER_UTIL_EXCEPTION_UTILS_H
6 changes: 3 additions & 3 deletions src/util/invariant_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Author: Chris Smowton, [email protected]

\*******************************************************************/

#ifndef CPROVER_UTIL_INVARIANT_TYPES_H
#define CPROVER_UTIL_INVARIANT_TYPES_H
#ifndef CPROVER_UTIL_INVARIANT_UTILS_H
#define CPROVER_UTIL_INVARIANT_UTILS_H

#include "invariant.h"

Expand Down Expand Up @@ -45,4 +45,4 @@ std::string pretty_print_invariant_with_irep(
#define DATA_INVARIANT_WITH_IREP(CONDITION, DESCRIPTION, IREP) \
INVARIANT_WITH_IREP((CONDITION), (DESCRIPTION), (IREP))

#endif
#endif // CPROVER_UTIL_INVARIANT_UTILS_H
29 changes: 29 additions & 0 deletions src/util/parse_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Author: Daniel Kroening, [email protected]
\*******************************************************************/

#include "parse_options.h"
#include "exception_utils.h"

#include <iostream>

Expand Down Expand Up @@ -65,3 +66,31 @@ int parse_options_baset::main()

return doit();
}

int parse_optionst::main()
{
// catch all exceptions here so that this code is not duplicated
// for each tool
try
{
return parse_options_baset::main();
}
catch(const system_exceptiont &e)
{
message.error() << e.what() << messaget::eom;
}
catch(const input_src_exceptiont &e)
{
message.error().source_location=e.source_location;
message.error() << e.what() << messaget::eom;
}
catch(const ui_exceptiont &e)
{
message.error() << e.what() << messaget::eom;
}
catch(const invariant_failedt &e)
{
message.error() << e.what() << messaget::eom;
}
return 6;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since all these exceptions are fatal and shouldn't be caught anywhere else, I wonder whether we should include the exit code in the exceptions. Then the different catch clauses would return via return e.exit_code().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that could make some sense.

}
20 changes: 20 additions & 0 deletions src/util/parse_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Author: Daniel Kroening, [email protected]
#include <string>

#include "cmdline.h"
#include "message.h"

class parse_options_baset
{
Expand All @@ -35,4 +36,23 @@ class parse_options_baset
bool parse_result;
};

class parse_optionst: public parse_options_baset
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could remove parse_optionst and include the new code directly in parse_options_baset.

{
public:
parse_optionst(
const std::string &optstring,
int argc,
const char **argv,
message_handlert &message_handler):
parse_options_baset(optstring, argc, argv),
message(message_handler)
{
}

int main() override;

protected:
messaget message;
};

#endif // CPROVER_UTIL_PARSE_OPTIONS_H