Skip to content

reflect: sort.SliceStable sorts incorrectly on arm64 with less function created with reflect.MakeFunc and slice of sufficient length #57184

@jfhamlin

Description

@jfhamlin

What version of Go are you using (go version)?

$ go version
go version devel go1.20-e738a2f Thu Dec 8 23:06:18 2022 +0000 darwin/arm64

Does this issue reproduce with the latest release?

Yes (with gotip)

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/jfhamlin/Library/Caches/go-build"
GOENV="/Users/jfhamlin/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/jfhamlin/Projects/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/jfhamlin/Projects/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/jfhamlin/sdk/gotip"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/jfhamlin/sdk/gotip/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="devel go1.20-e738a2f Thu Dec 8 23:06:18 2022 +0000"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="0"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/mm/bhf4r1f11zq3fc1gckc5wt6h0000gn/T/go-build3493514532=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

The sort.SliceStable function fails to sort a slice correctly when using a comparison function created using reflect.MakeFunc. I've only reproduced the issue on Apple silicon and only when the length of the slice being sorted is greater than or equal to 261. The same program produced the correct result on amd64.

Go Playground Link: https://go.dev/play/p/HSez0_sewXy?v=gotip
Note that the bug does not reproduce in the playground, presumably because it runs on amd64, not arm64.

package main

import (
	"log"
	"reflect"
	"sort"
)

func main() {
	// 261 is the shortest slice length for which this fails
	const length = 261
	x := make([]int, length)
	for i := 0; i < length; i++ {
		x[i] = i
	}

	isLessStatic := func(i, j int) bool {
		return x[i] < x[j]
	}

	isLessReflect := reflect.MakeFunc(reflect.TypeOf(isLessStatic), func(args []reflect.Value) []reflect.Value {
		//return []reflect.Value{reflect.ValueOf(false)} // also fails
		return []reflect.Value{reflect.ValueOf(x[args[0].Int()] < x[args[1].Int()])}
	}).Interface().(func(i, j int) bool)

	_ = isLessReflect
	sort.SliceStable(x, isLessReflect)
	// sort.SliceStable(x, isLessStatic) // passes

	for i := 0; i < length-1; i++ {
		if x[i] >= x[i+1] {
			log.Fatalf("not sorted! (length=%v)\n%v\n", length, x)
		}
	}
}

What did you expect to see?

(i.e. no output)

What did you see instead?

2022/12/08 19:59:15 not sorted! (length=261)
[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 260 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259]
exit status 1

(the greatest value, 260, is moved to the wrong index)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions