Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 <gtest/gtest.h> |
| 18 | |
Andreas Gampe | 178c880 | 2017-11-07 12:23:07 -0800 | [diff] [blame] | 19 | #include "android-base/stringprintf.h" |
Vladimir Marko | d830263 | 2019-11-14 13:16:16 +0000 | [diff] [blame^] | 20 | #include "android-base/strings.h" |
Andreas Gampe | 178c880 | 2017-11-07 12:23:07 -0800 | [diff] [blame] | 21 | |
Vladimir Marko | 3f795d2 | 2019-05-14 13:49:35 +0100 | [diff] [blame] | 22 | #include "base/stl_util.h" |
Vladimir Marko | d830263 | 2019-11-14 13:16:16 +0000 | [diff] [blame^] | 23 | #include "class_linker.h" |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 24 | #include "dexopt_test.h" |
Andreas Gampe | 178c880 | 2017-11-07 12:23:07 -0800 | [diff] [blame] | 25 | #include "noop_compiler_callbacks.h" |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | namespace gc { |
| 29 | namespace space { |
| 30 | |
| 31 | TEST_F(DexoptTest, ValidateOatFile) { |
| 32 | std::string dex1 = GetScratchDir() + "/Dex1.jar"; |
| 33 | std::string multidex1 = GetScratchDir() + "/MultiDex1.jar"; |
| 34 | std::string dex2 = GetScratchDir() + "/Dex2.jar"; |
| 35 | std::string oat_location = GetScratchDir() + "/Oat.oat"; |
| 36 | |
| 37 | Copy(GetDexSrc1(), dex1); |
| 38 | Copy(GetMultiDexSrc1(), multidex1); |
| 39 | Copy(GetDexSrc2(), dex2); |
| 40 | |
| 41 | std::string error_msg; |
| 42 | std::vector<std::string> args; |
| 43 | args.push_back("--dex-file=" + dex1); |
| 44 | args.push_back("--dex-file=" + multidex1); |
| 45 | args.push_back("--dex-file=" + dex2); |
| 46 | args.push_back("--oat-file=" + oat_location); |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 47 | ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg; |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 48 | |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 49 | std::unique_ptr<OatFile> oat(OatFile::Open(/*zip_fd=*/ -1, |
Nicolas Geoffray | 3002509 | 2018-04-19 14:43:29 +0100 | [diff] [blame] | 50 | oat_location.c_str(), |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 51 | oat_location.c_str(), |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 52 | /*executable=*/ false, |
| 53 | /*low_4gb=*/ false, |
| 54 | /*abs_dex_location=*/ nullptr, |
| 55 | /*reservation=*/ nullptr, |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 56 | &error_msg)); |
| 57 | ASSERT_TRUE(oat != nullptr) << error_msg; |
| 58 | |
| 59 | // Originally all the dex checksums should be up to date. |
| 60 | EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg; |
| 61 | |
| 62 | // Invalidate the dex1 checksum. |
| 63 | Copy(GetDexSrc2(), dex1); |
| 64 | EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg)); |
| 65 | |
| 66 | // Restore the dex1 checksum. |
| 67 | Copy(GetDexSrc1(), dex1); |
| 68 | EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg; |
| 69 | |
| 70 | // Invalidate the non-main multidex checksum. |
| 71 | Copy(GetMultiDexSrc2(), multidex1); |
| 72 | EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg)); |
| 73 | |
| 74 | // Restore the multidex checksum. |
| 75 | Copy(GetMultiDexSrc1(), multidex1); |
| 76 | EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg; |
| 77 | |
| 78 | // Invalidate the dex2 checksum. |
| 79 | Copy(GetDexSrc1(), dex2); |
| 80 | EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg)); |
| 81 | |
| 82 | // restore the dex2 checksum. |
| 83 | Copy(GetDexSrc2(), dex2); |
| 84 | EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg; |
| 85 | |
| 86 | // Replace the multidex file with a non-multidex file. |
| 87 | Copy(GetDexSrc1(), multidex1); |
| 88 | EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg)); |
| 89 | |
| 90 | // Restore the multidex file |
| 91 | Copy(GetMultiDexSrc1(), multidex1); |
| 92 | EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg; |
| 93 | |
| 94 | // Replace dex1 with a multidex file. |
| 95 | Copy(GetMultiDexSrc1(), dex1); |
| 96 | EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg)); |
| 97 | |
| 98 | // Restore the dex1 file. |
| 99 | Copy(GetDexSrc1(), dex1); |
| 100 | EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg; |
| 101 | |
| 102 | // Remove the dex2 file. |
| 103 | EXPECT_EQ(0, unlink(dex2.c_str())); |
| 104 | EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg)); |
| 105 | |
| 106 | // Restore the dex2 file. |
| 107 | Copy(GetDexSrc2(), dex2); |
| 108 | EXPECT_TRUE(ImageSpace::ValidateOatFile(*oat, &error_msg)) << error_msg; |
| 109 | |
| 110 | // Remove the multidex file. |
| 111 | EXPECT_EQ(0, unlink(multidex1.c_str())); |
| 112 | EXPECT_FALSE(ImageSpace::ValidateOatFile(*oat, &error_msg)); |
| 113 | } |
| 114 | |
Vladimir Marko | d830263 | 2019-11-14 13:16:16 +0000 | [diff] [blame^] | 115 | TEST_F(DexoptTest, Checksums) { |
| 116 | Runtime* runtime = Runtime::Current(); |
| 117 | ASSERT_TRUE(runtime != nullptr); |
| 118 | ASSERT_FALSE(runtime->GetHeap()->GetBootImageSpaces().empty()); |
| 119 | |
| 120 | std::vector<std::string> bcp = runtime->GetBootClassPath(); |
| 121 | std::vector<std::string> bcp_locations = runtime->GetBootClassPathLocations(); |
| 122 | std::vector<const DexFile*> dex_files = runtime->GetClassLinker()->GetBootClassPath(); |
| 123 | |
| 124 | std::string error_msg; |
| 125 | auto create_and_verify = [&]() { |
| 126 | std::string checksums = gc::space::ImageSpace::GetBootClassPathChecksums( |
| 127 | ArrayRef<gc::space::ImageSpace* const>(runtime->GetHeap()->GetBootImageSpaces()), |
| 128 | ArrayRef<const DexFile* const>(dex_files)); |
| 129 | return gc::space::ImageSpace::VerifyBootClassPathChecksums( |
| 130 | checksums, |
| 131 | android::base::Join(bcp_locations, ':'), |
| 132 | runtime->GetImageLocation(), |
| 133 | ArrayRef<const std::string>(bcp_locations), |
| 134 | ArrayRef<const std::string>(bcp), |
| 135 | kRuntimeISA, |
| 136 | gc::space::ImageSpaceLoadingOrder::kSystemFirst, |
| 137 | &error_msg); |
| 138 | }; |
| 139 | |
| 140 | ASSERT_TRUE(create_and_verify()) << error_msg; |
| 141 | |
| 142 | std::vector<std::unique_ptr<const DexFile>> opened_dex_files; |
| 143 | for (const std::string& src : { GetDexSrc1(), GetDexSrc2() }) { |
| 144 | std::vector<std::unique_ptr<const DexFile>> new_dex_files; |
| 145 | const ArtDexFileLoader dex_file_loader; |
| 146 | ASSERT_TRUE(dex_file_loader.Open(src.c_str(), |
| 147 | src, |
| 148 | /*verify=*/ true, |
| 149 | /*verify_checksum=*/ false, |
| 150 | &error_msg, |
| 151 | &new_dex_files)) |
| 152 | << error_msg; |
| 153 | |
| 154 | bcp.push_back(src); |
| 155 | bcp_locations.push_back(src); |
| 156 | for (std::unique_ptr<const DexFile>& df : new_dex_files) { |
| 157 | dex_files.push_back(df.get()); |
| 158 | opened_dex_files.push_back(std::move(df)); |
| 159 | } |
| 160 | |
| 161 | ASSERT_TRUE(create_and_verify()) << error_msg; |
| 162 | } |
| 163 | } |
| 164 | |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 165 | template <bool kImage, bool kRelocate, bool kImageDex2oat> |
Andreas Gampe | 178c880 | 2017-11-07 12:23:07 -0800 | [diff] [blame] | 166 | class ImageSpaceLoadingTest : public CommonRuntimeTest { |
| 167 | protected: |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 168 | void SetUpRuntimeOptions(RuntimeOptions* options) override { |
Andreas Gampe | 178c880 | 2017-11-07 12:23:07 -0800 | [diff] [blame] | 169 | if (kImage) { |
| 170 | options->emplace_back(android::base::StringPrintf("-Ximage:%s", GetCoreArtLocation().c_str()), |
| 171 | nullptr); |
| 172 | } |
| 173 | options->emplace_back(kRelocate ? "-Xrelocate" : "-Xnorelocate", nullptr); |
Andreas Gampe | 178c880 | 2017-11-07 12:23:07 -0800 | [diff] [blame] | 174 | options->emplace_back(kImageDex2oat ? "-Ximage-dex2oat" : "-Xnoimage-dex2oat", nullptr); |
| 175 | |
| 176 | // We want to test the relocation behavior of ImageSpace. As such, don't pretend we're a |
| 177 | // compiler. |
| 178 | callbacks_.reset(); |
Vladimir Marko | 3f795d2 | 2019-05-14 13:49:35 +0100 | [diff] [blame] | 179 | |
| 180 | // Clear DEX2OATBOOTCLASSPATH environment variable used for boot image compilation. |
| 181 | // We don't want that environment variable to affect the behavior of this test. |
| 182 | CHECK(old_dex2oat_bcp_ == nullptr); |
| 183 | const char* old_dex2oat_bcp = getenv("DEX2OATBOOTCLASSPATH"); |
| 184 | if (old_dex2oat_bcp != nullptr) { |
| 185 | old_dex2oat_bcp_.reset(strdup(old_dex2oat_bcp)); |
| 186 | CHECK(old_dex2oat_bcp_ != nullptr); |
| 187 | unsetenv("DEX2OATBOOTCLASSPATH"); |
| 188 | } |
Andreas Gampe | 178c880 | 2017-11-07 12:23:07 -0800 | [diff] [blame] | 189 | } |
Vladimir Marko | 3f795d2 | 2019-05-14 13:49:35 +0100 | [diff] [blame] | 190 | |
| 191 | void TearDown() override { |
| 192 | if (old_dex2oat_bcp_ != nullptr) { |
| 193 | int result = setenv("DEX2OATBOOTCLASSPATH", old_dex2oat_bcp_.get(), /* replace */ 0); |
| 194 | CHECK_EQ(result, 0); |
| 195 | old_dex2oat_bcp_.reset(); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | private: |
| 200 | UniqueCPtr<const char[]> old_dex2oat_bcp_; |
Andreas Gampe | 178c880 | 2017-11-07 12:23:07 -0800 | [diff] [blame] | 201 | }; |
| 202 | |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 203 | using ImageSpaceDex2oatTest = ImageSpaceLoadingTest<false, true, true>; |
Andreas Gampe | 178c880 | 2017-11-07 12:23:07 -0800 | [diff] [blame] | 204 | TEST_F(ImageSpaceDex2oatTest, Test) { |
| 205 | EXPECT_FALSE(Runtime::Current()->GetHeap()->GetBootImageSpaces().empty()); |
| 206 | } |
| 207 | |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 208 | using ImageSpaceNoDex2oatTest = ImageSpaceLoadingTest<true, true, false>; |
| 209 | TEST_F(ImageSpaceNoDex2oatTest, Test) { |
Andreas Gampe | 178c880 | 2017-11-07 12:23:07 -0800 | [diff] [blame] | 210 | EXPECT_FALSE(Runtime::Current()->GetHeap()->GetBootImageSpaces().empty()); |
| 211 | } |
| 212 | |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 213 | using ImageSpaceNoRelocateNoDex2oatTest = ImageSpaceLoadingTest<true, false, false>; |
| 214 | TEST_F(ImageSpaceNoRelocateNoDex2oatTest, Test) { |
| 215 | EXPECT_FALSE(Runtime::Current()->GetHeap()->GetBootImageSpaces().empty()); |
| 216 | } |
| 217 | |
| 218 | class NoAccessAndroidDataTest : public ImageSpaceLoadingTest<false, true, true> { |
Vladimir Marko | e307002 | 2018-08-22 09:36:19 +0000 | [diff] [blame] | 219 | protected: |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 220 | void SetUpRuntimeOptions(RuntimeOptions* options) override { |
Vladimir Marko | e307002 | 2018-08-22 09:36:19 +0000 | [diff] [blame] | 221 | const char* android_data = getenv("ANDROID_DATA"); |
| 222 | CHECK(android_data != nullptr); |
| 223 | old_android_data_ = android_data; |
| 224 | bad_android_data_ = old_android_data_ + "/no-android-data"; |
| 225 | int result = setenv("ANDROID_DATA", bad_android_data_.c_str(), /* replace */ 1); |
| 226 | CHECK_EQ(result, 0) << strerror(errno); |
| 227 | result = mkdir(bad_android_data_.c_str(), /* mode */ 0700); |
| 228 | CHECK_EQ(result, 0) << strerror(errno); |
| 229 | // Create a regular file "dalvik_cache". GetDalvikCache() shall get EEXIST |
| 230 | // when trying to create a directory with the same name and creating a |
| 231 | // subdirectory for a particular architecture shall fail. |
| 232 | bad_dalvik_cache_ = bad_android_data_ + "/dalvik-cache"; |
| 233 | int fd = creat(bad_dalvik_cache_.c_str(), /* mode */ 0); |
| 234 | CHECK_NE(fd, -1) << strerror(errno); |
| 235 | result = close(fd); |
| 236 | CHECK_EQ(result, 0) << strerror(errno); |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 237 | ImageSpaceLoadingTest<false, true, true>::SetUpRuntimeOptions(options); |
Vladimir Marko | e307002 | 2018-08-22 09:36:19 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 240 | void TearDown() override { |
Vladimir Marko | e307002 | 2018-08-22 09:36:19 +0000 | [diff] [blame] | 241 | int result = unlink(bad_dalvik_cache_.c_str()); |
| 242 | CHECK_EQ(result, 0) << strerror(errno); |
| 243 | result = rmdir(bad_android_data_.c_str()); |
| 244 | CHECK_EQ(result, 0) << strerror(errno); |
| 245 | result = setenv("ANDROID_DATA", old_android_data_.c_str(), /* replace */ 1); |
| 246 | CHECK_EQ(result, 0) << strerror(errno); |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 247 | ImageSpaceLoadingTest<false, true, true>::TearDown(); |
Vladimir Marko | e307002 | 2018-08-22 09:36:19 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | private: |
| 251 | std::string old_android_data_; |
| 252 | std::string bad_android_data_; |
| 253 | std::string bad_dalvik_cache_; |
| 254 | }; |
| 255 | |
| 256 | TEST_F(NoAccessAndroidDataTest, Test) { |
| 257 | EXPECT_TRUE(Runtime::Current()->GetHeap()->GetBootImageSpaces().empty()); |
| 258 | } |
| 259 | |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 260 | } // namespace space |
| 261 | } // namespace gc |
| 262 | } // namespace art |