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