From 84ae742ea01565054a4cccd7fbaed51753ecd80d Mon Sep 17 00:00:00 2001 From: Frank Tang Date: Tue, 5 Sep 2023 14:11:29 -0700 Subject: [PATCH] ICU-22479 Add fuzzer for Calendar API --- icu4c/source/test/fuzzer/Makefile.in | 2 +- icu4c/source/test/fuzzer/calendar_fuzzer.cpp | 132 +++++++++++++++++++ 2 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 icu4c/source/test/fuzzer/calendar_fuzzer.cpp diff --git a/icu4c/source/test/fuzzer/Makefile.in b/icu4c/source/test/fuzzer/Makefile.in index 4b0f610694a5..da22b464163c 100644 --- a/icu4c/source/test/fuzzer/Makefile.in +++ b/icu4c/source/test/fuzzer/Makefile.in @@ -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 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 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 diff --git a/icu4c/source/test/fuzzer/calendar_fuzzer.cpp b/icu4c/source/test/fuzzer/calendar_fuzzer.cpp new file mode 100644 index 000000000000..c6071fdd4110 --- /dev/null +++ b/icu4c/source/test/fuzzer/calendar_fuzzer.cpp @@ -0,0 +1,132 @@ +// © 2023 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html + +// Fuzzer for ICU Calendar. + +#include + +#include "fuzzer_utils.h" + +#include "unicode/calendar.h" +#include "unicode/localebuilder.h" +#include "unicode/locid.h" + +icu::TimeZone* CreateRandomTimeZone(uint16_t rnd) { + icu::Locale und("und"); + UErrorCode status = U_ZERO_ERROR; + std::unique_ptr enumeration( + icu::TimeZone::createEnumeration(status)); + if (U_SUCCESS(status)) { + int32_t count = enumeration->count(status); + if (U_SUCCESS(status)) { + int32_t i = rnd % count; + const icu::UnicodeString* id = nullptr; + do { + id = enumeration->snext(status); + } while (U_SUCCESS(status) && --i > 0); + if (U_SUCCESS(status)) { + return icu::TimeZone::createTimeZone(*id); + } + } + } + return icu::TimeZone::getGMT()->clone(); +} +const char* GetRandomCalendarType(uint8_t rnd) { + icu::Locale und("und"); + UErrorCode status = U_ZERO_ERROR; + std::unique_ptr enumeration( + icu::Calendar::getKeywordValuesForLocale("calendar", und, false, status)); + const char* type = ""; + if (U_SUCCESS(status)) { + int32_t count = enumeration->count(status); + if (U_SUCCESS(status)) { + int32_t i = rnd % count; + do { + type = enumeration->next(nullptr, status); + } while (U_SUCCESS(status) && --i > 0); + } + } + type = uloc_toUnicodeLocaleType("ca", type); + return type; +} + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { + uint16_t rnd; + if (size < 2*sizeof(rnd) + 1) return 0; + icu::StringPiece fuzzData(reinterpret_cast(data), size); + // Byte 0 and 1 randomly select a TimeZone + std::memcpy(&rnd, fuzzData.data(), sizeof(rnd)); + fuzzData.remove_prefix(sizeof(rnd)); + std::unique_ptr timeZone(CreateRandomTimeZone(rnd)); + + // Byte 1 and 2 randomly select a Locale + std::memcpy(&rnd, fuzzData.data(), sizeof(rnd)); + fuzzData.remove_prefix(sizeof(rnd)); + icu::Locale locale = GetRandomLocale(rnd); + + // Byte 4 randomly select a Calendar type + const char* type = GetRandomCalendarType(*fuzzData.data()); + fuzzData.remove_prefix(1); + + UErrorCode status = U_ZERO_ERROR; + icu::LocaleBuilder bld; + bld.setLocale(locale); + bld.setUnicodeLocaleKeyword("ca", type); + locale = bld.build(status); + if (U_FAILURE(status)) return 0; + std::unique_ptr cal( + icu::Calendar::createInstance(*timeZone, locale, status)); + if (U_FAILURE(status)) return 0; + cal->clear(); + + int32_t amount; + double time; + while (fuzzData.length() > 2 + static_cast(sizeof(time))) { + UCalendarDateFields field = static_cast( + (*fuzzData.data()) % UCAL_FIELD_COUNT); + fuzzData.remove_prefix(1); + + uint8_t command = *fuzzData.data(); + fuzzData.remove_prefix(1); + + std::memcpy(&time, fuzzData.data(), sizeof(time)); + std::memcpy(&amount, fuzzData.data(), sizeof(amount)); + fuzzData.remove_prefix(sizeof(time)); + + status = U_ZERO_ERROR; + switch (command % 7) { + case 0: + printf("setTime(%f)\n", time); + cal->setTime(time, status); + break; + case 1: + printf("getTime()\n"); + cal->getTime(status); + break; + case 2: + printf("set(%d, %d)\n", field, amount); + cal->set(field, amount); + break; + case 3: + printf("add(%d, %d)\n", field, amount); + cal->add(field, amount, status); + break; + case 4: + printf("roll(%d, %d)\n", field, amount); + cal->roll(field, amount, status); + break; + case 5: + printf("fieldDifference(%f, %d)\n", time, field); + cal->fieldDifference(time, field, status); + break; + case 6: + printf("get(%d)\n", field); + cal->get(field, status); + break; + default: + break; + } + } + + return EXIT_SUCCESS; +}