Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | #ifndef ART_RUNTIME_DEX2OAT_ENVIRONMENT_TEST_H_ |
| 18 | #define ART_RUNTIME_DEX2OAT_ENVIRONMENT_TEST_H_ |
| 19 | |
| 20 | #include <fstream> |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 26 | #include "base/file_utils.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 27 | #include "base/os.h" |
Andreas Gampe | 5678db5 | 2017-06-08 14:11:18 -0700 | [diff] [blame] | 28 | #include "base/stl_util.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 29 | #include "base/utils.h" |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 30 | #include "common_runtime_test.h" |
| 31 | #include "compiler_callbacks.h" |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 32 | #include "dex/art_dex_file_loader.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 33 | #include "dex/dex_file_loader.h" |
David Sehr | 97c381e | 2017-02-01 15:09:58 -0800 | [diff] [blame] | 34 | #include "exec_utils.h" |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 35 | #include "gc/heap.h" |
| 36 | #include "gc/space/image_space.h" |
| 37 | #include "oat_file_assistant.h" |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 38 | #include "runtime.h" |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 39 | |
| 40 | namespace art { |
| 41 | |
| 42 | // Test class that provides some helpers to set a test up for compilation using dex2oat. |
| 43 | class Dex2oatEnvironmentTest : public CommonRuntimeTest { |
| 44 | public: |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 45 | void SetUp() override { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 46 | CommonRuntimeTest::SetUp(); |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 47 | const ArtDexFileLoader dex_file_loader; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 48 | |
| 49 | // Create a scratch directory to work from. |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 50 | |
| 51 | // Get the realpath of the android data. The oat dir should always point to real location |
| 52 | // when generating oat files in dalvik-cache. This avoids complicating the unit tests |
| 53 | // when matching the expected paths. |
| 54 | UniqueCPtr<const char[]> android_data_real(realpath(android_data_.c_str(), nullptr)); |
| 55 | ASSERT_TRUE(android_data_real != nullptr) |
| 56 | << "Could not get the realpath of the android data" << android_data_ << strerror(errno); |
| 57 | |
| 58 | scratch_dir_.assign(android_data_real.get()); |
| 59 | scratch_dir_ += "/Dex2oatEnvironmentTest"; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 60 | ASSERT_EQ(0, mkdir(scratch_dir_.c_str(), 0700)); |
| 61 | |
| 62 | // Create a subdirectory in scratch for odex files. |
| 63 | odex_oat_dir_ = scratch_dir_ + "/oat"; |
| 64 | ASSERT_EQ(0, mkdir(odex_oat_dir_.c_str(), 0700)); |
| 65 | |
| 66 | odex_dir_ = odex_oat_dir_ + "/" + std::string(GetInstructionSetString(kRuntimeISA)); |
| 67 | ASSERT_EQ(0, mkdir(odex_dir_.c_str(), 0700)); |
| 68 | |
| 69 | // Verify the environment is as we expect |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 70 | std::vector<uint32_t> checksums; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 71 | std::string error_msg; |
| 72 | ASSERT_TRUE(OS::FileExists(GetSystemImageFile().c_str())) |
| 73 | << "Expected pre-compiled boot image to be at: " << GetSystemImageFile(); |
| 74 | ASSERT_TRUE(OS::FileExists(GetDexSrc1().c_str())) |
| 75 | << "Expected dex file to be at: " << GetDexSrc1(); |
| 76 | ASSERT_TRUE(OS::FileExists(GetStrippedDexSrc1().c_str())) |
| 77 | << "Expected stripped dex file to be at: " << GetStrippedDexSrc1(); |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 78 | ASSERT_FALSE( |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 79 | dex_file_loader.GetMultiDexChecksums(GetStrippedDexSrc1().c_str(), &checksums, &error_msg)) |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 80 | << "Expected stripped dex file to be stripped: " << GetStrippedDexSrc1(); |
| 81 | ASSERT_TRUE(OS::FileExists(GetDexSrc2().c_str())) |
| 82 | << "Expected dex file to be at: " << GetDexSrc2(); |
| 83 | |
| 84 | // GetMultiDexSrc2 should have the same primary dex checksum as |
| 85 | // GetMultiDexSrc1, but a different secondary dex checksum. |
| 86 | static constexpr bool kVerifyChecksum = true; |
| 87 | std::vector<std::unique_ptr<const DexFile>> multi1; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 88 | ASSERT_TRUE(dex_file_loader.Open(GetMultiDexSrc1().c_str(), |
| 89 | GetMultiDexSrc1().c_str(), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 90 | /* verify= */ true, |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 91 | kVerifyChecksum, |
| 92 | &error_msg, |
| 93 | &multi1)) << error_msg; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 94 | ASSERT_GT(multi1.size(), 1u); |
| 95 | |
| 96 | std::vector<std::unique_ptr<const DexFile>> multi2; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 97 | ASSERT_TRUE(dex_file_loader.Open(GetMultiDexSrc2().c_str(), |
| 98 | GetMultiDexSrc2().c_str(), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 99 | /* verify= */ true, |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 100 | kVerifyChecksum, |
| 101 | &error_msg, |
| 102 | &multi2)) << error_msg; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 103 | ASSERT_GT(multi2.size(), 1u); |
| 104 | |
| 105 | ASSERT_EQ(multi1[0]->GetLocationChecksum(), multi2[0]->GetLocationChecksum()); |
| 106 | ASSERT_NE(multi1[1]->GetLocationChecksum(), multi2[1]->GetLocationChecksum()); |
| 107 | } |
| 108 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 109 | void SetUpRuntimeOptions(RuntimeOptions* options) override { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 110 | // options->push_back(std::make_pair("-verbose:oat", nullptr)); |
| 111 | |
| 112 | // Set up the image location. |
| 113 | options->push_back(std::make_pair("-Ximage:" + GetImageLocation(), |
| 114 | nullptr)); |
| 115 | // Make sure compilercallbacks are not set so that relocation will be |
| 116 | // enabled. |
| 117 | callbacks_.reset(); |
| 118 | } |
| 119 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 120 | void TearDown() override { |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 121 | ClearDirectory(odex_dir_.c_str()); |
| 122 | ASSERT_EQ(0, rmdir(odex_dir_.c_str())); |
| 123 | |
| 124 | ClearDirectory(odex_oat_dir_.c_str()); |
| 125 | ASSERT_EQ(0, rmdir(odex_oat_dir_.c_str())); |
| 126 | |
| 127 | ClearDirectory(scratch_dir_.c_str()); |
| 128 | ASSERT_EQ(0, rmdir(scratch_dir_.c_str())); |
| 129 | |
| 130 | CommonRuntimeTest::TearDown(); |
| 131 | } |
| 132 | |
| 133 | static void Copy(const std::string& src, const std::string& dst) { |
| 134 | std::ifstream src_stream(src, std::ios::binary); |
| 135 | std::ofstream dst_stream(dst, std::ios::binary); |
| 136 | |
| 137 | dst_stream << src_stream.rdbuf(); |
| 138 | } |
| 139 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 140 | // Returns the path to an image location whose contents differ from the |
| 141 | // image at GetImageLocation(). This is used for testing mismatched |
| 142 | // image checksums in the oat_file_assistant_tests. |
| 143 | std::string GetImageLocation2() const { |
Richard Uhler | cdcbddf | 2017-01-19 16:58:39 +0000 | [diff] [blame] | 144 | return GetImageDirectory() + "/core-interpreter.art"; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | std::string GetDexSrc1() const { |
| 148 | return GetTestDexFileName("Main"); |
| 149 | } |
| 150 | |
| 151 | // Returns the path to a dex file equivalent to GetDexSrc1, but with the dex |
| 152 | // file stripped. |
| 153 | std::string GetStrippedDexSrc1() const { |
| 154 | return GetTestDexFileName("MainStripped"); |
| 155 | } |
| 156 | |
| 157 | std::string GetMultiDexSrc1() const { |
| 158 | return GetTestDexFileName("MultiDex"); |
| 159 | } |
| 160 | |
| 161 | // Returns the path to a multidex file equivalent to GetMultiDexSrc2, but |
| 162 | // with the contents of the secondary dex file changed. |
| 163 | std::string GetMultiDexSrc2() const { |
| 164 | return GetTestDexFileName("MultiDexModifiedSecondary"); |
| 165 | } |
| 166 | |
| 167 | std::string GetDexSrc2() const { |
| 168 | return GetTestDexFileName("Nested"); |
| 169 | } |
| 170 | |
| 171 | // Scratch directory, for dex and odex files (oat files will go in the |
| 172 | // dalvik cache). |
| 173 | const std::string& GetScratchDir() const { |
| 174 | return scratch_dir_; |
| 175 | } |
| 176 | |
| 177 | // Odex directory is the subdirectory in the scratch directory where odex |
| 178 | // files should be located. |
| 179 | const std::string& GetOdexDir() const { |
| 180 | return odex_dir_; |
| 181 | } |
| 182 | |
| 183 | private: |
| 184 | std::string scratch_dir_; |
| 185 | std::string odex_oat_dir_; |
| 186 | std::string odex_dir_; |
| 187 | }; |
| 188 | |
| 189 | } // namespace art |
| 190 | |
| 191 | #endif // ART_RUNTIME_DEX2OAT_ENVIRONMENT_TEST_H_ |