acompiled

The catalog contains seeded project examples. Your workspace stores your own projects and conversations.

tsvet/utf8-check

Public
Star9

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.

Code
Blame·4 commits across 164 linesoldernewer
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.
Fuzz the AVX2 version against the scalar oneAug 5 4// build/test.exe [fuzz iterations]
6#include <stdint.h>
7#include <stdio.h>
10
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" },
A block may not end inside a sequenceAug 12 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" },
47#define SAMPLES (sizeof samples / sizeof samples[0])
48
A block may not end inside a sequenceAug 12 49// Each sample is tried on its own, then again at the very end of a full
50// 32-byte block, where no next byte shows that it was cut short.
Fuzz the AVX2 version against the scalar oneAug 5 51static int fixed_cases(void)
A block may not end inside a sequenceAug 12 53 uint8_t buf[32];
54 int runs = 0, wrong = 0;
Scalar validator with every range from table 3-7Jul 20 55 for (size_t i = 0; i < SAMPLES; i++) {
AVX2: three nibble tables and a saturating subtractAug 4 56 size_t n = strlen(samples[i].bytes);
A block may not end inside a sequenceAug 12 57 memset(buf, 'a', sizeof buf);
58 memcpy(buf + 32 - n, samples[i].bytes, n);
59 for (int full = 0; full <= 1; full++) {
60 const uint8_t *p = full ? buf : (const uint8_t *)samples[i].bytes;
61 size_t len = full ? 32 : n;
62 int a = utf8_valid_scalar(p, len), b = utf8_valid_avx2(p, len);
63 runs++;
64 if (a == samples[i].valid && b == samples[i].valid)
65 continue;
66 wrong++;
67 printf("wrong: %s%s: scalar %d, avx2 %d\n", samples[i].what, full ? " at the end of a block" : "", a, b);
68 }
A block may not end inside a sequenceAug 12 70 printf("cases: %zu samples, %d runs, %d wrong\n", SAMPLES, runs, wrong);
72}
73
74static uint64_t rng = 0x9E3779B97F4A7C15ull;
75
76static uint32_t next(void)
77{
78 rng ^= rng << 13;
79 rng ^= rng >> 7;
80 rng ^= rng << 17;
81 return (uint32_t)rng;
82}
83
84// Append one random, well-formed code point as UTF-8.
85static size_t put(uint8_t *p)
86{
87 uint32_t r = next(), c;
88 switch (r % 6) {
89 case 0: case 1: case 2:
90 p[0] = (uint8_t)(r >> 8) & 0x7F;
91 return 1;
92 case 3:
93 c = 0x80 + (r >> 8) % 0x780;
94 p[0] = (uint8_t)(0xC0 | c >> 6);
95 p[1] = (uint8_t)(0x80 | (c & 63));
96 return 2;
97 case 4:
98 c = 0x800 + (r >> 8) % 0xF000; // skip the 0x800 surrogates
99 if (c >= 0xD800)
100 c += 0x800;
101 p[0] = (uint8_t)(0xE0 | c >> 12);
102 p[1] = (uint8_t)(0x80 | (c >> 6 & 63));
103 p[2] = (uint8_t)(0x80 | (c & 63));
104 return 3;
105 default:
106 c = 0x10000 + (r >> 8) % 0x100000;
107 p[0] = (uint8_t)(0xF0 | c >> 18);
108 p[1] = (uint8_t)(0x80 | (c >> 12 & 63));
109 p[2] = (uint8_t)(0x80 | (c >> 6 & 63));
110 p[3] = (uint8_t)(0x80 | (c & 63));
111 return 4;
112 }
113}
114
115// Damage a well-formed buffer in one of the ways real text goes wrong.
116static size_t corrupt(uint8_t *buf, size_t n)
117{
118 switch (next() % 3) {
119 case 0: // flip one bit
120 buf[next() % n] ^= (uint8_t)(1u << (next() % 8));
121 return n;
122 case 1: // cut the end off
123 return n - 1 - next() % (n < 3 ? n : 3);
124 default: // overwrite one byte with anything
125 buf[next() % n] = (uint8_t)next();
126 return n;
127 }
128}
129
130static long fuzz(long iterations)
131{
132 uint8_t buf[512];
133 long mismatches = 0, malformed = 0;
134 for (long i = 0; i < iterations; i++) {
135 size_t n = 0, want = next() % 400;
136 while (n < want)
137 n += put(buf + n);
138 if (n && next() % 2)
139 n = corrupt(buf, n);
140 int a = utf8_valid_scalar(buf, n);
141 int b = utf8_valid_avx2(buf, n);
142 malformed += !a;
143 if (a == b || mismatches++ >= 5)
144 continue;
145 printf("mismatch at input %ld (%zu bytes): scalar %d, avx2 %d\n ", i, n, a, b);
146 for (size_t k = n > 12 ? n - 12 : 0; k < n; k++)
147 printf("%02X ", buf[k]);
148 puts("<- the last bytes");
149 }
150 printf("fuzz: %ld inputs, %ld malformed, %ld mismatches\n", iterations, malformed, mismatches);
151 return mismatches;
152}
153
154int main(int argc, char **argv)
155{
156 long iterations = argc > 1 ? atol(argv[1]) : 1000000;
157 if (!__builtin_cpu_supports("avx2")) {
158 puts("this CPU has no AVX2; nothing to test");
159 return 2;
160 }
161 int wrong = fixed_cases();
162 long mismatches = fuzz(iterations);
163 return wrong || mismatches;