codec_extension.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package impl
  5. import (
  6. "sync"
  7. "sync/atomic"
  8. "google.golang.org/protobuf/encoding/protowire"
  9. "google.golang.org/protobuf/internal/errors"
  10. "google.golang.org/protobuf/reflect/protoreflect"
  11. )
  12. type extensionFieldInfo struct {
  13. wiretag uint64
  14. tagsize int
  15. unmarshalNeedsValue bool
  16. funcs valueCoderFuncs
  17. validation validationInfo
  18. }
  19. func getExtensionFieldInfo(xt protoreflect.ExtensionType) *extensionFieldInfo {
  20. if xi, ok := xt.(*ExtensionInfo); ok {
  21. xi.lazyInit()
  22. return xi.info
  23. }
  24. // Ideally we'd cache the resulting *extensionFieldInfo so we don't have to
  25. // recompute this metadata repeatedly. But without support for something like
  26. // weak references, such a cache would pin temporary values (like dynamic
  27. // extension types, constructed for the duration of a user request) to the
  28. // heap forever, causing memory usage of the cache to grow unbounded.
  29. // See discussion in https://github.com/golang/protobuf/issues/1521.
  30. return makeExtensionFieldInfo(xt.TypeDescriptor())
  31. }
  32. func makeExtensionFieldInfo(xd protoreflect.ExtensionDescriptor) *extensionFieldInfo {
  33. var wiretag uint64
  34. if !xd.IsPacked() {
  35. wiretag = protowire.EncodeTag(xd.Number(), wireTypes[xd.Kind()])
  36. } else {
  37. wiretag = protowire.EncodeTag(xd.Number(), protowire.BytesType)
  38. }
  39. e := &extensionFieldInfo{
  40. wiretag: wiretag,
  41. tagsize: protowire.SizeVarint(wiretag),
  42. funcs: encoderFuncsForValue(xd),
  43. }
  44. // Does the unmarshal function need a value passed to it?
  45. // This is true for composite types, where we pass in a message, list, or map to fill in,
  46. // and for enums, where we pass in a prototype value to specify the concrete enum type.
  47. switch xd.Kind() {
  48. case protoreflect.MessageKind, protoreflect.GroupKind, protoreflect.EnumKind:
  49. e.unmarshalNeedsValue = true
  50. default:
  51. if xd.Cardinality() == protoreflect.Repeated {
  52. e.unmarshalNeedsValue = true
  53. }
  54. }
  55. return e
  56. }
  57. type lazyExtensionValue struct {
  58. atomicOnce uint32 // atomically set if value is valid
  59. mu sync.Mutex
  60. xi *extensionFieldInfo
  61. value protoreflect.Value
  62. b []byte
  63. fn func() protoreflect.Value
  64. }
  65. type ExtensionField struct {
  66. typ protoreflect.ExtensionType
  67. // value is either the value of GetValue,
  68. // or a *lazyExtensionValue that then returns the value of GetValue.
  69. value protoreflect.Value
  70. lazy *lazyExtensionValue
  71. }
  72. func (f *ExtensionField) appendLazyBytes(xt protoreflect.ExtensionType, xi *extensionFieldInfo, num protowire.Number, wtyp protowire.Type, b []byte) {
  73. if f.lazy == nil {
  74. f.lazy = &lazyExtensionValue{xi: xi}
  75. }
  76. f.typ = xt
  77. f.lazy.xi = xi
  78. f.lazy.b = protowire.AppendTag(f.lazy.b, num, wtyp)
  79. f.lazy.b = append(f.lazy.b, b...)
  80. }
  81. func (f *ExtensionField) canLazy(xt protoreflect.ExtensionType) bool {
  82. if f.typ == nil {
  83. return true
  84. }
  85. if f.typ == xt && f.lazy != nil && atomic.LoadUint32(&f.lazy.atomicOnce) == 0 {
  86. return true
  87. }
  88. return false
  89. }
  90. func (f *ExtensionField) lazyInit() {
  91. f.lazy.mu.Lock()
  92. defer f.lazy.mu.Unlock()
  93. if atomic.LoadUint32(&f.lazy.atomicOnce) == 1 {
  94. return
  95. }
  96. if f.lazy.xi != nil {
  97. b := f.lazy.b
  98. val := f.typ.New()
  99. for len(b) > 0 {
  100. var tag uint64
  101. if b[0] < 0x80 {
  102. tag = uint64(b[0])
  103. b = b[1:]
  104. } else if len(b) >= 2 && b[1] < 128 {
  105. tag = uint64(b[0]&0x7f) + uint64(b[1])<<7
  106. b = b[2:]
  107. } else {
  108. var n int
  109. tag, n = protowire.ConsumeVarint(b)
  110. if n < 0 {
  111. panic(errors.New("bad tag in lazy extension decoding"))
  112. }
  113. b = b[n:]
  114. }
  115. num := protowire.Number(tag >> 3)
  116. wtyp := protowire.Type(tag & 7)
  117. var out unmarshalOutput
  118. var err error
  119. val, out, err = f.lazy.xi.funcs.unmarshal(b, val, num, wtyp, lazyUnmarshalOptions)
  120. if err != nil {
  121. panic(errors.New("decode failure in lazy extension decoding: %v", err))
  122. }
  123. b = b[out.n:]
  124. }
  125. f.lazy.value = val
  126. } else {
  127. f.lazy.value = f.lazy.fn()
  128. }
  129. f.lazy.xi = nil
  130. f.lazy.fn = nil
  131. f.lazy.b = nil
  132. atomic.StoreUint32(&f.lazy.atomicOnce, 1)
  133. }
  134. // Set sets the type and value of the extension field.
  135. // This must not be called concurrently.
  136. func (f *ExtensionField) Set(t protoreflect.ExtensionType, v protoreflect.Value) {
  137. f.typ = t
  138. f.value = v
  139. f.lazy = nil
  140. }
  141. // SetLazy sets the type and a value that is to be lazily evaluated upon first use.
  142. // This must not be called concurrently.
  143. func (f *ExtensionField) SetLazy(t protoreflect.ExtensionType, fn func() protoreflect.Value) {
  144. f.typ = t
  145. f.lazy = &lazyExtensionValue{fn: fn}
  146. }
  147. // Value returns the value of the extension field.
  148. // This may be called concurrently.
  149. func (f *ExtensionField) Value() protoreflect.Value {
  150. if f.lazy != nil {
  151. if atomic.LoadUint32(&f.lazy.atomicOnce) == 0 {
  152. f.lazyInit()
  153. }
  154. return f.lazy.value
  155. }
  156. return f.value
  157. }
  158. // Type returns the type of the extension field.
  159. // This may be called concurrently.
  160. func (f ExtensionField) Type() protoreflect.ExtensionType {
  161. return f.typ
  162. }
  163. // IsSet returns whether the extension field is set.
  164. // This may be called concurrently.
  165. func (f ExtensionField) IsSet() bool {
  166. return f.typ != nil
  167. }
  168. // IsLazy reports whether a field is lazily encoded.
  169. // It is exported for testing.
  170. func IsLazy(m protoreflect.Message, fd protoreflect.FieldDescriptor) bool {
  171. var mi *MessageInfo
  172. var p pointer
  173. switch m := m.(type) {
  174. case *messageState:
  175. mi = m.messageInfo()
  176. p = m.pointer()
  177. case *messageReflectWrapper:
  178. mi = m.messageInfo()
  179. p = m.pointer()
  180. default:
  181. return false
  182. }
  183. xd, ok := fd.(protoreflect.ExtensionTypeDescriptor)
  184. if !ok {
  185. return false
  186. }
  187. xt := xd.Type()
  188. ext := mi.extensionMap(p)
  189. if ext == nil {
  190. return false
  191. }
  192. f, ok := (*ext)[int32(fd.Number())]
  193. if !ok {
  194. return false
  195. }
  196. return f.typ == xt && f.lazy != nil && atomic.LoadUint32(&f.lazy.atomicOnce) == 0
  197. }