• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   std::string name(desc.name());
41   if (!desc.containing_type())
42     return allowlist.messages[name];
43 
44   Allowlist::Message& parent =
45       ResolveMessageForDescriptor(*desc.containing_type(), allowlist);
46   return parent.nested_messages[name];
47 }
48 
AllowlistEnum(const google::protobuf::EnumDescriptor & desc,Allowlist & allowlist)49 void AllowlistEnum(const google::protobuf::EnumDescriptor& desc,
50                    Allowlist& allowlist) {
51   std::string name(desc.name());
52   if (!desc.containing_type()) {
53     allowlist.enums.emplace(name);
54     return;
55   }
56 
57   auto& containing =
58       ResolveMessageForDescriptor(*desc.containing_type(), allowlist);
59   containing.enums.emplace(name);
60 }
61 
AllowlistField(const google::protobuf::FieldDescriptor & desc,Allowlist & allowlist)62 void AllowlistField(const google::protobuf::FieldDescriptor& desc,
63                     Allowlist& allowlist) {
64   auto& containing =
65       ResolveMessageForDescriptor(*desc.containing_type(), allowlist);
66 
67   // Check if this field is already allowed and return if so; otherwise add it.
68   // We need to do slightly different things based on whether or not this field
69   // is in a oneof.
70   if (desc.containing_oneof()) {
71     auto& oneof =
72         containing.oneofs[std::string(desc.containing_oneof()->name())];
73     if (!oneof.emplace(desc.number()).second) {
74       return;
75     }
76   } else {
77     if (!containing.fields.emplace(desc.number()).second)
78       return;
79   }
80 
81   switch (desc.type()) {
82     case google::protobuf::FieldDescriptor::TYPE_MESSAGE:
83       // For message types, we recursively allow all fields under it including
84       // any types those fields may depend on.
85       for (int i = 0; i < desc.message_type()->field_count(); ++i) {
86         AllowlistField(*desc.message_type()->field(i), allowlist);
87       }
88       break;
89     case google::protobuf::FieldDescriptor::TYPE_ENUM:
90       // For enums, we allow the enum type.
91       AllowlistEnum(*desc.enum_type(), allowlist);
92       break;
93     default:
94       // We don't need to do anything for primitive types.
95       break;
96   }
97 }
98 
99 }  // namespace
100 
AllowlistFromFieldList(const google::protobuf::Descriptor & desc,const std::vector<std::string> & allowed_fields,Allowlist & allowlist)101 base::Status AllowlistFromFieldList(
102     const google::protobuf::Descriptor& desc,
103     const std::vector<std::string>& allowed_fields,
104     Allowlist& allowlist) {
105   for (const auto& field_path : allowed_fields) {
106     std::vector<std::string> pieces = SplitFieldPath(field_path);
107     const auto* current = &desc;
108     for (size_t i = 0; i < pieces.size(); ++i) {
109       const auto* field = current->FindFieldByName(pieces[i]);
110       if (!field) {
111         return base::ErrStatus("Field %s in message %s not found.",
112                                pieces[i].c_str(),
113                                std::string(current->name()).c_str());
114       }
115       if (i == pieces.size() - 1) {
116         // For the last field, allow the field and any messages it depends on
117         // recursively.
118         AllowlistField(*field, allowlist);
119         break;
120       }
121 
122       // All fields before the last should lead to a message type.
123       if (field->type() != google::protobuf::FieldDescriptor::TYPE_MESSAGE) {
124         return base::ErrStatus("Field %s in message %s has a non-message type",
125                                std::string(field->name()).c_str(),
126                                std::string(desc.name()).c_str());
127       }
128       current = field->message_type();
129     }
130   }
131   return base::OkStatus();
132 }
133 
134 }  // namespace proto_merger
135 }  // namespace perfetto
136