Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
fuzz.cpp
Go to the documentation of this file.
2
7
8using namespace bb::avm2::fuzzer;
9
10void log_result(const SimulatorResult& result)
11{
12 info("Reverted: ", result.reverted);
13 info("Output: ", result.output);
14 info("Reason: ", result.revert_reason);
15}
16
18{
19 bool logging_enabled = std::getenv("AVM_FUZZER_LOGGING") != nullptr;
21 for (const auto& cfg_instruction : fuzzer_data.cfg_instructions) {
22 control_flow.process_cfg_instruction(cfg_instruction);
23 }
24 if (logging_enabled) {
25 info("Fuzzer data: ", fuzzer_data);
26 }
27 auto bytecode = control_flow.build_bytecode(fuzzer_data.return_options);
28 if (logging_enabled) {
29 info("Bytecode: ", bytecode);
30 }
31
32 auto cpp_simulator = CppSimulator();
33 JsSimulator* js_simulator = JsSimulator::getInstance();
34 SimulatorResult cpp_result;
35
38
39 try {
40 ws_mgr->checkpoint();
41 cpp_result = cpp_simulator.simulate(*ws_mgr, bytecode, fuzzer_data.calldata);
42 ws_mgr->revert();
43 } catch (const std::exception& e) {
44 info("CppSimulator failed with error: ", e.what());
45 throw std::runtime_error("Error simulating with CppSimulator");
46 }
47
48 ws_mgr->checkpoint();
49 auto js_result = js_simulator->simulate(*ws_mgr, bytecode, fuzzer_data.calldata);
50
51 // If the results does not match
52 if (!compare_simulator_results(cpp_result, js_result)) {
53 info("CppSimulator result: ");
54 log_result(cpp_result);
55 info("JsSimulator result: ");
56 log_result(js_result);
57 throw std::runtime_error("Simulator results are different");
58 }
59 if (logging_enabled) {
60 info("Simulator results match successfully");
61 log_result(cpp_result);
62 }
63 return cpp_result;
64}
const FF CONTRACT_ADDRESS
Definition constants.hpp:33
std::shared_ptr< Napi::ThreadSafeFunction > bytecode
uses barretenberg/vm2 to simulate the bytecode
Definition simulator.hpp:35
uses the yarn-project/simulator to simulate the bytecode Singleton, because initializing the simulato...
Definition simulator.hpp:44
static JsSimulator * getInstance()
SimulatorResult simulate(fuzzer::FuzzerWorldStateManager &ws_mgr, const std::vector< uint8_t > &bytecode, const std::vector< FF > &calldata) override
static FuzzerWorldStateManager * getInstance()
Definition dbs.hpp:108
void register_contract_address(const AztecAddress &contract_address)
Definition dbs.cpp:286
void info(Args... args)
Definition log.hpp:75
void log_result(const SimulatorResult &result)
Definition fuzz.cpp:10
SimulatorResult fuzz(FuzzerData &fuzzer_data)
fuzz CPP vs JS simulator with the given fuzzer data
Definition fuzz.cpp:17
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
bool compare_simulator_results(const SimulatorResult &result1, const SimulatorResult &result2)
describes the data which will be used for fuzzing Should contain instructions, calldata,...
ReturnOptions return_options
std::vector< bb::avm2::FF > calldata
std::vector< CFGInstruction > cfg_instructions
std::vector< std::vector< FuzzInstruction > > instruction_blocks
std::string revert_reason
Definition simulator.hpp:18
std::vector< FF > output
Definition simulator.hpp:16