Orion Hodson | 4c3ade6 | 2021-02-10 14:07:10 +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 | |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 17 | #include "odrefresh.h" |
Orion Hodson | 4c3ade6 | 2021-02-10 14:07:10 +0000 | [diff] [blame] | 18 | |
Jiakai Zhang | 4e6cc73 | 2021-09-17 04:46:24 +0000 | [diff] [blame] | 19 | #include <unistd.h> |
| 20 | |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 21 | #include <functional> |
| 22 | #include <memory> |
| 23 | #include <string_view> |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 24 | #include <utility> |
Jiakai Zhang | 4e6cc73 | 2021-09-17 04:46:24 +0000 | [diff] [blame] | 25 | #include <vector> |
| 26 | |
Jiakai Zhang | 34dcce5 | 2022-01-07 16:45:11 +0000 | [diff] [blame] | 27 | #include "android-base/file.h" |
Victor Hsieh | d750630 | 2021-09-24 17:08:23 +0000 | [diff] [blame] | 28 | #include "android-base/parseint.h" |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 29 | #include "android-base/scopeguard.h" |
Jiakai Zhang | 617c6ab | 2021-09-24 05:06:07 +0000 | [diff] [blame] | 30 | #include "android-base/stringprintf.h" |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 31 | #include "android-base/strings.h" |
| 32 | #include "arch/instruction_set.h" |
Orion Hodson | 4c3ade6 | 2021-02-10 14:07:10 +0000 | [diff] [blame] | 33 | #include "base/common_art_test.h" |
| 34 | #include "base/file_utils.h" |
Jiakai Zhang | 617c6ab | 2021-09-24 05:06:07 +0000 | [diff] [blame] | 35 | #include "base/stl_util.h" |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 36 | #include "exec_utils.h" |
| 37 | #include "gmock/gmock.h" |
| 38 | #include "gtest/gtest.h" |
Jiakai Zhang | 617c6ab | 2021-09-24 05:06:07 +0000 | [diff] [blame] | 39 | #include "odr_artifacts.h" |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 40 | #include "odr_common.h" |
| 41 | #include "odr_config.h" |
| 42 | #include "odr_fs_utils.h" |
| 43 | #include "odr_metrics.h" |
| 44 | #include "odrefresh/odrefresh.h" |
Orion Hodson | 4c3ade6 | 2021-02-10 14:07:10 +0000 | [diff] [blame] | 45 | |
| 46 | namespace art { |
| 47 | namespace odrefresh { |
| 48 | |
Jiakai Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 49 | using ::testing::_; |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 50 | using ::testing::AllOf; |
| 51 | using ::testing::Contains; |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 52 | using ::testing::HasSubstr; |
Jiakai Zhang | 8046b62 | 2022-01-28 21:40:17 +0000 | [diff] [blame] | 53 | using ::testing::Not; |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 54 | using ::testing::Return; |
| 55 | |
| 56 | constexpr int kReplace = 1; |
| 57 | |
| 58 | void CreateEmptyFile(const std::string& name) { |
| 59 | File* file = OS::CreateEmptyFile(name.c_str()); |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 60 | ASSERT_TRUE(file != nullptr) << "Cannot create file " << name; |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 61 | file->Release(); |
| 62 | delete file; |
| 63 | } |
| 64 | |
Jiakai Zhang | 4e6cc73 | 2021-09-17 04:46:24 +0000 | [diff] [blame] | 65 | android::base::ScopeGuard<std::function<void()>> ScopedCreateEmptyFile(const std::string& name) { |
| 66 | CreateEmptyFile(name); |
| 67 | return android::base::ScopeGuard([=]() { unlink(name.c_str()); }); |
| 68 | } |
| 69 | |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 70 | class MockExecUtils : public ExecUtils { |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 71 | public: |
| 72 | // A workaround to avoid MOCK_METHOD on a method with an `std::string*` parameter, which will lead |
| 73 | // to a conflict between gmock and android-base/logging.h (b/132668253). |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 74 | int ExecAndReturnCode(std::vector<std::string>& arg_vector, |
| 75 | time_t, |
| 76 | bool*, |
| 77 | std::string*) const override { |
| 78 | return DoExecAndReturnCode(arg_vector); |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 81 | MOCK_METHOD(int, DoExecAndReturnCode, (std::vector<std::string> & arg_vector), (const)); |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 84 | // Matches a flag that starts with `flag` and is a colon-separated list that contains an element |
| 85 | // that matches `matcher`. |
| 86 | MATCHER_P2(FlagContains, flag, matcher, "") { |
| 87 | std::string_view value = arg; |
| 88 | if (!android::base::ConsumePrefix(&value, flag)) { |
| 89 | return false; |
| 90 | } |
| 91 | for (std::string_view s : SplitString(value, ':')) { |
| 92 | if (ExplainMatchResult(matcher, s, result_listener)) { |
| 93 | return true; |
| 94 | } |
| 95 | } |
| 96 | return false; |
| 97 | } |
| 98 | |
Jiakai Zhang | 4e6cc73 | 2021-09-17 04:46:24 +0000 | [diff] [blame] | 99 | // Matches an FD of a file whose path matches `matcher`. |
| 100 | MATCHER_P(FdOf, matcher, "") { |
| 101 | char path[PATH_MAX]; |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 102 | int fd; |
| 103 | if (!android::base::ParseInt(std::string{arg}, &fd)) { |
| 104 | return false; |
| 105 | } |
Jiakai Zhang | 4e6cc73 | 2021-09-17 04:46:24 +0000 | [diff] [blame] | 106 | std::string proc_path = android::base::StringPrintf("/proc/self/fd/%d", fd); |
| 107 | ssize_t len = readlink(proc_path.c_str(), path, sizeof(path)); |
| 108 | if (len < 0) { |
| 109 | return false; |
| 110 | } |
Jiakai Zhang | b1d9035 | 2021-09-22 09:08:26 +0000 | [diff] [blame] | 111 | std::string path_str{path, static_cast<size_t>(len)}; |
Jiakai Zhang | 4e6cc73 | 2021-09-17 04:46:24 +0000 | [diff] [blame] | 112 | return ExplainMatchResult(matcher, path_str, result_listener); |
| 113 | } |
| 114 | |
Jiakai Zhang | 34dcce5 | 2022-01-07 16:45:11 +0000 | [diff] [blame] | 115 | void WriteFakeApexInfoList(const std::string& filename) { |
| 116 | std::string content = R"xml( |
| 117 | <?xml version="1.0" encoding="utf-8"?> |
| 118 | <apex-info-list> |
| 119 | <apex-info |
| 120 | moduleName="com.android.art" |
| 121 | modulePath="/data/apex/active/com.android.art@319999900.apex" |
| 122 | preinstalledModulePath="/system/apex/com.android.art.capex" |
| 123 | versionCode="319999900" |
| 124 | versionName="" |
| 125 | isFactory="false" |
| 126 | isActive="true" |
| 127 | lastUpdateMillis="12345678"> |
| 128 | </apex-info> |
| 129 | </apex-info-list> |
| 130 | )xml"; |
| 131 | android::base::WriteStringToFile(content, filename); |
| 132 | } |
| 133 | |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 134 | class OdRefreshTest : public CommonArtTest { |
| 135 | public: |
| 136 | OdRefreshTest() : config_("odrefresh") {} |
| 137 | |
| 138 | protected: |
| 139 | void SetUp() override { |
| 140 | CommonArtTest::SetUp(); |
| 141 | |
| 142 | temp_dir_ = std::make_unique<ScratchDir>(); |
| 143 | std::string_view temp_dir_path = temp_dir_->GetPath(); |
| 144 | android::base::ConsumeSuffix(&temp_dir_path, "/"); |
| 145 | |
| 146 | std::string android_root_path = Concatenate({temp_dir_path, "/system"}); |
| 147 | ASSERT_TRUE(EnsureDirectoryExists(android_root_path)); |
| 148 | android_root_env_ = std::make_unique<ScopedUnsetEnvironmentVariable>("ANDROID_ROOT"); |
| 149 | setenv("ANDROID_ROOT", android_root_path.c_str(), kReplace); |
| 150 | |
| 151 | std::string android_art_root_path = Concatenate({temp_dir_path, "/apex/com.android.art"}); |
| 152 | ASSERT_TRUE(EnsureDirectoryExists(android_art_root_path)); |
| 153 | android_art_root_env_ = std::make_unique<ScopedUnsetEnvironmentVariable>("ANDROID_ART_ROOT"); |
| 154 | setenv("ANDROID_ART_ROOT", android_art_root_path.c_str(), kReplace); |
| 155 | |
| 156 | std::string art_apex_data_path = Concatenate({temp_dir_path, kArtApexDataDefaultPath}); |
| 157 | ASSERT_TRUE(EnsureDirectoryExists(art_apex_data_path)); |
| 158 | art_apex_data_env_ = std::make_unique<ScopedUnsetEnvironmentVariable>("ART_APEX_DATA"); |
| 159 | setenv("ART_APEX_DATA", art_apex_data_path.c_str(), kReplace); |
| 160 | |
Jiakai Zhang | 4e6cc73 | 2021-09-17 04:46:24 +0000 | [diff] [blame] | 161 | dalvik_cache_dir_ = art_apex_data_path + "/dalvik-cache"; |
| 162 | ASSERT_TRUE(EnsureDirectoryExists(dalvik_cache_dir_ + "/x86_64")); |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 163 | |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 164 | std::string system_etc_dir = Concatenate({android_root_path, "/etc"}); |
| 165 | ASSERT_TRUE(EnsureDirectoryExists(system_etc_dir)); |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 166 | framework_profile_ = system_etc_dir + "/boot-image.prof"; |
| 167 | CreateEmptyFile(framework_profile_); |
| 168 | std::string art_etc_dir = Concatenate({android_art_root_path, "/etc"}); |
| 169 | ASSERT_TRUE(EnsureDirectoryExists(art_etc_dir)); |
| 170 | art_profile_ = art_etc_dir + "/boot-image.prof"; |
| 171 | CreateEmptyFile(art_profile_); |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 172 | |
Jiakai Zhang | 4e6cc73 | 2021-09-17 04:46:24 +0000 | [diff] [blame] | 173 | framework_dir_ = android_root_path + "/framework"; |
| 174 | framework_jar_ = framework_dir_ + "/framework.jar"; |
| 175 | location_provider_jar_ = framework_dir_ + "/com.android.location.provider.jar"; |
| 176 | services_jar_ = framework_dir_ + "/services.jar"; |
Jiakai Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 177 | services_foo_jar_ = framework_dir_ + "/services-foo.jar"; |
| 178 | services_bar_jar_ = framework_dir_ + "/services-bar.jar"; |
Jiakai Zhang | 4e6cc73 | 2021-09-17 04:46:24 +0000 | [diff] [blame] | 179 | std::string services_jar_prof = framework_dir_ + "/services.jar.prof"; |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 180 | art_javalib_dir_ = android_art_root_path + "/javalib"; |
| 181 | core_oj_jar_ = art_javalib_dir_ + "/core-oj.jar"; |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 182 | |
| 183 | // Create placeholder files. |
Jiakai Zhang | 4e6cc73 | 2021-09-17 04:46:24 +0000 | [diff] [blame] | 184 | ASSERT_TRUE(EnsureDirectoryExists(framework_dir_ + "/x86_64")); |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 185 | CreateEmptyFile(framework_jar_); |
| 186 | CreateEmptyFile(location_provider_jar_); |
| 187 | CreateEmptyFile(services_jar_); |
Jiakai Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 188 | CreateEmptyFile(services_foo_jar_); |
| 189 | CreateEmptyFile(services_bar_jar_); |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 190 | CreateEmptyFile(services_jar_prof); |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 191 | ASSERT_TRUE(EnsureDirectoryExists(art_javalib_dir_)); |
| 192 | CreateEmptyFile(core_oj_jar_); |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 193 | |
Jiakai Zhang | 34dcce5 | 2022-01-07 16:45:11 +0000 | [diff] [blame] | 194 | std::string apex_info_filename = Concatenate({temp_dir_path, "/apex-info-list.xml"}); |
| 195 | WriteFakeApexInfoList(apex_info_filename); |
| 196 | config_.SetApexInfoListFile(apex_info_filename); |
| 197 | |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 198 | config_.SetArtBinDir(Concatenate({temp_dir_path, "/bin"})); |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 199 | config_.SetBootClasspath(Concatenate({core_oj_jar_, ":", framework_jar_})); |
| 200 | config_.SetDex2oatBootclasspath(Concatenate({core_oj_jar_, ":", framework_jar_})); |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 201 | config_.SetSystemServerClasspath(Concatenate({location_provider_jar_, ":", services_jar_})); |
Jiakai Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 202 | config_.SetStandaloneSystemServerJars(Concatenate({services_foo_jar_, ":", services_bar_jar_})); |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 203 | config_.SetIsa(InstructionSet::kX86_64); |
| 204 | config_.SetZygoteKind(ZygoteKind::kZygote64_32); |
Victor Hsieh | a39356a | 2021-12-16 11:40:39 -0800 | [diff] [blame] | 205 | config_.SetSystemServerCompilerFilter("speed"); // specify a default |
Jiakai Zhang | 34dcce5 | 2022-01-07 16:45:11 +0000 | [diff] [blame] | 206 | config_.SetArtifactDirectory(dalvik_cache_dir_); |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 207 | |
Jiakai Zhang | 4e6cc73 | 2021-09-17 04:46:24 +0000 | [diff] [blame] | 208 | std::string staging_dir = dalvik_cache_dir_ + "/staging"; |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 209 | ASSERT_TRUE(EnsureDirectoryExists(staging_dir)); |
| 210 | config_.SetStagingDir(staging_dir); |
| 211 | |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 212 | auto mock_exec_utils = std::make_unique<MockExecUtils>(); |
| 213 | mock_exec_utils_ = mock_exec_utils.get(); |
| 214 | |
Jiakai Zhang | 4e6cc73 | 2021-09-17 04:46:24 +0000 | [diff] [blame] | 215 | metrics_ = std::make_unique<OdrMetrics>(dalvik_cache_dir_); |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 216 | odrefresh_ = std::make_unique<OnDeviceRefresh>( |
| 217 | config_, dalvik_cache_dir_ + "/cache-info.xml", std::move(mock_exec_utils)); |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void TearDown() override { |
| 221 | metrics_.reset(); |
| 222 | temp_dir_.reset(); |
| 223 | android_root_env_.reset(); |
| 224 | android_art_root_env_.reset(); |
| 225 | art_apex_data_env_.reset(); |
| 226 | |
| 227 | CommonArtTest::TearDown(); |
| 228 | } |
| 229 | |
| 230 | std::unique_ptr<ScratchDir> temp_dir_; |
| 231 | std::unique_ptr<ScopedUnsetEnvironmentVariable> android_root_env_; |
| 232 | std::unique_ptr<ScopedUnsetEnvironmentVariable> android_art_root_env_; |
| 233 | std::unique_ptr<ScopedUnsetEnvironmentVariable> art_apex_data_env_; |
| 234 | OdrConfig config_; |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 235 | MockExecUtils* mock_exec_utils_; |
| 236 | std::unique_ptr<OnDeviceRefresh> odrefresh_; |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 237 | std::unique_ptr<OdrMetrics> metrics_; |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 238 | std::string core_oj_jar_; |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 239 | std::string framework_jar_; |
| 240 | std::string location_provider_jar_; |
| 241 | std::string services_jar_; |
Jiakai Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 242 | std::string services_foo_jar_; |
| 243 | std::string services_bar_jar_; |
Jiakai Zhang | 4e6cc73 | 2021-09-17 04:46:24 +0000 | [diff] [blame] | 244 | std::string dalvik_cache_dir_; |
| 245 | std::string framework_dir_; |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 246 | std::string art_javalib_dir_; |
| 247 | std::string framework_profile_; |
| 248 | std::string art_profile_; |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 249 | }; |
| 250 | |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 251 | TEST_F(OdRefreshTest, BootClasspathJars) { |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 252 | EXPECT_CALL(*mock_exec_utils_, |
| 253 | DoExecAndReturnCode(AllOf( |
| 254 | Contains(Concatenate({"--dex-file=", core_oj_jar_})), |
| 255 | Contains(Concatenate({"--dex-file=", framework_jar_})), |
| 256 | Contains(FlagContains("--dex-fd=", FdOf(core_oj_jar_))), |
| 257 | Contains(FlagContains("--dex-fd=", FdOf(framework_jar_))), |
| 258 | Contains(FlagContains("--profile-file-fd=", FdOf(art_profile_))), |
| 259 | Contains(FlagContains("--profile-file-fd=", FdOf(framework_profile_))), |
| 260 | Contains(Concatenate({"--oat-location=", dalvik_cache_dir_, "/x86_64/boot.oat"})), |
| 261 | Contains(HasSubstr("--base=")), |
| 262 | Contains("--compiler-filter=speed-profile")))) |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 263 | .WillOnce(Return(0)); |
| 264 | |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 265 | EXPECT_EQ(odrefresh_->Compile(*metrics_, |
| 266 | CompilationOptions{ |
| 267 | .compile_boot_classpath_for_isas = {InstructionSet::kX86_64}, |
| 268 | }), |
Jiakai Zhang | 884e22f | 2021-12-23 20:04:46 +0000 | [diff] [blame] | 269 | ExitCode::kCompilationSuccess); |
| 270 | } |
| 271 | |
Jiakai Zhang | 8046b62 | 2022-01-28 21:40:17 +0000 | [diff] [blame] | 272 | TEST_F(OdRefreshTest, BootClasspathJarsFallback) { |
| 273 | // Simulate the case where dex2oat fails when generating the full boot image. |
| 274 | EXPECT_CALL(*mock_exec_utils_, |
| 275 | DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", core_oj_jar_})), |
| 276 | Contains(Concatenate({"--dex-file=", framework_jar_}))))) |
| 277 | .Times(2) |
| 278 | .WillRepeatedly(Return(1)); |
| 279 | |
| 280 | // It should fall back to generating a minimal boot image. |
| 281 | EXPECT_CALL( |
| 282 | *mock_exec_utils_, |
| 283 | DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", core_oj_jar_})), |
| 284 | Not(Contains(Concatenate({"--dex-file=", framework_jar_})))))) |
| 285 | .Times(2) |
| 286 | .WillOnce(Return(0)); |
| 287 | |
| 288 | EXPECT_EQ( |
| 289 | odrefresh_->Compile( |
| 290 | *metrics_, |
| 291 | CompilationOptions{ |
| 292 | .compile_boot_classpath_for_isas = {InstructionSet::kX86, InstructionSet::kX86_64}, |
| 293 | .system_server_jars_to_compile = odrefresh_->AllSystemServerJars(), |
| 294 | }), |
| 295 | ExitCode::kCompilationFailed); |
| 296 | } |
| 297 | |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 298 | TEST_F(OdRefreshTest, AllSystemServerJars) { |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 299 | EXPECT_CALL(*mock_exec_utils_, |
| 300 | DoExecAndReturnCode(AllOf( |
| 301 | Contains(Concatenate({"--dex-file=", location_provider_jar_})), |
| 302 | Contains("--class-loader-context=PCL[]")))) |
Jiakai Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 303 | .WillOnce(Return(0)); |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 304 | EXPECT_CALL(*mock_exec_utils_, |
| 305 | DoExecAndReturnCode(AllOf( |
| 306 | Contains(Concatenate({"--dex-file=", services_jar_})), |
| 307 | Contains(Concatenate({"--class-loader-context=PCL[", location_provider_jar_, |
| 308 | "]"}))))) |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 309 | .WillOnce(Return(0)); |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 310 | EXPECT_CALL(*mock_exec_utils_, |
| 311 | DoExecAndReturnCode(AllOf( |
| 312 | Contains(Concatenate({"--dex-file=", services_foo_jar_})), |
| 313 | Contains(Concatenate({"--class-loader-context=PCL[];PCL[", location_provider_jar_, |
| 314 | ":", services_jar_, "]"}))))) |
Jiakai Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 315 | .WillOnce(Return(0)); |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 316 | EXPECT_CALL(*mock_exec_utils_, |
| 317 | DoExecAndReturnCode(AllOf( |
| 318 | Contains(Concatenate({"--dex-file=", services_bar_jar_})), |
| 319 | Contains(Concatenate({"--class-loader-context=PCL[];PCL[", location_provider_jar_, |
| 320 | ":", services_jar_, "]"}))))) |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 321 | .WillOnce(Return(0)); |
| 322 | |
| 323 | EXPECT_EQ( |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 324 | odrefresh_->Compile(*metrics_, |
| 325 | CompilationOptions{ |
| 326 | .system_server_jars_to_compile = odrefresh_->AllSystemServerJars(), |
| 327 | }), |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 328 | ExitCode::kCompilationSuccess); |
| 329 | } |
| 330 | |
| 331 | TEST_F(OdRefreshTest, PartialSystemServerJars) { |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 332 | EXPECT_CALL(*mock_exec_utils_, |
| 333 | DoExecAndReturnCode(AllOf( |
| 334 | Contains(Concatenate({"--dex-file=", services_jar_})), |
| 335 | Contains(Concatenate({"--class-loader-context=PCL[", location_provider_jar_, "]"}))))) |
Jiakai Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 336 | .WillOnce(Return(0)); |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 337 | EXPECT_CALL(*mock_exec_utils_, |
| 338 | DoExecAndReturnCode(AllOf( |
| 339 | Contains(Concatenate({"--dex-file=", services_bar_jar_})), |
| 340 | Contains(Concatenate({"--class-loader-context=PCL[];PCL[", location_provider_jar_, |
| 341 | ":", services_jar_, "]"}))))) |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 342 | .WillOnce(Return(0)); |
| 343 | |
Jiakai Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 344 | EXPECT_EQ( |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 345 | odrefresh_->Compile(*metrics_, |
| 346 | CompilationOptions{ |
| 347 | .system_server_jars_to_compile = {services_jar_, services_bar_jar_}, |
| 348 | }), |
Jiakai Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 349 | ExitCode::kCompilationSuccess); |
| 350 | } |
| 351 | |
| 352 | // Verifies that odrefresh can run properly when the STANDALONE_SYSTEM_SERVER_JARS variable is |
| 353 | // missing, which is expected on Android S. |
| 354 | TEST_F(OdRefreshTest, MissingStandaloneSystemServerJars) { |
| 355 | config_.SetStandaloneSystemServerJars(""); |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 356 | EXPECT_CALL(*mock_exec_utils_, DoExecAndReturnCode(_)).WillRepeatedly(Return(0)); |
Jiakai Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 357 | EXPECT_EQ( |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 358 | odrefresh_->Compile(*metrics_, |
| 359 | CompilationOptions{ |
| 360 | .system_server_jars_to_compile = odrefresh_->AllSystemServerJars(), |
| 361 | }), |
Jiakai Zhang | 9b7ddf6 | 2021-11-01 11:36:52 +0000 | [diff] [blame] | 362 | ExitCode::kCompilationSuccess); |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 365 | TEST_F(OdRefreshTest, CompileSetsCompilerFilterToSpeed) { |
| 366 | // Test setup: use "speed" compiler filter. |
| 367 | config_.SetSystemServerCompilerFilter("speed"); |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 368 | |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 369 | // Uninteresting calls. |
| 370 | EXPECT_CALL( |
| 371 | *mock_exec_utils_, DoExecAndReturnCode(_)) |
| 372 | .Times(odrefresh_->AllSystemServerJars().size() - 2) |
| 373 | .WillRepeatedly(Return(0)); |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 374 | |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 375 | EXPECT_CALL( |
| 376 | *mock_exec_utils_, |
| 377 | DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", location_provider_jar_})), |
| 378 | Not(Contains(HasSubstr("--profile-file-fd="))), |
| 379 | Contains("--compiler-filter=speed")))) |
| 380 | .WillOnce(Return(0)); |
| 381 | EXPECT_CALL( |
| 382 | *mock_exec_utils_, |
| 383 | DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", services_jar_})), |
| 384 | Not(Contains(HasSubstr("--profile-file-fd="))), |
| 385 | Contains("--compiler-filter=speed")))) |
| 386 | .WillOnce(Return(0)); |
| 387 | EXPECT_EQ( |
| 388 | odrefresh_->Compile(*metrics_, |
| 389 | CompilationOptions{ |
| 390 | .system_server_jars_to_compile = odrefresh_->AllSystemServerJars(), |
| 391 | }), |
| 392 | ExitCode::kCompilationSuccess); |
| 393 | } |
Jiakai Zhang | eb65b41 | 2021-11-26 14:29:38 +0000 | [diff] [blame] | 394 | |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 395 | TEST_F(OdRefreshTest, CompileSetsCompilerFilterToSpeedProfile) { |
| 396 | // Test setup: with "speed-profile" compiler filter in the request, only apply if there is a |
| 397 | // profile, otherwise fallback to speed. |
| 398 | config_.SetSystemServerCompilerFilter("speed-profile"); |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 399 | |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 400 | // Uninteresting calls. |
| 401 | EXPECT_CALL( |
| 402 | *mock_exec_utils_, DoExecAndReturnCode(_)) |
| 403 | .Times(odrefresh_->AllSystemServerJars().size() - 2) |
| 404 | .WillRepeatedly(Return(0)); |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 405 | |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 406 | // services.jar has a profile, while location.provider.jar does not. |
| 407 | EXPECT_CALL( |
| 408 | *mock_exec_utils_, |
| 409 | DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", location_provider_jar_})), |
| 410 | Not(Contains(HasSubstr("--profile-file-fd="))), |
| 411 | Contains("--compiler-filter=speed")))) |
| 412 | .WillOnce(Return(0)); |
| 413 | EXPECT_CALL( |
| 414 | *mock_exec_utils_, |
| 415 | DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", services_jar_})), |
| 416 | Contains(HasSubstr("--profile-file-fd=")), |
| 417 | Contains("--compiler-filter=speed-profile")))) |
| 418 | .WillOnce(Return(0)); |
| 419 | EXPECT_EQ( |
| 420 | odrefresh_->Compile(*metrics_, |
| 421 | CompilationOptions{ |
| 422 | .system_server_jars_to_compile = odrefresh_->AllSystemServerJars(), |
| 423 | }), |
| 424 | ExitCode::kCompilationSuccess); |
| 425 | } |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 426 | |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 427 | TEST_F(OdRefreshTest, CompileSetsCompilerFilterToVerify) { |
| 428 | // Test setup: use "speed" compiler filter. |
| 429 | config_.SetSystemServerCompilerFilter("verify"); |
Jiakai Zhang | eb65b41 | 2021-11-26 14:29:38 +0000 | [diff] [blame] | 430 | |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 431 | // Uninteresting calls. |
| 432 | EXPECT_CALL( |
| 433 | *mock_exec_utils_, DoExecAndReturnCode(_)) |
| 434 | .Times(odrefresh_->AllSystemServerJars().size() - 2) |
| 435 | .WillRepeatedly(Return(0)); |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 436 | |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 437 | EXPECT_CALL( |
| 438 | *mock_exec_utils_, |
| 439 | DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", location_provider_jar_})), |
| 440 | Not(Contains(HasSubstr("--profile-file-fd="))), |
| 441 | Contains("--compiler-filter=verify")))) |
| 442 | .WillOnce(Return(0)); |
| 443 | EXPECT_CALL( |
| 444 | *mock_exec_utils_, |
| 445 | DoExecAndReturnCode(AllOf(Contains(Concatenate({"--dex-file=", services_jar_})), |
| 446 | Not(Contains(HasSubstr("--profile-file-fd="))), |
| 447 | Contains("--compiler-filter=verify")))) |
| 448 | .WillOnce(Return(0)); |
| 449 | EXPECT_EQ( |
| 450 | odrefresh_->Compile(*metrics_, |
| 451 | CompilationOptions{ |
| 452 | .system_server_jars_to_compile = odrefresh_->AllSystemServerJars(), |
| 453 | }), |
| 454 | ExitCode::kCompilationSuccess); |
Orion Hodson | 4c3ade6 | 2021-02-10 14:07:10 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 457 | TEST_F(OdRefreshTest, OutputFilesAndIsa) { |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 458 | EXPECT_CALL( |
| 459 | *mock_exec_utils_, |
| 460 | DoExecAndReturnCode(AllOf(Contains("--instruction-set=x86_64"), |
| 461 | Contains(HasSubstr("--image-fd=")), |
| 462 | Contains(HasSubstr("--output-vdex-fd=")), |
| 463 | Contains(HasSubstr("--oat-fd="))))) |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 464 | .WillOnce(Return(0)); |
| 465 | |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 466 | EXPECT_CALL( |
| 467 | *mock_exec_utils_, |
| 468 | DoExecAndReturnCode(AllOf(Contains("--instruction-set=x86_64"), |
| 469 | Contains(HasSubstr("--app-image-fd=")), |
| 470 | Contains(HasSubstr("--output-vdex-fd=")), |
| 471 | Contains(HasSubstr("--oat-fd="))))) |
| 472 | .Times(odrefresh_->AllSystemServerJars().size()) |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 473 | .WillRepeatedly(Return(0)); |
| 474 | |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 475 | EXPECT_EQ( |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 476 | odrefresh_->Compile(*metrics_, |
| 477 | CompilationOptions{ |
| 478 | .compile_boot_classpath_for_isas = {InstructionSet::kX86_64}, |
| 479 | .system_server_jars_to_compile = odrefresh_->AllSystemServerJars(), |
| 480 | }), |
Jiakai Zhang | 83c38e2 | 2021-11-03 20:54:55 +0000 | [diff] [blame] | 481 | ExitCode::kCompilationSuccess); |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 484 | TEST_F(OdRefreshTest, CompileChoosesBootImage_OnData) { |
| 485 | // Boot image is on /data. |
| 486 | OdrArtifacts artifacts = OdrArtifacts::ForBootImage(dalvik_cache_dir_ + "/x86_64/boot.art"); |
| 487 | auto file1 = ScopedCreateEmptyFile(artifacts.ImagePath()); |
| 488 | auto file2 = ScopedCreateEmptyFile(artifacts.VdexPath()); |
| 489 | auto file3 = ScopedCreateEmptyFile(artifacts.OatPath()); |
Victor Hsieh | 73c4f79 | 2021-10-01 18:13:52 +0000 | [diff] [blame] | 490 | |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 491 | EXPECT_CALL( |
| 492 | *mock_exec_utils_, |
| 493 | DoExecAndReturnCode(AllOf( |
| 494 | Contains(Concatenate({"--boot-image=", dalvik_cache_dir_, "/boot.art"})), |
| 495 | Contains(FlagContains("-Xbootclasspathimagefds:", FdOf(artifacts.ImagePath()))), |
| 496 | Contains(FlagContains("-Xbootclasspathvdexfds:", FdOf(artifacts.VdexPath()))), |
| 497 | Contains(FlagContains("-Xbootclasspathoatfds:", FdOf(artifacts.OatPath())))))) |
| 498 | .Times(odrefresh_->AllSystemServerJars().size()) |
| 499 | .WillRepeatedly(Return(0)); |
| 500 | EXPECT_EQ( |
| 501 | odrefresh_->Compile(*metrics_, |
| 502 | CompilationOptions{ |
| 503 | .system_server_jars_to_compile = odrefresh_->AllSystemServerJars(), |
| 504 | }), |
| 505 | ExitCode::kCompilationSuccess); |
| 506 | } |
Jiakai Zhang | 4e6cc73 | 2021-09-17 04:46:24 +0000 | [diff] [blame] | 507 | |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 508 | TEST_F(OdRefreshTest, CompileChoosesBootImage_OnSystem) { |
| 509 | // Boot image is on /system. |
| 510 | OdrArtifacts artifacts = |
| 511 | OdrArtifacts::ForBootImage(framework_dir_ + "/x86_64/boot-framework.art"); |
| 512 | auto file1 = ScopedCreateEmptyFile(artifacts.ImagePath()); |
| 513 | auto file2 = ScopedCreateEmptyFile(artifacts.VdexPath()); |
| 514 | auto file3 = ScopedCreateEmptyFile(artifacts.OatPath()); |
Jiakai Zhang | 4e6cc73 | 2021-09-17 04:46:24 +0000 | [diff] [blame] | 515 | |
Victor Hsieh | 7b79814 | 2022-01-18 15:13:14 -0800 | [diff] [blame] | 516 | // Ignore the execution for compiling the boot classpath. |
| 517 | EXPECT_CALL( |
| 518 | *mock_exec_utils_, DoExecAndReturnCode(Contains(HasSubstr("--image-fd=")))) |
| 519 | .WillRepeatedly(Return(0)); |
| 520 | EXPECT_CALL( |
| 521 | *mock_exec_utils_, |
| 522 | DoExecAndReturnCode(AllOf( |
| 523 | Contains(Concatenate({"--boot-image=", |
| 524 | GetPrebuiltPrimaryBootImageDir(), "/boot.art:", |
| 525 | framework_dir_, "/boot-framework.art"})), |
| 526 | Contains(FlagContains("-Xbootclasspathimagefds:", FdOf(artifacts.ImagePath()))), |
| 527 | Contains(FlagContains("-Xbootclasspathvdexfds:", FdOf(artifacts.VdexPath()))), |
| 528 | Contains(FlagContains("-Xbootclasspathoatfds:", FdOf(artifacts.OatPath())))))) |
| 529 | .Times(odrefresh_->AllSystemServerJars().size()) |
| 530 | .WillRepeatedly(Return(0)); |
| 531 | EXPECT_EQ( |
| 532 | odrefresh_->Compile(*metrics_, |
| 533 | CompilationOptions{ |
| 534 | .system_server_jars_to_compile = odrefresh_->AllSystemServerJars(), |
| 535 | }), |
| 536 | ExitCode::kCompilationSuccess); |
Jiakai Zhang | 4e6cc73 | 2021-09-17 04:46:24 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Orion Hodson | 4c3ade6 | 2021-02-10 14:07:10 +0000 | [diff] [blame] | 539 | } // namespace odrefresh |
| 540 | } // namespace art |