blob: b8bc47c908e333f9f0c633eee9d1991bdae44098 [file]
// Copyright 2026 The Dawn & Tint Authors
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef SRC_TINT_CMD_FUZZ_COMMON_FUZZTEST_INIT_H_
#define SRC_TINT_CMD_FUZZ_COMMON_FUZZTEST_INIT_H_
#include <cstddef>
#include <functional>
#include <string>
#include <string_view>
#include <vector>
#include "src/tint/cmd/fuzz/common/init.h"
#include "src/tint/cmd/fuzz/common/options.h"
namespace tint::fuzz::common {
/// Maximum size in bytes for raw WGSL strings generated or seeded by FuzzTest, matching
/// `max_len=10000` configured for `tint_wgsl_fuzzer`.
inline constexpr size_t kMaxWgslFuzzSize = 10000;
/// Default upper bound on raw seed file size read into memory (16 MiB).
inline constexpr size_t kMaxRawSeedReadBytes = size_t{16} * 1024 * 1024;
/// Execution mode for FuzzTest prototype binaries.
enum class FuzzerMode {
/// Standard Tint fuzzer mode.
kDefault,
#if TINT_BUILD_FUZZER_VULKAN_SUPPORT
/// Mesa Lavapipe SPIR-V writer mode (filters to `tint::spirv::writer::IRFuzzer` and exits
/// cleanly when the Vulkan ICD JSON is unavailable).
kMesa,
#endif
};
/// Parses CLI options, populates `options` in-place, initializes the active fuzzing/test
/// framework, and runs all selected fuzz tests.
/// @param type the input format family of the fuzzer
/// @param argc argument count from `main`
/// @param argv argument vector from `main` (may be compacted in-place)
/// @param default_fuzz_test full `Suite.Test` name selected when no explicit test mode is given
/// @param options Tint fuzzer options struct populated in-place
/// @param mode execution mode (default or Mesa Lavapipe)
/// @returns process exit code from option parsing or test execution
int RunFuzzTestMain(FuzzerType type,
int argc,
char** argv,
std::string_view default_fuzz_test,
Options* options,
FuzzerMode mode = FuzzerMode::kDefault);
/// Visits every regular non-empty raw seed file of at most `max_bytes` bytes discovered
/// recursively under seed/corpus directories passed on the command line, skipping serialized
/// FuzzTest corpus entries.
/// @param visitor callback receiving each seed file's moved binary contents
/// @param max_bytes maximum seed file size in bytes to read
void ForEachRawSeedFile(const std::function<void(std::string&&)>& visitor,
size_t max_bytes = kMaxRawSeedReadBytes);
/// Loads and parses the WGSL fuzzer dictionary when `-dict=`/`--dict=` is supplied, keeping
/// non-empty 7-bit ASCII entries within `kMaxWgslFuzzSize`.
/// @returns parsed dictionary tokens, or an empty vector if no dictionary was provided or readable
std::vector<std::string> LoadWgslDictionary();
/// Loads raw WGSL seed files from command-line seed/corpus directories, keeping non-empty 7-bit
/// ASCII inputs of at most `kMaxWgslFuzzSize` bytes.
/// @returns valid raw WGSL seed strings
std::vector<std::string> LoadRawWgslSeeds();
} // namespace tint::fuzz::common
#endif // SRC_TINT_CMD_FUZZ_COMMON_FUZZTEST_INIT_H_