codec_extension.go 6.3 KB

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