Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 "libdexopt.h" |
| 18 | |
| 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include "android-base/result.h" |
| 23 | #include "android-base/strings.h" |
| 24 | #include "base/common_art_test.h" |
| 25 | #include "gmock/gmock.h" |
| 26 | #include "gtest/gtest.h" |
| 27 | |
| 28 | #include "aidl/com/android/art/CompilerFilter.h" |
| 29 | #include "aidl/com/android/art/DexoptBcpExtArgs.h" |
| 30 | #include "aidl/com/android/art/DexoptSystemServerArgs.h" |
| 31 | #include "aidl/com/android/art/Isa.h" |
| 32 | |
| 33 | namespace art { |
| 34 | |
| 35 | using ::testing::AllOf; |
| 36 | using ::testing::Contains; |
| 37 | using ::testing::HasSubstr; |
| 38 | using ::testing::Not; |
| 39 | using aidl::com::android::art::CompilerFilter; |
| 40 | using aidl::com::android::art::DexoptBcpExtArgs; |
| 41 | using aidl::com::android::art::DexoptSystemServerArgs; |
| 42 | using aidl::com::android::art::Isa; |
| 43 | using android::base::Result; |
| 44 | |
| 45 | std::string GetEnvironmentVariableOrDie(const char* name) { |
| 46 | const char* value = getenv(name); |
| 47 | EXPECT_NE(value, nullptr); |
| 48 | return value; |
| 49 | } |
| 50 | |
| 51 | // See art_artd_tests.xml for *CLASSPATH setup. |
| 52 | class LibDexoptTest : public CommonArtTest { |
| 53 | protected: |
| 54 | void SetUp() override { |
| 55 | CommonArtTest::SetUp(); |
| 56 | |
| 57 | default_bcp_ext_args_.dexFds = {10, 11}; |
| 58 | default_bcp_ext_args_.bootClasspaths = android::base::Split( |
| 59 | GetEnvironmentVariableOrDie("DEX2OATBOOTCLASSPATH"), ":"); // from art_artd_tests.xml |
| 60 | default_bcp_ext_args_.bootClasspathFds = {21, 22}; |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 61 | default_bcp_ext_args_.profileFds = {30, 31}; |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 62 | default_bcp_ext_args_.dirtyImageObjectsFd = 31; |
| 63 | default_bcp_ext_args_.imageFd = 90; |
| 64 | default_bcp_ext_args_.vdexFd = 91; |
| 65 | default_bcp_ext_args_.oatFd = 92; |
| 66 | default_bcp_ext_args_.dexPaths = {"/path/to/foo.jar", "/path/to/bar.jar"}; |
| 67 | default_bcp_ext_args_.oatLocation = "/oat/location/bar.odex"; |
| 68 | default_bcp_ext_args_.isa = Isa::X86_64; |
| 69 | default_bcp_ext_args_.cpuSet = {0, 1}; |
| 70 | default_bcp_ext_args_.threads = 42; |
| 71 | ASSERT_EQ(default_bcp_ext_args_.bootClasspaths.size(), |
| 72 | default_bcp_ext_args_.bootClasspathFds.size()); |
| 73 | |
| 74 | default_system_server_args_.dexFd = 10; |
| 75 | default_system_server_args_.profileFd = 11; |
| 76 | default_system_server_args_.bootClasspaths = android::base::Split( |
| 77 | GetEnvironmentVariableOrDie("BOOTCLASSPATH"), ":"); // from art_artd_tests.xml |
| 78 | default_system_server_args_.bootClasspathFds = {21, 22, 23}; |
| 79 | default_system_server_args_.bootClasspathImageFds = {-1, 31, -1}; |
| 80 | default_system_server_args_.bootClasspathVdexFds = {-1, 32, -1}; |
| 81 | default_system_server_args_.bootClasspathOatFds = {-1, 33, -1}; |
| 82 | default_system_server_args_.classloaderFds = {40, 41}; |
| 83 | default_system_server_args_.classloaderContext = {"/cl/abc.jar", "/cl/def.jar"}; |
| 84 | default_system_server_args_.imageFd = 90; |
| 85 | default_system_server_args_.vdexFd = 91; |
| 86 | default_system_server_args_.oatFd = 92; |
| 87 | default_system_server_args_.dexPath = "/path/to/foo.jar"; |
| 88 | default_system_server_args_.oatLocation = "/oat/location/bar.odex"; |
| 89 | default_system_server_args_.isa = Isa::X86_64; |
| 90 | default_system_server_args_.compilerFilter = CompilerFilter::SPEED_PROFILE; |
| 91 | default_system_server_args_.cpuSet = {0, 1}; |
| 92 | default_system_server_args_.threads = 42; |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 93 | default_system_server_args_.bootImage = "/path/to/boot.art"; |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 94 | ASSERT_EQ(default_system_server_args_.bootClasspaths.size(), |
| 95 | default_system_server_args_.bootClasspathFds.size()); |
| 96 | } |
| 97 | |
| 98 | void TearDown() override { |
| 99 | CommonArtTest::TearDown(); |
| 100 | } |
| 101 | |
| 102 | std::vector<std::string> Dex2oatArgsFromBcpExtensionArgs(const DexoptBcpExtArgs& args) { |
| 103 | std::vector<std::string> cmdline; |
| 104 | Result<void> result = AddDex2oatArgsFromBcpExtensionArgs(args, cmdline); |
| 105 | EXPECT_TRUE(result.ok()) << "Failed expectation: " << result.error().message(); |
| 106 | return cmdline; |
| 107 | } |
| 108 | |
| 109 | std::vector<std::string> Dex2oatArgsFromSystemServerArgs(const DexoptSystemServerArgs& args) { |
| 110 | std::vector<std::string> cmdline; |
| 111 | Result<void> result = AddDex2oatArgsFromSystemServerArgs(args, cmdline); |
| 112 | EXPECT_TRUE(result.ok()) << "Failed expectation: " << result.error().message(); |
| 113 | return cmdline; |
| 114 | } |
| 115 | |
| 116 | bool DexoptBcpExtArgsIsInvalid(const DexoptBcpExtArgs& args) { |
| 117 | std::vector<std::string> cmdline; |
| 118 | Result<void> result = AddDex2oatArgsFromBcpExtensionArgs(args, cmdline); |
| 119 | return result.ok(); |
| 120 | } |
| 121 | |
| 122 | bool DexoptSystemServerArgsIsInvalid(const DexoptSystemServerArgs& args) { |
| 123 | std::vector<std::string> cmdline; |
| 124 | Result<void> result = AddDex2oatArgsFromSystemServerArgs(args, cmdline); |
| 125 | return result.ok(); |
| 126 | } |
| 127 | |
| 128 | DexoptBcpExtArgs default_bcp_ext_args_; |
| 129 | DexoptSystemServerArgs default_system_server_args_; |
| 130 | }; |
| 131 | |
| 132 | TEST_F(LibDexoptTest, AddDex2oatArgsFromBcpExtensionArgs) { |
| 133 | // Test basics with default args |
| 134 | { |
| 135 | std::vector<std::string> cmdline = Dex2oatArgsFromBcpExtensionArgs(default_bcp_ext_args_); |
| 136 | |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 137 | EXPECT_THAT(cmdline, |
| 138 | AllOf(Contains("--dex-fd=10"), |
| 139 | Contains("--dex-fd=11"), |
| 140 | Contains("--dex-file=/path/to/foo.jar"), |
| 141 | Contains("--dex-file=/path/to/bar.jar"), |
| 142 | Contains(HasSubstr("-Xbootclasspath:")), |
| 143 | Contains("-Xbootclasspathfds:21:22"), |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 144 | |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 145 | Contains("--profile-file-fd=30"), |
| 146 | Contains("--profile-file-fd=31"), |
| 147 | Contains("--compiler-filter=speed-profile"), |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 148 | |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 149 | Contains("--image-fd=90"), |
| 150 | Contains("--output-vdex-fd=91"), |
| 151 | Contains("--oat-fd=92"), |
| 152 | Contains("--oat-location=/oat/location/bar.odex"), |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 153 | |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 154 | Contains("--dirty-image-objects-fd=31"), |
| 155 | Contains("--instruction-set=x86_64"), |
| 156 | Contains("--cpu-set=0,1"), |
| 157 | Contains("-j42"), |
| 158 | |
| 159 | Contains(HasSubstr("--base=")))); |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // No profile |
| 163 | { |
| 164 | auto args = default_bcp_ext_args_; |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 165 | args.profileFds = {}; |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 166 | std::vector<std::string> cmdline = Dex2oatArgsFromBcpExtensionArgs(args); |
| 167 | |
| 168 | EXPECT_THAT(cmdline, AllOf( |
| 169 | Not(Contains(HasSubstr("--profile-file-fd="))), |
| 170 | Contains("--compiler-filter=speed"))); |
| 171 | } |
| 172 | |
| 173 | // No dirty image objects fd |
| 174 | { |
| 175 | auto args = default_bcp_ext_args_; |
| 176 | args.dirtyImageObjectsFd = -1; |
| 177 | std::vector<std::string> cmdline = Dex2oatArgsFromBcpExtensionArgs(args); |
| 178 | |
| 179 | EXPECT_THAT(cmdline, Not(Contains(HasSubstr("--dirty-image-objects-fd")))); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | TEST_F(LibDexoptTest, AddDex2oatArgsFromBcpExtensionArgs_InvalidArguments) { |
| 184 | // Mismatched dex number |
| 185 | { |
| 186 | auto args = default_bcp_ext_args_; |
| 187 | args.dexPaths = {"/path/to/foo.jar", "/path/to/bar.jar"}; |
| 188 | args.dexFds = {100, 101, 102}; |
| 189 | |
| 190 | EXPECT_FALSE(DexoptBcpExtArgsIsInvalid(args)); |
| 191 | } |
| 192 | |
| 193 | // Mismatched classpath arguments |
| 194 | { |
| 195 | auto args = default_bcp_ext_args_; |
| 196 | args.bootClasspathFds.emplace_back(200); |
| 197 | |
| 198 | EXPECT_FALSE(DexoptBcpExtArgsIsInvalid(args)); |
| 199 | } |
| 200 | |
| 201 | // Mismatched classpath arguments |
| 202 | { |
| 203 | auto args = default_bcp_ext_args_; |
| 204 | args.bootClasspaths.emplace_back("/unrecognized/jar"); |
| 205 | |
| 206 | EXPECT_FALSE(DexoptBcpExtArgsIsInvalid(args)); |
| 207 | } |
| 208 | |
| 209 | // Missing output fds |
| 210 | { |
| 211 | auto args = default_bcp_ext_args_; |
| 212 | args.imageFd = -1; |
| 213 | EXPECT_FALSE(DexoptBcpExtArgsIsInvalid(args)); |
| 214 | |
| 215 | args = default_bcp_ext_args_; |
| 216 | args.vdexFd = -1; |
| 217 | EXPECT_FALSE(DexoptBcpExtArgsIsInvalid(args)); |
| 218 | |
| 219 | args = default_bcp_ext_args_; |
| 220 | args.oatFd = -1; |
| 221 | EXPECT_FALSE(DexoptBcpExtArgsIsInvalid(args)); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | TEST_F(LibDexoptTest, AddDex2oatArgsFromSystemServerArgs) { |
| 226 | // Test basics with default args |
| 227 | { |
| 228 | std::vector<std::string> cmdline = Dex2oatArgsFromSystemServerArgs(default_system_server_args_); |
| 229 | |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 230 | EXPECT_THAT(cmdline, |
| 231 | AllOf(Contains("--dex-fd=10"), |
| 232 | Contains("--dex-file=/path/to/foo.jar"), |
| 233 | Contains(HasSubstr("-Xbootclasspath:")), |
| 234 | Contains("-Xbootclasspathfds:21:22:23"), |
| 235 | Contains("-Xbootclasspathimagefds:-1:31:-1"), |
| 236 | Contains("-Xbootclasspathvdexfds:-1:32:-1"), |
| 237 | Contains("-Xbootclasspathoatfds:-1:33:-1"), |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 238 | |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 239 | Contains("--profile-file-fd=11"), |
| 240 | Contains("--compiler-filter=speed-profile"), |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 241 | |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 242 | Contains("--app-image-fd=90"), |
| 243 | Contains("--output-vdex-fd=91"), |
| 244 | Contains("--oat-fd=92"), |
| 245 | Contains("--oat-location=/oat/location/bar.odex"), |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 246 | |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 247 | Contains("--class-loader-context-fds=40:41"), |
| 248 | Contains("--class-loader-context=PCL[/cl/abc.jar:/cl/def.jar]"), |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 249 | |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 250 | Contains("--instruction-set=x86_64"), |
| 251 | Contains("--cpu-set=0,1"), |
| 252 | Contains("-j42"), |
| 253 | |
| 254 | Contains("--boot-image=/path/to/boot.art"))); |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | // Test different compiler filters |
| 258 | { |
| 259 | // speed |
| 260 | auto args = default_system_server_args_; |
| 261 | args.compilerFilter = CompilerFilter::SPEED; |
| 262 | std::vector<std::string> cmdline = Dex2oatArgsFromSystemServerArgs(args); |
| 263 | |
| 264 | EXPECT_THAT(cmdline, AllOf( |
| 265 | Not(Contains(HasSubstr("--profile-file-fd="))), |
| 266 | Contains("--compiler-filter=speed"))); |
| 267 | |
| 268 | // verify |
| 269 | args = default_system_server_args_; |
| 270 | args.compilerFilter = CompilerFilter::VERIFY; |
| 271 | cmdline = Dex2oatArgsFromSystemServerArgs(args); |
| 272 | |
| 273 | EXPECT_THAT(cmdline, AllOf( |
| 274 | Not(Contains(HasSubstr("--profile-file-fd="))), |
| 275 | Contains("--compiler-filter=verify"))); |
| 276 | } |
| 277 | |
Jiakai Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 278 | // Test empty classloader context |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 279 | { |
| 280 | auto args = default_system_server_args_; |
| 281 | args.classloaderFds = {}; |
| 282 | args.classloaderContext = {}; |
| 283 | std::vector<std::string> cmdline = Dex2oatArgsFromSystemServerArgs(args); |
| 284 | |
| 285 | EXPECT_THAT(cmdline, AllOf( |
| 286 | Not(Contains(HasSubstr("--class-loader-context-fds"))), |
| 287 | Contains("--class-loader-context=PCL[]"))); |
| 288 | } |
Jiakai Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 289 | |
| 290 | // Test classloader context as parent |
| 291 | { |
| 292 | auto args = default_system_server_args_; |
| 293 | args.classloaderContextAsParent = true; |
| 294 | std::vector<std::string> cmdline = Dex2oatArgsFromSystemServerArgs(args); |
| 295 | |
| 296 | EXPECT_THAT(cmdline, Contains("--class-loader-context=PCL[];PCL[/cl/abc.jar:/cl/def.jar]")); |
| 297 | } |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | TEST_F(LibDexoptTest, AddDex2oatArgsFromSystemServerArgs_InvalidArguments) { |
| 301 | // Mismatched classpath arguments |
| 302 | { |
| 303 | auto args = default_system_server_args_; |
| 304 | args.bootClasspathFds.emplace_back(200); |
| 305 | |
| 306 | EXPECT_FALSE(DexoptSystemServerArgsIsInvalid(args)); |
| 307 | } |
| 308 | |
| 309 | // Unrecognized jar path |
| 310 | { |
| 311 | auto args = default_system_server_args_; |
| 312 | args.bootClasspaths.emplace_back("/unrecognized/jar"); |
| 313 | |
| 314 | EXPECT_FALSE(DexoptSystemServerArgsIsInvalid(args)); |
| 315 | } |
| 316 | |
| 317 | // speed-profile without profile fd |
| 318 | { |
| 319 | auto args = default_system_server_args_; |
| 320 | args.compilerFilter = CompilerFilter::SPEED_PROFILE; |
| 321 | args.profileFd = -1; |
| 322 | EXPECT_FALSE(DexoptSystemServerArgsIsInvalid(args)); |
| 323 | } |
| 324 | |
| 325 | // Missing output fds |
| 326 | { |
| 327 | auto args = default_system_server_args_; |
| 328 | args.imageFd = -1; |
| 329 | EXPECT_FALSE(DexoptSystemServerArgsIsInvalid(args)); |
| 330 | |
| 331 | args = default_system_server_args_; |
| 332 | args.vdexFd = -1; |
| 333 | EXPECT_FALSE(DexoptSystemServerArgsIsInvalid(args)); |
| 334 | |
| 335 | args = default_system_server_args_; |
| 336 | args.oatFd = -1; |
| 337 | EXPECT_FALSE(DexoptSystemServerArgsIsInvalid(args)); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | } // namespace art |