Skip to content
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

Uniform printing of simplified vs non-simplified #805

Merged
merged 2 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
51 changes: 51 additions & 0 deletions src/asm_cfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,57 @@ cfg_t prepare_cfg(const InstructionSeq& prog, const program_info& info, const pr
return cfg;
}

std::set<basic_block_t> basic_block_t::collect_basic_blocks(const cfg_t& cfg, const bool simplify) {
elazarg marked this conversation as resolved.
Show resolved Hide resolved
if (!simplify) {
std::set<basic_block_t> res;
for (const label_t& label : cfg.labels()) {
if (label != cfg.entry_label() && label != cfg.exit_label()) {
res.insert(basic_block_t{label});
}
}
return res;
}

std::set<basic_block_t> res;
std::set worklist(cfg.label_begin(), cfg.label_end());
std::set<label_t> seen;
while (!worklist.empty()) {
elazarg marked this conversation as resolved.
Show resolved Hide resolved
const label_t label = *worklist.begin();
worklist.erase(label);
if (seen.contains(label)) {
continue;
}
seen.insert(label);

const value_t* current_value = &cfg.get_node(label);
if (current_value->in_degree() == 1 && cfg.get_parent(label).out_degree() == 1) {
continue;
}
elazarg marked this conversation as resolved.
Show resolved Hide resolved
basic_block_t bb{label};
while (current_value->out_degree() == 1) {
const value_t& next_value = cfg.get_child(bb.last_label());
const label_t& next_label = next_value.label();

if (next_label == bb.first_label() || next_label == cfg.exit_label() || next_value.in_degree() != 1) {
break;
}

if (bb.first_label() == cfg.entry_label()) {
// Entry instruction is Undefined. We want to start with 0
bb.m_ts.clear();
}
bb.m_ts.push_back(next_label);

worklist.erase(next_label);
seen.insert(next_label);

current_value = &next_value;
}
res.emplace(std::move(bb));
}
return res;
}
elazarg marked this conversation as resolved.
Show resolved Hide resolved

template <typename T>
static vector<label_t> unique(const std::pair<T, T>& be) {
vector<label_t> res;
Expand Down
38 changes: 1 addition & 37 deletions src/crab/cfg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,43 +385,7 @@ class basic_block_t final {
public:
std::strong_ordering operator<=>(const basic_block_t& other) const { return first_label() <=> other.first_label(); }

static std::set<basic_block_t> collect_basic_blocks(const cfg_t& cfg) {
std::set<basic_block_t> res;

std::set worklist(cfg.label_begin(), cfg.label_end());
std::set<label_t> seen;
while (!worklist.empty()) {
const label_t label = *worklist.begin();
worklist.erase(label);
if (seen.contains(label)) {
continue;
}
seen.insert(label);

const value_t* current_value = &cfg.get_node(label);
if (current_value->in_degree() == 1 && cfg.get_parent(label).out_degree() == 1) {
continue;
}
basic_block_t bb{label};
while (current_value->out_degree() == 1) {
const value_t& next_value = cfg.get_child(bb.last_label());
const label_t& next_label = next_value.label();

if (next_label == bb.first_label() || next_label == cfg.exit_label() || next_value.in_degree() != 1) {
break;
}

bb.m_ts.push_back(next_label);

worklist.erase(next_label);
seen.insert(next_label);

current_value = &next_value;
}
res.emplace(std::move(bb));
}
return res;
}
static std::set<basic_block_t> collect_basic_blocks(const cfg_t& cfg, bool simplify);

explicit basic_block_t(const label_t& first_label) : m_ts{first_label} {}
basic_block_t(basic_block_t&&) noexcept = default;
Expand Down
39 changes: 13 additions & 26 deletions src/crab_verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,21 @@ bool Invariants::is_valid_after(const label_t& label, const string_invariant& st
return abstract_state <= invariants.at(label).post;
}

void Invariants::print_invariants(std::ostream& os, const cfg_t& cfg) const {
if (thread_local_options.verbosity_opts.simplify) {
for (const auto& bb : basic_block_t::collect_basic_blocks(cfg)) {
os << "\nPre-invariant : " << invariants.at(bb.first_label()).pre << "\n";
print_from(os, cfg.get_node(bb.first_label()));
print_label(os, cfg.get_node(bb.first_label()));
for (const auto& label : bb) {
const auto& value = cfg.get_node(label);
print_assertions(os, value);
print_instruction(os, value);
}
print_goto(os, cfg.get_node(bb.last_label()));
os << "\nPost-invariant: " << invariants.at(bb.last_label()).post << "\n";
}
} else {
LineInfoPrinter printer{os};
for (const label_t& label : cfg.sorted_labels()) {
void Invariants::print_invariants(std::ostream& os, const cfg_t& cfg, const bool simplify) const {
LineInfoPrinter printer{os};
for (const auto& bb : basic_block_t::collect_basic_blocks(cfg, simplify)) {
os << "\nPre-invariant : " << invariants.at(bb.first_label()).pre << "\n";
const crab::value_t& first_node = cfg.get_node(bb.first_label());
print_from(os, first_node);
print_label(os, first_node);
for (const label_t& label : bb) {
printer.print_line_info(label);
const auto& inv_pair = invariants.at(label);
os << "\nPre-invariant : " << inv_pair.pre << "\n";
const auto& value = cfg.get_node(label);
print_from(os, value);
print_label(os, value);
print_assertions(os, value);
print_instruction(os, value);
print_goto(os, value);
os << "\nPost-invariant: " << inv_pair.post << "\n";
const crab::value_t& node = cfg.get_node(label);
print_assertions(os, node);
print_instruction(os, node);
}
print_goto(os, cfg.get_node(bb.last_label()));
os << "\nPost-invariant: " << invariants.at(bb.last_label()).post << "\n";
}
os << "\n";
}
Expand Down
2 changes: 1 addition & 1 deletion src/crab_verifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Invariants final {
Invariants(const Invariants& invariants) = default;

bool is_valid_after(const label_t& label, const string_invariant& state) const;
void print_invariants(std::ostream& os, const cfg_t& cfg) const;
void print_invariants(std::ostream& os, const cfg_t& cfg, bool simplify) const;
elazarg marked this conversation as resolved.
Show resolved Hide resolved

string_invariant invariant_at(const label_t& label) const;

Expand Down
11 changes: 5 additions & 6 deletions src/main/check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,22 +248,21 @@ int main(int argc, char** argv) {
auto invariants = analyze(cfg);
const auto end = std::chrono::steady_clock::now();
const auto seconds = std::chrono::duration<double>(end - begin).count();

if (ebpf_verifier_options.verbosity_opts.print_invariants) {
invariants.print_invariants(std::cout, cfg);
const auto verbosity = ebpf_verifier_options.verbosity_opts;
if (verbosity.print_invariants) {
invariants.print_invariants(std::cout, cfg, verbosity.simplify);
}

bool pass;
if (ebpf_verifier_options.verbosity_opts.print_failures) {
if (verbosity.print_failures) {
auto report = invariants.check_assertions(cfg);
report.print_warnings(std::cout);
pass = report.verified();
} else {
pass = invariants.verified(cfg);
}
if (pass && ebpf_verifier_options.cfg_opts.check_for_termination &&
(ebpf_verifier_options.verbosity_opts.print_failures ||
ebpf_verifier_options.verbosity_opts.print_invariants)) {
(verbosity.print_failures || verbosity.print_invariants)) {
std::cout << "Program terminates within " << invariants.max_loop_count() << " loop iterations\n";
}
std::cout << pass << "," << seconds << "," << resident_set_size_kb() << "\n";
Expand Down
Loading