Commit 6c67dd90 authored by Russ Cox's avatar Russ Cox

runtime: remove some overuse of uintptr/unsafe.Pointer

Now 'go vet runtime' only shows:

        malloc.go:200: possible misuse of unsafe.Pointer
        malloc.go:214: possible misuse of unsafe.Pointer
        malloc.go:250: possible misuse of unsafe.Pointer
        stubs.go:167: possible misuse of unsafe.Pointer

Those are all unavoidable.

LGTM=josharian
R=golang-codereviews, dvyukov, josharian
CC=dave, golang-codereviews
https://golang.org/cl/135730043
parent 7dc25960
...@@ -85,7 +85,7 @@ type errorCString struct{ cstr unsafe.Pointer } ...@@ -85,7 +85,7 @@ type errorCString struct{ cstr unsafe.Pointer }
func (e errorCString) RuntimeError() {} func (e errorCString) RuntimeError() {}
func (e errorCString) Error() string { func (e errorCString) Error() string {
return "runtime error: " + cstringToGo(uintptr(e.cstr)) return "runtime error: " + cstringToGo(e.cstr)
} }
// For calling from C. // For calling from C.
......
...@@ -148,10 +148,10 @@ type stringStruct struct { ...@@ -148,10 +148,10 @@ type stringStruct struct {
len int len int
} }
func cstringToGo(str uintptr) (s string) { func cstringToGo(str unsafe.Pointer) (s string) {
i := 0 i := 0
for ; ; i++ { for ; ; i++ {
if *(*byte)(unsafe.Pointer(str + uintptr(i))) == 0 { if *(*byte)(unsafe.Pointer(uintptr(str) + uintptr(i))) == 0 {
break break
} }
} }
......
...@@ -49,7 +49,8 @@ func add(p unsafe.Pointer, x uintptr) unsafe.Pointer { ...@@ -49,7 +49,8 @@ func add(p unsafe.Pointer, x uintptr) unsafe.Pointer {
// n must be a power of 2 // n must be a power of 2
func roundup(p unsafe.Pointer, n uintptr) unsafe.Pointer { func roundup(p unsafe.Pointer, n uintptr) unsafe.Pointer {
return unsafe.Pointer((uintptr(p) + n - 1) &^ (n - 1)) delta := -uintptr(p) & (n - 1)
return unsafe.Pointer(uintptr(p) + delta)
} }
// in stubs.goc // in stubs.goc
......
...@@ -20,15 +20,16 @@ struct Ftab ...@@ -20,15 +20,16 @@ struct Ftab
}; };
extern byte pclntab[]; extern byte pclntab[];
extern byte epclntab[];
static Ftab *ftab; static Ftab *ftab;
extern uintptr runtime·nftab; static uintptr runtime·nftab;
static uint32 *filetab; static uint32 *filetab;
extern uint32 runtime·nfiletab; static uint32 runtime·nfiletab;
extern uintptr runtime·pclntab; extern Slice runtime·pclntab;
extern uintptr runtime·ftab0; extern Slice runtime·ftabs;
extern uintptr runtime·filetab0; extern Slice runtime·filetab;
extern uint32 runtime·pcquantum; extern uint32 runtime·pcquantum;
static String end = { (uint8*)"end", 3 }; static String end = { (uint8*)"end", 3 };
...@@ -65,9 +66,18 @@ runtime·symtabinit(void) ...@@ -65,9 +66,18 @@ runtime·symtabinit(void)
runtime·nfiletab = filetab[0]; runtime·nfiletab = filetab[0];
runtime·pcquantum = PCQuantum; runtime·pcquantum = PCQuantum;
runtime·pclntab = (uintptr)pclntab;
runtime·ftab0 = (uintptr)ftab; runtime·pclntab.array = (byte*)pclntab;
runtime·filetab0 = (uintptr)filetab; runtime·pclntab.len = (byte*)epclntab - (byte*)pclntab;
runtime·pclntab.cap = runtime·pclntab.len;
runtime·ftabs.array = (byte*)ftab;
runtime·ftabs.len = runtime·nftab+1;
runtime·ftabs.cap = runtime·ftabs.len;
runtime·filetab.array = (byte*)filetab;
runtime·filetab.len = filetab[0];
runtime·filetab.cap = runtime·filetab.len;
} }
static uint32 static uint32
......
...@@ -9,22 +9,22 @@ import "unsafe" ...@@ -9,22 +9,22 @@ import "unsafe"
// FuncForPC returns a *Func describing the function that contains the // FuncForPC returns a *Func describing the function that contains the
// given program counter address, or else nil. // given program counter address, or else nil.
func FuncForPC(pc uintptr) *Func { func FuncForPC(pc uintptr) *Func {
if nftab == 0 { if len(ftabs) == 0 {
return nil return nil
} }
if pc < ftabi(0).entry || pc >= ftabi(nftab).entry { if pc < ftabs[0].entry || pc >= ftabs[len(ftabs)-1].entry {
return nil return nil
} }
// binary search to find func with entry <= pc. // binary search to find func with entry <= pc.
lo := uintptr(0) lo := 0
nf := nftab nf := len(ftabs) - 1 // last entry is sentinel
for nf > 0 { for nf > 0 {
n := nf / 2 n := nf / 2
f := ftabi(lo + n) f := &ftabs[lo+n]
if f.entry <= pc && pc < ftabi(lo+n+1).entry { if f.entry <= pc && pc < ftabs[lo+n+1].entry {
return (*Func)(unsafe.Pointer(pclntab + f.funcoff)) return (*Func)(unsafe.Pointer(&pclntab[f.funcoff]))
} else if pc < f.entry { } else if pc < f.entry {
nf = n nf = n
} else { } else {
...@@ -39,7 +39,7 @@ func FuncForPC(pc uintptr) *Func { ...@@ -39,7 +39,7 @@ func FuncForPC(pc uintptr) *Func {
// Name returns the name of the function. // Name returns the name of the function.
func (f *Func) Name() string { func (f *Func) Name() string {
return cstringToGo(pclntab + uintptr(f.nameoff)) return cstringToGo(unsafe.Pointer(&pclntab[f.nameoff]))
} }
// Entry returns the entry address of the function. // Entry returns the entry address of the function.
...@@ -52,15 +52,15 @@ func (f *Func) Entry() uintptr { ...@@ -52,15 +52,15 @@ func (f *Func) Entry() uintptr {
// The result will not be accurate if pc is not a program // The result will not be accurate if pc is not a program
// counter within f. // counter within f.
func (f *Func) FileLine(pc uintptr) (file string, line int) { func (f *Func) FileLine(pc uintptr) (file string, line int) {
fileno := f.pcvalue(f.pcfile, pc) fileno := int(f.pcvalue(f.pcfile, pc))
if fileno == -1 || fileno >= int32(nfiletab) { if fileno == -1 || fileno >= len(filetab) {
return "?", 0 return "?", 0
} }
line = int(f.pcvalue(f.pcln, pc)) line = int(f.pcvalue(f.pcln, pc))
if line == -1 { if line == -1 {
return "?", 0 return "?", 0
} }
file = cstringToGo(pclntab + uintptr(filetabi(uintptr(fileno)))) file = cstringToGo(unsafe.Pointer(&pclntab[filetab[fileno]]))
return file, line return file, line
} }
...@@ -69,10 +69,15 @@ func (f *Func) pcvalue(off int32, targetpc uintptr) int32 { ...@@ -69,10 +69,15 @@ func (f *Func) pcvalue(off int32, targetpc uintptr) int32 {
if off == 0 { if off == 0 {
return -1 return -1
} }
p := pclntab + uintptr(off) p := pclntab[off:]
pc := f.entry pc := f.entry
val := int32(-1) val := int32(-1)
for step(&p, &pc, &val, pc == f.entry) { for {
var ok bool
p, ok = step(p, &pc, &val, pc == f.entry)
if !ok {
break
}
if targetpc < pc { if targetpc < pc {
return val return val
} }
...@@ -81,10 +86,10 @@ func (f *Func) pcvalue(off int32, targetpc uintptr) int32 { ...@@ -81,10 +86,10 @@ func (f *Func) pcvalue(off int32, targetpc uintptr) int32 {
} }
// step advances to the next pc, value pair in the encoded table. // step advances to the next pc, value pair in the encoded table.
func step(p *uintptr, pc *uintptr, val *int32, first bool) bool { func step(p []byte, pc *uintptr, val *int32, first bool) (newp []byte, ok bool) {
uvdelta := readvarint(p) p, uvdelta := readvarint(p)
if uvdelta == 0 && !first { if uvdelta == 0 && !first {
return false return nil, false
} }
if uvdelta&1 != 0 { if uvdelta&1 != 0 {
uvdelta = ^(uvdelta >> 1) uvdelta = ^(uvdelta >> 1)
...@@ -92,36 +97,32 @@ func step(p *uintptr, pc *uintptr, val *int32, first bool) bool { ...@@ -92,36 +97,32 @@ func step(p *uintptr, pc *uintptr, val *int32, first bool) bool {
uvdelta >>= 1 uvdelta >>= 1
} }
vdelta := int32(uvdelta) vdelta := int32(uvdelta)
pcdelta := readvarint(p) * pcquantum p, pcdelta := readvarint(p)
*pc += uintptr(pcdelta) *pc += uintptr(pcdelta * pcquantum)
*val += vdelta *val += vdelta
return true return p, true
} }
// readvarint reads a varint from *p and advances *p. // readvarint reads a varint from p.
func readvarint(pp *uintptr) uint32 { func readvarint(p []byte) (newp []byte, val uint32) {
var v, shift uint32 var v, shift uint32
p := *pp
for { for {
b := *(*byte)(unsafe.Pointer(p)) b := p[0]
p++ p = p[1:]
v |= (uint32(b) & 0x7F) << shift v |= (uint32(b) & 0x7F) << shift
if b&0x80 == 0 { if b&0x80 == 0 {
break break
} }
shift += 7 shift += 7
} }
*pp = p return p, v
return v
} }
// Populated by runtime·symtabinit during bootstrapping. Treat as immutable. // Populated by runtime·symtabinit during bootstrapping. Treat as immutable.
var ( var (
pclntab uintptr // address of pclntab pclntab []byte
ftab0 uintptr // address of first ftab entry ftabs []ftab
nftab uintptr filetab []uint32
filetab0 uintptr // address of first filetab entry
nfiletab uint32
pcquantum uint32 pcquantum uint32
) )
...@@ -143,11 +144,3 @@ type ftab struct { ...@@ -143,11 +144,3 @@ type ftab struct {
entry uintptr entry uintptr
funcoff uintptr funcoff uintptr
} }
func ftabi(i uintptr) (f ftab) {
return *(*ftab)(unsafe.Pointer(ftab0 + i*unsafe.Sizeof(f)))
}
func filetabi(i uintptr) (f uint32) {
return *(*uint32)(unsafe.Pointer(filetab0 + i*unsafe.Sizeof(f)))
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment