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

Most upvoted comments

I am currently prototyping https://github.com/golang/go/issues/71408#issuecomment-2612975288.

In this approach, normal Go programs will use newly introduced mapiterinit2 and mapiternext2, which behave the same as mapiterinit and mapiternext today. No extra allocation for normal Go programs.

mapiterinit and mapiternext will remain for the sole use of //go:linkname users. mapiterinit will 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 mapiterinit2 and mapiternext2 won’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 😛)

type Iter struct {
    key         unsafe.Pointer
    elem        unsafe.Pointer
    iter        *realIter
}

We could make mapiterinit, mapiternext, mapiterkey, mapiterelem operate 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:

type hiter struct {                       | type Iter struct {
        key         unsafe.Pointer        |     key         unsafe.Pointer
        value       unsafe.Pointer        |     elem        unsafe.Pointer
        t           unsafe.Pointer        |     typ         *abi.SwissMapType
        h           unsafe.Pointer        |     m           *Map
        buckets     unsafe.Pointer        |     tab         *table
        bptr        unsafe.Pointer        |     group.data  unsafe.Pointer
        overflow    *[]unsafe.Pointer     |     clearSeq    uint64
        oldoverflow *[]unsafe.Pointer     |     _unused     uint64 (64-bit) / struct{} (32-bit)
        startBucket uintptr               |     entryOffset uintptr
        offset      uint8                 |     dirOffset   uintptr
        wrapped     bool                  |
        B           uint8                 |
        i           uint8                 |
        bucket      uintptr               |     entryIdx    uint16
                                          |     globalDepth uint8
        checkBucket uintptr               |     dirIdx      int
}   
  • I shrank entryIdx to uint16 as it is always <1024. This lets it colocate with globalDepth.
  • entryOffset and dirOffset are now uintptr, as we don’t need 64-bits of randomness on 32-bit platforms.
  • All of the pointers in Iter are now aligned with pointers in hiter.
  • All of the non-pointers in Iter are aligned with non-pointers in hiter except clearSeq.

clearSeq still overlaps with the pointer overflow (and oldoverflow on 32-bit). This is somewhat problematic. clearSeq is a count of calls to clear(). 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 clearSeq gets 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 clearSeq a pointer to the sequence counter. It is rarely used, so wouldn’t hurt performance.

we have 5 non-pointer words, but hiter only has 4.

I count 6 in Iter - entryOffset, dirOffset, clearSeq, globalDepth, dirIdx, entryIdx.

Release blocker until we make a decision.

I opened a new issue against reflect2 here: https://github.com/modern-go/reflect2/issues/32

Not 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 reflect2 is quite popular, so if there was something simple we could do I would consider it.