1// Copyright 2018 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
5package impl
6
7import (
8 "fmt"
9 "reflect"
10 "strings"
11 "sync"
12
13 "google.golang.org/protobuf/internal/descopts"
14 ptag "google.golang.org/protobuf/internal/encoding/tag"
15 "google.golang.org/protobuf/internal/errors"
16 "google.golang.org/protobuf/internal/filedesc"
17 "google.golang.org/protobuf/internal/strs"
18 "google.golang.org/protobuf/reflect/protoreflect"
19 "google.golang.org/protobuf/runtime/protoiface"
20)
21
22// legacyWrapMessage wraps v as a protoreflect.Message,
23// where v must be a *struct kind and not implement the v2 API already.
24func legacyWrapMessage(v reflect.Value) protoreflect.Message {
25 t := v.Type()
26 if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct {
27 return aberrantMessage{v: v}
28 }
29 mt := legacyLoadMessageInfo(t, "")
30 return mt.MessageOf(v.Interface())
31}
32
33// legacyLoadMessageType dynamically loads a protoreflect.Type for t,
34// where t must be not implement the v2 API already.
35// The provided name is used if it cannot be determined from the message.
36func legacyLoadMessageType(t reflect.Type, name protoreflect.FullName) protoreflect.MessageType {
37 if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct {
38 return aberrantMessageType{t}
39 }
40 return legacyLoadMessageInfo(t, name)
41}
42
43var legacyMessageTypeCache sync.Map // map[reflect.Type]*MessageInfo
44
45// legacyLoadMessageInfo dynamically loads a *MessageInfo for t,
46// where t must be a *struct kind and not implement the v2 API already.
47// The provided name is used if it cannot be determined from the message.
48func legacyLoadMessageInfo(t reflect.Type, name protoreflect.FullName) *MessageInfo {
49 // Fast-path: check if a MessageInfo is cached for this concrete type.
50 if mt, ok := legacyMessageTypeCache.Load(t); ok {
51 return mt.(*MessageInfo)
52 }
53
54 // Slow-path: derive message descriptor and initialize MessageInfo.
55 mi := &MessageInfo{
56 Desc: legacyLoadMessageDesc(t, name),
57 GoReflectType: t,
58 }
59
60 var hasMarshal, hasUnmarshal bool
61 v := reflect.Zero(t).Interface()
62 if _, hasMarshal = v.(legacyMarshaler); hasMarshal {
63 mi.methods.Marshal = legacyMarshal
64
65 // We have no way to tell whether the type's Marshal method
66 // supports deterministic serialization or not, but this
67 // preserves the v1 implementation's behavior of always
68 // calling Marshal methods when present.
69 mi.methods.Flags |= protoiface.SupportMarshalDeterministic
70 }
71 if _, hasUnmarshal = v.(legacyUnmarshaler); hasUnmarshal {
72 mi.methods.Unmarshal = legacyUnmarshal
73 }
74 if _, hasMerge := v.(legacyMerger); hasMerge || (hasMarshal && hasUnmarshal) {
75 mi.methods.Merge = legacyMerge
76 }
77
78 if mi, ok := legacyMessageTypeCache.LoadOrStore(t, mi); ok {
79 return mi.(*MessageInfo)
80 }
81 return mi
82}
83
84var legacyMessageDescCache sync.Map // map[reflect.Type]protoreflect.MessageDescriptor
85
86// LegacyLoadMessageDesc returns an MessageDescriptor derived from the Go type,
87// which should be a *struct kind and must not implement the v2 API already.
88//
89// This is exported for testing purposes.
90func LegacyLoadMessageDesc(t reflect.Type) protoreflect.MessageDescriptor {
91 return legacyLoadMessageDesc(t, "")
92}
93func legacyLoadMessageDesc(t reflect.Type, name protoreflect.FullName) protoreflect.MessageDescriptor {
94 // Fast-path: check if a MessageDescriptor is cached for this concrete type.
95 if mi, ok := legacyMessageDescCache.Load(t); ok {
96 return mi.(protoreflect.MessageDescriptor)
97 }
98
99 // Slow-path: initialize MessageDescriptor from the raw descriptor.
100 mv := reflect.Zero(t).Interface()
101 if _, ok := mv.(protoreflect.ProtoMessage); ok {
102 panic(fmt.Sprintf("%v already implements proto.Message", t))
103 }
104 mdV1, ok := mv.(messageV1)
105 if !ok {
106 return aberrantLoadMessageDesc(t, name)
107 }
108
109 // If this is a dynamic message type where there isn't a 1-1 mapping between
110 // Go and protobuf types, calling the Descriptor method on the zero value of
111 // the message type isn't likely to work. If it panics, swallow the panic and
112 // continue as if the Descriptor method wasn't present.
113 b, idxs := func() ([]byte, []int) {
114 defer func() {
115 recover()
116 }()
117 return mdV1.Descriptor()
118 }()
119 if b == nil {
120 return aberrantLoadMessageDesc(t, name)
121 }
122
123 // If the Go type has no fields, then this might be a proto3 empty message
124 // from before the size cache was added. If there are any fields, check to
125 // see that at least one of them looks like something we generated.
126 if t.Elem().Kind() == reflect.Struct {
127 if nfield := t.Elem().NumField(); nfield > 0 {
128 hasProtoField := false
129 for i := 0; i < nfield; i++ {
130 f := t.Elem().Field(i)
131 if f.Tag.Get("protobuf") != "" || f.Tag.Get("protobuf_oneof") != "" || strings.HasPrefix(f.Name, "XXX_") {
132 hasProtoField = true
133 break
134 }
135 }
136 if !hasProtoField {
137 return aberrantLoadMessageDesc(t, name)
138 }
139 }
140 }
141
142 md := legacyLoadFileDesc(b).Messages().Get(idxs[0])
143 for _, i := range idxs[1:] {
144 md = md.Messages().Get(i)
145 }
146 if name != "" && md.FullName() != name {
147 panic(fmt.Sprintf("mismatching message name: got %v, want %v", md.FullName(), name))
148 }
149 if md, ok := legacyMessageDescCache.LoadOrStore(t, md); ok {
150 return md.(protoreflect.MessageDescriptor)
151 }
152 return md
153}
154
155var (
156 aberrantMessageDescLock sync.Mutex
157 aberrantMessageDescCache map[reflect.Type]protoreflect.MessageDescriptor
158)
159
160// aberrantLoadMessageDesc returns an MessageDescriptor derived from the Go type,
161// which must not implement protoreflect.ProtoMessage or messageV1.
162//
163// This is a best-effort derivation of the message descriptor using the protobuf
164// tags on the struct fields.
165func aberrantLoadMessageDesc(t reflect.Type, name protoreflect.FullName) protoreflect.MessageDescriptor {
166 aberrantMessageDescLock.Lock()
167 defer aberrantMessageDescLock.Unlock()
168 if aberrantMessageDescCache == nil {
169 aberrantMessageDescCache = make(map[reflect.Type]protoreflect.MessageDescriptor)
170 }
171 return aberrantLoadMessageDescReentrant(t, name)
172}
173func aberrantLoadMessageDescReentrant(t reflect.Type, name protoreflect.FullName) protoreflect.MessageDescriptor {
174 // Fast-path: check if an MessageDescriptor is cached for this concrete type.
175 if md, ok := aberrantMessageDescCache[t]; ok {
176 return md
177 }
178
179 // Slow-path: construct a descriptor from the Go struct type (best-effort).
180 // Cache the MessageDescriptor early on so that we can resolve internal
181 // cyclic references.
182 md := &filedesc.Message{L2: new(filedesc.MessageL2)}
183 md.L0.FullName = aberrantDeriveMessageName(t, name)
184 md.L0.ParentFile = filedesc.SurrogateProto2
185 aberrantMessageDescCache[t] = md
186
187 if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct {
188 return md
189 }
190
191 // Try to determine if the message is using proto3 by checking scalars.
192 for i := 0; i < t.Elem().NumField(); i++ {
193 f := t.Elem().Field(i)
194 if tag := f.Tag.Get("protobuf"); tag != "" {
195 switch f.Type.Kind() {
196 case reflect.Bool, reflect.Int32, reflect.Int64, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.String:
197 md.L0.ParentFile = filedesc.SurrogateProto3
198 }
199 for _, s := range strings.Split(tag, ",") {
200 if s == "proto3" {
201 md.L0.ParentFile = filedesc.SurrogateProto3
202 }
203 }
204 }
205 }
206
207 md.L1.EditionFeatures = md.L0.ParentFile.L1.EditionFeatures
208 // Obtain a list of oneof wrapper types.
209 var oneofWrappers []reflect.Type
210 methods := make([]reflect.Method, 0, 2)
211 if m, ok := t.MethodByName("XXX_OneofFuncs"); ok {
212 methods = append(methods, m)
213 }
214 if m, ok := t.MethodByName("XXX_OneofWrappers"); ok {
215 methods = append(methods, m)
216 }
217 for _, fn := range methods {
218 for _, v := range fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))}) {
219 if vs, ok := v.Interface().([]any); ok {
220 for _, v := range vs {
221 oneofWrappers = append(oneofWrappers, reflect.TypeOf(v))
222 }
223 }
224 }
225 }
226
227 // Obtain a list of the extension ranges.
228 if fn, ok := t.MethodByName("ExtensionRangeArray"); ok {
229 vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0]
230 for i := 0; i < vs.Len(); i++ {
231 v := vs.Index(i)
232 md.L2.ExtensionRanges.List = append(md.L2.ExtensionRanges.List, [2]protoreflect.FieldNumber{
233 protoreflect.FieldNumber(v.FieldByName("Start").Int()),
234 protoreflect.FieldNumber(v.FieldByName("End").Int() + 1),
235 })
236 md.L2.ExtensionRangeOptions = append(md.L2.ExtensionRangeOptions, nil)
237 }
238 }
239
240 // Derive the message fields by inspecting the struct fields.
241 for i := 0; i < t.Elem().NumField(); i++ {
242 f := t.Elem().Field(i)
243 if tag := f.Tag.Get("protobuf"); tag != "" {
244 tagKey := f.Tag.Get("protobuf_key")
245 tagVal := f.Tag.Get("protobuf_val")
246 aberrantAppendField(md, f.Type, tag, tagKey, tagVal)
247 }
248 if tag := f.Tag.Get("protobuf_oneof"); tag != "" {
249 n := len(md.L2.Oneofs.List)
250 md.L2.Oneofs.List = append(md.L2.Oneofs.List, filedesc.Oneof{})
251 od := &md.L2.Oneofs.List[n]
252 od.L0.FullName = md.FullName().Append(protoreflect.Name(tag))
253 od.L0.ParentFile = md.L0.ParentFile
254 od.L1.EditionFeatures = md.L1.EditionFeatures
255 od.L0.Parent = md
256 od.L0.Index = n
257
258 for _, t := range oneofWrappers {
259 if t.Implements(f.Type) {
260 f := t.Elem().Field(0)
261 if tag := f.Tag.Get("protobuf"); tag != "" {
262 aberrantAppendField(md, f.Type, tag, "", "")
263 fd := &md.L2.Fields.List[len(md.L2.Fields.List)-1]
264 fd.L1.ContainingOneof = od
265 fd.L1.EditionFeatures = od.L1.EditionFeatures
266 od.L1.Fields.List = append(od.L1.Fields.List, fd)
267 }
268 }
269 }
270 }
271 }
272
273 return md
274}
275
276func aberrantDeriveMessageName(t reflect.Type, name protoreflect.FullName) protoreflect.FullName {
277 if name.IsValid() {
278 return name
279 }
280 func() {
281 defer func() { recover() }() // swallow possible nil panics
282 if m, ok := reflect.Zero(t).Interface().(interface{ XXX_MessageName() string }); ok {
283 name = protoreflect.FullName(m.XXX_MessageName())
284 }
285 }()
286 if name.IsValid() {
287 return name
288 }
289 if t.Kind() == reflect.Ptr {
290 t = t.Elem()
291 }
292 return AberrantDeriveFullName(t)
293}
294
295func aberrantAppendField(md *filedesc.Message, goType reflect.Type, tag, tagKey, tagVal string) {
296 t := goType
297 isOptional := t.Kind() == reflect.Ptr && t.Elem().Kind() != reflect.Struct
298 isRepeated := t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8
299 if isOptional || isRepeated {
300 t = t.Elem()
301 }
302 fd := ptag.Unmarshal(tag, t, placeholderEnumValues{}).(*filedesc.Field)
303
304 // Append field descriptor to the message.
305 n := len(md.L2.Fields.List)
306 md.L2.Fields.List = append(md.L2.Fields.List, *fd)
307 fd = &md.L2.Fields.List[n]
308 fd.L0.FullName = md.FullName().Append(fd.Name())
309 fd.L0.ParentFile = md.L0.ParentFile
310 fd.L0.Parent = md
311 fd.L0.Index = n
312
313 if fd.L1.EditionFeatures.IsPacked {
314 fd.L1.Options = func() protoreflect.ProtoMessage {
315 opts := descopts.Field.ProtoReflect().New()
316 if fd.L1.EditionFeatures.IsPacked {
317 opts.Set(opts.Descriptor().Fields().ByName("packed"), protoreflect.ValueOfBool(fd.L1.EditionFeatures.IsPacked))
318 }
319 return opts.Interface()
320 }
321 }
322
323 // Populate Enum and Message.
324 if fd.Enum() == nil && fd.Kind() == protoreflect.EnumKind {
325 switch v := reflect.Zero(t).Interface().(type) {
326 case protoreflect.Enum:
327 fd.L1.Enum = v.Descriptor()
328 default:
329 fd.L1.Enum = LegacyLoadEnumDesc(t)
330 }
331 }
332 if fd.Message() == nil && (fd.Kind() == protoreflect.MessageKind || fd.Kind() == protoreflect.GroupKind) {
333 switch v := reflect.Zero(t).Interface().(type) {
334 case protoreflect.ProtoMessage:
335 fd.L1.Message = v.ProtoReflect().Descriptor()
336 case messageV1:
337 fd.L1.Message = LegacyLoadMessageDesc(t)
338 default:
339 if t.Kind() == reflect.Map {
340 n := len(md.L1.Messages.List)
341 md.L1.Messages.List = append(md.L1.Messages.List, filedesc.Message{L2: new(filedesc.MessageL2)})
342 md2 := &md.L1.Messages.List[n]
343 md2.L0.FullName = md.FullName().Append(protoreflect.Name(strs.MapEntryName(string(fd.Name()))))
344 md2.L0.ParentFile = md.L0.ParentFile
345 md2.L0.Parent = md
346 md2.L0.Index = n
347 md2.L1.EditionFeatures = md.L1.EditionFeatures
348
349 md2.L1.IsMapEntry = true
350 md2.L2.Options = func() protoreflect.ProtoMessage {
351 opts := descopts.Message.ProtoReflect().New()
352 opts.Set(opts.Descriptor().Fields().ByName("map_entry"), protoreflect.ValueOfBool(true))
353 return opts.Interface()
354 }
355
356 aberrantAppendField(md2, t.Key(), tagKey, "", "")
357 aberrantAppendField(md2, t.Elem(), tagVal, "", "")
358
359 fd.L1.Message = md2
360 break
361 }
362 fd.L1.Message = aberrantLoadMessageDescReentrant(t, "")
363 }
364 }
365}
366
367type placeholderEnumValues struct {
368 protoreflect.EnumValueDescriptors
369}
370
371func (placeholderEnumValues) ByNumber(n protoreflect.EnumNumber) protoreflect.EnumValueDescriptor {
372 return filedesc.PlaceholderEnumValue(protoreflect.FullName(fmt.Sprintf("UNKNOWN_%d", n)))
373}
374
375// legacyMarshaler is the proto.Marshaler interface superseded by protoiface.Methoder.
376type legacyMarshaler interface {
377 Marshal() ([]byte, error)
378}
379
380// legacyUnmarshaler is the proto.Unmarshaler interface superseded by protoiface.Methoder.
381type legacyUnmarshaler interface {
382 Unmarshal([]byte) error
383}
384
385// legacyMerger is the proto.Merger interface superseded by protoiface.Methoder.
386type legacyMerger interface {
387 Merge(protoiface.MessageV1)
388}
389
390var aberrantProtoMethods = &protoiface.Methods{
391 Marshal: legacyMarshal,
392 Unmarshal: legacyUnmarshal,
393 Merge: legacyMerge,
394
395 // We have no way to tell whether the type's Marshal method
396 // supports deterministic serialization or not, but this
397 // preserves the v1 implementation's behavior of always
398 // calling Marshal methods when present.
399 Flags: protoiface.SupportMarshalDeterministic,
400}
401
402func legacyMarshal(in protoiface.MarshalInput) (protoiface.MarshalOutput, error) {
403 v := in.Message.(unwrapper).protoUnwrap()
404 marshaler, ok := v.(legacyMarshaler)
405 if !ok {
406 return protoiface.MarshalOutput{}, errors.New("%T does not implement Marshal", v)
407 }
408 out, err := marshaler.Marshal()
409 if in.Buf != nil {
410 out = append(in.Buf, out...)
411 }
412 return protoiface.MarshalOutput{
413 Buf: out,
414 }, err
415}
416
417func legacyUnmarshal(in protoiface.UnmarshalInput) (protoiface.UnmarshalOutput, error) {
418 v := in.Message.(unwrapper).protoUnwrap()
419 unmarshaler, ok := v.(legacyUnmarshaler)
420 if !ok {
421 return protoiface.UnmarshalOutput{}, errors.New("%T does not implement Unmarshal", v)
422 }
423 return protoiface.UnmarshalOutput{}, unmarshaler.Unmarshal(in.Buf)
424}
425
426func legacyMerge(in protoiface.MergeInput) protoiface.MergeOutput {
427 // Check whether this supports the legacy merger.
428 dstv := in.Destination.(unwrapper).protoUnwrap()
429 merger, ok := dstv.(legacyMerger)
430 if ok {
431 merger.Merge(Export{}.ProtoMessageV1Of(in.Source))
432 return protoiface.MergeOutput{Flags: protoiface.MergeComplete}
433 }
434
435 // If legacy merger is unavailable, implement merge in terms of
436 // a marshal and unmarshal operation.
437 srcv := in.Source.(unwrapper).protoUnwrap()
438 marshaler, ok := srcv.(legacyMarshaler)
439 if !ok {
440 return protoiface.MergeOutput{}
441 }
442 dstv = in.Destination.(unwrapper).protoUnwrap()
443 unmarshaler, ok := dstv.(legacyUnmarshaler)
444 if !ok {
445 return protoiface.MergeOutput{}
446 }
447 if !in.Source.IsValid() {
448 // Legacy Marshal methods may not function on nil messages.
449 // Check for a typed nil source only after we confirm that
450 // legacy Marshal/Unmarshal methods are present, for
451 // consistency.
452 return protoiface.MergeOutput{Flags: protoiface.MergeComplete}
453 }
454 b, err := marshaler.Marshal()
455 if err != nil {
456 return protoiface.MergeOutput{}
457 }
458 err = unmarshaler.Unmarshal(b)
459 if err != nil {
460 return protoiface.MergeOutput{}
461 }
462 return protoiface.MergeOutput{Flags: protoiface.MergeComplete}
463}
464
465// aberrantMessageType implements MessageType for all types other than pointer-to-struct.
466type aberrantMessageType struct {
467 t reflect.Type
468}
469
470func (mt aberrantMessageType) New() protoreflect.Message {
471 if mt.t.Kind() == reflect.Ptr {
472 return aberrantMessage{reflect.New(mt.t.Elem())}
473 }
474 return aberrantMessage{reflect.Zero(mt.t)}
475}
476func (mt aberrantMessageType) Zero() protoreflect.Message {
477 return aberrantMessage{reflect.Zero(mt.t)}
478}
479func (mt aberrantMessageType) GoType() reflect.Type {
480 return mt.t
481}
482func (mt aberrantMessageType) Descriptor() protoreflect.MessageDescriptor {
483 return LegacyLoadMessageDesc(mt.t)
484}
485
486// aberrantMessage implements Message for all types other than pointer-to-struct.
487//
488// When the underlying type implements legacyMarshaler or legacyUnmarshaler,
489// the aberrant Message can be marshaled or unmarshaled. Otherwise, there is
490// not much that can be done with values of this type.
491type aberrantMessage struct {
492 v reflect.Value
493}
494
495// Reset implements the v1 proto.Message.Reset method.
496func (m aberrantMessage) Reset() {
497 if mr, ok := m.v.Interface().(interface{ Reset() }); ok {
498 mr.Reset()
499 return
500 }
501 if m.v.Kind() == reflect.Ptr && !m.v.IsNil() {
502 m.v.Elem().Set(reflect.Zero(m.v.Type().Elem()))
503 }
504}
505
506func (m aberrantMessage) ProtoReflect() protoreflect.Message {
507 return m
508}
509
510func (m aberrantMessage) Descriptor() protoreflect.MessageDescriptor {
511 return LegacyLoadMessageDesc(m.v.Type())
512}
513func (m aberrantMessage) Type() protoreflect.MessageType {
514 return aberrantMessageType{m.v.Type()}
515}
516func (m aberrantMessage) New() protoreflect.Message {
517 if m.v.Type().Kind() == reflect.Ptr {
518 return aberrantMessage{reflect.New(m.v.Type().Elem())}
519 }
520 return aberrantMessage{reflect.Zero(m.v.Type())}
521}
522func (m aberrantMessage) Interface() protoreflect.ProtoMessage {
523 return m
524}
525func (m aberrantMessage) Range(f func(protoreflect.FieldDescriptor, protoreflect.Value) bool) {
526 return
527}
528func (m aberrantMessage) Has(protoreflect.FieldDescriptor) bool {
529 return false
530}
531func (m aberrantMessage) Clear(protoreflect.FieldDescriptor) {
532 panic("invalid Message.Clear on " + string(m.Descriptor().FullName()))
533}
534func (m aberrantMessage) Get(fd protoreflect.FieldDescriptor) protoreflect.Value {
535 if fd.Default().IsValid() {
536 return fd.Default()
537 }
538 panic("invalid Message.Get on " + string(m.Descriptor().FullName()))
539}
540func (m aberrantMessage) Set(protoreflect.FieldDescriptor, protoreflect.Value) {
541 panic("invalid Message.Set on " + string(m.Descriptor().FullName()))
542}
543func (m aberrantMessage) Mutable(protoreflect.FieldDescriptor) protoreflect.Value {
544 panic("invalid Message.Mutable on " + string(m.Descriptor().FullName()))
545}
546func (m aberrantMessage) NewField(protoreflect.FieldDescriptor) protoreflect.Value {
547 panic("invalid Message.NewField on " + string(m.Descriptor().FullName()))
548}
549func (m aberrantMessage) WhichOneof(protoreflect.OneofDescriptor) protoreflect.FieldDescriptor {
550 panic("invalid Message.WhichOneof descriptor on " + string(m.Descriptor().FullName()))
551}
552func (m aberrantMessage) GetUnknown() protoreflect.RawFields {
553 return nil
554}
555func (m aberrantMessage) SetUnknown(protoreflect.RawFields) {
556 // SetUnknown discards its input on messages which don't support unknown field storage.
557}
558func (m aberrantMessage) IsValid() bool {
559 if m.v.Kind() == reflect.Ptr {
560 return !m.v.IsNil()
561 }
562 return false
563}
564func (m aberrantMessage) ProtoMethods() *protoiface.Methods {
565 return aberrantProtoMethods
566}
567func (m aberrantMessage) protoUnwrap() any {
568 return m.v.Interface()
569}