2018-10-18 18:06:29 +00:00
|
|
|
// Copyright 2018 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-11-27 06:32:06 +00:00
|
|
|
package legacy
|
2018-10-18 18:06:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
|
|
|
"io/ioutil"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
// TODO: Avoid reliance on old API. However, there is currently a
|
|
|
|
// chicken and egg problem where we need the descriptor protos to implement
|
|
|
|
// the new API.
|
|
|
|
protoV1 "github.com/golang/protobuf/proto"
|
|
|
|
descriptorV1 "github.com/golang/protobuf/protoc-gen-go/descriptor"
|
2018-11-27 06:32:06 +00:00
|
|
|
pref "github.com/golang/protobuf/v2/reflect/protoreflect"
|
2018-10-18 18:06:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Every enum and message type generated by protoc-gen-go since commit 2fc053c5
|
|
|
|
// on February 25th, 2016 has had a method to get the raw descriptor.
|
|
|
|
// Types that were not generated by protoc-gen-go or were generated prior
|
|
|
|
// to that version are not supported.
|
|
|
|
//
|
|
|
|
// The []byte returned is the encoded form of a FileDescriptorProto message
|
|
|
|
// compressed using GZIP. The []int is the path from the top-level file
|
|
|
|
// to the specific message or enum declaration.
|
|
|
|
type (
|
|
|
|
legacyEnum interface {
|
|
|
|
EnumDescriptor() ([]byte, []int)
|
|
|
|
}
|
|
|
|
legacyMessage interface {
|
|
|
|
Descriptor() ([]byte, []int)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var fileDescCache sync.Map // map[*byte]*descriptorV1.FileDescriptorProto
|
|
|
|
|
internal/impl: support legacy extension fields
Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.
Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.
Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go
In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.
In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.
In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.
In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.
Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
2018-11-06 21:05:20 +00:00
|
|
|
// legacyLoadFileDesc unmarshals b as a compressed FileDescriptorProto message.
|
2018-10-18 18:06:29 +00:00
|
|
|
//
|
|
|
|
// This assumes that b is immutable and that b does not refer to part of a
|
|
|
|
// concatenated series of GZIP files (which would require shenanigans that
|
|
|
|
// rely on the concatenation properties of both protobufs and GZIP).
|
|
|
|
// File descriptors generated by protoc-gen-go do not rely on that property.
|
internal/impl: support legacy extension fields
Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.
Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.
Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go
In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.
In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.
In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.
In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.
Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
2018-11-06 21:05:20 +00:00
|
|
|
func legacyLoadFileDesc(b []byte) *descriptorV1.FileDescriptorProto {
|
2018-10-18 18:06:29 +00:00
|
|
|
// Fast-path: check whether we already have a cached file descriptor.
|
|
|
|
if v, ok := fileDescCache.Load(&b[0]); ok {
|
|
|
|
return v.(*descriptorV1.FileDescriptorProto)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slow-path: decompress and unmarshal the file descriptor proto.
|
|
|
|
m := new(descriptorV1.FileDescriptorProto)
|
|
|
|
zr, err := gzip.NewReader(bytes.NewReader(b))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
b, err = ioutil.ReadAll(zr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
// TODO: What about extensions?
|
|
|
|
// The protoV1 API does not eagerly unmarshal extensions.
|
|
|
|
if err := protoV1.Unmarshal(b, m); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fileDescCache.Store(&b[0], m)
|
|
|
|
return m
|
|
|
|
}
|
2018-11-27 06:32:06 +00:00
|
|
|
|
|
|
|
// parentFileDescriptor returns the parent protoreflect.FileDescriptor for the
|
|
|
|
// provide descriptor. It returns nil if there is no parent.
|
|
|
|
func parentFileDescriptor(d pref.Descriptor) pref.FileDescriptor {
|
|
|
|
for ok := true; ok; d, ok = d.Parent() {
|
|
|
|
if fd, _ := d.(pref.FileDescriptor); fd != nil {
|
|
|
|
return fd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|