2
0
Fork 0
mirror of https://github.com/ii64/sonic.git synced 2026-06-21 00:46:43 +08:00

fix: use global var _KeepAlive to notice GC in shadow funcs (#145)

Co-authored-by: duanyi.aster <duanyi.aster@bytedance.com>
This commit is contained in:
Yi Duan 2021-12-02 11:08:40 +08:00 committed by GitHub
parent 3f2bab552b
commit d2711a6af0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 16 deletions

View file

@ -60,17 +60,35 @@ type _Decoder func(
fv uint64,
) (int, error)
var _KeepAlive struct {
s string
i int
vp unsafe.Pointer
sb *_Stack
fv uint64
ret int
err error
frame [_FP_offs]byte
}
var errCallShadow = errors.New("DON'T CALL THIS!")
//go:nosplit
// Faker func of _Decoder, used to export its stackmap as _Decoder's
func _Decoder_Shadow(s string, i int, vp unsafe.Pointer, sb *_Stack, fv uint64) (int, error) {
func _Decoder_Shadow(s string, i int, vp unsafe.Pointer, sb *_Stack, fv uint64) (ret int, err error) {
// align to assembler_amd64.go: _FP_offs
var stacks [_FP_offs]byte
runtime.KeepAlive(stacks)
var frame [_FP_offs]byte
// keep all args and stacks alive
_KeepAlive.s = s
_KeepAlive.i = i
_KeepAlive.vp = vp
_KeepAlive.sb = sb
_KeepAlive.fv = fv
_KeepAlive.ret = ret
_KeepAlive.err = err
_KeepAlive.frame = frame
// must keep sb noticeable to GC
runtime.KeepAlive(sb)
return 0, errCallShadow
}

View file

@ -1,3 +1,4 @@
/*
* Copyright 2021 ByteDance Inc.
*

View file

@ -26,7 +26,7 @@ import (
)
var (
debugSyncGC = os.Getenv("SONIC_SYNC_GC") != ""
debugSyncGC = os.Getenv("SONIC_SYNC_GC") != ""
debugAsyncGC = os.Getenv("SONIC_NO_ASYNC_GC") == ""
)

View file

@ -18,10 +18,9 @@ package encoder
import (
`bytes`
`errors`
`runtime`
`sync`
`unsafe`
`errors`
`github.com/bytedance/sonic/internal/caching`
`github.com/bytedance/sonic/internal/rt`
@ -58,24 +57,34 @@ type _Encoder func(
fv uint64,
) error
var _KeepAlive struct {
rb *[]byte
vp unsafe.Pointer
sb *_Stack
fv uint64
err error
frame [_FP_offs]byte
}
var errCallShadow = errors.New("DON'T CALL THIS!")
// Faker func of _Encoder, used to export its stackmap as _Encoder's
//go:nosplit
func _Encoder_Shadow(rb *[]byte, vp unsafe.Pointer, sb *_Stack, fv uint64) error {
func _Encoder_Shadow(rb *[]byte, vp unsafe.Pointer, sb *_Stack, fv uint64) (err error) {
// align to assembler_amd64.go: _FP_offs
var frames [_FP_offs]byte
runtime.KeepAlive(frames)
var frame [_FP_offs]byte
// must keep sb noticeable to GC
runtime.KeepAlive(sb)
runtime.KeepAlive(rb)
runtime.KeepAlive(vp)
// must keep all args and frames noticeable to GC
_KeepAlive.rb = rb
_KeepAlive.vp = vp
_KeepAlive.sb = sb
_KeepAlive.fv = fv
_KeepAlive.err = err
_KeepAlive.frame = frame
return errCallShadow
}
func newBytes() []byte {
if ret := bytesPool.Get(); ret != nil {
return ret.([]byte)