Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fc663142 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Remove old puller code from uid sandboxing"

parents 11d0c95f 75ad6c10
Loading
Loading
Loading
Loading
+30 −51
Original line number Diff line number Diff line
@@ -92,23 +92,21 @@ StatsPullerManager::StatsPullerManager()
}

bool StatsPullerManager::Pull(int tagId, const ConfigKey& configKey, const int64_t eventTimeNs,
                              vector<shared_ptr<LogEvent>>* data, bool useUids) {
                              vector<shared_ptr<LogEvent>>* data) {
    std::lock_guard<std::mutex> _l(mLock);
    return PullLocked(tagId, configKey, eventTimeNs, data, useUids);
    return PullLocked(tagId, configKey, eventTimeNs, data);
}

bool StatsPullerManager::Pull(int tagId, const vector<int32_t>& uids, const int64_t eventTimeNs,
                              vector<std::shared_ptr<LogEvent>>* data, bool useUids) {
                              vector<std::shared_ptr<LogEvent>>* data) {
    std::lock_guard<std::mutex> _l(mLock);
    return PullLocked(tagId, uids, eventTimeNs, data, useUids);
    return PullLocked(tagId, uids, eventTimeNs, data);
}

bool StatsPullerManager::PullLocked(int tagId, const ConfigKey& configKey,
                                    const int64_t eventTimeNs, vector<shared_ptr<LogEvent>>* data,
                                    bool useUids) {
                                    const int64_t eventTimeNs, vector<shared_ptr<LogEvent>>* data) {
    vector<int32_t> uids;
    if (useUids) {
        auto uidProviderIt = mPullUidProviders.find(configKey);
    const auto& uidProviderIt = mPullUidProviders.find(configKey);
    if (uidProviderIt == mPullUidProviders.end()) {
        ALOGE("Error pulling tag %d. No pull uid provider for config key %s", tagId,
              configKey.ToString().c_str());
@@ -123,15 +121,12 @@ bool StatsPullerManager::PullLocked(int tagId, const ConfigKey& configKey,
        return false;
    }
    uids = pullUidProvider->getPullAtomUids(tagId);
    }
    return PullLocked(tagId, uids, eventTimeNs, data, useUids);
    return PullLocked(tagId, uids, eventTimeNs, data);
}

bool StatsPullerManager::PullLocked(int tagId, const vector<int32_t>& uids,
                                    const int64_t eventTimeNs, vector<shared_ptr<LogEvent>>* data,
                                    bool useUids) {
                                    const int64_t eventTimeNs, vector<shared_ptr<LogEvent>>* data) {
    VLOG("Initiating pulling %d", tagId);
    if (useUids) {
    for (int32_t uid : uids) {
        PullerKey key = {.atomTag = tagId, .uid = uid};
        auto pullerIt = kAllPullAtomInfo.find(key);
@@ -147,20 +142,6 @@ bool StatsPullerManager::PullLocked(int tagId, const vector<int32_t>& uids,
    StatsdStats::getInstance().notePullerNotFound(tagId);
    ALOGW("StatsPullerManager: Unknown tagId %d", tagId);
    return false;  // Return early since we don't know what to pull.
    } else {
        PullerKey key = {.atomTag = tagId, .uid = -1};
        auto pullerIt = kAllPullAtomInfo.find(key);
        if (pullerIt != kAllPullAtomInfo.end()) {
            bool ret = pullerIt->second->Pull(eventTimeNs, data);
            VLOG("pulled %zu items", data->size());
            if (!ret) {
                StatsdStats::getInstance().notePullFailed(tagId);
            }
            return ret;
        }
        ALOGW("StatsPullerManager: Unknown tagId %d", tagId);
        return false;  // Return early since we don't know what to pull.
    }
}

bool StatsPullerManager::PullerForMatcherExists(int tagId) const {
@@ -352,8 +333,7 @@ int StatsPullerManager::ClearPullerCacheIfNecessary(int64_t timestampNs) {
void StatsPullerManager::RegisterPullAtomCallback(const int uid, const int32_t atomTag,
                                                  const int64_t coolDownNs, const int64_t timeoutNs,
                                                  const vector<int32_t>& additiveFields,
                                                  const shared_ptr<IPullAtomCallback>& callback,
                                                  bool useUid) {
                                                  const shared_ptr<IPullAtomCallback>& callback) {
    std::lock_guard<std::mutex> _l(mLock);
    VLOG("RegisterPullerCallback: adding puller for tag %d", atomTag);

@@ -368,16 +348,15 @@ void StatsPullerManager::RegisterPullAtomCallback(const int uid, const int32_t a

    sp<StatsCallbackPuller> puller = new StatsCallbackPuller(atomTag, callback, actualCoolDownNs,
                                                             actualTimeoutNs, additiveFields);
    PullerKey key = {.atomTag = atomTag, .uid = useUid ? uid : -1};
    PullerKey key = {.atomTag = atomTag, .uid = uid};
    AIBinder_linkToDeath(callback->asBinder().get(), mPullAtomCallbackDeathRecipient.get(),
                         new PullAtomCallbackDeathCookie(this, key, puller));
    kAllPullAtomInfo[key] = puller;
}

void StatsPullerManager::UnregisterPullAtomCallback(const int uid, const int32_t atomTag,
                                                    bool useUids) {
void StatsPullerManager::UnregisterPullAtomCallback(const int uid, const int32_t atomTag) {
    std::lock_guard<std::mutex> _l(mLock);
    PullerKey key = {.atomTag = atomTag, .uid = useUids ? uid : -1};
    PullerKey key = {.atomTag = atomTag, .uid = uid};
    if (kAllPullAtomInfo.find(key) != kAllPullAtomInfo.end()) {
        StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag,
                                                                         /*registered=*/false);
+6 −7
Original line number Diff line number Diff line
@@ -102,11 +102,11 @@ public:
    // If the metric wants to make any change to the data, like timestamps, they
    // should make a copy as this data may be shared with multiple metrics.
    virtual bool Pull(int tagId, const ConfigKey& configKey, const int64_t eventTimeNs,
                      vector<std::shared_ptr<LogEvent>>* data, bool useUids = true);
                      vector<std::shared_ptr<LogEvent>>* data);

    // Same as above, but directly specify the allowed uids to pull from.
    virtual bool Pull(int tagId, const vector<int32_t>& uids, const int64_t eventTimeNs,
                      vector<std::shared_ptr<LogEvent>>* data, bool useUids = true);
                      vector<std::shared_ptr<LogEvent>>* data);

    // Clear pull data cache immediately.
    int ForceClearPullerCache();
@@ -118,10 +118,9 @@ public:

    void RegisterPullAtomCallback(const int uid, const int32_t atomTag, const int64_t coolDownNs,
                                  const int64_t timeoutNs, const vector<int32_t>& additiveFields,
                                  const shared_ptr<IPullAtomCallback>& callback,
                                  bool useUid = true);
                                  const shared_ptr<IPullAtomCallback>& callback);

    void UnregisterPullAtomCallback(const int uid, const int32_t atomTag, bool useUids = true);
    void UnregisterPullAtomCallback(const int uid, const int32_t atomTag);

    std::map<const PullerKey, sp<StatsPuller>> kAllPullAtomInfo;

@@ -153,10 +152,10 @@ private:
    std::map<ConfigKey, wp<PullUidProvider>> mPullUidProviders;

    bool PullLocked(int tagId, const ConfigKey& configKey, const int64_t eventTimeNs,
                    vector<std::shared_ptr<LogEvent>>* data, bool useUids = true);
                    vector<std::shared_ptr<LogEvent>>* data);

    bool PullLocked(int tagId, const vector<int32_t>& uids, const int64_t eventTimeNs,
                    vector<std::shared_ptr<LogEvent>>* data, bool useUids);
                    vector<std::shared_ptr<LogEvent>>* data);

    // locks for data receiver and StatsCompanionService changes
    std::mutex mLock;
+8 −8
Original line number Diff line number Diff line
@@ -89,10 +89,10 @@ public:
sp<StatsPullerManager> createPullerManagerAndRegister() {
    sp<StatsPullerManager> pullerManager = new StatsPullerManager();
    shared_ptr<FakePullAtomCallback> cb1 = SharedRefBase::make<FakePullAtomCallback>(uid1);
    pullerManager->RegisterPullAtomCallback(uid1, pullTagId1, coolDownNs, timeoutNs, {}, cb1, true);
    pullerManager->RegisterPullAtomCallback(uid1, pullTagId1, coolDownNs, timeoutNs, {}, cb1);
    shared_ptr<FakePullAtomCallback> cb2 = SharedRefBase::make<FakePullAtomCallback>(uid2);
    pullerManager->RegisterPullAtomCallback(uid2, pullTagId1, coolDownNs, timeoutNs, {}, cb2, true);
    pullerManager->RegisterPullAtomCallback(uid1, pullTagId2, coolDownNs, timeoutNs, {}, cb1, true);
    pullerManager->RegisterPullAtomCallback(uid2, pullTagId1, coolDownNs, timeoutNs, {}, cb2);
    pullerManager->RegisterPullAtomCallback(uid1, pullTagId2, coolDownNs, timeoutNs, {}, cb1);
    return pullerManager;
}
}  // anonymous namespace
@@ -101,14 +101,14 @@ TEST(StatsPullerManagerTest, TestPullInvalidUid) {
    sp<StatsPullerManager> pullerManager = createPullerManagerAndRegister();

    vector<shared_ptr<LogEvent>> data;
    EXPECT_FALSE(pullerManager->Pull(pullTagId1, {unregisteredUid}, /*timestamp =*/1, &data, true));
    EXPECT_FALSE(pullerManager->Pull(pullTagId1, {unregisteredUid}, /*timestamp =*/1, &data));
}

TEST(StatsPullerManagerTest, TestPullChoosesCorrectUid) {
    sp<StatsPullerManager> pullerManager = createPullerManagerAndRegister();

    vector<shared_ptr<LogEvent>> data;
    EXPECT_TRUE(pullerManager->Pull(pullTagId1, {uid1}, /*timestamp =*/1, &data, true));
    EXPECT_TRUE(pullerManager->Pull(pullTagId1, {uid1}, /*timestamp =*/1, &data));
    ASSERT_EQ(data.size(), 1);
    EXPECT_EQ(data[0]->GetTagId(), pullTagId1);
    ASSERT_EQ(data[0]->getValues().size(), 1);
@@ -121,7 +121,7 @@ TEST(StatsPullerManagerTest, TestPullInvalidConfigKey) {
    pullerManager->RegisterPullUidProvider(configKey, uidProvider);

    vector<shared_ptr<LogEvent>> data;
    EXPECT_FALSE(pullerManager->Pull(pullTagId1, badConfigKey, /*timestamp =*/1, &data, true));
    EXPECT_FALSE(pullerManager->Pull(pullTagId1, badConfigKey, /*timestamp =*/1, &data));
}

TEST(StatsPullerManagerTest, TestPullConfigKeyGood) {
@@ -130,7 +130,7 @@ TEST(StatsPullerManagerTest, TestPullConfigKeyGood) {
    pullerManager->RegisterPullUidProvider(configKey, uidProvider);

    vector<shared_ptr<LogEvent>> data;
    EXPECT_TRUE(pullerManager->Pull(pullTagId1, configKey, /*timestamp =*/1, &data, true));
    EXPECT_TRUE(pullerManager->Pull(pullTagId1, configKey, /*timestamp =*/1, &data));
    EXPECT_EQ(data[0]->GetTagId(), pullTagId1);
    ASSERT_EQ(data[0]->getValues().size(), 1);
    EXPECT_EQ(data[0]->getValues()[0].mValue.int_value, uid2);
@@ -142,7 +142,7 @@ TEST(StatsPullerManagerTest, TestPullConfigKeyNoPullerWithUid) {
    pullerManager->RegisterPullUidProvider(configKey, uidProvider);

    vector<shared_ptr<LogEvent>> data;
    EXPECT_FALSE(pullerManager->Pull(pullTagId2, configKey, /*timestamp =*/1, &data, true));
    EXPECT_FALSE(pullerManager->Pull(pullTagId2, configKey, /*timestamp =*/1, &data));
}

}  // namespace statsd
+19 −19
Original line number Diff line number Diff line
@@ -137,9 +137,9 @@ TEST(GaugeMetricProducerTest, TestPulledEventsNoCondition) {
    sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>();
    EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, kConfigKey, _, _, _)).WillOnce(Return());
    EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, kConfigKey, _)).WillOnce(Return());
    EXPECT_CALL(*pullerManager, Pull(tagId, kConfigKey, bucketStartTimeNs, _, _))
    EXPECT_CALL(*pullerManager, Pull(tagId, kConfigKey, bucketStartTimeNs, _))
            .WillOnce(Invoke([](int tagId, const ConfigKey&, const int64_t eventTimeNs,
                                vector<std::shared_ptr<LogEvent>>* data, bool) {
                                vector<std::shared_ptr<LogEvent>>* data) {
                EXPECT_EQ(eventTimeNs, bucketStartTimeNs);
                data->clear();
                data->push_back(makeLogEvent(tagId, eventTimeNs + 10, 3, "some value", 11));
@@ -310,10 +310,10 @@ TEST_P(GaugeMetricProducerTest_PartialBucket, TestPulled) {
    sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>();
    EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, kConfigKey, _, _, _)).WillOnce(Return());
    EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, kConfigKey, _)).WillOnce(Return());
    EXPECT_CALL(*pullerManager, Pull(tagId, kConfigKey, _, _, _))
    EXPECT_CALL(*pullerManager, Pull(tagId, kConfigKey, _, _))
            .WillOnce(Return(false))
            .WillOnce(Invoke([](int tagId, const ConfigKey&, const int64_t eventTimeNs,
                                vector<std::shared_ptr<LogEvent>>* data, bool) {
                                vector<std::shared_ptr<LogEvent>>* data) {
                EXPECT_EQ(eventTimeNs, partialBucketSplitTimeNs);
                data->clear();
                data->push_back(CreateRepeatedValueLogEvent(tagId, eventTimeNs, 2));
@@ -388,7 +388,7 @@ TEST(GaugeMetricProducerTest, TestPulledWithAppUpgradeDisabled) {
    sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>();
    EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, kConfigKey, _, _, _)).WillOnce(Return());
    EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, kConfigKey, _)).WillOnce(Return());
    EXPECT_CALL(*pullerManager, Pull(tagId, kConfigKey, bucketStartTimeNs, _, _))
    EXPECT_CALL(*pullerManager, Pull(tagId, kConfigKey, bucketStartTimeNs, _))
            .WillOnce(Return(false));

    GaugeMetricProducer gaugeProducer(kConfigKey, metric, -1 /*-1 meaning no condition*/, {},
@@ -440,9 +440,9 @@ TEST(GaugeMetricProducerTest, TestPulledEventsWithCondition) {
    sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>();
    EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, kConfigKey, _, _, _)).WillOnce(Return());
    EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, kConfigKey, _)).WillOnce(Return());
    EXPECT_CALL(*pullerManager, Pull(tagId, kConfigKey, conditionChangeNs, _, _))
    EXPECT_CALL(*pullerManager, Pull(tagId, kConfigKey, conditionChangeNs, _))
            .WillOnce(Invoke([](int tagId, const ConfigKey&, const int64_t eventTimeNs,
                                vector<std::shared_ptr<LogEvent>>* data, bool) {
                                vector<std::shared_ptr<LogEvent>>* data) {
                data->clear();
                data->push_back(CreateRepeatedValueLogEvent(tagId, eventTimeNs + 10, 100));
                return true;
@@ -527,9 +527,9 @@ TEST(GaugeMetricProducerTest, TestPulledEventsWithSlicedCondition) {
    sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>();
    EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, kConfigKey, _, _, _)).WillOnce(Return());
    EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, kConfigKey, _)).WillOnce(Return());
    EXPECT_CALL(*pullerManager, Pull(tagId, kConfigKey, sliceConditionChangeNs, _, _))
    EXPECT_CALL(*pullerManager, Pull(tagId, kConfigKey, sliceConditionChangeNs, _))
            .WillOnce(Invoke([](int tagId, const ConfigKey&, const int64_t eventTimeNs,
                                vector<std::shared_ptr<LogEvent>>* data, bool) {
                                vector<std::shared_ptr<LogEvent>>* data) {
                data->clear();
                data->push_back(CreateTwoValueLogEvent(tagId, eventTimeNs + 10, 1000, 100));
                return true;
@@ -566,7 +566,7 @@ TEST(GaugeMetricProducerTest, TestPulledEventsAnomalyDetection) {
    sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>();
    EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, kConfigKey, _, _, _)).WillOnce(Return());
    EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, kConfigKey, _)).WillOnce(Return());
    EXPECT_CALL(*pullerManager, Pull(tagId, kConfigKey, bucketStartTimeNs, _, _))
    EXPECT_CALL(*pullerManager, Pull(tagId, kConfigKey, bucketStartTimeNs, _))
            .WillOnce(Return(false));

    GaugeMetric metric;
@@ -665,16 +665,16 @@ TEST(GaugeMetricProducerTest, TestPullOnTrigger) {
                    atomMatcherId, logEventMatcherIndex, atomMatcher, uidMap)});

    sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>();
    EXPECT_CALL(*pullerManager, Pull(tagId, kConfigKey, _, _, _))
    EXPECT_CALL(*pullerManager, Pull(tagId, kConfigKey, _, _))
            .WillOnce(Invoke([](int tagId, const ConfigKey&, const int64_t eventTimeNs,
                                vector<std::shared_ptr<LogEvent>>* data, bool) {
                                vector<std::shared_ptr<LogEvent>>* data) {
                EXPECT_EQ(eventTimeNs, bucketStartTimeNs + 10);
                data->clear();
                data->push_back(CreateRepeatedValueLogEvent(tagId, eventTimeNs, 4));
                return true;
            }))
            .WillOnce(Invoke([](int tagId, const ConfigKey&, const int64_t eventTimeNs,
                                vector<std::shared_ptr<LogEvent>>* data, bool) {
                                vector<std::shared_ptr<LogEvent>>* data) {
                EXPECT_EQ(eventTimeNs, bucketStartTimeNs + 20);
                data->clear();
                data->push_back(CreateRepeatedValueLogEvent(tagId, eventTimeNs, 5));
@@ -737,23 +737,23 @@ TEST(GaugeMetricProducerTest, TestRemoveDimensionInOutput) {
                    atomMatcherId, logEventMatcherIndex, atomMatcher, uidMap)});

    sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>();
    EXPECT_CALL(*pullerManager, Pull(tagId, kConfigKey, _, _, _))
    EXPECT_CALL(*pullerManager, Pull(tagId, kConfigKey, _, _))
            .WillOnce(Invoke([](int tagId, const ConfigKey&, const int64_t eventTimeNs,
                                vector<std::shared_ptr<LogEvent>>* data, bool) {
                                vector<std::shared_ptr<LogEvent>>* data) {
                EXPECT_EQ(eventTimeNs, bucketStartTimeNs + 3);
                data->clear();
                data->push_back(CreateTwoValueLogEvent(tagId, eventTimeNs, 3, 4));
                return true;
            }))
            .WillOnce(Invoke([](int tagId, const ConfigKey&, const int64_t eventTimeNs,
                                vector<std::shared_ptr<LogEvent>>* data, bool) {
                                vector<std::shared_ptr<LogEvent>>* data) {
                EXPECT_EQ(eventTimeNs, bucketStartTimeNs + 10);
                data->clear();
                data->push_back(CreateTwoValueLogEvent(tagId, eventTimeNs, 4, 5));
                return true;
            }))
            .WillOnce(Invoke([](int tagId, const ConfigKey&, const int64_t eventTimeNs,
                                vector<std::shared_ptr<LogEvent>>* data, bool) {
                                vector<std::shared_ptr<LogEvent>>* data) {
                EXPECT_EQ(eventTimeNs, bucketStartTimeNs + 20);
                data->clear();
                data->push_back(CreateTwoValueLogEvent(tagId, eventTimeNs, 4, 6));
@@ -815,10 +815,10 @@ TEST(GaugeMetricProducerTest_BucketDrop, TestBucketDropWhenBucketTooSmall) {
                    atomMatcherId, logEventMatcherIndex, atomMatcher, uidMap)});

    sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>();
    EXPECT_CALL(*pullerManager, Pull(tagId, kConfigKey, bucketStartTimeNs + 3, _, _))
    EXPECT_CALL(*pullerManager, Pull(tagId, kConfigKey, bucketStartTimeNs + 3, _))
            // Bucket start.
            .WillOnce(Invoke([](int tagId, const ConfigKey&, const int64_t eventTimeNs,
                                vector<std::shared_ptr<LogEvent>>* data, bool) {
                                vector<std::shared_ptr<LogEvent>>* data) {
                data->clear();
                data->push_back(CreateRepeatedValueLogEvent(tagId, eventTimeNs, 10));
                return true;
+156 −156

File changed.

Preview size limit exceeded, changes collapsed.

Loading