Skip to content

SMT2 type checking for define-fun #3421

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

Merged
merged 2 commits into from
Nov 18, 2018
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CORE
function-application1.smt2

^EXIT=0$
^SIGNAL=0$
^unsat$
--
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(set-logic QF_BV)

(define-fun min ((a (_ BitVec 8)) (b (_ BitVec 8))) (_ BitVec 8)
(ite (bvule a b) a b))

(define-fun p1 () Bool (= (min #x01 #x02) #x01))
(define-fun p2 () Bool (= (min #xff #xfe) #xfe))

; all must be true

(assert (not (and p1 p2)))

(check-sat)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CORE
function-application2.smt2

^EXIT=134$
^SIGNAL=0$
^line 5: type mismatch in function definition: expected `\(_ BitVec 16\)' but got `\(_ BitVec 8\)'$
Copy link
Collaborator

Choose a reason for hiding this comment

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

So the exit code is useless?

Copy link
Member Author

Choose a reason for hiding this comment

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

Most solvers seem to do the "continued-execution" error handling from the SMT-LIB standard; yes, the exit code is then used for SAT/UNSAT only.

--
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(set-logic QF_BV)

; the range type doesn't match!
(define-fun min ((a (_ BitVec 8)) (b (_ BitVec 8))) (_ BitVec 16)
(ite (bvule a b) a b))

(define-fun p1 () Bool (= (min #x01 #x02) #x01))
(define-fun p2 () Bool (= (min #xff #xfe) #xfe))

; all must be true

(assert (not (and p1 p2)))

(check-sat)
2 changes: 2 additions & 0 deletions scripts/delete_failing_smt2_solver_tests
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ rm Float8/test.desc
rm Free2/test.desc
rm Function1/test.desc
rm Function_Pointer3/test.desc
rm Initialization6/test.desc
rm Linking4/test.desc
rm Linking7/test.desc
rm Malloc17/test.desc
rm Malloc18/test.desc
rm Malloc19/test.desc
rm Malloc20/test.desc
rm Malloc21/test.desc
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am surprised that this is now causing more failures?

Copy link
Member Author

Choose a reason for hiding this comment

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

To be expected: the solver is now less permissive than it used to be. These two tests indicate cases in which the back-end generates invalid SMT-LIB files.

rm Malloc23/test.desc
rm Malloc24/test.desc
rm Memmove1/test.desc
Expand Down
44 changes: 40 additions & 4 deletions src/solvers/smt2/smt2_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ Author: Daniel Kroening, [email protected]

#include <util/arith_tools.h>

std::ostream &operator<<(std::ostream &out, const smt2_parsert::smt2_format &f)
{
if(f.type.id() == ID_unsignedbv)
out << "(_ BitVec " << to_unsignedbv_type(f.type).get_width() << ')';
else if(f.type.id() == ID_bool)
out << "Bool";
else if(f.type.id() == ID_integer)
out << "Int";
else if(f.type.id() == ID_real)
out << "Real";
else
out << "? " << f.type.id();

return out;
}

void smt2_parsert::command_sequence()
{
exit=false;
Expand Down Expand Up @@ -360,8 +376,8 @@ exprt smt2_parsert::multi_ary(irep_idt id, const exprt::operandst &op)
{
error() << "expression must have operands with matching types,"
" but got `"
<< op[0].type().pretty()
<< "' and `" << op[i].type().pretty() << '\'' << eom;
<< smt2_format(op[0].type()) << "' and `"
<< smt2_format(op[i].type()) << '\'' << eom;
return nil_exprt();
}
}
Expand All @@ -385,8 +401,8 @@ exprt smt2_parsert::binary_predicate(irep_idt id, const exprt::operandst &op)
{
error() << "expression must have operands with matching types,"
" but got `"
<< op[0].type().pretty()
<< "' and `" << op[1].type().pretty() << '\'' << eom;
<< smt2_format(op[0].type()) << "' and `"
<< smt2_format(op[1].type()) << '\'' << eom;
return nil_exprt();
}

Expand Down Expand Up @@ -1187,6 +1203,26 @@ void smt2_parsert::command(const std::string &c)
auto signature=function_signature_definition();
exprt body=expression();

// check type of body
if(signature.id() == ID_mathematical_function)
{
const auto &f_signature = to_mathematical_function_type(signature);
if(body.type() != f_signature.codomain())
{
error() << "type mismatch in function definition: expected `"
<< smt2_format(f_signature.codomain()) << "' but got `"
<< smt2_format(body.type()) << '\'' << eom;
return;
}
}
else if(body.type() != signature)
{
error() << "type mismatch in function definition: expected `"
<< smt2_format(signature) << "' but got `"
<< smt2_format(body.type()) << '\'' << eom;
return;
}

// set up the entry
auto &entry=id_map[id];
entry.type=signature;
Expand Down
11 changes: 11 additions & 0 deletions src/solvers/smt2/smt2_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ class smt2_parsert:public smt2_tokenizert
using id_mapt=std::map<irep_idt, idt>;
id_mapt id_map;

struct smt2_format
{
explicit smt2_format(const typet &_type) : type(_type)
{
}

const typet type;
};

protected:
bool exit;
void command_sequence();
Expand Down Expand Up @@ -82,4 +91,6 @@ class smt2_parsert:public smt2_tokenizert
exprt cast_bv_to_unsigned(const exprt &);
};

std::ostream &operator<<(std::ostream &, const smt2_parsert::smt2_format &);

#endif // CPROVER_SOLVERS_SMT2_SMT2_PARSER_H