ICU-20217 Adds additional ICU4C fuzzers.

This commit is contained in:
Norbert Runge 2019-01-25 12:58:18 -08:00 committed by gnrunge
parent ccba38d382
commit b4fef640cf
13 changed files with 184 additions and 0 deletions

View File

@ -0,0 +1,49 @@
// © 2019 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
// Fuzzer for ICU Locales.
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <string>
#include <vector>
#include "unicode/locid.h"
namespace {
void ConsumeNBytes(const uint8_t** data, size_t* size, size_t N) {
*data += N;
*size -= N;
}
uint8_t ConsumeUint8(const uint8_t** data, size_t* size) {
uint8_t tmp = 0;
if (*size >= 1) {
tmp = (*data)[0];
ConsumeNBytes(data, size, 1);
}
return tmp;
}
std::string ConsumeSubstring(const uint8_t** data, size_t* size) {
const size_t request_size = ConsumeUint8(data, size);
const char* substring_start = reinterpret_cast<const char*>(*data);
const size_t substring_size = std::min(*size, request_size);
ConsumeNBytes(data, size, substring_size);
return std::string(substring_start, substring_size);
}
} // namespace
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
const std::string language = ConsumeSubstring(&data, &size);
const std::string country = ConsumeSubstring(&data, &size);
const std::string variant = ConsumeSubstring(&data, &size);
const std::string kv_pairs = ConsumeSubstring(&data, &size);
icu::Locale locale(language.c_str(), country.c_str(), variant.c_str(),
kv_pairs.c_str());
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,12 @@
// © 2019 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
// Helper method for ICU locale fuzzer.
#include "locale_util.h"
#include <string>
std::string MakeZeroTerminatedInput(const uint8_t *data, int32_t size) {
return size == 0 ? "" : std::string(reinterpret_cast<const char *>(data), size);
}

View File

@ -0,0 +1,12 @@
// © 2019 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
#ifndef I18N_ICU_FUZZ_LOCALE_UTIL_H_
#define I18N_ICU_FUZZ_LOCALE_UTIL_H_
#include <string>
// Takes uint8_t data from fuzzer, and makes a zero terminated string.
std::string MakeZeroTerminatedInput(const uint8_t* data, int32_t size);
#endif // I18N_ICU_FUZZ_LOCALE_UTIL_H_

View File

@ -0,0 +1,20 @@
// © 2019 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
#include <string>
#include "locale_util.h"
#include "unicode/uloc.h"
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
// Canonicalized locale name.
char name[ULOC_FULLNAME_CAPACITY];
int32_t name_capacity = ULOC_FULLNAME_CAPACITY;
const std::string input = MakeZeroTerminatedInput(data, size);
UErrorCode status = U_ZERO_ERROR;
uloc_canonicalize(input.c_str(), name, name_capacity, &status);
return 0;
}

View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:cf5c0deef267ac7f6857d3c2b95644d74266dc3ec9de6461572469b4c1e15a07
size 746

View File

@ -0,0 +1,21 @@
// © 2019 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
#include <string>
#include "locale_util.h"
#include "unicode/uloc.h"
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
// Full locale id.
char locale_id[ULOC_FULLNAME_CAPACITY];
int32_t locale_id_capacity = ULOC_FULLNAME_CAPACITY;
const std::string input = MakeZeroTerminatedInput(data, size);
UErrorCode status = U_ZERO_ERROR;
uloc_forLanguageTag(input.c_str(), locale_id, locale_id_capacity, nullptr,
&status);
return 0;
}

View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:cf5c0deef267ac7f6857d3c2b95644d74266dc3ec9de6461572469b4c1e15a07
size 746

View File

@ -0,0 +1,20 @@
// © 2019 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
#include <string>
#include "locale_util.h"
#include "unicode/uloc.h"
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
// Full locale name.
char name[ULOC_FULLNAME_CAPACITY];
int32_t name_capacity = ULOC_FULLNAME_CAPACITY;
const std::string input = MakeZeroTerminatedInput(data, size);
UErrorCode status = U_ZERO_ERROR;
uloc_getName(input.c_str(), name, name_capacity, &status);
return 0;
}

View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:cf5c0deef267ac7f6857d3c2b95644d74266dc3ec9de6461572469b4c1e15a07
size 746

View File

@ -0,0 +1,15 @@
// © 2019 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
#include <string>
#include "locale_util.h"
#include "unicode/uloc.h"
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
const std::string input = MakeZeroTerminatedInput(data, size);
uloc_isRightToLeft(input.c_str());
return 0;
}

View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:cf5c0deef267ac7f6857d3c2b95644d74266dc3ec9de6461572469b4c1e15a07
size 746

View File

@ -0,0 +1,20 @@
// © 2019 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
#include <string>
#include "locale_util.h"
#include "unicode/uenum.h"
#include "unicode/uloc.h"
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
const std::string input = MakeZeroTerminatedInput(data, size);
UErrorCode status = U_ZERO_ERROR;
UEnumeration* enumeration = uloc_openKeywords(input.c_str(), &status);
// Have to clean up. Call works even for nullptr enumeration.
uenum_close(enumeration);
return 0;
}

View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:cf5c0deef267ac7f6857d3c2b95644d74266dc3ec9de6461572469b4c1e15a07
size 746