diff options
author | Alon Zakai <azakai@google.com> | 2023-03-01 07:15:03 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-01 07:15:03 -0800 |
commit | 059622228659fbf59dd82363fd16323725288de1 (patch) | |
tree | d544630488c12b66a144403365655e890739e480 /src | |
parent | 53a6fcf53188823cbf60a24c980fe114f8ab7172 (diff) | |
download | binaryen-059622228659fbf59dd82363fd16323725288de1.tar.gz binaryen-059622228659fbf59dd82363fd16323725288de1.tar.bz2 binaryen-059622228659fbf59dd82363fd16323725288de1.zip |
Fuzzer: Create struct/array initial values when fields are nondefaultable (#5529)
Diffstat (limited to 'src')
-rw-r--r-- | src/tools/fuzzing/fuzzing.cpp | 22 |
1 files changed, 18 insertions, 4 deletions
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<Expression*>{}); + auto& fields = heapType.getStruct().fields; + std::vector<Expression*> 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"); } |