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 "include/stats_event.h"
18 #include <stdlib.h>
19 #include <string.h>
20 #include <time.h>
21 #include "stats_buffer_writer.h"
22 
23 #define LOGGER_ENTRY_MAX_PAYLOAD 4068
24 // Max payload size is 4 bytes less as 4 bytes are reserved for stats_eventTag.
25 // See android_util_Stats_Log.cpp
26 #define MAX_EVENT_PAYLOAD (LOGGER_ENTRY_MAX_PAYLOAD - 4)
27 
28 /* POSITIONS */
29 #define POS_NUM_ELEMENTS 1
30 #define POS_TIMESTAMP (POS_NUM_ELEMENTS + sizeof(uint8_t))
31 #define POS_ATOM_ID (POS_TIMESTAMP + sizeof(uint8_t) + sizeof(uint64_t))
32 #define POS_FIRST_FIELD (POS_ATOM_ID + sizeof(uint8_t) + sizeof(uint32_t))
33 
34 /* LIMITS */
35 #define MAX_ANNOTATION_COUNT 15
36 #define MAX_BYTE_VALUE 127  // parsing side requires that lengths fit in 7 bits
37 
38 // The stats_event struct holds the serialized encoding of an event
39 // within a buf. Also includes other required fields.
40 struct stats_event {
41     uint8_t* buf;
42     size_t lastFieldPos;  // location of last field within the buf
43     size_t size;          // number of valid bytes within buffer
44     uint32_t numElements;
45     uint32_t atomId;
46     uint32_t errors;
47     bool truncate;
48     bool built;
49 };
50 
get_elapsed_realtime_ns()51 static int64_t get_elapsed_realtime_ns() {
52     struct timespec t;
53     t.tv_sec = t.tv_nsec = 0;
54     clock_gettime(CLOCK_BOOTTIME, &t);
55     return (int64_t)t.tv_sec * 1000000000LL + t.tv_nsec;
56 }
57 
stats_event_obtain()58 struct stats_event* stats_event_obtain() {
59     struct stats_event* event = malloc(sizeof(struct stats_event));
60     event->buf = (uint8_t*)calloc(MAX_EVENT_PAYLOAD, 1);
61     event->buf[0] = OBJECT_TYPE;
62     event->atomId = 0;
63     event->errors = 0;
64     event->truncate = true;  // truncate for both pulled and pushed atoms
65     event->built = false;
66 
67     // place the timestamp
68     uint64_t timestampNs = get_elapsed_realtime_ns();
69     event->buf[POS_TIMESTAMP] = INT64_TYPE;
70     memcpy(&event->buf[POS_TIMESTAMP + sizeof(uint8_t)], &timestampNs, sizeof(timestampNs));
71 
72     event->numElements = 1;
73     event->lastFieldPos = 0;  // 0 since we haven't written a field yet
74     event->size = POS_FIRST_FIELD;
75 
76     return event;
77 }
78 
stats_event_release(struct stats_event * event)79 void stats_event_release(struct stats_event* event) {
80     free(event->buf);
81     free(event);
82 }
83 
stats_event_set_atom_id(struct stats_event * event,uint32_t atomId)84 void stats_event_set_atom_id(struct stats_event* event, uint32_t atomId) {
85     event->atomId = atomId;
86     event->buf[POS_ATOM_ID] = INT32_TYPE;
87     memcpy(&event->buf[POS_ATOM_ID + sizeof(uint8_t)], &atomId, sizeof(atomId));
88     event->numElements++;
89 }
90 
91 // Side-effect: modifies event->errors if the buffer would overflow
overflows(struct stats_event * event,size_t size)92 static bool overflows(struct stats_event* event, size_t size) {
93     if (event->size + size > MAX_EVENT_PAYLOAD) {
94         event->errors |= ERROR_OVERFLOW;
95         return true;
96     }
97     return false;
98 }
99 
100 // Side-effect: all append functions increment event->size if there is
101 // sufficient space within the buffer to place the value
append_byte(struct stats_event * event,uint8_t value)102 static void append_byte(struct stats_event* event, uint8_t value) {
103     if (!overflows(event, sizeof(value))) {
104         event->buf[event->size] = value;
105         event->size += sizeof(value);
106     }
107 }
108 
append_bool(struct stats_event * event,bool value)109 static void append_bool(struct stats_event* event, bool value) {
110     append_byte(event, (uint8_t)value);
111 }
112 
append_int32(struct stats_event * event,int32_t value)113 static void append_int32(struct stats_event* event, int32_t value) {
114     if (!overflows(event, sizeof(value))) {
115         memcpy(&event->buf[event->size], &value, sizeof(value));
116         event->size += sizeof(value);
117     }
118 }
119 
append_int64(struct stats_event * event,int64_t value)120 static void append_int64(struct stats_event* event, int64_t value) {
121     if (!overflows(event, sizeof(value))) {
122         memcpy(&event->buf[event->size], &value, sizeof(value));
123         event->size += sizeof(value);
124     }
125 }
126 
append_float(struct stats_event * event,float value)127 static void append_float(struct stats_event* event, float value) {
128     if (!overflows(event, sizeof(value))) {
129         memcpy(&event->buf[event->size], &value, sizeof(value));
130         event->size += sizeof(float);
131     }
132 }
133 
append_byte_array(struct stats_event * event,const uint8_t * buf,size_t size)134 static void append_byte_array(struct stats_event* event, const uint8_t* buf, size_t size) {
135     if (!overflows(event, size)) {
136         memcpy(&event->buf[event->size], buf, size);
137         event->size += size;
138     }
139 }
140 
141 // Side-effect: modifies event->errors if buf is not properly null-terminated
append_string(struct stats_event * event,const char * buf)142 static void append_string(struct stats_event* event, const char* buf) {
143     size_t size = strnlen(buf, MAX_EVENT_PAYLOAD);
144     if (size == MAX_EVENT_PAYLOAD) {
145         event->errors |= ERROR_STRING_NOT_NULL_TERMINATED;
146         return;
147     }
148 
149     append_int32(event, size);
150     append_byte_array(event, (uint8_t*)buf, size);
151 }
152 
start_field(struct stats_event * event,uint8_t typeId)153 static void start_field(struct stats_event* event, uint8_t typeId) {
154     event->lastFieldPos = event->size;
155     append_byte(event, typeId);
156     event->numElements++;
157 }
158 
stats_event_write_int32(struct stats_event * event,int32_t value)159 void stats_event_write_int32(struct stats_event* event, int32_t value) {
160     if (event->errors) return;
161 
162     start_field(event, INT32_TYPE);
163     append_int32(event, value);
164 }
165 
stats_event_write_int64(struct stats_event * event,int64_t value)166 void stats_event_write_int64(struct stats_event* event, int64_t value) {
167     if (event->errors) return;
168 
169     start_field(event, INT64_TYPE);
170     append_int64(event, value);
171 }
172 
stats_event_write_float(struct stats_event * event,float value)173 void stats_event_write_float(struct stats_event* event, float value) {
174     if (event->errors) return;
175 
176     start_field(event, FLOAT_TYPE);
177     append_float(event, value);
178 }
179 
stats_event_write_bool(struct stats_event * event,bool value)180 void stats_event_write_bool(struct stats_event* event, bool value) {
181     if (event->errors) return;
182 
183     start_field(event, BOOL_TYPE);
184     append_bool(event, value);
185 }
186 
stats_event_write_byte_array(struct stats_event * event,const uint8_t * buf,size_t numBytes)187 void stats_event_write_byte_array(struct stats_event* event, const uint8_t* buf, size_t numBytes) {
188     if (event->errors) return;
189 
190     start_field(event, BYTE_ARRAY_TYPE);
191     append_int32(event, numBytes);
192     append_byte_array(event, buf, numBytes);
193 }
194 
195 // Value is assumed to be encoded using UTF8
stats_event_write_string8(struct stats_event * event,const char * value)196 void stats_event_write_string8(struct stats_event* event, const char* value) {
197     if (event->errors) return;
198 
199     start_field(event, STRING_TYPE);
200     append_string(event, value);
201 }
202 
203 // Tags are assumed to be encoded using UTF8
stats_event_write_attribution_chain(struct stats_event * event,const uint32_t * uids,const char * const * tags,uint8_t numNodes)204 void stats_event_write_attribution_chain(struct stats_event* event, const uint32_t* uids,
205                                          const char* const* tags, uint8_t numNodes) {
206     if (numNodes > MAX_BYTE_VALUE) event->errors |= ERROR_ATTRIBUTION_CHAIN_TOO_LONG;
207     if (event->errors) return;
208 
209     start_field(event, ATTRIBUTION_CHAIN_TYPE);
210     append_byte(event, numNodes);
211 
212     for (uint8_t i = 0; i < numNodes; i++) {
213         append_int32(event, uids[i]);
214         append_string(event, tags[i]);
215     }
216 }
217 
stats_event_write_key_value_pairs(struct stats_event * event,struct key_value_pair * pairs,uint8_t numPairs)218 void stats_event_write_key_value_pairs(struct stats_event* event, struct key_value_pair* pairs,
219                                        uint8_t numPairs) {
220     if (numPairs > MAX_BYTE_VALUE) event->errors |= ERROR_TOO_MANY_KEY_VALUE_PAIRS;
221     if (event->errors) return;
222 
223     start_field(event, KEY_VALUE_PAIRS_TYPE);
224     append_byte(event, numPairs);
225 
226     for (uint8_t i = 0; i < numPairs; i++) {
227         append_int32(event, pairs[i].key);
228         append_byte(event, pairs[i].valueType);
229         switch (pairs[i].valueType) {
230             case INT32_TYPE:
231                 append_int32(event, pairs[i].int32Value);
232                 break;
233             case INT64_TYPE:
234                 append_int64(event, pairs[i].int64Value);
235                 break;
236             case FLOAT_TYPE:
237                 append_float(event, pairs[i].floatValue);
238                 break;
239             case STRING_TYPE:
240                 append_string(event, pairs[i].stringValue);
241                 break;
242             default:
243                 event->errors |= ERROR_INVALID_VALUE_TYPE;
244                 return;
245         }
246     }
247 }
248 
249 // Side-effect: modifies event->errors if field has too many annotations
increment_annotation_count(struct stats_event * event)250 static void increment_annotation_count(struct stats_event* event) {
251     uint8_t fieldType = event->buf[event->lastFieldPos] & 0x0F;
252     uint32_t oldAnnotationCount = (event->buf[event->lastFieldPos] & 0xF0) >> 4;
253     uint32_t newAnnotationCount = oldAnnotationCount + 1;
254 
255     if (newAnnotationCount > MAX_ANNOTATION_COUNT) {
256         event->errors |= ERROR_TOO_MANY_ANNOTATIONS;
257         return;
258     }
259 
260     event->buf[event->lastFieldPos] = (((uint8_t)newAnnotationCount << 4) & 0xF0) | fieldType;
261 }
262 
stats_event_add_bool_annotation(struct stats_event * event,uint8_t annotationId,bool value)263 void stats_event_add_bool_annotation(struct stats_event* event, uint8_t annotationId, bool value) {
264     if (event->lastFieldPos == 0) event->errors |= ERROR_ANNOTATION_DOES_NOT_FOLLOW_FIELD;
265     if (annotationId > MAX_BYTE_VALUE) event->errors |= ERROR_ANNOTATION_ID_TOO_LARGE;
266     if (event->errors) return;
267 
268     append_byte(event, annotationId);
269     append_byte(event, BOOL_TYPE);
270     append_bool(event, value);
271     increment_annotation_count(event);
272 }
273 
stats_event_add_int32_annotation(struct stats_event * event,uint8_t annotationId,int32_t value)274 void stats_event_add_int32_annotation(struct stats_event* event, uint8_t annotationId,
275                                       int32_t value) {
276     if (event->lastFieldPos == 0) event->errors |= ERROR_ANNOTATION_DOES_NOT_FOLLOW_FIELD;
277     if (annotationId > MAX_BYTE_VALUE) event->errors |= ERROR_ANNOTATION_ID_TOO_LARGE;
278     if (event->errors) return;
279 
280     append_byte(event, annotationId);
281     append_byte(event, INT32_TYPE);
282     append_int32(event, value);
283     increment_annotation_count(event);
284 }
285 
stats_event_get_atom_id(struct stats_event * event)286 uint32_t stats_event_get_atom_id(struct stats_event* event) {
287     return event->atomId;
288 }
289 
stats_event_get_buffer(struct stats_event * event,size_t * size)290 uint8_t* stats_event_get_buffer(struct stats_event* event, size_t* size) {
291     if (size) *size = event->size;
292     return event->buf;
293 }
294 
stats_event_get_errors(struct stats_event * event)295 uint32_t stats_event_get_errors(struct stats_event* event) {
296     return event->errors;
297 }
298 
stats_event_truncate_buffer(struct stats_event * event,bool truncate)299 void stats_event_truncate_buffer(struct stats_event* event, bool truncate) {
300     event->truncate = truncate;
301 }
302 
stats_event_build(struct stats_event * event)303 void stats_event_build(struct stats_event* event) {
304     if (event->built) return;
305 
306     if (event->atomId == 0) event->errors |= ERROR_NO_ATOM_ID;
307 
308     if (event->numElements > MAX_BYTE_VALUE) {
309         event->errors |= ERROR_TOO_MANY_FIELDS;
310     } else {
311         event->buf[POS_NUM_ELEMENTS] = event->numElements;
312     }
313 
314     // If there are errors, rewrite buffer.
315     if (event->errors) {
316         event->buf[POS_NUM_ELEMENTS] = 3;
317         event->buf[POS_FIRST_FIELD] = ERROR_TYPE;
318         memcpy(&event->buf[POS_FIRST_FIELD + sizeof(uint8_t)], &event->errors,
319                sizeof(event->errors));
320         event->size = POS_FIRST_FIELD + sizeof(uint8_t) + sizeof(uint32_t);
321     }
322 
323     // Truncate the buffer to the appropriate length in order to limit our
324     // memory usage.
325     if (event->truncate) event->buf = (uint8_t*)realloc(event->buf, event->size);
326 
327     event->built = true;
328 }
329 
stats_event_write(struct stats_event * event)330 int stats_event_write(struct stats_event* event) {
331     stats_event_build(event);
332     return write_buffer_to_statsd(&event->buf, event->size, event->atomId);
333 }
334 
335 struct stats_event_api_table table = {
336         stats_event_obtain,
337         stats_event_build,
338         stats_event_write,
339         stats_event_release,
340         stats_event_set_atom_id,
341         stats_event_write_int32,
342         stats_event_write_int64,
343         stats_event_write_float,
344         stats_event_write_bool,
345         stats_event_write_byte_array,
346         stats_event_write_string8,
347         stats_event_write_attribution_chain,
348         stats_event_write_key_value_pairs,
349         stats_event_add_bool_annotation,
350         stats_event_add_int32_annotation,
351         stats_event_get_atom_id,
352         stats_event_get_buffer,
353         stats_event_get_errors,
354 };
355