Skip to content

Commit 1441111

Browse files
committed
Implement handling of unexpected responses from SMT solver
1 parent 040d6d1 commit 1441111

File tree

2 files changed

+102
-4
lines changed

2 files changed

+102
-4
lines changed

src/solvers/smt2_incremental/smt2_incremental_decision_procedure.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ static smt_responset get_response_to_command(
3232
return response;
3333
}
3434

35+
static optionalt<std::string>
36+
get_problem_messages(const smt_responset &response)
37+
{
38+
if(const auto error = response.cast<smt_error_responset>())
39+
{
40+
return "SMT solver returned an error message - " +
41+
id2string(error->message());
42+
}
43+
if(response.cast<smt_unsupported_responset>())
44+
{
45+
return {"SMT solver does not support given command."};
46+
}
47+
return {};
48+
}
49+
3550
/// \brief Find all sub expressions of the given \p expr which need to be
3651
/// expressed as separate smt commands.
3752
/// \return A collection of sub expressions, which need to be expressed as
@@ -236,5 +251,7 @@ decision_proceduret::resultt smt2_incremental_decision_proceduret::dec_solve()
236251
log.error() << "SMT2 solver returned \"unknown\"" << messaget::eom;
237252
return lookup_decision_procedure_result(check_sat_response->kind());
238253
}
239-
UNIMPLEMENTED_FEATURE("handling solver response.");
254+
if(const auto problem = get_problem_messages(result))
255+
throw analysis_exceptiont{*problem};
256+
throw analysis_exceptiont{"Unexpected kind of response from SMT solver."};
240257
}

unit/solvers/smt2_incremental/smt2_incremental_decision_procedure.cpp

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <solvers/smt2_incremental/smt_terms.h>
1212
#include <util/arith_tools.h>
1313
#include <util/bitvector_types.h>
14+
#include <util/exception_utils.h>
1415
#include <util/make_unique.h>
1516
#include <util/namespace.h>
1617
#include <util/symbol_table.h>
@@ -23,6 +24,36 @@
2324

2425
#include <deque>
2526

27+
class analysis_execption_with_messaget
28+
: public Catch::MatcherBase<analysis_exceptiont>
29+
{
30+
public:
31+
explicit analysis_execption_with_messaget(std::string message);
32+
bool match(const analysis_exceptiont &exception) const override;
33+
std::string describe() const override;
34+
35+
private:
36+
std::string expected;
37+
};
38+
39+
analysis_execption_with_messaget::analysis_execption_with_messaget(
40+
std::string message)
41+
: expected{std::move(message)}
42+
{
43+
}
44+
45+
bool analysis_execption_with_messaget::match(
46+
const analysis_exceptiont &exception) const
47+
{
48+
return expected == exception.what();
49+
}
50+
51+
std::string analysis_execption_with_messaget::describe() const
52+
{
53+
return std::string{"analysis_exceptiont with `.what' containing - \""} +
54+
expected + "\"";
55+
}
56+
2657
class smt_mock_solver_processt : public smt_base_solver_processt
2758
{
2859
std::function<void(const smt_commandt &)> _send;
@@ -293,7 +324,57 @@ TEST_CASE(
293324
"[core][smt2_incremental]")
294325
{
295326
decision_procedure_test_environmentt test{};
296-
test.mock_responses = {smt_success_responset{},
297-
smt_check_sat_responset{smt_sat_responset{}}};
298-
REQUIRE_NOTHROW(test.procedure());
327+
SECTION("Expected success response.")
328+
{
329+
test.mock_responses = {smt_success_responset{},
330+
smt_check_sat_responset{smt_sat_responset{}}};
331+
REQUIRE_NOTHROW(test.procedure());
332+
}
333+
SECTION("Duplicated success messages.")
334+
{
335+
test.mock_responses = {smt_success_responset{},
336+
smt_success_responset{},
337+
smt_check_sat_responset{smt_sat_responset{}}};
338+
REQUIRE_THROWS_MATCHES(
339+
test.procedure(),
340+
analysis_exceptiont,
341+
analysis_execption_with_messaget{
342+
"Unexpected kind of response from SMT solver."});
343+
}
344+
}
345+
346+
TEST_CASE(
347+
"smt2_incremental_decision_proceduret receives unexpected responses to "
348+
"check-sat.",
349+
"[core][smt2_incremental]")
350+
{
351+
decision_procedure_test_environmentt test{};
352+
SECTION("get-value response")
353+
{
354+
test.mock_responses = {
355+
smt_get_value_responset{{{"x", smt_bool_literal_termt{false}}}}};
356+
REQUIRE_THROWS_MATCHES(
357+
test.procedure(),
358+
analysis_exceptiont,
359+
analysis_execption_with_messaget{
360+
"Unexpected kind of response from SMT solver."});
361+
}
362+
SECTION("error message response")
363+
{
364+
test.mock_responses = {smt_error_responset{"foobar"}};
365+
REQUIRE_THROWS_MATCHES(
366+
test.procedure(),
367+
analysis_exceptiont,
368+
analysis_execption_with_messaget{
369+
"SMT solver returned an error message - foobar"});
370+
}
371+
SECTION("unsupported response")
372+
{
373+
test.mock_responses = {smt_unsupported_responset{}};
374+
REQUIRE_THROWS_MATCHES(
375+
test.procedure(),
376+
analysis_exceptiont,
377+
analysis_execption_with_messaget{
378+
"SMT solver does not support given command."});
379+
}
299380
}

0 commit comments

Comments
 (0)