blob: 1b37c666851ac3942f684f15fbf9afcf8a8db25d [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Andreas Gampe4a57d5f2019-04-03 12:31:18 -070019#include <ostream>
Elliott Hughes1f359b02011-07-17 14:27:17 -070020
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021#include "android-base/stringprintf.h"
22
Mathieu Chartierc7853442015-03-27 14:35:38 -070023#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070024#include "art_method-inl.h"
Andreas Gampe39b378c2017-12-07 15:44:13 -080025#include "base/aborting.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070026#include "base/enums.h"
David Sehr67bf42e2018-02-26 16:43:04 -080027#include "base/leb128.h"
David Sehr9c4a0152018-04-05 12:23:54 -070028#include "base/indenter.h"
Andreas Gampe57943812017-12-06 21:39:13 -080029#include "base/logging.h" // For VLOG.
Ian Rogers637c65b2013-05-31 11:46:00 -070030#include "base/mutex-inl.h"
David Brazdil2bb2fbd2018-11-13 18:24:26 +000031#include "base/sdk_version.h"
Vladimir Marko637ee0b2015-09-04 12:47:41 +010032#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080033#include "base/systrace.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010034#include "base/time_utils.h"
David Sehrc431b9d2018-03-02 12:01:51 -080035#include "base/utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070036#include "class_linker.h"
Vladimir Marko5868ada2020-05-12 11:50:34 +010037#include "class_root-inl.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000038#include "compiler_callbacks.h"
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -070039#include "dex/class_accessor-inl.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080040#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080041#include "dex/dex_file-inl.h"
42#include "dex/dex_file_exception_helpers.h"
43#include "dex/dex_instruction-inl.h"
44#include "dex/dex_instruction_utils.h"
Alex Lighteb7c1442015-08-31 13:17:42 -070045#include "experimental_flags.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070046#include "gc/accounting/card_table-inl.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070047#include "handle_scope-inl.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070048#include "intern_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049#include "mirror/class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070050#include "mirror/class.h"
Andreas Gampe51de69e2019-04-19 15:14:14 -070051#include "mirror/class_loader.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070052#include "mirror/dex_cache-inl.h"
Orion Hodsoncfa325e2016-10-13 10:25:54 +010053#include "mirror/method_handle_impl.h"
Orion Hodson2e599942017-09-22 16:17:41 +010054#include "mirror/method_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055#include "mirror/object-inl.h"
56#include "mirror/object_array-inl.h"
Orion Hodsonfe92d122018-01-02 10:45:17 +000057#include "mirror/var_handle.h"
Andreas Gampe51de69e2019-04-19 15:14:14 -070058#include "obj_ptr-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070059#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070060#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070061#include "runtime.h"
Andreas Gampe2ad6cce2019-04-11 16:17:39 -070062#include "scoped_newline.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070063#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070064#include "stack.h"
Nicolas Geoffrayb041a402017-11-13 15:16:22 +000065#include "vdex_file.h"
Alex Lighte2ddce32019-05-22 17:08:35 +000066#include "verifier/method_verifier.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070067#include "verifier_compiler_binding.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070068#include "verifier_deps.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070069
70namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070071namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070072
Andreas Gampe46ee31b2016-12-14 10:11:49 -080073using android::base::StringPrintf;
74
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070075static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogers2c8a8572011-10-24 17:11:36 -070076
Vladimir Marko69d310e2017-10-09 14:12:23 +010077PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& allocator)
78 : register_lines_(allocator.Adapter(kArenaAllocVerifier)) {}
Mathieu Chartierde40d472015-10-15 17:47:48 -070079
Andreas Gamped09c0592019-04-19 15:44:05 -070080void PcToRegisterLineTable::Init(RegisterTrackingMode mode,
81 InstructionFlags* flags,
82 uint32_t insns_size,
83 uint16_t registers_size,
84 ScopedArenaAllocator& allocator,
85 RegTypeCache* reg_types) {
Ian Rogersd81871c2011-10-03 13:57:23 -070086 DCHECK_GT(insns_size, 0U);
Mathieu Chartierde40d472015-10-15 17:47:48 -070087 register_lines_.resize(insns_size);
Ian Rogersd81871c2011-10-03 13:57:23 -070088 for (uint32_t i = 0; i < insns_size; i++) {
89 bool interesting = false;
90 switch (mode) {
91 case kTrackRegsAll:
92 interesting = flags[i].IsOpcode();
93 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070094 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070095 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070096 break;
97 case kTrackRegsBranches:
98 interesting = flags[i].IsBranchTarget();
99 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700100 }
101 if (interesting) {
Andreas Gamped09c0592019-04-19 15:44:05 -0700102 register_lines_[i].reset(RegisterLine::Create(registers_size, allocator, reg_types));
Ian Rogersd0fbd852013-09-24 18:17:04 -0700103 }
104 }
105}
106
Mathieu Chartierde40d472015-10-15 17:47:48 -0700107PcToRegisterLineTable::~PcToRegisterLineTable() {}
Ian Rogersd81871c2011-10-03 13:57:23 -0700108
Andreas Gampefc25ae92019-04-19 22:22:57 -0700109namespace impl {
110namespace {
111
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700112enum class CheckAccess {
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700113 kNo,
Andreas Gampe6087bc22019-06-03 15:52:08 -0700114 kOnResolvedClass,
115 kYes,
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700116};
117
118enum class FieldAccessType {
119 kAccGet,
120 kAccPut
121};
122
Andreas Gampec6582782019-07-10 15:06:18 -0700123// Instruction types that are not marked as throwing (because they normally would not), but for
124// historical reasons may do so. These instructions cannot be marked kThrow as that would introduce
125// a general flow that is unwanted.
126//
127// Note: Not implemented as Instruction::Flags value as that set is full and we'd need to increase
128// the struct size (making it a non-power-of-two) for a single element.
129//
130// Note: This should eventually be removed.
131constexpr bool IsCompatThrow(Instruction::Code opcode) {
Mathieu Chartiere8ed8662020-03-13 13:55:44 -0700132 return opcode == Instruction::Code::RETURN_OBJECT || opcode == Instruction::Code::MOVE_EXCEPTION;
Andreas Gampec6582782019-07-10 15:06:18 -0700133}
134
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700135template <bool kVerifierDebug>
Andreas Gampefc25ae92019-04-19 22:22:57 -0700136class MethodVerifier final : public ::art::verifier::MethodVerifier {
137 public:
138 bool IsInstanceConstructor() const {
139 return IsConstructor() && !IsStatic();
140 }
141
142 const RegType& ResolveCheckedClass(dex::TypeIndex class_idx) override
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700143 REQUIRES_SHARED(Locks::mutator_lock_) {
144 DCHECK(!HasFailures());
145 const RegType& result = ResolveClass<CheckAccess::kYes>(class_idx);
146 DCHECK(!HasFailures());
147 return result;
148 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700149
150 void FindLocksAtDexPc() REQUIRES_SHARED(Locks::mutator_lock_);
151
152 private:
153 MethodVerifier(Thread* self,
Andreas Gampee0bbab92019-07-25 12:28:22 -0700154 ClassLinker* class_linker,
Andreas Gampef1468b52019-07-26 09:22:39 -0700155 ArenaPool* arena_pool,
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +0000156 VerifierDeps* verifier_deps,
Andreas Gampefc25ae92019-04-19 22:22:57 -0700157 const DexFile* dex_file,
Andreas Gampe0134a282019-07-29 11:34:55 -0700158 const dex::CodeItem* code_item,
159 uint32_t method_idx,
160 bool can_load_classes,
161 bool allow_thread_suspension,
162 bool allow_soft_failures,
163 bool aot_mode,
Andreas Gampefc25ae92019-04-19 22:22:57 -0700164 Handle<mirror::DexCache> dex_cache,
165 Handle<mirror::ClassLoader> class_loader,
166 const dex::ClassDef& class_def,
Andreas Gampefc25ae92019-04-19 22:22:57 -0700167 ArtMethod* method,
168 uint32_t access_flags,
Andreas Gampefc25ae92019-04-19 22:22:57 -0700169 bool need_precise_constants,
170 bool verify_to_dump,
Andreas Gampe86bf0682019-07-29 11:22:44 -0700171 bool fill_register_lines,
Andreas Gampe86bf0682019-07-29 11:22:44 -0700172 uint32_t api_level) REQUIRES_SHARED(Locks::mutator_lock_)
173 : art::verifier::MethodVerifier(self,
174 class_linker,
175 arena_pool,
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +0000176 verifier_deps,
Andreas Gampe86bf0682019-07-29 11:22:44 -0700177 dex_file,
Nicolas Geoffray1960c422020-11-04 08:45:32 +0000178 class_def,
Andreas Gampe86bf0682019-07-29 11:22:44 -0700179 code_item,
180 method_idx,
181 can_load_classes,
182 allow_thread_suspension,
183 allow_soft_failures,
184 aot_mode),
185 method_being_verified_(method),
186 method_access_flags_(access_flags),
187 return_type_(nullptr),
188 dex_cache_(dex_cache),
189 class_loader_(class_loader),
Andreas Gampe86bf0682019-07-29 11:22:44 -0700190 declaring_class_(nullptr),
191 interesting_dex_pc_(-1),
192 monitor_enter_dex_pcs_(nullptr),
193 need_precise_constants_(need_precise_constants),
194 verify_to_dump_(verify_to_dump),
195 allow_thread_suspension_(allow_thread_suspension),
196 is_constructor_(false),
197 fill_register_lines_(fill_register_lines),
198 api_level_(api_level == 0 ? std::numeric_limits<uint32_t>::max() : api_level) {
199 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700200
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700201 void UninstantiableError(const char* descriptor) {
202 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
203 << "non-instantiable klass " << descriptor;
204 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700205 static bool IsInstantiableOrPrimitive(ObjPtr<mirror::Class> klass)
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700206 REQUIRES_SHARED(Locks::mutator_lock_) {
207 return klass->IsInstantiable() || klass->IsPrimitive();
208 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700209
210 // Is the method being verified a constructor? See the comment on the field.
211 bool IsConstructor() const {
212 return is_constructor_;
213 }
214
215 // Is the method verified static?
216 bool IsStatic() const {
217 return (method_access_flags_ & kAccStatic) != 0;
218 }
219
220 // Adds the given string to the beginning of the last failure message.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700221 void PrependToLastFailMessage(std::string prepend) {
222 size_t failure_num = failure_messages_.size();
223 DCHECK_NE(failure_num, 0U);
224 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
225 prepend += last_fail_message->str();
226 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
227 delete last_fail_message;
228 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700229
230 // Adds the given string to the end of the last failure message.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700231 void AppendToLastFailMessage(const std::string& append) {
232 size_t failure_num = failure_messages_.size();
233 DCHECK_NE(failure_num, 0U);
234 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
235 (*last_fail_message) << append;
236 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700237
Andreas Gampefc25ae92019-04-19 22:22:57 -0700238 /*
239 * Compute the width of the instruction at each address in the instruction stream, and store it in
240 * insn_flags_. Addresses that are in the middle of an instruction, or that are part of switch
241 * table data, are not touched (so the caller should probably initialize "insn_flags" to zero).
242 *
243 * The "new_instance_count_" and "monitor_enter_count_" fields in vdata are also set.
244 *
245 * Performs some static checks, notably:
246 * - opcode of first instruction begins at index 0
247 * - only documented instructions may appear
248 * - each instruction follows the last
249 * - last byte of last instruction is at (code_length-1)
250 *
251 * Logs an error and returns "false" on failure.
252 */
253 bool ComputeWidthsAndCountOps();
254
255 /*
256 * Set the "in try" flags for all instructions protected by "try" statements. Also sets the
257 * "branch target" flags for exception handlers.
258 *
259 * Call this after widths have been set in "insn_flags".
260 *
261 * Returns "false" if something in the exception table looks fishy, but we're expecting the
Orion Hodsonaacf9772020-07-22 21:51:00 +0100262 * exception table to be valid.
Andreas Gampefc25ae92019-04-19 22:22:57 -0700263 */
264 bool ScanTryCatchBlocks() REQUIRES_SHARED(Locks::mutator_lock_);
265
266 /*
267 * Perform static verification on all instructions in a method.
268 *
269 * Walks through instructions in a method calling VerifyInstruction on each.
270 */
271 template <bool kAllowRuntimeOnlyInstructions>
272 bool VerifyInstructions();
273
274 /*
275 * Perform static verification on an instruction.
276 *
277 * As a side effect, this sets the "branch target" flags in InsnFlags.
278 *
279 * "(CF)" items are handled during code-flow analysis.
280 *
281 * v3 4.10.1
282 * - target of each jump and branch instruction must be valid
283 * - targets of switch statements must be valid
284 * - operands referencing constant pool entries must be valid
285 * - (CF) operands of getfield, putfield, getstatic, putstatic must be valid
286 * - (CF) operands of method invocation instructions must be valid
287 * - (CF) only invoke-direct can call a method starting with '<'
288 * - (CF) <clinit> must never be called explicitly
289 * - operands of instanceof, checkcast, new (and variants) must be valid
290 * - new-array[-type] limited to 255 dimensions
291 * - can't use "new" on an array class
292 * - (?) limit dimensions in multi-array creation
293 * - local variable load/store register values must be in valid range
294 *
295 * v3 4.11.1.2
296 * - branches must be within the bounds of the code array
297 * - targets of all control-flow instructions are the start of an instruction
298 * - register accesses fall within range of allocated registers
299 * - (N/A) access to constant pool must be of appropriate type
300 * - code does not end in the middle of an instruction
301 * - execution cannot fall off the end of the code
302 * - (earlier) for each exception handler, the "try" area must begin and
303 * end at the start of an instruction (end can be at the end of the code)
304 * - (earlier) for each exception handler, the handler must start at a valid
305 * instruction
306 */
307 template <bool kAllowRuntimeOnlyInstructions>
308 bool VerifyInstruction(const Instruction* inst, uint32_t code_offset);
309
310 /* Ensure that the register index is valid for this code item. */
Andreas Gampe86bf0682019-07-29 11:22:44 -0700311 bool CheckRegisterIndex(uint32_t idx) {
312 if (UNLIKELY(idx >= code_item_accessor_.RegistersSize())) {
313 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
314 << code_item_accessor_.RegistersSize() << ")";
315 return false;
316 }
317 return true;
318 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700319
320 /* Ensure that the wide register index is valid for this code item. */
Andreas Gampe86bf0682019-07-29 11:22:44 -0700321 bool CheckWideRegisterIndex(uint32_t idx) {
322 if (UNLIKELY(idx + 1 >= code_item_accessor_.RegistersSize())) {
323 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
324 << "+1 >= " << code_item_accessor_.RegistersSize() << ")";
325 return false;
326 }
327 return true;
328 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700329
330 // Perform static checks on an instruction referencing a CallSite. All we do here is ensure that
331 // the call site index is in the valid range.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700332 bool CheckCallSiteIndex(uint32_t idx) {
333 uint32_t limit = dex_file_->NumCallSiteIds();
334 if (UNLIKELY(idx >= limit)) {
335 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad call site index " << idx << " (max "
336 << limit << ")";
337 return false;
338 }
339 return true;
340 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700341
342 // Perform static checks on a field Get or set instruction. All we do here is ensure that the
343 // field index is in the valid range.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700344 bool CheckFieldIndex(uint32_t idx) {
345 if (UNLIKELY(idx >= dex_file_->GetHeader().field_ids_size_)) {
346 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
347 << dex_file_->GetHeader().field_ids_size_ << ")";
348 return false;
349 }
350 return true;
351 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700352
353 // Perform static checks on a method invocation instruction. All we do here is ensure that the
354 // method index is in the valid range.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700355 bool CheckMethodIndex(uint32_t idx) {
356 if (UNLIKELY(idx >= dex_file_->GetHeader().method_ids_size_)) {
357 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
358 << dex_file_->GetHeader().method_ids_size_ << ")";
359 return false;
360 }
361 return true;
362 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700363
364 // Perform static checks on an instruction referencing a constant method handle. All we do here
365 // is ensure that the method index is in the valid range.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700366 bool CheckMethodHandleIndex(uint32_t idx) {
367 uint32_t limit = dex_file_->NumMethodHandles();
368 if (UNLIKELY(idx >= limit)) {
369 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method handle index " << idx << " (max "
370 << limit << ")";
371 return false;
372 }
373 return true;
374 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700375
376 // Perform static checks on a "new-instance" instruction. Specifically, make sure the class
377 // reference isn't for an array class.
378 bool CheckNewInstance(dex::TypeIndex idx);
379
380 // Perform static checks on a prototype indexing instruction. All we do here is ensure that the
381 // prototype index is in the valid range.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700382 bool CheckPrototypeIndex(uint32_t idx) {
383 if (UNLIKELY(idx >= dex_file_->GetHeader().proto_ids_size_)) {
384 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad prototype index " << idx << " (max "
385 << dex_file_->GetHeader().proto_ids_size_ << ")";
386 return false;
387 }
388 return true;
389 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700390
391 /* Ensure that the string index is in the valid range. */
Andreas Gampe86bf0682019-07-29 11:22:44 -0700392 bool CheckStringIndex(uint32_t idx) {
393 if (UNLIKELY(idx >= dex_file_->GetHeader().string_ids_size_)) {
394 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
395 << dex_file_->GetHeader().string_ids_size_ << ")";
396 return false;
397 }
398 return true;
399 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700400
401 // Perform static checks on an instruction that takes a class constant. Ensure that the class
402 // index is in the valid range.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700403 bool CheckTypeIndex(dex::TypeIndex idx) {
404 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
405 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
406 << dex_file_->GetHeader().type_ids_size_ << ")";
407 return false;
408 }
409 return true;
410 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700411
412 // Perform static checks on a "new-array" instruction. Specifically, make sure they aren't
413 // creating an array of arrays that causes the number of dimensions to exceed 255.
414 bool CheckNewArray(dex::TypeIndex idx);
415
416 // Verify an array data table. "cur_offset" is the offset of the fill-array-data instruction.
417 bool CheckArrayData(uint32_t cur_offset);
418
419 // Verify that the target of a branch instruction is valid. We don't expect code to jump directly
420 // into an exception handler, but it's valid to do so as long as the target isn't a
421 // "move-exception" instruction. We verify that in a later stage.
422 // The dex format forbids certain instructions from branching to themselves.
423 // Updates "insn_flags_", setting the "branch target" flag.
424 bool CheckBranchTarget(uint32_t cur_offset);
425
426 // Verify a switch table. "cur_offset" is the offset of the switch instruction.
427 // Updates "insn_flags_", setting the "branch target" flag.
428 bool CheckSwitchTargets(uint32_t cur_offset);
429
430 // Check the register indices used in a "vararg" instruction, such as invoke-virtual or
431 // filled-new-array.
432 // - vA holds word count (0-5), args[] have values.
433 // There are some tests we don't do here, e.g. we don't try to verify that invoking a method that
434 // takes a double is done with consecutive registers. This requires parsing the target method
435 // signature, which we will be doing later on during the code flow analysis.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700436 bool CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
437 uint16_t registers_size = code_item_accessor_.RegistersSize();
438 for (uint32_t idx = 0; idx < vA; idx++) {
439 if (UNLIKELY(arg[idx] >= registers_size)) {
440 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
441 << ") in non-range invoke (>= " << registers_size << ")";
442 return false;
443 }
444 }
445
446 return true;
447 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700448
449 // Check the register indices used in a "vararg/range" instruction, such as invoke-virtual/range
450 // or filled-new-array/range.
451 // - vA holds word count, vC holds index of first reg.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700452 bool CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
453 uint16_t registers_size = code_item_accessor_.RegistersSize();
454 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
455 // integer overflow when adding them here.
456 if (UNLIKELY(vA + vC > registers_size)) {
457 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
458 << " in range invoke (> " << registers_size << ")";
459 return false;
460 }
461 return true;
462 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700463
464 // Checks the method matches the expectations required to be signature polymorphic.
465 bool CheckSignaturePolymorphicMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
466
467 // Checks the invoked receiver matches the expectations for signature polymorphic methods.
468 bool CheckSignaturePolymorphicReceiver(const Instruction* inst) REQUIRES_SHARED(Locks::mutator_lock_);
469
470 // Extract the relative offset from a branch instruction.
471 // Returns "false" on failure (e.g. this isn't a branch instruction).
472 bool GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
473 bool* selfOkay);
474
475 /* Perform detailed code-flow analysis on a single method. */
476 bool VerifyCodeFlow() REQUIRES_SHARED(Locks::mutator_lock_);
477
478 // Set the register types for the first instruction in the method based on the method signature.
479 // This has the side-effect of validating the signature.
480 bool SetTypesFromSignature() REQUIRES_SHARED(Locks::mutator_lock_);
481
482 /*
483 * Perform code flow on a method.
484 *
485 * The basic strategy is as outlined in v3 4.11.1.2: set the "changed" bit on the first
486 * instruction, process it (setting additional "changed" bits), and repeat until there are no
487 * more.
488 *
489 * v3 4.11.1.1
490 * - (N/A) operand stack is always the same size
491 * - operand stack [registers] contain the correct types of values
492 * - local variables [registers] contain the correct types of values
493 * - methods are invoked with the appropriate arguments
494 * - fields are assigned using values of appropriate types
495 * - opcodes have the correct type values in operand registers
496 * - there is never an uninitialized class instance in a local variable in code protected by an
497 * exception handler (operand stack is okay, because the operand stack is discarded when an
498 * exception is thrown) [can't know what's a local var w/o the debug info -- should fall out of
499 * register typing]
500 *
501 * v3 4.11.1.2
502 * - execution cannot fall off the end of the code
503 *
504 * (We also do many of the items described in the "static checks" sections, because it's easier to
505 * do them here.)
506 *
507 * We need an array of RegType values, one per register, for every instruction. If the method uses
508 * monitor-enter, we need extra data for every register, and a stack for every "interesting"
509 * instruction. In theory this could become quite large -- up to several megabytes for a monster
510 * function.
511 *
512 * NOTE:
513 * The spec forbids backward branches when there's an uninitialized reference in a register. The
514 * idea is to prevent something like this:
515 * loop:
516 * move r1, r0
517 * new-instance r0, MyClass
518 * ...
519 * if-eq rN, loop // once
520 * initialize r0
521 *
522 * This leaves us with two different instances, both allocated by the same instruction, but only
523 * one is initialized. The scheme outlined in v3 4.11.1.4 wouldn't catch this, so they work around
524 * it by preventing backward branches. We achieve identical results without restricting code
525 * reordering by specifying that you can't execute the new-instance instruction if a register
526 * contains an uninitialized instance created by that same instruction.
527 */
Andreas Gampeb90cad52019-07-31 16:15:41 -0700528 template <bool kMonitorDexPCs>
Andreas Gampefc25ae92019-04-19 22:22:57 -0700529 bool CodeFlowVerifyMethod() REQUIRES_SHARED(Locks::mutator_lock_);
530
531 /*
532 * Perform verification for a single instruction.
533 *
534 * This requires fully decoding the instruction to determine the effect it has on registers.
535 *
536 * Finds zero or more following instructions and sets the "changed" flag if execution at that
537 * point needs to be (re-)evaluated. Register changes are merged into "reg_types_" at the target
538 * addresses. Does not set or clear any other flags in "insn_flags_".
539 */
540 bool CodeFlowVerifyInstruction(uint32_t* start_guess)
541 REQUIRES_SHARED(Locks::mutator_lock_);
542
543 // Perform verification of a new array instruction
544 void VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range)
545 REQUIRES_SHARED(Locks::mutator_lock_);
546
547 // Helper to perform verification on puts of primitive type.
548 void VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
549 const uint32_t vregA) REQUIRES_SHARED(Locks::mutator_lock_);
550
551 // Perform verification of an aget instruction. The destination register's type will be set to
552 // be that of component type of the array unless the array type is unknown, in which case a
553 // bottom type inferred from the type of instruction is used. is_primitive is false for an
554 // aget-object.
555 void VerifyAGet(const Instruction* inst, const RegType& insn_type,
556 bool is_primitive) REQUIRES_SHARED(Locks::mutator_lock_);
557
558 // Perform verification of an aput instruction.
559 void VerifyAPut(const Instruction* inst, const RegType& insn_type,
560 bool is_primitive) REQUIRES_SHARED(Locks::mutator_lock_);
561
562 // Lookup instance field and fail for resolution violations
563 ArtField* GetInstanceField(const RegType& obj_type, int field_idx)
564 REQUIRES_SHARED(Locks::mutator_lock_);
565
566 // Lookup static field and fail for resolution violations
567 ArtField* GetStaticField(int field_idx) REQUIRES_SHARED(Locks::mutator_lock_);
568
569 // Perform verification of an iget/sget/iput/sput instruction.
Andreas Gampefc25ae92019-04-19 22:22:57 -0700570 template <FieldAccessType kAccType>
571 void VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
572 bool is_primitive, bool is_static)
573 REQUIRES_SHARED(Locks::mutator_lock_);
574
Andreas Gampefc25ae92019-04-19 22:22:57 -0700575 // Resolves a class based on an index and, if C is kYes, performs access checks to ensure
576 // the referrer can access the resolved class.
577 template <CheckAccess C>
578 const RegType& ResolveClass(dex::TypeIndex class_idx)
579 REQUIRES_SHARED(Locks::mutator_lock_);
580
581 /*
582 * For the "move-exception" instruction at "work_insn_idx_", which must be at an exception handler
583 * address, determine the Join of all exceptions that can land here. Fails if no matching
584 * exception handler can be found or if the Join of exception types fails.
585 */
586 const RegType& GetCaughtExceptionType()
587 REQUIRES_SHARED(Locks::mutator_lock_);
588
589 /*
590 * Resolves a method based on an index and performs access checks to ensure
591 * the referrer can access the resolved method.
592 * Does not throw exceptions.
593 */
594 ArtMethod* ResolveMethodAndCheckAccess(uint32_t method_idx, MethodType method_type)
595 REQUIRES_SHARED(Locks::mutator_lock_);
596
597 /*
598 * Verify the arguments to a method. We're executing in "method", making
599 * a call to the method reference in vB.
600 *
601 * If this is a "direct" invoke, we allow calls to <init>. For calls to
602 * <init>, the first argument may be an uninitialized reference. Otherwise,
603 * calls to anything starting with '<' will be rejected, as will any
604 * uninitialized reference arguments.
605 *
606 * For non-static method calls, this will verify that the method call is
607 * appropriate for the "this" argument.
608 *
609 * The method reference is in vBBBB. The "is_range" parameter determines
610 * whether we use 0-4 "args" values or a range of registers defined by
611 * vAA and vCCCC.
612 *
613 * Widening conversions on integers and references are allowed, but
614 * narrowing conversions are not.
615 *
616 * Returns the resolved method on success, null on failure (with *failure
617 * set appropriately).
618 */
619 ArtMethod* VerifyInvocationArgs(const Instruction* inst, MethodType method_type, bool is_range)
620 REQUIRES_SHARED(Locks::mutator_lock_);
621
622 // Similar checks to the above, but on the proto. Will be used when the method cannot be
623 // resolved.
624 void VerifyInvocationArgsUnresolvedMethod(const Instruction* inst, MethodType method_type,
625 bool is_range)
626 REQUIRES_SHARED(Locks::mutator_lock_);
627
628 template <class T>
629 ArtMethod* VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
630 MethodType method_type, bool is_range,
631 ArtMethod* res_method)
632 REQUIRES_SHARED(Locks::mutator_lock_);
633
634 /*
635 * Verify the arguments present for a call site. Returns "true" if all is well, "false" otherwise.
636 */
637 bool CheckCallSite(uint32_t call_site_idx);
638
639 /*
640 * Verify that the target instruction is not "move-exception". It's important that the only way
641 * to execute a move-exception is as the first instruction of an exception handler.
642 * Returns "true" if all is well, "false" if the target instruction is move-exception.
643 */
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700644 bool CheckNotMoveException(const uint16_t* insns, int insn_idx) {
645 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
646 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
647 return false;
648 }
649 return true;
650 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700651
652 /*
653 * Verify that the target instruction is not "move-result". It is important that we cannot
654 * branch to move-result instructions, but we have to make this a distinct check instead of
655 * adding it to CheckNotMoveException, because it is legal to continue into "move-result"
656 * instructions - as long as the previous instruction was an invoke, which is checked elsewhere.
657 */
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700658 bool CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
659 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
660 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
661 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
662 return false;
663 }
664 return true;
665 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700666
667 /*
668 * Verify that the target instruction is not "move-result" or "move-exception". This is to
669 * be used when checking branch and switch instructions, but not instructions that can
670 * continue.
671 */
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700672 bool CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
673 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
674 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700675
676 /*
677 * Control can transfer to "next_insn". Merge the registers from merge_line into the table at
678 * next_insn, and set the changed flag on the target address if any of the registers were changed.
679 * In the case of fall-through, update the merge line on a change as its the working line for the
680 * next instruction.
681 * Returns "false" if an error is encountered.
682 */
683 bool UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line, bool update_merge_line)
684 REQUIRES_SHARED(Locks::mutator_lock_);
685
686 // Return the register type for the method.
687 const RegType& GetMethodReturnType() REQUIRES_SHARED(Locks::mutator_lock_);
688
689 // Get a type representing the declaring class of the method.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700690 const RegType& GetDeclaringClass() REQUIRES_SHARED(Locks::mutator_lock_) {
691 if (declaring_class_ == nullptr) {
692 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
693 const char* descriptor
694 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
695 if (method_being_verified_ != nullptr) {
696 ObjPtr<mirror::Class> klass = method_being_verified_->GetDeclaringClass();
697 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
698 } else {
699 declaring_class_ = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
700 }
701 }
702 return *declaring_class_;
703 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700704
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700705 InstructionFlags* CurrentInsnFlags() {
706 return &GetModifiableInstructionFlags(work_insn_idx_);
707 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700708
709 const RegType& DetermineCat1Constant(int32_t value, bool precise)
710 REQUIRES_SHARED(Locks::mutator_lock_);
711
712 // Try to create a register type from the given class. In case a precise type is requested, but
713 // the class is not instantiable, a soft error (of type NO_CLASS) will be enqueued and a
714 // non-precise reference will be returned.
715 // Note: we reuse NO_CLASS as this will throw an exception at runtime, when the failing class is
716 // actually touched.
717 const RegType& FromClass(const char* descriptor, ObjPtr<mirror::Class> klass, bool precise)
Andreas Gampe86bf0682019-07-29 11:22:44 -0700718 REQUIRES_SHARED(Locks::mutator_lock_) {
719 DCHECK(klass != nullptr);
720 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
721 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
722 << "non-instantiable klass " << descriptor;
723 precise = false;
724 }
725 return reg_types_.FromClass(descriptor, klass, precise);
726 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700727
728 ALWAYS_INLINE bool FailOrAbort(bool condition, const char* error_msg, uint32_t work_insn_idx);
729
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700730 ALWAYS_INLINE InstructionFlags& GetModifiableInstructionFlags(size_t index) {
731 return insn_flags_[index];
732 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700733
734 // Returns the method index of an invoke instruction.
735 uint16_t GetMethodIdxOfInvoke(const Instruction* inst)
Andreas Gampe86bf0682019-07-29 11:22:44 -0700736 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray4924ea92021-03-23 08:25:31 +0000737 return inst->VRegB();
Andreas Gampe86bf0682019-07-29 11:22:44 -0700738 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700739 // Returns the field index of a field access instruction.
740 uint16_t GetFieldIdxOfFieldAccess(const Instruction* inst, bool is_static)
Andreas Gampe86bf0682019-07-29 11:22:44 -0700741 REQUIRES_SHARED(Locks::mutator_lock_) {
742 if (is_static) {
743 return inst->VRegB_21c();
Andreas Gampe86bf0682019-07-29 11:22:44 -0700744 } else {
745 return inst->VRegC_22c();
746 }
747 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700748
749 // Run verification on the method. Returns true if verification completes and false if the input
750 // has an irrecoverable corruption.
751 bool Verify() override REQUIRES_SHARED(Locks::mutator_lock_);
752
Nicolas Geoffray44dc8a32021-06-21 15:23:49 +0100753 // For app-compatibility, code after a runtime throw is treated as dead code
754 // for apps targeting <= S.
755 void PotentiallyMarkRuntimeThrow() override;
756
Andreas Gampefc25ae92019-04-19 22:22:57 -0700757 // Dump the failures encountered by the verifier.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700758 std::ostream& DumpFailures(std::ostream& os) {
759 DCHECK_EQ(failures_.size(), failure_messages_.size());
Andreas Gampe4ec4d482019-08-01 10:00:23 -0700760 for (const auto* stream : failure_messages_) {
761 os << stream->str() << "\n";
Andreas Gampe86bf0682019-07-29 11:22:44 -0700762 }
763 return os;
764 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700765
766 // Dump the state of the verifier, namely each instruction, what flags are set on it, register
767 // information
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700768 void Dump(std::ostream& os) REQUIRES_SHARED(Locks::mutator_lock_) {
769 VariableIndentationOutputStream vios(&os);
770 Dump(&vios);
771 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700772 void Dump(VariableIndentationOutputStream* vios) REQUIRES_SHARED(Locks::mutator_lock_);
773
Andreas Gampe6087bc22019-06-03 15:52:08 -0700774 bool HandleMoveException(const Instruction* inst) REQUIRES_SHARED(Locks::mutator_lock_);
775
Andreas Gampefc25ae92019-04-19 22:22:57 -0700776 ArtMethod* method_being_verified_; // Its ArtMethod representation if known.
777 const uint32_t method_access_flags_; // Method's access flags.
778 const RegType* return_type_; // Lazily computed return type of the method.
779 // The dex_cache for the declaring class of the method.
780 Handle<mirror::DexCache> dex_cache_ GUARDED_BY(Locks::mutator_lock_);
781 // The class loader for the declaring class of the method.
782 Handle<mirror::ClassLoader> class_loader_ GUARDED_BY(Locks::mutator_lock_);
Andreas Gampefc25ae92019-04-19 22:22:57 -0700783 const RegType* declaring_class_; // Lazily computed reg type of the method's declaring class.
784
785 // The dex PC of a FindLocksAtDexPc request, -1 otherwise.
786 uint32_t interesting_dex_pc_;
787 // The container into which FindLocksAtDexPc should write the registers containing held locks,
788 // null if we're not doing FindLocksAtDexPc.
789 std::vector<DexLockInfo>* monitor_enter_dex_pcs_;
790
791
792 // An optimization where instead of generating unique RegTypes for constants we use imprecise
793 // constants that cover a range of constants. This isn't good enough for deoptimization that
794 // avoids loading from registers in the case of a constant as the dex instruction set lost the
795 // notion of whether a value should be in a floating point or general purpose register file.
796 const bool need_precise_constants_;
797
798 // Indicates whether we verify to dump the info. In that case we accept quickened instructions
799 // even though we might detect to be a compiler. Should only be set when running
800 // VerifyMethodAndDump.
801 const bool verify_to_dump_;
802
803 // Whether or not we call AllowThreadSuspension periodically, we want a way to disable this for
804 // thread dumping checkpoints since we may get thread suspension at an inopportune time due to
805 // FindLocksAtDexPC, resulting in deadlocks.
806 const bool allow_thread_suspension_;
807
808 // Whether the method seems to be a constructor. Note that this field exists as we can't trust
809 // the flags in the dex file. Some older code does not mark methods named "<init>" and "<clinit>"
810 // correctly.
811 //
812 // Note: this flag is only valid once Verify() has started.
813 bool is_constructor_;
814
Alex Lighte2ddce32019-05-22 17:08:35 +0000815 // Whether to attempt to fill all register lines for (ex) debugger use.
816 bool fill_register_lines_;
817
Andreas Gampefc25ae92019-04-19 22:22:57 -0700818 // API level, for dependent checks. Note: we do not use '0' for unset here, to simplify checks.
819 // Instead, unset level should correspond to max().
820 const uint32_t api_level_;
821
822 friend class ::art::verifier::MethodVerifier;
823
824 DISALLOW_COPY_AND_ASSIGN(MethodVerifier);
825};
826
Andreas Gampe7c038102014-10-27 20:08:46 -0700827// Note: returns true on failure.
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700828template <bool kVerifierDebug>
829inline bool MethodVerifier<kVerifierDebug>::FailOrAbort(bool condition,
830 const char* error_msg,
831 uint32_t work_insn_idx) {
Andreas Gampe7c038102014-10-27 20:08:46 -0700832 if (kIsDebugBuild) {
Andreas Gampef8f36c22016-11-19 14:51:52 -0800833 // In a debug build, abort if the error condition is wrong. Only warn if
834 // we are already aborting (as this verification is likely run to print
835 // lock information).
836 if (LIKELY(gAborting == 0)) {
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700837 DCHECK(condition) << error_msg << work_insn_idx << " "
838 << dex_file_->PrettyMethod(dex_method_idx_);
Andreas Gampef8f36c22016-11-19 14:51:52 -0800839 } else {
840 if (!condition) {
841 LOG(ERROR) << error_msg << work_insn_idx;
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700842 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
Andreas Gampef8f36c22016-11-19 14:51:52 -0800843 return true;
844 }
845 }
Andreas Gampe7c038102014-10-27 20:08:46 -0700846 } else {
847 // In a non-debug build, just fail the class.
848 if (!condition) {
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700849 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
Andreas Gampe7c038102014-10-27 20:08:46 -0700850 return true;
851 }
852 }
853
854 return false;
855}
856
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800857static bool IsLargeMethod(const CodeItemDataAccessor& accessor) {
858 if (!accessor.HasCodeItem()) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700859 return false;
860 }
861
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800862 uint16_t registers_size = accessor.RegistersSize();
863 uint32_t insns_size = accessor.InsnsSizeInCodeUnits();
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700864
865 return registers_size * insns_size > 4*1024*1024;
866}
867
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700868template <bool kVerifierDebug>
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700869void MethodVerifier<kVerifierDebug>::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700870 CHECK(monitor_enter_dex_pcs_ != nullptr);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800871 CHECK(code_item_accessor_.HasCodeItem()); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700872
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800873 // Quick check whether there are any monitor_enter instructions before verifying.
874 for (const DexInstructionPcPair& inst : code_item_accessor_) {
875 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
876 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
877 // verification. In practice, the phase we want relies on data structures set up by all the
878 // earlier passes, so we just run the full method verification and bail out early when we've
879 // got what we wanted.
880 Verify();
881 return;
882 }
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700883 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700884}
885
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700886template <bool kVerifierDebug>
887bool MethodVerifier<kVerifierDebug>::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700888 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
889 // the name.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800890 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Andreas Gampee6215c02015-08-31 18:54:38 -0700891 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
892 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
893 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
894 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
895 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
896 if ((method_access_flags_ & kAccConstructor) != 0) {
897 if (!constructor_by_name) {
898 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
899 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700900 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700901 }
902 is_constructor_ = true;
903 } else if (constructor_by_name) {
David Sehr709b0702016-10-13 09:12:37 -0700904 LOG(WARNING) << "Method " << dex_file_->PrettyMethod(dex_method_idx_)
Andreas Gampee6215c02015-08-31 18:54:38 -0700905 << " not marked as constructor.";
906 is_constructor_ = true;
907 }
908 // If it's a constructor, check whether IsStatic() matches the name.
909 // This should have been rejected by the dex file verifier. Only do in debug build.
910 if (kIsDebugBuild) {
911 if (IsConstructor()) {
912 if (IsStatic() ^ static_constructor_by_name) {
913 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
914 << "constructor name doesn't match static flag";
915 return false;
916 }
jeffhaobdb76512011-09-07 11:43:16 -0700917 }
jeffhaobdb76512011-09-07 11:43:16 -0700918 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700919
920 // Methods may only have one of public/protected/private.
921 // This should have been rejected by the dex file verifier. Only do in debug build.
922 if (kIsDebugBuild) {
923 size_t access_mod_count =
924 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
925 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
926 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
927 if (access_mod_count > 1) {
928 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
929 return false;
930 }
931 }
932
933 // If there aren't any instructions, make sure that's expected, then exit successfully.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800934 if (!code_item_accessor_.HasCodeItem()) {
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700935 // Only native or abstract methods may not have code.
936 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
937 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
938 return false;
939 }
940
Nicolas Geoffray5e82b182020-12-08 08:57:36 +0000941 // Test FastNative and CriticalNative annotations. We do this in the
942 // verifier for convenience.
943 if ((method_access_flags_ & kAccNative) != 0) {
944 // Fetch the flags from the annotations: the class linker hasn't processed
945 // them yet.
946 uint32_t native_access_flags = annotations::GetNativeMethodAnnotationAccessFlags(
947 *dex_file_, class_def_, dex_method_idx_);
948 if ((native_access_flags & kAccFastNative) != 0) {
949 if ((method_access_flags_ & kAccSynchronized) != 0) {
950 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "fast native methods cannot be synchronized";
951 return false;
952 }
953 }
954 if ((native_access_flags & kAccCriticalNative) != 0) {
955 if ((method_access_flags_ & kAccSynchronized) != 0) {
956 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "critical native methods cannot be synchronized";
957 return false;
958 }
959 if ((method_access_flags_ & kAccStatic) == 0) {
960 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "critical native methods must be static";
961 return false;
962 }
963 const char* shorty = dex_file_->GetMethodShorty(method_id);
964 for (size_t i = 0, len = strlen(shorty); i < len; ++i) {
965 if (Primitive::GetType(shorty[i]) == Primitive::kPrimNot) {
966 Fail(VERIFY_ERROR_BAD_CLASS_HARD) <<
967 "critical native methods must not have references as arguments or return type";
968 return false;
969 }
970 }
971 }
972 }
973
Andreas Gampee6215c02015-08-31 18:54:38 -0700974 // This should have been rejected by the dex file verifier. Only do in debug build.
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700975 // Note: the above will also be rejected in the dex file verifier, starting in dex version 37.
Andreas Gampee6215c02015-08-31 18:54:38 -0700976 if (kIsDebugBuild) {
Andreas Gampee6215c02015-08-31 18:54:38 -0700977 if ((method_access_flags_ & kAccAbstract) != 0) {
978 // Abstract methods are not allowed to have the following flags.
979 static constexpr uint32_t kForbidden =
980 kAccPrivate |
981 kAccStatic |
982 kAccFinal |
983 kAccNative |
984 kAccStrict |
985 kAccSynchronized;
986 if ((method_access_flags_ & kForbidden) != 0) {
987 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
988 << "method can't be abstract and private/static/final/native/strict/synchronized";
989 return false;
990 }
991 }
David Brazdil15fc7292016-09-02 14:13:18 +0100992 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700993 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000994 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700995 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000996 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700997 return false;
998 }
999 // In addition to the above, interface methods must not be protected.
1000 static constexpr uint32_t kForbidden = kAccProtected;
1001 if ((method_access_flags_ & kForbidden) != 0) {
1002 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
1003 return false;
1004 }
1005 }
1006 // We also don't allow constructors to be abstract or native.
1007 if (IsConstructor()) {
1008 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
1009 return false;
1010 }
1011 }
1012 return true;
1013 }
1014
1015 // This should have been rejected by the dex file verifier. Only do in debug build.
1016 if (kIsDebugBuild) {
1017 // When there's code, the method must not be native or abstract.
1018 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
1019 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
1020 return false;
1021 }
1022
David Brazdil15fc7292016-09-02 14:13:18 +01001023 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -07001024 // Interfaces may always have static initializers for their fields. If we are running with
1025 // default methods enabled we also allow other public, static, non-final methods to have code.
1026 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -07001027 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +00001028 if (IsInstanceConstructor()) {
1029 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
1030 return false;
1031 } else if (method_access_flags_ & kAccFinal) {
1032 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
1033 return false;
Alex Lightb55f1ac2016-04-12 15:50:55 -07001034 } else {
1035 uint32_t access_flag_options = kAccPublic;
Mathieu Chartierf6e31472017-12-28 13:32:08 -08001036 if (dex_file_->SupportsDefaultMethods()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07001037 access_flag_options |= kAccPrivate;
1038 }
1039 if (!(method_access_flags_ & access_flag_options)) {
1040 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1041 << "interfaces may not have protected or package-private members";
1042 return false;
1043 }
Alex Lighteb7c1442015-08-31 13:17:42 -07001044 }
Andreas Gampee6215c02015-08-31 18:54:38 -07001045 }
1046 }
1047
1048 // Instance constructors must not be synchronized.
1049 if (IsInstanceConstructor()) {
1050 static constexpr uint32_t kForbidden = kAccSynchronized;
1051 if ((method_access_flags_ & kForbidden) != 0) {
1052 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
1053 return false;
1054 }
1055 }
1056 }
1057
David Srbecky346fd962020-07-27 16:51:00 +01001058 // Consistency-check of the register counts.
1059 // ins + locals = registers, so make sure that ins <= registers.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001060 if (code_item_accessor_.InsSize() > code_item_accessor_.RegistersSize()) {
1061 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins="
1062 << code_item_accessor_.InsSize()
1063 << " regs=" << code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -07001064 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001065 }
Alex Lighteb7c1442015-08-31 13:17:42 -07001066
Ian Rogersd81871c2011-10-03 13:57:23 -07001067 // Allocate and initialize an array to hold instruction data.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001068 insn_flags_.reset(allocator_.AllocArray<InstructionFlags>(
1069 code_item_accessor_.InsnsSizeInCodeUnits()));
Mathieu Chartierde40d472015-10-15 17:47:48 -07001070 DCHECK(insn_flags_ != nullptr);
1071 std::uninitialized_fill_n(insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001072 code_item_accessor_.InsnsSizeInCodeUnits(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07001073 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -07001074 // Run through the instructions and see if the width checks out.
1075 bool result = ComputeWidthsAndCountOps();
Andreas Gampefef91cc2019-07-25 14:13:23 -07001076 bool allow_runtime_only_instructions = !IsAotMode() || verify_to_dump_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001077 // Flag instructions guarded by a "try" block and check exception handlers.
1078 result = result && ScanTryCatchBlocks();
1079 // Perform static instruction verification.
Andreas Gampebf1cb772017-05-15 15:39:00 -07001080 result = result && (allow_runtime_only_instructions
1081 ? VerifyInstructions<true>()
1082 : VerifyInstructions<false>());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001083 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +00001084 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -08001085
Vladimir Marko2b5eaa22013-12-13 13:59:30 +00001086 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -07001087}
1088
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001089template <bool kVerifierDebug>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001090bool MethodVerifier<kVerifierDebug>::ComputeWidthsAndCountOps() {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -07001091 // We can't assume the instruction is well formed, handle the case where calculating the size
1092 // goes past the end of the code item.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001093 SafeDexInstructionIterator it(code_item_accessor_.begin(), code_item_accessor_.end());
1094 for ( ; !it.IsErrorState() && it < code_item_accessor_.end(); ++it) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -07001095 // In case the instruction goes past the end of the code item, make sure to not process it.
1096 SafeDexInstructionIterator next = it;
1097 ++next;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001098 if (next.IsErrorState()) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -07001099 break;
1100 }
Andreas Gampe51de69e2019-04-19 15:14:14 -07001101 GetModifiableInstructionFlags(it.DexPc()).SetIsOpcode();
jeffhaobdb76512011-09-07 11:43:16 -07001102 }
1103
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001104 if (it != code_item_accessor_.end()) {
1105 const size_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaod5347e02012-03-22 17:25:05 -07001106 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001107 << it.DexPc() << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001108 return false;
1109 }
Andreas Gampe147a9112019-07-31 16:17:10 -07001110 DCHECK(GetInstructionFlags(0).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07001111
jeffhaobdb76512011-09-07 11:43:16 -07001112 return true;
1113}
1114
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001115template <bool kVerifierDebug>
1116bool MethodVerifier<kVerifierDebug>::ScanTryCatchBlocks() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001117 const uint32_t tries_size = code_item_accessor_.TriesSize();
jeffhaobdb76512011-09-07 11:43:16 -07001118 if (tries_size == 0) {
1119 return true;
1120 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001121 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001122 for (const dex::TryItem& try_item : code_item_accessor_.TryItems()) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001123 const uint32_t start = try_item.start_addr_;
1124 const uint32_t end = start + try_item.insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001125 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001126 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
1127 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001128 return false;
1129 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001130 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001131 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1132 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001133 return false;
1134 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001135 DexInstructionIterator end_it(code_item_accessor_.Insns(), end);
1136 for (DexInstructionIterator it(code_item_accessor_.Insns(), start); it < end_it; ++it) {
Andreas Gampe51de69e2019-04-19 15:14:14 -07001137 GetModifiableInstructionFlags(it.DexPc()).SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -07001138 }
1139 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001140 // Iterate over each of the handlers to verify target addresses.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001141 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
1142 const uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Andreas Gampee0bbab92019-07-25 12:28:22 -07001143 ClassLinker* linker = GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001144 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001145 CatchHandlerIterator iterator(handlers_ptr);
1146 for (; iterator.HasNext(); iterator.Next()) {
Igor Murashkin2ffb7032017-11-08 13:35:21 -08001147 uint32_t dex_pc = iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -07001148 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001149 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1150 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001151 return false;
1152 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001153 if (!CheckNotMoveResult(code_item_accessor_.Insns(), dex_pc)) {
Stephen Kyle9bc61992014-09-22 13:53:15 +01001154 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1155 << "exception handler begins with move-result* (" << dex_pc << ")";
1156 return false;
1157 }
Andreas Gampe51de69e2019-04-19 15:14:14 -07001158 GetModifiableInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001159 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1160 // unresolved exception types will be ignored by exception delivery
Andreas Gampea5b09a62016-11-17 15:21:22 -08001161 if (iterator.GetHandlerTypeIndex().IsValid()) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001162 ObjPtr<mirror::Class> exception_type =
1163 linker->ResolveType(iterator.GetHandlerTypeIndex(), dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07001164 if (exception_type == nullptr) {
1165 DCHECK(self_->IsExceptionPending());
1166 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001167 }
1168 }
jeffhaobdb76512011-09-07 11:43:16 -07001169 }
Ian Rogers0571d352011-11-03 19:51:38 -07001170 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001171 }
jeffhaobdb76512011-09-07 11:43:16 -07001172 return true;
1173}
1174
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001175template <bool kVerifierDebug>
Andreas Gampebf1cb772017-05-15 15:39:00 -07001176template <bool kAllowRuntimeOnlyInstructions>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001177bool MethodVerifier<kVerifierDebug>::VerifyInstructions() {
Andreas Gampe93adcb52019-06-06 20:16:07 -07001178 // Flag the start of the method as a branch target.
Andreas Gampe51de69e2019-04-19 15:14:14 -07001179 GetModifiableInstructionFlags(0).SetBranchTarget();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001180 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001181 const uint32_t dex_pc = inst.DexPc();
1182 if (!VerifyInstruction<kAllowRuntimeOnlyInstructions>(&inst.Inst(), dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001183 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001184 return false;
1185 }
Andreas Gampe93adcb52019-06-06 20:16:07 -07001186 // Flag some interesting instructions.
1187 if (inst->IsReturn()) {
1188 GetModifiableInstructionFlags(dex_pc).SetReturn();
1189 } else if (inst->Opcode() == Instruction::CHECK_CAST) {
1190 // The dex-to-dex compiler wants type information to elide check-casts.
Andreas Gampe51de69e2019-04-19 15:14:14 -07001191 GetModifiableInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -07001192 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001193 }
1194 return true;
1195}
1196
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001197template <bool kVerifierDebug>
Andreas Gampebf1cb772017-05-15 15:39:00 -07001198template <bool kAllowRuntimeOnlyInstructions>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001199bool MethodVerifier<kVerifierDebug>::VerifyInstruction(const Instruction* inst,
1200 uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001201 bool result = true;
1202 switch (inst->GetVerifyTypeArgumentA()) {
1203 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -07001204 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001205 break;
1206 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001207 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001208 break;
1209 }
1210 switch (inst->GetVerifyTypeArgumentB()) {
1211 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -07001212 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001213 break;
1214 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001215 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001216 break;
1217 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001218 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001219 break;
1220 case Instruction::kVerifyRegBNewInstance:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001221 result = result && CheckNewInstance(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001222 break;
1223 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001224 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001225 break;
1226 case Instruction::kVerifyRegBType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001227 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001228 break;
1229 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001230 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001231 break;
Orion Hodson2e599942017-09-22 16:17:41 +01001232 case Instruction::kVerifyRegBCallSite:
1233 result = result && CheckCallSiteIndex(inst->VRegB());
1234 break;
1235 case Instruction::kVerifyRegBMethodHandle:
1236 result = result && CheckMethodHandleIndex(inst->VRegB());
1237 break;
1238 case Instruction::kVerifyRegBPrototype:
1239 result = result && CheckPrototypeIndex(inst->VRegB());
1240 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001241 }
1242 switch (inst->GetVerifyTypeArgumentC()) {
1243 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001244 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001245 break;
1246 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001247 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001248 break;
1249 case Instruction::kVerifyRegCNewArray:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001250 result = result && CheckNewArray(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001251 break;
1252 case Instruction::kVerifyRegCType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001253 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001254 break;
1255 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001256 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001257 break;
1258 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001259 switch (inst->GetVerifyTypeArgumentH()) {
1260 case Instruction::kVerifyRegHPrototype:
1261 result = result && CheckPrototypeIndex(inst->VRegH());
1262 break;
1263 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001264 switch (inst->GetVerifyExtraFlags()) {
1265 case Instruction::kVerifyArrayData:
1266 result = result && CheckArrayData(code_offset);
1267 break;
1268 case Instruction::kVerifyBranchTarget:
1269 result = result && CheckBranchTarget(code_offset);
1270 break;
1271 case Instruction::kVerifySwitchTargets:
1272 result = result && CheckSwitchTargets(code_offset);
1273 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001274 case Instruction::kVerifyVarArgNonZero:
1275 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001276 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001277 // Instructions that can actually return a negative value shouldn't have this flag.
1278 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1279 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1280 v_a > Instruction::kMaxVarArgRegs) {
1281 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001282 "non-range invoke";
1283 return false;
1284 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001285
Ian Rogers29a26482014-05-02 15:27:29 -07001286 uint32_t args[Instruction::kMaxVarArgRegs];
1287 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001288 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001289 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001290 }
Andreas Gampec3314312014-06-19 18:13:29 -07001291 case Instruction::kVerifyVarArgRangeNonZero:
1292 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001293 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001294 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1295 inst->VRegA() <= 0) {
1296 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1297 "range invoke";
1298 return false;
1299 }
Ian Rogers29a26482014-05-02 15:27:29 -07001300 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001301 break;
1302 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001303 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001304 result = false;
1305 break;
1306 }
Andreas Gampebf1cb772017-05-15 15:39:00 -07001307 if (!kAllowRuntimeOnlyInstructions && inst->GetVerifyIsRuntimeOnly()) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001308 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1309 result = false;
1310 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001311 return result;
1312}
1313
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001314template <bool kVerifierDebug>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001315inline bool MethodVerifier<kVerifierDebug>::CheckNewInstance(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001316 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001317 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001318 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001319 return false;
1320 }
1321 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001322 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Andreas Gampe29e81422017-05-15 16:29:32 -07001323 if (UNLIKELY(descriptor[0] != 'L')) {
jeffhaod5347e02012-03-22 17:25:05 -07001324 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001325 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001326 } else if (UNLIKELY(strcmp(descriptor, "Ljava/lang/Class;") == 0)) {
Aart Bikdb698f12016-07-25 17:52:22 -07001327 // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an
1328 // exception is thrown when this statement is executed (compiled code would not do that).
1329 Fail(VERIFY_ERROR_INSTANTIATION);
Ian Rogersd81871c2011-10-03 13:57:23 -07001330 }
1331 return true;
1332}
1333
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001334template <bool kVerifierDebug>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001335bool MethodVerifier<kVerifierDebug>::CheckNewArray(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001336 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001337 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001338 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001339 return false;
1340 }
1341 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001342 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001343 const char* cp = descriptor;
1344 while (*cp++ == '[') {
1345 bracket_count++;
1346 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001347 if (UNLIKELY(bracket_count == 0)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001348 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001349 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1350 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001351 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001352 } else if (UNLIKELY(bracket_count > 255)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001353 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001354 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1355 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001356 return false;
1357 }
1358 return true;
1359}
1360
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001361template <bool kVerifierDebug>
1362bool MethodVerifier<kVerifierDebug>::CheckArrayData(uint32_t cur_offset) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001363 const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
1364 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001365 const uint16_t* array_data;
1366 int32_t array_data_offset;
1367
1368 DCHECK_LT(cur_offset, insn_count);
1369 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001370 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001371 if (UNLIKELY(static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
1372 cur_offset + array_data_offset + 2 >= insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001373 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001374 << ", data offset " << array_data_offset
1375 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001376 return false;
1377 }
1378 /* offset to array data table is a relative branch-style offset */
1379 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001380 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001381 if (UNLIKELY(!IsAligned<4>(array_data))) {
jeffhaod5347e02012-03-22 17:25:05 -07001382 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1383 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001384 return false;
1385 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001386 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1387 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001388 if (UNLIKELY(!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001389 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1390 << ", data offset " << array_data_offset
1391 << " not correctly visited, probably bad padding.";
1392 return false;
1393 }
1394
Ian Rogersd81871c2011-10-03 13:57:23 -07001395 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001396 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001397 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1398 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001399 if (UNLIKELY(cur_offset + array_data_offset + table_size > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001400 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1401 << ", data offset " << array_data_offset << ", end "
1402 << cur_offset + array_data_offset + table_size
1403 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001404 return false;
1405 }
1406 return true;
1407}
1408
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001409template <bool kVerifierDebug>
1410bool MethodVerifier<kVerifierDebug>::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001411 int32_t offset;
1412 bool isConditional, selfOkay;
1413 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1414 return false;
1415 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001416 if (UNLIKELY(!selfOkay && offset == 0)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001417 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1418 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001419 return false;
1420 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001421 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1422 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Andreas Gampe29e81422017-05-15 16:29:32 -07001423 if (UNLIKELY(((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset))) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001424 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1425 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001426 return false;
1427 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001428 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001429 if (UNLIKELY(abs_offset < 0 ||
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001430 (uint32_t) abs_offset >= code_item_accessor_.InsnsSizeInCodeUnits() ||
Andreas Gampe29e81422017-05-15 16:29:32 -07001431 !GetInstructionFlags(abs_offset).IsOpcode())) {
jeffhaod5347e02012-03-22 17:25:05 -07001432 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001433 << reinterpret_cast<void*>(abs_offset) << ") at "
1434 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001435 return false;
1436 }
Andreas Gampe51de69e2019-04-19 15:14:14 -07001437 GetModifiableInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001438 return true;
1439}
1440
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001441template <bool kVerifierDebug>
1442bool MethodVerifier<kVerifierDebug>::GetBranchOffset(uint32_t cur_offset,
1443 int32_t* pOffset,
1444 bool* pConditional,
1445 bool* selfOkay) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001446 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001447 *pConditional = false;
1448 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001449 switch (*insns & 0xff) {
1450 case Instruction::GOTO:
1451 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001452 break;
1453 case Instruction::GOTO_32:
1454 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001455 *selfOkay = true;
1456 break;
1457 case Instruction::GOTO_16:
1458 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001459 break;
1460 case Instruction::IF_EQ:
1461 case Instruction::IF_NE:
1462 case Instruction::IF_LT:
1463 case Instruction::IF_GE:
1464 case Instruction::IF_GT:
1465 case Instruction::IF_LE:
1466 case Instruction::IF_EQZ:
1467 case Instruction::IF_NEZ:
1468 case Instruction::IF_LTZ:
1469 case Instruction::IF_GEZ:
1470 case Instruction::IF_GTZ:
1471 case Instruction::IF_LEZ:
1472 *pOffset = (int16_t) insns[1];
1473 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001474 break;
1475 default:
1476 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001477 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001478 return true;
1479}
1480
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001481template <bool kVerifierDebug>
1482bool MethodVerifier<kVerifierDebug>::CheckSwitchTargets(uint32_t cur_offset) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001483 const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001484 DCHECK_LT(cur_offset, insn_count);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001485 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001486 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001487 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001488 if (UNLIKELY(static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1489 cur_offset + switch_offset + 2 > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001490 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001491 << ", switch offset " << switch_offset
1492 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001493 return false;
1494 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001495 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001496 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001497 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001498 if (UNLIKELY(!IsAligned<4>(switch_insns))) {
jeffhaod5347e02012-03-22 17:25:05 -07001499 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1500 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001501 return false;
1502 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001503 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1504 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001505 if (UNLIKELY(!GetInstructionFlags(cur_offset + switch_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001506 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1507 << ", switch offset " << switch_offset
1508 << " not correctly visited, probably bad padding.";
1509 return false;
1510 }
1511
David Brazdil5469d342015-09-25 16:57:53 +01001512 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1513
Ian Rogersd81871c2011-10-03 13:57:23 -07001514 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001515 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001516 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001517 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001518 /* 0=sig, 1=count, 2/3=firstKey */
1519 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001520 expected_signature = Instruction::kPackedSwitchSignature;
1521 } else {
1522 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001523 targets_offset = 2 + 2 * switch_count;
1524 expected_signature = Instruction::kSparseSwitchSignature;
1525 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001526 uint32_t table_size = targets_offset + switch_count * 2;
Andreas Gampe29e81422017-05-15 16:29:32 -07001527 if (UNLIKELY(switch_insns[0] != expected_signature)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001528 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1529 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1530 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001531 return false;
1532 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001533 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001534 if (UNLIKELY(cur_offset + switch_offset + table_size > (uint32_t) insn_count)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001535 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1536 << ", switch offset " << switch_offset
1537 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001538 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001539 return false;
1540 }
David Brazdil5469d342015-09-25 16:57:53 +01001541
1542 constexpr int32_t keys_offset = 2;
1543 if (switch_count > 1) {
1544 if (is_packed_switch) {
1545 /* for a packed switch, verify that keys do not overflow int32 */
1546 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1547 int32_t max_first_key =
1548 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
Andreas Gampe29e81422017-05-15 16:29:32 -07001549 if (UNLIKELY(first_key > max_first_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001550 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1551 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001552 return false;
1553 }
David Brazdil5469d342015-09-25 16:57:53 +01001554 } else {
1555 /* for a sparse switch, verify the keys are in ascending order */
1556 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1557 for (uint32_t targ = 1; targ < switch_count; targ++) {
1558 int32_t key =
1559 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1560 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001561 if (UNLIKELY(key <= last_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001562 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1563 << ", this=" << key;
1564 return false;
1565 }
1566 last_key = key;
1567 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001568 }
1569 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001570 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001571 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001572 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1573 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001574 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001575 if (UNLIKELY(abs_offset < 0 ||
1576 abs_offset >= static_cast<int32_t>(insn_count) ||
1577 !GetInstructionFlags(abs_offset).IsOpcode())) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001578 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1579 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1580 << reinterpret_cast<void*>(cur_offset)
1581 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001582 return false;
1583 }
Andreas Gampe51de69e2019-04-19 15:14:14 -07001584 GetModifiableInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001585 }
1586 return true;
1587}
1588
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001589template <bool kVerifierDebug>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001590bool MethodVerifier<kVerifierDebug>::VerifyCodeFlow() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001591 const uint16_t registers_size = code_item_accessor_.RegistersSize();
jeffhaobdb76512011-09-07 11:43:16 -07001592
Ian Rogersd81871c2011-10-03 13:57:23 -07001593 /* Create and initialize table holding register status */
Andreas Gampefef91cc2019-07-25 14:13:23 -07001594 RegisterTrackingMode base_mode = IsAotMode()
Andreas Gampe93adcb52019-06-06 20:16:07 -07001595 ? kTrackCompilerInterestPoints
1596 : kTrackRegsBranches;
1597 reg_table_.Init(fill_register_lines_ ? kTrackRegsAll : base_mode,
Brian Carlstrom93c33962013-07-26 10:37:43 -07001598 insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001599 code_item_accessor_.InsnsSizeInCodeUnits(),
Brian Carlstrom93c33962013-07-26 10:37:43 -07001600 registers_size,
Andreas Gamped09c0592019-04-19 15:44:05 -07001601 allocator_,
1602 GetRegTypeCache());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001603
Andreas Gamped09c0592019-04-19 15:44:05 -07001604 work_line_.reset(RegisterLine::Create(registers_size, allocator_, GetRegTypeCache()));
1605 saved_line_.reset(RegisterLine::Create(registers_size, allocator_, GetRegTypeCache()));
jeffhaobdb76512011-09-07 11:43:16 -07001606
Ian Rogersd81871c2011-10-03 13:57:23 -07001607 /* Initialize register types of method arguments. */
1608 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001609 DCHECK_NE(failures_.size(), 0U);
1610 std::string prepend("Bad signature in ");
David Sehr709b0702016-10-13 09:12:37 -07001611 prepend += dex_file_->PrettyMethod(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001612 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001613 return false;
1614 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001615 // We may have a runtime failure here, clear.
Andreas Gampe43884b22019-06-27 14:05:52 -07001616 flags_.have_pending_runtime_throw_failure_ = false;
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001617
Ian Rogersd81871c2011-10-03 13:57:23 -07001618 /* Perform code flow verification. */
Andreas Gampeb90cad52019-07-31 16:15:41 -07001619 bool res = LIKELY(monitor_enter_dex_pcs_ == nullptr)
1620 ? CodeFlowVerifyMethod</*kMonitorDexPCs=*/ false>()
1621 : CodeFlowVerifyMethod</*kMonitorDexPCs=*/ true>();
1622 if (UNLIKELY(!res)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001623 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001624 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001625 }
jeffhaobdb76512011-09-07 11:43:16 -07001626 return true;
1627}
1628
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001629template <bool kVerifierDebug>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001630void MethodVerifier<kVerifierDebug>::Dump(VariableIndentationOutputStream* vios) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001631 if (!code_item_accessor_.HasCodeItem()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001632 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001633 return;
jeffhaobdb76512011-09-07 11:43:16 -07001634 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001635 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001636 vios->Stream() << "Register Types:\n";
1637 ScopedIndentation indent1(vios);
1638 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001639 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001640 vios->Stream() << "Dumping instructions and register lines:\n";
1641 ScopedIndentation indent1(vios);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001642
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001643 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001644 const size_t dex_pc = inst.DexPc();
Andreas Gampe077d9db2018-01-19 18:54:14 -08001645
1646 // Might be asked to dump before the table is initialized.
1647 if (reg_table_.IsInitialized()) {
1648 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1649 if (reg_line != nullptr) {
1650 vios->Stream() << reg_line->Dump(this) << "\n";
1651 }
jeffhaobdb76512011-09-07 11:43:16 -07001652 }
Andreas Gampe077d9db2018-01-19 18:54:14 -08001653
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001654 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001655 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001656 const bool kDumpHexOfInstruction = false;
1657 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001658 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001659 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001660 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001661 }
jeffhaobdb76512011-09-07 11:43:16 -07001662}
1663
Ian Rogersd81871c2011-10-03 13:57:23 -07001664static bool IsPrimitiveDescriptor(char descriptor) {
1665 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001666 case 'I':
1667 case 'C':
1668 case 'S':
1669 case 'B':
1670 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001671 case 'F':
1672 case 'D':
1673 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001674 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001675 default:
1676 return false;
1677 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001678}
1679
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001680template <bool kVerifierDebug>
1681bool MethodVerifier<kVerifierDebug>::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001682 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001683
1684 // Should have been verified earlier.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001685 DCHECK_GE(code_item_accessor_.RegistersSize(), code_item_accessor_.InsSize());
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001686
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001687 uint32_t arg_start = code_item_accessor_.RegistersSize() - code_item_accessor_.InsSize();
1688 size_t expected_args = code_item_accessor_.InsSize(); /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001689
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001690 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001691 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001692 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001693 if (expected_args == 0) {
1694 // Expect at least a receiver.
1695 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1696 return false;
1697 }
1698
Ian Rogersd81871c2011-10-03 13:57:23 -07001699 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1700 // argument as uninitialized. This restricts field access until the superclass constructor is
1701 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001702 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001703 if (IsConstructor()) {
1704 if (declaring_class.IsJavaLangObject()) {
1705 // "this" is implicitly initialized.
1706 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001707 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001708 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001709 reg_line->SetRegisterType<LockOp::kClear>(
1710 this,
1711 arg_start + cur_arg,
1712 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001713 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001714 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001715 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001716 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001717 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001718 }
1719
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001720 const dex::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001721 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001722 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001723
1724 for (; iterator.HasNext(); iterator.Next()) {
1725 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001726 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001727 LOG(FATAL) << "Null descriptor";
1728 }
1729 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001730 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1731 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001732 return false;
1733 }
1734 switch (descriptor[0]) {
1735 case 'L':
1736 case '[':
1737 // We assume that reference arguments are initialized. The only way it could be otherwise
1738 // (assuming the caller was verified) is if the current method is <init>, but in that case
1739 // it's effectively considered initialized the instant we reach here (in the sense that we
1740 // can return without doing anything or call virtual methods).
1741 {
Andreas Gampe98be1a92017-08-28 08:25:45 -07001742 // Note: don't check access. No error would be thrown for declaring or passing an
1743 // inaccessible class. Only actual accesses to fields or methods will.
1744 const RegType& reg_type = ResolveClass<CheckAccess::kNo>(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001745 if (!reg_type.IsNonZeroReferenceTypes()) {
1746 DCHECK(HasFailures());
1747 return false;
1748 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001749 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001750 }
1751 break;
1752 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001753 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001754 break;
1755 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001756 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001757 break;
1758 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001759 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001760 break;
1761 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001762 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001763 break;
1764 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001765 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001766 break;
1767 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001768 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001769 break;
1770 case 'J':
1771 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001772 if (cur_arg + 1 >= expected_args) {
1773 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1774 << " args, found more (" << descriptor << ")";
1775 return false;
1776 }
1777
Ian Rogers7b078e82014-09-10 14:44:24 -07001778 const RegType* lo_half;
1779 const RegType* hi_half;
1780 if (descriptor[0] == 'J') {
1781 lo_half = &reg_types_.LongLo();
1782 hi_half = &reg_types_.LongHi();
1783 } else {
1784 lo_half = &reg_types_.DoubleLo();
1785 hi_half = &reg_types_.DoubleHi();
1786 }
1787 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001788 cur_arg++;
1789 break;
1790 }
1791 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001792 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1793 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001794 return false;
1795 }
1796 cur_arg++;
1797 }
1798 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001799 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1800 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001801 return false;
1802 }
1803 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1804 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1805 // format. Only major difference from the method argument format is that 'V' is supported.
1806 bool result;
1807 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1808 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001809 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001810 size_t i = 0;
1811 do {
1812 i++;
1813 } while (descriptor[i] == '['); // process leading [
1814 if (descriptor[i] == 'L') { // object array
1815 do {
1816 i++; // find closing ;
1817 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1818 result = descriptor[i] == ';';
1819 } else { // primitive array
1820 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1821 }
1822 } else if (descriptor[0] == 'L') {
1823 // could be more thorough here, but shouldn't be required
1824 size_t i = 0;
1825 do {
1826 i++;
1827 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1828 result = descriptor[i] == ';';
1829 } else {
1830 result = false;
1831 }
1832 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001833 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1834 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001835 }
1836 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001837}
1838
Andreas Gampeb90cad52019-07-31 16:15:41 -07001839COLD_ATTR
1840void HandleMonitorDexPcsWorkLine(
1841 std::vector<::art::verifier::MethodVerifier::DexLockInfo>* monitor_enter_dex_pcs,
1842 RegisterLine* work_line) {
1843 monitor_enter_dex_pcs->clear(); // The new work line is more accurate than the previous one.
1844
1845 std::map<uint32_t, ::art::verifier::MethodVerifier::DexLockInfo> depth_to_lock_info;
1846 auto collector = [&](uint32_t dex_reg, uint32_t depth) {
1847 auto insert_pair = depth_to_lock_info.emplace(
1848 depth, ::art::verifier::MethodVerifier::DexLockInfo(depth));
1849 auto it = insert_pair.first;
1850 auto set_insert_pair = it->second.dex_registers.insert(dex_reg);
1851 DCHECK(set_insert_pair.second);
1852 };
1853 work_line->IterateRegToLockDepths(collector);
1854 for (auto& pair : depth_to_lock_info) {
1855 monitor_enter_dex_pcs->push_back(pair.second);
1856 // Map depth to dex PC.
Andreas Gampe4ec4d482019-08-01 10:00:23 -07001857 monitor_enter_dex_pcs->back().dex_pc = work_line->GetMonitorEnterDexPc(pair.second.dex_pc);
Andreas Gampeb90cad52019-07-31 16:15:41 -07001858 }
1859}
1860
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001861template <bool kVerifierDebug>
Andreas Gampeb90cad52019-07-31 16:15:41 -07001862template <bool kMonitorDexPCs>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001863bool MethodVerifier<kVerifierDebug>::CodeFlowVerifyMethod() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001864 const uint16_t* insns = code_item_accessor_.Insns();
1865 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaoba5ebb92011-08-25 17:24:37 -07001866
jeffhaobdb76512011-09-07 11:43:16 -07001867 /* Begin by marking the first instruction as "changed". */
Andreas Gampe51de69e2019-04-19 15:14:14 -07001868 GetModifiableInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001869 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001870
jeffhaobdb76512011-09-07 11:43:16 -07001871 /* Continue until no instructions are marked "changed". */
1872 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001873 if (allow_thread_suspension_) {
1874 self_->AllowThreadSuspension();
1875 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001876 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1877 uint32_t insn_idx = start_guess;
1878 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001879 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001880 break;
1881 }
jeffhaobdb76512011-09-07 11:43:16 -07001882 if (insn_idx == insns_size) {
1883 if (start_guess != 0) {
1884 /* try again, starting from the top */
1885 start_guess = 0;
1886 continue;
1887 } else {
1888 /* all flags are clear */
1889 break;
1890 }
1891 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001892 // We carry the working set of registers from instruction to instruction. If this address can
1893 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1894 // "changed" flags, we need to load the set of registers from the table.
1895 // Because we always prefer to continue on to the next instruction, we should never have a
1896 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1897 // target.
1898 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001899 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001900 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001901 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001902 /*
David Srbecky346fd962020-07-27 16:51:00 +01001903 * Consistency check: retrieve the stored register line (assuming
jeffhaobdb76512011-09-07 11:43:16 -07001904 * a full table) and make sure it actually matches.
1905 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001906 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001907 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001908 if (work_line_->CompareLine(register_line) != 0) {
Andreas Gampe4a57d5f2019-04-03 12:31:18 -07001909 Dump(LOG_STREAM(FATAL_WITHOUT_ABORT));
1910 LOG(FATAL_WITHOUT_ABORT) << info_messages_.str();
David Sehr709b0702016-10-13 09:12:37 -07001911 LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001912 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001913 << " work_line=" << work_line_->Dump(this) << "\n"
1914 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001915 }
jeffhaobdb76512011-09-07 11:43:16 -07001916 }
jeffhaobdb76512011-09-07 11:43:16 -07001917 }
Andreas Gampeb90cad52019-07-31 16:15:41 -07001918
1919 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1920 // We want the state _before_ the instruction, for the case where the dex pc we're
1921 // interested in is itself a monitor-enter instruction (which is a likely place
1922 // for a thread to be suspended).
1923 if (kMonitorDexPCs && UNLIKELY(work_insn_idx_ == interesting_dex_pc_)) {
1924 HandleMonitorDexPcsWorkLine(monitor_enter_dex_pcs_, work_line_.get());
1925 }
1926
Ian Rogersd81871c2011-10-03 13:57:23 -07001927 if (!CodeFlowVerifyInstruction(&start_guess)) {
David Sehr709b0702016-10-13 09:12:37 -07001928 std::string prepend(dex_file_->PrettyMethod(dex_method_idx_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001929 prepend += " failed to verify: ";
1930 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001931 return false;
1932 }
jeffhaobdb76512011-09-07 11:43:16 -07001933 /* Clear "changed" and mark as visited. */
Andreas Gampe51de69e2019-04-19 15:14:14 -07001934 GetModifiableInstructionFlags(insn_idx).SetVisited();
1935 GetModifiableInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001936 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001937
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001938 if (kVerifierDebug) {
jeffhaobdb76512011-09-07 11:43:16 -07001939 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001940 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001941 * (besides the wasted space), but it indicates a flaw somewhere
1942 * down the line, possibly in the verifier.
1943 *
1944 * If we've substituted "always throw" instructions into the stream,
1945 * we are almost certainly going to have some dead code.
1946 */
1947 int dead_start = -1;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001948
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001949 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001950 const uint32_t insn_idx = inst.DexPc();
jeffhaobdb76512011-09-07 11:43:16 -07001951 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001952 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001953 * may or may not be preceded by a padding NOP (for alignment).
1954 */
1955 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1956 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1957 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001958 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001959 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1960 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1961 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Andreas Gampe51de69e2019-04-19 15:14:14 -07001962 GetModifiableInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001963 }
1964
Mathieu Chartierde40d472015-10-15 17:47:48 -07001965 if (!GetInstructionFlags(insn_idx).IsVisited()) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001966 if (dead_start < 0) {
jeffhaobdb76512011-09-07 11:43:16 -07001967 dead_start = insn_idx;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001968 }
jeffhaobdb76512011-09-07 11:43:16 -07001969 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001970 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1971 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001972 dead_start = -1;
1973 }
1974 }
1975 if (dead_start >= 0) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001976 LogVerifyInfo()
1977 << "dead code " << reinterpret_cast<void*>(dead_start)
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001978 << "-" << reinterpret_cast<void*>(code_item_accessor_.InsnsSizeInCodeUnits() - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001979 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001980 // To dump the state of the verify after a method, do something like:
David Sehr709b0702016-10-13 09:12:37 -07001981 // if (dex_file_->PrettyMethod(dex_method_idx_) ==
Ian Rogersc9e463c2013-06-05 16:52:26 -07001982 // "boolean java.lang.String.equals(java.lang.Object)") {
1983 // LOG(INFO) << info_messages_.str();
1984 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001985 }
jeffhaobdb76512011-09-07 11:43:16 -07001986 return true;
1987}
1988
Andreas Gampea727e372015-08-25 09:22:37 -07001989// Setup a register line for the given return instruction.
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001990template <bool kVerifierDebug>
1991static void AdjustReturnLine(MethodVerifier<kVerifierDebug>* verifier,
Andreas Gampea727e372015-08-25 09:22:37 -07001992 const Instruction* ret_inst,
1993 RegisterLine* line) {
1994 Instruction::Code opcode = ret_inst->Opcode();
1995
1996 switch (opcode) {
1997 case Instruction::RETURN_VOID:
Andreas Gampe51de69e2019-04-19 15:14:14 -07001998 if (verifier->IsInstanceConstructor()) {
1999 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
2000 line->CheckConstructorReturn(verifier);
2001 }
2002 line->MarkAllRegistersAsConflicts(verifier);
Andreas Gampea727e372015-08-25 09:22:37 -07002003 break;
2004
2005 case Instruction::RETURN:
2006 case Instruction::RETURN_OBJECT:
2007 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
2008 break;
2009
2010 case Instruction::RETURN_WIDE:
2011 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
2012 break;
2013
2014 default:
2015 LOG(FATAL) << "Unknown return opcode " << opcode;
2016 UNREACHABLE();
2017 }
2018}
2019
Andreas Gampe99db7bb2019-04-19 23:05:47 -07002020template <bool kVerifierDebug>
2021bool MethodVerifier<kVerifierDebug>::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07002022 /*
2023 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07002024 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07002025 * control to another statement:
2026 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002027 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07002028 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07002029 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07002030 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07002031 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07002032 * throw an exception that is handled by an encompassing "try"
2033 * block.
2034 *
2035 * We can also return, in which case there is no successor instruction
2036 * from this point.
2037 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08002038 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07002039 */
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002040 const uint16_t* insns = code_item_accessor_.Insns() + work_insn_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -07002041 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07002042 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07002043
jeffhaobdb76512011-09-07 11:43:16 -07002044 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07002045 bool just_set_result = false;
Andreas Gampe99db7bb2019-04-19 23:05:47 -07002046 if (kVerifierDebug) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002047 // Generate processing back trace to debug verifier
Andreas Gampe2ad6cce2019-04-11 16:17:39 -07002048 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
2049 << work_line_->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07002050 }
jeffhaobdb76512011-09-07 11:43:16 -07002051
2052 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002053 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07002054 * can throw an exception, we will copy/merge this into the "catch"
2055 * address rather than work_line, because we don't want the result
2056 * from the "successful" code path (e.g. a check-cast that "improves"
2057 * a type) to be visible to the exception handler.
2058 */
Andreas Gampec6582782019-07-10 15:06:18 -07002059 if (((opcode_flags & Instruction::kThrow) != 0 || IsCompatThrow(inst->Opcode())) &&
2060 CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002061 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002062 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002063 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07002064 }
Andreas Gampe43884b22019-06-27 14:05:52 -07002065 // Per-instruction flag, should not be set here.
2066 DCHECK(!flags_.have_pending_runtime_throw_failure_);
Andreas Gampe6087bc22019-06-03 15:52:08 -07002067 bool exc_handler_unreachable = false;
jeffhaobdb76512011-09-07 11:43:16 -07002068
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07002069
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002070 // We need to ensure the work line is consistent while performing validation. When we spot a
2071 // peephole pattern we compute a new line for either the fallthrough instruction or the
2072 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08002073 RegisterLineArenaUniquePtr branch_line;
2074 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002075
Sebastien Hertz5243e912013-05-21 10:55:07 +02002076 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07002077 case Instruction::NOP:
2078 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002079 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07002080 * a signature that looks like a NOP; if we see one of these in
2081 * the course of executing code then we have a problem.
2082 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002083 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07002084 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002085 }
2086 break;
2087
2088 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002089 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002090 break;
jeffhaobdb76512011-09-07 11:43:16 -07002091 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002092 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002093 break;
jeffhaobdb76512011-09-07 11:43:16 -07002094 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002095 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002096 break;
2097 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002098 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002099 break;
jeffhaobdb76512011-09-07 11:43:16 -07002100 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002101 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002102 break;
jeffhaobdb76512011-09-07 11:43:16 -07002103 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002104 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07002105 break;
2106 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002107 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002108 break;
jeffhaobdb76512011-09-07 11:43:16 -07002109 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002110 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002111 break;
jeffhaobdb76512011-09-07 11:43:16 -07002112 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002113 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002114 break;
2115
2116 /*
2117 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002118 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002119 * might want to hold the result in an actual CPU register, so the
2120 * Dalvik spec requires that these only appear immediately after an
2121 * invoke or filled-new-array.
2122 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002123 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002124 * redundant with the reset done below, but it can make the debug info
2125 * easier to read in some cases.)
2126 */
2127 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002128 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002129 break;
2130 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002131 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002132 break;
2133 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002134 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002135 break;
2136
Andreas Gampe6087bc22019-06-03 15:52:08 -07002137 case Instruction::MOVE_EXCEPTION:
2138 if (!HandleMoveException(inst)) {
2139 exc_handler_unreachable = true;
Sebastien Hertz270a0e12015-01-16 19:49:09 +01002140 }
jeffhaobdb76512011-09-07 11:43:16 -07002141 break;
Andreas Gampe6087bc22019-06-03 15:52:08 -07002142
jeffhaobdb76512011-09-07 11:43:16 -07002143 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002144 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002145 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002146 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002147 }
jeffhaobdb76512011-09-07 11:43:16 -07002148 }
2149 break;
2150 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002151 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002152 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002153 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002154 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002155 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2156 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002157 } else {
2158 // Compilers may generate synthetic functions that write byte values into boolean fields.
2159 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002160 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002161 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2163 ((return_type.IsBoolean() || return_type.IsByte() ||
2164 return_type.IsShort() || return_type.IsChar()) &&
2165 src_type.IsInteger()));
2166 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002167 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002168 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002169 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002170 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002171 }
jeffhaobdb76512011-09-07 11:43:16 -07002172 }
2173 }
2174 break;
2175 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002176 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002177 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002178 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002179 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002180 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002181 } else {
2182 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002183 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002184 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002185 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002186 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002187 }
jeffhaobdb76512011-09-07 11:43:16 -07002188 }
2189 }
2190 break;
2191 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002192 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002193 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002194 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002195 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002196 } else {
2197 /* return_type is the *expected* return type, not register value */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002198 DCHECK(!return_type.IsZeroOrNull());
Ian Rogersd81871c2011-10-03 13:57:23 -07002199 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002200 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002201 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002202 // Disallow returning undefined, conflict & uninitialized values and verify that the
2203 // reference in vAA is an instance of the "return_type."
2204 if (reg_type.IsUndefined()) {
2205 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2206 } else if (reg_type.IsConflict()) {
2207 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2208 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002209 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002210 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002211 } else if (!reg_type.IsReferenceTypes()) {
2212 // We really do expect a reference here.
2213 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2214 << reg_type;
David Brazdilca3c8c32016-09-06 14:04:48 +01002215 } else if (!return_type.IsAssignableFrom(reg_type, this)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002216 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
Nicolas Geoffray04ea42c2021-01-22 10:04:56 +00002217 Fail(api_level_ > 29u
2218 ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_UNRESOLVED_TYPE_CHECK)
Andreas Gampebc919af2019-07-10 14:09:51 -07002219 << " can't resolve returned type '" << return_type << "' or '" << reg_type << "'";
Jeff Haoa3faaf42013-09-03 19:07:00 -07002220 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002221 bool soft_error = false;
2222 // Check whether arrays are involved. They will show a valid class status, even
2223 // if their components are erroneous.
2224 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
David Brazdilca3c8c32016-09-06 14:04:48 +01002225 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error);
Andreas Gampe16f149c2015-03-23 10:10:20 -07002226 if (soft_error) {
2227 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2228 << reg_type << " vs " << return_type;
2229 }
2230 }
2231
2232 if (!soft_error) {
2233 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2234 << "', but expected from declaration '" << return_type << "'";
2235 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002236 }
jeffhaobdb76512011-09-07 11:43:16 -07002237 }
2238 }
2239 }
2240 break;
2241
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002242 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002243 case Instruction::CONST_4: {
2244 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002245 work_line_->SetRegisterType<LockOp::kClear>(
2246 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002247 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002248 }
2249 case Instruction::CONST_16: {
2250 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002251 work_line_->SetRegisterType<LockOp::kClear>(
2252 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002253 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002254 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002255 case Instruction::CONST: {
2256 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002257 work_line_->SetRegisterType<LockOp::kClear>(
2258 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002259 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002260 }
2261 case Instruction::CONST_HIGH16: {
2262 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002263 work_line_->SetRegisterType<LockOp::kClear>(
2264 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002265 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002266 }
jeffhaobdb76512011-09-07 11:43:16 -07002267 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002268 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002269 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002270 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2271 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002272 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002273 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002274 }
2275 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002276 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002277 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2278 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002279 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002280 break;
2281 }
2282 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002283 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002284 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2285 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002286 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002287 break;
2288 }
2289 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002290 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002291 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2292 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002293 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002294 break;
2295 }
jeffhaobdb76512011-09-07 11:43:16 -07002296 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002297 work_line_->SetRegisterType<LockOp::kClear>(
2298 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002299 break;
jeffhaobdb76512011-09-07 11:43:16 -07002300 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002301 work_line_->SetRegisterType<LockOp::kClear>(
2302 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002303 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002304 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002305 // Get type from instruction if unresolved then we need an access check
2306 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Andreas Gampe98be1a92017-08-28 08:25:45 -07002307 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002308 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002309 work_line_->SetRegisterType<LockOp::kClear>(
2310 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2311 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002312 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002313 }
Orion Hodson2e599942017-09-22 16:17:41 +01002314 case Instruction::CONST_METHOD_HANDLE:
2315 work_line_->SetRegisterType<LockOp::kClear>(
2316 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodHandle());
Orion Hodson2e599942017-09-22 16:17:41 +01002317 break;
2318 case Instruction::CONST_METHOD_TYPE:
2319 work_line_->SetRegisterType<LockOp::kClear>(
2320 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodType());
Orion Hodson2e599942017-09-22 16:17:41 +01002321 break;
jeffhaobdb76512011-09-07 11:43:16 -07002322 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002323 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002324 // Check whether the previous instruction is a move-object with vAA as a source, creating
2325 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002326 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002327 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002328 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002329 prev_idx--;
2330 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002331 const Instruction& prev_inst = code_item_accessor_.InstructionAt(prev_idx);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002332 switch (prev_inst.Opcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002333 case Instruction::MOVE_OBJECT:
2334 case Instruction::MOVE_OBJECT_16:
2335 case Instruction::MOVE_OBJECT_FROM16:
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002336 if (prev_inst.VRegB() == inst->VRegA_11x()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002337 // Redo the copy. This won't change the register types, but update the lock status
2338 // for the aliased register.
2339 work_line_->CopyRegister1(this,
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002340 prev_inst.VRegA(),
2341 prev_inst.VRegB(),
Andreas Gampec1474102015-08-18 08:57:44 -07002342 kTypeCategoryRef);
2343 }
2344 break;
2345
Alexey Grebenkince750492018-05-31 23:42:20 +03002346 // Catch a case of register aliasing when two registers are linked to the same
2347 // java.lang.Class object via two consequent const-class instructions immediately
2348 // preceding monitor-enter called on one of those registers.
2349 case Instruction::CONST_CLASS: {
2350 // Get the second previous instruction.
2351 if (prev_idx == 0 || GetInstructionFlags(prev_idx).IsBranchTarget()) {
2352 break;
2353 }
2354 prev_idx--;
2355 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
2356 prev_idx--;
2357 }
2358 const Instruction& prev2_inst = code_item_accessor_.InstructionAt(prev_idx);
2359
2360 // Match the pattern "const-class; const-class; monitor-enter;"
2361 if (prev2_inst.Opcode() != Instruction::CONST_CLASS) {
2362 break;
2363 }
2364
2365 // Ensure both const-classes are called for the same type_idx.
2366 if (prev_inst.VRegB_21c() != prev2_inst.VRegB_21c()) {
2367 break;
2368 }
2369
2370 // Update the lock status for the aliased register.
2371 if (prev_inst.VRegA() == inst->VRegA_11x()) {
2372 work_line_->CopyRegister1(this,
2373 prev2_inst.VRegA(),
2374 inst->VRegA_11x(),
2375 kTypeCategoryRef);
2376 } else if (prev2_inst.VRegA() == inst->VRegA_11x()) {
2377 work_line_->CopyRegister1(this,
2378 prev_inst.VRegA(),
2379 inst->VRegA_11x(),
2380 kTypeCategoryRef);
2381 }
2382 break;
2383 }
2384
Andreas Gampec1474102015-08-18 08:57:44 -07002385 default: // Other instruction types ignored.
2386 break;
2387 }
2388 }
jeffhaobdb76512011-09-07 11:43:16 -07002389 break;
2390 case Instruction::MONITOR_EXIT:
2391 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002392 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002393 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002394 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002395 * to the need to handle asynchronous exceptions, a now-deprecated
2396 * feature that Dalvik doesn't support.)
2397 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002398 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002399 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002400 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002401 * structured locking checks are working, the former would have
2402 * failed on the -enter instruction, and the latter is impossible.
2403 *
2404 * This is fortunate, because issue 3221411 prevents us from
2405 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002406 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002407 * some catch blocks (which will show up as "dead" code when
2408 * we skip them here); if we can't, then the code path could be
2409 * "live" so we still need to check it.
2410 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002411 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002412 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002413 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002414 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002415 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002416 /*
2417 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2418 * could be a "upcast" -- not expected, so we don't try to address it.)
2419 *
2420 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002421 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002422 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002423 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002424 const dex::TypeIndex type_idx((is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002425 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002426 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002427 // If this is a primitive type, fail HARD.
Andreas Gampee0bbab92019-07-25 12:28:22 -07002428 ObjPtr<mirror::Class> klass = GetClassLinker()->LookupResolvedType(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002429 type_idx, dex_cache_.Get(), class_loader_.Get());
Andreas Gampe00633eb2014-07-17 16:13:35 -07002430 if (klass != nullptr && klass->IsPrimitive()) {
2431 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2432 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2433 << GetDeclaringClass();
2434 break;
2435 }
2436
Ian Rogersad0b3a32012-04-16 14:50:24 -07002437 DCHECK_NE(failures_.size(), 0U);
2438 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002439 work_line_->SetRegisterType<LockOp::kClear>(this,
2440 inst->VRegA_22c(),
2441 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002442 }
2443 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002444 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002445 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002446 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002447 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002448 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002449 if (is_checkcast) {
2450 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2451 } else {
2452 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2453 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002454 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002455 if (is_checkcast) {
2456 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2457 } else {
2458 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2459 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002460 } else if (orig_type.IsUninitializedTypes()) {
2461 if (is_checkcast) {
2462 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2463 << orig_type_reg;
2464 } else {
2465 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2466 << orig_type_reg;
2467 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002468 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002469 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002470 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002471 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002472 work_line_->SetRegisterType<LockOp::kClear>(this,
2473 inst->VRegA_22c(),
2474 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002475 }
jeffhaobdb76512011-09-07 11:43:16 -07002476 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002477 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002478 }
2479 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002480 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002481 if (res_type.IsReferenceTypes()) {
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002482 if (!res_type.IsArrayTypes() && !res_type.IsZeroOrNull()) {
2483 // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002484 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002485 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002486 work_line_->SetRegisterType<LockOp::kClear>(this,
2487 inst->VRegA_12x(),
2488 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002489 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002490 } else {
2491 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002492 }
2493 break;
2494 }
2495 case Instruction::NEW_INSTANCE: {
Andreas Gampe98be1a92017-08-28 08:25:45 -07002496 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002497 if (res_type.IsConflict()) {
2498 DCHECK_NE(failures_.size(), 0U);
2499 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002500 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002501 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2502 // can't create an instance of an interface or abstract class */
2503 if (!res_type.IsInstantiableTypes()) {
2504 Fail(VERIFY_ERROR_INSTANTIATION)
2505 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002506 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002507 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002508 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002509 // Any registers holding previous allocations from this address that have not yet been
2510 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002511 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002512 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002513 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002514 break;
2515 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002516 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002517 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002518 break;
2519 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002520 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002521 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002522 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002523 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002524 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002525 just_set_result = true; // Filled new array range sets result register
2526 break;
jeffhaobdb76512011-09-07 11:43:16 -07002527 case Instruction::CMPL_FLOAT:
2528 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002529 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002530 break;
2531 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002532 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002533 break;
2534 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002535 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002536 break;
2537 case Instruction::CMPL_DOUBLE:
2538 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002539 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002540 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002541 break;
2542 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002543 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002544 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002545 break;
2546 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002547 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002548 break;
2549 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002550 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002551 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002552 break;
2553 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002554 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002555 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002556 break;
2557 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002558 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002559 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002560 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002561 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
David Brazdilca3c8c32016-09-06 14:04:48 +01002562 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002563 if (res_type.IsUninitializedTypes()) {
2564 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002565 } else if (!res_type.IsReferenceTypes()) {
2566 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002567 } else {
Nicolas Geoffray04ea42c2021-01-22 10:04:56 +00002568 Fail(res_type.IsUnresolvedTypes()
2569 ? VERIFY_ERROR_UNRESOLVED_TYPE_CHECK : VERIFY_ERROR_BAD_CLASS_SOFT)
David Brazdil68b5c0b2016-01-19 14:25:29 +00002570 << "thrown class " << res_type << " not instanceof Throwable";
2571 }
jeffhaobdb76512011-09-07 11:43:16 -07002572 }
2573 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002574 }
jeffhaobdb76512011-09-07 11:43:16 -07002575 case Instruction::GOTO:
2576 case Instruction::GOTO_16:
2577 case Instruction::GOTO_32:
2578 /* no effect on or use of registers */
2579 break;
2580
2581 case Instruction::PACKED_SWITCH:
2582 case Instruction::SPARSE_SWITCH:
2583 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002584 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002585 break;
2586
Ian Rogersd81871c2011-10-03 13:57:23 -07002587 case Instruction::FILL_ARRAY_DATA: {
2588 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002589 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002590 /* array_type can be null if the reg type is Zero */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002591 if (!array_type.IsZeroOrNull()) {
jeffhao457cc512012-02-02 16:55:13 -08002592 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002593 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2594 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002595 } else if (array_type.IsUnresolvedTypes()) {
2596 // If it's an unresolved array type, it must be non-primitive.
2597 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2598 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002599 } else {
Andreas Gampe51de69e2019-04-19 15:14:14 -07002600 const RegType& component_type = reg_types_.GetComponentType(array_type,
2601 class_loader_.Get());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002602 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002603 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002604 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2605 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002606 } else {
jeffhao457cc512012-02-02 16:55:13 -08002607 // Now verify if the element width in the table matches the element width declared in
2608 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002609 const uint16_t* array_data =
2610 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002611 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002612 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002613 } else {
2614 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2615 // Since we don't compress the data in Dex, expect to see equal width of data stored
2616 // in the table and expected from the array class.
2617 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002618 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2619 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002620 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002621 }
2622 }
jeffhaobdb76512011-09-07 11:43:16 -07002623 }
2624 }
2625 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002626 }
jeffhaobdb76512011-09-07 11:43:16 -07002627 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002628 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002629 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2630 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002631 bool mismatch = false;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002632 if (reg_type1.IsZeroOrNull()) { // zero then integral or reference expected
Ian Rogersd81871c2011-10-03 13:57:23 -07002633 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2634 } else if (reg_type1.IsReferenceTypes()) { // both references?
2635 mismatch = !reg_type2.IsReferenceTypes();
2636 } else { // both integral?
2637 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2638 }
2639 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002640 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2641 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002642 }
2643 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002644 }
jeffhaobdb76512011-09-07 11:43:16 -07002645 case Instruction::IF_LT:
2646 case Instruction::IF_GE:
2647 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002648 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002649 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2650 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002651 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002652 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2653 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002654 }
2655 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002656 }
jeffhaobdb76512011-09-07 11:43:16 -07002657 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002658 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002659 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002660 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002661 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2662 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002663 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002664
2665 // Find previous instruction - its existence is a precondition to peephole optimization.
Andreas Gampe147a9112019-07-31 16:17:10 -07002666 if (UNLIKELY(0 == work_insn_idx_)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002667 break;
2668 }
Andreas Gampe147a9112019-07-31 16:17:10 -07002669 uint32_t instance_of_idx = work_insn_idx_ - 1;
2670 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
2671 instance_of_idx--;
2672 }
2673 // Dex index 0 must be an opcode.
2674 DCHECK(GetInstructionFlags(instance_of_idx).IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002675
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002676 const Instruction& instance_of_inst = code_item_accessor_.InstructionAt(instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002677
2678 /* Check for peep-hole pattern of:
2679 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002680 * instance-of vX, vY, T;
2681 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002682 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002683 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002684 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002685 * and sharpen the type of vY to be type T.
2686 * Note, this pattern can't be if:
2687 * - if there are other branches to this branch,
2688 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002689 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002690 if (!CurrentInsnFlags()->IsBranchTarget() &&
Vladimir Markod7559b72017-09-28 13:50:37 +01002691 (Instruction::INSTANCE_OF == instance_of_inst.Opcode()) &&
2692 (inst->VRegA_21t() == instance_of_inst.VRegA_22c()) &&
2693 (instance_of_inst.VRegA_22c() != instance_of_inst.VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002694 // Check the type of the instance-of is different than that of registers type, as if they
2695 // are the same there is no work to be done here. Check that the conversion is not to or
2696 // from an unresolved type as type information is imprecise. If the instance-of is to an
2697 // interface then ignore the type information as interfaces can only be treated as Objects
2698 // and we don't want to disallow field and other operations on the object. If the value
2699 // being instance-of checked against is known null (zero) then allow the optimization as
2700 // we didn't have type information. If the merge of the instance-of type with the original
2701 // type is assignable to the original then allow optimization. This check is performed to
2702 // ensure that subsequent merges don't lose type information - such as becoming an
2703 // interface from a class that would lose information relevant to field checks.
Andreas Gamped78122b2019-07-10 14:06:53 -07002704 //
2705 // Note: do not do an access check. This may mark this with a runtime throw that actually
2706 // happens at the instanceof, not the branch (and branches aren't flagged to throw).
Vladimir Markod7559b72017-09-28 13:50:37 +01002707 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst.VRegB_22c());
Andreas Gamped78122b2019-07-10 14:06:53 -07002708 const RegType& cast_type = ResolveClass<CheckAccess::kNo>(
Vladimir Markod7559b72017-09-28 13:50:37 +01002709 dex::TypeIndex(instance_of_inst.VRegC_22c()));
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002710
Ian Rogersebbdd872014-07-07 23:53:08 -07002711 if (!orig_type.Equals(cast_type) &&
2712 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002713 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002714 !cast_type.GetClass()->IsInterface() &&
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002715 (orig_type.IsZeroOrNull() ||
David Brazdilca3c8c32016-09-06 14:04:48 +01002716 orig_type.IsStrictlyAssignableFrom(
2717 cast_type.Merge(orig_type, &reg_types_, this), this))) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002718 RegisterLine* update_line = RegisterLine::Create(code_item_accessor_.RegistersSize(),
Andreas Gamped09c0592019-04-19 15:44:05 -07002719 allocator_,
2720 GetRegTypeCache());
Ian Rogersfae370a2013-06-05 08:33:27 -07002721 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002722 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002723 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002724 branch_line.reset(update_line);
2725 }
2726 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002727 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002728 instance_of_inst.VRegB_22c(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002729 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002730 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002731 // See if instance-of was preceded by a move-object operation, common due to the small
2732 // register encoding space of instance-of, and propagate type information to the source
2733 // of the move-object.
Andreas Gampe43e43252019-01-08 12:06:57 -08002734 // Note: this is only valid if the move source was not clobbered.
Ian Rogers9b360392013-06-06 14:45:07 -07002735 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002736 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002737 move_idx--;
2738 }
Andreas Gampe147a9112019-07-31 16:17:10 -07002739 DCHECK(GetInstructionFlags(move_idx).IsOpcode());
Andreas Gampe43e43252019-01-08 12:06:57 -08002740 auto maybe_update_fn = [&instance_of_inst, update_line, this, &cast_type](
2741 uint16_t move_src,
2742 uint16_t move_trg)
2743 REQUIRES_SHARED(Locks::mutator_lock_) {
2744 if (move_trg == instance_of_inst.VRegB_22c() &&
2745 move_src != instance_of_inst.VRegA_22c()) {
2746 update_line->SetRegisterType<LockOp::kKeep>(this, move_src, cast_type);
2747 }
2748 };
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002749 const Instruction& move_inst = code_item_accessor_.InstructionAt(move_idx);
Vladimir Markod7559b72017-09-28 13:50:37 +01002750 switch (move_inst.Opcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002751 case Instruction::MOVE_OBJECT:
Andreas Gampe43e43252019-01-08 12:06:57 -08002752 maybe_update_fn(move_inst.VRegB_12x(), move_inst.VRegA_12x());
Ian Rogers9b360392013-06-06 14:45:07 -07002753 break;
2754 case Instruction::MOVE_OBJECT_FROM16:
Andreas Gampe43e43252019-01-08 12:06:57 -08002755 maybe_update_fn(move_inst.VRegB_22x(), move_inst.VRegA_22x());
Ian Rogers9b360392013-06-06 14:45:07 -07002756 break;
2757 case Instruction::MOVE_OBJECT_16:
Andreas Gampe43e43252019-01-08 12:06:57 -08002758 maybe_update_fn(move_inst.VRegB_32x(), move_inst.VRegA_32x());
Ian Rogers9b360392013-06-06 14:45:07 -07002759 break;
2760 default:
2761 break;
2762 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002763 }
2764 }
2765 }
2766
jeffhaobdb76512011-09-07 11:43:16 -07002767 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002768 }
jeffhaobdb76512011-09-07 11:43:16 -07002769 case Instruction::IF_LTZ:
2770 case Instruction::IF_GEZ:
2771 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002772 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002773 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002774 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002775 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2776 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002777 }
jeffhaobdb76512011-09-07 11:43:16 -07002778 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002779 }
jeffhaobdb76512011-09-07 11:43:16 -07002780 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002781 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002782 break;
jeffhaobdb76512011-09-07 11:43:16 -07002783 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002784 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002785 break;
jeffhaobdb76512011-09-07 11:43:16 -07002786 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002787 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002788 break;
jeffhaobdb76512011-09-07 11:43:16 -07002789 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002790 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002791 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002792 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002793 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002794 break;
jeffhaobdb76512011-09-07 11:43:16 -07002795 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002796 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002797 break;
2798 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002799 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002800 break;
2801
Ian Rogersd81871c2011-10-03 13:57:23 -07002802 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002803 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002804 break;
2805 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002806 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002807 break;
2808 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002809 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002810 break;
2811 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002812 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002813 break;
2814 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002815 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002816 break;
2817 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002818 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002819 break;
2820 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002821 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002822 break;
2823
jeffhaobdb76512011-09-07 11:43:16 -07002824 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002825 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002826 break;
jeffhaobdb76512011-09-07 11:43:16 -07002827 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002828 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002829 break;
jeffhaobdb76512011-09-07 11:43:16 -07002830 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002831 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002832 break;
jeffhaobdb76512011-09-07 11:43:16 -07002833 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002834 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002835 break;
2836 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002837 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002838 break;
2839 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002840 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002841 break;
2842 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002843 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2844 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002845 break;
jeffhaobdb76512011-09-07 11:43:16 -07002846
Ian Rogersd81871c2011-10-03 13:57:23 -07002847 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002848 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002849 break;
2850 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002851 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002852 break;
2853 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002854 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002855 break;
2856 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002857 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002858 break;
2859 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002860 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002861 break;
2862 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002863 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002864 break;
jeffhaobdb76512011-09-07 11:43:16 -07002865 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002866 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2867 false);
jeffhaobdb76512011-09-07 11:43:16 -07002868 break;
2869
jeffhaobdb76512011-09-07 11:43:16 -07002870 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002871 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002872 break;
jeffhaobdb76512011-09-07 11:43:16 -07002873 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002874 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002875 break;
jeffhaobdb76512011-09-07 11:43:16 -07002876 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002877 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002878 break;
jeffhaobdb76512011-09-07 11:43:16 -07002879 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002880 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002881 break;
2882 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002883 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002884 break;
2885 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002886 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002887 break;
2888 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002889 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2890 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002891 break;
2892
2893 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002894 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002895 break;
2896 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002897 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002898 break;
2899 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002900 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002901 break;
2902 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002903 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002904 break;
2905 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002906 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002907 break;
2908 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002909 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002910 break;
2911 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002912 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2913 true);
jeffhaobdb76512011-09-07 11:43:16 -07002914 break;
2915
2916 case Instruction::INVOKE_VIRTUAL:
2917 case Instruction::INVOKE_VIRTUAL_RANGE:
2918 case Instruction::INVOKE_SUPER:
David Srbeckyc5cd5892021-03-23 08:09:15 +00002919 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002920 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
David Srbeckyc5cd5892021-03-23 08:09:15 +00002921 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002922 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2923 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002924 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2925 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002926 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002927 if (called_method != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01002928 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2929 ? called_method->ResolveReturnType()
2930 : called_method->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002931 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002932 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
Vladimir Marko179b7c62019-03-22 13:38:57 +00002933 return_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07002934 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002935 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002936 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2937 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002938 }
2939 }
2940 if (return_type == nullptr) {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002941 uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002942 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002943 dex::TypeIndex return_type_idx =
2944 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002945 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Andreas Gampe51de69e2019-04-19 15:14:14 -07002946 return_type = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002947 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002948 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002949 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002950 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002951 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002952 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002953 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002954 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002955 }
jeffhaobdb76512011-09-07 11:43:16 -07002956 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002957 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002958 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002959 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002960 const char* return_type_descriptor;
2961 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002962 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002963 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002964 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002965 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002966 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Andreas Gampea5b09a62016-11-17 15:21:22 -08002967 dex::TypeIndex return_type_idx =
2968 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers46685432012-06-03 22:26:43 -07002969 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2970 } else {
2971 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002972 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Markob45528c2017-07-27 14:14:28 +01002973 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2974 ? called_method->ResolveReturnType()
2975 : called_method->LookupResolvedReturnType();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002976 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002977 return_type = &FromClass(return_type_descriptor,
Vladimir Markobcf17522018-06-01 13:14:32 +01002978 return_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07002979 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002980 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002981 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2982 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002983 }
Ian Rogers46685432012-06-03 22:26:43 -07002984 }
2985 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002986 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002987 * Some additional checks when calling a constructor. We know from the invocation arg check
2988 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2989 * that to require that called_method->klass is the same as this->klass or this->super,
2990 * allowing the latter only if the "this" argument is the same as the "this" argument to
2991 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002992 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002993 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
jeffhaob57e9522012-04-26 18:08:21 -07002994 if (this_type.IsConflict()) // failure.
2995 break;
jeffhaobdb76512011-09-07 11:43:16 -07002996
jeffhaob57e9522012-04-26 18:08:21 -07002997 /* no null refs allowed (?) */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002998 if (this_type.IsZeroOrNull()) {
jeffhaob57e9522012-04-26 18:08:21 -07002999 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
3000 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07003001 }
jeffhaob57e9522012-04-26 18:08:21 -07003002
3003 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003004 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07003005 // TODO: re-enable constructor type verification
3006 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07003007 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07003008 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
3009 // break;
3010 // }
jeffhaob57e9522012-04-26 18:08:21 -07003011
3012 /* arg must be an uninitialized reference */
3013 if (!this_type.IsUninitializedTypes()) {
3014 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
3015 << this_type;
3016 break;
3017 }
3018
3019 /*
3020 * Replace the uninitialized reference with an initialized one. We need to do this for all
3021 * registers that have the same object instance in them, not just the "this" register.
3022 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00003023 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003024 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07003025 if (return_type == nullptr) {
Andreas Gampe51de69e2019-04-19 15:14:14 -07003026 return_type = &reg_types_.FromDescriptor(class_loader_.Get(),
3027 return_type_descriptor,
3028 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07003029 }
3030 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003031 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003032 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07003033 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003034 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003035 just_set_result = true;
3036 break;
3037 }
3038 case Instruction::INVOKE_STATIC:
3039 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003040 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003041 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003042 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003043 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003044 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003045 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003046 dex::TypeIndex return_type_idx =
3047 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07003048 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003049 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003050 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003051 }
Andreas Gampe51de69e2019-04-19 15:14:14 -07003052 const RegType& return_type = reg_types_.FromDescriptor(class_loader_.Get(),
3053 descriptor,
3054 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003055 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003056 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003057 } else {
3058 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3059 }
jeffhaobdb76512011-09-07 11:43:16 -07003060 just_set_result = true;
3061 }
3062 break;
jeffhaobdb76512011-09-07 11:43:16 -07003063 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07003064 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003065 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003066 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003067 if (abs_method != nullptr) {
Vladimir Markod93e3742018-07-18 10:58:13 +01003068 ObjPtr<mirror::Class> called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003069 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
3070 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
David Sehr709b0702016-10-13 09:12:37 -07003071 << abs_method->PrettyMethod() << "'";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003072 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003073 }
Ian Rogers0d604842012-04-16 14:50:24 -07003074 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003075 /* Get the type of the "this" arg, which should either be a sub-interface of called
3076 * interface or Object (see comments in RegType::JoinClass).
3077 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003078 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08003079 if (this_type.IsZeroOrNull()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003080 /* null pointer always passes (and always fails at runtime) */
3081 } else {
3082 if (this_type.IsUninitializedTypes()) {
3083 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
3084 << this_type;
3085 break;
3086 }
3087 // In the past we have tried to assert that "called_interface" is assignable
3088 // from "this_type.GetClass()", however, as we do an imprecise Join
3089 // (RegType::JoinClass) we don't have full information on what interfaces are
3090 // implemented by "this_type". For example, two classes may implement the same
3091 // interfaces and have a common parent that doesn't implement the interface. The
3092 // join will set "this_type" to the parent class and a test that this implements
3093 // the interface will incorrectly fail.
3094 }
3095 /*
3096 * We don't have an object instance, so we can't find the concrete method. However, all of
3097 * the type information is in the abstract method, so we're good.
3098 */
3099 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003100 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003101 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003102 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003103 dex::TypeIndex return_type_idx =
3104 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003105 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003106 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003107 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003108 }
Andreas Gampe51de69e2019-04-19 15:14:14 -07003109 const RegType& return_type = reg_types_.FromDescriptor(class_loader_.Get(),
3110 descriptor,
3111 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003112 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003113 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003114 } else {
3115 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3116 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003117 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07003118 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003119 }
Narayan Kamath9823e782016-08-03 12:46:58 +01003120 case Instruction::INVOKE_POLYMORPHIC:
3121 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003122 bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
3123 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range);
3124 if (called_method == nullptr) {
3125 // Convert potential soft failures in VerifyInvocationArgs() to hard errors.
3126 if (failure_messages_.size() > 0) {
3127 std::string message = failure_messages_.back()->str();
3128 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message;
3129 } else {
3130 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure.";
3131 }
3132 break;
3133 }
3134 if (!CheckSignaturePolymorphicMethod(called_method) ||
3135 !CheckSignaturePolymorphicReceiver(inst)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00003136 DCHECK(HasFailures());
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003137 break;
3138 }
Orion Hodson06d10a72018-05-14 08:53:38 +01003139 const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
3140 const dex::ProtoIndex proto_idx(vRegH);
Orion Hodsonac141392017-01-13 11:53:47 +00003141 const char* return_descriptor =
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003142 dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx));
3143 const RegType& return_type =
Andreas Gampe51de69e2019-04-19 15:14:14 -07003144 reg_types_.FromDescriptor(class_loader_.Get(), return_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003145 if (!return_type.IsLowHalf()) {
3146 work_line_->SetResultRegisterType(this, return_type);
3147 } else {
3148 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3149 }
Orion Hodsonac141392017-01-13 11:53:47 +00003150 just_set_result = true;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003151 break;
Narayan Kamath9823e782016-08-03 12:46:58 +01003152 }
Orion Hodsonc069a302017-01-18 09:23:12 +00003153 case Instruction::INVOKE_CUSTOM:
3154 case Instruction::INVOKE_CUSTOM_RANGE: {
3155 // Verify registers based on method_type in the call site.
3156 bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE);
3157
3158 // Step 1. Check the call site that produces the method handle for invocation
3159 const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3160 if (!CheckCallSite(call_site_idx)) {
3161 DCHECK(HasFailures());
3162 break;
3163 }
3164
3165 // Step 2. Check the register arguments correspond to the expected arguments for the
3166 // method handle produced by step 1. The dex file verifier has checked ranges for
3167 // the first three arguments and CheckCallSite has checked the method handle type.
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003168 const dex::ProtoIndex proto_idx = dex_file_->GetProtoIndexForCallSite(call_site_idx);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003169 const dex::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00003170 DexFileParameterIterator param_it(*dex_file_, proto_id);
3171 // Treat method as static as it has yet to be determined.
3172 VerifyInvocationArgsFromIterator(&param_it, inst, METHOD_STATIC, is_range, nullptr);
3173 const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
3174
3175 // Step 3. Propagate return type information
3176 const RegType& return_type =
Andreas Gampe51de69e2019-04-19 15:14:14 -07003177 reg_types_.FromDescriptor(class_loader_.Get(), return_descriptor, false);
Orion Hodsonc069a302017-01-18 09:23:12 +00003178 if (!return_type.IsLowHalf()) {
3179 work_line_->SetResultRegisterType(this, return_type);
3180 } else {
3181 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3182 }
3183 just_set_result = true;
Orion Hodsonc069a302017-01-18 09:23:12 +00003184 break;
3185 }
jeffhaobdb76512011-09-07 11:43:16 -07003186 case Instruction::NEG_INT:
3187 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003188 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003189 break;
3190 case Instruction::NEG_LONG:
3191 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003192 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003193 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003194 break;
3195 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003196 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003197 break;
3198 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003199 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003200 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003201 break;
3202 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003203 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003204 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003205 break;
3206 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003207 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003208 break;
3209 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003210 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003211 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003212 break;
3213 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003214 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003215 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003216 break;
3217 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003218 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003219 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003220 break;
3221 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003222 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003223 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003224 break;
3225 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003226 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003227 break;
3228 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003229 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003230 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003231 break;
3232 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003233 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003234 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003235 break;
3236 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003237 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003238 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003239 break;
3240 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003241 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003242 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003243 break;
3244 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003245 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003246 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003247 break;
3248 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003249 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003250 break;
3251 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003252 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003253 break;
3254 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003255 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003256 break;
3257
3258 case Instruction::ADD_INT:
3259 case Instruction::SUB_INT:
3260 case Instruction::MUL_INT:
3261 case Instruction::REM_INT:
3262 case Instruction::DIV_INT:
3263 case Instruction::SHL_INT:
3264 case Instruction::SHR_INT:
3265 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003266 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003267 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003268 break;
3269 case Instruction::AND_INT:
3270 case Instruction::OR_INT:
3271 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003272 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003273 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003274 break;
3275 case Instruction::ADD_LONG:
3276 case Instruction::SUB_LONG:
3277 case Instruction::MUL_LONG:
3278 case Instruction::DIV_LONG:
3279 case Instruction::REM_LONG:
3280 case Instruction::AND_LONG:
3281 case Instruction::OR_LONG:
3282 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003283 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003284 reg_types_.LongLo(), reg_types_.LongHi(),
3285 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003286 break;
3287 case Instruction::SHL_LONG:
3288 case Instruction::SHR_LONG:
3289 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003290 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003291 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003292 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003293 break;
3294 case Instruction::ADD_FLOAT:
3295 case Instruction::SUB_FLOAT:
3296 case Instruction::MUL_FLOAT:
3297 case Instruction::DIV_FLOAT:
3298 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003299 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3300 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003301 break;
3302 case Instruction::ADD_DOUBLE:
3303 case Instruction::SUB_DOUBLE:
3304 case Instruction::MUL_DOUBLE:
3305 case Instruction::DIV_DOUBLE:
3306 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003307 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003308 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3309 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003310 break;
3311 case Instruction::ADD_INT_2ADDR:
3312 case Instruction::SUB_INT_2ADDR:
3313 case Instruction::MUL_INT_2ADDR:
3314 case Instruction::REM_INT_2ADDR:
3315 case Instruction::SHL_INT_2ADDR:
3316 case Instruction::SHR_INT_2ADDR:
3317 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003318 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3319 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003320 break;
3321 case Instruction::AND_INT_2ADDR:
3322 case Instruction::OR_INT_2ADDR:
3323 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003324 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3325 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003326 break;
3327 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003328 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3329 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003330 break;
3331 case Instruction::ADD_LONG_2ADDR:
3332 case Instruction::SUB_LONG_2ADDR:
3333 case Instruction::MUL_LONG_2ADDR:
3334 case Instruction::DIV_LONG_2ADDR:
3335 case Instruction::REM_LONG_2ADDR:
3336 case Instruction::AND_LONG_2ADDR:
3337 case Instruction::OR_LONG_2ADDR:
3338 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003339 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003340 reg_types_.LongLo(), reg_types_.LongHi(),
3341 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003342 break;
3343 case Instruction::SHL_LONG_2ADDR:
3344 case Instruction::SHR_LONG_2ADDR:
3345 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003346 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003347 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003348 break;
3349 case Instruction::ADD_FLOAT_2ADDR:
3350 case Instruction::SUB_FLOAT_2ADDR:
3351 case Instruction::MUL_FLOAT_2ADDR:
3352 case Instruction::DIV_FLOAT_2ADDR:
3353 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003354 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3355 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003356 break;
3357 case Instruction::ADD_DOUBLE_2ADDR:
3358 case Instruction::SUB_DOUBLE_2ADDR:
3359 case Instruction::MUL_DOUBLE_2ADDR:
3360 case Instruction::DIV_DOUBLE_2ADDR:
3361 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003362 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003363 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3364 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003365 break;
3366 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003367 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003368 case Instruction::MUL_INT_LIT16:
3369 case Instruction::DIV_INT_LIT16:
3370 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003371 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3372 true);
jeffhaobdb76512011-09-07 11:43:16 -07003373 break;
3374 case Instruction::AND_INT_LIT16:
3375 case Instruction::OR_INT_LIT16:
3376 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003377 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3378 true);
jeffhaobdb76512011-09-07 11:43:16 -07003379 break;
3380 case Instruction::ADD_INT_LIT8:
3381 case Instruction::RSUB_INT_LIT8:
3382 case Instruction::MUL_INT_LIT8:
3383 case Instruction::DIV_INT_LIT8:
3384 case Instruction::REM_INT_LIT8:
3385 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003386 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003387 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003388 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3389 false);
jeffhaobdb76512011-09-07 11:43:16 -07003390 break;
3391 case Instruction::AND_INT_LIT8:
3392 case Instruction::OR_INT_LIT8:
3393 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003394 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3395 false);
jeffhaobdb76512011-09-07 11:43:16 -07003396 break;
3397
Ian Rogersd81871c2011-10-03 13:57:23 -07003398 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003399 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
David Srbeckyc5cd5892021-03-23 08:09:15 +00003400 case Instruction::UNUSED_E3 ... Instruction::UNUSED_F9:
David Srbecky61c62422021-04-27 16:22:48 +01003401 case Instruction::UNUSED_73:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003402 case Instruction::UNUSED_79:
3403 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003404 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003405 break;
3406
3407 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003408 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003409 * complain if an instruction is missing (which is desirable).
3410 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003411 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003412
Andreas Gampe43884b22019-06-27 14:05:52 -07003413 if (flags_.have_pending_hard_failure_) {
Andreas Gampefef91cc2019-07-25 14:13:23 -07003414 if (IsAotMode()) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003415 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003416 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3417 LOG(ERROR) << "Pending failures:";
3418 for (auto& error : failures_) {
3419 LOG(ERROR) << error;
3420 }
3421 for (auto& error_msg : failure_messages_) {
3422 LOG(ERROR) << error_msg->str();
3423 }
3424 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3425 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003426 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003427 /* immediate failure, reject class */
3428 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3429 return false;
Andreas Gampe43884b22019-06-27 14:05:52 -07003430 } else if (flags_.have_pending_runtime_throw_failure_) {
Andreas Gampe6087bc22019-06-03 15:52:08 -07003431 LogVerifyInfo() << "Elevating opcode flags from " << opcode_flags << " to Throw";
Jeff Haoa3faaf42013-09-03 19:07:00 -07003432 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003433 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003434 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3435 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3436 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003437 }
jeffhaobdb76512011-09-07 11:43:16 -07003438 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003439 * If we didn't just set the result register, clear it out. This ensures that you can only use
3440 * "move-result" immediately after the result is set. (We could check this statically, but it's
3441 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003442 */
3443 if (!just_set_result) {
Andreas Gamped09c0592019-04-19 15:44:05 -07003444 work_line_->SetResultTypeToUnknown(GetRegTypeCache());
jeffhaobdb76512011-09-07 11:43:16 -07003445 }
3446
jeffhaobdb76512011-09-07 11:43:16 -07003447 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003448 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003449 *
3450 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003451 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003452 * somebody could get a reference field, check it for zero, and if the
3453 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003454 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003455 * that, and will reject the code.
3456 *
3457 * TODO: avoid re-fetching the branch target
3458 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003459 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003460 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003461 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003462 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003463 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003464 return false;
3465 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003466 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003467 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(),
3468 work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003469 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003470 }
jeffhaobdb76512011-09-07 11:43:16 -07003471 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003472 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003473 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003474 return false;
3475 }
3476 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003477 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003478 return false;
3479 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003480 }
jeffhaobdb76512011-09-07 11:43:16 -07003481 }
3482
3483 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003484 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003485 *
3486 * We've already verified that the table is structurally sound, so we
3487 * just need to walk through and tag the targets.
3488 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003489 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003490 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003491 const uint16_t* switch_insns = insns + offset_to_switch;
3492 int switch_count = switch_insns[1];
3493 int offset_to_targets, targ;
3494
3495 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3496 /* 0 = sig, 1 = count, 2/3 = first key */
3497 offset_to_targets = 4;
3498 } else {
3499 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003500 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003501 offset_to_targets = 2 + 2 * switch_count;
3502 }
3503
3504 /* verify each switch target */
3505 for (targ = 0; targ < switch_count; targ++) {
3506 int offset;
3507 uint32_t abs_offset;
3508
3509 /* offsets are 32-bit, and only partly endian-swapped */
3510 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003511 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003512 abs_offset = work_insn_idx_ + offset;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003513 DCHECK_LT(abs_offset, code_item_accessor_.InsnsSizeInCodeUnits());
3514 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003515 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003516 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003517 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003518 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003519 }
jeffhaobdb76512011-09-07 11:43:16 -07003520 }
3521 }
3522
3523 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003524 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3525 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003526 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003527 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003528 bool has_catch_all_handler = false;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003529 const dex::TryItem* try_item = code_item_accessor_.FindTryItem(work_insn_idx_);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003530 CHECK(try_item != nullptr);
Mathieu Chartierdc578c72017-12-27 11:51:45 -08003531 CatchHandlerIterator iterator(code_item_accessor_, *try_item);
jeffhaobdb76512011-09-07 11:43:16 -07003532
Andreas Gampef91baf12014-07-18 15:41:00 -07003533 // Need the linker to try and resolve the handled class to check if it's Throwable.
Andreas Gampee0bbab92019-07-25 12:28:22 -07003534 ClassLinker* linker = GetClassLinker();
Andreas Gampef91baf12014-07-18 15:41:00 -07003535
Ian Rogers0571d352011-11-03 19:51:38 -07003536 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003537 dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex();
3538 if (!handler_type_idx.IsValid()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003539 has_catch_all_handler = true;
3540 } else {
3541 // It is also a catch-all if it is java.lang.Throwable.
Vladimir Marko28e012a2017-12-07 11:22:59 +00003542 ObjPtr<mirror::Class> klass =
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003543 linker->ResolveType(handler_type_idx, dex_cache_, class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003544 if (klass != nullptr) {
Vladimir Markoadbceb72018-05-29 14:34:14 +01003545 if (klass == GetClassRoot<mirror::Throwable>()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003546 has_catch_all_handler = true;
3547 }
3548 } else {
3549 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003550 DCHECK(self_->IsExceptionPending());
3551 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003552 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003553 }
jeffhaobdb76512011-09-07 11:43:16 -07003554 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003555 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3556 * "work_regs", because at runtime the exception will be thrown before the instruction
3557 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003558 */
Andreas Gampebfcca582019-04-19 12:01:55 -07003559 if (kVerifierDebug) {
3560 LogVerifyInfo() << "Updating exception handler 0x"
3561 << std::hex << iterator.GetHandlerAddress();
3562 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003563 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003564 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003565 }
jeffhaobdb76512011-09-07 11:43:16 -07003566 }
3567
3568 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003569 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3570 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003571 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003572 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003573 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003574 * The state in work_line reflects the post-execution state. If the current instruction is a
3575 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003576 * it will do so before grabbing the lock).
3577 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003578 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003579 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003580 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003581 return false;
3582 }
3583 }
3584 }
3585
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003586 /* Handle "continue". Tag the next consecutive instruction.
3587 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3588 * because it changes work_line_ when performing peephole optimization
3589 * and this change should not be used in those cases.
3590 */
Andreas Gampe6087bc22019-06-03 15:52:08 -07003591 if ((opcode_flags & Instruction::kContinue) != 0 && !exc_handler_unreachable) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003592 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003593 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003594 if (next_insn_idx >= code_item_accessor_.InsnsSizeInCodeUnits()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003595 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3596 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003597 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003598 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3599 // next instruction isn't one.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003600 if (!CheckNotMoveException(code_item_accessor_.Insns(), next_insn_idx)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003601 return false;
3602 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003603 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003604 // Make workline consistent with fallthrough computed from peephole optimization.
3605 work_line_->CopyFromLine(fallthrough_line.get());
3606 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003607 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003608 // For returns we only care about the operand to the return, all other registers are dead.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003609 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003610 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003611 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003612 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003613 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003614 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3615 // needed. If the merge changes the state of the registers then the work line will be
3616 // updated.
3617 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003618 return false;
3619 }
3620 } else {
3621 /*
3622 * We're not recording register data for the next instruction, so we don't know what the
3623 * prior state was. We have to assume that something has changed and re-evaluate it.
3624 */
Andreas Gampe51de69e2019-04-19 15:14:14 -07003625 GetModifiableInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003626 }
3627 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003628
jeffhaod1f0fde2011-09-08 17:25:33 -07003629 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003630 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003631 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003632 }
3633
3634 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003635 * Update start_guess. Advance to the next instruction of that's
3636 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003637 * neither of those exists we're in a return or throw; leave start_guess
3638 * alone and let the caller sort it out.
3639 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003640 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003641 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003642 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003643 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003644 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003645 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003646 }
3647
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003648 DCHECK_LT(*start_guess, code_item_accessor_.InsnsSizeInCodeUnits());
Mathieu Chartierde40d472015-10-15 17:47:48 -07003649 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003650
Andreas Gampe43884b22019-06-27 14:05:52 -07003651 if (flags_.have_pending_runtime_throw_failure_) {
3652 flags_.have_any_pending_runtime_throw_failure_ = true;
Andreas Gampea727e372015-08-25 09:22:37 -07003653 // Reset the pending_runtime_throw flag now.
Andreas Gampe43884b22019-06-27 14:05:52 -07003654 flags_.have_pending_runtime_throw_failure_ = false;
Andreas Gampea727e372015-08-25 09:22:37 -07003655 }
3656
jeffhaobdb76512011-09-07 11:43:16 -07003657 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003658} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003659
Andreas Gampe99db7bb2019-04-19 23:05:47 -07003660template <bool kVerifierDebug>
3661template <CheckAccess C>
3662const RegType& MethodVerifier<kVerifierDebug>::ResolveClass(dex::TypeIndex class_idx) {
Andreas Gampee0bbab92019-07-25 12:28:22 -07003663 ClassLinker* linker = GetClassLinker();
Vladimir Marko28e012a2017-12-07 11:22:59 +00003664 ObjPtr<mirror::Class> klass = can_load_classes_
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003665 ? linker->ResolveType(class_idx, dex_cache_, class_loader_)
3666 : linker->LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003667 if (can_load_classes_ && klass == nullptr) {
3668 DCHECK(self_->IsExceptionPending());
3669 self_->ClearException();
3670 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003671 const RegType* result = nullptr;
Vladimir Marko9cb0c462017-04-21 13:31:41 +01003672 if (klass != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003673 bool precise = klass->CannotBeAssignedFromOtherTypes();
3674 if (precise && !IsInstantiableOrPrimitive(klass)) {
3675 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3676 UninstantiableError(descriptor);
3677 precise = false;
3678 }
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003679 result = reg_types_.FindClass(klass, precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003680 if (result == nullptr) {
3681 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003682 result = reg_types_.InsertClass(descriptor, klass, precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003683 }
3684 } else {
3685 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Andreas Gampe51de69e2019-04-19 15:14:14 -07003686 result = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003687 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003688 DCHECK(result != nullptr);
3689 if (result->IsConflict()) {
3690 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3691 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3692 << "' in " << GetDeclaringClass();
3693 return *result;
3694 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003695
Andreas Gampe629be512017-08-25 17:09:32 -07003696 // If requested, check if access is allowed. Unresolved types are included in this check, as the
3697 // interpreter only tests whether access is allowed when a class is not pre-verified and runs in
3698 // the access-checks interpreter. If result is primitive, skip the access check.
3699 //
3700 // Note: we do this for unresolved classes to trigger re-verification at runtime.
Andreas Gampe6087bc22019-06-03 15:52:08 -07003701 if (C != CheckAccess::kNo &&
Andreas Gampedc39d322018-09-04 09:26:03 -07003702 result->IsNonZeroReferenceTypes() &&
Andreas Gampe6087bc22019-06-03 15:52:08 -07003703 ((C == CheckAccess::kYes && IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP))
3704 || !result->IsUnresolvedTypes())) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003705 const RegType& referrer = GetDeclaringClass();
David Brazdil2bb2fbd2018-11-13 18:24:26 +00003706 if ((IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP) || !referrer.IsUnresolvedTypes()) &&
3707 !referrer.CanAccess(*result)) {
Andreas Gampe629be512017-08-25 17:09:32 -07003708 Fail(VERIFY_ERROR_ACCESS_CLASS) << "(possibly) illegal class access: '"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +01003709 << referrer << "' -> '" << *result << "'";
Mathieu Chartierde40d472015-10-15 17:47:48 -07003710 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003711 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003712 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003713}
3714
Andreas Gampe99db7bb2019-04-19 23:05:47 -07003715template <bool kVerifierDebug>
Andreas Gampe6087bc22019-06-03 15:52:08 -07003716bool MethodVerifier<kVerifierDebug>::HandleMoveException(const Instruction* inst) {
3717 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
3718 // where one entrypoint to the catch block is not actually an exception path.
3719 if (work_insn_idx_ == 0) {
3720 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
3721 return true;
3722 }
3723 /*
3724 * This statement can only appear as the first instruction in an exception handler. We verify
3725 * that as part of extracting the exception type from the catch block list.
3726 */
3727 auto caught_exc_type_fn = [&]() REQUIRES_SHARED(Locks::mutator_lock_) ->
3728 std::pair<bool, const RegType*> {
3729 const RegType* common_super = nullptr;
3730 if (code_item_accessor_.TriesSize() != 0) {
3731 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
3732 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3733 const RegType* unresolved = nullptr;
3734 for (uint32_t i = 0; i < handlers_size; i++) {
3735 CatchHandlerIterator iterator(handlers_ptr);
3736 for (; iterator.HasNext(); iterator.Next()) {
3737 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3738 if (!iterator.GetHandlerTypeIndex().IsValid()) {
3739 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003740 } else {
Andreas Gampe6087bc22019-06-03 15:52:08 -07003741 // Do access checks only on resolved exception classes.
3742 const RegType& exception =
3743 ResolveClass<CheckAccess::kOnResolvedClass>(iterator.GetHandlerTypeIndex());
3744 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) {
3745 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
3746 if (exception.IsUnresolvedTypes()) {
3747 if (unresolved == nullptr) {
3748 unresolved = &exception;
3749 } else {
3750 unresolved = &unresolved->SafeMerge(exception, &reg_types_, this);
3751 }
3752 } else {
3753 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class "
3754 << exception;
3755 return std::make_pair(true, &reg_types_.Conflict());
3756 }
3757 } else if (common_super == nullptr) {
3758 common_super = &exception;
3759 } else if (common_super->Equals(exception)) {
3760 // odd case, but nothing to do
3761 } else {
3762 common_super = &common_super->Merge(exception, &reg_types_, this);
3763 if (FailOrAbort(reg_types_.JavaLangThrowable(false).IsAssignableFrom(
3764 *common_super, this),
3765 "java.lang.Throwable is not assignable-from common_super at ",
3766 work_insn_idx_)) {
3767 break;
3768 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003769 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003770 }
3771 }
3772 }
Andreas Gampe6087bc22019-06-03 15:52:08 -07003773 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003774 }
Andreas Gampe6087bc22019-06-03 15:52:08 -07003775 if (unresolved != nullptr) {
Andreas Gampefef91cc2019-07-25 14:13:23 -07003776 if (!IsAotMode() && common_super == nullptr) {
Andreas Gampe6087bc22019-06-03 15:52:08 -07003777 // This is an unreachable handler.
Andreas Gampe0a7d0b12019-06-04 10:42:29 -07003778
3779 // We need to post a failure. The compiler currently does not handle unreachable
3780 // code correctly.
Andreas Gampe0d87f992019-07-10 13:19:45 -07003781 Fail(VERIFY_ERROR_SKIP_COMPILER, /*pending_exc=*/ false)
3782 << "Unresolved catch handler, fail for compiler";
Andreas Gampe0a7d0b12019-06-04 10:42:29 -07003783
Andreas Gampe6087bc22019-06-03 15:52:08 -07003784 return std::make_pair(false, unresolved);
3785 }
3786 // Soft-fail, but do not handle this with a synthetic throw.
Nicolas Geoffray04ea42c2021-01-22 10:04:56 +00003787 Fail(VERIFY_ERROR_UNRESOLVED_TYPE_CHECK, /*pending_exc=*/ false)
3788 << "Unresolved catch handler";
Andreas Gampe6087bc22019-06-03 15:52:08 -07003789 if (common_super != nullptr) {
3790 unresolved = &unresolved->Merge(*common_super, &reg_types_, this);
3791 }
3792 return std::make_pair(true, unresolved);
3793 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003794 }
Andreas Gampe6087bc22019-06-03 15:52:08 -07003795 if (common_super == nullptr) {
3796 /* no catch blocks, or no catches with classes we can find */
3797 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
3798 return std::make_pair(true, &reg_types_.Conflict());
3799 }
3800 return std::make_pair(true, common_super);
3801 };
3802 auto result = caught_exc_type_fn();
3803 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), *result.second);
3804 return result.first;
Ian Rogersd81871c2011-10-03 13:57:23 -07003805}
3806
Andreas Gampe99db7bb2019-04-19 23:05:47 -07003807template <bool kVerifierDebug>
3808ArtMethod* MethodVerifier<kVerifierDebug>::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003809 uint32_t dex_method_idx, MethodType method_type) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003810 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Andreas Gampe98be1a92017-08-28 08:25:45 -07003811 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003812 if (klass_type.IsConflict()) {
3813 std::string append(" in attempt to access method ");
3814 append += dex_file_->GetMethodName(method_id);
3815 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003816 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003817 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003818 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003819 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003820 }
Vladimir Markoba118822017-06-12 15:41:56 +01003821 ObjPtr<mirror::Class> klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003822 const RegType& referrer = GetDeclaringClass();
Andreas Gampee0bbab92019-07-25 12:28:22 -07003823 ClassLinker* class_linker = GetClassLinker();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003824
David Srbecky5de5efe2021-02-15 21:23:00 +00003825 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003826 if (res_method == nullptr) {
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00003827 res_method = class_linker->FindResolvedMethod(
3828 klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07003829 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003830
Alex Lightafb66472017-08-01 09:54:49 -07003831 bool must_fail = false;
3832 // This is traditional and helps with screwy bytecode. It will tell you that, yes, a method
3833 // exists, but that it's called incorrectly. This significantly helps debugging, as locally it's
3834 // hard to see the differences.
3835 // If we don't have res_method here we must fail. Just use this bool to make sure of that with a
3836 // DCHECK.
Vladimir Markoba118822017-06-12 15:41:56 +01003837 if (res_method == nullptr) {
Alex Lightafb66472017-08-01 09:54:49 -07003838 must_fail = true;
Vladimir Markoba118822017-06-12 15:41:56 +01003839 // Try to find the method also with the other type for better error reporting below
3840 // but do not store such bogus lookup result in the DexCache or VerifierDeps.
David Brazdil4525e0b2018-04-05 16:57:32 +01003841 res_method = class_linker->FindIncompatibleMethod(
3842 klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx);
Vladimir Markoba118822017-06-12 15:41:56 +01003843 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003844
3845 if (res_method == nullptr) {
3846 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
David Sehr709b0702016-10-13 09:12:37 -07003847 << klass->PrettyDescriptor() << "."
David Brazdilca3c8c32016-09-06 14:04:48 +01003848 << dex_file_->GetMethodName(method_id) << " "
3849 << dex_file_->GetMethodSignature(method_id);
3850 return nullptr;
3851 }
3852
Ian Rogersd81871c2011-10-03 13:57:23 -07003853 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3854 // enforce them here.
3855 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003856 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
David Sehr709b0702016-10-13 09:12:37 -07003857 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003858 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003859 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003860 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003861 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003862 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
David Sehr709b0702016-10-13 09:12:37 -07003863 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003864 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003865 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003866
3867 // Check that interface methods are static or match interface classes.
3868 // We only allow statics if we don't have default methods enabled.
3869 //
3870 // Note: this check must be after the initializer check, as those are required to fail a class,
3871 // while this check implies an IncompatibleClassChangeError.
3872 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003873 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003874 // default methods are supported for the dex file), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003875 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003876 method_type != METHOD_STATIC &&
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003877 (!dex_file_->SupportsDefaultMethods() ||
Alex Lightb55f1ac2016-04-12 15:50:55 -07003878 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003879 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003880 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003881 << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx)
3882 << " is in an interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003883 return nullptr;
3884 }
3885 } else {
3886 if (method_type == METHOD_INTERFACE) {
3887 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003888 << "interface method " << dex_file_->PrettyMethod(dex_method_idx)
3889 << " is in a non-interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003890 return nullptr;
3891 }
3892 }
3893
Alex Lightafb66472017-08-01 09:54:49 -07003894 // Check specifically for non-public object methods being provided for interface dispatch. This
3895 // can occur if we failed to find a method with FindInterfaceMethod but later find one with
3896 // FindClassMethod for error message use.
3897 if (method_type == METHOD_INTERFACE &&
3898 res_method->GetDeclaringClass()->IsObjectClass() &&
3899 !res_method->IsPublic()) {
3900 Fail(VERIFY_ERROR_NO_METHOD) << "invoke-interface " << klass->PrettyDescriptor() << "."
3901 << dex_file_->GetMethodName(method_id) << " "
3902 << dex_file_->GetMethodSignature(method_id) << " resolved to "
3903 << "non-public object method " << res_method->PrettyMethod() << " "
3904 << "but non-public Object methods are excluded from interface "
3905 << "method resolution.";
3906 return nullptr;
3907 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003908 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003909 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07003910 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call "
3911 << res_method->PrettyMethod()
Ian Rogersad0b3a32012-04-16 14:50:24 -07003912 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003913 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003914 }
jeffhaode0d9c92012-02-27 13:58:13 -08003915 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003916 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003917 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
David Sehr709b0702016-10-13 09:12:37 -07003918 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003919 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003920 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003921 // See if the method type implied by the invoke instruction matches the access flags for the
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003922 // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two
3923 // signature polymorphic methods supported by the run-time which are native methods with variable
3924 // arguments.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003925 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003926 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003927 ((method_type == METHOD_SUPER ||
3928 method_type == METHOD_VIRTUAL ||
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003929 method_type == METHOD_INTERFACE) && res_method->IsDirect()) ||
3930 ((method_type == METHOD_POLYMORPHIC) &&
3931 (!res_method->IsNative() || !res_method->IsVarargs()))) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003932 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003933 "type of " << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003934 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003935 }
Alex Lightafb66472017-08-01 09:54:49 -07003936 // Make sure we weren't expecting to fail.
3937 DCHECK(!must_fail) << "invoke type (" << method_type << ")"
3938 << klass->PrettyDescriptor() << "."
3939 << dex_file_->GetMethodName(method_id) << " "
3940 << dex_file_->GetMethodSignature(method_id) << " unexpectedly resolved to "
3941 << res_method->PrettyMethod() << " without error. Initially this method was "
3942 << "not found so we were expecting to fail for some reason.";
jeffhao8cd6dda2012-02-22 10:15:34 -08003943 return res_method;
3944}
3945
Andreas Gampe99db7bb2019-04-19 23:05:47 -07003946template <bool kVerifierDebug>
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003947template <class T>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07003948ArtMethod* MethodVerifier<kVerifierDebug>::VerifyInvocationArgsFromIterator(
Mathieu Chartiere401d142015-04-22 13:56:20 -07003949 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampee383d232018-06-19 12:29:51 -07003950 DCHECK_EQ(!is_range, inst->HasVarArgs());
3951
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003952 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3953 // match the call to the signature. Also, we might be calling through an abstract method
3954 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003955 const size_t expected_args = inst->VRegA();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003956 /* caught by static verifier */
3957 DCHECK(is_range || expected_args <= 5);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003958
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003959 if (expected_args > code_item_accessor_.OutsSize()) {
Orion Hodson1cda7c22017-08-10 13:06:45 +01003960 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003961 << ") exceeds outsSize ("
3962 << code_item_accessor_.OutsSize() << ")";
Orion Hodson1cda7c22017-08-10 13:06:45 +01003963 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003964 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003965
3966 /*
3967 * Check the "this" argument, which must be an instance of the class that declared the method.
3968 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3969 * rigorous check here (which is okay since we have to do it at runtime).
3970 */
3971 if (method_type != METHOD_STATIC) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003972 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003973 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Andreas Gampe43884b22019-06-27 14:05:52 -07003974 CHECK(flags_.have_pending_hard_failure_);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003975 return nullptr;
3976 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003977 bool is_init = false;
3978 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003979 if (res_method) {
3980 if (!res_method->IsConstructor()) {
3981 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3982 return nullptr;
3983 }
3984 } else {
3985 // Check whether the name of the called method is "<init>"
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00003986 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Jeff Hao0d087272014-08-04 14:47:17 -07003987 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003988 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3989 return nullptr;
3990 }
3991 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003992 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003993 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003994 const RegType& adjusted_type = is_init
3995 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
3996 : actual_arg_type;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08003997 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZeroOrNull()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003998 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003999 // Miranda methods have the declaring interface as their declaring class, not the abstract
4000 // class. It would be wrong to use this for the type check (interface type checks are
4001 // postponed to runtime).
4002 if (res_method != nullptr && !res_method->IsMiranda()) {
Vladimir Markod93e3742018-07-18 10:58:13 +01004003 ObjPtr<mirror::Class> klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004004 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07004005 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
4006 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004007 } else {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004008 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004009 const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07004010 res_method_class = &reg_types_.FromDescriptor(
Andreas Gampe51de69e2019-04-19 15:14:14 -07004011 class_loader_.Get(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07004012 dex_file_->StringByTypeIdx(class_idx),
4013 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004014 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004015 if (!res_method_class->IsAssignableFrom(adjusted_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004016 Fail(adjusted_type.IsUnresolvedTypes()
Nicolas Geoffray04ea42c2021-01-22 10:04:56 +00004017 ? VERIFY_ERROR_UNRESOLVED_TYPE_CHECK
David Brazdil68b5c0b2016-01-19 14:25:29 +00004018 : VERIFY_ERROR_BAD_CLASS_SOFT)
4019 << "'this' argument '" << actual_arg_type << "' not instance of '"
4020 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004021 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4022 // the compiler.
Andreas Gampe43884b22019-06-27 14:05:52 -07004023 if (flags_.have_pending_hard_failure_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004024 return nullptr;
4025 }
4026 }
4027 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004028 }
4029
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004030 uint32_t arg[5];
4031 if (!is_range) {
4032 inst->GetVarArgs(arg);
4033 }
4034 uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004035 for ( ; it->HasNext(); it->Next()) {
4036 if (sig_registers >= expected_args) {
4037 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004038 " argument registers, method signature has " << sig_registers + 1 << " or more";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004039 return nullptr;
4040 }
4041
4042 const char* param_descriptor = it->GetDescriptor();
4043
4044 if (param_descriptor == nullptr) {
4045 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
4046 "component";
4047 return nullptr;
4048 }
4049
Andreas Gampe51de69e2019-04-19 15:14:14 -07004050 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_.Get(),
4051 param_descriptor,
4052 false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004053 uint32_t get_reg = is_range ? inst->VRegC() + static_cast<uint32_t>(sig_registers) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004054 arg[sig_registers];
4055 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004056 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004057 if (!src_type.IsIntegralTypes()) {
4058 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
4059 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07004060 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004061 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07004062 } else {
4063 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
4064 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4065 // the compiler.
Andreas Gampe43884b22019-06-27 14:05:52 -07004066 if (flags_.have_pending_hard_failure_) {
Andreas Gampeda9badb2015-06-05 20:22:12 -07004067 return nullptr;
4068 }
4069 } else if (reg_type.IsLongOrDoubleTypes()) {
4070 // Check that registers are consecutive (for non-range invokes). Invokes are the only
4071 // instructions not specifying register pairs by the first component, but require them
4072 // nonetheless. Only check when there's an actual register in the parameters. If there's
4073 // none, this will fail below.
4074 if (!is_range && sig_registers + 1 < expected_args) {
4075 uint32_t second_reg = arg[sig_registers + 1];
4076 if (second_reg != get_reg + 1) {
4077 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
4078 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
4079 << second_reg << ".";
4080 return nullptr;
4081 }
4082 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004083 }
4084 }
4085 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
4086 }
4087 if (expected_args != sig_registers) {
4088 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004089 " argument registers, method signature has " << sig_registers;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004090 return nullptr;
4091 }
4092 return res_method;
4093}
4094
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004095template <bool kVerifierDebug>
4096void MethodVerifier<kVerifierDebug>::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
4097 MethodType method_type,
4098 bool is_range) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004099 // As the method may not have been resolved, make this static check against what we expect.
4100 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
4101 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004102 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004103 DexFileParameterIterator it(*dex_file_,
4104 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004105 VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004106}
4107
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004108template <bool kVerifierDebug>
4109bool MethodVerifier<kVerifierDebug>::CheckCallSite(uint32_t call_site_idx) {
Orion Hodson3a842f52017-04-21 15:24:10 +01004110 if (call_site_idx >= dex_file_->NumCallSiteIds()) {
4111 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx
4112 << " >= " << dex_file_->NumCallSiteIds();
4113 return false;
4114 }
4115
Orion Hodsonc069a302017-01-18 09:23:12 +00004116 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
Alex Light1e52a072019-06-25 09:12:04 -07004117 // Check essential arguments are provided. The dex file verifier has verified indices of the
Orion Hodsonc069a302017-01-18 09:23:12 +00004118 // main values (method handle, name, method_type).
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004119 static const size_t kRequiredArguments = 3;
4120 if (it.Size() < kRequiredArguments) {
Orion Hodsonc069a302017-01-18 09:23:12 +00004121 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4122 << " has too few arguments: "
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004123 << it.Size() << " < " << kRequiredArguments;
Orion Hodsonc069a302017-01-18 09:23:12 +00004124 return false;
4125 }
4126
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004127 std::pair<const EncodedArrayValueIterator::ValueType, size_t> type_and_max[kRequiredArguments] =
4128 { { EncodedArrayValueIterator::ValueType::kMethodHandle, dex_file_->NumMethodHandles() },
4129 { EncodedArrayValueIterator::ValueType::kString, dex_file_->NumStringIds() },
4130 { EncodedArrayValueIterator::ValueType::kMethodType, dex_file_->NumProtoIds() }
4131 };
4132 uint32_t index[kRequiredArguments];
4133
4134 // Check arguments have expected types and are within permitted ranges.
4135 for (size_t i = 0; i < kRequiredArguments; ++i) {
4136 if (it.GetValueType() != type_and_max[i].first) {
4137 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx
4138 << " argument " << i << " has wrong type "
4139 << it.GetValueType() << "!=" << type_and_max[i].first;
4140 return false;
4141 }
4142 index[i] = static_cast<uint32_t>(it.GetJavaValue().i);
4143 if (index[i] >= type_and_max[i].second) {
4144 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx
4145 << " argument " << i << " bad index "
4146 << index[i] << " >= " << type_and_max[i].second;
4147 return false;
4148 }
4149 it.Next();
Orion Hodsona5dca522018-02-27 12:42:11 +00004150 }
Orion Hodson3a842f52017-04-21 15:24:10 +01004151
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004152 // Check method handle kind is valid.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004153 const dex::MethodHandleItem& mh = dex_file_->GetMethodHandle(index[0]);
Orion Hodsonc069a302017-01-18 09:23:12 +00004154 if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) {
4155 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
Orion Hodson631827d2017-04-10 14:53:47 +01004156 << " argument 0 method handle type is not InvokeStatic: "
4157 << mh.method_handle_type_;
Orion Hodsonc069a302017-01-18 09:23:12 +00004158 return false;
4159 }
Orion Hodsonc069a302017-01-18 09:23:12 +00004160 return true;
4161}
4162
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004163class MethodParamListDescriptorIterator {
4164 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004165 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004166 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
4167 params_size_(params_ == nullptr ? 0 : params_->Size()) {
4168 }
4169
4170 bool HasNext() {
4171 return pos_ < params_size_;
4172 }
4173
4174 void Next() {
4175 ++pos_;
4176 }
4177
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004178 const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004179 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
4180 }
4181
4182 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004183 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004184 size_t pos_;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004185 const dex::TypeList* params_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004186 const size_t params_size_;
4187};
4188
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004189template <bool kVerifierDebug>
4190ArtMethod* MethodVerifier<kVerifierDebug>::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08004191 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08004192 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
4193 // we're making.
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004194 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Alex Light7268d472016-01-20 15:50:01 -08004195 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004196 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004197 // Check what we can statically.
Andreas Gampe43884b22019-06-27 14:05:52 -07004198 if (!flags_.have_pending_hard_failure_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004199 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
4200 }
4201 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08004202 }
4203
Ian Rogersd81871c2011-10-03 13:57:23 -07004204 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07004205 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08004206 if (method_type == METHOD_SUPER) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004207 dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
David Brazdilca3c8c32016-09-06 14:04:48 +01004208 const RegType& reference_type = reg_types_.FromDescriptor(
Andreas Gampe51de69e2019-04-19 15:14:14 -07004209 class_loader_.Get(),
David Brazdilca3c8c32016-09-06 14:04:48 +01004210 dex_file_->StringByTypeIdx(class_idx),
4211 false);
4212 if (reference_type.IsUnresolvedTypes()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004213 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4214 return nullptr;
4215 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004216 if (reference_type.GetClass()->IsInterface()) {
Alex Light55ea94d2016-03-15 09:50:26 -07004217 if (!GetDeclaringClass().HasClass()) {
4218 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4219 << "interface invoke-super";
4220 return nullptr;
David Brazdilca3c8c32016-09-06 14:04:48 +01004221 } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004222 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07004223 << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass())
4224 << " in method "
4225 << dex_file_->PrettyMethod(dex_method_idx_) << " to method "
4226 << dex_file_->PrettyMethod(method_idx) << " references "
4227 << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass());
Alex Lightfedd91d2016-01-07 14:49:16 -08004228 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004229 }
4230 } else {
4231 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4232 if (super.IsUnresolvedTypes()) {
4233 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004234 << dex_file_->PrettyMethod(dex_method_idx_)
4235 << " to super " << res_method->PrettyMethod();
Alex Light705ad492015-09-21 11:36:30 -07004236 return nullptr;
4237 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004238 if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) ||
Aart Bikf663e342016-04-04 17:28:59 -07004239 (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Alex Light705ad492015-09-21 11:36:30 -07004240 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004241 << dex_file_->PrettyMethod(dex_method_idx_)
Alex Light705ad492015-09-21 11:36:30 -07004242 << " to super " << super
4243 << "." << res_method->GetName()
4244 << res_method->GetSignature();
4245 return nullptr;
4246 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004247 }
4248 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004249
Andreas Gampe74979b12017-05-16 09:28:06 -07004250 if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004251 // Process the signature of the calling site that is invoking the method handle.
Orion Hodson06d10a72018-05-14 08:53:38 +01004252 dex::ProtoIndex proto_idx(inst->VRegH());
4253 DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(proto_idx));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004254 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4255 } else {
4256 // Process the target method's signature.
4257 MethodParamListDescriptorIterator it(res_method);
4258 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4259 }
4260}
4261
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004262template <bool kVerifierDebug>
4263bool MethodVerifier<kVerifierDebug>::CheckSignaturePolymorphicMethod(ArtMethod* method) {
Vladimir Markod93e3742018-07-18 10:58:13 +01004264 ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004265 const char* method_name = method->GetName();
4266
4267 const char* expected_return_descriptor;
Andreas Gampee0bbab92019-07-25 12:28:22 -07004268 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = GetClassLinker()->GetClassRoots();
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004269 if (klass == GetClassRoot<mirror::MethodHandle>(class_roots)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00004270 expected_return_descriptor = mirror::MethodHandle::GetReturnTypeDescriptor(method_name);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004271 } else if (klass == GetClassRoot<mirror::VarHandle>(class_roots)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00004272 expected_return_descriptor = mirror::VarHandle::GetReturnTypeDescriptor(method_name);
4273 } else {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004274 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004275 << "Signature polymorphic method in unsuppported class: " << klass->PrettyDescriptor();
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004276 return false;
4277 }
4278
Orion Hodsonfe92d122018-01-02 10:45:17 +00004279 if (expected_return_descriptor == nullptr) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004280 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4281 << "Signature polymorphic method name invalid: " << method_name;
4282 return false;
4283 }
4284
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004285 const dex::TypeList* types = method->GetParameterTypeList();
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004286 if (types->Size() != 1) {
4287 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4288 << "Signature polymorphic method has too many arguments " << types->Size() << " != 1";
4289 return false;
4290 }
4291
4292 const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_;
4293 const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index);
4294 if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) {
4295 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4296 << "Signature polymorphic method has unexpected argument type: " << argument_descriptor;
4297 return false;
4298 }
4299
4300 const char* return_descriptor = method->GetReturnTypeDescriptor();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004301 if (strcmp(return_descriptor, expected_return_descriptor) != 0) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004302 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004303 << "Signature polymorphic method has unexpected return type: " << return_descriptor
4304 << " != " << expected_return_descriptor;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004305 return false;
4306 }
4307
4308 return true;
4309}
4310
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004311template <bool kVerifierDebug>
4312bool MethodVerifier<kVerifierDebug>::CheckSignaturePolymorphicReceiver(const Instruction* inst) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004313 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004314 if (this_type.IsZeroOrNull()) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004315 /* null pointer always passes (and always fails at run time) */
4316 return true;
4317 } else if (!this_type.IsNonZeroReferenceTypes()) {
4318 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4319 << "invoke-polymorphic receiver is not a reference: "
4320 << this_type;
4321 return false;
4322 } else if (this_type.IsUninitializedReference()) {
4323 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4324 << "invoke-polymorphic receiver is uninitialized: "
4325 << this_type;
4326 return false;
4327 } else if (!this_type.HasClass()) {
4328 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4329 << "invoke-polymorphic receiver has no class: "
4330 << this_type;
4331 return false;
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004332 } else {
Andreas Gampee0bbab92019-07-25 12:28:22 -07004333 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = GetClassLinker()->GetClassRoots();
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004334 if (!this_type.GetClass()->IsSubClass(GetClassRoot<mirror::MethodHandle>(class_roots)) &&
4335 !this_type.GetClass()->IsSubClass(GetClassRoot<mirror::VarHandle>(class_roots))) {
4336 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4337 << "invoke-polymorphic receiver is not a subclass of MethodHandle or VarHandle: "
4338 << this_type;
4339 return false;
4340 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004341 }
4342 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -07004343}
4344
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004345template <bool kVerifierDebug>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004346void MethodVerifier<kVerifierDebug>::VerifyNewArray(const Instruction* inst,
4347 bool is_filled,
4348 bool is_range) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004349 dex::TypeIndex type_idx;
Sebastien Hertz5243e912013-05-21 10:55:07 +02004350 if (!is_filled) {
4351 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004352 type_idx = dex::TypeIndex(inst->VRegC_22c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004353 } else if (!is_range) {
4354 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004355 type_idx = dex::TypeIndex(inst->VRegB_35c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004356 } else {
4357 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004358 type_idx = dex::TypeIndex(inst->VRegB_3rc());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004359 }
Andreas Gampe98be1a92017-08-28 08:25:45 -07004360 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004361 if (res_type.IsConflict()) { // bad class
4362 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004363 } else {
4364 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4365 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004366 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004367 } else if (!is_filled) {
4368 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004369 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004370 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004371 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004372 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004373 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004374 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004375 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4376 // the list and fail. It's legal, if silly, for arg_count to be zero.
Andreas Gampe51de69e2019-04-19 15:14:14 -07004377 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_.Get());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004378 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4379 uint32_t arg[5];
4380 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004381 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004382 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004383 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004384 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004385 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4386 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004387 return;
4388 }
4389 }
4390 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004391 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004392 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004393 }
4394 }
4395}
4396
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004397template <bool kVerifierDebug>
4398void MethodVerifier<kVerifierDebug>::VerifyAGet(const Instruction* inst,
4399 const RegType& insn_type,
4400 bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004401 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004402 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004403 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004404 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004405 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004406 if (array_type.IsZeroOrNull()) {
Ian Rogers89310de2012-02-01 13:47:30 -08004407 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
Andreas Gampe52f205a2017-12-01 12:16:07 -08004408 // instruction type.
4409 if (!is_primitive) {
4410 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Null());
4411 } else if (insn_type.IsInteger()) {
4412 // Pick a non-zero constant (to distinguish with null) that can fit in any primitive.
4413 // We cannot use 'insn_type' as it could be a float array or an int array.
4414 work_line_->SetRegisterType<LockOp::kClear>(
4415 this, inst->VRegA_23x(), DetermineCat1Constant(1, need_precise_constants_));
4416 } else if (insn_type.IsCategory1Types()) {
4417 // Category 1
4418 // The 'insn_type' is exactly the type we need.
4419 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07004420 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004421 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004422 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4423 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004424 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004425 }
jeffhaofc3144e2012-02-01 17:21:15 -08004426 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004427 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004428 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004429 // Unresolved array types must be reference array types.
4430 if (is_primitive) {
4431 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4432 << " source for category 1 aget";
4433 } else {
4434 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4435 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004436 // Approximate with java.lang.Object[].
4437 work_line_->SetRegisterType<LockOp::kClear>(this,
4438 inst->VRegA_23x(),
4439 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004440 }
Ian Rogers89310de2012-02-01 13:47:30 -08004441 } else {
4442 /* verify the class */
Andreas Gampe51de69e2019-04-19 15:14:14 -07004443 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_.Get());
jeffhaofc3144e2012-02-01 17:21:15 -08004444 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004445 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004446 << " source for aget-object";
4447 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004448 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004449 << " source for category 1 aget";
4450 } else if (is_primitive && !insn_type.Equals(component_type) &&
4451 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004452 (insn_type.IsLong() && component_type.IsDouble()))) {
4453 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4454 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004455 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004456 // Use knowledge of the field type which is stronger than the type inferred from the
4457 // instruction, which can't differentiate object types and ints from floats, longs from
4458 // doubles.
4459 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004460 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004461 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004462 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004463 component_type.HighHalf(&reg_types_));
4464 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004465 }
4466 }
4467 }
4468}
4469
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004470template <bool kVerifierDebug>
4471void MethodVerifier<kVerifierDebug>::VerifyPrimitivePut(const RegType& target_type,
4472 const RegType& insn_type,
4473 const uint32_t vregA) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004474 // Primitive assignability rules are weaker than regular assignability rules.
4475 bool instruction_compatible;
4476 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004477 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004478 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004479 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004480 value_compatible = value_type.IsIntegralTypes();
4481 } else if (target_type.IsFloat()) {
4482 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4483 value_compatible = value_type.IsFloatTypes();
4484 } else if (target_type.IsLong()) {
4485 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004486 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4487 // as target_type depends on the resolved type of the field.
4488 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004489 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004490 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4491 } else {
4492 value_compatible = false;
4493 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004494 } else if (target_type.IsDouble()) {
4495 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004496 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4497 // as target_type depends on the resolved type of the field.
4498 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004499 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004500 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4501 } else {
4502 value_compatible = false;
4503 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004504 } else {
4505 instruction_compatible = false; // reference with primitive store
4506 value_compatible = false; // unused
4507 }
4508 if (!instruction_compatible) {
4509 // This is a global failure rather than a class change failure as the instructions and
4510 // the descriptors for the type should have been consistent within the same file at
4511 // compile time.
4512 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4513 << "' but expected type '" << target_type << "'";
4514 return;
4515 }
4516 if (!value_compatible) {
4517 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4518 << " of type " << value_type << " but expected " << target_type << " for put";
4519 return;
4520 }
4521}
4522
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004523template <bool kVerifierDebug>
4524void MethodVerifier<kVerifierDebug>::VerifyAPut(const Instruction* inst,
4525 const RegType& insn_type,
4526 bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004527 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004528 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004529 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004530 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004531 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004532 if (array_type.IsZeroOrNull()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004533 // Null array type; this code path will fail at runtime.
4534 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004535 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4536 // and fits multiple register types.
4537 const RegType* modified_reg_type = &insn_type;
4538 if ((modified_reg_type == &reg_types_.Integer()) ||
4539 (modified_reg_type == &reg_types_.LongLo())) {
4540 // May be integer or float | long or double. Overwrite insn_type accordingly.
4541 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4542 if (modified_reg_type == &reg_types_.Integer()) {
4543 if (&value_type == &reg_types_.Float()) {
4544 modified_reg_type = &value_type;
4545 }
4546 } else {
4547 if (&value_type == &reg_types_.DoubleLo()) {
4548 modified_reg_type = &value_type;
4549 }
4550 }
4551 }
4552 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004553 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004554 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004555 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004556 // Unresolved array types must be reference array types.
4557 if (is_primitive) {
4558 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4559 << "' but unresolved type '" << array_type << "'";
4560 } else {
4561 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4562 << " because of missing class";
4563 }
Ian Rogers89310de2012-02-01 13:47:30 -08004564 } else {
Andreas Gampe51de69e2019-04-19 15:14:14 -07004565 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_.Get());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004566 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004567 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004568 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004569 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004570 if (!component_type.IsReferenceTypes()) {
4571 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4572 << " source for aput-object";
4573 } else {
4574 // The instruction agrees with the type of array, confirm the value to be stored does too
4575 // Note: we use the instruction type (rather than the component type) for aput-object as
4576 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004577 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004578 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004579 }
4580 }
4581 }
4582}
4583
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004584template <bool kVerifierDebug>
4585ArtField* MethodVerifier<kVerifierDebug>::GetStaticField(int field_idx) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004586 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Ian Rogers90040192011-12-16 08:54:29 -08004587 // Check access to class
Andreas Gampe98be1a92017-08-28 08:25:45 -07004588 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004589 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004590 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4591 field_idx, dex_file_->GetFieldName(field_id),
4592 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004593 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004594 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004595 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004596 // Accessibility checks depend on resolved fields.
David Brazdil2bb2fbd2018-11-13 18:24:26 +00004597 DCHECK(klass_type.Equals(GetDeclaringClass()) ||
4598 !failures_.empty() ||
4599 IsSdkVersionSetAndLessThan(api_level_, SdkVersion::kP));
Andreas Gampe629be512017-08-25 17:09:32 -07004600
Ian Rogers7b078e82014-09-10 14:44:24 -07004601 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004602 }
Andreas Gampee0bbab92019-07-25 12:28:22 -07004603 ClassLinker* class_linker = GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004604 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004605
Ian Rogers7b078e82014-09-10 14:44:24 -07004606 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004607 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004608 << dex_file_->GetFieldName(field_id) << ") in "
4609 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004610 DCHECK(self_->IsExceptionPending());
4611 self_->ClearException();
4612 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004613 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4614 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004615 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004616 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004617 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004618 } else if (!field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004619 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004620 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004621 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004622 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004623}
4624
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004625template <bool kVerifierDebug>
4626ArtField* MethodVerifier<kVerifierDebug>::GetInstanceField(const RegType& obj_type, int field_idx) {
Andreas Gampeb34981b2019-05-06 13:00:40 -07004627 if (!obj_type.IsZeroOrNull() && !obj_type.IsReferenceTypes()) {
4628 // Trying to read a field from something that isn't a reference.
4629 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4630 << "non-reference type " << obj_type;
4631 return nullptr;
4632 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004633 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Aart Bik31883642016-06-06 15:02:44 -07004634 // Check access to class.
Andreas Gampe98be1a92017-08-28 08:25:45 -07004635 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004636 if (klass_type.IsConflict()) {
4637 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4638 field_idx, dex_file_->GetFieldName(field_id),
4639 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004640 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004641 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004642 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004643 // Accessibility checks depend on resolved fields.
David Brazdil2bb2fbd2018-11-13 18:24:26 +00004644 DCHECK(klass_type.Equals(GetDeclaringClass()) ||
4645 !failures_.empty() ||
4646 IsSdkVersionSetAndLessThan(api_level_, SdkVersion::kP));
Andreas Gampe629be512017-08-25 17:09:32 -07004647
Ian Rogers7b078e82014-09-10 14:44:24 -07004648 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004649 }
Andreas Gampee0bbab92019-07-25 12:28:22 -07004650 ClassLinker* class_linker = GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004651 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004652
Ian Rogers7b078e82014-09-10 14:44:24 -07004653 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004654 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004655 << dex_file_->GetFieldName(field_id) << ") in "
4656 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004657 DCHECK(self_->IsExceptionPending());
4658 self_->ClearException();
4659 return nullptr;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004660 } else if (obj_type.IsZeroOrNull()) {
Aart Bik31883642016-06-06 15:02:44 -07004661 // Cannot infer and check type, however, access will cause null pointer exception.
4662 // Fall through into a few last soft failure checks below.
Ian Rogerse1758fe2012-04-19 11:31:15 -07004663 } else {
David Brazdil0d638bb2016-07-27 15:29:25 +01004664 std::string temp;
Mathieu Chartier3398c782016-09-30 10:27:43 -07004665 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004666 const RegType& field_klass =
Vladimir Markobcf17522018-06-01 13:14:32 +01004667 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004668 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004669 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004670 // the field is declared in this class.
4671 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4672 // appear in constructors.
4673 if (!obj_type.IsUninitializedThisReference() ||
4674 !IsConstructor() ||
4675 !field_klass.Equals(GetDeclaringClass())) {
David Sehr709b0702016-10-13 09:12:37 -07004676 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField()
David Brazdil68b5c0b2016-01-19 14:25:29 +00004677 << " of a not fully initialized object within the context"
David Sehr709b0702016-10-13 09:12:37 -07004678 << " of " << dex_file_->PrettyMethod(dex_method_idx_);
David Brazdil68b5c0b2016-01-19 14:25:29 +00004679 return nullptr;
4680 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004681 } else if (!field_klass.IsAssignableFrom(obj_type, this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004682 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4683 // of C1. For resolution to occur the declared class of the field must be compatible with
4684 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004685 VerifyError type;
Andreas Gampefef91cc2019-07-25 14:13:23 -07004686 bool is_aot = IsAotMode();
Andreas Gampe66596242016-04-14 10:55:04 -07004687 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4688 // Compiler & unresolved types involved, retry at runtime.
Nicolas Geoffray04ea42c2021-01-22 10:04:56 +00004689 type = VerifyError::VERIFY_ERROR_UNRESOLVED_TYPE_CHECK;
Andreas Gampe66596242016-04-14 10:55:04 -07004690 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004691 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4692 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004693 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4694 }
David Sehr709b0702016-10-13 09:12:37 -07004695 Fail(type) << "cannot access instance field " << field->PrettyField()
Andreas Gampe66596242016-04-14 10:55:04 -07004696 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004697 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004698 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004699 }
Aart Bik31883642016-06-06 15:02:44 -07004700
4701 // Few last soft failure checks.
4702 if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4703 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004704 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004705 << " from " << GetDeclaringClass();
4706 return nullptr;
4707 } else if (field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004708 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004709 << " to not be static";
4710 return nullptr;
4711 }
4712
4713 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004714}
4715
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004716template <bool kVerifierDebug>
4717template <FieldAccessType kAccType>
4718void MethodVerifier<kVerifierDebug>::VerifyISFieldAccess(const Instruction* inst,
4719 const RegType& insn_type,
4720 bool is_primitive,
4721 bool is_static) {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004722 uint32_t field_idx = GetFieldIdxOfFieldAccess(inst, is_static);
Mathieu Chartierc7853442015-03-27 14:35:38 -07004723 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004724 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004725 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004726 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004727 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004728
4729 // One is not allowed to access fields on uninitialized references, except to write to
4730 // fields in the constructor (before calling another constructor).
4731 // GetInstanceField does an assignability check which will fail for uninitialized types.
4732 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4733 // checks at the same time that we're verifying a constructor).
4734 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4735 object_type.IsUninitializedThisReference();
4736 const RegType& adjusted_type = should_adjust
4737 ? GetRegTypeCache()->FromUninitialized(object_type)
4738 : object_type;
4739 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe43884b22019-06-27 14:05:52 -07004740 if (UNLIKELY(flags_.have_pending_hard_failure_)) {
Andreas Gampe896df402014-10-20 22:25:29 -07004741 return;
4742 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08004743 if (should_adjust) {
4744 if (field == nullptr) {
4745 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
4746 << "to the superclass being initialized in "
David Sehr709b0702016-10-13 09:12:37 -07004747 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004748 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4749 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
David Sehr709b0702016-10-13 09:12:37 -07004750 << field->PrettyField() << " of a not fully initialized "
Alex Light4a2c8fc2016-02-12 11:01:54 -08004751 << "object within the context of "
David Sehr709b0702016-10-13 09:12:37 -07004752 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004753 return;
4754 }
4755 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004756 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004757 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004758 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004759 if (kAccType == FieldAccessType::kAccPut) {
4760 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07004761 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Andreas Gampe896df402014-10-20 22:25:29 -07004762 << " from other class " << GetDeclaringClass();
Aart Bikc2bc2652016-05-23 14:58:49 -07004763 // Keep hunting for possible hard fails.
Andreas Gampe896df402014-10-20 22:25:29 -07004764 }
4765 }
4766
Mathieu Chartier3398c782016-09-30 10:27:43 -07004767 ObjPtr<mirror::Class> field_type_class =
Vladimir Marko208f6702017-12-08 12:00:50 +00004768 can_load_classes_ ? field->ResolveType() : field->LookupResolvedType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004769 if (field_type_class != nullptr) {
Mathieu Chartier3398c782016-09-30 10:27:43 -07004770 field_type = &FromClass(field->GetTypeDescriptor(),
Vladimir Markobcf17522018-06-01 13:14:32 +01004771 field_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004772 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004773 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004774 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4775 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004776 }
David Brazdil2bb2fbd2018-11-13 18:24:26 +00004777 } else if (IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP)) {
Andreas Gampe7da4c402017-08-25 11:30:48 -07004778 // If we don't have the field (it seems we failed resolution) and this is a PUT, we need to
4779 // redo verification at runtime as the field may be final, unless the field id shows it's in
4780 // the same class.
4781 //
4782 // For simplicity, it is OK to not distinguish compile-time vs runtime, and post this an
4783 // ACCESS_FIELD failure at runtime. This has the same effect as NO_FIELD - punting the class
4784 // to the access-checks interpreter.
4785 //
4786 // Note: see b/34966607. This and above may be changed in the future.
4787 if (kAccType == FieldAccessType::kAccPut) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004788 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Andreas Gampe7da4c402017-08-25 11:30:48 -07004789 const char* field_class_descriptor = dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Andreas Gampe51de69e2019-04-19 15:14:14 -07004790 const RegType* field_class_type = &reg_types_.FromDescriptor(class_loader_.Get(),
Andreas Gampe7da4c402017-08-25 11:30:48 -07004791 field_class_descriptor,
4792 false);
4793 if (!field_class_type->Equals(GetDeclaringClass())) {
4794 Fail(VERIFY_ERROR_ACCESS_FIELD) << "could not check field put for final field modify of "
4795 << field_class_descriptor
4796 << "."
4797 << dex_file_->GetFieldName(field_id)
4798 << " from other class "
4799 << GetDeclaringClass();
4800 }
4801 }
Ian Rogers0d604842012-04-16 14:50:24 -07004802 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004803 if (field_type == nullptr) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004804 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004805 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Andreas Gampe51de69e2019-04-19 15:14:14 -07004806 field_type = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004807 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004808 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004809 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004810 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4811 "Unexpected third access type");
4812 if (kAccType == FieldAccessType::kAccPut) {
4813 // sput or iput.
4814 if (is_primitive) {
4815 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004816 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004817 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004818 // If the field type is not a reference, this is a global failure rather than
4819 // a class change failure as the instructions and the descriptors for the type
4820 // should have been consistent within the same file at compile time.
4821 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4822 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004823 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004824 << " to be compatible with type '" << insn_type
4825 << "' but found type '" << *field_type
4826 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004827 return;
4828 }
4829 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004830 }
Andreas Gampe896df402014-10-20 22:25:29 -07004831 } else if (kAccType == FieldAccessType::kAccGet) {
4832 // sget or iget.
4833 if (is_primitive) {
4834 if (field_type->Equals(insn_type) ||
4835 (field_type->IsFloat() && insn_type.IsInteger()) ||
4836 (field_type->IsDouble() && insn_type.IsLong())) {
4837 // expected that read is of the correct primitive type or that int reads are reading
4838 // floats or long reads are reading doubles
4839 } else {
4840 // This is a global failure rather than a class change failure as the instructions and
4841 // the descriptors for the type should have been consistent within the same file at
4842 // compile time
David Sehr709b0702016-10-13 09:12:37 -07004843 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07004844 << " to be of type '" << insn_type
4845 << "' but found type '" << *field_type << "' in get";
4846 return;
4847 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004848 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004849 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004850 // If the field type is not a reference, this is a global failure rather than
4851 // a class change failure as the instructions and the descriptors for the type
4852 // should have been consistent within the same file at compile time.
4853 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4854 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004855 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004856 << " to be compatible with type '" << insn_type
4857 << "' but found type '" << *field_type
4858 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004859 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004860 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004861 }
Andreas Gampe896df402014-10-20 22:25:29 -07004862 return;
4863 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004864 }
Andreas Gampe896df402014-10-20 22:25:29 -07004865 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004866 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004867 } else {
4868 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4869 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004870 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004871 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004872 }
4873}
4874
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004875template <bool kVerifierDebug>
4876bool MethodVerifier<kVerifierDebug>::UpdateRegisters(uint32_t next_insn,
4877 RegisterLine* merge_line,
4878 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004879 bool changed = true;
4880 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004881 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004882 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004883 * We haven't processed this instruction before, and we haven't touched the registers here, so
4884 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4885 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004886 */
Andreas Gampea727e372015-08-25 09:22:37 -07004887 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004888 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004889 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004890 merge_line->VerifyMonitorStackEmpty(this);
4891
Ian Rogersb8c78592013-07-25 23:52:52 +00004892 // For returns we only care about the operand to the return, all other registers are dead.
4893 // Initialize them as conflicts so they don't add to GC and deoptimization information.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004894 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004895 AdjustReturnLine(this, ret_inst, target_line);
Aart Bik31883642016-06-06 15:02:44 -07004896 // Directly bail if a hard failure was found.
Andreas Gampe43884b22019-06-27 14:05:52 -07004897 if (flags_.have_pending_hard_failure_) {
Aart Bikb0526322016-06-01 14:06:00 -07004898 return false;
4899 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004900 }
jeffhaobdb76512011-09-07 11:43:16 -07004901 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08004902 RegisterLineArenaUniquePtr copy;
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004903 if (kVerifierDebug) {
Andreas Gamped09c0592019-04-19 15:44:05 -07004904 copy.reset(RegisterLine::Create(target_line->NumRegs(), allocator_, GetRegTypeCache()));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004905 copy->CopyFromLine(target_line);
4906 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004907 changed = target_line->MergeRegisters(this, merge_line);
Andreas Gampe43884b22019-06-27 14:05:52 -07004908 if (flags_.have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004909 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004910 }
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004911 if (kVerifierDebug && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004912 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004913 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004914 << copy->Dump(this) << " MERGE\n"
4915 << merge_line->Dump(this) << " ==\n"
Andreas Gampe2ad6cce2019-04-11 16:17:39 -07004916 << target_line->Dump(this);
jeffhaobdb76512011-09-07 11:43:16 -07004917 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004918 if (update_merge_line && changed) {
4919 merge_line->CopyFromLine(target_line);
4920 }
jeffhaobdb76512011-09-07 11:43:16 -07004921 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004922 if (changed) {
Andreas Gampe51de69e2019-04-19 15:14:14 -07004923 GetModifiableInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004924 }
4925 return true;
4926}
4927
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004928template <bool kVerifierDebug>
4929const RegType& MethodVerifier<kVerifierDebug>::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004930 if (return_type_ == nullptr) {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004931 if (method_being_verified_ != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01004932 ObjPtr<mirror::Class> return_type_class = can_load_classes_
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004933 ? method_being_verified_->ResolveReturnType()
4934 : method_being_verified_->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004935 if (return_type_class != nullptr) {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00004936 return_type_ = &FromClass(method_being_verified_->GetReturnTypeDescriptor(),
Vladimir Markobcf17522018-06-01 13:14:32 +01004937 return_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004938 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004939 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004940 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4941 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004942 }
4943 }
4944 if (return_type_ == nullptr) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004945 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4946 const dex::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004947 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004948 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Andreas Gampe51de69e2019-04-19 15:14:14 -07004949 return_type_ = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004950 }
4951 }
4952 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004953}
4954
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004955template <bool kVerifierDebug>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004956const RegType& MethodVerifier<kVerifierDebug>::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004957 if (precise) {
4958 // Precise constant type.
4959 return reg_types_.FromCat1Const(value, true);
4960 } else {
4961 // Imprecise constant type.
4962 if (value < -32768) {
4963 return reg_types_.IntConstant();
4964 } else if (value < -128) {
4965 return reg_types_.ShortConstant();
4966 } else if (value < 0) {
4967 return reg_types_.ByteConstant();
4968 } else if (value == 0) {
4969 return reg_types_.Zero();
4970 } else if (value == 1) {
4971 return reg_types_.One();
4972 } else if (value < 128) {
4973 return reg_types_.PosByteConstant();
4974 } else if (value < 32768) {
4975 return reg_types_.PosShortConstant();
4976 } else if (value < 65536) {
4977 return reg_types_.CharConstant();
4978 } else {
4979 return reg_types_.IntConstant();
4980 }
4981 }
4982}
4983
Nicolas Geoffray44dc8a32021-06-21 15:23:49 +01004984template <bool kVerifierDebug>
4985void MethodVerifier<kVerifierDebug>::PotentiallyMarkRuntimeThrow() {
4986 if (IsAotMode() || IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kT)) {
4987 return;
4988 }
4989 flags_.have_pending_runtime_throw_failure_ = true;
4990 // How to handle runtime failures for instructions that are not flagged kThrow.
4991 //
4992 // The verifier may fail before we touch any instruction, for the signature of a method. So
4993 // add a check.
4994 if (work_insn_idx_ < dex::kDexNoIndex) {
4995 const Instruction& inst = code_item_accessor_.InstructionAt(work_insn_idx_);
4996 Instruction::Code opcode = inst.Opcode();
4997 if ((Instruction::FlagsOf(opcode) & Instruction::kThrow) == 0 &&
4998 !impl::IsCompatThrow(opcode) &&
4999 GetInstructionFlags(work_insn_idx_).IsInTry()) {
5000 if (Runtime::Current()->IsVerifierMissingKThrowFatal()) {
5001 LOG(FATAL) << "Unexpected throw: " << std::hex << work_insn_idx_ << " " << opcode;
5002 UNREACHABLE();
5003 }
5004 // We need to save the work_line if the instruction wasn't throwing before. Otherwise
5005 // we'll try to merge garbage.
5006 // Note: this assumes that Fail is called before we do any work_line modifications.
5007 saved_line_->CopyFromLine(work_line_.get());
5008 }
5009 }
5010}
5011
Andreas Gampefc25ae92019-04-19 22:22:57 -07005012} // namespace
5013} // namespace impl
5014
5015MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005016 ClassLinker* class_linker,
Andreas Gampef1468b52019-07-26 09:22:39 -07005017 ArenaPool* arena_pool,
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +00005018 VerifierDeps* verifier_deps,
Andreas Gampefc25ae92019-04-19 22:22:57 -07005019 const DexFile* dex_file,
Nicolas Geoffray1960c422020-11-04 08:45:32 +00005020 const dex::ClassDef& class_def,
Andreas Gampefc25ae92019-04-19 22:22:57 -07005021 const dex::CodeItem* code_item,
5022 uint32_t dex_method_idx,
5023 bool can_load_classes,
5024 bool allow_thread_suspension,
Andreas Gampefef91cc2019-07-25 14:13:23 -07005025 bool allow_soft_failures,
5026 bool aot_mode)
Andreas Gampefc25ae92019-04-19 22:22:57 -07005027 : self_(self),
Andreas Gampef1468b52019-07-26 09:22:39 -07005028 arena_stack_(arena_pool),
Andreas Gampefc25ae92019-04-19 22:22:57 -07005029 allocator_(&arena_stack_),
Andreas Gampee0bbab92019-07-25 12:28:22 -07005030 reg_types_(class_linker, can_load_classes, allocator_, allow_thread_suspension),
Andreas Gampefc25ae92019-04-19 22:22:57 -07005031 reg_table_(allocator_),
5032 work_insn_idx_(dex::kDexNoIndex),
5033 dex_method_idx_(dex_method_idx),
5034 dex_file_(dex_file),
Nicolas Geoffray1960c422020-11-04 08:45:32 +00005035 class_def_(class_def),
Andreas Gampefc25ae92019-04-19 22:22:57 -07005036 code_item_accessor_(*dex_file, code_item),
Andreas Gampefef91cc2019-07-25 14:13:23 -07005037 // TODO: make it designated initialization when we compile as C++20.
Nicolas Geoffrayf8b52882021-06-25 17:00:30 +01005038 flags_({false, false, false, aot_mode}),
Andreas Gampefc25ae92019-04-19 22:22:57 -07005039 encountered_failure_types_(0),
5040 can_load_classes_(can_load_classes),
5041 allow_soft_failures_(allow_soft_failures),
Andreas Gampee0bbab92019-07-25 12:28:22 -07005042 class_linker_(class_linker),
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +00005043 verifier_deps_(verifier_deps),
Andreas Gampefc25ae92019-04-19 22:22:57 -07005044 link_(nullptr) {
5045 self->PushVerifier(this);
5046}
5047
5048MethodVerifier::~MethodVerifier() {
5049 Thread::Current()->PopVerifier(this);
5050 STLDeleteElements(&failure_messages_);
5051}
5052
5053MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005054 ClassLinker* class_linker,
Andreas Gampef1468b52019-07-26 09:22:39 -07005055 ArenaPool* arena_pool,
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +00005056 VerifierDeps* verifier_deps,
Andreas Gampefc25ae92019-04-19 22:22:57 -07005057 uint32_t method_idx,
5058 const DexFile* dex_file,
5059 Handle<mirror::DexCache> dex_cache,
5060 Handle<mirror::ClassLoader> class_loader,
5061 const dex::ClassDef& class_def,
5062 const dex::CodeItem* code_item,
5063 ArtMethod* method,
5064 uint32_t method_access_flags,
5065 CompilerCallbacks* callbacks,
Alex Lightc2d0c962019-10-23 14:14:25 -07005066 VerifierCallback* verifier_callback,
Andreas Gampefc25ae92019-04-19 22:22:57 -07005067 bool allow_soft_failures,
5068 HardFailLogMode log_level,
5069 bool need_precise_constants,
5070 uint32_t api_level,
Andreas Gampefef91cc2019-07-25 14:13:23 -07005071 bool aot_mode,
Andreas Gampefc25ae92019-04-19 22:22:57 -07005072 std::string* hard_failure_msg) {
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005073 if (VLOG_IS_ON(verifier_debug)) {
5074 return VerifyMethod<true>(self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005075 class_linker,
Andreas Gampef1468b52019-07-26 09:22:39 -07005076 arena_pool,
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +00005077 verifier_deps,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005078 method_idx,
5079 dex_file,
5080 dex_cache,
5081 class_loader,
5082 class_def,
5083 code_item,
5084 method,
5085 method_access_flags,
5086 callbacks,
Alex Lightc2d0c962019-10-23 14:14:25 -07005087 verifier_callback,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005088 allow_soft_failures,
5089 log_level,
5090 need_precise_constants,
5091 api_level,
Andreas Gampefef91cc2019-07-25 14:13:23 -07005092 aot_mode,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005093 hard_failure_msg);
5094 } else {
5095 return VerifyMethod<false>(self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005096 class_linker,
Andreas Gampef1468b52019-07-26 09:22:39 -07005097 arena_pool,
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +00005098 verifier_deps,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005099 method_idx,
5100 dex_file,
5101 dex_cache,
5102 class_loader,
5103 class_def,
5104 code_item,
5105 method,
5106 method_access_flags,
5107 callbacks,
Alex Lightc2d0c962019-10-23 14:14:25 -07005108 verifier_callback,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005109 allow_soft_failures,
5110 log_level,
5111 need_precise_constants,
5112 api_level,
Andreas Gampefef91cc2019-07-25 14:13:23 -07005113 aot_mode,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005114 hard_failure_msg);
5115 }
5116}
5117
Nicolas Geoffray1715efa2020-06-05 18:34:49 +01005118// Return whether the runtime knows how to execute a method without needing to
Nicolas Geoffraybd570592020-12-23 16:37:44 +00005119// re-verify it at runtime (and therefore save on first use of the class).
Nicolas Geoffray1715efa2020-06-05 18:34:49 +01005120// The AOT/JIT compiled code is not affected.
5121static inline bool CanRuntimeHandleVerificationFailure(uint32_t encountered_failure_types) {
5122 constexpr uint32_t unresolved_mask =
Nicolas Geoffrayd1728bf2021-01-12 14:02:29 +00005123 verifier::VerifyError::VERIFY_ERROR_UNRESOLVED_TYPE_CHECK |
Nicolas Geoffray04ea42c2021-01-22 10:04:56 +00005124 verifier::VerifyError::VERIFY_ERROR_NO_CLASS |
Nicolas Geoffrayb677aff2020-12-23 17:07:10 +00005125 verifier::VerifyError::VERIFY_ERROR_CLASS_CHANGE |
Nicolas Geoffray54ed0152020-12-23 17:29:32 +00005126 verifier::VerifyError::VERIFY_ERROR_INSTANTIATION |
Nicolas Geoffray1715efa2020-06-05 18:34:49 +01005127 verifier::VerifyError::VERIFY_ERROR_ACCESS_CLASS |
5128 verifier::VerifyError::VERIFY_ERROR_ACCESS_FIELD |
Nicolas Geoffraybd570592020-12-23 16:37:44 +00005129 verifier::VerifyError::VERIFY_ERROR_NO_METHOD |
Nicolas Geoffray1715efa2020-06-05 18:34:49 +01005130 verifier::VerifyError::VERIFY_ERROR_ACCESS_METHOD;
5131 return (encountered_failure_types & (~unresolved_mask)) == 0;
5132}
5133
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005134template <bool kVerifierDebug>
5135MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005136 ClassLinker* class_linker,
Andreas Gampef1468b52019-07-26 09:22:39 -07005137 ArenaPool* arena_pool,
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +00005138 VerifierDeps* verifier_deps,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005139 uint32_t method_idx,
5140 const DexFile* dex_file,
5141 Handle<mirror::DexCache> dex_cache,
5142 Handle<mirror::ClassLoader> class_loader,
5143 const dex::ClassDef& class_def,
5144 const dex::CodeItem* code_item,
5145 ArtMethod* method,
5146 uint32_t method_access_flags,
5147 CompilerCallbacks* callbacks,
Alex Lightc2d0c962019-10-23 14:14:25 -07005148 VerifierCallback* verifier_callback,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005149 bool allow_soft_failures,
5150 HardFailLogMode log_level,
5151 bool need_precise_constants,
5152 uint32_t api_level,
Andreas Gampefef91cc2019-07-25 14:13:23 -07005153 bool aot_mode,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005154 std::string* hard_failure_msg) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005155 MethodVerifier::FailureData result;
5156 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
5157
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005158 impl::MethodVerifier<kVerifierDebug> verifier(self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005159 class_linker,
Andreas Gampef1468b52019-07-26 09:22:39 -07005160 arena_pool,
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +00005161 verifier_deps,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005162 dex_file,
Andreas Gampe0134a282019-07-29 11:34:55 -07005163 code_item,
5164 method_idx,
Alex Lightc2d0c962019-10-23 14:14:25 -07005165 /* can_load_classes= */ true,
5166 /* allow_thread_suspension= */ true,
Andreas Gampe0134a282019-07-29 11:34:55 -07005167 allow_soft_failures,
5168 aot_mode,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005169 dex_cache,
5170 class_loader,
5171 class_def,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005172 method,
5173 method_access_flags,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005174 need_precise_constants,
5175 /* verify to dump */ false,
Alex Lighte2ddce32019-05-22 17:08:35 +00005176 /* fill_register_lines= */ false,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005177 api_level);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005178 if (verifier.Verify()) {
5179 // Verification completed, however failures may be pending that didn't cause the verification
5180 // to hard fail.
Andreas Gampe43884b22019-06-27 14:05:52 -07005181 CHECK(!verifier.flags_.have_pending_hard_failure_);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005182
5183 if (code_item != nullptr && callbacks != nullptr) {
5184 // Let the interested party know that the method was verified.
5185 callbacks->MethodVerified(&verifier);
5186 }
5187
Alex Lightc2d0c962019-10-23 14:14:25 -07005188 bool set_dont_compile = false;
Nicolas Geoffrayc3c44172021-01-07 10:03:39 +00005189 bool must_count_locks = false;
Andreas Gampefc25ae92019-04-19 22:22:57 -07005190 if (verifier.failures_.size() != 0) {
5191 if (VLOG_IS_ON(verifier)) {
5192 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
5193 << dex_file->PrettyMethod(method_idx) << "\n");
5194 }
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005195 if (kVerifierDebug) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005196 LOG(INFO) << verifier.info_messages_.str();
5197 verifier.Dump(LOG_STREAM(INFO));
5198 }
Nicolas Geoffray1715efa2020-06-05 18:34:49 +01005199 if (CanRuntimeHandleVerificationFailure(verifier.encountered_failure_types_)) {
Nicolas Geoffrayd1728bf2021-01-12 14:02:29 +00005200 if (verifier.encountered_failure_types_ & VERIFY_ERROR_UNRESOLVED_TYPE_CHECK) {
5201 result.kind = FailureKind::kTypeChecksFailure;
5202 } else {
5203 result.kind = FailureKind::kAccessChecksFailure;
5204 }
Nicolas Geoffray1715efa2020-06-05 18:34:49 +01005205 } else {
5206 result.kind = FailureKind::kSoftFailure;
5207 }
Nicolas Geoffrayc3c44172021-01-07 10:03:39 +00005208 if (!CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) {
Alex Lightc2d0c962019-10-23 14:14:25 -07005209 set_dont_compile = true;
Andreas Gampefc25ae92019-04-19 22:22:57 -07005210 }
Andreas Gampefc25ae92019-04-19 22:22:57 -07005211 if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) {
Alex Lightc2d0c962019-10-23 14:14:25 -07005212 must_count_locks = true;
Andreas Gampefc25ae92019-04-19 22:22:57 -07005213 }
Nicolas Geoffrayc3c44172021-01-07 10:03:39 +00005214 }
5215
Nicolas Geoffrayc3c44172021-01-07 10:03:39 +00005216 if (method != nullptr) {
Alex Lightc2d0c962019-10-23 14:14:25 -07005217 verifier_callback->SetDontCompile(method, set_dont_compile);
5218 verifier_callback->SetMustCountLocks(method, must_count_locks);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005219 }
5220 } else {
5221 // Bad method data.
5222 CHECK_NE(verifier.failures_.size(), 0U);
Nicolas Geoffrayf8b52882021-06-25 17:00:30 +01005223 CHECK(verifier.flags_.have_pending_hard_failure_);
5224 if (VLOG_IS_ON(verifier)) {
5225 log_level = std::max(HardFailLogMode::kLogVerbose, log_level);
5226 }
5227 if (log_level >= HardFailLogMode::kLogVerbose) {
5228 LogSeverity severity;
5229 switch (log_level) {
5230 case HardFailLogMode::kLogVerbose:
5231 severity = LogSeverity::VERBOSE;
5232 break;
5233 case HardFailLogMode::kLogWarning:
5234 severity = LogSeverity::WARNING;
5235 break;
5236 case HardFailLogMode::kLogInternalFatal:
5237 severity = LogSeverity::FATAL_WITHOUT_ABORT;
5238 break;
5239 default:
5240 LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level);
5241 UNREACHABLE();
5242 }
5243 verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in "
5244 << dex_file->PrettyMethod(method_idx)
5245 << "\n");
5246 }
5247 if (hard_failure_msg != nullptr) {
5248 CHECK(!verifier.failure_messages_.empty());
5249 *hard_failure_msg =
5250 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
5251 }
5252 result.kind = FailureKind::kHardFailure;
Andreas Gampefc25ae92019-04-19 22:22:57 -07005253
Nicolas Geoffrayf8b52882021-06-25 17:00:30 +01005254 if (callbacks != nullptr) {
5255 // Let the interested party know that we failed the class.
5256 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(class_def));
5257 callbacks->ClassRejected(ref);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005258 }
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005259 if (kVerifierDebug || VLOG_IS_ON(verifier)) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005260 LOG(ERROR) << verifier.info_messages_.str();
5261 verifier.Dump(LOG_STREAM(ERROR));
5262 }
5263 // Under verifier-debug, dump the complete log into the error message.
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005264 if (kVerifierDebug && hard_failure_msg != nullptr) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005265 hard_failure_msg->append("\n");
5266 hard_failure_msg->append(verifier.info_messages_.str());
5267 hard_failure_msg->append("\n");
5268 std::ostringstream oss;
5269 verifier.Dump(oss);
5270 hard_failure_msg->append(oss.str());
5271 }
5272 }
5273 if (kTimeVerifyMethod) {
5274 uint64_t duration_ns = NanoTime() - start_ns;
5275 if (duration_ns > MsToNs(Runtime::Current()->GetVerifierLoggingThresholdMs())) {
Andreas Gampeefdd1b02019-05-07 12:30:10 -07005276 double bytecodes_per_second =
5277 verifier.code_item_accessor_.InsnsSizeInCodeUnits() / (duration_ns * 1e-9);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005278 LOG(WARNING) << "Verification of " << dex_file->PrettyMethod(method_idx)
5279 << " took " << PrettyDuration(duration_ns)
Andreas Gampeefdd1b02019-05-07 12:30:10 -07005280 << (impl::IsLargeMethod(verifier.CodeItem()) ? " (large method)" : "")
Andreas Gamped91f8412019-06-13 10:11:14 -07005281 << " (" << StringPrintf("%.2f", bytecodes_per_second) << " bytecodes/s)"
5282 << " (" << verifier.allocator_.ApproximatePeakBytes()
5283 << "B approximate peak alloc)";
Andreas Gampefc25ae92019-04-19 22:22:57 -07005284 }
5285 }
5286 result.types = verifier.encountered_failure_types_;
5287 return result;
5288}
5289
Alex Lighte2ddce32019-05-22 17:08:35 +00005290MethodVerifier* MethodVerifier::CalculateVerificationInfo(
5291 Thread* self,
5292 ArtMethod* method,
5293 Handle<mirror::DexCache> dex_cache,
5294 Handle<mirror::ClassLoader> class_loader) {
5295 std::unique_ptr<impl::MethodVerifier<false>> verifier(
5296 new impl::MethodVerifier<false>(self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005297 Runtime::Current()->GetClassLinker(),
Andreas Gampef1468b52019-07-26 09:22:39 -07005298 Runtime::Current()->GetArenaPool(),
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +00005299 /* verifier_deps= */ nullptr,
Alex Lighte2ddce32019-05-22 17:08:35 +00005300 method->GetDexFile(),
Andreas Gampe0134a282019-07-29 11:34:55 -07005301 method->GetCodeItem(),
5302 method->GetDexMethodIndex(),
5303 /* can_load_classes= */ false,
5304 /* allow_thread_suspension= */ false,
5305 /* allow_soft_failures= */ true,
5306 Runtime::Current()->IsAotCompiler(),
Alex Lighte2ddce32019-05-22 17:08:35 +00005307 dex_cache,
5308 class_loader,
5309 *method->GetDeclaringClass()->GetClassDef(),
Alex Lighte2ddce32019-05-22 17:08:35 +00005310 method,
5311 method->GetAccessFlags(),
Alex Lighte2ddce32019-05-22 17:08:35 +00005312 /* need_precise_constants= */ true,
5313 /* verify_to_dump= */ false,
Alex Lighte2ddce32019-05-22 17:08:35 +00005314 /* fill_register_lines= */ true,
Alex Lightaf52cbe2019-05-22 15:05:09 -07005315 // Just use the verifier at the current skd-version.
5316 // This might affect what soft-verifier errors are reported.
5317 // Callers can then filter out relevant errors if needed.
5318 Runtime::Current()->GetTargetSdkVersion()));
Alex Lighte2ddce32019-05-22 17:08:35 +00005319 verifier->Verify();
5320 if (VLOG_IS_ON(verifier)) {
5321 verifier->DumpFailures(VLOG_STREAM(verifier));
5322 VLOG(verifier) << verifier->info_messages_.str();
5323 verifier->Dump(VLOG_STREAM(verifier));
5324 }
Andreas Gampe43884b22019-06-27 14:05:52 -07005325 if (verifier->flags_.have_pending_hard_failure_) {
Alex Lighte2ddce32019-05-22 17:08:35 +00005326 return nullptr;
5327 } else {
5328 return verifier.release();
5329 }
5330}
5331
Andreas Gampefc25ae92019-04-19 22:22:57 -07005332MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
5333 VariableIndentationOutputStream* vios,
5334 uint32_t dex_method_idx,
5335 const DexFile* dex_file,
5336 Handle<mirror::DexCache> dex_cache,
5337 Handle<mirror::ClassLoader> class_loader,
5338 const dex::ClassDef& class_def,
5339 const dex::CodeItem* code_item,
5340 ArtMethod* method,
5341 uint32_t method_access_flags,
5342 uint32_t api_level) {
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005343 impl::MethodVerifier<false>* verifier = new impl::MethodVerifier<false>(
5344 self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005345 Runtime::Current()->GetClassLinker(),
Andreas Gampef1468b52019-07-26 09:22:39 -07005346 Runtime::Current()->GetArenaPool(),
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +00005347 /* verifier_deps= */ nullptr,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005348 dex_file,
Andreas Gampe0134a282019-07-29 11:34:55 -07005349 code_item,
5350 dex_method_idx,
5351 /* can_load_classes= */ true,
5352 /* allow_thread_suspension= */ true,
5353 /* allow_soft_failures= */ true,
5354 Runtime::Current()->IsAotCompiler(),
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005355 dex_cache,
5356 class_loader,
5357 class_def,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005358 method,
5359 method_access_flags,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005360 /* need_precise_constants= */ true,
5361 /* verify_to_dump= */ true,
Alex Lighte2ddce32019-05-22 17:08:35 +00005362 /* fill_register_lines= */ false,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005363 api_level);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005364 verifier->Verify();
5365 verifier->DumpFailures(vios->Stream());
5366 vios->Stream() << verifier->info_messages_.str();
5367 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
5368 // and querying any info is dangerous/can abort.
Andreas Gampe43884b22019-06-27 14:05:52 -07005369 if (verifier->flags_.have_pending_hard_failure_) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005370 delete verifier;
5371 return nullptr;
5372 } else {
5373 verifier->Dump(vios);
5374 return verifier;
5375 }
5376}
5377
5378void MethodVerifier::FindLocksAtDexPc(
5379 ArtMethod* m,
5380 uint32_t dex_pc,
5381 std::vector<MethodVerifier::DexLockInfo>* monitor_enter_dex_pcs,
5382 uint32_t api_level) {
5383 StackHandleScope<2> hs(Thread::Current());
5384 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
5385 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005386 impl::MethodVerifier<false> verifier(hs.Self(),
Andreas Gampee0bbab92019-07-25 12:28:22 -07005387 Runtime::Current()->GetClassLinker(),
Andreas Gampef1468b52019-07-26 09:22:39 -07005388 Runtime::Current()->GetArenaPool(),
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +00005389 /* verifier_deps= */ nullptr,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005390 m->GetDexFile(),
Andreas Gampe0134a282019-07-29 11:34:55 -07005391 m->GetCodeItem(),
5392 m->GetDexMethodIndex(),
5393 /* can_load_classes= */ false,
5394 /* allow_thread_suspension= */ false,
5395 /* allow_soft_failures= */ true,
5396 Runtime::Current()->IsAotCompiler(),
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005397 dex_cache,
5398 class_loader,
5399 m->GetClassDef(),
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005400 m,
5401 m->GetAccessFlags(),
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005402 /* need_precise_constants= */ false,
5403 /* verify_to_dump= */ false,
Alex Lighte2ddce32019-05-22 17:08:35 +00005404 /* fill_register_lines= */ false,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005405 api_level);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005406 verifier.interesting_dex_pc_ = dex_pc;
5407 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
5408 verifier.FindLocksAtDexPc();
5409}
5410
5411MethodVerifier* MethodVerifier::CreateVerifier(Thread* self,
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +00005412 VerifierDeps* verifier_deps,
Andreas Gampefc25ae92019-04-19 22:22:57 -07005413 const DexFile* dex_file,
5414 Handle<mirror::DexCache> dex_cache,
5415 Handle<mirror::ClassLoader> class_loader,
5416 const dex::ClassDef& class_def,
5417 const dex::CodeItem* code_item,
5418 uint32_t method_idx,
5419 ArtMethod* method,
5420 uint32_t access_flags,
5421 bool can_load_classes,
5422 bool allow_soft_failures,
5423 bool need_precise_constants,
5424 bool verify_to_dump,
5425 bool allow_thread_suspension,
5426 uint32_t api_level) {
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005427 return new impl::MethodVerifier<false>(self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005428 Runtime::Current()->GetClassLinker(),
Andreas Gampef1468b52019-07-26 09:22:39 -07005429 Runtime::Current()->GetArenaPool(),
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +00005430 verifier_deps,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005431 dex_file,
Andreas Gampe0134a282019-07-29 11:34:55 -07005432 code_item,
5433 method_idx,
5434 can_load_classes,
5435 allow_thread_suspension,
5436 allow_soft_failures,
5437 Runtime::Current()->IsAotCompiler(),
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005438 dex_cache,
5439 class_loader,
5440 class_def,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005441 method,
5442 access_flags,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005443 need_precise_constants,
5444 verify_to_dump,
Alex Lighte2ddce32019-05-22 17:08:35 +00005445 /* fill_register_lines= */ false,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005446 api_level);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005447}
5448
Andreas Gampee0bbab92019-07-25 12:28:22 -07005449void MethodVerifier::Init(ClassLinker* class_linker) {
5450 art::verifier::RegTypeCache::Init(class_linker);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08005451}
5452
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005453void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005454 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08005455}
jeffhaod1224c72012-02-29 13:43:08 -08005456
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005457void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
5458 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07005459}
5460
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005461void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
5462 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08005463}
5464
Andreas Gampe4146e062019-07-10 13:18:04 -07005465std::ostream& MethodVerifier::Fail(VerifyError error, bool pending_exc) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005466 // Mark the error type as encountered.
5467 encountered_failure_types_ |= static_cast<uint32_t>(error);
5468
Andreas Gampe4146e062019-07-10 13:18:04 -07005469 if (pending_exc) {
5470 switch (error) {
5471 case VERIFY_ERROR_NO_CLASS:
Nicolas Geoffray04ea42c2021-01-22 10:04:56 +00005472 case VERIFY_ERROR_UNRESOLVED_TYPE_CHECK:
Andreas Gampe4146e062019-07-10 13:18:04 -07005473 case VERIFY_ERROR_NO_METHOD:
5474 case VERIFY_ERROR_ACCESS_CLASS:
5475 case VERIFY_ERROR_ACCESS_FIELD:
5476 case VERIFY_ERROR_ACCESS_METHOD:
5477 case VERIFY_ERROR_INSTANTIATION:
Nicolas Geoffray44dc8a32021-06-21 15:23:49 +01005478 case VERIFY_ERROR_CLASS_CHANGE: {
5479 PotentiallyMarkRuntimeThrow();
Andreas Gampe4146e062019-07-10 13:18:04 -07005480 break;
Nicolas Geoffray44dc8a32021-06-21 15:23:49 +01005481 }
Andreas Gampe7a82acc2019-07-01 13:54:21 -07005482
Nicolas Geoffray35a37202021-01-29 17:53:32 +00005483 case VERIFY_ERROR_LOCKING:
Nicolas Geoffray44dc8a32021-06-21 15:23:49 +01005484 PotentiallyMarkRuntimeThrow();
Nicolas Geoffray35a37202021-01-29 17:53:32 +00005485 // This will be reported to the runtime as a soft failure.
5486 break;
5487
Nicolas Geoffrayd1728bf2021-01-12 14:02:29 +00005488 // Indication that verification should be retried at runtime.
Andreas Gampe4146e062019-07-10 13:18:04 -07005489 case VERIFY_ERROR_BAD_CLASS_SOFT:
5490 if (!allow_soft_failures_) {
5491 flags_.have_pending_hard_failure_ = true;
5492 }
5493 break;
5494
Nicolas Geoffray35a37202021-01-29 17:53:32 +00005495 // Hard verification failures at compile time will still fail at runtime, so the class is
5496 // marked as rejected to prevent it from being compiled.
Andreas Gampe4146e062019-07-10 13:18:04 -07005497 case VERIFY_ERROR_BAD_CLASS_HARD: {
Andreas Gampe4a30f892019-07-10 04:57:58 +00005498 flags_.have_pending_hard_failure_ = true;
Andreas Gampe4146e062019-07-10 13:18:04 -07005499 break;
Andreas Gampe4a30f892019-07-10 04:57:58 +00005500 }
Andreas Gampe4a30f892019-07-10 04:57:58 +00005501
Andreas Gampe0d87f992019-07-10 13:19:45 -07005502 case VERIFY_ERROR_SKIP_COMPILER:
Andreas Gampe4146e062019-07-10 13:18:04 -07005503 // Nothing to do, just remember the failure type.
5504 break;
Andreas Gampe4a30f892019-07-10 04:57:58 +00005505 }
Andreas Gampe4146e062019-07-10 13:18:04 -07005506 } else if (kIsDebugBuild) {
5507 CHECK_NE(error, VERIFY_ERROR_BAD_CLASS_SOFT);
5508 CHECK_NE(error, VERIFY_ERROR_BAD_CLASS_HARD);
Andreas Gampe4a30f892019-07-10 04:57:58 +00005509 }
Andreas Gampe4146e062019-07-10 13:18:04 -07005510
Andreas Gampefc25ae92019-04-19 22:22:57 -07005511 failures_.push_back(error);
5512 std::string location(StringPrintf("%s: [0x%X] ", dex_file_->PrettyMethod(dex_method_idx_).c_str(),
5513 work_insn_idx_));
5514 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
5515 failure_messages_.push_back(failure_message);
5516 return *failure_message;
Andreas Gampef23f33d2015-06-23 14:18:17 -07005517}
5518
Andreas Gampefc25ae92019-04-19 22:22:57 -07005519ScopedNewLine MethodVerifier::LogVerifyInfo() {
5520 ScopedNewLine ret{info_messages_};
5521 ret << "VFY: " << dex_file_->PrettyMethod(dex_method_idx_)
5522 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
5523 return ret;
5524}
5525
5526static FailureKind FailureKindMax(FailureKind fk1, FailureKind fk2) {
5527 static_assert(FailureKind::kNoFailure < FailureKind::kSoftFailure
5528 && FailureKind::kSoftFailure < FailureKind::kHardFailure,
5529 "Unexpected FailureKind order");
5530 return std::max(fk1, fk2);
5531}
5532
5533void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) {
5534 kind = FailureKindMax(kind, fd.kind);
5535 types |= fd.types;
Andreas Gampe51de69e2019-04-19 15:14:14 -07005536}
5537
Ian Rogersd81871c2011-10-03 13:57:23 -07005538} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005539} // namespace art