Skip to content

Commit 16bf962

Browse files
FuzzTest Teamcopybara-github
FuzzTest Team
authored andcommitted
Add flatbuffers domain
PiperOrigin-RevId: 732932597
1 parent ae9c378 commit 16bf962

21 files changed

+1722
-4
lines changed

.github/workflows/cmake_test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ jobs:
7777
-D CMAKE_CXX_COMPILER_LAUNCHER=ccache \
7878
-D CMAKE_BUILD_TYPE=RelWithDebug \
7979
-D FUZZTEST_BUILD_TESTING=on \
80+
-D FUZZTEST_BUILD_FLATBUFFERS=on \
8081
&& cmake --build build -j $(nproc) \
8182
&& ctest --test-dir build -j $(nproc) --output-on-failure
8283
- name: Run all tests in default mode with gcc
@@ -90,6 +91,7 @@ jobs:
9091
-D CMAKE_CXX_COMPILER_LAUNCHER=ccache \
9192
-D CMAKE_BUILD_TYPE=RelWithDebug \
9293
-D FUZZTEST_BUILD_TESTING=on \
94+
-D FUZZTEST_BUILD_FLATBUFFERS=on \
9395
&& cmake --build build_gcc -j $(nproc) \
9496
&& ctest --test-dir build_gcc -j $(nproc) --output-on-failure
9597
- name: Run end-to-end tests in fuzzing mode
@@ -104,6 +106,7 @@ jobs:
104106
-D CMAKE_BUILD_TYPE=RelWithDebug \
105107
-D FUZZTEST_FUZZING_MODE=on \
106108
-D FUZZTEST_BUILD_TESTING=on \
109+
-D FUZZTEST_BUILD_FLATBUFFERS=on \
107110
&& cmake --build build -j $(nproc) \
108111
&& ctest --test-dir build -j $(nproc) --output-on-failure -R "functional_test"
109112
- name: Save new cache based on main

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.19)
22
project(fuzztest)
33

44
option(FUZZTEST_BUILD_TESTING "Building the tests." OFF)
5+
option(FUZZTEST_BUILD_FLATBUFFERS "Building the flatbuffers support." OFF)
56
option(FUZZTEST_FUZZING_MODE "Building the fuzztest in fuzzing mode." OFF)
67
set(FUZZTEST_COMPATIBILITY_MODE "" CACHE STRING "Compatibility mode. Available options: <empty>, libfuzzer")
78
set(CMAKE_CXX_STANDARD 17)

MODULE.bazel

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ bazel_dep(
4242
name = "platforms",
4343
version = "0.0.10",
4444
)
45+
bazel_dep(
46+
name = "flatbuffers",
47+
version = "25.2.10"
48+
)
4549
# GoogleTest is not a dev dependency, because it's needed when FuzzTest is used
4650
# with GoogleTest integration (e.g., googletest_adaptor). Note that the FuzzTest
4751
# framework can be used without GoogleTest integration as well.
@@ -55,8 +59,6 @@ bazel_dep(
5559
name = "protobuf",
5660
version = "30.2",
5761
)
58-
# TODO(lszekeres): Make this a dev dependency, as the protobuf library is only
59-
# required for testing.
6062
bazel_dep(
6163
name = "rules_proto",
6264
version = "7.1.0",

cmake/BuildDependencies.cmake

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ set(proto_TAG v30.2)
2121
set(nlohmann_json_URL https://github.com/nlohmann/json.git)
2222
set(nlohmann_json_TAG v3.11.3)
2323

24+
set(flatbuffers_URL https://github.com/google/flatbuffers.git)
25+
set(flatbuffers_TAG v25.2.10)
26+
2427
if(POLICY CMP0135)
2528
cmake_policy(SET CMP0135 NEW)
2629
set(CMAKE_POLICY_DEFAULT_CMP0135 NEW)
@@ -50,6 +53,14 @@ FetchContent_Declare(
5053
URL_HASH MD5=${antlr_cpp_MD5}
5154
)
5255

56+
if (FUZZTEST_BUILD_FLATBUFFERS)
57+
FetchContent_Declare(
58+
flatbuffers
59+
GIT_REPOSITORY ${flatbuffers_URL}
60+
GIT_TAG ${flatbuffers_TAG}
61+
)
62+
endif()
63+
5364
if (FUZZTEST_BUILD_TESTING)
5465

5566
FetchContent_Declare(
@@ -87,3 +98,9 @@ if (FUZZTEST_BUILD_TESTING)
8798
FetchContent_MakeAvailable(nlohmann_json)
8899

89100
endif ()
101+
102+
if (FUZZTEST_BUILD_FLATBUFFERS)
103+
set(FLATBUFFERS_BUILD_TESTS OFF)
104+
set(FLATBUFFERS_BUILD_INSTALL OFF)
105+
FetchContent_MakeAvailable(flatbuffers)
106+
endif()

cmake/generate_cmake_from_bazel.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"@abseil-cpp//absl/types:optional": "absl::optional",
5353
"@abseil-cpp//absl/types:span": "absl::span",
5454
"@abseil-cpp//absl/types:variant": "absl::variant",
55+
"@flatbuffers//:runtime_cc": "flatbuffers",
5556
"@googletest//:gtest": "GTest::gtest",
5657
"@googletest//:gtest_main": "GTest::gmock_main",
5758
"@protobuf//:protobuf": "protobuf::libprotobuf",

domain_tests/BUILD

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,29 @@ cc_test(
2525
":domain_testing",
2626
"@abseil-cpp//absl/container:flat_hash_set",
2727
"@abseil-cpp//absl/random",
28+
"@abseil-cpp//absl/random:bit_gen_ref",
29+
"@abseil-cpp//absl/status",
2830
"@abseil-cpp//absl/types:optional",
2931
"@abseil-cpp//absl/types:span",
3032
"@abseil-cpp//absl/types:variant",
3133
"@com_google_fuzztest//fuzztest:domain_core",
34+
"@com_google_fuzztest//fuzztest:type_support",
35+
"@googletest//:gtest_main",
36+
],
37+
)
38+
39+
cc_test(
40+
name = "arbitrary_domains_flatbuffers_test",
41+
srcs = ["arbitrary_domains_flatbuffers_test.cc"],
42+
deps = [
43+
":domain_testing",
44+
"@abseil-cpp//absl/container:flat_hash_map",
45+
"@abseil-cpp//absl/random",
46+
"@com_google_fuzztest//fuzztest:domain",
47+
"@com_google_fuzztest//fuzztest:flatbuffers",
48+
"@com_google_fuzztest//fuzztest:meta",
49+
"@com_google_fuzztest//fuzztest:test_flatbuffers_cc_fbs",
50+
"@flatbuffers//:runtime_cc",
3251
"@googletest//:gtest_main",
3352
],
3453
)

domain_tests/CMakeLists.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,40 @@ fuzztest_cc_test(
1111
DEPS
1212
fuzztest::domain_testing
1313
absl::flat_hash_set
14+
absl::random_bit_gen_ref
1415
absl::random_random
1516
absl::optional
1617
absl::span
18+
absl::status
1719
absl::variant
1820
fuzztest::domain_core
21+
fuzztest::type_support
1922
GTest::gmock_main
2023
)
2124

25+
if (FUZZTEST_BUILD_FLATBUFFERS)
26+
fuzztest_cc_test(
27+
NAME
28+
arbitrary_domains_flatbuffers_test
29+
SRCS
30+
"arbitrary_domains_flatbuffers_test.cc"
31+
DEPS
32+
absl::flat_hash_set
33+
absl::random_bit_gen_ref
34+
absl::random_random
35+
absl::strings
36+
flatbuffers
37+
fuzztest::domain
38+
fuzztest::domain_testing
39+
fuzztest::flatbuffers
40+
GTest::gmock_main
41+
test_flatbuffers
42+
)
43+
add_dependencies(fuzztest_arbitrary_domains_flatbuffers_test
44+
GENERATE_test_flatbuffers
45+
)
46+
endif()
47+
2248
fuzztest_cc_test(
2349
NAME
2450
arbitrary_domains_protobuf_test

domain_tests/aggregate_combinators_test.cc

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,23 @@
1717
#include <cstdint>
1818
#include <optional>
1919
#include <string>
20+
#include <tuple>
2021
#include <utility>
2122
#include <variant>
2223
#include <vector>
2324

2425
#include "gmock/gmock.h"
2526
#include "gtest/gtest.h"
2627
#include "absl/container/flat_hash_set.h"
28+
#include "absl/random/bit_gen_ref.h"
2729
#include "absl/random/random.h"
30+
#include "absl/status/status.h"
2831
#include "absl/types/optional.h"
2932
#include "absl/types/span.h"
3033
#include "absl/types/variant.h"
3134
#include "./fuzztest/domain_core.h"
3235
#include "./domain_tests/domain_testing.h"
36+
#include "./fuzztest/internal/type_support.h"
3337

3438
namespace fuzztest {
3539
namespace {
@@ -446,5 +450,97 @@ TEST(TupleOf, ValidationRejectsInvalidInnerValue) {
446450
R"(Invalid value in aggregate >> The value .+ is not InRange\(.+\))")));
447451
}
448452

453+
TEST(TupleOf, DomainWithPairCorpusType) {
454+
class DomainWithPairCorpusType
455+
: public fuzztest::domain_implementor::DomainBase<
456+
DomainWithPairCorpusType, std::pair<int, int>> {
457+
public:
458+
using corpus_type = DomainWithPairCorpusType::DomainBase::corpus_type;
459+
using value_type = DomainWithPairCorpusType::DomainBase::value_type;
460+
461+
corpus_type Init(absl::BitGenRef prng) { return std::make_pair(0, 0); }
462+
463+
void Mutate(corpus_type& val, absl::BitGenRef prng,
464+
const domain_implementor::MutationMetadata& metadata,
465+
bool only_shrink) {}
466+
467+
absl::Status ValidateCorpusValue(const corpus_type& value) const {
468+
return absl::OkStatus();
469+
}
470+
471+
auto GetPrinter() const { return internal::UnknownPrinter(); }
472+
};
473+
474+
{
475+
auto domain = TupleOf(DomainWithPairCorpusType{});
476+
auto value = std::make_tuple(std::make_pair(1, 2));
477+
auto optional_corpus_tuple = domain.FromValue(value);
478+
EXPECT_TRUE(optional_corpus_tuple.has_value());
479+
if (optional_corpus_tuple.has_value()) {
480+
EXPECT_EQ(std::get<0>(optional_corpus_tuple.value()),
481+
std::make_pair(1, 2));
482+
}
483+
}
484+
{
485+
auto domain =
486+
TupleOf(DomainWithPairCorpusType{}, DomainWithPairCorpusType{});
487+
auto value = std::make_tuple(std::make_pair(1, 2), std::make_pair(3, 4));
488+
auto optional_corpus_tuple = domain.FromValue(value);
489+
EXPECT_TRUE(optional_corpus_tuple.has_value());
490+
if (optional_corpus_tuple.has_value()) {
491+
EXPECT_EQ(std::get<0>(optional_corpus_tuple.value()),
492+
std::make_pair(1, 2));
493+
EXPECT_EQ(std::get<1>(optional_corpus_tuple.value()),
494+
std::make_pair(3, 4));
495+
}
496+
}
497+
}
498+
499+
TEST(TupleOf, DomainWithTupleCorpusType) {
500+
class DomainWithTupleCorpusType
501+
: public fuzztest::domain_implementor::DomainBase<
502+
DomainWithTupleCorpusType, std::tuple<int, int>> {
503+
public:
504+
using corpus_type = DomainWithTupleCorpusType::DomainBase::corpus_type;
505+
using value_type = DomainWithTupleCorpusType::DomainBase::value_type;
506+
507+
corpus_type Init(absl::BitGenRef prng) { return std::make_tuple(0, 0); }
508+
509+
void Mutate(corpus_type& val, absl::BitGenRef prng,
510+
const domain_implementor::MutationMetadata& metadata,
511+
bool only_shrink) {}
512+
513+
absl::Status ValidateCorpusValue(const corpus_type& value) const {
514+
return absl::OkStatus();
515+
}
516+
517+
auto GetPrinter() const { return internal::UnknownPrinter(); }
518+
};
519+
520+
{
521+
auto domain = TupleOf(DomainWithTupleCorpusType{});
522+
auto value = std::make_tuple(std::make_pair(1, 2));
523+
auto optional_corpus_tuple = domain.FromValue(value);
524+
EXPECT_TRUE(optional_corpus_tuple.has_value());
525+
if (optional_corpus_tuple.has_value()) {
526+
EXPECT_EQ(std::get<0>(optional_corpus_tuple.value()),
527+
std::make_tuple(1, 2));
528+
}
529+
}
530+
{
531+
auto domain =
532+
TupleOf(DomainWithTupleCorpusType{}, DomainWithTupleCorpusType{});
533+
auto value = std::make_tuple(std::make_tuple(1, 2), std::make_tuple(3, 4));
534+
auto optional_corpus_tuple = domain.FromValue(value);
535+
EXPECT_TRUE(optional_corpus_tuple.has_value());
536+
if (optional_corpus_tuple.has_value()) {
537+
EXPECT_EQ(std::get<0>(optional_corpus_tuple.value()),
538+
std::make_tuple(1, 2));
539+
EXPECT_EQ(std::get<1>(optional_corpus_tuple.value()),
540+
std::make_tuple(3, 4));
541+
}
542+
}
543+
}
544+
449545
} // namespace
450546
} // namespace fuzztest

0 commit comments

Comments
 (0)