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