blob: 63dc476560d508bbe5dc0d2825ebc1518500437a [file] [log] [blame]
Mathieu Chartier19510f02015-05-26 14:44:35 -07001/*
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
Mathieu Chartierdc00f182016-07-14 10:10:44 -070017#include <sstream>
Mathieu Chartier19510f02015-05-26 14:44:35 -070018#include <string>
19#include <vector>
Mathieu Chartier19510f02015-05-26 14:44:35 -070020
21#include "common_runtime_test.h"
22
23#include "base/stringprintf.h"
Mathieu Chartierdc00f182016-07-14 10:10:44 -070024#include "base/unix_file/fd_file.h"
Mathieu Chartier19510f02015-05-26 14:44:35 -070025#include "runtime/arch/instruction_set.h"
26#include "runtime/gc/heap.h"
27#include "runtime/gc/space/image_space.h"
28#include "runtime/os.h"
29#include "runtime/utils.h"
30#include "utils.h"
31
32#include <sys/types.h>
33#include <unistd.h>
34
35namespace art {
36
37class OatDumpTest : public CommonRuntimeTest {
38 protected:
39 virtual void SetUp() {
40 CommonRuntimeTest::SetUp();
41 core_art_location_ = GetCoreArtLocation();
42 core_oat_location_ = GetSystemImageFilename(GetCoreOatLocation().c_str(), kRuntimeISA);
43 }
44
45 // Returns path to the oatdump binary.
46 std::string GetOatDumpFilePath() {
47 std::string root = GetTestAndroidRoot();
48 root += "/bin/oatdump";
49 if (kIsDebugBuild) {
50 root += "d";
51 }
52 return root;
53 }
54
55 enum Mode {
56 kModeOat,
57 kModeArt,
58 kModeSymbolize,
59 };
60
61 // Run the test with custom arguments.
Mathieu Chartierdc00f182016-07-14 10:10:44 -070062 bool Exec(Mode mode,
63 const std::vector<std::string>& args,
64 bool list_only,
65 std::string* error_msg) {
Mathieu Chartier19510f02015-05-26 14:44:35 -070066 std::string file_path = GetOatDumpFilePath();
67
68 EXPECT_TRUE(OS::FileExists(file_path.c_str())) << file_path << " should be a valid file path";
69
Mathieu Chartierdc00f182016-07-14 10:10:44 -070070 // ScratchFile scratch;
Mathieu Chartier19510f02015-05-26 14:44:35 -070071 std::vector<std::string> exec_argv = { file_path };
Mathieu Chartierdc00f182016-07-14 10:10:44 -070072 std::vector<std::string> expected_prefixes;
Mathieu Chartier19510f02015-05-26 14:44:35 -070073 if (mode == kModeSymbolize) {
74 exec_argv.push_back("--symbolize=" + core_oat_location_);
75 exec_argv.push_back("--output=" + core_oat_location_ + ".symbolize");
Mathieu Chartier19510f02015-05-26 14:44:35 -070076 } else {
Mathieu Chartierdc00f182016-07-14 10:10:44 -070077 expected_prefixes.push_back("Dex file data for");
78 expected_prefixes.push_back("Num string ids:");
79 expected_prefixes.push_back("Num field ids:");
80 expected_prefixes.push_back("Num method ids:");
81 expected_prefixes.push_back("LOCATION:");
82 expected_prefixes.push_back("MAGIC:");
83 expected_prefixes.push_back("DEX FILE COUNT:");
84 if (!list_only) {
85 // Code and dex code do not show up if list only.
86 expected_prefixes.push_back("DEX CODE:");
87 expected_prefixes.push_back("CODE:");
88 }
89 if (mode == kModeArt) {
90 exec_argv.push_back("--image=" + core_art_location_);
91 exec_argv.push_back("--instruction-set=" + std::string(
92 GetInstructionSetString(kRuntimeISA)));
93 expected_prefixes.push_back("IMAGE LOCATION:");
94 expected_prefixes.push_back("IMAGE BEGIN:");
95 expected_prefixes.push_back("kDexCaches:");
96 } else {
97 CHECK_EQ(static_cast<size_t>(mode), static_cast<size_t>(kModeOat));
98 exec_argv.push_back("--oat-file=" + core_oat_location_);
99 }
Mathieu Chartier19510f02015-05-26 14:44:35 -0700100 }
101 exec_argv.insert(exec_argv.end(), args.begin(), args.end());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700102
103 bool result = true;
104 // We must set --android-root.
105 int link[2];
106 if (pipe(link) == -1) {
107 return false;
108 }
109
110 const pid_t pid = fork();
111 if (pid == -1) {
112 return false;
113 }
114
115 if (pid == 0) {
116 dup2(link[1], STDOUT_FILENO);
117 close(link[0]);
118 close(link[1]);
119 bool res = ::art::Exec(exec_argv, error_msg);
120 // Delete the runtime to prevent memory leaks and please valgrind.
121 delete Runtime::Current();
122 exit(res ? 0 : 1);
123 } else {
124 close(link[1]);
125 static const size_t kLineMax = 256;
126 char line[kLineMax] = {};
127 size_t line_len = 0;
128 size_t total = 0;
129 std::vector<bool> found(expected_prefixes.size(), false);
130 while (true) {
131 while (true) {
132 size_t spaces = 0;
133 // Trim spaces at the start of the line.
134 for (; spaces < line_len && isspace(line[spaces]); ++spaces) {}
135 if (spaces > 0) {
136 line_len -= spaces;
137 memmove(&line[0], &line[spaces], line_len);
138 }
139 ssize_t bytes_read =
140 TEMP_FAILURE_RETRY(read(link[0], &line[line_len], kLineMax - line_len));
141 if (bytes_read <= 0) {
142 break;
143 }
144 line_len += bytes_read;
145 total += bytes_read;
146 }
147 if (line_len == 0) {
148 break;
149 }
150 // Check contents.
151 for (size_t i = 0; i < expected_prefixes.size(); ++i) {
152 const std::string& expected = expected_prefixes[i];
153 if (!found[i] &&
154 line_len >= expected.length() &&
155 memcmp(line, expected.c_str(), expected.length()) == 0) {
156 found[i] = true;
157 }
158 }
159 // Skip to next line.
160 size_t next_line = 0;
161 for (; next_line + 1 < line_len && line[next_line] != '\n'; ++next_line) {}
162 line_len -= next_line + 1;
163 memmove(&line[0], &line[next_line + 1], line_len);
164 }
165 if (mode == kModeSymbolize) {
166 EXPECT_EQ(total, 0u);
167 } else {
168 EXPECT_GT(total, 0u);
169 }
170 LOG(INFO) << "Processed bytes " << total;
171 close(link[0]);
172 int status = 0;
173 if (waitpid(pid, &status, 0) != -1) {
174 result = (status == 0);
175 }
176
177 for (size_t i = 0; i < expected_prefixes.size(); ++i) {
178 if (!found[i]) {
179 LOG(ERROR) << "Did not find prefix " << expected_prefixes[i];
180 result = false;
181 }
182 }
183 }
184
185 return result;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700186 }
187
188 private:
189 std::string core_art_location_;
190 std::string core_oat_location_;
191};
192
Vladimir Marko7da31702016-03-29 18:42:21 +0100193// Disable tests on arm and mips as they are taking too long to run. b/27824283.
194#if !defined(__arm__) && !defined(__mips__)
Mathieu Chartier19510f02015-05-26 14:44:35 -0700195TEST_F(OatDumpTest, TestImage) {
196 std::string error_msg;
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700197 ASSERT_TRUE(Exec(kModeArt, {}, /*list_only*/ false, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700198}
199
200TEST_F(OatDumpTest, TestOatImage) {
201 std::string error_msg;
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700202 ASSERT_TRUE(Exec(kModeOat, {}, /*list_only*/ false, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700203}
204
Mathieu Chartier19510f02015-05-26 14:44:35 -0700205TEST_F(OatDumpTest, TestNoDumpVmap) {
206 std::string error_msg;
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700207 ASSERT_TRUE(Exec(kModeArt, {"--no-dump:vmap"}, /*list_only*/ false, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700208}
209
210TEST_F(OatDumpTest, TestNoDisassemble) {
211 std::string error_msg;
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700212 ASSERT_TRUE(Exec(kModeArt, {"--no-disassemble"}, /*list_only*/ false, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700213}
214
215TEST_F(OatDumpTest, TestListClasses) {
216 std::string error_msg;
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700217 ASSERT_TRUE(Exec(kModeArt, {"--list-classes"}, /*list_only*/ true, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700218}
219
220TEST_F(OatDumpTest, TestListMethods) {
221 std::string error_msg;
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700222 ASSERT_TRUE(Exec(kModeArt, {"--list-methods"}, /*list_only*/ true, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700223}
224
225TEST_F(OatDumpTest, TestSymbolize) {
226 std::string error_msg;
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700227 ASSERT_TRUE(Exec(kModeSymbolize, {}, /*list_only*/ true, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700228}
Nicolas Geoffray973ce7c2016-03-24 09:23:04 +0000229#endif
Mathieu Chartier19510f02015-05-26 14:44:35 -0700230} // namespace art