1 /*
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 
26 #include "arch/instruction_set.h"
27 #include "base/file_utils.h"
28 #include "base/os.h"
29 #include "base/unix_file/fd_file.h"
30 #include "base/utils.h"
31 #include "common_runtime_test.h"
32 #include "exec_utils.h"
33 #include "gc/heap.h"
34 #include "gc/space/image_space.h"
35 
36 #include <sys/types.h>
37 #include <unistd.h>
38 
39 namespace art {
40 
41 class OatDumpTest : public CommonRuntimeTest {
42  protected:
SetUp()43   virtual void SetUp() {
44     CommonRuntimeTest::SetUp();
45     core_art_location_ = GetCoreArtLocation();
46     core_oat_location_ = GetSystemImageFilename(GetCoreOatLocation().c_str(), kRuntimeISA);
47     tmp_dir_ = GetScratchDir();
48   }
49 
TearDown()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 
GetScratchDir()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;
65   }
66 
67   // Linking flavor.
68   enum class Flavor {
69     kDynamic,  // oatdump(d), dex2oat(d)
70     kStatic,   // oatdump(d)s, dex2oat(d)s
71   };
72 
73   // Returns path to the oatdump/dex2oat/dexdump binary.
GetExecutableFilePath(const char * name,bool is_debug,bool is_static,bool bitness)74   std::string GetExecutableFilePath(const char* name, bool is_debug, bool is_static, bool bitness) {
75     std::string path = GetArtBinDir() + '/' + name;
76     if (is_debug) {
77       path += 'd';
78     }
79     if (is_static) {
80       path += 's';
81     }
82     if (bitness) {
83       path += Is64BitInstructionSet(kRuntimeISA) ? "64" : "32";
84     }
85     return path;
86   }
87 
GetExecutableFilePath(Flavor flavor,const char * name,bool bitness)88   std::string GetExecutableFilePath(Flavor flavor, const char* name, bool bitness) {
89     return GetExecutableFilePath(name, kIsDebugBuild, flavor == Flavor::kStatic, bitness);
90   }
91 
92   enum Mode {
93     kModeOat,
94     kModeCoreOat,
95     kModeOatWithBootImage,
96     kModeAppImage,
97     kModeArt,
98     kModeSymbolize,
99   };
100 
101   // Display style.
102   enum Display {
103     kListOnly,
104     kListAndCode
105   };
106 
GetAppBaseName()107   std::string GetAppBaseName() {
108     // Use ProfileTestMultiDex as it contains references to boot image strings
109     // that shall use different code for PIC and non-PIC.
110     return "ProfileTestMultiDex";
111   }
112 
SetAppImageName(const std::string & name)113   void SetAppImageName(const std::string& name) {
114     app_image_name_ = name;
115   }
116 
GetAppImageName()117   std::string GetAppImageName() {
118     if (app_image_name_.empty()) {
119       app_image_name_ =  tmp_dir_ + "/" + GetAppBaseName() + ".art";
120     }
121     return app_image_name_;
122   }
123 
GetAppOdexName()124   std::string GetAppOdexName() {
125     return tmp_dir_ + "/" + GetAppBaseName() + ".odex";
126   }
127 
GenerateAppOdexFile(Flavor flavor,const std::vector<std::string> & args)128   ::testing::AssertionResult GenerateAppOdexFile(Flavor flavor,
129                                                  const std::vector<std::string>& args) {
130     std::string dex2oat_path =
131         GetExecutableFilePath(flavor, "dex2oat", /* bitness= */ kIsTargetBuild);
132     std::vector<std::string> exec_argv = {
133         dex2oat_path,
134         "--runtime-arg",
135         "-Xms64m",
136         "--runtime-arg",
137         "-Xmx512m",
138         "--runtime-arg",
139         "-Xnorelocate",
140         "--runtime-arg",
141         GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()),
142         "--runtime-arg",
143         GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()),
144         "--boot-image=" + GetCoreArtLocation(),
145         "--instruction-set=" + std::string(GetInstructionSetString(kRuntimeISA)),
146         "--dex-file=" + GetTestDexFileName(GetAppBaseName().c_str()),
147         "--oat-file=" + GetAppOdexName(),
148         "--compiler-filter=speed"
149     };
150     exec_argv.insert(exec_argv.end(), args.begin(), args.end());
151 
152     auto post_fork_fn = []() {
153       setpgid(0, 0);  // Change process groups, so we don't get reaped by ProcessManager.
154                       // Ignore setpgid errors.
155       return setenv("ANDROID_LOG_TAGS", "*:e", 1) == 0;  // We're only interested in errors and
156                                                          // fatal logs.
157     };
158 
159     std::string error_msg;
160     ForkAndExecResult res = ForkAndExec(exec_argv, post_fork_fn, &error_msg);
161     if (res.stage != ForkAndExecResult::kFinished) {
162       return ::testing::AssertionFailure() << strerror(errno);
163     }
164     return res.StandardSuccess() ? ::testing::AssertionSuccess()
165                                  : (::testing::AssertionFailure() << error_msg);
166   }
167 
168   // Run the test with custom arguments.
169   ::testing::AssertionResult Exec(Flavor flavor,
170                                   Mode mode,
171                                   const std::vector<std::string>& args,
172                                   Display display,
173                                   bool expect_failure = false) {
174     std::string file_path = GetExecutableFilePath(flavor, "oatdump", /* bitness= */ false);
175 
176     if (!OS::FileExists(file_path.c_str())) {
177       return ::testing::AssertionFailure() << file_path << " should be a valid file path";
178     }
179 
180     // ScratchFile scratch;
181     std::vector<std::string> exec_argv = { file_path };
182     std::vector<std::string> expected_prefixes;
183     if (mode == kModeSymbolize) {
184       exec_argv.push_back("--symbolize=" + core_oat_location_);
185       exec_argv.push_back("--output=" + core_oat_location_ + ".symbolize");
186     } else {
187       expected_prefixes.push_back("LOCATION:");
188       expected_prefixes.push_back("MAGIC:");
189       expected_prefixes.push_back("DEX FILE COUNT:");
190       if (display == kListAndCode) {
191         // Code and dex code do not show up if list only.
192         expected_prefixes.push_back("DEX CODE:");
193         expected_prefixes.push_back("CODE:");
194         expected_prefixes.push_back("StackMap");
195       }
196       if (mode == kModeArt) {
197         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()));
202         exec_argv.push_back("--image=" + core_art_location_);
203         exec_argv.push_back("--instruction-set=" + std::string(
204             GetInstructionSetString(kRuntimeISA)));
205         expected_prefixes.push_back("IMAGE LOCATION:");
206         expected_prefixes.push_back("IMAGE BEGIN:");
207         expected_prefixes.push_back("kDexCaches:");
208       } else if (mode == kModeOatWithBootImage) {
209         exec_argv.push_back("--runtime-arg");
210         exec_argv.push_back(GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()));
211         exec_argv.push_back("--runtime-arg");
212         exec_argv.push_back(
213             GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()));
214         exec_argv.push_back("--boot-image=" + GetCoreArtLocation());
215         exec_argv.push_back("--instruction-set=" + std::string(
216             GetInstructionSetString(kRuntimeISA)));
217         exec_argv.push_back("--oat-file=" + GetAppOdexName());
218       } else if (mode == kModeAppImage) {
219         exec_argv.push_back("--runtime-arg");
220         exec_argv.push_back(GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()));
221         exec_argv.push_back("--runtime-arg");
222         exec_argv.push_back(
223             GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()));
224         exec_argv.push_back("--image=" + GetCoreArtLocation());
225         exec_argv.push_back("--instruction-set=" + std::string(
226             GetInstructionSetString(kRuntimeISA)));
227         exec_argv.push_back("--app-oat=" + GetAppOdexName());
228         exec_argv.push_back("--app-image=" + GetAppImageName());
229       } else if (mode == kModeCoreOat) {
230         exec_argv.push_back("--oat-file=" + core_oat_location_);
231         exec_argv.push_back("--dex-file=" + GetLibCoreDexFileNames()[0]);
232       } else {
233         CHECK_EQ(static_cast<size_t>(mode), static_cast<size_t>(kModeOat));
234         exec_argv.push_back("--oat-file=" + GetAppOdexName());
235       }
236     }
237     exec_argv.insert(exec_argv.end(), args.begin(), args.end());
238 
239     std::vector<bool> found(expected_prefixes.size(), false);
240     auto line_handle_fn = [&found, &expected_prefixes](const char* line, size_t line_len) {
241       if (line_len == 0) {
242         return;
243       }
244       // Check contents.
245       for (size_t i = 0; i < expected_prefixes.size(); ++i) {
246         const std::string& expected = expected_prefixes[i];
247         if (!found[i] &&
248             line_len >= expected.length() &&
249             memcmp(line, expected.c_str(), expected.length()) == 0) {
250           found[i] = true;
251         }
252       }
253     };
254 
255     static constexpr size_t kLineMax = 256;
256     char line[kLineMax] = {};
257     size_t line_len = 0;
258     size_t total = 0;
259     bool ignore_next_line = false;
260     std::vector<char> error_buf;  // Buffer for debug output on error. Limited to 1M.
261     auto line_buf_fn = [&](char* buf, size_t len) {
262       total += len;
263 
264       if (len == 0 && line_len > 0 && !ignore_next_line) {
265         // Everything done, handle leftovers.
266         line_handle_fn(line, line_len);
267       }
268 
269       if (len > 0) {
270         size_t pos = error_buf.size();
271         if (pos < MB) {
272           error_buf.insert(error_buf.end(), buf, buf + len);
273         }
274       }
275 
276       while (len > 0) {
277         // Copy buf into the free tail of the line buffer, and move input buffer along.
278         size_t copy = std::min(kLineMax - line_len, len);
279         memcpy(&line[line_len], buf, copy);
280         buf += copy;
281         len -= copy;
282 
283         // Skip spaces up to len, return count of removed spaces. Declare a lambda for reuse.
284         auto trim_space = [&line](size_t len) {
285           size_t spaces = 0;
286           for (; spaces < len && isspace(line[spaces]); ++spaces) {}
287           if (spaces > 0) {
288             memmove(&line[0], &line[spaces], len - spaces);
289           }
290           return spaces;
291         };
292         // There can only be spaces if we freshly started a line.
293         if (line_len == 0) {
294           copy -= trim_space(copy);
295         }
296 
297         // Scan for newline characters.
298         size_t index = line_len;
299         line_len += copy;
300         while (index < line_len) {
301           if (line[index] == '\n') {
302             // Handle line.
303             if (!ignore_next_line) {
304               line_handle_fn(line, index);
305             }
306             // Move the rest to the front, but trim leading spaces.
307             line_len -= index + 1;
308             memmove(&line[0], &line[index + 1], line_len);
309             line_len -= trim_space(line_len);
310             index = 0;
311             ignore_next_line = false;
312           } else {
313             index++;
314           }
315         }
316 
317         // Handle a full line without newline characters. Ignore the "next" line, as it is the
318         // tail end of this.
319         if (line_len == kLineMax) {
320           if (!ignore_next_line) {
321             line_handle_fn(line, kLineMax);
322           }
323           line_len = 0;
324           ignore_next_line = true;
325         }
326       }
327     };
328 
329     auto post_fork_fn = []() {
330       setpgid(0, 0);  // Change process groups, so we don't get reaped by ProcessManager.
331       return true;    // Ignore setpgid failures.
332     };
333 
334     ForkAndExecResult res = ForkAndExec(exec_argv, post_fork_fn, line_buf_fn);
335     if (res.stage != ForkAndExecResult::kFinished) {
336       return ::testing::AssertionFailure() << strerror(errno);
337     }
338     error_buf.push_back(0);  // Make data a C string.
339 
340     if (!res.StandardSuccess()) {
341       if (expect_failure && WIFEXITED(res.status_code)) {
342         // Avoid crash as valid exit.
343         return ::testing::AssertionSuccess();
344       }
345       std::ostringstream cmd;
346       std::copy(exec_argv.begin(), exec_argv.end(), std::ostream_iterator<std::string>(cmd, " "));
347       LOG(ERROR) << "Output: " << error_buf.data();  // Output first as it might be extremely  long.
348       LOG(ERROR) << "Failed command: " << cmd.str();  // Useful to reproduce the failure separately.
349       return ::testing::AssertionFailure() << "Did not terminate successfully: " << res.status_code;
350     } else if (expect_failure) {
351       return ::testing::AssertionFailure() << "Expected failure";
352     }
353 
354     if (mode == kModeSymbolize) {
355       EXPECT_EQ(total, 0u);
356     } else {
357       EXPECT_GT(total, 0u);
358     }
359 
360     bool result = true;
361     std::ostringstream oss;
362     for (size_t i = 0; i < expected_prefixes.size(); ++i) {
363       if (!found[i]) {
364         oss << "Did not find prefix " << expected_prefixes[i] << std::endl;
365         result = false;
366       }
367     }
368     if (!result) {
369       oss << "Processed bytes " << total << ":" << std::endl;
370     }
371 
372     return result ? ::testing::AssertionSuccess()
373                   : (::testing::AssertionFailure() << oss.str() << error_buf.data());
374   }
375 
376   std::string tmp_dir_;
377   std::string app_image_name_;
378 
379  private:
380   std::string core_art_location_;
381   std::string core_oat_location_;
382 };
383 
384 }  // namespace art
385 
386 #endif  // ART_OATDUMP_OATDUMP_TEST_H_
387