json-iterator / go

A high-performance 100% compatible drop-in replacement of "encoding/json"
http://jsoniter.com/migrate-from-go-std.html
MIT License
13.42k stars 1.03k forks source link

SIGSEGV decoding 1.2 GB JSON file #612

Open ericlagergren opened 2 years ago

ericlagergren commented 2 years ago

darwin/arm64 on Go 1.18.

Using function to copy from one *os.File to another.

func prettyIter(w io.Writer, r io.Reader) error {
    json := jsoniter.ConfigCompatibleWithStandardLibrary
    d := json.NewDecoder(r)
    e := json.NewEncoder(w)
    e.SetIndent(" ", "  ")
    for {
        var v interface{}
        if err := d.Decode(&v); err != nil {
            if errors.Is(err, io.EOF) {
                break
            }
            return err
        }
        if err := e.Encode(v); err != nil {
            return err
        }
    }
    return nil
}
unexpected fault address 0x360808085a175a75
fatal error: fault
[signal SIGSEGV: segmentation violation code=0x2 addr=0x360808085a175a75 pc=0x104e1a5b0]

goroutine 1 [running]:
runtime.throw({0x104ea7de5?, 0xd0?})
    /usr/local/go/src/runtime/panic.go:992 +0x50 fp=0x14056c8d890 sp=0x14056c8d860 pc=0x104defb40
runtime.sigpanic()
    /usr/local/go/src/runtime/signal_unix.go:825 +0x1a4 fp=0x14056c8d8c0 sp=0x14056c8d890 pc=0x104e05c54
aeshashbody()
    /usr/local/go/src/runtime/asm_arm64.s:874 +0x2d0 fp=0x14056c8d8d0 sp=0x14056c8d8d0 pc=0x104e1a5b0
runtime.mapiternext(0x14000010180)
    /usr/local/go/src/runtime/map.go:934 +0x2c4 fp=0x14056c8d940 sp=0x14056c8d8d0 pc=0x104dcd5c4
runtime.mapiterinit(0x14056c8d9e8?, 0x104dcb020?, 0x141e5da7888?)
    /usr/local/go/src/runtime/map.go:861 +0x2f0 fp=0x14056c8d970 sp=0x14056c8d940 pc=0x104dcd2c0
reflect.mapiterinit(0x14056c8da68?, 0x104dcabb0?, 0x14056c8da98?)
    /usr/local/go/src/runtime/map.go:1373 +0x20 fp=0x14056c8d9a0 sp=0x14056c8d970 pc=0x104e181f0
github.com/modern-go/reflect2.(*UnsafeMapType).UnsafeIterate(...)
    /Users/eric/gopath/pkg/mod/github.com/modern-go/reflect2@v0.0.0-20180701023420-4b7aa43c6742/unsafe_map.go:112
github.com/json-iterator/go.(*sortKeysMapEncoder).Encode(0x141e5daca20, 0x1400000e078, 0x1400012c1e0)
    /Users/eric/gopath/pkg/mod/github.com/json-iterator/go@v1.1.9/reflect_map.go:291 +0x244 fp=0x14056c8db10 sp=0x14056c8d9a0 pc=0x104e8e674
github.com/json-iterator/go.(*onePtrEncoder).Encode(0x141e5daef20, 0x14000113d70, 0x14000010180?)
    /Users/eric/gopath/pkg/mod/github.com/json-iterator/go@v1.1.9/reflect.go:219 +0x8c fp=0x14056c8db50 sp=0x14056c8db10 pc=0x104e8728c
github.com/json-iterator/go.(*Stream).WriteVal(0x1400012c1e0, {0x104ee6c60, 0x14000113d70})
    /Users/eric/gopath/pkg/mod/github.com/json-iterator/go@v1.1.9/reflect.go:98 +0x174 fp=0x14056c8dbd0 sp=0x14056c8db50 pc=0x104e86654
github.com/json-iterator/go.(*dynamicEncoder).Encode(0x104ee11e0?, 0x141e5da7888?, 0x0?)
    /Users/eric/gopath/pkg/mod/github.com/json-iterator/go@v1.1.9/reflect_dynamic.go:15 +0x40 fp=0x14056c8dc00 sp=0x14056c8dbd0 pc=0x104e88a30
github.com/json-iterator/go.(*sliceEncoder).Encode(0x1400011e0a8, 0x1400011e030, 0x1400012c1e0)
    /Users/eric/gopath/pkg/mod/github.com/json-iterator/go@v1.1.9/reflect_slice.go:38 +0x2f4 fp=0x14056c8dcc0 sp=0x14056c8dc00 pc=0x104e942b4
github.com/json-iterator/go.(*Stream).WriteVal(0x1400012c1e0, {0x104edf1a0, 0x1400011e030})
    /Users/eric/gopath/pkg/mod/github.com/json-iterator/go@v1.1.9/reflect.go:98 +0x174 fp=0x14056c8dd40 sp=0x14056c8dcc0 pc=0x104e86654
github.com/json-iterator/go.(*Encoder).Encode(0x1400012a218, {0x104edf1a0?, 0x1400011e030?})
    /Users/eric/gopath/pkg/mod/github.com/json-iterator/go@v1.1.9/adapter.go:127 +0x34 fp=0x14056c8dd90 sp=0x14056c8dd40 pc=0x104e7c684
main.prettyIter({0x12bf19920, 0x14000110d10}, {0x104f086c8?, 0x1400012a208?})
    /Users/eric/git/pkg/cmd/pretty/main.go:112 +0xd4 fp=0x14056c8de00 sp=0x14056c8dd90 pc=0x104ea75c4
main.pretty({0x12bf19920?, 0x14000110d10?}, {0x104f086c8?, 0x1400012a208?})
    /Users/eric/git/pkg/cmd/pretty/main.go:75 +0x30 fp=0x14056c8de30 sp=0x14056c8de00 pc=0x104ea74b0
main.main1()
    /Users/eric/git/pkg/cmd/pretty/main.go:52 +0x340 fp=0x14056c8df30 sp=0x14056c8de30 pc=0x104ea7270
main.main()
    /Users/eric/git/pkg/cmd/pretty/main.go:18 +0x20 fp=0x14056c8df70 sp=0x14056c8df30 pc=0x104ea6ef0
runtime.main()
    /usr/local/go/src/runtime/proc.go:250 +0x250 fp=0x14056c8dfd0 sp=0x14056c8df70 pc=0x104df2080
runtime.goexit()
    /usr/local/go/src/runtime/asm_arm64.s:1259 +0x4 fp=0x14056c8dfd0 sp=0x14056c8dfd0 pc=0x104e1cf74
exit status 2
zenhack commented 2 years ago

Just hit this myself; looks like an upstream bug that was fixed here: https://github.com/modern-go/reflect2/commit/835802817d782825522f9d3fb740b00139f42e2, but version 1.1.9 has a version bound on reflect2 that allows versions without the bugfix. Upgrading to json-iterator 1.1.12 fixed the problem for me.