xref: /aosp_15_r20/art/dex2oat/driver/compiler_driver_test.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2011 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 "driver/compiler_driver.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include <limits>
20*795d594fSAndroid Build Coastguard Worker #include <stdint.h>
21*795d594fSAndroid Build Coastguard Worker #include <stdio.h>
22*795d594fSAndroid Build Coastguard Worker #include <memory>
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker #include "art_method-inl.h"
25*795d594fSAndroid Build Coastguard Worker #include "base/casts.h"
26*795d594fSAndroid Build Coastguard Worker #include "class_linker-inl.h"
27*795d594fSAndroid Build Coastguard Worker #include "common_compiler_driver_test.h"
28*795d594fSAndroid Build Coastguard Worker #include "compiled_method-inl.h"
29*795d594fSAndroid Build Coastguard Worker #include "compiler_callbacks.h"
30*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file.h"
31*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file_types.h"
32*795d594fSAndroid Build Coastguard Worker #include "gc/heap.h"
33*795d594fSAndroid Build Coastguard Worker #include "handle_scope-inl.h"
34*795d594fSAndroid Build Coastguard Worker #include "mirror/class-inl.h"
35*795d594fSAndroid Build Coastguard Worker #include "mirror/class_loader.h"
36*795d594fSAndroid Build Coastguard Worker #include "mirror/dex_cache-inl.h"
37*795d594fSAndroid Build Coastguard Worker #include "mirror/object-inl.h"
38*795d594fSAndroid Build Coastguard Worker #include "mirror/object_array-inl.h"
39*795d594fSAndroid Build Coastguard Worker #include "profile/profile_compilation_info.h"
40*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
41*795d594fSAndroid Build Coastguard Worker 
42*795d594fSAndroid Build Coastguard Worker namespace art {
43*795d594fSAndroid Build Coastguard Worker 
44*795d594fSAndroid Build Coastguard Worker class CompilerDriverTest : public CommonCompilerDriverTest {
45*795d594fSAndroid Build Coastguard Worker  protected:
CompileAllAndMakeExecutable(jobject class_loader)46*795d594fSAndroid Build Coastguard Worker   void CompileAllAndMakeExecutable(jobject class_loader) REQUIRES(!Locks::mutator_lock_) {
47*795d594fSAndroid Build Coastguard Worker     TimingLogger timings("CompilerDriverTest::CompileAllAndMakeExecutable", false, false);
48*795d594fSAndroid Build Coastguard Worker     dex_files_ = GetDexFiles(class_loader);
49*795d594fSAndroid Build Coastguard Worker     CompileAll(class_loader, dex_files_, &timings);
50*795d594fSAndroid Build Coastguard Worker     TimingLogger::ScopedTiming t("MakeAllExecutable", &timings);
51*795d594fSAndroid Build Coastguard Worker     MakeAllExecutable(class_loader);
52*795d594fSAndroid Build Coastguard Worker   }
53*795d594fSAndroid Build Coastguard Worker 
EnsureCompiled(jobject class_loader,const char * class_name,const char * method,const char * signature,bool is_virtual)54*795d594fSAndroid Build Coastguard Worker   void EnsureCompiled(jobject class_loader, const char* class_name, const char* method,
55*795d594fSAndroid Build Coastguard Worker                       const char* signature, bool is_virtual)
56*795d594fSAndroid Build Coastguard Worker       REQUIRES(!Locks::mutator_lock_) {
57*795d594fSAndroid Build Coastguard Worker     CompileAllAndMakeExecutable(class_loader);
58*795d594fSAndroid Build Coastguard Worker     Thread::Current()->TransitionFromSuspendedToRunnable();
59*795d594fSAndroid Build Coastguard Worker     bool started = runtime_->Start();
60*795d594fSAndroid Build Coastguard Worker     CHECK(started);
61*795d594fSAndroid Build Coastguard Worker     env_ = Thread::Current()->GetJniEnv();
62*795d594fSAndroid Build Coastguard Worker     class_ = env_->FindClass(class_name);
63*795d594fSAndroid Build Coastguard Worker     CHECK(class_ != nullptr) << "Class not found: " << class_name;
64*795d594fSAndroid Build Coastguard Worker     if (is_virtual) {
65*795d594fSAndroid Build Coastguard Worker       mid_ = env_->GetMethodID(class_, method, signature);
66*795d594fSAndroid Build Coastguard Worker     } else {
67*795d594fSAndroid Build Coastguard Worker       mid_ = env_->GetStaticMethodID(class_, method, signature);
68*795d594fSAndroid Build Coastguard Worker     }
69*795d594fSAndroid Build Coastguard Worker     CHECK(mid_ != nullptr) << "Method not found: " << class_name << "." << method << signature;
70*795d594fSAndroid Build Coastguard Worker   }
71*795d594fSAndroid Build Coastguard Worker 
MakeAllExecutable(jobject class_loader)72*795d594fSAndroid Build Coastguard Worker   void MakeAllExecutable(jobject class_loader) {
73*795d594fSAndroid Build Coastguard Worker     const std::vector<const DexFile*> class_path = GetDexFiles(class_loader);
74*795d594fSAndroid Build Coastguard Worker     for (size_t i = 0; i != class_path.size(); ++i) {
75*795d594fSAndroid Build Coastguard Worker       const DexFile* dex_file = class_path[i];
76*795d594fSAndroid Build Coastguard Worker       CHECK(dex_file != nullptr);
77*795d594fSAndroid Build Coastguard Worker       MakeDexFileExecutable(class_loader, *dex_file);
78*795d594fSAndroid Build Coastguard Worker     }
79*795d594fSAndroid Build Coastguard Worker   }
80*795d594fSAndroid Build Coastguard Worker 
MakeExecutable(ArtMethod * method)81*795d594fSAndroid Build Coastguard Worker   void MakeExecutable(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) {
82*795d594fSAndroid Build Coastguard Worker     CHECK(method != nullptr);
83*795d594fSAndroid Build Coastguard Worker 
84*795d594fSAndroid Build Coastguard Worker     const void* method_code = nullptr;
85*795d594fSAndroid Build Coastguard Worker     if (!method->IsAbstract()) {
86*795d594fSAndroid Build Coastguard Worker       MethodReference method_ref(method->GetDexFile(), method->GetDexMethodIndex());
87*795d594fSAndroid Build Coastguard Worker       const CompiledMethod* compiled_method = compiler_driver_->GetCompiledMethod(method_ref);
88*795d594fSAndroid Build Coastguard Worker       // If the code size is 0 it means the method was skipped due to profile guided compilation.
89*795d594fSAndroid Build Coastguard Worker       if (compiled_method != nullptr && compiled_method->GetQuickCode().size() != 0u) {
90*795d594fSAndroid Build Coastguard Worker         method_code = CommonCompilerTest::MakeExecutable(compiled_method->GetQuickCode(),
91*795d594fSAndroid Build Coastguard Worker                                                          compiled_method->GetVmapTable(),
92*795d594fSAndroid Build Coastguard Worker                                                          compiled_method->GetInstructionSet());
93*795d594fSAndroid Build Coastguard Worker         LOG(INFO) << "MakeExecutable " << method->PrettyMethod() << " code=" << method_code;
94*795d594fSAndroid Build Coastguard Worker       }
95*795d594fSAndroid Build Coastguard Worker     }
96*795d594fSAndroid Build Coastguard Worker     runtime_->GetInstrumentation()->InitializeMethodsCode(method, /*aot_code=*/ method_code);
97*795d594fSAndroid Build Coastguard Worker   }
98*795d594fSAndroid Build Coastguard Worker 
MakeDexFileExecutable(jobject class_loader,const DexFile & dex_file)99*795d594fSAndroid Build Coastguard Worker   void MakeDexFileExecutable(jobject class_loader, const DexFile& dex_file) {
100*795d594fSAndroid Build Coastguard Worker     ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
101*795d594fSAndroid Build Coastguard Worker     for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
102*795d594fSAndroid Build Coastguard Worker       const dex::ClassDef& class_def = dex_file.GetClassDef(i);
103*795d594fSAndroid Build Coastguard Worker       const char* descriptor = dex_file.GetClassDescriptor(class_def);
104*795d594fSAndroid Build Coastguard Worker       ScopedObjectAccess soa(Thread::Current());
105*795d594fSAndroid Build Coastguard Worker       StackHandleScope<1> hs(soa.Self());
106*795d594fSAndroid Build Coastguard Worker       Handle<mirror::ClassLoader> loader(
107*795d594fSAndroid Build Coastguard Worker           hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)));
108*795d594fSAndroid Build Coastguard Worker       ObjPtr<mirror::Class> c = FindClass(descriptor, loader);
109*795d594fSAndroid Build Coastguard Worker       CHECK(c != nullptr);
110*795d594fSAndroid Build Coastguard Worker       const auto pointer_size = class_linker->GetImagePointerSize();
111*795d594fSAndroid Build Coastguard Worker       for (auto& m : c->GetMethods(pointer_size)) {
112*795d594fSAndroid Build Coastguard Worker         MakeExecutable(&m);
113*795d594fSAndroid Build Coastguard Worker       }
114*795d594fSAndroid Build Coastguard Worker     }
115*795d594fSAndroid Build Coastguard Worker   }
116*795d594fSAndroid Build Coastguard Worker 
117*795d594fSAndroid Build Coastguard Worker   JNIEnv* env_;
118*795d594fSAndroid Build Coastguard Worker   jclass class_;
119*795d594fSAndroid Build Coastguard Worker   jmethodID mid_;
120*795d594fSAndroid Build Coastguard Worker   std::vector<const DexFile*> dex_files_;
121*795d594fSAndroid Build Coastguard Worker };
122*795d594fSAndroid Build Coastguard Worker 
123*795d594fSAndroid Build Coastguard Worker // Disabled due to 10 second runtime on host
124*795d594fSAndroid Build Coastguard Worker // TODO: Update the test for hash-based dex cache arrays. Bug: 30627598
TEST_F(CompilerDriverTest,DISABLED_LARGE_CompileDexLibCore)125*795d594fSAndroid Build Coastguard Worker TEST_F(CompilerDriverTest, DISABLED_LARGE_CompileDexLibCore) {
126*795d594fSAndroid Build Coastguard Worker   CompileAllAndMakeExecutable(nullptr);
127*795d594fSAndroid Build Coastguard Worker 
128*795d594fSAndroid Build Coastguard Worker   // All libcore references should resolve
129*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
130*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(java_lang_dex_file_ != nullptr);
131*795d594fSAndroid Build Coastguard Worker   const DexFile& dex = *java_lang_dex_file_;
132*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::DexCache> dex_cache = class_linker_->FindDexCache(soa.Self(), dex);
133*795d594fSAndroid Build Coastguard Worker   for (size_t i = 0; i < dex_cache->NumStrings(); i++) {
134*795d594fSAndroid Build Coastguard Worker     const ObjPtr<mirror::String> string = dex_cache->GetResolvedString(dex::StringIndex(i));
135*795d594fSAndroid Build Coastguard Worker     EXPECT_TRUE(string != nullptr) << "string_idx=" << i;
136*795d594fSAndroid Build Coastguard Worker   }
137*795d594fSAndroid Build Coastguard Worker   for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
138*795d594fSAndroid Build Coastguard Worker     const ObjPtr<mirror::Class> type = dex_cache->GetResolvedType(dex::TypeIndex(i));
139*795d594fSAndroid Build Coastguard Worker     EXPECT_TRUE(type != nullptr)
140*795d594fSAndroid Build Coastguard Worker         << "type_idx=" << i << " " << dex.GetTypeDescriptor(dex.GetTypeId(dex::TypeIndex(i)));
141*795d594fSAndroid Build Coastguard Worker   }
142*795d594fSAndroid Build Coastguard Worker   for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
143*795d594fSAndroid Build Coastguard Worker     // FIXME: This is outdated for hash-based method array.
144*795d594fSAndroid Build Coastguard Worker     ArtMethod* method = dex_cache->GetResolvedMethod(i);
145*795d594fSAndroid Build Coastguard Worker     EXPECT_TRUE(method != nullptr) << "method_idx=" << i
146*795d594fSAndroid Build Coastguard Worker                                 << " " << dex.GetMethodDeclaringClassDescriptor(dex.GetMethodId(i))
147*795d594fSAndroid Build Coastguard Worker                                 << " " << dex.GetMethodName(dex.GetMethodId(i));
148*795d594fSAndroid Build Coastguard Worker     EXPECT_TRUE(method->GetEntryPointFromQuickCompiledCode() != nullptr) << "method_idx=" << i
149*795d594fSAndroid Build Coastguard Worker         << " " << dex.GetMethodDeclaringClassDescriptor(dex.GetMethodId(i)) << " "
150*795d594fSAndroid Build Coastguard Worker         << dex.GetMethodName(dex.GetMethodId(i));
151*795d594fSAndroid Build Coastguard Worker   }
152*795d594fSAndroid Build Coastguard Worker   for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
153*795d594fSAndroid Build Coastguard Worker     // FIXME: This is outdated for hash-based field array.
154*795d594fSAndroid Build Coastguard Worker     ArtField* field = dex_cache->GetResolvedField(i);
155*795d594fSAndroid Build Coastguard Worker     EXPECT_TRUE(field != nullptr) << "field_idx=" << i
156*795d594fSAndroid Build Coastguard Worker                                << " " << dex.GetFieldDeclaringClassDescriptor(dex.GetFieldId(i))
157*795d594fSAndroid Build Coastguard Worker                                << " " << dex.GetFieldName(dex.GetFieldId(i));
158*795d594fSAndroid Build Coastguard Worker   }
159*795d594fSAndroid Build Coastguard Worker 
160*795d594fSAndroid Build Coastguard Worker   // TODO check Class::IsVerified for all classes
161*795d594fSAndroid Build Coastguard Worker 
162*795d594fSAndroid Build Coastguard Worker   // TODO: check that all Method::GetCode() values are non-null
163*795d594fSAndroid Build Coastguard Worker }
164*795d594fSAndroid Build Coastguard Worker 
TEST_F(CompilerDriverTest,AbstractMethodErrorStub)165*795d594fSAndroid Build Coastguard Worker TEST_F(CompilerDriverTest, AbstractMethodErrorStub) {
166*795d594fSAndroid Build Coastguard Worker   jobject class_loader;
167*795d594fSAndroid Build Coastguard Worker   {
168*795d594fSAndroid Build Coastguard Worker     ScopedObjectAccess soa(Thread::Current());
169*795d594fSAndroid Build Coastguard Worker     class_loader = LoadDex("AbstractMethod");
170*795d594fSAndroid Build Coastguard Worker   }
171*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(class_loader != nullptr);
172*795d594fSAndroid Build Coastguard Worker   EnsureCompiled(class_loader, "AbstractClass", "foo", "()V", true);
173*795d594fSAndroid Build Coastguard Worker 
174*795d594fSAndroid Build Coastguard Worker   // Create a jobj_ of ConcreteClass, NOT AbstractClass.
175*795d594fSAndroid Build Coastguard Worker   jclass c_class = env_->FindClass("ConcreteClass");
176*795d594fSAndroid Build Coastguard Worker 
177*795d594fSAndroid Build Coastguard Worker   jmethodID constructor = env_->GetMethodID(c_class, "<init>", "()V");
178*795d594fSAndroid Build Coastguard Worker 
179*795d594fSAndroid Build Coastguard Worker   jobject jobj_ = env_->NewObject(c_class, constructor);
180*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(jobj_ != nullptr);
181*795d594fSAndroid Build Coastguard Worker 
182*795d594fSAndroid Build Coastguard Worker   // Force non-virtual call to AbstractClass foo, will throw AbstractMethodError exception.
183*795d594fSAndroid Build Coastguard Worker   env_->CallNonvirtualVoidMethod(jobj_, class_, mid_);
184*795d594fSAndroid Build Coastguard Worker 
185*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(env_->ExceptionCheck(), JNI_TRUE);
186*795d594fSAndroid Build Coastguard Worker   jthrowable exception = env_->ExceptionOccurred();
187*795d594fSAndroid Build Coastguard Worker   env_->ExceptionClear();
188*795d594fSAndroid Build Coastguard Worker   jclass jlame = env_->FindClass("java/lang/AbstractMethodError");
189*795d594fSAndroid Build Coastguard Worker   EXPECT_TRUE(env_->IsInstanceOf(exception, jlame));
190*795d594fSAndroid Build Coastguard Worker   {
191*795d594fSAndroid Build Coastguard Worker     ScopedObjectAccess soa(Thread::Current());
192*795d594fSAndroid Build Coastguard Worker     Thread::Current()->ClearException();
193*795d594fSAndroid Build Coastguard Worker   }
194*795d594fSAndroid Build Coastguard Worker }
195*795d594fSAndroid Build Coastguard Worker 
196*795d594fSAndroid Build Coastguard Worker class CompilerDriverProfileTest : public CompilerDriverTest {
197*795d594fSAndroid Build Coastguard Worker  protected:
GetProfileCompilationInfo()198*795d594fSAndroid Build Coastguard Worker   ProfileCompilationInfo* GetProfileCompilationInfo() override {
199*795d594fSAndroid Build Coastguard Worker     ScopedObjectAccess soa(Thread::Current());
200*795d594fSAndroid Build Coastguard Worker     std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("ProfileTestMultiDex");
201*795d594fSAndroid Build Coastguard Worker 
202*795d594fSAndroid Build Coastguard Worker     ProfileCompilationInfo info;
203*795d594fSAndroid Build Coastguard Worker     for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
204*795d594fSAndroid Build Coastguard Worker       profile_info_.AddMethod(ProfileMethodInfo(MethodReference(dex_file.get(), 1)),
205*795d594fSAndroid Build Coastguard Worker                               ProfileCompilationInfo::MethodHotness::kFlagHot);
206*795d594fSAndroid Build Coastguard Worker       profile_info_.AddMethod(ProfileMethodInfo(MethodReference(dex_file.get(), 2)),
207*795d594fSAndroid Build Coastguard Worker                               ProfileCompilationInfo::MethodHotness::kFlagHot);
208*795d594fSAndroid Build Coastguard Worker     }
209*795d594fSAndroid Build Coastguard Worker     return &profile_info_;
210*795d594fSAndroid Build Coastguard Worker   }
211*795d594fSAndroid Build Coastguard Worker 
GetCompilerFilter() const212*795d594fSAndroid Build Coastguard Worker   CompilerFilter::Filter GetCompilerFilter() const override {
213*795d594fSAndroid Build Coastguard Worker     // Use a profile based filter.
214*795d594fSAndroid Build Coastguard Worker     return CompilerFilter::kSpeedProfile;
215*795d594fSAndroid Build Coastguard Worker   }
216*795d594fSAndroid Build Coastguard Worker 
GetExpectedMethodsForClass(const std::string & clazz)217*795d594fSAndroid Build Coastguard Worker   std::unordered_set<std::string> GetExpectedMethodsForClass(const std::string& clazz) {
218*795d594fSAndroid Build Coastguard Worker     if (clazz == "Main") {
219*795d594fSAndroid Build Coastguard Worker       return std::unordered_set<std::string>({
220*795d594fSAndroid Build Coastguard Worker           "java.lang.String Main.getA()",
221*795d594fSAndroid Build Coastguard Worker           "java.lang.String Main.getB()"});
222*795d594fSAndroid Build Coastguard Worker     } else if (clazz == "Second") {
223*795d594fSAndroid Build Coastguard Worker       return std::unordered_set<std::string>({
224*795d594fSAndroid Build Coastguard Worker           "java.lang.String Second.getX()",
225*795d594fSAndroid Build Coastguard Worker           "java.lang.String Second.getY()"});
226*795d594fSAndroid Build Coastguard Worker     } else {
227*795d594fSAndroid Build Coastguard Worker       return std::unordered_set<std::string>();
228*795d594fSAndroid Build Coastguard Worker     }
229*795d594fSAndroid Build Coastguard Worker   }
230*795d594fSAndroid Build Coastguard Worker 
CheckCompiledMethods(jobject class_loader,const std::string & clazz,const std::unordered_set<std::string> & expected_methods)231*795d594fSAndroid Build Coastguard Worker   void CheckCompiledMethods(jobject class_loader,
232*795d594fSAndroid Build Coastguard Worker                             const std::string& clazz,
233*795d594fSAndroid Build Coastguard Worker                             const std::unordered_set<std::string>& expected_methods) {
234*795d594fSAndroid Build Coastguard Worker     ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
235*795d594fSAndroid Build Coastguard Worker     Thread* self = Thread::Current();
236*795d594fSAndroid Build Coastguard Worker     ScopedObjectAccess soa(self);
237*795d594fSAndroid Build Coastguard Worker     StackHandleScope<1> hs(self);
238*795d594fSAndroid Build Coastguard Worker     Handle<mirror::ClassLoader> h_loader(
239*795d594fSAndroid Build Coastguard Worker         hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)));
240*795d594fSAndroid Build Coastguard Worker     ObjPtr<mirror::Class> klass = FindClass(clazz.c_str(), h_loader);
241*795d594fSAndroid Build Coastguard Worker     ASSERT_NE(klass, nullptr);
242*795d594fSAndroid Build Coastguard Worker 
243*795d594fSAndroid Build Coastguard Worker     const auto pointer_size = class_linker->GetImagePointerSize();
244*795d594fSAndroid Build Coastguard Worker     size_t number_of_compiled_methods = 0;
245*795d594fSAndroid Build Coastguard Worker     for (auto& m : klass->GetVirtualMethods(pointer_size)) {
246*795d594fSAndroid Build Coastguard Worker       std::string name = m.PrettyMethod(true);
247*795d594fSAndroid Build Coastguard Worker       const void* code = m.GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
248*795d594fSAndroid Build Coastguard Worker       ASSERT_NE(code, nullptr);
249*795d594fSAndroid Build Coastguard Worker       if (expected_methods.find(name) != expected_methods.end()) {
250*795d594fSAndroid Build Coastguard Worker         number_of_compiled_methods++;
251*795d594fSAndroid Build Coastguard Worker         EXPECT_FALSE(class_linker->IsQuickToInterpreterBridge(code));
252*795d594fSAndroid Build Coastguard Worker       } else {
253*795d594fSAndroid Build Coastguard Worker         EXPECT_TRUE(class_linker->IsQuickToInterpreterBridge(code));
254*795d594fSAndroid Build Coastguard Worker       }
255*795d594fSAndroid Build Coastguard Worker     }
256*795d594fSAndroid Build Coastguard Worker     EXPECT_EQ(expected_methods.size(), number_of_compiled_methods);
257*795d594fSAndroid Build Coastguard Worker   }
258*795d594fSAndroid Build Coastguard Worker 
259*795d594fSAndroid Build Coastguard Worker  private:
260*795d594fSAndroid Build Coastguard Worker   ProfileCompilationInfo profile_info_;
261*795d594fSAndroid Build Coastguard Worker };
262*795d594fSAndroid Build Coastguard Worker 
TEST_F(CompilerDriverProfileTest,ProfileGuidedCompilation)263*795d594fSAndroid Build Coastguard Worker TEST_F(CompilerDriverProfileTest, ProfileGuidedCompilation) {
264*795d594fSAndroid Build Coastguard Worker   Thread* self = Thread::Current();
265*795d594fSAndroid Build Coastguard Worker   jobject class_loader;
266*795d594fSAndroid Build Coastguard Worker   {
267*795d594fSAndroid Build Coastguard Worker     ScopedObjectAccess soa(self);
268*795d594fSAndroid Build Coastguard Worker     class_loader = LoadDex("ProfileTestMultiDex");
269*795d594fSAndroid Build Coastguard Worker   }
270*795d594fSAndroid Build Coastguard Worker   ASSERT_NE(class_loader, nullptr);
271*795d594fSAndroid Build Coastguard Worker 
272*795d594fSAndroid Build Coastguard Worker   // Need to enable dex-file writability. Methods rejected to be compiled will run through the
273*795d594fSAndroid Build Coastguard Worker   // dex-to-dex compiler.
274*795d594fSAndroid Build Coastguard Worker   for (const DexFile* dex_file : GetDexFiles(class_loader)) {
275*795d594fSAndroid Build Coastguard Worker     ASSERT_TRUE(dex_file->EnableWrite());
276*795d594fSAndroid Build Coastguard Worker   }
277*795d594fSAndroid Build Coastguard Worker 
278*795d594fSAndroid Build Coastguard Worker   CompileAllAndMakeExecutable(class_loader);
279*795d594fSAndroid Build Coastguard Worker 
280*795d594fSAndroid Build Coastguard Worker   std::unordered_set<std::string> m = GetExpectedMethodsForClass("Main");
281*795d594fSAndroid Build Coastguard Worker   std::unordered_set<std::string> s = GetExpectedMethodsForClass("Second");
282*795d594fSAndroid Build Coastguard Worker   CheckCompiledMethods(class_loader, "LMain;", m);
283*795d594fSAndroid Build Coastguard Worker   CheckCompiledMethods(class_loader, "LSecond;", s);
284*795d594fSAndroid Build Coastguard Worker }
285*795d594fSAndroid Build Coastguard Worker 
286*795d594fSAndroid Build Coastguard Worker // Test that a verify only compiler filter updates the CompiledClass map,
287*795d594fSAndroid Build Coastguard Worker // which will be used for OatClass.
288*795d594fSAndroid Build Coastguard Worker class CompilerDriverVerifyTest : public CompilerDriverTest {
289*795d594fSAndroid Build Coastguard Worker  protected:
GetCompilerFilter() const290*795d594fSAndroid Build Coastguard Worker   CompilerFilter::Filter GetCompilerFilter() const override {
291*795d594fSAndroid Build Coastguard Worker     return CompilerFilter::kVerify;
292*795d594fSAndroid Build Coastguard Worker   }
293*795d594fSAndroid Build Coastguard Worker 
CheckVerifiedClass(jobject class_loader,const std::string & clazz) const294*795d594fSAndroid Build Coastguard Worker   void CheckVerifiedClass(jobject class_loader, const std::string& clazz) const {
295*795d594fSAndroid Build Coastguard Worker     ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
296*795d594fSAndroid Build Coastguard Worker     Thread* self = Thread::Current();
297*795d594fSAndroid Build Coastguard Worker     ScopedObjectAccess soa(self);
298*795d594fSAndroid Build Coastguard Worker     StackHandleScope<1> hs(self);
299*795d594fSAndroid Build Coastguard Worker     Handle<mirror::ClassLoader> h_loader(
300*795d594fSAndroid Build Coastguard Worker         hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)));
301*795d594fSAndroid Build Coastguard Worker     ObjPtr<mirror::Class> klass = FindClass(clazz.c_str(), h_loader);
302*795d594fSAndroid Build Coastguard Worker     ASSERT_NE(klass, nullptr);
303*795d594fSAndroid Build Coastguard Worker     EXPECT_TRUE(klass->IsVerified());
304*795d594fSAndroid Build Coastguard Worker 
305*795d594fSAndroid Build Coastguard Worker     ClassStatus status;
306*795d594fSAndroid Build Coastguard Worker     bool found = compiler_driver_->GetCompiledClass(
307*795d594fSAndroid Build Coastguard Worker         ClassReference(&klass->GetDexFile(), klass->GetDexTypeIndex().index_), &status);
308*795d594fSAndroid Build Coastguard Worker     ASSERT_TRUE(found);
309*795d594fSAndroid Build Coastguard Worker     EXPECT_GE(status, ClassStatus::kVerified);
310*795d594fSAndroid Build Coastguard Worker   }
311*795d594fSAndroid Build Coastguard Worker };
312*795d594fSAndroid Build Coastguard Worker 
TEST_F(CompilerDriverVerifyTest,VerifyCompilation)313*795d594fSAndroid Build Coastguard Worker TEST_F(CompilerDriverVerifyTest, VerifyCompilation) {
314*795d594fSAndroid Build Coastguard Worker   Thread* self = Thread::Current();
315*795d594fSAndroid Build Coastguard Worker   jobject class_loader;
316*795d594fSAndroid Build Coastguard Worker   {
317*795d594fSAndroid Build Coastguard Worker     ScopedObjectAccess soa(self);
318*795d594fSAndroid Build Coastguard Worker     class_loader = LoadDex("ProfileTestMultiDex");
319*795d594fSAndroid Build Coastguard Worker   }
320*795d594fSAndroid Build Coastguard Worker   ASSERT_NE(class_loader, nullptr);
321*795d594fSAndroid Build Coastguard Worker 
322*795d594fSAndroid Build Coastguard Worker   CompileAllAndMakeExecutable(class_loader);
323*795d594fSAndroid Build Coastguard Worker 
324*795d594fSAndroid Build Coastguard Worker   CheckVerifiedClass(class_loader, "LMain;");
325*795d594fSAndroid Build Coastguard Worker   CheckVerifiedClass(class_loader, "LSecond;");
326*795d594fSAndroid Build Coastguard Worker }
327*795d594fSAndroid Build Coastguard Worker 
328*795d594fSAndroid Build Coastguard Worker // Test that a class of status ClassStatus::kRetryVerificationAtRuntime is indeed
329*795d594fSAndroid Build Coastguard Worker // recorded that way in the driver.
TEST_F(CompilerDriverVerifyTest,RetryVerifcationStatusCheckVerified)330*795d594fSAndroid Build Coastguard Worker TEST_F(CompilerDriverVerifyTest, RetryVerifcationStatusCheckVerified) {
331*795d594fSAndroid Build Coastguard Worker   Thread* const self = Thread::Current();
332*795d594fSAndroid Build Coastguard Worker   jobject class_loader;
333*795d594fSAndroid Build Coastguard Worker   std::vector<const DexFile*> dex_files;
334*795d594fSAndroid Build Coastguard Worker   const DexFile* dex_file = nullptr;
335*795d594fSAndroid Build Coastguard Worker   {
336*795d594fSAndroid Build Coastguard Worker     ScopedObjectAccess soa(self);
337*795d594fSAndroid Build Coastguard Worker     class_loader = LoadDex("ProfileTestMultiDex");
338*795d594fSAndroid Build Coastguard Worker     ASSERT_NE(class_loader, nullptr);
339*795d594fSAndroid Build Coastguard Worker     dex_files = GetDexFiles(class_loader);
340*795d594fSAndroid Build Coastguard Worker     ASSERT_GT(dex_files.size(), 0u);
341*795d594fSAndroid Build Coastguard Worker     dex_file = dex_files.front();
342*795d594fSAndroid Build Coastguard Worker   }
343*795d594fSAndroid Build Coastguard Worker   SetDexFilesForOatFile(dex_files);
344*795d594fSAndroid Build Coastguard Worker   callbacks_->SetDoesClassUnloading(true, compiler_driver_.get());
345*795d594fSAndroid Build Coastguard Worker   ClassReference ref(dex_file, 0u);
346*795d594fSAndroid Build Coastguard Worker   // Test that the status is read from the compiler driver as expected.
347*795d594fSAndroid Build Coastguard Worker   static_assert(enum_cast<size_t>(ClassStatus::kLast) < std::numeric_limits<size_t>::max(),
348*795d594fSAndroid Build Coastguard Worker                 "Make sure incrementing the class status does not overflow.");
349*795d594fSAndroid Build Coastguard Worker   for (size_t i = enum_cast<size_t>(ClassStatus::kRetryVerificationAtRuntime);
350*795d594fSAndroid Build Coastguard Worker        i <= enum_cast<size_t>(ClassStatus::kLast);
351*795d594fSAndroid Build Coastguard Worker        ++i) {
352*795d594fSAndroid Build Coastguard Worker     const ClassStatus expected_status = enum_cast<ClassStatus>(i);
353*795d594fSAndroid Build Coastguard Worker     // Skip unsupported status that are not supposed to be ever recorded.
354*795d594fSAndroid Build Coastguard Worker     if (expected_status == ClassStatus::kInitializing ||
355*795d594fSAndroid Build Coastguard Worker         expected_status == ClassStatus::kInitialized) {
356*795d594fSAndroid Build Coastguard Worker       continue;
357*795d594fSAndroid Build Coastguard Worker     }
358*795d594fSAndroid Build Coastguard Worker     compiler_driver_->RecordClassStatus(ref, expected_status);
359*795d594fSAndroid Build Coastguard Worker     ClassStatus status = {};
360*795d594fSAndroid Build Coastguard Worker     ASSERT_TRUE(compiler_driver_->GetCompiledClass(ref, &status));
361*795d594fSAndroid Build Coastguard Worker     EXPECT_EQ(status, expected_status);
362*795d594fSAndroid Build Coastguard Worker   }
363*795d594fSAndroid Build Coastguard Worker }
364*795d594fSAndroid Build Coastguard Worker 
365*795d594fSAndroid Build Coastguard Worker // TODO: need check-cast test (when stub complete & we can throw/catch
366*795d594fSAndroid Build Coastguard Worker 
367*795d594fSAndroid Build Coastguard Worker }  // namespace art
368