chromium/v8/src/wasm/fuzzing/random-module-generation.h

// Copyright 2024 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#if !V8_ENABLE_WEBASSEMBLY
#error This header should only be included if WebAssembly is enabled.
#endif  // !V8_ENABLE_WEBASSEMBLY

#ifndef V8_WASM_FUZZING_RANDOM_MODULE_GENERATION_H_
#define V8_WASM_FUZZING_RANDOM_MODULE_GENERATION_H_

#include "src/base/export-template.h"
#include "src/base/logging.h"
#include "src/base/vector.h"

namespace v8::internal {
class Zone;
}

namespace v8::internal::wasm::fuzzing {

// Defines what expressions should be generated by the fuzzer besides the MVP
// ones.
enum WasmModuleGenerationOptions : uint32_t {};

constexpr bool ShouldGenerateSIMD(WasmModuleGenerationOptions options) {}

constexpr bool ShouldGenerateWasmGC(WasmModuleGenerationOptions options) {}

#if !OFFICIAL_BUILD
// Generate a valid Wasm module based on the given input bytes.
// Returns an empty buffer on failure, valid module wire bytes otherwise.
// The bytes will be allocated in the zone.
// Defined in random-module-generation.cc.
template <WasmModuleGenerationOptions options>
V8_EXPORT_PRIVATE base::Vector<uint8_t> GenerateRandomWasmModule(
    Zone*, base::Vector<const uint8_t> data);

// Explicit template instantiation for kMVP.
extern template EXPORT_TEMPLATE_DECLARE()
    base::Vector<uint8_t> GenerateRandomWasmModule<
        WasmModuleGenerationOptions::kMVP>(Zone*,
                                           base::Vector<const uint8_t> data);

// Explicit template instantiation for kGenerateSIMD.
extern template EXPORT_TEMPLATE_DECLARE()
    base::Vector<uint8_t> GenerateRandomWasmModule<
        WasmModuleGenerationOptions::kGenerateSIMD>(
        Zone*, base::Vector<const uint8_t> data);

// Explicit template instantiation for kGenerateWasmGC.
extern template EXPORT_TEMPLATE_DECLARE()
    base::Vector<uint8_t> GenerateRandomWasmModule<
        WasmModuleGenerationOptions::kGenerateWasmGC>(
        Zone*, base::Vector<const uint8_t> data);

// Explicit template instantiation for kGenerateAll.
extern template EXPORT_TEMPLATE_DECLARE()
    base::Vector<uint8_t> GenerateRandomWasmModule<
        WasmModuleGenerationOptions::kGenerateAll>(
        Zone*, base::Vector<const uint8_t> data);

V8_EXPORT_PRIVATE base::Vector<uint8_t> GenerateWasmModuleForInitExpressions(
    Zone*, base::Vector<const uint8_t> data, size_t* count);

V8_EXPORT_PRIVATE base::Vector<uint8_t> GenerateWasmModuleForDeopt(
    Zone*, base::Vector<const uint8_t> data, std::vector<std::string>& callees,
    std::vector<std::string>& inlinees);
#endif

}  // namespace v8::internal::wasm::fuzzing

#endif  // V8_WASM_FUZZING_RANDOM_MODULE_GENERATION_H_