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.
A block may not end inside a sequence
Browse filesAll 3,418 inputs the fuzzer caught are a multiple of 32 bytes long and stop partway through a sequence: ...4D E2, ...F0 90 A9. Inside the input, the next block's tables catch a sequence cut short. At a block-aligned end there is no next block, and no padded tail either. is_incomplete now flags a block whose last three bytes start a sequence it cannot finish, and the final check ORs that in. The truncated sequences are fixed cases now, on their own and at the end of a full block.
failing
6 steps in 1 ms
2 files changed
+27−9
@@ -62,12 +62,14 @@ third_byte: times 32 db 0xE0 - 0x80 ; only 111_____ stays >= 0x80
6262 fourth_byte: times 32 db 0xF0 - 0x80 ; only 1111____ stays >= 0x80
6363 high_bit: times 32 db 0x80
6464 spaces: times 32 db 0x20
65+incomplete: times 29 db 0xFF ; a block may not end inside a sequence
66+ db 0xF0 - 1, 0xE0 - 1, 0xC0 - 1
6567
6668 section .text
6769 global utf8_valid_avx2
6870
6971 ; One 32-byte block. In: ymm0 = block, ymm1 = previous block.
70−; Accumulates into ymm2 (errors).
72+; Accumulates into ymm2 (errors); leaves ymm3 = "this block ends mid-sequence".
7173 %macro CHECK_BLOCK 0
7274 vperm2i128 ymm4, ymm1, ymm0, 0x21 ; [previous.hi | block.lo]
7375 vpalignr ymm5, ymm0, ymm4, 15 ; prev1: each byte's predecessor
@@ -93,6 +95,7 @@ spaces: times 32 db 0x20
9395 vpand ymm4, ymm4, [high_bit] ; must be a 2nd or 3rd continuation
9496 vpxor ymm4, ymm4, ymm3 ; ...exactly where the tables expect one
9597 vpor ymm2, ymm2, ymm4
98+ vpsubusb ymm3, ymm0, [incomplete]
9699 %endmacro
97100
98101 utf8_valid_avx2:
@@ -100,6 +103,7 @@ utf8_valid_avx2:
100103 mov r9, rdx ; bytes left
101104 vpxor xmm1, xmm1, xmm1 ; the block before the first is all zeros
102105 vpxor xmm2, xmm2, xmm2
106+ vpxor xmm3, xmm3, xmm3
103107 cmp r9, 32
104108 jb .tail
105109 .loop:
@@ -130,6 +134,7 @@ utf8_valid_avx2:
130134 pop rbp
131135 CHECK_BLOCK
132136 .eof:
137+ vpor ymm2, ymm2, ymm3 ; and the input may not stop mid-sequence
133138 vptest ymm2, ymm2
134139 setz al
135140 movzx eax, al
@@ -39,22 +39,35 @@ static const struct {
3939 { "\xC3\xA9\xA9", 0, "one continuation too many" },
4040 { "\xC3", 0, "a two-byte lead at the very end" },
4141 { "\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" },
4246 };
4347 #define SAMPLES (sizeof samples / sizeof samples[0])
4448
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.
4551 static int fixed_cases(void)
4652 {
47− int wrong = 0;
53+ uint8_t buf[32];
54+ int runs = 0, wrong = 0;
4855 for (size_t i = 0; i < SAMPLES; i++) {
49− const uint8_t *p = (const uint8_t *)samples[i].bytes;
5056 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)
53− continue;
54− wrong++;
55− printf("wrong: %s: scalar %d, avx2 %d\n", samples[i].what, a, b);
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+ }
5669 }
57− printf("cases: %zu samples, %d wrong\n", SAMPLES, wrong);
70+ printf("cases: %zu samples, %d runs, %d wrong\n", SAMPLES, runs, wrong);
5871 return wrong;
5972 }
6073