Skip to content

Fix bugs stemming from duplicate output streams in smt2_convt::write_footer() #6207

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
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
2 changes: 1 addition & 1 deletion regression/cbmc/Malloc6/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CORE broken-smt-backend
CORE
main.c
--pointer-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc/Pointer1/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CORE broken-smt-backend
CORE
main.c
--pointer-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc/Pointer28/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CORE broken-smt-backend
CORE
main.c
--pointer-check --little-endian
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc/Pointer4/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CORE broken-smt-backend
CORE
main.c
--pointer-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc/String8/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CORE broken-smt-backend
CORE
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc/String_Abstraction7/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CORE broken-smt-backend
CORE
main.c
--string-abstraction --pointer-check --bounds-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc/address_space_size_limit3/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CORE
CORE broken-smt-backend
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why has this now broken?

main.i
--32 --little-endian --object-bits 25 --pointer-check
^EXIT=10$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc/null7/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CORE broken-smt-backend
CORE
main.c
--pointer-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc/struct7/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CORE broken-smt-backend
CORE
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc/void_pointer2/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CORE broken-smt-backend
CORE
main.c
--pointer-check --no-simplify --unwind 3
^EXIT=0$
Expand Down
34 changes: 17 additions & 17 deletions src/solvers/smt2/smt2_conv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,55 +176,55 @@ void smt2_convt::write_header()
out << "(set-logic " << logic << ")" << "\n";
}

void smt2_convt::write_footer(std::ostream &os)
void smt2_convt::write_footer()
{
os << "\n";
out << "\n";

// fix up the object sizes
for(const auto &object : object_sizes)
define_object_size(object.second, object.first);

if(use_check_sat_assuming && !assumptions.empty())
{
os << "(check-sat-assuming (";
out << "(check-sat-assuming (";
for(const auto &assumption : assumptions)
convert_literal(to_literal_expr(assumption).get_literal());
os << "))\n";
out << "))\n";
}
else
{
// add the assumptions, if any
if(!assumptions.empty())
{
os << "; assumptions\n";
out << "; assumptions\n";

for(const auto &assumption : assumptions)
{
os << "(assert ";
out << "(assert ";
convert_literal(to_literal_expr(assumption).get_literal());
os << ")"
<< "\n";
out << ")"
<< "\n";
}
}

os << "(check-sat)\n";
out << "(check-sat)\n";
}

os << "\n";
out << "\n";

if(solver!=solvert::BOOLECTOR)
{
for(const auto &id : smt2_identifiers)
os << "(get-value (|" << id << "|))"
<< "\n";
out << "(get-value (|" << id << "|))"
<< "\n";
}

os << "\n";
out << "\n";

os << "(exit)\n";
out << "(exit)\n";

os << "; end of SMT2 file"
<< "\n";
out << "; end of SMT2 file"
<< "\n";
}

void smt2_convt::define_object_size(
Expand Down Expand Up @@ -267,7 +267,7 @@ void smt2_convt::define_object_size(

decision_proceduret::resultt smt2_convt::dec_solve()
{
write_footer(out);
write_footer();
out.flush();
return decision_proceduret::resultt::D_ERROR;
}
Expand Down
11 changes: 10 additions & 1 deletion src/solvers/smt2/smt2_conv.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,16 @@ class smt2_convt : public stack_decision_proceduret
resultt dec_solve() override;

void write_header();
void write_footer(std::ostream &);
/// Writes the end of the SMT file to the `smt_convt::out` stream. These parts
/// of the output may be changed when using multiple rounds of solving. They
/// include the following parts of the output file -
/// * The object size definitions.
/// * The assertions based on the `assumptions` member variable.
/// * The `(check-sat)` or `check-sat-assuming` command.
/// * A `(get-value |identifier|)` command for each of the identifiers in
/// `smt2_convt::smt2_identifiers`.
/// * An `(exit)` command.
void write_footer();

// tweaks for arrays
bool use_array_theory(const exprt &);
Expand Down
16 changes: 9 additions & 7 deletions src/solvers/smt2/smt2_dec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ decision_proceduret::resultt smt2_dect::dec_solve()
temp_file_stdout("smt2_dec_stdout_", ""),
temp_file_stderr("smt2_dec_stderr_", "");

{
// we write the problem into a file
std::ofstream problem_out(
temp_file_problem(), std::ios_base::out | std::ios_base::trunc);
problem_out << stringstream.str();
write_footer(problem_out);
}
const auto write_problem_to_file = [&](std::ofstream problem_out) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems longer, more complex and harder to debug than the code it replaces. What am I missing?

Copy link
Contributor

Choose a reason for hiding this comment

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

The old code would do the following:

  1. Write the internal stored string stream to file
  2. Call the footer to explicitly write to the file

The problem was that the write_footer call would call another function (define_object_size) that would write to the internal string stream.

The desirable behaviour is to do the following:

  1. Have stored string of the SMT that can be used for multiple calls to the solver
  2. Write this stored string to the file
  3. Add a footer to the file with the specific details of the current call/check (e.g. assumptions and check-sat-assuming).

The modified code stores the common SMT to the cached_output and then writes this at the beginning of each call. The write_footer function (and called functions such as define_object_size) now write to the same internal string stream as all the other SMT conversion code (reducing future misunderstandings like this fixes). The internal string stream is cleared before writing the footer and after, to ensure no accidental duplication of SMT instructions/data.

I hope this has helped clarify.

cached_output << stringstream.str();
stringstream.str(std::string{});
write_footer();
problem_out << cached_output.str() << stringstream.str();
stringstream.str(std::string{});
};
write_problem_to_file(std::ofstream(
temp_file_problem(), std::ios_base::out | std::ios_base::trunc));

std::vector<std::string> argv;
std::string stdin_filename;
Expand Down
4 changes: 4 additions & 0 deletions src/solvers/smt2/smt2_dec.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class smt2_dect : protected smt2_stringstreamt, public smt2_convt
protected:
message_handlert &message_handler;

/// Everything except the footer is cached, so that output files can be
/// rewritten with varying footers.
std::stringstream cached_output;

resultt read_result(std::istream &in);
};

Expand Down