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 one
Browse filesRandom well-formed text up to 400 bytes, half of it damaged: a flipped bit, a cut-off end, or one byte overwritten. The scalar version is the oracle. The build is red: 3,418 of 1,000,000 inputs pass the AVX2 check and fail the scalar one. The first five are in the log.
failing
6 steps in 1 ms
3 files changed
+102−12
@@ -18,10 +18,10 @@ name = "build the tests"
1818 run = ["gcc", "-O2", "-Wall", "-o", "build/test.exe", "test/test.c", "build/utf8_scalar.obj", "build/utf8_avx2.obj"]
1919
2020 [[step]]
21−name = "table 3-7"
21+name = "table 3-7, then 1,000,000 fuzzed inputs"
2222 on = "windows"
23−run = ["./build/test.exe"]
24−expect = "0 wrong"
23+run = ["./build/test.exe", "1000000"]
24+expect = "0 mismatches"
2525
2626 [[step]]
2727 name = "build the benchmark"
@@ -13,7 +13,7 @@
1313 ; F1..F3 80..BF 80..BF 80..BF
1414 ; F4 80..8F 80..BF 80..BF
1515 ;
16−; This is the oracle for the SIMD version to come.
16+; The AVX2 version is fuzzed against this one on every build.
1717
1818 default rel
1919 bits 64
@@ -1,9 +1,11 @@
1−// test.c: the boundaries of Unicode table 3-7, for both versions.
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.
23 //
3−// build/test.exe
4+// build/test.exe [fuzz iterations]
45
56 #include <stdint.h>
67 #include <stdio.h>
8+#include <stdlib.h>
79 #include <string.h>
810
911 int utf8_valid_scalar(const uint8_t *buf, size_t len);
@@ -40,12 +42,8 @@ static const struct {
4042 };
4143 #define SAMPLES (sizeof samples / sizeof samples[0])
4244
43−int main(void)
45+static int fixed_cases(void)
4446 {
45− if (!__builtin_cpu_supports("avx2")) {
46− puts("this CPU has no AVX2; nothing to test");
47− return 2;
48− }
4947 int wrong = 0;
5048 for (size_t i = 0; i < SAMPLES; i++) {
5149 const uint8_t *p = (const uint8_t *)samples[i].bytes;
@@ -57,5 +55,97 @@ int main(void)
5755 printf("wrong: %s: scalar %d, avx2 %d\n", samples[i].what, a, b);
5856 }
5957 printf("cases: %zu samples, %d wrong\n", SAMPLES, wrong);
60− return wrong != 0;
58+ return wrong;
59+}
60+
61+static uint64_t rng = 0x9E3779B97F4A7C15ull;
62+
63+static uint32_t next(void)
64+{
65+ rng ^= rng << 13;
66+ rng ^= rng >> 7;
67+ rng ^= rng << 17;
68+ return (uint32_t)rng;
69+}
70+
71+// Append one random, well-formed code point as UTF-8.
72+static 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+}
101+
102+// Damage a well-formed buffer in one of the ways real text goes wrong.
103+static 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+}
116+
117+static 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+}
140+
141+int 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;
61151 }