Skip to content

Commit 6105e8b

Browse files
committed
runtime: revert init order changes
First, remove the randomization of initialization order. Then, revert to source code order instead of sorted package path order. This restores the behavior that was in 1.12. A larger change which will implement the suggestion in #31636 will wait for 1.14. It's too complicated for 1.13 at this point (it has tricky interactions with plugins). Fixes #31636 Change-Id: I35b48e8cc21cf9f93c0973edd9193d2eac197628 Reviewed-on: https://go-review.googlesource.com/c/go/+/178297 Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 648c7b5 commit 6105e8b

File tree

9 files changed

+61
-14
lines changed

9 files changed

+61
-14
lines changed

src/cmd/compile/internal/gc/init.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ func fninit(n []*Node) {
3737
var fns []*obj.LSym // functions to call for package initialization
3838

3939
// Find imported packages with init tasks.
40-
for _, p := range types.ImportedPkgList() {
41-
if s, ok := p.LookupOK(".inittask"); ok {
42-
deps = append(deps, s.Linksym())
43-
}
40+
for _, s := range types.InitSyms {
41+
deps = append(deps, s.Linksym())
4442
}
4543

4644
// Make a function that contains all the initialization statements.

src/cmd/compile/internal/types/pkg.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func (pkg *Pkg) Lookup(name string) *Sym {
8484
return s
8585
}
8686

87+
// List of .inittask entries in imported packages, in source code order.
8788
var InitSyms []*Sym
8889

8990
// LookupOK looks up name in pkg and reports whether it previously existed.
@@ -100,7 +101,7 @@ func (pkg *Pkg) LookupOK(name string) (s *Sym, existed bool) {
100101
Name: name,
101102
Pkg: pkg,
102103
}
103-
if name == "init" {
104+
if name == ".inittask" {
104105
InitSyms = append(InitSyms, s)
105106
}
106107
pkg.Syms[name] = s

src/runtime/proc.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5211,15 +5211,6 @@ func doInit(t *initTask) {
52115211
throw("recursive call during initialization - linker skew")
52125212
default: // not initialized yet
52135213
t.state = 1 // initialization in progress
5214-
if raceenabled {
5215-
// Randomize initialization order of packages t depends on.
5216-
// TODO: enable always instead of just for race?
5217-
s := *(*[]uintptr)(unsafe.Pointer(&slice{array: add(unsafe.Pointer(t), 3*sys.PtrSize), len: int(t.ndeps), cap: int(t.ndeps)}))
5218-
for i := len(s) - 1; i > 0; i-- {
5219-
j := int(fastrandn(uint32(i + 1)))
5220-
s[i], s[j] = s[j], s[i]
5221-
}
5222-
}
52235214
for i := uintptr(0); i < t.ndeps; i++ {
52245215
p := add(unsafe.Pointer(t), (3+i)*sys.PtrSize)
52255216
t2 := *(**initTask)(p)

test/fixedbugs/issue31636.dir/a.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2019 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package a
6+
7+
func init() {
8+
println("a")
9+
}

test/fixedbugs/issue31636.dir/b.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2019 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package b
6+
7+
func init() {
8+
println("b")
9+
}

test/fixedbugs/issue31636.dir/c.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2019 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package c
6+
7+
func init() {
8+
println("c")
9+
}

test/fixedbugs/issue31636.dir/main.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2019 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
// We want the initializers of these packages to occur in source code
8+
// order. See issue 31636. This is the behavior up to and including
9+
// 1.13. For 1.14, we will move to a variant of lexicographic ordering
10+
// which will require a change to the test output of this test.
11+
import (
12+
_ "c"
13+
14+
_ "b"
15+
16+
_ "a"
17+
)
18+
19+
func main() {
20+
}

test/fixedbugs/issue31636.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rundir
2+
3+
// Copyright 2019 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package ignored

test/fixedbugs/issue31636.out

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
c
2+
b
3+
a

0 commit comments

Comments
 (0)