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