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