use only string type in the log source whitelist.

+ predefined "AID_X" will be provided as string type to statsd, and we will translate
  to integer uid using the static map.

Test: statsd_test

Change-Id: Ie47d8481e0c456457e6881ebb9cb4ce008e772b8
This commit is contained in:
Yao Chen
2017-12-22 14:35:34 -08:00
committed by Yangster-mac
parent ec397abf93
commit 147ce60278
10 changed files with 98 additions and 21 deletions

View File

@@ -127,7 +127,7 @@ void StatsLogProcessor::OnConfigUpdated(const ConfigKey& key, const StatsdConfig
if (newMetricsManager->isConfigValid()) {
mUidMap->OnConfigUpdated(key);
newMetricsManager->setAnomalyMonitor(mAnomalyMonitor);
if (config.log_source().package().size() > 0) {
if (newMetricsManager->shouldAddUidMapListener()) {
// We have to add listener after the MetricsManager is constructed because it's
// not safe to create wp or sp from this pointer inside its constructor.
mUidMap->addListener(newMetricsManager.get());
@@ -150,7 +150,8 @@ size_t StatsLogProcessor::GetMetricsSize(const ConfigKey& key) const {
return it->second->byteSize();
}
void StatsLogProcessor::onDumpReport(const ConfigKey& key, const uint64_t& dumpTimeStampNs, ConfigMetricsReportList* report) {
void StatsLogProcessor::onDumpReport(const ConfigKey& key, const uint64_t& dumpTimeStampNs,
ConfigMetricsReportList* report) {
auto it = mMetricsManagers.find(key);
if (it == mMetricsManagers.end()) {
ALOGW("Config source %s does not exist", key.ToString().c_str());

View File

@@ -248,11 +248,11 @@ StatsdConfig build_fake_config() {
details->add_section(12);
details->add_section(13);*/
AllowedLogSource* logSource = config.mutable_log_source();
logSource->add_uid(1000);
logSource->add_uid(0);
logSource->add_package("com.android.statsd.dogfood");
logSource->add_package("com.android.bluetooth");
config.add_allowed_log_source("AID_ROOT");
config.add_allowed_log_source("AID_SYSTEM");
config.add_allowed_log_source("AID_BLUETOOTH");
config.add_allowed_log_source("com.android.statsd.dogfood");
config.add_allowed_log_source("com.android.systemui");
// Count process state changes, slice by uid.
metric = config.add_count_metric();

View File

@@ -53,7 +53,7 @@ MetricsManager::MetricsManager(const ConfigKey& key, const StatsdConfig& config,
mAllMetricProducers, mAllAnomalyTrackers, mConditionToMetricMap,
mTrackerToMetricMap, mTrackerToConditionMap, mNoReportMetricIds);
if (!config.has_log_source()) {
if (config.allowed_log_source_size() == 0) {
// TODO(b/70794411): uncomment the following line and remove the hard coded log source
// after all configs have the log source added.
// mConfigValid = false;
@@ -63,10 +63,14 @@ MetricsManager::MetricsManager(const ConfigKey& key, const StatsdConfig& config,
mAllowedUid.push_back(0);
mAllowedLogSources.insert(mAllowedUid.begin(), mAllowedUid.end());
} else {
mAllowedUid.insert(mAllowedUid.begin(), config.log_source().uid().begin(),
config.log_source().uid().end());
mAllowedPkg.insert(mAllowedPkg.begin(), config.log_source().package().begin(),
config.log_source().package().end());
for (const auto& source : config.allowed_log_source()) {
auto it = UidMap::sAidToUidMapping.find(source);
if (it != UidMap::sAidToUidMapping.end()) {
mAllowedUid.push_back(it->second);
} else {
mAllowedPkg.push_back(source);
}
}
if (mAllowedUid.size() + mAllowedPkg.size() > StatsdStats::kMaxLogSourceCount) {
ALOGE("Too many log sources. This is likely to be an error in the config.");

View File

@@ -57,6 +57,10 @@ public:
void onUidMapReceived() override;
bool shouldAddUidMapListener() const {
return !mAllowedPkg.empty();
}
// Config source owner can call onDumpReport() to get all the metrics collected.
virtual void onDumpReport(android::util::ProtoOutputStream* protoOutput);
virtual void onDumpReport(const uint64_t& dumpTimeStampNs, ConfigMetricsReport* report);

View File

@@ -402,6 +402,79 @@ set<int32_t> UidMap::getAppUid(const string& package) const {
return results;
}
// Note not all the following AIDs are used as uids. Some are used only for gids.
// It's ok to leave them in the map, but we won't ever see them in the log's uid field.
// App's uid starts from 10000, and will not overlap with the following AIDs.
const std::map<string, uint32_t> UidMap::sAidToUidMapping = {{"AID_ROOT", 0},
{"AID_SYSTEM", 1000},
{"AID_RADIO", 1001},
{"AID_BLUETOOTH", 1002},
{"AID_GRAPHICS", 1003},
{"AID_INPUT", 1004},
{"AID_AUDIO", 1005},
{"AID_CAMERA", 1006},
{"AID_LOG", 1007},
{"AID_COMPASS", 1008},
{"AID_MOUNT", 1009},
{"AID_WIFI", 1010},
{"AID_ADB", 1011},
{"AID_INSTALL", 1012},
{"AID_MEDIA", 1013},
{"AID_DHCP", 1014},
{"AID_SDCARD_RW", 1015},
{"AID_VPN", 1016},
{"AID_KEYSTORE", 1017},
{"AID_USB", 1018},
{"AID_DRM", 1019},
{"AID_MDNSR", 1020},
{"AID_GPS", 1021},
// {"AID_UNUSED1", 1022},
{"AID_MEDIA_RW", 1023},
{"AID_MTP", 1024},
// {"AID_UNUSED2", 1025},
{"AID_DRMRPC", 1026},
{"AID_NFC", 1027},
{"AID_SDCARD_R", 1028},
{"AID_CLAT", 1029},
{"AID_LOOP_RADIO", 1030},
{"AID_MEDIA_DRM", 1031},
{"AID_PACKAGE_INFO", 1032},
{"AID_SDCARD_PICS", 1033},
{"AID_SDCARD_AV", 1034},
{"AID_SDCARD_ALL", 1035},
{"AID_LOGD", 1036},
{"AID_SHARED_RELRO", 1037},
{"AID_DBUS", 1038},
{"AID_TLSDATE", 1039},
{"AID_MEDIA_EX", 1040},
{"AID_AUDIOSERVER", 1041},
{"AID_METRICS_COLL", 1042},
{"AID_METRICSD", 1043},
{"AID_WEBSERV", 1044},
{"AID_DEBUGGERD", 1045},
{"AID_MEDIA_CODEC", 1046},
{"AID_CAMERASERVER", 1047},
{"AID_FIREWALL", 1048},
{"AID_TRUNKS", 1049},
{"AID_NVRAM", 1050},
{"AID_DNS", 1051},
{"AID_DNS_TETHER", 1052},
{"AID_WEBVIEW_ZYGOTE", 1053},
{"AID_VEHICLE_NETWORK", 1054},
{"AID_MEDIA_AUDIO", 1055},
{"AID_MEDIA_VIDEO", 1056},
{"AID_MEDIA_IMAGE", 1057},
{"AID_TOMBSTONED", 1058},
{"AID_MEDIA_OBB", 1059},
{"AID_ESE", 1060},
{"AID_OTA_UPDATE", 1061},
{"AID_AUTOMOTIVE_EVS", 1062},
{"AID_LOWPAN", 1063},
{"AID_HSM", 1064},
{"AID_SHELL", 2000},
{"AID_CACHE", 2001},
{"AID_DIAG", 2002}};
} // namespace statsd
} // namespace os
} // namespace android

View File

@@ -51,7 +51,7 @@ class UidMap : public virtual android::RefBase {
public:
UidMap();
~UidMap();
static const std::map<std::string, uint32_t> sAidToUidMapping;
/*
* All three inputs must be the same size, and the jth element in each array refers to the same
* tuple, ie. uid[j] corresponds to packageName[j] with versionCode[j].

View File

@@ -239,11 +239,6 @@ message Alert {
optional double trigger_if_sum_gt = 5;
}
message AllowedLogSource {
repeated int32 uid = 1;
repeated string package = 2;
}
message Alarm {
optional int64 id = 1;
optional int64 offset_millis = 2;
@@ -299,7 +294,7 @@ message StatsdConfig {
repeated Subscription subscription = 11;
optional AllowedLogSource log_source = 12;
repeated string allowed_log_source = 12;
repeated int64 no_report_metric = 13;
}

View File

@@ -20,7 +20,8 @@ LOCAL_PACKAGE_NAME := StatsdDogfood
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_SRC_FILES += ../../src/stats_log.proto \
../../src/atoms.proto
../../src/atoms.proto \
../../src/statsd_config.proto
LOCAL_PROTOC_FLAGS := --proto_path=$(LOCAL_PATH)/../../src/
LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res

View File

@@ -36,7 +36,6 @@ public class MainActivity extends Activity {
private final static String TAG = "StatsdDogfood";
private final static long CONFIG_ID = 987654321;
final int[] mUids = {11111111, 2222222};
StatsManager mStatsManager;
TextView mReportText;