]> Cypherpunks.ru repositories - gostls13.git/commitdiff
runtime: revert init order changes
authorKeith Randall <keithr@alum.mit.edu>
Tue, 21 May 2019 05:01:12 +0000 (01:01 -0400)
committerKeith Randall <khr@golang.org>
Wed, 22 May 2019 11:07:00 +0000 (11:07 +0000)
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 <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/compile/internal/gc/init.go
src/cmd/compile/internal/types/pkg.go
src/runtime/proc.go
test/fixedbugs/issue31636.dir/a.go [new file with mode: 0644]
test/fixedbugs/issue31636.dir/b.go [new file with mode: 0644]
test/fixedbugs/issue31636.dir/c.go [new file with mode: 0644]
test/fixedbugs/issue31636.dir/main.go [new file with mode: 0644]
test/fixedbugs/issue31636.go [new file with mode: 0644]
test/fixedbugs/issue31636.out [new file with mode: 0644]

index 6467aafd53ad7efcd0757f6b960dbf541364dc79..81572922168ecb8fb201626e0323100f11d5091e 100644 (file)
@@ -37,10 +37,8 @@ func fninit(n []*Node) {
        var fns []*obj.LSym  // functions to call for package initialization
 
        // Find imported packages with init tasks.
-       for _, p := range types.ImportedPkgList() {
-               if s, ok := p.LookupOK(".inittask"); ok {
-                       deps = append(deps, s.Linksym())
-               }
+       for _, s := range types.InitSyms {
+               deps = append(deps, s.Linksym())
        }
 
        // Make a function that contains all the initialization statements.
index e502b986aefb14c4facc0336b56f9c32487312cb..bcc67895095812f79dd3482684f3c80bb0c28894 100644 (file)
@@ -84,6 +84,7 @@ func (pkg *Pkg) Lookup(name string) *Sym {
        return s
 }
 
+// List of .inittask entries in imported packages, in source code order.
 var InitSyms []*Sym
 
 // 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) {
                Name: name,
                Pkg:  pkg,
        }
-       if name == "init" {
+       if name == ".inittask" {
                InitSyms = append(InitSyms, s)
        }
        pkg.Syms[name] = s
index bf7835eb19848e3c3a39f872aa9038e19b933539..e9eca23138845cad4043a313a88e1c03a0c0d92b 100644 (file)
@@ -5211,15 +5211,6 @@ func doInit(t *initTask) {
                throw("recursive call during initialization - linker skew")
        default: // not initialized yet
                t.state = 1 // initialization in progress
-               if raceenabled {
-                       // Randomize initialization order of packages t depends on.
-                       // TODO: enable always instead of just for race?
-                       s := *(*[]uintptr)(unsafe.Pointer(&slice{array: add(unsafe.Pointer(t), 3*sys.PtrSize), len: int(t.ndeps), cap: int(t.ndeps)}))
-                       for i := len(s) - 1; i > 0; i-- {
-                               j := int(fastrandn(uint32(i + 1)))
-                               s[i], s[j] = s[j], s[i]
-                       }
-               }
                for i := uintptr(0); i < t.ndeps; i++ {
                        p := add(unsafe.Pointer(t), (3+i)*sys.PtrSize)
                        t2 := *(**initTask)(p)
diff --git a/test/fixedbugs/issue31636.dir/a.go b/test/fixedbugs/issue31636.dir/a.go
new file mode 100644 (file)
index 0000000..e57e0d5
--- /dev/null
@@ -0,0 +1,9 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package a
+
+func init() {
+       println("a")
+}
diff --git a/test/fixedbugs/issue31636.dir/b.go b/test/fixedbugs/issue31636.dir/b.go
new file mode 100644 (file)
index 0000000..990e682
--- /dev/null
@@ -0,0 +1,9 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package b
+
+func init() {
+       println("b")
+}
diff --git a/test/fixedbugs/issue31636.dir/c.go b/test/fixedbugs/issue31636.dir/c.go
new file mode 100644 (file)
index 0000000..e53529a
--- /dev/null
@@ -0,0 +1,9 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package c
+
+func init() {
+       println("c")
+}
diff --git a/test/fixedbugs/issue31636.dir/main.go b/test/fixedbugs/issue31636.dir/main.go
new file mode 100644 (file)
index 0000000..d8ae902
--- /dev/null
@@ -0,0 +1,20 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+// We want the initializers of these packages to occur in source code
+// order. See issue 31636. This is the behavior up to and including
+// 1.13. For 1.14, we will move to a variant of lexicographic ordering
+// which will require a change to the test output of this test.
+import (
+       _ "c"
+
+       _ "b"
+
+       _ "a"
+)
+
+func main() {
+}
diff --git a/test/fixedbugs/issue31636.go b/test/fixedbugs/issue31636.go
new file mode 100644 (file)
index 0000000..af6f134
--- /dev/null
@@ -0,0 +1,7 @@
+// rundir
+
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package ignored
diff --git a/test/fixedbugs/issue31636.out b/test/fixedbugs/issue31636.out
new file mode 100644 (file)
index 0000000..e274b2b
--- /dev/null
@@ -0,0 +1,3 @@
+c
+b
+a