blob: fa416e7a68ab6d52977923f235b85e610c1c235c [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.
68 enum Flavor {
Vladimir Marko421087b2018-02-27 11:00:17 +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.
74 std::string GetExecutableFilePath(const char* name, bool is_debug, bool is_static) {
Roland Levillainfb6a5c02019-03-29 20:20:16 +000075 std::string path = GetAndroidRuntimeBinDir() + '/' + 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 }
Roland Levillainfb6a5c02019-03-29 20:20:16 +000082 return path;
Vladimir Marko1352f132017-04-28 15:28:29 +010083 }
84
Mathieu Chartier567dc6f2018-04-05 16:37:14 -070085 std::string GetExecutableFilePath(Flavor flavor, const char* name) {
86 return GetExecutableFilePath(name, kIsDebugBuild, flavor == kStatic);
87 }
88
Vladimir Marko1352f132017-04-28 15:28:29 +010089 enum Mode {
90 kModeOat,
Nicolas Geoffrayc75a6962019-01-26 10:16:54 +000091 kModeCoreOat,
Vladimir Marko421087b2018-02-27 11:00:17 +000092 kModeOatWithBootImage,
Vladimir Markoffe26cc2019-02-26 09:51:56 +000093 kModeAppImage,
Vladimir Marko1352f132017-04-28 15:28:29 +010094 kModeArt,
95 kModeSymbolize,
96 };
97
98 // Display style.
99 enum Display {
100 kListOnly,
101 kListAndCode
102 };
103
Vladimir Marko421087b2018-02-27 11:00:17 +0000104 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
Vladimir Markoffe26cc2019-02-26 09:51:56 +0000110 std::string GetAppImageName() {
111 return tmp_dir_ + "/" + GetAppBaseName() + ".art";
112 }
113
Vladimir Marko421087b2018-02-27 11:00:17 +0000114 std::string GetAppOdexName() {
115 return tmp_dir_ + "/" + GetAppBaseName() + ".odex";
116 }
117
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700118 ::testing::AssertionResult GenerateAppOdexFile(Flavor flavor,
119 const std::vector<std::string>& args) {
Vladimir Marko421087b2018-02-27 11:00:17 +0000120 std::string dex2oat_path = GetExecutableFilePath(flavor, "dex2oat");
121 std::vector<std::string> exec_argv = {
122 dex2oat_path,
123 "--runtime-arg",
124 "-Xms64m",
125 "--runtime-arg",
126 "-Xmx512m",
127 "--runtime-arg",
128 "-Xnorelocate",
Vladimir Marko7a85e702018-12-03 18:47:23 +0000129 "--runtime-arg",
130 GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()),
131 "--runtime-arg",
132 GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()),
Vladimir Marko421087b2018-02-27 11:00:17 +0000133 "--boot-image=" + GetCoreArtLocation(),
134 "--instruction-set=" + std::string(GetInstructionSetString(kRuntimeISA)),
135 "--dex-file=" + GetTestDexFileName(GetAppBaseName().c_str()),
136 "--oat-file=" + GetAppOdexName(),
137 "--compiler-filter=speed"
138 };
139 exec_argv.insert(exec_argv.end(), args.begin(), args.end());
140
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700141 auto post_fork_fn = []() {
142 setpgid(0, 0); // Change process groups, so we don't get reaped by ProcessManager.
143 // Ignore setpgid errors.
144 return setenv("ANDROID_LOG_TAGS", "*:e", 1) == 0; // We're only interested in errors and
145 // fatal logs.
146 };
147
148 std::string error_msg;
149 ForkAndExecResult res = ForkAndExec(exec_argv, post_fork_fn, &error_msg);
150 if (res.stage != ForkAndExecResult::kFinished) {
151 return ::testing::AssertionFailure() << strerror(errno);
152 }
153 return res.StandardSuccess() ? ::testing::AssertionSuccess()
154 : (::testing::AssertionFailure() << error_msg);
Vladimir Marko421087b2018-02-27 11:00:17 +0000155 }
156
Vladimir Marko1352f132017-04-28 15:28:29 +0100157 // Run the test with custom arguments.
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700158 ::testing::AssertionResult Exec(Flavor flavor,
159 Mode mode,
160 const std::vector<std::string>& args,
161 Display display) {
Vladimir Marko421087b2018-02-27 11:00:17 +0000162 std::string file_path = GetExecutableFilePath(flavor, "oatdump");
Vladimir Marko1352f132017-04-28 15:28:29 +0100163
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700164 if (!OS::FileExists(file_path.c_str())) {
165 return ::testing::AssertionFailure() << file_path << " should be a valid file path";
166 }
Vladimir Marko1352f132017-04-28 15:28:29 +0100167
168 // ScratchFile scratch;
169 std::vector<std::string> exec_argv = { file_path };
170 std::vector<std::string> expected_prefixes;
171 if (mode == kModeSymbolize) {
172 exec_argv.push_back("--symbolize=" + core_oat_location_);
173 exec_argv.push_back("--output=" + core_oat_location_ + ".symbolize");
174 } else {
Vladimir Marko1352f132017-04-28 15:28:29 +0100175 expected_prefixes.push_back("LOCATION:");
176 expected_prefixes.push_back("MAGIC:");
177 expected_prefixes.push_back("DEX FILE COUNT:");
178 if (display == kListAndCode) {
179 // Code and dex code do not show up if list only.
180 expected_prefixes.push_back("DEX CODE:");
181 expected_prefixes.push_back("CODE:");
David Srbecky42deda82018-08-10 11:23:27 +0100182 expected_prefixes.push_back("InlineInfo");
Vladimir Marko1352f132017-04-28 15:28:29 +0100183 }
184 if (mode == kModeArt) {
Vladimir Marko91f10322018-12-07 18:04:10 +0000185 exec_argv.push_back("--runtime-arg");
186 exec_argv.push_back(GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()));
187 exec_argv.push_back("--runtime-arg");
188 exec_argv.push_back(
189 GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()));
Vladimir Marko1352f132017-04-28 15:28:29 +0100190 exec_argv.push_back("--image=" + core_art_location_);
191 exec_argv.push_back("--instruction-set=" + std::string(
192 GetInstructionSetString(kRuntimeISA)));
193 expected_prefixes.push_back("IMAGE LOCATION:");
194 expected_prefixes.push_back("IMAGE BEGIN:");
195 expected_prefixes.push_back("kDexCaches:");
Vladimir Marko421087b2018-02-27 11:00:17 +0000196 } else if (mode == kModeOatWithBootImage) {
Vladimir Marko7a85e702018-12-03 18:47:23 +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 Marko421087b2018-02-27 11:00:17 +0000202 exec_argv.push_back("--boot-image=" + GetCoreArtLocation());
203 exec_argv.push_back("--instruction-set=" + std::string(
204 GetInstructionSetString(kRuntimeISA)));
205 exec_argv.push_back("--oat-file=" + GetAppOdexName());
Vladimir Markoffe26cc2019-02-26 09:51:56 +0000206 } else if (mode == kModeAppImage) {
207 exec_argv.push_back("--runtime-arg");
208 exec_argv.push_back(GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()));
209 exec_argv.push_back("--runtime-arg");
210 exec_argv.push_back(
211 GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()));
212 exec_argv.push_back("--image=" + GetCoreArtLocation());
213 exec_argv.push_back("--instruction-set=" + std::string(
214 GetInstructionSetString(kRuntimeISA)));
215 exec_argv.push_back("--app-oat=" + GetAppOdexName());
216 exec_argv.push_back("--app-image=" + GetAppImageName());
Nicolas Geoffrayc75a6962019-01-26 10:16:54 +0000217 } else if (mode == kModeCoreOat) {
218 exec_argv.push_back("--oat-file=" + core_oat_location_);
Vladimir Marko1352f132017-04-28 15:28:29 +0100219 } else {
220 CHECK_EQ(static_cast<size_t>(mode), static_cast<size_t>(kModeOat));
Nicolas Geoffray3ad2c2b2019-01-26 00:19:38 +0000221 exec_argv.push_back("--oat-file=" + GetAppOdexName());
Vladimir Marko1352f132017-04-28 15:28:29 +0100222 }
223 }
224 exec_argv.insert(exec_argv.end(), args.begin(), args.end());
225
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700226 std::vector<bool> found(expected_prefixes.size(), false);
227 auto line_handle_fn = [&found, &expected_prefixes](const char* line, size_t line_len) {
228 if (line_len == 0) {
229 return;
230 }
231 // Check contents.
232 for (size_t i = 0; i < expected_prefixes.size(); ++i) {
233 const std::string& expected = expected_prefixes[i];
234 if (!found[i] &&
235 line_len >= expected.length() &&
236 memcmp(line, expected.c_str(), expected.length()) == 0) {
237 found[i] = true;
238 }
239 }
240 };
241
242 static constexpr size_t kLineMax = 256;
243 char line[kLineMax] = {};
244 size_t line_len = 0;
245 size_t total = 0;
246 bool ignore_next_line = false;
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700247 std::vector<char> error_buf; // Buffer for debug output on error. Limited to 1M.
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700248 auto line_buf_fn = [&](char* buf, size_t len) {
249 total += len;
250
251 if (len == 0 && line_len > 0 && !ignore_next_line) {
252 // Everything done, handle leftovers.
253 line_handle_fn(line, line_len);
254 }
255
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700256 if (len > 0) {
257 size_t pos = error_buf.size();
258 if (pos < MB) {
Andreas Gampe764280a2018-07-17 10:17:22 -0700259 error_buf.insert(error_buf.end(), buf, buf + len);
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700260 }
261 }
262
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700263 while (len > 0) {
264 // Copy buf into the free tail of the line buffer, and move input buffer along.
265 size_t copy = std::min(kLineMax - line_len, len);
266 memcpy(&line[line_len], buf, copy);
267 buf += copy;
268 len -= copy;
269
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700270 // Skip spaces up to len, return count of removed spaces. Declare a lambda for reuse.
271 auto trim_space = [&line](size_t len) {
Vladimir Marko1352f132017-04-28 15:28:29 +0100272 size_t spaces = 0;
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700273 for (; spaces < len && isspace(line[spaces]); ++spaces) {}
Vladimir Marko1352f132017-04-28 15:28:29 +0100274 if (spaces > 0) {
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700275 memmove(&line[0], &line[spaces], len - spaces);
Vladimir Marko1352f132017-04-28 15:28:29 +0100276 }
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700277 return spaces;
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700278 };
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700279 // There can only be spaces if we freshly started a line.
280 if (line_len == 0) {
281 copy -= trim_space(copy);
282 }
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700283
284 // Scan for newline characters.
285 size_t index = line_len;
286 line_len += copy;
287 while (index < line_len) {
288 if (line[index] == '\n') {
289 // Handle line.
290 if (!ignore_next_line) {
291 line_handle_fn(line, index);
292 }
293 // Move the rest to the front, but trim leading spaces.
294 line_len -= index + 1;
295 memmove(&line[0], &line[index + 1], line_len);
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700296 line_len -= trim_space(line_len);
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700297 index = 0;
298 ignore_next_line = false;
299 } else {
300 index++;
Vladimir Marko1352f132017-04-28 15:28:29 +0100301 }
302 }
Vladimir Marko1352f132017-04-28 15:28:29 +0100303
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700304 // Handle a full line without newline characters. Ignore the "next" line, as it is the
305 // tail end of this.
306 if (line_len == kLineMax) {
307 if (!ignore_next_line) {
308 line_handle_fn(line, kLineMax);
309 }
310 line_len = 0;
311 ignore_next_line = true;
Vladimir Marko1352f132017-04-28 15:28:29 +0100312 }
313 }
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700314 };
315
316 auto post_fork_fn = []() {
317 setpgid(0, 0); // Change process groups, so we don't get reaped by ProcessManager.
318 return true; // Ignore setpgid failures.
319 };
320
321 ForkAndExecResult res = ForkAndExec(exec_argv, post_fork_fn, line_buf_fn);
322 if (res.stage != ForkAndExecResult::kFinished) {
323 return ::testing::AssertionFailure() << strerror(errno);
324 }
325 if (!res.StandardSuccess()) {
326 return ::testing::AssertionFailure() << "Did not terminate successfully: " << res.status_code;
Vladimir Marko1352f132017-04-28 15:28:29 +0100327 }
328
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700329 if (mode == kModeSymbolize) {
330 EXPECT_EQ(total, 0u);
Vladimir Marko421087b2018-02-27 11:00:17 +0000331 } else {
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700332 EXPECT_GT(total, 0u);
Vladimir Marko421087b2018-02-27 11:00:17 +0000333 }
Vladimir Marko421087b2018-02-27 11:00:17 +0000334
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700335 bool result = true;
336 std::ostringstream oss;
337 for (size_t i = 0; i < expected_prefixes.size(); ++i) {
338 if (!found[i]) {
339 oss << "Did not find prefix " << expected_prefixes[i] << std::endl;
340 result = false;
Mathieu Chartier567dc6f2018-04-05 16:37:14 -0700341 }
342 }
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700343 if (!result) {
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700344 oss << "Processed bytes " << total << ":" << std::endl;
345 error_buf.push_back(0); // Make data a C string.
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700346 }
347
348 return result ? ::testing::AssertionSuccess()
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700349 : (::testing::AssertionFailure() << oss.str() << error_buf.data());
Mathieu Chartier567dc6f2018-04-05 16:37:14 -0700350 }
351
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300352 std::string tmp_dir_;
353
Vladimir Marko1352f132017-04-28 15:28:29 +0100354 private:
355 std::string core_art_location_;
356 std::string core_oat_location_;
357};
358
359} // namespace art
360
361#endif // ART_OATDUMP_OATDUMP_TEST_H_