Skip to content

Commit

Permalink
ICU-22479 Add Fuzzer for DateFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankYFTang committed Sep 16, 2023
1 parent 14ca2b0 commit d082de5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion icu4c/source/test/fuzzer/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ CPPFLAGS += -I$(srcdir) -I$(top_srcdir)/common -I$(top_srcdir)/i18n -I$(top_srcd
DEFS += -D'U_TOPSRCDIR="$(top_srcdir)/"' -D'U_TOPBUILDDIR="$(BUILDDIR)"'
LIBS = $(LIBCTESTFW) $(LIBICUTOOLUTIL) $(LIBICUIO) $(LIBICUI18N) $(LIBICUUC) $(DEFAULT_LIBS) $(LIB_M)

FUZZER_TARGETS = break_iterator_fuzzer calendar_fuzzer collator_compare_fuzzer collator_rulebased_fuzzer converter_fuzzer locale_fuzzer locale_morph_fuzzer number_format_fuzzer ucasemap_fuzzer uloc_canonicalize_fuzzer uloc_for_language_tag_fuzzer uloc_get_name_fuzzer uloc_is_right_to_left_fuzzer uloc_open_keywords_fuzzer unicode_string_codepage_create_fuzzer uregex_open_fuzzer
FUZZER_TARGETS = break_iterator_fuzzer calendar_fuzzer collator_compare_fuzzer collator_rulebased_fuzzer converter_fuzzer date_format_fuzzer locale_fuzzer locale_morph_fuzzer number_format_fuzzer ucasemap_fuzzer uloc_canonicalize_fuzzer uloc_for_language_tag_fuzzer uloc_get_name_fuzzer uloc_is_right_to_left_fuzzer uloc_open_keywords_fuzzer unicode_string_codepage_create_fuzzer uregex_open_fuzzer

OBJECTS = $(FUZZER_TARGETS:%=%.o)
OBJECTS += fuzzer_driver.o locale_util.o
Expand Down
52 changes: 52 additions & 0 deletions icu4c/source/test/fuzzer/date_format_fuzzer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// © 2023 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html

// Fuzzer for ICU Calendar.

#include <cstring>

#include "fuzzer_utils.h"

#include "unicode/datefmt.h"
#include "unicode/locid.h"

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
uint16_t rnd;
UDate date;
icu::DateFormat::EStyle dateStyle;
icu::DateFormat::EStyle timeStyle;
if (size < sizeof(rnd) + sizeof(date) + sizeof(dateStyle) + sizeof(timeStyle)) return 0;
icu::StringPiece fuzzData(reinterpret_cast<const char *>(data), size);

std::memcpy(&rnd, fuzzData.data(), sizeof(rnd));
fuzzData.remove_prefix(sizeof(rnd));
icu::Locale locale = GetRandomLocale(rnd);

std::memcpy(&dateStyle, fuzzData.data(), sizeof(dateStyle));
fuzzData.remove_prefix(sizeof(dateStyle));
std::memcpy(&timeStyle, fuzzData.data(), sizeof(timeStyle));
fuzzData.remove_prefix(sizeof(timeStyle));
std::memcpy(&date, fuzzData.data(), sizeof(date));
fuzzData.remove_prefix(sizeof(date));

std::unique_ptr<icu::DateFormat> df(
icu::DateFormat::createDateTimeInstance(dateStyle, timeStyle, locale));
icu::UnicodeString appendTo;
df->format(date, appendTo);
icu::UnicodeString skeleton = icu::UnicodeString::fromUTF8(fuzzData);

UErrorCode status = U_ZERO_ERROR;
appendTo.remove();
df.reset(icu::DateFormat::createInstanceForSkeleton(skeleton, status));
if (U_SUCCESS(status)) {
df->format(date, appendTo);
}

status = U_ZERO_ERROR;
appendTo.remove();
df.reset(icu::DateFormat::createInstanceForSkeleton(skeleton, locale, status));
if (U_SUCCESS(status)) {
df->format(date, appendTo);
}
return EXIT_SUCCESS;
}

1 comment on commit d082de5

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: d082de5 Previous: 14ca2b0 Ratio
TestCharsetDecoderICU 6.033186941381195 ns/iter 2.7950331629283 ns/iter 2.16

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.