|
| 1 | +// Copyright (c) 2015-2016 Dave Collins <[email protected]> |
| 2 | +// |
| 3 | +// Permission to use, copy, modify, and distribute this software for any |
| 4 | +// purpose with or without fee is hereby granted, provided that the above |
| 5 | +// copyright notice and this permission notice appear in all copies. |
| 6 | +// |
| 7 | +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | +// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | +// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 | +// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | +// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 | +// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 13 | +// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 | + |
| 15 | +// NOTE: Due to the following build constraints, this file will only be compiled |
| 16 | +// when the code is not running on Google App Engine, compiled by GopherJS, and |
| 17 | +// "-tags safe" is not added to the go build command line. The "disableunsafe" |
| 18 | +// tag is deprecated and thus should not be used. |
| 19 | +// Go versions prior to 1.4 are disabled because they use a different layout |
| 20 | +// for interfaces which make the implementation of unsafeReflectValue more complex. |
| 21 | +//go:build !js && !appengine && !safe && !disableunsafe && go1.4 |
| 22 | +// +build !js,!appengine,!safe,!disableunsafe,go1.4 |
| 23 | + |
| 24 | +package spew |
| 25 | + |
| 26 | +import ( |
| 27 | + "reflect" |
| 28 | + "unsafe" |
| 29 | +) |
| 30 | + |
| 31 | +const ( |
| 32 | + // UnsafeDisabled is a build-time constant which specifies whether or |
| 33 | + // not access to the unsafe package is available. |
| 34 | + UnsafeDisabled = false |
| 35 | + |
| 36 | + // ptrSize is the size of a pointer on the current arch. |
| 37 | + ptrSize = unsafe.Sizeof((*byte)(nil)) |
| 38 | +) |
| 39 | + |
| 40 | +type flag uintptr |
| 41 | + |
| 42 | +var ( |
| 43 | + // flagRO indicates whether the value field of a reflect.Value |
| 44 | + // is read-only. |
| 45 | + flagRO flag |
| 46 | + |
| 47 | + // flagAddr indicates whether the address of the reflect.Value's |
| 48 | + // value may be taken. |
| 49 | + flagAddr flag |
| 50 | +) |
| 51 | + |
| 52 | +// flagKindMask holds the bits that make up the kind |
| 53 | +// part of the flags field. In all the supported versions, |
| 54 | +// it is in the lower 5 bits. |
| 55 | +const flagKindMask = flag(0x1f) |
| 56 | + |
| 57 | +// Different versions of Go have used different |
| 58 | +// bit layouts for the flags type. This table |
| 59 | +// records the known combinations. |
| 60 | +var okFlags = []struct { |
| 61 | + ro, addr flag |
| 62 | +}{{ |
| 63 | + // From Go 1.4 to 1.5 |
| 64 | + ro: 1 << 5, |
| 65 | + addr: 1 << 7, |
| 66 | +}, { |
| 67 | + // Up to Go tip. |
| 68 | + ro: 1<<5 | 1<<6, |
| 69 | + addr: 1 << 8, |
| 70 | +}} |
| 71 | + |
| 72 | +var flagValOffset = func() uintptr { |
| 73 | + field, ok := reflect.TypeOf(reflect.Value{}).FieldByName("flag") |
| 74 | + if !ok { |
| 75 | + panic("reflect.Value has no flag field") |
| 76 | + } |
| 77 | + return field.Offset |
| 78 | +}() |
| 79 | + |
| 80 | +// flagField returns a pointer to the flag field of a reflect.Value. |
| 81 | +func flagField(v *reflect.Value) *flag { |
| 82 | + return (*flag)(unsafe.Pointer(uintptr(unsafe.Pointer(v)) + flagValOffset)) |
| 83 | +} |
| 84 | + |
| 85 | +// unsafeReflectValue converts the passed reflect.Value into a one that bypasses |
| 86 | +// the typical safety restrictions preventing access to unaddressable and |
| 87 | +// unexported data. It works by digging the raw pointer to the underlying |
| 88 | +// value out of the protected value and generating a new unprotected (unsafe) |
| 89 | +// reflect.Value to it. |
| 90 | +// |
| 91 | +// This allows us to check for implementations of the Stringer and error |
| 92 | +// interfaces to be used for pretty printing ordinarily unaddressable and |
| 93 | +// inaccessible values such as unexported struct fields. |
| 94 | +func unsafeReflectValue(v reflect.Value) reflect.Value { |
| 95 | + if !v.IsValid() || (v.CanInterface() && v.CanAddr()) { |
| 96 | + return v |
| 97 | + } |
| 98 | + flagFieldPtr := flagField(&v) |
| 99 | + *flagFieldPtr &^= flagRO |
| 100 | + *flagFieldPtr |= flagAddr |
| 101 | + return v |
| 102 | +} |
| 103 | + |
| 104 | +// Sanity checks against future reflect package changes |
| 105 | +// to the type or semantics of the Value.flag field. |
| 106 | +func init() { |
| 107 | + field, ok := reflect.TypeOf(reflect.Value{}).FieldByName("flag") |
| 108 | + if !ok { |
| 109 | + panic("reflect.Value has no flag field") |
| 110 | + } |
| 111 | + if field.Type.Kind() != reflect.TypeOf(flag(0)).Kind() { |
| 112 | + panic("reflect.Value flag field has changed kind") |
| 113 | + } |
| 114 | + type t0 int |
| 115 | + var t struct { |
| 116 | + A t0 |
| 117 | + // t0 will have flagEmbedRO set. |
| 118 | + t0 |
| 119 | + // a will have flagStickyRO set |
| 120 | + a t0 |
| 121 | + } |
| 122 | + vA := reflect.ValueOf(t).FieldByName("A") |
| 123 | + va := reflect.ValueOf(t).FieldByName("a") |
| 124 | + vt0 := reflect.ValueOf(t).FieldByName("t0") |
| 125 | + |
| 126 | + // Infer flagRO from the difference between the flags |
| 127 | + // for the (otherwise identical) fields in t. |
| 128 | + flagPublic := *flagField(&vA) |
| 129 | + flagWithRO := *flagField(&va) | *flagField(&vt0) |
| 130 | + flagRO = flagPublic ^ flagWithRO |
| 131 | + |
| 132 | + // Infer flagAddr from the difference between a value |
| 133 | + // taken from a pointer and not. |
| 134 | + vPtrA := reflect.ValueOf(&t).Elem().FieldByName("A") |
| 135 | + flagNoPtr := *flagField(&vA) |
| 136 | + flagPtr := *flagField(&vPtrA) |
| 137 | + flagAddr = flagNoPtr ^ flagPtr |
| 138 | + |
| 139 | + // Check that the inferred flags tally with one of the known versions. |
| 140 | + for _, f := range okFlags { |
| 141 | + if flagRO == f.ro && flagAddr == f.addr { |
| 142 | + return |
| 143 | + } |
| 144 | + } |
| 145 | + panic("reflect.Value read-only flag has changed semantics") |
| 146 | +} |
0 commit comments