-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: enhance data race detection in encoder
- Loading branch information
Showing
11 changed files
with
226 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//go:build !race | ||
// +build !race | ||
|
||
/* | ||
* Copyright 2021 ByteDance Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package encoder | ||
|
||
import ( | ||
`runtime` | ||
|
||
`github.com/bytedance/sonic/internal/rt` | ||
) | ||
|
||
|
||
func encodeInto(buf *[]byte, val interface{}, opts Options) error { | ||
stk := newStack() | ||
efv := rt.UnpackEface(val) | ||
err := encodeTypedPointer(buf, efv.Type, &efv.Value, stk, uint64(opts)) | ||
|
||
/* return the stack into pool */ | ||
if err != nil { | ||
resetStack(stk) | ||
} | ||
freeStack(stk) | ||
|
||
/* avoid GC ahead */ | ||
runtime.KeepAlive(buf) | ||
runtime.KeepAlive(efv) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//go:build race | ||
// +build race | ||
|
||
/* | ||
* Copyright 2021 ByteDance Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package encoder | ||
|
||
import ( | ||
`encoding/json` | ||
`runtime` | ||
|
||
`github.com/bytedance/sonic/internal/rt` | ||
) | ||
|
||
|
||
func helpDetectDataRace(val interface{}) { | ||
_, _ = json.Marshal(val) | ||
} | ||
|
||
func encodeInto(buf *[]byte, val interface{}, opts Options) error { | ||
|
||
|
||
stk := newStack() | ||
efv := rt.UnpackEface(val) | ||
err := encodeTypedPointer(buf, efv.Type, &efv.Value, stk, uint64(opts)) | ||
|
||
/* return the stack into pool */ | ||
if err != nil { | ||
resetStack(stk) | ||
} | ||
freeStack(stk) | ||
|
||
/* avoid GC ahead */ | ||
runtime.KeepAlive(buf) | ||
runtime.KeepAlive(efv) | ||
|
||
/* put last to make the panic from sonic will always be caught at first */ | ||
helpDetectDataRace(val) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//go:build !race | ||
// +build !race | ||
|
||
/* | ||
* Copyright 2021 ByteDance Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package encoder | ||
|
||
import ( | ||
`runtime` | ||
|
||
`sync` | ||
`testing` | ||
) | ||
|
||
func TestGC(t *testing.T) { | ||
if debugSyncGC { | ||
return | ||
} | ||
out, err := Encode(_GenericValue, 0) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
n := len(out) | ||
wg := &sync.WaitGroup{} | ||
N := 10000 | ||
for i:=0; i<N; i++ { | ||
wg.Add(1) | ||
go func (wg *sync.WaitGroup, size int) { | ||
defer wg.Done() | ||
out, err := Encode(_GenericValue, 0) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(out) != size { | ||
t.Fatal(len(out), size) | ||
} | ||
runtime.GC() | ||
}(wg, n) | ||
} | ||
wg.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
//go:build !race | ||
// +build !race | ||
|
||
/* | ||
* Copyright 2024 ByteDance Inc. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
|
||
//go:build race | ||
// +build race | ||
|
||
/* | ||
* Copyright 2024 ByteDance Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
|
||
package issue_test | ||
|
||
import ( | ||
`github.com/bytedance/sonic` | ||
`testing` | ||
) | ||
|
||
type MyFoo struct { | ||
List []*int64 | ||
} | ||
|
||
func TestRaceEncode(t *testing.T) { | ||
f := &MyFoo{ | ||
List: []*int64{new(int64), new(int64)}, | ||
} | ||
|
||
go func() { | ||
f.List = []*int64{new(int64), new(int64)} | ||
}() | ||
|
||
go func() { | ||
sonic.Marshal(f) | ||
}() | ||
|
||
// encoding/json always detect data race when enabling `-race` here | ||
// go func() { | ||
// json.Marshal(f) | ||
// }() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/bin/bash | ||
|
||
set -xe | ||
cd ./issue_test | ||
mv race_test_go race_test.go | ||
go test -v -run=TestRaceEncode -race -count=100 . > test_race.log || true | ||
if ! grep -q "WARNING: DATA RACE" ./test_race.log; then | ||
echo "TEST FAILED: should data race here" | ||
exit 1 | ||
fi | ||
mv race_test.go race_test_go | ||
rm -vrf test_race.log |
Submodule asm2asm
updated
3 files
+0 −60 | .github/workflows/pr-check.yml | |
+0 −24 | .github/workflows/tests.yml | |
+50 −91 | asm2asm.py |