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