blob: 29bc40c30c8d54548aa69261392a4f3d71ba3ed8 [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 Markoc7aa87e2018-05-24 15:19:52 +010037#include "class_root.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) {
132 return opcode == Instruction::Code::RETURN_OBJECT;
133}
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,
Andreas Gampefc25ae92019-04-19 22:22:57 -0700156 const DexFile* dex_file,
Andreas Gampe0134a282019-07-29 11:34:55 -0700157 const dex::CodeItem* code_item,
158 uint32_t method_idx,
159 bool can_load_classes,
160 bool allow_thread_suspension,
161 bool allow_soft_failures,
162 bool aot_mode,
Andreas Gampefc25ae92019-04-19 22:22:57 -0700163 Handle<mirror::DexCache> dex_cache,
164 Handle<mirror::ClassLoader> class_loader,
165 const dex::ClassDef& class_def,
Andreas Gampefc25ae92019-04-19 22:22:57 -0700166 ArtMethod* method,
167 uint32_t access_flags,
Andreas Gampefc25ae92019-04-19 22:22:57 -0700168 bool need_precise_constants,
169 bool verify_to_dump,
Andreas Gampe86bf0682019-07-29 11:22:44 -0700170 bool fill_register_lines,
Andreas Gampe86bf0682019-07-29 11:22:44 -0700171 uint32_t api_level) REQUIRES_SHARED(Locks::mutator_lock_)
172 : art::verifier::MethodVerifier(self,
173 class_linker,
174 arena_pool,
175 dex_file,
176 code_item,
177 method_idx,
178 can_load_classes,
179 allow_thread_suspension,
180 allow_soft_failures,
181 aot_mode),
182 method_being_verified_(method),
183 method_access_flags_(access_flags),
184 return_type_(nullptr),
185 dex_cache_(dex_cache),
186 class_loader_(class_loader),
187 class_def_(class_def),
188 declaring_class_(nullptr),
189 interesting_dex_pc_(-1),
190 monitor_enter_dex_pcs_(nullptr),
191 need_precise_constants_(need_precise_constants),
192 verify_to_dump_(verify_to_dump),
193 allow_thread_suspension_(allow_thread_suspension),
194 is_constructor_(false),
195 fill_register_lines_(fill_register_lines),
196 api_level_(api_level == 0 ? std::numeric_limits<uint32_t>::max() : api_level) {
197 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700198
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700199 void UninstantiableError(const char* descriptor) {
200 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
201 << "non-instantiable klass " << descriptor;
202 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700203 static bool IsInstantiableOrPrimitive(ObjPtr<mirror::Class> klass)
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700204 REQUIRES_SHARED(Locks::mutator_lock_) {
205 return klass->IsInstantiable() || klass->IsPrimitive();
206 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700207
208 // Is the method being verified a constructor? See the comment on the field.
209 bool IsConstructor() const {
210 return is_constructor_;
211 }
212
213 // Is the method verified static?
214 bool IsStatic() const {
215 return (method_access_flags_ & kAccStatic) != 0;
216 }
217
218 // Adds the given string to the beginning of the last failure message.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700219 void PrependToLastFailMessage(std::string prepend) {
220 size_t failure_num = failure_messages_.size();
221 DCHECK_NE(failure_num, 0U);
222 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
223 prepend += last_fail_message->str();
224 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
225 delete last_fail_message;
226 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700227
228 // Adds the given string to the end of the last failure message.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700229 void AppendToLastFailMessage(const std::string& append) {
230 size_t failure_num = failure_messages_.size();
231 DCHECK_NE(failure_num, 0U);
232 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
233 (*last_fail_message) << append;
234 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700235
Andreas Gampefc25ae92019-04-19 22:22:57 -0700236 /*
237 * Compute the width of the instruction at each address in the instruction stream, and store it in
238 * insn_flags_. Addresses that are in the middle of an instruction, or that are part of switch
239 * table data, are not touched (so the caller should probably initialize "insn_flags" to zero).
240 *
241 * The "new_instance_count_" and "monitor_enter_count_" fields in vdata are also set.
242 *
243 * Performs some static checks, notably:
244 * - opcode of first instruction begins at index 0
245 * - only documented instructions may appear
246 * - each instruction follows the last
247 * - last byte of last instruction is at (code_length-1)
248 *
249 * Logs an error and returns "false" on failure.
250 */
251 bool ComputeWidthsAndCountOps();
252
253 /*
254 * Set the "in try" flags for all instructions protected by "try" statements. Also sets the
255 * "branch target" flags for exception handlers.
256 *
257 * Call this after widths have been set in "insn_flags".
258 *
259 * Returns "false" if something in the exception table looks fishy, but we're expecting the
260 * exception table to be somewhat sane.
261 */
262 bool ScanTryCatchBlocks() REQUIRES_SHARED(Locks::mutator_lock_);
263
264 /*
265 * Perform static verification on all instructions in a method.
266 *
267 * Walks through instructions in a method calling VerifyInstruction on each.
268 */
269 template <bool kAllowRuntimeOnlyInstructions>
270 bool VerifyInstructions();
271
272 /*
273 * Perform static verification on an instruction.
274 *
275 * As a side effect, this sets the "branch target" flags in InsnFlags.
276 *
277 * "(CF)" items are handled during code-flow analysis.
278 *
279 * v3 4.10.1
280 * - target of each jump and branch instruction must be valid
281 * - targets of switch statements must be valid
282 * - operands referencing constant pool entries must be valid
283 * - (CF) operands of getfield, putfield, getstatic, putstatic must be valid
284 * - (CF) operands of method invocation instructions must be valid
285 * - (CF) only invoke-direct can call a method starting with '<'
286 * - (CF) <clinit> must never be called explicitly
287 * - operands of instanceof, checkcast, new (and variants) must be valid
288 * - new-array[-type] limited to 255 dimensions
289 * - can't use "new" on an array class
290 * - (?) limit dimensions in multi-array creation
291 * - local variable load/store register values must be in valid range
292 *
293 * v3 4.11.1.2
294 * - branches must be within the bounds of the code array
295 * - targets of all control-flow instructions are the start of an instruction
296 * - register accesses fall within range of allocated registers
297 * - (N/A) access to constant pool must be of appropriate type
298 * - code does not end in the middle of an instruction
299 * - execution cannot fall off the end of the code
300 * - (earlier) for each exception handler, the "try" area must begin and
301 * end at the start of an instruction (end can be at the end of the code)
302 * - (earlier) for each exception handler, the handler must start at a valid
303 * instruction
304 */
305 template <bool kAllowRuntimeOnlyInstructions>
306 bool VerifyInstruction(const Instruction* inst, uint32_t code_offset);
307
308 /* Ensure that the register index is valid for this code item. */
Andreas Gampe86bf0682019-07-29 11:22:44 -0700309 bool CheckRegisterIndex(uint32_t idx) {
310 if (UNLIKELY(idx >= code_item_accessor_.RegistersSize())) {
311 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
312 << code_item_accessor_.RegistersSize() << ")";
313 return false;
314 }
315 return true;
316 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700317
318 /* Ensure that the wide register index is valid for this code item. */
Andreas Gampe86bf0682019-07-29 11:22:44 -0700319 bool CheckWideRegisterIndex(uint32_t idx) {
320 if (UNLIKELY(idx + 1 >= code_item_accessor_.RegistersSize())) {
321 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
322 << "+1 >= " << code_item_accessor_.RegistersSize() << ")";
323 return false;
324 }
325 return true;
326 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700327
328 // Perform static checks on an instruction referencing a CallSite. All we do here is ensure that
329 // the call site index is in the valid range.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700330 bool CheckCallSiteIndex(uint32_t idx) {
331 uint32_t limit = dex_file_->NumCallSiteIds();
332 if (UNLIKELY(idx >= limit)) {
333 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad call site index " << idx << " (max "
334 << limit << ")";
335 return false;
336 }
337 return true;
338 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700339
340 // Perform static checks on a field Get or set instruction. All we do here is ensure that the
341 // field index is in the valid range.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700342 bool CheckFieldIndex(uint32_t idx) {
343 if (UNLIKELY(idx >= dex_file_->GetHeader().field_ids_size_)) {
344 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
345 << dex_file_->GetHeader().field_ids_size_ << ")";
346 return false;
347 }
348 return true;
349 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700350
351 // Perform static checks on a method invocation instruction. All we do here is ensure that the
352 // method index is in the valid range.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700353 bool CheckMethodIndex(uint32_t idx) {
354 if (UNLIKELY(idx >= dex_file_->GetHeader().method_ids_size_)) {
355 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
356 << dex_file_->GetHeader().method_ids_size_ << ")";
357 return false;
358 }
359 return true;
360 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700361
362 // Perform static checks on an instruction referencing a constant method handle. All we do here
363 // is ensure that the method index is in the valid range.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700364 bool CheckMethodHandleIndex(uint32_t idx) {
365 uint32_t limit = dex_file_->NumMethodHandles();
366 if (UNLIKELY(idx >= limit)) {
367 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method handle index " << idx << " (max "
368 << limit << ")";
369 return false;
370 }
371 return true;
372 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700373
374 // Perform static checks on a "new-instance" instruction. Specifically, make sure the class
375 // reference isn't for an array class.
376 bool CheckNewInstance(dex::TypeIndex idx);
377
378 // Perform static checks on a prototype indexing instruction. All we do here is ensure that the
379 // prototype index is in the valid range.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700380 bool CheckPrototypeIndex(uint32_t idx) {
381 if (UNLIKELY(idx >= dex_file_->GetHeader().proto_ids_size_)) {
382 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad prototype index " << idx << " (max "
383 << dex_file_->GetHeader().proto_ids_size_ << ")";
384 return false;
385 }
386 return true;
387 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700388
389 /* Ensure that the string index is in the valid range. */
Andreas Gampe86bf0682019-07-29 11:22:44 -0700390 bool CheckStringIndex(uint32_t idx) {
391 if (UNLIKELY(idx >= dex_file_->GetHeader().string_ids_size_)) {
392 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
393 << dex_file_->GetHeader().string_ids_size_ << ")";
394 return false;
395 }
396 return true;
397 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700398
399 // Perform static checks on an instruction that takes a class constant. Ensure that the class
400 // index is in the valid range.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700401 bool CheckTypeIndex(dex::TypeIndex idx) {
402 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
403 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
404 << dex_file_->GetHeader().type_ids_size_ << ")";
405 return false;
406 }
407 return true;
408 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700409
410 // Perform static checks on a "new-array" instruction. Specifically, make sure they aren't
411 // creating an array of arrays that causes the number of dimensions to exceed 255.
412 bool CheckNewArray(dex::TypeIndex idx);
413
414 // Verify an array data table. "cur_offset" is the offset of the fill-array-data instruction.
415 bool CheckArrayData(uint32_t cur_offset);
416
417 // Verify that the target of a branch instruction is valid. We don't expect code to jump directly
418 // into an exception handler, but it's valid to do so as long as the target isn't a
419 // "move-exception" instruction. We verify that in a later stage.
420 // The dex format forbids certain instructions from branching to themselves.
421 // Updates "insn_flags_", setting the "branch target" flag.
422 bool CheckBranchTarget(uint32_t cur_offset);
423
424 // Verify a switch table. "cur_offset" is the offset of the switch instruction.
425 // Updates "insn_flags_", setting the "branch target" flag.
426 bool CheckSwitchTargets(uint32_t cur_offset);
427
428 // Check the register indices used in a "vararg" instruction, such as invoke-virtual or
429 // filled-new-array.
430 // - vA holds word count (0-5), args[] have values.
431 // There are some tests we don't do here, e.g. we don't try to verify that invoking a method that
432 // takes a double is done with consecutive registers. This requires parsing the target method
433 // signature, which we will be doing later on during the code flow analysis.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700434 bool CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
435 uint16_t registers_size = code_item_accessor_.RegistersSize();
436 for (uint32_t idx = 0; idx < vA; idx++) {
437 if (UNLIKELY(arg[idx] >= registers_size)) {
438 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
439 << ") in non-range invoke (>= " << registers_size << ")";
440 return false;
441 }
442 }
443
444 return true;
445 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700446
447 // Check the register indices used in a "vararg/range" instruction, such as invoke-virtual/range
448 // or filled-new-array/range.
449 // - vA holds word count, vC holds index of first reg.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700450 bool CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
451 uint16_t registers_size = code_item_accessor_.RegistersSize();
452 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
453 // integer overflow when adding them here.
454 if (UNLIKELY(vA + vC > registers_size)) {
455 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
456 << " in range invoke (> " << registers_size << ")";
457 return false;
458 }
459 return true;
460 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700461
462 // Checks the method matches the expectations required to be signature polymorphic.
463 bool CheckSignaturePolymorphicMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
464
465 // Checks the invoked receiver matches the expectations for signature polymorphic methods.
466 bool CheckSignaturePolymorphicReceiver(const Instruction* inst) REQUIRES_SHARED(Locks::mutator_lock_);
467
468 // Extract the relative offset from a branch instruction.
469 // Returns "false" on failure (e.g. this isn't a branch instruction).
470 bool GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
471 bool* selfOkay);
472
473 /* Perform detailed code-flow analysis on a single method. */
474 bool VerifyCodeFlow() REQUIRES_SHARED(Locks::mutator_lock_);
475
476 // Set the register types for the first instruction in the method based on the method signature.
477 // This has the side-effect of validating the signature.
478 bool SetTypesFromSignature() REQUIRES_SHARED(Locks::mutator_lock_);
479
480 /*
481 * Perform code flow on a method.
482 *
483 * The basic strategy is as outlined in v3 4.11.1.2: set the "changed" bit on the first
484 * instruction, process it (setting additional "changed" bits), and repeat until there are no
485 * more.
486 *
487 * v3 4.11.1.1
488 * - (N/A) operand stack is always the same size
489 * - operand stack [registers] contain the correct types of values
490 * - local variables [registers] contain the correct types of values
491 * - methods are invoked with the appropriate arguments
492 * - fields are assigned using values of appropriate types
493 * - opcodes have the correct type values in operand registers
494 * - there is never an uninitialized class instance in a local variable in code protected by an
495 * exception handler (operand stack is okay, because the operand stack is discarded when an
496 * exception is thrown) [can't know what's a local var w/o the debug info -- should fall out of
497 * register typing]
498 *
499 * v3 4.11.1.2
500 * - execution cannot fall off the end of the code
501 *
502 * (We also do many of the items described in the "static checks" sections, because it's easier to
503 * do them here.)
504 *
505 * We need an array of RegType values, one per register, for every instruction. If the method uses
506 * monitor-enter, we need extra data for every register, and a stack for every "interesting"
507 * instruction. In theory this could become quite large -- up to several megabytes for a monster
508 * function.
509 *
510 * NOTE:
511 * The spec forbids backward branches when there's an uninitialized reference in a register. The
512 * idea is to prevent something like this:
513 * loop:
514 * move r1, r0
515 * new-instance r0, MyClass
516 * ...
517 * if-eq rN, loop // once
518 * initialize r0
519 *
520 * This leaves us with two different instances, both allocated by the same instruction, but only
521 * one is initialized. The scheme outlined in v3 4.11.1.4 wouldn't catch this, so they work around
522 * it by preventing backward branches. We achieve identical results without restricting code
523 * reordering by specifying that you can't execute the new-instance instruction if a register
524 * contains an uninitialized instance created by that same instruction.
525 */
Andreas Gampeb90cad52019-07-31 16:15:41 -0700526 template <bool kMonitorDexPCs>
Andreas Gampefc25ae92019-04-19 22:22:57 -0700527 bool CodeFlowVerifyMethod() REQUIRES_SHARED(Locks::mutator_lock_);
528
529 /*
530 * Perform verification for a single instruction.
531 *
532 * This requires fully decoding the instruction to determine the effect it has on registers.
533 *
534 * Finds zero or more following instructions and sets the "changed" flag if execution at that
535 * point needs to be (re-)evaluated. Register changes are merged into "reg_types_" at the target
536 * addresses. Does not set or clear any other flags in "insn_flags_".
537 */
538 bool CodeFlowVerifyInstruction(uint32_t* start_guess)
539 REQUIRES_SHARED(Locks::mutator_lock_);
540
541 // Perform verification of a new array instruction
542 void VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range)
543 REQUIRES_SHARED(Locks::mutator_lock_);
544
545 // Helper to perform verification on puts of primitive type.
546 void VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
547 const uint32_t vregA) REQUIRES_SHARED(Locks::mutator_lock_);
548
549 // Perform verification of an aget instruction. The destination register's type will be set to
550 // be that of component type of the array unless the array type is unknown, in which case a
551 // bottom type inferred from the type of instruction is used. is_primitive is false for an
552 // aget-object.
553 void VerifyAGet(const Instruction* inst, const RegType& insn_type,
554 bool is_primitive) REQUIRES_SHARED(Locks::mutator_lock_);
555
556 // Perform verification of an aput instruction.
557 void VerifyAPut(const Instruction* inst, const RegType& insn_type,
558 bool is_primitive) REQUIRES_SHARED(Locks::mutator_lock_);
559
560 // Lookup instance field and fail for resolution violations
561 ArtField* GetInstanceField(const RegType& obj_type, int field_idx)
562 REQUIRES_SHARED(Locks::mutator_lock_);
563
564 // Lookup static field and fail for resolution violations
565 ArtField* GetStaticField(int field_idx) REQUIRES_SHARED(Locks::mutator_lock_);
566
567 // Perform verification of an iget/sget/iput/sput instruction.
Andreas Gampefc25ae92019-04-19 22:22:57 -0700568 template <FieldAccessType kAccType>
569 void VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
570 bool is_primitive, bool is_static)
571 REQUIRES_SHARED(Locks::mutator_lock_);
572
Andreas Gampefc25ae92019-04-19 22:22:57 -0700573 // Resolves a class based on an index and, if C is kYes, performs access checks to ensure
574 // the referrer can access the resolved class.
575 template <CheckAccess C>
576 const RegType& ResolveClass(dex::TypeIndex class_idx)
577 REQUIRES_SHARED(Locks::mutator_lock_);
578
579 /*
580 * For the "move-exception" instruction at "work_insn_idx_", which must be at an exception handler
581 * address, determine the Join of all exceptions that can land here. Fails if no matching
582 * exception handler can be found or if the Join of exception types fails.
583 */
584 const RegType& GetCaughtExceptionType()
585 REQUIRES_SHARED(Locks::mutator_lock_);
586
587 /*
588 * Resolves a method based on an index and performs access checks to ensure
589 * the referrer can access the resolved method.
590 * Does not throw exceptions.
591 */
592 ArtMethod* ResolveMethodAndCheckAccess(uint32_t method_idx, MethodType method_type)
593 REQUIRES_SHARED(Locks::mutator_lock_);
594
595 /*
596 * Verify the arguments to a method. We're executing in "method", making
597 * a call to the method reference in vB.
598 *
599 * If this is a "direct" invoke, we allow calls to <init>. For calls to
600 * <init>, the first argument may be an uninitialized reference. Otherwise,
601 * calls to anything starting with '<' will be rejected, as will any
602 * uninitialized reference arguments.
603 *
604 * For non-static method calls, this will verify that the method call is
605 * appropriate for the "this" argument.
606 *
607 * The method reference is in vBBBB. The "is_range" parameter determines
608 * whether we use 0-4 "args" values or a range of registers defined by
609 * vAA and vCCCC.
610 *
611 * Widening conversions on integers and references are allowed, but
612 * narrowing conversions are not.
613 *
614 * Returns the resolved method on success, null on failure (with *failure
615 * set appropriately).
616 */
617 ArtMethod* VerifyInvocationArgs(const Instruction* inst, MethodType method_type, bool is_range)
618 REQUIRES_SHARED(Locks::mutator_lock_);
619
620 // Similar checks to the above, but on the proto. Will be used when the method cannot be
621 // resolved.
622 void VerifyInvocationArgsUnresolvedMethod(const Instruction* inst, MethodType method_type,
623 bool is_range)
624 REQUIRES_SHARED(Locks::mutator_lock_);
625
626 template <class T>
627 ArtMethod* VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
628 MethodType method_type, bool is_range,
629 ArtMethod* res_method)
630 REQUIRES_SHARED(Locks::mutator_lock_);
631
632 /*
633 * Verify the arguments present for a call site. Returns "true" if all is well, "false" otherwise.
634 */
635 bool CheckCallSite(uint32_t call_site_idx);
636
637 /*
638 * Verify that the target instruction is not "move-exception". It's important that the only way
639 * to execute a move-exception is as the first instruction of an exception handler.
640 * Returns "true" if all is well, "false" if the target instruction is move-exception.
641 */
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700642 bool CheckNotMoveException(const uint16_t* insns, int insn_idx) {
643 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
644 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
645 return false;
646 }
647 return true;
648 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700649
650 /*
651 * Verify that the target instruction is not "move-result". It is important that we cannot
652 * branch to move-result instructions, but we have to make this a distinct check instead of
653 * adding it to CheckNotMoveException, because it is legal to continue into "move-result"
654 * instructions - as long as the previous instruction was an invoke, which is checked elsewhere.
655 */
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700656 bool CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
657 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
658 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
659 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
660 return false;
661 }
662 return true;
663 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700664
665 /*
666 * Verify that the target instruction is not "move-result" or "move-exception". This is to
667 * be used when checking branch and switch instructions, but not instructions that can
668 * continue.
669 */
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700670 bool CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
671 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
672 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700673
674 /*
675 * Control can transfer to "next_insn". Merge the registers from merge_line into the table at
676 * next_insn, and set the changed flag on the target address if any of the registers were changed.
677 * In the case of fall-through, update the merge line on a change as its the working line for the
678 * next instruction.
679 * Returns "false" if an error is encountered.
680 */
681 bool UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line, bool update_merge_line)
682 REQUIRES_SHARED(Locks::mutator_lock_);
683
684 // Return the register type for the method.
685 const RegType& GetMethodReturnType() REQUIRES_SHARED(Locks::mutator_lock_);
686
687 // Get a type representing the declaring class of the method.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700688 const RegType& GetDeclaringClass() REQUIRES_SHARED(Locks::mutator_lock_) {
689 if (declaring_class_ == nullptr) {
690 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
691 const char* descriptor
692 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
693 if (method_being_verified_ != nullptr) {
694 ObjPtr<mirror::Class> klass = method_being_verified_->GetDeclaringClass();
695 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
696 } else {
697 declaring_class_ = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
698 }
699 }
700 return *declaring_class_;
701 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700702
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700703 InstructionFlags* CurrentInsnFlags() {
704 return &GetModifiableInstructionFlags(work_insn_idx_);
705 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700706
707 const RegType& DetermineCat1Constant(int32_t value, bool precise)
708 REQUIRES_SHARED(Locks::mutator_lock_);
709
710 // Try to create a register type from the given class. In case a precise type is requested, but
711 // the class is not instantiable, a soft error (of type NO_CLASS) will be enqueued and a
712 // non-precise reference will be returned.
713 // Note: we reuse NO_CLASS as this will throw an exception at runtime, when the failing class is
714 // actually touched.
715 const RegType& FromClass(const char* descriptor, ObjPtr<mirror::Class> klass, bool precise)
Andreas Gampe86bf0682019-07-29 11:22:44 -0700716 REQUIRES_SHARED(Locks::mutator_lock_) {
717 DCHECK(klass != nullptr);
718 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
719 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
720 << "non-instantiable klass " << descriptor;
721 precise = false;
722 }
723 return reg_types_.FromClass(descriptor, klass, precise);
724 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700725
726 ALWAYS_INLINE bool FailOrAbort(bool condition, const char* error_msg, uint32_t work_insn_idx);
727
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700728 ALWAYS_INLINE InstructionFlags& GetModifiableInstructionFlags(size_t index) {
729 return insn_flags_[index];
730 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700731
732 // Returns the method index of an invoke instruction.
733 uint16_t GetMethodIdxOfInvoke(const Instruction* inst)
Andreas Gampe86bf0682019-07-29 11:22:44 -0700734 REQUIRES_SHARED(Locks::mutator_lock_) {
735 switch (inst->Opcode()) {
736 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
737 case Instruction::INVOKE_VIRTUAL_QUICK: {
738 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
739 << dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_;
740 DCHECK(method_being_verified_ != nullptr);
741 uint16_t method_idx = method_being_verified_->GetIndexFromQuickening(work_insn_idx_);
742 CHECK_NE(method_idx, DexFile::kDexNoIndex16);
743 return method_idx;
744 }
745 default: {
746 return inst->VRegB();
747 }
748 }
749 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700750 // Returns the field index of a field access instruction.
751 uint16_t GetFieldIdxOfFieldAccess(const Instruction* inst, bool is_static)
Andreas Gampe86bf0682019-07-29 11:22:44 -0700752 REQUIRES_SHARED(Locks::mutator_lock_) {
753 if (is_static) {
754 return inst->VRegB_21c();
755 } else if (inst->IsQuickened()) {
756 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
757 DCHECK(method_being_verified_ != nullptr);
758 uint16_t field_idx = method_being_verified_->GetIndexFromQuickening(work_insn_idx_);
759 CHECK_NE(field_idx, DexFile::kDexNoIndex16);
760 return field_idx;
761 } else {
762 return inst->VRegC_22c();
763 }
764 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700765
766 // Run verification on the method. Returns true if verification completes and false if the input
767 // has an irrecoverable corruption.
768 bool Verify() override REQUIRES_SHARED(Locks::mutator_lock_);
769
770 // Dump the failures encountered by the verifier.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700771 std::ostream& DumpFailures(std::ostream& os) {
772 DCHECK_EQ(failures_.size(), failure_messages_.size());
Andreas Gampe4ec4d482019-08-01 10:00:23 -0700773 for (const auto* stream : failure_messages_) {
774 os << stream->str() << "\n";
Andreas Gampe86bf0682019-07-29 11:22:44 -0700775 }
776 return os;
777 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700778
779 // Dump the state of the verifier, namely each instruction, what flags are set on it, register
780 // information
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700781 void Dump(std::ostream& os) REQUIRES_SHARED(Locks::mutator_lock_) {
782 VariableIndentationOutputStream vios(&os);
783 Dump(&vios);
784 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700785 void Dump(VariableIndentationOutputStream* vios) REQUIRES_SHARED(Locks::mutator_lock_);
786
Andreas Gampe6087bc22019-06-03 15:52:08 -0700787 bool HandleMoveException(const Instruction* inst) REQUIRES_SHARED(Locks::mutator_lock_);
788
Andreas Gampefc25ae92019-04-19 22:22:57 -0700789 ArtMethod* method_being_verified_; // Its ArtMethod representation if known.
790 const uint32_t method_access_flags_; // Method's access flags.
791 const RegType* return_type_; // Lazily computed return type of the method.
792 // The dex_cache for the declaring class of the method.
793 Handle<mirror::DexCache> dex_cache_ GUARDED_BY(Locks::mutator_lock_);
794 // The class loader for the declaring class of the method.
795 Handle<mirror::ClassLoader> class_loader_ GUARDED_BY(Locks::mutator_lock_);
796 const dex::ClassDef& class_def_; // The class def of the declaring class of the method.
797 const RegType* declaring_class_; // Lazily computed reg type of the method's declaring class.
798
799 // The dex PC of a FindLocksAtDexPc request, -1 otherwise.
800 uint32_t interesting_dex_pc_;
801 // The container into which FindLocksAtDexPc should write the registers containing held locks,
802 // null if we're not doing FindLocksAtDexPc.
803 std::vector<DexLockInfo>* monitor_enter_dex_pcs_;
804
805
806 // An optimization where instead of generating unique RegTypes for constants we use imprecise
807 // constants that cover a range of constants. This isn't good enough for deoptimization that
808 // avoids loading from registers in the case of a constant as the dex instruction set lost the
809 // notion of whether a value should be in a floating point or general purpose register file.
810 const bool need_precise_constants_;
811
812 // Indicates whether we verify to dump the info. In that case we accept quickened instructions
813 // even though we might detect to be a compiler. Should only be set when running
814 // VerifyMethodAndDump.
815 const bool verify_to_dump_;
816
817 // Whether or not we call AllowThreadSuspension periodically, we want a way to disable this for
818 // thread dumping checkpoints since we may get thread suspension at an inopportune time due to
819 // FindLocksAtDexPC, resulting in deadlocks.
820 const bool allow_thread_suspension_;
821
822 // Whether the method seems to be a constructor. Note that this field exists as we can't trust
823 // the flags in the dex file. Some older code does not mark methods named "<init>" and "<clinit>"
824 // correctly.
825 //
826 // Note: this flag is only valid once Verify() has started.
827 bool is_constructor_;
828
Alex Lighte2ddce32019-05-22 17:08:35 +0000829 // Whether to attempt to fill all register lines for (ex) debugger use.
830 bool fill_register_lines_;
831
Andreas Gampefc25ae92019-04-19 22:22:57 -0700832 // API level, for dependent checks. Note: we do not use '0' for unset here, to simplify checks.
833 // Instead, unset level should correspond to max().
834 const uint32_t api_level_;
835
836 friend class ::art::verifier::MethodVerifier;
837
838 DISALLOW_COPY_AND_ASSIGN(MethodVerifier);
839};
840
Andreas Gampe7c038102014-10-27 20:08:46 -0700841// Note: returns true on failure.
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700842template <bool kVerifierDebug>
843inline bool MethodVerifier<kVerifierDebug>::FailOrAbort(bool condition,
844 const char* error_msg,
845 uint32_t work_insn_idx) {
Andreas Gampe7c038102014-10-27 20:08:46 -0700846 if (kIsDebugBuild) {
Andreas Gampef8f36c22016-11-19 14:51:52 -0800847 // In a debug build, abort if the error condition is wrong. Only warn if
848 // we are already aborting (as this verification is likely run to print
849 // lock information).
850 if (LIKELY(gAborting == 0)) {
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700851 DCHECK(condition) << error_msg << work_insn_idx << " "
852 << dex_file_->PrettyMethod(dex_method_idx_);
Andreas Gampef8f36c22016-11-19 14:51:52 -0800853 } else {
854 if (!condition) {
855 LOG(ERROR) << error_msg << work_insn_idx;
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700856 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
Andreas Gampef8f36c22016-11-19 14:51:52 -0800857 return true;
858 }
859 }
Andreas Gampe7c038102014-10-27 20:08:46 -0700860 } else {
861 // In a non-debug build, just fail the class.
862 if (!condition) {
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700863 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
Andreas Gampe7c038102014-10-27 20:08:46 -0700864 return true;
865 }
866 }
867
868 return false;
869}
870
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800871static bool IsLargeMethod(const CodeItemDataAccessor& accessor) {
872 if (!accessor.HasCodeItem()) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700873 return false;
874 }
875
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800876 uint16_t registers_size = accessor.RegistersSize();
877 uint32_t insns_size = accessor.InsnsSizeInCodeUnits();
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700878
879 return registers_size * insns_size > 4*1024*1024;
880}
881
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700882template <bool kVerifierDebug>
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700883void MethodVerifier<kVerifierDebug>::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700884 CHECK(monitor_enter_dex_pcs_ != nullptr);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800885 CHECK(code_item_accessor_.HasCodeItem()); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700886
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800887 // Quick check whether there are any monitor_enter instructions before verifying.
888 for (const DexInstructionPcPair& inst : code_item_accessor_) {
889 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
890 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
891 // verification. In practice, the phase we want relies on data structures set up by all the
892 // earlier passes, so we just run the full method verification and bail out early when we've
893 // got what we wanted.
894 Verify();
895 return;
896 }
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700897 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700898}
899
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700900template <bool kVerifierDebug>
901bool MethodVerifier<kVerifierDebug>::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700902 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
903 // the name.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800904 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Andreas Gampee6215c02015-08-31 18:54:38 -0700905 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
906 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
907 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
908 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
909 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
910 if ((method_access_flags_ & kAccConstructor) != 0) {
911 if (!constructor_by_name) {
912 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
913 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700914 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700915 }
916 is_constructor_ = true;
917 } else if (constructor_by_name) {
David Sehr709b0702016-10-13 09:12:37 -0700918 LOG(WARNING) << "Method " << dex_file_->PrettyMethod(dex_method_idx_)
Andreas Gampee6215c02015-08-31 18:54:38 -0700919 << " not marked as constructor.";
920 is_constructor_ = true;
921 }
922 // If it's a constructor, check whether IsStatic() matches the name.
923 // This should have been rejected by the dex file verifier. Only do in debug build.
924 if (kIsDebugBuild) {
925 if (IsConstructor()) {
926 if (IsStatic() ^ static_constructor_by_name) {
927 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
928 << "constructor name doesn't match static flag";
929 return false;
930 }
jeffhaobdb76512011-09-07 11:43:16 -0700931 }
jeffhaobdb76512011-09-07 11:43:16 -0700932 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700933
934 // Methods may only have one of public/protected/private.
935 // This should have been rejected by the dex file verifier. Only do in debug build.
936 if (kIsDebugBuild) {
937 size_t access_mod_count =
938 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
939 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
940 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
941 if (access_mod_count > 1) {
942 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
943 return false;
944 }
945 }
946
947 // If there aren't any instructions, make sure that's expected, then exit successfully.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800948 if (!code_item_accessor_.HasCodeItem()) {
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700949 // Only native or abstract methods may not have code.
950 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
951 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
952 return false;
953 }
954
Andreas Gampee6215c02015-08-31 18:54:38 -0700955 // This should have been rejected by the dex file verifier. Only do in debug build.
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700956 // Note: the above will also be rejected in the dex file verifier, starting in dex version 37.
Andreas Gampee6215c02015-08-31 18:54:38 -0700957 if (kIsDebugBuild) {
Andreas Gampee6215c02015-08-31 18:54:38 -0700958 if ((method_access_flags_ & kAccAbstract) != 0) {
959 // Abstract methods are not allowed to have the following flags.
960 static constexpr uint32_t kForbidden =
961 kAccPrivate |
962 kAccStatic |
963 kAccFinal |
964 kAccNative |
965 kAccStrict |
966 kAccSynchronized;
967 if ((method_access_flags_ & kForbidden) != 0) {
968 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
969 << "method can't be abstract and private/static/final/native/strict/synchronized";
970 return false;
971 }
972 }
David Brazdil15fc7292016-09-02 14:13:18 +0100973 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700974 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000975 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700976 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000977 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700978 return false;
979 }
980 // In addition to the above, interface methods must not be protected.
981 static constexpr uint32_t kForbidden = kAccProtected;
982 if ((method_access_flags_ & kForbidden) != 0) {
983 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
984 return false;
985 }
986 }
987 // We also don't allow constructors to be abstract or native.
988 if (IsConstructor()) {
989 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
990 return false;
991 }
992 }
993 return true;
994 }
995
996 // This should have been rejected by the dex file verifier. Only do in debug build.
997 if (kIsDebugBuild) {
998 // When there's code, the method must not be native or abstract.
999 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
1000 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
1001 return false;
1002 }
1003
David Brazdil15fc7292016-09-02 14:13:18 +01001004 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -07001005 // Interfaces may always have static initializers for their fields. If we are running with
1006 // default methods enabled we also allow other public, static, non-final methods to have code.
1007 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -07001008 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +00001009 if (IsInstanceConstructor()) {
1010 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
1011 return false;
1012 } else if (method_access_flags_ & kAccFinal) {
1013 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
1014 return false;
Alex Lightb55f1ac2016-04-12 15:50:55 -07001015 } else {
1016 uint32_t access_flag_options = kAccPublic;
Mathieu Chartierf6e31472017-12-28 13:32:08 -08001017 if (dex_file_->SupportsDefaultMethods()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07001018 access_flag_options |= kAccPrivate;
1019 }
1020 if (!(method_access_flags_ & access_flag_options)) {
1021 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1022 << "interfaces may not have protected or package-private members";
1023 return false;
1024 }
Alex Lighteb7c1442015-08-31 13:17:42 -07001025 }
Andreas Gampee6215c02015-08-31 18:54:38 -07001026 }
1027 }
1028
1029 // Instance constructors must not be synchronized.
1030 if (IsInstanceConstructor()) {
1031 static constexpr uint32_t kForbidden = kAccSynchronized;
1032 if ((method_access_flags_ & kForbidden) != 0) {
1033 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
1034 return false;
1035 }
1036 }
1037 }
1038
Ian Rogersd81871c2011-10-03 13:57:23 -07001039 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001040 if (code_item_accessor_.InsSize() > code_item_accessor_.RegistersSize()) {
1041 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins="
1042 << code_item_accessor_.InsSize()
1043 << " regs=" << code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -07001044 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001045 }
Alex Lighteb7c1442015-08-31 13:17:42 -07001046
Ian Rogersd81871c2011-10-03 13:57:23 -07001047 // Allocate and initialize an array to hold instruction data.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001048 insn_flags_.reset(allocator_.AllocArray<InstructionFlags>(
1049 code_item_accessor_.InsnsSizeInCodeUnits()));
Mathieu Chartierde40d472015-10-15 17:47:48 -07001050 DCHECK(insn_flags_ != nullptr);
1051 std::uninitialized_fill_n(insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001052 code_item_accessor_.InsnsSizeInCodeUnits(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07001053 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -07001054 // Run through the instructions and see if the width checks out.
1055 bool result = ComputeWidthsAndCountOps();
Andreas Gampefef91cc2019-07-25 14:13:23 -07001056 bool allow_runtime_only_instructions = !IsAotMode() || verify_to_dump_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001057 // Flag instructions guarded by a "try" block and check exception handlers.
1058 result = result && ScanTryCatchBlocks();
1059 // Perform static instruction verification.
Andreas Gampebf1cb772017-05-15 15:39:00 -07001060 result = result && (allow_runtime_only_instructions
1061 ? VerifyInstructions<true>()
1062 : VerifyInstructions<false>());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001063 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +00001064 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -08001065
Vladimir Marko2b5eaa22013-12-13 13:59:30 +00001066 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -07001067}
1068
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001069template <bool kVerifierDebug>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001070bool MethodVerifier<kVerifierDebug>::ComputeWidthsAndCountOps() {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -07001071 // We can't assume the instruction is well formed, handle the case where calculating the size
1072 // goes past the end of the code item.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001073 SafeDexInstructionIterator it(code_item_accessor_.begin(), code_item_accessor_.end());
1074 for ( ; !it.IsErrorState() && it < code_item_accessor_.end(); ++it) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -07001075 // In case the instruction goes past the end of the code item, make sure to not process it.
1076 SafeDexInstructionIterator next = it;
1077 ++next;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001078 if (next.IsErrorState()) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -07001079 break;
1080 }
1081 Instruction::Code opcode = it->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -07001082 switch (opcode) {
1083 case Instruction::APUT_OBJECT:
1084 case Instruction::CHECK_CAST:
1085 has_check_casts_ = true;
1086 break;
Ian Rogersa9a82542013-10-04 11:17:26 -07001087 default:
1088 break;
jeffhaobdb76512011-09-07 11:43:16 -07001089 }
Andreas Gampe51de69e2019-04-19 15:14:14 -07001090 GetModifiableInstructionFlags(it.DexPc()).SetIsOpcode();
jeffhaobdb76512011-09-07 11:43:16 -07001091 }
1092
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001093 if (it != code_item_accessor_.end()) {
1094 const size_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaod5347e02012-03-22 17:25:05 -07001095 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001096 << it.DexPc() << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001097 return false;
1098 }
Andreas Gampe147a9112019-07-31 16:17:10 -07001099 DCHECK(GetInstructionFlags(0).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07001100
jeffhaobdb76512011-09-07 11:43:16 -07001101 return true;
1102}
1103
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001104template <bool kVerifierDebug>
1105bool MethodVerifier<kVerifierDebug>::ScanTryCatchBlocks() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001106 const uint32_t tries_size = code_item_accessor_.TriesSize();
jeffhaobdb76512011-09-07 11:43:16 -07001107 if (tries_size == 0) {
1108 return true;
1109 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001110 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001111 for (const dex::TryItem& try_item : code_item_accessor_.TryItems()) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001112 const uint32_t start = try_item.start_addr_;
1113 const uint32_t end = start + try_item.insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001114 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001115 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
1116 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001117 return false;
1118 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001119 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001120 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1121 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001122 return false;
1123 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001124 DexInstructionIterator end_it(code_item_accessor_.Insns(), end);
1125 for (DexInstructionIterator it(code_item_accessor_.Insns(), start); it < end_it; ++it) {
Andreas Gampe51de69e2019-04-19 15:14:14 -07001126 GetModifiableInstructionFlags(it.DexPc()).SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -07001127 }
1128 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001129 // Iterate over each of the handlers to verify target addresses.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001130 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
1131 const uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Andreas Gampee0bbab92019-07-25 12:28:22 -07001132 ClassLinker* linker = GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001133 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001134 CatchHandlerIterator iterator(handlers_ptr);
1135 for (; iterator.HasNext(); iterator.Next()) {
Igor Murashkin2ffb7032017-11-08 13:35:21 -08001136 uint32_t dex_pc = iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -07001137 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001138 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1139 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001140 return false;
1141 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001142 if (!CheckNotMoveResult(code_item_accessor_.Insns(), dex_pc)) {
Stephen Kyle9bc61992014-09-22 13:53:15 +01001143 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1144 << "exception handler begins with move-result* (" << dex_pc << ")";
1145 return false;
1146 }
Andreas Gampe51de69e2019-04-19 15:14:14 -07001147 GetModifiableInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001148 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1149 // unresolved exception types will be ignored by exception delivery
Andreas Gampea5b09a62016-11-17 15:21:22 -08001150 if (iterator.GetHandlerTypeIndex().IsValid()) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001151 ObjPtr<mirror::Class> exception_type =
1152 linker->ResolveType(iterator.GetHandlerTypeIndex(), dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07001153 if (exception_type == nullptr) {
1154 DCHECK(self_->IsExceptionPending());
1155 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001156 }
1157 }
jeffhaobdb76512011-09-07 11:43:16 -07001158 }
Ian Rogers0571d352011-11-03 19:51:38 -07001159 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001160 }
jeffhaobdb76512011-09-07 11:43:16 -07001161 return true;
1162}
1163
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001164template <bool kVerifierDebug>
Andreas Gampebf1cb772017-05-15 15:39:00 -07001165template <bool kAllowRuntimeOnlyInstructions>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001166bool MethodVerifier<kVerifierDebug>::VerifyInstructions() {
Andreas Gampe93adcb52019-06-06 20:16:07 -07001167 // Flag the start of the method as a branch target.
Andreas Gampe51de69e2019-04-19 15:14:14 -07001168 GetModifiableInstructionFlags(0).SetBranchTarget();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001169 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001170 const uint32_t dex_pc = inst.DexPc();
1171 if (!VerifyInstruction<kAllowRuntimeOnlyInstructions>(&inst.Inst(), dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001172 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001173 return false;
1174 }
Andreas Gampe93adcb52019-06-06 20:16:07 -07001175 // Flag some interesting instructions.
1176 if (inst->IsReturn()) {
1177 GetModifiableInstructionFlags(dex_pc).SetReturn();
1178 } else if (inst->Opcode() == Instruction::CHECK_CAST) {
1179 // The dex-to-dex compiler wants type information to elide check-casts.
Andreas Gampe51de69e2019-04-19 15:14:14 -07001180 GetModifiableInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -07001181 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001182 }
1183 return true;
1184}
1185
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001186template <bool kVerifierDebug>
Andreas Gampebf1cb772017-05-15 15:39:00 -07001187template <bool kAllowRuntimeOnlyInstructions>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001188bool MethodVerifier<kVerifierDebug>::VerifyInstruction(const Instruction* inst,
1189 uint32_t code_offset) {
Andreas Gampee05cc662017-05-15 10:17:30 -07001190 if (Instruction::kHaveExperimentalInstructions && UNLIKELY(inst->IsExperimental())) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -07001191 // Experimental instructions don't yet have verifier support implementation.
1192 // While it is possible to use them by themselves, when we try to use stable instructions
1193 // with a virtual register that was created by an experimental instruction,
1194 // the data flow analysis will fail.
1195 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
1196 << "experimental instruction is not supported by verifier; skipping verification";
Andreas Gampe43884b22019-06-27 14:05:52 -07001197 flags_.have_pending_experimental_failure_ = true;
Igor Murashkin4d7b75f2015-07-21 17:03:36 -07001198 return false;
1199 }
1200
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 /*
1903 * Sanity check: retrieve the stored register line (assuming
1904 * 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 Gampe68df3202015-06-22 11:35:46 -07001989// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1990// is no such field.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001991static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, dex::TypeIndex type_idx) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001992 const dex::ClassDef* class_def = dex_file.FindClassDef(type_idx);
Andreas Gampe68df3202015-06-22 11:35:46 -07001993 DCHECK(class_def != nullptr);
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -07001994 ClassAccessor accessor(dex_file, *class_def);
1995 for (const ClassAccessor::Field& field : accessor.GetInstanceFields()) {
1996 if (field.IsFinal()) {
1997 return field.GetIndex();
Andreas Gampe68df3202015-06-22 11:35:46 -07001998 }
Andreas Gampe68df3202015-06-22 11:35:46 -07001999 }
Andreas Gampee2abbc62017-09-15 11:59:26 -07002000 return dex::kDexNoIndex;
Andreas Gampe68df3202015-06-22 11:35:46 -07002001}
2002
Andreas Gampea727e372015-08-25 09:22:37 -07002003// Setup a register line for the given return instruction.
Andreas Gampe99db7bb2019-04-19 23:05:47 -07002004template <bool kVerifierDebug>
2005static void AdjustReturnLine(MethodVerifier<kVerifierDebug>* verifier,
Andreas Gampea727e372015-08-25 09:22:37 -07002006 const Instruction* ret_inst,
2007 RegisterLine* line) {
2008 Instruction::Code opcode = ret_inst->Opcode();
2009
2010 switch (opcode) {
2011 case Instruction::RETURN_VOID:
2012 case Instruction::RETURN_VOID_NO_BARRIER:
Andreas Gampe51de69e2019-04-19 15:14:14 -07002013 if (verifier->IsInstanceConstructor()) {
2014 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
2015 line->CheckConstructorReturn(verifier);
2016 }
2017 line->MarkAllRegistersAsConflicts(verifier);
Andreas Gampea727e372015-08-25 09:22:37 -07002018 break;
2019
2020 case Instruction::RETURN:
2021 case Instruction::RETURN_OBJECT:
2022 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
2023 break;
2024
2025 case Instruction::RETURN_WIDE:
2026 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
2027 break;
2028
2029 default:
2030 LOG(FATAL) << "Unknown return opcode " << opcode;
2031 UNREACHABLE();
2032 }
2033}
2034
Andreas Gampe99db7bb2019-04-19 23:05:47 -07002035template <bool kVerifierDebug>
2036bool MethodVerifier<kVerifierDebug>::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07002037 /*
2038 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07002039 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07002040 * control to another statement:
2041 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002042 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07002043 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07002044 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07002045 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07002046 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07002047 * throw an exception that is handled by an encompassing "try"
2048 * block.
2049 *
2050 * We can also return, in which case there is no successor instruction
2051 * from this point.
2052 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08002053 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07002054 */
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002055 const uint16_t* insns = code_item_accessor_.Insns() + work_insn_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -07002056 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07002057 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07002058
jeffhaobdb76512011-09-07 11:43:16 -07002059 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07002060 bool just_set_result = false;
Andreas Gampe99db7bb2019-04-19 23:05:47 -07002061 if (kVerifierDebug) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002062 // Generate processing back trace to debug verifier
Andreas Gampe2ad6cce2019-04-11 16:17:39 -07002063 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
2064 << work_line_->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07002065 }
jeffhaobdb76512011-09-07 11:43:16 -07002066
2067 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002068 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07002069 * can throw an exception, we will copy/merge this into the "catch"
2070 * address rather than work_line, because we don't want the result
2071 * from the "successful" code path (e.g. a check-cast that "improves"
2072 * a type) to be visible to the exception handler.
2073 */
Andreas Gampec6582782019-07-10 15:06:18 -07002074 if (((opcode_flags & Instruction::kThrow) != 0 || IsCompatThrow(inst->Opcode())) &&
2075 CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002076 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002077 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002078 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07002079 }
Andreas Gampe43884b22019-06-27 14:05:52 -07002080 // Per-instruction flag, should not be set here.
2081 DCHECK(!flags_.have_pending_runtime_throw_failure_);
Andreas Gampe6087bc22019-06-03 15:52:08 -07002082 bool exc_handler_unreachable = false;
jeffhaobdb76512011-09-07 11:43:16 -07002083
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07002084
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002085 // We need to ensure the work line is consistent while performing validation. When we spot a
2086 // peephole pattern we compute a new line for either the fallthrough instruction or the
2087 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08002088 RegisterLineArenaUniquePtr branch_line;
2089 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002090
Sebastien Hertz5243e912013-05-21 10:55:07 +02002091 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07002092 case Instruction::NOP:
2093 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002094 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07002095 * a signature that looks like a NOP; if we see one of these in
2096 * the course of executing code then we have a problem.
2097 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002098 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07002099 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002100 }
2101 break;
2102
2103 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002104 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002105 break;
jeffhaobdb76512011-09-07 11:43:16 -07002106 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002107 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002108 break;
jeffhaobdb76512011-09-07 11:43:16 -07002109 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002110 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002111 break;
2112 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002113 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002114 break;
jeffhaobdb76512011-09-07 11:43:16 -07002115 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002116 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002117 break;
jeffhaobdb76512011-09-07 11:43:16 -07002118 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002119 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07002120 break;
2121 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002122 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002123 break;
jeffhaobdb76512011-09-07 11:43:16 -07002124 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002125 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002126 break;
jeffhaobdb76512011-09-07 11:43:16 -07002127 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002128 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002129 break;
2130
2131 /*
2132 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002133 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002134 * might want to hold the result in an actual CPU register, so the
2135 * Dalvik spec requires that these only appear immediately after an
2136 * invoke or filled-new-array.
2137 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002138 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002139 * redundant with the reset done below, but it can make the debug info
2140 * easier to read in some cases.)
2141 */
2142 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002143 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002144 break;
2145 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002146 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002147 break;
2148 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002149 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002150 break;
2151
Andreas Gampe6087bc22019-06-03 15:52:08 -07002152 case Instruction::MOVE_EXCEPTION:
2153 if (!HandleMoveException(inst)) {
2154 exc_handler_unreachable = true;
Sebastien Hertz270a0e12015-01-16 19:49:09 +01002155 }
jeffhaobdb76512011-09-07 11:43:16 -07002156 break;
Andreas Gampe6087bc22019-06-03 15:52:08 -07002157
jeffhaobdb76512011-09-07 11:43:16 -07002158 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002159 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002160 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002161 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 }
jeffhaobdb76512011-09-07 11:43:16 -07002163 }
2164 break;
2165 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002166 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002167 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002168 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002169 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002170 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2171 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002172 } else {
2173 // Compilers may generate synthetic functions that write byte values into boolean fields.
2174 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002175 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002176 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002177 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2178 ((return_type.IsBoolean() || return_type.IsByte() ||
2179 return_type.IsShort() || return_type.IsChar()) &&
2180 src_type.IsInteger()));
2181 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002182 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002183 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002184 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002185 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002186 }
jeffhaobdb76512011-09-07 11:43:16 -07002187 }
2188 }
2189 break;
2190 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002191 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002192 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002193 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002194 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002195 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002196 } else {
2197 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002198 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002199 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002200 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002201 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002202 }
jeffhaobdb76512011-09-07 11:43:16 -07002203 }
2204 }
2205 break;
2206 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002207 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002208 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002209 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002210 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002211 } else {
2212 /* return_type is the *expected* return type, not register value */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002213 DCHECK(!return_type.IsZeroOrNull());
Ian Rogersd81871c2011-10-03 13:57:23 -07002214 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002215 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002216 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002217 // Disallow returning undefined, conflict & uninitialized values and verify that the
2218 // reference in vAA is an instance of the "return_type."
2219 if (reg_type.IsUndefined()) {
2220 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2221 } else if (reg_type.IsConflict()) {
2222 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2223 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002224 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002225 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002226 } else if (!reg_type.IsReferenceTypes()) {
2227 // We really do expect a reference here.
2228 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2229 << reg_type;
David Brazdilca3c8c32016-09-06 14:04:48 +01002230 } else if (!return_type.IsAssignableFrom(reg_type, this)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002231 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
Andreas Gampebc919af2019-07-10 14:09:51 -07002232 Fail(api_level_ > 29u ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_NO_CLASS)
2233 << " can't resolve returned type '" << return_type << "' or '" << reg_type << "'";
Jeff Haoa3faaf42013-09-03 19:07:00 -07002234 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002235 bool soft_error = false;
2236 // Check whether arrays are involved. They will show a valid class status, even
2237 // if their components are erroneous.
2238 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
David Brazdilca3c8c32016-09-06 14:04:48 +01002239 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error);
Andreas Gampe16f149c2015-03-23 10:10:20 -07002240 if (soft_error) {
2241 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2242 << reg_type << " vs " << return_type;
2243 }
2244 }
2245
2246 if (!soft_error) {
2247 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2248 << "', but expected from declaration '" << return_type << "'";
2249 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002250 }
jeffhaobdb76512011-09-07 11:43:16 -07002251 }
2252 }
2253 }
2254 break;
2255
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002256 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002257 case Instruction::CONST_4: {
2258 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002259 work_line_->SetRegisterType<LockOp::kClear>(
2260 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002261 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002262 }
2263 case Instruction::CONST_16: {
2264 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002265 work_line_->SetRegisterType<LockOp::kClear>(
2266 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002267 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002268 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002269 case Instruction::CONST: {
2270 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002271 work_line_->SetRegisterType<LockOp::kClear>(
2272 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002273 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002274 }
2275 case Instruction::CONST_HIGH16: {
2276 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002277 work_line_->SetRegisterType<LockOp::kClear>(
2278 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002279 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002280 }
jeffhaobdb76512011-09-07 11:43:16 -07002281 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002282 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002283 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
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_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002287 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002288 }
2289 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002290 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
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_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002294 break;
2295 }
2296 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002297 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002298 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2299 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002300 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002301 break;
2302 }
2303 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002304 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002305 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2306 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002307 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002308 break;
2309 }
jeffhaobdb76512011-09-07 11:43:16 -07002310 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002311 work_line_->SetRegisterType<LockOp::kClear>(
2312 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002313 break;
jeffhaobdb76512011-09-07 11:43:16 -07002314 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002315 work_line_->SetRegisterType<LockOp::kClear>(
2316 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002317 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002318 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002319 // Get type from instruction if unresolved then we need an access check
2320 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Andreas Gampe98be1a92017-08-28 08:25:45 -07002321 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002322 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002323 work_line_->SetRegisterType<LockOp::kClear>(
2324 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2325 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002326 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002327 }
Orion Hodson2e599942017-09-22 16:17:41 +01002328 case Instruction::CONST_METHOD_HANDLE:
2329 work_line_->SetRegisterType<LockOp::kClear>(
2330 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodHandle());
Orion Hodson2e599942017-09-22 16:17:41 +01002331 break;
2332 case Instruction::CONST_METHOD_TYPE:
2333 work_line_->SetRegisterType<LockOp::kClear>(
2334 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodType());
Orion Hodson2e599942017-09-22 16:17:41 +01002335 break;
jeffhaobdb76512011-09-07 11:43:16 -07002336 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002337 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002338 // Check whether the previous instruction is a move-object with vAA as a source, creating
2339 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002340 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002341 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002342 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002343 prev_idx--;
2344 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002345 const Instruction& prev_inst = code_item_accessor_.InstructionAt(prev_idx);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002346 switch (prev_inst.Opcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002347 case Instruction::MOVE_OBJECT:
2348 case Instruction::MOVE_OBJECT_16:
2349 case Instruction::MOVE_OBJECT_FROM16:
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002350 if (prev_inst.VRegB() == inst->VRegA_11x()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002351 // Redo the copy. This won't change the register types, but update the lock status
2352 // for the aliased register.
2353 work_line_->CopyRegister1(this,
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002354 prev_inst.VRegA(),
2355 prev_inst.VRegB(),
Andreas Gampec1474102015-08-18 08:57:44 -07002356 kTypeCategoryRef);
2357 }
2358 break;
2359
Alexey Grebenkince750492018-05-31 23:42:20 +03002360 // Catch a case of register aliasing when two registers are linked to the same
2361 // java.lang.Class object via two consequent const-class instructions immediately
2362 // preceding monitor-enter called on one of those registers.
2363 case Instruction::CONST_CLASS: {
2364 // Get the second previous instruction.
2365 if (prev_idx == 0 || GetInstructionFlags(prev_idx).IsBranchTarget()) {
2366 break;
2367 }
2368 prev_idx--;
2369 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
2370 prev_idx--;
2371 }
2372 const Instruction& prev2_inst = code_item_accessor_.InstructionAt(prev_idx);
2373
2374 // Match the pattern "const-class; const-class; monitor-enter;"
2375 if (prev2_inst.Opcode() != Instruction::CONST_CLASS) {
2376 break;
2377 }
2378
2379 // Ensure both const-classes are called for the same type_idx.
2380 if (prev_inst.VRegB_21c() != prev2_inst.VRegB_21c()) {
2381 break;
2382 }
2383
2384 // Update the lock status for the aliased register.
2385 if (prev_inst.VRegA() == inst->VRegA_11x()) {
2386 work_line_->CopyRegister1(this,
2387 prev2_inst.VRegA(),
2388 inst->VRegA_11x(),
2389 kTypeCategoryRef);
2390 } else if (prev2_inst.VRegA() == inst->VRegA_11x()) {
2391 work_line_->CopyRegister1(this,
2392 prev_inst.VRegA(),
2393 inst->VRegA_11x(),
2394 kTypeCategoryRef);
2395 }
2396 break;
2397 }
2398
Andreas Gampec1474102015-08-18 08:57:44 -07002399 default: // Other instruction types ignored.
2400 break;
2401 }
2402 }
jeffhaobdb76512011-09-07 11:43:16 -07002403 break;
2404 case Instruction::MONITOR_EXIT:
2405 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002406 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002407 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002408 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002409 * to the need to handle asynchronous exceptions, a now-deprecated
2410 * feature that Dalvik doesn't support.)
2411 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002412 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002413 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002414 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002415 * structured locking checks are working, the former would have
2416 * failed on the -enter instruction, and the latter is impossible.
2417 *
2418 * This is fortunate, because issue 3221411 prevents us from
2419 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002420 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002421 * some catch blocks (which will show up as "dead" code when
2422 * we skip them here); if we can't, then the code path could be
2423 * "live" so we still need to check it.
2424 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002425 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002426 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002427 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002428 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002429 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002430 /*
2431 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2432 * could be a "upcast" -- not expected, so we don't try to address it.)
2433 *
2434 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002435 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002436 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002437 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002438 const dex::TypeIndex type_idx((is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002439 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002440 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002441 // If this is a primitive type, fail HARD.
Andreas Gampee0bbab92019-07-25 12:28:22 -07002442 ObjPtr<mirror::Class> klass = GetClassLinker()->LookupResolvedType(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002443 type_idx, dex_cache_.Get(), class_loader_.Get());
Andreas Gampe00633eb2014-07-17 16:13:35 -07002444 if (klass != nullptr && klass->IsPrimitive()) {
2445 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2446 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2447 << GetDeclaringClass();
2448 break;
2449 }
2450
Ian Rogersad0b3a32012-04-16 14:50:24 -07002451 DCHECK_NE(failures_.size(), 0U);
2452 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002453 work_line_->SetRegisterType<LockOp::kClear>(this,
2454 inst->VRegA_22c(),
2455 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002456 }
2457 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002458 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002459 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002460 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002461 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002462 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002463 if (is_checkcast) {
2464 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2465 } else {
2466 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2467 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002468 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002469 if (is_checkcast) {
2470 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2471 } else {
2472 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2473 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002474 } else if (orig_type.IsUninitializedTypes()) {
2475 if (is_checkcast) {
2476 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2477 << orig_type_reg;
2478 } else {
2479 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2480 << orig_type_reg;
2481 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002482 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002483 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002484 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), 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_22c(),
2488 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002489 }
jeffhaobdb76512011-09-07 11:43:16 -07002490 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002491 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002492 }
2493 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002494 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002495 if (res_type.IsReferenceTypes()) {
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002496 if (!res_type.IsArrayTypes() && !res_type.IsZeroOrNull()) {
2497 // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002498 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002499 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002500 work_line_->SetRegisterType<LockOp::kClear>(this,
2501 inst->VRegA_12x(),
2502 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002503 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002504 } else {
2505 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002506 }
2507 break;
2508 }
2509 case Instruction::NEW_INSTANCE: {
Andreas Gampe98be1a92017-08-28 08:25:45 -07002510 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002511 if (res_type.IsConflict()) {
2512 DCHECK_NE(failures_.size(), 0U);
2513 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002514 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002515 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2516 // can't create an instance of an interface or abstract class */
2517 if (!res_type.IsInstantiableTypes()) {
2518 Fail(VERIFY_ERROR_INSTANTIATION)
2519 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002520 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002521 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002522 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002523 // Any registers holding previous allocations from this address that have not yet been
2524 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002525 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002526 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002527 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002528 break;
2529 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002530 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002531 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002532 break;
2533 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002534 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002535 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002536 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002537 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002538 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002539 just_set_result = true; // Filled new array range sets result register
2540 break;
jeffhaobdb76512011-09-07 11:43:16 -07002541 case Instruction::CMPL_FLOAT:
2542 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002543 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002544 break;
2545 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002546 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002547 break;
2548 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002549 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002550 break;
2551 case Instruction::CMPL_DOUBLE:
2552 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002553 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002554 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002555 break;
2556 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002557 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002558 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002559 break;
2560 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002561 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002562 break;
2563 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002564 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002565 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002566 break;
2567 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002568 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002569 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002570 break;
2571 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002572 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002573 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002574 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002575 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
David Brazdilca3c8c32016-09-06 14:04:48 +01002576 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002577 if (res_type.IsUninitializedTypes()) {
2578 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002579 } else if (!res_type.IsReferenceTypes()) {
2580 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002581 } else {
2582 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2583 << "thrown class " << res_type << " not instanceof Throwable";
2584 }
jeffhaobdb76512011-09-07 11:43:16 -07002585 }
2586 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002587 }
jeffhaobdb76512011-09-07 11:43:16 -07002588 case Instruction::GOTO:
2589 case Instruction::GOTO_16:
2590 case Instruction::GOTO_32:
2591 /* no effect on or use of registers */
2592 break;
2593
2594 case Instruction::PACKED_SWITCH:
2595 case Instruction::SPARSE_SWITCH:
2596 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002597 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002598 break;
2599
Ian Rogersd81871c2011-10-03 13:57:23 -07002600 case Instruction::FILL_ARRAY_DATA: {
2601 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002602 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002603 /* array_type can be null if the reg type is Zero */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002604 if (!array_type.IsZeroOrNull()) {
jeffhao457cc512012-02-02 16:55:13 -08002605 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002606 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2607 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002608 } else if (array_type.IsUnresolvedTypes()) {
2609 // If it's an unresolved array type, it must be non-primitive.
2610 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2611 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002612 } else {
Andreas Gampe51de69e2019-04-19 15:14:14 -07002613 const RegType& component_type = reg_types_.GetComponentType(array_type,
2614 class_loader_.Get());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002615 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002616 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002617 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2618 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002619 } else {
jeffhao457cc512012-02-02 16:55:13 -08002620 // Now verify if the element width in the table matches the element width declared in
2621 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002622 const uint16_t* array_data =
2623 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002624 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002625 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002626 } else {
2627 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2628 // Since we don't compress the data in Dex, expect to see equal width of data stored
2629 // in the table and expected from the array class.
2630 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002631 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2632 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002633 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002634 }
2635 }
jeffhaobdb76512011-09-07 11:43:16 -07002636 }
2637 }
2638 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002639 }
jeffhaobdb76512011-09-07 11:43:16 -07002640 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002641 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002642 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2643 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002644 bool mismatch = false;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002645 if (reg_type1.IsZeroOrNull()) { // zero then integral or reference expected
Ian Rogersd81871c2011-10-03 13:57:23 -07002646 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2647 } else if (reg_type1.IsReferenceTypes()) { // both references?
2648 mismatch = !reg_type2.IsReferenceTypes();
2649 } else { // both integral?
2650 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2651 }
2652 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002653 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2654 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002655 }
2656 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002657 }
jeffhaobdb76512011-09-07 11:43:16 -07002658 case Instruction::IF_LT:
2659 case Instruction::IF_GE:
2660 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002661 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002662 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2663 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002664 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002665 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2666 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002667 }
2668 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002669 }
jeffhaobdb76512011-09-07 11:43:16 -07002670 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002671 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002672 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002673 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002674 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2675 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002676 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002677
2678 // Find previous instruction - its existence is a precondition to peephole optimization.
Andreas Gampe147a9112019-07-31 16:17:10 -07002679 if (UNLIKELY(0 == work_insn_idx_)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002680 break;
2681 }
Andreas Gampe147a9112019-07-31 16:17:10 -07002682 uint32_t instance_of_idx = work_insn_idx_ - 1;
2683 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
2684 instance_of_idx--;
2685 }
2686 // Dex index 0 must be an opcode.
2687 DCHECK(GetInstructionFlags(instance_of_idx).IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002688
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002689 const Instruction& instance_of_inst = code_item_accessor_.InstructionAt(instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002690
2691 /* Check for peep-hole pattern of:
2692 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002693 * instance-of vX, vY, T;
2694 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002695 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002696 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002697 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002698 * and sharpen the type of vY to be type T.
2699 * Note, this pattern can't be if:
2700 * - if there are other branches to this branch,
2701 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002702 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002703 if (!CurrentInsnFlags()->IsBranchTarget() &&
Vladimir Markod7559b72017-09-28 13:50:37 +01002704 (Instruction::INSTANCE_OF == instance_of_inst.Opcode()) &&
2705 (inst->VRegA_21t() == instance_of_inst.VRegA_22c()) &&
2706 (instance_of_inst.VRegA_22c() != instance_of_inst.VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002707 // Check the type of the instance-of is different than that of registers type, as if they
2708 // are the same there is no work to be done here. Check that the conversion is not to or
2709 // from an unresolved type as type information is imprecise. If the instance-of is to an
2710 // interface then ignore the type information as interfaces can only be treated as Objects
2711 // and we don't want to disallow field and other operations on the object. If the value
2712 // being instance-of checked against is known null (zero) then allow the optimization as
2713 // we didn't have type information. If the merge of the instance-of type with the original
2714 // type is assignable to the original then allow optimization. This check is performed to
2715 // ensure that subsequent merges don't lose type information - such as becoming an
2716 // interface from a class that would lose information relevant to field checks.
Andreas Gamped78122b2019-07-10 14:06:53 -07002717 //
2718 // Note: do not do an access check. This may mark this with a runtime throw that actually
2719 // happens at the instanceof, not the branch (and branches aren't flagged to throw).
Vladimir Markod7559b72017-09-28 13:50:37 +01002720 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst.VRegB_22c());
Andreas Gamped78122b2019-07-10 14:06:53 -07002721 const RegType& cast_type = ResolveClass<CheckAccess::kNo>(
Vladimir Markod7559b72017-09-28 13:50:37 +01002722 dex::TypeIndex(instance_of_inst.VRegC_22c()));
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002723
Ian Rogersebbdd872014-07-07 23:53:08 -07002724 if (!orig_type.Equals(cast_type) &&
2725 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002726 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002727 !cast_type.GetClass()->IsInterface() &&
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002728 (orig_type.IsZeroOrNull() ||
David Brazdilca3c8c32016-09-06 14:04:48 +01002729 orig_type.IsStrictlyAssignableFrom(
2730 cast_type.Merge(orig_type, &reg_types_, this), this))) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002731 RegisterLine* update_line = RegisterLine::Create(code_item_accessor_.RegistersSize(),
Andreas Gamped09c0592019-04-19 15:44:05 -07002732 allocator_,
2733 GetRegTypeCache());
Ian Rogersfae370a2013-06-05 08:33:27 -07002734 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002735 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002736 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002737 branch_line.reset(update_line);
2738 }
2739 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002740 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002741 instance_of_inst.VRegB_22c(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002742 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002743 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002744 // See if instance-of was preceded by a move-object operation, common due to the small
2745 // register encoding space of instance-of, and propagate type information to the source
2746 // of the move-object.
Andreas Gampe43e43252019-01-08 12:06:57 -08002747 // Note: this is only valid if the move source was not clobbered.
Ian Rogers9b360392013-06-06 14:45:07 -07002748 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002749 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002750 move_idx--;
2751 }
Andreas Gampe147a9112019-07-31 16:17:10 -07002752 DCHECK(GetInstructionFlags(move_idx).IsOpcode());
Andreas Gampe43e43252019-01-08 12:06:57 -08002753 auto maybe_update_fn = [&instance_of_inst, update_line, this, &cast_type](
2754 uint16_t move_src,
2755 uint16_t move_trg)
2756 REQUIRES_SHARED(Locks::mutator_lock_) {
2757 if (move_trg == instance_of_inst.VRegB_22c() &&
2758 move_src != instance_of_inst.VRegA_22c()) {
2759 update_line->SetRegisterType<LockOp::kKeep>(this, move_src, cast_type);
2760 }
2761 };
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002762 const Instruction& move_inst = code_item_accessor_.InstructionAt(move_idx);
Vladimir Markod7559b72017-09-28 13:50:37 +01002763 switch (move_inst.Opcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002764 case Instruction::MOVE_OBJECT:
Andreas Gampe43e43252019-01-08 12:06:57 -08002765 maybe_update_fn(move_inst.VRegB_12x(), move_inst.VRegA_12x());
Ian Rogers9b360392013-06-06 14:45:07 -07002766 break;
2767 case Instruction::MOVE_OBJECT_FROM16:
Andreas Gampe43e43252019-01-08 12:06:57 -08002768 maybe_update_fn(move_inst.VRegB_22x(), move_inst.VRegA_22x());
Ian Rogers9b360392013-06-06 14:45:07 -07002769 break;
2770 case Instruction::MOVE_OBJECT_16:
Andreas Gampe43e43252019-01-08 12:06:57 -08002771 maybe_update_fn(move_inst.VRegB_32x(), move_inst.VRegA_32x());
Ian Rogers9b360392013-06-06 14:45:07 -07002772 break;
2773 default:
2774 break;
2775 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002776 }
2777 }
2778 }
2779
jeffhaobdb76512011-09-07 11:43:16 -07002780 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002781 }
jeffhaobdb76512011-09-07 11:43:16 -07002782 case Instruction::IF_LTZ:
2783 case Instruction::IF_GEZ:
2784 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002785 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002786 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002787 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002788 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2789 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002790 }
jeffhaobdb76512011-09-07 11:43:16 -07002791 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002792 }
jeffhaobdb76512011-09-07 11:43:16 -07002793 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002794 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002795 break;
jeffhaobdb76512011-09-07 11:43:16 -07002796 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002797 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002798 break;
jeffhaobdb76512011-09-07 11:43:16 -07002799 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002800 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002801 break;
jeffhaobdb76512011-09-07 11:43:16 -07002802 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002803 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002804 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002805 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002806 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002807 break;
jeffhaobdb76512011-09-07 11:43:16 -07002808 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002809 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002810 break;
2811 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002812 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002813 break;
2814
Ian Rogersd81871c2011-10-03 13:57:23 -07002815 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002816 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 break;
2818 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002819 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002820 break;
2821 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002822 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002823 break;
2824 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002825 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002826 break;
2827 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002828 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002829 break;
2830 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002831 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002832 break;
2833 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002834 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002835 break;
2836
jeffhaobdb76512011-09-07 11:43:16 -07002837 case Instruction::IGET_BOOLEAN:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002838 case Instruction::IGET_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002839 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002840 break;
jeffhaobdb76512011-09-07 11:43:16 -07002841 case Instruction::IGET_BYTE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002842 case Instruction::IGET_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002843 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002844 break;
jeffhaobdb76512011-09-07 11:43:16 -07002845 case Instruction::IGET_CHAR:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002846 case Instruction::IGET_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002847 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002848 break;
jeffhaobdb76512011-09-07 11:43:16 -07002849 case Instruction::IGET_SHORT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002850 case Instruction::IGET_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002851 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002852 break;
2853 case Instruction::IGET:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002854 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002855 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002856 break;
2857 case Instruction::IGET_WIDE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002858 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002859 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002860 break;
2861 case Instruction::IGET_OBJECT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002862 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002863 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2864 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002865 break;
jeffhaobdb76512011-09-07 11:43:16 -07002866
Ian Rogersd81871c2011-10-03 13:57:23 -07002867 case Instruction::IPUT_BOOLEAN:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002868 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002869 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002870 break;
2871 case Instruction::IPUT_BYTE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002872 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002873 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002874 break;
2875 case Instruction::IPUT_CHAR:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002876 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002877 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002878 break;
2879 case Instruction::IPUT_SHORT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002880 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002881 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002882 break;
2883 case Instruction::IPUT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002884 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002885 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002886 break;
2887 case Instruction::IPUT_WIDE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002888 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002889 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002890 break;
jeffhaobdb76512011-09-07 11:43:16 -07002891 case Instruction::IPUT_OBJECT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002892 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002893 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2894 false);
jeffhaobdb76512011-09-07 11:43:16 -07002895 break;
2896
jeffhaobdb76512011-09-07 11:43:16 -07002897 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002898 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002899 break;
jeffhaobdb76512011-09-07 11:43:16 -07002900 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002901 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002902 break;
jeffhaobdb76512011-09-07 11:43:16 -07002903 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002904 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002905 break;
jeffhaobdb76512011-09-07 11:43:16 -07002906 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002907 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002908 break;
2909 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002910 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002911 break;
2912 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002913 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002914 break;
2915 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002916 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2917 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002918 break;
2919
2920 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002921 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002922 break;
2923 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002924 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002925 break;
2926 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002927 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002928 break;
2929 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002930 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002931 break;
2932 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002933 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002934 break;
2935 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002936 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002937 break;
2938 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002939 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2940 true);
jeffhaobdb76512011-09-07 11:43:16 -07002941 break;
2942
2943 case Instruction::INVOKE_VIRTUAL:
2944 case Instruction::INVOKE_VIRTUAL_RANGE:
2945 case Instruction::INVOKE_SUPER:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002946 case Instruction::INVOKE_SUPER_RANGE:
2947 case Instruction::INVOKE_VIRTUAL_QUICK:
2948 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002949 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002950 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE ||
2951 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002952 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2953 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002954 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2955 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002956 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002957 if (called_method != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01002958 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2959 ? called_method->ResolveReturnType()
2960 : called_method->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002961 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002962 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
Vladimir Marko179b7c62019-03-22 13:38:57 +00002963 return_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07002964 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002965 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002966 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2967 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002968 }
2969 }
2970 if (return_type == nullptr) {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002971 uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002972 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002973 dex::TypeIndex return_type_idx =
2974 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002975 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Andreas Gampe51de69e2019-04-19 15:14:14 -07002976 return_type = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002977 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002978 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002979 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002980 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002981 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002982 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002983 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002984 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002985 }
jeffhaobdb76512011-09-07 11:43:16 -07002986 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002987 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002988 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002989 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002990 const char* return_type_descriptor;
2991 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002992 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002993 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002994 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002995 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002996 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Andreas Gampea5b09a62016-11-17 15:21:22 -08002997 dex::TypeIndex return_type_idx =
2998 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers46685432012-06-03 22:26:43 -07002999 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
3000 } else {
3001 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003002 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Markob45528c2017-07-27 14:14:28 +01003003 ObjPtr<mirror::Class> return_type_class = can_load_classes_
3004 ? called_method->ResolveReturnType()
3005 : called_method->LookupResolvedReturnType();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003006 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07003007 return_type = &FromClass(return_type_descriptor,
Vladimir Markobcf17522018-06-01 13:14:32 +01003008 return_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07003009 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07003010 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003011 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3012 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003013 }
Ian Rogers46685432012-06-03 22:26:43 -07003014 }
3015 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07003016 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003017 * Some additional checks when calling a constructor. We know from the invocation arg check
3018 * that the "this" argument is an instance of called_method->klass. Now we further restrict
3019 * that to require that called_method->klass is the same as this->klass or this->super,
3020 * allowing the latter only if the "this" argument is the same as the "this" argument to
3021 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07003022 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003023 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
jeffhaob57e9522012-04-26 18:08:21 -07003024 if (this_type.IsConflict()) // failure.
3025 break;
jeffhaobdb76512011-09-07 11:43:16 -07003026
jeffhaob57e9522012-04-26 18:08:21 -07003027 /* no null refs allowed (?) */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08003028 if (this_type.IsZeroOrNull()) {
jeffhaob57e9522012-04-26 18:08:21 -07003029 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
3030 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07003031 }
jeffhaob57e9522012-04-26 18:08:21 -07003032
3033 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003034 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07003035 // TODO: re-enable constructor type verification
3036 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07003037 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07003038 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
3039 // break;
3040 // }
jeffhaob57e9522012-04-26 18:08:21 -07003041
3042 /* arg must be an uninitialized reference */
3043 if (!this_type.IsUninitializedTypes()) {
3044 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
3045 << this_type;
3046 break;
3047 }
3048
3049 /*
3050 * Replace the uninitialized reference with an initialized one. We need to do this for all
3051 * registers that have the same object instance in them, not just the "this" register.
3052 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00003053 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003054 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07003055 if (return_type == nullptr) {
Andreas Gampe51de69e2019-04-19 15:14:14 -07003056 return_type = &reg_types_.FromDescriptor(class_loader_.Get(),
3057 return_type_descriptor,
3058 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07003059 }
3060 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003061 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003062 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07003063 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003064 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003065 just_set_result = true;
3066 break;
3067 }
3068 case Instruction::INVOKE_STATIC:
3069 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003070 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003071 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003072 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003073 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003074 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003075 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003076 dex::TypeIndex return_type_idx =
3077 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07003078 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003079 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003080 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003081 }
Andreas Gampe51de69e2019-04-19 15:14:14 -07003082 const RegType& return_type = reg_types_.FromDescriptor(class_loader_.Get(),
3083 descriptor,
3084 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003085 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003086 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003087 } else {
3088 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3089 }
jeffhaobdb76512011-09-07 11:43:16 -07003090 just_set_result = true;
3091 }
3092 break;
jeffhaobdb76512011-09-07 11:43:16 -07003093 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07003094 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003095 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003096 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003097 if (abs_method != nullptr) {
Vladimir Markod93e3742018-07-18 10:58:13 +01003098 ObjPtr<mirror::Class> called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003099 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
3100 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
David Sehr709b0702016-10-13 09:12:37 -07003101 << abs_method->PrettyMethod() << "'";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003102 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003103 }
Ian Rogers0d604842012-04-16 14:50:24 -07003104 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003105 /* Get the type of the "this" arg, which should either be a sub-interface of called
3106 * interface or Object (see comments in RegType::JoinClass).
3107 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003108 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08003109 if (this_type.IsZeroOrNull()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003110 /* null pointer always passes (and always fails at runtime) */
3111 } else {
3112 if (this_type.IsUninitializedTypes()) {
3113 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
3114 << this_type;
3115 break;
3116 }
3117 // In the past we have tried to assert that "called_interface" is assignable
3118 // from "this_type.GetClass()", however, as we do an imprecise Join
3119 // (RegType::JoinClass) we don't have full information on what interfaces are
3120 // implemented by "this_type". For example, two classes may implement the same
3121 // interfaces and have a common parent that doesn't implement the interface. The
3122 // join will set "this_type" to the parent class and a test that this implements
3123 // the interface will incorrectly fail.
3124 }
3125 /*
3126 * We don't have an object instance, so we can't find the concrete method. However, all of
3127 * the type information is in the abstract method, so we're good.
3128 */
3129 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003130 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003131 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003132 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003133 dex::TypeIndex return_type_idx =
3134 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003135 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003136 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003137 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003138 }
Andreas Gampe51de69e2019-04-19 15:14:14 -07003139 const RegType& return_type = reg_types_.FromDescriptor(class_loader_.Get(),
3140 descriptor,
3141 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003142 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003143 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003144 } else {
3145 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3146 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003147 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07003148 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003149 }
Narayan Kamath9823e782016-08-03 12:46:58 +01003150 case Instruction::INVOKE_POLYMORPHIC:
3151 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003152 bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
3153 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range);
3154 if (called_method == nullptr) {
3155 // Convert potential soft failures in VerifyInvocationArgs() to hard errors.
3156 if (failure_messages_.size() > 0) {
3157 std::string message = failure_messages_.back()->str();
3158 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message;
3159 } else {
3160 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure.";
3161 }
3162 break;
3163 }
3164 if (!CheckSignaturePolymorphicMethod(called_method) ||
3165 !CheckSignaturePolymorphicReceiver(inst)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00003166 DCHECK(HasFailures());
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003167 break;
3168 }
Orion Hodson06d10a72018-05-14 08:53:38 +01003169 const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
3170 const dex::ProtoIndex proto_idx(vRegH);
Orion Hodsonac141392017-01-13 11:53:47 +00003171 const char* return_descriptor =
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003172 dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx));
3173 const RegType& return_type =
Andreas Gampe51de69e2019-04-19 15:14:14 -07003174 reg_types_.FromDescriptor(class_loader_.Get(), return_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003175 if (!return_type.IsLowHalf()) {
3176 work_line_->SetResultRegisterType(this, return_type);
3177 } else {
3178 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3179 }
Orion Hodsonac141392017-01-13 11:53:47 +00003180 just_set_result = true;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003181 break;
Narayan Kamath9823e782016-08-03 12:46:58 +01003182 }
Orion Hodsonc069a302017-01-18 09:23:12 +00003183 case Instruction::INVOKE_CUSTOM:
3184 case Instruction::INVOKE_CUSTOM_RANGE: {
3185 // Verify registers based on method_type in the call site.
3186 bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE);
3187
3188 // Step 1. Check the call site that produces the method handle for invocation
3189 const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3190 if (!CheckCallSite(call_site_idx)) {
3191 DCHECK(HasFailures());
3192 break;
3193 }
3194
3195 // Step 2. Check the register arguments correspond to the expected arguments for the
3196 // method handle produced by step 1. The dex file verifier has checked ranges for
3197 // the first three arguments and CheckCallSite has checked the method handle type.
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003198 const dex::ProtoIndex proto_idx = dex_file_->GetProtoIndexForCallSite(call_site_idx);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003199 const dex::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00003200 DexFileParameterIterator param_it(*dex_file_, proto_id);
3201 // Treat method as static as it has yet to be determined.
3202 VerifyInvocationArgsFromIterator(&param_it, inst, METHOD_STATIC, is_range, nullptr);
3203 const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
3204
3205 // Step 3. Propagate return type information
3206 const RegType& return_type =
Andreas Gampe51de69e2019-04-19 15:14:14 -07003207 reg_types_.FromDescriptor(class_loader_.Get(), return_descriptor, false);
Orion Hodsonc069a302017-01-18 09:23:12 +00003208 if (!return_type.IsLowHalf()) {
3209 work_line_->SetResultRegisterType(this, return_type);
3210 } else {
3211 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3212 }
3213 just_set_result = true;
Orion Hodsonc069a302017-01-18 09:23:12 +00003214 break;
3215 }
jeffhaobdb76512011-09-07 11:43:16 -07003216 case Instruction::NEG_INT:
3217 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003218 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003219 break;
3220 case Instruction::NEG_LONG:
3221 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003222 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
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::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003226 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003227 break;
3228 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003229 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003230 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003231 break;
3232 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003233 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003234 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003235 break;
3236 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003237 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003238 break;
3239 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003240 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003241 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003242 break;
3243 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003244 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003245 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003246 break;
3247 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003248 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003249 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003250 break;
3251 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003252 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003253 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003254 break;
3255 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003256 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003257 break;
3258 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003259 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003260 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003261 break;
3262 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003263 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003264 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003265 break;
3266 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003267 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003268 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003269 break;
3270 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003271 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003272 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003273 break;
3274 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003275 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003276 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003277 break;
3278 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003279 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003280 break;
3281 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003282 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003283 break;
3284 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003285 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003286 break;
3287
3288 case Instruction::ADD_INT:
3289 case Instruction::SUB_INT:
3290 case Instruction::MUL_INT:
3291 case Instruction::REM_INT:
3292 case Instruction::DIV_INT:
3293 case Instruction::SHL_INT:
3294 case Instruction::SHR_INT:
3295 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003296 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003297 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003298 break;
3299 case Instruction::AND_INT:
3300 case Instruction::OR_INT:
3301 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003302 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003303 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003304 break;
3305 case Instruction::ADD_LONG:
3306 case Instruction::SUB_LONG:
3307 case Instruction::MUL_LONG:
3308 case Instruction::DIV_LONG:
3309 case Instruction::REM_LONG:
3310 case Instruction::AND_LONG:
3311 case Instruction::OR_LONG:
3312 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003313 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003314 reg_types_.LongLo(), reg_types_.LongHi(),
3315 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003316 break;
3317 case Instruction::SHL_LONG:
3318 case Instruction::SHR_LONG:
3319 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003320 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003321 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003322 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003323 break;
3324 case Instruction::ADD_FLOAT:
3325 case Instruction::SUB_FLOAT:
3326 case Instruction::MUL_FLOAT:
3327 case Instruction::DIV_FLOAT:
3328 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003329 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3330 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003331 break;
3332 case Instruction::ADD_DOUBLE:
3333 case Instruction::SUB_DOUBLE:
3334 case Instruction::MUL_DOUBLE:
3335 case Instruction::DIV_DOUBLE:
3336 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003337 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003338 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3339 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003340 break;
3341 case Instruction::ADD_INT_2ADDR:
3342 case Instruction::SUB_INT_2ADDR:
3343 case Instruction::MUL_INT_2ADDR:
3344 case Instruction::REM_INT_2ADDR:
3345 case Instruction::SHL_INT_2ADDR:
3346 case Instruction::SHR_INT_2ADDR:
3347 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003348 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3349 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003350 break;
3351 case Instruction::AND_INT_2ADDR:
3352 case Instruction::OR_INT_2ADDR:
3353 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003354 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3355 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003356 break;
3357 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003358 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3359 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003360 break;
3361 case Instruction::ADD_LONG_2ADDR:
3362 case Instruction::SUB_LONG_2ADDR:
3363 case Instruction::MUL_LONG_2ADDR:
3364 case Instruction::DIV_LONG_2ADDR:
3365 case Instruction::REM_LONG_2ADDR:
3366 case Instruction::AND_LONG_2ADDR:
3367 case Instruction::OR_LONG_2ADDR:
3368 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003369 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003370 reg_types_.LongLo(), reg_types_.LongHi(),
3371 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003372 break;
3373 case Instruction::SHL_LONG_2ADDR:
3374 case Instruction::SHR_LONG_2ADDR:
3375 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003376 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003377 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003378 break;
3379 case Instruction::ADD_FLOAT_2ADDR:
3380 case Instruction::SUB_FLOAT_2ADDR:
3381 case Instruction::MUL_FLOAT_2ADDR:
3382 case Instruction::DIV_FLOAT_2ADDR:
3383 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003384 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3385 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003386 break;
3387 case Instruction::ADD_DOUBLE_2ADDR:
3388 case Instruction::SUB_DOUBLE_2ADDR:
3389 case Instruction::MUL_DOUBLE_2ADDR:
3390 case Instruction::DIV_DOUBLE_2ADDR:
3391 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003392 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003393 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3394 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003395 break;
3396 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003397 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003398 case Instruction::MUL_INT_LIT16:
3399 case Instruction::DIV_INT_LIT16:
3400 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003401 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3402 true);
jeffhaobdb76512011-09-07 11:43:16 -07003403 break;
3404 case Instruction::AND_INT_LIT16:
3405 case Instruction::OR_INT_LIT16:
3406 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003407 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3408 true);
jeffhaobdb76512011-09-07 11:43:16 -07003409 break;
3410 case Instruction::ADD_INT_LIT8:
3411 case Instruction::RSUB_INT_LIT8:
3412 case Instruction::MUL_INT_LIT8:
3413 case Instruction::DIV_INT_LIT8:
3414 case Instruction::REM_INT_LIT8:
3415 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003416 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003417 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003418 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3419 false);
jeffhaobdb76512011-09-07 11:43:16 -07003420 break;
3421 case Instruction::AND_INT_LIT8:
3422 case Instruction::OR_INT_LIT8:
3423 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003424 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3425 false);
jeffhaobdb76512011-09-07 11:43:16 -07003426 break;
3427
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003428 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003429 case Instruction::RETURN_VOID_NO_BARRIER:
3430 if (IsConstructor() && !IsStatic()) {
Vladimir Markod93e3742018-07-18 10:58:13 +01003431 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003432 if (declaring_class.IsUnresolvedReference()) {
3433 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3434 // manually over the underlying dex file.
3435 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3436 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
Andreas Gampee2abbc62017-09-15 11:59:26 -07003437 if (first_index != dex::kDexNoIndex) {
Andreas Gampe68df3202015-06-22 11:35:46 -07003438 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3439 << first_index;
3440 }
3441 break;
3442 }
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003443 ObjPtr<mirror::Class> klass = declaring_class.GetClass();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003444 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3445 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003446 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
David Sehr709b0702016-10-13 09:12:37 -07003447 << klass->GetInstanceField(i)->PrettyField();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003448 break;
3449 }
3450 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003451 }
Andreas Gampeb2917962015-07-31 13:36:10 -07003452 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3453 // quickened opcodes (otherwise this could be a fall-through).
3454 if (!IsConstructor()) {
3455 if (!GetMethodReturnType().IsConflict()) {
3456 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3457 }
3458 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003459 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003460
Ian Rogersd81871c2011-10-03 13:57:23 -07003461 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003462 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Narayan Kamath8ec3bd22016-08-03 12:46:23 +01003463 case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003464 case Instruction::UNUSED_79:
3465 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003466 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003467 break;
3468
3469 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003470 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003471 * complain if an instruction is missing (which is desirable).
3472 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003473 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003474
Andreas Gampe43884b22019-06-27 14:05:52 -07003475 if (flags_.have_pending_hard_failure_) {
Andreas Gampefef91cc2019-07-25 14:13:23 -07003476 if (IsAotMode()) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003477 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003478 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3479 LOG(ERROR) << "Pending failures:";
3480 for (auto& error : failures_) {
3481 LOG(ERROR) << error;
3482 }
3483 for (auto& error_msg : failure_messages_) {
3484 LOG(ERROR) << error_msg->str();
3485 }
3486 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3487 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003488 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003489 /* immediate failure, reject class */
3490 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3491 return false;
Andreas Gampe43884b22019-06-27 14:05:52 -07003492 } else if (flags_.have_pending_runtime_throw_failure_) {
Andreas Gampe6087bc22019-06-03 15:52:08 -07003493 LogVerifyInfo() << "Elevating opcode flags from " << opcode_flags << " to Throw";
Jeff Haoa3faaf42013-09-03 19:07:00 -07003494 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003495 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003496 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3497 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3498 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003499 }
jeffhaobdb76512011-09-07 11:43:16 -07003500 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003501 * If we didn't just set the result register, clear it out. This ensures that you can only use
3502 * "move-result" immediately after the result is set. (We could check this statically, but it's
3503 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003504 */
3505 if (!just_set_result) {
Andreas Gamped09c0592019-04-19 15:44:05 -07003506 work_line_->SetResultTypeToUnknown(GetRegTypeCache());
jeffhaobdb76512011-09-07 11:43:16 -07003507 }
3508
jeffhaobdb76512011-09-07 11:43:16 -07003509 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003510 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003511 *
3512 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003513 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003514 * somebody could get a reference field, check it for zero, and if the
3515 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003516 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003517 * that, and will reject the code.
3518 *
3519 * TODO: avoid re-fetching the branch target
3520 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003521 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003522 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003523 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003524 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003525 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003526 return false;
3527 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003528 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003529 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(),
3530 work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003531 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003532 }
jeffhaobdb76512011-09-07 11:43:16 -07003533 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003534 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003535 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003536 return false;
3537 }
3538 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003539 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003540 return false;
3541 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003542 }
jeffhaobdb76512011-09-07 11:43:16 -07003543 }
3544
3545 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003546 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003547 *
3548 * We've already verified that the table is structurally sound, so we
3549 * just need to walk through and tag the targets.
3550 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003551 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003552 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003553 const uint16_t* switch_insns = insns + offset_to_switch;
3554 int switch_count = switch_insns[1];
3555 int offset_to_targets, targ;
3556
3557 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3558 /* 0 = sig, 1 = count, 2/3 = first key */
3559 offset_to_targets = 4;
3560 } else {
3561 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003562 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003563 offset_to_targets = 2 + 2 * switch_count;
3564 }
3565
3566 /* verify each switch target */
3567 for (targ = 0; targ < switch_count; targ++) {
3568 int offset;
3569 uint32_t abs_offset;
3570
3571 /* offsets are 32-bit, and only partly endian-swapped */
3572 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003573 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003574 abs_offset = work_insn_idx_ + offset;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003575 DCHECK_LT(abs_offset, code_item_accessor_.InsnsSizeInCodeUnits());
3576 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003577 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003578 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003579 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003580 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003581 }
jeffhaobdb76512011-09-07 11:43:16 -07003582 }
3583 }
3584
3585 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003586 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3587 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003588 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003589 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003590 bool has_catch_all_handler = false;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003591 const dex::TryItem* try_item = code_item_accessor_.FindTryItem(work_insn_idx_);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003592 CHECK(try_item != nullptr);
Mathieu Chartierdc578c72017-12-27 11:51:45 -08003593 CatchHandlerIterator iterator(code_item_accessor_, *try_item);
jeffhaobdb76512011-09-07 11:43:16 -07003594
Andreas Gampef91baf12014-07-18 15:41:00 -07003595 // Need the linker to try and resolve the handled class to check if it's Throwable.
Andreas Gampee0bbab92019-07-25 12:28:22 -07003596 ClassLinker* linker = GetClassLinker();
Andreas Gampef91baf12014-07-18 15:41:00 -07003597
Ian Rogers0571d352011-11-03 19:51:38 -07003598 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003599 dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex();
3600 if (!handler_type_idx.IsValid()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003601 has_catch_all_handler = true;
3602 } else {
3603 // It is also a catch-all if it is java.lang.Throwable.
Vladimir Marko28e012a2017-12-07 11:22:59 +00003604 ObjPtr<mirror::Class> klass =
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003605 linker->ResolveType(handler_type_idx, dex_cache_, class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003606 if (klass != nullptr) {
Vladimir Markoadbceb72018-05-29 14:34:14 +01003607 if (klass == GetClassRoot<mirror::Throwable>()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003608 has_catch_all_handler = true;
3609 }
3610 } else {
3611 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003612 DCHECK(self_->IsExceptionPending());
3613 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003614 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003615 }
jeffhaobdb76512011-09-07 11:43:16 -07003616 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003617 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3618 * "work_regs", because at runtime the exception will be thrown before the instruction
3619 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003620 */
Andreas Gampebfcca582019-04-19 12:01:55 -07003621 if (kVerifierDebug) {
3622 LogVerifyInfo() << "Updating exception handler 0x"
3623 << std::hex << iterator.GetHandlerAddress();
3624 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003625 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003626 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003627 }
jeffhaobdb76512011-09-07 11:43:16 -07003628 }
3629
3630 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003631 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3632 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003633 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003634 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003635 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003636 * The state in work_line reflects the post-execution state. If the current instruction is a
3637 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003638 * it will do so before grabbing the lock).
3639 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003640 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003641 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003642 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003643 return false;
3644 }
3645 }
3646 }
3647
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003648 /* Handle "continue". Tag the next consecutive instruction.
3649 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3650 * because it changes work_line_ when performing peephole optimization
3651 * and this change should not be used in those cases.
3652 */
Andreas Gampe6087bc22019-06-03 15:52:08 -07003653 if ((opcode_flags & Instruction::kContinue) != 0 && !exc_handler_unreachable) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003654 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003655 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003656 if (next_insn_idx >= code_item_accessor_.InsnsSizeInCodeUnits()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003657 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3658 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003659 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003660 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3661 // next instruction isn't one.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003662 if (!CheckNotMoveException(code_item_accessor_.Insns(), next_insn_idx)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003663 return false;
3664 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003665 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003666 // Make workline consistent with fallthrough computed from peephole optimization.
3667 work_line_->CopyFromLine(fallthrough_line.get());
3668 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003669 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003670 // For returns we only care about the operand to the return, all other registers are dead.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003671 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003672 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003673 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003674 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003675 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003676 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3677 // needed. If the merge changes the state of the registers then the work line will be
3678 // updated.
3679 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003680 return false;
3681 }
3682 } else {
3683 /*
3684 * We're not recording register data for the next instruction, so we don't know what the
3685 * prior state was. We have to assume that something has changed and re-evaluate it.
3686 */
Andreas Gampe51de69e2019-04-19 15:14:14 -07003687 GetModifiableInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003688 }
3689 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003690
jeffhaod1f0fde2011-09-08 17:25:33 -07003691 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003692 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003693 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003694 }
3695
3696 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003697 * Update start_guess. Advance to the next instruction of that's
3698 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003699 * neither of those exists we're in a return or throw; leave start_guess
3700 * alone and let the caller sort it out.
3701 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003702 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003703 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003704 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003705 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003706 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003707 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003708 }
3709
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003710 DCHECK_LT(*start_guess, code_item_accessor_.InsnsSizeInCodeUnits());
Mathieu Chartierde40d472015-10-15 17:47:48 -07003711 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003712
Andreas Gampe43884b22019-06-27 14:05:52 -07003713 if (flags_.have_pending_runtime_throw_failure_) {
3714 flags_.have_any_pending_runtime_throw_failure_ = true;
Andreas Gampea727e372015-08-25 09:22:37 -07003715 // Reset the pending_runtime_throw flag now.
Andreas Gampe43884b22019-06-27 14:05:52 -07003716 flags_.have_pending_runtime_throw_failure_ = false;
Andreas Gampea727e372015-08-25 09:22:37 -07003717 }
3718
jeffhaobdb76512011-09-07 11:43:16 -07003719 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003720} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003721
Andreas Gampe99db7bb2019-04-19 23:05:47 -07003722template <bool kVerifierDebug>
3723template <CheckAccess C>
3724const RegType& MethodVerifier<kVerifierDebug>::ResolveClass(dex::TypeIndex class_idx) {
Andreas Gampee0bbab92019-07-25 12:28:22 -07003725 ClassLinker* linker = GetClassLinker();
Vladimir Marko28e012a2017-12-07 11:22:59 +00003726 ObjPtr<mirror::Class> klass = can_load_classes_
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003727 ? linker->ResolveType(class_idx, dex_cache_, class_loader_)
3728 : linker->LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003729 if (can_load_classes_ && klass == nullptr) {
3730 DCHECK(self_->IsExceptionPending());
3731 self_->ClearException();
3732 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003733 const RegType* result = nullptr;
Vladimir Marko9cb0c462017-04-21 13:31:41 +01003734 if (klass != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003735 bool precise = klass->CannotBeAssignedFromOtherTypes();
3736 if (precise && !IsInstantiableOrPrimitive(klass)) {
3737 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3738 UninstantiableError(descriptor);
3739 precise = false;
3740 }
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003741 result = reg_types_.FindClass(klass, precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003742 if (result == nullptr) {
3743 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003744 result = reg_types_.InsertClass(descriptor, klass, precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003745 }
3746 } else {
3747 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Andreas Gampe51de69e2019-04-19 15:14:14 -07003748 result = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003749 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003750 DCHECK(result != nullptr);
3751 if (result->IsConflict()) {
3752 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3753 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3754 << "' in " << GetDeclaringClass();
3755 return *result;
3756 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003757
3758 // Record result of class resolution attempt.
Vladimir Markobcf17522018-06-01 13:14:32 +01003759 VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass);
David Brazdilca3c8c32016-09-06 14:04:48 +01003760
Andreas Gampe629be512017-08-25 17:09:32 -07003761 // If requested, check if access is allowed. Unresolved types are included in this check, as the
3762 // interpreter only tests whether access is allowed when a class is not pre-verified and runs in
3763 // the access-checks interpreter. If result is primitive, skip the access check.
3764 //
3765 // Note: we do this for unresolved classes to trigger re-verification at runtime.
Andreas Gampe6087bc22019-06-03 15:52:08 -07003766 if (C != CheckAccess::kNo &&
Andreas Gampedc39d322018-09-04 09:26:03 -07003767 result->IsNonZeroReferenceTypes() &&
Andreas Gampe6087bc22019-06-03 15:52:08 -07003768 ((C == CheckAccess::kYes && IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP))
3769 || !result->IsUnresolvedTypes())) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003770 const RegType& referrer = GetDeclaringClass();
David Brazdil2bb2fbd2018-11-13 18:24:26 +00003771 if ((IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP) || !referrer.IsUnresolvedTypes()) &&
3772 !referrer.CanAccess(*result)) {
Andreas Gampe629be512017-08-25 17:09:32 -07003773 Fail(VERIFY_ERROR_ACCESS_CLASS) << "(possibly) illegal class access: '"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +01003774 << referrer << "' -> '" << *result << "'";
Mathieu Chartierde40d472015-10-15 17:47:48 -07003775 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003776 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003777 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003778}
3779
Andreas Gampe99db7bb2019-04-19 23:05:47 -07003780template <bool kVerifierDebug>
Andreas Gampe6087bc22019-06-03 15:52:08 -07003781bool MethodVerifier<kVerifierDebug>::HandleMoveException(const Instruction* inst) {
3782 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
3783 // where one entrypoint to the catch block is not actually an exception path.
3784 if (work_insn_idx_ == 0) {
3785 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
3786 return true;
3787 }
3788 /*
3789 * This statement can only appear as the first instruction in an exception handler. We verify
3790 * that as part of extracting the exception type from the catch block list.
3791 */
3792 auto caught_exc_type_fn = [&]() REQUIRES_SHARED(Locks::mutator_lock_) ->
3793 std::pair<bool, const RegType*> {
3794 const RegType* common_super = nullptr;
3795 if (code_item_accessor_.TriesSize() != 0) {
3796 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
3797 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3798 const RegType* unresolved = nullptr;
3799 for (uint32_t i = 0; i < handlers_size; i++) {
3800 CatchHandlerIterator iterator(handlers_ptr);
3801 for (; iterator.HasNext(); iterator.Next()) {
3802 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3803 if (!iterator.GetHandlerTypeIndex().IsValid()) {
3804 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003805 } else {
Andreas Gampe6087bc22019-06-03 15:52:08 -07003806 // Do access checks only on resolved exception classes.
3807 const RegType& exception =
3808 ResolveClass<CheckAccess::kOnResolvedClass>(iterator.GetHandlerTypeIndex());
3809 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) {
3810 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
3811 if (exception.IsUnresolvedTypes()) {
3812 if (unresolved == nullptr) {
3813 unresolved = &exception;
3814 } else {
3815 unresolved = &unresolved->SafeMerge(exception, &reg_types_, this);
3816 }
3817 } else {
3818 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class "
3819 << exception;
3820 return std::make_pair(true, &reg_types_.Conflict());
3821 }
3822 } else if (common_super == nullptr) {
3823 common_super = &exception;
3824 } else if (common_super->Equals(exception)) {
3825 // odd case, but nothing to do
3826 } else {
3827 common_super = &common_super->Merge(exception, &reg_types_, this);
3828 if (FailOrAbort(reg_types_.JavaLangThrowable(false).IsAssignableFrom(
3829 *common_super, this),
3830 "java.lang.Throwable is not assignable-from common_super at ",
3831 work_insn_idx_)) {
3832 break;
3833 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003834 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003835 }
3836 }
3837 }
Andreas Gampe6087bc22019-06-03 15:52:08 -07003838 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003839 }
Andreas Gampe6087bc22019-06-03 15:52:08 -07003840 if (unresolved != nullptr) {
Andreas Gampefef91cc2019-07-25 14:13:23 -07003841 if (!IsAotMode() && common_super == nullptr) {
Andreas Gampe6087bc22019-06-03 15:52:08 -07003842 // This is an unreachable handler.
Andreas Gampe0a7d0b12019-06-04 10:42:29 -07003843
3844 // We need to post a failure. The compiler currently does not handle unreachable
3845 // code correctly.
Andreas Gampe0d87f992019-07-10 13:19:45 -07003846 Fail(VERIFY_ERROR_SKIP_COMPILER, /*pending_exc=*/ false)
3847 << "Unresolved catch handler, fail for compiler";
Andreas Gampe0a7d0b12019-06-04 10:42:29 -07003848
Andreas Gampe6087bc22019-06-03 15:52:08 -07003849 return std::make_pair(false, unresolved);
3850 }
3851 // Soft-fail, but do not handle this with a synthetic throw.
Andreas Gampe0d87f992019-07-10 13:19:45 -07003852 Fail(VERIFY_ERROR_NO_CLASS, /*pending_exc=*/ false) << "Unresolved catch handler";
Andreas Gampe6087bc22019-06-03 15:52:08 -07003853 if (common_super != nullptr) {
3854 unresolved = &unresolved->Merge(*common_super, &reg_types_, this);
3855 }
3856 return std::make_pair(true, unresolved);
3857 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003858 }
Andreas Gampe6087bc22019-06-03 15:52:08 -07003859 if (common_super == nullptr) {
3860 /* no catch blocks, or no catches with classes we can find */
3861 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
3862 return std::make_pair(true, &reg_types_.Conflict());
3863 }
3864 return std::make_pair(true, common_super);
3865 };
3866 auto result = caught_exc_type_fn();
3867 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), *result.second);
3868 return result.first;
Ian Rogersd81871c2011-10-03 13:57:23 -07003869}
3870
Andreas Gampe99db7bb2019-04-19 23:05:47 -07003871template <bool kVerifierDebug>
3872ArtMethod* MethodVerifier<kVerifierDebug>::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003873 uint32_t dex_method_idx, MethodType method_type) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003874 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Andreas Gampe98be1a92017-08-28 08:25:45 -07003875 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003876 if (klass_type.IsConflict()) {
3877 std::string append(" in attempt to access method ");
3878 append += dex_file_->GetMethodName(method_id);
3879 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003880 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003881 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003882 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003883 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003884 }
Vladimir Markoba118822017-06-12 15:41:56 +01003885 ObjPtr<mirror::Class> klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003886 const RegType& referrer = GetDeclaringClass();
Andreas Gampee0bbab92019-07-25 12:28:22 -07003887 ClassLinker* class_linker = GetClassLinker();
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00003888 PointerSize pointer_size = class_linker->GetImagePointerSize();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003889
Mathieu Chartiere401d142015-04-22 13:56:20 -07003890 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003891 if (res_method == nullptr) {
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00003892 res_method = class_linker->FindResolvedMethod(
3893 klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07003894 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003895
Vladimir Markoba118822017-06-12 15:41:56 +01003896 // Record result of method resolution attempt. The klass resolution has recorded whether
3897 // the class is an interface or not and therefore the type of the lookup performed above.
3898 // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type.
3899 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_method);
3900
Alex Lightafb66472017-08-01 09:54:49 -07003901 bool must_fail = false;
3902 // This is traditional and helps with screwy bytecode. It will tell you that, yes, a method
3903 // exists, but that it's called incorrectly. This significantly helps debugging, as locally it's
3904 // hard to see the differences.
3905 // If we don't have res_method here we must fail. Just use this bool to make sure of that with a
3906 // DCHECK.
Vladimir Markoba118822017-06-12 15:41:56 +01003907 if (res_method == nullptr) {
Alex Lightafb66472017-08-01 09:54:49 -07003908 must_fail = true;
Vladimir Markoba118822017-06-12 15:41:56 +01003909 // Try to find the method also with the other type for better error reporting below
3910 // but do not store such bogus lookup result in the DexCache or VerifierDeps.
David Brazdil4525e0b2018-04-05 16:57:32 +01003911 res_method = class_linker->FindIncompatibleMethod(
3912 klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx);
Vladimir Markoba118822017-06-12 15:41:56 +01003913 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003914
3915 if (res_method == nullptr) {
3916 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
David Sehr709b0702016-10-13 09:12:37 -07003917 << klass->PrettyDescriptor() << "."
David Brazdilca3c8c32016-09-06 14:04:48 +01003918 << dex_file_->GetMethodName(method_id) << " "
3919 << dex_file_->GetMethodSignature(method_id);
3920 return nullptr;
3921 }
3922
Ian Rogersd81871c2011-10-03 13:57:23 -07003923 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3924 // enforce them here.
3925 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003926 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
David Sehr709b0702016-10-13 09:12:37 -07003927 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003928 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003929 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003930 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003931 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003932 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
David Sehr709b0702016-10-13 09:12:37 -07003933 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003934 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003935 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003936
3937 // Check that interface methods are static or match interface classes.
3938 // We only allow statics if we don't have default methods enabled.
3939 //
3940 // Note: this check must be after the initializer check, as those are required to fail a class,
3941 // while this check implies an IncompatibleClassChangeError.
3942 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003943 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003944 // default methods are supported for the dex file), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003945 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003946 method_type != METHOD_STATIC &&
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003947 (!dex_file_->SupportsDefaultMethods() ||
Alex Lightb55f1ac2016-04-12 15:50:55 -07003948 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003949 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003950 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003951 << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx)
3952 << " is in an interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003953 return nullptr;
3954 }
3955 } else {
3956 if (method_type == METHOD_INTERFACE) {
3957 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003958 << "interface method " << dex_file_->PrettyMethod(dex_method_idx)
3959 << " is in a non-interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003960 return nullptr;
3961 }
3962 }
3963
Alex Lightafb66472017-08-01 09:54:49 -07003964 // Check specifically for non-public object methods being provided for interface dispatch. This
3965 // can occur if we failed to find a method with FindInterfaceMethod but later find one with
3966 // FindClassMethod for error message use.
3967 if (method_type == METHOD_INTERFACE &&
3968 res_method->GetDeclaringClass()->IsObjectClass() &&
3969 !res_method->IsPublic()) {
3970 Fail(VERIFY_ERROR_NO_METHOD) << "invoke-interface " << klass->PrettyDescriptor() << "."
3971 << dex_file_->GetMethodName(method_id) << " "
3972 << dex_file_->GetMethodSignature(method_id) << " resolved to "
3973 << "non-public object method " << res_method->PrettyMethod() << " "
3974 << "but non-public Object methods are excluded from interface "
3975 << "method resolution.";
3976 return nullptr;
3977 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003978 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003979 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07003980 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call "
3981 << res_method->PrettyMethod()
Ian Rogersad0b3a32012-04-16 14:50:24 -07003982 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003983 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003984 }
jeffhaode0d9c92012-02-27 13:58:13 -08003985 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003986 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003987 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
David Sehr709b0702016-10-13 09:12:37 -07003988 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003989 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003990 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003991 // See if the method type implied by the invoke instruction matches the access flags for the
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003992 // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two
3993 // signature polymorphic methods supported by the run-time which are native methods with variable
3994 // arguments.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003995 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003996 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003997 ((method_type == METHOD_SUPER ||
3998 method_type == METHOD_VIRTUAL ||
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003999 method_type == METHOD_INTERFACE) && res_method->IsDirect()) ||
4000 ((method_type == METHOD_POLYMORPHIC) &&
4001 (!res_method->IsNative() || !res_method->IsVarargs()))) {
Ian Rogers2fc14272012-08-30 10:56:57 -07004002 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004003 "type of " << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07004004 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004005 }
Alex Lightafb66472017-08-01 09:54:49 -07004006 // Make sure we weren't expecting to fail.
4007 DCHECK(!must_fail) << "invoke type (" << method_type << ")"
4008 << klass->PrettyDescriptor() << "."
4009 << dex_file_->GetMethodName(method_id) << " "
4010 << dex_file_->GetMethodSignature(method_id) << " unexpectedly resolved to "
4011 << res_method->PrettyMethod() << " without error. Initially this method was "
4012 << "not found so we were expecting to fail for some reason.";
jeffhao8cd6dda2012-02-22 10:15:34 -08004013 return res_method;
4014}
4015
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004016template <bool kVerifierDebug>
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004017template <class T>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004018ArtMethod* MethodVerifier<kVerifierDebug>::VerifyInvocationArgsFromIterator(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004019 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampee383d232018-06-19 12:29:51 -07004020 DCHECK_EQ(!is_range, inst->HasVarArgs());
4021
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004022 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4023 // match the call to the signature. Also, we might be calling through an abstract method
4024 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004025 const size_t expected_args = inst->VRegA();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004026 /* caught by static verifier */
4027 DCHECK(is_range || expected_args <= 5);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004028
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004029 if (expected_args > code_item_accessor_.OutsSize()) {
Orion Hodson1cda7c22017-08-10 13:06:45 +01004030 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004031 << ") exceeds outsSize ("
4032 << code_item_accessor_.OutsSize() << ")";
Orion Hodson1cda7c22017-08-10 13:06:45 +01004033 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004034 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004035
4036 /*
4037 * Check the "this" argument, which must be an instance of the class that declared the method.
4038 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4039 * rigorous check here (which is okay since we have to do it at runtime).
4040 */
4041 if (method_type != METHOD_STATIC) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004042 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004043 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Andreas Gampe43884b22019-06-27 14:05:52 -07004044 CHECK(flags_.have_pending_hard_failure_);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004045 return nullptr;
4046 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004047 bool is_init = false;
4048 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004049 if (res_method) {
4050 if (!res_method->IsConstructor()) {
4051 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
4052 return nullptr;
4053 }
4054 } else {
4055 // Check whether the name of the called method is "<init>"
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004056 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Jeff Hao0d087272014-08-04 14:47:17 -07004057 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004058 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
4059 return nullptr;
4060 }
4061 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004062 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004063 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004064 const RegType& adjusted_type = is_init
4065 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
4066 : actual_arg_type;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004067 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZeroOrNull()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004068 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07004069 // Miranda methods have the declaring interface as their declaring class, not the abstract
4070 // class. It would be wrong to use this for the type check (interface type checks are
4071 // postponed to runtime).
4072 if (res_method != nullptr && !res_method->IsMiranda()) {
Vladimir Markod93e3742018-07-18 10:58:13 +01004073 ObjPtr<mirror::Class> klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004074 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07004075 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
4076 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004077 } else {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004078 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004079 const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07004080 res_method_class = &reg_types_.FromDescriptor(
Andreas Gampe51de69e2019-04-19 15:14:14 -07004081 class_loader_.Get(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07004082 dex_file_->StringByTypeIdx(class_idx),
4083 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004084 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004085 if (!res_method_class->IsAssignableFrom(adjusted_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004086 Fail(adjusted_type.IsUnresolvedTypes()
4087 ? VERIFY_ERROR_NO_CLASS
4088 : VERIFY_ERROR_BAD_CLASS_SOFT)
4089 << "'this' argument '" << actual_arg_type << "' not instance of '"
4090 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004091 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4092 // the compiler.
Andreas Gampe43884b22019-06-27 14:05:52 -07004093 if (flags_.have_pending_hard_failure_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004094 return nullptr;
4095 }
4096 }
4097 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004098 }
4099
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004100 uint32_t arg[5];
4101 if (!is_range) {
4102 inst->GetVarArgs(arg);
4103 }
4104 uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004105 for ( ; it->HasNext(); it->Next()) {
4106 if (sig_registers >= expected_args) {
4107 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004108 " argument registers, method signature has " << sig_registers + 1 << " or more";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004109 return nullptr;
4110 }
4111
4112 const char* param_descriptor = it->GetDescriptor();
4113
4114 if (param_descriptor == nullptr) {
4115 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
4116 "component";
4117 return nullptr;
4118 }
4119
Andreas Gampe51de69e2019-04-19 15:14:14 -07004120 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_.Get(),
4121 param_descriptor,
4122 false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004123 uint32_t get_reg = is_range ? inst->VRegC() + static_cast<uint32_t>(sig_registers) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004124 arg[sig_registers];
4125 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004126 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004127 if (!src_type.IsIntegralTypes()) {
4128 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
4129 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07004130 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004131 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07004132 } else {
4133 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
4134 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4135 // the compiler.
Andreas Gampe43884b22019-06-27 14:05:52 -07004136 if (flags_.have_pending_hard_failure_) {
Andreas Gampeda9badb2015-06-05 20:22:12 -07004137 return nullptr;
4138 }
4139 } else if (reg_type.IsLongOrDoubleTypes()) {
4140 // Check that registers are consecutive (for non-range invokes). Invokes are the only
4141 // instructions not specifying register pairs by the first component, but require them
4142 // nonetheless. Only check when there's an actual register in the parameters. If there's
4143 // none, this will fail below.
4144 if (!is_range && sig_registers + 1 < expected_args) {
4145 uint32_t second_reg = arg[sig_registers + 1];
4146 if (second_reg != get_reg + 1) {
4147 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
4148 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
4149 << second_reg << ".";
4150 return nullptr;
4151 }
4152 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004153 }
4154 }
4155 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
4156 }
4157 if (expected_args != sig_registers) {
4158 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004159 " argument registers, method signature has " << sig_registers;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004160 return nullptr;
4161 }
4162 return res_method;
4163}
4164
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004165template <bool kVerifierDebug>
4166void MethodVerifier<kVerifierDebug>::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
4167 MethodType method_type,
4168 bool is_range) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004169 // As the method may not have been resolved, make this static check against what we expect.
4170 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
4171 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004172 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004173 DexFileParameterIterator it(*dex_file_,
4174 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004175 VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004176}
4177
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004178template <bool kVerifierDebug>
4179bool MethodVerifier<kVerifierDebug>::CheckCallSite(uint32_t call_site_idx) {
Orion Hodson3a842f52017-04-21 15:24:10 +01004180 if (call_site_idx >= dex_file_->NumCallSiteIds()) {
4181 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx
4182 << " >= " << dex_file_->NumCallSiteIds();
4183 return false;
4184 }
4185
Orion Hodsonc069a302017-01-18 09:23:12 +00004186 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
Alex Light1e52a072019-06-25 09:12:04 -07004187 // Check essential arguments are provided. The dex file verifier has verified indices of the
Orion Hodsonc069a302017-01-18 09:23:12 +00004188 // main values (method handle, name, method_type).
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004189 static const size_t kRequiredArguments = 3;
4190 if (it.Size() < kRequiredArguments) {
Orion Hodsonc069a302017-01-18 09:23:12 +00004191 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4192 << " has too few arguments: "
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004193 << it.Size() << " < " << kRequiredArguments;
Orion Hodsonc069a302017-01-18 09:23:12 +00004194 return false;
4195 }
4196
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004197 std::pair<const EncodedArrayValueIterator::ValueType, size_t> type_and_max[kRequiredArguments] =
4198 { { EncodedArrayValueIterator::ValueType::kMethodHandle, dex_file_->NumMethodHandles() },
4199 { EncodedArrayValueIterator::ValueType::kString, dex_file_->NumStringIds() },
4200 { EncodedArrayValueIterator::ValueType::kMethodType, dex_file_->NumProtoIds() }
4201 };
4202 uint32_t index[kRequiredArguments];
4203
4204 // Check arguments have expected types and are within permitted ranges.
4205 for (size_t i = 0; i < kRequiredArguments; ++i) {
4206 if (it.GetValueType() != type_and_max[i].first) {
4207 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx
4208 << " argument " << i << " has wrong type "
4209 << it.GetValueType() << "!=" << type_and_max[i].first;
4210 return false;
4211 }
4212 index[i] = static_cast<uint32_t>(it.GetJavaValue().i);
4213 if (index[i] >= type_and_max[i].second) {
4214 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx
4215 << " argument " << i << " bad index "
4216 << index[i] << " >= " << type_and_max[i].second;
4217 return false;
4218 }
4219 it.Next();
Orion Hodsona5dca522018-02-27 12:42:11 +00004220 }
Orion Hodson3a842f52017-04-21 15:24:10 +01004221
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004222 // Check method handle kind is valid.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004223 const dex::MethodHandleItem& mh = dex_file_->GetMethodHandle(index[0]);
Orion Hodsonc069a302017-01-18 09:23:12 +00004224 if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) {
4225 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
Orion Hodson631827d2017-04-10 14:53:47 +01004226 << " argument 0 method handle type is not InvokeStatic: "
4227 << mh.method_handle_type_;
Orion Hodsonc069a302017-01-18 09:23:12 +00004228 return false;
4229 }
Orion Hodsonc069a302017-01-18 09:23:12 +00004230 return true;
4231}
4232
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004233class MethodParamListDescriptorIterator {
4234 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004235 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004236 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
4237 params_size_(params_ == nullptr ? 0 : params_->Size()) {
4238 }
4239
4240 bool HasNext() {
4241 return pos_ < params_size_;
4242 }
4243
4244 void Next() {
4245 ++pos_;
4246 }
4247
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004248 const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004249 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
4250 }
4251
4252 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004253 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004254 size_t pos_;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004255 const dex::TypeList* params_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004256 const size_t params_size_;
4257};
4258
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004259template <bool kVerifierDebug>
4260ArtMethod* MethodVerifier<kVerifierDebug>::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08004261 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08004262 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
4263 // we're making.
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004264 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Alex Light7268d472016-01-20 15:50:01 -08004265 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004266 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004267 // Check what we can statically.
Andreas Gampe43884b22019-06-27 14:05:52 -07004268 if (!flags_.have_pending_hard_failure_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004269 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
4270 }
4271 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08004272 }
4273
Ian Rogersd81871c2011-10-03 13:57:23 -07004274 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07004275 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08004276 if (method_type == METHOD_SUPER) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004277 dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
David Brazdilca3c8c32016-09-06 14:04:48 +01004278 const RegType& reference_type = reg_types_.FromDescriptor(
Andreas Gampe51de69e2019-04-19 15:14:14 -07004279 class_loader_.Get(),
David Brazdilca3c8c32016-09-06 14:04:48 +01004280 dex_file_->StringByTypeIdx(class_idx),
4281 false);
4282 if (reference_type.IsUnresolvedTypes()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004283 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4284 return nullptr;
4285 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004286 if (reference_type.GetClass()->IsInterface()) {
Alex Light55ea94d2016-03-15 09:50:26 -07004287 if (!GetDeclaringClass().HasClass()) {
4288 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4289 << "interface invoke-super";
4290 return nullptr;
David Brazdilca3c8c32016-09-06 14:04:48 +01004291 } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004292 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07004293 << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass())
4294 << " in method "
4295 << dex_file_->PrettyMethod(dex_method_idx_) << " to method "
4296 << dex_file_->PrettyMethod(method_idx) << " references "
4297 << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass());
Alex Lightfedd91d2016-01-07 14:49:16 -08004298 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004299 }
4300 } else {
4301 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4302 if (super.IsUnresolvedTypes()) {
4303 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004304 << dex_file_->PrettyMethod(dex_method_idx_)
4305 << " to super " << res_method->PrettyMethod();
Alex Light705ad492015-09-21 11:36:30 -07004306 return nullptr;
4307 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004308 if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) ||
Aart Bikf663e342016-04-04 17:28:59 -07004309 (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Alex Light705ad492015-09-21 11:36:30 -07004310 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004311 << dex_file_->PrettyMethod(dex_method_idx_)
Alex Light705ad492015-09-21 11:36:30 -07004312 << " to super " << super
4313 << "." << res_method->GetName()
4314 << res_method->GetSignature();
4315 return nullptr;
4316 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004317 }
4318 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004319
Andreas Gampe74979b12017-05-16 09:28:06 -07004320 if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004321 // Process the signature of the calling site that is invoking the method handle.
Orion Hodson06d10a72018-05-14 08:53:38 +01004322 dex::ProtoIndex proto_idx(inst->VRegH());
4323 DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(proto_idx));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004324 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4325 } else {
4326 // Process the target method's signature.
4327 MethodParamListDescriptorIterator it(res_method);
4328 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4329 }
4330}
4331
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004332template <bool kVerifierDebug>
4333bool MethodVerifier<kVerifierDebug>::CheckSignaturePolymorphicMethod(ArtMethod* method) {
Vladimir Markod93e3742018-07-18 10:58:13 +01004334 ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004335 const char* method_name = method->GetName();
4336
4337 const char* expected_return_descriptor;
Andreas Gampee0bbab92019-07-25 12:28:22 -07004338 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = GetClassLinker()->GetClassRoots();
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004339 if (klass == GetClassRoot<mirror::MethodHandle>(class_roots)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00004340 expected_return_descriptor = mirror::MethodHandle::GetReturnTypeDescriptor(method_name);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004341 } else if (klass == GetClassRoot<mirror::VarHandle>(class_roots)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00004342 expected_return_descriptor = mirror::VarHandle::GetReturnTypeDescriptor(method_name);
4343 } else {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004344 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004345 << "Signature polymorphic method in unsuppported class: " << klass->PrettyDescriptor();
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004346 return false;
4347 }
4348
Orion Hodsonfe92d122018-01-02 10:45:17 +00004349 if (expected_return_descriptor == nullptr) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004350 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4351 << "Signature polymorphic method name invalid: " << method_name;
4352 return false;
4353 }
4354
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004355 const dex::TypeList* types = method->GetParameterTypeList();
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004356 if (types->Size() != 1) {
4357 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4358 << "Signature polymorphic method has too many arguments " << types->Size() << " != 1";
4359 return false;
4360 }
4361
4362 const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_;
4363 const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index);
4364 if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) {
4365 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4366 << "Signature polymorphic method has unexpected argument type: " << argument_descriptor;
4367 return false;
4368 }
4369
4370 const char* return_descriptor = method->GetReturnTypeDescriptor();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004371 if (strcmp(return_descriptor, expected_return_descriptor) != 0) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004372 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004373 << "Signature polymorphic method has unexpected return type: " << return_descriptor
4374 << " != " << expected_return_descriptor;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004375 return false;
4376 }
4377
4378 return true;
4379}
4380
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004381template <bool kVerifierDebug>
4382bool MethodVerifier<kVerifierDebug>::CheckSignaturePolymorphicReceiver(const Instruction* inst) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004383 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004384 if (this_type.IsZeroOrNull()) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004385 /* null pointer always passes (and always fails at run time) */
4386 return true;
4387 } else if (!this_type.IsNonZeroReferenceTypes()) {
4388 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4389 << "invoke-polymorphic receiver is not a reference: "
4390 << this_type;
4391 return false;
4392 } else if (this_type.IsUninitializedReference()) {
4393 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4394 << "invoke-polymorphic receiver is uninitialized: "
4395 << this_type;
4396 return false;
4397 } else if (!this_type.HasClass()) {
4398 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4399 << "invoke-polymorphic receiver has no class: "
4400 << this_type;
4401 return false;
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004402 } else {
Andreas Gampee0bbab92019-07-25 12:28:22 -07004403 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = GetClassLinker()->GetClassRoots();
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004404 if (!this_type.GetClass()->IsSubClass(GetClassRoot<mirror::MethodHandle>(class_roots)) &&
4405 !this_type.GetClass()->IsSubClass(GetClassRoot<mirror::VarHandle>(class_roots))) {
4406 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4407 << "invoke-polymorphic receiver is not a subclass of MethodHandle or VarHandle: "
4408 << this_type;
4409 return false;
4410 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004411 }
4412 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -07004413}
4414
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004415template <bool kVerifierDebug>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004416void MethodVerifier<kVerifierDebug>::VerifyNewArray(const Instruction* inst,
4417 bool is_filled,
4418 bool is_range) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004419 dex::TypeIndex type_idx;
Sebastien Hertz5243e912013-05-21 10:55:07 +02004420 if (!is_filled) {
4421 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004422 type_idx = dex::TypeIndex(inst->VRegC_22c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004423 } else if (!is_range) {
4424 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004425 type_idx = dex::TypeIndex(inst->VRegB_35c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004426 } else {
4427 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004428 type_idx = dex::TypeIndex(inst->VRegB_3rc());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004429 }
Andreas Gampe98be1a92017-08-28 08:25:45 -07004430 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004431 if (res_type.IsConflict()) { // bad class
4432 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004433 } else {
4434 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4435 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004436 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004437 } else if (!is_filled) {
4438 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004439 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004440 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004441 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004442 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004443 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004444 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004445 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4446 // the list and fail. It's legal, if silly, for arg_count to be zero.
Andreas Gampe51de69e2019-04-19 15:14:14 -07004447 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_.Get());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004448 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4449 uint32_t arg[5];
4450 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004451 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004452 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004453 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004454 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004455 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4456 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004457 return;
4458 }
4459 }
4460 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004461 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004462 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004463 }
4464 }
4465}
4466
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004467template <bool kVerifierDebug>
4468void MethodVerifier<kVerifierDebug>::VerifyAGet(const Instruction* inst,
4469 const RegType& insn_type,
4470 bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004471 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004472 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004473 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004474 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004475 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004476 if (array_type.IsZeroOrNull()) {
Ian Rogers89310de2012-02-01 13:47:30 -08004477 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
Andreas Gampe52f205a2017-12-01 12:16:07 -08004478 // instruction type.
4479 if (!is_primitive) {
4480 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Null());
4481 } else if (insn_type.IsInteger()) {
4482 // Pick a non-zero constant (to distinguish with null) that can fit in any primitive.
4483 // We cannot use 'insn_type' as it could be a float array or an int array.
4484 work_line_->SetRegisterType<LockOp::kClear>(
4485 this, inst->VRegA_23x(), DetermineCat1Constant(1, need_precise_constants_));
4486 } else if (insn_type.IsCategory1Types()) {
4487 // Category 1
4488 // The 'insn_type' is exactly the type we need.
4489 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07004490 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004491 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004492 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4493 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004494 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004495 }
jeffhaofc3144e2012-02-01 17:21:15 -08004496 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004497 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004498 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004499 // Unresolved array types must be reference array types.
4500 if (is_primitive) {
4501 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4502 << " source for category 1 aget";
4503 } else {
4504 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4505 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004506 // Approximate with java.lang.Object[].
4507 work_line_->SetRegisterType<LockOp::kClear>(this,
4508 inst->VRegA_23x(),
4509 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004510 }
Ian Rogers89310de2012-02-01 13:47:30 -08004511 } else {
4512 /* verify the class */
Andreas Gampe51de69e2019-04-19 15:14:14 -07004513 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_.Get());
jeffhaofc3144e2012-02-01 17:21:15 -08004514 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004515 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004516 << " source for aget-object";
4517 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004518 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004519 << " source for category 1 aget";
4520 } else if (is_primitive && !insn_type.Equals(component_type) &&
4521 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004522 (insn_type.IsLong() && component_type.IsDouble()))) {
4523 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4524 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004525 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004526 // Use knowledge of the field type which is stronger than the type inferred from the
4527 // instruction, which can't differentiate object types and ints from floats, longs from
4528 // doubles.
4529 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004530 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004531 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004532 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004533 component_type.HighHalf(&reg_types_));
4534 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004535 }
4536 }
4537 }
4538}
4539
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004540template <bool kVerifierDebug>
4541void MethodVerifier<kVerifierDebug>::VerifyPrimitivePut(const RegType& target_type,
4542 const RegType& insn_type,
4543 const uint32_t vregA) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004544 // Primitive assignability rules are weaker than regular assignability rules.
4545 bool instruction_compatible;
4546 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004547 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004548 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004549 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004550 value_compatible = value_type.IsIntegralTypes();
4551 } else if (target_type.IsFloat()) {
4552 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4553 value_compatible = value_type.IsFloatTypes();
4554 } else if (target_type.IsLong()) {
4555 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004556 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4557 // as target_type depends on the resolved type of the field.
4558 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004559 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004560 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4561 } else {
4562 value_compatible = false;
4563 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004564 } else if (target_type.IsDouble()) {
4565 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004566 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4567 // as target_type depends on the resolved type of the field.
4568 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004569 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004570 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4571 } else {
4572 value_compatible = false;
4573 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004574 } else {
4575 instruction_compatible = false; // reference with primitive store
4576 value_compatible = false; // unused
4577 }
4578 if (!instruction_compatible) {
4579 // This is a global failure rather than a class change failure as the instructions and
4580 // the descriptors for the type should have been consistent within the same file at
4581 // compile time.
4582 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4583 << "' but expected type '" << target_type << "'";
4584 return;
4585 }
4586 if (!value_compatible) {
4587 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4588 << " of type " << value_type << " but expected " << target_type << " for put";
4589 return;
4590 }
4591}
4592
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004593template <bool kVerifierDebug>
4594void MethodVerifier<kVerifierDebug>::VerifyAPut(const Instruction* inst,
4595 const RegType& insn_type,
4596 bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004597 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004598 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004599 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004600 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004601 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004602 if (array_type.IsZeroOrNull()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004603 // Null array type; this code path will fail at runtime.
4604 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004605 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4606 // and fits multiple register types.
4607 const RegType* modified_reg_type = &insn_type;
4608 if ((modified_reg_type == &reg_types_.Integer()) ||
4609 (modified_reg_type == &reg_types_.LongLo())) {
4610 // May be integer or float | long or double. Overwrite insn_type accordingly.
4611 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4612 if (modified_reg_type == &reg_types_.Integer()) {
4613 if (&value_type == &reg_types_.Float()) {
4614 modified_reg_type = &value_type;
4615 }
4616 } else {
4617 if (&value_type == &reg_types_.DoubleLo()) {
4618 modified_reg_type = &value_type;
4619 }
4620 }
4621 }
4622 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004623 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004624 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004625 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004626 // Unresolved array types must be reference array types.
4627 if (is_primitive) {
4628 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4629 << "' but unresolved type '" << array_type << "'";
4630 } else {
4631 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4632 << " because of missing class";
4633 }
Ian Rogers89310de2012-02-01 13:47:30 -08004634 } else {
Andreas Gampe51de69e2019-04-19 15:14:14 -07004635 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_.Get());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004636 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004637 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004638 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004639 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004640 if (!component_type.IsReferenceTypes()) {
4641 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4642 << " source for aput-object";
4643 } else {
4644 // The instruction agrees with the type of array, confirm the value to be stored does too
4645 // Note: we use the instruction type (rather than the component type) for aput-object as
4646 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004647 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004648 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004649 }
4650 }
4651 }
4652}
4653
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004654template <bool kVerifierDebug>
4655ArtField* MethodVerifier<kVerifierDebug>::GetStaticField(int field_idx) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004656 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Ian Rogers90040192011-12-16 08:54:29 -08004657 // Check access to class
Andreas Gampe98be1a92017-08-28 08:25:45 -07004658 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004659 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004660 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4661 field_idx, dex_file_->GetFieldName(field_id),
4662 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004663 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004664 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004665 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004666 // Accessibility checks depend on resolved fields.
David Brazdil2bb2fbd2018-11-13 18:24:26 +00004667 DCHECK(klass_type.Equals(GetDeclaringClass()) ||
4668 !failures_.empty() ||
4669 IsSdkVersionSetAndLessThan(api_level_, SdkVersion::kP));
Andreas Gampe629be512017-08-25 17:09:32 -07004670
Ian Rogers7b078e82014-09-10 14:44:24 -07004671 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004672 }
Andreas Gampee0bbab92019-07-25 12:28:22 -07004673 ClassLinker* class_linker = GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004674 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004675
4676 // Record result of the field resolution attempt.
4677 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4678
Ian Rogers7b078e82014-09-10 14:44:24 -07004679 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004680 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004681 << dex_file_->GetFieldName(field_id) << ") in "
4682 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004683 DCHECK(self_->IsExceptionPending());
4684 self_->ClearException();
4685 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004686 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4687 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004688 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004689 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004690 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004691 } else if (!field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004692 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004693 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004694 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004695 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004696}
4697
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004698template <bool kVerifierDebug>
4699ArtField* MethodVerifier<kVerifierDebug>::GetInstanceField(const RegType& obj_type, int field_idx) {
Andreas Gampeb34981b2019-05-06 13:00:40 -07004700 if (!obj_type.IsZeroOrNull() && !obj_type.IsReferenceTypes()) {
4701 // Trying to read a field from something that isn't a reference.
4702 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4703 << "non-reference type " << obj_type;
4704 return nullptr;
4705 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004706 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Aart Bik31883642016-06-06 15:02:44 -07004707 // Check access to class.
Andreas Gampe98be1a92017-08-28 08:25:45 -07004708 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004709 if (klass_type.IsConflict()) {
4710 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4711 field_idx, dex_file_->GetFieldName(field_id),
4712 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004713 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004714 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004715 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004716 // Accessibility checks depend on resolved fields.
David Brazdil2bb2fbd2018-11-13 18:24:26 +00004717 DCHECK(klass_type.Equals(GetDeclaringClass()) ||
4718 !failures_.empty() ||
4719 IsSdkVersionSetAndLessThan(api_level_, SdkVersion::kP));
Andreas Gampe629be512017-08-25 17:09:32 -07004720
Ian Rogers7b078e82014-09-10 14:44:24 -07004721 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004722 }
Andreas Gampee0bbab92019-07-25 12:28:22 -07004723 ClassLinker* class_linker = GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004724 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004725
4726 // Record result of the field resolution attempt.
4727 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4728
Ian Rogers7b078e82014-09-10 14:44:24 -07004729 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004730 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004731 << dex_file_->GetFieldName(field_id) << ") in "
4732 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004733 DCHECK(self_->IsExceptionPending());
4734 self_->ClearException();
4735 return nullptr;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004736 } else if (obj_type.IsZeroOrNull()) {
Aart Bik31883642016-06-06 15:02:44 -07004737 // Cannot infer and check type, however, access will cause null pointer exception.
4738 // Fall through into a few last soft failure checks below.
Ian Rogerse1758fe2012-04-19 11:31:15 -07004739 } else {
David Brazdil0d638bb2016-07-27 15:29:25 +01004740 std::string temp;
Mathieu Chartier3398c782016-09-30 10:27:43 -07004741 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004742 const RegType& field_klass =
Vladimir Markobcf17522018-06-01 13:14:32 +01004743 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004744 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004745 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004746 // the field is declared in this class.
4747 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4748 // appear in constructors.
4749 if (!obj_type.IsUninitializedThisReference() ||
4750 !IsConstructor() ||
4751 !field_klass.Equals(GetDeclaringClass())) {
David Sehr709b0702016-10-13 09:12:37 -07004752 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField()
David Brazdil68b5c0b2016-01-19 14:25:29 +00004753 << " of a not fully initialized object within the context"
David Sehr709b0702016-10-13 09:12:37 -07004754 << " of " << dex_file_->PrettyMethod(dex_method_idx_);
David Brazdil68b5c0b2016-01-19 14:25:29 +00004755 return nullptr;
4756 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004757 } else if (!field_klass.IsAssignableFrom(obj_type, this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004758 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4759 // of C1. For resolution to occur the declared class of the field must be compatible with
4760 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004761 VerifyError type;
Andreas Gampefef91cc2019-07-25 14:13:23 -07004762 bool is_aot = IsAotMode();
Andreas Gampe66596242016-04-14 10:55:04 -07004763 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4764 // Compiler & unresolved types involved, retry at runtime.
4765 type = VerifyError::VERIFY_ERROR_NO_CLASS;
4766 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004767 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4768 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004769 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4770 }
David Sehr709b0702016-10-13 09:12:37 -07004771 Fail(type) << "cannot access instance field " << field->PrettyField()
Andreas Gampe66596242016-04-14 10:55:04 -07004772 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004773 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004774 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004775 }
Aart Bik31883642016-06-06 15:02:44 -07004776
4777 // Few last soft failure checks.
4778 if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4779 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004780 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004781 << " from " << GetDeclaringClass();
4782 return nullptr;
4783 } else if (field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004784 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004785 << " to not be static";
4786 return nullptr;
4787 }
4788
4789 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004790}
4791
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004792template <bool kVerifierDebug>
4793template <FieldAccessType kAccType>
4794void MethodVerifier<kVerifierDebug>::VerifyISFieldAccess(const Instruction* inst,
4795 const RegType& insn_type,
4796 bool is_primitive,
4797 bool is_static) {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004798 uint32_t field_idx = GetFieldIdxOfFieldAccess(inst, is_static);
Mathieu Chartierc7853442015-03-27 14:35:38 -07004799 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004800 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004801 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004802 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004803 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004804
4805 // One is not allowed to access fields on uninitialized references, except to write to
4806 // fields in the constructor (before calling another constructor).
4807 // GetInstanceField does an assignability check which will fail for uninitialized types.
4808 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4809 // checks at the same time that we're verifying a constructor).
4810 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4811 object_type.IsUninitializedThisReference();
4812 const RegType& adjusted_type = should_adjust
4813 ? GetRegTypeCache()->FromUninitialized(object_type)
4814 : object_type;
4815 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe43884b22019-06-27 14:05:52 -07004816 if (UNLIKELY(flags_.have_pending_hard_failure_)) {
Andreas Gampe896df402014-10-20 22:25:29 -07004817 return;
4818 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08004819 if (should_adjust) {
4820 if (field == nullptr) {
4821 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
4822 << "to the superclass being initialized in "
David Sehr709b0702016-10-13 09:12:37 -07004823 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004824 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4825 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
David Sehr709b0702016-10-13 09:12:37 -07004826 << field->PrettyField() << " of a not fully initialized "
Alex Light4a2c8fc2016-02-12 11:01:54 -08004827 << "object within the context of "
David Sehr709b0702016-10-13 09:12:37 -07004828 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004829 return;
4830 }
4831 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004832 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004833 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004834 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004835 if (kAccType == FieldAccessType::kAccPut) {
4836 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07004837 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Andreas Gampe896df402014-10-20 22:25:29 -07004838 << " from other class " << GetDeclaringClass();
Aart Bikc2bc2652016-05-23 14:58:49 -07004839 // Keep hunting for possible hard fails.
Andreas Gampe896df402014-10-20 22:25:29 -07004840 }
4841 }
4842
Mathieu Chartier3398c782016-09-30 10:27:43 -07004843 ObjPtr<mirror::Class> field_type_class =
Vladimir Marko208f6702017-12-08 12:00:50 +00004844 can_load_classes_ ? field->ResolveType() : field->LookupResolvedType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004845 if (field_type_class != nullptr) {
Mathieu Chartier3398c782016-09-30 10:27:43 -07004846 field_type = &FromClass(field->GetTypeDescriptor(),
Vladimir Markobcf17522018-06-01 13:14:32 +01004847 field_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004848 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004849 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004850 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4851 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004852 }
David Brazdil2bb2fbd2018-11-13 18:24:26 +00004853 } else if (IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP)) {
Andreas Gampe7da4c402017-08-25 11:30:48 -07004854 // If we don't have the field (it seems we failed resolution) and this is a PUT, we need to
4855 // redo verification at runtime as the field may be final, unless the field id shows it's in
4856 // the same class.
4857 //
4858 // For simplicity, it is OK to not distinguish compile-time vs runtime, and post this an
4859 // ACCESS_FIELD failure at runtime. This has the same effect as NO_FIELD - punting the class
4860 // to the access-checks interpreter.
4861 //
4862 // Note: see b/34966607. This and above may be changed in the future.
4863 if (kAccType == FieldAccessType::kAccPut) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004864 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Andreas Gampe7da4c402017-08-25 11:30:48 -07004865 const char* field_class_descriptor = dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Andreas Gampe51de69e2019-04-19 15:14:14 -07004866 const RegType* field_class_type = &reg_types_.FromDescriptor(class_loader_.Get(),
Andreas Gampe7da4c402017-08-25 11:30:48 -07004867 field_class_descriptor,
4868 false);
4869 if (!field_class_type->Equals(GetDeclaringClass())) {
4870 Fail(VERIFY_ERROR_ACCESS_FIELD) << "could not check field put for final field modify of "
4871 << field_class_descriptor
4872 << "."
4873 << dex_file_->GetFieldName(field_id)
4874 << " from other class "
4875 << GetDeclaringClass();
4876 }
4877 }
Ian Rogers0d604842012-04-16 14:50:24 -07004878 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004879 if (field_type == nullptr) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004880 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004881 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Andreas Gampe51de69e2019-04-19 15:14:14 -07004882 field_type = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004883 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004884 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004885 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004886 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4887 "Unexpected third access type");
4888 if (kAccType == FieldAccessType::kAccPut) {
4889 // sput or iput.
4890 if (is_primitive) {
4891 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004892 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004893 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004894 // If the field type is not a reference, this is a global failure rather than
4895 // a class change failure as the instructions and the descriptors for the type
4896 // should have been consistent within the same file at compile time.
4897 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4898 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004899 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004900 << " to be compatible with type '" << insn_type
4901 << "' but found type '" << *field_type
4902 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004903 return;
4904 }
4905 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004906 }
Andreas Gampe896df402014-10-20 22:25:29 -07004907 } else if (kAccType == FieldAccessType::kAccGet) {
4908 // sget or iget.
4909 if (is_primitive) {
4910 if (field_type->Equals(insn_type) ||
4911 (field_type->IsFloat() && insn_type.IsInteger()) ||
4912 (field_type->IsDouble() && insn_type.IsLong())) {
4913 // expected that read is of the correct primitive type or that int reads are reading
4914 // floats or long reads are reading doubles
4915 } else {
4916 // This is a global failure rather than a class change failure as the instructions and
4917 // the descriptors for the type should have been consistent within the same file at
4918 // compile time
David Sehr709b0702016-10-13 09:12:37 -07004919 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07004920 << " to be of type '" << insn_type
4921 << "' but found type '" << *field_type << "' in get";
4922 return;
4923 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004924 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004925 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004926 // If the field type is not a reference, this is a global failure rather than
4927 // a class change failure as the instructions and the descriptors for the type
4928 // should have been consistent within the same file at compile time.
4929 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4930 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004931 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004932 << " to be compatible with type '" << insn_type
4933 << "' but found type '" << *field_type
4934 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004935 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004936 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004937 }
Andreas Gampe896df402014-10-20 22:25:29 -07004938 return;
4939 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004940 }
Andreas Gampe896df402014-10-20 22:25:29 -07004941 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004942 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004943 } else {
4944 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4945 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004946 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004947 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004948 }
4949}
4950
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004951template <bool kVerifierDebug>
4952bool MethodVerifier<kVerifierDebug>::UpdateRegisters(uint32_t next_insn,
4953 RegisterLine* merge_line,
4954 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004955 bool changed = true;
4956 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004957 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004958 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004959 * We haven't processed this instruction before, and we haven't touched the registers here, so
4960 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4961 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004962 */
Andreas Gampea727e372015-08-25 09:22:37 -07004963 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004964 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004965 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004966 merge_line->VerifyMonitorStackEmpty(this);
4967
Ian Rogersb8c78592013-07-25 23:52:52 +00004968 // For returns we only care about the operand to the return, all other registers are dead.
4969 // Initialize them as conflicts so they don't add to GC and deoptimization information.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004970 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004971 AdjustReturnLine(this, ret_inst, target_line);
Aart Bik31883642016-06-06 15:02:44 -07004972 // Directly bail if a hard failure was found.
Andreas Gampe43884b22019-06-27 14:05:52 -07004973 if (flags_.have_pending_hard_failure_) {
Aart Bikb0526322016-06-01 14:06:00 -07004974 return false;
4975 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004976 }
jeffhaobdb76512011-09-07 11:43:16 -07004977 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08004978 RegisterLineArenaUniquePtr copy;
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004979 if (kVerifierDebug) {
Andreas Gamped09c0592019-04-19 15:44:05 -07004980 copy.reset(RegisterLine::Create(target_line->NumRegs(), allocator_, GetRegTypeCache()));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004981 copy->CopyFromLine(target_line);
4982 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004983 changed = target_line->MergeRegisters(this, merge_line);
Andreas Gampe43884b22019-06-27 14:05:52 -07004984 if (flags_.have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004985 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004986 }
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004987 if (kVerifierDebug && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004988 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004989 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004990 << copy->Dump(this) << " MERGE\n"
4991 << merge_line->Dump(this) << " ==\n"
Andreas Gampe2ad6cce2019-04-11 16:17:39 -07004992 << target_line->Dump(this);
jeffhaobdb76512011-09-07 11:43:16 -07004993 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004994 if (update_merge_line && changed) {
4995 merge_line->CopyFromLine(target_line);
4996 }
jeffhaobdb76512011-09-07 11:43:16 -07004997 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004998 if (changed) {
Andreas Gampe51de69e2019-04-19 15:14:14 -07004999 GetModifiableInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07005000 }
5001 return true;
5002}
5003
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005004template <bool kVerifierDebug>
5005const RegType& MethodVerifier<kVerifierDebug>::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005006 if (return_type_ == nullptr) {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00005007 if (method_being_verified_ != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01005008 ObjPtr<mirror::Class> return_type_class = can_load_classes_
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00005009 ? method_being_verified_->ResolveReturnType()
5010 : method_being_verified_->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005011 if (return_type_class != nullptr) {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00005012 return_type_ = &FromClass(method_being_verified_->GetReturnTypeDescriptor(),
Vladimir Markobcf17522018-06-01 13:14:32 +01005013 return_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07005014 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005015 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07005016 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
5017 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005018 }
5019 }
5020 if (return_type_ == nullptr) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08005021 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
5022 const dex::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -08005023 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005024 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Andreas Gampe51de69e2019-04-19 15:14:14 -07005025 return_type_ = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005026 }
5027 }
5028 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005029}
5030
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005031template <bool kVerifierDebug>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005032const RegType& MethodVerifier<kVerifierDebug>::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01005033 if (precise) {
5034 // Precise constant type.
5035 return reg_types_.FromCat1Const(value, true);
5036 } else {
5037 // Imprecise constant type.
5038 if (value < -32768) {
5039 return reg_types_.IntConstant();
5040 } else if (value < -128) {
5041 return reg_types_.ShortConstant();
5042 } else if (value < 0) {
5043 return reg_types_.ByteConstant();
5044 } else if (value == 0) {
5045 return reg_types_.Zero();
5046 } else if (value == 1) {
5047 return reg_types_.One();
5048 } else if (value < 128) {
5049 return reg_types_.PosByteConstant();
5050 } else if (value < 32768) {
5051 return reg_types_.PosShortConstant();
5052 } else if (value < 65536) {
5053 return reg_types_.CharConstant();
5054 } else {
5055 return reg_types_.IntConstant();
5056 }
5057 }
5058}
5059
Andreas Gampefc25ae92019-04-19 22:22:57 -07005060} // namespace
5061} // namespace impl
5062
5063MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005064 ClassLinker* class_linker,
Andreas Gampef1468b52019-07-26 09:22:39 -07005065 ArenaPool* arena_pool,
Andreas Gampefc25ae92019-04-19 22:22:57 -07005066 const DexFile* dex_file,
5067 const dex::CodeItem* code_item,
5068 uint32_t dex_method_idx,
5069 bool can_load_classes,
5070 bool allow_thread_suspension,
Andreas Gampefef91cc2019-07-25 14:13:23 -07005071 bool allow_soft_failures,
5072 bool aot_mode)
Andreas Gampefc25ae92019-04-19 22:22:57 -07005073 : self_(self),
Andreas Gampef1468b52019-07-26 09:22:39 -07005074 arena_stack_(arena_pool),
Andreas Gampefc25ae92019-04-19 22:22:57 -07005075 allocator_(&arena_stack_),
Andreas Gampee0bbab92019-07-25 12:28:22 -07005076 reg_types_(class_linker, can_load_classes, allocator_, allow_thread_suspension),
Andreas Gampefc25ae92019-04-19 22:22:57 -07005077 reg_table_(allocator_),
5078 work_insn_idx_(dex::kDexNoIndex),
5079 dex_method_idx_(dex_method_idx),
5080 dex_file_(dex_file),
5081 code_item_accessor_(*dex_file, code_item),
Andreas Gampefef91cc2019-07-25 14:13:23 -07005082 // TODO: make it designated initialization when we compile as C++20.
5083 flags_({false, false, false, false, aot_mode}),
Andreas Gampefc25ae92019-04-19 22:22:57 -07005084 encountered_failure_types_(0),
5085 can_load_classes_(can_load_classes),
5086 allow_soft_failures_(allow_soft_failures),
5087 has_check_casts_(false),
Andreas Gampee0bbab92019-07-25 12:28:22 -07005088 class_linker_(class_linker),
Andreas Gampefc25ae92019-04-19 22:22:57 -07005089 link_(nullptr) {
5090 self->PushVerifier(this);
5091}
5092
5093MethodVerifier::~MethodVerifier() {
5094 Thread::Current()->PopVerifier(this);
5095 STLDeleteElements(&failure_messages_);
5096}
5097
5098MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005099 ClassLinker* class_linker,
Andreas Gampef1468b52019-07-26 09:22:39 -07005100 ArenaPool* arena_pool,
Andreas Gampefc25ae92019-04-19 22:22:57 -07005101 uint32_t method_idx,
5102 const DexFile* dex_file,
5103 Handle<mirror::DexCache> dex_cache,
5104 Handle<mirror::ClassLoader> class_loader,
5105 const dex::ClassDef& class_def,
5106 const dex::CodeItem* code_item,
5107 ArtMethod* method,
5108 uint32_t method_access_flags,
5109 CompilerCallbacks* callbacks,
5110 bool allow_soft_failures,
5111 HardFailLogMode log_level,
5112 bool need_precise_constants,
5113 uint32_t api_level,
Andreas Gampefef91cc2019-07-25 14:13:23 -07005114 bool aot_mode,
Alex Lightdb55a112019-10-17 10:32:47 -07005115 bool allow_suspension,
Andreas Gampefc25ae92019-04-19 22:22:57 -07005116 std::string* hard_failure_msg) {
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005117 if (VLOG_IS_ON(verifier_debug)) {
5118 return VerifyMethod<true>(self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005119 class_linker,
Andreas Gampef1468b52019-07-26 09:22:39 -07005120 arena_pool,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005121 method_idx,
5122 dex_file,
5123 dex_cache,
5124 class_loader,
5125 class_def,
5126 code_item,
5127 method,
5128 method_access_flags,
5129 callbacks,
5130 allow_soft_failures,
5131 log_level,
5132 need_precise_constants,
5133 api_level,
Andreas Gampefef91cc2019-07-25 14:13:23 -07005134 aot_mode,
Alex Lightdb55a112019-10-17 10:32:47 -07005135 allow_suspension,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005136 hard_failure_msg);
5137 } else {
5138 return VerifyMethod<false>(self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005139 class_linker,
Andreas Gampef1468b52019-07-26 09:22:39 -07005140 arena_pool,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005141 method_idx,
5142 dex_file,
5143 dex_cache,
5144 class_loader,
5145 class_def,
5146 code_item,
5147 method,
5148 method_access_flags,
5149 callbacks,
5150 allow_soft_failures,
5151 log_level,
5152 need_precise_constants,
5153 api_level,
Andreas Gampefef91cc2019-07-25 14:13:23 -07005154 aot_mode,
Alex Lightdb55a112019-10-17 10:32:47 -07005155 allow_suspension,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005156 hard_failure_msg);
5157 }
5158}
5159
5160template <bool kVerifierDebug>
5161MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005162 ClassLinker* class_linker,
Andreas Gampef1468b52019-07-26 09:22:39 -07005163 ArenaPool* arena_pool,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005164 uint32_t method_idx,
5165 const DexFile* dex_file,
5166 Handle<mirror::DexCache> dex_cache,
5167 Handle<mirror::ClassLoader> class_loader,
5168 const dex::ClassDef& class_def,
5169 const dex::CodeItem* code_item,
5170 ArtMethod* method,
5171 uint32_t method_access_flags,
5172 CompilerCallbacks* callbacks,
5173 bool allow_soft_failures,
5174 HardFailLogMode log_level,
5175 bool need_precise_constants,
5176 uint32_t api_level,
Andreas Gampefef91cc2019-07-25 14:13:23 -07005177 bool aot_mode,
Alex Lightdb55a112019-10-17 10:32:47 -07005178 bool allow_suspension,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005179 std::string* hard_failure_msg) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005180 MethodVerifier::FailureData result;
5181 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
5182
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005183 impl::MethodVerifier<kVerifierDebug> verifier(self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005184 class_linker,
Andreas Gampef1468b52019-07-26 09:22:39 -07005185 arena_pool,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005186 dex_file,
Andreas Gampe0134a282019-07-29 11:34:55 -07005187 code_item,
5188 method_idx,
Alex Lightdb55a112019-10-17 10:32:47 -07005189 /* can_load_classes= */ allow_suspension,
5190 /* allow_thread_suspension= */ allow_suspension,
Andreas Gampe0134a282019-07-29 11:34:55 -07005191 allow_soft_failures,
5192 aot_mode,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005193 dex_cache,
5194 class_loader,
5195 class_def,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005196 method,
5197 method_access_flags,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005198 need_precise_constants,
5199 /* verify to dump */ false,
Alex Lighte2ddce32019-05-22 17:08:35 +00005200 /* fill_register_lines= */ false,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005201 api_level);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005202 if (verifier.Verify()) {
5203 // Verification completed, however failures may be pending that didn't cause the verification
5204 // to hard fail.
Andreas Gampe43884b22019-06-27 14:05:52 -07005205 CHECK(!verifier.flags_.have_pending_hard_failure_);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005206
5207 if (code_item != nullptr && callbacks != nullptr) {
5208 // Let the interested party know that the method was verified.
5209 callbacks->MethodVerified(&verifier);
5210 }
5211
5212 if (verifier.failures_.size() != 0) {
5213 if (VLOG_IS_ON(verifier)) {
5214 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
5215 << dex_file->PrettyMethod(method_idx) << "\n");
5216 }
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005217 if (kVerifierDebug) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005218 LOG(INFO) << verifier.info_messages_.str();
5219 verifier.Dump(LOG_STREAM(INFO));
5220 }
5221 result.kind = FailureKind::kSoftFailure;
5222 if (method != nullptr &&
5223 !CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) {
5224 method->SetDontCompile();
5225 }
5226 }
5227 if (method != nullptr) {
5228 if (verifier.HasInstructionThatWillThrow()) {
5229 method->SetDontCompile();
Andreas Gampefef91cc2019-07-25 14:13:23 -07005230 if (aot_mode && (callbacks != nullptr) && !callbacks->IsBootImage()) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005231 // When compiling apps, make HasInstructionThatWillThrow a soft error to trigger
5232 // re-verification at runtime.
5233 // The dead code after the throw is not verified and might be invalid. This may cause
5234 // the JIT compiler to crash since it assumes that all the code is valid.
5235 //
5236 // There's a strong assumption that the entire boot image is verified and all its dex
5237 // code is valid (even the dead and unverified one). As such this is done only for apps.
5238 // (CompilerDriver DCHECKs in VerifyClassVisitor that methods from boot image are
5239 // fully verified).
5240 result.kind = FailureKind::kSoftFailure;
5241 }
5242 }
5243 if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) {
5244 method->SetMustCountLocks();
5245 }
5246 }
5247 } else {
5248 // Bad method data.
5249 CHECK_NE(verifier.failures_.size(), 0U);
5250
Andreas Gampe43884b22019-06-27 14:05:52 -07005251 if (UNLIKELY(verifier.flags_.have_pending_experimental_failure_)) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005252 // Failed due to being forced into interpreter. This is ok because
5253 // we just want to skip verification.
5254 result.kind = FailureKind::kSoftFailure;
5255 } else {
Andreas Gampe43884b22019-06-27 14:05:52 -07005256 CHECK(verifier.flags_.have_pending_hard_failure_);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005257 if (VLOG_IS_ON(verifier)) {
5258 log_level = std::max(HardFailLogMode::kLogVerbose, log_level);
5259 }
5260 if (log_level >= HardFailLogMode::kLogVerbose) {
5261 LogSeverity severity;
5262 switch (log_level) {
5263 case HardFailLogMode::kLogVerbose:
5264 severity = LogSeverity::VERBOSE;
5265 break;
5266 case HardFailLogMode::kLogWarning:
5267 severity = LogSeverity::WARNING;
5268 break;
5269 case HardFailLogMode::kLogInternalFatal:
5270 severity = LogSeverity::FATAL_WITHOUT_ABORT;
5271 break;
5272 default:
5273 LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level);
5274 UNREACHABLE();
5275 }
5276 verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in "
5277 << dex_file->PrettyMethod(method_idx)
5278 << "\n");
5279 }
5280 if (hard_failure_msg != nullptr) {
5281 CHECK(!verifier.failure_messages_.empty());
5282 *hard_failure_msg =
5283 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
5284 }
5285 result.kind = FailureKind::kHardFailure;
5286
5287 if (callbacks != nullptr) {
5288 // Let the interested party know that we failed the class.
5289 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(class_def));
5290 callbacks->ClassRejected(ref);
5291 }
5292 }
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005293 if (kVerifierDebug || VLOG_IS_ON(verifier)) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005294 LOG(ERROR) << verifier.info_messages_.str();
5295 verifier.Dump(LOG_STREAM(ERROR));
5296 }
5297 // Under verifier-debug, dump the complete log into the error message.
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005298 if (kVerifierDebug && hard_failure_msg != nullptr) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005299 hard_failure_msg->append("\n");
5300 hard_failure_msg->append(verifier.info_messages_.str());
5301 hard_failure_msg->append("\n");
5302 std::ostringstream oss;
5303 verifier.Dump(oss);
5304 hard_failure_msg->append(oss.str());
5305 }
5306 }
5307 if (kTimeVerifyMethod) {
5308 uint64_t duration_ns = NanoTime() - start_ns;
5309 if (duration_ns > MsToNs(Runtime::Current()->GetVerifierLoggingThresholdMs())) {
Andreas Gampeefdd1b02019-05-07 12:30:10 -07005310 double bytecodes_per_second =
5311 verifier.code_item_accessor_.InsnsSizeInCodeUnits() / (duration_ns * 1e-9);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005312 LOG(WARNING) << "Verification of " << dex_file->PrettyMethod(method_idx)
5313 << " took " << PrettyDuration(duration_ns)
Andreas Gampeefdd1b02019-05-07 12:30:10 -07005314 << (impl::IsLargeMethod(verifier.CodeItem()) ? " (large method)" : "")
Andreas Gamped91f8412019-06-13 10:11:14 -07005315 << " (" << StringPrintf("%.2f", bytecodes_per_second) << " bytecodes/s)"
5316 << " (" << verifier.allocator_.ApproximatePeakBytes()
5317 << "B approximate peak alloc)";
Andreas Gampefc25ae92019-04-19 22:22:57 -07005318 }
5319 }
5320 result.types = verifier.encountered_failure_types_;
5321 return result;
5322}
5323
Alex Lighte2ddce32019-05-22 17:08:35 +00005324MethodVerifier* MethodVerifier::CalculateVerificationInfo(
5325 Thread* self,
5326 ArtMethod* method,
5327 Handle<mirror::DexCache> dex_cache,
5328 Handle<mirror::ClassLoader> class_loader) {
5329 std::unique_ptr<impl::MethodVerifier<false>> verifier(
5330 new impl::MethodVerifier<false>(self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005331 Runtime::Current()->GetClassLinker(),
Andreas Gampef1468b52019-07-26 09:22:39 -07005332 Runtime::Current()->GetArenaPool(),
Alex Lighte2ddce32019-05-22 17:08:35 +00005333 method->GetDexFile(),
Andreas Gampe0134a282019-07-29 11:34:55 -07005334 method->GetCodeItem(),
5335 method->GetDexMethodIndex(),
5336 /* can_load_classes= */ false,
5337 /* allow_thread_suspension= */ false,
5338 /* allow_soft_failures= */ true,
5339 Runtime::Current()->IsAotCompiler(),
Alex Lighte2ddce32019-05-22 17:08:35 +00005340 dex_cache,
5341 class_loader,
5342 *method->GetDeclaringClass()->GetClassDef(),
Alex Lighte2ddce32019-05-22 17:08:35 +00005343 method,
5344 method->GetAccessFlags(),
Alex Lighte2ddce32019-05-22 17:08:35 +00005345 /* need_precise_constants= */ true,
5346 /* verify_to_dump= */ false,
Alex Lighte2ddce32019-05-22 17:08:35 +00005347 /* fill_register_lines= */ true,
Alex Lightaf52cbe2019-05-22 15:05:09 -07005348 // Just use the verifier at the current skd-version.
5349 // This might affect what soft-verifier errors are reported.
5350 // Callers can then filter out relevant errors if needed.
5351 Runtime::Current()->GetTargetSdkVersion()));
Alex Lighte2ddce32019-05-22 17:08:35 +00005352 verifier->Verify();
5353 if (VLOG_IS_ON(verifier)) {
5354 verifier->DumpFailures(VLOG_STREAM(verifier));
5355 VLOG(verifier) << verifier->info_messages_.str();
5356 verifier->Dump(VLOG_STREAM(verifier));
5357 }
Andreas Gampe43884b22019-06-27 14:05:52 -07005358 if (verifier->flags_.have_pending_hard_failure_) {
Alex Lighte2ddce32019-05-22 17:08:35 +00005359 return nullptr;
5360 } else {
5361 return verifier.release();
5362 }
5363}
5364
Andreas Gampefc25ae92019-04-19 22:22:57 -07005365MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
5366 VariableIndentationOutputStream* vios,
5367 uint32_t dex_method_idx,
5368 const DexFile* dex_file,
5369 Handle<mirror::DexCache> dex_cache,
5370 Handle<mirror::ClassLoader> class_loader,
5371 const dex::ClassDef& class_def,
5372 const dex::CodeItem* code_item,
5373 ArtMethod* method,
5374 uint32_t method_access_flags,
5375 uint32_t api_level) {
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005376 impl::MethodVerifier<false>* verifier = new impl::MethodVerifier<false>(
5377 self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005378 Runtime::Current()->GetClassLinker(),
Andreas Gampef1468b52019-07-26 09:22:39 -07005379 Runtime::Current()->GetArenaPool(),
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005380 dex_file,
Andreas Gampe0134a282019-07-29 11:34:55 -07005381 code_item,
5382 dex_method_idx,
5383 /* can_load_classes= */ true,
5384 /* allow_thread_suspension= */ true,
5385 /* allow_soft_failures= */ true,
5386 Runtime::Current()->IsAotCompiler(),
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005387 dex_cache,
5388 class_loader,
5389 class_def,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005390 method,
5391 method_access_flags,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005392 /* need_precise_constants= */ true,
5393 /* verify_to_dump= */ true,
Alex Lighte2ddce32019-05-22 17:08:35 +00005394 /* fill_register_lines= */ false,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005395 api_level);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005396 verifier->Verify();
5397 verifier->DumpFailures(vios->Stream());
5398 vios->Stream() << verifier->info_messages_.str();
5399 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
5400 // and querying any info is dangerous/can abort.
Andreas Gampe43884b22019-06-27 14:05:52 -07005401 if (verifier->flags_.have_pending_hard_failure_) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005402 delete verifier;
5403 return nullptr;
5404 } else {
5405 verifier->Dump(vios);
5406 return verifier;
5407 }
5408}
5409
5410void MethodVerifier::FindLocksAtDexPc(
5411 ArtMethod* m,
5412 uint32_t dex_pc,
5413 std::vector<MethodVerifier::DexLockInfo>* monitor_enter_dex_pcs,
5414 uint32_t api_level) {
5415 StackHandleScope<2> hs(Thread::Current());
5416 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
5417 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005418 impl::MethodVerifier<false> verifier(hs.Self(),
Andreas Gampee0bbab92019-07-25 12:28:22 -07005419 Runtime::Current()->GetClassLinker(),
Andreas Gampef1468b52019-07-26 09:22:39 -07005420 Runtime::Current()->GetArenaPool(),
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005421 m->GetDexFile(),
Andreas Gampe0134a282019-07-29 11:34:55 -07005422 m->GetCodeItem(),
5423 m->GetDexMethodIndex(),
5424 /* can_load_classes= */ false,
5425 /* allow_thread_suspension= */ false,
5426 /* allow_soft_failures= */ true,
5427 Runtime::Current()->IsAotCompiler(),
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005428 dex_cache,
5429 class_loader,
5430 m->GetClassDef(),
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005431 m,
5432 m->GetAccessFlags(),
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005433 /* need_precise_constants= */ false,
5434 /* verify_to_dump= */ false,
Alex Lighte2ddce32019-05-22 17:08:35 +00005435 /* fill_register_lines= */ false,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005436 api_level);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005437 verifier.interesting_dex_pc_ = dex_pc;
5438 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
5439 verifier.FindLocksAtDexPc();
5440}
5441
5442MethodVerifier* MethodVerifier::CreateVerifier(Thread* self,
5443 const DexFile* dex_file,
5444 Handle<mirror::DexCache> dex_cache,
5445 Handle<mirror::ClassLoader> class_loader,
5446 const dex::ClassDef& class_def,
5447 const dex::CodeItem* code_item,
5448 uint32_t method_idx,
5449 ArtMethod* method,
5450 uint32_t access_flags,
5451 bool can_load_classes,
5452 bool allow_soft_failures,
5453 bool need_precise_constants,
5454 bool verify_to_dump,
5455 bool allow_thread_suspension,
5456 uint32_t api_level) {
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005457 return new impl::MethodVerifier<false>(self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005458 Runtime::Current()->GetClassLinker(),
Andreas Gampef1468b52019-07-26 09:22:39 -07005459 Runtime::Current()->GetArenaPool(),
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005460 dex_file,
Andreas Gampe0134a282019-07-29 11:34:55 -07005461 code_item,
5462 method_idx,
5463 can_load_classes,
5464 allow_thread_suspension,
5465 allow_soft_failures,
5466 Runtime::Current()->IsAotCompiler(),
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005467 dex_cache,
5468 class_loader,
5469 class_def,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005470 method,
5471 access_flags,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005472 need_precise_constants,
5473 verify_to_dump,
Alex Lighte2ddce32019-05-22 17:08:35 +00005474 /* fill_register_lines= */ false,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005475 api_level);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005476}
5477
Andreas Gampee0bbab92019-07-25 12:28:22 -07005478void MethodVerifier::Init(ClassLinker* class_linker) {
5479 art::verifier::RegTypeCache::Init(class_linker);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08005480}
5481
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005482void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005483 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08005484}
jeffhaod1224c72012-02-29 13:43:08 -08005485
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005486void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
5487 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07005488}
5489
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005490void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
5491 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08005492}
5493
Andreas Gampe4146e062019-07-10 13:18:04 -07005494std::ostream& MethodVerifier::Fail(VerifyError error, bool pending_exc) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005495 // Mark the error type as encountered.
5496 encountered_failure_types_ |= static_cast<uint32_t>(error);
5497
Andreas Gampe4146e062019-07-10 13:18:04 -07005498 if (pending_exc) {
5499 switch (error) {
5500 case VERIFY_ERROR_NO_CLASS:
5501 case VERIFY_ERROR_NO_FIELD:
5502 case VERIFY_ERROR_NO_METHOD:
5503 case VERIFY_ERROR_ACCESS_CLASS:
5504 case VERIFY_ERROR_ACCESS_FIELD:
5505 case VERIFY_ERROR_ACCESS_METHOD:
5506 case VERIFY_ERROR_INSTANTIATION:
5507 case VERIFY_ERROR_CLASS_CHANGE:
5508 case VERIFY_ERROR_FORCE_INTERPRETER:
5509 case VERIFY_ERROR_LOCKING:
Andreas Gampefef91cc2019-07-25 14:13:23 -07005510 if (IsAotMode() || !can_load_classes_) {
Andreas Gampe4146e062019-07-10 13:18:04 -07005511 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
5512 // class change and instantiation errors into soft verification errors so that we
5513 // re-verify at runtime. We may fail to find or to agree on access because of not yet
5514 // available class loaders, or class loaders that will differ at runtime. In these cases,
5515 // we don't want to affect the soundness of the code being compiled. Instead, the
5516 // generated code runs "slow paths" that dynamically perform the verification and cause
5517 // the behavior to be that akin to an interpreter.
5518 error = VERIFY_ERROR_BAD_CLASS_SOFT;
5519 } else {
5520 // If we fail again at runtime, mark that this instruction would throw and force this
5521 // method to be executed using the interpreter with checks.
5522 flags_.have_pending_runtime_throw_failure_ = true;
Andreas Gampe16a08f62019-07-18 10:59:08 -07005523 }
5524 // How to handle runtime failures for instructions that are not flagged kThrow.
5525 //
5526 // The verifier may fail before we touch any instruction, for the signature of a method. So
5527 // add a check.
5528 if (work_insn_idx_ < dex::kDexNoIndex) {
5529 const Instruction& inst = code_item_accessor_.InstructionAt(work_insn_idx_);
5530 Instruction::Code opcode = inst.Opcode();
5531 if ((Instruction::FlagsOf(opcode) & Instruction::kThrow) == 0 &&
5532 !impl::IsCompatThrow(opcode) &&
5533 GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gamped84794d2019-07-18 13:40:03 -07005534 if (Runtime::Current()->IsVerifierMissingKThrowFatal()) {
5535 LOG(FATAL) << "Unexpected throw: " << std::hex << work_insn_idx_ << " " << opcode;
5536 UNREACHABLE();
5537 }
Andreas Gampe16a08f62019-07-18 10:59:08 -07005538 // We need to save the work_line if the instruction wasn't throwing before. Otherwise
5539 // we'll try to merge garbage.
5540 // Note: this assumes that Fail is called before we do any work_line modifications.
5541 saved_line_->CopyFromLine(work_line_.get());
Andreas Gampefc25ae92019-04-19 22:22:57 -07005542 }
5543 }
Andreas Gampe4146e062019-07-10 13:18:04 -07005544 break;
Andreas Gampe7a82acc2019-07-01 13:54:21 -07005545
Andreas Gampe4146e062019-07-10 13:18:04 -07005546 // Indication that verification should be retried at runtime.
5547 case VERIFY_ERROR_BAD_CLASS_SOFT:
5548 if (!allow_soft_failures_) {
5549 flags_.have_pending_hard_failure_ = true;
5550 }
5551 break;
5552
5553 // Hard verification failures at compile time will still fail at runtime, so the class is
5554 // marked as rejected to prevent it from being compiled.
5555 case VERIFY_ERROR_BAD_CLASS_HARD: {
Andreas Gampe4a30f892019-07-10 04:57:58 +00005556 flags_.have_pending_hard_failure_ = true;
Andreas Gampe4146e062019-07-10 13:18:04 -07005557 break;
Andreas Gampe4a30f892019-07-10 04:57:58 +00005558 }
Andreas Gampe4a30f892019-07-10 04:57:58 +00005559
Andreas Gampe0d87f992019-07-10 13:19:45 -07005560 case VERIFY_ERROR_SKIP_COMPILER:
Andreas Gampe4146e062019-07-10 13:18:04 -07005561 // Nothing to do, just remember the failure type.
5562 break;
Andreas Gampe4a30f892019-07-10 04:57:58 +00005563 }
Andreas Gampe4146e062019-07-10 13:18:04 -07005564 } else if (kIsDebugBuild) {
5565 CHECK_NE(error, VERIFY_ERROR_BAD_CLASS_SOFT);
5566 CHECK_NE(error, VERIFY_ERROR_BAD_CLASS_HARD);
Andreas Gampe4a30f892019-07-10 04:57:58 +00005567 }
Andreas Gampe4146e062019-07-10 13:18:04 -07005568
Andreas Gampefc25ae92019-04-19 22:22:57 -07005569 failures_.push_back(error);
5570 std::string location(StringPrintf("%s: [0x%X] ", dex_file_->PrettyMethod(dex_method_idx_).c_str(),
5571 work_insn_idx_));
5572 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
5573 failure_messages_.push_back(failure_message);
5574 return *failure_message;
Andreas Gampef23f33d2015-06-23 14:18:17 -07005575}
5576
Andreas Gampefc25ae92019-04-19 22:22:57 -07005577ScopedNewLine MethodVerifier::LogVerifyInfo() {
5578 ScopedNewLine ret{info_messages_};
5579 ret << "VFY: " << dex_file_->PrettyMethod(dex_method_idx_)
5580 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
5581 return ret;
5582}
5583
5584static FailureKind FailureKindMax(FailureKind fk1, FailureKind fk2) {
5585 static_assert(FailureKind::kNoFailure < FailureKind::kSoftFailure
5586 && FailureKind::kSoftFailure < FailureKind::kHardFailure,
5587 "Unexpected FailureKind order");
5588 return std::max(fk1, fk2);
5589}
5590
5591void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) {
5592 kind = FailureKindMax(kind, fd.kind);
5593 types |= fd.types;
Andreas Gampe51de69e2019-04-19 15:14:14 -07005594}
5595
Ian Rogersd81871c2011-10-03 13:57:23 -07005596} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005597} // namespace art