Debian Codegraph @

internal/spew/bypass.go

raw
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
20 //go:build !js && !appengine && !safe && !disableunsafe
21 // +build !js,!appengine,!safe,!disableunsafe
22
23 package spew
24
25 import (
26 "reflect"
27 "unsafe"
28
29 "github.com/maxatome/go-testdeep/internal/or"
30 )
31
32 // UnsafeDisabled is a build-time constant which specifies whether or
33 // not access to the unsafe package is available.
34 const UnsafeDisabled = false
35
36 type flag uintptr
37
38 var (
39 // flagRO indicates whether the value field of a reflect.Value
40 // is read-only.
41 flagRO flag
42
43 // flagAddr indicates whether the address of the reflect.Value's
44 // value may be taken.
45 flagAddr flag
46 )
47
48 // flagKindMask holds the bits that make up the kind
49 // part of the flags field. In all the supported versions,
50 // it is in the lower 5 bits.
51 const flagKindMask = flag(0x1f)
52
53 // Different versions of Go have used different bit layouts for the
54 // flags type. This layout is OK since go 1.6.
55 var okFlags = struct {
56 ro, addr flag
57 }{
58 ro: 1<<5 | 1<<6,
59 addr: 1 << 8,
60 }
61
62 var flagValOffset = func() uintptr {
63 field, ok := reflect.TypeOf(reflect.Value{}).FieldByName("flag")
64 or.Panic(ok, "reflect.Value has no flag field")
65 return field.Offset
66 }()
67
68 // flagField returns a pointer to the flag field of a reflect.Value.
69 func flagField(v *reflect.Value) *flag {
70 return (*flag)(unsafe.Pointer(uintptr(unsafe.Pointer(v)) + flagValOffset))
71 }
72
73 // UnsafeReflectValue converts the passed reflect.Value into a one that bypasses
74 // the typical safety restrictions preventing access to unaddressable and
75 // unexported data. It works by digging the raw pointer to the underlying
76 // value out of the protected value and generating a new unprotected (unsafe)
77 // reflect.Value to it.
78 //
79 // This allows us to check for implementations of the fmt.Stringer and error
80 // interfaces to be used for pretty printing ordinarily unaddressable and
81 // inaccessible values such as unexported struct fields.
82 func UnsafeReflectValue(v reflect.Value) reflect.Value {
83 if v.IsValid() && (!v.CanInterface() || !v.CanAddr()) {
84 flagFieldPtr := flagField(&v)
85 *flagFieldPtr &^= flagRO
86 *flagFieldPtr |= flagAddr
87 }
88 return v
89 }
90
91 // Sanity checks against future reflect package changes
92 // to the type or semantics of the Value.flag field.
93 func init() {
94 field, ok := reflect.TypeOf(reflect.Value{}).FieldByName("flag")
95 or.Panic(ok, "reflect.Value has no flag field")
96
97 or.Panic(field.Type.Kind() == reflect.TypeOf(flag(0)).Kind(),
98 "reflect.Value flag field has changed kind")
99
100 type t0 int
101 var t struct {
102 A t0
103 // t0 will have flagEmbedRO set.
104 t0
105 // a will have flagStickyRO set
106 a t0
107 }
108 vA := reflect.ValueOf(t).FieldByName("A")
109 va := reflect.ValueOf(t).FieldByName("a")
110 vt0 := reflect.ValueOf(t).FieldByName("t0")
111
112 // Infer flagRO from the difference between the flags
113 // for the (otherwise identical) fields in t.
114 flagPublic := *flagField(&vA)
115 flagWithRO := *flagField(&va) | *flagField(&vt0)
116 flagRO = flagPublic ^ flagWithRO
117
118 // Infer flagAddr from the difference between a value
119 // taken from a pointer and not.
120 vPtrA := reflect.ValueOf(&t).Elem().FieldByName("A")
121 flagNoPtr := *flagField(&vA)
122 flagPtr := *flagField(&vPtrA)
123 flagAddr = flagNoPtr ^ flagPtr
124
125 // Check that the inferred flags tally with the known version
126 or.Panic(flagRO == okFlags.ro && flagAddr == okFlags.addr,
127 "reflect.Value read-only flag has changed semantics")
128 }