OSDN Git Service

Merge "Implement QS spec."
[android-x86/frameworks-base.git] / tools / incident_section_gen / main.cpp
1 /*
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
18 #include <frameworks/base/core/proto/android/os/incident.pb.h>
19
20 #include <map>
21 #include <set>
22 #include <string>
23 #include <sstream>
24
25 using namespace android;
26 using namespace android::os;
27 using namespace google::protobuf;
28 using namespace google::protobuf::io;
29 using namespace google::protobuf::internal;
30 using namespace std;
31
32 /**
33  * Implementation details:
34  * This binary auto generates .cpp files for incident and incidentd.
35  *
36  * When argument "incident" is specified, it generates incident_section.cpp file.
37  *
38  * When argument "incidentd" is specified, it generates section_list.cpp file.
39  *
40  * In section_list.cpp file, it generates a SECTION_LIST array and a PRIVACY_POLICY_LIST array.
41  * For SECTION_LIST, it generates Section.h classes only for proto fields with section option enabled.
42  * For PRIVACY_POLICY_LIST, it generates Privacy.h classes only for proto fields with privacy option enabled.
43  *
44  * For Privacy struct, it is possible to have self recursion definitions since protobuf is defining "classes"
45  * So the logic to handle it becomes very complicated when Privacy tag of a message contains a list of Privacies
46  * of its sub-messages. The code also handles multiple depth of self recursion fields.
47  *
48  * For example here is a one level self recursion message WindowManager:
49  * message WindowState {
50  *     string state = 1 [(privacy).dest = LOCAL];
51  *     int32  display_id = 2;
52  *     repeated WindowState child_windows = 3;
53  * }
54  *
55  * message WindowManager {
56  *     WindowState my_window = 1;
57  * }
58  *
59  * When generating Privacy options for WindowManager, this tool will generate cpp syntax source code:
60  *
61  * #include "section_list.h"
62  * ...
63  * Privacy WindowState__state { 1, 9, NULL, LOCAL, NULL }; // first two integers are values for field id and proto type.
64  * Privacy WindowState__child_windows { 3, 11, NULL, UNSET, NULL }; // reserved for WindowState_LIST
65  * Privacy* WindowState__MSG__UNSET[] = {
66  *     &WindowState_state,
67  *     // display id is default, nothing is generated.
68  *     &WindowState_child_windows,
69  *     NULL  // terminator of the array
70  * };
71  * Privacy WindowState__my_window { 1, 11, WindowState__MSG__UNSET, UNSET, NULL };
72  *
73  * createList() {
74  *    ...
75  *    WindowState_child_windows.children = WindowState__MSG_UNSET; // point to its own definition after the list is defined.
76  *    ...
77  * }
78  *
79  * const Privacy** PRIVACY_POLICY_LIST = createList();
80  * const int PRIVACY_POLICY_COUNT = 1;
81  *
82  * Privacy Value Inheritance rules:
83  * 1. Both field and message can be tagged with DESTINATION: LOCAL(L), EXPLICIT(E), AUTOMATIC(A).
84  * 2. Primitives inherits containing message's tag unless defined explicitly.
85  * 3. Containing message's tag doesn't apply to message fields, even when unset (in this case, uses its default message tag).
86  * 4. Message field tag overrides its default message tag.
87  * 5. UNSET tag defaults to EXPLICIT.
88  */
89
90 // The assignments will be called when constructs PRIVACY_POLICY_LIST, has to be global variable
91 vector<string> gSelfRecursionAssignments;
92
93 static inline void emptyline() {
94     printf("\n");
95 }
96
97 static void generateHead(const char* header) {
98     printf("// Auto generated file. Do not modify\n");
99     emptyline();
100     printf("#include \"%s.h\"\n", header);
101     emptyline();
102 }
103
104 // ======================== incident_sections =============================
105 static bool generateIncidentSectionsCpp(Descriptor const* descriptor)
106 {
107     generateHead("incident_sections");
108
109     map<string,FieldDescriptor const*> sections;
110     int N;
111     N = descriptor->field_count();
112     for (int i=0; i<N; i++) {
113         const FieldDescriptor* field = descriptor->field(i);
114         if (field->type() == FieldDescriptor::TYPE_MESSAGE) {
115             sections[field->name()] = field;
116         }
117     }
118
119     printf("IncidentSection const INCIDENT_SECTIONS[] = {\n");
120     N = sections.size();
121     int i = 0;
122     for (map<string,FieldDescriptor const*>::const_iterator it = sections.begin();
123             it != sections.end(); it++, i++) {
124         const FieldDescriptor* field = it->second;
125         printf("    { %d, \"%s\" }", field->number(), field->name().c_str());
126         if (i != N-1) {
127             printf(",\n");
128         } else {
129             printf("\n");
130         }
131     }
132     printf("};\n");
133
134     printf("const int INCIDENT_SECTION_COUNT = %d;\n", N);
135
136     return true;
137 }
138
139 // ========================= section_list ===================================
140 static void splitAndPrint(const string& args) {
141     size_t base = 0;
142     size_t found;
143     while (true) {
144         found = args.find_first_of(" ", base);
145         if (found != base) {
146             string arg = args.substr(base, found - base);
147             printf(" \"%s\",", arg.c_str());
148         }
149         if (found == args.npos) break;
150         base = found + 1;
151     }
152 }
153
154 static string replaceAll(const string& fieldName, const char oldC, const string& newS) {
155     if (fieldName.find_first_of(oldC) == fieldName.npos) return fieldName.c_str();
156     size_t pos = 0, idx = 0;
157     char* res = new char[fieldName.size() * newS.size() + 1]; // assign a larger buffer
158     while (pos != fieldName.size()) {
159         char cur = fieldName[pos++];
160         if (cur != oldC) {
161             res[idx++] = cur;
162             continue;
163         }
164
165         for (size_t i=0; i<newS.size(); i++) {
166             res[idx++] = newS[i];
167         }
168     }
169     res[idx] = '\0';
170     string result(res);
171     delete [] res;
172     return result;
173 }
174
175 static inline void printPrivacy(const string& name, const FieldDescriptor* field, const string& children,
176         const Destination dest, const string& patterns, const string& comments = "") {
177     printf("Privacy %s = { %d, %d, %s, %d, %s };%s\n", name.c_str(), field->number(), field->type(),
178         children.c_str(), dest, patterns.c_str(), comments.c_str());
179 }
180
181 // Get Custom Options ================================================================================
182 static inline SectionFlags getSectionFlags(const FieldDescriptor* field) {
183     return field->options().GetExtension(section);
184 }
185
186 static inline PrivacyFlags getPrivacyFlags(const FieldDescriptor* field) {
187     return field->options().GetExtension(privacy);
188 }
189
190 static inline PrivacyFlags getPrivacyFlags(const Descriptor* descriptor) {
191     return descriptor->options().GetExtension(msg_privacy);
192 }
193
194 // Get Destinations ===================================================================================
195 static inline Destination getMessageDest(const Descriptor* descriptor, const Destination overridden) {
196     return overridden != DEST_UNSET ? overridden : getPrivacyFlags(descriptor).dest();
197 }
198
199 // Returns field's own dest, when it is a message field, uses its message default tag if unset.
200 static inline Destination getFieldDest(const FieldDescriptor* field) {
201     Destination fieldDest = getPrivacyFlags(field).dest();
202     return field->type() != FieldDescriptor::TYPE_MESSAGE ? fieldDest :
203             getMessageDest(field->message_type(), fieldDest);
204 }
205
206 // Get Names ===========================================================================================
207 static inline string getFieldName(const FieldDescriptor* field) {
208     // replace . with double underscores to avoid name conflicts since fields use snake naming convention
209     return replaceAll(field->full_name(), '.', "__");
210 }
211
212
213 static inline string getMessageName(const Descriptor* descriptor, const Destination overridden) {
214     // replace . with one underscore since messages use camel naming convention
215     return replaceAll(descriptor->full_name(), '.', "_") + "__MSG__" +
216             to_string(getMessageDest(descriptor, overridden));
217 }
218
219 // IsDefault ============================================================================================
220 // Returns true if a field is default. Default is defined as this field has same dest as its containing message.
221 // For message fields, it only looks at its field tag and own default mesaage tag, doesn't recursively go deeper.
222 static inline bool isDefaultField(const FieldDescriptor* field, const Destination containerDest) {
223     Destination fieldDest = getFieldDest(field);
224     if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
225         return fieldDest == containerDest || (fieldDest == DEST_UNSET);
226     } else {
227         return fieldDest == containerDest ||
228             (containerDest == DEST_UNSET && fieldDest == DEST_EXPLICIT) ||
229             (containerDest == DEST_EXPLICIT && fieldDest == DEST_UNSET);
230     }
231 }
232
233 static bool isDefaultMessageImpl(const Descriptor* descriptor, const Destination dest, set<string>* parents) {
234     const int N = descriptor->field_count();
235     const Destination messageDest = getMessageDest(descriptor, dest);
236     parents->insert(descriptor->full_name());
237     for (int i=0; i<N; ++i) {
238         const FieldDescriptor* field = descriptor->field(i);
239         const Destination fieldDest = getFieldDest(field);
240         // If current field is not default, return false immediately
241         if (!isDefaultField(field, messageDest)) return false;
242         switch (field->type()) {
243             case FieldDescriptor::TYPE_MESSAGE:
244                 // if self recursion, don't go deep.
245                 if (parents->find(field->message_type()->full_name()) != parents->end()) break;
246                 // if is a default message, just continue
247                 if (isDefaultMessageImpl(field->message_type(), fieldDest, parents)) break;
248                 // sub message is not default, so this message is always not default
249                 return false;
250             case FieldDescriptor::TYPE_STRING:
251                 if (getPrivacyFlags(field).patterns_size() != 0) return false;
252             default:
253                 continue;
254         }
255     }
256     parents->erase(descriptor->full_name());
257     return true;
258 }
259
260 // Recursively look at if this message is default, meaning all its fields and sub-messages
261 // can be described by the same dest.
262 static bool isDefaultMessage(const Descriptor* descriptor, const Destination dest) {
263     set<string> parents;
264     return isDefaultMessageImpl(descriptor, dest, &parents);
265 }
266
267 // ===============================================================================================================
268 static bool numberInOrder(const FieldDescriptor* f1, const FieldDescriptor* f2) {
269     return f1->number() < f2->number();
270 }
271
272 // field numbers are possibly out of order, sort them here.
273 static vector<const FieldDescriptor*> sortFields(const Descriptor* descriptor) {
274     vector<const FieldDescriptor*> fields;
275     fields.reserve(descriptor->field_count());
276     for (int i=0; i<descriptor->field_count(); i++) {
277         fields.push_back(descriptor->field(i));
278     }
279     std::sort(fields.begin(), fields.end(), numberInOrder);
280     return fields;
281 }
282
283 // This function looks for privacy tags of a message type and recursively its sub-messages.
284 // It generates Privacy objects for each non-default fields including non-default sub-messages.
285 // And if the message has Privacy objects generated, it returns a list of them.
286 // Returns false if the descriptor doesn't have any non default privacy flags set, including its submessages
287 static bool generatePrivacyFlags(const Descriptor* descriptor, const Destination overridden,
288         map<string, bool> &variableNames, set<string>* parents) {
289     const string messageName = getMessageName(descriptor, overridden);
290     const Destination messageDest = getMessageDest(descriptor, overridden);
291
292     if (variableNames.find(messageName) != variableNames.end()) {
293         bool hasDefault = variableNames[messageName];
294         return !hasDefault; // if has default, then don't generate privacy flags.
295     }
296     // insert the message type name so sub-message will figure out if self-recursion occurs
297     parents->insert(messageName);
298
299     // sort fields based on number, iterate though them and generate sub flags first
300     vector<const FieldDescriptor*> fieldsInOrder = sortFields(descriptor);
301     bool hasDefaultFlags[fieldsInOrder.size()];
302     for (size_t i=0; i<fieldsInOrder.size(); i++) {
303         const FieldDescriptor* field = fieldsInOrder[i];
304         const string fieldName = getFieldName(field);
305         const Destination fieldDest = getFieldDest(field);
306
307         if (variableNames.find(fieldName) != variableNames.end()) {
308             hasDefaultFlags[i] = variableNames[fieldName];
309             continue;
310         }
311         hasDefaultFlags[i] = isDefaultField(field, messageDest);
312
313         string fieldMessageName;
314         PrivacyFlags p = getPrivacyFlags(field);
315         switch (field->type()) {
316             case FieldDescriptor::TYPE_MESSAGE:
317                 fieldMessageName = getMessageName(field->message_type(), fieldDest);
318                 if (parents->find(fieldMessageName) != parents->end()) { // Self-Recursion proto definition
319                     if (hasDefaultFlags[i]) {
320                         hasDefaultFlags[i] = isDefaultMessage(field->message_type(), fieldDest);
321                     }
322                     if (!hasDefaultFlags[i]) {
323                         printPrivacy(fieldName, field, "NULL", fieldDest, "NULL",
324                             " // self recursion field of " + fieldMessageName);
325                         // generate the assignment and used to construct createList function later on.
326                         gSelfRecursionAssignments.push_back(fieldName + ".children = " + fieldMessageName);
327                     }
328                 } else if (generatePrivacyFlags(field->message_type(), p.dest(), variableNames, parents)) {
329                     if (variableNames.find(fieldName) == variableNames.end()) {
330                         printPrivacy(fieldName, field, fieldMessageName, fieldDest, "NULL");
331                     }
332                     hasDefaultFlags[i] = false;
333                 } else if (!hasDefaultFlags[i]) {
334                     printPrivacy(fieldName, field, "NULL", fieldDest, "NULL");
335                 }
336                 break;
337             case FieldDescriptor::TYPE_STRING:
338                 if (p.patterns_size() != 0) { // if patterns are specified
339                     if (hasDefaultFlags[i]) break;
340                     printf("const char* %s_patterns[] = {\n", fieldName.c_str());
341                     for (int j=0; j<p.patterns_size(); j++) {
342                         // generated string needs to escape backslash too, duplicate it to allow escape again.
343                         printf("    \"%s\",\n", replaceAll(p.patterns(j), '\\', "\\\\").c_str());
344                     }
345                     printf("    NULL };\n");
346                     printPrivacy(fieldName, field, "NULL", fieldDest, fieldName + "_patterns");
347                     break;
348                 }
349                 // else treat string field as primitive field and goes to default
350             default:
351                 if (!hasDefaultFlags[i]) printPrivacy(fieldName, field, "NULL", fieldDest, "NULL");
352         }
353         // Don't generate a variable twice
354         if (!hasDefaultFlags[i]) variableNames[fieldName] = false;
355     }
356
357     bool allDefaults = true;
358     for (size_t i=0; i<fieldsInOrder.size(); i++) {
359         allDefaults &= hasDefaultFlags[i];
360     }
361
362     parents->erase(messageName); // erase the message type name when exit the message.
363     variableNames[messageName] = allDefaults; // store the privacy tags of the message here to avoid overhead.
364
365     if (allDefaults) return false;
366
367     emptyline();
368     int policyCount = 0;
369     printf("Privacy* %s[] = {\n", messageName.c_str());
370     for (size_t i=0; i<fieldsInOrder.size(); i++) {
371         const FieldDescriptor* field = fieldsInOrder[i];
372         if (hasDefaultFlags[i]) continue;
373         printf("    &%s,\n", getFieldName(field).c_str());
374         policyCount++;
375     }
376     printf("    NULL };\n");
377     emptyline();
378     return true;
379 }
380
381 static bool generateSectionListCpp(Descriptor const* descriptor) {
382     generateHead("section_list");
383
384     // generates SECTION_LIST
385     printf("// Generate SECTION_LIST.\n\n");
386
387     printf("const Section* SECTION_LIST[] = {\n");
388     for (int i=0; i<descriptor->field_count(); i++) {
389         const FieldDescriptor* field = descriptor->field(i);
390
391         if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
392             continue;
393         }
394         const SectionFlags s = getSectionFlags(field);
395         switch (s.type()) {
396             case SECTION_NONE:
397                 continue;
398             case SECTION_FILE:
399                 printf("    new FileSection(%d, \"%s\"),\n", field->number(), s.args().c_str());
400                 break;
401             case SECTION_COMMAND:
402                 printf("    new CommandSection(%d,", field->number());
403                 splitAndPrint(s.args());
404                 printf(" NULL),\n");
405                 break;
406             case SECTION_DUMPSYS:
407                 printf("    new DumpsysSection(%d,", field->number());
408                 splitAndPrint(s.args());
409                 printf(" NULL),\n");
410                 break;
411             case SECTION_LOG:
412                 printf("    new LogSection(%d, %s),\n", field->number(), s.args().c_str());
413                 break;
414             case SECTION_GZIP:
415                 printf("    new GZipSection(%d,", field->number());
416                 splitAndPrint(s.args());
417                 printf(" NULL),\n");
418                 break;
419         }
420     }
421     printf("    NULL };\n");
422
423     emptyline();
424     printf("// =============================================================================\n");
425     emptyline();
426
427     // generates PRIVACY_POLICY_LIST
428     printf("// Generate PRIVACY_POLICY_LIST.\n\n");
429     map<string, bool> variableNames;
430     set<string> parents;
431     vector<const FieldDescriptor*> fieldsInOrder = sortFields(descriptor);
432     bool skip[fieldsInOrder.size()];
433     const Destination incidentDest = getPrivacyFlags(descriptor).dest();
434
435     for (size_t i=0; i<fieldsInOrder.size(); i++) {
436         const FieldDescriptor* field = fieldsInOrder[i];
437         const string fieldName = getFieldName(field);
438         const Destination fieldDest = getFieldDest(field);
439         const string fieldMessageName = getMessageName(field->message_type(), fieldDest);
440
441         skip[i] = true;
442
443         if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
444             continue;
445         }
446         // generate privacy flags for each section.
447         if (generatePrivacyFlags(field->message_type(), fieldDest, variableNames, &parents)) {
448             printPrivacy(fieldName, field, fieldMessageName, fieldDest, "NULL");
449         } else if (isDefaultField(field, incidentDest)) {
450             continue; // don't create a new privacy if the value is default.
451         } else {
452             printPrivacy(fieldName, field, "NULL", fieldDest, "NULL");
453         }
454         skip[i] = false;
455     }
456
457     // generate final PRIVACY_POLICY_LIST
458     emptyline();
459     int policyCount = 0;
460     if (gSelfRecursionAssignments.empty()) {
461         printf("Privacy* privacyArray[] = {\n");
462         for (size_t i=0; i<fieldsInOrder.size(); i++) {
463             if (skip[i]) continue;
464             printf("    &%s,\n", getFieldName(fieldsInOrder[i]).c_str());
465             policyCount++;
466         }
467         printf("};\n\n");
468         printf("const Privacy** PRIVACY_POLICY_LIST = const_cast<const Privacy**>(privacyArray);\n\n");
469         printf("const int PRIVACY_POLICY_COUNT = %d;\n", policyCount);
470     } else {
471         for (size_t i=0; i<fieldsInOrder.size(); i++) {
472             if (!skip[i]) policyCount++;
473         }
474
475         printf("static const Privacy** createList() {\n");
476         for (size_t i=0; i<gSelfRecursionAssignments.size(); ++i) {
477             printf("    %s;\n", gSelfRecursionAssignments[i].c_str());
478         }
479         printf("    Privacy** privacyArray = (Privacy**)malloc(%d * sizeof(Privacy**));\n", policyCount);
480         policyCount = 0; // reset
481         for (size_t i=0; i<fieldsInOrder.size(); i++) {
482             if (skip[i]) continue;
483             printf("    privacyArray[%d] = &%s;\n", policyCount++, getFieldName(fieldsInOrder[i]).c_str());
484         }
485         printf("    return const_cast<const Privacy**>(privacyArray);\n");
486         printf("}\n\n");
487         printf("const Privacy** PRIVACY_POLICY_LIST = createList();\n\n");
488         printf("const int PRIVACY_POLICY_COUNT = %d;\n", policyCount);
489     }
490     return true;
491 }
492
493 // ================================================================================
494 static string replace_string(const string& str, const char replace, const char with)
495 {
496     string result(str);
497     const int N = result.size();
498     for (int i=0; i<N; i++) {
499         if (result[i] == replace) {
500             result[i] = with;
501         }
502     }
503     return result;
504 }
505
506 static void generateCsv(Descriptor const* descriptor, const string& indent, set<string>* parents) {
507     DebugStringOptions options;
508     options.include_comments = true;
509     for (int i=0; i<descriptor->field_count(); i++) {
510         const FieldDescriptor* field = descriptor->field(i);
511         stringstream text;
512         if (field->type() == FieldDescriptor::TYPE_MESSAGE) {
513             text << field->message_type()->name();
514         } else {
515             text << field->type_name();
516         }
517         text << " " << field->name();
518         printf("%s%s,\n", indent.c_str(), replace_string(text.str(), '\n', ' ').c_str());
519         if (field->type() == FieldDescriptor::TYPE_MESSAGE &&
520             parents->find(field->message_type()->full_name()) == parents->end()) {
521             parents->insert(field->message_type()->full_name());
522             generateCsv(field->message_type(), indent + ",", parents);
523             parents->erase(field->message_type()->full_name());
524         }
525     }
526 }
527
528 // ================================================================================
529 int main(int argc, char const *argv[])
530 {
531     if (argc < 2) return 1;
532     const char* module = argv[1];
533
534     Descriptor const* descriptor = IncidentProto::descriptor();
535
536     if (strcmp(module, "incident") == 0) {
537         return !generateIncidentSectionsCpp(descriptor);
538     }
539     if (strcmp(module, "incidentd") == 0 ) {
540         return !generateSectionListCpp(descriptor);
541     }
542     // Generates Csv Format of proto definition for each section.
543     if (strcmp(module, "csv") == 0 && argc > 2) {
544         int sectionId = atoi(argv[2]);
545         for (int i=0; i<descriptor->field_count(); i++) {
546             const FieldDescriptor* field = descriptor->field(i);
547             if (strcmp(field->name().c_str(), argv[2]) == 0
548                 || field->number() == sectionId) {
549                 set<string> parents;
550                 printf("%s\n", field->name().c_str());
551                 generateCsv(field->message_type(), "", &parents);
552                 break;
553             }
554         }
555         // Returns failure if csv is enabled to prevent Android building with it.
556         // It doesn't matter if this command runs manually.
557         return 1;
558     }
559     // Returns failure if not called by the whitelisted modules
560     return 1;
561 }