From 059622228659fbf59dd82363fd16323725288de1 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 1 Mar 2023 07:15:03 -0800 Subject: Fuzzer: Create struct/array initial values when fields are nondefaultable (#5529) --- src/tools/fuzzing/fuzzing.cpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'src/tools/fuzzing/fuzzing.cpp') diff --git a/src/tools/fuzzing/fuzzing.cpp b/src/tools/fuzzing/fuzzing.cpp index c4c47ee23..03ee7d376 100644 --- a/src/tools/fuzzing/fuzzing.cpp +++ b/src/tools/fuzzing/fuzzing.cpp @@ -2146,12 +2146,26 @@ Expression* TranslateToFuzzReader::makeConstCompoundRef(Type type) { // to make a constant for the initializer of a global, for example. We've // already handled simple cases of this above, for basic heap types, so what // we have left here are user-defined heap types like structs. - // TODO: support non-defaultable fields. for now, just use default values. if (type.isStruct()) { - return builder.makeStructNew(type.getHeapType(), - std::vector{}); + auto& fields = heapType.getStruct().fields; + std::vector values; + // TODO: use non-default values randomly even when not necessary, sometimes + if (std::any_of(fields.begin(), fields.end(), [&](const Field& field) { + return !field.type.isDefaultable(); + })) { + // There is a nondefaultable field, which we must create. + for (auto& field : fields) { + values.push_back(makeTrivial(field.type)); + } + } + return builder.makeStructNew(heapType, values); } else if (type.isArray()) { - return builder.makeArrayNew(type.getHeapType(), makeConst(Type::i32)); + auto element = heapType.getArray().element; + Expression* init = nullptr; + if (!element.type.isDefaultable()) { + init = makeTrivial(element.type); + } + return builder.makeArrayNew(type.getHeapType(), makeConst(Type::i32), init); } else { WASM_UNREACHABLE("bad user-defined ref type"); } -- cgit v1.2.3