Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 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 | |
Andreas Gampe | 2c30e4a | 2017-08-23 11:31:32 -0700 | [diff] [blame] | 26 | #include "arch/instruction_set.h" |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 27 | #include "base/file_utils.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 28 | #include "base/os.h" |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 29 | #include "base/unix_file/fd_file.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 30 | #include "base/utils.h" |
Andreas Gampe | 2c30e4a | 2017-08-23 11:31:32 -0700 | [diff] [blame] | 31 | #include "common_runtime_test.h" |
| 32 | #include "exec_utils.h" |
| 33 | #include "gc/heap.h" |
| 34 | #include "gc/space/image_space.h" |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 35 | |
| 36 | #include <sys/types.h> |
| 37 | #include <unistd.h> |
| 38 | |
| 39 | namespace art { |
| 40 | |
| 41 | class OatDumpTest : public CommonRuntimeTest { |
| 42 | protected: |
| 43 | virtual void SetUp() { |
| 44 | CommonRuntimeTest::SetUp(); |
| 45 | core_art_location_ = GetCoreArtLocation(); |
| 46 | core_oat_location_ = GetSystemImageFilename(GetCoreOatLocation().c_str(), kRuntimeISA); |
Anestis Bechtsoudis | a1f56a8 | 2017-10-08 23:37:10 +0300 | [diff] [blame] | 47 | tmp_dir_ = GetScratchDir(); |
| 48 | } |
| 49 | |
| 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 | |
| 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; |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // Linking flavor. |
| 68 | enum Flavor { |
Vladimir Marko | 421087b | 2018-02-27 11:00:17 +0000 | [diff] [blame] | 69 | kDynamic, // oatdump(d), dex2oat(d) |
| 70 | kStatic, // oatdump(d)s, dex2oat(d)s |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 71 | }; |
| 72 | |
Mathieu Chartier | 567dc6f | 2018-04-05 16:37:14 -0700 | [diff] [blame] | 73 | // Returns path to the oatdump/dex2oat/dexdump binary. |
| 74 | std::string GetExecutableFilePath(const char* name, bool is_debug, bool is_static) { |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 75 | std::string root = GetTestAndroidRoot(); |
Vladimir Marko | 421087b | 2018-02-27 11:00:17 +0000 | [diff] [blame] | 76 | root += "/bin/"; |
| 77 | root += name; |
Mathieu Chartier | 567dc6f | 2018-04-05 16:37:14 -0700 | [diff] [blame] | 78 | if (is_debug) { |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 79 | root += "d"; |
| 80 | } |
Mathieu Chartier | 567dc6f | 2018-04-05 16:37:14 -0700 | [diff] [blame] | 81 | if (is_static) { |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 82 | root += "s"; |
| 83 | } |
| 84 | return root; |
| 85 | } |
| 86 | |
Mathieu Chartier | 567dc6f | 2018-04-05 16:37:14 -0700 | [diff] [blame] | 87 | std::string GetExecutableFilePath(Flavor flavor, const char* name) { |
| 88 | return GetExecutableFilePath(name, kIsDebugBuild, flavor == kStatic); |
| 89 | } |
| 90 | |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 91 | enum Mode { |
| 92 | kModeOat, |
Vladimir Marko | 421087b | 2018-02-27 11:00:17 +0000 | [diff] [blame] | 93 | kModeOatWithBootImage, |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 94 | kModeArt, |
| 95 | kModeSymbolize, |
| 96 | }; |
| 97 | |
| 98 | // Display style. |
| 99 | enum Display { |
| 100 | kListOnly, |
| 101 | kListAndCode |
| 102 | }; |
| 103 | |
Vladimir Marko | 421087b | 2018-02-27 11:00:17 +0000 | [diff] [blame] | 104 | std::string GetAppBaseName() { |
| 105 | // Use ProfileTestMultiDex as it contains references to boot image strings |
| 106 | // that shall use different code for PIC and non-PIC. |
| 107 | return "ProfileTestMultiDex"; |
| 108 | } |
| 109 | |
| 110 | std::string GetAppOdexName() { |
| 111 | return tmp_dir_ + "/" + GetAppBaseName() + ".odex"; |
| 112 | } |
| 113 | |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 114 | ::testing::AssertionResult GenerateAppOdexFile(Flavor flavor, |
| 115 | const std::vector<std::string>& args) { |
Vladimir Marko | 421087b | 2018-02-27 11:00:17 +0000 | [diff] [blame] | 116 | std::string dex2oat_path = GetExecutableFilePath(flavor, "dex2oat"); |
| 117 | std::vector<std::string> exec_argv = { |
| 118 | dex2oat_path, |
| 119 | "--runtime-arg", |
| 120 | "-Xms64m", |
| 121 | "--runtime-arg", |
| 122 | "-Xmx512m", |
| 123 | "--runtime-arg", |
| 124 | "-Xnorelocate", |
| 125 | "--boot-image=" + GetCoreArtLocation(), |
| 126 | "--instruction-set=" + std::string(GetInstructionSetString(kRuntimeISA)), |
| 127 | "--dex-file=" + GetTestDexFileName(GetAppBaseName().c_str()), |
| 128 | "--oat-file=" + GetAppOdexName(), |
| 129 | "--compiler-filter=speed" |
| 130 | }; |
| 131 | exec_argv.insert(exec_argv.end(), args.begin(), args.end()); |
| 132 | |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 133 | auto post_fork_fn = []() { |
| 134 | setpgid(0, 0); // Change process groups, so we don't get reaped by ProcessManager. |
| 135 | // Ignore setpgid errors. |
| 136 | return setenv("ANDROID_LOG_TAGS", "*:e", 1) == 0; // We're only interested in errors and |
| 137 | // fatal logs. |
| 138 | }; |
| 139 | |
| 140 | std::string error_msg; |
| 141 | ForkAndExecResult res = ForkAndExec(exec_argv, post_fork_fn, &error_msg); |
| 142 | if (res.stage != ForkAndExecResult::kFinished) { |
| 143 | return ::testing::AssertionFailure() << strerror(errno); |
| 144 | } |
| 145 | return res.StandardSuccess() ? ::testing::AssertionSuccess() |
| 146 | : (::testing::AssertionFailure() << error_msg); |
Vladimir Marko | 421087b | 2018-02-27 11:00:17 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 149 | // Run the test with custom arguments. |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 150 | ::testing::AssertionResult Exec(Flavor flavor, |
| 151 | Mode mode, |
| 152 | const std::vector<std::string>& args, |
| 153 | Display display) { |
Vladimir Marko | 421087b | 2018-02-27 11:00:17 +0000 | [diff] [blame] | 154 | std::string file_path = GetExecutableFilePath(flavor, "oatdump"); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 155 | |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 156 | if (!OS::FileExists(file_path.c_str())) { |
| 157 | return ::testing::AssertionFailure() << file_path << " should be a valid file path"; |
| 158 | } |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 159 | |
| 160 | // ScratchFile scratch; |
| 161 | std::vector<std::string> exec_argv = { file_path }; |
| 162 | std::vector<std::string> expected_prefixes; |
| 163 | if (mode == kModeSymbolize) { |
| 164 | exec_argv.push_back("--symbolize=" + core_oat_location_); |
| 165 | exec_argv.push_back("--output=" + core_oat_location_ + ".symbolize"); |
| 166 | } else { |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 167 | expected_prefixes.push_back("LOCATION:"); |
| 168 | expected_prefixes.push_back("MAGIC:"); |
| 169 | expected_prefixes.push_back("DEX FILE COUNT:"); |
| 170 | if (display == kListAndCode) { |
| 171 | // Code and dex code do not show up if list only. |
| 172 | expected_prefixes.push_back("DEX CODE:"); |
| 173 | expected_prefixes.push_back("CODE:"); |
David Srbecky | 42deda8 | 2018-08-10 11:23:27 +0100 | [diff] [blame] | 174 | expected_prefixes.push_back("InlineInfo"); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 175 | } |
| 176 | if (mode == kModeArt) { |
| 177 | exec_argv.push_back("--image=" + core_art_location_); |
| 178 | exec_argv.push_back("--instruction-set=" + std::string( |
| 179 | GetInstructionSetString(kRuntimeISA))); |
| 180 | expected_prefixes.push_back("IMAGE LOCATION:"); |
| 181 | expected_prefixes.push_back("IMAGE BEGIN:"); |
| 182 | expected_prefixes.push_back("kDexCaches:"); |
Vladimir Marko | 421087b | 2018-02-27 11:00:17 +0000 | [diff] [blame] | 183 | } else if (mode == kModeOatWithBootImage) { |
| 184 | exec_argv.push_back("--boot-image=" + GetCoreArtLocation()); |
| 185 | exec_argv.push_back("--instruction-set=" + std::string( |
| 186 | GetInstructionSetString(kRuntimeISA))); |
| 187 | exec_argv.push_back("--oat-file=" + GetAppOdexName()); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 188 | } else { |
| 189 | CHECK_EQ(static_cast<size_t>(mode), static_cast<size_t>(kModeOat)); |
| 190 | exec_argv.push_back("--oat-file=" + core_oat_location_); |
| 191 | } |
| 192 | } |
| 193 | exec_argv.insert(exec_argv.end(), args.begin(), args.end()); |
| 194 | |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 195 | std::vector<bool> found(expected_prefixes.size(), false); |
| 196 | auto line_handle_fn = [&found, &expected_prefixes](const char* line, size_t line_len) { |
| 197 | if (line_len == 0) { |
| 198 | return; |
| 199 | } |
| 200 | // Check contents. |
| 201 | for (size_t i = 0; i < expected_prefixes.size(); ++i) { |
| 202 | const std::string& expected = expected_prefixes[i]; |
| 203 | if (!found[i] && |
| 204 | line_len >= expected.length() && |
| 205 | memcmp(line, expected.c_str(), expected.length()) == 0) { |
| 206 | found[i] = true; |
| 207 | } |
| 208 | } |
| 209 | }; |
| 210 | |
| 211 | static constexpr size_t kLineMax = 256; |
| 212 | char line[kLineMax] = {}; |
| 213 | size_t line_len = 0; |
| 214 | size_t total = 0; |
| 215 | bool ignore_next_line = false; |
Andreas Gampe | 3cfc2e7 | 2018-07-16 14:10:14 -0700 | [diff] [blame] | 216 | std::vector<char> error_buf; // Buffer for debug output on error. Limited to 1M. |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 217 | auto line_buf_fn = [&](char* buf, size_t len) { |
| 218 | total += len; |
| 219 | |
| 220 | if (len == 0 && line_len > 0 && !ignore_next_line) { |
| 221 | // Everything done, handle leftovers. |
| 222 | line_handle_fn(line, line_len); |
| 223 | } |
| 224 | |
Andreas Gampe | 3cfc2e7 | 2018-07-16 14:10:14 -0700 | [diff] [blame] | 225 | if (len > 0) { |
| 226 | size_t pos = error_buf.size(); |
| 227 | if (pos < MB) { |
Andreas Gampe | 764280a | 2018-07-17 10:17:22 -0700 | [diff] [blame] | 228 | error_buf.insert(error_buf.end(), buf, buf + len); |
Andreas Gampe | 3cfc2e7 | 2018-07-16 14:10:14 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 232 | while (len > 0) { |
| 233 | // Copy buf into the free tail of the line buffer, and move input buffer along. |
| 234 | size_t copy = std::min(kLineMax - line_len, len); |
| 235 | memcpy(&line[line_len], buf, copy); |
| 236 | buf += copy; |
| 237 | len -= copy; |
| 238 | |
Andreas Gampe | 3cfc2e7 | 2018-07-16 14:10:14 -0700 | [diff] [blame] | 239 | // Skip spaces up to len, return count of removed spaces. Declare a lambda for reuse. |
| 240 | auto trim_space = [&line](size_t len) { |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 241 | size_t spaces = 0; |
Andreas Gampe | 3cfc2e7 | 2018-07-16 14:10:14 -0700 | [diff] [blame] | 242 | for (; spaces < len && isspace(line[spaces]); ++spaces) {} |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 243 | if (spaces > 0) { |
Andreas Gampe | 3cfc2e7 | 2018-07-16 14:10:14 -0700 | [diff] [blame] | 244 | memmove(&line[0], &line[spaces], len - spaces); |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 245 | } |
Andreas Gampe | 3cfc2e7 | 2018-07-16 14:10:14 -0700 | [diff] [blame] | 246 | return spaces; |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 247 | }; |
Andreas Gampe | 3cfc2e7 | 2018-07-16 14:10:14 -0700 | [diff] [blame] | 248 | // There can only be spaces if we freshly started a line. |
| 249 | if (line_len == 0) { |
| 250 | copy -= trim_space(copy); |
| 251 | } |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 252 | |
| 253 | // Scan for newline characters. |
| 254 | size_t index = line_len; |
| 255 | line_len += copy; |
| 256 | while (index < line_len) { |
| 257 | if (line[index] == '\n') { |
| 258 | // Handle line. |
| 259 | if (!ignore_next_line) { |
| 260 | line_handle_fn(line, index); |
| 261 | } |
| 262 | // Move the rest to the front, but trim leading spaces. |
| 263 | line_len -= index + 1; |
| 264 | memmove(&line[0], &line[index + 1], line_len); |
Andreas Gampe | 3cfc2e7 | 2018-07-16 14:10:14 -0700 | [diff] [blame] | 265 | line_len -= trim_space(line_len); |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 266 | index = 0; |
| 267 | ignore_next_line = false; |
| 268 | } else { |
| 269 | index++; |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 270 | } |
| 271 | } |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 272 | |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 273 | // Handle a full line without newline characters. Ignore the "next" line, as it is the |
| 274 | // tail end of this. |
| 275 | if (line_len == kLineMax) { |
| 276 | if (!ignore_next_line) { |
| 277 | line_handle_fn(line, kLineMax); |
| 278 | } |
| 279 | line_len = 0; |
| 280 | ignore_next_line = true; |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 281 | } |
| 282 | } |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 283 | }; |
| 284 | |
| 285 | auto post_fork_fn = []() { |
| 286 | setpgid(0, 0); // Change process groups, so we don't get reaped by ProcessManager. |
| 287 | return true; // Ignore setpgid failures. |
| 288 | }; |
| 289 | |
| 290 | ForkAndExecResult res = ForkAndExec(exec_argv, post_fork_fn, line_buf_fn); |
| 291 | if (res.stage != ForkAndExecResult::kFinished) { |
| 292 | return ::testing::AssertionFailure() << strerror(errno); |
| 293 | } |
| 294 | if (!res.StandardSuccess()) { |
| 295 | return ::testing::AssertionFailure() << "Did not terminate successfully: " << res.status_code; |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 296 | } |
| 297 | |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 298 | if (mode == kModeSymbolize) { |
| 299 | EXPECT_EQ(total, 0u); |
Vladimir Marko | 421087b | 2018-02-27 11:00:17 +0000 | [diff] [blame] | 300 | } else { |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 301 | EXPECT_GT(total, 0u); |
Vladimir Marko | 421087b | 2018-02-27 11:00:17 +0000 | [diff] [blame] | 302 | } |
Vladimir Marko | 421087b | 2018-02-27 11:00:17 +0000 | [diff] [blame] | 303 | |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 304 | bool result = true; |
| 305 | std::ostringstream oss; |
| 306 | for (size_t i = 0; i < expected_prefixes.size(); ++i) { |
| 307 | if (!found[i]) { |
| 308 | oss << "Did not find prefix " << expected_prefixes[i] << std::endl; |
| 309 | result = false; |
Mathieu Chartier | 567dc6f | 2018-04-05 16:37:14 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 312 | if (!result) { |
Andreas Gampe | 3cfc2e7 | 2018-07-16 14:10:14 -0700 | [diff] [blame] | 313 | oss << "Processed bytes " << total << ":" << std::endl; |
| 314 | error_buf.push_back(0); // Make data a C string. |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | return result ? ::testing::AssertionSuccess() |
Andreas Gampe | 3cfc2e7 | 2018-07-16 14:10:14 -0700 | [diff] [blame] | 318 | : (::testing::AssertionFailure() << oss.str() << error_buf.data()); |
Mathieu Chartier | 567dc6f | 2018-04-05 16:37:14 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Anestis Bechtsoudis | a1f56a8 | 2017-10-08 23:37:10 +0300 | [diff] [blame] | 321 | std::string tmp_dir_; |
| 322 | |
Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 323 | private: |
| 324 | std::string core_art_location_; |
| 325 | std::string core_oat_location_; |
| 326 | }; |
| 327 | |
| 328 | } // namespace art |
| 329 | |
| 330 | #endif // ART_OATDUMP_OATDUMP_TEST_H_ |