Skip to content

Commit

Permalink
Fix concurrent map access
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Nov 8, 2024
1 parent b8e44e7 commit 50606ff
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion bbq/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func NewCompiler(
Elaboration: elaboration,
Config: &Config{},
globals: make(map[string]*global),
importedGlobals: indexedNativeFunctions,
importedGlobals: NativeFunctions(),
typesInPool: make(map[sema.TypeID]uint16),
constantsInPool: make(map[constantsCacheKey]*constant),
compositeTypeStack: &Stack[*sema.CompositeType]{
Expand Down
10 changes: 8 additions & 2 deletions bbq/compiler/native_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@ import (
"github.com/onflow/cadence/bbq/commons"
)

var indexedNativeFunctions = make(map[string]*global)
var nativeFunctions []*global

func NativeFunctions() map[string]*global {
funcs := make(map[string]*global, len(nativeFunctions))
for _, value := range nativeFunctions {
funcs[value.name] = value
}
return funcs
}

var builtinTypes = []sema.Type{
sema.StringType,
sema.AccountType,
Expand Down Expand Up @@ -72,5 +79,4 @@ func addNativeFunction(name string) {
name: name,
}
nativeFunctions = append(nativeFunctions, global)
indexedNativeFunctions[name] = global
}
12 changes: 10 additions & 2 deletions bbq/vm/native_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,23 @@ import (
"github.com/onflow/cadence/stdlib"
)

var NativeFunctions = map[string]Value{}
var nativeFunctions = map[string]Value{}

// BuiltInLocation is the location of built-in constructs.
// It's always nil.
var BuiltInLocation common.Location = nil

func NativeFunctions() map[string]Value {
funcs := make(map[string]Value, len(nativeFunctions))
for name, value := range nativeFunctions {

Check failure on line 39 in bbq/vm/native_functions.go

View workflow job for this annotation

GitHub Actions / Lint

range statement over map: map[string]github.com/onflow/cadence/bbq/vm.Value (maprange)
funcs[name] = value
}
return funcs
}

func RegisterFunction(functionName string, functionValue NativeFunctionValue) {
functionValue.Name = functionName
NativeFunctions[functionName] = functionValue
nativeFunctions[functionName] = functionValue
}

func RegisterTypeBoundFunction(typeName, functionName string, functionValue NativeFunctionValue) {
Expand Down
7 changes: 4 additions & 3 deletions bbq/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ func NewVM(
// linkedGlobalsCache is a local cache-alike that is being used to hold already linked imports.
linkedGlobalsCache := map[common.Location]LinkedGlobals{
BuiltInLocation: {
// It is safe to re-use native functions map here because,
// once put into the cache, it will only be used for read-only operations.
indexedGlobals: NativeFunctions,
// It is NOT safe to re-use native functions map here because,
// once put into the cache, it will be updated by adding the
// globals of the current program.
indexedGlobals: NativeFunctions(),
},
}

Expand Down

0 comments on commit 50606ff

Please sign in to comment.