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

fix:(loader) avoid race on lastmoduledatap with go plugin (only effects on byted-tango) #707

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion go.work.sum
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
github.com/bytedance/sonic/loader v0.2.0/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14 h1:k5II8e6QD8mITdi+okbbmR/cIyEbeXLBhy5Ha4nevyc=
55 changes: 55 additions & 0 deletions loader/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// +build !bytedance_tango

/**
* 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
*
* https://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 loader

import (
"sync/atomic"
"unsafe"
)

func registerModule(mod *moduledata) {
registerModuleLockFree(&lastmoduledatap, mod)
}

func registerModuleLockFree(tail **moduledata, mod *moduledata) {
for {
oldTail := loadModule(tail)
if casModule(tail, oldTail, mod) {
storeModule(&oldTail.next, mod)
break
}
}
}

func loadModule(p **moduledata) *moduledata {
return (*moduledata)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
}

func storeModule(p **moduledata, value *moduledata) {
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(value))
}

func casModule(p **moduledata, oldValue *moduledata, newValue *moduledata) bool {
return atomic.CompareAndSwapPointer(
(*unsafe.Pointer)(unsafe.Pointer(p)),
unsafe.Pointer(oldValue),
unsafe.Pointer(newValue),
)
}

34 changes: 34 additions & 0 deletions loader/register_tango.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// +build bytedance_tango

/**
* 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
*
* https://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 loader

import (
"sync"
_ "unsafe"
)

//go:linkname pluginsMu plugin.pluginsMu
var pluginsMu sync.Mutex

func registerModule(mod *moduledata) {
pluginsMu.Lock()
defer pluginsMu.Unlock()
lastmoduledatap.next = mod
lastmoduledatap = mod
}
13 changes: 6 additions & 7 deletions loader/stubs_test.go → loader/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,24 @@ import (
"sync"
)

func Test_registerModuleLockFree(t *testing.T) {
n, parallel := 1000, 8
head := moduledata{}
tail := &head
func Test_registerModuleRace(t *testing.T) {
n, parallel := 100, 8
s := &moduledata{}
registerModule(s)
wg := sync.WaitGroup{}
wg.Add(parallel)
filler := func(n int) {
defer wg.Done()
for i := 0; i < n; i++ {
m := &moduledata{}
registerModuleLockFree(&tail, m)
registerModule(&moduledata{})
}
}
for i := 0; i < parallel; i++ {
go filler(n)
}
wg.Wait()
i := 0
for p := head.next; p != nil; p = p.next {
for p := s.next; p != nil; p = p.next {
i += 1
}
if i != parallel * n {
Expand Down
32 changes: 0 additions & 32 deletions loader/stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,12 @@
package loader

import (
"sync/atomic"
"unsafe"
_ `unsafe`
)

//go:linkname lastmoduledatap runtime.lastmoduledatap
//goland:noinspection GoUnusedGlobalVariable
var lastmoduledatap *moduledata

func registerModule(mod *moduledata) {
registerModuleLockFree(&lastmoduledatap, mod)
}

//go:linkname moduledataverify1 runtime.moduledataverify1
func moduledataverify1(_ *moduledata)

func registerModuleLockFree(tail **moduledata, mod *moduledata) {
for {
oldTail := loadModule(tail)
if casModule(tail, oldTail, mod) {
storeModule(&oldTail.next, mod)
break
}
}
}

func loadModule(p **moduledata) *moduledata {
return (*moduledata)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
}

func storeModule(p **moduledata, value *moduledata) {
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(value))
}

func casModule(p **moduledata, oldValue *moduledata, newValue *moduledata) bool {
return atomic.CompareAndSwapPointer(
(*unsafe.Pointer)(unsafe.Pointer(p)),
unsafe.Pointer(oldValue),
unsafe.Pointer(newValue),
)
}
Loading