1 /*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by appicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/tools/proto_merger/allowlist.h"
18
19 #include <google/protobuf/descriptor.pb.h>
20
21 #include "perfetto/ext/base/string_utils.h"
22
23 namespace perfetto {
24 namespace proto_merger {
25 namespace {
26
SplitFieldPath(const std::string & name)27 std::vector<std::string> SplitFieldPath(const std::string& name) {
28 if (name.empty())
29 return {};
30
31 if (name[0] == '.')
32 return base::SplitString(name.substr(1), ".");
33
34 return base::SplitString(name, ".");
35 }
36
ResolveMessageForDescriptor(const google::protobuf::Descriptor & desc,Allowlist & allowlist)37 Allowlist::Message& ResolveMessageForDescriptor(
38 const google::protobuf::Descriptor& desc,
39 Allowlist& allowlist) {
40 if (!desc.containing_type())
41 return allowlist.messages[desc.name()];
42
43 Allowlist::Message& parent =
44 ResolveMessageForDescriptor(*desc.containing_type(), allowlist);
45 return parent.nested_messages[desc.name()];
46 }
47
AllowlistEnum(const google::protobuf::EnumDescriptor & desc,Allowlist & allowlist)48 void AllowlistEnum(const google::protobuf::EnumDescriptor& desc,
49 Allowlist& allowlist) {
50 if (!desc.containing_type()) {
51 allowlist.enums.emplace(desc.name());
52 return;
53 }
54
55 auto& containing =
56 ResolveMessageForDescriptor(*desc.containing_type(), allowlist);
57 containing.enums.emplace(desc.name());
58 }
59
AllowlistField(const google::protobuf::FieldDescriptor & desc,Allowlist & allowlist)60 void AllowlistField(const google::protobuf::FieldDescriptor& desc,
61 Allowlist& allowlist) {
62 auto& containing =
63 ResolveMessageForDescriptor(*desc.containing_type(), allowlist);
64
65 // Check if this field is already allowed and return if so; otherwise add it.
66 // We need to do slightly different things based on whether or not this field
67 // is in a oneof.
68 if (desc.containing_oneof()) {
69 auto& oneof = containing.oneofs[desc.containing_oneof()->name()];
70 if (!oneof.emplace(desc.number()).second) {
71 return;
72 }
73 } else {
74 if (!containing.fields.emplace(desc.number()).second)
75 return;
76 }
77
78 switch (desc.type()) {
79 case google::protobuf::FieldDescriptor::TYPE_MESSAGE:
80 // For message types, we recursively allow all fields under it including
81 // any types those fields may depend on.
82 for (int i = 0; i < desc.message_type()->field_count(); ++i) {
83 AllowlistField(*desc.message_type()->field(i), allowlist);
84 }
85 break;
86 case google::protobuf::FieldDescriptor::TYPE_ENUM:
87 // For enums, we allow the enum type.
88 AllowlistEnum(*desc.enum_type(), allowlist);
89 break;
90 default:
91 // We don't need to do anything for primitive types.
92 break;
93 }
94 }
95
96 } // namespace
97
AllowlistFromFieldList(const google::protobuf::Descriptor & desc,const std::vector<std::string> & allowed_fields,Allowlist & allowlist)98 base::Status AllowlistFromFieldList(
99 const google::protobuf::Descriptor& desc,
100 const std::vector<std::string>& allowed_fields,
101 Allowlist& allowlist) {
102 for (const auto& field_path : allowed_fields) {
103 std::vector<std::string> pieces = SplitFieldPath(field_path);
104 const auto* current = &desc;
105 for (size_t i = 0; i < pieces.size(); ++i) {
106 const auto* field = current->FindFieldByName(pieces[i]);
107 if (!field) {
108 return base::ErrStatus("Field %s in message %s not found.",
109 pieces[i].c_str(), current->name().c_str());
110 }
111 if (i == pieces.size() - 1) {
112 // For the last field, allow the field and any messages it depends on
113 // recursively.
114 AllowlistField(*field, allowlist);
115 break;
116 }
117
118 // All fields before the last should lead to a message type.
119 if (field->type() != google::protobuf::FieldDescriptor::TYPE_MESSAGE) {
120 return base::ErrStatus("Field %s in message %s has a non-message type",
121 field->name().c_str(), desc.name().c_str());
122 }
123 current = field->message_type();
124 }
125 }
126 return base::OkStatus();
127 }
128
129 } // namespace proto_merger
130 } // namespace perfetto
131