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.
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.
3//
4// build/test.exe [fuzz iterations]
6#include <stdint.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
11int utf8_valid_scalar(const uint8_t *buf, size_t len);
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 { "\xE2\x82", 0, "a three-byte sequence missing its last byte" },
43 { "\xE2\x82" "A", 0, "a three-byte sequence cut short by ASCII" },
44 { "\xF0\x9F\x98", 0, "a four-byte sequence missing its last byte" },
45 { "\xF0\x9F\x98" "A", 0, "a four-byte sequence cut short by ASCII" },
46};
47#define SAMPLES (sizeof samples / sizeof samples[0])
49// Each sample is tried at offsets 0 to 63 behind ASCII, so it also straddles
50// a 32-byte block boundary, once at the end of the input and once followed
51// by 32 more bytes of ASCII.
52static int fixed_cases(void)
53{
54 uint8_t buf[160];
55 int runs = 0, failures = 0;
56 for (size_t i = 0; i < SAMPLES; i++) {
57 size_t n = strlen(samples[i].bytes);
58 for (size_t offset = 0; offset < 64; offset++) {
59 for (size_t after = 0; after <= 32; after += 32) {
60 memset(buf, 'a', sizeof buf);
61 memcpy(buf + offset, samples[i].bytes, n);
62 int a = utf8_valid_scalar(buf, offset + n + after);
63 int b = utf8_valid_avx2(buf, offset + n + after);
64 runs++;
65 if (a == samples[i].valid && b == samples[i].valid)
66 continue;
67 if (failures++ < 8)
68 printf("wrong: %s at offset %zu%s: scalar %d, avx2 %d\n", samples[i].what, offset,
69 after ? " with ASCII after" : "", a, b);
70 }
71 }
72 }
73 printf("cases: %zu samples, %d runs, %d wrong\n", SAMPLES, runs, failures);
74 return failures;
75}
77static uint64_t rng = 0x9E3779B97F4A7C15ull;
79static uint32_t next(void)
80{
81 rng ^= rng << 13;
82 rng ^= rng >> 7;
83 rng ^= rng << 17;
84 return (uint32_t)rng;
85}
87// Append one random, well-formed code point as UTF-8.
88static size_t put(uint8_t *p)
89{
90 uint32_t r = next(), c;
91 switch (r % 6) {
92 case 0: case 1: case 2:
93 p[0] = (uint8_t)(r >> 8) & 0x7F;
94 return 1;
95 case 3:
96 c = 0x80 + (r >> 8) % 0x780;
97 p[0] = (uint8_t)(0xC0 | c >> 6);
98 p[1] = (uint8_t)(0x80 | (c & 63));
99 return 2;
100 case 4:
101 c = 0x800 + (r >> 8) % 0xF000; // skip the 0x800 surrogates
102 if (c >= 0xD800)
103 c += 0x800;
104 p[0] = (uint8_t)(0xE0 | c >> 12);
105 p[1] = (uint8_t)(0x80 | (c >> 6 & 63));
106 p[2] = (uint8_t)(0x80 | (c & 63));
107 return 3;
108 default:
109 c = 0x10000 + (r >> 8) % 0x100000;
110 p[0] = (uint8_t)(0xF0 | c >> 18);
111 p[1] = (uint8_t)(0x80 | (c >> 12 & 63));
112 p[2] = (uint8_t)(0x80 | (c >> 6 & 63));
113 p[3] = (uint8_t)(0x80 | (c & 63));
114 return 4;
115 }
116}
118// Damage a well-formed buffer in one of the ways real text goes wrong.
119static size_t corrupt(uint8_t *buf, size_t n)
120{
121 switch (next() % 3) {
122 case 0: // flip one bit
123 buf[next() % n] ^= (uint8_t)(1u << (next() % 8));
124 return n;
125 case 1: // cut the end off
126 return n - 1 - next() % (n < 3 ? n : 3);
127 default: // overwrite one byte with anything
128 buf[next() % n] = (uint8_t)next();
129 return n;
130 }
131}
133static long fuzz(long iterations)
134{
135 uint8_t buf[512];
136 long mismatches = 0, malformed = 0;
137 for (long i = 0; i < iterations; i++) {
138 size_t n = 0, want = next() % 400;
139 while (n < want)
140 n += put(buf + n);
141 if (n && next() % 2)
142 n = corrupt(buf, n);
143 int a = utf8_valid_scalar(buf, n);
144 int b = utf8_valid_avx2(buf, n);
145 malformed += !a;
146 if (a == b || mismatches++ >= 5)
147 continue;
148 printf("mismatch at input %ld (%zu bytes): scalar %d, avx2 %d\n ", i, n, a, b);
149 for (size_t k = n > 12 ? n - 12 : 0; k < n; k++)
150 printf("%02X ", buf[k]);
151 puts("<- the last bytes");
152 }
153 printf("fuzz: %ld inputs, %ld malformed, %ld mismatches\n", iterations, malformed, mismatches);
154 return mismatches;
155}
157int main(int argc, char **argv)
158{
159 long iterations = argc > 1 ? atol(argv[1]) : 1000000;
160 if (!__builtin_cpu_supports("avx2")) {
161 puts("this CPU has no AVX2; nothing to test");
162 return 2;
163 }
164 int wrong = fixed_cases();
165 long mismatches = fuzz(iterations);
166 return wrong || mismatches;
167}