1 /* 2 * Copyright (C) 2015 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_OATDUMP_OATDUMP_TEST_H_ 18 #define ART_OATDUMP_OATDUMP_TEST_H_ 19 20 #include <sstream> 21 #include <string> 22 #include <vector> 23 24 #include "android-base/strings.h" 25 26 #include "arch/instruction_set.h" 27 #include "base/file_utils.h" 28 #include "base/os.h" 29 #include "base/unix_file/fd_file.h" 30 #include "base/utils.h" 31 #include "common_runtime_test.h" 32 #include "exec_utils.h" 33 #include "gc/heap.h" 34 #include "gc/space/image_space.h" 35 36 #include <sys/types.h> 37 #include <unistd.h> 38 39 namespace art { 40 41 class OatDumpTest : public CommonRuntimeTest { 42 protected: SetUp()43 virtual void SetUp() { 44 CommonRuntimeTest::SetUp(); 45 core_art_location_ = GetCoreArtLocation(); 46 core_oat_location_ = GetSystemImageFilename(GetCoreOatLocation().c_str(), kRuntimeISA); 47 tmp_dir_ = GetScratchDir(); 48 } 49 TearDown()50 virtual void TearDown() { 51 ClearDirectory(tmp_dir_.c_str(), /*recursive*/ false); 52 ASSERT_EQ(rmdir(tmp_dir_.c_str()), 0); 53 CommonRuntimeTest::TearDown(); 54 } 55 GetScratchDir()56 std::string GetScratchDir() { 57 // ANDROID_DATA needs to be set 58 CHECK_NE(static_cast<char*>(nullptr), getenv("ANDROID_DATA")); 59 std::string dir = getenv("ANDROID_DATA"); 60 dir += "/oatdump-tmp-dir-XXXXXX"; 61 if (mkdtemp(&dir[0]) == nullptr) { 62 PLOG(FATAL) << "mkdtemp(\"" << &dir[0] << "\") failed"; 63 } 64 return dir; 65 } 66 67 // Linking flavor. 68 enum Flavor { 69 kDynamic, // oatdump(d), dex2oat(d) 70 kStatic, // oatdump(d)s, dex2oat(d)s 71 }; 72 73 // Returns path to the oatdump/dex2oat/dexdump binary. GetExecutableFilePath(const char * name,bool is_debug,bool is_static,bool bitness)74 std::string GetExecutableFilePath(const char* name, bool is_debug, bool is_static, bool bitness) { 75 std::string path = GetArtBinDir() + '/' + name; 76 if (is_debug) { 77 path += 'd'; 78 } 79 if (is_static) { 80 path += 's'; 81 } 82 if (bitness) { 83 path += Is64BitInstructionSet(kRuntimeISA) ? "64" : "32"; 84 } 85 return path; 86 } 87 GetExecutableFilePath(Flavor flavor,const char * name,bool bitness)88 std::string GetExecutableFilePath(Flavor flavor, const char* name, bool bitness) { 89 return GetExecutableFilePath(name, kIsDebugBuild, flavor == kStatic, bitness); 90 } 91 92 enum Mode { 93 kModeOat, 94 kModeCoreOat, 95 kModeOatWithBootImage, 96 kModeAppImage, 97 kModeArt, 98 kModeSymbolize, 99 }; 100 101 // Display style. 102 enum Display { 103 kListOnly, 104 kListAndCode 105 }; 106 GetAppBaseName()107 std::string GetAppBaseName() { 108 // Use ProfileTestMultiDex as it contains references to boot image strings 109 // that shall use different code for PIC and non-PIC. 110 return "ProfileTestMultiDex"; 111 } 112 SetAppImageName(const std::string & name)113 void SetAppImageName(const std::string& name) { 114 app_image_name_ = name; 115 } 116 GetAppImageName()117 std::string GetAppImageName() { 118 if (app_image_name_.empty()) { 119 app_image_name_ = tmp_dir_ + "/" + GetAppBaseName() + ".art"; 120 } 121 return app_image_name_; 122 } 123 GetAppOdexName()124 std::string GetAppOdexName() { 125 return tmp_dir_ + "/" + GetAppBaseName() + ".odex"; 126 } 127 GenerateAppOdexFile(Flavor flavor,const std::vector<std::string> & args)128 ::testing::AssertionResult GenerateAppOdexFile(Flavor flavor, 129 const std::vector<std::string>& args) { 130 std::string dex2oat_path = 131 GetExecutableFilePath(flavor, "dex2oat", /* bitness= */ kIsTargetBuild); 132 std::vector<std::string> exec_argv = { 133 dex2oat_path, 134 "--runtime-arg", 135 "-Xms64m", 136 "--runtime-arg", 137 "-Xmx512m", 138 "--runtime-arg", 139 "-Xnorelocate", 140 "--runtime-arg", 141 GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()), 142 "--runtime-arg", 143 GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()), 144 "--boot-image=" + GetCoreArtLocation(), 145 "--instruction-set=" + std::string(GetInstructionSetString(kRuntimeISA)), 146 "--dex-file=" + GetTestDexFileName(GetAppBaseName().c_str()), 147 "--oat-file=" + GetAppOdexName(), 148 "--compiler-filter=speed" 149 }; 150 exec_argv.insert(exec_argv.end(), args.begin(), args.end()); 151 152 auto post_fork_fn = []() { 153 setpgid(0, 0); // Change process groups, so we don't get reaped by ProcessManager. 154 // Ignore setpgid errors. 155 return setenv("ANDROID_LOG_TAGS", "*:e", 1) == 0; // We're only interested in errors and 156 // fatal logs. 157 }; 158 159 std::string error_msg; 160 ForkAndExecResult res = ForkAndExec(exec_argv, post_fork_fn, &error_msg); 161 if (res.stage != ForkAndExecResult::kFinished) { 162 return ::testing::AssertionFailure() << strerror(errno); 163 } 164 return res.StandardSuccess() ? ::testing::AssertionSuccess() 165 : (::testing::AssertionFailure() << error_msg); 166 } 167 168 // Run the test with custom arguments. 169 ::testing::AssertionResult Exec(Flavor flavor, 170 Mode mode, 171 const std::vector<std::string>& args, 172 Display display, 173 bool expect_failure = false) { 174 std::string file_path = GetExecutableFilePath(flavor, "oatdump", /* bitness= */ false); 175 176 if (!OS::FileExists(file_path.c_str())) { 177 return ::testing::AssertionFailure() << file_path << " should be a valid file path"; 178 } 179 180 // ScratchFile scratch; 181 std::vector<std::string> exec_argv = { file_path }; 182 std::vector<std::string> expected_prefixes; 183 if (mode == kModeSymbolize) { 184 exec_argv.push_back("--symbolize=" + core_oat_location_); 185 exec_argv.push_back("--output=" + core_oat_location_ + ".symbolize"); 186 } else { 187 expected_prefixes.push_back("LOCATION:"); 188 expected_prefixes.push_back("MAGIC:"); 189 expected_prefixes.push_back("DEX FILE COUNT:"); 190 if (display == kListAndCode) { 191 // Code and dex code do not show up if list only. 192 expected_prefixes.push_back("DEX CODE:"); 193 expected_prefixes.push_back("CODE:"); 194 expected_prefixes.push_back("StackMap"); 195 } 196 if (mode == kModeArt) { 197 exec_argv.push_back("--runtime-arg"); 198 exec_argv.push_back(GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames())); 199 exec_argv.push_back("--runtime-arg"); 200 exec_argv.push_back( 201 GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations())); 202 exec_argv.push_back("--image=" + core_art_location_); 203 exec_argv.push_back("--instruction-set=" + std::string( 204 GetInstructionSetString(kRuntimeISA))); 205 expected_prefixes.push_back("IMAGE LOCATION:"); 206 expected_prefixes.push_back("IMAGE BEGIN:"); 207 expected_prefixes.push_back("kDexCaches:"); 208 } else if (mode == kModeOatWithBootImage) { 209 exec_argv.push_back("--runtime-arg"); 210 exec_argv.push_back(GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames())); 211 exec_argv.push_back("--runtime-arg"); 212 exec_argv.push_back( 213 GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations())); 214 exec_argv.push_back("--boot-image=" + GetCoreArtLocation()); 215 exec_argv.push_back("--instruction-set=" + std::string( 216 GetInstructionSetString(kRuntimeISA))); 217 exec_argv.push_back("--oat-file=" + GetAppOdexName()); 218 } else if (mode == kModeAppImage) { 219 exec_argv.push_back("--runtime-arg"); 220 exec_argv.push_back(GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames())); 221 exec_argv.push_back("--runtime-arg"); 222 exec_argv.push_back( 223 GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations())); 224 exec_argv.push_back("--image=" + GetCoreArtLocation()); 225 exec_argv.push_back("--instruction-set=" + std::string( 226 GetInstructionSetString(kRuntimeISA))); 227 exec_argv.push_back("--app-oat=" + GetAppOdexName()); 228 exec_argv.push_back("--app-image=" + GetAppImageName()); 229 } else if (mode == kModeCoreOat) { 230 exec_argv.push_back("--oat-file=" + core_oat_location_); 231 } else { 232 CHECK_EQ(static_cast<size_t>(mode), static_cast<size_t>(kModeOat)); 233 exec_argv.push_back("--oat-file=" + GetAppOdexName()); 234 } 235 } 236 exec_argv.insert(exec_argv.end(), args.begin(), args.end()); 237 238 std::vector<bool> found(expected_prefixes.size(), false); 239 auto line_handle_fn = [&found, &expected_prefixes](const char* line, size_t line_len) { 240 if (line_len == 0) { 241 return; 242 } 243 // Check contents. 244 for (size_t i = 0; i < expected_prefixes.size(); ++i) { 245 const std::string& expected = expected_prefixes[i]; 246 if (!found[i] && 247 line_len >= expected.length() && 248 memcmp(line, expected.c_str(), expected.length()) == 0) { 249 found[i] = true; 250 } 251 } 252 }; 253 254 static constexpr size_t kLineMax = 256; 255 char line[kLineMax] = {}; 256 size_t line_len = 0; 257 size_t total = 0; 258 bool ignore_next_line = false; 259 std::vector<char> error_buf; // Buffer for debug output on error. Limited to 1M. 260 auto line_buf_fn = [&](char* buf, size_t len) { 261 total += len; 262 263 if (len == 0 && line_len > 0 && !ignore_next_line) { 264 // Everything done, handle leftovers. 265 line_handle_fn(line, line_len); 266 } 267 268 if (len > 0) { 269 size_t pos = error_buf.size(); 270 if (pos < MB) { 271 error_buf.insert(error_buf.end(), buf, buf + len); 272 } 273 } 274 275 while (len > 0) { 276 // Copy buf into the free tail of the line buffer, and move input buffer along. 277 size_t copy = std::min(kLineMax - line_len, len); 278 memcpy(&line[line_len], buf, copy); 279 buf += copy; 280 len -= copy; 281 282 // Skip spaces up to len, return count of removed spaces. Declare a lambda for reuse. 283 auto trim_space = [&line](size_t len) { 284 size_t spaces = 0; 285 for (; spaces < len && isspace(line[spaces]); ++spaces) {} 286 if (spaces > 0) { 287 memmove(&line[0], &line[spaces], len - spaces); 288 } 289 return spaces; 290 }; 291 // There can only be spaces if we freshly started a line. 292 if (line_len == 0) { 293 copy -= trim_space(copy); 294 } 295 296 // Scan for newline characters. 297 size_t index = line_len; 298 line_len += copy; 299 while (index < line_len) { 300 if (line[index] == '\n') { 301 // Handle line. 302 if (!ignore_next_line) { 303 line_handle_fn(line, index); 304 } 305 // Move the rest to the front, but trim leading spaces. 306 line_len -= index + 1; 307 memmove(&line[0], &line[index + 1], line_len); 308 line_len -= trim_space(line_len); 309 index = 0; 310 ignore_next_line = false; 311 } else { 312 index++; 313 } 314 } 315 316 // Handle a full line without newline characters. Ignore the "next" line, as it is the 317 // tail end of this. 318 if (line_len == kLineMax) { 319 if (!ignore_next_line) { 320 line_handle_fn(line, kLineMax); 321 } 322 line_len = 0; 323 ignore_next_line = true; 324 } 325 } 326 }; 327 328 auto post_fork_fn = []() { 329 setpgid(0, 0); // Change process groups, so we don't get reaped by ProcessManager. 330 return true; // Ignore setpgid failures. 331 }; 332 333 ForkAndExecResult res = ForkAndExec(exec_argv, post_fork_fn, line_buf_fn); 334 if (res.stage != ForkAndExecResult::kFinished) { 335 return ::testing::AssertionFailure() << strerror(errno); 336 } 337 error_buf.push_back(0); // Make data a C string. 338 339 if (!res.StandardSuccess()) { 340 if (expect_failure && WIFEXITED(res.status_code)) { 341 // Avoid crash as valid exit. 342 return ::testing::AssertionSuccess(); 343 } 344 return ::testing::AssertionFailure() << "Did not terminate successfully: " << res.status_code 345 << " " << error_buf.data(); 346 } else if (expect_failure) { 347 return ::testing::AssertionFailure() << "Expected failure"; 348 } 349 350 if (mode == kModeSymbolize) { 351 EXPECT_EQ(total, 0u); 352 } else { 353 EXPECT_GT(total, 0u); 354 } 355 356 bool result = true; 357 std::ostringstream oss; 358 for (size_t i = 0; i < expected_prefixes.size(); ++i) { 359 if (!found[i]) { 360 oss << "Did not find prefix " << expected_prefixes[i] << std::endl; 361 result = false; 362 } 363 } 364 if (!result) { 365 oss << "Processed bytes " << total << ":" << std::endl; 366 } 367 368 return result ? ::testing::AssertionSuccess() 369 : (::testing::AssertionFailure() << oss.str() << error_buf.data()); 370 } 371 372 std::string tmp_dir_; 373 std::string app_image_name_; 374 375 private: 376 std::string core_art_location_; 377 std::string core_oat_location_; 378 }; 379 380 } // namespace art 381 382 #endif // ART_OATDUMP_OATDUMP_TEST_H_ 383