// test.c: the boundaries of Unicode table 3-7, then a fuzzer that uses the // scalar version as the oracle for the AVX2 one. // // build/test.exe [fuzz iterations] #include #include #include #include int utf8_valid_scalar(const uint8_t *buf, size_t len); int utf8_valid_avx2(const uint8_t *buf, size_t len); static const struct { const char *bytes; int valid; const char *what; } samples[] = { { "", 1, "nothing at all" }, { "plain ASCII", 1, "ASCII" }, { "\xC2\x80", 1, "U+0080, the first two-byte code point" }, { "\xDF\xBF", 1, "U+07FF, the last" }, { "\xE0\xA0\x80", 1, "U+0800, the first three-byte code point" }, { "\xED\x9F\xBF", 1, "U+D7FF, just below the surrogates" }, { "\xEE\x80\x80", 1, "U+E000, just above them" }, { "\xEF\xBF\xBF", 1, "U+FFFF" }, { "\xF0\x90\x80\x80", 1, "U+10000, the first four-byte code point" }, { "\xF4\x8F\xBF\xBF", 1, "U+10FFFF, the last code point" }, { "\xC0\x80", 0, "an overlong NUL" }, { "\xC1\xBF", 0, "an overlong U+007F" }, { "\xE0\x9F\xBF", 0, "an overlong U+07FF" }, { "\xED\xA0\x80", 0, "U+D800, a surrogate" }, { "\xED\xBF\xBF", 0, "U+DFFF, a surrogate" }, { "\xF0\x8F\xBF\xBF", 0, "an overlong U+FFFF" }, { "\xF4\x90\x80\x80", 0, "U+110000, past the end of Unicode" }, { "\xF5\x80\x80\x80", 0, "F5, which never appears" }, { "\xFF", 0, "FF, which never appears" }, { "\x80", 0, "a continuation with no lead" }, { "\xC3\xA9\xA9", 0, "one continuation too many" }, { "\xC3", 0, "a two-byte lead at the very end" }, { "\xC3" "A", 0, "a two-byte lead followed by ASCII" }, { "\xE2\x82", 0, "a three-byte sequence missing its last byte" }, { "\xE2\x82" "A", 0, "a three-byte sequence cut short by ASCII" }, { "\xF0\x9F\x98", 0, "a four-byte sequence missing its last byte" }, { "\xF0\x9F\x98" "A", 0, "a four-byte sequence cut short by ASCII" }, }; #define SAMPLES (sizeof samples / sizeof samples[0]) // Each sample is tried at offsets 0 to 63 behind ASCII, so it also straddles // a 32-byte block boundary, once at the end of the input and once followed // by 32 more bytes of ASCII. static int fixed_cases(void) { uint8_t buf[160]; int runs = 0, failures = 0; for (size_t i = 0; i < SAMPLES; i++) { size_t n = strlen(samples[i].bytes); for (size_t offset = 0; offset < 64; offset++) { for (size_t after = 0; after <= 32; after += 32) { memset(buf, 'a', sizeof buf); memcpy(buf + offset, samples[i].bytes, n); int a = utf8_valid_scalar(buf, offset + n + after); int b = utf8_valid_avx2(buf, offset + n + after); runs++; if (a == samples[i].valid && b == samples[i].valid) continue; if (failures++ < 8) printf("wrong: %s at offset %zu%s: scalar %d, avx2 %d\n", samples[i].what, offset, after ? " with ASCII after" : "", a, b); } } } printf("cases: %zu samples, %d runs, %d wrong\n", SAMPLES, runs, failures); return failures; } static uint64_t rng = 0x9E3779B97F4A7C15ull; static uint32_t next(void) { rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; return (uint32_t)rng; } // Append one random, well-formed code point as UTF-8. static size_t put(uint8_t *p) { uint32_t r = next(), c; switch (r % 6) { case 0: case 1: case 2: p[0] = (uint8_t)(r >> 8) & 0x7F; return 1; case 3: c = 0x80 + (r >> 8) % 0x780; p[0] = (uint8_t)(0xC0 | c >> 6); p[1] = (uint8_t)(0x80 | (c & 63)); return 2; case 4: c = 0x800 + (r >> 8) % 0xF000; // skip the 0x800 surrogates if (c >= 0xD800) c += 0x800; p[0] = (uint8_t)(0xE0 | c >> 12); p[1] = (uint8_t)(0x80 | (c >> 6 & 63)); p[2] = (uint8_t)(0x80 | (c & 63)); return 3; default: c = 0x10000 + (r >> 8) % 0x100000; p[0] = (uint8_t)(0xF0 | c >> 18); p[1] = (uint8_t)(0x80 | (c >> 12 & 63)); p[2] = (uint8_t)(0x80 | (c >> 6 & 63)); p[3] = (uint8_t)(0x80 | (c & 63)); return 4; } } // Damage a well-formed buffer in one of the ways real text goes wrong. static size_t corrupt(uint8_t *buf, size_t n) { switch (next() % 3) { case 0: // flip one bit buf[next() % n] ^= (uint8_t)(1u << (next() % 8)); return n; case 1: // cut the end off return n - 1 - next() % (n < 3 ? n : 3); default: // overwrite one byte with anything buf[next() % n] = (uint8_t)next(); return n; } } static long fuzz(long iterations) { uint8_t buf[512]; long mismatches = 0, malformed = 0; for (long i = 0; i < iterations; i++) { size_t n = 0, want = next() % 400; while (n < want) n += put(buf + n); if (n && next() % 2) n = corrupt(buf, n); int a = utf8_valid_scalar(buf, n); int b = utf8_valid_avx2(buf, n); malformed += !a; if (a == b || mismatches++ >= 5) continue; printf("mismatch at input %ld (%zu bytes): scalar %d, avx2 %d\n ", i, n, a, b); for (size_t k = n > 12 ? n - 12 : 0; k < n; k++) printf("%02X ", buf[k]); puts("<- the last bytes"); } printf("fuzz: %ld inputs, %ld malformed, %ld mismatches\n", iterations, malformed, mismatches); return mismatches; } int main(int argc, char **argv) { long iterations = argc > 1 ? atol(argv[1]) : 1000000; if (!__builtin_cpu_supports("avx2")) { puts("this CPU has no AVX2; nothing to test"); return 2; } int wrong = fixed_cases(); long mismatches = fuzz(iterations); return wrong || mismatches; }