2014-08-19 01:49:59 -06:00
|
|
|
// Copyright 2014 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
|
|
|
|
|
2014-08-21 10:41:09 -06:00
|
|
|
import "unsafe"
|
|
|
|
|
liblink, runtime: diagnose and fix C code running on Go stack
This CL contains compiler+runtime changes that detect C code
running on Go (not g0, not gsignal) stacks, and it contains
corrections for what it detected.
The detection works by changing the C prologue to use a different
stack guard word in the G than Go prologue does. On the g0 and
gsignal stacks, that stack guard word is set to the usual
stack guard value. But on ordinary Go stacks, that stack
guard word is set to ^0, which will make any stack split
check fail. The C prologue then calls morestackc instead
of morestack, and morestackc aborts the program with
a message about running C code on a Go stack.
This check catches all C code running on the Go stack
except NOSPLIT code. The NOSPLIT code is allowed,
so the check is complete. Since it is a dynamic check,
the code must execute to be caught. But unlike the static
checks we've been using in cmd/ld, the dynamic check
works with function pointers and other indirect calls.
For example it caught sigpanic being pushed onto Go
stacks in the signal handlers.
Fixes #8667.
LGTM=khr, iant
R=golang-codereviews, khr, iant
CC=golang-codereviews, r
https://golang.org/cl/133700043
2014-09-08 12:05:23 -06:00
|
|
|
func newsysmon()
|
|
|
|
|
|
|
|
func runtime_init()
|
|
|
|
func main_init()
|
|
|
|
func main_main()
|
|
|
|
|
|
|
|
// The main goroutine.
|
|
|
|
func main() {
|
|
|
|
g := getg()
|
|
|
|
|
|
|
|
// Racectx of m0->g0 is used only as the parent of the main goroutine.
|
|
|
|
// It must not be used for anything else.
|
|
|
|
g.m.g0.racectx = 0
|
|
|
|
|
|
|
|
// Max stack size is 1 GB on 64-bit, 250 MB on 32-bit.
|
|
|
|
// Using decimal instead of binary GB and MB because
|
|
|
|
// they look nicer in the stack overflow failure message.
|
|
|
|
if ptrSize == 8 {
|
|
|
|
maxstacksize = 1000000000
|
|
|
|
} else {
|
|
|
|
maxstacksize = 250000000
|
|
|
|
}
|
|
|
|
|
|
|
|
onM(newsysmon)
|
|
|
|
|
|
|
|
// Lock the main goroutine onto this, the main OS thread,
|
|
|
|
// during initialization. Most programs won't care, but a few
|
|
|
|
// do require certain calls to be made by the main thread.
|
|
|
|
// Those can arrange for main.main to run in the main thread
|
|
|
|
// by calling runtime.LockOSThread during initialization
|
|
|
|
// to preserve the lock.
|
|
|
|
lockOSThread()
|
|
|
|
|
|
|
|
// Defer unlock so that runtime.Goexit during init does the unlock too.
|
|
|
|
needUnlock := true
|
|
|
|
defer func() {
|
|
|
|
if needUnlock {
|
|
|
|
unlockOSThread()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if g.m != &m0 {
|
|
|
|
gothrow("runtime.main not on m0")
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime_init()
|
|
|
|
memstats.enablegc = true // now that runtime is initialized, GC is okay
|
|
|
|
|
|
|
|
main_init()
|
|
|
|
|
|
|
|
needUnlock = false
|
|
|
|
unlockOSThread()
|
|
|
|
|
|
|
|
main_main()
|
|
|
|
if raceenabled {
|
|
|
|
racefini()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make racy client program work: if panicking on
|
|
|
|
// another goroutine at the same time as main returns,
|
|
|
|
// let the other goroutine finish printing the panic trace.
|
|
|
|
// Once it does, it will exit. See issue 3934.
|
|
|
|
if panicking != 0 {
|
|
|
|
gopark(nil, nil, "panicwait")
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(0)
|
|
|
|
for {
|
|
|
|
var x *int32
|
|
|
|
*x = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 10:41:09 -06:00
|
|
|
var parkunlock_c byte
|
|
|
|
|
2014-08-29 01:08:10 -06:00
|
|
|
// start forcegc helper goroutine
|
|
|
|
func init() {
|
2014-09-02 17:18:46 -06:00
|
|
|
go forcegchelper()
|
|
|
|
}
|
|
|
|
|
|
|
|
func forcegchelper() {
|
|
|
|
forcegc.g = getg()
|
|
|
|
forcegc.g.issystem = true
|
|
|
|
for {
|
|
|
|
lock(&forcegc.lock)
|
|
|
|
if forcegc.idle != 0 {
|
|
|
|
gothrow("forcegc: phase error")
|
|
|
|
}
|
|
|
|
atomicstore(&forcegc.idle, 1)
|
|
|
|
goparkunlock(&forcegc.lock, "force gc (idle)")
|
|
|
|
// this goroutine is explicitly resumed by sysmon
|
|
|
|
if debug.gctrace > 0 {
|
|
|
|
println("GC forced")
|
2014-08-29 01:08:10 -06:00
|
|
|
}
|
2014-09-02 17:18:46 -06:00
|
|
|
gogc(1)
|
|
|
|
}
|
2014-08-29 01:08:10 -06:00
|
|
|
}
|
|
|
|
|
2014-08-19 01:49:59 -06:00
|
|
|
// Gosched yields the processor, allowing other goroutines to run. It does not
|
|
|
|
// suspend the current goroutine, so execution resumes automatically.
|
|
|
|
func Gosched() {
|
2014-09-03 09:35:22 -06:00
|
|
|
mcall(gosched_m)
|
2014-08-19 01:49:59 -06:00
|
|
|
}
|
2014-08-21 10:41:09 -06:00
|
|
|
|
|
|
|
// Puts the current goroutine into a waiting state and calls unlockf.
|
|
|
|
// If unlockf returns false, the goroutine is resumed.
|
|
|
|
func gopark(unlockf unsafe.Pointer, lock unsafe.Pointer, reason string) {
|
|
|
|
mp := acquirem()
|
|
|
|
gp := mp.curg
|
2014-09-04 12:19:50 -06:00
|
|
|
status := readgstatus(gp)
|
|
|
|
if status != _Grunning && status != _Gscanrunning {
|
2014-08-21 10:41:09 -06:00
|
|
|
gothrow("gopark: bad g status")
|
|
|
|
}
|
|
|
|
mp.waitlock = lock
|
2014-08-28 14:23:10 -06:00
|
|
|
mp.waitunlockf = unlockf
|
2014-08-21 10:41:09 -06:00
|
|
|
gp.waitreason = reason
|
|
|
|
releasem(mp)
|
|
|
|
// can't do anything that might move the G between Ms here.
|
2014-09-03 09:35:22 -06:00
|
|
|
mcall(park_m)
|
2014-08-21 10:41:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Puts the current goroutine into a waiting state and unlocks the lock.
|
|
|
|
// The goroutine can be made runnable again by calling goready(gp).
|
2014-08-27 21:32:49 -06:00
|
|
|
func goparkunlock(lock *mutex, reason string) {
|
2014-08-21 10:41:09 -06:00
|
|
|
gopark(unsafe.Pointer(&parkunlock_c), unsafe.Pointer(lock), reason)
|
|
|
|
}
|
|
|
|
|
|
|
|
func goready(gp *g) {
|
|
|
|
mp := acquirem()
|
|
|
|
mp.ptrarg[0] = unsafe.Pointer(gp)
|
2014-09-03 09:35:22 -06:00
|
|
|
onM(ready_m)
|
2014-08-21 10:41:09 -06:00
|
|
|
releasem(mp)
|
|
|
|
}
|
|
|
|
|
|
|
|
//go:nosplit
|
|
|
|
func acquireSudog() *sudog {
|
|
|
|
c := gomcache()
|
|
|
|
s := c.sudogcache
|
|
|
|
if s != nil {
|
2014-08-25 10:12:26 -06:00
|
|
|
c.sudogcache = s.next
|
2014-08-21 10:41:09 -06:00
|
|
|
return s
|
|
|
|
}
|
2014-09-07 21:16:12 -06:00
|
|
|
|
|
|
|
// Delicate dance: the semaphore implementation calls
|
|
|
|
// acquireSudog, acquireSudog calls new(sudog),
|
|
|
|
// new calls malloc, malloc can call the garbage collector,
|
|
|
|
// and the garbage collector calls the semaphore implementation
|
|
|
|
// in stoptheworld.
|
|
|
|
// Break the cycle by doing acquirem/releasem around new(sudog).
|
|
|
|
// The acquirem/releasem increments m.locks during new(sudog),
|
|
|
|
// which keeps the garbage collector from being invoked.
|
|
|
|
mp := acquirem()
|
|
|
|
p := new(sudog)
|
|
|
|
releasem(mp)
|
|
|
|
return p
|
2014-08-21 10:41:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
//go:nosplit
|
|
|
|
func releaseSudog(s *sudog) {
|
|
|
|
c := gomcache()
|
2014-08-25 10:12:26 -06:00
|
|
|
s.next = c.sudogcache
|
2014-08-21 10:41:09 -06:00
|
|
|
c.sudogcache = s
|
|
|
|
}
|
2014-09-03 09:10:38 -06:00
|
|
|
|
|
|
|
// funcPC returns the entry PC of the function f.
|
|
|
|
// It assumes that f is a func value. Otherwise the behavior is undefined.
|
2014-09-04 11:51:12 -06:00
|
|
|
//go:nosplit
|
2014-09-03 09:10:38 -06:00
|
|
|
func funcPC(f interface{}) uintptr {
|
|
|
|
return **(**uintptr)(add(unsafe.Pointer(&f), ptrSize))
|
|
|
|
}
|
2014-09-04 19:12:31 -06:00
|
|
|
|
|
|
|
// called from assembly
|
|
|
|
func badmcall(fn func(*g)) {
|
|
|
|
gothrow("runtime: mcall called on m->g0 stack")
|
|
|
|
}
|
|
|
|
|
|
|
|
func badmcall2(fn func(*g)) {
|
|
|
|
gothrow("runtime: mcall function returned")
|
|
|
|
}
|
2014-09-06 11:07:23 -06:00
|
|
|
|
2014-09-06 11:12:47 -06:00
|
|
|
func badreflectcall() {
|
|
|
|
panic("runtime: arg size to reflect.call more than 1GB")
|
|
|
|
}
|
|
|
|
|
2014-09-06 11:07:23 -06:00
|
|
|
func lockedOSThread() bool {
|
|
|
|
gp := getg()
|
|
|
|
return gp.lockedm != nil && gp.m.lockedg != nil
|
|
|
|
}
|