Skip to content

Commit 683f982

Browse files
authored
fix: fix native C FSM issues (#25)
* add submodule code * build: update asm2asm * fix: incorrect free size of `ReqCache` * update bench.py * opt: reduce J2TExtra memory * test: add `go:nocheckptr` * test: add parallel tests * feat: update pcsp * feat: update asm2asm * update sonic * not support go1.15 * fallback for all * update sonic * rollback asm2asm * update sonic * update go mod
1 parent 1f846f2 commit 683f982

34 files changed

+5896
-6512
lines changed

.github/workflows/push-check-linux-arm.yml

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Compatibility Test
2+
3+
on: push
4+
5+
jobs:
6+
build:
7+
strategy:
8+
matrix:
9+
go-version: [1.15.x, 1.20.x]
10+
os: [ARM64, X64]
11+
runs-on: ${{ matrix.os }}
12+
steps:
13+
- uses: actions/checkout@v2
14+
15+
- name: Set up Go
16+
uses: actions/setup-go@v2
17+
with:
18+
go-version: ${{ matrix.go-version }}
19+
20+
- uses: actions/cache@v2
21+
with:
22+
path: ~/go/pkg/mod
23+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
24+
restore-keys: |
25+
${{ runner.os }}-go-
26+
27+
- name: Unit Test
28+
run: |
29+
go test -race github.com/cloudwego/dynamicgo/thrift
30+
go test -race github.com/cloudwego/dynamicgo/thrift/annotation
31+
go test -race github.com/cloudwego/dynamicgo/thrift/generic
32+
go test -race github.com/cloudwego/dynamicgo/conv/t2j
33+
go test -race github.com/cloudwego/dynamicgo/http
34+
go test -race github.com/cloudwego/dynamicgo/internal/json

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
[submodule "tools/asm2asm"]
1+
[submodule "asm2asm"]
22
path = tools/asm2asm
33
url = https://github.com/chenzhuoyu/asm2asm.git

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,4 @@ $(foreach \
105105
arch, \
106106
${ARCH}, \
107107
$(eval $(call build_arch,${arch})) \
108-
)
108+
)

bench.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import argparse
2121

2222
gbench_prefix = "SONIC_NO_ASYNC_GC=1 go test -benchmem -run=none "
23-
mainbranch = "master"
23+
mainbranch = "main"
2424

2525
def run(cmd):
2626
print(cmd)

conv/j2t/error.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/cloudwego/dynamicgo/http"
2626
"github.com/cloudwego/dynamicgo/internal/json"
2727
"github.com/cloudwego/dynamicgo/internal/native/types"
28+
"github.com/cloudwego/dynamicgo/internal/rt"
2829
"github.com/cloudwego/dynamicgo/meta"
2930
"github.com/cloudwego/dynamicgo/thrift"
3031
)
@@ -37,15 +38,20 @@ func newError(code meta.ErrCode, msg string, err error) error {
3738

3839
type _J2TExtra_STRUCT struct {
3940
desc unsafe.Pointer
40-
reqs thrift.RequiresBitmap
41+
reqs string
4142
}
4243

43-
func getJ2TExtraStruct(fsm *types.J2TStateMachine, offset int) (*thrift.TypeDescriptor, *_J2TExtra_STRUCT) {
44+
//go:nocheckptr
45+
func getJ2TExtraStruct(fsm *types.J2TStateMachine, offset int) (td *thrift.TypeDescriptor, reqs thrift.RequiresBitmap) {
4446
state := fsm.At(offset - 1)
4547
if state == nil {
46-
return nil, nil
48+
return nil, thrift.RequiresBitmap{}
4749
}
48-
return (*thrift.TypeDescriptor)(state.TdPointer()), (*_J2TExtra_STRUCT)(unsafe.Pointer(&state.Extra))
50+
td = (*thrift.TypeDescriptor)(unsafe.Pointer(state.TdPointer()))
51+
je := (*_J2TExtra_STRUCT)(unsafe.Pointer(&state.Extra))
52+
v := rt.Str2Mem(je.reqs)
53+
reqs = *(*thrift.RequiresBitmap)(unsafe.Pointer(&v))
54+
return
4955
}
5056

5157
func (self BinaryConv) handleError(ctx context.Context, fsm *types.J2TStateMachine, buf *[]byte, src []byte, req http.RequestGetter, ret uint64, top bool) (cont bool, err error) {
@@ -55,14 +61,14 @@ func (self BinaryConv) handleError(ctx context.Context, fsm *types.J2TStateMachi
5561
switch e {
5662
case types.ERR_HTTP_MAPPING:
5763
{
58-
desc, ext := getJ2TExtraStruct(fsm, fsm.SP)
59-
if desc == nil || ext == nil {
64+
desc, reqs := getJ2TExtraStruct(fsm, fsm.SP)
65+
if desc == nil {
6066
return false, newError(meta.ErrConvert, "invalid json input", nil)
6167
}
6268
if desc.Type() != thrift.STRUCT {
6369
return false, newError(meta.ErrConvert, "invalid descriptor while http mapping", nil)
6470
}
65-
return true, self.writeHttpRequestToThrift(ctx, req, desc.Struct(), &ext.reqs, buf, false, top)
71+
return true, self.writeHttpRequestToThrift(ctx, req, desc.Struct(), reqs, buf, false, top)
6672
}
6773
case types.ERR_HTTP_MAPPING_END:
6874
{

conv/j2t/impl.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ import (
3333
"github.com/cloudwego/dynamicgo/thrift/base"
3434
)
3535

36+
const (
37+
_GUARD_SLICE_FACTOR = 1
38+
)
39+
3640
func (self *BinaryConv) do(ctx context.Context, src []byte, desc *thrift.TypeDescriptor, buf *[]byte, req http.RequestGetter) error {
41+
//NOTICE: output buffer must be larger than src buffer
42+
rt.GuardSlice(buf, len(src)*_GUARD_SLICE_FACTOR)
43+
3744
if self.opts.EnableThriftBase {
3845
if f := desc.Struct().GetRequestBase(); f != nil {
3946
if err := writeRequestBaseToThrift(ctx, buf, f); err != nil {
@@ -50,7 +57,7 @@ func (self *BinaryConv) do(ctx context.Context, src []byte, desc *thrift.TypeDes
5057
st.Requires().CopyTo(reqs)
5158
// check if any http-mapping exists
5259
if desc.Struct().HttpMappingFields() != nil {
53-
if err := self.writeHttpRequestToThrift(ctx, req, st, reqs, buf, true, true); err != nil {
60+
if err := self.writeHttpRequestToThrift(ctx, req, st, *reqs, buf, true, true); err != nil {
5461
return err
5562
}
5663
}
@@ -175,7 +182,7 @@ func writeRequestBaseToThrift(ctx context.Context, buf *[]byte, field *thrift.Fi
175182
return nil
176183
}
177184

178-
func (self *BinaryConv) writeHttpRequestToThrift(ctx context.Context, req http.RequestGetter, desc *thrift.StructDescriptor, reqs *thrift.RequiresBitmap, buf *[]byte, nobody bool, top bool) (err error) {
185+
func (self *BinaryConv) writeHttpRequestToThrift(ctx context.Context, req http.RequestGetter, desc *thrift.StructDescriptor, reqs thrift.RequiresBitmap, buf *[]byte, nobody bool, top bool) (err error) {
179186
if req == nil {
180187
return newError(meta.ErrInvalidParam, "http request is nil", nil)
181188
}

conv/t2j/conv_amd64_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//go:build amd64
2-
// +build amd64
1+
//go:build amd64 && go1.16
2+
// +build amd64,go1.16
33

44
/**
55
* Copyright 2023 CloudWeGo Authors.

conv/t2j/conv_fallback_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//go:build !amd64
2-
// +build !amd64
1+
//go:build !amd64 || !go1.16
2+
// +build !amd64 !go1.16
33

44
/**
55
* Copyright 2023 CloudWeGo Authors.

conv/t2j/impl.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ import (
3030
"github.com/cloudwego/dynamicgo/thrift/base"
3131
)
3232

33+
const (
34+
_GUARD_SLICE_FACTOR = 2
35+
)
36+
3337
//go:noinline
3438
func wrapError(code meta.ErrCode, msg string, err error) error {
3539
// panic(msg)
@@ -66,6 +70,9 @@ func (self *BinaryConv) readResponseBase(ctx context.Context, p *thrift.BinaryPr
6670
}
6771

6872
func (self *BinaryConv) do(ctx context.Context, src []byte, desc *thrift.TypeDescriptor, out *[]byte, resp http.ResponseSetter) (err error) {
73+
//NOTICE: output buffer must be larger than src buffer
74+
rt.GuardSlice(out, len(src)*_GUARD_SLICE_FACTOR)
75+
6976
var p = thrift.BinaryProtocol{
7077
Buf: src,
7178
}

go.mod

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,17 @@ go 1.16
44

55
require (
66
github.com/apache/thrift v0.13.0
7-
github.com/bytedance/sonic v1.8.3-0.20230228111319-6d60889e3b65
7+
github.com/bytedance/sonic v1.8.8
88
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311
99
github.com/choleraehyq/pid v0.0.16 // indirect
1010
github.com/cloudwego/kitex v0.4.4
1111
github.com/cloudwego/thriftgo v0.2.7
1212
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
13-
github.com/google/go-cmp v0.5.9 // indirect
1413
github.com/iancoleman/strcase v0.2.0
1514
github.com/klauspost/cpuid/v2 v2.2.4
16-
github.com/kr/text v0.2.0 // indirect
17-
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
1815
github.com/stretchr/testify v1.8.2
1916
github.com/thrift-iterator/go v0.0.0-20190402154806-9b5a67519118
2017
github.com/v2pro/plz v0.0.0-20221028024117-e5f9aec5b631 // indirect
2118
github.com/v2pro/quokka v0.0.0-20171201153428-382cb39c6ee6 // indirect
2219
github.com/v2pro/wombat v0.0.0-20180402055224-a56dbdcddef2 // indirect
23-
golang.org/x/arch v0.2.0 // indirect
24-
golang.org/x/text v0.7.0 // indirect
25-
google.golang.org/genproto v0.0.0-20220916172020-2692e8806bfa // indirect
26-
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
2720
)

0 commit comments

Comments
 (0)