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.
11 func checkptrAlignment(p unsafe
.Pointer
, elem
*_type
, n
uintptr) {
12 // Check that (*[n]elem)(p) is appropriately aligned.
13 // Note that we allow unaligned pointers if the types they point to contain
14 // no pointers themselves. See issue 37298.
15 // TODO(mdempsky): What about fieldAlign?
16 if elem
.ptrdata
!= 0 && uintptr(p
)&(uintptr(elem
.align
)-1) != 0 {
17 throw("checkptr: unsafe pointer conversion")
20 // Check that (*[n]elem)(p) doesn't straddle multiple heap objects.
21 if size
:= n
* elem
.size
; size
> 1 && checkptrBase(p
) != checkptrBase(add(p
, size
-1)) {
22 throw("checkptr: unsafe pointer conversion")
26 func checkptrArithmetic(p unsafe
.Pointer
, originals
[]unsafe
.Pointer
) {
27 if 0 < uintptr(p
) && uintptr(p
) < minLegalPointer
{
28 throw("checkptr: unsafe pointer arithmetic")
31 // Check that if the computed pointer p points into a heap
32 // object, then one of the original pointers must have pointed
33 // into the same object.
34 base
:= checkptrBase(p
)
39 for _
, original
:= range originals
{
40 if base
== checkptrBase(original
) {
45 throw("checkptr: unsafe pointer arithmetic")
48 // checkptrBase returns the base address for the allocation containing
51 // Importantly, if p1 and p2 point into the same variable, then
52 // checkptrBase(p1) == checkptrBase(p2). However, the converse/inverse
53 // is not necessarily true as allocations can have trailing padding,
54 // and multiple variables may be packed into a single allocation.
55 func checkptrBase(p unsafe
.Pointer
) uintptr {
57 if gp
:= getg(); gp
.stack
.lo
<= uintptr(p
) && uintptr(p
) < gp
.stack
.hi
{
58 // TODO(mdempsky): Walk the stack to identify the
59 // specific stack frame or even stack object that p
62 // In the mean time, use "1" as a pseudo-address to
63 // represent the stack. This is an invalid address on
64 // all platforms, so it's guaranteed to be distinct
65 // from any of the addresses we might return below.
69 // heap (must check after stack because of #35068)
70 if base
, _
, _
:= findObject(uintptr(p
), 0, 0); base
!= 0 {
75 for _
, datap
:= range activeModules() {
76 if datap
.data
<= uintptr(p
) && uintptr(p
) < datap
.edata
{
79 if datap
.bss
<= uintptr(p
) && uintptr(p
) < datap
.ebss
{