2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <frameworks/base/core/proto/android/os/incident.pb.h>
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;
33 * Implementation details:
34 * This binary auto generates .cpp files for incident and incidentd.
36 * When argument "incident" is specified, it generates incident_section.cpp file.
38 * When argument "incidentd" is specified, it generates section_list.cpp file.
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.
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.
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;
55 * message WindowManager {
56 * WindowState my_window = 1;
59 * When generating Privacy options for WindowManager, this tool will generate cpp syntax source code:
61 * #include "section_list.h"
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[] = {
67 * // display id is default, nothing is generated.
68 * &WindowState_child_windows,
69 * NULL // terminator of the array
71 * Privacy WindowState__my_window { 1, 11, WindowState__MSG__UNSET, UNSET, NULL };
75 * WindowState_child_windows.children = WindowState__MSG_UNSET; // point to its own definition after the list is defined.
79 * const Privacy** PRIVACY_POLICY_LIST = createList();
80 * const int PRIVACY_POLICY_COUNT = 1;
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.
90 // The assignments will be called when constructs PRIVACY_POLICY_LIST, has to be global variable
91 vector<string> gSelfRecursionAssignments;
93 static inline void emptyline() {
97 static void generateHead(const char* header) {
98 printf("// Auto generated file. Do not modify\n");
100 printf("#include \"%s.h\"\n", header);
104 // ======================== incident_sections =============================
105 static bool generateIncidentSectionsCpp(Descriptor const* descriptor)
107 generateHead("incident_sections");
109 map<string,FieldDescriptor const*> sections;
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;
119 printf("IncidentSection const INCIDENT_SECTIONS[] = {\n");
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());
134 printf("const int INCIDENT_SECTION_COUNT = %d;\n", N);
139 // ========================= section_list ===================================
140 static void splitAndPrint(const string& args) {
144 found = args.find_first_of(" ", base);
146 string arg = args.substr(base, found - base);
147 printf(" \"%s\",", arg.c_str());
149 if (found == args.npos) break;
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++];
165 for (size_t i=0; i<newS.size(); i++) {
166 res[idx++] = newS[i];
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());
181 // Get Custom Options ================================================================================
182 static inline SectionFlags getSectionFlags(const FieldDescriptor* field) {
183 return field->options().GetExtension(section);
186 static inline PrivacyFlags getPrivacyFlags(const FieldDescriptor* field) {
187 return field->options().GetExtension(privacy);
190 static inline PrivacyFlags getPrivacyFlags(const Descriptor* descriptor) {
191 return descriptor->options().GetExtension(msg_privacy);
194 // Get Destinations ===================================================================================
195 static inline Destination getMessageDest(const Descriptor* descriptor, const Destination overridden) {
196 return overridden != DEST_UNSET ? overridden : getPrivacyFlags(descriptor).dest();
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);
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(), '.', "__");
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));
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);
227 return fieldDest == containerDest ||
228 (containerDest == DEST_UNSET && fieldDest == DEST_EXPLICIT) ||
229 (containerDest == DEST_EXPLICIT && fieldDest == DEST_UNSET);
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
250 case FieldDescriptor::TYPE_STRING:
251 if (getPrivacyFlags(field).patterns_size() != 0) return false;
256 parents->erase(descriptor->full_name());
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) {
264 return isDefaultMessageImpl(descriptor, dest, &parents);
267 // ===============================================================================================================
268 static bool numberInOrder(const FieldDescriptor* f1, const FieldDescriptor* f2) {
269 return f1->number() < f2->number();
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));
279 std::sort(fields.begin(), fields.end(), numberInOrder);
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);
292 if (variableNames.find(messageName) != variableNames.end()) {
293 bool hasDefault = variableNames[messageName];
294 return !hasDefault; // if has default, then don't generate privacy flags.
296 // insert the message type name so sub-message will figure out if self-recursion occurs
297 parents->insert(messageName);
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);
307 if (variableNames.find(fieldName) != variableNames.end()) {
308 hasDefaultFlags[i] = variableNames[fieldName];
311 hasDefaultFlags[i] = isDefaultField(field, messageDest);
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);
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);
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");
332 hasDefaultFlags[i] = false;
333 } else if (!hasDefaultFlags[i]) {
334 printPrivacy(fieldName, field, "NULL", fieldDest, "NULL");
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());
345 printf(" NULL };\n");
346 printPrivacy(fieldName, field, "NULL", fieldDest, fieldName + "_patterns");
349 // else treat string field as primitive field and goes to default
351 if (!hasDefaultFlags[i]) printPrivacy(fieldName, field, "NULL", fieldDest, "NULL");
353 // Don't generate a variable twice
354 if (!hasDefaultFlags[i]) variableNames[fieldName] = false;
357 bool allDefaults = true;
358 for (size_t i=0; i<fieldsInOrder.size(); i++) {
359 allDefaults &= hasDefaultFlags[i];
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.
365 if (allDefaults) return false;
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());
376 printf(" NULL };\n");
381 static bool generateSectionListCpp(Descriptor const* descriptor) {
382 generateHead("section_list");
384 // generates SECTION_LIST
385 printf("// Generate SECTION_LIST.\n\n");
387 printf("const Section* SECTION_LIST[] = {\n");
388 for (int i=0; i<descriptor->field_count(); i++) {
389 const FieldDescriptor* field = descriptor->field(i);
391 if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
394 const SectionFlags s = getSectionFlags(field);
399 printf(" new FileSection(%d, \"%s\"),\n", field->number(), s.args().c_str());
401 case SECTION_COMMAND:
402 printf(" new CommandSection(%d,", field->number());
403 splitAndPrint(s.args());
406 case SECTION_DUMPSYS:
407 printf(" new DumpsysSection(%d,", field->number());
408 splitAndPrint(s.args());
412 printf(" new LogSection(%d, %s),\n", field->number(), s.args().c_str());
415 printf(" new GZipSection(%d,", field->number());
416 splitAndPrint(s.args());
421 printf(" NULL };\n");
424 printf("// =============================================================================\n");
427 // generates PRIVACY_POLICY_LIST
428 printf("// Generate PRIVACY_POLICY_LIST.\n\n");
429 map<string, bool> variableNames;
431 vector<const FieldDescriptor*> fieldsInOrder = sortFields(descriptor);
432 bool skip[fieldsInOrder.size()];
433 const Destination incidentDest = getPrivacyFlags(descriptor).dest();
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);
443 if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
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.
452 printPrivacy(fieldName, field, "NULL", fieldDest, "NULL");
457 // generate final PRIVACY_POLICY_LIST
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());
468 printf("const Privacy** PRIVACY_POLICY_LIST = const_cast<const Privacy**>(privacyArray);\n\n");
469 printf("const int PRIVACY_POLICY_COUNT = %d;\n", policyCount);
471 for (size_t i=0; i<fieldsInOrder.size(); i++) {
472 if (!skip[i]) policyCount++;
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());
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());
485 printf(" return const_cast<const Privacy**>(privacyArray);\n");
487 printf("const Privacy** PRIVACY_POLICY_LIST = createList();\n\n");
488 printf("const int PRIVACY_POLICY_COUNT = %d;\n", policyCount);
493 // ================================================================================
494 static string replace_string(const string& str, const char replace, const char with)
497 const int N = result.size();
498 for (int i=0; i<N; i++) {
499 if (result[i] == replace) {
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);
512 if (field->type() == FieldDescriptor::TYPE_MESSAGE) {
513 text << field->message_type()->name();
515 text << field->type_name();
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());
528 // ================================================================================
529 int main(int argc, char const *argv[])
531 if (argc < 2) return 1;
532 const char* module = argv[1];
534 Descriptor const* descriptor = IncidentProto::descriptor();
536 if (strcmp(module, "incident") == 0) {
537 return !generateIncidentSectionsCpp(descriptor);
539 if (strcmp(module, "incidentd") == 0 ) {
540 return !generateSectionListCpp(descriptor);
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) {
550 printf("%s\n", field->name().c_str());
551 generateCsv(field->message_type(), "", &parents);
555 // Returns failure if csv is enabled to prevent Android building with it.
556 // It doesn't matter if this command runs manually.
559 // Returns failure if not called by the whitelisted modules