blob: e66ead7ec34063869d137fedad4fd565b03c2a59 [file] [log] [blame]
Yao Chend54f9dd2017-10-17 17:37:48 +00001/*
2 * Copyright (C) 2017, 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 "Collation.h"
Yangster-macba5b9e42018-01-10 21:31:59 -080018#include "frameworks/base/cmds/statsd/src/atoms.pb.h"
Yao Chend54f9dd2017-10-17 17:37:48 +000019
20#include <stdio.h>
21#include <map>
22
23namespace android {
24namespace stats_log_api_gen {
25
Stefan Lafon9478f352017-10-30 21:20:20 -070026using google::protobuf::EnumDescriptor;
Yao Chend54f9dd2017-10-17 17:37:48 +000027using google::protobuf::FieldDescriptor;
28using google::protobuf::FileDescriptor;
29using google::protobuf::SourceLocation;
30using std::map;
31
32
33//
34// AtomDecl class
35//
36
37AtomDecl::AtomDecl()
38 :code(0),
39 name()
40{
41}
42
43AtomDecl::AtomDecl(const AtomDecl& that)
Yao Chen9c1debe2018-02-19 14:39:19 -080044 : code(that.code),
45 name(that.name),
46 message(that.message),
47 fields(that.fields),
48 primaryFields(that.primaryFields),
Yao Chenc40a19d2018-03-15 16:48:25 -070049 exclusiveField(that.exclusiveField),
Yao Chenbbdd67d2018-10-24 12:15:56 -070050 uidField(that.uidField),
Andrei Oneada01ea52019-01-30 15:28:36 +000051 whitelisted(that.whitelisted),
Tej Singh810eeb32019-03-07 19:08:52 -080052 binaryFields(that.binaryFields),
53 hasModule(that.hasModule),
54 moduleName(that.moduleName) {}
Yao Chend54f9dd2017-10-17 17:37:48 +000055
56AtomDecl::AtomDecl(int c, const string& n, const string& m)
57 :code(c),
58 name(n),
59 message(m)
60{
61}
62
63AtomDecl::~AtomDecl()
64{
65}
66
67
68/**
69 * Print an error message for a FieldDescriptor, including the file name and line number.
70 */
71static void
72print_error(const FieldDescriptor* field, const char* format, ...)
73{
74 const Descriptor* message = field->containing_type();
75 const FileDescriptor* file = message->file();
76
77 SourceLocation loc;
78 if (field->GetSourceLocation(&loc)) {
79 // TODO: this will work if we can figure out how to pass --include_source_info to protoc
80 fprintf(stderr, "%s:%d: ", file->name().c_str(), loc.start_line);
81 } else {
82 fprintf(stderr, "%s: ", file->name().c_str());
83 }
84 va_list args;
85 va_start(args, format);
86 vfprintf(stderr, format, args);
87 va_end (args);
88}
89
90/**
91 * Convert a protobuf type into a java type.
92 */
93static java_type_t
94java_type(const FieldDescriptor* field)
95{
96 int protoType = field->type();
97 switch (protoType) {
98 case FieldDescriptor::TYPE_DOUBLE:
99 return JAVA_TYPE_DOUBLE;
100 case FieldDescriptor::TYPE_FLOAT:
101 return JAVA_TYPE_FLOAT;
102 case FieldDescriptor::TYPE_INT64:
103 return JAVA_TYPE_LONG;
104 case FieldDescriptor::TYPE_UINT64:
105 return JAVA_TYPE_LONG;
106 case FieldDescriptor::TYPE_INT32:
107 return JAVA_TYPE_INT;
108 case FieldDescriptor::TYPE_FIXED64:
109 return JAVA_TYPE_LONG;
110 case FieldDescriptor::TYPE_FIXED32:
111 return JAVA_TYPE_INT;
112 case FieldDescriptor::TYPE_BOOL:
113 return JAVA_TYPE_BOOLEAN;
114 case FieldDescriptor::TYPE_STRING:
115 return JAVA_TYPE_STRING;
116 case FieldDescriptor::TYPE_GROUP:
117 return JAVA_TYPE_UNKNOWN;
118 case FieldDescriptor::TYPE_MESSAGE:
119 // TODO: not the final package name
Yangster-mac7604aea2017-12-11 22:55:49 -0800120 if (field->message_type()->full_name() ==
Yangster-mac20877162017-12-22 17:19:39 -0800121 "android.os.statsd.AttributionNode") {
Yangster-mac7604aea2017-12-11 22:55:49 -0800122 return JAVA_TYPE_ATTRIBUTION_CHAIN;
Yangster-mac48b3d622018-08-18 12:38:11 -0700123 } else if (field->message_type()->full_name() ==
124 "android.os.statsd.KeyValuePair") {
125 return JAVA_TYPE_KEY_VALUE_PAIR;
Yao Chenbbdd67d2018-10-24 12:15:56 -0700126 } else if (field->options().GetExtension(os::statsd::log_mode) ==
127 os::statsd::LogMode::MODE_BYTES) {
128 return JAVA_TYPE_BYTE_ARRAY;
Yao Chend54f9dd2017-10-17 17:37:48 +0000129 } else {
130 return JAVA_TYPE_OBJECT;
131 }
132 case FieldDescriptor::TYPE_BYTES:
133 return JAVA_TYPE_BYTE_ARRAY;
134 case FieldDescriptor::TYPE_UINT32:
135 return JAVA_TYPE_INT;
136 case FieldDescriptor::TYPE_ENUM:
Stefan Lafon9478f352017-10-30 21:20:20 -0700137 return JAVA_TYPE_ENUM;
Yao Chend54f9dd2017-10-17 17:37:48 +0000138 case FieldDescriptor::TYPE_SFIXED32:
139 return JAVA_TYPE_INT;
140 case FieldDescriptor::TYPE_SFIXED64:
141 return JAVA_TYPE_LONG;
142 case FieldDescriptor::TYPE_SINT32:
143 return JAVA_TYPE_INT;
144 case FieldDescriptor::TYPE_SINT64:
145 return JAVA_TYPE_LONG;
146 default:
147 return JAVA_TYPE_UNKNOWN;
148 }
149}
150
151/**
Yangster-macba5b9e42018-01-10 21:31:59 -0800152 * Gather the enums info.
153 */
154void collate_enums(const EnumDescriptor &enumDescriptor, AtomField *atomField) {
155 for (int i = 0; i < enumDescriptor.value_count(); i++) {
156 atomField->enumValues[enumDescriptor.value(i)->number()] =
157 enumDescriptor.value(i)->name().c_str();
158 }
159}
160
161/**
Yangster-mac7604aea2017-12-11 22:55:49 -0800162 * Gather the info about an atom proto.
163 */
164int collate_atom(const Descriptor *atom, AtomDecl *atomDecl,
165 vector<java_type_t> *signature) {
166
167 int errorCount = 0;
Andrei Oneada01ea52019-01-30 15:28:36 +0000168
Yangster-mac7604aea2017-12-11 22:55:49 -0800169 // Build a sorted list of the fields. Descriptor has them in source file
170 // order.
171 map<int, const FieldDescriptor *> fields;
172 for (int j = 0; j < atom->field_count(); j++) {
173 const FieldDescriptor *field = atom->field(j);
174 fields[field->number()] = field;
175 }
176
177 // Check that the parameters start at 1 and go up sequentially.
178 int expectedNumber = 1;
179 for (map<int, const FieldDescriptor *>::const_iterator it = fields.begin();
180 it != fields.end(); it++) {
181 const int number = it->first;
182 const FieldDescriptor *field = it->second;
183 if (number != expectedNumber) {
184 print_error(field,
185 "Fields must be numbered consecutively starting at 1:"
186 " '%s' is %d but should be %d\n",
187 field->name().c_str(), number, expectedNumber);
188 errorCount++;
189 expectedNumber = number;
190 continue;
191 }
192 expectedNumber++;
193 }
194
195 // Check that only allowed types are present. Remove any invalid ones.
196 for (map<int, const FieldDescriptor *>::const_iterator it = fields.begin();
197 it != fields.end(); it++) {
198 const FieldDescriptor *field = it->second;
Yao Chenbbdd67d2018-10-24 12:15:56 -0700199 bool isBinaryField = field->options().GetExtension(os::statsd::log_mode) ==
200 os::statsd::LogMode::MODE_BYTES;
Yangster-mac7604aea2017-12-11 22:55:49 -0800201
202 java_type_t javaType = java_type(field);
203
204 if (javaType == JAVA_TYPE_UNKNOWN) {
205 print_error(field, "Unkown type for field: %s\n", field->name().c_str());
206 errorCount++;
207 continue;
Chenjie Yu159e4f82018-08-29 11:49:11 -0700208 } else if (javaType == JAVA_TYPE_OBJECT &&
209 atomDecl->code < PULL_ATOM_START_ID) {
Yao Chenbbdd67d2018-10-24 12:15:56 -0700210 // Allow attribution chain, but only at position 1.
211 print_error(field,
212 "Message type not allowed for field in pushed atoms: %s\n",
213 field->name().c_str());
214 errorCount++;
215 continue;
216 } else if (javaType == JAVA_TYPE_BYTE_ARRAY && !isBinaryField) {
217 print_error(field, "Raw bytes type not allowed for field: %s\n",
218 field->name().c_str());
219 errorCount++;
220 continue;
221 }
222
223 if (isBinaryField && javaType != JAVA_TYPE_BYTE_ARRAY) {
224 print_error(field, "Cannot mark field %s as bytes.\n",
225 field->name().c_str());
226 errorCount++;
227 continue;
Yangster-mac7604aea2017-12-11 22:55:49 -0800228 }
229 }
230
231 // Check that if there's an attribution chain, it's at position 1.
232 for (map<int, const FieldDescriptor *>::const_iterator it = fields.begin();
233 it != fields.end(); it++) {
234 int number = it->first;
235 if (number != 1) {
236 const FieldDescriptor *field = it->second;
237 java_type_t javaType = java_type(field);
238 if (javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
239 print_error(
240 field,
241 "AttributionChain fields must have field id 1, in message: '%s'\n",
242 atom->name().c_str());
243 errorCount++;
244 }
245 }
246 }
247
248 // Build the type signature and the atom data.
249 for (map<int, const FieldDescriptor *>::const_iterator it = fields.begin();
250 it != fields.end(); it++) {
251 const FieldDescriptor *field = it->second;
252 java_type_t javaType = java_type(field);
Yao Chenbbdd67d2018-10-24 12:15:56 -0700253 bool isBinaryField = field->options().GetExtension(os::statsd::log_mode) ==
254 os::statsd::LogMode::MODE_BYTES;
Yangster-mac7604aea2017-12-11 22:55:49 -0800255
256 AtomField atField(field->name(), javaType);
Chenjie Yu159e4f82018-08-29 11:49:11 -0700257 // Generate signature for pushed atoms
258 if (atomDecl->code < PULL_ATOM_START_ID) {
259 if (javaType == JAVA_TYPE_ENUM) {
260 // All enums are treated as ints when it comes to function signatures.
261 signature->push_back(JAVA_TYPE_INT);
262 collate_enums(*field->enum_type(), &atField);
Yao Chenbbdd67d2018-10-24 12:15:56 -0700263 } else if (javaType == JAVA_TYPE_OBJECT && isBinaryField) {
264 signature->push_back(JAVA_TYPE_BYTE_ARRAY);
Chenjie Yu159e4f82018-08-29 11:49:11 -0700265 } else {
Yao Chenbbdd67d2018-10-24 12:15:56 -0700266 signature->push_back(javaType);
Chenjie Yu159e4f82018-08-29 11:49:11 -0700267 }
268 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800269 if (javaType == JAVA_TYPE_ENUM) {
270 // All enums are treated as ints when it comes to function signatures.
Yangster-macba5b9e42018-01-10 21:31:59 -0800271 collate_enums(*field->enum_type(), &atField);
Yangster-mac7604aea2017-12-11 22:55:49 -0800272 }
273 atomDecl->fields.push_back(atField);
Yao Chen9c1debe2018-02-19 14:39:19 -0800274
Yangster-macb6b77c62018-10-12 19:33:24 -0700275 if (field->options().GetExtension(os::statsd::state_field_option).option() ==
Yao Chen9c1debe2018-02-19 14:39:19 -0800276 os::statsd::StateField::PRIMARY) {
277 if (javaType == JAVA_TYPE_UNKNOWN ||
278 javaType == JAVA_TYPE_ATTRIBUTION_CHAIN ||
279 javaType == JAVA_TYPE_OBJECT || javaType == JAVA_TYPE_BYTE_ARRAY) {
280 errorCount++;
281 }
282 atomDecl->primaryFields.push_back(it->first);
283 }
284
Yangster-macb6b77c62018-10-12 19:33:24 -0700285 if (field->options().GetExtension(os::statsd::state_field_option).option() ==
Yao Chen9c1debe2018-02-19 14:39:19 -0800286 os::statsd::StateField::EXCLUSIVE) {
287 if (javaType == JAVA_TYPE_UNKNOWN ||
288 javaType == JAVA_TYPE_ATTRIBUTION_CHAIN ||
289 javaType == JAVA_TYPE_OBJECT || javaType == JAVA_TYPE_BYTE_ARRAY) {
290 errorCount++;
291 }
292
293 if (atomDecl->exclusiveField == 0) {
294 atomDecl->exclusiveField = it->first;
295 } else {
296 errorCount++;
297 }
298 }
Yao Chenc40a19d2018-03-15 16:48:25 -0700299
300 if (field->options().GetExtension(os::statsd::is_uid) == true) {
301 if (javaType != JAVA_TYPE_INT) {
302 errorCount++;
303 }
304
305 if (atomDecl->uidField == 0) {
306 atomDecl->uidField = it->first;
307 } else {
308 errorCount++;
309 }
310 }
Yao Chenbbdd67d2018-10-24 12:15:56 -0700311 // Binary field validity is already checked above.
312 if (isBinaryField) {
313 atomDecl->binaryFields.push_back(it->first);
314 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800315 }
316
317 return errorCount;
318}
319
Yangster-macba5b9e42018-01-10 21:31:59 -0800320// This function flattens the fields of the AttributionNode proto in an Atom proto and generates
321// the corresponding atom decl and signature.
322bool get_non_chained_node(const Descriptor *atom, AtomDecl *atomDecl,
323 vector<java_type_t> *signature) {
324 // Build a sorted list of the fields. Descriptor has them in source file
325 // order.
326 map<int, const FieldDescriptor *> fields;
327 for (int j = 0; j < atom->field_count(); j++) {
328 const FieldDescriptor *field = atom->field(j);
329 fields[field->number()] = field;
330 }
331
332 AtomDecl attributionDecl;
333 vector<java_type_t> attributionSignature;
334 collate_atom(android::os::statsd::AttributionNode::descriptor(),
335 &attributionDecl, &attributionSignature);
336
337 // Build the type signature and the atom data.
338 bool has_attribution_node = false;
339 for (map<int, const FieldDescriptor *>::const_iterator it = fields.begin();
340 it != fields.end(); it++) {
341 const FieldDescriptor *field = it->second;
342 java_type_t javaType = java_type(field);
343 if (javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
344 atomDecl->fields.insert(
345 atomDecl->fields.end(),
346 attributionDecl.fields.begin(), attributionDecl.fields.end());
347 signature->insert(
348 signature->end(),
349 attributionSignature.begin(), attributionSignature.end());
350 has_attribution_node = true;
351
352 } else {
353 AtomField atField(field->name(), javaType);
354 if (javaType == JAVA_TYPE_ENUM) {
355 // All enums are treated as ints when it comes to function signatures.
356 signature->push_back(JAVA_TYPE_INT);
357 collate_enums(*field->enum_type(), &atField);
358 } else {
359 signature->push_back(javaType);
360 }
361 atomDecl->fields.push_back(atField);
362 }
363 }
364 return has_attribution_node;
365}
366
Yangster-mac7604aea2017-12-11 22:55:49 -0800367/**
Yao Chend54f9dd2017-10-17 17:37:48 +0000368 * Gather the info about the atoms.
369 */
Yangster-mac7604aea2017-12-11 22:55:49 -0800370int collate_atoms(const Descriptor *descriptor, Atoms *atoms) {
371 int errorCount = 0;
372 const bool dbg = false;
Yao Chend54f9dd2017-10-17 17:37:48 +0000373
Yangster-mac7604aea2017-12-11 22:55:49 -0800374 for (int i = 0; i < descriptor->field_count(); i++) {
375 const FieldDescriptor *atomField = descriptor->field(i);
Yao Chend54f9dd2017-10-17 17:37:48 +0000376
377 if (dbg) {
Yangster-mac7604aea2017-12-11 22:55:49 -0800378 printf(" %s (%d)\n", atomField->name().c_str(), atomField->number());
Yao Chend54f9dd2017-10-17 17:37:48 +0000379 }
380
Yangster-mac7604aea2017-12-11 22:55:49 -0800381 // StatsEvent only has one oneof, which contains only messages. Don't allow
382 // other types.
383 if (atomField->type() != FieldDescriptor::TYPE_MESSAGE) {
384 print_error(atomField,
385 "Bad type for atom. StatsEvent can only have message type "
386 "fields: %s\n",
387 atomField->name().c_str());
388 errorCount++;
389 continue;
390 }
391
392 const Descriptor *atom = atomField->message_type();
393 AtomDecl atomDecl(atomField->number(), atomField->name(), atom->name());
Andrei Oneada01ea52019-01-30 15:28:36 +0000394
395 if (atomField->options().GetExtension(os::statsd::allow_from_any_uid) == true) {
Tej Singh810eeb32019-03-07 19:08:52 -0800396 atomDecl.whitelisted = true;
397 }
398
399 if (atomField->options().HasExtension(os::statsd::log_from_module)) {
400 atomDecl.hasModule = true;
401 atomDecl.moduleName = atomField->options().GetExtension(os::statsd::log_from_module);
Andrei Oneada01ea52019-01-30 15:28:36 +0000402 }
403
Yangster-mac7604aea2017-12-11 22:55:49 -0800404 vector<java_type_t> signature;
405 errorCount += collate_atom(atom, &atomDecl, &signature);
Yao Chen9c1debe2018-02-19 14:39:19 -0800406 if (atomDecl.primaryFields.size() != 0 && atomDecl.exclusiveField == 0) {
407 errorCount++;
408 }
Tej Singh810eeb32019-03-07 19:08:52 -0800409
410 // Add the signature if does not already exist.
411 auto signature_to_modules_it = atoms->signatures_to_modules.find(signature);
412 if (signature_to_modules_it == atoms->signatures_to_modules.end()) {
413 set<string> modules;
414 if (atomDecl.hasModule) {
415 modules.insert(atomDecl.moduleName);
416 }
417 atoms->signatures_to_modules[signature] = modules;
418 } else {
419 if (atomDecl.hasModule) {
420 signature_to_modules_it->second.insert(atomDecl.moduleName);
421 }
422 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800423 atoms->decls.insert(atomDecl);
Yangster-macba5b9e42018-01-10 21:31:59 -0800424
425 AtomDecl nonChainedAtomDecl(atomField->number(), atomField->name(), atom->name());
426 vector<java_type_t> nonChainedSignature;
427 if (get_non_chained_node(atom, &nonChainedAtomDecl, &nonChainedSignature)) {
Tej Singh810eeb32019-03-07 19:08:52 -0800428 auto it = atoms->non_chained_signatures_to_modules.find(signature);
429 if (it == atoms->non_chained_signatures_to_modules.end()) {
430 set<string> modules_non_chained;
431 if (atomDecl.hasModule) {
432 modules_non_chained.insert(atomDecl.moduleName);
433 }
434 atoms->non_chained_signatures_to_modules[nonChainedSignature] = modules_non_chained;
435 } else {
436 if (atomDecl.hasModule) {
437 it->second.insert(atomDecl.moduleName);
438 }
439 }
Yangster-macba5b9e42018-01-10 21:31:59 -0800440 atoms->non_chained_decls.insert(nonChainedAtomDecl);
441 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800442 }
443
444 if (dbg) {
445 printf("signatures = [\n");
Tej Singh810eeb32019-03-07 19:08:52 -0800446 for (map<vector<java_type_t>, set<string>>::const_iterator it =
447 atoms->signatures_to_modules.begin();
448 it != atoms->signatures_to_modules.end(); it++) {
Yangster-mac7604aea2017-12-11 22:55:49 -0800449 printf(" ");
Tej Singh810eeb32019-03-07 19:08:52 -0800450 for (vector<java_type_t>::const_iterator jt = it->first.begin();
451 jt != it->first.end(); jt++) {
Yangster-mac7604aea2017-12-11 22:55:49 -0800452 printf(" %d", (int)*jt);
453 }
454 printf("\n");
455 }
456 printf("]\n");
457 }
458
459 return errorCount;
Yao Chend54f9dd2017-10-17 17:37:48 +0000460}
461
462} // namespace stats_log_api_gen
463} // namespace android