1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
|
#include <cstring>
#include <fuzzer/FuzzedDataProvider.h>
#include <memory>
#include <string>
#include <iostream>
#include "simdutf.h"
// useful for debugging
static void print_input(const std::string& s,
const simdutf::implementation* const e) {
printf("We are about to abort on the following input: ");
for (auto c : s) {
printf("%02x ", (unsigned char)c);
}
printf("\n");
std::cout << "string length : " << s.size() << " bytes" << std::endl;
std::cout << "implementation->name() = " << e->name() << std::endl;
}
/**
* We do round trips from UTF-8 to UTF-16, from UTF-8 to UTF-32, from UTF-16 to
* UTF-8.
* We do round trips from Latin 1 to UTF-8, from Latin 1 to UTF-16, from Latin 1
* to UTF-32. We test all available kernels. We also try to transcode invalid
* inputs.
*/
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
FuzzedDataProvider fdp(data, size);
constexpr int kMaxStringSize = 1024;
std::string source = fdp.ConsumeRandomLengthString(kMaxStringSize);
for (auto& e : simdutf::get_available_implementations()) {
if (!e->supported_by_runtime_system()) {
continue;
}
/**
* Transcoding from UTF-8 to UTF-16LE.
*/
bool validutf8 = e->validate_utf8(source.c_str(), source.size());
auto rutf8 = e->validate_utf8_with_errors(source.c_str(), source.size());
if (validutf8 != (rutf8.error == simdutf::SUCCESS)) { // they should agree
print_input(source, e);
abort();
}
if (validutf8) {
// We need a buffer of size where to write the UTF-16LE words.
size_t expected_utf16words =
e->utf16_length_from_utf8(source.c_str(), source.size());
std::unique_ptr<char16_t[]> utf16_output{
new char16_t[expected_utf16words]};
// convert to UTF-16LE
size_t utf16words = e->convert_utf8_to_utf16le(
source.c_str(), source.size(), utf16_output.get());
// It wrote utf16words * sizeof(char16_t) bytes.
bool validutf16 = e->validate_utf16le(utf16_output.get(), utf16words);
if (!validutf16) {
print_input(source, e);
abort();
}
// convert it back:
// We need a buffer of size where to write the UTF-8 words.
size_t expected_utf8words =
e->utf8_length_from_utf16le(utf16_output.get(), utf16words);
std::unique_ptr<char[]> utf8_output{new char[expected_utf8words]};
// convert to UTF-8
size_t utf8words = e->convert_utf16le_to_utf8(
utf16_output.get(), utf16words, utf8_output.get());
std::string final_string(utf8_output.get(), utf8words);
if (final_string != source) {
print_input(source, e);
abort();
}
} else {
// invalid input!!!
// We need a buffer of size where to write the UTF-16LE words.
size_t expected_utf16words =
e->utf16_length_from_utf8(source.c_str(), source.size());
std::unique_ptr<char16_t[]> utf16_output{
new char16_t[expected_utf16words]};
// convert to UTF-16LE
size_t utf16words = e->convert_utf8_to_utf16le(
source.c_str(), source.size(), utf16_output.get());
if (utf16words != 0) {
print_input(source, e);
abort();
}
}
/**
* Transcoding from UTF-8 to UTF-16BE.
*/
if (validutf8) {
// We need a buffer of size where to write the UTF-16BE words.
size_t expected_utf16words =
e->utf16_length_from_utf8(source.c_str(), source.size());
std::unique_ptr<char16_t[]> utf16_output{
new char16_t[expected_utf16words]};
// convert to UTF-16BE
size_t utf16words = e->convert_utf8_to_utf16be(
source.c_str(), source.size(), utf16_output.get());
// It wrote utf16words * sizeof(char16_t) bytes.
bool validutf16 = e->validate_utf16be(utf16_output.get(), utf16words);
if (!validutf16) {
print_input(source, e);
abort();
}
// convert it back:
// We need a buffer of size where to write the UTF-8 words.
size_t expected_utf8words =
e->utf8_length_from_utf16be(utf16_output.get(), utf16words);
std::unique_ptr<char[]> utf8_output{new char[expected_utf8words]};
// convert to UTF-8
size_t utf8words = e->convert_utf16be_to_utf8(
utf16_output.get(), utf16words, utf8_output.get());
std::string final_string(utf8_output.get(), utf8words);
if (final_string != source) {
print_input(source, e);
abort();
}
} else {
// invalid input!!!
// We need a buffer of size where to write the UTF-16BE words.
size_t expected_utf16words =
e->utf16_length_from_utf8(source.c_str(), source.size());
std::unique_ptr<char16_t[]> utf16_output{
new char16_t[expected_utf16words]};
// convert to UTF-16BE
size_t utf16words = e->convert_utf8_to_utf16be(
source.c_str(), source.size(), utf16_output.get());
if (utf16words != 0) {
print_input(source, e);
abort();
}
}
/**
* Transcoding from UTF-8 to UTF-32.
*/
if (validutf8) {
// We need a buffer of size where to write the UTF-32 words.
size_t expected_utf32words =
e->utf32_length_from_utf8(source.c_str(), source.size());
std::unique_ptr<char32_t[]> utf32_output{
new char32_t[expected_utf32words]};
// convert to UTF-32
size_t utf32words = e->convert_utf8_to_utf32(
source.c_str(), source.size(), utf32_output.get());
// It wrote utf32words * sizeof(char32_t) bytes.
bool validutf32 = e->validate_utf32(utf32_output.get(), utf32words);
if (!validutf32) {
return -1;
}
// convert it back:
// We need a buffer of size where to write the UTF-8 words.
size_t expected_utf8words =
e->utf8_length_from_utf32(utf32_output.get(), utf32words);
std::unique_ptr<char[]> utf8_output{new char[expected_utf8words]};
// convert to UTF-8
size_t utf8words = e->convert_utf32_to_utf8(
utf32_output.get(), utf32words, utf8_output.get());
std::string final_string(utf8_output.get(), utf8words);
if (source != final_string) {
print_input(source, e);
abort();
}
} else {
// invalid input!!!
size_t expected_utf32words =
e->utf32_length_from_utf8(source.c_str(), source.size());
std::unique_ptr<char32_t[]> utf32_output{
new char32_t[expected_utf32words]};
// convert to UTF-32
size_t utf32words = e->convert_utf8_to_utf32(
source.c_str(), source.size(), utf32_output.get());
if (utf32words != 0) {
print_input(source, e);
abort();
}
}
/**
* Transcoding from UTF-8 to Latin 1
*/
if (validutf8) {
// We need a buffer of size where to write the UTF-16LE words.
size_t expected_latin1words =
e->latin1_length_from_utf8(source.c_str(), source.size());
std::unique_ptr<char[]> latin1_output{new char[expected_latin1words]};
// convert to latin1
size_t latin1words = e->convert_utf8_to_latin1(
source.c_str(), source.size(), latin1_output.get());
if (latin1words != 0) {
// convert it back:
// We need a buffer of size where to write the UTF-8 words.
size_t expected_utf8words =
e->utf8_length_from_latin1(latin1_output.get(), latin1words);
std::unique_ptr<char[]> utf8_output{new char[expected_utf8words]};
// convert to UTF-8
size_t utf8words = e->convert_latin1_to_utf8(
latin1_output.get(), latin1words, utf8_output.get());
std::string final_string(utf8_output.get(), utf8words);
if (final_string != source) {
print_input(source, e);
abort();
}
}
} else {
// invalid input!!!
// We need a buffer of size where to write the Latin 1 words.
size_t expected_latin1words =
e->latin1_length_from_utf8(source.c_str(), source.size());
std::unique_ptr<char[]> latin1_output{new char[expected_latin1words]};
// convert to Latin 1
size_t latin1words = e->convert_utf8_to_latin1(
source.c_str(), source.size(), latin1_output.get());
if (latin1words != 0) {
print_input(source, e);
abort();
}
}
/**
* Transcoding from UTF-16LE to UTF-8.
*/
{
// Get new source data here as this will allow the fuzzer to optimize it's
// input for UTF16-LE.
source = fdp.ConsumeRandomLengthString(kMaxStringSize);
// We copy to avoid alignment issues.
std::unique_ptr<char16_t[]> utf16_source{new char16_t[source.size() / 2]};
if (source.data() != nullptr) {
std::memcpy(utf16_source.get(), source.data(), source.size() / 2 * 2);
}
bool validutf16le =
e->validate_utf16le(utf16_source.get(), source.size() / 2);
auto rutf16le = e->validate_utf16le_with_errors(utf16_source.get(),
source.size() / 2);
if (validutf16le !=
(rutf16le.error == simdutf::SUCCESS)) { // they should agree
print_input(source, e);
abort();
}
if (validutf16le) {
// We need a buffer of size where to write the UTF-16 words.
size_t expected_utf8words =
e->utf8_length_from_utf16le(utf16_source.get(), source.size() / 2);
std::unique_ptr<char[]> utf8_output{new char[expected_utf8words]};
size_t utf8words = e->convert_utf16le_to_utf8(
utf16_source.get(), source.size() / 2, utf8_output.get());
// It wrote utf16words * sizeof(char16_t) bytes.
bool validutf8 = e->validate_utf8(utf8_output.get(), utf8words);
if (!validutf8) {
print_input(source, e);
abort();
}
// convert it back:
// We need a buffer of size where to write the UTF-16 words.
size_t expected_utf16words =
e->utf16_length_from_utf8(utf8_output.get(), utf8words);
std::unique_ptr<char16_t[]> utf16_output{
new char16_t[expected_utf16words]};
// convert to UTF-8
size_t utf16words = e->convert_utf8_to_utf16le(
utf8_output.get(), utf8words, utf16_output.get());
for (size_t i = 0; i < source.size() / 2; i++) {
if (utf16_output.get()[i] != (utf16_source.get())[i]) {
print_input(source, e);
abort();
}
}
} else {
// invalid input!!!
// We need a buffer of size where to write the UTF-16 words.
size_t expected_utf8words =
e->utf8_length_from_utf16le(utf16_source.get(), source.size() / 2);
std::unique_ptr<char[]> utf8_output{new char[expected_utf8words]};
size_t utf8words = e->convert_utf16le_to_utf8(
utf16_source.get(), source.size() / 2, utf8_output.get());
if (utf8words != 0) {
print_input(source, e);
abort();
}
}
}
/**
* Transcoding from UTF-16BE to UTF-8.
*/
{
// Get new source data here as this will allow the fuzzer to optimize it's
// input for UTF16-BE.
source = fdp.ConsumeRandomLengthString(kMaxStringSize);
std::unique_ptr<char16_t[]> utf16_source{new char16_t[source.size() / 2]};
if (source.data() != nullptr) {
std::memcpy(utf16_source.get(), source.data(), source.size() / 2 * 2);
}
bool validutf16be =
e->validate_utf16be(utf16_source.get(), source.size() / 2);
auto rutf16be = e->validate_utf16be_with_errors(utf16_source.get(),
source.size() / 2);
if (validutf16be !=
(rutf16be.error == simdutf::SUCCESS)) { // they should agree
print_input(source, e);
abort();
}
if (validutf16be) {
// We need a buffer of size where to write the UTF-16 words.
size_t expected_utf8words =
e->utf8_length_from_utf16be(utf16_source.get(), source.size() / 2);
std::unique_ptr<char[]> utf8_output{new char[expected_utf8words]};
size_t utf8words = e->convert_utf16be_to_utf8(
utf16_source.get(), source.size() / 2, utf8_output.get());
// It wrote utf16words * sizeof(char16_t) bytes.
bool validutf8 = e->validate_utf8(utf8_output.get(), utf8words);
if (!validutf8) {
print_input(source, e);
abort();
}
// convert it back:
// We need a buffer of size where to write the UTF-16 words.
size_t expected_utf16words =
e->utf16_length_from_utf8(utf8_output.get(), utf8words);
std::unique_ptr<char16_t[]> utf16_output{
new char16_t[expected_utf16words]};
// convert to UTF-8
size_t utf16words = e->convert_utf8_to_utf16be(
utf8_output.get(), utf8words, utf16_output.get());
for (size_t i = 0; i < source.size() / 2; i++) {
if (utf16_output.get()[i] != (utf16_source.get())[i]) {
print_input(source, e);
abort();
}
}
} else {
// invalid input!!!
// We need a buffer of size where to write the UTF-16 words.
size_t expected_utf8words =
e->utf8_length_from_utf16be(utf16_source.get(), source.size() / 2);
std::unique_ptr<char[]> utf8_output{new char[expected_utf8words]};
size_t utf8words = e->convert_utf16be_to_utf8(
utf16_source.get(), source.size() / 2, utf8_output.get());
if (utf8words != 0) {
print_input(source, e);
abort();
}
}
}
/**
* Transcoding from latin1 to UTF-8.
*/
// Get new source data here as this will allow the fuzzer to optimize it's
// input for latin1.
source = fdp.ConsumeRandomLengthString(kMaxStringSize);
bool validlatin1 = true; // has to be
if (validlatin1) {
// We need a buffer of size where to write the UTF-8 words.
size_t expected_utf8words =
e->utf8_length_from_latin1(source.c_str(), source.size());
std::unique_ptr<char[]> utf8_output{new char[expected_utf8words]};
size_t utf8words = e->convert_latin1_to_utf8(
source.c_str(), source.size(), utf8_output.get());
// It wrote utf8words * sizeof(char) bytes.
bool validutf8 = e->validate_utf8(utf8_output.get(), utf8words);
if (!validutf8) {
print_input(source, e);
abort();
}
// convert it back:
// We need a buffer of size where to write the latin1 words.
size_t expected_latin1words =
e->latin1_length_from_utf8(utf8_output.get(), utf8words);
std::unique_ptr<char[]> latin1_output{new char[expected_latin1words]};
// convert to latin1
size_t latin1words = e->convert_utf8_to_latin1(
utf8_output.get(), utf8words, latin1_output.get());
for (size_t i = 0; i < source.size(); i++) {
if (latin1_output.get()[i] != (source.c_str())[i]) {
print_input(source, e);
abort();
}
}
}
if (validlatin1) {
// We need a buffer of size where to write the UTF-16 words.
size_t expected_utf16words = e->utf16_length_from_latin1(source.size());
std::unique_ptr<char16_t[]> utf16_output{
new char16_t[expected_utf16words]};
size_t utf16words = e->convert_latin1_to_utf16le(
source.c_str(), source.size(), utf16_output.get());
// It wrote utf16words * sizeof(char16_t) bytes.
bool validutf16 = e->validate_utf16le(utf16_output.get(), utf16words);
if (!validutf16) {
print_input(source, e);
abort();
}
// convert it back:
// We need a buffer of size where to write the latin1 words.
size_t expected_latin1words = e->latin1_length_from_utf16(utf16words);
std::unique_ptr<char[]> latin1_output{new char[expected_latin1words]};
// convert to latin1
size_t latin1words = e->convert_utf16le_to_latin1(
utf16_output.get(), utf16words, latin1_output.get());
for (size_t i = 0; i < source.size(); i++) {
if (latin1_output.get()[i] != (source.c_str())[i]) {
print_input(source, e);
abort();
}
}
}
if (validlatin1) {
// We need a buffer of size where to write the UTF-16 words.
size_t expected_utf16words = e->utf16_length_from_latin1(source.size());
std::unique_ptr<char16_t[]> utf16_output{
new char16_t[expected_utf16words]};
size_t utf16words = e->convert_latin1_to_utf16be(
source.c_str(), source.size(), utf16_output.get());
// It wrote utf16words * sizeof(char16_t) bytes.
bool validutf16 = e->validate_utf16be(utf16_output.get(), utf16words);
if (!validutf16) {
print_input(source, e);
abort();
}
// convert it back:
// We need a buffer of size where to write the latin1 words.
size_t expected_latin1words = e->latin1_length_from_utf16(utf16words);
std::unique_ptr<char[]> latin1_output{new char[expected_latin1words]};
// convert to latin1
size_t latin1words = e->convert_utf16be_to_latin1(
utf16_output.get(), utf16words, latin1_output.get());
for (size_t i = 0; i < source.size(); i++) {
if (latin1_output.get()[i] != (source.c_str())[i]) {
print_input(source, e);
abort();
}
}
}
if (validlatin1) {
// We need a buffer of size where to write the UTF-16 words.
size_t expected_utf32words = e->utf32_length_from_latin1(source.size());
std::unique_ptr<char32_t[]> utf32_output{
new char32_t[expected_utf32words]};
size_t utf32words = e->convert_latin1_to_utf32(
source.c_str(), source.size(), utf32_output.get());
// It wrote utf16words * sizeof(char16_t) bytes.
bool validutf32 = e->validate_utf32(utf32_output.get(), utf32words);
if (!validutf32) {
print_input(source, e);
abort();
}
// convert it back:
// We need a buffer of size where to write the latin1 words.
size_t expected_latin1words = e->latin1_length_from_utf32(utf32words);
std::unique_ptr<char[]> latin1_output{new char[expected_latin1words]};
// convert to latin1
size_t latin1words = e->convert_utf32_to_latin1(
utf32_output.get(), utf32words, latin1_output.get());
for (size_t i = 0; i < source.size(); i++) {
if (latin1_output.get()[i] != (source.c_str())[i]) {
print_input(source, e);
abort();
}
}
}
/// Base64 tests. We begin by trying to decode the input, even if we
/// expect it to fail.
{
size_t max_length_needed =
e->maximal_binary_length_from_base64(source.data(), source.size());
std::vector<char> back(max_length_needed);
simdutf::result r =
e->base64_to_binary(source.data(), source.size(), back.data());
if (r.error == simdutf::error_code::SUCCESS) {
// We expect failure but if we succeed, then we should have a roundtrip.
back.resize(r.count);
std::vector<char> back2(e->base64_length_from_binary(back.size()));
size_t base64size =
e->binary_to_base64(back.data(), back.size(), back2.data());
back2.resize(base64size);
std::vector<char> back3(
e->maximal_binary_length_from_base64(back2.data(), back2.size()));
simdutf::result r2 =
e->base64_to_binary(back2.data(), back2.size(), back3.data());
if (r2.error != simdutf::error_code::SUCCESS) {
print_input(source, e);
return false;
}
if (r2.count != back.size()) {
print_input(source, e);
return false;
}
if (back3.size() != back.size()) {
print_input(source, e);
return false;
}
}
}
// Same as above, but we use the safe decoder version.
{
size_t max_length_needed =
e->maximal_binary_length_from_base64(source.data(), source.size());
std::vector<char> back(max_length_needed);
simdutf::result r = simdutf::base64_to_binary_safe(
source.data(), source.size(), back.data(), max_length_needed);
if (r.error == simdutf::error_code::SUCCESS) {
// We expect failure but if we succeed, then we should have a roundtrip.
back.resize(max_length_needed);
std::vector<char> back2(e->base64_length_from_binary(back.size()));
size_t base64size =
e->binary_to_base64(back.data(), back.size(), back2.data());
back2.resize(base64size);
size_t max_length_needed2 =
e->maximal_binary_length_from_base64(back2.data(), back2.size());
std::vector<char> back3(max_length_needed2);
simdutf::result r2 = simdutf::base64_to_binary_safe(
back2.data(), back2.size(), back3.data(), max_length_needed2);
if (r2.error != simdutf::error_code::SUCCESS) {
print_input(source, e);
return false;
}
if (max_length_needed != back.size()) {
print_input(source, e);
return false;
}
if (back3.size() != back.size()) {
print_input(source, e);
return false;
}
}
}
/// Base64 tests. We encode the content as binary in base64 and we decode
/// it, it should always succeed.
{
source = fdp.ConsumeRandomLengthString(kMaxStringSize);
std::vector<char> base64buffer(
e->base64_length_from_binary(source.size()));
size_t base64size = e->binary_to_base64(source.data(), source.size(),
base64buffer.data());
if (base64size != base64buffer.size()) {
print_input(source, e);
abort();
}
std::vector<char> back(e->maximal_binary_length_from_base64(
base64buffer.data(), base64buffer.size()));
simdutf::result r = e->base64_to_binary(base64buffer.data(),
base64buffer.size(), back.data());
if (r.error != simdutf::error_code::SUCCESS) {
print_input(source, e);
abort();
}
if (r.count != source.size()) {
print_input(source, e);
abort();
}
for (size_t i = 0; i < source.size(); i++) {
if (back[i] != (source.c_str())[i]) {
print_input(source, e);
abort();
}
}
size_t max_length = back.size();
r = simdutf::base64_to_binary_safe(
base64buffer.data(), base64buffer.size(), back.data(), max_length);
if (r.error != simdutf::error_code::SUCCESS) {
printf("base64 round trip failed, error code %d\n", r.error);
print_input(source, e);
return false;
}
if (max_length != source.size()) {
printf("base64 safe round trip failed, not the same size %zu %zu\n",
max_length, source.size());
print_input(source, e);
return false;
}
for (size_t i = 0; i < source.size(); i++) {
if (back[i] != (source.c_str())[i]) {
printf("base64 round trip failed, same size, different content\n");
print_input(source, e);
return false;
}
}
}
} // for (auto &e : simdutf::get_available_implementations()) {
return 0;
} // extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|