Source file
src/runtime/checkptr.go
Documentation: runtime
1
2
3
4
5 package runtime
6
7 import "unsafe"
8
9 func checkptrAlignment(p unsafe.Pointer, elem *_type, n uintptr) {
10
11
12
13
14 if elem.ptrdata != 0 && uintptr(p)&(uintptr(elem.align)-1) != 0 {
15 throw("checkptr: misaligned pointer conversion")
16 }
17
18
19 if size := n * elem.size; size > 1 && checkptrBase(p) != checkptrBase(add(p, size-1)) {
20 throw("checkptr: converted pointer straddles multiple allocations")
21 }
22 }
23
24 func checkptrArithmetic(p unsafe.Pointer, originals []unsafe.Pointer) {
25 if 0 < uintptr(p) && uintptr(p) < minLegalPointer {
26 throw("checkptr: pointer arithmetic computed bad pointer value")
27 }
28
29
30
31
32 base := checkptrBase(p)
33 if base == 0 {
34 return
35 }
36
37 for _, original := range originals {
38 if base == checkptrBase(original) {
39 return
40 }
41 }
42
43 throw("checkptr: pointer arithmetic result points to invalid allocation")
44 }
45
46
47
48
49
50
51
52
53 func checkptrBase(p unsafe.Pointer) uintptr {
54
55 if gp := getg(); gp.stack.lo <= uintptr(p) && uintptr(p) < gp.stack.hi {
56
57
58
59
60
61
62
63
64 return 1
65 }
66
67
68 if base, _, _ := findObject(uintptr(p), 0, 0); base != 0 {
69 return base
70 }
71
72
73 for _, datap := range activeModules() {
74 if datap.data <= uintptr(p) && uintptr(p) < datap.edata {
75 return datap.data
76 }
77 if datap.bss <= uintptr(p) && uintptr(p) < datap.ebss {
78 return datap.bss
79 }
80 }
81
82 return 0
83 }
84
View as plain text