mirror of
https://github.com/zaerl/mojibake.git
synced 2026-11-30 16:48:26 +01:00
779 lines
29 KiB
C
779 lines
29 KiB
C
/**
|
|
* The Mojibake library
|
|
*
|
|
* This file is distributed under the MIT License. See LICENSE for details.
|
|
*/
|
|
|
|
#include <stdarg.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "../src/mojibake.h"
|
|
|
|
int LLVMFuzzerInitialize(int *argc, char ***argv);
|
|
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
|
|
|
|
// Keeps calls to pure functions from being discarded.
|
|
static volatile size_t fuzz_sink;
|
|
|
|
// An allocator that records each requested size in a header and aborts when the library passes a
|
|
// different size back to realloc or free, so the fuzzer reports every allocator contract violation.
|
|
typedef union fuzz_block_header {
|
|
size_t size;
|
|
long double long_double_alignment;
|
|
long long long_long_alignment;
|
|
void *pointer_alignment;
|
|
} fuzz_block_header;
|
|
|
|
static void *fuzz_alloc(void *context, size_t size) {
|
|
(void)context;
|
|
|
|
if(size == 0) {
|
|
abort();
|
|
}
|
|
|
|
fuzz_block_header *header = (fuzz_block_header *)malloc(sizeof(*header) + size);
|
|
|
|
if(header == NULL) {
|
|
return NULL;
|
|
}
|
|
|
|
header->size = size;
|
|
|
|
return header + 1;
|
|
}
|
|
|
|
static void *fuzz_realloc(void *context, void *ptr, size_t old_size, size_t new_size) {
|
|
(void)context;
|
|
fuzz_block_header *header = ptr == NULL ? NULL : (fuzz_block_header *)ptr - 1;
|
|
|
|
if(new_size == 0 || (header == NULL ? old_size != 0 : header->size != old_size)) {
|
|
abort();
|
|
}
|
|
|
|
fuzz_block_header *new_header = (fuzz_block_header *)realloc(header,
|
|
sizeof(*new_header) + new_size);
|
|
|
|
if(new_header == NULL) {
|
|
return NULL;
|
|
}
|
|
|
|
new_header->size = new_size;
|
|
|
|
return new_header + 1;
|
|
}
|
|
|
|
static void fuzz_free(void *context, void *ptr, size_t size) {
|
|
(void)context;
|
|
|
|
if(ptr == NULL || ((fuzz_block_header *)ptr - 1)->size != size) {
|
|
abort();
|
|
}
|
|
|
|
free((fuzz_block_header *)ptr - 1);
|
|
}
|
|
|
|
int LLVMFuzzerInitialize(int *argc, char ***argv) {
|
|
(void)argc;
|
|
(void)argv;
|
|
mjb_allocator allocator = { NULL, fuzz_alloc, fuzz_realloc, fuzz_free };
|
|
|
|
if(mjb_set_allocator(&allocator) != MJB_STATUS_OK) {
|
|
abort();
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static uint32_t fuzz_u32(const uint8_t *data, size_t size, uint8_t variant) {
|
|
uint32_t value = variant;
|
|
size_t limit = size < 4 ? size : 4;
|
|
|
|
for(size_t i = 0; i < limit; ++i) {
|
|
value |= (uint32_t)data[i] << (i * 8);
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
static int fuzz_utf8_vsnprintf(char *buffer, size_t buffer_size, const char *format, ...) {
|
|
va_list args;
|
|
va_start(args, format);
|
|
int required = mjb_utf8_vsnprintf(buffer, buffer_size, format, args);
|
|
va_end(args);
|
|
|
|
return required;
|
|
}
|
|
|
|
static int fuzz_utf8_grapheme_vsnprintf(char *buffer, size_t buffer_size, const char *format, ...) {
|
|
va_list args;
|
|
va_start(args, format);
|
|
int required = mjb_utf8_grapheme_vsnprintf(buffer, buffer_size, format, args);
|
|
va_end(args);
|
|
|
|
return required;
|
|
}
|
|
|
|
static mjb_codepoint fuzz_codepoint(const uint8_t *data, size_t size, uint8_t variant) {
|
|
uint32_t value = fuzz_u32(data, size, variant);
|
|
|
|
switch(variant & 0x7) {
|
|
case 0:
|
|
return value & 0x7F;
|
|
|
|
case 1:
|
|
return value % (MJB_CODEPOINT_MAX + 1u);
|
|
|
|
case 2:
|
|
return 0x0300u + (value % 0x400u);
|
|
|
|
case 3:
|
|
return MJB_CP_HANGUL_S_BASE + (value % MJB_CP_HANGUL_S_COUNT);
|
|
|
|
case 4:
|
|
if(variant & 0x20) {
|
|
return MJB_CP_HANGUL_T_BASE + (value % MJB_CP_HANGUL_T_COUNT);
|
|
}
|
|
|
|
if(variant & 0x10) {
|
|
return MJB_CP_HANGUL_V_BASE + (value % MJB_CP_HANGUL_V_COUNT);
|
|
}
|
|
|
|
return MJB_CP_HANGUL_L_BASE + (value % MJB_CP_HANGUL_L_COUNT);
|
|
|
|
case 5:
|
|
return MJB_CJK_IDEOGRAPH_START +
|
|
(value % (MJB_CJK_IDEOGRAPH_END - MJB_CJK_IDEOGRAPH_START + 1u));
|
|
|
|
case 6:
|
|
return 0x1F300u + (value % 0x500u);
|
|
|
|
default:
|
|
return MJB_CODEPOINT_NOT_VALID + (value & 0xFFu);
|
|
}
|
|
}
|
|
|
|
static bool fuzz_next_codepoint(const mjb_character *character, mjb_character_position type,
|
|
void *context) {
|
|
(void)context;
|
|
if(character != NULL) {
|
|
fuzz_sink += (size_t)character->codepoint;
|
|
fuzz_sink += (size_t)type;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static void fuzz_boundary_iterators(const char *buffer, size_t byte_length, mjb_encoding encoding) {
|
|
size_t guard_limit = (byte_length + 8) * 4;
|
|
|
|
mjb_grapheme_break_state grapheme_state = { 0 };
|
|
for(size_t guard = 0; guard < guard_limit; ++guard) {
|
|
mjb_text text = mjb_text_make(buffer, byte_length, encoding);
|
|
mjb_break_type bt = mjb_grapheme_break_next(&text, &grapheme_state);
|
|
fuzz_sink += (size_t)bt + grapheme_state.index;
|
|
|
|
if(bt == MJB_BT_NOT_SET) {
|
|
break;
|
|
}
|
|
|
|
if(guard + 1 == guard_limit) {
|
|
abort();
|
|
}
|
|
}
|
|
|
|
mjb_word_break_state word_state;
|
|
word_state.index = 0;
|
|
for(size_t guard = 0; guard < guard_limit; ++guard) {
|
|
mjb_text text = mjb_text_make(buffer, byte_length, encoding);
|
|
mjb_break_type bt = mjb_word_break_next(&text, &word_state);
|
|
fuzz_sink += (size_t)bt + word_state.index;
|
|
|
|
if(bt == MJB_BT_NOT_SET) {
|
|
break;
|
|
}
|
|
|
|
if(guard + 1 == guard_limit) {
|
|
abort();
|
|
}
|
|
}
|
|
|
|
mjb_line_break_state line_state = { 0 };
|
|
for(size_t guard = 0; guard < guard_limit; ++guard) {
|
|
mjb_text text = mjb_text_make(buffer, byte_length, encoding);
|
|
mjb_break_type bt = mjb_line_break_next(&text, &line_state);
|
|
fuzz_sink += (size_t)bt + line_state.index;
|
|
|
|
if(bt == MJB_BT_NOT_SET) {
|
|
break;
|
|
}
|
|
|
|
if(guard + 1 == guard_limit) {
|
|
abort();
|
|
}
|
|
}
|
|
|
|
mjb_sentence_break_state sentence_state = { 0 };
|
|
for(size_t guard = 0; guard < guard_limit; ++guard) {
|
|
mjb_text text = mjb_text_make(buffer, byte_length, encoding);
|
|
mjb_break_type bt = mjb_sentence_break_next(&text, &sentence_state);
|
|
fuzz_sink += (size_t)bt + sentence_state.index;
|
|
|
|
if(bt == MJB_BT_NOT_SET) {
|
|
break;
|
|
}
|
|
|
|
if(guard + 1 == guard_limit) {
|
|
abort();
|
|
}
|
|
}
|
|
}
|
|
|
|
static void fuzz_codepoint_apis(mjb_codepoint codepoint, uint8_t variant) {
|
|
mjb_character character;
|
|
mjb_numeric_value numeric;
|
|
mjb_block_info block;
|
|
mjb_emoji_properties emoji;
|
|
mjb_east_asian_width width;
|
|
mjb_script scripts[8];
|
|
size_t script_count = variant % 9;
|
|
mjb_codepoint decomposition[3];
|
|
char encoded[5];
|
|
char hangul_name[128];
|
|
bool binary_property_value = false;
|
|
int32_t integer_property_value = 0;
|
|
mjb_property property = (mjb_property)(variant % MJB_PR_COUNT);
|
|
mjb_property property_name = (mjb_property)(variant % (MJB_PR_COUNT + 2));
|
|
|
|
static const mjb_encoding encodings[] = { MJB_ENC_UTF_8, MJB_ENC_UTF_16LE, MJB_ENC_UTF_16BE,
|
|
MJB_ENC_UTF_32LE, MJB_ENC_UTF_32BE, MJB_ENC_ASCII };
|
|
|
|
fuzz_sink += (size_t)mjb_codepoint_info(codepoint, &character);
|
|
fuzz_sink += (size_t)mjb_codepoint_is_valid(codepoint);
|
|
fuzz_sink += (size_t)mjb_codepoint_is_graphic(codepoint);
|
|
fuzz_sink += (size_t)mjb_codepoint_is_combining(codepoint);
|
|
fuzz_sink += (size_t)mjb_codepoint_is_hangul_leading_jamo(codepoint);
|
|
fuzz_sink += (size_t)mjb_codepoint_is_hangul_vowel_jamo(codepoint);
|
|
fuzz_sink += (size_t)mjb_codepoint_is_hangul_trailing_jamo(codepoint);
|
|
fuzz_sink += (size_t)mjb_codepoint_is_hangul_jamo(codepoint);
|
|
fuzz_sink += (size_t)mjb_codepoint_is_hangul_syllable(codepoint);
|
|
fuzz_sink += (size_t)mjb_codepoint_is_cjk_ideograph(codepoint);
|
|
fuzz_sink += (size_t)mjb_codepoint_is_cjk_extension_ideograph(codepoint);
|
|
fuzz_sink += (size_t)mjb_codepoint_is_emoji(codepoint);
|
|
fuzz_sink += (size_t)mjb_codepoint_is_emoji_presentation(codepoint);
|
|
fuzz_sink += (size_t)mjb_codepoint_is_emoji_modifier(codepoint);
|
|
fuzz_sink += (size_t)mjb_codepoint_is_emoji_modifier_base(codepoint);
|
|
fuzz_sink += (size_t)mjb_codepoint_is_emoji_component(codepoint);
|
|
fuzz_sink += (size_t)mjb_codepoint_is_extended_pictographic(codepoint);
|
|
fuzz_sink += (size_t)mjb_category_is_graphic((mjb_category)(variant % MJB_CATEGORY_COUNT));
|
|
fuzz_sink += (size_t)mjb_category_is_combining((mjb_category)(variant % MJB_CATEGORY_COUNT));
|
|
fuzz_sink += (size_t)mjb_codepoint_numeric_value(codepoint, &numeric);
|
|
fuzz_sink += (size_t)mjb_codepoint_property_binary(codepoint, property, &binary_property_value);
|
|
fuzz_sink += (size_t)binary_property_value;
|
|
fuzz_sink += (size_t)mjb_codepoint_property_int(codepoint, property, &integer_property_value);
|
|
fuzz_sink += (size_t)integer_property_value;
|
|
fuzz_sink += (size_t)mjb_codepoint_block(codepoint, &block);
|
|
fuzz_sink += (size_t)mjb_codepoint_plane(codepoint);
|
|
fuzz_sink += (size_t)mjb_plane_is_valid((mjb_plane)((int)(variant % (MJB_PLANE_NUM + 2)) - 1));
|
|
|
|
const char *plane_name = mjb_plane_name((mjb_plane)((int)(variant % (MJB_PLANE_NUM + 2)) - 1),
|
|
(variant & 0x80) != 0);
|
|
if(plane_name != NULL) {
|
|
fuzz_sink += (unsigned char)plane_name[0];
|
|
}
|
|
|
|
fuzz_sink += (size_t)mjb_codepoint_is_id_start(codepoint);
|
|
fuzz_sink += (size_t)mjb_codepoint_is_id_continue(codepoint);
|
|
fuzz_sink += (size_t)mjb_codepoint_is_xid_start(codepoint);
|
|
fuzz_sink += (size_t)mjb_codepoint_is_xid_continue(codepoint);
|
|
fuzz_sink += (size_t)mjb_codepoint_is_pattern_syntax(codepoint);
|
|
fuzz_sink += (size_t)mjb_codepoint_is_pattern_white_space(codepoint);
|
|
fuzz_sink += (size_t)mjb_codepoint_script(codepoint);
|
|
fuzz_sink += (size_t)mjb_codepoint_script_extensions(codepoint, scripts, &script_count);
|
|
fuzz_sink += script_count;
|
|
|
|
const char *name = mjb_property_name(property_name);
|
|
if(name != NULL) {
|
|
fuzz_sink += (unsigned char)name[0];
|
|
}
|
|
|
|
if(mjb_codepoint_emoji_properties(codepoint, &emoji) == MJB_STATUS_OK) {
|
|
fuzz_sink += (size_t)emoji.emoji + (size_t)emoji.presentation + (size_t)emoji.modifier +
|
|
(size_t)emoji.modifier_base + (size_t)emoji.component +
|
|
(size_t)emoji.extended_pictographic;
|
|
}
|
|
|
|
if(mjb_codepoint_east_asian_width(codepoint, &width) == MJB_STATUS_OK) {
|
|
fuzz_sink += (size_t)width;
|
|
}
|
|
|
|
fuzz_sink += (size_t)mjb_hangul_syllable_name(codepoint, hangul_name, sizeof(hangul_name));
|
|
fuzz_sink += (size_t)mjb_hangul_syllable_decompose(codepoint, decomposition);
|
|
|
|
mjb_codepoint syllable = 0;
|
|
// The fuzzed codepoint is the trailing jamo, so invalid trailing values are exercised too.
|
|
fuzz_sink += (size_t)mjb_hangul_syllable_compose(MJB_CP_HANGUL_L_BASE +
|
|
(variant % MJB_CP_HANGUL_L_COUNT),
|
|
MJB_CP_HANGUL_V_BASE + (variant % MJB_CP_HANGUL_V_COUNT), codepoint, &syllable);
|
|
fuzz_sink += syllable;
|
|
fuzz_sink += mjb_codepoint_encode(codepoint, encoded, sizeof(encoded),
|
|
encodings[(variant >> 2) % 6]);
|
|
}
|
|
|
|
static void fuzz_emoji_string_api_input(const char *buffer, size_t byte_length,
|
|
mjb_encoding encoding) {
|
|
mjb_emoji_sequence emoji;
|
|
|
|
mjb_text text = mjb_text_make(buffer, byte_length, encoding);
|
|
if(mjb_emoji_sequence_info(&text, &emoji) == MJB_STATUS_OK) {
|
|
fuzz_sink += (size_t)emoji.type + (size_t)emoji.qualification + emoji.codepoint_count;
|
|
}
|
|
|
|
text = mjb_text_make(buffer, byte_length, encoding);
|
|
fuzz_sink += (size_t)mjb_is_emoji_sequence(&text);
|
|
text = mjb_text_make(buffer, byte_length, encoding);
|
|
fuzz_sink += (size_t)mjb_is_rgi_emoji(&text);
|
|
}
|
|
|
|
static void fuzz_emoji_string_apis(const char *buffer, size_t byte_length, mjb_encoding encoding) {
|
|
fuzz_emoji_string_api_input(buffer, byte_length, encoding);
|
|
|
|
if(byte_length > 0) {
|
|
fuzz_emoji_string_api_input(buffer, byte_length - 1, encoding);
|
|
}
|
|
}
|
|
|
|
static void fuzz_idna_apis(const char *buffer, size_t byte_length, mjb_encoding encoding,
|
|
mjb_encoding output_encoding, uint8_t variant, bool to_ascii) {
|
|
mjb_idna_info info = { 0 };
|
|
mjb_result result = { 0 };
|
|
mjb_text text = mjb_text_make(buffer, byte_length, encoding);
|
|
mjb_status status;
|
|
|
|
if(to_ascii) {
|
|
status = mjb_idna_to_ascii(&text, output_encoding, &info, &result);
|
|
} else {
|
|
status = mjb_idna_to_unicode(&text, output_encoding, &info, &result);
|
|
}
|
|
|
|
fuzz_sink += (size_t)status + info.errors;
|
|
|
|
if(status == MJB_STATUS_OK) {
|
|
mjb_result_free(&result);
|
|
}
|
|
|
|
mjb_buffer into = mjb_buffer_make(NULL, 0, output_encoding);
|
|
info.errors = 0;
|
|
|
|
if(to_ascii) {
|
|
status = mjb_idna_to_ascii_into(&text, &info, &into);
|
|
} else {
|
|
status = mjb_idna_to_unicode_into(&text, &info, &into);
|
|
}
|
|
|
|
fuzz_sink += (size_t)status + info.errors + into.size;
|
|
|
|
if(status != MJB_STATUS_OK || into.size > 4096) {
|
|
return;
|
|
}
|
|
|
|
char output[4096];
|
|
size_t required = into.size;
|
|
// Sometimes leave one byte short to exercise the too-small path
|
|
into = mjb_buffer_make(output, (variant & 0x80) != 0 && required > 0 ? required - 1 : required,
|
|
output_encoding);
|
|
info.errors = 0;
|
|
|
|
if(to_ascii) {
|
|
status = mjb_idna_to_ascii_into(&text, &info, &into);
|
|
} else {
|
|
status = mjb_idna_to_unicode_into(&text, &info, &into);
|
|
}
|
|
|
|
fuzz_sink += (size_t)status + info.errors + into.size;
|
|
}
|
|
|
|
static void fuzz_format_apis(const char *buffer, size_t byte_length, uint8_t variant) {
|
|
char output[64] = { 0 };
|
|
size_t output_size = variant % (sizeof(output) + 1);
|
|
char *destination = output_size == 0 && (variant & 0x80) != 0 ? NULL : output;
|
|
int precision = (int)(byte_length > 4096 ? 4096 : byte_length);
|
|
|
|
fuzz_sink += (size_t)mjb_utf8_snprintf(destination, output_size, "%.*s", precision, buffer);
|
|
fuzz_sink += (size_t)fuzz_utf8_vsnprintf(destination, output_size, "%.*s", precision, buffer);
|
|
fuzz_sink += (size_t)mjb_utf8_grapheme_snprintf(destination, output_size, "%.*s", precision,
|
|
buffer);
|
|
fuzz_sink += (size_t)fuzz_utf8_grapheme_vsnprintf(destination, output_size, "%.*s", precision,
|
|
buffer);
|
|
|
|
if(output_size > 0) {
|
|
fuzz_sink += (unsigned char)output[0];
|
|
}
|
|
}
|
|
|
|
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
|
if(size < 2) {
|
|
return 0;
|
|
}
|
|
|
|
uint8_t selector = data[0];
|
|
uint8_t variant = data[1];
|
|
const char *buffer = (const char *)data + 2;
|
|
size -= 2;
|
|
|
|
static const mjb_encoding encodings[] = { MJB_ENC_UTF_8, MJB_ENC_UTF_16LE, MJB_ENC_UTF_16BE,
|
|
MJB_ENC_UTF_32LE, MJB_ENC_UTF_32BE, MJB_ENC_ASCII };
|
|
mjb_encoding encoding = encodings[variant % 6];
|
|
mjb_malformed_policy malformed_policy = (mjb_malformed_policy)(variant % 3);
|
|
|
|
// Exercise the language-sensitive casing and folding paths too.
|
|
static const mjb_locale locales[] = { MJB_LOCALE_NOT_SET, MJB_LOCALE_EN, MJB_LOCALE_TR,
|
|
MJB_LOCALE_AZ, MJB_LOCALE_LT };
|
|
mjb_locale case_locale = locales[(variant >> 3) % 5];
|
|
|
|
const char *version = mjb_version();
|
|
const char *unicode_version = mjb_unicode_version();
|
|
fuzz_sink += (unsigned char)version[0];
|
|
fuzz_sink += (size_t)mjb_version_number();
|
|
fuzz_sink += (unsigned char)unicode_version[0];
|
|
|
|
mjb_result result = { 0 };
|
|
mjb_codepoint codepoint = fuzz_codepoint((const uint8_t *)buffer, size, variant);
|
|
|
|
switch(selector % 23) {
|
|
case 0: { // Normalization, all four forms
|
|
mjb_text text = mjb_text_make(buffer, size, encoding);
|
|
|
|
if(mjb_normalize(&text, malformed_policy, (mjb_normalization)(variant % 4),
|
|
MJB_ENC_UTF_8, &result, NULL) == MJB_STATUS_OK) {
|
|
mjb_result_free(&result);
|
|
}
|
|
|
|
mjb_buffer into = mjb_buffer_make(NULL, 0, MJB_ENC_UTF_8);
|
|
|
|
if(mjb_normalize_into(&text, malformed_policy, (mjb_normalization)(variant % 4), &into,
|
|
NULL) == MJB_STATUS_OK &&
|
|
into.size <= 4096) {
|
|
char output[4096];
|
|
into.data = output;
|
|
fuzz_sink += (size_t)mjb_normalize_into(&text, malformed_policy,
|
|
(mjb_normalization)(variant % 4), &into, NULL);
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 1: { // Normalization quick check
|
|
mjb_quick_check_result quick_check;
|
|
mjb_text text = mjb_text_make(buffer, size, encoding);
|
|
fuzz_sink += (size_t)mjb_normalization_quick_check(&text,
|
|
(mjb_normalization)(variant % 4), &quick_check);
|
|
break;
|
|
}
|
|
|
|
case 2: { // Case conversion and folding, all transforming types
|
|
mjb_text text = mjb_text_make(buffer, size, encoding);
|
|
if(mjb_map_case(&text, malformed_policy, (mjb_map_case_type)(1 + (variant % 5)),
|
|
case_locale, MJB_ENC_UTF_8, &result, NULL) == MJB_STATUS_OK) {
|
|
mjb_result_free(&result);
|
|
}
|
|
|
|
mjb_buffer into = mjb_buffer_make(NULL, 0, MJB_ENC_UTF_8);
|
|
|
|
if(mjb_map_case_into(&text, malformed_policy, (mjb_map_case_type)(1 + (variant % 5)),
|
|
case_locale, &into, NULL) == MJB_STATUS_OK &&
|
|
into.size <= 4096) {
|
|
char output[4096];
|
|
size_t required = into.size;
|
|
// Sometimes leave one byte short to exercise the too-small path
|
|
into = mjb_buffer_make(output,
|
|
(variant & 0x80) != 0 && required > 0 ? required - 1 : required, MJB_ENC_UTF_8);
|
|
fuzz_sink += (size_t)mjb_map_case_into(&text, malformed_policy,
|
|
(mjb_map_case_type)(1 + (variant % 5)), case_locale, &into, NULL);
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 3: { // Bidirectional algorithm, all three directions
|
|
mjb_bidi_paragraph para;
|
|
mjb_direction direction = (mjb_direction)(variant % 3);
|
|
|
|
mjb_text text = mjb_text_make(buffer, size, encoding);
|
|
if(mjb_bidi_resolve(&text, direction, ¶) == MJB_STATUS_OK) {
|
|
if(para.count > 0 && para.count <= 4096) {
|
|
size_t visual_order[4096];
|
|
|
|
if(mjb_bidi_reorder_line(¶, 0, para.count, visual_order) == MJB_STATUS_OK) {
|
|
size_t run_count = 0;
|
|
mjb_status runs_status = mjb_bidi_line_runs(¶, visual_order, para.count,
|
|
NULL, &run_count);
|
|
fuzz_sink += (size_t)runs_status;
|
|
}
|
|
}
|
|
|
|
mjb_bidi_paragraph_free(¶);
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 4: // Encoding detection
|
|
fuzz_sink += (size_t)mjb_detect_encoding(buffer, size);
|
|
fuzz_sink += (size_t)mjb_is_utf8(buffer, size);
|
|
fuzz_sink += (size_t)mjb_is_utf16(buffer, size);
|
|
fuzz_sink += (size_t)mjb_is_ascii(buffer, size);
|
|
break;
|
|
|
|
case 5: { // Encoding conversion
|
|
mjb_encoding output_encoding = encodings[(variant >> 1) % 6];
|
|
|
|
mjb_malformed_policy policy = (mjb_malformed_policy)(variant % 3);
|
|
|
|
mjb_text text = mjb_text_make(buffer, size, encoding);
|
|
if(mjb_convert_encoding(&text, policy, output_encoding, &result, NULL) ==
|
|
MJB_STATUS_OK) {
|
|
mjb_result_free(&result);
|
|
}
|
|
|
|
mjb_buffer into = mjb_buffer_make(NULL, 0, output_encoding);
|
|
|
|
if(mjb_convert_encoding_into(&text, policy, &into, NULL) == MJB_STATUS_OK &&
|
|
into.size <= 4096) {
|
|
char output[4096];
|
|
size_t required = into.size;
|
|
// Sometimes leave one byte short to exercise the too-small path
|
|
into = mjb_buffer_make(output,
|
|
(variant & 0x80) != 0 && required > 0 ? required - 1 : required,
|
|
output_encoding);
|
|
fuzz_sink += (size_t)mjb_convert_encoding_into(&text, policy, &into, NULL);
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 6: { // String filtering, all filter combinations
|
|
mjb_malformed_policy policy = (mjb_malformed_policy)(variant % 3);
|
|
|
|
mjb_text text = mjb_text_make(buffer, size, encoding);
|
|
if(mjb_filter(&text, policy, (mjb_filter_flags)(variant & 0x1F), MJB_ENC_UTF_8, &result,
|
|
NULL) == MJB_STATUS_OK) {
|
|
mjb_result_free(&result);
|
|
}
|
|
|
|
mjb_buffer into = mjb_buffer_make(NULL, 0, MJB_ENC_UTF_8);
|
|
|
|
if(mjb_filter_into(&text, policy, (mjb_filter_flags)(variant & 0x1F), &into, NULL) ==
|
|
MJB_STATUS_OK &&
|
|
into.size <= 4096) {
|
|
char output[4096];
|
|
size_t required = into.size;
|
|
// Sometimes leave one byte short to exercise the too-small path
|
|
into = mjb_buffer_make(output,
|
|
(variant & 0x80) != 0 && required > 0 ? required - 1 : required, MJB_ENC_UTF_8);
|
|
fuzz_sink += (size_t)mjb_filter_into(&text, policy,
|
|
(mjb_filter_flags)(variant & 0x1F), &into, NULL);
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 7: { // Collation key
|
|
mjb_collation_strength strength = (mjb_collation_strength)((variant >> 5) % 4);
|
|
|
|
mjb_text text = mjb_text_make(buffer, size, encoding);
|
|
if(mjb_collation_key(&text, malformed_policy,
|
|
(variant & 0x10) ? MJB_COLLATION_SHIFTED : MJB_COLLATION_NON_IGNORABLE, strength,
|
|
&result, NULL) == MJB_STATUS_OK) {
|
|
mjb_result_free(&result);
|
|
}
|
|
|
|
mjb_collation_variable_weighting variable_weighting = (variant & 0x10) ?
|
|
MJB_COLLATION_SHIFTED :
|
|
MJB_COLLATION_NON_IGNORABLE;
|
|
mjb_buffer into = mjb_buffer_make(NULL, 0, MJB_ENC_UNKNOWN);
|
|
|
|
if(mjb_collation_key_into(&text, malformed_policy, variable_weighting, strength, &into,
|
|
NULL) == MJB_STATUS_OK &&
|
|
into.size <= 4096) {
|
|
char output[4096];
|
|
into.data = output;
|
|
fuzz_sink += (size_t)mjb_collation_key_into(&text, malformed_policy,
|
|
variable_weighting, strength, &into, NULL);
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 8: { // Collation comparison, input split in two halves
|
|
int order;
|
|
mjb_text left = mjb_text_make(buffer, size / 2, encoding);
|
|
mjb_text right = mjb_text_make(buffer + size / 2, size - size / 2, encoding);
|
|
mjb_diagnostic first_diagnostic;
|
|
mjb_diagnostic second_diagnostic;
|
|
fuzz_sink += (size_t)mjb_collation_compare(&left, &right, malformed_policy,
|
|
(variant & 0x10) ? MJB_COLLATION_SHIFTED : MJB_COLLATION_NON_IGNORABLE,
|
|
(mjb_collation_strength)((variant >> 5) % 4), &order, &first_diagnostic,
|
|
&second_diagnostic);
|
|
fuzz_sink += first_diagnostic.byte_offset + second_diagnostic.byte_offset;
|
|
break;
|
|
}
|
|
|
|
case 9: { // Segmentation: grapheme, word and width truncation, segment counts
|
|
size_t segment_count = 0;
|
|
mjb_text text = mjb_text_make(buffer, size, encoding);
|
|
fuzz_sink += (size_t)mjb_codepoint_count(&text, MJB_MALFORMED_REPLACE, &segment_count,
|
|
NULL);
|
|
fuzz_sink += segment_count;
|
|
text = mjb_text_make(buffer, size, encoding);
|
|
mjb_grapheme_prefix_length(&text, variant);
|
|
text = mjb_text_make(buffer, size, encoding);
|
|
mjb_word_prefix_length(&text, variant);
|
|
text = mjb_text_make(buffer, size, encoding);
|
|
mjb_grapheme_prefix_length_for_width(&text, (mjb_terminal_width_profile)(variant % 2),
|
|
variant);
|
|
text = mjb_text_make(buffer, size, encoding);
|
|
mjb_word_prefix_length_for_width(&text, (mjb_terminal_width_profile)(variant % 2),
|
|
variant);
|
|
|
|
text = mjb_text_make(buffer, size, encoding);
|
|
fuzz_sink += (size_t)mjb_grapheme_count(&text, malformed_policy, &segment_count, NULL);
|
|
fuzz_sink += segment_count;
|
|
text = mjb_text_make(buffer, size, encoding);
|
|
fuzz_sink += (size_t)mjb_sentence_count(&text, malformed_policy, &segment_count, NULL);
|
|
fuzz_sink += segment_count;
|
|
text = mjb_text_make(buffer, size, encoding);
|
|
fuzz_sink += (size_t)mjb_word_count(&text, malformed_policy, &segment_count, NULL);
|
|
fuzz_sink += segment_count;
|
|
break;
|
|
}
|
|
|
|
case 10: { // Terminal width
|
|
size_t width = 0;
|
|
mjb_text text = mjb_text_make(buffer, size, encoding);
|
|
mjb_status status = mjb_terminal_width(&text, malformed_policy,
|
|
(mjb_terminal_width_profile)(variant % 2), &width, NULL);
|
|
fuzz_sink += (size_t)status;
|
|
if(status == MJB_STATUS_OK) {
|
|
fuzz_sink += width;
|
|
}
|
|
break;
|
|
}
|
|
|
|
case 11: { // Identifier validation and resolved scripts
|
|
mjb_text text = mjb_text_make(buffer, size, encoding);
|
|
mjb_is_identifier(&text,
|
|
(variant & 0x10) ? MJB_IDENTIFIER_NFKC : MJB_IDENTIFIER_DEFAULT);
|
|
|
|
mjb_script scripts[8];
|
|
size_t script_count = variant % 9;
|
|
mjb_script_set_kind kind;
|
|
text = mjb_text_make(buffer, size, encoding);
|
|
fuzz_sink += (size_t)mjb_resolved_script_set(&text, scripts, &script_count, &kind);
|
|
fuzz_sink += script_count + (size_t)kind;
|
|
break;
|
|
}
|
|
|
|
case 12: { // Confusable skeleton and pairwise detection
|
|
mjb_text text = mjb_text_make(buffer, size, encoding);
|
|
if(mjb_confusable_skeleton(&text, MJB_ENC_UTF_8, &result) == MJB_STATUS_OK) {
|
|
mjb_result_free(&result);
|
|
}
|
|
|
|
mjb_buffer into = mjb_buffer_make(NULL, 0, MJB_ENC_UTF_8);
|
|
|
|
if(mjb_confusable_skeleton_into(&text, &into) == MJB_STATUS_OK && into.size <= 4096) {
|
|
char output[4096];
|
|
into.data = output;
|
|
fuzz_sink += (size_t)mjb_confusable_skeleton_into(&text, &into);
|
|
}
|
|
|
|
bool confusable;
|
|
mjb_text left = mjb_text_make(buffer, size / 2, encoding);
|
|
mjb_text right = mjb_text_make(buffer + size / 2, size - size / 2, encoding);
|
|
mjb_diagnostic first_diagnostic;
|
|
mjb_diagnostic second_diagnostic;
|
|
fuzz_sink += (size_t)mjb_confusable_match(&left, &right, malformed_policy, &confusable,
|
|
&first_diagnostic, &second_diagnostic);
|
|
fuzz_sink += first_diagnostic.byte_offset + second_diagnostic.byte_offset;
|
|
break;
|
|
}
|
|
|
|
case 13: { // BCP 47 locale parsing
|
|
mjb_locale_id locale;
|
|
mjb_text text = mjb_text_make(buffer, size, encoding);
|
|
mjb_status locale_status = mjb_locale_parse(&text, &locale);
|
|
fuzz_sink += (size_t)locale_status;
|
|
break;
|
|
}
|
|
|
|
case 14: // Codepoint metadata, properties, Hangul, emoji and encoding helpers
|
|
fuzz_codepoint_apis(codepoint, variant);
|
|
break;
|
|
|
|
case 15: // Emoji sequence string APIs
|
|
fuzz_emoji_string_apis(buffer, size, encoding);
|
|
break;
|
|
|
|
case 16: { // Raw boundary iterators and character callback API
|
|
fuzz_boundary_iterators(buffer, size, encoding);
|
|
mjb_text text = mjb_text_make(buffer, size, encoding);
|
|
fuzz_sink += (size_t)mjb_for_each_codepoint(&text, MJB_MALFORMED_REPLACE,
|
|
fuzz_next_codepoint, NULL, NULL);
|
|
break;
|
|
}
|
|
|
|
case 17: { // Identifier-oriented NFKC case folding
|
|
mjb_text text = mjb_text_make(buffer, size, encoding);
|
|
if(mjb_nfkc_casefold(&text, malformed_policy, MJB_ENC_UTF_8, &result, NULL) ==
|
|
MJB_STATUS_OK) {
|
|
mjb_result_free(&result);
|
|
}
|
|
|
|
mjb_buffer into = mjb_buffer_make(NULL, 0, MJB_ENC_UTF_8);
|
|
|
|
if(mjb_nfkc_casefold_into(&text, malformed_policy, &into, NULL) == MJB_STATUS_OK &&
|
|
into.size <= 4096) {
|
|
char output[4096];
|
|
into.data = output;
|
|
fuzz_sink += (size_t)mjb_nfkc_casefold_into(&text, malformed_policy, &into, NULL);
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case 18: // IDNA ToASCII, allocating and caller-provided output
|
|
fuzz_idna_apis(buffer, size, encoding, encodings[(variant >> 1) % 6], variant, true);
|
|
break;
|
|
|
|
case 19: // IDNA ToUnicode, allocating and caller-provided output
|
|
fuzz_idna_apis(buffer, size, encoding, encodings[(variant >> 1) % 6], variant, false);
|
|
break;
|
|
|
|
case 20: { // Unicode caseless matching, input split in two halves
|
|
bool matches = false;
|
|
mjb_text left = mjb_text_make(buffer, size / 2, encoding);
|
|
mjb_text right = mjb_text_make(buffer + size / 2, size - size / 2, encoding);
|
|
mjb_diagnostic first_diagnostic;
|
|
mjb_diagnostic second_diagnostic;
|
|
fuzz_sink += (size_t)mjb_caseless_match(&left, &right, malformed_policy,
|
|
(mjb_caseless_mode)(variant % 4), &matches, &first_diagnostic, &second_diagnostic);
|
|
fuzz_sink += first_diagnostic.byte_offset + second_diagnostic.byte_offset;
|
|
fuzz_sink += (size_t)matches;
|
|
break;
|
|
}
|
|
|
|
case 21: // UTF-8 codepoint- and grapheme-safe formatting
|
|
fuzz_format_apis(buffer, size, variant);
|
|
break;
|
|
|
|
case 22:
|
|
(void)mjb_status_message((mjb_status)(variant % 256));
|
|
break;
|
|
}
|
|
|
|
return 0;
|
|
}
|