xref: /aosp_15_r20/art/runtime/common_runtime_test.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2012 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "common_runtime_test.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include <dirent.h>
20*795d594fSAndroid Build Coastguard Worker #include <dlfcn.h>
21*795d594fSAndroid Build Coastguard Worker #include <fcntl.h>
22*795d594fSAndroid Build Coastguard Worker #include <stdlib.h>
23*795d594fSAndroid Build Coastguard Worker #include <cstdio>
24*795d594fSAndroid Build Coastguard Worker #include "nativehelper/scoped_local_ref.h"
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
27*795d594fSAndroid Build Coastguard Worker 
28*795d594fSAndroid Build Coastguard Worker #include "art_field-inl.h"
29*795d594fSAndroid Build Coastguard Worker #include "base/file_utils.h"
30*795d594fSAndroid Build Coastguard Worker #include "base/logging.h"
31*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
32*795d594fSAndroid Build Coastguard Worker #include "base/mem_map.h"
33*795d594fSAndroid Build Coastguard Worker #include "base/mutex.h"
34*795d594fSAndroid Build Coastguard Worker #include "base/os.h"
35*795d594fSAndroid Build Coastguard Worker #include "base/runtime_debug.h"
36*795d594fSAndroid Build Coastguard Worker #include "base/stl_util.h"
37*795d594fSAndroid Build Coastguard Worker #include "base/unix_file/fd_file.h"
38*795d594fSAndroid Build Coastguard Worker #include "class_linker.h"
39*795d594fSAndroid Build Coastguard Worker #include "class_loader_utils.h"
40*795d594fSAndroid Build Coastguard Worker #include "compiler_callbacks.h"
41*795d594fSAndroid Build Coastguard Worker #include "dex/art_dex_file_loader.h"
42*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file-inl.h"
43*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file_loader.h"
44*795d594fSAndroid Build Coastguard Worker #include "dex/method_reference.h"
45*795d594fSAndroid Build Coastguard Worker #include "dex/primitive.h"
46*795d594fSAndroid Build Coastguard Worker #include "dex/type_reference.h"
47*795d594fSAndroid Build Coastguard Worker #include "gc/heap.h"
48*795d594fSAndroid Build Coastguard Worker #include "gc/space/image_space.h"
49*795d594fSAndroid Build Coastguard Worker #include "gc_root-inl.h"
50*795d594fSAndroid Build Coastguard Worker #include "gtest/gtest.h"
51*795d594fSAndroid Build Coastguard Worker #include "handle_scope-inl.h"
52*795d594fSAndroid Build Coastguard Worker #include "interpreter/unstarted_runtime.h"
53*795d594fSAndroid Build Coastguard Worker #include "jni/java_vm_ext.h"
54*795d594fSAndroid Build Coastguard Worker #include "jni/jni_internal.h"
55*795d594fSAndroid Build Coastguard Worker #include "mirror/class-alloc-inl.h"
56*795d594fSAndroid Build Coastguard Worker #include "mirror/class-inl.h"
57*795d594fSAndroid Build Coastguard Worker #include "mirror/class_loader-inl.h"
58*795d594fSAndroid Build Coastguard Worker #include "mirror/object_array-alloc-inl.h"
59*795d594fSAndroid Build Coastguard Worker #include "native/dalvik_system_DexFile.h"
60*795d594fSAndroid Build Coastguard Worker #include "noop_compiler_callbacks.h"
61*795d594fSAndroid Build Coastguard Worker #include "profile/profile_compilation_info.h"
62*795d594fSAndroid Build Coastguard Worker #include "runtime-inl.h"
63*795d594fSAndroid Build Coastguard Worker #include "runtime_intrinsics.h"
64*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
65*795d594fSAndroid Build Coastguard Worker #include "thread.h"
66*795d594fSAndroid Build Coastguard Worker #include "well_known_classes-inl.h"
67*795d594fSAndroid Build Coastguard Worker 
68*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
69*795d594fSAndroid Build Coastguard Worker 
70*795d594fSAndroid Build Coastguard Worker using android::base::StringPrintf;
71*795d594fSAndroid Build Coastguard Worker 
72*795d594fSAndroid Build Coastguard Worker static bool unstarted_initialized_ = false;
73*795d594fSAndroid Build Coastguard Worker 
CommonRuntimeTestImpl()74*795d594fSAndroid Build Coastguard Worker CommonRuntimeTestImpl::CommonRuntimeTestImpl()
75*795d594fSAndroid Build Coastguard Worker     : class_linker_(nullptr),
76*795d594fSAndroid Build Coastguard Worker       java_lang_dex_file_(nullptr),
77*795d594fSAndroid Build Coastguard Worker       boot_class_path_(),
78*795d594fSAndroid Build Coastguard Worker       callbacks_(),
79*795d594fSAndroid Build Coastguard Worker       use_boot_image_(false) {
80*795d594fSAndroid Build Coastguard Worker }
81*795d594fSAndroid Build Coastguard Worker 
~CommonRuntimeTestImpl()82*795d594fSAndroid Build Coastguard Worker CommonRuntimeTestImpl::~CommonRuntimeTestImpl() {
83*795d594fSAndroid Build Coastguard Worker   // Ensure the dex files are cleaned up before the runtime.
84*795d594fSAndroid Build Coastguard Worker   loaded_dex_files_.clear();
85*795d594fSAndroid Build Coastguard Worker   runtime_.reset();
86*795d594fSAndroid Build Coastguard Worker }
87*795d594fSAndroid Build Coastguard Worker 
SetUp()88*795d594fSAndroid Build Coastguard Worker void CommonRuntimeTestImpl::SetUp() {
89*795d594fSAndroid Build Coastguard Worker   CommonArtTestImpl::SetUp();
90*795d594fSAndroid Build Coastguard Worker 
91*795d594fSAndroid Build Coastguard Worker   std::string min_heap_string(StringPrintf("-Xms%zdm", gc::Heap::kDefaultInitialSize / MB));
92*795d594fSAndroid Build Coastguard Worker   std::string max_heap_string(StringPrintf("-Xmx%zdm", gc::Heap::kDefaultMaximumSize / MB));
93*795d594fSAndroid Build Coastguard Worker 
94*795d594fSAndroid Build Coastguard Worker   RuntimeOptions options;
95*795d594fSAndroid Build Coastguard Worker   std::string boot_class_path_string =
96*795d594fSAndroid Build Coastguard Worker       GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames());
97*795d594fSAndroid Build Coastguard Worker   std::string boot_class_path_locations_string =
98*795d594fSAndroid Build Coastguard Worker       GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations());
99*795d594fSAndroid Build Coastguard Worker 
100*795d594fSAndroid Build Coastguard Worker   options.push_back(std::make_pair(boot_class_path_string, nullptr));
101*795d594fSAndroid Build Coastguard Worker   options.push_back(std::make_pair(boot_class_path_locations_string, nullptr));
102*795d594fSAndroid Build Coastguard Worker   if (use_boot_image_) {
103*795d594fSAndroid Build Coastguard Worker     options.emplace_back("-Ximage:" + GetImageLocation(), nullptr);
104*795d594fSAndroid Build Coastguard Worker   }
105*795d594fSAndroid Build Coastguard Worker   options.push_back(std::make_pair("-Xcheck:jni", nullptr));
106*795d594fSAndroid Build Coastguard Worker   options.push_back(std::make_pair(min_heap_string, nullptr));
107*795d594fSAndroid Build Coastguard Worker   options.push_back(std::make_pair(max_heap_string, nullptr));
108*795d594fSAndroid Build Coastguard Worker 
109*795d594fSAndroid Build Coastguard Worker   // Technically this is redundant w/ common_art_test, but still check.
110*795d594fSAndroid Build Coastguard Worker   options.push_back(std::make_pair("-XX:SlowDebug=true", nullptr));
111*795d594fSAndroid Build Coastguard Worker   static bool gSlowDebugTestFlag = false;
112*795d594fSAndroid Build Coastguard Worker   RegisterRuntimeDebugFlag(&gSlowDebugTestFlag);
113*795d594fSAndroid Build Coastguard Worker 
114*795d594fSAndroid Build Coastguard Worker   // Create default compiler callbacks. `SetUpRuntimeOptions()` can replace or remove this.
115*795d594fSAndroid Build Coastguard Worker   callbacks_.reset(new NoopCompilerCallbacks());
116*795d594fSAndroid Build Coastguard Worker 
117*795d594fSAndroid Build Coastguard Worker   SetUpRuntimeOptions(&options);
118*795d594fSAndroid Build Coastguard Worker 
119*795d594fSAndroid Build Coastguard Worker   // Install compiler-callbacks if SetUpRuntimeOptions hasn't deleted them.
120*795d594fSAndroid Build Coastguard Worker   if (callbacks_.get() != nullptr) {
121*795d594fSAndroid Build Coastguard Worker     options.push_back(std::make_pair("compilercallbacks", callbacks_.get()));
122*795d594fSAndroid Build Coastguard Worker   }
123*795d594fSAndroid Build Coastguard Worker 
124*795d594fSAndroid Build Coastguard Worker   PreRuntimeCreate();
125*795d594fSAndroid Build Coastguard Worker   {
126*795d594fSAndroid Build Coastguard Worker     ScopedLogSeverity sls(LogSeverity::WARNING);
127*795d594fSAndroid Build Coastguard Worker     if (!Runtime::Create(options, false)) {
128*795d594fSAndroid Build Coastguard Worker       LOG(FATAL) << "Failed to create runtime";
129*795d594fSAndroid Build Coastguard Worker       UNREACHABLE();
130*795d594fSAndroid Build Coastguard Worker     }
131*795d594fSAndroid Build Coastguard Worker   }
132*795d594fSAndroid Build Coastguard Worker   PostRuntimeCreate();
133*795d594fSAndroid Build Coastguard Worker   runtime_.reset(Runtime::Current());
134*795d594fSAndroid Build Coastguard Worker   class_linker_ = runtime_->GetClassLinker();
135*795d594fSAndroid Build Coastguard Worker 
136*795d594fSAndroid Build Coastguard Worker   // Runtime::Create acquired the mutator_lock_ that is normally given away when we
137*795d594fSAndroid Build Coastguard Worker   // Runtime::Start, give it away now and then switch to a more managable ScopedObjectAccess.
138*795d594fSAndroid Build Coastguard Worker   Thread::Current()->TransitionFromRunnableToSuspended(ThreadState::kNative);
139*795d594fSAndroid Build Coastguard Worker 
140*795d594fSAndroid Build Coastguard Worker   // Get the boot class path from the runtime so it can be used in tests.
141*795d594fSAndroid Build Coastguard Worker   boot_class_path_ = class_linker_->GetBootClassPath();
142*795d594fSAndroid Build Coastguard Worker   ASSERT_FALSE(boot_class_path_.empty());
143*795d594fSAndroid Build Coastguard Worker   java_lang_dex_file_ = boot_class_path_[0];
144*795d594fSAndroid Build Coastguard Worker 
145*795d594fSAndroid Build Coastguard Worker   FinalizeSetup();
146*795d594fSAndroid Build Coastguard Worker 
147*795d594fSAndroid Build Coastguard Worker   if (kIsDebugBuild) {
148*795d594fSAndroid Build Coastguard Worker     // Ensure that we're really running with debug checks enabled.
149*795d594fSAndroid Build Coastguard Worker     CHECK(gSlowDebugTestFlag);
150*795d594fSAndroid Build Coastguard Worker   }
151*795d594fSAndroid Build Coastguard Worker }
152*795d594fSAndroid Build Coastguard Worker 
FinalizeSetup()153*795d594fSAndroid Build Coastguard Worker void CommonRuntimeTestImpl::FinalizeSetup() {
154*795d594fSAndroid Build Coastguard Worker   // Initialize maps for unstarted runtime. This needs to be here, as running clinits needs this
155*795d594fSAndroid Build Coastguard Worker   // set up.
156*795d594fSAndroid Build Coastguard Worker   if (!unstarted_initialized_) {
157*795d594fSAndroid Build Coastguard Worker     interpreter::UnstartedRuntime::Initialize();
158*795d594fSAndroid Build Coastguard Worker     unstarted_initialized_ = true;
159*795d594fSAndroid Build Coastguard Worker   } else {
160*795d594fSAndroid Build Coastguard Worker     interpreter::UnstartedRuntime::Reinitialize();
161*795d594fSAndroid Build Coastguard Worker   }
162*795d594fSAndroid Build Coastguard Worker 
163*795d594fSAndroid Build Coastguard Worker   {
164*795d594fSAndroid Build Coastguard Worker     ScopedObjectAccess soa(Thread::Current());
165*795d594fSAndroid Build Coastguard Worker     runtime_->GetClassLinker()->RunEarlyRootClinits(soa.Self());
166*795d594fSAndroid Build Coastguard Worker     InitializeIntrinsics();
167*795d594fSAndroid Build Coastguard Worker     runtime_->RunRootClinits(soa.Self());
168*795d594fSAndroid Build Coastguard Worker   }
169*795d594fSAndroid Build Coastguard Worker 
170*795d594fSAndroid Build Coastguard Worker   runtime_->GetHeap()->VerifyHeap();  // Check for heap corruption before the test
171*795d594fSAndroid Build Coastguard Worker   // Reduce timinig-dependent flakiness in OOME behavior (eg StubTest.AllocObject).
172*795d594fSAndroid Build Coastguard Worker   runtime_->GetHeap()->SetMinIntervalHomogeneousSpaceCompactionByOom(0U);
173*795d594fSAndroid Build Coastguard Worker }
174*795d594fSAndroid Build Coastguard Worker 
TearDown()175*795d594fSAndroid Build Coastguard Worker void CommonRuntimeTestImpl::TearDown() {
176*795d594fSAndroid Build Coastguard Worker   CommonArtTestImpl::TearDown();
177*795d594fSAndroid Build Coastguard Worker   if (runtime_ != nullptr) {
178*795d594fSAndroid Build Coastguard Worker     runtime_->GetHeap()->VerifyHeap();  // Check for heap corruption after the test
179*795d594fSAndroid Build Coastguard Worker   }
180*795d594fSAndroid Build Coastguard Worker }
181*795d594fSAndroid Build Coastguard Worker 
182*795d594fSAndroid Build Coastguard Worker // Check that for target builds we have ART_TARGET_NATIVETEST_DIR set.
183*795d594fSAndroid Build Coastguard Worker #ifdef ART_TARGET
184*795d594fSAndroid Build Coastguard Worker #ifndef ART_TARGET_NATIVETEST_DIR
185*795d594fSAndroid Build Coastguard Worker #error "ART_TARGET_NATIVETEST_DIR not set."
186*795d594fSAndroid Build Coastguard Worker #endif
187*795d594fSAndroid Build Coastguard Worker // Wrap it as a string literal.
188*795d594fSAndroid Build Coastguard Worker #define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/"
189*795d594fSAndroid Build Coastguard Worker #else
190*795d594fSAndroid Build Coastguard Worker #define ART_TARGET_NATIVETEST_DIR_STRING ""
191*795d594fSAndroid Build Coastguard Worker #endif
192*795d594fSAndroid Build Coastguard Worker 
GetDexFiles(jobject jclass_loader)193*795d594fSAndroid Build Coastguard Worker std::vector<const DexFile*> CommonRuntimeTestImpl::GetDexFiles(jobject jclass_loader) {
194*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
195*795d594fSAndroid Build Coastguard Worker 
196*795d594fSAndroid Build Coastguard Worker   StackHandleScope<1> hs(soa.Self());
197*795d594fSAndroid Build Coastguard Worker   Handle<mirror::ClassLoader> class_loader = hs.NewHandle(
198*795d594fSAndroid Build Coastguard Worker       soa.Decode<mirror::ClassLoader>(jclass_loader));
199*795d594fSAndroid Build Coastguard Worker   return GetDexFiles(soa.Self(), class_loader);
200*795d594fSAndroid Build Coastguard Worker }
201*795d594fSAndroid Build Coastguard Worker 
GetDexFiles(Thread * self,Handle<mirror::ClassLoader> class_loader)202*795d594fSAndroid Build Coastguard Worker std::vector<const DexFile*> CommonRuntimeTestImpl::GetDexFiles(
203*795d594fSAndroid Build Coastguard Worker     Thread* self,
204*795d594fSAndroid Build Coastguard Worker     Handle<mirror::ClassLoader> class_loader) {
205*795d594fSAndroid Build Coastguard Worker   DCHECK((class_loader->GetClass() == WellKnownClasses::dalvik_system_PathClassLoader) ||
206*795d594fSAndroid Build Coastguard Worker          (class_loader->GetClass() == WellKnownClasses::dalvik_system_DelegateLastClassLoader));
207*795d594fSAndroid Build Coastguard Worker 
208*795d594fSAndroid Build Coastguard Worker   std::vector<const DexFile*> ret;
209*795d594fSAndroid Build Coastguard Worker   VisitClassLoaderDexFiles(self,
210*795d594fSAndroid Build Coastguard Worker                            class_loader,
211*795d594fSAndroid Build Coastguard Worker                            [&](const DexFile* cp_dex_file) {
212*795d594fSAndroid Build Coastguard Worker                              if (cp_dex_file == nullptr) {
213*795d594fSAndroid Build Coastguard Worker                                LOG(WARNING) << "Null DexFile";
214*795d594fSAndroid Build Coastguard Worker                              } else {
215*795d594fSAndroid Build Coastguard Worker                                ret.push_back(cp_dex_file);
216*795d594fSAndroid Build Coastguard Worker                              }
217*795d594fSAndroid Build Coastguard Worker                              return true;
218*795d594fSAndroid Build Coastguard Worker                            });
219*795d594fSAndroid Build Coastguard Worker   return ret;
220*795d594fSAndroid Build Coastguard Worker }
221*795d594fSAndroid Build Coastguard Worker 
GetFirstDexFile(jobject jclass_loader)222*795d594fSAndroid Build Coastguard Worker const DexFile* CommonRuntimeTestImpl::GetFirstDexFile(jobject jclass_loader) {
223*795d594fSAndroid Build Coastguard Worker   std::vector<const DexFile*> tmp(GetDexFiles(jclass_loader));
224*795d594fSAndroid Build Coastguard Worker   DCHECK(!tmp.empty());
225*795d594fSAndroid Build Coastguard Worker   const DexFile* ret = tmp[0];
226*795d594fSAndroid Build Coastguard Worker   DCHECK(ret != nullptr);
227*795d594fSAndroid Build Coastguard Worker   return ret;
228*795d594fSAndroid Build Coastguard Worker }
229*795d594fSAndroid Build Coastguard Worker 
LoadMultiDex(const char * first_dex_name,const char * second_dex_name)230*795d594fSAndroid Build Coastguard Worker jobject CommonRuntimeTestImpl::LoadMultiDex(const char* first_dex_name,
231*795d594fSAndroid Build Coastguard Worker                                             const char* second_dex_name) {
232*795d594fSAndroid Build Coastguard Worker   std::vector<std::unique_ptr<const DexFile>> first_dex_files = OpenTestDexFiles(first_dex_name);
233*795d594fSAndroid Build Coastguard Worker   std::vector<std::unique_ptr<const DexFile>> second_dex_files = OpenTestDexFiles(second_dex_name);
234*795d594fSAndroid Build Coastguard Worker   std::vector<const DexFile*> class_path;
235*795d594fSAndroid Build Coastguard Worker   CHECK_NE(0U, first_dex_files.size());
236*795d594fSAndroid Build Coastguard Worker   CHECK_NE(0U, second_dex_files.size());
237*795d594fSAndroid Build Coastguard Worker   for (auto& dex_file : first_dex_files) {
238*795d594fSAndroid Build Coastguard Worker     class_path.push_back(dex_file.get());
239*795d594fSAndroid Build Coastguard Worker     loaded_dex_files_.push_back(std::move(dex_file));
240*795d594fSAndroid Build Coastguard Worker   }
241*795d594fSAndroid Build Coastguard Worker   for (auto& dex_file : second_dex_files) {
242*795d594fSAndroid Build Coastguard Worker     class_path.push_back(dex_file.get());
243*795d594fSAndroid Build Coastguard Worker     loaded_dex_files_.push_back(std::move(dex_file));
244*795d594fSAndroid Build Coastguard Worker   }
245*795d594fSAndroid Build Coastguard Worker 
246*795d594fSAndroid Build Coastguard Worker   Thread* self = Thread::Current();
247*795d594fSAndroid Build Coastguard Worker   jobject class_loader = Runtime::Current()->GetClassLinker()->CreatePathClassLoader(self,
248*795d594fSAndroid Build Coastguard Worker                                                                                      class_path);
249*795d594fSAndroid Build Coastguard Worker   self->SetClassLoaderOverride(class_loader);
250*795d594fSAndroid Build Coastguard Worker   return class_loader;
251*795d594fSAndroid Build Coastguard Worker }
252*795d594fSAndroid Build Coastguard Worker 
LoadDex(const char * dex_name)253*795d594fSAndroid Build Coastguard Worker jobject CommonRuntimeTestImpl::LoadDex(const char* dex_name) {
254*795d594fSAndroid Build Coastguard Worker   jobject class_loader = LoadDexInPathClassLoader(dex_name, nullptr);
255*795d594fSAndroid Build Coastguard Worker   Thread::Current()->SetClassLoaderOverride(class_loader);
256*795d594fSAndroid Build Coastguard Worker   return class_loader;
257*795d594fSAndroid Build Coastguard Worker }
258*795d594fSAndroid Build Coastguard Worker 
259*795d594fSAndroid Build Coastguard Worker jobject
LoadDexInWellKnownClassLoader(ScopedObjectAccess & soa,const std::vector<std::string> & dex_names,ObjPtr<mirror::Class> loader_class,jobject parent_loader,jobject shared_libraries,jobject shared_libraries_after)260*795d594fSAndroid Build Coastguard Worker CommonRuntimeTestImpl::LoadDexInWellKnownClassLoader(ScopedObjectAccess& soa,
261*795d594fSAndroid Build Coastguard Worker                                                      const std::vector<std::string>& dex_names,
262*795d594fSAndroid Build Coastguard Worker                                                      ObjPtr<mirror::Class> loader_class,
263*795d594fSAndroid Build Coastguard Worker                                                      jobject parent_loader,
264*795d594fSAndroid Build Coastguard Worker                                                      jobject shared_libraries,
265*795d594fSAndroid Build Coastguard Worker                                                      jobject shared_libraries_after) {
266*795d594fSAndroid Build Coastguard Worker   std::vector<const DexFile*> class_path;
267*795d594fSAndroid Build Coastguard Worker   for (const std::string& dex_name : dex_names) {
268*795d594fSAndroid Build Coastguard Worker     std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles(dex_name.c_str());
269*795d594fSAndroid Build Coastguard Worker     CHECK_NE(0U, dex_files.size());
270*795d594fSAndroid Build Coastguard Worker     for (auto& dex_file : dex_files) {
271*795d594fSAndroid Build Coastguard Worker       class_path.push_back(dex_file.get());
272*795d594fSAndroid Build Coastguard Worker       loaded_dex_files_.push_back(std::move(dex_file));
273*795d594fSAndroid Build Coastguard Worker     }
274*795d594fSAndroid Build Coastguard Worker   }
275*795d594fSAndroid Build Coastguard Worker   StackHandleScope<4> hs(soa.Self());
276*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> h_loader_class = hs.NewHandle(loader_class);
277*795d594fSAndroid Build Coastguard Worker   Handle<mirror::ClassLoader> h_parent_loader =
278*795d594fSAndroid Build Coastguard Worker       hs.NewHandle(soa.Decode<mirror::ClassLoader>(parent_loader));
279*795d594fSAndroid Build Coastguard Worker   Handle<mirror::ObjectArray<mirror::ClassLoader>> h_shared_libraries =
280*795d594fSAndroid Build Coastguard Worker       hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::ClassLoader>>(shared_libraries));
281*795d594fSAndroid Build Coastguard Worker   Handle<mirror::ObjectArray<mirror::ClassLoader>> h_shared_libraries_after =
282*795d594fSAndroid Build Coastguard Worker       hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::ClassLoader>>(shared_libraries_after));
283*795d594fSAndroid Build Coastguard Worker 
284*795d594fSAndroid Build Coastguard Worker   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
285*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::ClassLoader> result = class_linker->CreateWellKnownClassLoader(
286*795d594fSAndroid Build Coastguard Worker       soa.Self(),
287*795d594fSAndroid Build Coastguard Worker       class_path,
288*795d594fSAndroid Build Coastguard Worker       h_loader_class,
289*795d594fSAndroid Build Coastguard Worker       h_parent_loader,
290*795d594fSAndroid Build Coastguard Worker       h_shared_libraries,
291*795d594fSAndroid Build Coastguard Worker       h_shared_libraries_after);
292*795d594fSAndroid Build Coastguard Worker 
293*795d594fSAndroid Build Coastguard Worker   {
294*795d594fSAndroid Build Coastguard Worker     // Verify we build the correct chain.
295*795d594fSAndroid Build Coastguard Worker 
296*795d594fSAndroid Build Coastguard Worker     // Verify that the result has the correct class.
297*795d594fSAndroid Build Coastguard Worker     CHECK_EQ(h_loader_class.Get(), result->GetClass());
298*795d594fSAndroid Build Coastguard Worker     // Verify that the parent is not null. The boot class loader will be set up as a
299*795d594fSAndroid Build Coastguard Worker     // proper object.
300*795d594fSAndroid Build Coastguard Worker     ObjPtr<mirror::ClassLoader> actual_parent(result->GetParent());
301*795d594fSAndroid Build Coastguard Worker     CHECK(actual_parent != nullptr);
302*795d594fSAndroid Build Coastguard Worker 
303*795d594fSAndroid Build Coastguard Worker     if (parent_loader != nullptr) {
304*795d594fSAndroid Build Coastguard Worker       // We were given a parent. Verify that it's what we expect.
305*795d594fSAndroid Build Coastguard Worker       CHECK_EQ(h_parent_loader.Get(), actual_parent);
306*795d594fSAndroid Build Coastguard Worker     } else {
307*795d594fSAndroid Build Coastguard Worker       // No parent given. The parent must be the BootClassLoader.
308*795d594fSAndroid Build Coastguard Worker       CHECK(class_linker->IsBootClassLoader(actual_parent));
309*795d594fSAndroid Build Coastguard Worker     }
310*795d594fSAndroid Build Coastguard Worker   }
311*795d594fSAndroid Build Coastguard Worker 
312*795d594fSAndroid Build Coastguard Worker   return soa.Env()->GetVm()->AddGlobalRef(soa.Self(), result);
313*795d594fSAndroid Build Coastguard Worker }
314*795d594fSAndroid Build Coastguard Worker 
LoadDexInPathClassLoader(const std::string & dex_name,jobject parent_loader,jobject shared_libraries,jobject shared_libraries_after)315*795d594fSAndroid Build Coastguard Worker jobject CommonRuntimeTestImpl::LoadDexInPathClassLoader(const std::string& dex_name,
316*795d594fSAndroid Build Coastguard Worker                                                         jobject parent_loader,
317*795d594fSAndroid Build Coastguard Worker                                                         jobject shared_libraries,
318*795d594fSAndroid Build Coastguard Worker                                                         jobject shared_libraries_after) {
319*795d594fSAndroid Build Coastguard Worker   return LoadDexInPathClassLoader(std::vector<std::string>{ dex_name },
320*795d594fSAndroid Build Coastguard Worker                                   parent_loader,
321*795d594fSAndroid Build Coastguard Worker                                   shared_libraries,
322*795d594fSAndroid Build Coastguard Worker                                   shared_libraries_after);
323*795d594fSAndroid Build Coastguard Worker }
324*795d594fSAndroid Build Coastguard Worker 
LoadDexInPathClassLoader(const std::vector<std::string> & names,jobject parent_loader,jobject shared_libraries,jobject shared_libraries_after)325*795d594fSAndroid Build Coastguard Worker jobject CommonRuntimeTestImpl::LoadDexInPathClassLoader(const std::vector<std::string>& names,
326*795d594fSAndroid Build Coastguard Worker                                                         jobject parent_loader,
327*795d594fSAndroid Build Coastguard Worker                                                         jobject shared_libraries,
328*795d594fSAndroid Build Coastguard Worker                                                         jobject shared_libraries_after) {
329*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
330*795d594fSAndroid Build Coastguard Worker   return LoadDexInWellKnownClassLoader(soa,
331*795d594fSAndroid Build Coastguard Worker                                        names,
332*795d594fSAndroid Build Coastguard Worker                                        WellKnownClasses::dalvik_system_PathClassLoader.Get(),
333*795d594fSAndroid Build Coastguard Worker                                        parent_loader,
334*795d594fSAndroid Build Coastguard Worker                                        shared_libraries,
335*795d594fSAndroid Build Coastguard Worker                                        shared_libraries_after);
336*795d594fSAndroid Build Coastguard Worker }
337*795d594fSAndroid Build Coastguard Worker 
LoadDexInDelegateLastClassLoader(const std::string & dex_name,jobject parent_loader)338*795d594fSAndroid Build Coastguard Worker jobject CommonRuntimeTestImpl::LoadDexInDelegateLastClassLoader(const std::string& dex_name,
339*795d594fSAndroid Build Coastguard Worker                                                                 jobject parent_loader) {
340*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
341*795d594fSAndroid Build Coastguard Worker   return LoadDexInWellKnownClassLoader(
342*795d594fSAndroid Build Coastguard Worker       soa,
343*795d594fSAndroid Build Coastguard Worker       { dex_name },
344*795d594fSAndroid Build Coastguard Worker       WellKnownClasses::dalvik_system_DelegateLastClassLoader.Get(),
345*795d594fSAndroid Build Coastguard Worker       parent_loader);
346*795d594fSAndroid Build Coastguard Worker }
347*795d594fSAndroid Build Coastguard Worker 
LoadDexInInMemoryDexClassLoader(const std::string & dex_name,jobject parent_loader)348*795d594fSAndroid Build Coastguard Worker jobject CommonRuntimeTestImpl::LoadDexInInMemoryDexClassLoader(const std::string& dex_name,
349*795d594fSAndroid Build Coastguard Worker                                                                jobject parent_loader) {
350*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
351*795d594fSAndroid Build Coastguard Worker   return LoadDexInWellKnownClassLoader(
352*795d594fSAndroid Build Coastguard Worker       soa,
353*795d594fSAndroid Build Coastguard Worker       { dex_name },
354*795d594fSAndroid Build Coastguard Worker       WellKnownClasses::dalvik_system_InMemoryDexClassLoader.Get(),
355*795d594fSAndroid Build Coastguard Worker       parent_loader);
356*795d594fSAndroid Build Coastguard Worker }
357*795d594fSAndroid Build Coastguard Worker 
FillHeap(Thread * self,ClassLinker * class_linker,VariableSizedHandleScope * handle_scope)358*795d594fSAndroid Build Coastguard Worker void CommonRuntimeTestImpl::FillHeap(Thread* self,
359*795d594fSAndroid Build Coastguard Worker                                      ClassLinker* class_linker,
360*795d594fSAndroid Build Coastguard Worker                                      VariableSizedHandleScope* handle_scope) {
361*795d594fSAndroid Build Coastguard Worker   DCHECK(handle_scope != nullptr);
362*795d594fSAndroid Build Coastguard Worker 
363*795d594fSAndroid Build Coastguard Worker   Runtime::Current()->GetHeap()->SetIdealFootprint(1 * GB);
364*795d594fSAndroid Build Coastguard Worker 
365*795d594fSAndroid Build Coastguard Worker   // Class java.lang.Object.
366*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> c(handle_scope->NewHandle(
367*795d594fSAndroid Build Coastguard Worker       class_linker->FindSystemClass(self, "Ljava/lang/Object;")));
368*795d594fSAndroid Build Coastguard Worker   // Array helps to fill memory faster.
369*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> ca(handle_scope->NewHandle(
370*795d594fSAndroid Build Coastguard Worker       class_linker->FindSystemClass(self, "[Ljava/lang/Object;")));
371*795d594fSAndroid Build Coastguard Worker 
372*795d594fSAndroid Build Coastguard Worker   // Start allocating with ~128K
373*795d594fSAndroid Build Coastguard Worker   size_t length = 128 * KB;
374*795d594fSAndroid Build Coastguard Worker   while (length > 40) {
375*795d594fSAndroid Build Coastguard Worker     const int32_t array_length = length / 4;  // Object[] has elements of size 4.
376*795d594fSAndroid Build Coastguard Worker     MutableHandle<mirror::Object> h(handle_scope->NewHandle<mirror::Object>(
377*795d594fSAndroid Build Coastguard Worker         mirror::ObjectArray<mirror::Object>::Alloc(self, ca.Get(), array_length)));
378*795d594fSAndroid Build Coastguard Worker     if (self->IsExceptionPending() || h == nullptr) {
379*795d594fSAndroid Build Coastguard Worker       self->ClearException();
380*795d594fSAndroid Build Coastguard Worker 
381*795d594fSAndroid Build Coastguard Worker       // Try a smaller length
382*795d594fSAndroid Build Coastguard Worker       length = length / 2;
383*795d594fSAndroid Build Coastguard Worker       // Use at most a quarter the reported free space.
384*795d594fSAndroid Build Coastguard Worker       size_t mem = Runtime::Current()->GetHeap()->GetFreeMemory();
385*795d594fSAndroid Build Coastguard Worker       if (length * 4 > mem) {
386*795d594fSAndroid Build Coastguard Worker         length = mem / 4;
387*795d594fSAndroid Build Coastguard Worker       }
388*795d594fSAndroid Build Coastguard Worker     }
389*795d594fSAndroid Build Coastguard Worker   }
390*795d594fSAndroid Build Coastguard Worker 
391*795d594fSAndroid Build Coastguard Worker   // Allocate simple objects till it fails.
392*795d594fSAndroid Build Coastguard Worker   while (!self->IsExceptionPending()) {
393*795d594fSAndroid Build Coastguard Worker     handle_scope->NewHandle<mirror::Object>(c->AllocObject(self));
394*795d594fSAndroid Build Coastguard Worker   }
395*795d594fSAndroid Build Coastguard Worker   self->ClearException();
396*795d594fSAndroid Build Coastguard Worker }
397*795d594fSAndroid Build Coastguard Worker 
SetUpRuntimeOptionsForFillHeap(RuntimeOptions * options)398*795d594fSAndroid Build Coastguard Worker void CommonRuntimeTestImpl::SetUpRuntimeOptionsForFillHeap(RuntimeOptions *options) {
399*795d594fSAndroid Build Coastguard Worker   // Use a smaller heap
400*795d594fSAndroid Build Coastguard Worker   bool found = false;
401*795d594fSAndroid Build Coastguard Worker   for (std::pair<std::string, const void*>& pair : *options) {
402*795d594fSAndroid Build Coastguard Worker     if (pair.first.find("-Xmx") == 0) {
403*795d594fSAndroid Build Coastguard Worker       pair.first = "-Xmx4M";  // Smallest we can go.
404*795d594fSAndroid Build Coastguard Worker       found = true;
405*795d594fSAndroid Build Coastguard Worker     }
406*795d594fSAndroid Build Coastguard Worker   }
407*795d594fSAndroid Build Coastguard Worker   if (!found) {
408*795d594fSAndroid Build Coastguard Worker     options->emplace_back("-Xmx4M", nullptr);
409*795d594fSAndroid Build Coastguard Worker   }
410*795d594fSAndroid Build Coastguard Worker }
411*795d594fSAndroid Build Coastguard Worker 
MakeInterpreted(ObjPtr<mirror::Class> klass)412*795d594fSAndroid Build Coastguard Worker void CommonRuntimeTestImpl::MakeInterpreted(ObjPtr<mirror::Class> klass) {
413*795d594fSAndroid Build Coastguard Worker   PointerSize pointer_size = class_linker_->GetImagePointerSize();
414*795d594fSAndroid Build Coastguard Worker   for (ArtMethod& method : klass->GetMethods(pointer_size)) {
415*795d594fSAndroid Build Coastguard Worker     Runtime::Current()->GetInstrumentation()->InitializeMethodsCode(&method, /*aot_code=*/ nullptr);
416*795d594fSAndroid Build Coastguard Worker   }
417*795d594fSAndroid Build Coastguard Worker }
418*795d594fSAndroid Build Coastguard Worker 
StartDex2OatCommandLine(std::vector<std::string> * argv,std::string * error_msg,bool use_runtime_bcp_and_image)419*795d594fSAndroid Build Coastguard Worker bool CommonRuntimeTestImpl::StartDex2OatCommandLine(/*out*/std::vector<std::string>* argv,
420*795d594fSAndroid Build Coastguard Worker                                                     /*out*/std::string* error_msg,
421*795d594fSAndroid Build Coastguard Worker                                                     bool use_runtime_bcp_and_image) {
422*795d594fSAndroid Build Coastguard Worker   DCHECK(argv != nullptr);
423*795d594fSAndroid Build Coastguard Worker   DCHECK(argv->empty());
424*795d594fSAndroid Build Coastguard Worker 
425*795d594fSAndroid Build Coastguard Worker   Runtime* runtime = Runtime::Current();
426*795d594fSAndroid Build Coastguard Worker   if (use_runtime_bcp_and_image && runtime->GetHeap()->GetBootImageSpaces().empty()) {
427*795d594fSAndroid Build Coastguard Worker     *error_msg = "No image location found for Dex2Oat.";
428*795d594fSAndroid Build Coastguard Worker     return false;
429*795d594fSAndroid Build Coastguard Worker   }
430*795d594fSAndroid Build Coastguard Worker 
431*795d594fSAndroid Build Coastguard Worker   argv->push_back(runtime->GetCompilerExecutable());
432*795d594fSAndroid Build Coastguard Worker   if (runtime->IsJavaDebuggable()) {
433*795d594fSAndroid Build Coastguard Worker     argv->push_back("--debuggable");
434*795d594fSAndroid Build Coastguard Worker   }
435*795d594fSAndroid Build Coastguard Worker   runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(argv);
436*795d594fSAndroid Build Coastguard Worker 
437*795d594fSAndroid Build Coastguard Worker   if (use_runtime_bcp_and_image) {
438*795d594fSAndroid Build Coastguard Worker     argv->push_back("--runtime-arg");
439*795d594fSAndroid Build Coastguard Worker     argv->push_back(GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()));
440*795d594fSAndroid Build Coastguard Worker     argv->push_back("--runtime-arg");
441*795d594fSAndroid Build Coastguard Worker     argv->push_back(GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()));
442*795d594fSAndroid Build Coastguard Worker 
443*795d594fSAndroid Build Coastguard Worker     const std::vector<gc::space::ImageSpace*>& image_spaces =
444*795d594fSAndroid Build Coastguard Worker         runtime->GetHeap()->GetBootImageSpaces();
445*795d594fSAndroid Build Coastguard Worker     DCHECK(!image_spaces.empty());
446*795d594fSAndroid Build Coastguard Worker     argv->push_back("--boot-image=" + image_spaces[0]->GetImageLocation());
447*795d594fSAndroid Build Coastguard Worker   }
448*795d594fSAndroid Build Coastguard Worker 
449*795d594fSAndroid Build Coastguard Worker   std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
450*795d594fSAndroid Build Coastguard Worker   argv->insert(argv->end(), compiler_options.begin(), compiler_options.end());
451*795d594fSAndroid Build Coastguard Worker   return true;
452*795d594fSAndroid Build Coastguard Worker }
453*795d594fSAndroid Build Coastguard Worker 
CompileBootImage(const std::vector<std::string> & extra_args,const std::string & image_file_name_prefix,ArrayRef<const std::string> dex_files,ArrayRef<const std::string> dex_locations,std::string * error_msg,const std::string & use_fd_prefix)454*795d594fSAndroid Build Coastguard Worker bool CommonRuntimeTestImpl::CompileBootImage(const std::vector<std::string>& extra_args,
455*795d594fSAndroid Build Coastguard Worker                                              const std::string& image_file_name_prefix,
456*795d594fSAndroid Build Coastguard Worker                                              ArrayRef<const std::string> dex_files,
457*795d594fSAndroid Build Coastguard Worker                                              ArrayRef<const std::string> dex_locations,
458*795d594fSAndroid Build Coastguard Worker                                              std::string* error_msg,
459*795d594fSAndroid Build Coastguard Worker                                              const std::string& use_fd_prefix) {
460*795d594fSAndroid Build Coastguard Worker   Runtime* const runtime = Runtime::Current();
461*795d594fSAndroid Build Coastguard Worker   std::vector<std::string> argv {
462*795d594fSAndroid Build Coastguard Worker     runtime->GetCompilerExecutable(),
463*795d594fSAndroid Build Coastguard Worker     "--runtime-arg",
464*795d594fSAndroid Build Coastguard Worker     "-Xms64m",
465*795d594fSAndroid Build Coastguard Worker     "--runtime-arg",
466*795d594fSAndroid Build Coastguard Worker     "-Xmx64m",
467*795d594fSAndroid Build Coastguard Worker     "--runtime-arg",
468*795d594fSAndroid Build Coastguard Worker     "-Xverify:softfail",
469*795d594fSAndroid Build Coastguard Worker     "--force-determinism",
470*795d594fSAndroid Build Coastguard Worker   };
471*795d594fSAndroid Build Coastguard Worker   CHECK_EQ(dex_files.size(), dex_locations.size());
472*795d594fSAndroid Build Coastguard Worker   for (const std::string& dex_file : dex_files) {
473*795d594fSAndroid Build Coastguard Worker     argv.push_back("--dex-file=" + dex_file);
474*795d594fSAndroid Build Coastguard Worker   }
475*795d594fSAndroid Build Coastguard Worker   for (const std::string& dex_location : dex_locations) {
476*795d594fSAndroid Build Coastguard Worker     argv.push_back("--dex-location=" + dex_location);
477*795d594fSAndroid Build Coastguard Worker   }
478*795d594fSAndroid Build Coastguard Worker   if (runtime->IsJavaDebuggable()) {
479*795d594fSAndroid Build Coastguard Worker     argv.push_back("--debuggable");
480*795d594fSAndroid Build Coastguard Worker   }
481*795d594fSAndroid Build Coastguard Worker   runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
482*795d594fSAndroid Build Coastguard Worker 
483*795d594fSAndroid Build Coastguard Worker   if (!kIsTargetBuild) {
484*795d594fSAndroid Build Coastguard Worker     argv.push_back("--host");
485*795d594fSAndroid Build Coastguard Worker   }
486*795d594fSAndroid Build Coastguard Worker 
487*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<File> art_file;
488*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<File> vdex_file;
489*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<File> oat_file;
490*795d594fSAndroid Build Coastguard Worker   if (!use_fd_prefix.empty()) {
491*795d594fSAndroid Build Coastguard Worker     art_file.reset(OS::CreateEmptyFile((use_fd_prefix + ".art").c_str()));
492*795d594fSAndroid Build Coastguard Worker     vdex_file.reset(OS::CreateEmptyFile((use_fd_prefix + ".vdex").c_str()));
493*795d594fSAndroid Build Coastguard Worker     oat_file.reset(OS::CreateEmptyFile((use_fd_prefix + ".oat").c_str()));
494*795d594fSAndroid Build Coastguard Worker     argv.push_back("--image-fd=" + std::to_string(art_file->Fd()));
495*795d594fSAndroid Build Coastguard Worker     argv.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
496*795d594fSAndroid Build Coastguard Worker     argv.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
497*795d594fSAndroid Build Coastguard Worker     argv.push_back("--oat-location=" + image_file_name_prefix + ".oat");
498*795d594fSAndroid Build Coastguard Worker   } else {
499*795d594fSAndroid Build Coastguard Worker     argv.push_back("--image=" + image_file_name_prefix + ".art");
500*795d594fSAndroid Build Coastguard Worker     argv.push_back("--oat-file=" + image_file_name_prefix + ".oat");
501*795d594fSAndroid Build Coastguard Worker     argv.push_back("--oat-location=" + image_file_name_prefix + ".oat");
502*795d594fSAndroid Build Coastguard Worker   }
503*795d594fSAndroid Build Coastguard Worker 
504*795d594fSAndroid Build Coastguard Worker   std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
505*795d594fSAndroid Build Coastguard Worker   argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
506*795d594fSAndroid Build Coastguard Worker 
507*795d594fSAndroid Build Coastguard Worker   // We must set --android-root.
508*795d594fSAndroid Build Coastguard Worker   const char* android_root = getenv("ANDROID_ROOT");
509*795d594fSAndroid Build Coastguard Worker   CHECK(android_root != nullptr);
510*795d594fSAndroid Build Coastguard Worker   argv.push_back("--android-root=" + std::string(android_root));
511*795d594fSAndroid Build Coastguard Worker   argv.insert(argv.end(), extra_args.begin(), extra_args.end());
512*795d594fSAndroid Build Coastguard Worker 
513*795d594fSAndroid Build Coastguard Worker   bool result = RunDex2Oat(argv, error_msg);
514*795d594fSAndroid Build Coastguard Worker   if (art_file != nullptr) {
515*795d594fSAndroid Build Coastguard Worker     CHECK_EQ(0, art_file->FlushClose());
516*795d594fSAndroid Build Coastguard Worker   }
517*795d594fSAndroid Build Coastguard Worker   if (vdex_file != nullptr) {
518*795d594fSAndroid Build Coastguard Worker     CHECK_EQ(0, vdex_file->FlushClose());
519*795d594fSAndroid Build Coastguard Worker   }
520*795d594fSAndroid Build Coastguard Worker   if (oat_file != nullptr) {
521*795d594fSAndroid Build Coastguard Worker     CHECK_EQ(0, oat_file->FlushClose());
522*795d594fSAndroid Build Coastguard Worker   }
523*795d594fSAndroid Build Coastguard Worker   return result;
524*795d594fSAndroid Build Coastguard Worker }
525*795d594fSAndroid Build Coastguard Worker 
RunDex2Oat(const std::vector<std::string> & args,std::string * error_msg)526*795d594fSAndroid Build Coastguard Worker bool CommonRuntimeTestImpl::RunDex2Oat(const std::vector<std::string>& args,
527*795d594fSAndroid Build Coastguard Worker                                        std::string* error_msg) {
528*795d594fSAndroid Build Coastguard Worker   // We only want fatal logging for the error message.
529*795d594fSAndroid Build Coastguard Worker   auto post_fork_fn = []() { return setenv("ANDROID_LOG_TAGS", "*:f", 1) == 0; };
530*795d594fSAndroid Build Coastguard Worker   ForkAndExecResult res = ForkAndExec(args, post_fork_fn, error_msg);
531*795d594fSAndroid Build Coastguard Worker   if (res.stage != ForkAndExecResult::kFinished) {
532*795d594fSAndroid Build Coastguard Worker     *error_msg = strerror(errno);
533*795d594fSAndroid Build Coastguard Worker     return false;
534*795d594fSAndroid Build Coastguard Worker   }
535*795d594fSAndroid Build Coastguard Worker   return res.StandardSuccess();
536*795d594fSAndroid Build Coastguard Worker }
537*795d594fSAndroid Build Coastguard Worker 
GetImageLocation()538*795d594fSAndroid Build Coastguard Worker std::string CommonRuntimeTestImpl::GetImageLocation() {
539*795d594fSAndroid Build Coastguard Worker   return GetImageDirectory() + "/boot.art";
540*795d594fSAndroid Build Coastguard Worker }
541*795d594fSAndroid Build Coastguard Worker 
GetSystemImageFile()542*795d594fSAndroid Build Coastguard Worker std::string CommonRuntimeTestImpl::GetSystemImageFile() {
543*795d594fSAndroid Build Coastguard Worker   std::string isa = GetInstructionSetString(kRuntimeISA);
544*795d594fSAndroid Build Coastguard Worker   return GetImageDirectory() + "/" + isa + "/boot.art";
545*795d594fSAndroid Build Coastguard Worker }
546*795d594fSAndroid Build Coastguard Worker 
VisitDexes(ArrayRef<const std::string> dexes,const std::function<void (MethodReference)> & method_visitor,const std::function<void (TypeReference)> & class_visitor,size_t method_frequency,size_t class_frequency)547*795d594fSAndroid Build Coastguard Worker void CommonRuntimeTestImpl::VisitDexes(ArrayRef<const std::string> dexes,
548*795d594fSAndroid Build Coastguard Worker                                        const std::function<void(MethodReference)>& method_visitor,
549*795d594fSAndroid Build Coastguard Worker                                        const std::function<void(TypeReference)>& class_visitor,
550*795d594fSAndroid Build Coastguard Worker                                        size_t method_frequency,
551*795d594fSAndroid Build Coastguard Worker                                        size_t class_frequency) {
552*795d594fSAndroid Build Coastguard Worker   size_t method_counter = 0;
553*795d594fSAndroid Build Coastguard Worker   size_t class_counter = 0;
554*795d594fSAndroid Build Coastguard Worker   for (const std::string& dex : dexes) {
555*795d594fSAndroid Build Coastguard Worker     std::vector<std::unique_ptr<const DexFile>> dex_files;
556*795d594fSAndroid Build Coastguard Worker     std::string error_msg;
557*795d594fSAndroid Build Coastguard Worker     ArtDexFileLoader dex_file_loader(dex);
558*795d594fSAndroid Build Coastguard Worker     CHECK(dex_file_loader.Open(/*verify*/ true,
559*795d594fSAndroid Build Coastguard Worker                                /*verify_checksum*/ false,
560*795d594fSAndroid Build Coastguard Worker                                &error_msg,
561*795d594fSAndroid Build Coastguard Worker                                &dex_files))
562*795d594fSAndroid Build Coastguard Worker         << error_msg;
563*795d594fSAndroid Build Coastguard Worker     for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
564*795d594fSAndroid Build Coastguard Worker       for (size_t i = 0; i < dex_file->NumMethodIds(); ++i) {
565*795d594fSAndroid Build Coastguard Worker         if (++method_counter % method_frequency == 0) {
566*795d594fSAndroid Build Coastguard Worker           method_visitor(MethodReference(dex_file.get(), i));
567*795d594fSAndroid Build Coastguard Worker         }
568*795d594fSAndroid Build Coastguard Worker       }
569*795d594fSAndroid Build Coastguard Worker       for (size_t i = 0; i < dex_file->NumTypeIds(); ++i) {
570*795d594fSAndroid Build Coastguard Worker         if (++class_counter % class_frequency == 0) {
571*795d594fSAndroid Build Coastguard Worker           class_visitor(TypeReference(dex_file.get(), dex::TypeIndex(i)));
572*795d594fSAndroid Build Coastguard Worker         }
573*795d594fSAndroid Build Coastguard Worker       }
574*795d594fSAndroid Build Coastguard Worker     }
575*795d594fSAndroid Build Coastguard Worker   }
576*795d594fSAndroid Build Coastguard Worker }
577*795d594fSAndroid Build Coastguard Worker 
GenerateProfile(ArrayRef<const std::string> dexes,File * out_file,size_t method_frequency,size_t type_frequency,bool for_boot_image)578*795d594fSAndroid Build Coastguard Worker void CommonRuntimeTestImpl::GenerateProfile(ArrayRef<const std::string> dexes,
579*795d594fSAndroid Build Coastguard Worker                                             File* out_file,
580*795d594fSAndroid Build Coastguard Worker                                             size_t method_frequency,
581*795d594fSAndroid Build Coastguard Worker                                             size_t type_frequency,
582*795d594fSAndroid Build Coastguard Worker                                             bool for_boot_image) {
583*795d594fSAndroid Build Coastguard Worker   ProfileCompilationInfo profile(for_boot_image);
584*795d594fSAndroid Build Coastguard Worker   VisitDexes(
585*795d594fSAndroid Build Coastguard Worker       dexes,
586*795d594fSAndroid Build Coastguard Worker       [&profile](MethodReference ref) {
587*795d594fSAndroid Build Coastguard Worker         uint32_t flags = ProfileCompilationInfo::MethodHotness::kFlagHot |
588*795d594fSAndroid Build Coastguard Worker             ProfileCompilationInfo::MethodHotness::kFlagStartup;
589*795d594fSAndroid Build Coastguard Worker         EXPECT_TRUE(profile.AddMethod(
590*795d594fSAndroid Build Coastguard Worker             ProfileMethodInfo(ref),
591*795d594fSAndroid Build Coastguard Worker             static_cast<ProfileCompilationInfo::MethodHotness::Flag>(flags)));
592*795d594fSAndroid Build Coastguard Worker       },
593*795d594fSAndroid Build Coastguard Worker       [&profile](TypeReference ref) {
594*795d594fSAndroid Build Coastguard Worker         std::set<dex::TypeIndex> classes;
595*795d594fSAndroid Build Coastguard Worker         classes.insert(ref.TypeIndex());
596*795d594fSAndroid Build Coastguard Worker         EXPECT_TRUE(profile.AddClassesForDex(ref.dex_file, classes.begin(), classes.end()));
597*795d594fSAndroid Build Coastguard Worker       },
598*795d594fSAndroid Build Coastguard Worker       method_frequency,
599*795d594fSAndroid Build Coastguard Worker       type_frequency);
600*795d594fSAndroid Build Coastguard Worker   profile.Save(out_file->Fd());
601*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(out_file->Flush(), 0);
602*795d594fSAndroid Build Coastguard Worker }
603*795d594fSAndroid Build Coastguard Worker 
FindClass(const char * descriptor,Handle<mirror::ClassLoader> class_loader) const604*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> CommonRuntimeTestImpl::FindClass(
605*795d594fSAndroid Build Coastguard Worker     const char* descriptor,
606*795d594fSAndroid Build Coastguard Worker     Handle<mirror::ClassLoader> class_loader) const {
607*795d594fSAndroid Build Coastguard Worker   return class_linker_->FindClass(Thread::Current(), descriptor, strlen(descriptor), class_loader);
608*795d594fSAndroid Build Coastguard Worker }
609*795d594fSAndroid Build Coastguard Worker 
CheckJniAbortCatcher()610*795d594fSAndroid Build Coastguard Worker CheckJniAbortCatcher::CheckJniAbortCatcher() : vm_(Runtime::Current()->GetJavaVM()) {
611*795d594fSAndroid Build Coastguard Worker   vm_->SetCheckJniAbortHook(Hook, &actual_);
612*795d594fSAndroid Build Coastguard Worker }
613*795d594fSAndroid Build Coastguard Worker 
~CheckJniAbortCatcher()614*795d594fSAndroid Build Coastguard Worker CheckJniAbortCatcher::~CheckJniAbortCatcher() {
615*795d594fSAndroid Build Coastguard Worker   vm_->SetCheckJniAbortHook(nullptr, nullptr);
616*795d594fSAndroid Build Coastguard Worker   EXPECT_TRUE(actual_.empty()) << actual_;
617*795d594fSAndroid Build Coastguard Worker }
618*795d594fSAndroid Build Coastguard Worker 
Check(const std::string & expected_text)619*795d594fSAndroid Build Coastguard Worker void CheckJniAbortCatcher::Check(const std::string& expected_text) {
620*795d594fSAndroid Build Coastguard Worker   Check(expected_text.c_str());
621*795d594fSAndroid Build Coastguard Worker }
622*795d594fSAndroid Build Coastguard Worker 
Check(const char * expected_text)623*795d594fSAndroid Build Coastguard Worker void CheckJniAbortCatcher::Check(const char* expected_text) {
624*795d594fSAndroid Build Coastguard Worker   EXPECT_TRUE(actual_.find(expected_text) != std::string::npos) << "\n"
625*795d594fSAndroid Build Coastguard Worker       << "Expected to find: " << expected_text << "\n"
626*795d594fSAndroid Build Coastguard Worker       << "In the output   : " << actual_;
627*795d594fSAndroid Build Coastguard Worker   actual_.clear();
628*795d594fSAndroid Build Coastguard Worker }
629*795d594fSAndroid Build Coastguard Worker 
Hook(void * data,const std::string & reason)630*795d594fSAndroid Build Coastguard Worker void CheckJniAbortCatcher::Hook(void* data, const std::string& reason) {
631*795d594fSAndroid Build Coastguard Worker   // We use += because when we're hooking the aborts like this, multiple problems can be found.
632*795d594fSAndroid Build Coastguard Worker   *reinterpret_cast<std::string*>(data) += reason;
633*795d594fSAndroid Build Coastguard Worker }
634*795d594fSAndroid Build Coastguard Worker 
635*795d594fSAndroid Build Coastguard Worker }  // namespace art
636