Skip to content

Commit

Permalink
[fuzzers] Update chttp2 grpc_fuzzer fuzzers to fuzztest (grpc#38535)
Browse files Browse the repository at this point in the history
Closes grpc#38535

COPYBARA_INTEGRATE_REVIEW=grpc#38535 from ctiller:huffy 623f4d9
PiperOrigin-RevId: 720303196
  • Loading branch information
ctiller authored and copybara-github committed Jan 27, 2025
1 parent 460d637 commit 682ae95
Show file tree
Hide file tree
Showing 86 changed files with 63 additions and 151 deletions.
35 changes: 11 additions & 24 deletions test/core/transport/chttp2/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

load("//bazel:custom_exec_properties.bzl", "LARGE_MACHINE")
load("//bazel:grpc_build_system.bzl", "grpc_cc_proto_library", "grpc_cc_test", "grpc_internal_proto_library", "grpc_package")
load("//test/core/test_util:grpc_fuzzer.bzl", "grpc_fuzz_test", "grpc_fuzzer", "grpc_proto_fuzzer")
load("//test/core/test_util:grpc_fuzzer.bzl", "grpc_fuzz_test", "grpc_proto_fuzzer")

licenses(["notice"])

Expand Down Expand Up @@ -83,46 +83,33 @@ grpc_fuzz_test(
],
)

grpc_fuzzer(
grpc_fuzz_test(
name = "hpack_parser_input_size_fuzzer",
srcs = ["hpack_parser_input_size_fuzzer.cc"],
corpus = "hpack_parser_input_size_corpus",
external_deps = [
"absl/cleanup",
"absl/status:statusor",
"absl/status",
"absl/random:mocking_bit_gen",
"gtest",
"fuzztest",
"fuzztest_main",
],
tags = ["no_windows"],
uses_polling = False,
deps = [
"//:grpc",
"//test/core/test_util:grpc_test_util",
"//test/core/test_util:grpc_test_util_base",
],
)

grpc_fuzzer(
name = "decode_huff_fuzzer",
srcs = ["decode_huff_fuzzer.cc"],
corpus = "decode_huff_corpus",
external_deps = [
"absl/strings",
],
tags = ["no_windows"],
deps = [
"//:grpc",
"//src/core:decode_huff",
"//src/core:huffsyms",
],
)

grpc_fuzzer(
name = "encode_decode_huff_fuzzer",
srcs = ["encode_decode_huff_fuzzer.cc"],
corpus = "encode_decode_huff_corpus",
grpc_fuzz_test(
name = "huff_fuzzer",
srcs = ["huff_fuzzer.cc"],
external_deps = [
"absl/strings",
"gtest",
"fuzztest",
"fuzztest_main",
],
tags = ["no_windows"],
deps = [
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Empty file.

This file was deleted.

This file was deleted.

Empty file.

This file was deleted.

This file was deleted.

74 changes: 0 additions & 74 deletions test/core/transport/chttp2/encode_decode_huff_fuzzer.cc

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
31 changes: 12 additions & 19 deletions test/core/transport/chttp2/hpack_parser_input_size_fuzzer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "fuzztest/fuzztest.h"
#include "gtest/gtest.h"
#include "src/core/ext/transport/chttp2/transport/hpack_parser.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/exec_ctx.h"
Expand All @@ -44,8 +46,7 @@
#include "src/core/util/status_helper.h"
#include "test/core/test_util/slice_splitter.h"

bool squelch = true;
bool leak_check = true;
extern gpr_timespec (*gpr_now_impl)(gpr_clock_type clock_type);

namespace grpc_core {
namespace {
Expand Down Expand Up @@ -134,24 +135,16 @@ std::string Stringify(absl::StatusOr<std::string> result) {
}
}

} // namespace
} // namespace grpc_core

extern gpr_timespec (*gpr_now_impl)(gpr_clock_type clock_type);

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
void SameHpackResultRegardlessOfSplitMode(std::vector<uint8_t> buffer) {
gpr_now_impl = [](gpr_clock_type clock_type) {
return gpr_timespec{10, 0, clock_type};
};
auto slice = grpc_core::Slice::FromCopiedBuffer(data, size);
auto full = grpc_core::Stringify(
grpc_core::TestVector(GRPC_SLICE_SPLIT_IDENTITY, slice.Ref()));
auto one_byte = grpc_core::Stringify(
grpc_core::TestVector(GRPC_SLICE_SPLIT_ONE_BYTE, slice.Ref()));
if (full != one_byte) {
fprintf(stderr, "MISMATCHED RESULTS\nFULL SLICE: %s\nONE BYTE: %s\n",
full.c_str(), one_byte.c_str());
abort();
}
return 0;
auto slice = Slice::FromCopiedBuffer(buffer.data(), buffer.size());
auto full = Stringify(TestVector(GRPC_SLICE_SPLIT_IDENTITY, slice.Ref()));
auto one_byte = Stringify(TestVector(GRPC_SLICE_SPLIT_ONE_BYTE, slice.Ref()));
EXPECT_EQ(full, one_byte);
}
FUZZ_TEST(HpackParser, SameHpackResultRegardlessOfSplitMode);

} // namespace
} // namespace grpc_core
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <grpc/slice.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <cstdint>
Expand All @@ -23,11 +25,37 @@

#include "absl/strings/str_cat.h"
#include "absl/strings/str_join.h"
#include "fuzztest/fuzztest.h"
#include "gtest/gtest.h"
#include "src/core/ext/transport/chttp2/transport/bin_encoder.h"
#include "src/core/ext/transport/chttp2/transport/decode_huff.h"
#include "src/core/ext/transport/chttp2/transport/huffsyms.h"
#include "src/core/util/dump_args.h"

bool squelch = true;
bool leak_check = true;
namespace grpc_core {
namespace {

std::string ToString(std::optional<std::vector<uint8_t>> s) {
if (s == std::nullopt) return "nullopt";
return absl::StrCat("{", absl::StrJoin(*s, ","), "}");
}

void EncodeDecodeRoundTrips(std::vector<uint8_t> buffer) {
grpc_slice uncompressed = grpc_slice_from_copied_buffer(
reinterpret_cast<const char*>(buffer.data()), buffer.size());
grpc_slice compressed = grpc_chttp2_huffman_compress(uncompressed);
std::vector<uint8_t> uncompressed_again;
auto add = [&uncompressed_again](uint8_t c) {
uncompressed_again.push_back(c);
};
EXPECT_TRUE(HuffDecoder<decltype(add)>(add, GRPC_SLICE_START_PTR(compressed),
GRPC_SLICE_END_PTR(compressed))
.Run());
EXPECT_EQ(buffer, uncompressed_again);
grpc_slice_unref(uncompressed);
grpc_slice_unref(compressed);
}
FUZZ_TEST(HuffTest, EncodeDecodeRoundTrips);

std::optional<std::vector<uint8_t>> DecodeHuffSlow(const uint8_t* begin,
const uint8_t* end) {
Expand Down Expand Up @@ -71,29 +99,23 @@ std::optional<std::vector<uint8_t>> DecodeHuffSlow(const uint8_t* begin,
return out;
}

std::string ToString(std::optional<std::vector<uint8_t>> s) {
if (s == std::nullopt) return "nullopt";
return absl::StrCat("{", absl::StrJoin(*s, ","), "}");
}

std::optional<std::vector<uint8_t>> DecodeHuffFast(const uint8_t* begin,
const uint8_t* end) {
std::vector<uint8_t> v;
auto f = [&](uint8_t x) { v.push_back(x); };
if (!grpc_core::HuffDecoder<decltype(f)>(f, begin, end).Run()) {
if (!HuffDecoder<decltype(f)>(f, begin, end).Run()) {
return std::nullopt;
}
return v;
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
auto slow = DecodeHuffSlow(data, data + size);
auto fast = DecodeHuffFast(data, data + size);
if (slow != fast) {
fprintf(stderr, "MISMATCH:\ninpt: %s\nslow: %s\nfast: %s\n",
ToString(std::vector<uint8_t>(data, data + size)).c_str(),
ToString(slow).c_str(), ToString(fast).c_str());
abort();
}
return 0;
void DifferentialOptimizedTest(std::vector<uint8_t> buffer) {
auto slow = DecodeHuffSlow(buffer.data(), buffer.data() + buffer.size());
auto fast = DecodeHuffFast(buffer.data(), buffer.data() + buffer.size());
EXPECT_EQ(fast, slow) << GRPC_DUMP_ARGS(ToString(buffer), ToString(slow),
ToString(fast));
}
FUZZ_TEST(HuffTest, DifferentialOptimizedTest);

} // namespace
} // namespace grpc_core

0 comments on commit 682ae95

Please sign in to comment.