145
|
1 // Copyright 2019 The Go Authors. All rights reserved.
|
|
2 // Use of this source code is governed by a BSD-style
|
|
3 // license that can be found in the LICENSE file.
|
|
4
|
|
5 // +build ignore
|
|
6
|
|
7 package runtime
|
|
8
|
|
9 import "unsafe"
|
|
10
|
|
11 type ptrAlignError struct {
|
|
12 ptr unsafe.Pointer
|
|
13 elem *_type
|
|
14 n uintptr
|
|
15 }
|
|
16
|
|
17 func (e ptrAlignError) RuntimeError() {}
|
|
18
|
|
19 func (e ptrAlignError) Error() string {
|
|
20 return "runtime error: unsafe pointer conversion"
|
|
21 }
|
|
22
|
|
23 func checkptrAlignment(p unsafe.Pointer, elem *_type, n uintptr) {
|
|
24 // Check that (*[n]elem)(p) is appropriately aligned.
|
|
25 // TODO(mdempsky): What about fieldAlign?
|
|
26 if uintptr(p)&(uintptr(elem.align)-1) != 0 {
|
|
27 panic(ptrAlignError{p, elem, n})
|
|
28 }
|
|
29
|
|
30 // Check that (*[n]elem)(p) doesn't straddle multiple heap objects.
|
|
31 if size := n * elem.size; size > 1 && checkptrBase(p) != checkptrBase(add(p, size-1)) {
|
|
32 panic(ptrAlignError{p, elem, n})
|
|
33 }
|
|
34 }
|
|
35
|
|
36 type ptrArithError struct {
|
|
37 ptr unsafe.Pointer
|
|
38 originals []unsafe.Pointer
|
|
39 }
|
|
40
|
|
41 func (e ptrArithError) RuntimeError() {}
|
|
42
|
|
43 func (e ptrArithError) Error() string {
|
|
44 return "runtime error: unsafe pointer arithmetic"
|
|
45 }
|
|
46
|
|
47 func checkptrArithmetic(p unsafe.Pointer, originals []unsafe.Pointer) {
|
|
48 if 0 < uintptr(p) && uintptr(p) < minLegalPointer {
|
|
49 panic(ptrArithError{p, originals})
|
|
50 }
|
|
51
|
|
52 // Check that if the computed pointer p points into a heap
|
|
53 // object, then one of the original pointers must have pointed
|
|
54 // into the same object.
|
|
55 base := checkptrBase(p)
|
|
56 if base == 0 {
|
|
57 return
|
|
58 }
|
|
59
|
|
60 for _, original := range originals {
|
|
61 if base == checkptrBase(original) {
|
|
62 return
|
|
63 }
|
|
64 }
|
|
65
|
|
66 panic(ptrArithError{p, originals})
|
|
67 }
|
|
68
|
|
69 // checkptrBase returns the base address for the allocation containing
|
|
70 // the address p.
|
|
71 //
|
|
72 // Importantly, if p1 and p2 point into the same variable, then
|
|
73 // checkptrBase(p1) == checkptrBase(p2). However, the converse/inverse
|
|
74 // is not necessarily true as allocations can have trailing padding,
|
|
75 // and multiple variables may be packed into a single allocation.
|
|
76 func checkptrBase(p unsafe.Pointer) uintptr {
|
|
77 // stack
|
|
78 if gp := getg(); gp.stack.lo <= uintptr(p) && uintptr(p) < gp.stack.hi {
|
|
79 // TODO(mdempsky): Walk the stack to identify the
|
|
80 // specific stack frame or even stack object that p
|
|
81 // points into.
|
|
82 //
|
|
83 // In the mean time, use "1" as a pseudo-address to
|
|
84 // represent the stack. This is an invalid address on
|
|
85 // all platforms, so it's guaranteed to be distinct
|
|
86 // from any of the addresses we might return below.
|
|
87 return 1
|
|
88 }
|
|
89
|
|
90 // heap (must check after stack because of #35068)
|
|
91 if base, _, _ := findObject(uintptr(p), 0, 0); base != 0 {
|
|
92 return base
|
|
93 }
|
|
94
|
|
95 // data or bss
|
|
96 for _, datap := range activeModules() {
|
|
97 if datap.data <= uintptr(p) && uintptr(p) < datap.edata {
|
|
98 return datap.data
|
|
99 }
|
|
100 if datap.bss <= uintptr(p) && uintptr(p) < datap.ebss {
|
|
101 return datap.bss
|
|
102 }
|
|
103 }
|
|
104
|
|
105 return 0
|
|
106 }
|