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