| 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. |
| Vladimir Marko | a64c1ad | 2021-03-08 14:27:05 +0000 | [diff] [blame] | 68 | enum class Flavor { |
| David Srbecky | e81f10a | 2019-07-04 10:00:12 +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. |
| Nicolas Geoffray | b0c6cb5 | 2020-04-20 15:12:42 +0100 | [diff] [blame] | 74 | std::string GetExecutableFilePath(const char* name, bool is_debug, bool is_static, bool bitness) { |
| Martin Stjernholm | e58624f | 2019-09-20 15:53:40 +0100 | [diff] [blame] | 75 | std::string path = GetArtBinDir() + '/' + name; |
| Mathieu Chartier | 567dc6f | 2018-04-05 16:37:14 -0700 | [diff] [blame] | 76 | if (is_debug) { |
| Roland Levillain | fb6a5c0 | 2019-03-29 20:20:16 +0000 | [diff] [blame] | 77 | path += 'd'; |
| Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 78 | } |
| Mathieu Chartier | 567dc6f | 2018-04-05 16:37:14 -0700 | [diff] [blame] | 79 | if (is_static) { |
| Roland Levillain | fb6a5c0 | 2019-03-29 20:20:16 +0000 | [diff] [blame] | 80 | path += 's'; |
| Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 81 | } |
| Nicolas Geoffray | b0c6cb5 | 2020-04-20 15:12:42 +0100 | [diff] [blame] | 82 | if (bitness) { |
| 83 | path += Is64BitInstructionSet(kRuntimeISA) ? "64" : "32"; |
| 84 | } |
| Roland Levillain | fb6a5c0 | 2019-03-29 20:20:16 +0000 | [diff] [blame] | 85 | return path; |
| Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| Nicolas Geoffray | b0c6cb5 | 2020-04-20 15:12:42 +0100 | [diff] [blame] | 88 | std::string GetExecutableFilePath(Flavor flavor, const char* name, bool bitness) { |
| Vladimir Marko | a64c1ad | 2021-03-08 14:27:05 +0000 | [diff] [blame] | 89 | return GetExecutableFilePath(name, kIsDebugBuild, flavor == Flavor::kStatic, bitness); |
| Mathieu Chartier | 567dc6f | 2018-04-05 16:37:14 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 92 | enum Mode { |
| 93 | kModeOat, |
| Nicolas Geoffray | c75a696 | 2019-01-26 10:16:54 +0000 | [diff] [blame] | 94 | kModeCoreOat, |
| Vladimir Marko | 421087b | 2018-02-27 11:00:17 +0000 | [diff] [blame] | 95 | kModeOatWithBootImage, |
| Vladimir Marko | ffe26cc | 2019-02-26 09:51:56 +0000 | [diff] [blame] | 96 | kModeAppImage, |
| Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 97 | kModeArt, |
| 98 | kModeSymbolize, |
| 99 | }; |
| 100 | |
| 101 | // Display style. |
| 102 | enum Display { |
| 103 | kListOnly, |
| 104 | kListAndCode |
| 105 | }; |
| 106 | |
| Vladimir Marko | 421087b | 2018-02-27 11:00:17 +0000 | [diff] [blame] | 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 | |
| Mathieu Chartier | 6b689ce | 2019-07-18 14:17:47 -0700 | [diff] [blame] | 113 | void SetAppImageName(const std::string& name) { |
| 114 | app_image_name_ = name; |
| 115 | } |
| 116 | |
| Vladimir Marko | ffe26cc | 2019-02-26 09:51:56 +0000 | [diff] [blame] | 117 | std::string GetAppImageName() { |
| Mathieu Chartier | 6b689ce | 2019-07-18 14:17:47 -0700 | [diff] [blame] | 118 | if (app_image_name_.empty()) { |
| 119 | app_image_name_ = tmp_dir_ + "/" + GetAppBaseName() + ".art"; |
| 120 | } |
| 121 | return app_image_name_; |
| Vladimir Marko | ffe26cc | 2019-02-26 09:51:56 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| Vladimir Marko | 421087b | 2018-02-27 11:00:17 +0000 | [diff] [blame] | 124 | std::string GetAppOdexName() { |
| 125 | return tmp_dir_ + "/" + GetAppBaseName() + ".odex"; |
| 126 | } |
| 127 | |
| Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 128 | ::testing::AssertionResult GenerateAppOdexFile(Flavor flavor, |
| 129 | const std::vector<std::string>& args) { |
| Nicolas Geoffray | b0c6cb5 | 2020-04-20 15:12:42 +0100 | [diff] [blame] | 130 | std::string dex2oat_path = |
| 131 | GetExecutableFilePath(flavor, "dex2oat", /* bitness= */ kIsTargetBuild); |
| Vladimir Marko | 421087b | 2018-02-27 11:00:17 +0000 | [diff] [blame] | 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", |
| Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 140 | "--runtime-arg", |
| 141 | GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()), |
| 142 | "--runtime-arg", |
| 143 | GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()), |
| Vladimir Marko | 421087b | 2018-02-27 11:00:17 +0000 | [diff] [blame] | 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 | |
| Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 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); |
| Vladimir Marko | 421087b | 2018-02-27 11:00:17 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 168 | // Run the test with custom arguments. |
| Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 169 | ::testing::AssertionResult Exec(Flavor flavor, |
| 170 | Mode mode, |
| 171 | const std::vector<std::string>& args, |
| Mathieu Chartier | 6b689ce | 2019-07-18 14:17:47 -0700 | [diff] [blame] | 172 | Display display, |
| 173 | bool expect_failure = false) { |
| Nicolas Geoffray | b0c6cb5 | 2020-04-20 15:12:42 +0100 | [diff] [blame] | 174 | std::string file_path = GetExecutableFilePath(flavor, "oatdump", /* bitness= */ false); |
| Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 175 | |
| Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 176 | if (!OS::FileExists(file_path.c_str())) { |
| 177 | return ::testing::AssertionFailure() << file_path << " should be a valid file path"; |
| 178 | } |
| Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 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 { |
| Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 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:"); |
| David Srbecky | 697c47a | 2019-06-16 21:53:07 +0100 | [diff] [blame] | 194 | expected_prefixes.push_back("StackMap"); |
| Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 195 | } |
| 196 | if (mode == kModeArt) { |
| Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 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())); |
| Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 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:"); |
| Vladimir Marko | 421087b | 2018-02-27 11:00:17 +0000 | [diff] [blame] | 208 | } else if (mode == kModeOatWithBootImage) { |
| Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 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())); |
| Vladimir Marko | 421087b | 2018-02-27 11:00:17 +0000 | [diff] [blame] | 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()); |
| Vladimir Marko | ffe26cc | 2019-02-26 09:51:56 +0000 | [diff] [blame] | 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()); |
| Nicolas Geoffray | c75a696 | 2019-01-26 10:16:54 +0000 | [diff] [blame] | 229 | } else if (mode == kModeCoreOat) { |
| 230 | exec_argv.push_back("--oat-file=" + core_oat_location_); |
| David Srbecky | 0c0f302 | 2020-02-13 15:53:01 +0000 | [diff] [blame] | 231 | exec_argv.push_back("--dex-file=" + GetLibCoreDexFileNames()[0]); |
| Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 232 | } else { |
| 233 | CHECK_EQ(static_cast<size_t>(mode), static_cast<size_t>(kModeOat)); |
| Nicolas Geoffray | 3ad2c2b | 2019-01-26 00:19:38 +0000 | [diff] [blame] | 234 | exec_argv.push_back("--oat-file=" + GetAppOdexName()); |
| Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | exec_argv.insert(exec_argv.end(), args.begin(), args.end()); |
| 238 | |
| Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 239 | std::vector<bool> found(expected_prefixes.size(), false); |
| 240 | auto line_handle_fn = [&found, &expected_prefixes](const char* line, size_t line_len) { |
| 241 | if (line_len == 0) { |
| 242 | return; |
| 243 | } |
| 244 | // Check contents. |
| 245 | for (size_t i = 0; i < expected_prefixes.size(); ++i) { |
| 246 | const std::string& expected = expected_prefixes[i]; |
| 247 | if (!found[i] && |
| 248 | line_len >= expected.length() && |
| 249 | memcmp(line, expected.c_str(), expected.length()) == 0) { |
| 250 | found[i] = true; |
| 251 | } |
| 252 | } |
| 253 | }; |
| 254 | |
| 255 | static constexpr size_t kLineMax = 256; |
| 256 | char line[kLineMax] = {}; |
| 257 | size_t line_len = 0; |
| 258 | size_t total = 0; |
| 259 | bool ignore_next_line = false; |
| Andreas Gampe | 3cfc2e7 | 2018-07-16 14:10:14 -0700 | [diff] [blame] | 260 | 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] | 261 | auto line_buf_fn = [&](char* buf, size_t len) { |
| 262 | total += len; |
| 263 | |
| 264 | if (len == 0 && line_len > 0 && !ignore_next_line) { |
| 265 | // Everything done, handle leftovers. |
| 266 | line_handle_fn(line, line_len); |
| 267 | } |
| 268 | |
| Andreas Gampe | 3cfc2e7 | 2018-07-16 14:10:14 -0700 | [diff] [blame] | 269 | if (len > 0) { |
| 270 | size_t pos = error_buf.size(); |
| 271 | if (pos < MB) { |
| Andreas Gampe | 764280a | 2018-07-17 10:17:22 -0700 | [diff] [blame] | 272 | error_buf.insert(error_buf.end(), buf, buf + len); |
| Andreas Gampe | 3cfc2e7 | 2018-07-16 14:10:14 -0700 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
| Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 276 | while (len > 0) { |
| 277 | // Copy buf into the free tail of the line buffer, and move input buffer along. |
| 278 | size_t copy = std::min(kLineMax - line_len, len); |
| 279 | memcpy(&line[line_len], buf, copy); |
| 280 | buf += copy; |
| 281 | len -= copy; |
| 282 | |
| Andreas Gampe | 3cfc2e7 | 2018-07-16 14:10:14 -0700 | [diff] [blame] | 283 | // Skip spaces up to len, return count of removed spaces. Declare a lambda for reuse. |
| 284 | auto trim_space = [&line](size_t len) { |
| Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 285 | size_t spaces = 0; |
| Andreas Gampe | 3cfc2e7 | 2018-07-16 14:10:14 -0700 | [diff] [blame] | 286 | for (; spaces < len && isspace(line[spaces]); ++spaces) {} |
| Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 287 | if (spaces > 0) { |
| Andreas Gampe | 3cfc2e7 | 2018-07-16 14:10:14 -0700 | [diff] [blame] | 288 | memmove(&line[0], &line[spaces], len - spaces); |
| Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 289 | } |
| Andreas Gampe | 3cfc2e7 | 2018-07-16 14:10:14 -0700 | [diff] [blame] | 290 | return spaces; |
| Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 291 | }; |
| Andreas Gampe | 3cfc2e7 | 2018-07-16 14:10:14 -0700 | [diff] [blame] | 292 | // There can only be spaces if we freshly started a line. |
| 293 | if (line_len == 0) { |
| 294 | copy -= trim_space(copy); |
| 295 | } |
| Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 296 | |
| 297 | // Scan for newline characters. |
| 298 | size_t index = line_len; |
| 299 | line_len += copy; |
| 300 | while (index < line_len) { |
| 301 | if (line[index] == '\n') { |
| 302 | // Handle line. |
| 303 | if (!ignore_next_line) { |
| 304 | line_handle_fn(line, index); |
| 305 | } |
| 306 | // Move the rest to the front, but trim leading spaces. |
| 307 | line_len -= index + 1; |
| 308 | memmove(&line[0], &line[index + 1], line_len); |
| Andreas Gampe | 3cfc2e7 | 2018-07-16 14:10:14 -0700 | [diff] [blame] | 309 | line_len -= trim_space(line_len); |
| Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 310 | index = 0; |
| 311 | ignore_next_line = false; |
| 312 | } else { |
| 313 | index++; |
| Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 314 | } |
| 315 | } |
| Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 316 | |
| Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 317 | // Handle a full line without newline characters. Ignore the "next" line, as it is the |
| 318 | // tail end of this. |
| 319 | if (line_len == kLineMax) { |
| 320 | if (!ignore_next_line) { |
| 321 | line_handle_fn(line, kLineMax); |
| 322 | } |
| 323 | line_len = 0; |
| 324 | ignore_next_line = true; |
| Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 325 | } |
| 326 | } |
| Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | auto post_fork_fn = []() { |
| 330 | setpgid(0, 0); // Change process groups, so we don't get reaped by ProcessManager. |
| 331 | return true; // Ignore setpgid failures. |
| 332 | }; |
| 333 | |
| 334 | ForkAndExecResult res = ForkAndExec(exec_argv, post_fork_fn, line_buf_fn); |
| 335 | if (res.stage != ForkAndExecResult::kFinished) { |
| 336 | return ::testing::AssertionFailure() << strerror(errno); |
| 337 | } |
| Mathieu Chartier | 6b689ce | 2019-07-18 14:17:47 -0700 | [diff] [blame] | 338 | error_buf.push_back(0); // Make data a C string. |
| 339 | |
| Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 340 | if (!res.StandardSuccess()) { |
| Mathieu Chartier | 6b689ce | 2019-07-18 14:17:47 -0700 | [diff] [blame] | 341 | if (expect_failure && WIFEXITED(res.status_code)) { |
| 342 | // Avoid crash as valid exit. |
| 343 | return ::testing::AssertionSuccess(); |
| 344 | } |
| David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 345 | std::ostringstream cmd; |
| 346 | std::copy(exec_argv.begin(), exec_argv.end(), std::ostream_iterator<std::string>(cmd, " ")); |
| 347 | LOG(ERROR) << "Output: " << error_buf.data(); // Output first as it might be extremely long. |
| 348 | LOG(ERROR) << "Failed command: " << cmd.str(); // Useful to reproduce the failure separately. |
| 349 | return ::testing::AssertionFailure() << "Did not terminate successfully: " << res.status_code; |
| Mathieu Chartier | 6b689ce | 2019-07-18 14:17:47 -0700 | [diff] [blame] | 350 | } else if (expect_failure) { |
| 351 | return ::testing::AssertionFailure() << "Expected failure"; |
| Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 352 | } |
| 353 | |
| Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 354 | if (mode == kModeSymbolize) { |
| 355 | EXPECT_EQ(total, 0u); |
| Vladimir Marko | 421087b | 2018-02-27 11:00:17 +0000 | [diff] [blame] | 356 | } else { |
| Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 357 | EXPECT_GT(total, 0u); |
| Vladimir Marko | 421087b | 2018-02-27 11:00:17 +0000 | [diff] [blame] | 358 | } |
| Vladimir Marko | 421087b | 2018-02-27 11:00:17 +0000 | [diff] [blame] | 359 | |
| Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 360 | bool result = true; |
| 361 | std::ostringstream oss; |
| 362 | for (size_t i = 0; i < expected_prefixes.size(); ++i) { |
| 363 | if (!found[i]) { |
| 364 | oss << "Did not find prefix " << expected_prefixes[i] << std::endl; |
| 365 | result = false; |
| Mathieu Chartier | 567dc6f | 2018-04-05 16:37:14 -0700 | [diff] [blame] | 366 | } |
| 367 | } |
| Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 368 | if (!result) { |
| Andreas Gampe | 3cfc2e7 | 2018-07-16 14:10:14 -0700 | [diff] [blame] | 369 | oss << "Processed bytes " << total << ":" << std::endl; |
| Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | return result ? ::testing::AssertionSuccess() |
| Andreas Gampe | 3cfc2e7 | 2018-07-16 14:10:14 -0700 | [diff] [blame] | 373 | : (::testing::AssertionFailure() << oss.str() << error_buf.data()); |
| Mathieu Chartier | 567dc6f | 2018-04-05 16:37:14 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| Anestis Bechtsoudis | a1f56a8 | 2017-10-08 23:37:10 +0300 | [diff] [blame] | 376 | std::string tmp_dir_; |
| Mathieu Chartier | 6b689ce | 2019-07-18 14:17:47 -0700 | [diff] [blame] | 377 | std::string app_image_name_; |
| Anestis Bechtsoudis | a1f56a8 | 2017-10-08 23:37:10 +0300 | [diff] [blame] | 378 | |
| Vladimir Marko | 1352f13 | 2017-04-28 15:28:29 +0100 | [diff] [blame] | 379 | private: |
| 380 | std::string core_art_location_; |
| 381 | std::string core_oat_location_; |
| 382 | }; |
| 383 | |
| 384 | } // namespace art |
| 385 | |
| 386 | #endif // ART_OATDUMP_OATDUMP_TEST_H_ |