1
0
mirror of https://github.com/golang/go synced 2024-11-19 00:24:41 -07:00

runtime: introduce, use funcPC to convert Go func to PC

This removes the ** unsafe hack.

Real bug fixed at chan.go:101.

LGTM=dave, r, iant
R=golang-codereviews, dave, r, iant
CC=dvyukov, golang-codereviews, khr
https://golang.org/cl/140870044
This commit is contained in:
Russ Cox 2014-09-03 11:10:38 -04:00
parent 2c780a35b9
commit 0e07f1c99f
9 changed files with 33 additions and 72 deletions

View File

@ -82,9 +82,7 @@ func chansend1(t *chantype, c *hchan, elem unsafe.Pointer) {
*/ */
func chansend(t *chantype, c *hchan, ep unsafe.Pointer, block bool, callerpc uintptr) bool { func chansend(t *chantype, c *hchan, ep unsafe.Pointer, block bool, callerpc uintptr) bool {
if raceenabled { if raceenabled {
fn := chansend raceReadObjectPC(t.elem, ep, callerpc, funcPC(chansend))
pc := **(**uintptr)(unsafe.Pointer(&fn))
raceReadObjectPC(t.elem, ep, callerpc, pc)
} }
if c == nil { if c == nil {
@ -100,9 +98,7 @@ func chansend(t *chantype, c *hchan, ep unsafe.Pointer, block bool, callerpc uin
} }
if raceenabled { if raceenabled {
fn := chansend racereadpc(unsafe.Pointer(c), callerpc, funcPC(chansend))
pc := **(**uintptr)(unsafe.Pointer(&fn))
racereadpc(unsafe.Pointer(c), pc, callerpc)
} }
// Fast path: check for failed non-blocking operation without acquiring the lock. // Fast path: check for failed non-blocking operation without acquiring the lock.
@ -269,9 +265,7 @@ func closechan(c *hchan) {
if raceenabled { if raceenabled {
callerpc := getcallerpc(unsafe.Pointer(&c)) callerpc := getcallerpc(unsafe.Pointer(&c))
fn := closechan racewritepc(unsafe.Pointer(c), callerpc, funcPC(closechan))
pc := **(**uintptr)(unsafe.Pointer(&fn))
racewritepc(unsafe.Pointer(c), callerpc, pc)
racerelease(unsafe.Pointer(c)) racerelease(unsafe.Pointer(c))
} }

View File

@ -289,8 +289,7 @@ func (p *cpuProfile) flushlog() bool {
log := &p.log[p.toggle] log := &p.log[p.toggle]
q := uintptr(0) q := uintptr(0)
if p.lost > 0 { if p.lost > 0 {
f := lostProfileData lostPC := funcPC(lostProfileData)
lostPC := **(**uintptr)(unsafe.Pointer(&f))
log[0] = p.lost log[0] = p.lost
log[1] = 1 log[1] = 1
log[2] = lostPC log[2] = lostPC

View File

@ -86,7 +86,7 @@ func ParForSetup(desc *ParFor, nthr, n uint32, ctx *byte, wait bool, body func(*
mp := acquirem() mp := acquirem()
mp.ptrarg[0] = unsafe.Pointer(desc) mp.ptrarg[0] = unsafe.Pointer(desc)
mp.ptrarg[1] = unsafe.Pointer(ctx) mp.ptrarg[1] = unsafe.Pointer(ctx)
mp.ptrarg[2] = **(**unsafe.Pointer)(unsafe.Pointer(&body)) mp.ptrarg[2] = unsafe.Pointer(funcPC(body)) // TODO(rsc): Should be a scalar.
mp.scalararg[0] = uintptr(nthr) mp.scalararg[0] = uintptr(nthr)
mp.scalararg[1] = uintptr(n) mp.scalararg[1] = uintptr(n)
mp.scalararg[2] = 0 mp.scalararg[2] = 0

View File

@ -235,8 +235,7 @@ func makemap(t *maptype, hint int64) *hmap {
func mapaccess1(t *maptype, h *hmap, key unsafe.Pointer) unsafe.Pointer { func mapaccess1(t *maptype, h *hmap, key unsafe.Pointer) unsafe.Pointer {
if raceenabled && h != nil { if raceenabled && h != nil {
callerpc := getcallerpc(unsafe.Pointer(&t)) callerpc := getcallerpc(unsafe.Pointer(&t))
fn := mapaccess1 pc := funcPC(mapaccess1)
pc := **(**uintptr)(unsafe.Pointer(&fn))
racereadpc(unsafe.Pointer(h), callerpc, pc) racereadpc(unsafe.Pointer(h), callerpc, pc)
raceReadObjectPC(t.key, key, callerpc, pc) raceReadObjectPC(t.key, key, callerpc, pc)
} }
@ -284,8 +283,7 @@ func mapaccess1(t *maptype, h *hmap, key unsafe.Pointer) unsafe.Pointer {
func mapaccess2(t *maptype, h *hmap, key unsafe.Pointer) (unsafe.Pointer, bool) { func mapaccess2(t *maptype, h *hmap, key unsafe.Pointer) (unsafe.Pointer, bool) {
if raceenabled && h != nil { if raceenabled && h != nil {
callerpc := getcallerpc(unsafe.Pointer(&t)) callerpc := getcallerpc(unsafe.Pointer(&t))
fn := mapaccess2 pc := funcPC(mapaccess2)
pc := **(**uintptr)(unsafe.Pointer(&fn))
racereadpc(unsafe.Pointer(h), callerpc, pc) racereadpc(unsafe.Pointer(h), callerpc, pc)
raceReadObjectPC(t.key, key, callerpc, pc) raceReadObjectPC(t.key, key, callerpc, pc)
} }
@ -379,8 +377,7 @@ func mapassign1(t *maptype, h *hmap, key unsafe.Pointer, val unsafe.Pointer) {
} }
if raceenabled { if raceenabled {
callerpc := getcallerpc(unsafe.Pointer(&t)) callerpc := getcallerpc(unsafe.Pointer(&t))
fn := mapassign1 pc := funcPC(mapassign1)
pc := **(**uintptr)(unsafe.Pointer(&fn))
racewritepc(unsafe.Pointer(h), callerpc, pc) racewritepc(unsafe.Pointer(h), callerpc, pc)
raceReadObjectPC(t.key, key, callerpc, pc) raceReadObjectPC(t.key, key, callerpc, pc)
raceReadObjectPC(t.elem, val, callerpc, pc) raceReadObjectPC(t.elem, val, callerpc, pc)
@ -488,8 +485,7 @@ again:
func mapdelete(t *maptype, h *hmap, key unsafe.Pointer) { func mapdelete(t *maptype, h *hmap, key unsafe.Pointer) {
if raceenabled && h != nil { if raceenabled && h != nil {
callerpc := getcallerpc(unsafe.Pointer(&t)) callerpc := getcallerpc(unsafe.Pointer(&t))
fn := mapdelete pc := funcPC(mapdelete)
pc := **(**uintptr)(unsafe.Pointer(&fn))
racewritepc(unsafe.Pointer(h), callerpc, pc) racewritepc(unsafe.Pointer(h), callerpc, pc)
raceReadObjectPC(t.key, key, callerpc, pc) raceReadObjectPC(t.key, key, callerpc, pc)
} }
@ -545,9 +541,7 @@ func mapiterinit(t *maptype, h *hmap, it *hiter) {
if raceenabled && h != nil { if raceenabled && h != nil {
callerpc := getcallerpc(unsafe.Pointer(&t)) callerpc := getcallerpc(unsafe.Pointer(&t))
fn := mapiterinit racereadpc(unsafe.Pointer(h), callerpc, funcPC(mapiterinit))
pc := **(**uintptr)(unsafe.Pointer(&fn))
racereadpc(unsafe.Pointer(h), callerpc, pc)
} }
if h == nil || h.count == 0 { if h == nil || h.count == 0 {
@ -591,9 +585,7 @@ func mapiternext(it *hiter) {
h := it.h h := it.h
if raceenabled { if raceenabled {
callerpc := getcallerpc(unsafe.Pointer(&it)) callerpc := getcallerpc(unsafe.Pointer(&it))
fn := mapiternext racereadpc(unsafe.Pointer(h), callerpc, funcPC(mapiternext))
pc := **(**uintptr)(unsafe.Pointer(&fn))
racereadpc(unsafe.Pointer(h), callerpc, pc)
} }
t := it.t t := it.t
bucket := it.bucket bucket := it.bucket
@ -942,9 +934,7 @@ func reflect_maplen(h *hmap) int {
} }
if raceenabled { if raceenabled {
callerpc := getcallerpc(unsafe.Pointer(&h)) callerpc := getcallerpc(unsafe.Pointer(&h))
fn := reflect_maplen racereadpc(unsafe.Pointer(h), callerpc, funcPC(reflect_maplen))
pc := **(**uintptr)(unsafe.Pointer(&fn))
racereadpc(unsafe.Pointer(h), callerpc, pc)
} }
return h.count return h.count
} }

View File

@ -11,9 +11,7 @@ import (
func mapaccess1_fast32(t *maptype, h *hmap, key uint32) unsafe.Pointer { func mapaccess1_fast32(t *maptype, h *hmap, key uint32) unsafe.Pointer {
if raceenabled && h != nil { if raceenabled && h != nil {
callerpc := getcallerpc(unsafe.Pointer(&t)) callerpc := getcallerpc(unsafe.Pointer(&t))
fn := mapaccess1_fast32 racereadpc(unsafe.Pointer(h), callerpc, funcPC(mapaccess1_fast32))
pc := **(**uintptr)(unsafe.Pointer(&fn))
racereadpc(unsafe.Pointer(h), callerpc, pc)
} }
if h == nil || h.count == 0 { if h == nil || h.count == 0 {
return unsafe.Pointer(t.elem.zero) return unsafe.Pointer(t.elem.zero)
@ -55,9 +53,7 @@ func mapaccess1_fast32(t *maptype, h *hmap, key uint32) unsafe.Pointer {
func mapaccess2_fast32(t *maptype, h *hmap, key uint32) (unsafe.Pointer, bool) { func mapaccess2_fast32(t *maptype, h *hmap, key uint32) (unsafe.Pointer, bool) {
if raceenabled && h != nil { if raceenabled && h != nil {
callerpc := getcallerpc(unsafe.Pointer(&t)) callerpc := getcallerpc(unsafe.Pointer(&t))
fn := mapaccess2_fast32 racereadpc(unsafe.Pointer(h), callerpc, funcPC(mapaccess2_fast32))
pc := **(**uintptr)(unsafe.Pointer(&fn))
racereadpc(unsafe.Pointer(h), callerpc, pc)
} }
if h == nil || h.count == 0 { if h == nil || h.count == 0 {
return unsafe.Pointer(t.elem.zero), false return unsafe.Pointer(t.elem.zero), false
@ -99,9 +95,7 @@ func mapaccess2_fast32(t *maptype, h *hmap, key uint32) (unsafe.Pointer, bool) {
func mapaccess1_fast64(t *maptype, h *hmap, key uint64) unsafe.Pointer { func mapaccess1_fast64(t *maptype, h *hmap, key uint64) unsafe.Pointer {
if raceenabled && h != nil { if raceenabled && h != nil {
callerpc := getcallerpc(unsafe.Pointer(&t)) callerpc := getcallerpc(unsafe.Pointer(&t))
fn := mapaccess1_fast64 racereadpc(unsafe.Pointer(h), callerpc, funcPC(mapaccess1_fast64))
pc := **(**uintptr)(unsafe.Pointer(&fn))
racereadpc(unsafe.Pointer(h), callerpc, pc)
} }
if h == nil || h.count == 0 { if h == nil || h.count == 0 {
return unsafe.Pointer(t.elem.zero) return unsafe.Pointer(t.elem.zero)
@ -143,9 +137,7 @@ func mapaccess1_fast64(t *maptype, h *hmap, key uint64) unsafe.Pointer {
func mapaccess2_fast64(t *maptype, h *hmap, key uint64) (unsafe.Pointer, bool) { func mapaccess2_fast64(t *maptype, h *hmap, key uint64) (unsafe.Pointer, bool) {
if raceenabled && h != nil { if raceenabled && h != nil {
callerpc := getcallerpc(unsafe.Pointer(&t)) callerpc := getcallerpc(unsafe.Pointer(&t))
fn := mapaccess2_fast64 racereadpc(unsafe.Pointer(h), callerpc, funcPC(mapaccess2_fast64))
pc := **(**uintptr)(unsafe.Pointer(&fn))
racereadpc(unsafe.Pointer(h), callerpc, pc)
} }
if h == nil || h.count == 0 { if h == nil || h.count == 0 {
return unsafe.Pointer(t.elem.zero), false return unsafe.Pointer(t.elem.zero), false
@ -187,9 +179,7 @@ func mapaccess2_fast64(t *maptype, h *hmap, key uint64) (unsafe.Pointer, bool) {
func mapaccess1_faststr(t *maptype, h *hmap, ky string) unsafe.Pointer { func mapaccess1_faststr(t *maptype, h *hmap, ky string) unsafe.Pointer {
if raceenabled && h != nil { if raceenabled && h != nil {
callerpc := getcallerpc(unsafe.Pointer(&t)) callerpc := getcallerpc(unsafe.Pointer(&t))
fn := mapaccess1_faststr racereadpc(unsafe.Pointer(h), callerpc, funcPC(mapaccess1_faststr))
pc := **(**uintptr)(unsafe.Pointer(&fn))
racereadpc(unsafe.Pointer(h), callerpc, pc)
} }
if h == nil || h.count == 0 { if h == nil || h.count == 0 {
return unsafe.Pointer(t.elem.zero) return unsafe.Pointer(t.elem.zero)
@ -291,9 +281,7 @@ dohash:
func mapaccess2_faststr(t *maptype, h *hmap, ky string) (unsafe.Pointer, bool) { func mapaccess2_faststr(t *maptype, h *hmap, ky string) (unsafe.Pointer, bool) {
if raceenabled && h != nil { if raceenabled && h != nil {
callerpc := getcallerpc(unsafe.Pointer(&t)) callerpc := getcallerpc(unsafe.Pointer(&t))
fn := mapaccess2_faststr racereadpc(unsafe.Pointer(h), callerpc, funcPC(mapaccess2_faststr))
pc := **(**uintptr)(unsafe.Pointer(&fn))
racereadpc(unsafe.Pointer(h), callerpc, pc)
} }
if h == nil || h.count == 0 { if h == nil || h.count == 0 {
return unsafe.Pointer(t.elem.zero), false return unsafe.Pointer(t.elem.zero), false

View File

@ -110,3 +110,9 @@ func releaseSudog(s *sudog) {
s.next = c.sudogcache s.next = c.sudogcache
c.sudogcache = s c.sudogcache = s
} }
// funcPC returns the entry PC of the function f.
// It assumes that f is a func value. Otherwise the behavior is undefined.
func funcPC(f interface{}) uintptr {
return **(**uintptr)(add(unsafe.Pointer(&f), ptrSize))
}

View File

@ -13,17 +13,10 @@ const (
) )
var ( var (
chansendpc uintptr chansendpc = funcPC(chansend)
chanrecvpc uintptr chanrecvpc = funcPC(chanrecv)
) )
func init() {
f := chansend
chansendpc = **(**uintptr)(unsafe.Pointer(&f))
g := chanrecv
chanrecvpc = **(**uintptr)(unsafe.Pointer(&g))
}
func selectsize(size uintptr) uintptr { func selectsize(size uintptr) uintptr {
selsize := unsafe.Sizeof(_select{}) + selsize := unsafe.Sizeof(_select{}) +
(size-1)*unsafe.Sizeof(_select{}.scase[0]) + (size-1)*unsafe.Sizeof(_select{}.scase[0]) +
@ -286,7 +279,6 @@ func selectgoImpl(sel *_select) (uintptr, uint16) {
k *scase k *scase
sglist *sudog sglist *sudog
sgnext *sudog sgnext *sudog
fn func(*g, *_select) bool
) )
loop: loop:
@ -371,8 +363,7 @@ loop:
// wait for someone to wake us up // wait for someone to wake us up
gp.param = nil gp.param = nil
fn = selparkcommit gopark(unsafe.Pointer(funcPC(selparkcommit)), unsafe.Pointer(sel), "select")
gopark(**(**unsafe.Pointer)(unsafe.Pointer(&fn)), unsafe.Pointer(sel), "select")
// someone woke us up // someone woke us up
sellock(sel) sellock(sel)

View File

@ -48,9 +48,7 @@ func growslice(t *slicetype, old sliceStruct, n int64) sliceStruct {
if raceenabled { if raceenabled {
callerpc := getcallerpc(unsafe.Pointer(&t)) callerpc := getcallerpc(unsafe.Pointer(&t))
fn := growslice racereadrangepc(old.array, old.len*int(t.elem.size), callerpc, funcPC(growslice))
pc := **(**uintptr)(unsafe.Pointer(&fn))
racereadrangepc(old.array, old.len*int(t.elem.size), callerpc, pc)
} }
et := t.elem et := t.elem
@ -105,8 +103,7 @@ func slicecopy(to sliceStruct, fm sliceStruct, width uintptr) int {
if raceenabled { if raceenabled {
callerpc := getcallerpc(unsafe.Pointer(&to)) callerpc := getcallerpc(unsafe.Pointer(&to))
fn := slicecopy pc := funcPC(slicecopy)
pc := **(**uintptr)(unsafe.Pointer(&fn))
racewriterangepc(to.array, n*int(width), callerpc, pc) racewriterangepc(to.array, n*int(width), callerpc, pc)
racereadrangepc(fm.array, n*int(width), callerpc, pc) racereadrangepc(fm.array, n*int(width), callerpc, pc)
} }
@ -133,8 +130,7 @@ func slicestringcopy(to []byte, fm string) int {
if raceenabled { if raceenabled {
callerpc := getcallerpc(unsafe.Pointer(&to)) callerpc := getcallerpc(unsafe.Pointer(&to))
fn := slicestringcopy pc := funcPC(slicestringcopy)
pc := **(**uintptr)(unsafe.Pointer(&fn))
racewriterangepc(unsafe.Pointer(&to[0]), n, callerpc, pc) racewriterangepc(unsafe.Pointer(&to[0]), n, callerpc, pc)
} }

View File

@ -61,11 +61,10 @@ func concatstring5(a [5]string) string {
func slicebytetostring(b []byte) string { func slicebytetostring(b []byte) string {
if raceenabled && len(b) > 0 { if raceenabled && len(b) > 0 {
fn := slicebytetostring
racereadrangepc(unsafe.Pointer(&b[0]), racereadrangepc(unsafe.Pointer(&b[0]),
len(b), len(b),
getcallerpc(unsafe.Pointer(&b)), getcallerpc(unsafe.Pointer(&b)),
**(**uintptr)(unsafe.Pointer(&fn))) funcPC(slicebytetostring))
} }
s, c := rawstring(len(b)) s, c := rawstring(len(b))
copy(c, b) copy(c, b)
@ -82,11 +81,10 @@ func slicebytetostringtmp(b []byte) string {
// m is a string-keyed map and k is a []byte. // m is a string-keyed map and k is a []byte.
if raceenabled && len(b) > 0 { if raceenabled && len(b) > 0 {
fn := slicebytetostringtmp
racereadrangepc(unsafe.Pointer(&b[0]), racereadrangepc(unsafe.Pointer(&b[0]),
len(b), len(b),
getcallerpc(unsafe.Pointer(&b)), getcallerpc(unsafe.Pointer(&b)),
**(**uintptr)(unsafe.Pointer(&fn))) funcPC(slicebytetostringtmp))
} }
return *(*string)(unsafe.Pointer(&b)) return *(*string)(unsafe.Pointer(&b))
} }
@ -120,11 +118,10 @@ func stringtoslicerune(s string) []rune {
func slicerunetostring(a []rune) string { func slicerunetostring(a []rune) string {
if raceenabled && len(a) > 0 { if raceenabled && len(a) > 0 {
fn := slicerunetostring
racereadrangepc(unsafe.Pointer(&a[0]), racereadrangepc(unsafe.Pointer(&a[0]),
len(a)*int(unsafe.Sizeof(a[0])), len(a)*int(unsafe.Sizeof(a[0])),
getcallerpc(unsafe.Pointer(&a)), getcallerpc(unsafe.Pointer(&a)),
**(**uintptr)(unsafe.Pointer(&fn))) funcPC(slicerunetostring))
} }
var dum [4]byte var dum [4]byte
size1 := 0 size1 := 0