1 /*
2 * Copyright (C) 2019 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 "palette/palette.h"
18
19 #include <jni.h>
20 #include <sys/mman.h>
21 #include <sys/syscall.h>
22 #include <unistd.h>
23
24 #include <cstring>
25 #include <filesystem>
26
27 #ifdef ART_TARGET_ANDROID
28 #include "android-modules-utils/sdk_level.h"
29 #include "android/api-level.h"
30 #endif
31
32 #include "base/common_art_test.h"
33 #include "gtest/gtest.h"
34
35 namespace {
36
GetTid()37 pid_t GetTid() {
38 #ifdef __BIONIC__
39 return gettid();
40 #else // __BIONIC__
41 return syscall(__NR_gettid);
42 #endif // __BIONIC__
43 }
44
45 #ifdef ART_TARGET_ANDROID
PaletteSetTaskProfilesIsSupported(palette_status_t res)46 bool PaletteSetTaskProfilesIsSupported(palette_status_t res) {
47 if (android::modules::sdklevel::IsAtLeastU()) {
48 return true;
49 }
50 EXPECT_EQ(PALETTE_STATUS_NOT_SUPPORTED, res)
51 << "Device API level: " << android_get_device_api_level();
52 return false;
53 }
PaletteDebugStoreIsSupported()54 bool PaletteDebugStoreIsSupported() {
55 // TODO(b/345433959): Switch to android::modules::sdklevel::IsAtLeastW
56 return android_get_device_api_level() >= 36;
57 }
58 #endif
59
60 } // namespace
61
62 class PaletteClientTest : public testing::Test {};
63
TEST_F(PaletteClientTest,SchedPriority)64 TEST_F(PaletteClientTest, SchedPriority) {
65 int32_t tid = GetTid();
66 int32_t saved_priority;
67 EXPECT_EQ(PALETTE_STATUS_OK, PaletteSchedGetPriority(tid, &saved_priority));
68
69 EXPECT_EQ(PALETTE_STATUS_INVALID_ARGUMENT, PaletteSchedSetPriority(tid, /*java_priority=*/ 0));
70 EXPECT_EQ(PALETTE_STATUS_INVALID_ARGUMENT, PaletteSchedSetPriority(tid, /*java_priority=*/ -1));
71 EXPECT_EQ(PALETTE_STATUS_INVALID_ARGUMENT, PaletteSchedSetPriority(tid, /*java_priority=*/ 11));
72
73 EXPECT_EQ(PALETTE_STATUS_OK, PaletteSchedSetPriority(tid, /*java_priority=*/ 1));
74 EXPECT_EQ(PALETTE_STATUS_OK, PaletteSchedSetPriority(tid, saved_priority));
75 }
76
TEST_F(PaletteClientTest,Trace)77 TEST_F(PaletteClientTest, Trace) {
78 bool enabled = false;
79 EXPECT_EQ(PALETTE_STATUS_OK, PaletteTraceEnabled(&enabled));
80 EXPECT_EQ(PALETTE_STATUS_OK, PaletteTraceBegin("Hello world!"));
81 EXPECT_EQ(PALETTE_STATUS_OK, PaletteTraceEnd());
82 EXPECT_EQ(PALETTE_STATUS_OK, PaletteTraceIntegerValue("Beans", /*value=*/ 3));
83 }
84
TEST_F(PaletteClientTest,Ashmem)85 TEST_F(PaletteClientTest, Ashmem) {
86 #ifndef ART_TARGET_ANDROID
87 GTEST_SKIP() << "ashmem is only supported on Android";
88 #else
89 int fd;
90 EXPECT_EQ(PALETTE_STATUS_OK, PaletteAshmemCreateRegion("ashmem-test", 4096, &fd));
91 EXPECT_EQ(PALETTE_STATUS_OK, PaletteAshmemSetProtRegion(fd, PROT_READ | PROT_EXEC));
92 EXPECT_EQ(0, close(fd));
93 #endif
94 }
95
96 class PaletteClientJniTest : public art::CommonArtTest {};
97
TEST_F(PaletteClientJniTest,JniInvocation)98 TEST_F(PaletteClientJniTest, JniInvocation) {
99 bool enabled;
100 EXPECT_EQ(PALETTE_STATUS_OK, PaletteShouldReportJniInvocations(&enabled));
101
102 std::string boot_class_path_string =
103 GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames());
104 std::string boot_class_path_locations_string =
105 GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations());
106
107 JavaVMOption options[] = {
108 {.optionString = boot_class_path_string.c_str(), .extraInfo = nullptr},
109 {.optionString = boot_class_path_locations_string.c_str(), .extraInfo = nullptr},
110 };
111 JavaVMInitArgs vm_args = {
112 .version = JNI_VERSION_1_6,
113 .nOptions = std::size(options),
114 .options = options,
115 .ignoreUnrecognized = JNI_TRUE,
116 };
117
118 JavaVM* jvm = nullptr;
119 JNIEnv* env = nullptr;
120 EXPECT_EQ(JNI_OK, JNI_CreateJavaVM(&jvm, &env, &vm_args));
121 ASSERT_NE(nullptr, env);
122
123 PaletteNotifyBeginJniInvocation(env);
124 PaletteNotifyEndJniInvocation(env);
125
126 EXPECT_EQ(JNI_OK, jvm->DestroyJavaVM());
127 }
128
TEST_F(PaletteClientTest,SetTaskProfiles)129 TEST_F(PaletteClientTest, SetTaskProfiles) {
130 #ifndef ART_TARGET_ANDROID
131 GTEST_SKIP() << "SetTaskProfiles is only supported on Android";
132 #else
133 if (!std::filesystem::exists("/sys/fs/cgroup/cgroup.controllers")) {
134 // This is intended to detect ART chroot setups, where SetTaskProfiles won't work.
135 GTEST_SKIP() << "Kernel cgroup support missing";
136 }
137
138 const char* profiles[] = {"ProcessCapacityHigh", "TimerSlackNormal"};
139 palette_status_t res = PaletteSetTaskProfiles(GetTid(), &profiles[0], 2);
140 if (PaletteSetTaskProfilesIsSupported(res)) {
141 // SetTaskProfiles will only work fully if we run as root. Otherwise it'll
142 // return false which is mapped to PALETTE_STATUS_FAILED_CHECK_LOG.
143 if (getuid() == 0) {
144 EXPECT_EQ(PALETTE_STATUS_OK, res);
145 } else {
146 EXPECT_EQ(PALETTE_STATUS_FAILED_CHECK_LOG, res);
147 }
148 }
149 #endif
150 }
151
TEST_F(PaletteClientTest,SetTaskProfilesCpp)152 TEST_F(PaletteClientTest, SetTaskProfilesCpp) {
153 #ifndef ART_TARGET_ANDROID
154 GTEST_SKIP() << "SetTaskProfiles is only supported on Android";
155 #else
156 if (!std::filesystem::exists("/sys/fs/cgroup/cgroup.controllers")) {
157 // This is intended to detect ART chroot setups, where SetTaskProfiles won't work.
158 GTEST_SKIP() << "Kernel cgroup support missing";
159 }
160
161 std::vector<std::string> profiles = {"ProcessCapacityHigh", "TimerSlackNormal"};
162 palette_status_t res = PaletteSetTaskProfiles(GetTid(), profiles);
163 if (PaletteSetTaskProfilesIsSupported(res)) {
164 // SetTaskProfiles will only work fully if we run as root. Otherwise it'll
165 // return false which is mapped to PALETTE_STATUS_FAILED_CHECK_LOG.
166 if (getuid() == 0) {
167 EXPECT_EQ(PALETTE_STATUS_OK, res);
168 } else {
169 EXPECT_EQ(PALETTE_STATUS_FAILED_CHECK_LOG, res);
170 }
171 }
172 #endif
173 }
174
TEST_F(PaletteClientTest,DebugStore)175 TEST_F(PaletteClientTest, DebugStore) {
176 #ifndef ART_TARGET_ANDROID
177 GTEST_SKIP() << "DebugStore is only supported on Android";
178 #else
179 std::array<char, 20> result{};
180 // Make sure the we are on a correct API level.
181 if (!PaletteDebugStoreIsSupported()) {
182 GTEST_SKIP() << "DebugStore is only supported on API 36+";
183 }
184 palette_status_t pstatus = PaletteDebugStoreGetString(result.data(), result.size());
185 EXPECT_EQ(PALETTE_STATUS_OK, pstatus);
186
187 size_t len = strnlen(result.data(), result.size());
188 EXPECT_TRUE(len < result.size());
189
190 const char* start = "1,0,";
191 const char* end = "::";
192 EXPECT_TRUE(len > strlen(start) + strlen(end));
193 EXPECT_EQ(strncmp(result.data() + len - strlen(end), end, strlen(end)), 0);
194 #endif
195 }
196