Skip to content
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
1 change: 1 addition & 0 deletions barretenberg/cpp/pil/vm2/opcodes/send_l2_to_l1_msg.pil
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ namespace execution; // this is a virtual gadget that shares rows with the execu
public_inputs.cols[2]
};

// Increase num message if error is off. We increase even in the discard case, since discard only implies not writing to public inputs.
#[EMIT_L2_TO_L1_MSG_NUM_L2_TO_L1_MSGS_EMITTED_INCREASE]
sel_execute_send_l2_to_l1_msg * (prev_num_l2_to_l1_messages + (1 - sel_opcode_error) - num_l2_to_l1_messages) = 0;
1 change: 0 additions & 1 deletion barretenberg/cpp/pil/vm2/precomputed.pil
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ pol constant envvar_pi_row_idx;
pol constant is_address;
pol constant is_sender;
pol constant is_transactionfee;
pol constant is_feeperl2gas;
pol constant is_isstaticcall;
pol constant is_l2gasleft;
pol constant is_dagasleft;
Expand Down
1 change: 0 additions & 1 deletion barretenberg/cpp/pil/vm2/trees/nullifier_check.pil
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ namespace nullifier_check;
// Inputs to the gadget
pol commit write;
write * (1 - write) = 0;
pol READ = 1 - write;
// If writing, sel must be on
write * (1 - sel) = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ namespace retrieved_bytecodes_tree_check;
// Inputs to the gadget
pol commit write;
write * (1 - write) = 0;
pol READ = 1 - write;

pol commit class_id;
pol commit root;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ namespace written_public_data_slots_tree_check;
// Inputs to the gadget
pol commit write;
write * (1 - write) = 0;
pol READ = 1 - write;

pol commit slot;
pol commit root;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include "barretenberg/avm_fuzzer/common/interfaces/dbs.hpp"
#include "barretenberg/avm_fuzzer/fuzz_lib/constants.hpp"
#include "barretenberg/avm_fuzzer/fuzz_lib/contract_db_proxy.hpp"
#include "barretenberg/avm_fuzzer/fuzz_lib/control_flow.hpp"
#include "barretenberg/avm_fuzzer/fuzz_lib/fuzz.hpp"
#include "barretenberg/avm_fuzzer/fuzz_lib/fuzzer_context.hpp"
#include "barretenberg/avm_fuzzer/fuzz_lib/fuzzer_data.hpp"
#include "barretenberg/avm_fuzzer/fuzz_lib/simulator.hpp"
#include "barretenberg/avm_fuzzer/mutations/fuzzer_data.hpp"
Expand All @@ -17,6 +17,22 @@
using FuzzInstruction = ::FuzzInstruction;
using namespace bb::avm2::fuzzer;

namespace {

FuzzerContext create_context_with_predefined_functions()
{
FuzzerContext context;

// Register predefined functions
for (const auto& function : PREDEFINED_FUNCTIONS) {
context.register_contract_from_bytecode(function);
}

return context;
}

} // namespace

/// Initializes the typescript simulator process and the world state manager
/// See yarn-project/simulator/scripts/fuzzing/
extern "C" int LLVMFuzzerInitialize(int*, char***)
Expand All @@ -42,7 +58,8 @@ SimulatorResult fuzz(const uint8_t* buffer, size_t size)

FuzzerWorldStateManager* ws_mgr = FuzzerWorldStateManager::getInstance();
ws_mgr->fork();
auto res = fuzz_against_ts_simulator(deserialized_data);
auto context = create_context_with_predefined_functions();
auto res = fuzz_against_ts_simulator(deserialized_data, context);
ws_mgr->reset_world_state();

return res;
Expand All @@ -53,6 +70,7 @@ extern "C" size_t LLVMFuzzerCustomMutator(uint8_t* serialized_fuzzer_data,
size_t max_size,
unsigned int seed)
{
auto context = create_context_with_predefined_functions();
auto rng = std::mt19937_64(seed);
FuzzerData deserialized_data;
try {
Expand All @@ -62,7 +80,7 @@ extern "C" size_t LLVMFuzzerCustomMutator(uint8_t* serialized_fuzzer_data,
} catch (const std::exception& e) {
deserialized_data = FuzzerData();
}
mutate_fuzzer_data(deserialized_data, rng);
mutate_fuzzer_data(deserialized_data, rng, context);
auto [mutated_serialized_fuzzer_data, mutated_serialized_fuzzer_data_size] =
msgpack_encode_buffer(deserialized_data);
if (mutated_serialized_fuzzer_data_size > max_size) {
Expand Down
27 changes: 22 additions & 5 deletions barretenberg/cpp/src/barretenberg/avm_fuzzer/common/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,38 @@ Process::~Process()
void Process::write_line(const std::string& line) const
{
std::string command = line + "\n";
write(stdin_fd, command.c_str(), command.size());
fsync(stdin_fd);
const char* data = command.c_str();
size_t remaining = command.size();

// We use a loop to ensure all data is written but throw if we encounter an error.
// This enables partial writes to be handled correctly.
while (remaining > 0) {
ssize_t written = write(stdin_fd, data, remaining);
if (written < 0) {
if (errno == EINTR) {
continue;
}
throw std::runtime_error("write() error: " + std::string(std::strerror(errno)));
}
data += written;
remaining -= static_cast<size_t>(written);
}
}

std::string Process::read_line() const
{
char buffer[4096]; // NOLINT
std::string response;
ssize_t bytes_read = 0;
fsync(stdout_fd);
while ((bytes_read = read(stdout_fd, buffer, sizeof(buffer))) > 0) {
response.append(buffer, static_cast<size_t>(bytes_read));
if (response.find('\n') != std::string::npos) {
// Check for newline in just the newly read data instead of going back through the entire response
const char* newline_pos = static_cast<const char*>(memchr(buffer, '\n', static_cast<size_t>(bytes_read)));
if (newline_pos != nullptr) {
// Found newline - append only up to and including the newline
response.append(buffer, static_cast<size_t>(newline_pos - buffer + 1));
break;
}
response.append(buffer, static_cast<size_t>(bytes_read));
}
if (bytes_read < 0 && errno != EINTR) {
throw std::runtime_error("read() error: " + std::string(std::strerror(errno)));
Expand Down

This file was deleted.

This file was deleted.

15 changes: 6 additions & 9 deletions barretenberg/cpp/src/barretenberg/avm_fuzzer/fuzz_lib/fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

#include "barretenberg/avm_fuzzer/common/interfaces/dbs.hpp"
#include "barretenberg/avm_fuzzer/fuzz_lib/constants.hpp"
#include "barretenberg/avm_fuzzer/fuzz_lib/contract_db_proxy.hpp"
#include "barretenberg/avm_fuzzer/fuzz_lib/control_flow.hpp"
#include "barretenberg/avm_fuzzer/fuzz_lib/fuzzer_context.hpp"
#include "barretenberg/avm_fuzzer/fuzz_lib/fuzzer_data.hpp"
#include "barretenberg/avm_fuzzer/fuzz_lib/simulator.hpp"
#include "barretenberg/common/log.hpp"
#include "barretenberg/vm2/simulation/lib/contract_crypto.hpp"

using namespace bb::avm2::fuzzer;

SimulatorResult fuzz_against_ts_simulator(FuzzerData& fuzzer_data)
SimulatorResult fuzz_against_ts_simulator(FuzzerData& fuzzer_data, FuzzerContext& context)
{
auto control_flow = ControlFlow(fuzzer_data.instruction_blocks);
for (const auto& cfg_instruction : fuzzer_data.cfg_instructions) {
Expand All @@ -28,12 +28,9 @@ SimulatorResult fuzz_against_ts_simulator(FuzzerData& fuzzer_data)
SimulatorResult cpp_result;

FuzzerWorldStateManager* ws_mgr = FuzzerWorldStateManager::getInstance();
ContractDBProxy* contract_db_proxy = ContractDBProxy::get_instance();
for (const auto& function : PREDEFINED_FUNCTIONS) {
ContractDBProxy::register_contract_from_bytecode(function);
}
auto contract_address = ContractDBProxy::register_contract_from_bytecode(bytecode);
FuzzerContractDB contract_db = *contract_db_proxy->get_contract_db();

auto contract_address = context.register_contract_from_bytecode(bytecode);
FuzzerContractDB contract_db = context.get_contract_db();

// Create the transaction
auto tx = create_default_tx(
Expand All @@ -54,7 +51,7 @@ SimulatorResult fuzz_against_ts_simulator(FuzzerData& fuzzer_data)
ws_mgr->checkpoint();
auto js_result = js_simulator->simulate(*ws_mgr, contract_db, tx);

ContractDBProxy::reset_instance();
context.reset();

// If the results does not match
if (!compare_simulator_results(cpp_result, js_result)) {
Expand Down
Loading
Loading