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 26 #include "base/file_utils.h" 27 #include "base/os.h" 28 #include "base/stl_util.h" 29 #include "base/utils.h" 30 #include "common_runtime_test.h" 31 #include "compiler_callbacks.h" 32 #include "dex/art_dex_file_loader.h" 33 #include "dex/dex_file_loader.h" 34 #include "exec_utils.h" 35 #include "gc/heap.h" 36 #include "gc/space/image_space.h" 37 #include "oat_file_assistant.h" 38 #include "runtime.h" 39 40 namespace art { 41 42 static constexpr bool kDebugArgs = false; 43 44 class Dex2oatScratchDirs { 45 public: SetUp(const std::string & android_data)46 void SetUp(const std::string& android_data) { 47 // Create a scratch directory to work from. 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"; 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 TearDown()68 void TearDown() { 69 CommonArtTest::ClearDirectory(odex_dir_.c_str()); 70 ASSERT_EQ(0, rmdir(odex_dir_.c_str())); 71 72 CommonArtTest::ClearDirectory(odex_oat_dir_.c_str()); 73 ASSERT_EQ(0, rmdir(odex_oat_dir_.c_str())); 74 75 CommonArtTest::ClearDirectory(scratch_dir_.c_str()); 76 ASSERT_EQ(0, rmdir(scratch_dir_.c_str())); 77 } 78 79 // Scratch directory, for dex and odex files (oat files will go in the 80 // dalvik cache). GetScratchDir()81 const std::string& GetScratchDir() const { return scratch_dir_; } 82 83 // Odex directory is the subdirectory in the scratch directory where odex 84 // files should be located. GetOdexDir()85 const std::string& GetOdexDir() const { return odex_dir_; } 86 87 private: 88 std::string scratch_dir_; 89 std::string odex_oat_dir_; 90 std::string odex_dir_; 91 }; 92 93 // Test class that provides some helpers to set a test up for compilation using dex2oat. 94 class Dex2oatEnvironmentTest : public Dex2oatScratchDirs, public CommonRuntimeTest { 95 public: SetUp()96 void SetUp() override { 97 CommonRuntimeTest::SetUp(); 98 Dex2oatScratchDirs::SetUp(android_data_); 99 100 const ArtDexFileLoader dex_file_loader; 101 102 // Verify the environment is as we expect 103 std::vector<uint32_t> checksums; 104 std::vector<std::string> dex_locations; 105 std::string error_msg; 106 ASSERT_TRUE(OS::FileExists(GetSystemImageFile().c_str())) 107 << "Expected pre-compiled boot image to be at: " << GetSystemImageFile(); 108 ASSERT_TRUE(OS::FileExists(GetDexSrc1().c_str())) 109 << "Expected dex file to be at: " << GetDexSrc1(); 110 ASSERT_TRUE(OS::FileExists(GetResourceOnlySrc1().c_str())) 111 << "Expected stripped dex file to be at: " << GetResourceOnlySrc1(); 112 ASSERT_FALSE( 113 dex_file_loader.GetMultiDexChecksums( 114 GetResourceOnlySrc1().c_str(), &checksums, &dex_locations, &error_msg)) 115 << "Expected stripped dex file to be stripped: " << GetResourceOnlySrc1(); 116 ASSERT_TRUE(OS::FileExists(GetDexSrc2().c_str())) 117 << "Expected dex file to be at: " << GetDexSrc2(); 118 119 // GetMultiDexSrc2 should have the same primary dex checksum as 120 // GetMultiDexSrc1, but a different secondary dex checksum. 121 static constexpr bool kVerifyChecksum = true; 122 std::vector<std::unique_ptr<const DexFile>> multi1; 123 ASSERT_TRUE(dex_file_loader.Open(GetMultiDexSrc1().c_str(), 124 GetMultiDexSrc1().c_str(), 125 /* verify= */ true, 126 kVerifyChecksum, 127 &error_msg, 128 &multi1)) << error_msg; 129 ASSERT_GT(multi1.size(), 1u); 130 131 std::vector<std::unique_ptr<const DexFile>> multi2; 132 ASSERT_TRUE(dex_file_loader.Open(GetMultiDexSrc2().c_str(), 133 GetMultiDexSrc2().c_str(), 134 /* verify= */ true, 135 kVerifyChecksum, 136 &error_msg, 137 &multi2)) << error_msg; 138 ASSERT_GT(multi2.size(), 1u); 139 140 ASSERT_EQ(multi1[0]->GetLocationChecksum(), multi2[0]->GetLocationChecksum()); 141 ASSERT_NE(multi1[1]->GetLocationChecksum(), multi2[1]->GetLocationChecksum()); 142 } 143 SetUpRuntimeOptions(RuntimeOptions * options)144 void SetUpRuntimeOptions(RuntimeOptions* options) override { 145 // options->push_back(std::make_pair("-verbose:oat", nullptr)); 146 147 // Set up the image location. 148 options->push_back(std::make_pair("-Ximage:" + GetImageLocation(), 149 nullptr)); 150 // Make sure compilercallbacks are not set so that relocation will be 151 // enabled. 152 callbacks_.reset(); 153 } 154 TearDown()155 void TearDown() override { 156 Dex2oatScratchDirs::TearDown(); 157 CommonRuntimeTest::TearDown(); 158 } 159 Copy(const std::string & src,const std::string & dst)160 static void Copy(const std::string& src, const std::string& dst) { 161 std::ifstream src_stream(src, std::ios::binary); 162 std::ofstream dst_stream(dst, std::ios::binary); 163 164 dst_stream << src_stream.rdbuf(); 165 } 166 GetDexSrc1()167 std::string GetDexSrc1() const { 168 return GetTestDexFileName("Main"); 169 } 170 171 // Returns the path to a dex file equivalent to GetDexSrc1, but with the dex 172 // file stripped. GetResourceOnlySrc1()173 std::string GetResourceOnlySrc1() const { 174 return GetTestDexFileName("MainStripped"); 175 } 176 GetMultiDexSrc1()177 std::string GetMultiDexSrc1() const { 178 return GetTestDexFileName("MultiDex"); 179 } 180 181 // Returns the path to a multidex file equivalent to GetMultiDexSrc2, but 182 // with the contents of the secondary dex file changed. GetMultiDexSrc2()183 std::string GetMultiDexSrc2() const { 184 return GetTestDexFileName("MultiDexModifiedSecondary"); 185 } 186 GetDexSrc2()187 std::string GetDexSrc2() const { 188 return GetTestDexFileName("Nested"); 189 } 190 Dex2Oat(const std::vector<std::string> & dex2oat_args,std::string * output,std::string * error_msg)191 int Dex2Oat(const std::vector<std::string>& dex2oat_args, 192 std::string* output, 193 std::string* error_msg) { 194 std::vector<std::string> argv; 195 if (!CommonRuntimeTest::StartDex2OatCommandLine(&argv, error_msg)) { 196 ::testing::AssertionFailure() << "Could not start dex2oat cmd line " << *error_msg; 197 } 198 199 Runtime* runtime = Runtime::Current(); 200 if (!runtime->IsVerificationEnabled()) { 201 argv.push_back("--compiler-filter=assume-verified"); 202 } 203 204 if (runtime->MustRelocateIfPossible()) { 205 argv.push_back("--runtime-arg"); 206 argv.push_back("-Xrelocate"); 207 } else { 208 argv.push_back("--runtime-arg"); 209 argv.push_back("-Xnorelocate"); 210 } 211 212 if (!kIsTargetBuild) { 213 argv.push_back("--host"); 214 } 215 216 argv.insert(argv.end(), dex2oat_args.begin(), dex2oat_args.end()); 217 218 // We must set --android-root. 219 const char* android_root = getenv("ANDROID_ROOT"); 220 CHECK(android_root != nullptr); 221 argv.push_back("--android-root=" + std::string(android_root)); 222 223 if (kDebugArgs) { 224 std::string all_args; 225 for (const std::string& arg : argv) { 226 all_args += arg + " "; 227 } 228 LOG(ERROR) << all_args; 229 } 230 231 // We need dex2oat to actually log things. 232 auto post_fork_fn = []() { return setenv("ANDROID_LOG_TAGS", "*:d", 1) == 0; }; 233 ForkAndExecResult res = ForkAndExec(argv, post_fork_fn, output); 234 if (res.stage != ForkAndExecResult::kFinished) { 235 *error_msg = strerror(errno); 236 ::testing::AssertionFailure() << "Failed to finish dex2oat invocation: " << *error_msg; 237 } 238 239 if (!res.StandardSuccess()) { 240 // We cannot use ASSERT_TRUE since the method returns an int and not void. 241 ::testing::AssertionFailure() << "dex2oat fork/exec failed: " << *error_msg; 242 } 243 244 return res.status_code; 245 } 246 }; 247 248 } // namespace art 249 250 #endif // ART_RUNTIME_DEX2OAT_ENVIRONMENT_TEST_H_ 251