Debian Codegraph @

simd/guard_amd64_test.go

raw symbols SCIP
1 //go:build amd64 && linux
2
3 package simdscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/
4
5 import (
6 "syscallscip-go gomod github.com/golang/go/src go1.25.0 syscall/"
7 "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/"
8
9 "github.com/stretchr/testify/requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/"
10 )
11
12 // Every kernel in this package is self-bounding: it clamps its own vector
13 // loops and finishes short remainders itself, so it may be called directly
14 // with any length. That is not testable with ordinary slices — Go rounds
15 // allocations up to a size class, so a read past the end lands on
16 // zero-filled slack and the wrong answer never materializes. These tests
17 // place the last byte of the input flush against an unmapped page, turning
18 // any over-read into a fault instead of a silent success.
19 //
20 // countNonASCIIAVX2 is the reason this file exists: it is the one kernel
21 // whose result depends on the trailing bytes, so it cannot use the
22 // overlapping-window epilogue its siblings use, and an earlier version
23 // looped while base < end and read whole vectors past the slice.
24
25 // guardedTail returns a slice of n bytes whose final byte is the last
26 // readable byte before an unmapped page, plus a cleanup function. Reading
27 // even one byte past the end faults.
28 func guardedTailscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/guardedTail().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, n int) []byte {
29 t.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Helper().()
30 pageSize := syscallscip-go gomod github.com/golang/go/src go1.25.0 syscall/.Getpagesizescip-go gomod github.com/golang/go/src go1.25.0 syscall/Getpagesize().()
31 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.LessOrEqualscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/LessOrEqual().(t, n, pageSize, "input must fit in one page")
32
33 // Two pages, then drop read access to the second one.
34 region, err := syscallscip-go gomod github.com/golang/go/src go1.25.0 syscall/.Mmapscip-go gomod github.com/golang/go/src go1.25.0 syscall/Mmap().(-1, 0, 2*pageSize,
35 syscallscip-go gomod github.com/golang/go/src go1.25.0 syscall/.PROT_READscip-go gomod github.com/golang/go/src go1.25.0 syscall/PROT_READ.|syscallscip-go gomod github.com/golang/go/src go1.25.0 syscall/.PROT_WRITEscip-go gomod github.com/golang/go/src go1.25.0 syscall/PROT_WRITE.,
36 syscallscip-go gomod github.com/golang/go/src go1.25.0 syscall/.MAP_PRIVATEscip-go gomod github.com/golang/go/src go1.25.0 syscall/MAP_PRIVATE.|syscallscip-go gomod github.com/golang/go/src go1.25.0 syscall/.MAP_ANONscip-go gomod github.com/golang/go/src go1.25.0 syscall/MAP_ANON.)
37 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/NoError().(t, err)
38 t.Cleanupscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Cleanup().(func() {
39 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/NoError().(t, syscallscip-go gomod github.com/golang/go/src go1.25.0 syscall/.Munmapscip-go gomod github.com/golang/go/src go1.25.0 syscall/Munmap().(region))
40 })
41 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/NoError().(t, syscallscip-go gomod github.com/golang/go/src go1.25.0 syscall/.Mprotectscip-go gomod github.com/golang/go/src go1.25.0 syscall/Mprotect().(region[pageSize:], syscallscip-go gomod github.com/golang/go/src go1.25.0 syscall/.PROT_NONEscip-go gomod github.com/golang/go/src go1.25.0 syscall/PROT_NONE.))
42
43 return region[pageSize-n : pageSize : pageSize]
44 }
45
46 func Test_KernelsAreSelfBoundingscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/Test_KernelsAreSelfBounding().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
47 if !Acceleratedscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/Accelerated().() {
48 t.Skipscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skip().("AVX2 kernels not active on this CPU")
49 }
50 // Lengths that leave every possible sub-vector and sub-block
51 // remainder, including the 1..31 that has no whole vector at all.
52 for _, n := range []int{
53 1, 2, 7, 8, 15, 16, 31, 32, 33, 63, 64, 65, 95,
54 96, 127, 128, 129, 159, 160, 161, 200, 255, 256, 257, 384, 385,
55 } {
56 buf := guardedTailscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/guardedTail().(t, n)
57 for i := range buf {
58 buf[i] = byte('a' + i%23)
59 }
60 // Each kernel is called directly, bypassing the *Impl dispatch, so
61 // the guard is on the kernel itself and not on a wrapper.
62 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Truescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/True().(t, isASCIIAVX2scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/isASCIIAVX2().(buf), "isASCIIAVX2 len %d", n)
63 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, -1, firstNonASCIIAVX2scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/firstNonASCIIAVX2().(buf), "firstNonASCIIAVX2 len %d", n)
64 if hasPOPCNTscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/hasPOPCNT. {
65 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, 0, countNonASCIIAVX2scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/countNonASCIIAVX2().(buf), "countNonASCIIAVX2 len %d", n)
66 }
67 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, -1, memchr2AVX2scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/memchr2AVX2().(buf, 'z', 'y'), "memchr2AVX2 len %d", n)
68 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, -1, memchr3AVX2scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/memchr3AVX2().(buf, 'z', 'y', 'x'), "memchr3AVX2 len %d", n)
69 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, -1, memchrDigitAVX2scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/memchrDigitAVX2().(buf), "memchrDigitAVX2 len %d", n)
70 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, -1, memchrNotWordAVX2scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/memchrNotWordAVX2().(buf), "memchrNotWordAVX2 len %d", n)
71 for _, offset := range []int{1, 2, 8, 31, 32, 33} {
72 if offset >= n {
73 continue
74 }
75 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, -1, memchrPairAVX2scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/memchrPairAVX2().(buf, 'z', 'y', offset),
76 "memchrPairAVX2 len %d offset %d", n, offset)
77 }
78
79 // The word scan needs a haystack with no word characters, so it
80 // too has to walk to the end.
81 for i := range buf {
82 buf[i] = '.'
83 }
84 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, -1, memchrWordAVX2scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/memchrWordAVX2().(buf), "memchrWordAVX2 len %d", n)
85 }
86 }
87
88 // Test_CountNonASCIIAVX2_CountsExactly pins the counting kernel's tail
89 // handling: an over-read that happened to land on non-ASCII bytes would
90 // inflate the count rather than fault, which the guard test alone cannot
91 // distinguish from a correct scan of zero-filled slack.
92 func Test_CountNonASCIIAVX2_CountsExactlyscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/Test_CountNonASCIIAVX2_CountsExactly().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
93 if !Acceleratedscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/Accelerated().() || !hasPOPCNTscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/hasPOPCNT. {
94 t.Skipscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skip().("AVX2+POPCNT kernels not active on this CPU")
95 }
96 for n := range 400 {
97 buf := guardedTailscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/guardedTail().(t, max(n, 1))[:n]
98 want := 0
99 for i := range buf {
100 buf[i] = byte(i * 7)
101 if buf[i] >= 0x80 {
102 want++
103 }
104 }
105 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, want, countNonASCIIAVX2scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/countNonASCIIAVX2().(buf), "len %d", n)
106 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, want, CountNonASCIIscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/CountNonASCII().(buf), "dispatched, len %d", n)
107 }
108 }