blob: 3c7fb7a26a84a08be8e3c9978f8f3252c092b5dd [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
17#include "verifier_deps.h"
18
19#include "compiler_callbacks.h"
David Brazdil6f82fbd2016-09-14 11:55:26 +010020#include "leb128.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010021#include "mirror/class-inl.h"
Mathieu Chartier3398c782016-09-30 10:27:43 -070022#include "obj_ptr-inl.h"
David Brazdilca3c8c32016-09-06 14:04:48 +010023#include "runtime.h"
24
25namespace art {
26namespace verifier {
27
28VerifierDeps::VerifierDeps(const std::vector<const DexFile*>& dex_files) {
29 MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
30 for (const DexFile* dex_file : dex_files) {
31 DCHECK(GetDexFileDeps(*dex_file) == nullptr);
32 std::unique_ptr<DexFileDeps> deps(new DexFileDeps());
33 dex_deps_.emplace(dex_file, std::move(deps));
34 }
35}
36
37VerifierDeps::DexFileDeps* VerifierDeps::GetDexFileDeps(const DexFile& dex_file) {
38 auto it = dex_deps_.find(&dex_file);
39 return (it == dex_deps_.end()) ? nullptr : it->second.get();
40}
41
42template <typename T>
43uint16_t VerifierDeps::GetAccessFlags(T* element) {
44 static_assert(kAccJavaFlagsMask == 0xFFFF, "Unexpected value of a constant");
45 if (element == nullptr) {
46 return VerifierDeps::kUnresolvedMarker;
47 } else {
48 uint16_t access_flags = Low16Bits(element->GetAccessFlags());
49 CHECK_NE(access_flags, VerifierDeps::kUnresolvedMarker);
50 return access_flags;
51 }
52}
53
54template <typename T>
55uint32_t VerifierDeps::GetDeclaringClassStringId(const DexFile& dex_file, T* element) {
56 static_assert(kAccJavaFlagsMask == 0xFFFF, "Unexpected value of a constant");
57 if (element == nullptr) {
58 return VerifierDeps::kUnresolvedMarker;
59 } else {
60 std::string temp;
61 uint32_t string_id = GetIdFromString(
62 dex_file, element->GetDeclaringClass()->GetDescriptor(&temp));
63 return string_id;
64 }
65}
66
67uint32_t VerifierDeps::GetIdFromString(const DexFile& dex_file, const std::string& str) {
68 const DexFile::StringId* string_id = dex_file.FindStringId(str.c_str());
69 if (string_id != nullptr) {
70 // String is in the DEX file. Return its ID.
71 return dex_file.GetIndexForStringId(*string_id);
72 }
73
74 // String is not in the DEX file. Assign a new ID to it which is higher than
75 // the number of strings in the DEX file.
76
77 DexFileDeps* deps = GetDexFileDeps(dex_file);
78 DCHECK(deps != nullptr);
79
80 uint32_t num_ids_in_dex = dex_file.NumStringIds();
81 uint32_t num_extra_ids = deps->strings_.size();
82
83 for (size_t i = 0; i < num_extra_ids; ++i) {
84 if (deps->strings_[i] == str) {
85 return num_ids_in_dex + i;
86 }
87 }
88
89 deps->strings_.push_back(str);
90
91 uint32_t new_id = num_ids_in_dex + num_extra_ids;
92 CHECK_GE(new_id, num_ids_in_dex); // check for overflows
93 DCHECK_EQ(str, GetStringFromId(dex_file, new_id));
94
95 return new_id;
96}
97
98std::string VerifierDeps::GetStringFromId(const DexFile& dex_file, uint32_t string_id) {
99 uint32_t num_ids_in_dex = dex_file.NumStringIds();
100 if (string_id < num_ids_in_dex) {
101 return std::string(dex_file.StringDataByIdx(string_id));
102 } else {
103 DexFileDeps* deps = GetDexFileDeps(dex_file);
104 DCHECK(deps != nullptr);
105 string_id -= num_ids_in_dex;
106 CHECK_LT(string_id, deps->strings_.size());
107 return deps->strings_[string_id];
108 }
109}
110
Mathieu Chartier3398c782016-09-30 10:27:43 -0700111bool VerifierDeps::IsInClassPath(ObjPtr<mirror::Class> klass) {
David Brazdilca3c8c32016-09-06 14:04:48 +0100112 DCHECK(klass != nullptr);
113
Mathieu Chartier3398c782016-09-30 10:27:43 -0700114 ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache();
David Brazdilca3c8c32016-09-06 14:04:48 +0100115 if (dex_cache == nullptr) {
116 // This is a synthesized class, in this case always an array. They are not
117 // defined in the compiled DEX files and therefore are part of the classpath.
118 // We could avoid recording dependencies on arrays with component types in
119 // the compiled DEX files but we choose to record them anyway so as to
120 // record the access flags VM sets for array classes.
David Sehr709b0702016-10-13 09:12:37 -0700121 DCHECK(klass->IsArrayClass()) << klass->PrettyDescriptor();
David Brazdilca3c8c32016-09-06 14:04:48 +0100122 return true;
123 }
124
125 const DexFile* dex_file = dex_cache->GetDexFile();
126 DCHECK(dex_file != nullptr);
127
128 // Test if the `dex_deps_` contains an entry for `dex_file`. If not, the dex
129 // file was not registered as being compiled and we assume `klass` is in the
130 // classpath.
131 return (GetDexFileDeps(*dex_file) == nullptr);
132}
133
134void VerifierDeps::AddClassResolution(const DexFile& dex_file,
135 uint16_t type_idx,
136 mirror::Class* klass) {
137 DexFileDeps* dex_deps = GetDexFileDeps(dex_file);
138 if (dex_deps == nullptr) {
139 // This invocation is from verification of a dex file which is not being compiled.
140 return;
141 }
142
143 if (klass != nullptr && !IsInClassPath(klass)) {
144 // Class resolved into one of the DEX files which are being compiled.
145 // This is not a classpath dependency.
146 return;
147 }
148
149 MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
150 dex_deps->classes_.emplace(ClassResolution(type_idx, GetAccessFlags(klass)));
151}
152
153void VerifierDeps::AddFieldResolution(const DexFile& dex_file,
154 uint32_t field_idx,
155 ArtField* field) {
156 DexFileDeps* dex_deps = GetDexFileDeps(dex_file);
157 if (dex_deps == nullptr) {
158 // This invocation is from verification of a dex file which is not being compiled.
159 return;
160 }
161
162 if (field != nullptr && !IsInClassPath(field->GetDeclaringClass())) {
163 // Field resolved into one of the DEX files which are being compiled.
164 // This is not a classpath dependency.
165 return;
166 }
167
168 MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
169 dex_deps->fields_.emplace(FieldResolution(
170 field_idx, GetAccessFlags(field), GetDeclaringClassStringId(dex_file, field)));
171}
172
173void VerifierDeps::AddMethodResolution(const DexFile& dex_file,
174 uint32_t method_idx,
175 MethodResolutionKind resolution_kind,
176 ArtMethod* method) {
177 DexFileDeps* dex_deps = GetDexFileDeps(dex_file);
178 if (dex_deps == nullptr) {
179 // This invocation is from verification of a dex file which is not being compiled.
180 return;
181 }
182
183 if (method != nullptr && !IsInClassPath(method->GetDeclaringClass())) {
184 // Method resolved into one of the DEX files which are being compiled.
185 // This is not a classpath dependency.
186 return;
187 }
188
189 MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
190 MethodResolution method_tuple(method_idx,
191 GetAccessFlags(method),
192 GetDeclaringClassStringId(dex_file, method));
193 if (resolution_kind == kDirectMethodResolution) {
194 dex_deps->direct_methods_.emplace(method_tuple);
195 } else if (resolution_kind == kVirtualMethodResolution) {
196 dex_deps->virtual_methods_.emplace(method_tuple);
197 } else {
198 DCHECK_EQ(resolution_kind, kInterfaceMethodResolution);
199 dex_deps->interface_methods_.emplace(method_tuple);
200 }
201}
202
203void VerifierDeps::AddAssignability(const DexFile& dex_file,
204 mirror::Class* destination,
205 mirror::Class* source,
206 bool is_strict,
207 bool is_assignable) {
208 // Test that the method is only called on reference types.
209 // Note that concurrent verification of `destination` and `source` may have
210 // set their status to erroneous. However, the tests performed below rely
211 // merely on no issues with linking (valid access flags, superclass and
212 // implemented interfaces). If the class at any point reached the IsResolved
213 // status, the requirement holds. This is guaranteed by RegTypeCache::ResolveClass.
214 DCHECK(destination != nullptr && !destination->IsPrimitive());
215 DCHECK(source != nullptr && !source->IsPrimitive());
216
217 if (destination == source ||
218 destination->IsObjectClass() ||
219 (!is_strict && destination->IsInterface())) {
220 // Cases when `destination` is trivially assignable from `source`.
221 DCHECK(is_assignable);
222 return;
223 }
224
225 DCHECK_EQ(is_assignable, destination->IsAssignableFrom(source));
226
227 if (destination->IsArrayClass() && source->IsArrayClass()) {
228 // Both types are arrays. Break down to component types and add recursively.
229 // This helps filter out destinations from compiled DEX files (see below)
230 // and deduplicate entries with the same canonical component type.
231 mirror::Class* destination_component = destination->GetComponentType();
232 mirror::Class* source_component = source->GetComponentType();
233
234 // Only perform the optimization if both types are resolved which guarantees
235 // that they linked successfully, as required at the top of this method.
236 if (destination_component->IsResolved() && source_component->IsResolved()) {
237 AddAssignability(dex_file,
238 destination_component,
239 source_component,
240 /* is_strict */ true,
241 is_assignable);
242 return;
243 }
244 }
245
246 DexFileDeps* dex_deps = GetDexFileDeps(dex_file);
247 if (dex_deps == nullptr) {
248 // This invocation is from verification of a DEX file which is not being compiled.
249 return;
250 }
251
252 if (!IsInClassPath(destination) && !IsInClassPath(source)) {
253 // Both `destination` and `source` are defined in the compiled DEX files.
254 // No need to record a dependency.
255 return;
256 }
257
258 MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
259
260 // Get string IDs for both descriptors and store in the appropriate set.
261
262 std::string temp1, temp2;
263 std::string destination_desc(destination->GetDescriptor(&temp1));
264 std::string source_desc(source->GetDescriptor(&temp2));
265 uint32_t destination_id = GetIdFromString(dex_file, destination_desc);
266 uint32_t source_id = GetIdFromString(dex_file, source_desc);
267
268 if (is_assignable) {
269 dex_deps->assignable_types_.emplace(TypeAssignability(destination_id, source_id));
270 } else {
271 dex_deps->unassignable_types_.emplace(TypeAssignability(destination_id, source_id));
272 }
273}
274
275static inline VerifierDeps* GetVerifierDepsSingleton() {
276 CompilerCallbacks* callbacks = Runtime::Current()->GetCompilerCallbacks();
277 if (callbacks == nullptr) {
278 return nullptr;
279 }
280 return callbacks->GetVerifierDeps();
281}
282
283void VerifierDeps::MaybeRecordClassResolution(const DexFile& dex_file,
284 uint16_t type_idx,
285 mirror::Class* klass) {
286 VerifierDeps* singleton = GetVerifierDepsSingleton();
287 if (singleton != nullptr) {
288 singleton->AddClassResolution(dex_file, type_idx, klass);
289 }
290}
291
292void VerifierDeps::MaybeRecordFieldResolution(const DexFile& dex_file,
293 uint32_t field_idx,
294 ArtField* field) {
295 VerifierDeps* singleton = GetVerifierDepsSingleton();
296 if (singleton != nullptr) {
297 singleton->AddFieldResolution(dex_file, field_idx, field);
298 }
299}
300
301void VerifierDeps::MaybeRecordMethodResolution(const DexFile& dex_file,
302 uint32_t method_idx,
303 MethodResolutionKind resolution_kind,
304 ArtMethod* method) {
305 VerifierDeps* singleton = GetVerifierDepsSingleton();
306 if (singleton != nullptr) {
307 singleton->AddMethodResolution(dex_file, method_idx, resolution_kind, method);
308 }
309}
310
311void VerifierDeps::MaybeRecordAssignability(const DexFile& dex_file,
312 mirror::Class* destination,
313 mirror::Class* source,
314 bool is_strict,
315 bool is_assignable) {
316 VerifierDeps* singleton = GetVerifierDepsSingleton();
317 if (singleton != nullptr) {
318 singleton->AddAssignability(dex_file, destination, source, is_strict, is_assignable);
319 }
320}
321
David Brazdil6f82fbd2016-09-14 11:55:26 +0100322static inline uint32_t DecodeUint32WithOverflowCheck(const uint8_t** in, const uint8_t* end) {
323 CHECK_LT(*in, end);
324 return DecodeUnsignedLeb128(in);
325}
326
327template<typename T1, typename T2>
328static inline void EncodeTuple(std::vector<uint8_t>* out, const std::tuple<T1, T2>& t) {
329 EncodeUnsignedLeb128(out, std::get<0>(t));
330 EncodeUnsignedLeb128(out, std::get<1>(t));
331}
332
333template<typename T1, typename T2>
334static inline void DecodeTuple(const uint8_t** in, const uint8_t* end, std::tuple<T1, T2>* t) {
335 T1 v1 = static_cast<T1>(DecodeUint32WithOverflowCheck(in, end));
336 T2 v2 = static_cast<T2>(DecodeUint32WithOverflowCheck(in, end));
337 *t = std::make_tuple(v1, v2);
338}
339
340template<typename T1, typename T2, typename T3>
341static inline void EncodeTuple(std::vector<uint8_t>* out, const std::tuple<T1, T2, T3>& t) {
342 EncodeUnsignedLeb128(out, std::get<0>(t));
343 EncodeUnsignedLeb128(out, std::get<1>(t));
344 EncodeUnsignedLeb128(out, std::get<2>(t));
345}
346
347template<typename T1, typename T2, typename T3>
348static inline void DecodeTuple(const uint8_t** in, const uint8_t* end, std::tuple<T1, T2, T3>* t) {
349 T1 v1 = static_cast<T1>(DecodeUint32WithOverflowCheck(in, end));
350 T2 v2 = static_cast<T2>(DecodeUint32WithOverflowCheck(in, end));
351 T3 v3 = static_cast<T2>(DecodeUint32WithOverflowCheck(in, end));
352 *t = std::make_tuple(v1, v2, v3);
353}
354
355template<typename T>
356static inline void EncodeSet(std::vector<uint8_t>* out, const std::set<T>& set) {
357 EncodeUnsignedLeb128(out, set.size());
358 for (const T& entry : set) {
359 EncodeTuple(out, entry);
360 }
361}
362
363template<typename T>
364static inline void DecodeSet(const uint8_t** in, const uint8_t* end, std::set<T>* set) {
365 DCHECK(set->empty());
366 size_t num_entries = DecodeUint32WithOverflowCheck(in, end);
367 for (size_t i = 0; i < num_entries; ++i) {
368 T tuple;
369 DecodeTuple(in, end, &tuple);
370 set->emplace(tuple);
371 }
372}
373
374static inline void EncodeStringVector(std::vector<uint8_t>* out,
375 const std::vector<std::string>& strings) {
376 EncodeUnsignedLeb128(out, strings.size());
377 for (const std::string& str : strings) {
378 const uint8_t* data = reinterpret_cast<const uint8_t*>(str.c_str());
379 size_t length = str.length() + 1;
380 out->insert(out->end(), data, data + length);
381 DCHECK_EQ(0u, out->back());
382 }
383}
384
385static inline void DecodeStringVector(const uint8_t** in,
386 const uint8_t* end,
387 std::vector<std::string>* strings) {
388 DCHECK(strings->empty());
389 size_t num_strings = DecodeUint32WithOverflowCheck(in, end);
390 strings->reserve(num_strings);
391 for (size_t i = 0; i < num_strings; ++i) {
392 CHECK_LT(*in, end);
393 const char* string_start = reinterpret_cast<const char*>(*in);
394 strings->emplace_back(std::string(string_start));
395 *in += strings->back().length() + 1;
396 }
397}
398
399void VerifierDeps::Encode(std::vector<uint8_t>* buffer) const {
400 MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
401 for (auto& entry : dex_deps_) {
402 EncodeStringVector(buffer, entry.second->strings_);
403 EncodeSet(buffer, entry.second->assignable_types_);
404 EncodeSet(buffer, entry.second->unassignable_types_);
405 EncodeSet(buffer, entry.second->classes_);
406 EncodeSet(buffer, entry.second->fields_);
407 EncodeSet(buffer, entry.second->direct_methods_);
408 EncodeSet(buffer, entry.second->virtual_methods_);
409 EncodeSet(buffer, entry.second->interface_methods_);
410 }
411}
412
413VerifierDeps::VerifierDeps(const std::vector<const DexFile*>& dex_files, ArrayRef<uint8_t> data)
414 : VerifierDeps(dex_files) {
415 const uint8_t* data_start = data.data();
416 const uint8_t* data_end = data_start + data.size();
417 for (auto& entry : dex_deps_) {
418 DecodeStringVector(&data_start, data_end, &entry.second->strings_);
419 DecodeSet(&data_start, data_end, &entry.second->assignable_types_);
420 DecodeSet(&data_start, data_end, &entry.second->unassignable_types_);
421 DecodeSet(&data_start, data_end, &entry.second->classes_);
422 DecodeSet(&data_start, data_end, &entry.second->fields_);
423 DecodeSet(&data_start, data_end, &entry.second->direct_methods_);
424 DecodeSet(&data_start, data_end, &entry.second->virtual_methods_);
425 DecodeSet(&data_start, data_end, &entry.second->interface_methods_);
426 }
427 CHECK_LE(data_start, data_end);
428}
429
430bool VerifierDeps::Equals(const VerifierDeps& rhs) const {
431 MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_);
432
433 if (dex_deps_.size() != rhs.dex_deps_.size()) {
434 return false;
435 }
436
437 auto lhs_it = dex_deps_.begin();
438 auto rhs_it = rhs.dex_deps_.begin();
439
440 for (; (lhs_it != dex_deps_.end()) && (rhs_it != rhs.dex_deps_.end()); lhs_it++, rhs_it++) {
441 const DexFile* lhs_dex_file = lhs_it->first;
442 const DexFile* rhs_dex_file = rhs_it->first;
443 if (lhs_dex_file != rhs_dex_file) {
444 return false;
445 }
446
447 DexFileDeps* lhs_deps = lhs_it->second.get();
448 DexFileDeps* rhs_deps = rhs_it->second.get();
449 if (!lhs_deps->Equals(*rhs_deps)) {
450 return false;
451 }
452 }
453
454 DCHECK((lhs_it == dex_deps_.end()) && (rhs_it == rhs.dex_deps_.end()));
455 return true;
456}
457
458bool VerifierDeps::DexFileDeps::Equals(const VerifierDeps::DexFileDeps& rhs) const {
459 return (strings_ == rhs.strings_) &&
460 (assignable_types_ == rhs.assignable_types_) &&
461 (unassignable_types_ == rhs.unassignable_types_) &&
462 (classes_ == rhs.classes_) &&
463 (fields_ == rhs.fields_) &&
464 (direct_methods_ == rhs.direct_methods_) &&
465 (virtual_methods_ == rhs.virtual_methods_) &&
466 (interface_methods_ == rhs.interface_methods_);
467}
468
David Brazdilca3c8c32016-09-06 14:04:48 +0100469} // namespace verifier
470} // namespace art