-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for limited mode without unsafe pkg.
This commit adds support for compiling spew without the unsafe package. When compiled without the unsafe package, some of the more advanced features such as invoking stringers on pointers from non-pointer variables and unexported struct fields are not available. By default, spew will be compiled in the limited mode for Google App Engine since the unsafe package is not available there. Additionally, spew can be compiled without the unsafe package manually by specifying the "disableunsafe" build tag. Finally, a new package-level constant named "UnsafeDisabled" has been exposed which can be used to programmatically determine if spew was compiled with access to the unsafe package.
- Loading branch information
Showing
14 changed files
with
432 additions
and
273 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// Copyright (c) 2015 Dave Collins <[email protected]> | ||
// | ||
// Permission to use, copy, modify, and distribute this software for any | ||
// purpose with or without fee is hereby granted, provided that the above | ||
// copyright notice and this permission notice appear in all copies. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
|
||
// NOTE: Due to the following build constraints, this file will only be compiled | ||
// when the code is not running on Google App Engine and "-tags disableunsafe" | ||
// is not added to the go build command line. | ||
// +build !appengine,!disableunsafe | ||
|
||
package spew | ||
|
||
import ( | ||
"reflect" | ||
"unsafe" | ||
) | ||
|
||
const ( | ||
// UnsafeDisabled is a build-time constant which specifies whether or | ||
// not access to the unsafe package is available. | ||
UnsafeDisabled = false | ||
|
||
// ptrSize is the size of a pointer on the current arch. | ||
ptrSize = unsafe.Sizeof((*byte)(nil)) | ||
) | ||
|
||
var ( | ||
// offsetPtr, offsetScalar, and offsetFlag are the offsets for the | ||
// internal reflect.Value fields. These values are valid before golang | ||
// commit ecccf07e7f9d which changed the format. The are also valid | ||
// after commit 82f48826c6c7 which changed the format again to mirror | ||
// the original format. Code in the init function updates these offsets | ||
// as necessary. | ||
offsetPtr = uintptr(ptrSize) | ||
offsetScalar = uintptr(0) | ||
offsetFlag = uintptr(ptrSize * 2) | ||
|
||
// flagKindWidth and flagKindShift indicate various bits that the | ||
// reflect package uses internally to track kind information. | ||
// | ||
// flagRO indicates whether or not the value field of a reflect.Value is | ||
// read-only. | ||
// | ||
// flagIndir indicates whether the value field of a reflect.Value is | ||
// the actual data or a pointer to the data. | ||
// | ||
// These values are valid before golang commit 90a7c3c86944 which | ||
// changed their positions. Code in the init function updates these | ||
// flags as necessary. | ||
flagKindWidth = uintptr(5) | ||
flagKindShift = uintptr(flagKindWidth - 1) | ||
flagRO = uintptr(1 << 0) | ||
flagIndir = uintptr(1 << 1) | ||
) | ||
|
||
func init() { | ||
// Older versions of reflect.Value stored small integers directly in the | ||
// ptr field (which is named val in the older versions). Versions | ||
// between commits ecccf07e7f9d and 82f48826c6c7 added a new field named | ||
// scalar for this purpose which unfortunately came before the flag | ||
// field, so the offset of the flag field is different for those | ||
// versions. | ||
// | ||
// This code constructs a new reflect.Value from a known small integer | ||
// and checks if the size of the reflect.Value struct indicates it has | ||
// the scalar field. When it does, the offsets are updated accordingly. | ||
vv := reflect.ValueOf(0xf00) | ||
if unsafe.Sizeof(vv) == (ptrSize * 4) { | ||
offsetScalar = ptrSize * 2 | ||
offsetFlag = ptrSize * 3 | ||
} | ||
|
||
// Commit 90a7c3c86944 changed the flag positions such that the low | ||
// order bits are the kind. This code extracts the kind from the flags | ||
// field and ensures it's the correct type. When it's not, the flag | ||
// order has been changed to the newer format, so the flags are updated | ||
// accordingly. | ||
upf := unsafe.Pointer(uintptr(unsafe.Pointer(&vv)) + offsetFlag) | ||
upfv := *(*uintptr)(upf) | ||
flagKindMask := uintptr((1<<flagKindWidth - 1) << flagKindShift) | ||
if (upfv&flagKindMask)>>flagKindShift != uintptr(reflect.Int) { | ||
flagKindShift = 0 | ||
flagRO = 1 << 5 | ||
flagIndir = 1 << 6 | ||
} | ||
} | ||
|
||
// unsafeReflectValue converts the passed reflect.Value into a one that bypasses | ||
// the typical safety restrictions preventing access to unaddressable and | ||
// unexported data. It works by digging the raw pointer to the underlying | ||
// value out of the protected value and generating a new unprotected (unsafe) | ||
// reflect.Value to it. | ||
// | ||
// This allows us to check for implementations of the Stringer and error | ||
// interfaces to be used for pretty printing ordinarily unaddressable and | ||
// inaccessible values such as unexported struct fields. | ||
func unsafeReflectValue(v reflect.Value) (rv reflect.Value) { | ||
indirects := 1 | ||
vt := v.Type() | ||
upv := unsafe.Pointer(uintptr(unsafe.Pointer(&v)) + offsetPtr) | ||
rvf := *(*uintptr)(unsafe.Pointer(uintptr(unsafe.Pointer(&v)) + offsetFlag)) | ||
if rvf&flagIndir != 0 { | ||
vt = reflect.PtrTo(v.Type()) | ||
indirects++ | ||
} else if offsetScalar != 0 { | ||
// The value is in the scalar field when it's not one of the | ||
// reference types. | ||
switch vt.Kind() { | ||
case reflect.Uintptr: | ||
case reflect.Chan: | ||
case reflect.Func: | ||
case reflect.Map: | ||
case reflect.Ptr: | ||
case reflect.UnsafePointer: | ||
default: | ||
upv = unsafe.Pointer(uintptr(unsafe.Pointer(&v)) + | ||
offsetScalar) | ||
} | ||
} | ||
|
||
pv := reflect.NewAt(vt, upv) | ||
rv = pv | ||
for i := 0; i < indirects; i++ { | ||
rv = rv.Elem() | ||
} | ||
return rv | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) 2015 Dave Collins <[email protected]> | ||
// | ||
// Permission to use, copy, modify, and distribute this software for any | ||
// purpose with or without fee is hereby granted, provided that the above | ||
// copyright notice and this permission notice appear in all copies. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
|
||
// NOTE: Due to the following build constraints, this file will only be compiled | ||
// when either the code is running on Google App Engine or "-tags disableunsafe" | ||
// is added to the go build command line. | ||
// +build appengine disableunsafe | ||
|
||
package spew | ||
|
||
import "reflect" | ||
|
||
const ( | ||
// UnsafeDisabled is a build-time constant which specifies whether or | ||
// not access to the unsafe package is available. | ||
UnsafeDisabled = true | ||
) | ||
|
||
// unsafeReflectValue typically converts the passed reflect.Value into a one | ||
// that bypasses the typical safety restrictions preventing access to | ||
// unaddressable and unexported data. However, doing this relies on access to | ||
// the unsafe package. This is a stub version which simply returns the passed | ||
// reflect.Value when the unsafe package is not available. | ||
func unsafeReflectValue(v reflect.Value) reflect.Value { | ||
return v | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.