go: runtime: jsoniter stack corruption with 32-bit arches, potential GC issues with 64-bit arches
Go version
go1.24rc2 linux/amd64
Output of go env in your module/workspace:
AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='0'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GO386='sse2'
GOARCH='386'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/bryan/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/bryan/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m32 -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1397494606=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/tmp/jsoniter/go.mod'
GOMODCACHE='/home/bryan/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/bryan'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/bryan/pkg/mod/golang.org/toolchain@v0.0.1-go1.24rc2.linux-amd64'
GOSUMDB='sum.golang.org'
GOTELEMETRY='on'
GOTELEMETRYDIR='/home/bryan/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/bryan/pkg/mod/golang.org/toolchain@v0.0.1-go1.24rc2.linux-amd64/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.24rc2'
GOWORK=''
PKG_CONFIG='pkg-config'
What did you do?
When GOARCH was set to 386, and trying out the release candidates of Go 1.24, a unit test failed in the project Prometheus: https://github.com/prometheus/prometheus/actions/runs/12932314085/job/36070245239?pr=15707
I have simplified to just one use of the ‘jsoniter’ library. https://go.dev/play/p/gtfdrAhV48r?v=gotip
(That link gets a different error: could not parse netrc (GOAUTH=netrc): $HOME is not defined, so I’ll supply files too.)
go.mod go.sum jsoniter_test.go
What did you see happen?
$ GOARCH=386 go test
--- FAIL: TestJsonIter (0.00s)
jsoniter_test.go:21: Result too short (28): "{\"item1\":[{\"type\":\"gauge\"}]}"
FAIL
exit status 1
FAIL v1 0.001s
What did you expect to see?
The resulting JSON should be about 57 bytes long, with two items in it. The test passes with GOARCH=amd64 and Go 1.24rc2, also with GOARCH=386 on Go 1.23.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 23 (23 by maintainers)
Commits related to this issue
- runtime: rename mapiterinit and mapiternext mapiterinit allows external linkname. These users must allocate their own iter struct for initialization by mapiterinit. Since the type is unexported, they... — committed to golang/go by prattmic 2 years ago
- runtime: mapiter linkname compatibility layer This CL reintroduces the various mapiter* linkname functions with a compatibility layer that is careful to maintain compatibility with users of the linkn... — committed to tangzixuan/go by prattmic 2 years ago
I am currently prototyping https://github.com/golang/go/issues/71408#issuecomment-2612975288.
In this approach, normal Go programs will use newly introduced
mapiterinit2andmapiternext2, which behave the same asmapiterinitandmapiternexttoday. No extra allocation for normal Go programs.mapiterinitandmapiternextwill remain for the sole use of//go:linknameusers.mapiterinitwill have an extra allocation vs today.Personally, I think that is a good compromise. No impact on normal Go programs, maintains correctness for users of linkname, and doesn’t require any migration.
It’s true that linkname users will take a small performance hit. I think that is an acceptable trade-off for maintainability. It’s true that there is a question of whether these users will try to work around this compatibility layer. It will be more difficult though, as
mapiterinit2andmapiternext2won’t allow linkname.In my opinion we should not introduce an extra allocation for all programs in order to support the reflect2 package. That is carrying compatibility too far.
(Sorry for using this issue as brainstorming board 😛)
We could make
mapiterinit,mapiternext,mapiterkey,mapiterelemoperate on this wrapper type and introduce new functions used by compiler/std that operate on the real iteration type. Then only linkname users will take the indirection performance hit.Here’s what I’ve got so far:
entryIdxto uint16 as it is always <1024. This lets it colocate withglobalDepth.entryOffsetanddirOffsetare nowuintptr, as we don’t need 64-bits of randomness on 32-bit platforms.hiter.hiterexceptclearSeq.clearSeqstill overlaps with the pointeroverflow(andoldoverflowon 32-bit). This is somewhat problematic.clearSeqis a count of calls toclear(). It will almost always be quite small, but can technically get arbitrarily large.We could consider this acceptable. With all of the pointers in Iter covered, we won’t get memory corruption. If
clearSeqgets too high, applications might get spurious “found pointer to free object” throws, but they should be rare.FYI Prometheus unit tests pass on all architectures with 1.24RC3. Thanks!
We could also mix these ideas, and make
clearSeqa pointer to the sequence counter. It is rarely used, so wouldn’t hurt performance.I count 6 in
Iter-entryOffset,dirOffset,clearSeq,globalDepth,dirIdx,entryIdx.Release blocker until we make a decision.
I opened a new issue against
reflect2here: https://github.com/modern-go/reflect2/issues/32Not sure what if anything the Go project should do about this. I’ll leave this issue open in case anyone has some suggestions, but I’m inclined to say “tough luck”. But
reflect2is quite popular, so if there was something simple we could do I would consider it.