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 "art_method-inl.h"
24 #include "class_linker-inl.h"
25 #include "common_compiler_test.h"
26 #include "dex_file.h"
27 #include "gc/heap.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:
CompileAll(jobject class_loader)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 GetDexFiles(class_loader),
45 &timings);
46 t.NewTiming("MakeAllExecutable");
47 MakeAllExecutable(class_loader);
48 }
49
EnsureCompiled(jobject class_loader,const char * class_name,const char * method,const char * signature,bool is_virtual)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_ != nullptr) << "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_ != nullptr) << "Method not found: " << class_name << "." << method << signature;
66 }
67
MakeAllExecutable(jobject class_loader)68 void MakeAllExecutable(jobject class_loader) {
69 const std::vector<const DexFile*> class_path = GetDexFiles(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 != nullptr);
73 MakeDexFileExecutable(class_loader, *dex_file);
74 }
75 }
76
MakeDexFileExecutable(jobject class_loader,const DexFile & dex_file)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 StackHandleScope<1> hs(soa.Self());
84 Handle<mirror::ClassLoader> loader(
85 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(class_loader)));
86 mirror::Class* c = class_linker->FindClass(soa.Self(), descriptor, loader);
87 CHECK(c != nullptr);
88 const auto pointer_size = class_linker->GetImagePointerSize();
89 for (auto& m : c->GetDirectMethods(pointer_size)) {
90 MakeExecutable(&m);
91 }
92 for (auto& m : c->GetVirtualMethods(pointer_size)) {
93 MakeExecutable(&m);
94 }
95 }
96 }
97
98 JNIEnv* env_;
99 jclass class_;
100 jmethodID mid_;
101 };
102
103 // Disabled due to 10 second runtime on host
TEST_F(CompilerDriverTest,DISABLED_LARGE_CompileDexLibCore)104 TEST_F(CompilerDriverTest, DISABLED_LARGE_CompileDexLibCore) {
105 CompileAll(nullptr);
106
107 // All libcore references should resolve
108 ScopedObjectAccess soa(Thread::Current());
109 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
110 const DexFile& dex = *java_lang_dex_file_;
111 mirror::DexCache* dex_cache = class_linker_->FindDexCache(dex);
112 EXPECT_EQ(dex.NumStringIds(), dex_cache->NumStrings());
113 for (size_t i = 0; i < dex_cache->NumStrings(); i++) {
114 const mirror::String* string = dex_cache->GetResolvedString(i);
115 EXPECT_TRUE(string != nullptr) << "string_idx=" << i;
116 }
117 EXPECT_EQ(dex.NumTypeIds(), dex_cache->NumResolvedTypes());
118 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
119 mirror::Class* type = dex_cache->GetResolvedType(i);
120 EXPECT_TRUE(type != nullptr) << "type_idx=" << i
121 << " " << dex.GetTypeDescriptor(dex.GetTypeId(i));
122 }
123 EXPECT_EQ(dex.NumMethodIds(), dex_cache->NumResolvedMethods());
124 auto* cl = Runtime::Current()->GetClassLinker();
125 auto pointer_size = cl->GetImagePointerSize();
126 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
127 ArtMethod* method = dex_cache->GetResolvedMethod(i, pointer_size);
128 EXPECT_TRUE(method != nullptr) << "method_idx=" << i
129 << " " << dex.GetMethodDeclaringClassDescriptor(dex.GetMethodId(i))
130 << " " << dex.GetMethodName(dex.GetMethodId(i));
131 EXPECT_TRUE(method->GetEntryPointFromQuickCompiledCode() != nullptr) << "method_idx=" << i
132 << " " << dex.GetMethodDeclaringClassDescriptor(dex.GetMethodId(i)) << " "
133 << dex.GetMethodName(dex.GetMethodId(i));
134 }
135 EXPECT_EQ(dex.NumFieldIds(), dex_cache->NumResolvedFields());
136 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
137 ArtField* field = cl->GetResolvedField(i, dex_cache);
138 EXPECT_TRUE(field != nullptr) << "field_idx=" << i
139 << " " << dex.GetFieldDeclaringClassDescriptor(dex.GetFieldId(i))
140 << " " << dex.GetFieldName(dex.GetFieldId(i));
141 }
142
143 // TODO check Class::IsVerified for all classes
144
145 // TODO: check that all Method::GetCode() values are non-null
146 }
147
TEST_F(CompilerDriverTest,AbstractMethodErrorStub)148 TEST_F(CompilerDriverTest, AbstractMethodErrorStub) {
149 TEST_DISABLED_FOR_HEAP_REFERENCE_POISONING();
150 jobject class_loader;
151 {
152 ScopedObjectAccess soa(Thread::Current());
153 CompileVirtualMethod(NullHandle<mirror::ClassLoader>(), "java.lang.Class", "isFinalizable",
154 "()Z");
155 CompileDirectMethod(NullHandle<mirror::ClassLoader>(), "java.lang.Object", "<init>", "()V");
156 class_loader = LoadDex("AbstractMethod");
157 }
158 ASSERT_TRUE(class_loader != nullptr);
159 EnsureCompiled(class_loader, "AbstractClass", "foo", "()V", true);
160
161 // Create a jobj_ of ConcreteClass, NOT AbstractClass.
162 jclass c_class = env_->FindClass("ConcreteClass");
163
164 jmethodID constructor = env_->GetMethodID(c_class, "<init>", "()V");
165
166 jobject jobj_ = env_->NewObject(c_class, constructor);
167 ASSERT_TRUE(jobj_ != nullptr);
168
169 // Force non-virtual call to AbstractClass foo, will throw AbstractMethodError exception.
170 env_->CallNonvirtualVoidMethod(jobj_, class_, mid_);
171
172 EXPECT_EQ(env_->ExceptionCheck(), JNI_TRUE);
173 jthrowable exception = env_->ExceptionOccurred();
174 env_->ExceptionClear();
175 jclass jlame = env_->FindClass("java/lang/AbstractMethodError");
176 EXPECT_TRUE(env_->IsInstanceOf(exception, jlame));
177 {
178 ScopedObjectAccess soa(Thread::Current());
179 Thread::Current()->ClearException();
180 }
181 }
182
183 class CompilerDriverMethodsTest : public CompilerDriverTest {
184 protected:
GetCompiledMethods()185 std::unordered_set<std::string>* GetCompiledMethods() OVERRIDE {
186 return new std::unordered_set<std::string>({
187 "byte StaticLeafMethods.identity(byte)",
188 "int StaticLeafMethods.sum(int, int, int)",
189 "double StaticLeafMethods.sum(double, double, double, double)"
190 });
191 }
192 };
193
TEST_F(CompilerDriverMethodsTest,Selection)194 TEST_F(CompilerDriverMethodsTest, Selection) {
195 Thread* self = Thread::Current();
196 jobject class_loader;
197 {
198 ScopedObjectAccess soa(self);
199 class_loader = LoadDex("StaticLeafMethods");
200 }
201 ASSERT_NE(class_loader, nullptr);
202
203 // Need to enable dex-file writability. Methods rejected to be compiled will run through the
204 // dex-to-dex compiler.
205 for (const DexFile* dex_file : GetDexFiles(class_loader)) {
206 ASSERT_TRUE(dex_file->EnableWrite());
207 }
208
209 CompileAll(class_loader);
210
211 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
212 StackHandleScope<1> hs(self);
213 ScopedObjectAccess soa(self);
214 Handle<mirror::ClassLoader> h_loader(hs.NewHandle(
215 reinterpret_cast<mirror::ClassLoader*>(self->DecodeJObject(class_loader))));
216 mirror::Class* klass = class_linker->FindClass(self, "LStaticLeafMethods;", h_loader);
217 ASSERT_NE(klass, nullptr);
218
219 std::unique_ptr<std::unordered_set<std::string>> expected(GetCompiledMethods());
220
221 const auto pointer_size = class_linker->GetImagePointerSize();
222 for (auto& m : klass->GetDirectMethods(pointer_size)) {
223 std::string name = PrettyMethod(&m, true);
224 const void* code = m.GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
225 ASSERT_NE(code, nullptr);
226 if (expected->find(name) != expected->end()) {
227 expected->erase(name);
228 EXPECT_FALSE(class_linker->IsQuickToInterpreterBridge(code));
229 } else {
230 EXPECT_TRUE(class_linker->IsQuickToInterpreterBridge(code));
231 }
232 }
233 EXPECT_TRUE(expected->empty());
234 }
235
236 // TODO: need check-cast test (when stub complete & we can throw/catch
237
238 } // namespace art
239