UTF-8 validation in x86-64: a scalar reference and an AVX2 version that checks 32 bytes per step. Every build fuzzes one against the other.
Fuzz the AVX2 version against the scalar oneAug 5
1// test.c: the boundaries of Unicode table 3-7, then a fuzzer that uses the
2// scalar version as the oracle for the AVX2 one.
6#include <stdint.h>
7#include <stdio.h>
11int utf8_valid_scalar(const uint8_t *buf, size_t len);
AVX2: three nibble tables and a saturating subtractAug 4
12int utf8_valid_avx2(const uint8_t *buf, size_t len);
14static const struct {
15 const char *bytes;
16 int valid;
17 const char *what;
18} samples[] = {
19 { "", 1, "nothing at all" },
20 { "plain ASCII", 1, "ASCII" },
21 { "\xC2\x80", 1, "U+0080, the first two-byte code point" },
22 { "\xDF\xBF", 1, "U+07FF, the last" },
23 { "\xE0\xA0\x80", 1, "U+0800, the first three-byte code point" },
24 { "\xED\x9F\xBF", 1, "U+D7FF, just below the surrogates" },
25 { "\xEE\x80\x80", 1, "U+E000, just above them" },
26 { "\xEF\xBF\xBF", 1, "U+FFFF" },
27 { "\xF0\x90\x80\x80", 1, "U+10000, the first four-byte code point" },
28 { "\xF4\x8F\xBF\xBF", 1, "U+10FFFF, the last code point" },
29 { "\xC0\x80", 0, "an overlong NUL" },
30 { "\xC1\xBF", 0, "an overlong U+007F" },
31 { "\xE0\x9F\xBF", 0, "an overlong U+07FF" },
32 { "\xED\xA0\x80", 0, "U+D800, a surrogate" },
33 { "\xED\xBF\xBF", 0, "U+DFFF, a surrogate" },
34 { "\xF0\x8F\xBF\xBF", 0, "an overlong U+FFFF" },
35 { "\xF4\x90\x80\x80", 0, "U+110000, past the end of Unicode" },
36 { "\xF5\x80\x80\x80", 0, "F5, which never appears" },
37 { "\xFF", 0, "FF, which never appears" },
38 { "\x80", 0, "a continuation with no lead" },
39 { "\xC3\xA9\xA9", 0, "one continuation too many" },
40 { "\xC3", 0, "a two-byte lead at the very end" },
41 { "\xC3" "A", 0, "a two-byte lead followed by ASCII" },
42};
43#define SAMPLES (sizeof samples / sizeof samples[0])
47 int wrong = 0;
48 for (size_t i = 0; i < SAMPLES; i++) {
AVX2: three nibble tables and a saturating subtractAug 4
49 const uint8_t *p = (const uint8_t *)samples[i].bytes;
50 size_t n = strlen(samples[i].bytes);
51 int a = utf8_valid_scalar(p, n), b = utf8_valid_avx2(p, n);
52 if (a == samples[i].valid && b == samples[i].valid)
54 wrong++;
AVX2: three nibble tables and a saturating subtractAug 4
55 printf("wrong: %s: scalar %d, avx2 %d\n", samples[i].what, a, b);
57 printf("cases: %zu samples, %d wrong\n", SAMPLES, wrong);
59}
61static uint64_t rng = 0x9E3779B97F4A7C15ull;
63static uint32_t next(void)
64{
65 rng ^= rng << 13;
66 rng ^= rng >> 7;
67 rng ^= rng << 17;
68 return (uint32_t)rng;
69}
71// Append one random, well-formed code point as UTF-8.
72static size_t put(uint8_t *p)
73{
74 uint32_t r = next(), c;
75 switch (r % 6) {
76 case 0: case 1: case 2:
77 p[0] = (uint8_t)(r >> 8) & 0x7F;
78 return 1;
79 case 3:
80 c = 0x80 + (r >> 8) % 0x780;
81 p[0] = (uint8_t)(0xC0 | c >> 6);
82 p[1] = (uint8_t)(0x80 | (c & 63));
83 return 2;
84 case 4:
85 c = 0x800 + (r >> 8) % 0xF000; // skip the 0x800 surrogates
86 if (c >= 0xD800)
87 c += 0x800;
88 p[0] = (uint8_t)(0xE0 | c >> 12);
89 p[1] = (uint8_t)(0x80 | (c >> 6 & 63));
90 p[2] = (uint8_t)(0x80 | (c & 63));
91 return 3;
92 default:
93 c = 0x10000 + (r >> 8) % 0x100000;
94 p[0] = (uint8_t)(0xF0 | c >> 18);
95 p[1] = (uint8_t)(0x80 | (c >> 12 & 63));
96 p[2] = (uint8_t)(0x80 | (c >> 6 & 63));
97 p[3] = (uint8_t)(0x80 | (c & 63));
98 return 4;
99 }
100}
102// Damage a well-formed buffer in one of the ways real text goes wrong.
103static size_t corrupt(uint8_t *buf, size_t n)
104{
105 switch (next() % 3) {
106 case 0: // flip one bit
107 buf[next() % n] ^= (uint8_t)(1u << (next() % 8));
108 return n;
109 case 1: // cut the end off
110 return n - 1 - next() % (n < 3 ? n : 3);
111 default: // overwrite one byte with anything
112 buf[next() % n] = (uint8_t)next();
113 return n;
114 }
115}
117static long fuzz(long iterations)
118{
119 uint8_t buf[512];
120 long mismatches = 0, malformed = 0;
121 for (long i = 0; i < iterations; i++) {
122 size_t n = 0, want = next() % 400;
123 while (n < want)
124 n += put(buf + n);
125 if (n && next() % 2)
126 n = corrupt(buf, n);
127 int a = utf8_valid_scalar(buf, n);
128 int b = utf8_valid_avx2(buf, n);
129 malformed += !a;
130 if (a == b || mismatches++ >= 5)
131 continue;
132 printf("mismatch at input %ld (%zu bytes): scalar %d, avx2 %d\n ", i, n, a, b);
133 for (size_t k = n > 12 ? n - 12 : 0; k < n; k++)
134 printf("%02X ", buf[k]);
135 puts("<- the last bytes");
136 }
137 printf("fuzz: %ld inputs, %ld malformed, %ld mismatches\n", iterations, malformed, mismatches);
138 return mismatches;
139}
141int main(int argc, char **argv)
142{
143 long iterations = argc > 1 ? atol(argv[1]) : 1000000;
144 if (!__builtin_cpu_supports("avx2")) {
145 puts("this CPU has no AVX2; nothing to test");
146 return 2;
147 }
148 int wrong = fixed_cases();
149 long mismatches = fuzz(iterations);
150 return wrong || mismatches;