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