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" |
Andreas Gampe | 5678db5 | 2017-06-08 14:11:18 -0700 | [diff] [blame] | 27 | #include "base/stl_util.h" |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 28 | #include "common_runtime_test.h" |
| 29 | #include "compiler_callbacks.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 30 | #include "dex/dex_file_loader.h" |
David Sehr | 97c381e | 2017-02-01 15:09:58 -0800 | [diff] [blame] | 31 | #include "exec_utils.h" |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 32 | #include "gc/heap.h" |
| 33 | #include "gc/space/image_space.h" |
| 34 | #include "oat_file_assistant.h" |
| 35 | #include "os.h" |
| 36 | #include "runtime.h" |
| 37 | #include "utils.h" |
| 38 | |
| 39 | namespace art { |
| 40 | |
| 41 | // Test class that provides some helpers to set a test up for compilation using dex2oat. |
| 42 | class Dex2oatEnvironmentTest : public CommonRuntimeTest { |
| 43 | public: |
| 44 | virtual void SetUp() OVERRIDE { |
| 45 | CommonRuntimeTest::SetUp(); |
| 46 | |
| 47 | // Create a scratch directory to work from. |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 48 | |
| 49 | // Get the realpath of the android data. The oat dir should always point to real location |
| 50 | // when generating oat files in dalvik-cache. This avoids complicating the unit tests |
| 51 | // when matching the expected paths. |
| 52 | UniqueCPtr<const char[]> android_data_real(realpath(android_data_.c_str(), nullptr)); |
| 53 | ASSERT_TRUE(android_data_real != nullptr) |
| 54 | << "Could not get the realpath of the android data" << android_data_ << strerror(errno); |
| 55 | |
| 56 | scratch_dir_.assign(android_data_real.get()); |
| 57 | scratch_dir_ += "/Dex2oatEnvironmentTest"; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 58 | ASSERT_EQ(0, mkdir(scratch_dir_.c_str(), 0700)); |
| 59 | |
| 60 | // Create a subdirectory in scratch for odex files. |
| 61 | odex_oat_dir_ = scratch_dir_ + "/oat"; |
| 62 | ASSERT_EQ(0, mkdir(odex_oat_dir_.c_str(), 0700)); |
| 63 | |
| 64 | odex_dir_ = odex_oat_dir_ + "/" + std::string(GetInstructionSetString(kRuntimeISA)); |
| 65 | ASSERT_EQ(0, mkdir(odex_dir_.c_str(), 0700)); |
| 66 | |
| 67 | // Verify the environment is as we expect |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 68 | std::vector<uint32_t> checksums; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 69 | std::string error_msg; |
| 70 | ASSERT_TRUE(OS::FileExists(GetSystemImageFile().c_str())) |
| 71 | << "Expected pre-compiled boot image to be at: " << GetSystemImageFile(); |
| 72 | ASSERT_TRUE(OS::FileExists(GetDexSrc1().c_str())) |
| 73 | << "Expected dex file to be at: " << GetDexSrc1(); |
| 74 | ASSERT_TRUE(OS::FileExists(GetStrippedDexSrc1().c_str())) |
| 75 | << "Expected stripped dex file to be at: " << GetStrippedDexSrc1(); |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 76 | ASSERT_FALSE( |
| 77 | DexFileLoader::GetMultiDexChecksums(GetStrippedDexSrc1().c_str(), &checksums, &error_msg)) |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 78 | << "Expected stripped dex file to be stripped: " << GetStrippedDexSrc1(); |
| 79 | ASSERT_TRUE(OS::FileExists(GetDexSrc2().c_str())) |
| 80 | << "Expected dex file to be at: " << GetDexSrc2(); |
| 81 | |
| 82 | // GetMultiDexSrc2 should have the same primary dex checksum as |
| 83 | // GetMultiDexSrc1, but a different secondary dex checksum. |
| 84 | static constexpr bool kVerifyChecksum = true; |
| 85 | std::vector<std::unique_ptr<const DexFile>> multi1; |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 86 | ASSERT_TRUE(DexFileLoader::Open(GetMultiDexSrc1().c_str(), |
| 87 | GetMultiDexSrc1().c_str(), |
Nicolas Geoffray | 095c6c9 | 2017-10-19 13:59:55 +0100 | [diff] [blame] | 88 | /* verify */ true, |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 89 | kVerifyChecksum, |
| 90 | &error_msg, |
| 91 | &multi1)) << error_msg; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 92 | ASSERT_GT(multi1.size(), 1u); |
| 93 | |
| 94 | std::vector<std::unique_ptr<const DexFile>> multi2; |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 95 | ASSERT_TRUE(DexFileLoader::Open(GetMultiDexSrc2().c_str(), |
| 96 | GetMultiDexSrc2().c_str(), |
Nicolas Geoffray | 095c6c9 | 2017-10-19 13:59:55 +0100 | [diff] [blame] | 97 | /* verify */ true, |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 98 | kVerifyChecksum, |
| 99 | &error_msg, |
| 100 | &multi2)) << error_msg; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 101 | ASSERT_GT(multi2.size(), 1u); |
| 102 | |
| 103 | ASSERT_EQ(multi1[0]->GetLocationChecksum(), multi2[0]->GetLocationChecksum()); |
| 104 | ASSERT_NE(multi1[1]->GetLocationChecksum(), multi2[1]->GetLocationChecksum()); |
| 105 | } |
| 106 | |
| 107 | virtual void SetUpRuntimeOptions(RuntimeOptions* options) OVERRIDE { |
| 108 | // options->push_back(std::make_pair("-verbose:oat", nullptr)); |
| 109 | |
| 110 | // Set up the image location. |
| 111 | options->push_back(std::make_pair("-Ximage:" + GetImageLocation(), |
| 112 | nullptr)); |
| 113 | // Make sure compilercallbacks are not set so that relocation will be |
| 114 | // enabled. |
| 115 | callbacks_.reset(); |
| 116 | } |
| 117 | |
| 118 | virtual void TearDown() OVERRIDE { |
| 119 | ClearDirectory(odex_dir_.c_str()); |
| 120 | ASSERT_EQ(0, rmdir(odex_dir_.c_str())); |
| 121 | |
| 122 | ClearDirectory(odex_oat_dir_.c_str()); |
| 123 | ASSERT_EQ(0, rmdir(odex_oat_dir_.c_str())); |
| 124 | |
| 125 | ClearDirectory(scratch_dir_.c_str()); |
| 126 | ASSERT_EQ(0, rmdir(scratch_dir_.c_str())); |
| 127 | |
| 128 | CommonRuntimeTest::TearDown(); |
| 129 | } |
| 130 | |
| 131 | static void Copy(const std::string& src, const std::string& dst) { |
| 132 | std::ifstream src_stream(src, std::ios::binary); |
| 133 | std::ofstream dst_stream(dst, std::ios::binary); |
| 134 | |
| 135 | dst_stream << src_stream.rdbuf(); |
| 136 | } |
| 137 | |
| 138 | // Returns the directory where the pre-compiled core.art can be found. |
| 139 | // TODO: We should factor out this into common tests somewhere rather than |
| 140 | // re-hardcoding it here (This was copied originally from the elf writer |
| 141 | // test). |
| 142 | std::string GetImageDirectory() const { |
| 143 | if (IsHost()) { |
| 144 | const char* host_dir = getenv("ANDROID_HOST_OUT"); |
| 145 | CHECK(host_dir != nullptr); |
| 146 | return std::string(host_dir) + "/framework"; |
| 147 | } else { |
| 148 | return std::string("/data/art-test"); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | std::string GetImageLocation() const { |
| 153 | return GetImageDirectory() + "/core.art"; |
| 154 | } |
| 155 | |
| 156 | std::string GetSystemImageFile() const { |
| 157 | return GetImageDirectory() + "/" + GetInstructionSetString(kRuntimeISA) |
| 158 | + "/core.art"; |
| 159 | } |
| 160 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 161 | bool GetCachedImageFile(const std::string& image_location, |
| 162 | /*out*/std::string* image, |
| 163 | /*out*/std::string* error_msg) const { |
Richard Uhler | 55b58b6 | 2016-08-12 09:05:13 -0700 | [diff] [blame] | 164 | std::string cache; |
| 165 | bool have_android_data; |
| 166 | bool dalvik_cache_exists; |
| 167 | bool is_global_cache; |
| 168 | GetDalvikCache(GetInstructionSetString(kRuntimeISA), |
| 169 | true, |
| 170 | &cache, |
| 171 | &have_android_data, |
| 172 | &dalvik_cache_exists, |
| 173 | &is_global_cache); |
| 174 | if (!dalvik_cache_exists) { |
| 175 | *error_msg = "Failed to create dalvik cache"; |
| 176 | return false; |
| 177 | } |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 178 | return GetDalvikCacheFilename(image_location.c_str(), cache.c_str(), image, error_msg); |
| 179 | } |
| 180 | |
| 181 | // Returns the path to an image location whose contents differ from the |
| 182 | // image at GetImageLocation(). This is used for testing mismatched |
| 183 | // image checksums in the oat_file_assistant_tests. |
| 184 | std::string GetImageLocation2() const { |
Richard Uhler | cdcbddf | 2017-01-19 16:58:39 +0000 | [diff] [blame] | 185 | return GetImageDirectory() + "/core-interpreter.art"; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | std::string GetDexSrc1() const { |
| 189 | return GetTestDexFileName("Main"); |
| 190 | } |
| 191 | |
| 192 | // Returns the path to a dex file equivalent to GetDexSrc1, but with the dex |
| 193 | // file stripped. |
| 194 | std::string GetStrippedDexSrc1() const { |
| 195 | return GetTestDexFileName("MainStripped"); |
| 196 | } |
| 197 | |
| 198 | std::string GetMultiDexSrc1() const { |
| 199 | return GetTestDexFileName("MultiDex"); |
| 200 | } |
| 201 | |
| 202 | // Returns the path to a multidex file equivalent to GetMultiDexSrc2, but |
| 203 | // with the contents of the secondary dex file changed. |
| 204 | std::string GetMultiDexSrc2() const { |
| 205 | return GetTestDexFileName("MultiDexModifiedSecondary"); |
| 206 | } |
| 207 | |
| 208 | std::string GetDexSrc2() const { |
| 209 | return GetTestDexFileName("Nested"); |
| 210 | } |
| 211 | |
| 212 | // Scratch directory, for dex and odex files (oat files will go in the |
| 213 | // dalvik cache). |
| 214 | const std::string& GetScratchDir() const { |
| 215 | return scratch_dir_; |
| 216 | } |
| 217 | |
| 218 | // Odex directory is the subdirectory in the scratch directory where odex |
| 219 | // files should be located. |
| 220 | const std::string& GetOdexDir() const { |
| 221 | return odex_dir_; |
| 222 | } |
| 223 | |
| 224 | private: |
| 225 | std::string scratch_dir_; |
| 226 | std::string odex_oat_dir_; |
| 227 | std::string odex_dir_; |
| 228 | }; |
| 229 | |
| 230 | } // namespace art |
| 231 | |
| 232 | #endif // ART_RUNTIME_DEX2OAT_ENVIRONMENT_TEST_H_ |