blob: 3ead8de90518cde1eb41b8359bb2ac5f447037b1 [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) {
Vladimir Marko1352f132017-04-28 15:28:29 +010075 std::string root = GetTestAndroidRoot();
Vladimir Marko421087b2018-02-27 11:00:17 +000076 root += "/bin/";
77 root += name;
Mathieu Chartier567dc6f2018-04-05 16:37:14 -070078 if (is_debug) {
Vladimir Marko1352f132017-04-28 15:28:29 +010079 root += "d";
80 }
Mathieu Chartier567dc6f2018-04-05 16:37:14 -070081 if (is_static) {
Vladimir Marko1352f132017-04-28 15:28:29 +010082 root += "s";
83 }
84 return root;
85 }
86
Mathieu Chartier567dc6f2018-04-05 16:37:14 -070087 std::string GetExecutableFilePath(Flavor flavor, const char* name) {
88 return GetExecutableFilePath(name, kIsDebugBuild, flavor == kStatic);
89 }
90
Vladimir Marko1352f132017-04-28 15:28:29 +010091 enum Mode {
92 kModeOat,
Nicolas Geoffrayc75a6962019-01-26 10:16:54 +000093 kModeCoreOat,
Vladimir Marko421087b2018-02-27 11:00:17 +000094 kModeOatWithBootImage,
Vladimir Marko1352f132017-04-28 15:28:29 +010095 kModeArt,
96 kModeSymbolize,
97 };
98
99 // Display style.
100 enum Display {
101 kListOnly,
102 kListAndCode
103 };
104
Vladimir Marko421087b2018-02-27 11:00:17 +0000105 std::string GetAppBaseName() {
106 // Use ProfileTestMultiDex as it contains references to boot image strings
107 // that shall use different code for PIC and non-PIC.
108 return "ProfileTestMultiDex";
109 }
110
111 std::string GetAppOdexName() {
112 return tmp_dir_ + "/" + GetAppBaseName() + ".odex";
113 }
114
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700115 ::testing::AssertionResult GenerateAppOdexFile(Flavor flavor,
116 const std::vector<std::string>& args) {
Vladimir Marko421087b2018-02-27 11:00:17 +0000117 std::string dex2oat_path = GetExecutableFilePath(flavor, "dex2oat");
118 std::vector<std::string> exec_argv = {
119 dex2oat_path,
120 "--runtime-arg",
121 "-Xms64m",
122 "--runtime-arg",
123 "-Xmx512m",
124 "--runtime-arg",
125 "-Xnorelocate",
Vladimir Marko7a85e702018-12-03 18:47:23 +0000126 "--runtime-arg",
127 GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()),
128 "--runtime-arg",
129 GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()),
Vladimir Marko421087b2018-02-27 11:00:17 +0000130 "--boot-image=" + GetCoreArtLocation(),
131 "--instruction-set=" + std::string(GetInstructionSetString(kRuntimeISA)),
132 "--dex-file=" + GetTestDexFileName(GetAppBaseName().c_str()),
133 "--oat-file=" + GetAppOdexName(),
134 "--compiler-filter=speed"
135 };
136 exec_argv.insert(exec_argv.end(), args.begin(), args.end());
137
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700138 auto post_fork_fn = []() {
139 setpgid(0, 0); // Change process groups, so we don't get reaped by ProcessManager.
140 // Ignore setpgid errors.
141 return setenv("ANDROID_LOG_TAGS", "*:e", 1) == 0; // We're only interested in errors and
142 // fatal logs.
143 };
144
145 std::string error_msg;
146 ForkAndExecResult res = ForkAndExec(exec_argv, post_fork_fn, &error_msg);
147 if (res.stage != ForkAndExecResult::kFinished) {
148 return ::testing::AssertionFailure() << strerror(errno);
149 }
150 return res.StandardSuccess() ? ::testing::AssertionSuccess()
151 : (::testing::AssertionFailure() << error_msg);
Vladimir Marko421087b2018-02-27 11:00:17 +0000152 }
153
Vladimir Marko1352f132017-04-28 15:28:29 +0100154 // Run the test with custom arguments.
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700155 ::testing::AssertionResult Exec(Flavor flavor,
156 Mode mode,
157 const std::vector<std::string>& args,
158 Display display) {
Vladimir Marko421087b2018-02-27 11:00:17 +0000159 std::string file_path = GetExecutableFilePath(flavor, "oatdump");
Vladimir Marko1352f132017-04-28 15:28:29 +0100160
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700161 if (!OS::FileExists(file_path.c_str())) {
162 return ::testing::AssertionFailure() << file_path << " should be a valid file path";
163 }
Vladimir Marko1352f132017-04-28 15:28:29 +0100164
165 // ScratchFile scratch;
166 std::vector<std::string> exec_argv = { file_path };
167 std::vector<std::string> expected_prefixes;
168 if (mode == kModeSymbolize) {
169 exec_argv.push_back("--symbolize=" + core_oat_location_);
170 exec_argv.push_back("--output=" + core_oat_location_ + ".symbolize");
171 } else {
Vladimir Marko1352f132017-04-28 15:28:29 +0100172 expected_prefixes.push_back("LOCATION:");
173 expected_prefixes.push_back("MAGIC:");
174 expected_prefixes.push_back("DEX FILE COUNT:");
175 if (display == kListAndCode) {
176 // Code and dex code do not show up if list only.
177 expected_prefixes.push_back("DEX CODE:");
178 expected_prefixes.push_back("CODE:");
David Srbecky42deda82018-08-10 11:23:27 +0100179 expected_prefixes.push_back("InlineInfo");
Vladimir Marko1352f132017-04-28 15:28:29 +0100180 }
181 if (mode == kModeArt) {
Vladimir Marko91f10322018-12-07 18:04:10 +0000182 exec_argv.push_back("--runtime-arg");
183 exec_argv.push_back(GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()));
184 exec_argv.push_back("--runtime-arg");
185 exec_argv.push_back(
186 GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()));
Vladimir Marko1352f132017-04-28 15:28:29 +0100187 exec_argv.push_back("--image=" + core_art_location_);
188 exec_argv.push_back("--instruction-set=" + std::string(
189 GetInstructionSetString(kRuntimeISA)));
190 expected_prefixes.push_back("IMAGE LOCATION:");
191 expected_prefixes.push_back("IMAGE BEGIN:");
192 expected_prefixes.push_back("kDexCaches:");
Vladimir Marko421087b2018-02-27 11:00:17 +0000193 } else if (mode == kModeOatWithBootImage) {
Vladimir Marko7a85e702018-12-03 18:47:23 +0000194 exec_argv.push_back("--runtime-arg");
195 exec_argv.push_back(GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()));
196 exec_argv.push_back("--runtime-arg");
197 exec_argv.push_back(
198 GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()));
Vladimir Marko421087b2018-02-27 11:00:17 +0000199 exec_argv.push_back("--boot-image=" + GetCoreArtLocation());
200 exec_argv.push_back("--instruction-set=" + std::string(
201 GetInstructionSetString(kRuntimeISA)));
202 exec_argv.push_back("--oat-file=" + GetAppOdexName());
Nicolas Geoffrayc75a6962019-01-26 10:16:54 +0000203 } else if (mode == kModeCoreOat) {
204 exec_argv.push_back("--oat-file=" + core_oat_location_);
Vladimir Marko1352f132017-04-28 15:28:29 +0100205 } else {
206 CHECK_EQ(static_cast<size_t>(mode), static_cast<size_t>(kModeOat));
Nicolas Geoffray3ad2c2b2019-01-26 00:19:38 +0000207 exec_argv.push_back("--oat-file=" + GetAppOdexName());
Vladimir Marko1352f132017-04-28 15:28:29 +0100208 }
209 }
210 exec_argv.insert(exec_argv.end(), args.begin(), args.end());
211
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700212 std::vector<bool> found(expected_prefixes.size(), false);
213 auto line_handle_fn = [&found, &expected_prefixes](const char* line, size_t line_len) {
214 if (line_len == 0) {
215 return;
216 }
217 // Check contents.
218 for (size_t i = 0; i < expected_prefixes.size(); ++i) {
219 const std::string& expected = expected_prefixes[i];
220 if (!found[i] &&
221 line_len >= expected.length() &&
222 memcmp(line, expected.c_str(), expected.length()) == 0) {
223 found[i] = true;
224 }
225 }
226 };
227
228 static constexpr size_t kLineMax = 256;
229 char line[kLineMax] = {};
230 size_t line_len = 0;
231 size_t total = 0;
232 bool ignore_next_line = false;
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700233 std::vector<char> error_buf; // Buffer for debug output on error. Limited to 1M.
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700234 auto line_buf_fn = [&](char* buf, size_t len) {
235 total += len;
236
237 if (len == 0 && line_len > 0 && !ignore_next_line) {
238 // Everything done, handle leftovers.
239 line_handle_fn(line, line_len);
240 }
241
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700242 if (len > 0) {
243 size_t pos = error_buf.size();
244 if (pos < MB) {
Andreas Gampe764280a2018-07-17 10:17:22 -0700245 error_buf.insert(error_buf.end(), buf, buf + len);
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700246 }
247 }
248
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700249 while (len > 0) {
250 // Copy buf into the free tail of the line buffer, and move input buffer along.
251 size_t copy = std::min(kLineMax - line_len, len);
252 memcpy(&line[line_len], buf, copy);
253 buf += copy;
254 len -= copy;
255
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700256 // Skip spaces up to len, return count of removed spaces. Declare a lambda for reuse.
257 auto trim_space = [&line](size_t len) {
Vladimir Marko1352f132017-04-28 15:28:29 +0100258 size_t spaces = 0;
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700259 for (; spaces < len && isspace(line[spaces]); ++spaces) {}
Vladimir Marko1352f132017-04-28 15:28:29 +0100260 if (spaces > 0) {
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700261 memmove(&line[0], &line[spaces], len - spaces);
Vladimir Marko1352f132017-04-28 15:28:29 +0100262 }
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700263 return spaces;
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700264 };
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700265 // There can only be spaces if we freshly started a line.
266 if (line_len == 0) {
267 copy -= trim_space(copy);
268 }
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700269
270 // Scan for newline characters.
271 size_t index = line_len;
272 line_len += copy;
273 while (index < line_len) {
274 if (line[index] == '\n') {
275 // Handle line.
276 if (!ignore_next_line) {
277 line_handle_fn(line, index);
278 }
279 // Move the rest to the front, but trim leading spaces.
280 line_len -= index + 1;
281 memmove(&line[0], &line[index + 1], line_len);
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700282 line_len -= trim_space(line_len);
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700283 index = 0;
284 ignore_next_line = false;
285 } else {
286 index++;
Vladimir Marko1352f132017-04-28 15:28:29 +0100287 }
288 }
Vladimir Marko1352f132017-04-28 15:28:29 +0100289
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700290 // Handle a full line without newline characters. Ignore the "next" line, as it is the
291 // tail end of this.
292 if (line_len == kLineMax) {
293 if (!ignore_next_line) {
294 line_handle_fn(line, kLineMax);
295 }
296 line_len = 0;
297 ignore_next_line = true;
Vladimir Marko1352f132017-04-28 15:28:29 +0100298 }
299 }
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700300 };
301
302 auto post_fork_fn = []() {
303 setpgid(0, 0); // Change process groups, so we don't get reaped by ProcessManager.
304 return true; // Ignore setpgid failures.
305 };
306
307 ForkAndExecResult res = ForkAndExec(exec_argv, post_fork_fn, line_buf_fn);
308 if (res.stage != ForkAndExecResult::kFinished) {
309 return ::testing::AssertionFailure() << strerror(errno);
310 }
311 if (!res.StandardSuccess()) {
312 return ::testing::AssertionFailure() << "Did not terminate successfully: " << res.status_code;
Vladimir Marko1352f132017-04-28 15:28:29 +0100313 }
314
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700315 if (mode == kModeSymbolize) {
316 EXPECT_EQ(total, 0u);
Vladimir Marko421087b2018-02-27 11:00:17 +0000317 } else {
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700318 EXPECT_GT(total, 0u);
Vladimir Marko421087b2018-02-27 11:00:17 +0000319 }
Vladimir Marko421087b2018-02-27 11:00:17 +0000320
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700321 bool result = true;
322 std::ostringstream oss;
323 for (size_t i = 0; i < expected_prefixes.size(); ++i) {
324 if (!found[i]) {
325 oss << "Did not find prefix " << expected_prefixes[i] << std::endl;
326 result = false;
Mathieu Chartier567dc6f2018-04-05 16:37:14 -0700327 }
328 }
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700329 if (!result) {
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700330 oss << "Processed bytes " << total << ":" << std::endl;
331 error_buf.push_back(0); // Make data a C string.
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700332 }
333
334 return result ? ::testing::AssertionSuccess()
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700335 : (::testing::AssertionFailure() << oss.str() << error_buf.data());
Mathieu Chartier567dc6f2018-04-05 16:37:14 -0700336 }
337
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300338 std::string tmp_dir_;
339
Vladimir Marko1352f132017-04-28 15:28:29 +0100340 private:
341 std::string core_art_location_;
342 std::string core_oat_location_;
343};
344
345} // namespace art
346
347#endif // ART_OATDUMP_OATDUMP_TEST_H_