1 /*
2 * Copyright (C) 2024 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 <android-base/strings.h>
18 #include <config.pb.h>
19 #include <string>
20
21 namespace android {
22 namespace uprobestats {
23 namespace guardrail {
24
25 using std::string;
26
27 namespace {
28
29 constexpr std::array kAllowedMethodPrefixes = {
30 "com.android.server.am.ActivityManagerService$LocalService."
31 "updateDeviceIdleTempAllowlist",
32 "com.android.server.am.CachedAppOptimizer",
33 "com.android.server.am.OomAdjuster",
34 "com.android.server.am.OomAdjusterModernImpl",
35 };
36
37 } // namespace
38
getFullMethodName(const::uprobestats::protos::UprobestatsConfig::Task::ProbeConfig & probeConfig,bool executabeMethodFileOffsetsApiEnabled)39 std::string getFullMethodName(
40 const ::uprobestats::protos::UprobestatsConfig::Task::ProbeConfig
41 &probeConfig,
42 bool executabeMethodFileOffsetsApiEnabled) {
43 if (executabeMethodFileOffsetsApiEnabled &&
44 probeConfig.has_fully_qualified_class_name()) {
45 return probeConfig.fully_qualified_class_name() + "." +
46 probeConfig.method_name();
47 }
48 const string &methodSignature = probeConfig.method_signature();
49 std::vector<string> components = android::base::Split(methodSignature, " ");
50 if (components.size() < 2) {
51 return "";
52 }
53 return components[1];
54 }
55
isAllowed(const::uprobestats::protos::UprobestatsConfig & config,const string & buildType,bool executabeMethodFileOffsetsApiEnabled)56 bool isAllowed(const ::uprobestats::protos::UprobestatsConfig &config,
57 const string &buildType,
58 bool executabeMethodFileOffsetsApiEnabled) {
59 if (buildType != "user") {
60 return true;
61 }
62 for (const auto &task : config.tasks()) {
63 for (const auto &probeConfig : task.probe_configs()) {
64 const string &fullMethodName =
65 getFullMethodName(probeConfig, executabeMethodFileOffsetsApiEnabled);
66 bool allowed = false;
67 for (const std::string allowedPrefix : kAllowedMethodPrefixes) {
68 if (android::base::StartsWith(fullMethodName, allowedPrefix + ".") ||
69 android::base::StartsWith(fullMethodName, allowedPrefix + "$") ||
70 android::base::StartsWith(fullMethodName, allowedPrefix + "(") ||
71 fullMethodName == allowedPrefix) {
72 allowed = true;
73 break;
74 }
75 }
76 if (!allowed) {
77 return false;
78 }
79 }
80 }
81 return true;
82 }
83
84 } // namespace guardrail
85 } // namespace uprobestats
86 } // namespace android
87