Skip to content

Commit

Permalink
Merge pull request #24320 from IoannisRP/ik-bazel-tests-fuzz
Browse files Browse the repository at this point in the history
bytes: bazelize iobuf_fuzz
  • Loading branch information
rockwotj authored Dec 19, 2024
2 parents 27905b2 + 2992a1a commit c32a996
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ common --extra_toolchains=@llvm_18_toolchain//:all
common --@toolchains_llvm//toolchain/config:libunwind=False
common --@toolchains_llvm//toolchain/config:compiler-rt=False
build --linkopt --unwindlib=libgcc
build --linkopt -stdlib=libc++

common:clang-19 --extra_toolchains=@llvm_19_toolchain//:all

Expand Down Expand Up @@ -47,6 +48,7 @@ build:sanitizer --linkopt -fsanitize=address,undefined,vptr,function
build:sanitizer --linkopt --rtlib=compiler-rt
build:sanitizer --linkopt -fsanitize-link-c++-runtime
build:sanitizer --copt -O1
build:sanitizer --//bazel:has_sanitizers=True
# seastar has to be run with system allocator when using sanitizers
build:sanitizer --@seastar//:system_allocator=True
# krb5 is a foreign cc build and needs explicit list of sanitizers to build the shared library
Expand Down
13 changes: 13 additions & 0 deletions bazel/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")

bool_flag(
name = "has_sanitizers",
build_setting_default = False,
)

config_setting(
name = "enable_fuzz_testing",
flag_values = {
":has_sanitizers": "true",
},
)
67 changes: 67 additions & 0 deletions bazel/test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,53 @@ def _redpanda_cc_test(
local_defines = local_defines,
)

def _redpanda_cc_fuzz_test(
name,
timeout,
srcs = [],
defines = [],
deps = [],
custom_args = [],
env = {},
data = []):
"""
Helper to define a Redpanda C++ fuzzing test.
Args:
name: name of the test
timeout: same as native cc_test
srcs: test source files
defines: definitions of object-like macros
deps: test dependencies
custom_args: arguments from cc_test users
env: environment variables
data: data file dependencies
"""
native.cc_test(
name = name,
timeout = timeout,
srcs = srcs,
defines = defines,
deps = deps,
copts = redpanda_copts(),
args = custom_args,
features = [
"layering_check",
],
tags = [
"fuzz",
],
env = env,
data = data,
linkopts = [
"-fsanitize=fuzzer",
],
target_compatible_with = select({
"//bazel:enable_fuzz_testing": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
)

def _redpanda_cc_unit_test(cpu, memory, **kwargs):
extra_args = [
"--unsafe-bypass-fsync 1",
Expand Down Expand Up @@ -231,6 +278,26 @@ def redpanda_cc_bench(
],
)

def redpanda_cc_fuzz_test(
name,
timeout,
srcs = [],
defines = [],
deps = [],
args = [],
env = {},
data = []):
_redpanda_cc_fuzz_test(
data = data,
env = env,
name = name,
timeout = timeout,
srcs = srcs,
defines = defines,
deps = deps,
custom_args = args,
)

def redpanda_cc_btest_no_seastar(
name,
timeout,
Expand Down
17 changes: 16 additions & 1 deletion src/v/bytes/tests/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//bazel:test.bzl", "redpanda_cc_btest", "redpanda_cc_gtest")
load("//bazel:test.bzl", "redpanda_cc_btest", "redpanda_cc_fuzz_test", "redpanda_cc_gtest")

redpanda_cc_btest(
name = "iobuf_test",
Expand Down Expand Up @@ -76,3 +76,18 @@ redpanda_cc_btest(
"@seastar//:testing",
],
)

redpanda_cc_fuzz_test(
name = "iobuf_fuzz",
timeout = "short",
srcs = ["iobuf_fuzz.cc"],
args = [
"-max_total_time=30",
"-rss_limit_mb=8192",
],
deps = [
"//src/v/base",
"//src/v/bytes:iobuf",
"//src/v/bytes:scattered_message",
],
)
4 changes: 2 additions & 2 deletions src/v/bytes/tests/iobuf_fuzz.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
* -format=html ../src/v/bytes/iobuf.h ../src/v/bytes/iobuf.cc > cov.html
*/
#include "base/vassert.h"
#include "bytes/bytes.h"
#include "bytes/iobuf.h"
#include "bytes/scattered_message.h"

#include <exception>
#include <numeric>

/*
Expand Down Expand Up @@ -508,7 +508,7 @@ class driver {

template<typename T>
T read() {
if (std::distance(pc_, program_.cend()) < sizeof(T)) {
if (std::cmp_less(std::distance(pc_, program_.cend()), sizeof(T))) {
throw end_of_program();
}
T ret;
Expand Down

0 comments on commit c32a996

Please sign in to comment.