#include "src/wasm/fuzzing/random-module-generation.h"
#include <algorithm>
#include <array>
#include <optional>
#include "src/base/small-vector.h"
#include "src/base/utils/random-number-generator.h"
#include "src/wasm/function-body-decoder.h"
#include "src/wasm/wasm-module-builder.h"
#include "src/wasm/wasm-module.h"
#include "src/wasm/wasm-opcodes-inl.h"
#ifdef OFFICIAL_BUILD
#error Exclude this compilation unit in official builds.
#endif
namespace v8::internal::wasm::fuzzing {
namespace {
constexpr int kMaxArrays = …;
constexpr int kMaxStructs = …;
constexpr int kMaxStructFields = …;
constexpr int kMaxFunctions = …;
constexpr int kMaxGlobals = …;
constexpr uint32_t kMaxLocals = …;
constexpr int kMaxParameters = …;
constexpr int kMaxReturns = …;
constexpr int kMaxExceptions = …;
constexpr int kMaxTableSize = …;
constexpr int kMaxTables = …;
constexpr int kMaxMemories = …;
constexpr int kMaxMemorySize = …;
constexpr int kMaxArraySize = …;
constexpr int kMaxPassiveDataSegments = …;
constexpr uint32_t kMaxRecursionDepth = …;
constexpr int kMaxCatchCases = …;
struct StringImports { … };
template <typename... T>
constexpr auto CreateArray(T... elements) { … }
template <typename T, size_t... N>
constexpr auto ConcatArrays(std::array<T, N>... array) { … }
template <bool predicate, typename T, size_t kSize1, size_t kSize2>
constexpr auto AppendArrayIf(std::array<T, kSize1> array1,
std::array<T, kSize2> array2) { … }
class DataRange { … };
template <>
bool DataRange::get() { … }
enum IncludeNumericTypes { … };
enum IncludePackedTypes { … };
enum IncludeAllGenerics { … };
enum IncludeS128 { … };
template <WasmModuleGenerationOptions options>
ValueType GetValueTypeHelper(DataRange* data, uint32_t num_nullable_types,
uint32_t num_non_nullable_types,
IncludeNumericTypes include_numeric_types,
IncludePackedTypes include_packed_types,
IncludeAllGenerics include_all_generics,
IncludeS128 include_s128 = kIncludeS128) { … }
template <WasmModuleGenerationOptions options>
ValueType GetValueType(DataRange* data, uint32_t num_types) { … }
void GeneratePassiveDataSegment(DataRange* range, WasmModuleBuilder* builder) { … }
uint32_t GenerateRefTypeElementSegment(DataRange* range,
WasmModuleBuilder* builder,
ValueType element_type) { … }
template <WasmModuleGenerationOptions options>
std::vector<ValueType> GenerateTypes(DataRange* data, uint32_t num_ref_types) { … }
FunctionSig* CreateSignature(Zone* zone,
base::Vector<const ValueType> param_types,
base::Vector<const ValueType> return_types) { … }
template <WasmModuleGenerationOptions options>
class BodyGen { … };
WasmInitExpr GenerateInitExpr(Zone* zone, DataRange& range,
WasmModuleBuilder* builder, ValueType type,
const std::vector<uint32_t>& structs,
const std::vector<uint32_t>& arrays,
uint32_t recursion_depth);
template <WasmModuleGenerationOptions options>
class ModuleGen { … };
WasmInitExpr GenerateStructNewInitExpr(Zone* zone, DataRange& range,
WasmModuleBuilder* builder,
uint32_t index,
const std::vector<uint32_t>& structs,
const std::vector<uint32_t>& arrays,
uint32_t recursion_depth) { … }
WasmInitExpr GenerateArrayInitExpr(Zone* zone, DataRange& range,
WasmModuleBuilder* builder, uint32_t index,
const std::vector<uint32_t>& structs,
const std::vector<uint32_t>& arrays,
uint32_t recursion_depth) { … }
WasmInitExpr GenerateInitExpr(Zone* zone, DataRange& range,
WasmModuleBuilder* builder, ValueType type,
const std::vector<uint32_t>& structs,
const std::vector<uint32_t>& arrays,
uint32_t recursion_depth) { … }
}
template <WasmModuleGenerationOptions options>
base::Vector<uint8_t> GenerateRandomWasmModule(
Zone* zone, base::Vector<const uint8_t> data) { … }
base::Vector<uint8_t> GenerateWasmModuleForInitExpressions(
Zone* zone, base::Vector<const uint8_t> data, size_t* count) { … }
namespace {
bool HasSameReturns(const FunctionSig* a, const FunctionSig* b) { … }
template <WasmModuleGenerationOptions options>
void EmitDeoptAndReturnValues(BodyGen<options> gen_body, WasmFunctionBuilder* f,
const FunctionSig* target_sig,
uint32_t target_sig_index, uint32_t global_index,
uint32_t table_index, DataRange* data) { … }
template <WasmModuleGenerationOptions options>
void EmitCallAndReturnValues(BodyGen<options> gen_body, WasmFunctionBuilder* f,
WasmFunctionBuilder* callee, uint32_t table_index,
DataRange* data) { … }
}
base::Vector<uint8_t> GenerateWasmModuleForDeopt(
Zone* zone, base::Vector<const uint8_t> data,
std::vector<std::string>& callees, std::vector<std::string>& inlinees) { … }
template EXPORT_TEMPLATE_DEFINE(…)
base::Vector<uint8_t> GenerateRandomWasmModule<
WasmModuleGenerationOptions::kMVP>(Zone*,
base::Vector<const uint8_t> data);
template EXPORT_TEMPLATE_DEFINE(…)
base::Vector<uint8_t> GenerateRandomWasmModule<
WasmModuleGenerationOptions::kGenerateSIMD>(
Zone*, base::Vector<const uint8_t> data);
template EXPORT_TEMPLATE_DEFINE(…)
base::Vector<uint8_t> GenerateRandomWasmModule<
WasmModuleGenerationOptions::kGenerateWasmGC>(
Zone*, base::Vector<const uint8_t> data);
template EXPORT_TEMPLATE_DEFINE(…)
base::Vector<uint8_t> GenerateRandomWasmModule<
WasmModuleGenerationOptions::kGenerateAll>(
Zone*, base::Vector<const uint8_t> data);
}