Skip to content

Commit

Permalink
Constrained input generators for fuzzers (facebookincubator#11368)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebookincubator#11368

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 Nov 12, 2024
1 parent 34d7c61 commit adc5c9b
Show file tree
Hide file tree
Showing 12 changed files with 1,265 additions and 184 deletions.
1 change: 1 addition & 0 deletions velox/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ add_subdirectory(flag_definitions)
add_subdirectory(external/date)
add_subdirectory(external/md5)
add_subdirectory(external/hdfs)
add_subdirectory(experimental/fuzzer_input_generator)
#

# examples depend on expression
Expand Down
30 changes: 30 additions & 0 deletions velox/experimental/fuzzer_input_generator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# 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.
velox_add_library(velox_fuzzer_constrained_input_generators
ConstrainedGenerators.cpp ConstrainedVectorGenerator.cpp)

velox_link_libraries(
velox_fuzzer_constrained_input_generators
Folly::folly
velox_expression
velox_type
velox_vector_fuzzer_util)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
target_compile_options(velox_fuzzer_constrained_input_generators
PRIVATE -Wno-deprecated-declarations)
endif()

if(${VELOX_BUILD_TESTING})
add_subdirectory(tests)
endif()
191 changes: 191 additions & 0 deletions velox/experimental/fuzzer_input_generator/ConstrainedGenerators.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/*
* 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>

#include "velox/vector/fuzzer/Utils.h"

namespace facebook::velox::fuzzer {

// 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];
}

// JsonInputGenerator
folly::json::serialization_opts JsonInputGenerator::getSerializationOptions() {
folly::json::serialization_opts opts;
opts.allow_non_string_keys = true;
opts.allow_nan_inf = true;
if (makeRandomVariation_) {
opts.convert_int_keys = rand<bool>(rng_);
opts.pretty_formatting = rand<bool>(rng_);
opts.pretty_formatting_indent_width = rand<uint32_t>(rng_, 0, 4);
opts.encode_non_ascii = rand<bool>(rng_);
opts.allow_trailing_comma = rand<bool>(rng_);
opts.sort_keys = rand<bool>(rng_);
opts.skip_invalid_utf8 = rand<bool>(rng_);
opts.parse_numbers_as_strings = rand<bool>(rng_);
}
return opts;
}

variant JsonInputGenerator::generate() {
const auto object = objectGenerator_->generate();
const folly::dynamic jsonObject = convertVariantToDynamic(object);
const auto jsonString = folly::json::serialize(jsonObject, opts_);
if (makeRandomVariation_ && coinToss(rng_, 0.5)) {
makeRandomVariation(jsonString);
}
return variant(jsonString);
}

folly::dynamic JsonInputGenerator::convertVariantToDynamic(
const variant& object) {
if (object.isNull()) {
return folly::dynamic();
}

switch (object.kind()) {
case TypeKind::BOOLEAN:
return convertVariantToDynamicPrimitive<TypeKind::BOOLEAN>(object);
case TypeKind::TINYINT:
return convertVariantToDynamicPrimitive<TypeKind::TINYINT>(object);
case TypeKind::SMALLINT:
return convertVariantToDynamicPrimitive<TypeKind::SMALLINT>(object);
case TypeKind::INTEGER:
return convertVariantToDynamicPrimitive<TypeKind::INTEGER>(object);
case TypeKind::BIGINT:
return convertVariantToDynamicPrimitive<TypeKind::BIGINT>(object);
case TypeKind::REAL:
return convertVariantToDynamicPrimitive<TypeKind::REAL>(object);
case TypeKind::DOUBLE:
return convertVariantToDynamicPrimitive<TypeKind::DOUBLE>(object);
case TypeKind::VARCHAR:
return convertVariantToDynamicPrimitive<TypeKind::VARCHAR>(object);
case TypeKind::VARBINARY:
return convertVariantToDynamicPrimitive<TypeKind::VARBINARY>(object);
case TypeKind::TIMESTAMP:
return convertVariantToDynamicPrimitive<TypeKind::TIMESTAMP>(object);
case TypeKind::HUGEINT:
return convertVariantToDynamicPrimitive<TypeKind::HUGEINT>(object);
case TypeKind::ARRAY: {
folly::dynamic array = folly::dynamic::array;
for (const auto& element : object.value<TypeKind::ARRAY>()) {
array.push_back(convertVariantToDynamic(element));
}
return array;
}
case TypeKind::MAP: {
folly::dynamic map = folly::dynamic::object;
for (const auto& [key, value] : object.value<TypeKind::MAP>()) {
map[convertVariantToDynamic(key)] = convertVariantToDynamic(value);
}
return map;
}
case TypeKind::ROW: {
folly::dynamic array = folly::dynamic::array;
for (const auto& element : object.value<TypeKind::ROW>()) {
array.push_back(convertVariantToDynamic(element));
}
return array;
}
default:
VELOX_UNREACHABLE("Unsupported type");
}
}

std::vector<std::string> getControlCharacters() {
static std::vector<std::string> controlCharacters = {
"\x00", "\x01", "\x02", "\x03", "\x04", "\x05", "\x06",
"\x07", "\x08", "\x09", "\x0A", "\x0B", "\x0C", "\x0D",
"\x0E", "\x0F", "\x10", "\x11", "\x12", "\x13", "\x14",
"\x15", "\x16", "\x17", "\x18", "\x19", "\x1A", "\x1B",
"\x1C", "\x1D", "\x1E", "\x1F", "\x20", "\x7F", "\u0080",
"\u0081", "\u0082", "\u0083", "\u0084", "\u0085", "\u0086", "\u0087",
"\u0088", "\u0089", "\u008A", "\u008B", "\u008C", "\u008D", "\u008E",
"\u008F", "\u0090", "\u0091", "\u0092", "\u0093", "\u0094", "\u0095",
"\u0096", "\u0097", "\u0098", "\u0099", "\u009A", "\u009B", "\u009C",
"\u009D", "\u009E", "\u009F"};
return controlCharacters;
};

void JsonInputGenerator::makeRandomVariation(std::string json) {
if (coinToss(rng_, 0.1)) {
const auto controlCharacters = getControlCharacters();
const auto index = rand<uint32_t>(rng_, 0, controlCharacters.size() - 1);
const auto controlCharacter = controlCharacters[index];
const auto indexToInsert = rand<uint32_t>(rng_, 0, json.size());
json.insert(indexToInsert, controlCharacter);
} else if (coinToss(rng_, 0.1)) {
const auto size = rand<uint32_t>(rng_, 0, json.size());
json.resize(size);
}
}

// 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, std::vector<std::unique_ptr<AbstractInputGenerator>>{});
}
return generator;
}

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

0 comments on commit adc5c9b

Please sign in to comment.