• 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 <gtest/gtest.h>
21 
22 #include <functional>
23 #include <string>
24 
25 #include <sys/wait.h>
26 
27 #include <android-base/logging.h>
28 
29 #include "base/file_utils.h"
30 #include "base/globals.h"
31 #include "base/memory_tool.h"
32 #include "base/mutex.h"
33 #include "base/os.h"
34 #include "base/unix_file/fd_file.h"
35 #include "dex/art_dex_file_loader.h"
36 #include "dex/compact_dex_level.h"
37 #include "dex/compact_dex_file.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   // Get the names of the libcore modules.
143   virtual std::vector<std::string> GetLibCoreModuleNames() const;
144 
145   // Gets the paths of the libcore dex files for given modules.
146   std::vector<std::string> GetLibCoreDexFileNames(const std::vector<std::string>& modules) const;
147 
148   // Gets the paths of the libcore dex files.
149   std::vector<std::string> GetLibCoreDexFileNames() const;
150 
151   // Gets the locations of the libcore dex files for given modules.
152   std::vector<std::string> GetLibCoreDexLocations(const std::vector<std::string>& modules) const;
153 
154   // Gets the locations of the libcore dex files.
155   std::vector<std::string> GetLibCoreDexLocations() const;
156 
157   static std::string GetClassPathOption(const char* option,
158                                         const std::vector<std::string>& class_path);
159 
160   // Retuerns the filename for a test dex (i.e. XandY or ManyMethods).
161   std::string GetTestDexFileName(const char* name) const;
162 
163   template <typename Mutator>
MutateDexFile(File * output_dex,const std::string & input_jar,const Mutator & mutator)164   bool MutateDexFile(File* output_dex, const std::string& input_jar, const Mutator& mutator) {
165     std::vector<std::unique_ptr<const DexFile>> dex_files;
166     std::string error_msg;
167     const ArtDexFileLoader dex_file_loader;
168     CHECK(dex_file_loader.Open(input_jar.c_str(),
169                                input_jar.c_str(),
170                                /*verify*/ true,
171                                /*verify_checksum*/ true,
172                                &error_msg,
173                                &dex_files)) << error_msg;
174     EXPECT_EQ(dex_files.size(), 1u) << "Only one input dex is supported";
175     const std::unique_ptr<const DexFile>& dex = dex_files[0];
176     CHECK(dex->EnableWrite()) << "Failed to enable write";
177     DexFile* dex_file = const_cast<DexFile*>(dex.get());
178     mutator(dex_file);
179     const_cast<DexFile::Header&>(dex_file->GetHeader()).checksum_ = dex_file->CalculateChecksum();
180     if (!output_dex->WriteFully(dex->Begin(), dex->Size())) {
181       return false;
182     }
183     if (output_dex->Flush() != 0) {
184       PLOG(FATAL) << "Could not flush the output file.";
185     }
186     return true;
187   }
188 
189   struct ForkAndExecResult {
190     enum Stage {
191       kLink,
192       kFork,
193       kWaitpid,
194       kFinished,
195     };
196     Stage stage;
197     int status_code;
198 
StandardSuccessForkAndExecResult199     bool StandardSuccess() {
200       return stage == kFinished && WIFEXITED(status_code) && WEXITSTATUS(status_code) == 0;
201     }
202   };
203   using OutputHandlerFn = std::function<void(char*, size_t)>;
204   using PostForkFn = std::function<bool()>;
205   static ForkAndExecResult ForkAndExec(const std::vector<std::string>& argv,
206                                        const PostForkFn& post_fork,
207                                        const OutputHandlerFn& handler);
208   static ForkAndExecResult ForkAndExec(const std::vector<std::string>& argv,
209                                        const PostForkFn& post_fork,
210                                        std::string* output);
211 
212   // Helper - find prebuilt tool (e.g. objdump).
213   static std::string GetAndroidTool(const char* name, InstructionSet isa = InstructionSet::kX86_64);
214 
215  protected:
IsHost()216   static bool IsHost() {
217     return !kIsTargetBuild;
218   }
219 
220   // Returns ${ANDROID_BUILD_TOP}. Ensure it has tailing /.
221   static std::string GetAndroidBuildTop();
222 
223   // Returns ${ANDROID_HOST_OUT}.
224   static std::string GetAndroidHostOut();
225 
226   // File location to boot.art, e.g. /apex/com.android.art/javalib/boot.art
227   static std::string GetCoreArtLocation();
228 
229   // File location to boot.oat, e.g. /apex/com.android.art/javalib/boot.oat
230   static std::string GetCoreOatLocation();
231 
232   std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location);
233 
234   void ClearDirectory(const char* dirpath, bool recursive = true);
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 #define TEST_DISABLED_FOR_TARGET() \
291   if (kIsTargetBuild) { \
292     printf("WARNING: TEST DISABLED FOR TARGET\n"); \
293     return; \
294   }
295 
296 #define TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS() \
297   if (!kHostStaticBuildEnabled) { \
298     printf("WARNING: TEST DISABLED FOR NON-STATIC HOST BUILDS\n"); \
299     return; \
300   }
301 
302 #define TEST_DISABLED_FOR_MEMORY_TOOL() \
303   if (kRunningOnMemoryTool) { \
304     printf("WARNING: TEST DISABLED FOR MEMORY TOOL\n"); \
305     return; \
306   }
307 
308 #define TEST_DISABLED_FOR_HEAP_POISONING() \
309   if (kPoisonHeapReferences) { \
310     printf("WARNING: TEST DISABLED FOR HEAP POISONING\n"); \
311     return; \
312   }
313 }  // namespace art
314 
315 #define TEST_DISABLED_FOR_MEMORY_TOOL_WITH_HEAP_POISONING() \
316   if (kRunningOnMemoryTool && kPoisonHeapReferences) { \
317     printf("WARNING: TEST DISABLED FOR MEMORY TOOL WITH HEAP POISONING\n"); \
318     return; \
319   }
320 
321 #endif  // ART_LIBARTBASE_BASE_COMMON_ART_TEST_H_
322