Skip to content

Commit 1cf5dad

Browse files
author
Alan Jowett
committed
PR feedback
Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>
1 parent 79b4c76 commit 1cf5dad

File tree

5 files changed

+59
-38
lines changed

5 files changed

+59
-38
lines changed

src/asm_parse.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Instruction parse_instruction(const std::string& line, const std::map<std::strin
1818
* @param[out] numeric_ranges A vector of numeric ranges that are extracted from the constraints.
1919
*
2020
* @return A vector of crab::linear_constraint_t
21+
* Example of string constraints include:
22+
* r0.type=number
23+
* r0.uvalue=5
24+
* r0.svalue=5
2125
*/
2226
std::vector<crab::linear_constraint_t> parse_linear_constraints(const std::set<std::string>& constraints,
2327
std::vector<crab::interval_t>& numeric_ranges);

src/asm_syntax.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ struct label_t {
2323
explicit label_t(const int index, const int to = -1, std::string stack_frame_prefix = {}) noexcept
2424
: from(index), to(to), stack_frame_prefix(std::move(stack_frame_prefix)) {}
2525

26+
/**
27+
* @brief Construct a new label t object from a string.
28+
*
29+
* @param[in] string_label The string representation of the label.
30+
*
31+
* @throw std::invalid_argument The label format is invalid.
32+
* @throw std::out_of_range The label value causes numeric overflow.
33+
*
34+
* Example labels:
35+
* "2:-1" - a label which falls through to the next instruction.
36+
* "2:5" - a label which jumps to instruction 5.
37+
* "2:-1/5:-1" - a label which falls through to the next instruction, with a stack frame prefix denoting where the label was called.
38+
*/
2639
explicit label_t(std::string_view string_label) {
2740
auto pos = string_label.find(STACK_FRAME_DELIMITER);
2841
if (pos != std::string_view::npos) {

src/config.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ const ebpf_verifier_options_t ebpf_verifier_default_options = {
1414
.allow_division_by_zero = true,
1515
.setup_constraints = true,
1616
.big_endian = false,
17-
.store_pre_invariants = false, // Enable this to permit usage of the ebpf_check_constraints_at_label and ebpf_get_invariants_at_label functions.
17+
.store_pre_invariants = false, // When enabled, stores pre-invariants to support ebpf_check_constraints_at_label and
18+
// ebpf_get_invariants_at_label. Disabled by default to avoid memory overhead when
19+
// these functions are not used.
1820
};

src/crab_verifier.cpp

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -282,49 +282,51 @@ void ebpf_verifier_clear_thread_local_state() {
282282
}
283283

284284
bool ebpf_check_constraints_at_label(std::ostream& os, const std::string& label_string,
285-
const std::set<std::string>& constraints) try {
286-
label_t label = label_t(label_string);
287-
if (!saved_pre_invariants.has_value()) {
288-
os << "No pre-invariants available\n";
289-
return false;
290-
}
291-
if (saved_pre_invariants.value().find(label) == saved_pre_invariants.value().end()) {
292-
os << "No pre-invariants available for label " << label << "\n";
293-
return false;
294-
}
295-
ebpf_domain_t from_inv(saved_pre_invariants.value().at(label));
296-
auto concrete_domain = ebpf_domain_t::from_constraints(constraints, false);
285+
const std::set<std::string>& constraints) {
286+
try {
287+
label_t label = label_t(label_string);
288+
if (!saved_pre_invariants.has_value()) {
289+
os << "No pre-invariants available\n";
290+
return false;
291+
}
292+
if (saved_pre_invariants.value().find(label) == saved_pre_invariants.value().end()) {
293+
os << "No pre-invariants available for label " << label << "\n";
294+
return false;
295+
}
296+
ebpf_domain_t from_inv(saved_pre_invariants.value().at(label));
297+
auto concrete_domain = ebpf_domain_t::from_constraints(constraints, false);
298+
299+
if (concrete_domain.is_bottom()) {
300+
os << "The provided constraints are unsatisfiable (concrete domain is bottom)\n";
301+
os << "Concrete constraints are self-contradictory\n";
302+
os << concrete_domain << "\n";
303+
return false;
304+
}
297305

298-
if (concrete_domain.is_bottom()) {
299-
os << "The provided constraints are unsatisfiable (concrete domain is bottom)\n";
300-
os << "Concrete constraints are self-contradictory\n";
301-
os << concrete_domain << "\n";
302-
return false;
303-
}
306+
if (from_inv.is_bottom()) {
307+
os << "The abstract state is unreachable\n";
308+
os << from_inv << "\n";
309+
return false;
310+
}
304311

305-
if (from_inv.is_bottom()) {
306-
os << "The abstract state is unreachable\n";
307-
os << from_inv << "\n";
308-
return false;
309-
}
312+
if ((from_inv & concrete_domain).is_bottom()) {
313+
os << "Concrete state does not match invariant\n";
310314

311-
if ((from_inv & concrete_domain).is_bottom()) {
312-
os << "Concrete state does not match invariant\n";
315+
// Print the concrete state
316+
os << "--- Concrete state ---\n";
317+
os << concrete_domain << "\n";
313318

314-
// Print the concrete state
315-
os << "--- Concrete state ---\n";
316-
os << concrete_domain << "\n";
319+
os << "--- Abstract state ---\n";
320+
os << from_inv << "\n";
317321

318-
os << "--- Abstract state ---\n";
319-
os << from_inv << "\n";
322+
return false;
323+
}
320324

325+
return true;
326+
} catch (std::exception& e) {
327+
os << "Error occurred while checking constraints: " << e.what() << "\n";
321328
return false;
322329
}
323-
324-
return true;
325-
} catch (std::exception& e) {
326-
os << "Error occurred while checking constraints: " << e.what() << "\n";
327-
return false;
328330
}
329331

330332
std::set<std::string> ebpf_get_invariants_at_label(const std::string& label)

src/crab_verifier.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ void ebpf_verifier_clear_thread_local_state();
4343
* @param[in,out] os Print output to this stream.
4444
* @param[in] label The location in the CFG to check against.
4545
* @param[in] constraints The concrete state to check.
46-
* @return true The state is valid.
47-
* @return false The state is invalid.
46+
* @retval true The state is valid.
47+
* @retval false The state is invalid.
4848
* @throw std::invalid_argument The label format is invalid
4949
* @throw std::out_of_range The label value causes numeric overflow
5050
*/

0 commit comments

Comments
 (0)