blob: 3ec5b94afac1126d6f21a858874f1982db0cb185 [file] [log] [blame]
Vladimir Marko1352f132017-04-28 15:28:29 +01001/*
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 Gampe2c30e4a2017-08-23 11:31:32 -070026#include "arch/instruction_set.h"
David Sehr891a50e2017-10-27 17:01:07 -070027#include "base/file_utils.h"
David Sehrc431b9d2018-03-02 12:01:51 -080028#include "base/os.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010029#include "base/unix_file/fd_file.h"
David Sehrc431b9d2018-03-02 12:01:51 -080030#include "base/utils.h"
Andreas Gampe2c30e4a2017-08-23 11:31:32 -070031#include "common_runtime_test.h"
32#include "exec_utils.h"
33#include "gc/heap.h"
34#include "gc/space/image_space.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010035
36#include <sys/types.h>
37#include <unistd.h>
38
39namespace art {
40
41class 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 Bechtsoudisa1f56a82017-10-08 23:37:10 +030047 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 Marko1352f132017-04-28 15:28:29 +010065 }
66
67 // Linking flavor.
Vladimir Markoa64c1ad2021-03-08 14:27:05 +000068 enum class Flavor {
David Srbeckye81f10a2019-07-04 10:00:12 +000069 kDynamic, // oatdump(d), dex2oat(d)
70 kStatic, // oatdump(d)s, dex2oat(d)s
Vladimir Marko1352f132017-04-28 15:28:29 +010071 };
72
Mathieu Chartier567dc6f2018-04-05 16:37:14 -070073 // Returns path to the oatdump/dex2oat/dexdump binary.
Nicolas Geoffrayb0c6cb52020-04-20 15:12:42 +010074 std::string GetExecutableFilePath(const char* name, bool is_debug, bool is_static, bool bitness) {
Martin Stjernholme58624f2019-09-20 15:53:40 +010075 std::string path = GetArtBinDir() + '/' + name;
Mathieu Chartier567dc6f2018-04-05 16:37:14 -070076 if (is_debug) {
Roland Levillainfb6a5c02019-03-29 20:20:16 +000077 path += 'd';
Vladimir Marko1352f132017-04-28 15:28:29 +010078 }
Mathieu Chartier567dc6f2018-04-05 16:37:14 -070079 if (is_static) {
Roland Levillainfb6a5c02019-03-29 20:20:16 +000080 path += 's';
Vladimir Marko1352f132017-04-28 15:28:29 +010081 }
Nicolas Geoffrayb0c6cb52020-04-20 15:12:42 +010082 if (bitness) {
83 path += Is64BitInstructionSet(kRuntimeISA) ? "64" : "32";
84 }
Roland Levillainfb6a5c02019-03-29 20:20:16 +000085 return path;
Vladimir Marko1352f132017-04-28 15:28:29 +010086 }
87
Nicolas Geoffrayb0c6cb52020-04-20 15:12:42 +010088 std::string GetExecutableFilePath(Flavor flavor, const char* name, bool bitness) {
Vladimir Markoa64c1ad2021-03-08 14:27:05 +000089 return GetExecutableFilePath(name, kIsDebugBuild, flavor == Flavor::kStatic, bitness);
Mathieu Chartier567dc6f2018-04-05 16:37:14 -070090 }
91
Vladimir Marko1352f132017-04-28 15:28:29 +010092 enum Mode {
93 kModeOat,
Nicolas Geoffrayc75a6962019-01-26 10:16:54 +000094 kModeCoreOat,
Vladimir Marko421087b2018-02-27 11:00:17 +000095 kModeOatWithBootImage,
Vladimir Markoffe26cc2019-02-26 09:51:56 +000096 kModeAppImage,
Vladimir Marko1352f132017-04-28 15:28:29 +010097 kModeArt,
98 kModeSymbolize,
99 };
100
101 // Display style.
102 enum Display {
103 kListOnly,
104 kListAndCode
105 };
106
Vladimir Marko421087b2018-02-27 11:00:17 +0000107 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 Chartier6b689ce2019-07-18 14:17:47 -0700113 void SetAppImageName(const std::string& name) {
114 app_image_name_ = name;
115 }
116
Vladimir Markoffe26cc2019-02-26 09:51:56 +0000117 std::string GetAppImageName() {
Mathieu Chartier6b689ce2019-07-18 14:17:47 -0700118 if (app_image_name_.empty()) {
119 app_image_name_ = tmp_dir_ + "/" + GetAppBaseName() + ".art";
120 }
121 return app_image_name_;
Vladimir Markoffe26cc2019-02-26 09:51:56 +0000122 }
123
Vladimir Marko421087b2018-02-27 11:00:17 +0000124 std::string GetAppOdexName() {
125 return tmp_dir_ + "/" + GetAppBaseName() + ".odex";
126 }
127
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700128 ::testing::AssertionResult GenerateAppOdexFile(Flavor flavor,
129 const std::vector<std::string>& args) {
Nicolas Geoffrayb0c6cb52020-04-20 15:12:42 +0100130 std::string dex2oat_path =
131 GetExecutableFilePath(flavor, "dex2oat", /* bitness= */ kIsTargetBuild);
Vladimir Marko421087b2018-02-27 11:00:17 +0000132 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 Marko7a85e702018-12-03 18:47:23 +0000140 "--runtime-arg",
141 GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()),
142 "--runtime-arg",
143 GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()),
Vladimir Marko421087b2018-02-27 11:00:17 +0000144 "--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 Gampe38aa0b52018-07-10 23:26:55 -0700152 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 Marko421087b2018-02-27 11:00:17 +0000166 }
167
Vladimir Marko1352f132017-04-28 15:28:29 +0100168 // Run the test with custom arguments.
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700169 ::testing::AssertionResult Exec(Flavor flavor,
170 Mode mode,
171 const std::vector<std::string>& args,
Mathieu Chartier6b689ce2019-07-18 14:17:47 -0700172 Display display,
173 bool expect_failure = false) {
Nicolas Geoffrayb0c6cb52020-04-20 15:12:42 +0100174 std::string file_path = GetExecutableFilePath(flavor, "oatdump", /* bitness= */ false);
Vladimir Marko1352f132017-04-28 15:28:29 +0100175
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700176 if (!OS::FileExists(file_path.c_str())) {
177 return ::testing::AssertionFailure() << file_path << " should be a valid file path";
178 }
Vladimir Marko1352f132017-04-28 15:28:29 +0100179
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 Marko1352f132017-04-28 15:28:29 +0100187 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 Srbecky697c47a2019-06-16 21:53:07 +0100194 expected_prefixes.push_back("StackMap");
Vladimir Marko1352f132017-04-28 15:28:29 +0100195 }
196 if (mode == kModeArt) {
Vladimir Marko91f10322018-12-07 18:04:10 +0000197 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 Marko1352f132017-04-28 15:28:29 +0100202 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 Marko421087b2018-02-27 11:00:17 +0000208 } else if (mode == kModeOatWithBootImage) {
Vladimir Marko7a85e702018-12-03 18:47:23 +0000209 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 Marko421087b2018-02-27 11:00:17 +0000214 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 Markoffe26cc2019-02-26 09:51:56 +0000218 } 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 Geoffrayc75a6962019-01-26 10:16:54 +0000229 } else if (mode == kModeCoreOat) {
230 exec_argv.push_back("--oat-file=" + core_oat_location_);
David Srbecky0c0f3022020-02-13 15:53:01 +0000231 exec_argv.push_back("--dex-file=" + GetLibCoreDexFileNames()[0]);
Vladimir Marko1352f132017-04-28 15:28:29 +0100232 } else {
233 CHECK_EQ(static_cast<size_t>(mode), static_cast<size_t>(kModeOat));
Nicolas Geoffray3ad2c2b2019-01-26 00:19:38 +0000234 exec_argv.push_back("--oat-file=" + GetAppOdexName());
Vladimir Marko1352f132017-04-28 15:28:29 +0100235 }
236 }
237 exec_argv.insert(exec_argv.end(), args.begin(), args.end());
238
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700239 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 Gampe3cfc2e72018-07-16 14:10:14 -0700260 std::vector<char> error_buf; // Buffer for debug output on error. Limited to 1M.
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700261 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 Gampe3cfc2e72018-07-16 14:10:14 -0700269 if (len > 0) {
270 size_t pos = error_buf.size();
271 if (pos < MB) {
Andreas Gampe764280a2018-07-17 10:17:22 -0700272 error_buf.insert(error_buf.end(), buf, buf + len);
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700273 }
274 }
275
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700276 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 Gampe3cfc2e72018-07-16 14:10:14 -0700283 // Skip spaces up to len, return count of removed spaces. Declare a lambda for reuse.
284 auto trim_space = [&line](size_t len) {
Vladimir Marko1352f132017-04-28 15:28:29 +0100285 size_t spaces = 0;
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700286 for (; spaces < len && isspace(line[spaces]); ++spaces) {}
Vladimir Marko1352f132017-04-28 15:28:29 +0100287 if (spaces > 0) {
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700288 memmove(&line[0], &line[spaces], len - spaces);
Vladimir Marko1352f132017-04-28 15:28:29 +0100289 }
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700290 return spaces;
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700291 };
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700292 // There can only be spaces if we freshly started a line.
293 if (line_len == 0) {
294 copy -= trim_space(copy);
295 }
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700296
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 Gampe3cfc2e72018-07-16 14:10:14 -0700309 line_len -= trim_space(line_len);
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700310 index = 0;
311 ignore_next_line = false;
312 } else {
313 index++;
Vladimir Marko1352f132017-04-28 15:28:29 +0100314 }
315 }
Vladimir Marko1352f132017-04-28 15:28:29 +0100316
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700317 // 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 Marko1352f132017-04-28 15:28:29 +0100325 }
326 }
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700327 };
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 Chartier6b689ce2019-07-18 14:17:47 -0700338 error_buf.push_back(0); // Make data a C string.
339
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700340 if (!res.StandardSuccess()) {
Mathieu Chartier6b689ce2019-07-18 14:17:47 -0700341 if (expect_failure && WIFEXITED(res.status_code)) {
342 // Avoid crash as valid exit.
343 return ::testing::AssertionSuccess();
344 }
David Srbecky86d6cd52020-12-02 18:13:10 +0000345 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 Chartier6b689ce2019-07-18 14:17:47 -0700350 } else if (expect_failure) {
351 return ::testing::AssertionFailure() << "Expected failure";
Vladimir Marko1352f132017-04-28 15:28:29 +0100352 }
353
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700354 if (mode == kModeSymbolize) {
355 EXPECT_EQ(total, 0u);
Vladimir Marko421087b2018-02-27 11:00:17 +0000356 } else {
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700357 EXPECT_GT(total, 0u);
Vladimir Marko421087b2018-02-27 11:00:17 +0000358 }
Vladimir Marko421087b2018-02-27 11:00:17 +0000359
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700360 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 Chartier567dc6f2018-04-05 16:37:14 -0700366 }
367 }
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700368 if (!result) {
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700369 oss << "Processed bytes " << total << ":" << std::endl;
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700370 }
371
372 return result ? ::testing::AssertionSuccess()
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700373 : (::testing::AssertionFailure() << oss.str() << error_buf.data());
Mathieu Chartier567dc6f2018-04-05 16:37:14 -0700374 }
375
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300376 std::string tmp_dir_;
Mathieu Chartier6b689ce2019-07-18 14:17:47 -0700377 std::string app_image_name_;
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300378
Vladimir Marko1352f132017-04-28 15:28:29 +0100379 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_