Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

found bad pointer in Go heap error #63

Closed
zchee opened this issue Nov 23, 2020 · 2 comments
Closed

found bad pointer in Go heap error #63

zchee opened this issue Nov 23, 2020 · 2 comments

Comments

@zchee
Copy link
Contributor

zchee commented Nov 23, 2020

Currently, the encoder sometimes gives the found bad pointer in Go heap or found pointer to free object error.
It may be related to garbage collection.


Reproduce:

  • GCGC=1 is required. apply GC pressure to the test
  • -gcflags='all=-N -l' is not required. If omitted, that error happens about once in 10 times
$ GOGC=1 go1.15.5 test -v -count 1 -run=. -gcflags='all=-N -l' .
.
.
.
runtime: pointer 0xc0002abdc8 to unused region of span span.base()=0xc00022c000 span.limit=0xc000232000 span.state=1
runtime: found in object at *(0xc000103140+0x18)
object=0xc000103140 s.base()=0xc000102000 s.limit=0xc000103fe0 s.spanclass=14 s.elemsize=96 s.state=mSpanInUse
 *(object+0) = 0x0
 *(object+8) = 0x0
 *(object+16) = 0x127d000
=== RUN   Test_MarshalJSON
 *(object+24) = 0xc0002abdc8 <==
 *(object+32) = 0xc0002abdf8
 *(object+40) = 0xc0002abdf8
 *(object+48) = 0x0=== RUN   Test_MarshalJSON/*struct

 *(object+56) = 0x0
 *(object+64) = 0x0
 *(object+72) = 0x6000106
 *(object+80) = 0x0
 *(object+88) = 0xffffffffffffffff
fatal error: found bad pointer in Go heap (incorrect use of unsafe or cgo?)

runtime stack:
runtime.throw(0x12c1de1, 0x3e)
	/Users/zchee/sdk/go1.15.5/src/runtime/panic.go:1116 +0x72 fp=0x70000d27dd98 sp=0x70000d27dd68 pc=0x103ccb2
runtime.badPointer(0x8a7b810, 0xc0002abdc8, 0xc000103140, 0x18)
	/Users/zchee/sdk/go1.15.5/src/runtime/mbitmap.go:380 +0x255 fp=0x70000d27dde0 sp=0x70000d27dd98 pc=0x1018375
runtime.findObject(0xc0002abdc8, 0xc000103140, 0x18, 0x1b, 0x20300000000003, 0x89fffff)
	/Users/zchee/sdk/go1.15.5/src/runtime/mbitmap.go:416 +0xb5 fp=0x70000d27de30 sp=0x70000d27dde0 pc=0x1018455
runtime.scanobject(0xc000103140, 0xc000037698)
	/Users/zchee/sdk/go1.15.5/src/runtime/mgcmark.go:1385 +0x17c fp=0x70000d27dec0 sp=0x70000d27de30 pc=0x102487c
runtime.gcDrain(0xc000037698, 0x2)
	/Users/zchee/sdk/go1.15.5/src/runtime/mgcmark.go:1143 +0x225 fp=0x70000d27df18 sp=0x70000d27dec0 pc=0x1024165
runtime.gcBgMarkWorker.func2()
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1977 +0xef fp=0x70000d27df58 sp=0x70000d27df18 pc=0x106d06f
runtime.systemstack(0x70000d27df70)
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:370 +0x66 fp=0x70000d27df60 sp=0x70000d27df58 pc=0x10742c6
runtime.mstart()
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:1116 fp=0x70000d27df68 sp=0x70000d27df60 pc=0x1041ee0

goroutine 108 [GC worker (idle)]:
runtime.systemstack_switch()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:330 fp=0xc00005ef58 sp=0xc00005ef50 pc=0x1074240
runtime.gcBgMarkWorker(0xc000036000)
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1945 +0x1eb fp=0xc00005efd8 sp=0xc00005ef58 pc=0x1020bcb
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc00005efe0 sp=0xc00005efd8 pc=0x1075e61
created by runtime.gcBgMarkStartWorkers
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1839 +0x77

goroutine 1 [chan receive]:
runtime.gopark(0x12c47f8, 0xc00039e0b8, 0xc00013170e, 0x2)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc000135788 sp=0xc000135758 pc=0x103f915
runtime.chanrecv(0xc00039e060, 0xc00013588e, 0xc000000101, 0x111e768)
	/Users/zchee/sdk/go1.15.5/src/runtime/chan.go:577 +0x286 fp=0xc000135818 sp=0xc000135788 pc=0x10069c6
runtime.chanrecv1(0xc00039e060, 0xc00013588e)
	/Users/zchee/sdk/go1.15.5/src/runtime/chan.go:439 +0x2b fp=0xc000135848 sp=0xc000135818 pc=0x100672b
testing.(*T).Run(0xc000382300, 0x12b2678, 0x10, 0x12c4578, 0x5fbb4900)
	/Users/zchee/sdk/go1.15.5/src/testing/testing.go:1169 +0x676 fp=0xc000135980 sp=0xc000135848 pc=0x111e796
testing.runTests.func1(0xc000104180)
	/Users/zchee/sdk/go1.15.5/src/testing/testing.go:1439 +0xde fp=0xc000135a20 sp=0xc000135980 pc=0x112553e
testing.tRunner(0xc000104180, 0xc000135c00)
	/Users/zchee/sdk/go1.15.5/src/testing/testing.go:1123 +0x1a3 fp=0xc000135b18 sp=0xc000135a20 pc=0x111e0a3
testing.runTests(0xc00011aa80, 0x13d4de0, 0x34, 0x34, 0xbfe6f0bf02c532b0, 0x8bb2d532a3, 0x13da8c0, 0xc000130100)
	/Users/zchee/sdk/go1.15.5/src/testing/testing.go:1437 +0x465 fp=0xc000135c30 sp=0xc000135b18 pc=0x1120c85
testing.(*M).Run(0xc00016c200, 0x0)
	/Users/zchee/sdk/go1.15.5/src/testing/testing.go:1345 +0x507 fp=0xc000135f08 sp=0xc000135c30 pc=0x111f267
main.main()
	_testmain.go:175 +0xc5 fp=0xc000135f88 sp=0xc000135f08 pc=0x1259665
runtime.main()
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:204 +0x1cf fp=0xc000135fe0 sp=0xc000135f88 pc=0x103f4ef
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc000135fe8 sp=0xc000135fe0 pc=0x1075e61

goroutine 2 [force gc (idle)]:
runtime.gopark(0x12c4a30, 0x13da4c0, 0x13d1411, 0x1)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc000062f88 sp=0xc000062f58 pc=0x103f915
runtime.goparkunlock(0x13da4c0, 0x1411, 0x1)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:312 +0x53 fp=0xc000062fb8 sp=0xc000062f88 pc=0x103f9d3
runtime.forcegchelper()
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:255 +0xc5 fp=0xc000062fe0 sp=0xc000062fb8 pc=0x103f785
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc000062fe8 sp=0xc000062fe0 pc=0x1075e61
created by runtime.init.6
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:243 +0x35

goroutine 3 [GC sweep wait]:
runtime.gopark(0x12c4a30, 0x13da620, 0x102140c, 0x1)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc000063780 sp=0xc000063750 pc=0x103f915
runtime.goparkunlock(0x13da620, 0x12e140c, 0x1)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:312 +0x53 fp=0xc0000637b0 sp=0xc000063780 pc=0x103f9d3
runtime.bgsweep(0xc0000244d0)
	/Users/zchee/sdk/go1.15.5/src/runtime/mgcsweep.go:163 +0xa8 fp=0xc0000637d8 sp=0xc0000637b0 pc=0x1028268
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc0000637e0 sp=0xc0000637d8 pc=0x1075e61
created by runtime.gcenable
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:217 +0x5c

goroutine 4 [GC scavenge wait]:
runtime.gopark(0x12c4a30, 0x13da800, 0x13d140d, 0x1)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc000063f50 sp=0xc000063f20 pc=0x103f915
runtime.goparkunlock(0x13da800, 0x12e140d, 0x1)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:312 +0x53 fp=0xc000063f80 sp=0xc000063f50 pc=0x103f9d3
runtime.bgscavenge(0xc0000244d0)
	/Users/zchee/sdk/go1.15.5/src/runtime/mgcscavenge.go:314 +0x2a5 fp=0xc000063fd8 sp=0xc000063f80 pc=0x10262e5
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc000063fe0 sp=0xc000063fd8 pc=0x1075e61
created by runtime.gcenable
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:218 +0x7e

goroutine 17 [finalizer wait]:
runtime.gopark(0x12c4a30, 0x140a2c8, 0x1401410, 0x1)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc00005e728 sp=0xc00005e6f8 pc=0x103f915
runtime.goparkunlock(0x140a2c8, 0x1410, 0x1)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:312 +0x53 fp=0xc00005e758 sp=0xc00005e728 pc=0x103f9d3
runtime.runfinq()
	/Users/zchee/sdk/go1.15.5/src/runtime/mfinal.go:175 +0x96 fp=0xc00005e7e0 sp=0xc00005e758 pc=0x101d156
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc00005e7e8 sp=0xc00005e7e0 pc=0x1075e61
created by runtime.createfing
	/Users/zchee/sdk/go1.15.5/src/runtime/mfinal.go:156 +0x65

goroutine 109 [GC worker (idle)]:
runtime.gopark(0x12c48a0, 0xc0002fae10, 0x1418, 0x0)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc00005f758 sp=0xc00005f728 pc=0x103f915
runtime.gcBgMarkWorker(0xc000042800)
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1891 +0x11f fp=0xc00005f7d8 sp=0xc00005f758 pc=0x1020aff
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc00005f7e0 sp=0xc00005f7d8 pc=0x1075e61
created by runtime.gcBgMarkStartWorkers
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1839 +0x77

goroutine 110 [GC worker (idle)]:
runtime.gopark(0x12c48a0, 0xc00001a260, 0x1418, 0x0)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc00005ff58 sp=0xc00005ff28 pc=0x103f915
runtime.gcBgMarkWorker(0xc000045000)
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1891 +0x11f fp=0xc00005ffd8 sp=0xc00005ff58 pc=0x1020aff
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc00005ffe0 sp=0xc00005ffd8 pc=0x1075e61
created by runtime.gcBgMarkStartWorkers
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1839 +0x77

goroutine 111 [GC worker (idle)]:
runtime.gopark(0x12c48a0, 0xc00001a280, 0x1418, 0x0)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc000060758 sp=0xc000060728 pc=0x103f915
runtime.gcBgMarkWorker(0xc00004c800)
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1891 +0x11f fp=0xc0000607d8 sp=0xc000060758 pc=0x1020aff
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc0000607e0 sp=0xc0000607d8 pc=0x1075e61
created by runtime.gcBgMarkStartWorkers
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1839 +0x77

goroutine 5 [GC worker (idle)]:
runtime.gopark(0x12c48a0, 0xc00001a250, 0x1418, 0x0)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc000064758 sp=0xc000064728 pc=0x103f915
runtime.gcBgMarkWorker(0xc000038800)
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1891 +0x11f fp=0xc0000647d8 sp=0xc000064758 pc=0x1020aff
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc0000647e0 sp=0xc0000647d8 pc=0x1075e61
created by runtime.gcBgMarkStartWorkers
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1839 +0x77

goroutine 6 [GC worker (idle)]:
runtime.gopark(0x12c48a0, 0xc0000ac000, 0x1418, 0x0)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc000064f58 sp=0xc000064f28 pc=0x103f915
runtime.gcBgMarkWorker(0xc00003b000)
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1891 +0x11f fp=0xc000064fd8 sp=0xc000064f58 pc=0x1020aff
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc000064fe0 sp=0xc000064fd8 pc=0x1075e61
created by runtime.gcBgMarkStartWorkers
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1839 +0x77

goroutine 113 [GC worker (idle)]:
runtime.gopark(0x12c48a0, 0xc0000ac010, 0xc000091418, 0x0)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc000062758 sp=0xc000062728 pc=0x103f915
runtime.gcBgMarkWorker(0xc00003d800)
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1891 +0x11f fp=0xc0000627d8 sp=0xc000062758 pc=0x1020aff
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc0000627e0 sp=0xc0000627d8 pc=0x1075e61
created by runtime.gcBgMarkStartWorkers
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1839 +0x77

goroutine 114 [GC worker (idle)]:
runtime.gopark(0x12c48a0, 0xc0002fae00, 0x1418, 0x0)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc0000b2758 sp=0xc0000b2728 pc=0x103f915
runtime.gcBgMarkWorker(0xc000040000)
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1891 +0x11f fp=0xc0000b27d8 sp=0xc0000b2758 pc=0x1020aff
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc0000b27e0 sp=0xc0000b27d8 pc=0x1075e61
created by runtime.gcBgMarkStartWorkers
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1839 +0x77

goroutine 115 [GC worker (idle)]:
runtime.gopark(0x12c48a0, 0xc0000ac020, 0x1418, 0x0)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc0000b2f58 sp=0xc0000b2f28 pc=0x103f915
runtime.gcBgMarkWorker(0xc000047800)
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1891 +0x11f fp=0xc0000b2fd8 sp=0xc0000b2f58 pc=0x1020aff
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc0000b2fe0 sp=0xc0000b2fd8 pc=0x1075e61
created by runtime.gcBgMarkStartWorkers
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1839 +0x77

goroutine 116 [GC worker (idle)]:
runtime.gopark(0x12c48a0, 0xc00001a270, 0x1418, 0x0)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc0000b3758 sp=0xc0000b3728 pc=0x103f915
runtime.gcBgMarkWorker(0xc00004a000)
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1891 +0x11f fp=0xc0000b37d8 sp=0xc0000b3758 pc=0x1020aff
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc0000b37e0 sp=0xc0000b37d8 pc=0x1075e61
created by runtime.gcBgMarkStartWorkers
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1839 +0x77

goroutine 117 [GC worker (idle)]:
runtime.gopark(0x12c48a0, 0xc0000ac030, 0x1418, 0x0)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc0000b3f58 sp=0xc0000b3f28 pc=0x103f915
runtime.gcBgMarkWorker(0xc00004f000)
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1891 +0x11f fp=0xc0000b3fd8 sp=0xc0000b3f58 pc=0x1020aff
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc0000b3fe0 sp=0xc0000b3fd8 pc=0x1075e61
created by runtime.gcBgMarkStartWorkers
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1839 +0x77

goroutine 118 [GC worker (idle)]:
runtime.gopark(0x12c48a0, 0xc0000ac040, 0x1418, 0x0)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc0000b4758 sp=0xc0000b4728 pc=0x103f915
runtime.gcBgMarkWorker(0xc000051800)
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1891 +0x11f fp=0xc0000b47d8 sp=0xc0000b4758 pc=0x1020aff
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc0000b47e0 sp=0xc0000b47d8 pc=0x1075e61
created by runtime.gcBgMarkStartWorkers
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1839 +0x77

goroutine 7 [GC worker (idle)]:
runtime.gopark(0x12c48a0, 0xc0000ac050, 0x1418, 0x0)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc000065758 sp=0xc000065728 pc=0x103f915
runtime.gcBgMarkWorker(0xc000054000)
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1891 +0x11f fp=0xc0000657d8 sp=0xc000065758 pc=0x1020aff
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc0000657e0 sp=0xc0000657d8 pc=0x1075e61
created by runtime.gcBgMarkStartWorkers
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1839 +0x77

goroutine 112 [GC worker (idle)]:
runtime.gopark(0x12c48a0, 0xc0002fae20, 0x1418, 0x0)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc000060f58 sp=0xc000060f28 pc=0x103f915
runtime.gcBgMarkWorker(0xc000056800)
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1891 +0x11f fp=0xc000060fd8 sp=0xc000060f58 pc=0x1020aff
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc000060fe0 sp=0xc000060fd8 pc=0x1075e61
created by runtime.gcBgMarkStartWorkers
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1839 +0x77

goroutine 129 [GC worker (idle)]:
runtime.gopark(0x12c48a0, 0xc0000ac060, 0x1418, 0x0)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc000061758 sp=0xc000061728 pc=0x103f915
runtime.gcBgMarkWorker(0xc000059000)
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1891 +0x11f fp=0xc0000617d8 sp=0xc000061758 pc=0x1020aff
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc0000617e0 sp=0xc0000617d8 pc=0x1075e61
created by runtime.gcBgMarkStartWorkers
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1839 +0x77

goroutine 130 [GC worker (idle)]:
runtime.gopark(0x12c48a0, 0xc0002fae30, 0x1418, 0x0)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc000061f58 sp=0xc000061f28 pc=0x103f915
runtime.gcBgMarkWorker(0xc00005b800)
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1891 +0x11f fp=0xc000061fd8 sp=0xc000061f58 pc=0x1020aff
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc000061fe0 sp=0xc000061fd8 pc=0x1075e61
created by runtime.gcBgMarkStartWorkers
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:1839 +0x77

goroutine 145 [chan receive]:
runtime.gopark(0x12c47f8, 0xc00039e178, 0xc0003a170e, 0x2)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc0003a6ca8 sp=0xc0003a6c78 pc=0x103f915
runtime.chanrecv(0xc00039e120, 0xc0003a6dae, 0xc000382401, 0x111e768)
	/Users/zchee/sdk/go1.15.5/src/runtime/chan.go:577 +0x286 fp=0xc0003a6d38 sp=0xc0003a6ca8 pc=0x10069c6
runtime.chanrecv1(0xc00039e120, 0xc0003a6dae)
	/Users/zchee/sdk/go1.15.5/src/runtime/chan.go:439 +0x2b fp=0xc0003a6d68 sp=0xc0003a6d38 pc=0x100672b
testing.(*T).Run(0xc000382600, 0x12afb4b, 0x7, 0x12c4568, 0x0)
	/Users/zchee/sdk/go1.15.5/src/testing/testing.go:1169 +0x676 fp=0xc0003a6ea0 sp=0xc0003a6d68 pc=0x111e796
github.com/goccy/go-json_test.Test_MarshalJSON(0xc000382300)
	/Users/zchee/go/src/github.com/goccy/go-json/encode_test.go:419 +0x4c fp=0xc0003a6ed8 sp=0xc0003a6ea0 pc=0x122108c
testing.tRunner(0xc000382300, 0x12c4578)
	/Users/zchee/sdk/go1.15.5/src/testing/testing.go:1123 +0x1a3 fp=0xc0003a6fd0 sp=0xc0003a6ed8 pc=0x111e0a3
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc0003a6fd8 sp=0xc0003a6fd0 pc=0x1075e61
created by testing.(*T).Run
	/Users/zchee/sdk/go1.15.5/src/testing/testing.go:1168 +0x648

goroutine 146 [runnable]:
runtime.newobject(0x1286140, 0xc0003b6000)
	/Users/zchee/sdk/go1.15.5/src/runtime/malloc.go:1194 +0x51 fp=0xc0003a79d0 sp=0xc0003a79c8 pc=0x100ef71
github.com/goccy/go-json.(*Encoder).encode(0xc00016b340, 0x127aa20, 0xc0003a7e4f, 0x0, 0x0)
	/Users/zchee/go/src/github.com/goccy/go-json/encode.go:219 +0x568 fp=0xc0003a7b50 sp=0xc0003a79d0 pc=0x11946a8
github.com/goccy/go-json.(*Encoder).encodeForMarshal(0xc00016b340, 0x127aa20, 0xc0003a7e4f, 0x0, 0x0, 0x0, 0x0, 0x0)
	/Users/zchee/go/src/github.com/goccy/go-json/encode.go:151 +0x7f fp=0xc0003a7c60 sp=0xc0003a7b50 pc=0x1193dbf
github.com/goccy/go-json.MarshalWithOption(0x127aa20, 0xc0003a7e4f, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
	/Users/zchee/go/src/github.com/goccy/go-json/json.go:169 +0x1cf fp=0xc0003a7d60 sp=0xc0003a7c60 pc=0x120f14f
github.com/goccy/go-json.Marshal(0x127aa20, 0xc0003a7e4f, 0x0, 0x0, 0x0, 0x0, 0x0)
	/Users/zchee/go/src/github.com/goccy/go-json/json.go:157 +0x99 fp=0xc0003a7e10 sp=0xc0003a7d60 pc=0x120ee99
github.com/goccy/go-json_test.Test_MarshalJSON.func1(0xc000382600)
	/Users/zchee/go/src/github.com/goccy/go-json/encode_test.go:420 +0x77 fp=0xc0003a7ed8 sp=0xc0003a7e10 pc=0x1248477
testing.tRunner(0xc000382600, 0x12c4568)
	/Users/zchee/sdk/go1.15.5/src/testing/testing.go:1123 +0x1a3 fp=0xc0003a7fd0 sp=0xc0003a7ed8 pc=0x111e0a3
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc0003a7fd8 sp=0xc0003a7fd0 pc=0x1075e61
created by testing.(*T).Run
	/Users/zchee/sdk/go1.15.5/src/testing/testing.go:1168 +0x648
FAIL	github.com/goccy/go-json	0.043s
FAIL
$ GOGC=1 go1.15.5 test -v -race -count 1 -run=. -gcflags='all=-N -l' .
.
.
.
=== RUN   TestMarshalAllValue
runtime: pointer 0xc0001edc30 to unallocated span span.base()=0xc0001e6000 span.limit=0xc0001ee000 span.state=0
fatal error: found bad pointer in Go heap (incorrect use of unsafe or cgo?)

goroutine 62 [running]:
runtime.throw(0x14533e4, 0x3e)
	/Users/zchee/sdk/go1.15.5/src/runtime/panic.go:1116 +0x72 fp=0xc000289928 sp=0xc0002898f8 pc=0x107e432
runtime.badPointer(0x22c1fe8, 0xc0001edc30, 0x0, 0x0)
	/Users/zchee/sdk/go1.15.5/src/runtime/mbitmap.go:380 +0x255 fp=0xc000289970 sp=0xc000289928 pc=0x1059a75
runtime.findObject(0xc0001edc30, 0x0, 0x0, 0x14, 0x117297c, 0x1)
	/Users/zchee/sdk/go1.15.5/src/runtime/mbitmap.go:416 +0xb5 fp=0xc0002899c0 sp=0xc000289970 pc=0x1059b55
runtime.checkptrBase(0xc0001edc30, 0xc000289a78)
	/Users/zchee/sdk/go1.15.5/src/runtime/checkptr.go:68 +0x4f fp=0xc000289a00 sp=0xc0002899c0 pc=0x104820f
runtime.checkptrAlignment(0xc0001edc30, 0x13fd620, 0x1)
	/Users/zchee/sdk/go1.15.5/src/runtime/checkptr.go:19 +0x73 fp=0xc000289a38 sp=0xc000289a00 pc=0x1048033
reflect.Value.IsNil(0x1404300, 0xc0001edc30, 0x94, 0xc0001edc00)
	/Users/zchee/sdk/go1.15.5/src/reflect/value.go:1084 +0x26d fp=0xc000289b08 sp=0xc000289a38 pc=0x116e2ad
github.com/goccy/go-json.(*Encoder).run(0xc0001272d0, 0xc00020cde0, 0xc00022ba40, 0x0, 0x0)
	/Users/zchee/go/src/github.com/goccy/go-json/encode_vm.go:221 +0x89b fp=0xc00029efe8 sp=0xc000289b08 pc=0x12f21fb
github.com/goccy/go-json.(*Encoder).encode(0xc0001272d0, 0x143f280, 0xc00029fa38, 0x0, 0x0)
	/Users/zchee/go/src/github.com/goccy/go-json/encode.go:239 +0xc4a fp=0xc00029f338 sp=0xc00029efe8 pc=0x12ad34a
github.com/goccy/go-json.(*Encoder).encodeForMarshal(0xc0001272d0, 0x143f280, 0xc0000cfa38, 0x0, 0x0, 0x0, 0x0, 0x0)
	/Users/zchee/go/src/github.com/goccy/go-json/encode.go:151 +0xb5 fp=0xc00029f4d0 sp=0xc00029f338 pc=0x12ac275
github.com/goccy/go-json.MarshalWithOption(0x143f280, 0xc0000cfa38, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
	/Users/zchee/go/src/github.com/goccy/go-json/json.go:169 +0x265 fp=0xc00029f628 sp=0xc00029f4d0 pc=0x136d145
github.com/goccy/go-json.Marshal(0x143f280, 0xc000072a38, 0x0, 0x0, 0x0, 0x0, 0x0)
	/Users/zchee/go/src/github.com/goccy/go-json/json.go:157 +0xe5 fp=0xc00029f708 sp=0xc00029f628 pc=0x136cde5
github.com/goccy/go-json_test.TestMarshalAllValue(0xc0001c0f00)
	/Users/zchee/go/src/github.com/goccy/go-json/decode_test.go:1689 +0xe5 fp=0xc00029fe78 sp=0xc00029f708 pc=0x1386445
testing.tRunner(0xc0001c0f00, 0x14558a8)
	/Users/zchee/sdk/go1.15.5/src/testing/testing.go:1123 +0x2a5 fp=0xc00029ffd0 sp=0xc00029fe78 pc=0x11eb305
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc00029ffd8 sp=0xc00029ffd0 pc=0x10b99a1
created by testing.(*T).Run
	/Users/zchee/sdk/go1.15.5/src/testing/testing.go:1168 +0xb07

goroutine 1 [chan receive]:
runtime.gopark(0x1455ea0, 0xc0001d0478, 0x11e170e, 0x2)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc0000f5290 sp=0xc0000f5260 pc=0x1081095
runtime.chanrecv(0xc0001d0420, 0xc0000f5392, 0xc000000101, 0x11ebee7)
	/Users/zchee/sdk/go1.15.5/src/runtime/chan.go:577 +0x246 fp=0xc0000f5320 sp=0xc0000f5290 pc=0x10475a6
runtime.chanrecv1(0xc0001d0420, 0xc0000f5392)
	/Users/zchee/sdk/go1.15.5/src/runtime/chan.go:439 +0x2b fp=0xc0000f5350 sp=0xc0000f5320 pc=0x104734b
testing.(*T).Run(0xc0001c0f00, 0x1444c67, 0x13, 0x14558a8, 0x315f500c5800)
	/Users/zchee/sdk/go1.15.5/src/testing/testing.go:1169 +0xb68 fp=0xc0000f55d0 sp=0xc0000f5350 pc=0x11ebf48
testing.runTests.func1(0xc00009aa80)
	/Users/zchee/sdk/go1.15.5/src/testing/testing.go:1439 +0x186 fp=0xc0000f56b0 sp=0xc0000f55d0 pc=0x11f7a66
testing.tRunner(0xc00009aa80, 0xc0000f5a10)
	/Users/zchee/sdk/go1.15.5/src/testing/testing.go:1123 +0x2a5 fp=0xc0000f5808 sp=0xc0000f56b0 pc=0x11eb305
testing.runTests(0xc0000dca80, 0x1738860, 0x34, 0x34, 0xbfe6f01e4f6a6170, 0x8bb2ee9638, 0x173e320, 0xc0000c0100)
	/Users/zchee/sdk/go1.15.5/src/testing/testing.go:1437 +0x7f1 fp=0xc0000f5a40 sp=0xc0000f5808 pc=0x11efc51
testing.(*M).Run(0xc000128200, 0x0)
	/Users/zchee/sdk/go1.15.5/src/testing/testing.go:1345 +0x905 fp=0xc0000f5e90 sp=0xc0000f5a40 pc=0x11ed305
main.main()
	_testmain.go:175 +0x1bd fp=0xc0000f5f88 sp=0xc0000f5e90 pc=0x13e269d
runtime.main()
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:204 +0x1cf fp=0xc0000f5fe0 sp=0xc0000f5f88 pc=0x1080c6f
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc0000f5fe8 sp=0xc0000f5fe0 pc=0x10b99a1

goroutine 2 [force gc (idle)]:
runtime.gopark(0x1456190, 0x173df10, 0x1731411, 0x1)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc000064f88 sp=0xc000064f58 pc=0x1081095
runtime.goparkunlock(0x173df10, 0x1411, 0x1)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:312 +0x53 fp=0xc000064fb8 sp=0xc000064f88 pc=0x1081153
runtime.forcegchelper()
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:255 +0xc5 fp=0xc000064fe0 sp=0xc000064fb8 pc=0x1080f05
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc000064fe8 sp=0xc000064fe0 pc=0x10b99a1
created by runtime.init.6
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:243 +0x35

goroutine 18 [GC sweep wait]:
runtime.gopark(0x1456190, 0x173e080, 0x106140c, 0x1)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc000060780 sp=0xc000060750 pc=0x1081095
runtime.goparkunlock(0x173e080, 0x15f140c, 0x1)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:312 +0x53 fp=0xc0000607b0 sp=0xc000060780 pc=0x1081153
runtime.bgsweep(0xc0000a6000)
	/Users/zchee/sdk/go1.15.5/src/runtime/mgcsweep.go:163 +0xa8 fp=0xc0000607d8 sp=0xc0000607b0 pc=0x1069968
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc0000607e0 sp=0xc0000607d8 pc=0x10b99a1
created by runtime.gcenable
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:217 +0x5c

goroutine 19 [GC scavenge wait]:
runtime.gopark(0x1456190, 0x173e260, 0x106140d, 0x1)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc000060f50 sp=0xc000060f20 pc=0x1081095
runtime.goparkunlock(0x173e260, 0x15f140d, 0x1)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:312 +0x53 fp=0xc000060f80 sp=0xc000060f50 pc=0x1081153
runtime.bgscavenge(0xc0000a6000)
	/Users/zchee/sdk/go1.15.5/src/runtime/mgcscavenge.go:265 +0xdc fp=0xc000060fd8 sp=0xc000060f80 pc=0x106781c
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc000060fe0 sp=0xc000060fd8 pc=0x10b99a1
created by runtime.gcenable
	/Users/zchee/sdk/go1.15.5/src/runtime/mgc.go:218 +0x7e

goroutine 20 [finalizer wait]:
runtime.gopark(0x1456190, 0x176dd28, 0x1761410, 0x1)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:306 +0xd5 fp=0xc000064728 sp=0xc0000646f8 pc=0x1081095
runtime.goparkunlock(0x176dd28, 0x1711410, 0x1)
	/Users/zchee/sdk/go1.15.5/src/runtime/proc.go:312 +0x53 fp=0xc000064758 sp=0xc000064728 pc=0x1081153
runtime.runfinq()
	/Users/zchee/sdk/go1.15.5/src/runtime/mfinal.go:175 +0x96 fp=0xc0000647e0 sp=0xc000064758 pc=0x105e856
runtime.goexit()
	/Users/zchee/sdk/go1.15.5/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc0000647e8 sp=0xc0000647e0 pc=0x10b99a1
created by runtime.createfing
	/Users/zchee/sdk/go1.15.5/src/runtime/mfinal.go:156 +0x65
FAIL	github.com/goccy/go-json	0.067s
FAIL

There are two workarounds, but both will increase allocation.

diff --git a/encode.go b/encode.go
index ca1cab1..a3ab000 100644
--- a/encode.go
+++ b/encode.go
@@ -183,6 +183,7 @@ func (e *Encoder) encode(v interface{}) error {
 			code = codeSet.code
 		}
 		ctx := codeSet.ctx.Get().(*encodeRuntimeContext)
+		ctx.keepRefs = append(ctx.keepRefs, header.ptr)
 		p := uintptr(header.ptr)
 		ctx.init(p)
 		err := e.run(ctx, code)
@@ -227,6 +228,7 @@ func (e *Encoder) encode(v interface{}) error {
 	cachedOpcode.set(typeptr, codeSet)
 	p := uintptr(header.ptr)
 	ctx := codeSet.ctx.Get().(*encodeRuntimeContext)
+	ctx.keepRefs = append(ctx.keepRefs, header.ptr)
 	ctx.init(p)
 
 	var c *opcode
diff --git a/encode.go b/encode.go
index ca1cab1..513709f 100644
--- a/encode.go
+++ b/encode.go
@@ -183,7 +183,7 @@ func (e *Encoder) encode(v interface{}) error {
 			code = codeSet.code
 		}
 		ctx := codeSet.ctx.Get().(*encodeRuntimeContext)
-		p := uintptr(header.ptr)
+		p := header.ptr
 		ctx.init(p)
 		err := e.run(ctx, code)
 		codeSet.ctx.Put(ctx)
@@ -218,14 +218,14 @@ func (e *Encoder) encode(v interface{}) error {
 		ctx: sync.Pool{
 			New: func() interface{} {
 				return &encodeRuntimeContext{
-					ptrs:     make([]uintptr, codeLength),
+					ptrs:     make([]unsafe.Pointer, codeLength),
 					keepRefs: make([]unsafe.Pointer, 8),
 				}
 			},
 		},
 	}
 	cachedOpcode.set(typeptr, codeSet)
-	p := uintptr(header.ptr)
+	p := header.ptr
 	ctx := codeSet.ctx.Get().(*encodeRuntimeContext)
 	ctx.init(p)
 
diff --git a/encode_context.go b/encode_context.go
index 7a241ff..30b3d44 100644
--- a/encode_context.go
+++ b/encode_context.go
@@ -84,11 +84,11 @@ func (c *encodeCompileContext) decPtrIndex() {
 }
 
 type encodeRuntimeContext struct {
-	ptrs     []uintptr
+	ptrs     []unsafe.Pointer
 	keepRefs []unsafe.Pointer
 }
 
-func (c *encodeRuntimeContext) init(p uintptr) {
+func (c *encodeRuntimeContext) init(p unsafe.Pointer) {
 	c.ptrs[0] = p
 	c.keepRefs = c.keepRefs[:0]
 }
diff --git a/encode_vm.go b/encode_vm.go
index 2a83ca0..127f5e5 100644
--- a/encode_vm.go
+++ b/encode_vm.go
@@ -267,7 +267,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
 
 			newLen := offsetNum + totalLength + nextTotalLength
 			if curlen < newLen {
-				ctx.ptrs = append(ctx.ptrs, make([]uintptr, newLen-curlen)...)
+				ctx.ptrs = append(ctx.ptrs, make([]unsafe.Pointer, newLen-curlen)...)
 			}
 			ctxptr = ctx.ptr() + ptrOffset // assign new ctxptr
 
@@ -344,7 +344,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
 
 			newLen := offsetNum + totalLength + nextTotalLength
 			if curlen < newLen {
-				ctx.ptrs = append(ctx.ptrs, make([]uintptr, newLen-curlen)...)
+				ctx.ptrs = append(ctx.ptrs, make([]unsafe.Pointer, newLen-curlen)...)
 			}
 			ctxptr = ctx.ptr() + ptrOffset // assign new ctxptr
 
@@ -1015,7 +1015,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
 
 			newLen := offsetNum + totalLength + nextTotalLength
 			if curlen < newLen {
-				ctx.ptrs = append(ctx.ptrs, make([]uintptr, newLen-curlen)...)
+				ctx.ptrs = append(ctx.ptrs, make([]unsafe.Pointer, newLen-curlen)...)
 			}
 			ctxptr = ctx.ptr() + ptrOffset // assign new ctxptr
 

Run

$ cd benchmarks && go1.15.5 mod vendor && go1.15.5 test -v -run='^$' -count 1 -benchmem -bench='Benchmark_Encode_MediumStruct_GoJson' .

in 73ba041:

goos: darwin
goarch: amd64
pkg: benchmark
Benchmark_Encode_MediumStruct_GoJson
Benchmark_Encode_MediumStruct_GoJson-16    	 2105785	       572 ns/op	     320 B/op	       1 allocs/op
PASS
ok  	benchmark	2.016s

patched 1:

goos: darwin
goarch: amd64
pkg: benchmark
Benchmark_Encode_MediumStruct_GoJson
Benchmark_Encode_MediumStruct_GoJson-16    	 1255018	       957 ns/op	     632 B/op	      15 allocs/op
PASS
ok  	benchmark	2.210s

patched 2:

goos: darwin
goarch: amd64
pkg: benchmark
Benchmark_Encode_MediumStruct_GoJson
Benchmark_Encode_MediumStruct_GoJson-16    	 1263481	       941 ns/op	     632 B/op	      15 allocs/op
PASS
ok  	benchmark	2.446s
@zchee
Copy link
Contributor Author

zchee commented Nov 23, 2020

ref: use GODEBUG=invalidptr=0 doesn't panic.

@zchee
Copy link
Contributor Author

zchee commented Jan 5, 2021

Seems fixed this issue. Close.

@zchee zchee closed this as completed Jan 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant