diff --git a/test/fuzzer/wasm-compile.cc b/test/fuzzer/wasm-compile.cc index 590f4834b1..310bb45204 100644 --- a/test/fuzzer/wasm-compile.cc +++ b/test/fuzzer/wasm-compile.cc @@ -61,14 +61,15 @@ class DataRange { return split; } - template + template T get() { + STATIC_ASSERT(max_bytes <= sizeof(T)); // We want to support the case where we have less than sizeof(T) bytes // remaining in the slice. For example, if we emit an i32 constant, it's // okay if we don't have a full four bytes available, we'll just use what // we have. We aren't concerned about endianness because we are generating // arbitrary expressions. - const size_t num_bytes = std::min(sizeof(T), data_.size()); + const size_t num_bytes = std::min(max_bytes, data_.size()); T result = T(); memcpy(&result, data_.start(), num_bytes); data_ += num_bytes; @@ -342,6 +343,16 @@ class WasmGenerator { local_op(data, kExprTeeLocal); } + template + void i32_const(DataRange& data) { + builder_->EmitI32Const(data.get()); + } + + template + void i64_const(DataRange& data) { + builder_->EmitI64Const(data.get()); + } + Var GetRandomGlobal(DataRange& data, bool ensure_mutable) { uint32_t index; if (ensure_mutable) { @@ -507,12 +518,17 @@ void WasmGenerator::Generate(DataRange& data) { template <> void WasmGenerator::Generate(DataRange& data) { GeneratorRecursionScope rec_scope(this); - if (recursion_limit_reached() || data.size() <= sizeof(uint32_t)) { + if (recursion_limit_reached() || data.size() <= 1) { builder_->EmitI32Const(data.get()); return; } constexpr generate_fn alternates[] = { + &WasmGenerator::i32_const<1>, + &WasmGenerator::i32_const<2>, + &WasmGenerator::i32_const<3>, + &WasmGenerator::i32_const<4>, + &WasmGenerator::sequence, &WasmGenerator::sequence, &WasmGenerator::sequence, @@ -598,12 +614,21 @@ void WasmGenerator::Generate(DataRange& data) { template <> void WasmGenerator::Generate(DataRange& data) { GeneratorRecursionScope rec_scope(this); - if (recursion_limit_reached() || data.size() <= sizeof(uint64_t)) { + if (recursion_limit_reached() || data.size() <= 1) { builder_->EmitI64Const(data.get()); return; } constexpr generate_fn alternates[] = { + &WasmGenerator::i64_const<1>, + &WasmGenerator::i64_const<2>, + &WasmGenerator::i64_const<3>, + &WasmGenerator::i64_const<4>, + &WasmGenerator::i64_const<5>, + &WasmGenerator::i64_const<6>, + &WasmGenerator::i64_const<7>, + &WasmGenerator::i64_const<8>, + &WasmGenerator::sequence, &WasmGenerator::sequence, &WasmGenerator::sequence,