• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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_RUNTIME_COMMON_RUNTIME_TEST_H_
18 #define ART_RUNTIME_COMMON_RUNTIME_TEST_H_
19 
20 #include <gtest/gtest.h>
21 #include <jni.h>
22 
23 #include <string>
24 
25 #include <android-base/logging.h>
26 
27 #include "arch/instruction_set.h"
28 #include "base/common_art_test.h"
29 #include "base/locks.h"
30 #include "base/os.h"
31 #include "base/unix_file/fd_file.h"
32 #include "dex/art_dex_file_loader.h"
33 #include "dex/compact_dex_level.h"
34 // TODO: Add inl file and avoid including inl.
35 #include "obj_ptr-inl.h"
36 #include "runtime_globals.h"
37 #include "scoped_thread_state_change-inl.h"
38 
39 namespace art {
40 
41 using LogSeverity = android::base::LogSeverity;
42 using ScopedLogSeverity = android::base::ScopedLogSeverity;
43 
44 class ClassLinker;
45 class CompilerCallbacks;
46 class DexFile;
47 class JavaVMExt;
48 class Runtime;
49 typedef std::vector<std::pair<std::string, const void*>> RuntimeOptions;
50 class Thread;
51 class VariableSizedHandleScope;
52 
53 class CommonRuntimeTestImpl : public CommonArtTestImpl {
54  public:
55   CommonRuntimeTestImpl();
56   virtual ~CommonRuntimeTestImpl();
57 
58   static std::string GetAndroidTargetToolsDir(InstructionSet isa);
59 
60   // A helper function to fill the heap.
61   static void FillHeap(Thread* self,
62                        ClassLinker* class_linker,
63                        VariableSizedHandleScope* handle_scope)
64       REQUIRES_SHARED(Locks::mutator_lock_);
65   // A helper to set up a small heap (4M) to make FillHeap faster.
66   static void SetUpRuntimeOptionsForFillHeap(RuntimeOptions *options);
67 
68   template <typename Mutator>
MutateDexFile(File * output_dex,const std::string & input_jar,const Mutator & mutator)69   bool MutateDexFile(File* output_dex, const std::string& input_jar, const Mutator& mutator) {
70     std::vector<std::unique_ptr<const DexFile>> dex_files;
71     std::string error_msg;
72     const ArtDexFileLoader dex_file_loader;
73     CHECK(dex_file_loader.Open(input_jar.c_str(),
74                                input_jar.c_str(),
75                                /*verify=*/ true,
76                                /*verify_checksum=*/ true,
77                                &error_msg,
78                                &dex_files)) << error_msg;
79     EXPECT_EQ(dex_files.size(), 1u) << "Only one input dex is supported";
80     const std::unique_ptr<const DexFile>& dex = dex_files[0];
81     CHECK(dex->EnableWrite()) << "Failed to enable write";
82     DexFile* dex_file = const_cast<DexFile*>(dex.get());
83     mutator(dex_file);
84     const_cast<DexFile::Header&>(dex_file->GetHeader()).checksum_ = dex_file->CalculateChecksum();
85     if (!output_dex->WriteFully(dex->Begin(), dex->Size())) {
86       return false;
87     }
88     if (output_dex->Flush() != 0) {
89       PLOG(FATAL) << "Could not flush the output file.";
90     }
91     return true;
92   }
93 
94   void MakeInterpreted(ObjPtr<mirror::Class> klass)
95       REQUIRES_SHARED(Locks::mutator_lock_);
96 
97   bool StartDex2OatCommandLine(/*out*/std::vector<std::string>* argv,
98                                /*out*/std::string* error_msg);
99 
100  protected:
101   // Allow subclases such as CommonCompilerTest to add extra options.
SetUpRuntimeOptions(RuntimeOptions * options ATTRIBUTE_UNUSED)102   virtual void SetUpRuntimeOptions(RuntimeOptions* options ATTRIBUTE_UNUSED) {}
103 
104   // Called before the runtime is created.
PreRuntimeCreate()105   virtual void PreRuntimeCreate() {}
106 
107   // Called after the runtime is created.
PostRuntimeCreate()108   virtual void PostRuntimeCreate() {}
109 
110   // Loads the test dex file identified by the given dex_name into a PathClassLoader.
111   // Returns the created class loader.
112   jobject LoadDex(const char* dex_name) REQUIRES_SHARED(Locks::mutator_lock_);
113   // Loads the test dex file identified by the given first_dex_name and second_dex_name
114   // into a PathClassLoader. Returns the created class loader.
115   jobject LoadMultiDex(const char* first_dex_name, const char* second_dex_name)
116       REQUIRES_SHARED(Locks::mutator_lock_);
117 
118   jobject LoadDexInPathClassLoader(const std::string& dex_name,
119                                    jobject parent_loader,
120                                    jobject shared_libraries = nullptr);
121   jobject LoadDexInDelegateLastClassLoader(const std::string& dex_name, jobject parent_loader);
122   jobject LoadDexInInMemoryDexClassLoader(const std::string& dex_name, jobject parent_loader);
123   jobject LoadDexInWellKnownClassLoader(const std::string& dex_name,
124                                         jclass loader_class,
125                                         jobject parent_loader,
126                                         jobject shared_libraries = nullptr);
127 
128   std::unique_ptr<Runtime> runtime_;
129 
130   // The class_linker_, java_lang_dex_file_, and boot_class_path_ are all
131   // owned by the runtime.
132   ClassLinker* class_linker_;
133   const DexFile* java_lang_dex_file_;
134   std::vector<const DexFile*> boot_class_path_;
135 
136   // Get the dex files from a PathClassLoader or DelegateLastClassLoader.
137   // This only looks into the current class loader and does not recurse into the parents.
138   std::vector<const DexFile*> GetDexFiles(jobject jclass_loader);
139   std::vector<const DexFile*> GetDexFiles(ScopedObjectAccess& soa,
140                                           Handle<mirror::ClassLoader> class_loader)
141     REQUIRES_SHARED(Locks::mutator_lock_);
142 
143   // Get the first dex file from a PathClassLoader. Will abort if it is null.
144   const DexFile* GetFirstDexFile(jobject jclass_loader);
145 
146   std::unique_ptr<CompilerCallbacks> callbacks_;
147 
148   virtual void SetUp();
149 
150   virtual void TearDown();
151 
152   // Called to finish up runtime creation and filling test fields. By default runs root
153   // initializers, initialize well-known classes, and creates the heap thread pool.
154   virtual void FinalizeSetup();
155 };
156 
157 template <typename TestType>
158 class CommonRuntimeTestBase : public TestType, public CommonRuntimeTestImpl {
159  public:
CommonRuntimeTestBase()160   CommonRuntimeTestBase() {}
~CommonRuntimeTestBase()161   virtual ~CommonRuntimeTestBase() {}
162 
163  protected:
SetUp()164   void SetUp() override {
165     CommonRuntimeTestImpl::SetUp();
166   }
167 
TearDown()168   void TearDown() override {
169     CommonRuntimeTestImpl::TearDown();
170   }
171 };
172 
173 using CommonRuntimeTest = CommonRuntimeTestBase<testing::Test>;
174 
175 template <typename Param>
176 using CommonRuntimeTestWithParam = CommonRuntimeTestBase<testing::TestWithParam<Param>>;
177 
178 // Sets a CheckJni abort hook to catch failures. Note that this will cause CheckJNI to carry on
179 // rather than aborting, so be careful!
180 class CheckJniAbortCatcher {
181  public:
182   CheckJniAbortCatcher();
183 
184   ~CheckJniAbortCatcher();
185 
186   void Check(const std::string& expected_text);
187   void Check(const char* expected_text);
188 
189  private:
190   static void Hook(void* data, const std::string& reason);
191 
192   JavaVMExt* const vm_;
193   std::string actual_;
194 
195   DISALLOW_COPY_AND_ASSIGN(CheckJniAbortCatcher);
196 };
197 
198 #define TEST_DISABLED_FOR_ARM() \
199   if (kRuntimeISA == InstructionSet::kArm || kRuntimeISA == InstructionSet::kThumb2) { \
200     printf("WARNING: TEST DISABLED FOR ARM\n"); \
201     return; \
202   }
203 
204 #define TEST_DISABLED_FOR_ARM64() \
205   if (kRuntimeISA == InstructionSet::kArm64) { \
206     printf("WARNING: TEST DISABLED FOR ARM64\n"); \
207     return; \
208   }
209 
210 #define TEST_DISABLED_FOR_MIPS() \
211   if (kRuntimeISA == InstructionSet::kMips) { \
212     printf("WARNING: TEST DISABLED FOR MIPS\n"); \
213     return; \
214   }
215 
216 #define TEST_DISABLED_FOR_MIPS64() \
217   if (kRuntimeISA == InstructionSet::kMips64) { \
218     printf("WARNING: TEST DISABLED FOR MIPS64\n"); \
219     return; \
220   }
221 
222 #define TEST_DISABLED_FOR_X86() \
223   if (kRuntimeISA == InstructionSet::kX86) { \
224     printf("WARNING: TEST DISABLED FOR X86\n"); \
225     return; \
226   }
227 
228 #define TEST_DISABLED_FOR_STRING_COMPRESSION() \
229   if (mirror::kUseStringCompression) { \
230     printf("WARNING: TEST DISABLED FOR STRING COMPRESSION\n"); \
231     return; \
232   }
233 
234 #define TEST_DISABLED_WITHOUT_BAKER_READ_BARRIERS() \
235   if (!kEmitCompilerReadBarrier || !kUseBakerReadBarrier) { \
236     printf("WARNING: TEST DISABLED FOR GC WITHOUT BAKER READ BARRIER\n"); \
237     return; \
238   }
239 
240 #define TEST_DISABLED_FOR_HEAP_POISONING() \
241   if (kPoisonHeapReferences) { \
242     printf("WARNING: TEST DISABLED FOR HEAP POISONING\n"); \
243     return; \
244   }
245 
246 #define TEST_DISABLED_FOR_MEMORY_TOOL_WITH_HEAP_POISONING_WITHOUT_READ_BARRIERS() \
247   if (kRunningOnMemoryTool && kPoisonHeapReferences && !kEmitCompilerReadBarrier) { \
248     printf("WARNING: TEST DISABLED FOR MEMORY TOOL WITH HEAP POISONING WITHOUT READ BARRIERS\n"); \
249     return; \
250   }
251 
252 }  // namespace art
253 
254 #endif  // ART_RUNTIME_COMMON_RUNTIME_TEST_H_
255