Commit a09cd8cc authored by Cherry Zhang's avatar Cherry Zhang

[dev.link] cmd/compile: fix data race on LSym.Pkg

LSym may be created concurrently. Reading/writing LSym.Pkg may
cause data race (see
https://build.golang.org/log/f0351c5cc7bf4c92e3aa5e78e294c2d009ebf118).
Fix this by setting LSym.Pkg only when holding the lock.

Change-Id: Ib3160ecf47c4ca530b09369e0e8284db6597cfd0
Reviewed-on: https://go-review.googlesource.com/c/go/+/198492Reviewed-by: default avatarThan McIntosh <thanm@google.com>
parent 53b7c182
...@@ -76,24 +76,22 @@ func (sym *Sym) LinksymName() string { ...@@ -76,24 +76,22 @@ func (sym *Sym) LinksymName() string {
return sym.Pkg.Prefix + "." + sym.Name return sym.Pkg.Prefix + "." + sym.Name
} }
func (sym *Sym) Linksym() (r *obj.LSym) { func (sym *Sym) Linksym() *obj.LSym {
if sym == nil { if sym == nil {
return nil return nil
} }
if sym.Func() { initPkg := func(r *obj.LSym) {
// This is a function symbol. Mark it as "internal ABI".
r = Ctxt.LookupABI(sym.LinksymName(), obj.ABIInternal)
} else {
r = Ctxt.Lookup(sym.LinksymName())
}
if r.Pkg == "" {
if sym.Linkname != "" { if sym.Linkname != "" {
r.Pkg = "_" r.Pkg = "_"
} else { } else {
r.Pkg = sym.Pkg.Prefix r.Pkg = sym.Pkg.Prefix
} }
} }
return if sym.Func() {
// This is a function symbol. Mark it as "internal ABI".
return Ctxt.LookupABIInit(sym.LinksymName(), obj.ABIInternal, initPkg)
}
return Ctxt.LookupInit(sym.LinksymName(), initPkg)
} }
// Less reports whether symbol a is ordered before symbol b. // Less reports whether symbol a is ordered before symbol b.
......
...@@ -78,6 +78,13 @@ func (ctxt *Link) LookupStatic(name string) *LSym { ...@@ -78,6 +78,13 @@ func (ctxt *Link) LookupStatic(name string) *LSym {
// LookupABI looks up a symbol with the given ABI. // LookupABI looks up a symbol with the given ABI.
// If it does not exist, it creates it. // If it does not exist, it creates it.
func (ctxt *Link) LookupABI(name string, abi ABI) *LSym { func (ctxt *Link) LookupABI(name string, abi ABI) *LSym {
return ctxt.LookupABIInit(name, abi, nil)
}
// LookupABI looks up a symbol with the given ABI.
// If it does not exist, it creates it and
// passes it to init for one-time initialization.
func (ctxt *Link) LookupABIInit(name string, abi ABI, init func(s *LSym)) *LSym {
var hash map[string]*LSym var hash map[string]*LSym
switch abi { switch abi {
case ABI0: case ABI0:
...@@ -94,6 +101,9 @@ func (ctxt *Link) LookupABI(name string, abi ABI) *LSym { ...@@ -94,6 +101,9 @@ func (ctxt *Link) LookupABI(name string, abi ABI) *LSym {
s = &LSym{Name: name} s = &LSym{Name: name}
s.SetABI(abi) s.SetABI(abi)
hash[name] = s hash[name] = s
if init != nil {
init(s)
}
} }
ctxt.hashmu.Unlock() ctxt.hashmu.Unlock()
return s return s
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment