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
26 #include "base/testing.h"
27 #include "gtest/gtest.h"
28
29 #ifdef ART_TARGET_ANDROID
30 #include "android-modules-utils/sdk_level.h"
31 #include "android/api-level.h"
32 #include "nativehelper/JniInvocation.h"
33 #endif
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
TEST_F(PaletteClientTest,JniInvocation)96 TEST_F(PaletteClientTest, JniInvocation) {
97 #ifndef ART_TARGET_ANDROID
98 // On host we need to use the runtime linked into the test to start a VM (e.g.
99 // by inheriting CommonArtTest), while on device it needs to launch the
100 // runtime through libnativehelper. Let's not bother on host since this test
101 // is only for native API coverage on device.
102 GTEST_SKIP() << "Will only spin up a VM on Android";
103 #else
104 bool enabled;
105 EXPECT_EQ(PALETTE_STATUS_OK, PaletteShouldReportJniInvocations(&enabled));
106
107 // Load the default JNI_CreateJavaVM implementation, i.e., libart.so.
108 JniInvocation jni_invocation;
109 ASSERT_TRUE(jni_invocation.Init(/*library=*/ nullptr));
110
111 std::string boot_class_path_string =
112 art::testing::GetClassPathOption("-Xbootclasspath:", art::testing::GetLibCoreDexFileNames());
113 std::string boot_class_path_locations_string = art::testing::GetClassPathOption(
114 "-Xbootclasspath-locations:", art::testing::GetLibCoreDexLocations());
115
116 JavaVMOption options[] = {
117 {.optionString = boot_class_path_string.c_str(), .extraInfo = nullptr},
118 {.optionString = boot_class_path_locations_string.c_str(), .extraInfo = nullptr},
119 };
120 JavaVMInitArgs vm_args = {
121 .version = JNI_VERSION_1_6,
122 .nOptions = std::size(options),
123 .options = options,
124 .ignoreUnrecognized = JNI_TRUE,
125 };
126
127 JavaVM* jvm = nullptr;
128 JNIEnv* env = nullptr;
129 EXPECT_EQ(JNI_OK, JNI_CreateJavaVM(&jvm, &env, &vm_args));
130 ASSERT_NE(nullptr, env);
131
132 PaletteNotifyBeginJniInvocation(env);
133 PaletteNotifyEndJniInvocation(env);
134
135 EXPECT_EQ(JNI_OK, jvm->DestroyJavaVM());
136 #endif
137 }
138
TEST_F(PaletteClientTest,SetTaskProfiles)139 TEST_F(PaletteClientTest, SetTaskProfiles) {
140 #ifndef ART_TARGET_ANDROID
141 GTEST_SKIP() << "SetTaskProfiles is only supported on Android";
142 #else
143 const char* profiles[] = {"ProcessCapacityHigh", "TimerSlackNormal"};
144 palette_status_t res = PaletteSetTaskProfiles(GetTid(), &profiles[0], 2);
145 if (PaletteSetTaskProfilesIsSupported(res)) {
146 // SetTaskProfiles will only work fully if we run as root. Otherwise it'll
147 // return false which is mapped to PALETTE_STATUS_FAILED_CHECK_LOG.
148 if (getuid() == 0) {
149 EXPECT_EQ(PALETTE_STATUS_OK, res);
150 } else {
151 EXPECT_EQ(PALETTE_STATUS_FAILED_CHECK_LOG, res);
152 }
153 }
154 #endif
155 }
156
TEST_F(PaletteClientTest,SetTaskProfilesCpp)157 TEST_F(PaletteClientTest, SetTaskProfilesCpp) {
158 #ifndef ART_TARGET_ANDROID
159 GTEST_SKIP() << "SetTaskProfiles is only supported on Android";
160 #else
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