Commit 7b072d5b authored by Manos Koukoutos's avatar Manos Koukoutos Committed by V8 LUCI CQ

[fuzzer] Add proper support for abstract ref types

Abstract reference types in the fuzzer have only generated trivial
values. This CL adds the capability for them to generate values of their
subtypes in addition.
Drive-by: Fix emission of multiple tables in wasm-fuzzer-common.

Bug: v8:11954
Change-Id: Id434109c9ae6c1e1b799414c90f18180b8895755
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3109672
Commit-Queue: Manos Koukoutos <manoskouk@chromium.org>
Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/main@{#76430}
parent 19b523fd
......@@ -288,6 +288,10 @@ class V8_EXPORT_PRIVATE WasmModuleBuilder : public ZoneObject {
Zone* zone() { return zone_; }
bool IsSignature(uint32_t index) {
return types_[index].kind == Type::kFunctionSig;
}
FunctionSig* GetSignature(uint32_t index) {
DCHECK(types_[index].kind == Type::kFunctionSig);
return types_[index].sig;
......
......@@ -1667,6 +1667,55 @@ void WasmGenerator::Generate(ValueType type, DataRange* data) {
}
void WasmGenerator::GenerateOptRef(HeapType type, DataRange* data) {
switch (type.representation()) {
// For abstract types, generate one of their subtypes, or fall back to the
// default case.
case HeapType::kAny: {
// Weighed according to the types in the module.
// TODO(manoskouk): Generate i31ref.
uint32_t num_types = builder_->builder()->NumTypes();
uint8_t random = data->get<uint8_t>() % (num_types + 2);
if (random < num_structs_ + num_arrays_) {
GenerateOptRef(HeapType(HeapType::kData), data);
return;
} else if (random < num_types) {
GenerateOptRef(HeapType(HeapType::kFunc), data);
return;
} else if (random == num_types) {
GenerateOptRef(HeapType(HeapType::kExtern), data);
return;
}
// Else fall back to the default case outside the switch.
break;
}
case HeapType::kData:
case HeapType::kEq: {
uint8_t random = data->get<uint8_t>() % (num_structs_ + num_arrays_ + 1);
if (random > 0) {
GenerateOptRef(HeapType(random - 1), data);
return;
}
// Else fall back to the default case outside the switch.
break;
}
case HeapType::kFunc: {
uint32_t num_signatures =
builder_->builder()->NumTypes() - num_structs_ - num_arrays_;
uint32_t random = data->get<uint32_t>() % (num_signatures + 1);
if (random > 0) {
uint32_t signature_index = random + num_arrays_ + num_structs_ - 1;
DCHECK(builder_->builder()->IsSignature(signature_index));
GenerateOptRef(HeapType(signature_index), data);
return;
}
// Else fall back to the default case outside the switch.
break;
}
// TODO(manoskouk): Add i31ref case.
default:
break;
}
constexpr GenerateFnWithHeap alternatives[] = {
&WasmGenerator::ref_null, &WasmGenerator::get_local_opt_ref,
&WasmGenerator::new_object};
......
......@@ -443,14 +443,13 @@ void GenerateTestCase(Isolate* isolate, ModuleWireBytes wire_bytes,
Zone tmp_zone(isolate->allocator(), ZONE_NAME);
// TODO(9495): Add support for talbes with explicit initializers.
// TODO(9495): Add support for tables with explicit initializers.
for (const WasmTable& table : module->tables) {
os << "builder.setTableBounds(" << table.initial_size << ", ";
if (table.has_maximum_size) {
os << table.maximum_size << ");\n";
} else {
os << "undefined);\n";
}
os << "builder.addTable(" << ValueTypeToConstantName(table.type) << ", "
<< table.initial_size << ", "
<< (table.has_maximum_size ? std::to_string(table.maximum_size)
: "undefined")
<< ", undefined)\n";
}
for (const WasmElemSegment& elem_segment : module->elem_segments) {
const char* status_str =
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment