1 /*
2 * Copyright (C) 2019, 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 "utils.h"
18
19 namespace android {
20 namespace stats_log_api_gen {
21
22 /**
23 * Inlining this method because "android-base/strings.h" is not available on
24 * google3.
25 */
Split(const string & s,const string & delimiters)26 static vector<string> Split(const string& s, const string& delimiters) {
27 GOOGLE_CHECK_NE(delimiters.size(), 0U);
28
29 vector<string> result;
30
31 size_t base = 0;
32 size_t found;
33 while (true) {
34 found = s.find_first_of(delimiters, base);
35 result.push_back(s.substr(base, found - base));
36 if (found == s.npos) break;
37 base = found + 1;
38 }
39
40 return result;
41 }
42
build_non_chained_decl_map(const Atoms & atoms,std::map<int,AtomDeclSet::const_iterator> * decl_map)43 void build_non_chained_decl_map(const Atoms& atoms,
44 std::map<int, AtomDeclSet::const_iterator>* decl_map) {
45 for (AtomDeclSet::const_iterator atomIt = atoms.non_chained_decls.begin();
46 atomIt != atoms.non_chained_decls.end(); atomIt++) {
47 decl_map->insert(std::make_pair((*atomIt)->code, atomIt));
48 }
49 }
50
get_annotation_id_constants()51 const map<AnnotationId, string>& get_annotation_id_constants() {
52 static const map<AnnotationId, string>* ANNOTATION_ID_CONSTANTS =
53 new map<AnnotationId, string>{
54 {ANNOTATION_ID_IS_UID, "ANNOTATION_ID_IS_UID"},
55 {ANNOTATION_ID_TRUNCATE_TIMESTAMP, "ANNOTATION_ID_TRUNCATE_TIMESTAMP"},
56 {ANNOTATION_ID_PRIMARY_FIELD, "ANNOTATION_ID_PRIMARY_FIELD"},
57 {ANNOTATION_ID_PRIMARY_FIELD_FIRST_UID, "ANNOTATION_ID_PRIMARY_FIELD_FIRST_UID"},
58 {ANNOTATION_ID_EXCLUSIVE_STATE, "ANNOTATION_ID_EXCLUSIVE_STATE"},
59 {ANNOTATION_ID_TRIGGER_STATE_RESET, "ANNOTATION_ID_TRIGGER_STATE_RESET"},
60 {ANNOTATION_ID_STATE_NESTED, "ANNOTATION_ID_STATE_NESTED"}};
61
62 return *ANNOTATION_ID_CONSTANTS;
63 }
64
65 /**
66 * Turn lower and camel case into upper case with underscores.
67 */
make_constant_name(const string & str)68 string make_constant_name(const string& str) {
69 string result;
70 const int N = str.size();
71 bool underscore_next = false;
72 for (int i = 0; i < N; i++) {
73 char c = str[i];
74 if (c >= 'A' && c <= 'Z') {
75 if (underscore_next) {
76 result += '_';
77 underscore_next = false;
78 }
79 } else if (c >= 'a' && c <= 'z') {
80 c = 'A' + c - 'a';
81 underscore_next = true;
82 } else if (c == '_') {
83 underscore_next = false;
84 }
85 result += c;
86 }
87 return result;
88 }
89
cpp_type_name(java_type_t type)90 const char* cpp_type_name(java_type_t type) {
91 switch (type) {
92 case JAVA_TYPE_BOOLEAN:
93 return "bool";
94 case JAVA_TYPE_INT: // Fallthrough.
95 case JAVA_TYPE_ENUM:
96 return "int32_t";
97 case JAVA_TYPE_LONG:
98 return "int64_t";
99 case JAVA_TYPE_FLOAT:
100 return "float";
101 case JAVA_TYPE_DOUBLE:
102 return "double";
103 case JAVA_TYPE_STRING:
104 return "char const*";
105 case JAVA_TYPE_BYTE_ARRAY:
106 return "const BytesField&";
107 case JAVA_TYPE_BOOLEAN_ARRAY:
108 return "const bool*";
109 case JAVA_TYPE_INT_ARRAY: // Fallthrough.
110 case JAVA_TYPE_ENUM_ARRAY:
111 return "const std::vector<int32_t>&";
112 case JAVA_TYPE_LONG_ARRAY:
113 return "const std::vector<int64_t>&";
114 case JAVA_TYPE_FLOAT_ARRAY:
115 return "const std::vector<float>&";
116 case JAVA_TYPE_STRING_ARRAY:
117 return "const std::vector<char const*>&";
118 case JAVA_TYPE_DOUBLE_ARRAY:
119 return "const std::vector<double>&";
120 default:
121 return "UNKNOWN";
122 }
123 }
124
java_type_name(java_type_t type)125 const char* java_type_name(java_type_t type) {
126 switch (type) {
127 case JAVA_TYPE_BOOLEAN:
128 return "boolean";
129 case JAVA_TYPE_INT: // Fallthrough.
130 case JAVA_TYPE_ENUM:
131 return "int";
132 case JAVA_TYPE_LONG:
133 return "long";
134 case JAVA_TYPE_FLOAT:
135 return "float";
136 case JAVA_TYPE_DOUBLE:
137 return "double";
138 case JAVA_TYPE_STRING:
139 return "java.lang.String";
140 case JAVA_TYPE_BYTE_ARRAY:
141 return "byte[]";
142 case JAVA_TYPE_BOOLEAN_ARRAY:
143 return "boolean[]";
144 case JAVA_TYPE_INT_ARRAY: // Fallthrough.
145 case JAVA_TYPE_ENUM_ARRAY:
146 return "int[]";
147 case JAVA_TYPE_LONG_ARRAY:
148 return "long[]";
149 case JAVA_TYPE_FLOAT_ARRAY:
150 return "float[]";
151 case JAVA_TYPE_STRING_ARRAY:
152 return "java.lang.String[]";
153 case JAVA_TYPE_DOUBLE_ARRAY:
154 return "double[]";
155 default:
156 return "UNKNOWN";
157 }
158 }
159
160 // Does not include AttributionChain type.
is_repeated_field(java_type_t type)161 bool is_repeated_field(java_type_t type) {
162 switch (type) {
163 case JAVA_TYPE_BOOLEAN_ARRAY:
164 case JAVA_TYPE_INT_ARRAY:
165 case JAVA_TYPE_FLOAT_ARRAY:
166 case JAVA_TYPE_LONG_ARRAY:
167 case JAVA_TYPE_STRING_ARRAY:
168 case JAVA_TYPE_ENUM_ARRAY:
169 return true;
170 default:
171 return false;
172 }
173 }
174
175 // Native
176 // Writes namespaces for the cpp and header files, returning the number of
177 // namespaces written.
write_namespace(FILE * out,const string & cppNamespaces)178 void write_namespace(FILE* out, const string& cppNamespaces) {
179 vector<string> cppNamespaceVec = Split(cppNamespaces, ",");
180 for (const string& cppNamespace : cppNamespaceVec) {
181 fprintf(out, "namespace %s {\n", cppNamespace.c_str());
182 }
183 }
184
185 // Writes namespace closing brackets for cpp and header files.
write_closing_namespace(FILE * out,const string & cppNamespaces)186 void write_closing_namespace(FILE* out, const string& cppNamespaces) {
187 vector<string> cppNamespaceVec = Split(cppNamespaces, ",");
188 for (auto it = cppNamespaceVec.rbegin(); it != cppNamespaceVec.rend(); ++it) {
189 fprintf(out, "} // namespace %s\n", it->c_str());
190 }
191 }
192
write_cpp_usage(FILE * out,const string & method_name,const string & atom_code_name,const shared_ptr<AtomDecl> atom,const AtomDecl & attributionDecl)193 static void write_cpp_usage(FILE* out, const string& method_name, const string& atom_code_name,
194 const shared_ptr<AtomDecl> atom, const AtomDecl& attributionDecl) {
195 fprintf(out, " * Usage: %s(StatsLog.%s", method_name.c_str(), atom_code_name.c_str());
196
197 for (vector<AtomField>::const_iterator field = atom->fields.begin();
198 field != atom->fields.end(); field++) {
199 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
200 for (const auto& chainField : attributionDecl.fields) {
201 if (chainField.javaType == JAVA_TYPE_STRING) {
202 fprintf(out, ", const std::vector<%s>& %s", cpp_type_name(chainField.javaType),
203 chainField.name.c_str());
204 } else {
205 fprintf(out, ", const %s* %s, size_t %s_length",
206 cpp_type_name(chainField.javaType), chainField.name.c_str(),
207 chainField.name.c_str());
208 }
209 }
210 } else {
211 fprintf(out, ", %s %s", cpp_type_name(field->javaType), field->name.c_str());
212 }
213 }
214 fprintf(out, ");\n");
215 }
216
write_native_atom_constants(FILE * out,const Atoms & atoms,const AtomDecl & attributionDecl)217 void write_native_atom_constants(FILE* out, const Atoms& atoms, const AtomDecl& attributionDecl) {
218 fprintf(out, "/**\n");
219 fprintf(out, " * Constants for atom codes.\n");
220 fprintf(out, " */\n");
221 fprintf(out, "enum {\n");
222
223 std::map<int, AtomDeclSet::const_iterator> atom_code_to_non_chained_decl_map;
224 build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
225
226 size_t i = 0;
227 // Print atom constants
228 for (AtomDeclSet::const_iterator atomIt = atoms.decls.begin(); atomIt != atoms.decls.end();
229 atomIt++) {
230 string constant = make_constant_name((*atomIt)->name);
231 fprintf(out, "\n");
232 fprintf(out, " /**\n");
233 fprintf(out, " * %s %s\n", (*atomIt)->message.c_str(), (*atomIt)->name.c_str());
234 write_cpp_usage(out, "stats_write", constant, *atomIt, attributionDecl);
235
236 auto non_chained_decl = atom_code_to_non_chained_decl_map.find((*atomIt)->code);
237 if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
238 write_cpp_usage(out, "stats_write_non_chained", constant, *non_chained_decl->second,
239 attributionDecl);
240 }
241 fprintf(out, " */\n");
242 char const* const comma = (i == atoms.decls.size() - 1) ? "" : ",";
243 fprintf(out, " %s = %d%s\n", constant.c_str(), (*atomIt)->code, comma);
244 i++;
245 }
246 fprintf(out, "\n");
247 fprintf(out, "};\n");
248 fprintf(out, "\n");
249 }
250
251 // Java
write_java_atom_codes(FILE * out,const Atoms & atoms)252 void write_java_atom_codes(FILE* out, const Atoms& atoms) {
253 fprintf(out, " // Constants for atom codes.\n");
254
255 std::map<int, AtomDeclSet::const_iterator> atom_code_to_non_chained_decl_map;
256 build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
257
258 // Print constants for the atom codes.
259 for (AtomDeclSet::const_iterator atomIt = atoms.decls.begin(); atomIt != atoms.decls.end();
260 atomIt++) {
261 string constant = make_constant_name((*atomIt)->name);
262 fprintf(out, "\n");
263 fprintf(out, " /**\n");
264 fprintf(out, " * %s %s<br>\n", (*atomIt)->message.c_str(), (*atomIt)->name.c_str());
265 write_java_usage(out, "write", constant, **atomIt);
266 auto non_chained_decl = atom_code_to_non_chained_decl_map.find((*atomIt)->code);
267 if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
268 write_java_usage(out, "write_non_chained", constant, **(non_chained_decl->second));
269 }
270 fprintf(out, " */\n");
271 fprintf(out, " public static final int %s = %d;\n", constant.c_str(), (*atomIt)->code);
272 }
273 fprintf(out, "\n");
274 }
275
write_java_enum_values(FILE * out,const Atoms & atoms)276 void write_java_enum_values(FILE* out, const Atoms& atoms) {
277 fprintf(out, " // Constants for enum values.\n\n");
278 for (AtomDeclSet::const_iterator atomIt = atoms.decls.begin(); atomIt != atoms.decls.end();
279 atomIt++) {
280 for (vector<AtomField>::const_iterator field = (*atomIt)->fields.begin();
281 field != (*atomIt)->fields.end(); field++) {
282 if (field->javaType == JAVA_TYPE_ENUM) {
283 fprintf(out, " // Values for %s.%s\n", (*atomIt)->message.c_str(),
284 field->name.c_str());
285 for (map<int, string>::const_iterator value = field->enumValues.begin();
286 value != field->enumValues.end(); value++) {
287 fprintf(out, " public static final int %s__%s__%s = %d;\n",
288 make_constant_name((*atomIt)->message).c_str(),
289 make_constant_name(field->name).c_str(),
290 make_constant_name(value->second).c_str(), value->first);
291 }
292 fprintf(out, "\n");
293 }
294 }
295 }
296 }
297
write_java_usage(FILE * out,const string & method_name,const string & atom_code_name,const AtomDecl & atom)298 void write_java_usage(FILE* out, const string& method_name, const string& atom_code_name,
299 const AtomDecl& atom) {
300 fprintf(out, " * Usage: StatsLog.%s(StatsLog.%s", method_name.c_str(),
301 atom_code_name.c_str());
302 for (vector<AtomField>::const_iterator field = atom.fields.begin(); field != atom.fields.end();
303 field++) {
304 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
305 fprintf(out, ", android.os.WorkSource workSource");
306 } else if (field->javaType == JAVA_TYPE_BYTE_ARRAY) {
307 fprintf(out, ", byte[] %s", field->name.c_str());
308 } else {
309 fprintf(out, ", %s %s", java_type_name(field->javaType), field->name.c_str());
310 }
311 }
312 fprintf(out, ");<br>\n");
313 }
314
write_java_non_chained_methods(FILE * out,const SignatureInfoMap & signatureInfoMap)315 int write_java_non_chained_methods(FILE* out, const SignatureInfoMap& signatureInfoMap) {
316 for (auto signatureInfoMapIt = signatureInfoMap.begin();
317 signatureInfoMapIt != signatureInfoMap.end(); signatureInfoMapIt++) {
318 // Print method signature.
319 fprintf(out, " public static void write_non_chained(int code");
320 vector<java_type_t> signature = signatureInfoMapIt->first;
321 int argIndex = 1;
322 for (vector<java_type_t>::const_iterator arg = signature.begin(); arg != signature.end();
323 arg++) {
324 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
325 fprintf(stderr, "Non chained signatures should not have attribution chains.\n");
326 return 1;
327 } else {
328 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
329 }
330 argIndex++;
331 }
332 fprintf(out, ") {\n");
333
334 fprintf(out, " write(code");
335 argIndex = 1;
336 for (vector<java_type_t>::const_iterator arg = signature.begin(); arg != signature.end();
337 arg++) {
338 // First two args are uid and tag of attribution chain.
339 if (argIndex == 1) {
340 fprintf(out, ", new int[] {arg%d}", argIndex);
341 } else if (argIndex == 2) {
342 fprintf(out, ", new java.lang.String[] {arg%d}", argIndex);
343 } else {
344 fprintf(out, ", arg%d", argIndex);
345 }
346 argIndex++;
347 }
348 fprintf(out, ");\n");
349 fprintf(out, " }\n");
350 fprintf(out, "\n");
351 }
352 return 0;
353 }
354
write_java_work_source_methods(FILE * out,const SignatureInfoMap & signatureInfoMap)355 int write_java_work_source_methods(FILE* out, const SignatureInfoMap& signatureInfoMap) {
356 fprintf(out, " // WorkSource methods.\n");
357 for (auto signatureInfoMapIt = signatureInfoMap.begin();
358 signatureInfoMapIt != signatureInfoMap.end(); signatureInfoMapIt++) {
359 vector<java_type_t> signature = signatureInfoMapIt->first;
360 // Determine if there is Attribution in this signature.
361 int attributionArg = -1;
362 int argIndexMax = 0;
363 for (vector<java_type_t>::const_iterator arg = signature.begin(); arg != signature.end();
364 arg++) {
365 argIndexMax++;
366 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
367 if (attributionArg > -1) {
368 fprintf(stderr, "An atom contains multiple AttributionNode fields.\n");
369 fprintf(stderr, "This is not supported. Aborting WorkSource method writing.\n");
370 fprintf(out,
371 "\n// Invalid for WorkSource: more than one attribution "
372 "chain.\n");
373 return 1;
374 }
375 attributionArg = argIndexMax;
376 }
377 }
378 if (attributionArg < 0) {
379 continue;
380 }
381
382 fprintf(out, "\n");
383 // Method header (signature)
384 fprintf(out, " public static void write(int code");
385 int argIndex = 1;
386 for (vector<java_type_t>::const_iterator arg = signature.begin(); arg != signature.end();
387 arg++) {
388 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
389 fprintf(out, ", android.os.WorkSource ws");
390 } else {
391 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
392 }
393 argIndex++;
394 }
395 fprintf(out, ") {\n");
396
397 // write_non_chained() component. TODO: Remove when flat uids are no longer
398 // needed.
399 fprintf(out, " for (int i = 0; i < ws.size(); ++i) {\n");
400 fprintf(out, " write_non_chained(code");
401 for (int argIndex = 1; argIndex <= argIndexMax; argIndex++) {
402 if (argIndex == attributionArg) {
403 fprintf(out, ", ws.getUid(i), ws.getPackageName(i)");
404 } else {
405 fprintf(out, ", arg%d", argIndex);
406 }
407 }
408 fprintf(out, ");\n");
409 fprintf(out, " }\n"); // close for-loop
410
411 // write() component.
412 fprintf(out,
413 " java.util.List<android.os.WorkSource.WorkChain> workChains = "
414 "ws.getWorkChains();\n");
415 fprintf(out, " if (workChains != null) {\n");
416 fprintf(out,
417 " for (android.os.WorkSource.WorkChain wc : workChains) "
418 "{\n");
419 fprintf(out, " write(code");
420 for (int argIndex = 1; argIndex <= argIndexMax; argIndex++) {
421 if (argIndex == attributionArg) {
422 fprintf(out, ", wc.getUids(), wc.getTags()");
423 } else {
424 fprintf(out, ", arg%d", argIndex);
425 }
426 }
427 fprintf(out, ");\n");
428 fprintf(out, " }\n"); // close for-loop
429 fprintf(out, " }\n"); // close if
430 fprintf(out, " }\n"); // close method
431 }
432 return 0;
433 }
434
435 } // namespace stats_log_api_gen
436 } // namespace android
437