Mercurial > hg > CbC > CbC_gcc
view libgo/go/runtime/checkptr.go @ 158:494b0b89df80 default tip
...
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 25 May 2020 18:13:55 +0900 |
parents | 1830386684a0 |
children |
line wrap: on
line source
// Copyright 2019 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // +build ignore package runtime 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) { // Check that (*[n]elem)(p) is appropriately aligned. // TODO(mdempsky): What about fieldAlign? if uintptr(p)&(uintptr(elem.align)-1) != 0 { panic(ptrAlignError{p, elem, n}) } // 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)) { panic(ptrAlignError{p, elem, n}) } } 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) { if 0 < uintptr(p) && uintptr(p) < minLegalPointer { panic(ptrArithError{p, originals}) } // Check that if the computed pointer p points into a heap // object, then one of the original pointers must have pointed // into the same object. base := checkptrBase(p) if base == 0 { return } for _, original := range originals { if base == checkptrBase(original) { return } } panic(ptrArithError{p, originals}) } // checkptrBase returns the base address for the allocation containing // the address p. // // Importantly, if p1 and p2 point into the same variable, then // checkptrBase(p1) == checkptrBase(p2). However, the converse/inverse // is not necessarily true as allocations can have trailing padding, // and multiple variables may be packed into a single allocation. func checkptrBase(p unsafe.Pointer) uintptr { // stack if gp := getg(); gp.stack.lo <= uintptr(p) && uintptr(p) < gp.stack.hi { // TODO(mdempsky): Walk the stack to identify the // specific stack frame or even stack object that p // points into. // // In the mean time, use "1" as a pseudo-address to // represent the stack. This is an invalid address on // all platforms, so it's guaranteed to be distinct // from any of the addresses we might return below. return 1 } // heap (must check after stack because of #35068) if base, _, _ := findObject(uintptr(p), 0, 0); base != 0 { return base } // data or bss for _, datap := range activeModules() { if datap.data <= uintptr(p) && uintptr(p) < datap.edata { return datap.data } if datap.bss <= uintptr(p) && uintptr(p) < datap.ebss { return datap.bss } } return 0 }