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.
9 func checkptrAlignment(p unsafe.Pointer, elem *_type, n uintptr) {
10 // nil pointer is always suitably aligned (#47430).
15 // Check that (*[n]elem)(p) is appropriately aligned.
16 // Note that we allow unaligned pointers if the types they point to contain
17 // no pointers themselves. See issue 37298.
18 // TODO(mdempsky): What about fieldAlign?
19 if elem.PtrBytes != 0 && uintptr(p)&(uintptr(elem.Align_)-1) != 0 {
20 throw("checkptr: misaligned pointer conversion")
23 // Check that (*[n]elem)(p) doesn't straddle multiple heap objects.
24 // TODO(mdempsky): Fix #46938 so we don't need to worry about overflow here.
25 if checkptrStraddles(p, n*elem.Size_) {
26 throw("checkptr: converted pointer straddles multiple allocations")
30 // checkptrStraddles reports whether the first size-bytes of memory
31 // addressed by ptr is known to straddle more than one Go allocation.
32 func checkptrStraddles(ptr unsafe.Pointer, size uintptr) bool {
37 // Check that add(ptr, size-1) won't overflow. This avoids the risk
38 // of producing an illegal pointer value (assuming ptr is legal).
39 if uintptr(ptr) >= -(size - 1) {
42 end := add(ptr, size-1)
44 // TODO(mdempsky): Detect when [ptr, end] contains Go allocations,
45 // but neither ptr nor end point into one themselves.
47 return checkptrBase(ptr) != checkptrBase(end)
50 func checkptrArithmetic(p unsafe.Pointer, originals []unsafe.Pointer) {
51 if 0 < uintptr(p) && uintptr(p) < minLegalPointer {
52 throw("checkptr: pointer arithmetic computed bad pointer value")
55 // Check that if the computed pointer p points into a heap
56 // object, then one of the original pointers must have pointed
57 // into the same object.
58 base := checkptrBase(p)
63 for _, original := range originals {
64 if base == checkptrBase(original) {
69 throw("checkptr: pointer arithmetic result points to invalid allocation")
72 // checkptrBase returns the base address for the allocation containing
75 // Importantly, if p1 and p2 point into the same variable, then
76 // checkptrBase(p1) == checkptrBase(p2). However, the converse/inverse
77 // is not necessarily true as allocations can have trailing padding,
78 // and multiple variables may be packed into a single allocation.
79 func checkptrBase(p unsafe.Pointer) uintptr {
81 if gp := getg(); gp.stack.lo <= uintptr(p) && uintptr(p) < gp.stack.hi {
82 // TODO(mdempsky): Walk the stack to identify the
83 // specific stack frame or even stack object that p
86 // In the mean time, use "1" as a pseudo-address to
87 // represent the stack. This is an invalid address on
88 // all platforms, so it's guaranteed to be distinct
89 // from any of the addresses we might return below.
93 // heap (must check after stack because of #35068)
94 if base, _, _ := findObject(uintptr(p), 0, 0); base != 0 {
99 for _, datap := range activeModules() {
100 if datap.data <= uintptr(p) && uintptr(p) < datap.edata {
103 if datap.bss <= uintptr(p) && uintptr(p) < datap.ebss {