• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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_LIBARTBASE_BASE_COMMON_ART_TEST_H_
18 #define ART_LIBARTBASE_BASE_COMMON_ART_TEST_H_
19 
20 #include <sys/types.h>
21 #include <sys/wait.h>
22 
23 #include <functional>
24 #include <string>
25 #include <vector>
26 
27 #include "android-base/logging.h"
28 #include "base/file_utils.h"
29 #include "base/globals.h"
30 #include "base/memory_tool.h"
31 #include "base/mutex.h"
32 #include "base/os.h"
33 #include "base/unix_file/fd_file.h"
34 #include "dex/art_dex_file_loader.h"
35 #include "dex/compact_dex_file.h"
36 #include "dex/compact_dex_level.h"
37 #include "gtest/gtest.h"
38 
39 namespace art {
40 
41 using LogSeverity = android::base::LogSeverity;
42 using ScopedLogSeverity = android::base::ScopedLogSeverity;
43 
44 class DexFile;
45 
46 class ScratchDir {
47  public:
48   explicit ScratchDir(bool keep_files = false);
49 
50   ~ScratchDir();
51 
GetPath()52   const std::string& GetPath() const {
53     return path_;
54   }
55 
56  private:
57   std::string path_;
58   bool keep_files_;  // Useful for debugging.
59 
60   DISALLOW_COPY_AND_ASSIGN(ScratchDir);
61 };
62 
63 class ScratchFile {
64  public:
65   ScratchFile();
66 
67   explicit ScratchFile(const std::string& filename);
68 
69   ScratchFile(const ScratchFile& other, const char* suffix);
70 
71   ScratchFile(ScratchFile&& other) noexcept;
72 
73   ScratchFile& operator=(ScratchFile&& other) noexcept;
74 
75   explicit ScratchFile(File* file);
76 
77   ~ScratchFile();
78 
GetFilename()79   const std::string& GetFilename() const {
80     return filename_;
81   }
82 
GetFile()83   File* GetFile() const {
84     return file_.get();
85   }
86 
87   int GetFd() const;
88 
89   void Close();
90   void Unlink();
91 
92  private:
93   std::string filename_;
94   std::unique_ptr<File> file_;
95 };
96 
97 // Helper class that removes an environment variable whilst in scope.
98 class ScopedUnsetEnvironmentVariable {
99  public:
ScopedUnsetEnvironmentVariable(const char * variable)100   explicit ScopedUnsetEnvironmentVariable(const char* variable)
101       : variable_{variable}, old_value_{GetOldValue(variable)} {
102     unsetenv(variable);
103   }
104 
~ScopedUnsetEnvironmentVariable()105   ~ScopedUnsetEnvironmentVariable() {
106     if (old_value_.has_value()) {
107       static constexpr int kReplace = 1;  // tidy-issue: replace argument has libc dependent name.
108       setenv(variable_, old_value_.value().c_str(), kReplace);
109     } else {
110       unsetenv(variable_);
111     }
112   }
113 
114  private:
GetOldValue(const char * variable)115   static std::optional<std::string> GetOldValue(const char* variable) {
116     const char* value = getenv(variable);
117     return value != nullptr ? std::optional<std::string>{value} : std::nullopt;
118   }
119 
120   const char* variable_;
121   std::optional<std::string> old_value_;
122   DISALLOW_COPY_AND_ASSIGN(ScopedUnsetEnvironmentVariable);
123 };
124 
125 class CommonArtTestImpl {
126  public:
127   CommonArtTestImpl() = default;
128   virtual ~CommonArtTestImpl() = default;
129 
130   // Set up ANDROID_BUILD_TOP, ANDROID_HOST_OUT, ANDROID_ROOT, ANDROID_I18N_ROOT,
131   // ANDROID_ART_ROOT, and ANDROID_TZDATA_ROOT environment variables using sensible defaults
132   // if not already set.
133   static void SetUpAndroidRootEnvVars();
134 
135   // Set up the ANDROID_DATA environment variable, creating the directory if required.
136   // Note: setting up ANDROID_DATA may create a temporary directory. If this is used in a
137   // non-derived class, be sure to also call the corresponding tear-down below.
138   static void SetUpAndroidDataDir(std::string& android_data);
139 
140   static void TearDownAndroidDataDir(const std::string& android_data, bool fail_on_error);
141 
142   static void ClearDirectory(const char* dirpath, bool recursive = true);
143 
144   // Get the names of the libcore modules.
145   virtual std::vector<std::string> GetLibCoreModuleNames() const;
146 
147   // Gets the paths of the libcore dex files for given modules.
148   std::vector<std::string> GetLibCoreDexFileNames(const std::vector<std::string>& modules) const;
149 
150   // Gets the paths of the libcore dex files.
151   std::vector<std::string> GetLibCoreDexFileNames() const;
152 
153   // Gets the locations of the libcore dex files for given modules.
154   std::vector<std::string> GetLibCoreDexLocations(const std::vector<std::string>& modules) const;
155 
156   // Gets the locations of the libcore dex files.
157   std::vector<std::string> GetLibCoreDexLocations() const;
158 
159   static std::string GetClassPathOption(const char* option,
160                                         const std::vector<std::string>& class_path);
161 
162   // Retuerns the filename for a test dex (i.e. XandY or ManyMethods).
163   std::string GetTestDexFileName(const char* name) const;
164 
165   template <typename Mutator>
MutateDexFile(File * output_dex,const std::string & input_jar,const Mutator & mutator)166   bool MutateDexFile(File* output_dex, const std::string& input_jar, const Mutator& mutator) {
167     std::vector<std::unique_ptr<const DexFile>> dex_files;
168     std::string error_msg;
169     const ArtDexFileLoader dex_file_loader;
170     CHECK(dex_file_loader.Open(input_jar.c_str(),
171                                input_jar.c_str(),
172                                /*verify*/ true,
173                                /*verify_checksum*/ true,
174                                &error_msg,
175                                &dex_files)) << error_msg;
176     EXPECT_EQ(dex_files.size(), 1u) << "Only one input dex is supported";
177     const std::unique_ptr<const DexFile>& dex = dex_files[0];
178     CHECK(dex->EnableWrite()) << "Failed to enable write";
179     DexFile* dex_file = const_cast<DexFile*>(dex.get());
180     mutator(dex_file);
181     const_cast<DexFile::Header&>(dex_file->GetHeader()).checksum_ = dex_file->CalculateChecksum();
182     if (!output_dex->WriteFully(dex->Begin(), dex->Size())) {
183       return false;
184     }
185     if (output_dex->Flush() != 0) {
186       PLOG(FATAL) << "Could not flush the output file.";
187     }
188     return true;
189   }
190 
191   struct ForkAndExecResult {
192     enum Stage {
193       kLink,
194       kFork,
195       kWaitpid,
196       kFinished,
197     };
198     Stage stage;
199     int status_code;
200 
StandardSuccessForkAndExecResult201     bool StandardSuccess() {
202       return stage == kFinished && WIFEXITED(status_code) && WEXITSTATUS(status_code) == 0;
203     }
204   };
205   using OutputHandlerFn = std::function<void(char*, size_t)>;
206   using PostForkFn = std::function<bool()>;
207   static ForkAndExecResult ForkAndExec(const std::vector<std::string>& argv,
208                                        const PostForkFn& post_fork,
209                                        const OutputHandlerFn& handler);
210   static ForkAndExecResult ForkAndExec(const std::vector<std::string>& argv,
211                                        const PostForkFn& post_fork,
212                                        std::string* output);
213 
214   // Helper - find prebuilt tool (e.g. objdump).
215   static std::string GetAndroidTool(const char* name, InstructionSet isa = InstructionSet::kX86_64);
216 
217  protected:
IsHost()218   static bool IsHost() {
219     return !kIsTargetBuild;
220   }
221 
222   // Returns ${ANDROID_BUILD_TOP}. Ensure it has tailing /.
223   static std::string GetAndroidBuildTop();
224 
225   // Returns ${ANDROID_HOST_OUT}.
226   static std::string GetAndroidHostOut();
227 
228   // File location to boot.art, e.g. /apex/com.android.art/javalib/boot.art
229   static std::string GetCoreArtLocation();
230 
231   // File location to boot.oat, e.g. /apex/com.android.art/javalib/boot.oat
232   static std::string GetCoreOatLocation();
233 
234   std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location);
235 
236   // Open a file (allows reading of framework jars).
237   std::vector<std::unique_ptr<const DexFile>> OpenDexFiles(const char* filename);
238 
239   // Open a single dex file (aborts if there are more than one).
240   std::unique_ptr<const DexFile> OpenDexFile(const char* filename);
241 
242   // Open a test file (art-gtest-*.jar).
243   std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name);
244 
245   std::unique_ptr<const DexFile> OpenTestDexFile(const char* name);
246 
247   std::string android_data_;
248   std::string android_system_ext_;
249   std::string dalvik_cache_;
250 
251   virtual void SetUp();
252 
253   virtual void TearDown();
254 
255   // Creates the class path string for the given dex files (the list of dex file locations
256   // separated by ':').
257   std::string CreateClassPath(const std::vector<std::unique_ptr<const DexFile>>& dex_files);
258   // Same as CreateClassPath but add the dex file checksum after each location. The separator
259   // is '*'.
260   std::string CreateClassPathWithChecksums(
261       const std::vector<std::unique_ptr<const DexFile>>& dex_files);
262 
263   static std::string GetImageDirectory();
264   static std::string GetCoreFileLocation(const char* suffix);
265 
266   std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_;
267 };
268 
269 template <typename TestType>
270 class CommonArtTestBase : public TestType, public CommonArtTestImpl {
271  public:
CommonArtTestBase()272   CommonArtTestBase() {}
~CommonArtTestBase()273   virtual ~CommonArtTestBase() {}
274 
275  protected:
SetUp()276   void SetUp() override {
277     CommonArtTestImpl::SetUp();
278   }
279 
TearDown()280   void TearDown() override {
281     CommonArtTestImpl::TearDown();
282   }
283 };
284 
285 using CommonArtTest = CommonArtTestBase<testing::Test>;
286 
287 template <typename Param>
288 using CommonArtTestWithParam = CommonArtTestBase<testing::TestWithParam<Param>>;
289 
290 // Returns a list of PIDs of the processes whose process name (the first commandline argument) fully
291 // matches the given name.
292 std::vector<pid_t> GetPidByName(const std::string& process_name);
293 
294 #define TEST_DISABLED_FOR_TARGET() \
295   if (kIsTargetBuild) { \
296     printf("WARNING: TEST DISABLED FOR TARGET\n"); \
297     return; \
298   }
299 
300 #define TEST_DISABLED_FOR_HOST() \
301   if (!kIsTargetBuild) { \
302     printf("WARNING: TEST DISABLED FOR HOST\n"); \
303     return; \
304   }
305 
306 #define TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS() \
307   if (!kHostStaticBuildEnabled) { \
308     printf("WARNING: TEST DISABLED FOR NON-STATIC HOST BUILDS\n"); \
309     return; \
310   }
311 
312 #define TEST_DISABLED_FOR_MEMORY_TOOL() \
313   if (kRunningOnMemoryTool) { \
314     printf("WARNING: TEST DISABLED FOR MEMORY TOOL\n"); \
315     return; \
316   }
317 
318 #define TEST_DISABLED_FOR_HEAP_POISONING() \
319   if (kPoisonHeapReferences) { \
320     printf("WARNING: TEST DISABLED FOR HEAP POISONING\n"); \
321     return; \
322   }
323 }  // namespace art
324 
325 #define TEST_DISABLED_FOR_MEMORY_TOOL_WITH_HEAP_POISONING() \
326   if (kRunningOnMemoryTool && kPoisonHeapReferences) { \
327     printf("WARNING: TEST DISABLED FOR MEMORY TOOL WITH HEAP POISONING\n"); \
328     return; \
329   }
330 
331 #endif  // ART_LIBARTBASE_BASE_COMMON_ART_TEST_H_
332