blob: 3a53998e17e6904f806ef940c5570192acfc0d9e [file] [log] [blame]
David Brazdilca3c8c32016-09-06 14:04:48 +01001/*
2 * Copyright (C) 2016 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
Nicolas Geoffray08025182016-10-25 17:20:18 +010017// Test is in compiler, as it uses compiler related code.
18#include "verifier/verifier_deps.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010019
20#include "class_linker.h"
Nicolas Geoffray08025182016-10-25 17:20:18 +010021#include "compiler/common_compiler_test.h"
22#include "compiler/driver/compiler_options.h"
23#include "compiler/driver/compiler_driver.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010024#include "compiler_callbacks.h"
25#include "dex_file.h"
26#include "handle_scope-inl.h"
Nicolas Geoffray08025182016-10-25 17:20:18 +010027#include "verifier/method_verifier-inl.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010028#include "mirror/class_loader.h"
29#include "runtime.h"
30#include "thread.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070031#include "scoped_thread_state_change-inl.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010032
33namespace art {
34namespace verifier {
35
36class VerifierDepsCompilerCallbacks : public CompilerCallbacks {
37 public:
38 explicit VerifierDepsCompilerCallbacks()
39 : CompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileApp),
40 deps_(nullptr) {}
41
42 void MethodVerified(verifier::MethodVerifier* verifier ATTRIBUTE_UNUSED) OVERRIDE {}
43 void ClassRejected(ClassReference ref ATTRIBUTE_UNUSED) OVERRIDE {}
44 bool IsRelocationPossible() OVERRIDE { return false; }
45
46 verifier::VerifierDeps* GetVerifierDeps() const OVERRIDE { return deps_; }
47 void SetVerifierDeps(verifier::VerifierDeps* deps) { deps_ = deps; }
48
49 private:
50 verifier::VerifierDeps* deps_;
51};
52
Nicolas Geoffray08025182016-10-25 17:20:18 +010053class VerifierDepsTest : public CommonCompilerTest {
David Brazdilca3c8c32016-09-06 14:04:48 +010054 public:
55 void SetUpRuntimeOptions(RuntimeOptions* options) {
Nicolas Geoffray08025182016-10-25 17:20:18 +010056 CommonCompilerTest::SetUpRuntimeOptions(options);
David Brazdilca3c8c32016-09-06 14:04:48 +010057 callbacks_.reset(new VerifierDepsCompilerCallbacks());
58 }
59
60 mirror::Class* FindClassByName(const std::string& name, ScopedObjectAccess* soa)
61 REQUIRES_SHARED(Locks::mutator_lock_) {
62 StackHandleScope<1> hs(Thread::Current());
63 Handle<mirror::ClassLoader> class_loader_handle(
Mathieu Chartier0795f232016-09-27 18:43:30 -070064 hs.NewHandle(soa->Decode<mirror::ClassLoader>(class_loader_)));
David Brazdil6f82fbd2016-09-14 11:55:26 +010065 mirror::Class* klass = class_linker_->FindClass(Thread::Current(),
66 name.c_str(),
67 class_loader_handle);
68 if (klass == nullptr) {
69 DCHECK(Thread::Current()->IsExceptionPending());
70 Thread::Current()->ClearException();
71 }
72 return klass;
73 }
74
75 void SetVerifierDeps(const std::vector<const DexFile*>& dex_files) {
76 verifier_deps_.reset(new verifier::VerifierDeps(dex_files));
77 VerifierDepsCompilerCallbacks* callbacks =
78 reinterpret_cast<VerifierDepsCompilerCallbacks*>(callbacks_.get());
79 callbacks->SetVerifierDeps(verifier_deps_.get());
David Brazdilca3c8c32016-09-06 14:04:48 +010080 }
81
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +010082 void LoadDexFile(ScopedObjectAccess* soa, const char* name1, const char* name2 = nullptr)
83 REQUIRES_SHARED(Locks::mutator_lock_) {
84 class_loader_ = (name2 == nullptr) ? LoadDex(name1) : LoadMultiDex(name1, name2);
85 dex_files_ = GetDexFiles(class_loader_);
86 primary_dex_file_ = dex_files_.front();
87
88 SetVerifierDeps(dex_files_);
89 StackHandleScope<1> hs(soa->Self());
90 Handle<mirror::ClassLoader> loader =
91 hs.NewHandle(soa->Decode<mirror::ClassLoader>(class_loader_));
92 for (const DexFile* dex_file : dex_files_) {
93 class_linker_->RegisterDexFile(*dex_file, loader.Get());
94 }
95 }
96
David Brazdilca3c8c32016-09-06 14:04:48 +010097 void LoadDexFile(ScopedObjectAccess* soa) REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +010098 LoadDexFile(soa, "VerifierDeps");
99 CHECK_EQ(dex_files_.size(), 1u);
David Brazdilca3c8c32016-09-06 14:04:48 +0100100 klass_Main_ = FindClassByName("LMain;", soa);
101 CHECK(klass_Main_ != nullptr);
David Brazdilca3c8c32016-09-06 14:04:48 +0100102 }
103
104 bool VerifyMethod(const std::string& method_name) {
105 ScopedObjectAccess soa(Thread::Current());
106 LoadDexFile(&soa);
107
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100108 StackHandleScope<2> hs(soa.Self());
David Brazdilca3c8c32016-09-06 14:04:48 +0100109 Handle<mirror::ClassLoader> class_loader_handle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700110 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_)));
David Brazdilca3c8c32016-09-06 14:04:48 +0100111 Handle<mirror::DexCache> dex_cache_handle(hs.NewHandle(klass_Main_->GetDexCache()));
112
113 const DexFile::ClassDef* class_def = klass_Main_->GetClassDef();
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100114 const uint8_t* class_data = primary_dex_file_->GetClassData(*class_def);
David Brazdilca3c8c32016-09-06 14:04:48 +0100115 CHECK(class_data != nullptr);
116
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100117 ClassDataItemIterator it(*primary_dex_file_, class_data);
David Brazdilca3c8c32016-09-06 14:04:48 +0100118 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
119 it.Next();
120 }
121
122 ArtMethod* method = nullptr;
123 while (it.HasNextDirectMethod()) {
124 ArtMethod* resolved_method = class_linker_->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100125 *primary_dex_file_,
David Brazdilca3c8c32016-09-06 14:04:48 +0100126 it.GetMemberIndex(),
127 dex_cache_handle,
128 class_loader_handle,
129 nullptr,
130 it.GetMethodInvokeType(*class_def));
131 CHECK(resolved_method != nullptr);
132 if (method_name == resolved_method->GetName()) {
133 method = resolved_method;
134 break;
135 }
136 it.Next();
137 }
138 CHECK(method != nullptr);
139
140 MethodVerifier verifier(Thread::Current(),
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100141 primary_dex_file_,
David Brazdilca3c8c32016-09-06 14:04:48 +0100142 dex_cache_handle,
143 class_loader_handle,
144 *class_def,
145 it.GetMethodCodeItem(),
146 it.GetMemberIndex(),
147 method,
148 it.GetMethodAccessFlags(),
149 true /* can_load_classes */,
150 true /* allow_soft_failures */,
151 true /* need_precise_constants */,
152 false /* verify to dump */,
153 true /* allow_thread_suspension */);
154 verifier.Verify();
155 return !verifier.HasFailures();
156 }
157
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100158 void VerifyDexFile(const char* multidex = nullptr) {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100159 std::string error_msg;
Nicolas Geoffray08025182016-10-25 17:20:18 +0100160 {
161 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100162 LoadDexFile(&soa, "VerifierDeps", multidex);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100163 }
Nicolas Geoffray08025182016-10-25 17:20:18 +0100164 TimingLogger timings("Verify", false, false);
Nicolas Geoffray08025182016-10-25 17:20:18 +0100165 compiler_options_->boot_image_ = false;
166 compiler_driver_->InitializeThreadPools();
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100167 compiler_driver_->Verify(class_loader_, dex_files_, &timings);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100168 }
169
David Brazdilca3c8c32016-09-06 14:04:48 +0100170 bool TestAssignabilityRecording(const std::string& dst,
171 const std::string& src,
172 bool is_strict,
173 bool is_assignable) {
174 ScopedObjectAccess soa(Thread::Current());
175 LoadDexFile(&soa);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100176 mirror::Class* klass_dst = FindClassByName(dst, &soa);
177 DCHECK(klass_dst != nullptr);
178 mirror::Class* klass_src = FindClassByName(src, &soa);
179 DCHECK(klass_src != nullptr);
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100180 verifier_deps_->AddAssignability(*primary_dex_file_,
David Brazdil6f82fbd2016-09-14 11:55:26 +0100181 klass_dst,
182 klass_src,
David Brazdilca3c8c32016-09-06 14:04:48 +0100183 is_strict,
184 is_assignable);
185 return true;
186 }
187
Nicolas Geoffray08025182016-10-25 17:20:18 +0100188 bool HasUnverifiedClass(const std::string& cls) {
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100189 const DexFile::TypeId* type_id = primary_dex_file_->FindTypeId(cls.c_str());
Nicolas Geoffray08025182016-10-25 17:20:18 +0100190 DCHECK(type_id != nullptr);
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100191 uint16_t index = primary_dex_file_->GetIndexForTypeId(*type_id);
Nicolas Geoffray08025182016-10-25 17:20:18 +0100192 MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
193 for (const auto& dex_dep : verifier_deps_->dex_deps_) {
194 for (uint16_t entry : dex_dep.second->unverified_classes_) {
195 if (index == entry) {
196 return true;
197 }
198 }
199 }
200 return false;
201 }
202
David Brazdilca3c8c32016-09-06 14:04:48 +0100203 // Iterates over all assignability records and tries to find an entry which
204 // matches the expected destination/source pair.
205 bool HasAssignable(const std::string& expected_destination,
206 const std::string& expected_source,
207 bool expected_is_assignable) {
208 MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
209 for (auto& dex_dep : verifier_deps_->dex_deps_) {
210 const DexFile& dex_file = *dex_dep.first;
211 auto& storage = expected_is_assignable ? dex_dep.second->assignable_types_
212 : dex_dep.second->unassignable_types_;
213 for (auto& entry : storage) {
214 std::string actual_destination =
215 verifier_deps_->GetStringFromId(dex_file, entry.GetDestination());
216 std::string actual_source = verifier_deps_->GetStringFromId(dex_file, entry.GetSource());
217 if ((expected_destination == actual_destination) && (expected_source == actual_source)) {
218 return true;
219 }
220 }
221 }
222 return false;
223 }
224
225 // Iterates over all class resolution records, finds an entry which matches
226 // the given class descriptor and tests its properties.
227 bool HasClass(const std::string& expected_klass,
228 bool expected_resolved,
229 const std::string& expected_access_flags = "") {
230 MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
231 for (auto& dex_dep : verifier_deps_->dex_deps_) {
232 for (auto& entry : dex_dep.second->classes_) {
233 if (expected_resolved != entry.IsResolved()) {
234 continue;
235 }
236
237 std::string actual_klass = dex_dep.first->StringByTypeIdx(entry.GetDexTypeIndex());
238 if (expected_klass != actual_klass) {
239 continue;
240 }
241
242 if (expected_resolved) {
243 // Test access flags. Note that PrettyJavaAccessFlags always appends
244 // a space after the modifiers. Add it to the expected access flags.
245 std::string actual_access_flags = PrettyJavaAccessFlags(entry.GetAccessFlags());
246 if (expected_access_flags + " " != actual_access_flags) {
247 continue;
248 }
249 }
250
251 return true;
252 }
253 }
254 return false;
255 }
256
257 // Iterates over all field resolution records, finds an entry which matches
258 // the given field class+name+type and tests its properties.
259 bool HasField(const std::string& expected_klass,
260 const std::string& expected_name,
261 const std::string& expected_type,
262 bool expected_resolved,
263 const std::string& expected_access_flags = "",
264 const std::string& expected_decl_klass = "") {
265 MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
266 for (auto& dex_dep : verifier_deps_->dex_deps_) {
267 for (auto& entry : dex_dep.second->fields_) {
268 if (expected_resolved != entry.IsResolved()) {
269 continue;
270 }
271
272 const DexFile::FieldId& field_id = dex_dep.first->GetFieldId(entry.GetDexFieldIndex());
273
274 std::string actual_klass = dex_dep.first->StringByTypeIdx(field_id.class_idx_);
275 if (expected_klass != actual_klass) {
276 continue;
277 }
278
279 std::string actual_name = dex_dep.first->StringDataByIdx(field_id.name_idx_);
280 if (expected_name != actual_name) {
281 continue;
282 }
283
284 std::string actual_type = dex_dep.first->StringByTypeIdx(field_id.type_idx_);
285 if (expected_type != actual_type) {
286 continue;
287 }
288
289 if (expected_resolved) {
290 // Test access flags. Note that PrettyJavaAccessFlags always appends
291 // a space after the modifiers. Add it to the expected access flags.
292 std::string actual_access_flags = PrettyJavaAccessFlags(entry.GetAccessFlags());
293 if (expected_access_flags + " " != actual_access_flags) {
294 continue;
295 }
296
297 std::string actual_decl_klass = verifier_deps_->GetStringFromId(
298 *dex_dep.first, entry.GetDeclaringClassIndex());
299 if (expected_decl_klass != actual_decl_klass) {
300 continue;
301 }
302 }
303
304 return true;
305 }
306 }
307 return false;
308 }
309
310 // Iterates over all method resolution records, finds an entry which matches
311 // the given field kind+class+name+signature and tests its properties.
312 bool HasMethod(const std::string& expected_kind,
313 const std::string& expected_klass,
314 const std::string& expected_name,
315 const std::string& expected_signature,
316 bool expected_resolved,
317 const std::string& expected_access_flags = "",
318 const std::string& expected_decl_klass = "") {
319 MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
320 for (auto& dex_dep : verifier_deps_->dex_deps_) {
321 auto& storage = (expected_kind == "direct") ? dex_dep.second->direct_methods_
322 : (expected_kind == "virtual") ? dex_dep.second->virtual_methods_
323 : dex_dep.second->interface_methods_;
324 for (auto& entry : storage) {
325 if (expected_resolved != entry.IsResolved()) {
326 continue;
327 }
328
329 const DexFile::MethodId& method_id = dex_dep.first->GetMethodId(entry.GetDexMethodIndex());
330
331 std::string actual_klass = dex_dep.first->StringByTypeIdx(method_id.class_idx_);
332 if (expected_klass != actual_klass) {
333 continue;
334 }
335
336 std::string actual_name = dex_dep.first->StringDataByIdx(method_id.name_idx_);
337 if (expected_name != actual_name) {
338 continue;
339 }
340
341 std::string actual_signature = dex_dep.first->GetMethodSignature(method_id).ToString();
342 if (expected_signature != actual_signature) {
343 continue;
344 }
345
346 if (expected_resolved) {
347 // Test access flags. Note that PrettyJavaAccessFlags always appends
348 // a space after the modifiers. Add it to the expected access flags.
349 std::string actual_access_flags = PrettyJavaAccessFlags(entry.GetAccessFlags());
350 if (expected_access_flags + " " != actual_access_flags) {
351 continue;
352 }
353
354 std::string actual_decl_klass = verifier_deps_->GetStringFromId(
355 *dex_dep.first, entry.GetDeclaringClassIndex());
356 if (expected_decl_klass != actual_decl_klass) {
357 continue;
358 }
359 }
360
361 return true;
362 }
363 }
364 return false;
365 }
366
David Brazdil6f82fbd2016-09-14 11:55:26 +0100367 size_t NumberOfCompiledDexFiles() {
368 MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
369 return verifier_deps_->dex_deps_.size();
370 }
371
372 size_t HasEachKindOfRecord() {
373 MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
374
375 bool has_strings = false;
376 bool has_assignability = false;
377 bool has_classes = false;
378 bool has_fields = false;
379 bool has_methods = false;
Nicolas Geoffray08025182016-10-25 17:20:18 +0100380 bool has_unverified_classes = false;
David Brazdil6f82fbd2016-09-14 11:55:26 +0100381
382 for (auto& entry : verifier_deps_->dex_deps_) {
383 has_strings |= !entry.second->strings_.empty();
384 has_assignability |= !entry.second->assignable_types_.empty();
385 has_assignability |= !entry.second->unassignable_types_.empty();
386 has_classes |= !entry.second->classes_.empty();
387 has_fields |= !entry.second->fields_.empty();
388 has_methods |= !entry.second->direct_methods_.empty();
389 has_methods |= !entry.second->virtual_methods_.empty();
390 has_methods |= !entry.second->interface_methods_.empty();
Nicolas Geoffray08025182016-10-25 17:20:18 +0100391 has_unverified_classes |= !entry.second->unverified_classes_.empty();
David Brazdil6f82fbd2016-09-14 11:55:26 +0100392 }
393
Nicolas Geoffray08025182016-10-25 17:20:18 +0100394 return has_strings &&
395 has_assignability &&
396 has_classes &&
397 has_fields &&
398 has_methods &&
399 has_unverified_classes;
David Brazdil6f82fbd2016-09-14 11:55:26 +0100400 }
401
David Brazdilca3c8c32016-09-06 14:04:48 +0100402 std::unique_ptr<verifier::VerifierDeps> verifier_deps_;
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100403 std::vector<const DexFile*> dex_files_;
404 const DexFile* primary_dex_file_;
David Brazdilca3c8c32016-09-06 14:04:48 +0100405 jobject class_loader_;
406 mirror::Class* klass_Main_;
407};
408
409TEST_F(VerifierDepsTest, StringToId) {
410 ScopedObjectAccess soa(Thread::Current());
411 LoadDexFile(&soa);
412
413 MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
414
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100415 uint32_t id_Main1 = verifier_deps_->GetIdFromString(*primary_dex_file_, "LMain;");
416 ASSERT_LT(id_Main1, primary_dex_file_->NumStringIds());
417 ASSERT_EQ("LMain;", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Main1));
David Brazdilca3c8c32016-09-06 14:04:48 +0100418
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100419 uint32_t id_Main2 = verifier_deps_->GetIdFromString(*primary_dex_file_, "LMain;");
420 ASSERT_LT(id_Main2, primary_dex_file_->NumStringIds());
421 ASSERT_EQ("LMain;", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Main2));
David Brazdilca3c8c32016-09-06 14:04:48 +0100422
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100423 uint32_t id_Lorem1 = verifier_deps_->GetIdFromString(*primary_dex_file_, "Lorem ipsum");
424 ASSERT_GE(id_Lorem1, primary_dex_file_->NumStringIds());
425 ASSERT_EQ("Lorem ipsum", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Lorem1));
David Brazdilca3c8c32016-09-06 14:04:48 +0100426
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100427 uint32_t id_Lorem2 = verifier_deps_->GetIdFromString(*primary_dex_file_, "Lorem ipsum");
428 ASSERT_GE(id_Lorem2, primary_dex_file_->NumStringIds());
429 ASSERT_EQ("Lorem ipsum", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Lorem2));
David Brazdilca3c8c32016-09-06 14:04:48 +0100430
431 ASSERT_EQ(id_Main1, id_Main2);
432 ASSERT_EQ(id_Lorem1, id_Lorem2);
433 ASSERT_NE(id_Main1, id_Lorem1);
434}
435
436TEST_F(VerifierDepsTest, Assignable_BothInBoot) {
437 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/util/TimeZone;",
438 /* src */ "Ljava/util/SimpleTimeZone;",
439 /* is_strict */ true,
440 /* is_assignable */ true));
441 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
442}
443
444TEST_F(VerifierDepsTest, Assignable_DestinationInBoot1) {
445 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/net/Socket;",
446 /* src */ "LMySSLSocket;",
447 /* is_strict */ true,
448 /* is_assignable */ true));
449 ASSERT_TRUE(HasAssignable("Ljava/net/Socket;", "LMySSLSocket;", true));
450}
451
452TEST_F(VerifierDepsTest, Assignable_DestinationInBoot2) {
453 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/util/TimeZone;",
454 /* src */ "LMySimpleTimeZone;",
455 /* is_strict */ true,
456 /* is_assignable */ true));
457 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "LMySimpleTimeZone;", true));
458}
459
460TEST_F(VerifierDepsTest, Assignable_DestinationInBoot3) {
461 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/util/Collection;",
462 /* src */ "LMyThreadSet;",
463 /* is_strict */ true,
464 /* is_assignable */ true));
465 ASSERT_TRUE(HasAssignable("Ljava/util/Collection;", "LMyThreadSet;", true));
466}
467
468TEST_F(VerifierDepsTest, Assignable_BothArrays_Resolved) {
469 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "[[Ljava/util/TimeZone;",
470 /* src */ "[[Ljava/util/SimpleTimeZone;",
471 /* is_strict */ true,
472 /* is_assignable */ true));
473 // If the component types of both arrays are resolved, we optimize the list of
474 // dependencies by recording a dependency on the component types.
475 ASSERT_FALSE(HasAssignable("[[Ljava/util/TimeZone;", "[[Ljava/util/SimpleTimeZone;", true));
476 ASSERT_FALSE(HasAssignable("[Ljava/util/TimeZone;", "[Ljava/util/SimpleTimeZone;", true));
477 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
478}
479
480TEST_F(VerifierDepsTest, Assignable_BothArrays_Erroneous) {
481 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "[[Ljava/util/TimeZone;",
482 /* src */ "[[LMyErroneousTimeZone;",
483 /* is_strict */ true,
484 /* is_assignable */ true));
485 // If the component type of an array is erroneous, we record the dependency on
486 // the array type.
487 ASSERT_FALSE(HasAssignable("[[Ljava/util/TimeZone;", "[[LMyErroneousTimeZone;", true));
488 ASSERT_TRUE(HasAssignable("[Ljava/util/TimeZone;", "[LMyErroneousTimeZone;", true));
489 ASSERT_FALSE(HasAssignable("Ljava/util/TimeZone;", "LMyErroneousTimeZone;", true));
490}
491
492 // We test that VerifierDeps does not try to optimize by storing assignability
493 // of the component types. This is due to the fact that the component type may
494 // be an erroneous class, even though the array type has resolved status.
495
496TEST_F(VerifierDepsTest, Assignable_ArrayToInterface1) {
497 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/io/Serializable;",
498 /* src */ "[Ljava/util/TimeZone;",
499 /* is_strict */ true,
500 /* is_assignable */ true));
501 ASSERT_TRUE(HasAssignable("Ljava/io/Serializable;", "[Ljava/util/TimeZone;", true));
502}
503
504TEST_F(VerifierDepsTest, Assignable_ArrayToInterface2) {
505 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/io/Serializable;",
506 /* src */ "[LMyThreadSet;",
507 /* is_strict */ true,
508 /* is_assignable */ true));
509 ASSERT_TRUE(HasAssignable("Ljava/io/Serializable;", "[LMyThreadSet;", true));
510}
511
512TEST_F(VerifierDepsTest, NotAssignable_BothInBoot) {
513 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
514 /* src */ "Ljava/util/SimpleTimeZone;",
515 /* is_strict */ true,
516 /* is_assignable */ false));
517 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
518}
519
520TEST_F(VerifierDepsTest, NotAssignable_DestinationInBoot1) {
521 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
522 /* src */ "LMySSLSocket;",
523 /* is_strict */ true,
524 /* is_assignable */ false));
525 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "LMySSLSocket;", false));
526}
527
528TEST_F(VerifierDepsTest, NotAssignable_DestinationInBoot2) {
529 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "Ljava/lang/Exception;",
530 /* src */ "LMySimpleTimeZone;",
531 /* is_strict */ true,
532 /* is_assignable */ false));
533 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "LMySimpleTimeZone;", false));
534}
535
536TEST_F(VerifierDepsTest, NotAssignable_BothArrays) {
537 ASSERT_TRUE(TestAssignabilityRecording(/* dst */ "[Ljava/lang/Exception;",
538 /* src */ "[Ljava/util/SimpleTimeZone;",
539 /* is_strict */ true,
540 /* is_assignable */ false));
541 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
542}
543
544TEST_F(VerifierDepsTest, ArgumentType_ResolvedClass) {
545 ASSERT_TRUE(VerifyMethod("ArgumentType_ResolvedClass"));
546 ASSERT_TRUE(HasClass("Ljava/lang/Thread;", true, "public"));
547}
548
549TEST_F(VerifierDepsTest, ArgumentType_ResolvedReferenceArray) {
550 ASSERT_TRUE(VerifyMethod("ArgumentType_ResolvedReferenceArray"));
551 ASSERT_TRUE(HasClass("[Ljava/lang/Thread;", true, "public final abstract"));
552}
553
554TEST_F(VerifierDepsTest, ArgumentType_ResolvedPrimitiveArray) {
555 ASSERT_TRUE(VerifyMethod("ArgumentType_ResolvedPrimitiveArray"));
556 ASSERT_TRUE(HasClass("[B", true, "public final abstract"));
557}
558
559TEST_F(VerifierDepsTest, ArgumentType_UnresolvedClass) {
560 ASSERT_TRUE(VerifyMethod("ArgumentType_UnresolvedClass"));
561 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
562}
563
564TEST_F(VerifierDepsTest, ArgumentType_UnresolvedSuper) {
565 ASSERT_TRUE(VerifyMethod("ArgumentType_UnresolvedSuper"));
566 ASSERT_TRUE(HasClass("LMySetWithUnresolvedSuper;", false));
567}
568
569TEST_F(VerifierDepsTest, ReturnType_Reference) {
570 ASSERT_TRUE(VerifyMethod("ReturnType_Reference"));
571 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/lang/IllegalStateException;", true));
572}
573
574TEST_F(VerifierDepsTest, ReturnType_Array) {
575 ASSERT_FALSE(VerifyMethod("ReturnType_Array"));
576 ASSERT_TRUE(HasAssignable("Ljava/lang/Integer;", "Ljava/lang/IllegalStateException;", false));
577}
578
579TEST_F(VerifierDepsTest, InvokeArgumentType) {
580 ASSERT_TRUE(VerifyMethod("InvokeArgumentType"));
581 ASSERT_TRUE(HasClass("Ljava/text/SimpleDateFormat;", true, "public"));
582 ASSERT_TRUE(HasClass("Ljava/util/SimpleTimeZone;", true, "public"));
583 ASSERT_TRUE(HasMethod("virtual",
584 "Ljava/text/SimpleDateFormat;",
585 "setTimeZone",
586 "(Ljava/util/TimeZone;)V",
587 true,
588 "public",
589 "Ljava/text/DateFormat;"));
590 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
591}
592
593TEST_F(VerifierDepsTest, MergeTypes_RegisterLines) {
594 ASSERT_TRUE(VerifyMethod("MergeTypes_RegisterLines"));
595 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "LMySocketTimeoutException;", true));
596 ASSERT_TRUE(HasAssignable(
597 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
598}
599
600TEST_F(VerifierDepsTest, MergeTypes_IfInstanceOf) {
601 ASSERT_TRUE(VerifyMethod("MergeTypes_IfInstanceOf"));
602 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
603 ASSERT_TRUE(HasAssignable(
604 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
605 ASSERT_TRUE(HasAssignable("Ljava/net/SocketTimeoutException;", "Ljava/lang/Exception;", false));
606}
607
608TEST_F(VerifierDepsTest, MergeTypes_Unresolved) {
609 ASSERT_TRUE(VerifyMethod("MergeTypes_Unresolved"));
610 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
611 ASSERT_TRUE(HasAssignable(
612 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
613}
614
615TEST_F(VerifierDepsTest, ConstClass_Resolved) {
616 ASSERT_TRUE(VerifyMethod("ConstClass_Resolved"));
617 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
618}
619
620TEST_F(VerifierDepsTest, ConstClass_Unresolved) {
621 ASSERT_TRUE(VerifyMethod("ConstClass_Unresolved"));
622 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
623}
624
625TEST_F(VerifierDepsTest, CheckCast_Resolved) {
626 ASSERT_TRUE(VerifyMethod("CheckCast_Resolved"));
627 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
628}
629
630TEST_F(VerifierDepsTest, CheckCast_Unresolved) {
631 ASSERT_TRUE(VerifyMethod("CheckCast_Unresolved"));
632 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
633}
634
635TEST_F(VerifierDepsTest, InstanceOf_Resolved) {
636 ASSERT_TRUE(VerifyMethod("InstanceOf_Resolved"));
637 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
638}
639
640TEST_F(VerifierDepsTest, InstanceOf_Unresolved) {
641 ASSERT_TRUE(VerifyMethod("InstanceOf_Unresolved"));
642 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
643}
644
645TEST_F(VerifierDepsTest, NewInstance_Resolved) {
646 ASSERT_TRUE(VerifyMethod("NewInstance_Resolved"));
647 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
648}
649
650TEST_F(VerifierDepsTest, NewInstance_Unresolved) {
651 ASSERT_TRUE(VerifyMethod("NewInstance_Unresolved"));
652 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
653}
654
655TEST_F(VerifierDepsTest, NewArray_Resolved) {
656 ASSERT_TRUE(VerifyMethod("NewArray_Resolved"));
657 ASSERT_TRUE(HasClass("[Ljava/lang/IllegalStateException;", true, "public final abstract"));
658}
659
660TEST_F(VerifierDepsTest, NewArray_Unresolved) {
661 ASSERT_TRUE(VerifyMethod("NewArray_Unresolved"));
662 ASSERT_TRUE(HasClass("[LUnresolvedClass;", false));
663}
664
665TEST_F(VerifierDepsTest, Throw) {
666 ASSERT_TRUE(VerifyMethod("Throw"));
667 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/lang/IllegalStateException;", true));
668}
669
670TEST_F(VerifierDepsTest, MoveException_Resolved) {
671 ASSERT_TRUE(VerifyMethod("MoveException_Resolved"));
672 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
673 ASSERT_TRUE(HasClass("Ljava/net/SocketTimeoutException;", true, "public"));
674 ASSERT_TRUE(HasClass("Ljava/util/zip/ZipException;", true, "public"));
675
676 // Testing that all exception types are assignable to Throwable.
677 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/io/InterruptedIOException;", true));
678 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
679 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/util/zip/ZipException;", true));
680
681 // Testing that the merge type is assignable to Throwable.
682 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/io/IOException;", true));
683
684 // Merging of exception types.
685 ASSERT_TRUE(HasAssignable("Ljava/io/IOException;", "Ljava/io/InterruptedIOException;", true));
686 ASSERT_TRUE(HasAssignable("Ljava/io/IOException;", "Ljava/util/zip/ZipException;", true));
687 ASSERT_TRUE(HasAssignable(
688 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
689}
690
691TEST_F(VerifierDepsTest, MoveException_Unresolved) {
692 ASSERT_FALSE(VerifyMethod("MoveException_Unresolved"));
693 ASSERT_TRUE(HasClass("LUnresolvedException;", false));
694}
695
696TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInReferenced) {
697 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInReferenced"));
698 ASSERT_TRUE(HasClass("Ljava/lang/System;", true, "public final"));
699 ASSERT_TRUE(HasField("Ljava/lang/System;",
700 "out",
701 "Ljava/io/PrintStream;",
702 true,
703 "public final static",
704 "Ljava/lang/System;"));
705}
706
707TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInSuperclass1) {
708 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInSuperclass1"));
709 ASSERT_TRUE(HasClass("Ljava/util/SimpleTimeZone;", true, "public"));
710 ASSERT_TRUE(HasField(
711 "Ljava/util/SimpleTimeZone;", "LONG", "I", true, "public final static", "Ljava/util/TimeZone;"));
712}
713
714TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInSuperclass2) {
715 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInSuperclass2"));
716 ASSERT_TRUE(HasField(
717 "LMySimpleTimeZone;", "SHORT", "I", true, "public final static", "Ljava/util/TimeZone;"));
718}
719
720TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface1) {
721 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface1"));
722 ASSERT_TRUE(HasClass("Ljavax/xml/transform/dom/DOMResult;", true, "public"));
723 ASSERT_TRUE(HasField("Ljavax/xml/transform/dom/DOMResult;",
724 "PI_ENABLE_OUTPUT_ESCAPING",
725 "Ljava/lang/String;",
726 true,
727 "public final static",
728 "Ljavax/xml/transform/Result;"));
729}
730
731TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface2) {
732 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface2"));
733 ASSERT_TRUE(HasField("LMyDOMResult;",
734 "PI_ENABLE_OUTPUT_ESCAPING",
735 "Ljava/lang/String;",
736 true,
737 "public final static",
738 "Ljavax/xml/transform/Result;"));
739}
740
741TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface3) {
742 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface3"));
743 ASSERT_TRUE(HasField("LMyResult;",
744 "PI_ENABLE_OUTPUT_ESCAPING",
745 "Ljava/lang/String;",
746 true,
747 "public final static",
748 "Ljavax/xml/transform/Result;"));
749}
750
751TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface4) {
752 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface4"));
753 ASSERT_TRUE(HasField("LMyDocument;",
754 "ELEMENT_NODE",
755 "S",
756 true,
757 "public final static",
758 "Lorg/w3c/dom/Node;"));
759}
760
761TEST_F(VerifierDepsTest, StaticField_Unresolved_ReferrerInBoot) {
762 ASSERT_TRUE(VerifyMethod("StaticField_Unresolved_ReferrerInBoot"));
763 ASSERT_TRUE(HasClass("Ljava/util/TimeZone;", true, "public abstract"));
764 ASSERT_TRUE(HasField("Ljava/util/TimeZone;", "x", "I", false));
765}
766
767TEST_F(VerifierDepsTest, StaticField_Unresolved_ReferrerInDex) {
768 ASSERT_TRUE(VerifyMethod("StaticField_Unresolved_ReferrerInDex"));
769 ASSERT_TRUE(HasField("LMyThreadSet;", "x", "I", false));
770}
771
772TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInReferenced) {
773 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInReferenced"));
774 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
775 ASSERT_TRUE(HasField("Ljava/io/InterruptedIOException;",
776 "bytesTransferred",
777 "I",
778 true,
779 "public",
780 "Ljava/io/InterruptedIOException;"));
781 ASSERT_TRUE(HasAssignable(
782 "Ljava/io/InterruptedIOException;", "LMySocketTimeoutException;", true));
783}
784
785TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInSuperclass1) {
786 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInSuperclass1"));
787 ASSERT_TRUE(HasClass("Ljava/net/SocketTimeoutException;", true, "public"));
788 ASSERT_TRUE(HasField("Ljava/net/SocketTimeoutException;",
789 "bytesTransferred",
790 "I",
791 true,
792 "public",
793 "Ljava/io/InterruptedIOException;"));
794 ASSERT_TRUE(HasAssignable(
795 "Ljava/io/InterruptedIOException;", "LMySocketTimeoutException;", true));
796}
797
798TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInSuperclass2) {
799 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInSuperclass2"));
800 ASSERT_TRUE(HasField("LMySocketTimeoutException;",
801 "bytesTransferred",
802 "I",
803 true,
804 "public",
805 "Ljava/io/InterruptedIOException;"));
806 ASSERT_TRUE(HasAssignable(
807 "Ljava/io/InterruptedIOException;", "LMySocketTimeoutException;", true));
808}
809
810TEST_F(VerifierDepsTest, InstanceField_Unresolved_ReferrerInBoot) {
811 ASSERT_TRUE(VerifyMethod("InstanceField_Unresolved_ReferrerInBoot"));
812 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
813 ASSERT_TRUE(HasField("Ljava/io/InterruptedIOException;", "x", "I", false));
814}
815
816TEST_F(VerifierDepsTest, InstanceField_Unresolved_ReferrerInDex) {
817 ASSERT_TRUE(VerifyMethod("InstanceField_Unresolved_ReferrerInDex"));
818 ASSERT_TRUE(HasField("LMyThreadSet;", "x", "I", false));
819}
820
821TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInReferenced) {
822 ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInReferenced"));
823 ASSERT_TRUE(HasClass("Ljava/net/Socket;", true, "public"));
824 ASSERT_TRUE(HasMethod("direct",
825 "Ljava/net/Socket;",
826 "setSocketImplFactory",
827 "(Ljava/net/SocketImplFactory;)V",
828 true,
829 "public static",
830 "Ljava/net/Socket;"));
831}
832
833TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInSuperclass1) {
834 ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInSuperclass1"));
835 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public abstract"));
836 ASSERT_TRUE(HasMethod("direct",
837 "Ljavax/net/ssl/SSLSocket;",
838 "setSocketImplFactory",
839 "(Ljava/net/SocketImplFactory;)V",
840 true,
841 "public static",
842 "Ljava/net/Socket;"));
843}
844
845TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInSuperclass2) {
846 ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInSuperclass2"));
847 ASSERT_TRUE(HasMethod("direct",
848 "LMySSLSocket;",
849 "setSocketImplFactory",
850 "(Ljava/net/SocketImplFactory;)V",
851 true,
852 "public static",
853 "Ljava/net/Socket;"));
854}
855
856TEST_F(VerifierDepsTest, InvokeStatic_DeclaredInInterface1) {
857 ASSERT_TRUE(VerifyMethod("InvokeStatic_DeclaredInInterface1"));
858 ASSERT_TRUE(HasClass("Ljava/util/Map$Entry;", true, "public abstract interface"));
859 ASSERT_TRUE(HasMethod("direct",
860 "Ljava/util/Map$Entry;",
861 "comparingByKey",
862 "()Ljava/util/Comparator;",
863 true,
864 "public static",
865 "Ljava/util/Map$Entry;"));
866}
867
868TEST_F(VerifierDepsTest, InvokeStatic_DeclaredInInterface2) {
869 ASSERT_FALSE(VerifyMethod("InvokeStatic_DeclaredInInterface2"));
870 ASSERT_TRUE(HasClass("Ljava/util/AbstractMap$SimpleEntry;", true, "public"));
871 ASSERT_TRUE(HasMethod("direct",
872 "Ljava/util/AbstractMap$SimpleEntry;",
873 "comparingByKey",
874 "()Ljava/util/Comparator;",
875 false));
876}
877
878TEST_F(VerifierDepsTest, InvokeStatic_Unresolved1) {
879 ASSERT_FALSE(VerifyMethod("InvokeStatic_Unresolved1"));
880 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public abstract"));
881 ASSERT_TRUE(HasMethod("direct", "Ljavax/net/ssl/SSLSocket;", "x", "()V", false));
882}
883
884TEST_F(VerifierDepsTest, InvokeStatic_Unresolved2) {
885 ASSERT_FALSE(VerifyMethod("InvokeStatic_Unresolved2"));
886 ASSERT_TRUE(HasMethod("direct", "LMySSLSocket;", "x", "()V", false));
887}
888
889TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInReferenced) {
890 ASSERT_TRUE(VerifyMethod("InvokeDirect_Resolved_DeclaredInReferenced"));
891 ASSERT_TRUE(HasClass("Ljava/net/Socket;", true, "public"));
892 ASSERT_TRUE(HasMethod(
893 "direct", "Ljava/net/Socket;", "<init>", "()V", true, "public", "Ljava/net/Socket;"));
894}
895
896TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInSuperclass1) {
897 ASSERT_FALSE(VerifyMethod("InvokeDirect_Resolved_DeclaredInSuperclass1"));
898 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public abstract"));
899 ASSERT_TRUE(HasMethod("direct",
900 "Ljavax/net/ssl/SSLSocket;",
901 "checkOldImpl",
902 "()V",
903 true,
904 "private",
905 "Ljava/net/Socket;"));
906}
907
908TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInSuperclass2) {
909 ASSERT_FALSE(VerifyMethod("InvokeDirect_Resolved_DeclaredInSuperclass2"));
910 ASSERT_TRUE(HasMethod(
911 "direct", "LMySSLSocket;", "checkOldImpl", "()V", true, "private", "Ljava/net/Socket;"));
912}
913
914TEST_F(VerifierDepsTest, InvokeDirect_Unresolved1) {
915 ASSERT_FALSE(VerifyMethod("InvokeDirect_Unresolved1"));
916 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public abstract"));
917 ASSERT_TRUE(HasMethod("direct", "Ljavax/net/ssl/SSLSocket;", "x", "()V", false));
918}
919
920TEST_F(VerifierDepsTest, InvokeDirect_Unresolved2) {
921 ASSERT_FALSE(VerifyMethod("InvokeDirect_Unresolved2"));
922 ASSERT_TRUE(HasMethod("direct", "LMySSLSocket;", "x", "()V", false));
923}
924
925TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInReferenced) {
926 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInReferenced"));
927 ASSERT_TRUE(HasClass("Ljava/lang/Throwable;", true, "public"));
928 ASSERT_TRUE(HasMethod("virtual",
929 "Ljava/lang/Throwable;",
930 "getMessage",
931 "()Ljava/lang/String;",
932 true,
933 "public",
934 "Ljava/lang/Throwable;"));
935 // Type dependency on `this` argument.
936 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "LMySocketTimeoutException;", true));
937}
938
939TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperclass1) {
940 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperclass1"));
941 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
942 ASSERT_TRUE(HasMethod("virtual",
943 "Ljava/io/InterruptedIOException;",
944 "getMessage",
945 "()Ljava/lang/String;",
946 true,
947 "public",
948 "Ljava/lang/Throwable;"));
949 // Type dependency on `this` argument.
950 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "LMySocketTimeoutException;", true));
951}
952
953TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperclass2) {
954 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperclass2"));
955 ASSERT_TRUE(HasMethod("virtual",
956 "LMySocketTimeoutException;",
957 "getMessage",
958 "()Ljava/lang/String;",
959 true,
960 "public",
961 "Ljava/lang/Throwable;"));
962}
963
964TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperinterface) {
965 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperinterface"));
966 ASSERT_TRUE(HasMethod("virtual",
967 "LMyThreadSet;",
968 "size",
969 "()I",
970 true,
971 "public abstract",
972 "Ljava/util/Set;"));
973}
974
975TEST_F(VerifierDepsTest, InvokeVirtual_Unresolved1) {
976 ASSERT_FALSE(VerifyMethod("InvokeVirtual_Unresolved1"));
977 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
978 ASSERT_TRUE(HasMethod("virtual", "Ljava/io/InterruptedIOException;", "x", "()V", false));
979}
980
981TEST_F(VerifierDepsTest, InvokeVirtual_Unresolved2) {
982 ASSERT_FALSE(VerifyMethod("InvokeVirtual_Unresolved2"));
983 ASSERT_TRUE(HasMethod("virtual", "LMySocketTimeoutException;", "x", "()V", false));
984}
985
986TEST_F(VerifierDepsTest, InvokeVirtual_ActuallyDirect) {
987 ASSERT_FALSE(VerifyMethod("InvokeVirtual_ActuallyDirect"));
988 ASSERT_TRUE(HasMethod("virtual", "LMyThread;", "activeCount", "()I", false));
989 ASSERT_TRUE(HasMethod("direct",
990 "LMyThread;",
991 "activeCount",
992 "()I",
993 true,
994 "public static",
995 "Ljava/lang/Thread;"));
996}
997
998TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInReferenced) {
999 ASSERT_TRUE(VerifyMethod("InvokeInterface_Resolved_DeclaredInReferenced"));
1000 ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public abstract interface"));
1001 ASSERT_TRUE(HasMethod("interface",
1002 "Ljava/lang/Runnable;",
1003 "run",
1004 "()V",
1005 true,
1006 "public abstract",
1007 "Ljava/lang/Runnable;"));
1008}
1009
1010TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperclass) {
1011 ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperclass"));
1012 ASSERT_TRUE(HasMethod("interface", "LMyThread;", "join", "()V", false));
1013}
1014
1015TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperinterface1) {
1016 ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperinterface1"));
1017 ASSERT_TRUE(HasMethod("interface",
1018 "LMyThreadSet;",
1019 "run",
1020 "()V",
1021 true,
1022 "public abstract",
1023 "Ljava/lang/Runnable;"));
1024}
1025
1026TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperinterface2) {
1027 ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperinterface2"));
1028 ASSERT_TRUE(HasMethod("interface",
1029 "LMyThreadSet;",
1030 "isEmpty",
1031 "()Z",
1032 true,
1033 "public abstract",
1034 "Ljava/util/Set;"));
1035}
1036
1037TEST_F(VerifierDepsTest, InvokeInterface_Unresolved1) {
1038 ASSERT_FALSE(VerifyMethod("InvokeInterface_Unresolved1"));
1039 ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public abstract interface"));
1040 ASSERT_TRUE(HasMethod("interface", "Ljava/lang/Runnable;", "x", "()V", false));
1041}
1042
1043TEST_F(VerifierDepsTest, InvokeInterface_Unresolved2) {
1044 ASSERT_FALSE(VerifyMethod("InvokeInterface_Unresolved2"));
1045 ASSERT_TRUE(HasMethod("interface", "LMyThreadSet;", "x", "()V", false));
1046}
1047
1048TEST_F(VerifierDepsTest, InvokeSuper_ThisAssignable) {
1049 ASSERT_TRUE(VerifyMethod("InvokeSuper_ThisAssignable"));
1050 ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public abstract interface"));
1051 ASSERT_TRUE(HasAssignable("Ljava/lang/Runnable;", "LMain;", true));
1052 ASSERT_TRUE(HasMethod("interface",
1053 "Ljava/lang/Runnable;",
1054 "run",
1055 "()V",
1056 true,
1057 "public abstract",
1058 "Ljava/lang/Runnable;"));
1059}
1060
1061TEST_F(VerifierDepsTest, InvokeSuper_ThisNotAssignable) {
1062 ASSERT_FALSE(VerifyMethod("InvokeSuper_ThisNotAssignable"));
1063 ASSERT_TRUE(HasClass("Ljava/lang/Integer;", true, "public final"));
1064 ASSERT_TRUE(HasAssignable("Ljava/lang/Integer;", "LMain;", false));
1065 ASSERT_TRUE(HasMethod(
1066 "virtual", "Ljava/lang/Integer;", "intValue", "()I", true, "public", "Ljava/lang/Integer;"));
1067}
1068
David Brazdil6f82fbd2016-09-14 11:55:26 +01001069TEST_F(VerifierDepsTest, EncodeDecode) {
1070 VerifyDexFile();
1071
1072 ASSERT_EQ(1u, NumberOfCompiledDexFiles());
1073 ASSERT_TRUE(HasEachKindOfRecord());
1074
1075 std::vector<uint8_t> buffer;
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +01001076 verifier_deps_->Encode(dex_files_, &buffer);
David Brazdil6f82fbd2016-09-14 11:55:26 +01001077 ASSERT_FALSE(buffer.empty());
1078
Nicolas Geoffraye70dd562016-10-30 21:03:35 +00001079 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
David Brazdil6f82fbd2016-09-14 11:55:26 +01001080 ASSERT_TRUE(verifier_deps_->Equals(decoded_deps));
1081}
1082
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +01001083TEST_F(VerifierDepsTest, EncodeDecodeMulti) {
1084 VerifyDexFile("MultiDex");
1085
1086 ASSERT_GT(NumberOfCompiledDexFiles(), 1u);
1087 std::vector<uint8_t> buffer;
1088 verifier_deps_->Encode(dex_files_, &buffer);
1089 ASSERT_FALSE(buffer.empty());
1090
1091 // Create new DexFile, to mess with std::map order: the verifier deps used
1092 // to iterate over the map, which doesn't guarantee insertion order. We fixed
1093 // this by passing the expected order when encoding/decoding.
1094 std::vector<std::unique_ptr<const DexFile>> first_dex_files = OpenTestDexFiles("VerifierDeps");
1095 std::vector<std::unique_ptr<const DexFile>> second_dex_files = OpenTestDexFiles("MultiDex");
1096 std::vector<const DexFile*> dex_files;
1097 for (auto& dex_file : first_dex_files) {
1098 dex_files.push_back(dex_file.get());
1099 }
1100 for (auto& dex_file : second_dex_files) {
1101 dex_files.push_back(dex_file.get());
1102 }
1103
1104 // Dump the new verifier deps to ensure it can properly read the data.
Nicolas Geoffraye70dd562016-10-30 21:03:35 +00001105 VerifierDeps decoded_deps(dex_files, ArrayRef<const uint8_t>(buffer));
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +01001106 std::ostringstream stream;
1107 VariableIndentationOutputStream os(&stream);
1108 decoded_deps.Dump(&os);
1109}
1110
Nicolas Geoffray08025182016-10-25 17:20:18 +01001111TEST_F(VerifierDepsTest, UnverifiedClasses) {
1112 VerifyDexFile();
1113 ASSERT_FALSE(HasUnverifiedClass("LMyThread;"));
1114 // Test that a class with a soft failure is recorded.
1115 ASSERT_TRUE(HasUnverifiedClass("LMain;"));
1116 // Test that a class with hard failure is recorded.
1117 ASSERT_TRUE(HasUnverifiedClass("LMyVerificationFailure;"));
1118 // Test that a class with unresolved super is recorded.
1119 ASSERT_FALSE(HasUnverifiedClass("LMyClassWithNoSuper;"));
1120 // Test that a class with unresolved super and hard failure is recorded.
1121 ASSERT_TRUE(HasUnverifiedClass("LMyClassWithNoSuperButFailures;"));
1122}
1123
David Brazdilca3c8c32016-09-06 14:04:48 +01001124} // namespace verifier
1125} // namespace art