1 /*
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 #define STATSD_DEBUG false  // STOPSHIP if true
18 #include "Log.h"
19 
20 #include "SimpleAtomMatchingTracker.h"
21 
22 namespace android {
23 namespace os {
24 namespace statsd {
25 
26 using std::shared_ptr;
27 using std::unordered_map;
28 using std::vector;
29 
SimpleAtomMatchingTracker(const int64_t id,const uint64_t protoHash,const SimpleAtomMatcher & matcher,const sp<UidMap> & uidMap)30 SimpleAtomMatchingTracker::SimpleAtomMatchingTracker(const int64_t id, const uint64_t protoHash,
31                                                      const SimpleAtomMatcher& matcher,
32                                                      const sp<UidMap>& uidMap)
33     : AtomMatchingTracker(id, protoHash), mMatcher(matcher), mUidMap(uidMap) {
34     if (!matcher.has_atom_id()) {
35         mInitialized = false;
36     } else {
37         mAtomIds.insert(matcher.atom_id());
38         mInitialized = true;
39     }
40 }
41 
~SimpleAtomMatchingTracker()42 SimpleAtomMatchingTracker::~SimpleAtomMatchingTracker() {
43 }
44 
init(int matcherIndex,const vector<AtomMatcher> & allAtomMatchers,const vector<sp<AtomMatchingTracker>> & allAtomMatchingTrackers,const unordered_map<int64_t,int> & matcherMap,vector<uint8_t> & stack)45 MatcherInitResult SimpleAtomMatchingTracker::init(
46         int matcherIndex, const vector<AtomMatcher>& allAtomMatchers,
47         const vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
48         const unordered_map<int64_t, int>& matcherMap, vector<uint8_t>& stack) {
49     MatcherInitResult result{nullopt /* invalidConfigReason */,
50                              false /* hasStringTransformation */};
51     // no need to do anything.
52     if (!mInitialized) {
53         result.invalidConfigReason = createInvalidConfigReasonWithMatcher(
54                 INVALID_CONFIG_REASON_MATCHER_TRACKER_NOT_INITIALIZED, mId);
55         return result;
56     }
57 
58     for (const FieldValueMatcher& fvm : mMatcher.field_value_matcher()) {
59         if (fvm.has_replace_string()) {
60             result.hasStringTransformation = true;
61             break;
62         }
63     }
64 
65     return result;
66 }
67 
onConfigUpdated(const AtomMatcher & matcher,const unordered_map<int64_t,int> & atomMatchingTrackerMap)68 optional<InvalidConfigReason> SimpleAtomMatchingTracker::onConfigUpdated(
69         const AtomMatcher& matcher, const unordered_map<int64_t, int>& atomMatchingTrackerMap) {
70     // Do not need to update mMatcher since the matcher must be identical across the update.
71     if (!mInitialized) {
72         return createInvalidConfigReasonWithMatcher(
73                 INVALID_CONFIG_REASON_MATCHER_TRACKER_NOT_INITIALIZED, mId);
74     }
75     return nullopt;
76 }
77 
onLogEvent(const LogEvent & event,int matcherIndex,const vector<sp<AtomMatchingTracker>> & allAtomMatchingTrackers,vector<MatchingState> & matcherResults,vector<shared_ptr<LogEvent>> & matcherTransformations)78 void SimpleAtomMatchingTracker::onLogEvent(
79         const LogEvent& event, int matcherIndex,
80         const vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
81         vector<MatchingState>& matcherResults,
82         vector<shared_ptr<LogEvent>>& matcherTransformations) {
83     if (matcherResults[matcherIndex] != MatchingState::kNotComputed) {
84         VLOG("Matcher %lld already evaluated ", (long long)mId);
85         return;
86     }
87 
88     if (mAtomIds.find(event.GetTagId()) == mAtomIds.end()) {
89         matcherResults[matcherIndex] = MatchingState::kNotMatched;
90         return;
91     }
92 
93     auto [matched, transformedEvent] = matchesSimple(mUidMap, mMatcher, event);
94     matcherResults[matcherIndex] = matched ? MatchingState::kMatched : MatchingState::kNotMatched;
95     VLOG("Stats SimpleAtomMatcher %lld matched? %d", (long long)mId, matched);
96 
97     if (matched && transformedEvent != nullptr) {
98         matcherTransformations[matcherIndex] = std::move(transformedEvent);
99     }
100 }
101 
102 }  // namespace statsd
103 }  // namespace os
104 }  // namespace android
105