Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #define DEBUG false |
| 18 | #include "Log.h" |
| 19 | #include "FieldValue.h" |
| 20 | #include "HashableDimensionKey.h" |
Muhammad Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame^] | 21 | #include "atoms_info.h" |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 22 | #include "math.h" |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | namespace os { |
| 26 | namespace statsd { |
| 27 | |
Yao Chen | 4c959cb | 2018-02-13 13:27:48 -0800 | [diff] [blame] | 28 | int32_t getEncodedField(int32_t pos[], int32_t depth, bool includeDepth) { |
| 29 | int32_t field = 0; |
| 30 | for (int32_t i = 0; i <= depth; i++) { |
| 31 | int32_t shiftBits = 8 * (kMaxLogDepth - i); |
| 32 | field |= (pos[i] << shiftBits); |
| 33 | } |
| 34 | |
| 35 | if (includeDepth) { |
| 36 | field |= (depth << 24); |
| 37 | } |
| 38 | return field; |
| 39 | } |
| 40 | |
| 41 | int32_t encodeMatcherMask(int32_t mask[], int32_t depth) { |
| 42 | return getEncodedField(mask, depth, false) | 0xff000000; |
| 43 | } |
| 44 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 45 | bool Field::matches(const Matcher& matcher) const { |
| 46 | if (mTag != matcher.mMatcher.getTag()) { |
| 47 | return false; |
| 48 | } |
| 49 | if ((mField & matcher.mMask) == matcher.mMatcher.getField()) { |
| 50 | return true; |
| 51 | } |
| 52 | |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 53 | if (matcher.hasAllPositionMatcher() && |
| 54 | (mField & (matcher.mMask & kClearAllPositionMatcherMask)) == matcher.mMatcher.getField()) { |
| 55 | return true; |
| 56 | } |
| 57 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 58 | return false; |
Yao Chen | 4c959cb | 2018-02-13 13:27:48 -0800 | [diff] [blame] | 59 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 60 | |
| 61 | void translateFieldMatcher(int tag, const FieldMatcher& matcher, int depth, int* pos, int* mask, |
| 62 | std::vector<Matcher>* output) { |
| 63 | if (depth > kMaxLogDepth) { |
| 64 | ALOGE("depth > 2"); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | pos[depth] = matcher.field(); |
| 69 | mask[depth] = 0x7f; |
| 70 | |
| 71 | if (matcher.has_position()) { |
| 72 | depth++; |
| 73 | if (depth > 2) { |
| 74 | return; |
| 75 | } |
| 76 | switch (matcher.position()) { |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 77 | case Position::ALL: |
| 78 | pos[depth] = 0x00; |
| 79 | mask[depth] = 0x7f; |
| 80 | break; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 81 | case Position::ANY: |
| 82 | pos[depth] = 0; |
| 83 | mask[depth] = 0; |
| 84 | break; |
| 85 | case Position::FIRST: |
| 86 | pos[depth] = 1; |
| 87 | mask[depth] = 0x7f; |
| 88 | break; |
| 89 | case Position::LAST: |
| 90 | pos[depth] = 0x80; |
| 91 | mask[depth] = 0x80; |
| 92 | break; |
| 93 | case Position::POSITION_UNKNOWN: |
| 94 | pos[depth] = 0; |
| 95 | mask[depth] = 0; |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if (matcher.child_size() == 0) { |
| 101 | output->push_back(Matcher(Field(tag, pos, depth), encodeMatcherMask(mask, depth))); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 102 | } else { |
| 103 | for (const auto& child : matcher.child()) { |
| 104 | translateFieldMatcher(tag, child, depth + 1, pos, mask, output); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void translateFieldMatcher(const FieldMatcher& matcher, std::vector<Matcher>* output) { |
| 110 | int pos[] = {1, 1, 1}; |
| 111 | int mask[] = {0x7f, 0x7f, 0x7f}; |
| 112 | int tag = matcher.field(); |
| 113 | for (const auto& child : matcher.child()) { |
| 114 | translateFieldMatcher(tag, child, 0, pos, mask, output); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | bool isAttributionUidField(const FieldValue& value) { |
Rafal Slawik | 390692b | 2019-09-18 12:40:17 +0100 | [diff] [blame] | 119 | return isAttributionUidField(value.mField, value.mValue); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Yao Chen | 4ce0729 | 2019-02-13 13:06:36 -0800 | [diff] [blame] | 122 | int32_t getUidIfExists(const FieldValue& value) { |
Yao Chen | 4ce0729 | 2019-02-13 13:06:36 -0800 | [diff] [blame] | 123 | // the field is uid field if the field is the uid field in attribution node or marked as |
| 124 | // is_uid in atoms.proto |
Rafal Slawik | 390692b | 2019-09-18 12:40:17 +0100 | [diff] [blame] | 125 | bool isUid = isAttributionUidField(value) || isUidField(value.mField, value.mValue); |
Yao Chen | 4ce0729 | 2019-02-13 13:06:36 -0800 | [diff] [blame] | 126 | return isUid ? value.mValue.int_value : -1; |
| 127 | } |
| 128 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 129 | bool isAttributionUidField(const Field& field, const Value& value) { |
| 130 | int f = field.getField() & 0xff007f; |
| 131 | if (f == 0x10001 && value.getType() == INT) { |
| 132 | return true; |
| 133 | } |
| 134 | return false; |
| 135 | } |
| 136 | |
tsaichristine | 7a57b8e | 2019-06-24 18:25:38 -0700 | [diff] [blame] | 137 | bool isUidField(const Field& field, const Value& value) { |
| 138 | auto it = android::util::AtomsInfo::kAtomsWithUidField.find(field.getTag()); |
| 139 | |
| 140 | if (it != android::util::AtomsInfo::kAtomsWithUidField.end()) { |
Rafal Slawik | 390692b | 2019-09-18 12:40:17 +0100 | [diff] [blame] | 141 | int uidField = it->second; // uidField is the field number in proto |
tsaichristine | 7a57b8e | 2019-06-24 18:25:38 -0700 | [diff] [blame] | 142 | return field.getDepth() == 0 && field.getPosAtDepth(0) == uidField && |
| 143 | value.getType() == INT; |
| 144 | } |
| 145 | |
| 146 | return false; |
| 147 | } |
| 148 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 149 | Value::Value(const Value& from) { |
| 150 | type = from.getType(); |
| 151 | switch (type) { |
| 152 | case INT: |
| 153 | int_value = from.int_value; |
| 154 | break; |
| 155 | case LONG: |
| 156 | long_value = from.long_value; |
| 157 | break; |
| 158 | case FLOAT: |
| 159 | float_value = from.float_value; |
| 160 | break; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 161 | case DOUBLE: |
| 162 | double_value = from.double_value; |
| 163 | break; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 164 | case STRING: |
| 165 | str_value = from.str_value; |
| 166 | break; |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 167 | case STORAGE: |
| 168 | storage_value = from.storage_value; |
| 169 | break; |
Yangster-mac | f520492 | 2018-02-23 13:08:03 -0800 | [diff] [blame] | 170 | default: |
| 171 | break; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | std::string Value::toString() const { |
| 176 | switch (type) { |
| 177 | case INT: |
| 178 | return std::to_string(int_value) + "[I]"; |
| 179 | case LONG: |
| 180 | return std::to_string(long_value) + "[L]"; |
| 181 | case FLOAT: |
| 182 | return std::to_string(float_value) + "[F]"; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 183 | case DOUBLE: |
| 184 | return std::to_string(double_value) + "[D]"; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 185 | case STRING: |
| 186 | return str_value + "[S]"; |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 187 | case STORAGE: |
| 188 | return "bytes of size " + std::to_string(storage_value.size()) + "[ST]"; |
Yangster-mac | f520492 | 2018-02-23 13:08:03 -0800 | [diff] [blame] | 189 | default: |
| 190 | return "[UNKNOWN]"; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 194 | bool Value::isZero() const { |
| 195 | switch (type) { |
| 196 | case INT: |
| 197 | return int_value == 0; |
| 198 | case LONG: |
| 199 | return long_value == 0; |
| 200 | case FLOAT: |
| 201 | return fabs(float_value) <= std::numeric_limits<float>::epsilon(); |
| 202 | case DOUBLE: |
| 203 | return fabs(double_value) <= std::numeric_limits<double>::epsilon(); |
| 204 | case STRING: |
| 205 | return str_value.size() == 0; |
| 206 | case STORAGE: |
| 207 | return storage_value.size() == 0; |
| 208 | default: |
| 209 | return false; |
| 210 | } |
| 211 | } |
| 212 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 213 | bool Value::operator==(const Value& that) const { |
| 214 | if (type != that.getType()) return false; |
| 215 | |
| 216 | switch (type) { |
| 217 | case INT: |
| 218 | return int_value == that.int_value; |
| 219 | case LONG: |
| 220 | return long_value == that.long_value; |
| 221 | case FLOAT: |
| 222 | return float_value == that.float_value; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 223 | case DOUBLE: |
| 224 | return double_value == that.double_value; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 225 | case STRING: |
| 226 | return str_value == that.str_value; |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 227 | case STORAGE: |
| 228 | return storage_value == that.storage_value; |
Yangster-mac | f520492 | 2018-02-23 13:08:03 -0800 | [diff] [blame] | 229 | default: |
| 230 | return false; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
| 234 | bool Value::operator!=(const Value& that) const { |
| 235 | if (type != that.getType()) return true; |
| 236 | switch (type) { |
| 237 | case INT: |
| 238 | return int_value != that.int_value; |
| 239 | case LONG: |
| 240 | return long_value != that.long_value; |
| 241 | case FLOAT: |
| 242 | return float_value != that.float_value; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 243 | case DOUBLE: |
| 244 | return double_value != that.double_value; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 245 | case STRING: |
| 246 | return str_value != that.str_value; |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 247 | case STORAGE: |
| 248 | return storage_value != that.storage_value; |
Yangster-mac | f520492 | 2018-02-23 13:08:03 -0800 | [diff] [blame] | 249 | default: |
| 250 | return false; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
| 254 | bool Value::operator<(const Value& that) const { |
| 255 | if (type != that.getType()) return type < that.getType(); |
| 256 | |
| 257 | switch (type) { |
| 258 | case INT: |
| 259 | return int_value < that.int_value; |
| 260 | case LONG: |
| 261 | return long_value < that.long_value; |
| 262 | case FLOAT: |
| 263 | return float_value < that.float_value; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 264 | case DOUBLE: |
| 265 | return double_value < that.double_value; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 266 | case STRING: |
| 267 | return str_value < that.str_value; |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 268 | case STORAGE: |
| 269 | return storage_value < that.storage_value; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 270 | default: |
| 271 | return false; |
| 272 | } |
| 273 | } |
| 274 | |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 275 | bool Value::operator>(const Value& that) const { |
| 276 | if (type != that.getType()) return type > that.getType(); |
| 277 | |
| 278 | switch (type) { |
| 279 | case INT: |
| 280 | return int_value > that.int_value; |
| 281 | case LONG: |
| 282 | return long_value > that.long_value; |
| 283 | case FLOAT: |
| 284 | return float_value > that.float_value; |
| 285 | case DOUBLE: |
| 286 | return double_value > that.double_value; |
| 287 | case STRING: |
| 288 | return str_value > that.str_value; |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 289 | case STORAGE: |
| 290 | return storage_value > that.storage_value; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 291 | default: |
| 292 | return false; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | bool Value::operator>=(const Value& that) const { |
| 297 | if (type != that.getType()) return type >= that.getType(); |
| 298 | |
| 299 | switch (type) { |
| 300 | case INT: |
| 301 | return int_value >= that.int_value; |
| 302 | case LONG: |
| 303 | return long_value >= that.long_value; |
| 304 | case FLOAT: |
| 305 | return float_value >= that.float_value; |
| 306 | case DOUBLE: |
| 307 | return double_value >= that.double_value; |
| 308 | case STRING: |
| 309 | return str_value >= that.str_value; |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 310 | case STORAGE: |
| 311 | return storage_value >= that.storage_value; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 312 | default: |
| 313 | return false; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | Value Value::operator-(const Value& that) const { |
| 318 | Value v; |
| 319 | if (type != that.type) { |
| 320 | ALOGE("Can't operate on different value types, %d, %d", type, that.type); |
| 321 | return v; |
| 322 | } |
| 323 | if (type == STRING) { |
| 324 | ALOGE("Can't operate on string value type"); |
| 325 | return v; |
| 326 | } |
| 327 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 328 | if (type == STORAGE) { |
| 329 | ALOGE("Can't operate on storage value type"); |
| 330 | return v; |
| 331 | } |
| 332 | |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 333 | switch (type) { |
| 334 | case INT: |
| 335 | v.setInt(int_value - that.int_value); |
| 336 | break; |
| 337 | case LONG: |
| 338 | v.setLong(long_value - that.long_value); |
| 339 | break; |
| 340 | case FLOAT: |
| 341 | v.setFloat(float_value - that.float_value); |
| 342 | break; |
| 343 | case DOUBLE: |
| 344 | v.setDouble(double_value - that.double_value); |
| 345 | break; |
| 346 | default: |
| 347 | break; |
| 348 | } |
| 349 | return v; |
| 350 | } |
| 351 | |
| 352 | Value& Value::operator=(const Value& that) { |
| 353 | type = that.type; |
| 354 | switch (type) { |
| 355 | case INT: |
| 356 | int_value = that.int_value; |
| 357 | break; |
| 358 | case LONG: |
| 359 | long_value = that.long_value; |
| 360 | break; |
| 361 | case FLOAT: |
| 362 | float_value = that.float_value; |
| 363 | break; |
| 364 | case DOUBLE: |
| 365 | double_value = that.double_value; |
| 366 | break; |
| 367 | case STRING: |
| 368 | str_value = that.str_value; |
| 369 | break; |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 370 | case STORAGE: |
| 371 | storage_value = that.storage_value; |
| 372 | break; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 373 | default: |
| 374 | break; |
| 375 | } |
| 376 | return *this; |
| 377 | } |
| 378 | |
| 379 | Value& Value::operator+=(const Value& that) { |
| 380 | if (type != that.type) { |
| 381 | ALOGE("Can't operate on different value types, %d, %d", type, that.type); |
| 382 | return *this; |
| 383 | } |
| 384 | if (type == STRING) { |
| 385 | ALOGE("Can't operate on string value type"); |
| 386 | return *this; |
| 387 | } |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 388 | if (type == STORAGE) { |
| 389 | ALOGE("Can't operate on storage value type"); |
| 390 | return *this; |
| 391 | } |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 392 | |
| 393 | switch (type) { |
| 394 | case INT: |
| 395 | int_value += that.int_value; |
| 396 | break; |
| 397 | case LONG: |
| 398 | long_value += that.long_value; |
| 399 | break; |
| 400 | case FLOAT: |
| 401 | float_value += that.float_value; |
| 402 | break; |
| 403 | case DOUBLE: |
| 404 | double_value += that.double_value; |
| 405 | break; |
| 406 | default: |
| 407 | break; |
| 408 | } |
| 409 | return *this; |
| 410 | } |
| 411 | |
| 412 | double Value::getDouble() const { |
| 413 | switch (type) { |
| 414 | case INT: |
| 415 | return int_value; |
| 416 | case LONG: |
| 417 | return long_value; |
| 418 | case FLOAT: |
| 419 | return float_value; |
| 420 | case DOUBLE: |
| 421 | return double_value; |
| 422 | default: |
| 423 | return 0; |
| 424 | } |
| 425 | } |
| 426 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 427 | bool equalDimensions(const std::vector<Matcher>& dimension_a, |
| 428 | const std::vector<Matcher>& dimension_b) { |
| 429 | bool eq = dimension_a.size() == dimension_b.size(); |
| 430 | for (size_t i = 0; eq && i < dimension_a.size(); ++i) { |
| 431 | if (dimension_b[i] != dimension_a[i]) { |
| 432 | eq = false; |
| 433 | } |
| 434 | } |
| 435 | return eq; |
| 436 | } |
| 437 | |
| 438 | bool HasPositionANY(const FieldMatcher& matcher) { |
| 439 | if (matcher.has_position() && matcher.position() == Position::ANY) { |
| 440 | return true; |
| 441 | } |
| 442 | for (const auto& child : matcher.child()) { |
| 443 | if (HasPositionANY(child)) { |
| 444 | return true; |
| 445 | } |
| 446 | } |
| 447 | return false; |
| 448 | } |
| 449 | |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 450 | bool HasPositionALL(const FieldMatcher& matcher) { |
| 451 | if (matcher.has_position() && matcher.position() == Position::ALL) { |
| 452 | return true; |
| 453 | } |
| 454 | for (const auto& child : matcher.child()) { |
| 455 | if (HasPositionALL(child)) { |
| 456 | return true; |
| 457 | } |
| 458 | } |
| 459 | return false; |
| 460 | } |
| 461 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 462 | } // namespace statsd |
| 463 | } // namespace os |
tsaichristine | 7a57b8e | 2019-06-24 18:25:38 -0700 | [diff] [blame] | 464 | } // namespace android |