Skip to content

Commit 0be2c86

Browse files
committed
wrap namespace prevail everywhere
Signed-off-by: Elazar Gershuni <[email protected]>
1 parent 4550c76 commit 0be2c86

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+255
-207
lines changed

src/asm_cfg.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ using std::string;
1919
using std::to_string;
2020
using std::vector;
2121

22+
namespace prevail {
2223
struct cfg_builder_t final {
2324
Program prog;
2425

@@ -46,7 +47,7 @@ struct cfg_builder_t final {
4647
if (const auto it = prog.m_cfg.neighbours.find(_label); it != prog.m_cfg.neighbours.end()) {
4748
CRAB_ERROR("Label ", to_string(_label), " already exists");
4849
}
49-
prog.m_cfg.neighbours.emplace(_label, prevail::cfg_t::adjacent_t{});
50+
prog.m_cfg.neighbours.emplace(_label, cfg_t::adjacent_t{});
5051
prog.m_instructions.emplace(_label, ins);
5152
}
5253

@@ -82,8 +83,6 @@ struct cfg_builder_t final {
8283
}
8384
};
8485

85-
using prevail::basic_block_t;
86-
8786
/// Get the inverse of a given comparison operation.
8887
static Condition::Op reverse(const Condition::Op op) {
8988
switch (op) {
@@ -300,7 +299,7 @@ Program Program::from_sequence(const InstructionSeq& inst_seq, const program_inf
300299
// hierarchical decomposition of the CFG that identifies all strongly connected components (cycles) and their entry
301300
// points. These entry points serve as natural locations for loop counters that help verify program termination.
302301
if (options.cfg_opts.check_for_termination) {
303-
const prevail::wto_t wto{builder.prog.cfg()};
302+
const wto_t wto{builder.prog.cfg()};
304303
wto.for_each_loop_head([&](const label_t& label) -> void {
305304
builder.insert_after(label, label_t::make_increment_counter(label), IncrementLoopCounter{label});
306305
});
@@ -448,7 +447,7 @@ std::map<std::string, int> collect_stats(const Program& prog) {
448447
return res;
449448
}
450449

451-
prevail::cfg_t prevail::cfg_from_adjacency_list(const std::map<label_t, std::vector<label_t>>& adj_list) {
450+
cfg_t cfg_from_adjacency_list(const std::map<label_t, std::vector<label_t>>& adj_list) {
452451
cfg_builder_t builder;
453452
for (const auto& label : std::views::keys(adj_list)) {
454453
if (label == label_t::entry || label == label_t::exit) {
@@ -463,3 +462,4 @@ prevail::cfg_t prevail::cfg_from_adjacency_list(const std::map<label_t, std::vec
463462
}
464463
return builder.prog.cfg();
465464
}
465+
} // namespace prevail

src/asm_files.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "crab_utils/num_safety.hpp"
1717
#include "platform.hpp"
1818

19+
namespace prevail {
20+
1921
template <typename T>
2022
requires std::is_trivially_copyable_v<T>
2123
static std::vector<T> vector_of(const char* data, const ELFIO::Elf_Xword size) {
@@ -646,3 +648,4 @@ std::vector<raw_program> read_elf(std::istream& input_stream, const std::string&
646648
program_reader.read_programs();
647649
return std::move(program_reader.raw_programs);
648650
}
651+
} // namespace prevail

src/asm_files.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <vector>
88

99
#include "platform.hpp"
10-
10+
namespace prevail {
1111
class UnmarshalError final : public std::runtime_error {
1212
public:
1313
explicit UnmarshalError(const std::string& what) : std::runtime_error(what) {}
@@ -23,3 +23,4 @@ std::vector<raw_program> read_elf(std::istream& input_stream, const std::string&
2323
void write_binary_file(std::string path, const char* data, size_t size);
2424

2525
std::ifstream open_asm_file(std::string path);
26+
} // namespace prevail

src/asm_marshal.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
using std::vector;
1212

13+
namespace prevail {
14+
1315
static uint8_t op(const Condition::Op op) {
1416
using Op = Condition::Op;
1517
switch (op) {
@@ -296,10 +298,10 @@ struct MarshalVisitor {
296298
};
297299

298300
vector<ebpf_inst> marshal(const Instruction& ins, const pc_t pc) {
299-
return std::visit(MarshalVisitor{prevail::label_to_offset16(pc), prevail::label_to_offset32(pc)}, ins);
301+
return std::visit(MarshalVisitor{label_to_offset16(pc), label_to_offset32(pc)}, ins);
300302
}
301303

302-
int asm_syntax::size(const Instruction& inst) {
304+
int size(const Instruction& inst) {
303305
if (const auto pins = std::get_if<Bin>(&inst)) {
304306
if (pins->lddw) {
305307
return 2;
@@ -340,3 +342,4 @@ vector<ebpf_inst> marshal(const InstructionSeq& insts) {
340342
}
341343
return res;
342344
}
345+
} // namespace prevail

src/asm_marshal.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@
77
#include "asm_syntax.hpp"
88
#include "ebpf_vm_isa.hpp"
99

10+
namespace prevail {
11+
1012
std::vector<ebpf_inst> marshal(const Instruction& ins, pc_t pc);
1113
// TODO marshal to ostream?
14+
15+
} // namespace prevail

src/asm_ostream.cpp

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "platform.hpp"
1919
#include "spec_type_descriptors.hpp"
2020

21-
using prevail::TypeGroup;
2221
using std::optional;
2322
using std::string;
2423
using std::vector;
@@ -69,8 +68,6 @@ string to_string(label_t const& label) {
6968
return str.str();
7069
}
7170

72-
} // namespace prevail
73-
7471
struct LineInfoPrinter {
7572
std::ostream& os;
7673
std::string previous_source_line;
@@ -108,7 +105,7 @@ void print_jump(std::ostream& o, const std::string& direction, const std::set<la
108105
void print_program(const Program& prog, std::ostream& os, const bool simplify, const printfunc& prefunc,
109106
const printfunc& postfunc) {
110107
LineInfoPrinter printer{os};
111-
for (const prevail::basic_block_t& bb : prevail::basic_block_t::collect_basic_blocks(prog.cfg(), simplify)) {
108+
for (const basic_block_t& bb : basic_block_t::collect_basic_blocks(prog.cfg(), simplify)) {
112109
prefunc(os, bb.first_label());
113110
print_jump(os, "from", prog.cfg().parents_of(bb.first_label()));
114111
os << bb.first_label() << ":\n";
@@ -195,7 +192,6 @@ void print_invariants(std::ostream& os, const Program& prog, const bool simplify
195192
});
196193
}
197194

198-
namespace asm_syntax {
199195
std::ostream& operator<<(std::ostream& os, const ArgSingle::Kind kind) {
200196
switch (kind) {
201197
case ArgSingle::Kind::ANYTHING: return os << "uint64_t";
@@ -314,12 +310,10 @@ struct AssertionPrinterVisitor {
314310
}
315311

316312
void operator()(const BoundedLoopCount& a) {
317-
_os << prevail::variable_t::loop_counter(to_string(a.name)) << " < " << a.limit;
313+
_os << variable_t::loop_counter(to_string(a.name)) << " < " << a.limit;
318314
}
319315

320-
static prevail::variable_t typereg(const Reg& r) {
321-
return prevail::variable_t::reg(prevail::data_kind_t::types, r.v);
322-
}
316+
static variable_t typereg(const Reg& r) { return variable_t::reg(data_kind_t::types, r.v); }
323317

324318
void operator()(ValidSize const& a) {
325319
const auto op = a.can_be_zero ? " >= " : " > ";
@@ -336,9 +330,7 @@ struct AssertionPrinterVisitor {
336330
<< "))";
337331
}
338332

339-
void operator()(ZeroCtxOffset const& a) {
340-
_os << prevail::variable_t::reg(prevail::data_kind_t::ctx_offsets, a.reg.v) << " == 0";
341-
}
333+
void operator()(ZeroCtxOffset const& a) { _os << variable_t::reg(data_kind_t::ctx_offsets, a.reg.v) << " == 0"; }
342334

343335
void operator()(Comparable const& a) {
344336
if (a.or_r2_is_number) {
@@ -530,9 +522,7 @@ struct CommandPrinterVisitor {
530522
print(b.cond);
531523
}
532524

533-
void operator()(IncrementLoopCounter const& a) {
534-
os_ << prevail::variable_t::loop_counter(to_string(a.name)) << "++";
535-
}
525+
void operator()(IncrementLoopCounter const& a) { os_ << variable_t::loop_counter(to_string(a.name)) << "++"; }
536526
};
537527
// ReSharper restore CppMemberFunctionMayBeConst
538528

@@ -596,7 +586,7 @@ void print(const InstructionSeq& insts, std::ostream& out, const std::optional<c
596586
}
597587
if (const auto jmp = std::get_if<Jmp>(&ins)) {
598588
if (!pc_of_label.contains(jmp->target)) {
599-
throw std::runtime_error(string("Cannot find label ") + prevail::to_string(jmp->target));
589+
throw std::runtime_error(string("Cannot find label ") + to_string(jmp->target));
600590
}
601591
const pc_t target_pc = pc_of_label.at(jmp->target);
602592
visitor(*jmp, target_pc - static_cast<int>(pc) - 1);
@@ -609,8 +599,6 @@ void print(const InstructionSeq& insts, std::ostream& out, const std::optional<c
609599
}
610600
}
611601

612-
} // namespace asm_syntax
613-
614602
std::ostream& operator<<(std::ostream& o, const EbpfMapDescriptor& desc) {
615603
return o << "(" << "original_fd = " << desc.original_fd << ", " << "inner_map_fd = " << desc.inner_map_fd << ", "
616604
<< "type = " << desc.type << ", " << "max_entries = " << desc.max_entries << ", "
@@ -629,4 +617,5 @@ std::ostream& operator<<(std::ostream& os, const btf_line_info_t& line_info) {
629617
os << "; " << line_info.file_name << ":" << line_info.line_number << "\n";
630618
os << "; " << line_info.source_line << "\n";
631619
return os;
632-
}
620+
}
621+
} // namespace prevail

src/asm_parse.cpp

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
using std::regex;
1919
using std::regex_match;
2020

21-
using prevail::linear_constraint_t;
22-
using prevail::linear_expression_t;
23-
using prevail::number_t;
24-
21+
namespace prevail {
2522
#define REG R"_(\s*(r\d\d?)\s*)_"
2623
#define WREG R"_(\s*([wr]\d\d?)\s*)_"
2724
#define IMM R"_(\s*\[?([-+]?(?:0x)?[0-9a-f]+)\]?\s*)_"
@@ -279,21 +276,19 @@ static InstructionSeq parse_program(std::istream& is) {
279276

280277
static uint8_t regnum(const std::string& s) { return static_cast<uint8_t>(boost::lexical_cast<uint16_t>(s.substr(1))); }
281278

282-
static prevail::variable_t special_var(const std::string& s) {
279+
static variable_t special_var(const std::string& s) {
283280
if (s == "packet_size") {
284-
return prevail::variable_t::packet_size();
281+
return variable_t::packet_size();
285282
}
286283
if (s == "meta_offset") {
287-
return prevail::variable_t::meta_offset();
284+
return variable_t::meta_offset();
288285
}
289286
throw std::runtime_error(std::string() + "Bad special variable: " + s);
290287
}
291288

292289
std::vector<linear_constraint_t> parse_linear_constraints(const std::set<std::string>& constraints,
293-
std::vector<prevail::interval_t>& numeric_ranges) {
294-
using namespace prevail::dsl_syntax;
295-
using prevail::regkind;
296-
using prevail::variable_t;
290+
std::vector<interval_t>& numeric_ranges) {
291+
using namespace dsl_syntax;
297292

298293
std::vector<linear_constraint_t> res;
299294
for (const std::string& cst_text : constraints) {
@@ -321,8 +316,8 @@ std::vector<linear_constraint_t> parse_linear_constraints(const std::set<std::st
321316
} else if (regex_match(cst_text, m,
322317
regex(REG DOT "type"
323318
"=" TYPE))) {
324-
variable_t d = variable_t::reg(prevail::data_kind_t::types, regnum(m[1]));
325-
res.push_back(d == prevail::string_to_type_encoding(m[2]));
319+
variable_t d = variable_t::reg(data_kind_t::types, regnum(m[1]));
320+
res.push_back(d == string_to_type_encoding(m[2]));
326321
} else if (regex_match(cst_text, m, regex(REG DOT KIND "=" IMM))) {
327322
variable_t d = variable_t::reg(regkind(m[2]), regnum(m[1]));
328323
number_t value;
@@ -352,28 +347,28 @@ std::vector<linear_constraint_t> parse_linear_constraints(const std::set<std::st
352347
} else if (regex_match(cst_text, m,
353348
regex("s" ARRAY_RANGE DOT "type"
354349
"=" TYPE))) {
355-
prevail::type_encoding_t type = prevail::string_to_type_encoding(m[3]);
356-
if (type == prevail::type_encoding_t::T_NUM) {
350+
type_encoding_t type = string_to_type_encoding(m[3]);
351+
if (type == T_NUM) {
357352
numeric_ranges.emplace_back(signed_number(m[1]), signed_number(m[2]));
358353
} else {
359354
number_t lb = signed_number(m[1]);
360355
number_t ub = signed_number(m[2]);
361-
variable_t d = variable_t::cell_var(prevail::data_kind_t::types, lb, ub - lb + 1);
356+
variable_t d = variable_t::cell_var(data_kind_t::types, lb, ub - lb + 1);
362357
res.push_back(d == type);
363358
}
364359
} else if (regex_match(cst_text, m,
365360
regex("s" ARRAY_RANGE DOT "svalue"
366361
"=" IMM))) {
367362
number_t lb = signed_number(m[1]);
368363
number_t ub = signed_number(m[2]);
369-
variable_t d = variable_t::cell_var(prevail::data_kind_t::svalues, lb, ub - lb + 1);
364+
variable_t d = variable_t::cell_var(data_kind_t::svalues, lb, ub - lb + 1);
370365
res.push_back(d == signed_number(m[3]));
371366
} else if (regex_match(cst_text, m,
372367
regex("s" ARRAY_RANGE DOT "uvalue"
373368
"=" IMM))) {
374369
number_t lb = signed_number(m[1]);
375370
number_t ub = signed_number(m[2]);
376-
variable_t d = variable_t::cell_var(prevail::data_kind_t::uvalues, lb, ub - lb + 1);
371+
variable_t d = variable_t::cell_var(data_kind_t::uvalues, lb, ub - lb + 1);
377372
res.push_back(d == unsigned_number(m[3]));
378373
} else {
379374
throw std::runtime_error(std::string("Unknown constraint: ") + cst_text);
@@ -385,9 +380,9 @@ std::vector<linear_constraint_t> parse_linear_constraints(const std::set<std::st
385380
// return a-b, taking account potential optional-none
386381
string_invariant string_invariant::operator-(const string_invariant& b) const {
387382
if (this->is_bottom()) {
388-
return string_invariant::bottom();
383+
return bottom();
389384
}
390-
string_invariant res = string_invariant::top();
385+
string_invariant res = top();
391386
for (const std::string& cst : this->value()) {
392387
if (b.is_bottom() || !b.contains(cst)) {
393388
res.maybe_inv->insert(cst);
@@ -436,3 +431,4 @@ std::ostream& operator<<(std::ostream& o, const string_invariant& inv) {
436431
o << "]";
437432
return o;
438433
}
434+
} // namespace prevail

src/asm_parse.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66

77
#include "asm_syntax.hpp"
88

9+
namespace prevail {
10+
911
Instruction parse_instruction(const std::string& line, const std::map<std::string, label_t>& label_name_to_label);
12+
13+
}

src/asm_syntax.hpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
#include "crab_utils/num_safety.hpp"
1616
#include "spec_type_descriptors.hpp"
1717

18-
using prevail::label_t;
19-
2018
// Assembly syntax.
21-
namespace asm_syntax {
19+
namespace prevail {
2220

2321
/// Immediate argument.
2422
struct Imm {
@@ -332,7 +330,6 @@ struct ValidStore {
332330
constexpr bool operator==(const ValidStore&) const = default;
333331
};
334332

335-
using prevail::TypeGroup;
336333
struct TypeConstraint {
337334
Reg reg;
338335
TypeGroup types;
@@ -368,7 +365,7 @@ std::string to_string(Instruction const& ins);
368365
std::ostream& operator<<(std::ostream& os, Bin::Op op);
369366
std::ostream& operator<<(std::ostream& os, Condition::Op op);
370367

371-
inline std::ostream& operator<<(std::ostream& os, const Imm imm) { return os << prevail::to_signed(imm.v); }
368+
inline std::ostream& operator<<(std::ostream& os, const Imm imm) { return os << to_signed(imm.v); }
372369
inline std::ostream& operator<<(std::ostream& os, Reg const& a) { return os << "r" << gsl::narrow<int>(a.v); }
373370
inline std::ostream& operator<<(std::ostream& os, Value const& a) {
374371
if (const auto pa = std::get_if<Imm>(&a)) {
@@ -385,12 +382,9 @@ void print(const InstructionSeq& insts, std::ostream& out, const std::optional<c
385382

386383
int size(const Instruction& inst);
387384

388-
} // namespace asm_syntax
389-
390-
using namespace asm_syntax;
391-
using prevail::pc_t;
392-
393385
template <class... Ts>
394386
struct overloaded : Ts... {
395387
using Ts::operator()...;
396388
};
389+
390+
} // namespace prevail

src/asm_unmarshal.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using std::string;
1414
using std::vector;
1515

16+
namespace prevail {
1617
int opcode_to_width(const uint8_t opcode) {
1718
switch (opcode & INST_SIZE_MASK) {
1819
case INST_SIZE_B: return 1;
@@ -234,9 +235,9 @@ struct Unmarshaller {
234235
throw InvalidInstruction(pc, "unsupported immediate");
235236
}
236237

237-
static uint64_t sign_extend(const int32_t imm) { return prevail::to_unsigned(int64_t{imm}); }
238+
static uint64_t sign_extend(const int32_t imm) { return to_unsigned(int64_t{imm}); }
238239

239-
static uint64_t zero_extend(const int32_t imm) { return uint64_t{prevail::to_unsigned(imm)}; }
240+
static uint64_t zero_extend(const int32_t imm) { return uint64_t{to_unsigned(imm)}; }
240241

241242
static auto getBinValue(const pc_t pc, const ebpf_inst inst) -> Value {
242243
if (inst.opcode & INST_SRC_REG) {
@@ -819,3 +820,4 @@ Call make_call(const int imm, const ebpf_platform_t& platform) {
819820
const program_info info{.platform = &platform};
820821
return Unmarshaller{notes, info}.makeCall(imm);
821822
}
823+
} // namespace prevail

0 commit comments

Comments
 (0)