discard.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2017 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. package proto
  32. import (
  33. "fmt"
  34. "reflect"
  35. "strings"
  36. "sync"
  37. "sync/atomic"
  38. )
  39. type generatedDiscarder interface {
  40. XXX_DiscardUnknown()
  41. }
  42. // DiscardUnknown recursively discards all unknown fields from this message
  43. // and all embedded messages.
  44. //
  45. // When unmarshaling a message with unrecognized fields, the tags and values
  46. // of such fields are preserved in the Message. This allows a later call to
  47. // marshal to be able to produce a message that continues to have those
  48. // unrecognized fields. To avoid this, DiscardUnknown is used to
  49. // explicitly clear the unknown fields after unmarshaling.
  50. //
  51. // For proto2 messages, the unknown fields of message extensions are only
  52. // discarded from messages that have been accessed via GetExtension.
  53. func DiscardUnknown(m Message) {
  54. if m, ok := m.(generatedDiscarder); ok {
  55. m.XXX_DiscardUnknown()
  56. return
  57. }
  58. // TODO: Dynamically populate a InternalMessageInfo for legacy messages,
  59. // but the master branch has no implementation for InternalMessageInfo,
  60. // so it would be more work to replicate that approach.
  61. discardLegacy(m)
  62. }
  63. // DiscardUnknown recursively discards all unknown fields.
  64. func (a *InternalMessageInfo) DiscardUnknown(m Message) {
  65. di := atomicLoadDiscardInfo(&a.discard)
  66. if di == nil {
  67. di = getDiscardInfo(reflect.TypeOf(m).Elem())
  68. atomicStoreDiscardInfo(&a.discard, di)
  69. }
  70. di.discard(toPointer(&m))
  71. }
  72. type discardInfo struct {
  73. typ reflect.Type
  74. initialized int32 // 0: only typ is valid, 1: everything is valid
  75. lock sync.Mutex
  76. fields []discardFieldInfo
  77. unrecognized field
  78. }
  79. type discardFieldInfo struct {
  80. field field // Offset of field, guaranteed to be valid
  81. discard func(src pointer)
  82. }
  83. var (
  84. discardInfoMap = map[reflect.Type]*discardInfo{}
  85. discardInfoLock sync.Mutex
  86. )
  87. func getDiscardInfo(t reflect.Type) *discardInfo {
  88. discardInfoLock.Lock()
  89. defer discardInfoLock.Unlock()
  90. di := discardInfoMap[t]
  91. if di == nil {
  92. di = &discardInfo{typ: t}
  93. discardInfoMap[t] = di
  94. }
  95. return di
  96. }
  97. func (di *discardInfo) discard(src pointer) {
  98. if src.isNil() {
  99. return // Nothing to do.
  100. }
  101. if atomic.LoadInt32(&di.initialized) == 0 {
  102. di.computeDiscardInfo()
  103. }
  104. for _, fi := range di.fields {
  105. sfp := src.offset(fi.field)
  106. fi.discard(sfp)
  107. }
  108. // For proto2 messages, only discard unknown fields in message extensions
  109. // that have been accessed via GetExtension.
  110. if em, err := extendable(src.asPointerTo(di.typ).Interface()); err == nil {
  111. // Ignore lock since DiscardUnknown is not concurrency safe.
  112. emm, _ := em.extensionsRead()
  113. for _, mx := range emm {
  114. if m, ok := mx.value.(Message); ok {
  115. DiscardUnknown(m)
  116. }
  117. }
  118. }
  119. if di.unrecognized.IsValid() {
  120. *src.offset(di.unrecognized).toBytes() = nil
  121. }
  122. }
  123. func (di *discardInfo) computeDiscardInfo() {
  124. di.lock.Lock()
  125. defer di.lock.Unlock()
  126. if di.initialized != 0 {
  127. return
  128. }
  129. t := di.typ
  130. n := t.NumField()
  131. for i := 0; i < n; i++ {
  132. f := t.Field(i)
  133. if strings.HasPrefix(f.Name, "XXX_") {
  134. continue
  135. }
  136. dfi := discardFieldInfo{field: toField(&f)}
  137. tf := f.Type
  138. // Unwrap tf to get its most basic type.
  139. var isPointer, isSlice bool
  140. if tf.Kind() == reflect.Slice && tf.Elem().Kind() != reflect.Uint8 {
  141. isSlice = true
  142. tf = tf.Elem()
  143. }
  144. if tf.Kind() == reflect.Ptr {
  145. isPointer = true
  146. tf = tf.Elem()
  147. }
  148. if isPointer && isSlice && tf.Kind() != reflect.Struct {
  149. panic(fmt.Sprintf("%v.%s cannot be a slice of pointers to primitive types", t, f.Name))
  150. }
  151. switch tf.Kind() {
  152. case reflect.Struct:
  153. switch {
  154. case !isPointer:
  155. panic(fmt.Sprintf("%v.%s cannot be a direct struct value", t, f.Name))
  156. case isSlice: // E.g., []*pb.T
  157. di := getDiscardInfo(tf)
  158. dfi.discard = func(src pointer) {
  159. sps := src.getPointerSlice()
  160. for _, sp := range sps {
  161. if !sp.isNil() {
  162. di.discard(sp)
  163. }
  164. }
  165. }
  166. default: // E.g., *pb.T
  167. di := getDiscardInfo(tf)
  168. dfi.discard = func(src pointer) {
  169. sp := src.getPointer()
  170. if !sp.isNil() {
  171. di.discard(sp)
  172. }
  173. }
  174. }
  175. case reflect.Map:
  176. switch {
  177. case isPointer || isSlice:
  178. panic(fmt.Sprintf("%v.%s cannot be a pointer to a map or a slice of map values", t, f.Name))
  179. default: // E.g., map[K]V
  180. if tf.Elem().Kind() == reflect.Ptr { // Proto struct (e.g., *T)
  181. dfi.discard = func(src pointer) {
  182. sm := src.asPointerTo(tf).Elem()
  183. if sm.Len() == 0 {
  184. return
  185. }
  186. for _, key := range sm.MapKeys() {
  187. val := sm.MapIndex(key)
  188. DiscardUnknown(val.Interface().(Message))
  189. }
  190. }
  191. } else {
  192. dfi.discard = func(pointer) {} // Noop
  193. }
  194. }
  195. case reflect.Interface:
  196. // Must be oneof field.
  197. switch {
  198. case isPointer || isSlice:
  199. panic(fmt.Sprintf("%v.%s cannot be a pointer to a interface or a slice of interface values", t, f.Name))
  200. default: // E.g., interface{}
  201. // TODO: Make this faster?
  202. dfi.discard = func(src pointer) {
  203. su := src.asPointerTo(tf).Elem()
  204. if !su.IsNil() {
  205. sv := su.Elem().Elem().Field(0)
  206. if sv.Kind() == reflect.Ptr && sv.IsNil() {
  207. return
  208. }
  209. switch sv.Type().Kind() {
  210. case reflect.Ptr: // Proto struct (e.g., *T)
  211. DiscardUnknown(sv.Interface().(Message))
  212. }
  213. }
  214. }
  215. }
  216. default:
  217. continue
  218. }
  219. di.fields = append(di.fields, dfi)
  220. }
  221. di.unrecognized = invalidField
  222. if f, ok := t.FieldByName("XXX_unrecognized"); ok {
  223. if f.Type != reflect.TypeOf([]byte{}) {
  224. panic("expected XXX_unrecognized to be of type []byte")
  225. }
  226. di.unrecognized = toField(&f)
  227. }
  228. atomic.StoreInt32(&di.initialized, 1)
  229. }
  230. func discardLegacy(m Message) {
  231. v := reflect.ValueOf(m)
  232. if v.Kind() != reflect.Ptr || v.IsNil() {
  233. return
  234. }
  235. v = v.Elem()
  236. if v.Kind() != reflect.Struct {
  237. return
  238. }
  239. t := v.Type()
  240. for i := 0; i < v.NumField(); i++ {
  241. f := t.Field(i)
  242. if strings.HasPrefix(f.Name, "XXX_") {
  243. continue
  244. }
  245. vf := v.Field(i)
  246. tf := f.Type
  247. // Unwrap tf to get its most basic type.
  248. var isPointer, isSlice bool
  249. if tf.Kind() == reflect.Slice && tf.Elem().Kind() != reflect.Uint8 {
  250. isSlice = true
  251. tf = tf.Elem()
  252. }
  253. if tf.Kind() == reflect.Ptr {
  254. isPointer = true
  255. tf = tf.Elem()
  256. }
  257. if isPointer && isSlice && tf.Kind() != reflect.Struct {
  258. panic(fmt.Sprintf("%T.%s cannot be a slice of pointers to primitive types", m, f.Name))
  259. }
  260. switch tf.Kind() {
  261. case reflect.Struct:
  262. switch {
  263. case !isPointer:
  264. panic(fmt.Sprintf("%T.%s cannot be a direct struct value", m, f.Name))
  265. case isSlice: // E.g., []*pb.T
  266. for j := 0; j < vf.Len(); j++ {
  267. discardLegacy(vf.Index(j).Interface().(Message))
  268. }
  269. default: // E.g., *pb.T
  270. discardLegacy(vf.Interface().(Message))
  271. }
  272. case reflect.Map:
  273. switch {
  274. case isPointer || isSlice:
  275. panic(fmt.Sprintf("%T.%s cannot be a pointer to a map or a slice of map values", m, f.Name))
  276. default: // E.g., map[K]V
  277. tv := vf.Type().Elem()
  278. if tv.Kind() == reflect.Ptr && tv.Implements(protoMessageType) { // Proto struct (e.g., *T)
  279. for _, key := range vf.MapKeys() {
  280. val := vf.MapIndex(key)
  281. discardLegacy(val.Interface().(Message))
  282. }
  283. }
  284. }
  285. case reflect.Interface:
  286. // Must be oneof field.
  287. switch {
  288. case isPointer || isSlice:
  289. panic(fmt.Sprintf("%T.%s cannot be a pointer to a interface or a slice of interface values", m, f.Name))
  290. default: // E.g., test_proto.isCommunique_Union interface
  291. if !vf.IsNil() && f.Tag.Get("protobuf_oneof") != "" {
  292. vf = vf.Elem() // E.g., *test_proto.Communique_Msg
  293. if !vf.IsNil() {
  294. vf = vf.Elem() // E.g., test_proto.Communique_Msg
  295. vf = vf.Field(0) // E.g., Proto struct (e.g., *T) or primitive value
  296. if vf.Kind() == reflect.Ptr {
  297. discardLegacy(vf.Interface().(Message))
  298. }
  299. }
  300. }
  301. }
  302. }
  303. }
  304. if vf := v.FieldByName("XXX_unrecognized"); vf.IsValid() {
  305. if vf.Type() != reflect.TypeOf([]byte{}) {
  306. panic("expected XXX_unrecognized to be of type []byte")
  307. }
  308. vf.Set(reflect.ValueOf([]byte(nil)))
  309. }
  310. // For proto2 messages, only discard unknown fields in message extensions
  311. // that have been accessed via GetExtension.
  312. if em, err := extendable(m); err == nil {
  313. // Ignore lock since discardLegacy is not concurrency safe.
  314. emm, _ := em.extensionsRead()
  315. for _, mx := range emm {
  316. if m, ok := mx.value.(Message); ok {
  317. discardLegacy(m)
  318. }
  319. }
  320. }
  321. }