blob: 4094425f87e8dacd342827b3b90439622370f6cd [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
Andreas Gampec6ea7d02017-02-01 16:46:28 -080020#include "art_method-inl.h"
David Sehr9c4a0152018-04-05 12:23:54 -070021#include "base/indenter.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010022#include "class_linker.h"
Vladimir Marko815d5e52019-03-04 10:18:31 +000023#include "common_compiler_driver_test.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010024#include "compiler_callbacks.h"
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -070025#include "dex/class_accessor-inl.h"
26#include "dex/class_iterator.h"
David Sehr9e734c72018-01-04 17:56:19 -080027#include "dex/dex_file-inl.h"
28#include "dex/dex_file_types.h"
Andreas Gamped9911ee2017-03-27 13:27:24 -070029#include "dex/verification_results.h"
30#include "dex/verified_method.h"
Andreas Gamped482e732017-04-24 17:59:09 -070031#include "driver/compiler_driver-inl.h"
Andreas Gamped9911ee2017-03-27 13:27:24 -070032#include "driver/compiler_options.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010033#include "handle_scope-inl.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010034#include "mirror/class_loader.h"
35#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070036#include "scoped_thread_state_change-inl.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070037#include "thread.h"
Mathieu Chartier93764b82017-07-17 14:51:53 -070038#include "utils/atomic_dex_ref_map-inl.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070039#include "verifier/method_verifier-inl.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010040
41namespace art {
42namespace verifier {
43
44class VerifierDepsCompilerCallbacks : public CompilerCallbacks {
45 public:
Igor Murashkin2ffb7032017-11-08 13:35:21 -080046 VerifierDepsCompilerCallbacks()
David Brazdilca3c8c32016-09-06 14:04:48 +010047 : CompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileApp),
48 deps_(nullptr) {}
49
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010050 void MethodVerified(verifier::MethodVerifier* verifier ATTRIBUTE_UNUSED) override {}
51 void ClassRejected(ClassReference ref ATTRIBUTE_UNUSED) override {}
David Brazdilca3c8c32016-09-06 14:04:48 +010052
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010053 verifier::VerifierDeps* GetVerifierDeps() const override { return deps_; }
Andreas Gampefa6a1b02018-09-07 08:11:55 -070054 void SetVerifierDeps(verifier::VerifierDeps* deps) override { deps_ = deps; }
David Brazdilca3c8c32016-09-06 14:04:48 +010055
56 private:
57 verifier::VerifierDeps* deps_;
58};
59
Vladimir Marko815d5e52019-03-04 10:18:31 +000060class VerifierDepsTest : public CommonCompilerDriverTest {
David Brazdilca3c8c32016-09-06 14:04:48 +010061 public:
Andreas Gampefa6a1b02018-09-07 08:11:55 -070062 void SetUpRuntimeOptions(RuntimeOptions* options) override {
Nicolas Geoffray08025182016-10-25 17:20:18 +010063 CommonCompilerTest::SetUpRuntimeOptions(options);
David Brazdilca3c8c32016-09-06 14:04:48 +010064 callbacks_.reset(new VerifierDepsCompilerCallbacks());
65 }
66
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010067 ObjPtr<mirror::Class> FindClassByName(ScopedObjectAccess& soa, const std::string& name)
David Brazdilca3c8c32016-09-06 14:04:48 +010068 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010069 StackHandleScope<1> hs(soa.Self());
David Brazdilca3c8c32016-09-06 14:04:48 +010070 Handle<mirror::ClassLoader> class_loader_handle(
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010071 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_)));
72 ObjPtr<mirror::Class> klass =
73 class_linker_->FindClass(soa.Self(), name.c_str(), class_loader_handle);
David Brazdil6f82fbd2016-09-14 11:55:26 +010074 if (klass == nullptr) {
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010075 DCHECK(soa.Self()->IsExceptionPending());
76 soa.Self()->ClearException();
David Brazdil6f82fbd2016-09-14 11:55:26 +010077 }
78 return klass;
79 }
80
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +000081 void SetupCompilerDriver() {
Vladimir Marko9c4b9702018-11-14 15:09:02 +000082 compiler_options_->image_type_ = CompilerOptions::ImageType::kNone;
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +000083 compiler_driver_->InitializeThreadPools();
84 }
85
Andreas Gampe3db70682018-12-26 15:12:03 -080086 void VerifyWithCompilerDriver(verifier::VerifierDeps* verifier_deps) {
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +000087 TimingLogger timings("Verify", false, false);
88 // The compiler driver handles the verifier deps in the callbacks, so
89 // remove what this class did for unit testing.
Andreas Gampe3db70682018-12-26 15:12:03 -080090 if (verifier_deps == nullptr) {
Mathieu Chartier72041a02017-07-14 18:23:25 -070091 // Create some verifier deps by default if they are not already specified.
Andreas Gampe3db70682018-12-26 15:12:03 -080092 verifier_deps = new verifier::VerifierDeps(dex_files_);
93 verifier_deps_.reset(verifier_deps);
Mathieu Chartier72041a02017-07-14 18:23:25 -070094 }
Andreas Gampe3db70682018-12-26 15:12:03 -080095 callbacks_->SetVerifierDeps(verifier_deps);
Vladimir Marko2afaff72018-11-30 17:01:50 +000096 compiler_driver_->Verify(class_loader_, dex_files_, &timings, verification_results_.get());
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000097 callbacks_->SetVerifierDeps(nullptr);
Nicolas Geoffray1d0ae3f2016-12-06 13:40:16 +000098 // Clear entries in the verification results to avoid hitting a DCHECK that
99 // we always succeed inserting a new entry after verifying.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700100 AtomicDexRefMap<MethodReference, const VerifiedMethod*>* map =
Vladimir Marko2afaff72018-11-30 17:01:50 +0000101 &verification_results_->atomic_verified_methods_;
Mathieu Chartier93764b82017-07-17 14:51:53 -0700102 map->Visit([](const DexFileReference& ref ATTRIBUTE_UNUSED, const VerifiedMethod* method) {
Nicolas Geoffray1d0ae3f2016-12-06 13:40:16 +0000103 delete method;
104 });
105 map->ClearEntries();
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000106 }
107
David Brazdil6f82fbd2016-09-14 11:55:26 +0100108 void SetVerifierDeps(const std::vector<const DexFile*>& dex_files) {
109 verifier_deps_.reset(new verifier::VerifierDeps(dex_files));
110 VerifierDepsCompilerCallbacks* callbacks =
111 reinterpret_cast<VerifierDepsCompilerCallbacks*>(callbacks_.get());
112 callbacks->SetVerifierDeps(verifier_deps_.get());
David Brazdilca3c8c32016-09-06 14:04:48 +0100113 }
114
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100115 void LoadDexFile(ScopedObjectAccess& soa, const char* name1, const char* name2 = nullptr)
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100116 REQUIRES_SHARED(Locks::mutator_lock_) {
117 class_loader_ = (name2 == nullptr) ? LoadDex(name1) : LoadMultiDex(name1, name2);
118 dex_files_ = GetDexFiles(class_loader_);
119 primary_dex_file_ = dex_files_.front();
120
121 SetVerifierDeps(dex_files_);
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100122 StackHandleScope<1> hs(soa.Self());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100123 Handle<mirror::ClassLoader> loader =
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100124 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_));
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100125 for (const DexFile* dex_file : dex_files_) {
126 class_linker_->RegisterDexFile(*dex_file, loader.Get());
127 }
Nicolas Geoffray1d0ae3f2016-12-06 13:40:16 +0000128 for (const DexFile* dex_file : dex_files_) {
Vladimir Marko2afaff72018-11-30 17:01:50 +0000129 verification_results_->AddDexFile(dex_file);
Nicolas Geoffray1d0ae3f2016-12-06 13:40:16 +0000130 }
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100131 SetDexFilesForOatFile(dex_files_);
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100132 }
133
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100134 void LoadDexFile(ScopedObjectAccess& soa) REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100135 LoadDexFile(soa, "VerifierDeps");
136 CHECK_EQ(dex_files_.size(), 1u);
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100137 klass_Main_ = FindClassByName(soa, "LMain;");
David Brazdilca3c8c32016-09-06 14:04:48 +0100138 CHECK(klass_Main_ != nullptr);
David Brazdilca3c8c32016-09-06 14:04:48 +0100139 }
140
141 bool VerifyMethod(const std::string& method_name) {
142 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100143 LoadDexFile(soa);
David Brazdilca3c8c32016-09-06 14:04:48 +0100144
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100145 StackHandleScope<2> hs(soa.Self());
David Brazdilca3c8c32016-09-06 14:04:48 +0100146 Handle<mirror::ClassLoader> class_loader_handle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700147 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_)));
David Brazdilca3c8c32016-09-06 14:04:48 +0100148 Handle<mirror::DexCache> dex_cache_handle(hs.NewHandle(klass_Main_->GetDexCache()));
149
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800150 const dex::ClassDef* class_def = klass_Main_->GetClassDef();
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -0700151 ClassAccessor accessor(*primary_dex_file_, *class_def);
David Brazdilca3c8c32016-09-06 14:04:48 +0100152
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -0700153 bool has_failures = true;
154 bool found_method = false;
David Brazdilca3c8c32016-09-06 14:04:48 +0100155
Mathieu Chartier0d896bd2018-05-25 00:20:27 -0700156 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
Vladimir Markoba118822017-06-12 15:41:56 +0100157 ArtMethod* resolved_method =
158 class_linker_->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -0700159 method.GetIndex(),
Vladimir Markoba118822017-06-12 15:41:56 +0100160 dex_cache_handle,
161 class_loader_handle,
Andreas Gampe3db70682018-12-26 15:12:03 -0800162 /* referrer= */ nullptr,
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -0700163 method.GetInvokeType(class_def->access_flags_));
David Brazdilca3c8c32016-09-06 14:04:48 +0100164 CHECK(resolved_method != nullptr);
165 if (method_name == resolved_method->GetName()) {
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -0700166 soa.Self()->SetVerifierDeps(callbacks_->GetVerifierDeps());
Andreas Gampefc25ae92019-04-19 22:22:57 -0700167 std::unique_ptr<MethodVerifier> verifier(
168 MethodVerifier::CreateVerifier(soa.Self(),
169 primary_dex_file_,
170 dex_cache_handle,
171 class_loader_handle,
172 *class_def,
173 method.GetCodeItem(),
174 method.GetIndex(),
175 resolved_method,
176 method.GetAccessFlags(),
177 /* can_load_classes= */ true,
178 /* allow_soft_failures= */ true,
179 /* need_precise_constants= */ true,
180 /* verify to dump */ false,
181 /* allow_thread_suspension= */ true,
182 /* api_level= */ 0));
183 verifier->Verify();
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -0700184 soa.Self()->SetVerifierDeps(nullptr);
Andreas Gampefc25ae92019-04-19 22:22:57 -0700185 has_failures = verifier->HasFailures();
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -0700186 found_method = true;
David Brazdilca3c8c32016-09-06 14:04:48 +0100187 }
Mathieu Chartier0d896bd2018-05-25 00:20:27 -0700188 }
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -0700189 CHECK(found_method) << "Expected to find method " << method_name;
190 return !has_failures;
David Brazdilca3c8c32016-09-06 14:04:48 +0100191 }
192
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100193 void VerifyDexFile(const char* multidex = nullptr) {
Nicolas Geoffray08025182016-10-25 17:20:18 +0100194 {
195 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100196 LoadDexFile(soa, "VerifierDeps", multidex);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100197 }
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000198 SetupCompilerDriver();
Andreas Gampe3db70682018-12-26 15:12:03 -0800199 VerifyWithCompilerDriver(/* verifier_deps= */ nullptr);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100200 }
201
David Brazdilca3c8c32016-09-06 14:04:48 +0100202 bool TestAssignabilityRecording(const std::string& dst,
203 const std::string& src,
204 bool is_strict,
205 bool is_assignable) {
206 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100207 LoadDexFile(soa);
208 StackHandleScope<1> hs(soa.Self());
209 Handle<mirror::Class> klass_dst = hs.NewHandle(FindClassByName(soa, dst));
Nicolas Geoffraybdb540d2017-04-19 13:50:34 +0100210 DCHECK(klass_dst != nullptr) << dst;
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100211 ObjPtr<mirror::Class> klass_src = FindClassByName(soa, src);
Nicolas Geoffraybdb540d2017-04-19 13:50:34 +0100212 DCHECK(klass_src != nullptr) << src;
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100213 verifier_deps_->AddAssignability(*primary_dex_file_,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100214 klass_dst.Get(),
David Brazdil6f82fbd2016-09-14 11:55:26 +0100215 klass_src,
David Brazdilca3c8c32016-09-06 14:04:48 +0100216 is_strict,
217 is_assignable);
218 return true;
219 }
220
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000221 // Check that the status of classes in `class_loader_` match the
222 // expected status in `deps`.
223 void VerifyClassStatus(const verifier::VerifierDeps& deps) {
224 ScopedObjectAccess soa(Thread::Current());
225 StackHandleScope<2> hs(soa.Self());
226 Handle<mirror::ClassLoader> class_loader_handle(
227 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader_)));
228 MutableHandle<mirror::Class> cls(hs.NewHandle<mirror::Class>(nullptr));
229 for (const DexFile* dex_file : dex_files_) {
David Brazdilfeb22822019-02-13 21:25:57 +0000230 const std::vector<bool>& verified_classes = deps.GetVerifiedClasses(*dex_file);
231 ASSERT_EQ(verified_classes.size(), dex_file->NumClassDefs());
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000232 for (uint32_t i = 0; i < dex_file->NumClassDefs(); ++i) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800233 const dex::ClassDef& class_def = dex_file->GetClassDef(i);
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000234 const char* descriptor = dex_file->GetClassDescriptor(class_def);
235 cls.Assign(class_linker_->FindClass(soa.Self(), descriptor, class_loader_handle));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800236 if (cls == nullptr) {
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000237 CHECK(soa.Self()->IsExceptionPending());
238 soa.Self()->ClearException();
Andreas Gampebb30d5d2018-04-23 09:59:25 -0700239 } else if (&cls->GetDexFile() != dex_file) {
240 // Ignore classes from different dex files.
David Brazdilfeb22822019-02-13 21:25:57 +0000241 } else if (verified_classes[i]) {
Vladimir Marko2c64a832018-01-04 11:31:56 +0000242 ASSERT_EQ(cls->GetStatus(), ClassStatus::kVerified);
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000243 } else {
Vladimir Marko2c64a832018-01-04 11:31:56 +0000244 ASSERT_LT(cls->GetStatus(), ClassStatus::kVerified);
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +0000245 }
246 }
247 }
248 }
249
David Brazdilfeb22822019-02-13 21:25:57 +0000250 uint16_t GetClassDefIndex(const std::string& cls, const DexFile& dex_file) {
251 const dex::TypeId* type_id = dex_file.FindTypeId(cls.c_str());
252 DCHECK(type_id != nullptr);
253 dex::TypeIndex type_idx = dex_file.GetIndexForTypeId(*type_id);
254 const dex::ClassDef* class_def = dex_file.FindClassDef(type_idx);
255 DCHECK(class_def != nullptr);
256 return dex_file.GetIndexForClassDef(*class_def);
257 }
258
Nicolas Geoffray08025182016-10-25 17:20:18 +0100259 bool HasUnverifiedClass(const std::string& cls) {
Nicolas Geoffray7cc3ae52017-03-07 14:33:37 +0000260 return HasUnverifiedClass(cls, *primary_dex_file_);
261 }
262
263 bool HasUnverifiedClass(const std::string& cls, const DexFile& dex_file) {
David Brazdilfeb22822019-02-13 21:25:57 +0000264 uint16_t class_def_idx = GetClassDefIndex(cls, dex_file);
265 return !verifier_deps_->GetVerifiedClasses(dex_file)[class_def_idx];
266 }
267
268 bool HasRedefinedClass(const std::string& cls) {
269 uint16_t class_def_idx = GetClassDefIndex(cls, *primary_dex_file_);
270 return verifier_deps_->GetRedefinedClasses(*primary_dex_file_)[class_def_idx];
Nicolas Geoffray08025182016-10-25 17:20:18 +0100271 }
272
David Brazdilca3c8c32016-09-06 14:04:48 +0100273 // Iterates over all assignability records and tries to find an entry which
274 // matches the expected destination/source pair.
275 bool HasAssignable(const std::string& expected_destination,
276 const std::string& expected_source,
277 bool expected_is_assignable) {
David Brazdilca3c8c32016-09-06 14:04:48 +0100278 for (auto& dex_dep : verifier_deps_->dex_deps_) {
279 const DexFile& dex_file = *dex_dep.first;
280 auto& storage = expected_is_assignable ? dex_dep.second->assignable_types_
281 : dex_dep.second->unassignable_types_;
282 for (auto& entry : storage) {
283 std::string actual_destination =
284 verifier_deps_->GetStringFromId(dex_file, entry.GetDestination());
285 std::string actual_source = verifier_deps_->GetStringFromId(dex_file, entry.GetSource());
286 if ((expected_destination == actual_destination) && (expected_source == actual_source)) {
287 return true;
288 }
289 }
290 }
291 return false;
292 }
293
294 // Iterates over all class resolution records, finds an entry which matches
295 // the given class descriptor and tests its properties.
296 bool HasClass(const std::string& expected_klass,
297 bool expected_resolved,
298 const std::string& expected_access_flags = "") {
David Brazdilca3c8c32016-09-06 14:04:48 +0100299 for (auto& dex_dep : verifier_deps_->dex_deps_) {
300 for (auto& entry : dex_dep.second->classes_) {
301 if (expected_resolved != entry.IsResolved()) {
302 continue;
303 }
304
305 std::string actual_klass = dex_dep.first->StringByTypeIdx(entry.GetDexTypeIndex());
306 if (expected_klass != actual_klass) {
307 continue;
308 }
309
310 if (expected_resolved) {
311 // Test access flags. Note that PrettyJavaAccessFlags always appends
312 // a space after the modifiers. Add it to the expected access flags.
313 std::string actual_access_flags = PrettyJavaAccessFlags(entry.GetAccessFlags());
314 if (expected_access_flags + " " != actual_access_flags) {
315 continue;
316 }
317 }
318
319 return true;
320 }
321 }
322 return false;
323 }
324
325 // Iterates over all field resolution records, finds an entry which matches
326 // the given field class+name+type and tests its properties.
327 bool HasField(const std::string& expected_klass,
328 const std::string& expected_name,
329 const std::string& expected_type,
330 bool expected_resolved,
331 const std::string& expected_access_flags = "",
332 const std::string& expected_decl_klass = "") {
David Brazdilca3c8c32016-09-06 14:04:48 +0100333 for (auto& dex_dep : verifier_deps_->dex_deps_) {
334 for (auto& entry : dex_dep.second->fields_) {
335 if (expected_resolved != entry.IsResolved()) {
336 continue;
337 }
338
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800339 const dex::FieldId& field_id = dex_dep.first->GetFieldId(entry.GetDexFieldIndex());
David Brazdilca3c8c32016-09-06 14:04:48 +0100340
341 std::string actual_klass = dex_dep.first->StringByTypeIdx(field_id.class_idx_);
342 if (expected_klass != actual_klass) {
343 continue;
344 }
345
346 std::string actual_name = dex_dep.first->StringDataByIdx(field_id.name_idx_);
347 if (expected_name != actual_name) {
348 continue;
349 }
350
351 std::string actual_type = dex_dep.first->StringByTypeIdx(field_id.type_idx_);
352 if (expected_type != actual_type) {
353 continue;
354 }
355
356 if (expected_resolved) {
357 // Test access flags. Note that PrettyJavaAccessFlags always appends
358 // a space after the modifiers. Add it to the expected access flags.
359 std::string actual_access_flags = PrettyJavaAccessFlags(entry.GetAccessFlags());
360 if (expected_access_flags + " " != actual_access_flags) {
361 continue;
362 }
363
364 std::string actual_decl_klass = verifier_deps_->GetStringFromId(
365 *dex_dep.first, entry.GetDeclaringClassIndex());
366 if (expected_decl_klass != actual_decl_klass) {
367 continue;
368 }
369 }
370
371 return true;
372 }
373 }
374 return false;
375 }
376
377 // Iterates over all method resolution records, finds an entry which matches
378 // the given field kind+class+name+signature and tests its properties.
Vladimir Markoba118822017-06-12 15:41:56 +0100379 bool HasMethod(const std::string& expected_klass,
David Brazdilca3c8c32016-09-06 14:04:48 +0100380 const std::string& expected_name,
381 const std::string& expected_signature,
Andreas Gampe3db70682018-12-26 15:12:03 -0800382 bool expect_resolved,
David Brazdilca3c8c32016-09-06 14:04:48 +0100383 const std::string& expected_access_flags = "",
384 const std::string& expected_decl_klass = "") {
David Brazdilca3c8c32016-09-06 14:04:48 +0100385 for (auto& dex_dep : verifier_deps_->dex_deps_) {
Vladimir Markoba118822017-06-12 15:41:56 +0100386 for (const VerifierDeps::MethodResolution& entry : dex_dep.second->methods_) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800387 if (expect_resolved != entry.IsResolved()) {
David Brazdilca3c8c32016-09-06 14:04:48 +0100388 continue;
389 }
390
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800391 const dex::MethodId& method_id = dex_dep.first->GetMethodId(entry.GetDexMethodIndex());
David Brazdilca3c8c32016-09-06 14:04:48 +0100392
393 std::string actual_klass = dex_dep.first->StringByTypeIdx(method_id.class_idx_);
394 if (expected_klass != actual_klass) {
395 continue;
396 }
397
398 std::string actual_name = dex_dep.first->StringDataByIdx(method_id.name_idx_);
399 if (expected_name != actual_name) {
400 continue;
401 }
402
403 std::string actual_signature = dex_dep.first->GetMethodSignature(method_id).ToString();
404 if (expected_signature != actual_signature) {
405 continue;
406 }
407
Andreas Gampe3db70682018-12-26 15:12:03 -0800408 if (expect_resolved) {
David Brazdilca3c8c32016-09-06 14:04:48 +0100409 // Test access flags. Note that PrettyJavaAccessFlags always appends
410 // a space after the modifiers. Add it to the expected access flags.
411 std::string actual_access_flags = PrettyJavaAccessFlags(entry.GetAccessFlags());
412 if (expected_access_flags + " " != actual_access_flags) {
413 continue;
414 }
415
416 std::string actual_decl_klass = verifier_deps_->GetStringFromId(
417 *dex_dep.first, entry.GetDeclaringClassIndex());
418 if (expected_decl_klass != actual_decl_klass) {
419 continue;
420 }
421 }
422
423 return true;
424 }
425 }
426 return false;
427 }
428
David Brazdil6f82fbd2016-09-14 11:55:26 +0100429 size_t NumberOfCompiledDexFiles() {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100430 return verifier_deps_->dex_deps_.size();
431 }
432
David Brazdilfeb22822019-02-13 21:25:57 +0000433 bool HasBoolValue(const std::vector<bool>& vec, bool value) {
434 return std::count(vec.begin(), vec.end(), value) > 0;
435 }
436
Andreas Gampe654698d2018-09-20 13:34:35 -0700437 bool HasEachKindOfRecord() {
David Brazdil6f82fbd2016-09-14 11:55:26 +0100438 bool has_strings = false;
439 bool has_assignability = false;
440 bool has_classes = false;
441 bool has_fields = false;
442 bool has_methods = false;
David Brazdilfeb22822019-02-13 21:25:57 +0000443 bool has_verified_classes = false;
Nicolas Geoffray08025182016-10-25 17:20:18 +0100444 bool has_unverified_classes = false;
David Brazdilfeb22822019-02-13 21:25:57 +0000445 bool has_redefined_classes = false;
446 bool has_not_redefined_classes = false;
David Brazdil6f82fbd2016-09-14 11:55:26 +0100447
448 for (auto& entry : verifier_deps_->dex_deps_) {
449 has_strings |= !entry.second->strings_.empty();
450 has_assignability |= !entry.second->assignable_types_.empty();
451 has_assignability |= !entry.second->unassignable_types_.empty();
452 has_classes |= !entry.second->classes_.empty();
453 has_fields |= !entry.second->fields_.empty();
Vladimir Markoba118822017-06-12 15:41:56 +0100454 has_methods |= !entry.second->methods_.empty();
David Brazdilfeb22822019-02-13 21:25:57 +0000455 has_verified_classes |= HasBoolValue(entry.second->verified_classes_, true);
456 has_unverified_classes |= HasBoolValue(entry.second->verified_classes_, false);
457 has_redefined_classes |= HasBoolValue(entry.second->redefined_classes_, true);
458 has_not_redefined_classes |= HasBoolValue(entry.second->redefined_classes_, false);
David Brazdil6f82fbd2016-09-14 11:55:26 +0100459 }
460
Nicolas Geoffray08025182016-10-25 17:20:18 +0100461 return has_strings &&
462 has_assignability &&
463 has_classes &&
464 has_fields &&
465 has_methods &&
David Brazdilfeb22822019-02-13 21:25:57 +0000466 has_verified_classes &&
467 has_unverified_classes &&
468 has_redefined_classes &&
469 has_not_redefined_classes;
David Brazdil6f82fbd2016-09-14 11:55:26 +0100470 }
471
David Brazdil8fd67222019-02-05 18:13:44 +0000472 // Load the dex file again with a new class loader, decode the VerifierDeps
473 // in `buffer`, allow the caller to modify the deps and then run validation.
474 template<typename Fn>
475 bool RunValidation(Fn fn, const std::vector<uint8_t>& buffer, std::string* error_msg) {
476 ScopedObjectAccess soa(Thread::Current());
477
478 jobject second_loader = LoadDex("VerifierDeps");
479 const auto& second_dex_files = GetDexFiles(second_loader);
480
481 VerifierDeps decoded_deps(second_dex_files, ArrayRef<const uint8_t>(buffer));
482 VerifierDeps::DexFileDeps* decoded_dex_deps =
483 decoded_deps.GetDexFileDeps(*second_dex_files.front());
484
485 // Let the test modify the dependencies.
486 fn(*decoded_dex_deps);
487
488 StackHandleScope<1> hs(soa.Self());
489 Handle<mirror::ClassLoader> new_class_loader =
490 hs.NewHandle<mirror::ClassLoader>(soa.Decode<mirror::ClassLoader>(second_loader));
David Brazdilfeb22822019-02-13 21:25:57 +0000491
492 return decoded_deps.ValidateDependencies(soa.Self(),
493 new_class_loader,
494 std::vector<const DexFile*>(),
495 error_msg);
David Brazdil8fd67222019-02-05 18:13:44 +0000496 }
497
David Brazdilca3c8c32016-09-06 14:04:48 +0100498 std::unique_ptr<verifier::VerifierDeps> verifier_deps_;
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100499 std::vector<const DexFile*> dex_files_;
500 const DexFile* primary_dex_file_;
David Brazdilca3c8c32016-09-06 14:04:48 +0100501 jobject class_loader_;
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100502 ObjPtr<mirror::Class> klass_Main_;
David Brazdilca3c8c32016-09-06 14:04:48 +0100503};
504
505TEST_F(VerifierDepsTest, StringToId) {
506 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100507 LoadDexFile(soa);
David Brazdilca3c8c32016-09-06 14:04:48 +0100508
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800509 dex::StringIndex id_Main1 = verifier_deps_->GetIdFromString(*primary_dex_file_, "LMain;");
510 ASSERT_LT(id_Main1.index_, primary_dex_file_->NumStringIds());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100511 ASSERT_EQ("LMain;", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Main1));
David Brazdilca3c8c32016-09-06 14:04:48 +0100512
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800513 dex::StringIndex id_Main2 = verifier_deps_->GetIdFromString(*primary_dex_file_, "LMain;");
514 ASSERT_LT(id_Main2.index_, primary_dex_file_->NumStringIds());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100515 ASSERT_EQ("LMain;", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Main2));
David Brazdilca3c8c32016-09-06 14:04:48 +0100516
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800517 dex::StringIndex id_Lorem1 = verifier_deps_->GetIdFromString(*primary_dex_file_, "Lorem ipsum");
518 ASSERT_GE(id_Lorem1.index_, primary_dex_file_->NumStringIds());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100519 ASSERT_EQ("Lorem ipsum", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Lorem1));
David Brazdilca3c8c32016-09-06 14:04:48 +0100520
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800521 dex::StringIndex id_Lorem2 = verifier_deps_->GetIdFromString(*primary_dex_file_, "Lorem ipsum");
522 ASSERT_GE(id_Lorem2.index_, primary_dex_file_->NumStringIds());
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100523 ASSERT_EQ("Lorem ipsum", verifier_deps_->GetStringFromId(*primary_dex_file_, id_Lorem2));
David Brazdilca3c8c32016-09-06 14:04:48 +0100524
525 ASSERT_EQ(id_Main1, id_Main2);
526 ASSERT_EQ(id_Lorem1, id_Lorem2);
527 ASSERT_NE(id_Main1, id_Lorem1);
528}
529
530TEST_F(VerifierDepsTest, Assignable_BothInBoot) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800531 ASSERT_TRUE(TestAssignabilityRecording(/* dst= */ "Ljava/util/TimeZone;",
532 /* src= */ "Ljava/util/SimpleTimeZone;",
533 /* is_strict= */ true,
534 /* is_assignable= */ true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100535 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
536}
537
538TEST_F(VerifierDepsTest, Assignable_DestinationInBoot1) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800539 ASSERT_TRUE(TestAssignabilityRecording(/* dst= */ "Ljava/net/Socket;",
540 /* src= */ "LMySSLSocket;",
541 /* is_strict= */ true,
542 /* is_assignable= */ true));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000543 ASSERT_TRUE(HasAssignable("Ljava/net/Socket;", "Ljavax/net/ssl/SSLSocket;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100544}
545
546TEST_F(VerifierDepsTest, Assignable_DestinationInBoot2) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800547 ASSERT_TRUE(TestAssignabilityRecording(/* dst= */ "Ljava/util/TimeZone;",
548 /* src= */ "LMySimpleTimeZone;",
549 /* is_strict= */ true,
550 /* is_assignable= */ true));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000551 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100552}
553
554TEST_F(VerifierDepsTest, Assignable_DestinationInBoot3) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800555 ASSERT_TRUE(TestAssignabilityRecording(/* dst= */ "Ljava/util/Collection;",
556 /* src= */ "LMyThreadSet;",
557 /* is_strict= */ true,
558 /* is_assignable= */ true));
Nicolas Geoffray0e2fe0f2016-12-21 16:54:52 +0000559 ASSERT_TRUE(HasAssignable("Ljava/util/Collection;", "Ljava/util/Set;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100560}
561
562TEST_F(VerifierDepsTest, Assignable_BothArrays_Resolved) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800563 ASSERT_TRUE(TestAssignabilityRecording(/* dst= */ "[[Ljava/util/TimeZone;",
564 /* src= */ "[[Ljava/util/SimpleTimeZone;",
565 /* is_strict= */ true,
566 /* is_assignable= */ true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100567 // If the component types of both arrays are resolved, we optimize the list of
568 // dependencies by recording a dependency on the component types.
569 ASSERT_FALSE(HasAssignable("[[Ljava/util/TimeZone;", "[[Ljava/util/SimpleTimeZone;", true));
570 ASSERT_FALSE(HasAssignable("[Ljava/util/TimeZone;", "[Ljava/util/SimpleTimeZone;", true));
571 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
572}
573
David Brazdilca3c8c32016-09-06 14:04:48 +0100574TEST_F(VerifierDepsTest, NotAssignable_BothInBoot) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800575 ASSERT_TRUE(TestAssignabilityRecording(/* dst= */ "Ljava/lang/Exception;",
576 /* src= */ "Ljava/util/SimpleTimeZone;",
577 /* is_strict= */ true,
578 /* is_assignable= */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100579 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
580}
581
582TEST_F(VerifierDepsTest, NotAssignable_DestinationInBoot1) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800583 ASSERT_TRUE(TestAssignabilityRecording(/* dst= */ "Ljava/lang/Exception;",
584 /* src= */ "LMySSLSocket;",
585 /* is_strict= */ true,
586 /* is_assignable= */ false));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000587 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljavax/net/ssl/SSLSocket;", false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100588}
589
590TEST_F(VerifierDepsTest, NotAssignable_DestinationInBoot2) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800591 ASSERT_TRUE(TestAssignabilityRecording(/* dst= */ "Ljava/lang/Exception;",
592 /* src= */ "LMySimpleTimeZone;",
593 /* is_strict= */ true,
594 /* is_assignable= */ false));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000595 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100596}
597
598TEST_F(VerifierDepsTest, NotAssignable_BothArrays) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800599 ASSERT_TRUE(TestAssignabilityRecording(/* dst= */ "[Ljava/lang/Exception;",
600 /* src= */ "[Ljava/util/SimpleTimeZone;",
601 /* is_strict= */ true,
602 /* is_assignable= */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100603 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/util/SimpleTimeZone;", false));
604}
605
606TEST_F(VerifierDepsTest, ArgumentType_ResolvedClass) {
607 ASSERT_TRUE(VerifyMethod("ArgumentType_ResolvedClass"));
608 ASSERT_TRUE(HasClass("Ljava/lang/Thread;", true, "public"));
609}
610
David Brazdilca3c8c32016-09-06 14:04:48 +0100611TEST_F(VerifierDepsTest, ArgumentType_UnresolvedClass) {
612 ASSERT_TRUE(VerifyMethod("ArgumentType_UnresolvedClass"));
613 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
614}
615
616TEST_F(VerifierDepsTest, ArgumentType_UnresolvedSuper) {
617 ASSERT_TRUE(VerifyMethod("ArgumentType_UnresolvedSuper"));
618 ASSERT_TRUE(HasClass("LMySetWithUnresolvedSuper;", false));
619}
620
621TEST_F(VerifierDepsTest, ReturnType_Reference) {
622 ASSERT_TRUE(VerifyMethod("ReturnType_Reference"));
623 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/lang/IllegalStateException;", true));
624}
625
626TEST_F(VerifierDepsTest, ReturnType_Array) {
627 ASSERT_FALSE(VerifyMethod("ReturnType_Array"));
628 ASSERT_TRUE(HasAssignable("Ljava/lang/Integer;", "Ljava/lang/IllegalStateException;", false));
629}
630
631TEST_F(VerifierDepsTest, InvokeArgumentType) {
632 ASSERT_TRUE(VerifyMethod("InvokeArgumentType"));
633 ASSERT_TRUE(HasClass("Ljava/text/SimpleDateFormat;", true, "public"));
634 ASSERT_TRUE(HasClass("Ljava/util/SimpleTimeZone;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100635 ASSERT_TRUE(HasMethod("Ljava/text/SimpleDateFormat;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100636 "setTimeZone",
637 "(Ljava/util/TimeZone;)V",
Andreas Gampe3db70682018-12-26 15:12:03 -0800638 /* expect_resolved= */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100639 "public",
640 "Ljava/text/DateFormat;"));
641 ASSERT_TRUE(HasAssignable("Ljava/util/TimeZone;", "Ljava/util/SimpleTimeZone;", true));
642}
643
644TEST_F(VerifierDepsTest, MergeTypes_RegisterLines) {
645 ASSERT_TRUE(VerifyMethod("MergeTypes_RegisterLines"));
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000646 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100647 ASSERT_TRUE(HasAssignable(
648 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
649}
650
651TEST_F(VerifierDepsTest, MergeTypes_IfInstanceOf) {
652 ASSERT_TRUE(VerifyMethod("MergeTypes_IfInstanceOf"));
653 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
654 ASSERT_TRUE(HasAssignable(
655 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
656 ASSERT_TRUE(HasAssignable("Ljava/net/SocketTimeoutException;", "Ljava/lang/Exception;", false));
657}
658
659TEST_F(VerifierDepsTest, MergeTypes_Unresolved) {
660 ASSERT_TRUE(VerifyMethod("MergeTypes_Unresolved"));
661 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "Ljava/net/SocketTimeoutException;", true));
662 ASSERT_TRUE(HasAssignable(
663 "Ljava/lang/Exception;", "Ljava/util/concurrent/TimeoutException;", true));
664}
665
666TEST_F(VerifierDepsTest, ConstClass_Resolved) {
667 ASSERT_TRUE(VerifyMethod("ConstClass_Resolved"));
668 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
669}
670
671TEST_F(VerifierDepsTest, ConstClass_Unresolved) {
Andreas Gampe629be512017-08-25 17:09:32 -0700672 ASSERT_FALSE(VerifyMethod("ConstClass_Unresolved"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100673 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
674}
675
676TEST_F(VerifierDepsTest, CheckCast_Resolved) {
677 ASSERT_TRUE(VerifyMethod("CheckCast_Resolved"));
678 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
679}
680
681TEST_F(VerifierDepsTest, CheckCast_Unresolved) {
Andreas Gampe629be512017-08-25 17:09:32 -0700682 ASSERT_FALSE(VerifyMethod("CheckCast_Unresolved"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100683 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
684}
685
686TEST_F(VerifierDepsTest, InstanceOf_Resolved) {
687 ASSERT_TRUE(VerifyMethod("InstanceOf_Resolved"));
688 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
689}
690
691TEST_F(VerifierDepsTest, InstanceOf_Unresolved) {
Andreas Gampe629be512017-08-25 17:09:32 -0700692 ASSERT_FALSE(VerifyMethod("InstanceOf_Unresolved"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100693 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
694}
695
696TEST_F(VerifierDepsTest, NewInstance_Resolved) {
697 ASSERT_TRUE(VerifyMethod("NewInstance_Resolved"));
698 ASSERT_TRUE(HasClass("Ljava/lang/IllegalStateException;", true, "public"));
699}
700
701TEST_F(VerifierDepsTest, NewInstance_Unresolved) {
Andreas Gampe629be512017-08-25 17:09:32 -0700702 ASSERT_FALSE(VerifyMethod("NewInstance_Unresolved"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100703 ASSERT_TRUE(HasClass("LUnresolvedClass;", false));
704}
705
David Brazdilca3c8c32016-09-06 14:04:48 +0100706TEST_F(VerifierDepsTest, NewArray_Unresolved) {
Andreas Gampe629be512017-08-25 17:09:32 -0700707 ASSERT_FALSE(VerifyMethod("NewArray_Unresolved"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100708 ASSERT_TRUE(HasClass("[LUnresolvedClass;", false));
709}
710
711TEST_F(VerifierDepsTest, Throw) {
712 ASSERT_TRUE(VerifyMethod("Throw"));
713 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/lang/IllegalStateException;", true));
714}
715
716TEST_F(VerifierDepsTest, MoveException_Resolved) {
717 ASSERT_TRUE(VerifyMethod("MoveException_Resolved"));
718 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
719 ASSERT_TRUE(HasClass("Ljava/net/SocketTimeoutException;", true, "public"));
720 ASSERT_TRUE(HasClass("Ljava/util/zip/ZipException;", true, "public"));
721
722 // Testing that all exception types are assignable to Throwable.
723 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/io/InterruptedIOException;", true));
724 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
725 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/util/zip/ZipException;", true));
726
727 // Testing that the merge type is assignable to Throwable.
728 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/io/IOException;", true));
729
730 // Merging of exception types.
731 ASSERT_TRUE(HasAssignable("Ljava/io/IOException;", "Ljava/io/InterruptedIOException;", true));
732 ASSERT_TRUE(HasAssignable("Ljava/io/IOException;", "Ljava/util/zip/ZipException;", true));
733 ASSERT_TRUE(HasAssignable(
734 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
735}
736
737TEST_F(VerifierDepsTest, MoveException_Unresolved) {
738 ASSERT_FALSE(VerifyMethod("MoveException_Unresolved"));
739 ASSERT_TRUE(HasClass("LUnresolvedException;", false));
740}
741
742TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInReferenced) {
743 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInReferenced"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000744 ASSERT_TRUE(HasClass("Ljava/lang/System;", true, "public"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100745 ASSERT_TRUE(HasField("Ljava/lang/System;",
746 "out",
747 "Ljava/io/PrintStream;",
748 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000749 "public static",
David Brazdilca3c8c32016-09-06 14:04:48 +0100750 "Ljava/lang/System;"));
751}
752
753TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInSuperclass1) {
754 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInSuperclass1"));
755 ASSERT_TRUE(HasClass("Ljava/util/SimpleTimeZone;", true, "public"));
756 ASSERT_TRUE(HasField(
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000757 "Ljava/util/SimpleTimeZone;", "LONG", "I", true, "public static", "Ljava/util/TimeZone;"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100758}
759
760TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInSuperclass2) {
761 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInSuperclass2"));
762 ASSERT_TRUE(HasField(
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000763 "LMySimpleTimeZone;", "SHORT", "I", true, "public static", "Ljava/util/TimeZone;"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100764}
765
766TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface1) {
767 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface1"));
768 ASSERT_TRUE(HasClass("Ljavax/xml/transform/dom/DOMResult;", true, "public"));
769 ASSERT_TRUE(HasField("Ljavax/xml/transform/dom/DOMResult;",
770 "PI_ENABLE_OUTPUT_ESCAPING",
771 "Ljava/lang/String;",
772 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000773 "public static",
David Brazdilca3c8c32016-09-06 14:04:48 +0100774 "Ljavax/xml/transform/Result;"));
775}
776
777TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface2) {
778 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface2"));
779 ASSERT_TRUE(HasField("LMyDOMResult;",
780 "PI_ENABLE_OUTPUT_ESCAPING",
781 "Ljava/lang/String;",
782 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000783 "public static",
David Brazdilca3c8c32016-09-06 14:04:48 +0100784 "Ljavax/xml/transform/Result;"));
785}
786
787TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface3) {
788 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface3"));
789 ASSERT_TRUE(HasField("LMyResult;",
790 "PI_ENABLE_OUTPUT_ESCAPING",
791 "Ljava/lang/String;",
792 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000793 "public static",
David Brazdilca3c8c32016-09-06 14:04:48 +0100794 "Ljavax/xml/transform/Result;"));
795}
796
797TEST_F(VerifierDepsTest, StaticField_Resolved_DeclaredInInterface4) {
798 ASSERT_TRUE(VerifyMethod("StaticField_Resolved_DeclaredInInterface4"));
799 ASSERT_TRUE(HasField("LMyDocument;",
800 "ELEMENT_NODE",
801 "S",
802 true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000803 "public static",
David Brazdilca3c8c32016-09-06 14:04:48 +0100804 "Lorg/w3c/dom/Node;"));
805}
806
807TEST_F(VerifierDepsTest, StaticField_Unresolved_ReferrerInBoot) {
808 ASSERT_TRUE(VerifyMethod("StaticField_Unresolved_ReferrerInBoot"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000809 ASSERT_TRUE(HasClass("Ljava/util/TimeZone;", true, "public"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100810 ASSERT_TRUE(HasField("Ljava/util/TimeZone;", "x", "I", false));
811}
812
813TEST_F(VerifierDepsTest, StaticField_Unresolved_ReferrerInDex) {
814 ASSERT_TRUE(VerifyMethod("StaticField_Unresolved_ReferrerInDex"));
815 ASSERT_TRUE(HasField("LMyThreadSet;", "x", "I", false));
816}
817
818TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInReferenced) {
819 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInReferenced"));
820 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
821 ASSERT_TRUE(HasField("Ljava/io/InterruptedIOException;",
822 "bytesTransferred",
823 "I",
824 true,
825 "public",
826 "Ljava/io/InterruptedIOException;"));
827 ASSERT_TRUE(HasAssignable(
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000828 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100829}
830
831TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInSuperclass1) {
832 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInSuperclass1"));
833 ASSERT_TRUE(HasClass("Ljava/net/SocketTimeoutException;", true, "public"));
834 ASSERT_TRUE(HasField("Ljava/net/SocketTimeoutException;",
835 "bytesTransferred",
836 "I",
837 true,
838 "public",
839 "Ljava/io/InterruptedIOException;"));
840 ASSERT_TRUE(HasAssignable(
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000841 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100842}
843
844TEST_F(VerifierDepsTest, InstanceField_Resolved_DeclaredInSuperclass2) {
845 ASSERT_TRUE(VerifyMethod("InstanceField_Resolved_DeclaredInSuperclass2"));
846 ASSERT_TRUE(HasField("LMySocketTimeoutException;",
847 "bytesTransferred",
848 "I",
849 true,
850 "public",
851 "Ljava/io/InterruptedIOException;"));
852 ASSERT_TRUE(HasAssignable(
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000853 "Ljava/io/InterruptedIOException;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100854}
855
856TEST_F(VerifierDepsTest, InstanceField_Unresolved_ReferrerInBoot) {
857 ASSERT_TRUE(VerifyMethod("InstanceField_Unresolved_ReferrerInBoot"));
858 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
859 ASSERT_TRUE(HasField("Ljava/io/InterruptedIOException;", "x", "I", false));
860}
861
862TEST_F(VerifierDepsTest, InstanceField_Unresolved_ReferrerInDex) {
863 ASSERT_TRUE(VerifyMethod("InstanceField_Unresolved_ReferrerInDex"));
864 ASSERT_TRUE(HasField("LMyThreadSet;", "x", "I", false));
865}
866
867TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInReferenced) {
868 ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInReferenced"));
869 ASSERT_TRUE(HasClass("Ljava/net/Socket;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100870 ASSERT_TRUE(HasMethod("Ljava/net/Socket;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100871 "setSocketImplFactory",
872 "(Ljava/net/SocketImplFactory;)V",
Andreas Gampe3db70682018-12-26 15:12:03 -0800873 /* expect_resolved= */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100874 "public static",
875 "Ljava/net/Socket;"));
876}
877
878TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInSuperclass1) {
879 ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInSuperclass1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000880 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100881 ASSERT_TRUE(HasMethod("Ljavax/net/ssl/SSLSocket;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100882 "setSocketImplFactory",
883 "(Ljava/net/SocketImplFactory;)V",
Andreas Gampe3db70682018-12-26 15:12:03 -0800884 /* expect_resolved= */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100885 "public static",
886 "Ljava/net/Socket;"));
887}
888
889TEST_F(VerifierDepsTest, InvokeStatic_Resolved_DeclaredInSuperclass2) {
890 ASSERT_TRUE(VerifyMethod("InvokeStatic_Resolved_DeclaredInSuperclass2"));
Vladimir Markoba118822017-06-12 15:41:56 +0100891 ASSERT_TRUE(HasMethod("LMySSLSocket;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100892 "setSocketImplFactory",
893 "(Ljava/net/SocketImplFactory;)V",
Andreas Gampe3db70682018-12-26 15:12:03 -0800894 /* expect_resolved= */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100895 "public static",
896 "Ljava/net/Socket;"));
897}
898
899TEST_F(VerifierDepsTest, InvokeStatic_DeclaredInInterface1) {
900 ASSERT_TRUE(VerifyMethod("InvokeStatic_DeclaredInInterface1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000901 ASSERT_TRUE(HasClass("Ljava/util/Map$Entry;", true, "public interface"));
Vladimir Markoba118822017-06-12 15:41:56 +0100902 ASSERT_TRUE(HasMethod("Ljava/util/Map$Entry;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100903 "comparingByKey",
904 "()Ljava/util/Comparator;",
Andreas Gampe3db70682018-12-26 15:12:03 -0800905 /* expect_resolved= */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100906 "public static",
907 "Ljava/util/Map$Entry;"));
908}
909
910TEST_F(VerifierDepsTest, InvokeStatic_DeclaredInInterface2) {
911 ASSERT_FALSE(VerifyMethod("InvokeStatic_DeclaredInInterface2"));
912 ASSERT_TRUE(HasClass("Ljava/util/AbstractMap$SimpleEntry;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100913 ASSERT_TRUE(HasMethod("Ljava/util/AbstractMap$SimpleEntry;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100914 "comparingByKey",
915 "()Ljava/util/Comparator;",
Andreas Gampe3db70682018-12-26 15:12:03 -0800916 /* expect_resolved= */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100917}
918
919TEST_F(VerifierDepsTest, InvokeStatic_Unresolved1) {
920 ASSERT_FALSE(VerifyMethod("InvokeStatic_Unresolved1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000921 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100922 ASSERT_TRUE(HasMethod("Ljavax/net/ssl/SSLSocket;",
923 "x",
924 "()V",
Andreas Gampe3db70682018-12-26 15:12:03 -0800925 /* expect_resolved= */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100926}
927
928TEST_F(VerifierDepsTest, InvokeStatic_Unresolved2) {
929 ASSERT_FALSE(VerifyMethod("InvokeStatic_Unresolved2"));
Vladimir Markoba118822017-06-12 15:41:56 +0100930 ASSERT_TRUE(HasMethod("LMySSLSocket;",
931 "x",
932 "()V",
Andreas Gampe3db70682018-12-26 15:12:03 -0800933 /* expect_resolved= */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100934}
935
936TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInReferenced) {
937 ASSERT_TRUE(VerifyMethod("InvokeDirect_Resolved_DeclaredInReferenced"));
938 ASSERT_TRUE(HasClass("Ljava/net/Socket;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100939 ASSERT_TRUE(HasMethod("Ljava/net/Socket;",
940 "<init>",
941 "()V",
Andreas Gampe3db70682018-12-26 15:12:03 -0800942 /* expect_resolved= */ true,
Vladimir Markoba118822017-06-12 15:41:56 +0100943 "public",
944 "Ljava/net/Socket;"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100945}
946
947TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInSuperclass1) {
948 ASSERT_FALSE(VerifyMethod("InvokeDirect_Resolved_DeclaredInSuperclass1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000949 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100950 ASSERT_TRUE(HasMethod("Ljavax/net/ssl/SSLSocket;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100951 "checkOldImpl",
952 "()V",
Andreas Gampe3db70682018-12-26 15:12:03 -0800953 /* expect_resolved= */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100954 "private",
955 "Ljava/net/Socket;"));
956}
957
958TEST_F(VerifierDepsTest, InvokeDirect_Resolved_DeclaredInSuperclass2) {
959 ASSERT_FALSE(VerifyMethod("InvokeDirect_Resolved_DeclaredInSuperclass2"));
Vladimir Markoba118822017-06-12 15:41:56 +0100960 ASSERT_TRUE(HasMethod("LMySSLSocket;",
961 "checkOldImpl",
962 "()V",
Andreas Gampe3db70682018-12-26 15:12:03 -0800963 /* expect_resolved= */ true,
Vladimir Markoba118822017-06-12 15:41:56 +0100964 "private",
965 "Ljava/net/Socket;"));
David Brazdilca3c8c32016-09-06 14:04:48 +0100966}
967
968TEST_F(VerifierDepsTest, InvokeDirect_Unresolved1) {
969 ASSERT_FALSE(VerifyMethod("InvokeDirect_Unresolved1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +0000970 ASSERT_TRUE(HasClass("Ljavax/net/ssl/SSLSocket;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100971 ASSERT_TRUE(HasMethod("Ljavax/net/ssl/SSLSocket;",
972 "x",
973 "()V",
Andreas Gampe3db70682018-12-26 15:12:03 -0800974 /* expect_resolved= */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100975}
976
977TEST_F(VerifierDepsTest, InvokeDirect_Unresolved2) {
978 ASSERT_FALSE(VerifyMethod("InvokeDirect_Unresolved2"));
Vladimir Markoba118822017-06-12 15:41:56 +0100979 ASSERT_TRUE(HasMethod("LMySSLSocket;",
980 "x",
981 "()V",
Andreas Gampe3db70682018-12-26 15:12:03 -0800982 /* expect_resolved= */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +0100983}
984
985TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInReferenced) {
986 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInReferenced"));
987 ASSERT_TRUE(HasClass("Ljava/lang/Throwable;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +0100988 ASSERT_TRUE(HasMethod("Ljava/lang/Throwable;",
David Brazdilca3c8c32016-09-06 14:04:48 +0100989 "getMessage",
990 "()Ljava/lang/String;",
Andreas Gampe3db70682018-12-26 15:12:03 -0800991 /* expect_resolved= */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +0100992 "public",
993 "Ljava/lang/Throwable;"));
994 // Type dependency on `this` argument.
Nicolas Geoffray119e8462016-12-21 10:29:43 +0000995 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +0100996}
997
998TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperclass1) {
999 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperclass1"));
1000 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +01001001 ASSERT_TRUE(HasMethod("Ljava/io/InterruptedIOException;",
David Brazdilca3c8c32016-09-06 14:04:48 +01001002 "getMessage",
1003 "()Ljava/lang/String;",
Andreas Gampe3db70682018-12-26 15:12:03 -08001004 /* expect_resolved= */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +01001005 "public",
1006 "Ljava/lang/Throwable;"));
1007 // Type dependency on `this` argument.
Nicolas Geoffray119e8462016-12-21 10:29:43 +00001008 ASSERT_TRUE(HasAssignable("Ljava/lang/Throwable;", "Ljava/net/SocketTimeoutException;", true));
David Brazdilca3c8c32016-09-06 14:04:48 +01001009}
1010
1011TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperclass2) {
1012 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperclass2"));
Vladimir Markoba118822017-06-12 15:41:56 +01001013 ASSERT_TRUE(HasMethod("LMySocketTimeoutException;",
David Brazdilca3c8c32016-09-06 14:04:48 +01001014 "getMessage",
1015 "()Ljava/lang/String;",
Andreas Gampe3db70682018-12-26 15:12:03 -08001016 /* expect_resolved= */ true,
David Brazdilca3c8c32016-09-06 14:04:48 +01001017 "public",
1018 "Ljava/lang/Throwable;"));
1019}
1020
1021TEST_F(VerifierDepsTest, InvokeVirtual_Resolved_DeclaredInSuperinterface) {
1022 ASSERT_TRUE(VerifyMethod("InvokeVirtual_Resolved_DeclaredInSuperinterface"));
Vladimir Markoba118822017-06-12 15:41:56 +01001023 ASSERT_TRUE(HasMethod("LMyThreadSet;",
David Brazdilca3c8c32016-09-06 14:04:48 +01001024 "size",
1025 "()I",
Andreas Gampe3db70682018-12-26 15:12:03 -08001026 /* expect_resolved= */ true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001027 "public",
David Brazdilca3c8c32016-09-06 14:04:48 +01001028 "Ljava/util/Set;"));
1029}
1030
1031TEST_F(VerifierDepsTest, InvokeVirtual_Unresolved1) {
1032 ASSERT_FALSE(VerifyMethod("InvokeVirtual_Unresolved1"));
1033 ASSERT_TRUE(HasClass("Ljava/io/InterruptedIOException;", true, "public"));
Vladimir Markoba118822017-06-12 15:41:56 +01001034 ASSERT_TRUE(HasMethod("Ljava/io/InterruptedIOException;",
1035 "x",
1036 "()V",
Andreas Gampe3db70682018-12-26 15:12:03 -08001037 /* expect_resolved= */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +01001038}
1039
1040TEST_F(VerifierDepsTest, InvokeVirtual_Unresolved2) {
1041 ASSERT_FALSE(VerifyMethod("InvokeVirtual_Unresolved2"));
Vladimir Markoba118822017-06-12 15:41:56 +01001042 ASSERT_TRUE(HasMethod("LMySocketTimeoutException;",
1043 "x",
1044 "()V",
Andreas Gampe3db70682018-12-26 15:12:03 -08001045 /* expect_resolved= */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +01001046}
1047
1048TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInReferenced) {
1049 ASSERT_TRUE(VerifyMethod("InvokeInterface_Resolved_DeclaredInReferenced"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001050 ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public interface"));
Vladimir Markoba118822017-06-12 15:41:56 +01001051 ASSERT_TRUE(HasMethod("Ljava/lang/Runnable;",
David Brazdilca3c8c32016-09-06 14:04:48 +01001052 "run",
1053 "()V",
Andreas Gampe3db70682018-12-26 15:12:03 -08001054 /* expect_resolved= */ true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001055 "public",
David Brazdilca3c8c32016-09-06 14:04:48 +01001056 "Ljava/lang/Runnable;"));
1057}
1058
1059TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperclass) {
1060 ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperclass"));
Vladimir Markoba118822017-06-12 15:41:56 +01001061 // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type.
1062 ASSERT_TRUE(HasMethod("LMyThread;",
1063 "join",
1064 "()V",
Andreas Gampe3db70682018-12-26 15:12:03 -08001065 /* expect_resolved= */ true,
Vladimir Markoba118822017-06-12 15:41:56 +01001066 "public",
1067 "Ljava/lang/Thread;"));
David Brazdilca3c8c32016-09-06 14:04:48 +01001068}
1069
1070TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperinterface1) {
1071 ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperinterface1"));
Vladimir Markoba118822017-06-12 15:41:56 +01001072 // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type.
1073 ASSERT_TRUE(HasMethod("LMyThreadSet;",
David Brazdilca3c8c32016-09-06 14:04:48 +01001074 "run",
1075 "()V",
Andreas Gampe3db70682018-12-26 15:12:03 -08001076 /* expect_resolved= */ true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001077 "public",
Vladimir Markoba118822017-06-12 15:41:56 +01001078 "Ljava/lang/Thread;"));
David Brazdilca3c8c32016-09-06 14:04:48 +01001079}
1080
1081TEST_F(VerifierDepsTest, InvokeInterface_Resolved_DeclaredInSuperinterface2) {
1082 ASSERT_FALSE(VerifyMethod("InvokeInterface_Resolved_DeclaredInSuperinterface2"));
Vladimir Markoba118822017-06-12 15:41:56 +01001083 ASSERT_TRUE(HasMethod("LMyThreadSet;",
David Brazdilca3c8c32016-09-06 14:04:48 +01001084 "isEmpty",
1085 "()Z",
Andreas Gampe3db70682018-12-26 15:12:03 -08001086 /* expect_resolved= */ true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001087 "public",
David Brazdilca3c8c32016-09-06 14:04:48 +01001088 "Ljava/util/Set;"));
1089}
1090
1091TEST_F(VerifierDepsTest, InvokeInterface_Unresolved1) {
1092 ASSERT_FALSE(VerifyMethod("InvokeInterface_Unresolved1"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001093 ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public interface"));
Vladimir Markoba118822017-06-12 15:41:56 +01001094 ASSERT_TRUE(HasMethod("Ljava/lang/Runnable;",
1095 "x",
1096 "()V",
Andreas Gampe3db70682018-12-26 15:12:03 -08001097 /* expect_resolved= */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +01001098}
1099
1100TEST_F(VerifierDepsTest, InvokeInterface_Unresolved2) {
1101 ASSERT_FALSE(VerifyMethod("InvokeInterface_Unresolved2"));
Andreas Gampe3db70682018-12-26 15:12:03 -08001102 ASSERT_TRUE(HasMethod("LMyThreadSet;", "x", "()V", /* expect_resolved= */ false));
David Brazdilca3c8c32016-09-06 14:04:48 +01001103}
1104
1105TEST_F(VerifierDepsTest, InvokeSuper_ThisAssignable) {
1106 ASSERT_TRUE(VerifyMethod("InvokeSuper_ThisAssignable"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001107 ASSERT_TRUE(HasClass("Ljava/lang/Runnable;", true, "public interface"));
Nicolas Geoffray0e2fe0f2016-12-21 16:54:52 +00001108 ASSERT_TRUE(HasAssignable("Ljava/lang/Runnable;", "Ljava/lang/Thread;", true));
Vladimir Markoba118822017-06-12 15:41:56 +01001109 ASSERT_TRUE(HasMethod("Ljava/lang/Runnable;",
David Brazdilca3c8c32016-09-06 14:04:48 +01001110 "run",
1111 "()V",
Andreas Gampe3db70682018-12-26 15:12:03 -08001112 /* expect_resolved= */ true,
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001113 "public",
David Brazdilca3c8c32016-09-06 14:04:48 +01001114 "Ljava/lang/Runnable;"));
1115}
1116
1117TEST_F(VerifierDepsTest, InvokeSuper_ThisNotAssignable) {
1118 ASSERT_FALSE(VerifyMethod("InvokeSuper_ThisNotAssignable"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001119 ASSERT_TRUE(HasClass("Ljava/lang/Integer;", true, "public"));
Nicolas Geoffray119e8462016-12-21 10:29:43 +00001120 ASSERT_TRUE(HasAssignable("Ljava/lang/Integer;", "Ljava/lang/Thread;", false));
Vladimir Markoba118822017-06-12 15:41:56 +01001121 ASSERT_TRUE(HasMethod("Ljava/lang/Integer;",
1122 "intValue", "()I",
Andreas Gampe3db70682018-12-26 15:12:03 -08001123 /* expect_resolved= */ true,
Vladimir Markoba118822017-06-12 15:41:56 +01001124 "public", "Ljava/lang/Integer;"));
David Brazdilca3c8c32016-09-06 14:04:48 +01001125}
1126
Nicolas Geoffray0f1cb172017-01-05 15:23:19 +00001127TEST_F(VerifierDepsTest, ArgumentType_ResolvedReferenceArray) {
1128 ASSERT_TRUE(VerifyMethod("ArgumentType_ResolvedReferenceArray"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001129 ASSERT_TRUE(HasClass("[Ljava/lang/Thread;", true, "public"));
Nicolas Geoffray0f1cb172017-01-05 15:23:19 +00001130}
1131
1132TEST_F(VerifierDepsTest, NewArray_Resolved) {
1133 ASSERT_TRUE(VerifyMethod("NewArray_Resolved"));
Nicolas Geoffray6e54f782017-03-08 15:27:09 +00001134 ASSERT_TRUE(HasClass("[Ljava/lang/IllegalStateException;", true, "public"));
Nicolas Geoffray0f1cb172017-01-05 15:23:19 +00001135}
1136
David Brazdil6f82fbd2016-09-14 11:55:26 +01001137TEST_F(VerifierDepsTest, EncodeDecode) {
1138 VerifyDexFile();
1139
1140 ASSERT_EQ(1u, NumberOfCompiledDexFiles());
1141 ASSERT_TRUE(HasEachKindOfRecord());
1142
1143 std::vector<uint8_t> buffer;
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +01001144 verifier_deps_->Encode(dex_files_, &buffer);
David Brazdil6f82fbd2016-09-14 11:55:26 +01001145 ASSERT_FALSE(buffer.empty());
1146
Nicolas Geoffraye70dd562016-10-30 21:03:35 +00001147 VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
David Brazdil6f82fbd2016-09-14 11:55:26 +01001148 ASSERT_TRUE(verifier_deps_->Equals(decoded_deps));
1149}
1150
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +01001151TEST_F(VerifierDepsTest, EncodeDecodeMulti) {
1152 VerifyDexFile("MultiDex");
1153
1154 ASSERT_GT(NumberOfCompiledDexFiles(), 1u);
1155 std::vector<uint8_t> buffer;
1156 verifier_deps_->Encode(dex_files_, &buffer);
1157 ASSERT_FALSE(buffer.empty());
1158
1159 // Create new DexFile, to mess with std::map order: the verifier deps used
1160 // to iterate over the map, which doesn't guarantee insertion order. We fixed
1161 // this by passing the expected order when encoding/decoding.
1162 std::vector<std::unique_ptr<const DexFile>> first_dex_files = OpenTestDexFiles("VerifierDeps");
1163 std::vector<std::unique_ptr<const DexFile>> second_dex_files = OpenTestDexFiles("MultiDex");
1164 std::vector<const DexFile*> dex_files;
1165 for (auto& dex_file : first_dex_files) {
1166 dex_files.push_back(dex_file.get());
1167 }
1168 for (auto& dex_file : second_dex_files) {
1169 dex_files.push_back(dex_file.get());
1170 }
1171
1172 // Dump the new verifier deps to ensure it can properly read the data.
Nicolas Geoffraye70dd562016-10-30 21:03:35 +00001173 VerifierDeps decoded_deps(dex_files, ArrayRef<const uint8_t>(buffer));
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +01001174 std::ostringstream stream;
1175 VariableIndentationOutputStream os(&stream);
1176 decoded_deps.Dump(&os);
1177}
1178
Nicolas Geoffray08025182016-10-25 17:20:18 +01001179TEST_F(VerifierDepsTest, UnverifiedClasses) {
1180 VerifyDexFile();
1181 ASSERT_FALSE(HasUnverifiedClass("LMyThread;"));
1182 // Test that a class with a soft failure is recorded.
1183 ASSERT_TRUE(HasUnverifiedClass("LMain;"));
1184 // Test that a class with hard failure is recorded.
1185 ASSERT_TRUE(HasUnverifiedClass("LMyVerificationFailure;"));
1186 // Test that a class with unresolved super is recorded.
Nicolas Geoffray7cc3ae52017-03-07 14:33:37 +00001187 ASSERT_TRUE(HasUnverifiedClass("LMyClassWithNoSuper;"));
Nicolas Geoffray08025182016-10-25 17:20:18 +01001188 // Test that a class with unresolved super and hard failure is recorded.
1189 ASSERT_TRUE(HasUnverifiedClass("LMyClassWithNoSuperButFailures;"));
1190}
1191
David Brazdilfeb22822019-02-13 21:25:57 +00001192TEST_F(VerifierDepsTest, RedefinedClass) {
1193 VerifyDexFile();
1194 // Test that a class which redefines a boot classpath class has dependencies recorded.
1195 ASSERT_TRUE(HasRedefinedClass("Ljava/net/SocketTimeoutException;"));
1196 // These come from test case InstanceField_Resolved_DeclaredInSuperclass1.
1197 ASSERT_TRUE(HasClass("Ljava/net/SocketTimeoutException;", true, "public"));
1198 ASSERT_TRUE(HasField("Ljava/net/SocketTimeoutException;",
1199 "bytesTransferred",
1200 "I",
1201 true,
1202 "public",
1203 "Ljava/io/InterruptedIOException;"));
1204}
1205
Mathieu Chartierbf755fe2017-08-01 13:42:56 -07001206TEST_F(VerifierDepsTest, UnverifiedOrder) {
1207 ScopedObjectAccess soa(Thread::Current());
1208 jobject loader = LoadDex("VerifierDeps");
1209 std::vector<const DexFile*> dex_files = GetDexFiles(loader);
1210 ASSERT_GT(dex_files.size(), 0u);
1211 const DexFile* dex_file = dex_files[0];
1212 VerifierDeps deps1(dex_files);
1213 Thread* const self = Thread::Current();
1214 ASSERT_TRUE(self->GetVerifierDeps() == nullptr);
1215 self->SetVerifierDeps(&deps1);
1216 deps1.MaybeRecordVerificationStatus(*dex_file,
David Brazdilfeb22822019-02-13 21:25:57 +00001217 dex_file->GetClassDef(0u),
Mathieu Chartierbf755fe2017-08-01 13:42:56 -07001218 verifier::FailureKind::kHardFailure);
1219 deps1.MaybeRecordVerificationStatus(*dex_file,
David Brazdilfeb22822019-02-13 21:25:57 +00001220 dex_file->GetClassDef(1u),
Mathieu Chartierbf755fe2017-08-01 13:42:56 -07001221 verifier::FailureKind::kHardFailure);
1222 VerifierDeps deps2(dex_files);
1223 self->SetVerifierDeps(nullptr);
1224 self->SetVerifierDeps(&deps2);
1225 deps2.MaybeRecordVerificationStatus(*dex_file,
David Brazdilfeb22822019-02-13 21:25:57 +00001226 dex_file->GetClassDef(1u),
Mathieu Chartierbf755fe2017-08-01 13:42:56 -07001227 verifier::FailureKind::kHardFailure);
1228 deps2.MaybeRecordVerificationStatus(*dex_file,
David Brazdilfeb22822019-02-13 21:25:57 +00001229 dex_file->GetClassDef(0u),
Mathieu Chartierbf755fe2017-08-01 13:42:56 -07001230 verifier::FailureKind::kHardFailure);
1231 self->SetVerifierDeps(nullptr);
1232 std::vector<uint8_t> buffer1;
1233 deps1.Encode(dex_files, &buffer1);
1234 std::vector<uint8_t> buffer2;
1235 deps2.Encode(dex_files, &buffer2);
1236 EXPECT_EQ(buffer1, buffer2);
1237}
1238
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001239TEST_F(VerifierDepsTest, VerifyDeps) {
David Brazdil8fd67222019-02-05 18:13:44 +00001240 std::string error_msg;
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001241
David Brazdil8fd67222019-02-05 18:13:44 +00001242 VerifyDexFile();
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001243 ASSERT_EQ(1u, NumberOfCompiledDexFiles());
1244 ASSERT_TRUE(HasEachKindOfRecord());
1245
1246 // When validating, we create a new class loader, as
1247 // the existing `class_loader_` may contain erroneous classes,
1248 // that ClassLinker::FindClass won't return.
1249
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001250 std::vector<uint8_t> buffer;
1251 verifier_deps_->Encode(dex_files_, &buffer);
1252 ASSERT_FALSE(buffer.empty());
1253
David Brazdil8fd67222019-02-05 18:13:44 +00001254 // Check that dependencies are satisfied after decoding `buffer`.
1255 ASSERT_TRUE(RunValidation([](VerifierDeps::DexFileDeps&) {}, buffer, &error_msg))
1256 << error_msg;
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001257
David Brazdil8fd67222019-02-05 18:13:44 +00001258 // Mess with the dependencies to make sure we catch any change and fail to verify.
1259 ASSERT_FALSE(RunValidation([](VerifierDeps::DexFileDeps& deps) {
1260 deps.assignable_types_.insert(*deps.unassignable_types_.begin());
1261 }, buffer, &error_msg));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001262
David Brazdil8fd67222019-02-05 18:13:44 +00001263 // Mess with the unassignable_types.
1264 ASSERT_FALSE(RunValidation([](VerifierDeps::DexFileDeps& deps) {
1265 deps.unassignable_types_.insert(*deps.assignable_types_.begin());
1266 }, buffer, &error_msg));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001267
David Brazdil8fd67222019-02-05 18:13:44 +00001268 // Mess with classes.
1269 ASSERT_FALSE(RunValidation([](VerifierDeps::DexFileDeps& deps) {
1270 for (const auto& entry : deps.classes_) {
1271 if (entry.IsResolved()) {
1272 deps.classes_.insert(VerifierDeps::ClassResolution(
1273 entry.GetDexTypeIndex(), VerifierDeps::kUnresolvedMarker));
1274 return;
1275 }
1276 }
1277 LOG(FATAL) << "Could not find any resolved classes";
1278 UNREACHABLE();
1279 }, buffer, &error_msg));
1280 ASSERT_FALSE(RunValidation([](VerifierDeps::DexFileDeps& deps) {
1281 for (const auto& entry : deps.classes_) {
1282 if (!entry.IsResolved()) {
1283 deps.classes_.insert(VerifierDeps::ClassResolution(
1284 entry.GetDexTypeIndex(), VerifierDeps::kUnresolvedMarker - 1));
1285 return;
1286 }
1287 }
1288 LOG(FATAL) << "Could not find any unresolved classes";
1289 UNREACHABLE();
1290 }, buffer, &error_msg));
1291 ASSERT_FALSE(RunValidation([](VerifierDeps::DexFileDeps& deps) {
1292 for (const auto& entry : deps.classes_) {
1293 if (entry.IsResolved()) {
1294 deps.classes_.insert(VerifierDeps::ClassResolution(
1295 entry.GetDexTypeIndex(), entry.GetAccessFlags() - 1));
1296 return;
1297 }
1298 }
1299 LOG(FATAL) << "Could not find any resolved classes";
1300 UNREACHABLE();
1301 }, buffer, &error_msg));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001302
David Brazdil8fd67222019-02-05 18:13:44 +00001303 // Mess with fields.
1304 ASSERT_FALSE(RunValidation([](VerifierDeps::DexFileDeps& deps) {
1305 for (const auto& entry : deps.fields_) {
1306 if (entry.IsResolved()) {
1307 deps.fields_.insert(VerifierDeps::FieldResolution(entry.GetDexFieldIndex(),
1308 VerifierDeps::kUnresolvedMarker,
1309 entry.GetDeclaringClassIndex()));
1310 return;
1311 }
1312 }
1313 LOG(FATAL) << "Could not find any resolved fields";
1314 UNREACHABLE();
1315 }, buffer, &error_msg));
1316 ASSERT_FALSE(RunValidation([](VerifierDeps::DexFileDeps& deps) {
1317 for (const auto& entry : deps.fields_) {
1318 if (!entry.IsResolved()) {
1319 constexpr dex::StringIndex kStringIndexZero(0); // We know there is a class there.
1320 deps.fields_.insert(VerifierDeps::FieldResolution(0 /* we know there is a field there */,
1321 VerifierDeps::kUnresolvedMarker - 1,
1322 kStringIndexZero));
1323 return;
1324 }
1325 }
1326 LOG(FATAL) << "Could not find any unresolved fields";
1327 UNREACHABLE();
1328 }, buffer, &error_msg));
1329 ASSERT_FALSE(RunValidation([](VerifierDeps::DexFileDeps& deps) {
1330 for (const auto& entry : deps.fields_) {
1331 if (entry.IsResolved()) {
1332 deps.fields_.insert(VerifierDeps::FieldResolution(entry.GetDexFieldIndex(),
1333 entry.GetAccessFlags() - 1,
1334 entry.GetDeclaringClassIndex()));
1335 return;
1336 }
1337 }
1338 LOG(FATAL) << "Could not find any resolved fields";
1339 UNREACHABLE();
1340 }, buffer, &error_msg));
1341 ASSERT_FALSE(RunValidation([](VerifierDeps::DexFileDeps& deps) {
1342 for (const auto& entry : deps.fields_) {
1343 constexpr dex::StringIndex kNewTypeIndex(0);
1344 if (entry.GetDeclaringClassIndex() != kNewTypeIndex) {
1345 deps.fields_.insert(VerifierDeps::FieldResolution(entry.GetDexFieldIndex(),
1346 entry.GetAccessFlags(),
1347 kNewTypeIndex));
1348 return;
1349 }
1350 }
1351 LOG(FATAL) << "Could not find any suitable fields";
1352 UNREACHABLE();
1353 }, buffer, &error_msg));
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001354
David Brazdil8fd67222019-02-05 18:13:44 +00001355 // Mess with methods.
1356 ASSERT_FALSE(RunValidation([](VerifierDeps::DexFileDeps& deps) {
1357 std::set<VerifierDeps::MethodResolution>* methods = &deps.methods_;
1358 for (const auto& entry : *methods) {
1359 if (entry.IsResolved()) {
1360 methods->insert(VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1361 VerifierDeps::kUnresolvedMarker,
1362 entry.GetDeclaringClassIndex()));
1363 return;
1364 }
1365 }
1366 LOG(FATAL) << "Could not find any resolved methods";
1367 UNREACHABLE();
1368 }, buffer, &error_msg));
1369 ASSERT_FALSE(RunValidation([](VerifierDeps::DexFileDeps& deps) {
1370 std::set<VerifierDeps::MethodResolution>* methods = &deps.methods_;
1371 for (const auto& entry : *methods) {
1372 if (!entry.IsResolved()) {
1373 constexpr dex::StringIndex kStringIndexZero(0); // We know there is a class there.
1374 methods->insert(VerifierDeps::MethodResolution(0 /* we know there is a method there */,
1375 VerifierDeps::kUnresolvedMarker - 1,
1376 kStringIndexZero));
1377 return;
1378 }
1379 }
1380 LOG(FATAL) << "Could not find any unresolved methods";
1381 UNREACHABLE();
1382 }, buffer, &error_msg));
1383 ASSERT_FALSE(RunValidation([](VerifierDeps::DexFileDeps& deps) {
1384 std::set<VerifierDeps::MethodResolution>* methods = &deps.methods_;
1385 for (const auto& entry : *methods) {
1386 if (entry.IsResolved()) {
1387 methods->insert(VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1388 entry.GetAccessFlags() - 1,
1389 entry.GetDeclaringClassIndex()));
1390 return;
1391 }
1392 }
1393 LOG(FATAL) << "Could not find any resolved methods";
1394 UNREACHABLE();
1395 }, buffer, &error_msg));
1396 ASSERT_FALSE(RunValidation([](VerifierDeps::DexFileDeps& deps) {
1397 std::set<VerifierDeps::MethodResolution>* methods = &deps.methods_;
1398 for (const auto& entry : *methods) {
1399 constexpr dex::StringIndex kNewTypeIndex(0);
1400 if (entry.IsResolved() && entry.GetDeclaringClassIndex() != kNewTypeIndex) {
1401 methods->insert(VerifierDeps::MethodResolution(entry.GetDexMethodIndex(),
1402 entry.GetAccessFlags(),
1403 kNewTypeIndex));
1404 return;
1405 }
1406 }
1407 LOG(FATAL) << "Could not find any suitable methods";
1408 UNREACHABLE();
1409 }, buffer, &error_msg));
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001410}
1411
1412TEST_F(VerifierDepsTest, CompilerDriver) {
1413 SetupCompilerDriver();
1414
1415 // Test both multi-dex and single-dex configuration.
1416 for (const char* multi : { "MultiDex", static_cast<const char*>(nullptr) }) {
1417 // Test that the compiler driver behaves as expected when the dependencies
1418 // verify and when they don't verify.
1419 for (bool verify_failure : { false, true }) {
1420 {
1421 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001422 LoadDexFile(soa, "VerifierDeps", multi);
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001423 }
Andreas Gampe3db70682018-12-26 15:12:03 -08001424 VerifyWithCompilerDriver(/* verifier_deps= */ nullptr);
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001425
1426 std::vector<uint8_t> buffer;
1427 verifier_deps_->Encode(dex_files_, &buffer);
1428
1429 {
1430 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01001431 LoadDexFile(soa, "VerifierDeps", multi);
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001432 }
1433 verifier::VerifierDeps decoded_deps(dex_files_, ArrayRef<const uint8_t>(buffer));
1434 if (verify_failure) {
1435 // Just taint the decoded VerifierDeps with one invalid entry.
1436 VerifierDeps::DexFileDeps* deps = decoded_deps.GetDexFileDeps(*primary_dex_file_);
1437 bool found = false;
1438 for (const auto& entry : deps->classes_) {
1439 if (entry.IsResolved()) {
1440 deps->classes_.insert(VerifierDeps::ClassResolution(
1441 entry.GetDexTypeIndex(), VerifierDeps::kUnresolvedMarker));
1442 found = true;
1443 break;
1444 }
1445 }
1446 ASSERT_TRUE(found);
1447 }
1448 VerifyWithCompilerDriver(&decoded_deps);
1449
1450 if (verify_failure) {
1451 ASSERT_FALSE(verifier_deps_ == nullptr);
1452 ASSERT_FALSE(verifier_deps_->Equals(decoded_deps));
1453 } else {
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001454 VerifyClassStatus(decoded_deps);
1455 }
Nicolas Geoffray8904b6f2016-10-28 19:50:34 +01001456 }
1457 }
1458}
1459
Nicolas Geoffray7cc3ae52017-03-07 14:33:37 +00001460TEST_F(VerifierDepsTest, MultiDexVerification) {
1461 VerifyDexFile("VerifierDepsMulti");
1462 ASSERT_EQ(NumberOfCompiledDexFiles(), 2u);
1463
1464 ASSERT_TRUE(HasUnverifiedClass("LMySoftVerificationFailure;", *dex_files_[1]));
1465 ASSERT_TRUE(HasUnverifiedClass("LMySub1SoftVerificationFailure;", *dex_files_[0]));
1466 ASSERT_TRUE(HasUnverifiedClass("LMySub2SoftVerificationFailure;", *dex_files_[0]));
1467
1468 std::vector<uint8_t> buffer;
1469 verifier_deps_->Encode(dex_files_, &buffer);
1470 ASSERT_FALSE(buffer.empty());
1471}
1472
Nicolas Geoffrayfc38e912017-03-16 16:51:59 +00001473TEST_F(VerifierDepsTest, NotAssignable_InterfaceWithClassInBoot) {
Andreas Gampe3db70682018-12-26 15:12:03 -08001474 ASSERT_TRUE(TestAssignabilityRecording(/* dst= */ "Ljava/lang/Exception;",
1475 /* src= */ "LIface;",
1476 /* is_strict= */ true,
1477 /* is_assignable= */ false));
Nicolas Geoffrayfc38e912017-03-16 16:51:59 +00001478 ASSERT_TRUE(HasAssignable("Ljava/lang/Exception;", "LIface;", false));
1479}
1480
Nicolas Geoffraybdb540d2017-04-19 13:50:34 +01001481TEST_F(VerifierDepsTest, Assignable_Arrays) {
Andreas Gampe3db70682018-12-26 15:12:03 -08001482 ASSERT_TRUE(TestAssignabilityRecording(/* dst= */ "[LIface;",
1483 /* src= */ "[LMyClassExtendingInterface;",
1484 /* is_strict= */ false,
1485 /* is_assignable= */ true));
Nicolas Geoffraybdb540d2017-04-19 13:50:34 +01001486 ASSERT_FALSE(HasAssignable(
Andreas Gampe3db70682018-12-26 15:12:03 -08001487 "LIface;", "LMyClassExtendingInterface;", /* expected_is_assignable= */ true));
Nicolas Geoffraybdb540d2017-04-19 13:50:34 +01001488 ASSERT_FALSE(HasAssignable(
Andreas Gampe3db70682018-12-26 15:12:03 -08001489 "LIface;", "LMyClassExtendingInterface;", /* expected_is_assignable= */ false));
Nicolas Geoffraybdb540d2017-04-19 13:50:34 +01001490}
1491
David Brazdilca3c8c32016-09-06 14:04:48 +01001492} // namespace verifier
1493} // namespace art