This removes a //go:linkname usage in the coverage implementation.
For #67401.
Change-Id: I0602172c7e372a84465160dbf46d9fa371582fff
Reviewed-on: https://go-review.googlesource.com/c/go/+/586259
Auto-Submit: Russ Cox <rsc@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
return
}
+ // Special case: runtime must define error even if imported packages mention it (#29304).
+ forceNeed := typ == types.ErrorType && base.Ctxt.Pkgpath == "runtime"
+
// If a type was found in an imported package, then we can assume
// that package (or one of its transitive dependencies) already
// generated method wrappers for it.
- if r.importedDef() {
+ if r.importedDef() && !forceNeed {
haveWrapperTypes = append(haveWrapperTypes, typ)
} else {
needWrapperTypes = append(needWrapperTypes, typ)
"runtime",
"internal/runtime/atomic",
+ "internal/runtime/exithook",
"runtime/internal/math",
"runtime/internal/sys",
"internal/runtime/syscall",
internal/nettrace,
internal/platform,
internal/profilerecord,
+ internal/runtime/exithook,
internal/trace/traceviewer/format,
log/internal,
math/bits,
internal/goexperiment,
internal/goos,
internal/profilerecord,
+ internal/runtime/exithook,
math/bits
< internal/bytealg
< internal/stringslite
package cfile
-import _ "unsafe"
+import "internal/runtime/exithook"
// InitHook is invoked from the main package "init" routine in
// programs built with "-cover". This function is intended to be
// Note: hooks are run in reverse registration order, so
// register the counter data hook before the meta-data hook
// (in the case where two hooks are needed).
- runOnNonZeroExit := true
- runtime_addExitHook(emitCounterData, runOnNonZeroExit)
+ exithook.Add(exithook.Hook{F: emitCounterData, RunOnFailure: true})
if istest {
- runtime_addExitHook(emitMetaData, runOnNonZeroExit)
+ exithook.Add(exithook.Hook{F: emitMetaData, RunOnFailure: true})
} else {
emitMetaData()
}
}
-
-//go:linkname runtime_addExitHook runtime.addExitHook
-func runtime_addExitHook(f func(), runOnNonZeroExit bool)
--- /dev/null
+// Copyright 2024 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 exithook provides limited support for on-exit cleanup.
+//
+// CAREFUL! The expectation is that Add should only be called
+// from a safe context (e.g. not an error/panic path or signal
+// handler, preemption enabled, allocation allowed, write barriers
+// allowed, etc), and that the exit function F will be invoked under
+// similar circumstances. That is the say, we are expecting that F
+// uses normal / high-level Go code as opposed to one of the more
+// restricted dialects used for the trickier parts of the runtime.
+package exithook
+
+// A Hook is a function to be run at program termination
+// (when someone invokes os.Exit, or when main.main returns).
+// Hooks are run in reverse order of registration:
+// the first hook added is the last one run.
+type Hook struct {
+ F func() // func to run
+ RunOnFailure bool // whether to run on non-zero exit code
+}
+
+var (
+ hooks []Hook
+ running bool
+)
+
+// Add adds a new exit hook.
+func Add(h Hook) {
+ hooks = append(hooks, h)
+}
+
+// Run runs the exit hooks.
+// It returns an error if Run is already running or
+// if one of the hooks panics.
+func Run(code int) (err error) {
+ if running {
+ return exitError("exit hook invoked exit")
+ }
+ running = true
+
+ defer func() {
+ if x := recover(); x != nil {
+ err = exitError("exit hook invoked panic")
+ }
+ }()
+
+ local := hooks
+ hooks = nil
+ for i := len(local) - 1; i >= 0; i-- {
+ h := local[i]
+ if code == 0 || h.RunOnFailure {
+ h.F()
+ }
+ }
+ running = false
+ return nil
+}
+
+type exitError string
+
+func (e exitError) Error() string { return string(e) }
+++ /dev/null
-// Copyright 2022 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 runtime
-
-// addExitHook registers the specified function 'f' to be run at
-// program termination (e.g. when someone invokes os.Exit(), or when
-// main.main returns). Hooks are run in reverse order of registration:
-// first hook added is the last one run.
-//
-// CAREFUL: the expectation is that addExitHook should only be called
-// from a safe context (e.g. not an error/panic path or signal
-// handler, preemption enabled, allocation allowed, write barriers
-// allowed, etc), and that the exit function 'f' will be invoked under
-// similar circumstances. That is the say, we are expecting that 'f'
-// uses normal / high-level Go code as opposed to one of the more
-// restricted dialects used for the trickier parts of the runtime.
-func addExitHook(f func(), runOnNonZeroExit bool) {
- exitHooks.hooks = append(exitHooks.hooks, exitHook{f: f, runOnNonZeroExit: runOnNonZeroExit})
-}
-
-// exitHook stores a function to be run on program exit, registered
-// by the utility runtime.addExitHook.
-type exitHook struct {
- f func() // func to run
- runOnNonZeroExit bool // whether to run on non-zero exit code
-}
-
-// exitHooks stores state related to hook functions registered to
-// run when program execution terminates.
-var exitHooks struct {
- hooks []exitHook
- runningExitHooks bool
-}
-
-// runExitHooks runs any registered exit hook functions (funcs
-// previously registered using runtime.addExitHook). Here 'exitCode'
-// is the status code being passed to os.Exit, or zero if the program
-// is terminating normally without calling os.Exit.
-func runExitHooks(exitCode int) {
- if exitHooks.runningExitHooks {
- throw("internal error: exit hook invoked exit")
- }
- exitHooks.runningExitHooks = true
-
- runExitHook := func(f func()) (caughtPanic bool) {
- defer func() {
- if x := recover(); x != nil {
- caughtPanic = true
- }
- }()
- f()
- return
- }
-
- for i := range exitHooks.hooks {
- h := exitHooks.hooks[len(exitHooks.hooks)-i-1]
- if exitCode != 0 && !h.runOnNonZeroExit {
- continue
- }
- if caughtPanic := runExitHook(h.f); caughtPanic {
- throw("internal error: exit hook invoked panic")
- }
- }
- exitHooks.hooks = nil
- exitHooks.runningExitHooks = false
-}
//go:linkname overflowError
//go:linkname divideError
-// used in runtime/coverage and in tests
-//go:linkname addExitHook
-
// used in tests
//go:linkname extraMInUse
//go:linkname getm
"internal/goarch"
"internal/goos"
"internal/runtime/atomic"
+ "internal/runtime/exithook"
"internal/stringslite"
"runtime/internal/sys"
"unsafe"
}
}
+func runExitHooks(code int) {
+ if err := exithook.Run(code); err != nil {
+ throw(err.Error())
+ }
+}
+
// start forcegc helper goroutine
func init() {
go forcegchelper()
import (
"flag"
"os"
+ "internal/runtime/exithook"
_ "unsafe"
)
}
}
-//go:linkname runtime_addExitHook runtime.addExitHook
-func runtime_addExitHook(f func(), runOnNonZeroExit bool)
-
func testSimple() {
f1 := func() { println("foo") }
f2 := func() { println("bar") }
- runtime_addExitHook(f1, false)
- runtime_addExitHook(f2, false)
+ exithook.Add(exithook.Hook{F: f1})
+ exithook.Add(exithook.Hook{F: f2})
// no explicit call to os.Exit
}
func testGoodExit() {
f1 := func() { println("apple") }
f2 := func() { println("orange") }
- runtime_addExitHook(f1, false)
- runtime_addExitHook(f2, false)
+ exithook.Add(exithook.Hook{F: f1})
+ exithook.Add(exithook.Hook{F: f2})
// explicit call to os.Exit
os.Exit(0)
}
f3 := func() { println("blek") }
f4 := func() { println("blub") }
f5 := func() { println("blat") }
- runtime_addExitHook(f1, false)
- runtime_addExitHook(f2, true)
- runtime_addExitHook(f3, false)
- runtime_addExitHook(f4, true)
- runtime_addExitHook(f5, false)
+ exithook.Add(exithook.Hook{F: f1})
+ exithook.Add(exithook.Hook{F: f2, RunOnFailure: true})
+ exithook.Add(exithook.Hook{F: f3})
+ exithook.Add(exithook.Hook{F: f4, RunOnFailure: true})
+ exithook.Add(exithook.Hook{F: f5})
os.Exit(1)
}
f1 := func() { println("ok") }
f2 := func() { panic("BADBADBAD") }
f3 := func() { println("good") }
- runtime_addExitHook(f1, true)
- runtime_addExitHook(f2, true)
- runtime_addExitHook(f3, true)
+ exithook.Add(exithook.Hook{F: f1, RunOnFailure: true})
+ exithook.Add(exithook.Hook{F: f2, RunOnFailure: true})
+ exithook.Add(exithook.Hook{F: f3, RunOnFailure: true})
os.Exit(0)
}
f1 := func() { println("ok") }
f2 := func() { os.Exit(1) }
f3 := func() { println("good") }
- runtime_addExitHook(f1, true)
- runtime_addExitHook(f2, true)
- runtime_addExitHook(f3, true)
+ exithook.Add(exithook.Hook{F: f1, RunOnFailure: true})
+ exithook.Add(exithook.Hook{F: f2, RunOnFailure: true})
+ exithook.Add(exithook.Hook{F: f3, RunOnFailure: true})
os.Exit(1)
}