Commit 56d6b879 authored by Matthew Dempsky's avatar Matthew Dempsky

runtime: change checkptr to use throw instead of panic

Updates #34964.

Change-Id: I5afb2c1e77a9a47358a1d0d108c4a787d7172b94
Reviewed-on: https://go-review.googlesource.com/c/go/+/214217
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarAustin Clements <austin@google.com>
parent 65219650
...@@ -6,45 +6,22 @@ package runtime ...@@ -6,45 +6,22 @@ package runtime
import "unsafe" import "unsafe"
type ptrAlignError struct {
ptr unsafe.Pointer
elem *_type
n uintptr
}
func (e ptrAlignError) RuntimeError() {}
func (e ptrAlignError) Error() string {
return "runtime error: unsafe pointer conversion"
}
func checkptrAlignment(p unsafe.Pointer, elem *_type, n uintptr) { func checkptrAlignment(p unsafe.Pointer, elem *_type, n uintptr) {
// Check that (*[n]elem)(p) is appropriately aligned. // Check that (*[n]elem)(p) is appropriately aligned.
// TODO(mdempsky): What about fieldAlign? // TODO(mdempsky): What about fieldAlign?
if uintptr(p)&(uintptr(elem.align)-1) != 0 { if uintptr(p)&(uintptr(elem.align)-1) != 0 {
panic(ptrAlignError{p, elem, n}) throw("checkptr: unsafe pointer conversion")
} }
// Check that (*[n]elem)(p) doesn't straddle multiple heap objects. // Check that (*[n]elem)(p) doesn't straddle multiple heap objects.
if size := n * elem.size; size > 1 && checkptrBase(p) != checkptrBase(add(p, size-1)) { if size := n * elem.size; size > 1 && checkptrBase(p) != checkptrBase(add(p, size-1)) {
panic(ptrAlignError{p, elem, n}) throw("checkptr: unsafe pointer conversion")
} }
} }
type ptrArithError struct {
ptr unsafe.Pointer
originals []unsafe.Pointer
}
func (e ptrArithError) RuntimeError() {}
func (e ptrArithError) Error() string {
return "runtime error: unsafe pointer arithmetic"
}
func checkptrArithmetic(p unsafe.Pointer, originals []unsafe.Pointer) { func checkptrArithmetic(p unsafe.Pointer, originals []unsafe.Pointer) {
if 0 < uintptr(p) && uintptr(p) < minLegalPointer { if 0 < uintptr(p) && uintptr(p) < minLegalPointer {
panic(ptrArithError{p, originals}) throw("checkptr: unsafe pointer arithmetic")
} }
// Check that if the computed pointer p points into a heap // Check that if the computed pointer p points into a heap
...@@ -61,7 +38,7 @@ func checkptrArithmetic(p unsafe.Pointer, originals []unsafe.Pointer) { ...@@ -61,7 +38,7 @@ func checkptrArithmetic(p unsafe.Pointer, originals []unsafe.Pointer) {
} }
} }
panic(ptrArithError{p, originals}) throw("checkptr: unsafe pointer arithmetic")
} }
// checkptrBase returns the base address for the allocation containing // checkptrBase returns the base address for the allocation containing
......
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