blob: 4ee510130b313b3d73afa1d69f8f474e51f05d41 [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,
Vladimir Marko421087b2018-02-27 11:00:17 +000093 kModeOatWithBootImage,
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
110 std::string GetAppOdexName() {
111 return tmp_dir_ + "/" + GetAppBaseName() + ".odex";
112 }
113
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700114 ::testing::AssertionResult GenerateAppOdexFile(Flavor flavor,
115 const std::vector<std::string>& args) {
Vladimir Marko421087b2018-02-27 11:00:17 +0000116 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 Gampe38aa0b52018-07-10 23:26:55 -0700133 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 Marko421087b2018-02-27 11:00:17 +0000147 }
148
Vladimir Marko1352f132017-04-28 15:28:29 +0100149 // Run the test with custom arguments.
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700150 ::testing::AssertionResult Exec(Flavor flavor,
151 Mode mode,
152 const std::vector<std::string>& args,
153 Display display) {
Vladimir Marko421087b2018-02-27 11:00:17 +0000154 std::string file_path = GetExecutableFilePath(flavor, "oatdump");
Vladimir Marko1352f132017-04-28 15:28:29 +0100155
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700156 if (!OS::FileExists(file_path.c_str())) {
157 return ::testing::AssertionFailure() << file_path << " should be a valid file path";
158 }
Vladimir Marko1352f132017-04-28 15:28:29 +0100159
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 Marko1352f132017-04-28 15:28:29 +0100167 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 Srbecky42deda82018-08-10 11:23:27 +0100174 expected_prefixes.push_back("InlineInfo");
Vladimir Marko1352f132017-04-28 15:28:29 +0100175 }
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 Marko421087b2018-02-27 11:00:17 +0000183 } 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 Marko1352f132017-04-28 15:28:29 +0100188 } 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 Gampe38aa0b52018-07-10 23:26:55 -0700195 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 Gampe3cfc2e72018-07-16 14:10:14 -0700216 std::vector<char> error_buf; // Buffer for debug output on error. Limited to 1M.
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700217 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 Gampe3cfc2e72018-07-16 14:10:14 -0700225 if (len > 0) {
226 size_t pos = error_buf.size();
227 if (pos < MB) {
Andreas Gampe764280a2018-07-17 10:17:22 -0700228 error_buf.insert(error_buf.end(), buf, buf + len);
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700229 }
230 }
231
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700232 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 Gampe3cfc2e72018-07-16 14:10:14 -0700239 // Skip spaces up to len, return count of removed spaces. Declare a lambda for reuse.
240 auto trim_space = [&line](size_t len) {
Vladimir Marko1352f132017-04-28 15:28:29 +0100241 size_t spaces = 0;
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700242 for (; spaces < len && isspace(line[spaces]); ++spaces) {}
Vladimir Marko1352f132017-04-28 15:28:29 +0100243 if (spaces > 0) {
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700244 memmove(&line[0], &line[spaces], len - spaces);
Vladimir Marko1352f132017-04-28 15:28:29 +0100245 }
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700246 return spaces;
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700247 };
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700248 // There can only be spaces if we freshly started a line.
249 if (line_len == 0) {
250 copy -= trim_space(copy);
251 }
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700252
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 Gampe3cfc2e72018-07-16 14:10:14 -0700265 line_len -= trim_space(line_len);
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700266 index = 0;
267 ignore_next_line = false;
268 } else {
269 index++;
Vladimir Marko1352f132017-04-28 15:28:29 +0100270 }
271 }
Vladimir Marko1352f132017-04-28 15:28:29 +0100272
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700273 // 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 Marko1352f132017-04-28 15:28:29 +0100281 }
282 }
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700283 };
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 Marko1352f132017-04-28 15:28:29 +0100296 }
297
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700298 if (mode == kModeSymbolize) {
299 EXPECT_EQ(total, 0u);
Vladimir Marko421087b2018-02-27 11:00:17 +0000300 } else {
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700301 EXPECT_GT(total, 0u);
Vladimir Marko421087b2018-02-27 11:00:17 +0000302 }
Vladimir Marko421087b2018-02-27 11:00:17 +0000303
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700304 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 Chartier567dc6f2018-04-05 16:37:14 -0700310 }
311 }
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700312 if (!result) {
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700313 oss << "Processed bytes " << total << ":" << std::endl;
314 error_buf.push_back(0); // Make data a C string.
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700315 }
316
317 return result ? ::testing::AssertionSuccess()
Andreas Gampe3cfc2e72018-07-16 14:10:14 -0700318 : (::testing::AssertionFailure() << oss.str() << error_buf.data());
Mathieu Chartier567dc6f2018-04-05 16:37:14 -0700319 }
320
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300321 std::string tmp_dir_;
322
Vladimir Marko1352f132017-04-28 15:28:29 +0100323 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_