2
0
Fork 0
mirror of https://github.com/ii64/sonic.git synced 2026-06-23 09:56:44 +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, fv uint64,
) (int, error) ) (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!") var errCallShadow = errors.New("DON'T CALL THIS!")
//go:nosplit //go:nosplit
// Faker func of _Decoder, used to export its stackmap as _Decoder's // 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 // align to assembler_amd64.go: _FP_offs
var stacks [_FP_offs]byte var frame [_FP_offs]byte
runtime.KeepAlive(stacks)
// 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 return 0, errCallShadow
} }

View file

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

View file

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

View file

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