blob: 22db818086168ac89449f156dc58a73d9dcd08a5 [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
Roland Levillain04147ef2016-09-06 11:09:41 +010045 // Linking flavor.
46 enum Flavor {
47 kDynamic, // oatdump(d)
48 kStatic, // oatdump(d)s
49 };
50
Mathieu Chartier19510f02015-05-26 14:44:35 -070051 // Returns path to the oatdump binary.
Roland Levillain04147ef2016-09-06 11:09:41 +010052 std::string GetOatDumpFilePath(Flavor flavor) {
Mathieu Chartier19510f02015-05-26 14:44:35 -070053 std::string root = GetTestAndroidRoot();
54 root += "/bin/oatdump";
55 if (kIsDebugBuild) {
56 root += "d";
57 }
Roland Levillain04147ef2016-09-06 11:09:41 +010058 if (flavor == kStatic) {
59 root += "s";
60 }
Mathieu Chartier19510f02015-05-26 14:44:35 -070061 return root;
62 }
63
64 enum Mode {
65 kModeOat,
66 kModeArt,
67 kModeSymbolize,
68 };
69
Roland Levillain04147ef2016-09-06 11:09:41 +010070 // Display style.
71 enum Display {
72 kListOnly,
73 kListAndCode
74 };
75
Mathieu Chartier19510f02015-05-26 14:44:35 -070076 // Run the test with custom arguments.
Roland Levillain04147ef2016-09-06 11:09:41 +010077 bool Exec(Flavor flavor,
78 Mode mode,
Mathieu Chartierdc00f182016-07-14 10:10:44 -070079 const std::vector<std::string>& args,
Roland Levillain04147ef2016-09-06 11:09:41 +010080 Display display,
Mathieu Chartierdc00f182016-07-14 10:10:44 -070081 std::string* error_msg) {
Roland Levillain04147ef2016-09-06 11:09:41 +010082 std::string file_path = GetOatDumpFilePath(flavor);
Mathieu Chartier19510f02015-05-26 14:44:35 -070083
84 EXPECT_TRUE(OS::FileExists(file_path.c_str())) << file_path << " should be a valid file path";
85
Mathieu Chartierdc00f182016-07-14 10:10:44 -070086 // ScratchFile scratch;
Mathieu Chartier19510f02015-05-26 14:44:35 -070087 std::vector<std::string> exec_argv = { file_path };
Mathieu Chartierdc00f182016-07-14 10:10:44 -070088 std::vector<std::string> expected_prefixes;
Mathieu Chartier19510f02015-05-26 14:44:35 -070089 if (mode == kModeSymbolize) {
90 exec_argv.push_back("--symbolize=" + core_oat_location_);
91 exec_argv.push_back("--output=" + core_oat_location_ + ".symbolize");
Mathieu Chartier19510f02015-05-26 14:44:35 -070092 } else {
Mathieu Chartierdc00f182016-07-14 10:10:44 -070093 expected_prefixes.push_back("Dex file data for");
94 expected_prefixes.push_back("Num string ids:");
95 expected_prefixes.push_back("Num field ids:");
96 expected_prefixes.push_back("Num method ids:");
97 expected_prefixes.push_back("LOCATION:");
98 expected_prefixes.push_back("MAGIC:");
99 expected_prefixes.push_back("DEX FILE COUNT:");
Roland Levillain04147ef2016-09-06 11:09:41 +0100100 if (display == kListAndCode) {
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700101 // Code and dex code do not show up if list only.
102 expected_prefixes.push_back("DEX CODE:");
103 expected_prefixes.push_back("CODE:");
104 }
105 if (mode == kModeArt) {
106 exec_argv.push_back("--image=" + core_art_location_);
107 exec_argv.push_back("--instruction-set=" + std::string(
108 GetInstructionSetString(kRuntimeISA)));
109 expected_prefixes.push_back("IMAGE LOCATION:");
110 expected_prefixes.push_back("IMAGE BEGIN:");
111 expected_prefixes.push_back("kDexCaches:");
112 } else {
113 CHECK_EQ(static_cast<size_t>(mode), static_cast<size_t>(kModeOat));
114 exec_argv.push_back("--oat-file=" + core_oat_location_);
115 }
Mathieu Chartier19510f02015-05-26 14:44:35 -0700116 }
117 exec_argv.insert(exec_argv.end(), args.begin(), args.end());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700118
119 bool result = true;
120 // We must set --android-root.
121 int link[2];
122 if (pipe(link) == -1) {
David Sehrda820e92016-08-04 09:41:55 -0700123 *error_msg = strerror(errno);
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700124 return false;
125 }
126
127 const pid_t pid = fork();
128 if (pid == -1) {
David Sehrda820e92016-08-04 09:41:55 -0700129 *error_msg = strerror(errno);
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700130 return false;
131 }
132
133 if (pid == 0) {
134 dup2(link[1], STDOUT_FILENO);
135 close(link[0]);
136 close(link[1]);
David Sehrda820e92016-08-04 09:41:55 -0700137 // change process groups, so we don't get reaped by ProcessManager
138 setpgid(0, 0);
139 // Use execv here rather than art::Exec to avoid blocking on waitpid here.
140 std::vector<char*> argv;
141 for (size_t i = 0; i < exec_argv.size(); ++i) {
142 argv.push_back(const_cast<char*>(exec_argv[i].c_str()));
143 }
144 argv.push_back(nullptr);
145 UNUSED(execv(argv[0], &argv[0]));
146 const std::string command_line(Join(exec_argv, ' '));
147 PLOG(ERROR) << "Failed to execv(" << command_line << ")";
148 // _exit to avoid atexit handlers in child.
149 _exit(1);
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700150 } else {
151 close(link[1]);
152 static const size_t kLineMax = 256;
153 char line[kLineMax] = {};
154 size_t line_len = 0;
155 size_t total = 0;
156 std::vector<bool> found(expected_prefixes.size(), false);
157 while (true) {
158 while (true) {
159 size_t spaces = 0;
160 // Trim spaces at the start of the line.
161 for (; spaces < line_len && isspace(line[spaces]); ++spaces) {}
162 if (spaces > 0) {
163 line_len -= spaces;
164 memmove(&line[0], &line[spaces], line_len);
165 }
166 ssize_t bytes_read =
167 TEMP_FAILURE_RETRY(read(link[0], &line[line_len], kLineMax - line_len));
168 if (bytes_read <= 0) {
169 break;
170 }
171 line_len += bytes_read;
172 total += bytes_read;
173 }
174 if (line_len == 0) {
175 break;
176 }
177 // Check contents.
178 for (size_t i = 0; i < expected_prefixes.size(); ++i) {
179 const std::string& expected = expected_prefixes[i];
180 if (!found[i] &&
181 line_len >= expected.length() &&
182 memcmp(line, expected.c_str(), expected.length()) == 0) {
183 found[i] = true;
184 }
185 }
186 // Skip to next line.
187 size_t next_line = 0;
188 for (; next_line + 1 < line_len && line[next_line] != '\n'; ++next_line) {}
189 line_len -= next_line + 1;
190 memmove(&line[0], &line[next_line + 1], line_len);
191 }
192 if (mode == kModeSymbolize) {
193 EXPECT_EQ(total, 0u);
194 } else {
195 EXPECT_GT(total, 0u);
196 }
197 LOG(INFO) << "Processed bytes " << total;
198 close(link[0]);
199 int status = 0;
200 if (waitpid(pid, &status, 0) != -1) {
201 result = (status == 0);
202 }
203
204 for (size_t i = 0; i < expected_prefixes.size(); ++i) {
205 if (!found[i]) {
206 LOG(ERROR) << "Did not find prefix " << expected_prefixes[i];
207 result = false;
208 }
209 }
210 }
211
212 return result;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700213 }
214
215 private:
216 std::string core_art_location_;
217 std::string core_oat_location_;
218};
219
Vladimir Marko7da31702016-03-29 18:42:21 +0100220// Disable tests on arm and mips as they are taking too long to run. b/27824283.
221#if !defined(__arm__) && !defined(__mips__)
Mathieu Chartier19510f02015-05-26 14:44:35 -0700222TEST_F(OatDumpTest, TestImage) {
223 std::string error_msg;
Roland Levillain04147ef2016-09-06 11:09:41 +0100224 ASSERT_TRUE(Exec(kDynamic, kModeArt, {}, kListAndCode, &error_msg)) << error_msg;
225}
226TEST_F(OatDumpTest, TestImageStatic) {
227 TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS();
228 std::string error_msg;
229 ASSERT_TRUE(Exec(kStatic, kModeArt, {}, kListAndCode, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700230}
231
232TEST_F(OatDumpTest, TestOatImage) {
233 std::string error_msg;
Roland Levillain04147ef2016-09-06 11:09:41 +0100234 ASSERT_TRUE(Exec(kDynamic, kModeOat, {}, kListAndCode, &error_msg)) << error_msg;
235}
236TEST_F(OatDumpTest, TestOatImageStatic) {
237 TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS();
238 std::string error_msg;
239 ASSERT_TRUE(Exec(kStatic, kModeOat, {}, kListAndCode, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700240}
241
Mathieu Chartier19510f02015-05-26 14:44:35 -0700242TEST_F(OatDumpTest, TestNoDumpVmap) {
243 std::string error_msg;
Roland Levillain04147ef2016-09-06 11:09:41 +0100244 ASSERT_TRUE(Exec(kDynamic, kModeArt, {"--no-dump:vmap"}, kListAndCode, &error_msg)) << error_msg;
245}
246TEST_F(OatDumpTest, TestNoDumpVmapStatic) {
247 TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS();
248 std::string error_msg;
249 ASSERT_TRUE(Exec(kStatic, kModeArt, {"--no-dump:vmap"}, kListAndCode, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700250}
251
252TEST_F(OatDumpTest, TestNoDisassemble) {
253 std::string error_msg;
Roland Levillain04147ef2016-09-06 11:09:41 +0100254 ASSERT_TRUE(Exec(kDynamic, kModeArt, {"--no-disassemble"}, kListAndCode, &error_msg))
255 << error_msg;
256}
257TEST_F(OatDumpTest, TestNoDisassembleStatic) {
258 TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS();
259 std::string error_msg;
260 ASSERT_TRUE(Exec(kStatic, kModeArt, {"--no-disassemble"}, kListAndCode, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700261}
262
263TEST_F(OatDumpTest, TestListClasses) {
264 std::string error_msg;
Roland Levillain04147ef2016-09-06 11:09:41 +0100265 ASSERT_TRUE(Exec(kDynamic, kModeArt, {"--list-classes"}, kListOnly, &error_msg)) << error_msg;
266}
267TEST_F(OatDumpTest, TestListClassesStatic) {
268 TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS();
269 std::string error_msg;
270 ASSERT_TRUE(Exec(kStatic, kModeArt, {"--list-classes"}, kListOnly, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700271}
272
273TEST_F(OatDumpTest, TestListMethods) {
274 std::string error_msg;
Roland Levillain04147ef2016-09-06 11:09:41 +0100275 ASSERT_TRUE(Exec(kDynamic, kModeArt, {"--list-methods"}, kListOnly, &error_msg)) << error_msg;
276}
277TEST_F(OatDumpTest, TestListMethodsStatic) {
278 TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS();
279 std::string error_msg;
280 ASSERT_TRUE(Exec(kStatic, kModeArt, {"--list-methods"}, kListOnly, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700281}
282
283TEST_F(OatDumpTest, TestSymbolize) {
284 std::string error_msg;
Roland Levillain04147ef2016-09-06 11:09:41 +0100285 ASSERT_TRUE(Exec(kDynamic, kModeSymbolize, {}, kListOnly, &error_msg)) << error_msg;
286}
287TEST_F(OatDumpTest, TestSymbolizeStatic) {
288 TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS();
289 std::string error_msg;
290 ASSERT_TRUE(Exec(kStatic, kModeSymbolize, {}, kListOnly, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700291}
Nicolas Geoffray973ce7c2016-03-24 09:23:04 +0000292#endif
Mathieu Chartier19510f02015-05-26 14:44:35 -0700293} // namespace art