Don't read protobuf static data across shared library lines directly (#6979)

Reading exported data from shared objects on windows is broken. It requires __declspec(dllimport). However, this is problematic, as we use the same static libraries both from a shared and static context. So we can't just blindly apply dllimport.

The linker should have caught this, as data members are exported in a different way. However, due to a bug in native-utils, data member symbols were exposed directly. However, interacting with those data member was completely broken.

The only way we can really solve this is to just not use static data members. We're pretty good about this in WPILib itself. However, protobuf is absolutely terrible at this. There are a ton of inline functions that access global data. For the protobuf library itself, we can solve this easily enough.

However, for the generated protobuf code, this is much more problematic. The member needed to bypass the global data is private. This means using just the stock protobuf code, this problem is not solvable. But, protobuf generated code has insertion points. Those insertion points let us add our own code into the generated code via a protoc plugin. And it just so happens that an insertion point exists to add extra public methodsto the generated protobuf header. There is also an insertion point to let us add to the cpp file.

The methods we need are the getters, for unpacking protobufs. For any protobuf that has a message as a member, we generate a new wpi_x() getter (the existing one is just x(), where x is the field name). We then implement this in the cpp file. A trick we can use is in the cpp file, we can safely call the x() function, as the cpp file is in the same library as the global. Thus we can call that inline method, and not actually need to directly access any internal private state of the protobuf object.

TL;DR, all protobuf classes that have messages as fields now have a wpi_x() accessor that must be used instead of x() if you want the code to work on windows. After wpilibsuite/native-utils#212, the bad code will fail to link, rather then just fail at runtime.
This commit is contained in:
Thad House
2024-08-21 07:53:20 -07:00
committed by GitHub
parent eef516fcc9
commit a9ac5b8e24
40 changed files with 443 additions and 91 deletions

View File

@@ -976,13 +976,13 @@ class PROTOBUF_EXPORT FieldDescriptor : private internal::SymbolBase {
mutable std::atomic<const Message*> default_generated_instance_;
};
static const CppType kTypeToCppTypeMap[MAX_TYPE + 1];
static const CppType *GetTypeToCppTypeMap();
static const char* const kTypeToName[MAX_TYPE + 1];
static const char* const *GetTypeToName();
static const char* const kCppTypeToName[MAX_CPPTYPE + 1];
static const char* const *GetCppTypeToName();
static const char* const kLabelToName[MAX_LABEL + 1];
static const char* const *GetLabelToName();
// Must be constructed using DescriptorPool.
FieldDescriptor() {}
@@ -2392,27 +2392,27 @@ inline int MethodDescriptor::index() const {
}
inline const char* FieldDescriptor::type_name() const {
return kTypeToName[type()];
return GetTypeToName()[type()];
}
inline FieldDescriptor::CppType FieldDescriptor::cpp_type() const {
return kTypeToCppTypeMap[type()];
return GetTypeToCppTypeMap()[type()];
}
inline const char* FieldDescriptor::cpp_type_name() const {
return kCppTypeToName[kTypeToCppTypeMap[type()]];
return GetCppTypeToName()[GetTypeToCppTypeMap()[type()]];
}
inline FieldDescriptor::CppType FieldDescriptor::TypeToCppType(Type type) {
return kTypeToCppTypeMap[type];
return GetTypeToCppTypeMap()[type];
}
inline const char* FieldDescriptor::TypeName(Type type) {
return kTypeToName[type];
return GetTypeToName()[type];
}
inline const char* FieldDescriptor::CppTypeName(CppType cpp_type) {
return kCppTypeToName[cpp_type];
return GetCppTypeToName()[cpp_type];
}
inline bool FieldDescriptor::IsTypePackable(Type field_type) {

View File

@@ -791,31 +791,31 @@ class Symbol {
const internal::SymbolBase* ptr_;
};
const FieldDescriptor::CppType
FieldDescriptor::kTypeToCppTypeMap[MAX_TYPE + 1] = {
static_cast<CppType>(0), // 0 is reserved for errors
static const FieldDescriptor::CppType
kTypeToCppTypeMap[FieldDescriptor::MAX_TYPE + 1] = {
static_cast<FieldDescriptor::CppType>(0), // 0 is reserved for errors
CPPTYPE_DOUBLE, // TYPE_DOUBLE
CPPTYPE_FLOAT, // TYPE_FLOAT
CPPTYPE_INT64, // TYPE_INT64
CPPTYPE_UINT64, // TYPE_UINT64
CPPTYPE_INT32, // TYPE_INT32
CPPTYPE_UINT64, // TYPE_FIXED64
CPPTYPE_UINT32, // TYPE_FIXED32
CPPTYPE_BOOL, // TYPE_BOOL
CPPTYPE_STRING, // TYPE_STRING
CPPTYPE_MESSAGE, // TYPE_GROUP
CPPTYPE_MESSAGE, // TYPE_MESSAGE
CPPTYPE_STRING, // TYPE_BYTES
CPPTYPE_UINT32, // TYPE_UINT32
CPPTYPE_ENUM, // TYPE_ENUM
CPPTYPE_INT32, // TYPE_SFIXED32
CPPTYPE_INT64, // TYPE_SFIXED64
CPPTYPE_INT32, // TYPE_SINT32
CPPTYPE_INT64, // TYPE_SINT64
FieldDescriptor::CPPTYPE_DOUBLE, // TYPE_DOUBLE
FieldDescriptor::CPPTYPE_FLOAT, // TYPE_FLOAT
FieldDescriptor::CPPTYPE_INT64, // TYPE_INT64
FieldDescriptor::CPPTYPE_UINT64, // TYPE_UINT64
FieldDescriptor::CPPTYPE_INT32, // TYPE_INT32
FieldDescriptor::CPPTYPE_UINT64, // TYPE_FIXED64
FieldDescriptor::CPPTYPE_UINT32, // TYPE_FIXED32
FieldDescriptor::CPPTYPE_BOOL, // TYPE_BOOL
FieldDescriptor::CPPTYPE_STRING, // TYPE_STRING
FieldDescriptor::CPPTYPE_MESSAGE, // TYPE_GROUP
FieldDescriptor::CPPTYPE_MESSAGE, // TYPE_MESSAGE
FieldDescriptor::CPPTYPE_STRING, // TYPE_BYTES
FieldDescriptor::CPPTYPE_UINT32, // TYPE_UINT32
FieldDescriptor::CPPTYPE_ENUM, // TYPE_ENUM
FieldDescriptor::CPPTYPE_INT32, // TYPE_SFIXED32
FieldDescriptor::CPPTYPE_INT64, // TYPE_SFIXED64
FieldDescriptor::CPPTYPE_INT32, // TYPE_SINT32
FieldDescriptor::CPPTYPE_INT64, // TYPE_SINT64
};
const char* const FieldDescriptor::kTypeToName[MAX_TYPE + 1] = {
static const char* const kTypeToName[FieldDescriptor::MAX_TYPE + 1] = {
"ERROR", // 0 is reserved for errors
"double", // TYPE_DOUBLE
@@ -838,7 +838,7 @@ const char* const FieldDescriptor::kTypeToName[MAX_TYPE + 1] = {
"sint64", // TYPE_SINT64
};
const char* const FieldDescriptor::kCppTypeToName[MAX_CPPTYPE + 1] = {
static const char* const kCppTypeToName[FieldDescriptor::MAX_CPPTYPE + 1] = {
"ERROR", // 0 is reserved for errors
"int32", // CPPTYPE_INT32
@@ -853,7 +853,7 @@ const char* const FieldDescriptor::kCppTypeToName[MAX_CPPTYPE + 1] = {
"message", // CPPTYPE_MESSAGE
};
const char* const FieldDescriptor::kLabelToName[MAX_LABEL + 1] = {
static const char* const kLabelToName[FieldDescriptor::MAX_LABEL + 1] = {
"ERROR", // 0 is reserved for errors
"optional", // LABEL_OPTIONAL
@@ -861,6 +861,22 @@ const char* const FieldDescriptor::kLabelToName[MAX_LABEL + 1] = {
"repeated", // LABEL_REPEATED
};
const FieldDescriptor::CppType *FieldDescriptor::GetTypeToCppTypeMap() {
return kTypeToCppTypeMap;
}
const char* const *FieldDescriptor::GetTypeToName() {
return kTypeToName;
}
const char* const *FieldDescriptor::GetCppTypeToName() {
return kCppTypeToName;
}
const char* const *FieldDescriptor::GetLabelToName() {
return kLabelToName;
}
const char* FileDescriptor::SyntaxName(FileDescriptor::Syntax syntax) {
switch (syntax) {
case SYNTAX_PROTO2: