Skip to content

Commit f0bb52f

Browse files
authored
Merge pull request #592 from Graveflo/avx-bug
Fix out-of-bounds read in blendLineCoverageMaskAvx2
2 parents 7ecb73c + 8f06f1b commit f0bb52f

3 files changed

Lines changed: 53 additions & 1 deletion

File tree

‎src/pixie/simd/avx2.nim‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ proc blendLineCoverageMaskAvx2*(
695695
15, -1, 15, -1, 11, -1, 11, -1, 7, -1, 7, -1, 3, -1, 3, -1,
696696
15, -1, 15, -1, 11, -1, 11, -1, 7, -1, 7, -1, 3, -1, 3, -1
697697
)
698-
while i < len - 16:
698+
while i <= len - 32:
699699
let
700700
coverage = mm256_loadu_si256(coverages[i].addr)
701701
eqZero = mm256_cmpeq_epi8(coverage, vecZero)

‎tests/test_simd.nim‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import pixie/simd
2+
3+
when allowSimd and defined(amd64) and not defined(pixieNoAvx):
4+
import chroma, pixie/blends, pixie/common
5+
6+
if cpuHasAvx2:
7+
let
8+
original = rgbx(120, 80, 40, 255)
9+
mask = rgbx(255, 255, 255, 255)
10+
var cases: int
11+
for length in 0 .. 160:
12+
for shift in 0 .. 7:
13+
for pattern in 0 .. 3:
14+
var pixels = newSeq[ColorRGBX](length + 96)
15+
for pixel in pixels.mitems:
16+
pixel = original
17+
18+
# Exercise every pixel alignment, with sentinels on both sides.
19+
var start: int
20+
while (cast[uint](pixels[start].addr) and 31) != 0:
21+
inc start
22+
start += 16 + shift
23+
24+
# Padding makes the old overread safe while its extra stores still
25+
# corrupt the sentinels, independently of the allocator's layout.
26+
var coverages = newSeq[uint8](length + 32)
27+
for i in 0 ..< length:
28+
coverages[i] = case pattern
29+
of 0: 0'u8
30+
of 1: 255'u8
31+
of 2: 128'u8
32+
else: ((i * 73 + 19) mod 256).uint8
33+
34+
blendLineCoverageMaskAvx2(
35+
cast[ptr UncheckedArray[ColorRGBX]](pixels[start].addr),
36+
cast[ptr UncheckedArray[uint8]](coverages[0].addr),
37+
mask,
38+
length
39+
)
40+
41+
for i, pixel in pixels:
42+
let expected =
43+
if i >= start and i < start + length:
44+
blendMask(original, mask * coverages[i - start])
45+
else:
46+
original
47+
doAssert pixel == expected,
48+
"length=" & $length & " shift=" & $shift &
49+
" pattern=" & $pattern & " pixel=" & $(i - start)
50+
inc cases
51+
echo "AVX2 coverage mask: ", cases, " cases passed"

‎tests/tests.nim‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import
1313
test_png,
1414
test_ppm,
1515
test_qoi,
16+
test_simd,
1617
test_webp,
1718
test_svg,
1819
xrays

0 commit comments

Comments
 (0)