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