blob: 674bee13918683b7b0c03a7898d84a12516bd751 [file] [log] [blame]
Joe Onorato1754d742016-11-21 17:51:35 -08001/*
2 * Copyright (C) 2016 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 applicable 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
Joe Onorato76690122016-12-20 08:18:32 -080018#include <frameworks/base/core/proto/android/os/incident.pb.h>
Joe Onorato1754d742016-11-21 17:51:35 -080019
Joe Onorato1754d742016-11-21 17:51:35 -080020#include <map>
Yi Jinbe6de302017-10-24 12:30:24 -070021#include <set>
Yi Jinf8601842017-08-15 22:01:41 -070022#include <string>
Joe Onorato1754d742016-11-21 17:51:35 -080023
Yi Jinf8601842017-08-15 22:01:41 -070024using namespace android;
Joe Onorato1754d742016-11-21 17:51:35 -080025using namespace android::os;
26using namespace google::protobuf;
27using namespace google::protobuf::io;
28using namespace google::protobuf::internal;
29using namespace std;
30
Yi Jinbe6de302017-10-24 12:30:24 -070031/**
32 * Implementation details:
33 * This binary auto generates .cpp files for incident and incidentd.
34 *
35 * When argument "incident" is specified, it generates incident_section.cpp file.
36 *
37 * When argument "incidentd" is specified, it generates section_list.cpp file.
38 *
39 * In section_list.cpp file, it generates a SECTION_LIST array and a PRIVACY_POLICY_LIST array.
40 * For SECTION_LIST, it generates Section.h classes only for proto fields with section option enabled.
41 * For PRIVACY_POLICY_LIST, it generates Privacy.h classes only for proto fields with privacy option enabled.
42 *
43 * For Privacy struct, it is possible to have self recursion definitions since protobuf is defining "classes"
44 * So the logic to handle it becomes very complicated when Privacy tag of a message contains a list of Privacies
45 * of its sub-messages. The code also handles multiple depth of self recursion fields.
46 *
47 * For example here is a one level self recursion message WindowManager:
48 * message WindowState {
49 * string state = 1 [(privacy).dest = LOCAL];
50 * int32 display_id = 2;
51 * repeated WindowState child_windows = 3;
52 * }
53 *
54 * message WindowManager {
55 * WindowState my_window = 1;
56 * }
57 *
58 * When generating Privacy options for WindowManager, this tool will generate cpp syntax source code:
59 *
60 * #include "section_list.h"
61 * ...
62 * Privacy WindowState_state { 1, 9, NULL, LOCAL, NULL }; // first two integers are values for field id and proto type.
63 * Privacy WindowState_child_windows { 3, 11, NULL, DEFAULT, NULL }; // reserved for WindowState_LIST
64 * Privacy* WindowState_MSG_[] = {
65 * &WindowState_state,
66 * // display id is default, nothing is generated.
67 * &WindowState_child_windows,
68 * NULL // terminator of the array
69 * };
70 * Privacy WindowState_my_window { 1, 11, WindowState_my_window_LIST, DEFAULT, NULL };
71 *
72 * createList() {
73 * ...
74 * WindowState_child_windows.children = WindowState_my_window_LIST; // point to its own definition after the list is defined.
75 * ...
76 * }
77 *
78 * const Privacy** PRIVACY_POLICY_LIST = createList();
79 * const int PRIVACY_POLICY_COUNT = 1;
80 */
81
82// The assignments will be called when constructs PRIVACY_POLICY_LIST, has to be global variable
83vector<string> gSelfRecursionAssignments;
84
Yi Jin0ed9b682017-08-18 14:51:20 -070085static inline void emptyline() {
Joe Onorato1754d742016-11-21 17:51:35 -080086 printf("\n");
Yi Jinf8601842017-08-15 22:01:41 -070087}
Joe Onorato1754d742016-11-21 17:51:35 -080088
Yi Jin0ed9b682017-08-18 14:51:20 -070089static void generateHead(const char* header) {
90 printf("// Auto generated file. Do not modify\n");
91 emptyline();
92 printf("#include \"%s.h\"\n", header);
93 emptyline();
94}
95
Yi Jinbe6de302017-10-24 12:30:24 -070096// ======================== incident_sections =============================
Yi Jin0ed9b682017-08-18 14:51:20 -070097static bool generateIncidentSectionsCpp(Descriptor const* descriptor)
Yi Jinf8601842017-08-15 22:01:41 -070098{
99 generateHead("incident_sections");
100
101 map<string,FieldDescriptor const*> sections;
102 int N;
Joe Onorato1754d742016-11-21 17:51:35 -0800103 N = descriptor->field_count();
104 for (int i=0; i<N; i++) {
105 const FieldDescriptor* field = descriptor->field(i);
106 if (field->type() == FieldDescriptor::TYPE_MESSAGE) {
107 sections[field->name()] = field;
108 }
109 }
110
111 printf("IncidentSection const INCIDENT_SECTIONS[] = {\n");
112 N = sections.size();
113 int i = 0;
114 for (map<string,FieldDescriptor const*>::const_iterator it = sections.begin();
115 it != sections.end(); it++, i++) {
116 const FieldDescriptor* field = it->second;
117 printf(" { %d, \"%s\" }", field->number(), field->name().c_str());
118 if (i != N-1) {
119 printf(",\n");
120 } else {
121 printf("\n");
122 }
123 }
124 printf("};\n");
125
126 printf("const int INCIDENT_SECTION_COUNT = %d;\n", N);
127
Yi Jinf8601842017-08-15 22:01:41 -0700128 return true;
129}
130
Yi Jinbe6de302017-10-24 12:30:24 -0700131// ========================= section_list ===================================
Yi Jinf8601842017-08-15 22:01:41 -0700132static void splitAndPrint(const string& args) {
133 size_t base = 0;
134 size_t found;
135 while (true) {
136 found = args.find_first_of(" ", base);
137 if (found != base) {
138 string arg = args.substr(base, found - base);
139 printf(" \"%s\",", arg.c_str());
140 }
141 if (found == args.npos) break;
142 base = found + 1;
143 }
144}
145
Yi Jinbe6de302017-10-24 12:30:24 -0700146static string replaceAll(const string& fieldName, const char oldC, const string& newS) {
147 if (fieldName.find_first_of(oldC) == fieldName.npos) return fieldName.c_str();
Yi Jin0ed9b682017-08-18 14:51:20 -0700148 size_t pos = 0, idx = 0;
Yi Jinbe6de302017-10-24 12:30:24 -0700149 char* res = new char[fieldName.size() * newS.size() + 1]; // assign a larger buffer
150 while (pos != fieldName.size()) {
151 char cur = fieldName[pos++];
Yi Jin0ed9b682017-08-18 14:51:20 -0700152 if (cur != oldC) {
153 res[idx++] = cur;
154 continue;
155 }
156
157 for (size_t i=0; i<newS.size(); i++) {
158 res[idx++] = newS[i];
159 }
160 }
161 res[idx] = '\0';
Yi Jinbe6de302017-10-24 12:30:24 -0700162 string result(res);
Yunlian Jiang3809bbf2017-09-05 15:50:58 -0700163 delete [] res;
164 return result;
Yi Jin0ed9b682017-08-18 14:51:20 -0700165}
166
Yi Jinbe6de302017-10-24 12:30:24 -0700167static string getFieldName(const FieldDescriptor* field) {
168 return replaceAll(field->full_name(), '.', "__");
Yi Jin0ed9b682017-08-18 14:51:20 -0700169}
170
Yi Jinbe6de302017-10-24 12:30:24 -0700171static string getMessageTypeName(const Descriptor* descriptor) {
172 return replaceAll(descriptor->full_name(), '.', "_") + "_MSG_";
173}
174
175static inline SectionFlags getSectionFlags(const FieldDescriptor* field) {
176 return field->options().GetExtension(section);
177}
178
179static inline PrivacyFlags getPrivacyFlags(const FieldDescriptor* field) {
180 return field->options().GetExtension(privacy);
181}
182
183static inline bool isDefaultField(const FieldDescriptor* field) {
184 return getPrivacyFlags(field).dest() == PrivacyFlags::default_instance().dest();
185}
186
187static bool isDefaultMessageImpl(const Descriptor* descriptor, set<string>* parents) {
188 int N = descriptor->field_count();
189 parents->insert(descriptor->full_name());
190 for (int i=0; i<N; ++i) {
191 const FieldDescriptor* field = descriptor->field(i);
192 // look at if the current field is default or not, return false immediately
193 if (!isDefaultField(field)) return false;
194
195 switch (field->type()) {
196 case FieldDescriptor::TYPE_MESSAGE:
197 // if self recursion, don't go deep.
198 if (parents->find(field->message_type()->full_name()) != parents->end()) break;
199 // if is a default message, just continue
200 if (isDefaultMessageImpl(field->message_type(), parents)) break;
201 // sub message is not default, so this message is always not default
202 return false;
203 case FieldDescriptor::TYPE_STRING:
204 if (getPrivacyFlags(field).patterns_size() != 0) return false;
205 default:
206 continue;
207 }
208 }
209 parents->erase(descriptor->full_name());
210 return true;
211}
212
213static bool isDefaultMessage(const Descriptor* descriptor) {
214 set<string> parents;
215 return isDefaultMessageImpl(descriptor, &parents);
216}
217
218// This function is called for looking at privacy tags for a message type and recursively its sub-messages
219// It prints out each fields's privacy tags and a List of Privacy of the message itself (don't print default values)
Yi Jin22769e02017-10-16 14:42:50 -0700220// Returns false if the descriptor doesn't have any non default privacy flags set, including its submessages
Yi Jinbe6de302017-10-24 12:30:24 -0700221static bool generatePrivacyFlags(const Descriptor* descriptor, map<string, bool> &msgNames, set<string>* parents) {
Yi Jin0ed9b682017-08-18 14:51:20 -0700222 bool hasDefaultFlags[descriptor->field_count()];
Yi Jinbe6de302017-10-24 12:30:24 -0700223
224 string messageTypeName = getMessageTypeName(descriptor);
225 // if the message is already defined, skip it.
226 if (msgNames.find(messageTypeName) != msgNames.end()) {
227 bool hasDefault = msgNames[messageTypeName];
228 return !hasDefault; // don't generate if it has default privacy.
229 }
230 // insert the message type name so sub-message will figure out if self-recursion occurs
231 parents->insert(messageTypeName);
232
Yi Jin0ed9b682017-08-18 14:51:20 -0700233 // iterate though its field and generate sub flags first
234 for (int i=0; i<descriptor->field_count(); i++) {
235 hasDefaultFlags[i] = true; // set default to true
Yi Jinbe6de302017-10-24 12:30:24 -0700236
Yi Jin0ed9b682017-08-18 14:51:20 -0700237 const FieldDescriptor* field = descriptor->field(i);
Yi Jinbe6de302017-10-24 12:30:24 -0700238 const string fieldName = getFieldName(field);
239 // check if the same field name is already defined.
240 if (msgNames.find(fieldName) != msgNames.end()) {
241 hasDefaultFlags[i] = msgNames[fieldName];
Yi Jin0ed9b682017-08-18 14:51:20 -0700242 continue;
243 };
244
Yi Jinbe6de302017-10-24 12:30:24 -0700245 PrivacyFlags p = getPrivacyFlags(field);
246 string fieldMessageName;
Yi Jin0ed9b682017-08-18 14:51:20 -0700247 switch (field->type()) {
248 case FieldDescriptor::TYPE_MESSAGE:
Yi Jinbe6de302017-10-24 12:30:24 -0700249 fieldMessageName = getMessageTypeName(field->message_type());
250 if (parents->find(fieldMessageName) != parents->end()) { // Self-Recursion proto definition
251 if (isDefaultField(field)) {
252 hasDefaultFlags[i] = isDefaultMessage(field->message_type());
253 } else {
254 hasDefaultFlags[i] = false;
255 }
256 if (!hasDefaultFlags[i]) {
257 printf("Privacy %s = { %d, %d, NULL, %d, NULL }; // self recursion field of %s\n",
258 fieldName.c_str(), field->number(), field->type(), p.dest(), fieldMessageName.c_str());
259 // generate the assignment and used to construct createList function later on.
260 gSelfRecursionAssignments.push_back(fieldName + ".children = " + fieldMessageName);
261 }
262 break;
263 } else if (generatePrivacyFlags(field->message_type(), msgNames, parents)) {
264 printf("Privacy %s = { %d, %d, %s, %d, NULL };\n", fieldName.c_str(), field->number(),
265 field->type(), fieldMessageName.c_str(), p.dest());
266 } else if (isDefaultField(field)) {
Yi Jin22769e02017-10-16 14:42:50 -0700267 // don't create a new privacy if the value is default.
268 break;
Yi Jinbe6de302017-10-24 12:30:24 -0700269 } else {
270 printf("Privacy %s = { %d, %d, NULL, %d, NULL };\n", fieldName.c_str(), field->number(),
Yi Jin22769e02017-10-16 14:42:50 -0700271 field->type(), p.dest());
272 }
Yi Jin0ed9b682017-08-18 14:51:20 -0700273 hasDefaultFlags[i] = false;
274 break;
275 case FieldDescriptor::TYPE_STRING:
Yi Jinbe6de302017-10-24 12:30:24 -0700276 if (isDefaultField(field) && p.patterns_size() == 0) break;
Yi Jin0ed9b682017-08-18 14:51:20 -0700277
Yi Jinbe6de302017-10-24 12:30:24 -0700278 printf("const char* %s_patterns[] = {\n", fieldName.c_str());
Yi Jin0ed9b682017-08-18 14:51:20 -0700279 for (int i=0; i<p.patterns_size(); i++) {
280 // the generated string need to escape backslash as well, need to dup it here
Yunlian Jiang3809bbf2017-09-05 15:50:58 -0700281 printf(" \"%s\",\n", replaceAll(p.patterns(i), '\\', "\\\\").c_str());
Yi Jin0ed9b682017-08-18 14:51:20 -0700282 }
283 printf(" NULL };\n");
Yi Jinbe6de302017-10-24 12:30:24 -0700284 printf("Privacy %s = { %d, %d, NULL, %d, %s_patterns };\n", fieldName.c_str(), field->number(),
285 field->type(), p.dest(), fieldName.c_str());
Yi Jin0ed9b682017-08-18 14:51:20 -0700286 hasDefaultFlags[i] = false;
287 break;
288 default:
Yi Jinbe6de302017-10-24 12:30:24 -0700289 if (isDefaultField(field)) break;
290 printf("Privacy %s = { %d, %d, NULL, %d, NULL };\n", fieldName.c_str(), field->number(),
Yi Jin22769e02017-10-16 14:42:50 -0700291 field->type(), p.dest());
Yi Jin0ed9b682017-08-18 14:51:20 -0700292 hasDefaultFlags[i] = false;
293 }
294 // add the field name to message map, true means it has default flags
Yi Jinbe6de302017-10-24 12:30:24 -0700295 msgNames[fieldName] = hasDefaultFlags[i];
Yi Jin0ed9b682017-08-18 14:51:20 -0700296 }
297
298 bool allDefaults = true;
299 for (int i=0; i<descriptor->field_count(); i++) {
300 allDefaults &= hasDefaultFlags[i];
301 }
Yi Jinbe6de302017-10-24 12:30:24 -0700302
303 parents->erase(messageTypeName); // erase the message type name when exit the message.
304 msgNames[messageTypeName] = allDefaults; // store the privacy tags of the message here to avoid overhead.
305
Yi Jin22769e02017-10-16 14:42:50 -0700306 if (allDefaults) return false;
Yi Jin0ed9b682017-08-18 14:51:20 -0700307
308 emptyline();
Yi Jin7e0b4e52017-09-12 20:00:25 -0700309 int policyCount = 0;
Yi Jinbe6de302017-10-24 12:30:24 -0700310 printf("Privacy* %s[] = {\n", messageTypeName.c_str());
Yi Jin0ed9b682017-08-18 14:51:20 -0700311 for (int i=0; i<descriptor->field_count(); i++) {
312 const FieldDescriptor* field = descriptor->field(i);
313 if (hasDefaultFlags[i]) continue;
Yi Jinbe6de302017-10-24 12:30:24 -0700314 printf(" &%s,\n", getFieldName(field).c_str());
Yi Jin7e0b4e52017-09-12 20:00:25 -0700315 policyCount++;
Yi Jin0ed9b682017-08-18 14:51:20 -0700316 }
Yi Jinbe6de302017-10-24 12:30:24 -0700317 printf(" NULL };\n");
Yi Jin0ed9b682017-08-18 14:51:20 -0700318 emptyline();
Yi Jin22769e02017-10-16 14:42:50 -0700319 return true;
Yi Jin0ed9b682017-08-18 14:51:20 -0700320}
321
322static bool generateSectionListCpp(Descriptor const* descriptor) {
Yi Jinf8601842017-08-15 22:01:41 -0700323 generateHead("section_list");
324
Yi Jin0ed9b682017-08-18 14:51:20 -0700325 // generates SECTION_LIST
Yi Jinbe6de302017-10-24 12:30:24 -0700326 printf("// Generate SECTION_LIST.\n\n");
327
Yi Jinf8601842017-08-15 22:01:41 -0700328 printf("const Section* SECTION_LIST[] = {\n");
Yi Jinf8601842017-08-15 22:01:41 -0700329 for (int i=0; i<descriptor->field_count(); i++) {
330 const FieldDescriptor* field = descriptor->field(i);
331
332 if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
333 continue;
334 }
Yi Jinbe6de302017-10-24 12:30:24 -0700335 const SectionFlags s = getSectionFlags(field);
Yi Jinf8601842017-08-15 22:01:41 -0700336 switch (s.type()) {
337 case SECTION_NONE:
338 continue;
339 case SECTION_FILE:
340 printf(" new FileSection(%d, \"%s\"),\n", field->number(), s.args().c_str());
341 break;
342 case SECTION_COMMAND:
343 printf(" new CommandSection(%d,", field->number());
344 splitAndPrint(s.args());
345 printf(" NULL),\n");
346 break;
347 case SECTION_DUMPSYS:
348 printf(" new DumpsysSection(%d,", field->number());
349 splitAndPrint(s.args());
350 printf(" NULL),\n");
351 break;
352 }
353 }
Yi Jin0ed9b682017-08-18 14:51:20 -0700354 printf(" NULL };\n");
Yi Jinbe6de302017-10-24 12:30:24 -0700355
356 emptyline();
357 printf("// =============================================================================\n");
Yi Jin0ed9b682017-08-18 14:51:20 -0700358 emptyline();
359
Yi Jinbe6de302017-10-24 12:30:24 -0700360 // generates PRIVACY_POLICY_LIST
361 printf("// Generate PRIVACY_POLICY_LIST.\n\n");
Yi Jin0ed9b682017-08-18 14:51:20 -0700362 map<string, bool> messageNames;
Yi Jinbe6de302017-10-24 12:30:24 -0700363 set<string> parents;
364 bool skip[descriptor->field_count()];
365
366 for (int i=0; i<descriptor->field_count(); i++) {
367 const FieldDescriptor* field = descriptor->field(i);
368 const string fieldName = getFieldName(field);
369 PrivacyFlags p = getPrivacyFlags(field);
370
371 skip[i] = true;
372
373 if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
374 continue;
375 }
376 // generate privacy flags for each field.
377 if (generatePrivacyFlags(field->message_type(), messageNames, &parents)) {
378 printf("Privacy %s { %d, %d, %s, %d, NULL };\n", fieldName.c_str(), field->number(),
379 field->type(), getMessageTypeName(field->message_type()).c_str(), p.dest());
380 } else if (isDefaultField(field)) {
381 continue; // don't create a new privacy if the value is default.
382 } else {
383 printf("Privacy %s { %d, %d, NULL, %d, NULL };\n", fieldName.c_str(), field->number(),
384 field->type(), p.dest());
385 }
386 skip[i] = false;
Yi Jin0ed9b682017-08-18 14:51:20 -0700387 }
388
Yi Jinbe6de302017-10-24 12:30:24 -0700389 // generate final PRIVACY_POLICY_LIST
390 emptyline();
391 int policyCount = 0;
392 if (gSelfRecursionAssignments.empty()) {
393 printf("Privacy* privacyArray[] = {\n");
394 for (int i=0; i<descriptor->field_count(); i++) {
395 if (skip[i]) continue;
396 printf(" &%s,\n", getFieldName(descriptor->field(i)).c_str());
397 policyCount++;
398 }
399 printf("};\n\n");
400 printf("const Privacy** PRIVACY_POLICY_LIST = const_cast<const Privacy**>(privacyArray);\n\n");
401 printf("const int PRIVACY_POLICY_COUNT = %d;\n", policyCount);
402 } else {
403 for (int i=0; i<descriptor->field_count(); i++) {
404 if (!skip[i]) policyCount++;
405 }
406
407 printf("static const Privacy** createList() {\n");
408 for (size_t i=0; i<gSelfRecursionAssignments.size(); ++i) {
409 printf(" %s;\n", gSelfRecursionAssignments[i].c_str());
410 }
411 printf(" Privacy** privacyArray = (Privacy**)malloc(%d * sizeof(Privacy**));\n", policyCount);
412 policyCount = 0; // reset
413 for (int i=0; i<descriptor->field_count(); i++) {
414 if (skip[i]) continue;
415 printf(" privacyArray[%d] = &%s;\n", policyCount++, getFieldName(descriptor->field(i)).c_str());
416 }
417 printf(" return const_cast<const Privacy**>(privacyArray);\n");
418 printf("}\n\n");
419 printf("const Privacy** PRIVACY_POLICY_LIST = createList();\n\n");
420 printf("const int PRIVACY_POLICY_COUNT = %d;\n", policyCount);
421 }
Yi Jinf8601842017-08-15 22:01:41 -0700422 return true;
423}
424
425// ================================================================================
426int main(int argc, char const *argv[])
427{
428 if (argc != 2) return 1;
429 const char* module = argv[1];
430
Yi Jin0ed9b682017-08-18 14:51:20 -0700431 Descriptor const* descriptor = IncidentProto::descriptor();
432
Yi Jinf8601842017-08-15 22:01:41 -0700433 if (strcmp(module, "incident") == 0) {
Yi Jin0ed9b682017-08-18 14:51:20 -0700434 return !generateIncidentSectionsCpp(descriptor);
Yi Jinf8601842017-08-15 22:01:41 -0700435 }
436 if (strcmp(module, "incidentd") == 0 ) {
Yi Jin0ed9b682017-08-18 14:51:20 -0700437 return !generateSectionListCpp(descriptor);
Yi Jinf8601842017-08-15 22:01:41 -0700438 }
439
440 // return failure if not called by the whitelisted modules
441 return 1;
Joe Onorato1754d742016-11-21 17:51:35 -0800442}