compiler_driver_test.cc revision 00f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abac
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 "compiler.h" 18 19#include <stdint.h> 20#include <stdio.h> 21 22#include "UniquePtr.h" 23#include "class_linker.h" 24#include "common_test.h" 25#include "dex_cache.h" 26#include "dex_file.h" 27#include "heap.h" 28#include "object.h" 29 30namespace art { 31 32class CompilerTest : public CommonTest { 33 protected: 34 void CompileAll(jobject class_loader) LOCKS_EXCLUDED(GlobalSynchronization::mutator_lock_) { 35 compiler_->CompileAll(class_loader, Runtime::Current()->GetCompileTimeClassPath(class_loader)); 36 MakeAllExecutable(class_loader); 37 } 38 39 void EnsureCompiled(jobject class_loader, const char* class_name, const char* method, 40 const char* signature, bool is_virtual) 41 LOCKS_EXCLUDED(GlobalSynchronization::mutator_lock_) { 42 CompileAll(class_loader); 43 Thread::Current()->TransitionFromSuspendedToRunnable(); 44 runtime_->Start(); 45 env_ = Thread::Current()->GetJniEnv(); 46 class_ = env_->FindClass(class_name); 47 CHECK(class_ != NULL) << "Class not found: " << class_name; 48 if (is_virtual) { 49 mid_ = env_->GetMethodID(class_, method, signature); 50 } else { 51 mid_ = env_->GetStaticMethodID(class_, method, signature); 52 } 53 CHECK(mid_ != NULL) << "Method not found: " << class_name << "." << method << signature; 54 } 55 56 void MakeAllExecutable(jobject class_loader) { 57 const std::vector<const DexFile*>& class_path 58 = Runtime::Current()->GetCompileTimeClassPath(class_loader); 59 for (size_t i = 0; i != class_path.size(); ++i) { 60 const DexFile* dex_file = class_path[i]; 61 CHECK(dex_file != NULL); 62 MakeDexFileExecutable(class_loader, *dex_file); 63 } 64 } 65 66 void MakeDexFileExecutable(jobject class_loader, const DexFile& dex_file) { 67 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 68 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) { 69 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i); 70 const char* descriptor = dex_file.GetClassDescriptor(class_def); 71 ScopedObjectAccess soa(Thread::Current()); 72 Class* c = class_linker->FindClass(descriptor, soa.Decode<ClassLoader*>(class_loader)); 73 CHECK(c != NULL); 74 for (size_t i = 0; i < c->NumDirectMethods(); i++) { 75 MakeExecutable(c->GetDirectMethod(i)); 76 } 77 for (size_t i = 0; i < c->NumVirtualMethods(); i++) { 78 MakeExecutable(c->GetVirtualMethod(i)); 79 } 80 } 81 } 82 83 JNIEnv* env_; 84 jclass class_; 85 jmethodID mid_; 86}; 87 88// Disabled due to 10 second runtime on host 89TEST_F(CompilerTest, DISABLED_LARGE_CompileDexLibCore) { 90 CompileAll(NULL); 91 92 // All libcore references should resolve 93 ScopedObjectAccess soa(Thread::Current()); 94 const DexFile* dex = java_lang_dex_file_; 95 DexCache* dex_cache = class_linker_->FindDexCache(*dex); 96 EXPECT_EQ(dex->NumStringIds(), dex_cache->NumStrings()); 97 for (size_t i = 0; i < dex_cache->NumStrings(); i++) { 98 const String* string = dex_cache->GetResolvedString(i); 99 EXPECT_TRUE(string != NULL) << "string_idx=" << i; 100 } 101 EXPECT_EQ(dex->NumTypeIds(), dex_cache->NumResolvedTypes()); 102 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) { 103 Class* type = dex_cache->GetResolvedType(i); 104 EXPECT_TRUE(type != NULL) << "type_idx=" << i 105 << " " << dex->GetTypeDescriptor(dex->GetTypeId(i)); 106 } 107 EXPECT_EQ(dex->NumMethodIds(), dex_cache->NumResolvedMethods()); 108 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) { 109 Method* method = dex_cache->GetResolvedMethod(i); 110 EXPECT_TRUE(method != NULL) << "method_idx=" << i 111 << " " << dex->GetMethodDeclaringClassDescriptor(dex->GetMethodId(i)) 112 << " " << dex->GetMethodName(dex->GetMethodId(i)); 113 EXPECT_TRUE(method->GetCode() != NULL) << "method_idx=" << i 114 << " " 115 << dex->GetMethodDeclaringClassDescriptor(dex->GetMethodId(i)) 116 << " " << dex->GetMethodName(dex->GetMethodId(i)); 117 } 118 EXPECT_EQ(dex->NumFieldIds(), dex_cache->NumResolvedFields()); 119 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) { 120 Field* field = dex_cache->GetResolvedField(i); 121 EXPECT_TRUE(field != NULL) << "field_idx=" << i 122 << " " << dex->GetFieldDeclaringClassDescriptor(dex->GetFieldId(i)) 123 << " " << dex->GetFieldName(dex->GetFieldId(i)); 124 } 125 126 // TODO check Class::IsVerified for all classes 127 128 // TODO: check that all Method::GetCode() values are non-null 129} 130 131TEST_F(CompilerTest, AbstractMethodErrorStub) { 132 jobject class_loader; 133 { 134 ScopedObjectAccess soa(Thread::Current()); 135 CompileVirtualMethod(NULL, "java.lang.Class", "isFinalizable", "()Z"); 136 CompileDirectMethod(NULL, "java.lang.Object", "<init>", "()V"); 137 class_loader = LoadDex("AbstractMethod"); 138 } 139 ASSERT_TRUE(class_loader != NULL); 140 EnsureCompiled(class_loader, "AbstractClass", "foo", "()V", true); 141 142 // Create a jobj_ of ConcreteClass, NOT AbstractClass. 143 jclass c_class = env_->FindClass("ConcreteClass"); 144 jmethodID constructor = env_->GetMethodID(c_class, "<init>", "()V"); 145 jobject jobj_ = env_->NewObject(c_class, constructor); 146 ASSERT_TRUE(jobj_ != NULL); 147 148 // Force non-virtual call to AbstractClass foo, will throw AbstractMethodError exception. 149 env_->CallNonvirtualVoidMethod(jobj_, class_, mid_); 150 EXPECT_EQ(env_->ExceptionCheck(), JNI_TRUE); 151 jthrowable exception = env_->ExceptionOccurred(); 152 env_->ExceptionClear(); 153 jclass jlame = env_->FindClass("java/lang/AbstractMethodError"); 154 EXPECT_TRUE(env_->IsInstanceOf(exception, jlame)); 155 Thread::Current()->ClearException(); 156} 157 158// TODO: need check-cast test (when stub complete & we can throw/catch 159 160} // namespace art 161