blob: 373adca994a1634c026772c966acb6f116968eda [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 }
Tej Singh2910d5a2019-03-23 17:26:32 -0700229
230 // Doubles are not supported yet.
231 if (javaType == JAVA_TYPE_DOUBLE) {
232 print_error(field, "Doubles are not supported in atoms. Please change field %s to float\n",
233 field->name().c_str());
234 errorCount++;
235 continue;
236 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800237 }
238
239 // Check that if there's an attribution chain, it's at position 1.
240 for (map<int, const FieldDescriptor *>::const_iterator it = fields.begin();
241 it != fields.end(); it++) {
242 int number = it->first;
243 if (number != 1) {
244 const FieldDescriptor *field = it->second;
245 java_type_t javaType = java_type(field);
246 if (javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
247 print_error(
248 field,
249 "AttributionChain fields must have field id 1, in message: '%s'\n",
250 atom->name().c_str());
251 errorCount++;
252 }
253 }
254 }
255
256 // Build the type signature and the atom data.
257 for (map<int, const FieldDescriptor *>::const_iterator it = fields.begin();
258 it != fields.end(); it++) {
259 const FieldDescriptor *field = it->second;
260 java_type_t javaType = java_type(field);
Yao Chenbbdd67d2018-10-24 12:15:56 -0700261 bool isBinaryField = field->options().GetExtension(os::statsd::log_mode) ==
262 os::statsd::LogMode::MODE_BYTES;
Yangster-mac7604aea2017-12-11 22:55:49 -0800263
264 AtomField atField(field->name(), javaType);
Chenjie Yu159e4f82018-08-29 11:49:11 -0700265 // Generate signature for pushed atoms
266 if (atomDecl->code < PULL_ATOM_START_ID) {
267 if (javaType == JAVA_TYPE_ENUM) {
268 // All enums are treated as ints when it comes to function signatures.
269 signature->push_back(JAVA_TYPE_INT);
270 collate_enums(*field->enum_type(), &atField);
Yao Chenbbdd67d2018-10-24 12:15:56 -0700271 } else if (javaType == JAVA_TYPE_OBJECT && isBinaryField) {
272 signature->push_back(JAVA_TYPE_BYTE_ARRAY);
Chenjie Yu159e4f82018-08-29 11:49:11 -0700273 } else {
Yao Chenbbdd67d2018-10-24 12:15:56 -0700274 signature->push_back(javaType);
Chenjie Yu159e4f82018-08-29 11:49:11 -0700275 }
276 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800277 if (javaType == JAVA_TYPE_ENUM) {
278 // All enums are treated as ints when it comes to function signatures.
Yangster-macba5b9e42018-01-10 21:31:59 -0800279 collate_enums(*field->enum_type(), &atField);
Yangster-mac7604aea2017-12-11 22:55:49 -0800280 }
281 atomDecl->fields.push_back(atField);
Yao Chen9c1debe2018-02-19 14:39:19 -0800282
Yangster-macb6b77c62018-10-12 19:33:24 -0700283 if (field->options().GetExtension(os::statsd::state_field_option).option() ==
Yao Chen9c1debe2018-02-19 14:39:19 -0800284 os::statsd::StateField::PRIMARY) {
285 if (javaType == JAVA_TYPE_UNKNOWN ||
286 javaType == JAVA_TYPE_ATTRIBUTION_CHAIN ||
287 javaType == JAVA_TYPE_OBJECT || javaType == JAVA_TYPE_BYTE_ARRAY) {
288 errorCount++;
289 }
290 atomDecl->primaryFields.push_back(it->first);
291 }
292
Yangster-macb6b77c62018-10-12 19:33:24 -0700293 if (field->options().GetExtension(os::statsd::state_field_option).option() ==
Yao Chen9c1debe2018-02-19 14:39:19 -0800294 os::statsd::StateField::EXCLUSIVE) {
295 if (javaType == JAVA_TYPE_UNKNOWN ||
296 javaType == JAVA_TYPE_ATTRIBUTION_CHAIN ||
297 javaType == JAVA_TYPE_OBJECT || javaType == JAVA_TYPE_BYTE_ARRAY) {
298 errorCount++;
299 }
300
301 if (atomDecl->exclusiveField == 0) {
302 atomDecl->exclusiveField = it->first;
303 } else {
304 errorCount++;
305 }
306 }
Yao Chenc40a19d2018-03-15 16:48:25 -0700307
308 if (field->options().GetExtension(os::statsd::is_uid) == true) {
309 if (javaType != JAVA_TYPE_INT) {
310 errorCount++;
311 }
312
313 if (atomDecl->uidField == 0) {
314 atomDecl->uidField = it->first;
315 } else {
316 errorCount++;
317 }
318 }
Yao Chenbbdd67d2018-10-24 12:15:56 -0700319 // Binary field validity is already checked above.
320 if (isBinaryField) {
321 atomDecl->binaryFields.push_back(it->first);
322 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800323 }
324
325 return errorCount;
326}
327
Yangster-macba5b9e42018-01-10 21:31:59 -0800328// This function flattens the fields of the AttributionNode proto in an Atom proto and generates
329// the corresponding atom decl and signature.
330bool get_non_chained_node(const Descriptor *atom, AtomDecl *atomDecl,
331 vector<java_type_t> *signature) {
332 // Build a sorted list of the fields. Descriptor has them in source file
333 // order.
334 map<int, const FieldDescriptor *> fields;
335 for (int j = 0; j < atom->field_count(); j++) {
336 const FieldDescriptor *field = atom->field(j);
337 fields[field->number()] = field;
338 }
339
340 AtomDecl attributionDecl;
341 vector<java_type_t> attributionSignature;
342 collate_atom(android::os::statsd::AttributionNode::descriptor(),
343 &attributionDecl, &attributionSignature);
344
345 // Build the type signature and the atom data.
346 bool has_attribution_node = false;
347 for (map<int, const FieldDescriptor *>::const_iterator it = fields.begin();
348 it != fields.end(); it++) {
349 const FieldDescriptor *field = it->second;
350 java_type_t javaType = java_type(field);
351 if (javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
352 atomDecl->fields.insert(
353 atomDecl->fields.end(),
354 attributionDecl.fields.begin(), attributionDecl.fields.end());
355 signature->insert(
356 signature->end(),
357 attributionSignature.begin(), attributionSignature.end());
358 has_attribution_node = true;
359
360 } else {
361 AtomField atField(field->name(), javaType);
362 if (javaType == JAVA_TYPE_ENUM) {
363 // All enums are treated as ints when it comes to function signatures.
364 signature->push_back(JAVA_TYPE_INT);
365 collate_enums(*field->enum_type(), &atField);
366 } else {
367 signature->push_back(javaType);
368 }
369 atomDecl->fields.push_back(atField);
370 }
371 }
372 return has_attribution_node;
373}
374
Yangster-mac7604aea2017-12-11 22:55:49 -0800375/**
Yao Chend54f9dd2017-10-17 17:37:48 +0000376 * Gather the info about the atoms.
377 */
Yangster-mac7604aea2017-12-11 22:55:49 -0800378int collate_atoms(const Descriptor *descriptor, Atoms *atoms) {
379 int errorCount = 0;
380 const bool dbg = false;
Yao Chend54f9dd2017-10-17 17:37:48 +0000381
Yangster-mac7604aea2017-12-11 22:55:49 -0800382 for (int i = 0; i < descriptor->field_count(); i++) {
383 const FieldDescriptor *atomField = descriptor->field(i);
Yao Chend54f9dd2017-10-17 17:37:48 +0000384
385 if (dbg) {
Yangster-mac7604aea2017-12-11 22:55:49 -0800386 printf(" %s (%d)\n", atomField->name().c_str(), atomField->number());
Yao Chend54f9dd2017-10-17 17:37:48 +0000387 }
388
Yangster-mac7604aea2017-12-11 22:55:49 -0800389 // StatsEvent only has one oneof, which contains only messages. Don't allow
390 // other types.
391 if (atomField->type() != FieldDescriptor::TYPE_MESSAGE) {
392 print_error(atomField,
393 "Bad type for atom. StatsEvent can only have message type "
394 "fields: %s\n",
395 atomField->name().c_str());
396 errorCount++;
397 continue;
398 }
399
400 const Descriptor *atom = atomField->message_type();
401 AtomDecl atomDecl(atomField->number(), atomField->name(), atom->name());
Andrei Oneada01ea52019-01-30 15:28:36 +0000402
403 if (atomField->options().GetExtension(os::statsd::allow_from_any_uid) == true) {
Tej Singh810eeb32019-03-07 19:08:52 -0800404 atomDecl.whitelisted = true;
405 }
406
407 if (atomField->options().HasExtension(os::statsd::log_from_module)) {
408 atomDecl.hasModule = true;
409 atomDecl.moduleName = atomField->options().GetExtension(os::statsd::log_from_module);
Andrei Oneada01ea52019-01-30 15:28:36 +0000410 }
411
Yangster-mac7604aea2017-12-11 22:55:49 -0800412 vector<java_type_t> signature;
413 errorCount += collate_atom(atom, &atomDecl, &signature);
Yao Chen9c1debe2018-02-19 14:39:19 -0800414 if (atomDecl.primaryFields.size() != 0 && atomDecl.exclusiveField == 0) {
415 errorCount++;
416 }
Tej Singh810eeb32019-03-07 19:08:52 -0800417
418 // Add the signature if does not already exist.
419 auto signature_to_modules_it = atoms->signatures_to_modules.find(signature);
420 if (signature_to_modules_it == atoms->signatures_to_modules.end()) {
421 set<string> modules;
422 if (atomDecl.hasModule) {
423 modules.insert(atomDecl.moduleName);
424 }
425 atoms->signatures_to_modules[signature] = modules;
426 } else {
427 if (atomDecl.hasModule) {
428 signature_to_modules_it->second.insert(atomDecl.moduleName);
429 }
430 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800431 atoms->decls.insert(atomDecl);
Yangster-macba5b9e42018-01-10 21:31:59 -0800432
433 AtomDecl nonChainedAtomDecl(atomField->number(), atomField->name(), atom->name());
434 vector<java_type_t> nonChainedSignature;
435 if (get_non_chained_node(atom, &nonChainedAtomDecl, &nonChainedSignature)) {
Tej Singh810eeb32019-03-07 19:08:52 -0800436 auto it = atoms->non_chained_signatures_to_modules.find(signature);
437 if (it == atoms->non_chained_signatures_to_modules.end()) {
438 set<string> modules_non_chained;
439 if (atomDecl.hasModule) {
440 modules_non_chained.insert(atomDecl.moduleName);
441 }
442 atoms->non_chained_signatures_to_modules[nonChainedSignature] = modules_non_chained;
443 } else {
444 if (atomDecl.hasModule) {
445 it->second.insert(atomDecl.moduleName);
446 }
447 }
Yangster-macba5b9e42018-01-10 21:31:59 -0800448 atoms->non_chained_decls.insert(nonChainedAtomDecl);
449 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800450 }
451
452 if (dbg) {
453 printf("signatures = [\n");
Tej Singh810eeb32019-03-07 19:08:52 -0800454 for (map<vector<java_type_t>, set<string>>::const_iterator it =
455 atoms->signatures_to_modules.begin();
456 it != atoms->signatures_to_modules.end(); it++) {
Yangster-mac7604aea2017-12-11 22:55:49 -0800457 printf(" ");
Tej Singh810eeb32019-03-07 19:08:52 -0800458 for (vector<java_type_t>::const_iterator jt = it->first.begin();
459 jt != it->first.end(); jt++) {
Yangster-mac7604aea2017-12-11 22:55:49 -0800460 printf(" %d", (int)*jt);
461 }
462 printf("\n");
463 }
464 printf("]\n");
465 }
466
467 return errorCount;
Yao Chend54f9dd2017-10-17 17:37:48 +0000468}
469
470} // namespace stats_log_api_gen
471} // namespace android