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

threads: lock around invocations of memory.Grow #2279

Merged
merged 1 commit into from
Jul 4, 2024
Merged
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
8 changes: 7 additions & 1 deletion internal/wasm/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ type MemoryInstance struct {
definition api.MemoryDefinition

// Mux is used in interpreter mode to prevent overlapping calls to atomic instructions,
// introduced with WebAssembly threads proposal.
// introduced with WebAssembly threads proposal, and in compiler mode to make memory modifications
// within Grow non-racy for the Go race detector.
Mux sync.Mutex

// waiters implements atomic wait and notify. It is implemented similarly to golang.org/x/sync/semaphore,
Expand Down Expand Up @@ -227,6 +228,11 @@ func MemoryPagesToBytesNum(pages uint32) (bytesNum uint64) {

// Grow implements the same method as documented on api.Memory.
func (m *MemoryInstance) Grow(delta uint32) (result uint32, ok bool) {
if m.Shared {
m.Mux.Lock()
defer m.Mux.Unlock()
}

currentPages := m.Pages()
if delta == 0 {
return currentPages, true
Expand Down
Loading