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