Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions internal/kernels/bin32.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ const (
// operation, so the result is the scalar loop's bit for bit.
func Bin32(op int, dst, a, b []float32) {
n := len(dst)
// By length, not by reslice: a[:n] is checked against cap(a), so a short
// operand with spare capacity would be over-read (#130).
if len(a) < n || len(b) < n {
panic("kernels: Bin32 operand shorter than dst")
}
if n == 0 {
return
}
Expand All @@ -26,6 +31,9 @@ func Bin32(op int, dst, a, b []float32) {
// bin32Go is the scalar oracle the kernels are tested against, and the
// implementation where there is no kernel.
func bin32Go(op int, dst, a, b []float32) {
if len(a) < len(dst) || len(b) < len(dst) {
panic("kernels: Bin32 operand shorter than dst")
}
a, b = a[:len(dst)], b[:len(dst)]
switch op {
case Add32:
Expand Down Expand Up @@ -56,6 +64,9 @@ func bin32Go(op int, dst, a, b []float32) {
// twice float32's precision.
func Sqrt32(dst, src []float32) {
n := len(dst)
if len(src) < n { // by length: src[:n] is checked against cap (#130)
panic("kernels: Sqrt32 src shorter than dst")
}
if n == 0 {
return
}
Expand All @@ -65,6 +76,9 @@ func Sqrt32(dst, src []float32) {
// sqrt32Go is the scalar oracle for Sqrt32, and the implementation where
// there is no kernel.
func sqrt32Go(dst, src []float32) {
if len(src) < len(dst) {
panic("kernels: Sqrt32 src shorter than dst")
}
src = src[:len(dst)]
for i := range dst {
dst[i] = float32(math.Sqrt(float64(src[i])))
Expand Down
38 changes: 38 additions & 0 deletions internal/kernels/bin32_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kernels

import (
"fmt"
"math"
"testing"
)
Expand Down Expand Up @@ -115,6 +116,43 @@ func TestBin32RefusesBadInput(t *testing.T) {
}
}

// TestShortOperandWithSpareCapacityIsRefused is #130: a reslice x[:n] is
// checked against cap(x), not len(x), so an operand of length 3 and capacity 8
// passed with a dst of length 4 used to be read one element past its length.
// Every entry point must refuse it by length. The hidden element is a NaN so
// an over-read that slipped through would also show in the result.
func TestShortOperandWithSpareCapacityIsRefused(t *testing.T) {
short := func() []float32 {
s := make([]float32, 8)
for i := range s {
s[i] = float32(i + 1)
}
s[3] = float32(math.NaN())
return s[:3] // len 3, cap 8
}
full := func() []float32 { return []float32{1, 2, 3, 4} }
cases := map[string]func(){}
for op := Add32; op <= Div32; op++ {
op := op
cases[fmt.Sprintf("Bin32 op %d short a", op)] = func() { Bin32(op, make([]float32, 4), short(), full()) }
cases[fmt.Sprintf("Bin32 op %d short b", op)] = func() { Bin32(op, make([]float32, 4), full(), short()) }
cases[fmt.Sprintf("bin32Go op %d short a", op)] = func() { bin32Go(op, make([]float32, 4), short(), full()) }
cases[fmt.Sprintf("bin32Go op %d short b", op)] = func() { bin32Go(op, make([]float32, 4), full(), short()) }
}
cases["Sqrt32 short src"] = func() { Sqrt32(make([]float32, 4), short()) }
cases["sqrt32Go short src"] = func() { sqrt32Go(make([]float32, 4), short()) }
for name, f := range cases {
func() {
defer func() {
if recover() == nil {
t.Errorf("%s: a short operand with spare capacity was accepted", name)
}
}()
f()
}()
}
}

// TestBin32OperandsLongerThanDst pins the contract the floats kernels need: they
// process len(a) elements and panic on a shorter operand, so Bin32 and Sqrt32
// must hand them operands of exactly len(dst) -- longer inputs are cut, shorter
Expand Down
Loading