|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | +#include <array> |
| 5 | +#include <cstddef> |
| 6 | +#include <functional> |
| 7 | +#include <memory> |
| 8 | +#include <shared_mutex> |
| 9 | +#include <span> |
| 10 | +#include <unordered_map> |
| 11 | +#include <utility> |
| 12 | + |
| 13 | +#include "barretenberg/common/tuple.hpp" |
| 14 | +#include "barretenberg/vm2/common/field.hpp" |
| 15 | +#include "barretenberg/vm2/common/map.hpp" |
| 16 | +#include "barretenberg/vm2/constraining/flavor_settings.hpp" |
| 17 | +#include "barretenberg/vm2/generated/columns.hpp" |
| 18 | +#include "barretenberg/vm2/tracegen/lib/trace_conversion.hpp" |
| 19 | + |
| 20 | +/// WARNING: here is a hack. But we are in the fuzzer code, so it is okay. |
| 21 | +/// We need to undefine private to access the trace member of TraceContainer. |
| 22 | +#pragma push_macro("private") |
| 23 | +#define private public |
| 24 | +#include "barretenberg/vm2/tracegen/trace_container.hpp" |
| 25 | +#pragma pop_macro("private") |
| 26 | + |
| 27 | +namespace bb::avm2::fuzzer { |
| 28 | + |
| 29 | +// TraceContainer clone with a deep-copying copy constructor/assignment. |
| 30 | +// Uses the public TraceContainer API to stay compatible with vm2 tracegen builders. |
| 31 | +class CopyableTraceContainer : public bb::avm2::tracegen::TraceContainer { |
| 32 | + public: |
| 33 | + CopyableTraceContainer() = default; |
| 34 | + ~CopyableTraceContainer() = default; |
| 35 | + CopyableTraceContainer(const CopyableTraceContainer& other) |
| 36 | + { |
| 37 | + for (size_t col = 0; col < num_columns(); ++col) { |
| 38 | + auto& dst = (*trace)[col]; |
| 39 | + auto& src = (*other.trace)[col]; |
| 40 | + std::shared_lock src_lock(src.mutex); |
| 41 | + std::unique_lock dst_lock(dst.mutex); |
| 42 | + dst.rows = src.rows; |
| 43 | + dst.max_row_number = src.max_row_number; |
| 44 | + dst.row_number_dirty = src.row_number_dirty; |
| 45 | + } |
| 46 | + } |
| 47 | + explicit CopyableTraceContainer(const TraceContainer& other) |
| 48 | + { |
| 49 | + for (size_t col = 0; col < num_columns(); ++col) { |
| 50 | + auto& dst = (*trace)[col]; |
| 51 | + auto& src = (*other.trace)[col]; |
| 52 | + std::shared_lock src_lock(src.mutex); |
| 53 | + std::unique_lock dst_lock(dst.mutex); |
| 54 | + dst.rows = src.rows; |
| 55 | + dst.max_row_number = src.max_row_number; |
| 56 | + dst.row_number_dirty = src.row_number_dirty; |
| 57 | + } |
| 58 | + } |
| 59 | + CopyableTraceContainer(CopyableTraceContainer&&) noexcept = default; |
| 60 | + CopyableTraceContainer& operator=(CopyableTraceContainer&&) noexcept = default; |
| 61 | + |
| 62 | + CopyableTraceContainer& operator=(const CopyableTraceContainer& other) |
| 63 | + { |
| 64 | + if (this == &other) { |
| 65 | + return *this; |
| 66 | + } |
| 67 | + CopyableTraceContainer copy(other); |
| 68 | + *this = std::move(copy); |
| 69 | + return *this; |
| 70 | + } |
| 71 | +}; |
| 72 | + |
| 73 | +} // namespace bb::avm2::fuzzer |
0 commit comments