Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve fuzzing coverage of abseil-cpp #12721

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions projects/abseil-cpp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,92 @@ cc_fuzz_test(
"@com_google_absl//absl/base:base",
],
)

cc_fuzz_test(
name = "format_arg_fuzzer",
srcs = ["format_arg_fuzzer.cc"],
deps = ["@com_google_absl//absl/strings:str_format"],
)

cc_fuzz_test(
name = "safe_strto128_fuzzer",
srcs = ["safe_strto128_fuzzer.cc"],
deps = [
"@com_google_absl//absl/numeric:int128",
"@com_google_absl//absl/strings",
],
)

cc_fuzz_test(
name = "format_parser_fuzzer",
srcs = ["format_parser_fuzzer.cc"],
deps = [
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/strings:string_view",
],
)

cc_fuzz_test(

name = "format_summarize_fuzzer",
srcs = ["format_summarize_fuzzer.cc"],
deps = [
"@com_google_absl//absl/strings:str_format",
],
)

cc_fuzz_test(
name = "format_arg_dispatch_fuzzer",
srcs = ["format_arg_dispatch_fuzzer.cc"],
deps = ["@com_google_absl//absl/strings:str_format"],
)

cc_fuzz_test(
name = "invoke_object_fuzzer",
srcs = ["invoke_object_fuzzer.cc"],
deps = [
"@com_google_absl//absl/functional:function_ref",
],
)

cc_fuzz_test(
name = "strcontains_ignorecase_fuzzer",
srcs = ["strcontains_ignorecase_fuzzer.cc"],
deps = [
"@com_google_absl//absl/strings",
],
)

cc_fuzz_test(
name = "bigunsigned_constructor_fuzzer",
srcs = ["bigunsigned_constructor_fuzzer.cc"],
deps = [
"@com_google_absl//absl/strings",
],
)

cc_fuzz_test(
name = "int128_ostream_fuzzer",
srcs = ["int128_ostream_fuzzer.cc"],
deps = [
"@com_google_absl//absl/numeric:int128",
],
)

cc_fuzz_test(
name = "parsed_format_base_fuzzer",
srcs = ["parsed_format_base_fuzzer.cc"],
deps = [
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/strings:string_view",
],
)

cc_fuzz_test(
name = "format_wchar_fuzzer",
srcs = ["format_wchar_fuzzer.cc"],
deps = [
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/strings:string_view",
],
)
61 changes: 61 additions & 0 deletions projects/abseil-cpp/bigunsigned_constructor_fuzzer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2024 Google LLC
//
// 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 <fuzzer/FuzzedDataProvider.h>
#include <stddef.h>
#include <stdint.h>
#include <string>

#include "absl/strings/string_view.h"
#include "absl/strings/internal/charconv_bigint.h"

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
FuzzedDataProvider provider(data, size);

// Test with decimal number
std::string numeric_str(
provider.ConsumeIntegralInRange<size_t>(1, 20),
provider.PickValueInArray({'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'})
);
absl::strings_internal::BigUnsigned<4> numeric_num(numeric_str);
numeric_num.ToString();

// Test with maximum digits
std::string max_str(absl::strings_internal::BigUnsigned<4>::Digits10(), '9');
absl::strings_internal::BigUnsigned<4> max_num(max_str);
max_num.ToString();

// Test with mixed chars
std::string mixed_str(
provider.ConsumeIntegralInRange<size_t>(1, 20),
provider.PickValueInArray({'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'})
);
absl::strings_internal::BigUnsigned<4> mixed_num(mixed_str);
mixed_num.ToString();

// Test empty string
absl::strings_internal::BigUnsigned<4> empty_num("");
empty_num.ToString();

// Test non-digit string
std::string non_digit_str(
provider.ConsumeIntegralInRange<size_t>(1, 20),
provider.ConsumeIntegralInRange<char>('a', 'z')
);
absl::strings_internal::BigUnsigned<4> non_digit_num(non_digit_str);
non_digit_num.ToString();

return 0;
}
76 changes: 76 additions & 0 deletions projects/abseil-cpp/format_arg_dispatch_fuzzer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2024 Google LLC
//
// 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 <fuzzer/FuzzedDataProvider.h>
#include <cstdint>
#include <string>
#include <limits>
#include "absl/strings/str_format.h"

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
FuzzedDataProvider provider(data, size);
std::string output;

// Integers
int int_val = provider.ConsumeIntegral<int>();
unsigned int uint_val = provider.ConsumeIntegral<unsigned int>();
int64_t int64_val = provider.ConsumeIntegral<int64_t>();
uint64_t uint64_val = provider.ConsumeIntegral<uint64_t>();

absl::StrFormat("%d%x%o%u%X%ld%lu", int_val, int_val, int_val,
uint_val, uint_val, int64_val, uint64_val);

absl::StrFormat("%d%d%u", std::numeric_limits<int>::max(),
std::numeric_limits<int>::min(),
std::numeric_limits<unsigned int>::max());

// Floating point
double double_val = provider.ConsumeFloatingPoint<double>();
absl::StrFormat("%f%e%g", double_val, double_val, double_val);

static const double special_vals[] = {
std::numeric_limits<double>::infinity(),
-std::numeric_limits<double>::infinity(),
std::numeric_limits<double>::quiet_NaN(),
std::numeric_limits<double>::denorm_min(),
std::numeric_limits<double>::min(),
std::numeric_limits<double>::max()
};
for (double val : special_vals) {
absl::StrFormat("%f%e%g", val, val, val);
}

// Bool and char
bool bool_val = provider.ConsumeBool();
char char_val = provider.ConsumeIntegral<char>();
absl::StrFormat("%d%v%c", bool_val, bool_val, char_val);

static const char special_chars[] = {
'\0', '\n', '\t', '\r', '\\', '\'', '\"', '\x1F'
};
for (char c : special_chars) {
absl::StrFormat("%c", c);
}

// Strings and format flags
std::string str = provider.ConsumeRandomLengthString();
absl::StrFormat("%s%10s%-10s", str.c_str(), str.c_str(), str.c_str());
absl::StrFormat("%s", "");

// Format flags, width and precision
absl::StrFormat("%+d% d%-10d%010d%#x", int_val, int_val, int_val, int_val, int_val);
absl::StrFormat("%10.5f%.10f%15.10f", double_val, double_val, double_val);

return 0;
}
149 changes: 149 additions & 0 deletions projects/abseil-cpp/format_arg_fuzzer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// Copyright 2024 Google LLC
//
// 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 <fuzzer/FuzzedDataProvider.h>
#include <limits>
#include <string>
#include "absl/strings/str_format.h"

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
if (size < 4) return 0;
FuzzedDataProvider provider(data, size);
std::string result;

// Test literal percent (was missing)
result = absl::StrFormat("%%");

// Test basic integer types with different formats and sizes
int value = provider.ConsumeIntegral<int>();
result = absl::StrFormat("%d", value);
result = absl::StrFormat("%i", value);
result = absl::StrFormat("%u", value);
result = absl::StrFormat("%x", value);
result = absl::StrFormat("%X", value);
result = absl::StrFormat("%o", value);

// Test integers with length modifiers
result = absl::StrFormat("%hhd", value);
result = absl::StrFormat("%hd", value);
result = absl::StrFormat("%ld", value);
result = absl::StrFormat("%lld", value);
result = absl::StrFormat("%zd", value);
result = absl::StrFormat("%jd", value);
result = absl::StrFormat("%td", value);

// Test different integer sizes
int8_t i8 = provider.ConsumeIntegral<int8_t>();
result = absl::StrFormat("%d", i8);

int16_t i16 = provider.ConsumeIntegral<int16_t>();
result = absl::StrFormat("%d", i16);

int64_t i64 = provider.ConsumeIntegral<int64_t>();
result = absl::StrFormat("%d", i64);

uint64_t u64 = provider.ConsumeIntegral<uint64_t>();
result = absl::StrFormat("%u", u64);

// Test integer format flags
result = absl::StrFormat("%+d", value);
result = absl::StrFormat("% d", value);
result = absl::StrFormat("%-8d", value);
result = absl::StrFormat("%08d", value);
result = absl::StrFormat("%#x", value);
// Additional flag combinations (were missing)
result = absl::StrFormat("%+#x", value);
result = absl::StrFormat("% #x", value);
result = absl::StrFormat("%-#x", value);

// Test floating point formats
double double_val = provider.ConsumeFloatingPoint<double>();
result = absl::StrFormat("%f", double_val);
result = absl::StrFormat("%e", double_val);
result = absl::StrFormat("%g", double_val);
result = absl::StrFormat("%E", double_val);
result = absl::StrFormat("%G", double_val);
result = absl::StrFormat("%.2f", double_val);
result = absl::StrFormat("%10.2f", double_val);

// Additional float tests (were missing)
result = absl::StrFormat("%a", double_val);
result = absl::StrFormat("%A", double_val);
result = absl::StrFormat("%#f", double_val);

// Test extreme width/precision (were missing)
result = absl::StrFormat("%100d", value);
result = absl::StrFormat("%.100d", value);
result = absl::StrFormat("%100.100d", value);

// Test string formats
std::string str = provider.ConsumeRandomLengthString();
result = absl::StrFormat("%s", str);
result = absl::StrFormat("%10s", str);
result = absl::StrFormat("%-10s", str);
result = absl::StrFormat("%.5s", str);

// Test invalid format combinations (were missing)
result = absl::StrFormat("%+s", str); // + with string
result = absl::StrFormat("% p", &str); // space with pointer

// Test pointer format
void* ptr = reinterpret_cast<void*>(provider.ConsumeIntegral<uintptr_t>());
result = absl::StrFormat("%p", ptr);

// Test character format
char c = provider.ConsumeIntegral<char>();
result = absl::StrFormat("%c", c);

// Test width and precision
int width = provider.ConsumeIntegralInRange<int>(0, 100);
int precision = provider.ConsumeIntegralInRange<int>(0, 20);
result = absl::StrFormat("%*d", width, value);
result = absl::StrFormat("%.*f", precision, double_val);
result = absl::StrFormat("%*.*f", width, precision, double_val);

// Test limit values
result = absl::StrFormat("%d", std::numeric_limits<int>::max());
result = absl::StrFormat("%d", std::numeric_limits<int>::min());
result = absl::StrFormat("%u", std::numeric_limits<unsigned int>::max());
result = absl::StrFormat("%lld", std::numeric_limits<long long>::max());
result = absl::StrFormat("%lld", std::numeric_limits<long long>::min());
result = absl::StrFormat("%llu", std::numeric_limits<unsigned long long>::max());

// Test special floating point values (were missing)
double special_values[] = {
std::numeric_limits<double>::infinity(),
-std::numeric_limits<double>::infinity(),
std::numeric_limits<double>::quiet_NaN(),
std::numeric_limits<double>::denorm_min(),
std::numeric_limits<double>::min(),
std::numeric_limits<double>::max()
};

for (double d : special_values) {
result = absl::StrFormat("%f", d);
result = absl::StrFormat("%e", d);
result = absl::StrFormat("%g", d);
result = absl::StrFormat("%a", d);
}

// Test multiple argument formats
result = absl::StrFormat("%d:%s:%g", value, str, double_val);
result = absl::StrFormat("%x:%p:%.2f", value, ptr, double_val);
result = absl::StrFormat("i=%d s='%s' f=%g", value, str, double_val);

return 0;
}
Loading