codec_extension.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. // isUnexpandedLazy returns true if the ExensionField is lazy and not
  91. // yet expanded, which means it's present and already checked for
  92. // initialized required fields.
  93. func (f *ExtensionField) isUnexpandedLazy() bool {
  94. return f.lazy != nil && atomic.LoadUint32(&f.lazy.atomicOnce) == 0
  95. }
  96. // lazyBuffer retrieves the buffer for a lazy extension if it's not yet expanded.
  97. //
  98. // The returned buffer has to be kept over whatever operation we're planning,
  99. // as re-retrieving it will fail after the message is lazily decoded.
  100. func (f *ExtensionField) lazyBuffer() []byte {
  101. // This function might be in the critical path, so check the atomic without
  102. // taking a look first, then only take the lock if needed.
  103. if !f.isUnexpandedLazy() {
  104. return nil
  105. }
  106. f.lazy.mu.Lock()
  107. defer f.lazy.mu.Unlock()
  108. return f.lazy.b
  109. }
  110. func (f *ExtensionField) lazyInit() {
  111. f.lazy.mu.Lock()
  112. defer f.lazy.mu.Unlock()
  113. if atomic.LoadUint32(&f.lazy.atomicOnce) == 1 {
  114. return
  115. }
  116. if f.lazy.xi != nil {
  117. b := f.lazy.b
  118. val := f.typ.New()
  119. for len(b) > 0 {
  120. var tag uint64
  121. if b[0] < 0x80 {
  122. tag = uint64(b[0])
  123. b = b[1:]
  124. } else if len(b) >= 2 && b[1] < 128 {
  125. tag = uint64(b[0]&0x7f) + uint64(b[1])<<7
  126. b = b[2:]
  127. } else {
  128. var n int
  129. tag, n = protowire.ConsumeVarint(b)
  130. if n < 0 {
  131. panic(errors.New("bad tag in lazy extension decoding"))
  132. }
  133. b = b[n:]
  134. }
  135. num := protowire.Number(tag >> 3)
  136. wtyp := protowire.Type(tag & 7)
  137. var out unmarshalOutput
  138. var err error
  139. val, out, err = f.lazy.xi.funcs.unmarshal(b, val, num, wtyp, lazyUnmarshalOptions)
  140. if err != nil {
  141. panic(errors.New("decode failure in lazy extension decoding: %v", err))
  142. }
  143. b = b[out.n:]
  144. }
  145. f.lazy.value = val
  146. } else {
  147. f.lazy.value = f.lazy.fn()
  148. }
  149. f.lazy.xi = nil
  150. f.lazy.fn = nil
  151. f.lazy.b = nil
  152. atomic.StoreUint32(&f.lazy.atomicOnce, 1)
  153. }
  154. // Set sets the type and value of the extension field.
  155. // This must not be called concurrently.
  156. func (f *ExtensionField) Set(t protoreflect.ExtensionType, v protoreflect.Value) {
  157. f.typ = t
  158. f.value = v
  159. f.lazy = nil
  160. }
  161. // SetLazy sets the type and a value that is to be lazily evaluated upon first use.
  162. // This must not be called concurrently.
  163. func (f *ExtensionField) SetLazy(t protoreflect.ExtensionType, fn func() protoreflect.Value) {
  164. f.typ = t
  165. f.lazy = &lazyExtensionValue{fn: fn}
  166. }
  167. // Value returns the value of the extension field.
  168. // This may be called concurrently.
  169. func (f *ExtensionField) Value() protoreflect.Value {
  170. if f.lazy != nil {
  171. if atomic.LoadUint32(&f.lazy.atomicOnce) == 0 {
  172. f.lazyInit()
  173. }
  174. return f.lazy.value
  175. }
  176. return f.value
  177. }
  178. // Type returns the type of the extension field.
  179. // This may be called concurrently.
  180. func (f ExtensionField) Type() protoreflect.ExtensionType {
  181. return f.typ
  182. }
  183. // IsSet returns whether the extension field is set.
  184. // This may be called concurrently.
  185. func (f ExtensionField) IsSet() bool {
  186. return f.typ != nil
  187. }
  188. // IsLazy reports whether a field is lazily encoded.
  189. // It is exported for testing.
  190. func IsLazy(m protoreflect.Message, fd protoreflect.FieldDescriptor) bool {
  191. var mi *MessageInfo
  192. var p pointer
  193. switch m := m.(type) {
  194. case *messageState:
  195. mi = m.messageInfo()
  196. p = m.pointer()
  197. case *messageReflectWrapper:
  198. mi = m.messageInfo()
  199. p = m.pointer()
  200. default:
  201. return false
  202. }
  203. xd, ok := fd.(protoreflect.ExtensionTypeDescriptor)
  204. if !ok {
  205. return false
  206. }
  207. xt := xd.Type()
  208. ext := mi.extensionMap(p)
  209. if ext == nil {
  210. return false
  211. }
  212. f, ok := (*ext)[int32(fd.Number())]
  213. if !ok {
  214. return false
  215. }
  216. return f.typ == xt && f.lazy != nil && atomic.LoadUint32(&f.lazy.atomicOnce) == 0
  217. }