Skip to content

Commit

Permalink
Constrained input generators for fuzzers
Browse files Browse the repository at this point in the history
Summary:
DO NOT REVIEW FOR NOW.

This diff adds a prototype of the constrained input generator for fuzzers. These generators can be 
used to generate random data satisfying given constraints, needed for fuzzers for testing 
functions that have special requirement on input data, such as cdf functions, subscript functions, 
etc.

Differential Revision: D65101030
  • Loading branch information
kagamiori authored and facebook-github-bot committed Oct 28, 2024
1 parent 27d0527 commit fa787ca
Show file tree
Hide file tree
Showing 5 changed files with 684 additions and 36 deletions.
107 changes: 107 additions & 0 deletions velox/experimental/fuzzer_input_generator/ConstrainedGenerators.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "velox/experimental/fuzzer_input_generator/ConstrainedGenerators.h"

#include <boost/random/uniform_int_distribution.hpp>

namespace facebook::velox::fuzzer {

FOLLY_ALWAYS_INLINE char16_t getRandomChar(
FuzzerGenerator& rng,
const std::vector<std::pair<char16_t, char16_t>>& charSet) {
const auto& chars = charSet.size() == 1
? charSet.front()
: charSet[rand<uint32_t>(rng) % charSet.size()];
auto size = chars.second - chars.first;
auto inc = (rand<uint32_t>(rng) % size);
char16_t res = chars.first + inc;
return res;
}

/// Generates a random string (string size and encoding are passed through
/// Options).
std::string randString(
FuzzerGenerator& rng,
size_t length,
UTF8CharList encoding,
std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t>& converter) {
std::string buf;
std::u16string wbuf;
wbuf.resize(length);

for (size_t i = 0; i < length; ++i) {
wbuf[i] = getRandomChar(rng, kUTFChatSets[encoding]);
}
buf.append(converter.to_bytes(wbuf));
return buf;
}

// AbstractInputGenerator
AbstractInputGenerator::AbstractInputGenerator(
size_t seed,
const TypePtr& type,
std::unique_ptr<AbstractInputGenerator>&& next)
: type_{type}, next_{std::move(next)} {
rng_.seed(seed);
}

// NotEqualConstrainedGenerator
variant NotEqualConstrainedGenerator::generate() {
variant value;
do {
value = next_->generate();
} while (value == excludedValue_);
return value;
}

// SetConstrainedGenerator
variant SetConstrainedGenerator::generate() {
const auto index =
boost::random::uniform_int_distribution<size_t>(0, set_.size() - 1)(rng_);
return set_[index];
}

// Utility functions
template <bool, TypeKind KIND>
std::unique_ptr<AbstractInputGenerator> getRandomInputGeneratorPrimitive(
size_t seed,
const TypePtr& type) {
using T = typename TypeTraits<KIND>::NativeType;
std::unique_ptr<AbstractInputGenerator> generator =
std::make_unique<RandomInputGenerator<T>>(seed, type);
return generator;
}

std::unique_ptr<AbstractInputGenerator> getRandomInputGenerator(
size_t seed,
const TypePtr& type) {
std::unique_ptr<AbstractInputGenerator> generator;
if (type->isPrimitiveType()) {
return VELOX_DYNAMIC_SCALAR_TEMPLATE_TYPE_DISPATCH(
getRandomInputGeneratorPrimitive, false, type->kind(), seed, type);
} else if (type->isArray()) {
generator = std::make_unique<RandomInputGenerator<ArrayType>>(seed, type);
} else if (type->isMap()) {
generator = std::make_unique<RandomInputGenerator<MapType>>(seed, type);

} else if (type->isRow()) {
generator = std::make_unique<RandomInputGenerator<RowType>>(seed, type);
}
return generator;
}

} // namespace facebook::velox::fuzzer
Loading

0 comments on commit fa787ca

Please sign in to comment.