Skip to content

Commit 1afc566

Browse files
authored
fix: msgpack stack blowups on schema gen (#2259)
1 parent 9fc3f3d commit 1afc566

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

circuits/cpp/barretenberg/cpp/src/barretenberg/serialize/msgpack_impl/check_memory_span.hpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,19 @@ template <typename T, typename... Args> std::string check_memory_span(T* obj, Ar
5959
return {};
6060
}
6161

62-
template <msgpack_concepts::HasMsgPack T> std::string check_msgpack_method(T& object)
62+
template <msgpack_concepts::HasMsgPack T> std::string check_msgpack_method(const T& object)
6363
{
6464
std::string result;
6565
auto checker = [&](auto&... values) { result = check_memory_span(&object, &values...); };
66-
object.msgpack([&](auto&... keys_and_values) { std::apply(checker, drop_keys(std::tie(keys_and_values...))); });
66+
const_cast<T&>(object).msgpack( // NOLINT
67+
[&](auto&... keys_and_values) { std::apply(checker, drop_keys(std::tie(keys_and_values...))); });
6768
return result;
6869
}
69-
void check_msgpack_usage(auto object)
70+
void check_msgpack_usage(const auto& object)
7071
{
7172
std::string result = check_msgpack_method(object);
7273
if (!result.empty()) {
7374
throw_or_abort(result);
7475
}
7576
}
76-
} // namespace msgpack
77+
} // namespace msgpack

circuits/cpp/barretenberg/cpp/src/barretenberg/serialize/msgpack_impl/schema_impl.hpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ struct MsgpackSchemaPacker : msgpack::packer<msgpack::sbuffer> {
6666

6767
// Note: if this fails to compile, check first in list of template Arg's
6868
// it may need a msgpack_schema_pack specialization (particularly if it doesn't define MSGPACK_FIELDS).
69-
(_msgpack_schema_pack(*this, Args{}), ...); /* pack schemas of all template Args */
69+
(_msgpack_schema_pack(*this, *std::make_unique<Args>()), ...); /* pack schemas of all template Args */
7070
}
7171
/**
7272
* @brief Encode a type that defines msgpack based on its key value pairs.
@@ -108,7 +108,10 @@ concept SchemaPackable = requires(T value, MsgpackSchemaPacker packer) { msgpack
108108

109109
// Helper for packing (key, value, key, value, ...) arguments
110110
template <typename Value, typename... Rest>
111-
inline void _schema_pack_map_content(MsgpackSchemaPacker& packer, std::string key, Value value, Rest... rest)
111+
inline void _schema_pack_map_content(MsgpackSchemaPacker& packer,
112+
std::string key,
113+
const Value& value,
114+
const Rest&... rest)
112115
{
113116
static_assert(
114117
msgpack_concepts::SchemaPackable<Value>,
@@ -200,7 +203,9 @@ inline void msgpack_schema_pack(MsgpackSchemaPacker& packer, std::array<T, N> co
200203
packer.pack("array");
201204
// That has a size 2 tuple as its 2nd arg
202205
packer.pack_array(2); /* param list format for consistency*/
203-
_msgpack_schema_pack(packer, T{});
206+
// To avoid WASM problems with large stack objects, we use a heap allocation.
207+
// Small note: This works because make_unique goes of scope only when the whole line is done.
208+
_msgpack_schema_pack(packer, *std::make_unique<T>());
204209
packer.pack(N);
205210
}
206211

0 commit comments

Comments
 (0)