blob: a1f9a2ab089ede7825f54a1fd1b1e9fa745f7032 [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
123template <bool kVerifierDebug>
Andreas Gampefc25ae92019-04-19 22:22:57 -0700124class MethodVerifier final : public ::art::verifier::MethodVerifier {
125 public:
126 bool IsInstanceConstructor() const {
127 return IsConstructor() && !IsStatic();
128 }
129
130 const RegType& ResolveCheckedClass(dex::TypeIndex class_idx) override
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700131 REQUIRES_SHARED(Locks::mutator_lock_) {
132 DCHECK(!HasFailures());
133 const RegType& result = ResolveClass<CheckAccess::kYes>(class_idx);
134 DCHECK(!HasFailures());
135 return result;
136 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700137
138 void FindLocksAtDexPc() REQUIRES_SHARED(Locks::mutator_lock_);
139
140 private:
141 MethodVerifier(Thread* self,
142 const DexFile* dex_file,
143 Handle<mirror::DexCache> dex_cache,
144 Handle<mirror::ClassLoader> class_loader,
145 const dex::ClassDef& class_def,
146 const dex::CodeItem* code_item,
147 uint32_t method_idx,
148 ArtMethod* method,
149 uint32_t access_flags,
150 bool can_load_classes,
151 bool allow_soft_failures,
152 bool need_precise_constants,
153 bool verify_to_dump,
154 bool allow_thread_suspension,
Alex Lighte2ddce32019-05-22 17:08:35 +0000155 bool fill_register_lines_,
Andreas Gampefc25ae92019-04-19 22:22:57 -0700156 uint32_t api_level)
157 REQUIRES_SHARED(Locks::mutator_lock_);
158
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700159 void UninstantiableError(const char* descriptor) {
160 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
161 << "non-instantiable klass " << descriptor;
162 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700163 static bool IsInstantiableOrPrimitive(ObjPtr<mirror::Class> klass)
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700164 REQUIRES_SHARED(Locks::mutator_lock_) {
165 return klass->IsInstantiable() || klass->IsPrimitive();
166 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700167
168 // Is the method being verified a constructor? See the comment on the field.
169 bool IsConstructor() const {
170 return is_constructor_;
171 }
172
173 // Is the method verified static?
174 bool IsStatic() const {
175 return (method_access_flags_ & kAccStatic) != 0;
176 }
177
178 // Adds the given string to the beginning of the last failure message.
179 void PrependToLastFailMessage(std::string);
180
181 // Adds the given string to the end of the last failure message.
182 void AppendToLastFailMessage(const std::string& append);
183
Andreas Gampefc25ae92019-04-19 22:22:57 -0700184 /*
185 * Compute the width of the instruction at each address in the instruction stream, and store it in
186 * insn_flags_. Addresses that are in the middle of an instruction, or that are part of switch
187 * table data, are not touched (so the caller should probably initialize "insn_flags" to zero).
188 *
189 * The "new_instance_count_" and "monitor_enter_count_" fields in vdata are also set.
190 *
191 * Performs some static checks, notably:
192 * - opcode of first instruction begins at index 0
193 * - only documented instructions may appear
194 * - each instruction follows the last
195 * - last byte of last instruction is at (code_length-1)
196 *
197 * Logs an error and returns "false" on failure.
198 */
199 bool ComputeWidthsAndCountOps();
200
201 /*
202 * Set the "in try" flags for all instructions protected by "try" statements. Also sets the
203 * "branch target" flags for exception handlers.
204 *
205 * Call this after widths have been set in "insn_flags".
206 *
207 * Returns "false" if something in the exception table looks fishy, but we're expecting the
208 * exception table to be somewhat sane.
209 */
210 bool ScanTryCatchBlocks() REQUIRES_SHARED(Locks::mutator_lock_);
211
212 /*
213 * Perform static verification on all instructions in a method.
214 *
215 * Walks through instructions in a method calling VerifyInstruction on each.
216 */
217 template <bool kAllowRuntimeOnlyInstructions>
218 bool VerifyInstructions();
219
220 /*
221 * Perform static verification on an instruction.
222 *
223 * As a side effect, this sets the "branch target" flags in InsnFlags.
224 *
225 * "(CF)" items are handled during code-flow analysis.
226 *
227 * v3 4.10.1
228 * - target of each jump and branch instruction must be valid
229 * - targets of switch statements must be valid
230 * - operands referencing constant pool entries must be valid
231 * - (CF) operands of getfield, putfield, getstatic, putstatic must be valid
232 * - (CF) operands of method invocation instructions must be valid
233 * - (CF) only invoke-direct can call a method starting with '<'
234 * - (CF) <clinit> must never be called explicitly
235 * - operands of instanceof, checkcast, new (and variants) must be valid
236 * - new-array[-type] limited to 255 dimensions
237 * - can't use "new" on an array class
238 * - (?) limit dimensions in multi-array creation
239 * - local variable load/store register values must be in valid range
240 *
241 * v3 4.11.1.2
242 * - branches must be within the bounds of the code array
243 * - targets of all control-flow instructions are the start of an instruction
244 * - register accesses fall within range of allocated registers
245 * - (N/A) access to constant pool must be of appropriate type
246 * - code does not end in the middle of an instruction
247 * - execution cannot fall off the end of the code
248 * - (earlier) for each exception handler, the "try" area must begin and
249 * end at the start of an instruction (end can be at the end of the code)
250 * - (earlier) for each exception handler, the handler must start at a valid
251 * instruction
252 */
253 template <bool kAllowRuntimeOnlyInstructions>
254 bool VerifyInstruction(const Instruction* inst, uint32_t code_offset);
255
256 /* Ensure that the register index is valid for this code item. */
257 bool CheckRegisterIndex(uint32_t idx);
258
259 /* Ensure that the wide register index is valid for this code item. */
260 bool CheckWideRegisterIndex(uint32_t idx);
261
262 // Perform static checks on an instruction referencing a CallSite. All we do here is ensure that
263 // the call site index is in the valid range.
264 bool CheckCallSiteIndex(uint32_t idx);
265
266 // Perform static checks on a field Get or set instruction. All we do here is ensure that the
267 // field index is in the valid range.
268 bool CheckFieldIndex(uint32_t idx);
269
270 // Perform static checks on a method invocation instruction. All we do here is ensure that the
271 // method index is in the valid range.
272 bool CheckMethodIndex(uint32_t idx);
273
274 // Perform static checks on an instruction referencing a constant method handle. All we do here
275 // is ensure that the method index is in the valid range.
276 bool CheckMethodHandleIndex(uint32_t idx);
277
278 // Perform static checks on a "new-instance" instruction. Specifically, make sure the class
279 // reference isn't for an array class.
280 bool CheckNewInstance(dex::TypeIndex idx);
281
282 // Perform static checks on a prototype indexing instruction. All we do here is ensure that the
283 // prototype index is in the valid range.
284 bool CheckPrototypeIndex(uint32_t idx);
285
286 /* Ensure that the string index is in the valid range. */
287 bool CheckStringIndex(uint32_t idx);
288
289 // Perform static checks on an instruction that takes a class constant. Ensure that the class
290 // index is in the valid range.
291 bool CheckTypeIndex(dex::TypeIndex idx);
292
293 // Perform static checks on a "new-array" instruction. Specifically, make sure they aren't
294 // creating an array of arrays that causes the number of dimensions to exceed 255.
295 bool CheckNewArray(dex::TypeIndex idx);
296
297 // Verify an array data table. "cur_offset" is the offset of the fill-array-data instruction.
298 bool CheckArrayData(uint32_t cur_offset);
299
300 // Verify that the target of a branch instruction is valid. We don't expect code to jump directly
301 // into an exception handler, but it's valid to do so as long as the target isn't a
302 // "move-exception" instruction. We verify that in a later stage.
303 // The dex format forbids certain instructions from branching to themselves.
304 // Updates "insn_flags_", setting the "branch target" flag.
305 bool CheckBranchTarget(uint32_t cur_offset);
306
307 // Verify a switch table. "cur_offset" is the offset of the switch instruction.
308 // Updates "insn_flags_", setting the "branch target" flag.
309 bool CheckSwitchTargets(uint32_t cur_offset);
310
311 // Check the register indices used in a "vararg" instruction, such as invoke-virtual or
312 // filled-new-array.
313 // - vA holds word count (0-5), args[] have values.
314 // There are some tests we don't do here, e.g. we don't try to verify that invoking a method that
315 // takes a double is done with consecutive registers. This requires parsing the target method
316 // signature, which we will be doing later on during the code flow analysis.
317 bool CheckVarArgRegs(uint32_t vA, uint32_t arg[]);
318
319 // Check the register indices used in a "vararg/range" instruction, such as invoke-virtual/range
320 // or filled-new-array/range.
321 // - vA holds word count, vC holds index of first reg.
322 bool CheckVarArgRangeRegs(uint32_t vA, uint32_t vC);
323
324 // Checks the method matches the expectations required to be signature polymorphic.
325 bool CheckSignaturePolymorphicMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
326
327 // Checks the invoked receiver matches the expectations for signature polymorphic methods.
328 bool CheckSignaturePolymorphicReceiver(const Instruction* inst) REQUIRES_SHARED(Locks::mutator_lock_);
329
330 // Extract the relative offset from a branch instruction.
331 // Returns "false" on failure (e.g. this isn't a branch instruction).
332 bool GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
333 bool* selfOkay);
334
335 /* Perform detailed code-flow analysis on a single method. */
336 bool VerifyCodeFlow() REQUIRES_SHARED(Locks::mutator_lock_);
337
338 // Set the register types for the first instruction in the method based on the method signature.
339 // This has the side-effect of validating the signature.
340 bool SetTypesFromSignature() REQUIRES_SHARED(Locks::mutator_lock_);
341
342 /*
343 * Perform code flow on a method.
344 *
345 * The basic strategy is as outlined in v3 4.11.1.2: set the "changed" bit on the first
346 * instruction, process it (setting additional "changed" bits), and repeat until there are no
347 * more.
348 *
349 * v3 4.11.1.1
350 * - (N/A) operand stack is always the same size
351 * - operand stack [registers] contain the correct types of values
352 * - local variables [registers] contain the correct types of values
353 * - methods are invoked with the appropriate arguments
354 * - fields are assigned using values of appropriate types
355 * - opcodes have the correct type values in operand registers
356 * - there is never an uninitialized class instance in a local variable in code protected by an
357 * exception handler (operand stack is okay, because the operand stack is discarded when an
358 * exception is thrown) [can't know what's a local var w/o the debug info -- should fall out of
359 * register typing]
360 *
361 * v3 4.11.1.2
362 * - execution cannot fall off the end of the code
363 *
364 * (We also do many of the items described in the "static checks" sections, because it's easier to
365 * do them here.)
366 *
367 * We need an array of RegType values, one per register, for every instruction. If the method uses
368 * monitor-enter, we need extra data for every register, and a stack for every "interesting"
369 * instruction. In theory this could become quite large -- up to several megabytes for a monster
370 * function.
371 *
372 * NOTE:
373 * The spec forbids backward branches when there's an uninitialized reference in a register. The
374 * idea is to prevent something like this:
375 * loop:
376 * move r1, r0
377 * new-instance r0, MyClass
378 * ...
379 * if-eq rN, loop // once
380 * initialize r0
381 *
382 * This leaves us with two different instances, both allocated by the same instruction, but only
383 * one is initialized. The scheme outlined in v3 4.11.1.4 wouldn't catch this, so they work around
384 * it by preventing backward branches. We achieve identical results without restricting code
385 * reordering by specifying that you can't execute the new-instance instruction if a register
386 * contains an uninitialized instance created by that same instruction.
387 */
388 bool CodeFlowVerifyMethod() REQUIRES_SHARED(Locks::mutator_lock_);
389
390 /*
391 * Perform verification for a single instruction.
392 *
393 * This requires fully decoding the instruction to determine the effect it has on registers.
394 *
395 * Finds zero or more following instructions and sets the "changed" flag if execution at that
396 * point needs to be (re-)evaluated. Register changes are merged into "reg_types_" at the target
397 * addresses. Does not set or clear any other flags in "insn_flags_".
398 */
399 bool CodeFlowVerifyInstruction(uint32_t* start_guess)
400 REQUIRES_SHARED(Locks::mutator_lock_);
401
402 // Perform verification of a new array instruction
403 void VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range)
404 REQUIRES_SHARED(Locks::mutator_lock_);
405
406 // Helper to perform verification on puts of primitive type.
407 void VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
408 const uint32_t vregA) REQUIRES_SHARED(Locks::mutator_lock_);
409
410 // Perform verification of an aget instruction. The destination register's type will be set to
411 // be that of component type of the array unless the array type is unknown, in which case a
412 // bottom type inferred from the type of instruction is used. is_primitive is false for an
413 // aget-object.
414 void VerifyAGet(const Instruction* inst, const RegType& insn_type,
415 bool is_primitive) REQUIRES_SHARED(Locks::mutator_lock_);
416
417 // Perform verification of an aput instruction.
418 void VerifyAPut(const Instruction* inst, const RegType& insn_type,
419 bool is_primitive) REQUIRES_SHARED(Locks::mutator_lock_);
420
421 // Lookup instance field and fail for resolution violations
422 ArtField* GetInstanceField(const RegType& obj_type, int field_idx)
423 REQUIRES_SHARED(Locks::mutator_lock_);
424
425 // Lookup static field and fail for resolution violations
426 ArtField* GetStaticField(int field_idx) REQUIRES_SHARED(Locks::mutator_lock_);
427
428 // Perform verification of an iget/sget/iput/sput instruction.
Andreas Gampefc25ae92019-04-19 22:22:57 -0700429 template <FieldAccessType kAccType>
430 void VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
431 bool is_primitive, bool is_static)
432 REQUIRES_SHARED(Locks::mutator_lock_);
433
Andreas Gampefc25ae92019-04-19 22:22:57 -0700434 // Resolves a class based on an index and, if C is kYes, performs access checks to ensure
435 // the referrer can access the resolved class.
436 template <CheckAccess C>
437 const RegType& ResolveClass(dex::TypeIndex class_idx)
438 REQUIRES_SHARED(Locks::mutator_lock_);
439
440 /*
441 * For the "move-exception" instruction at "work_insn_idx_", which must be at an exception handler
442 * address, determine the Join of all exceptions that can land here. Fails if no matching
443 * exception handler can be found or if the Join of exception types fails.
444 */
445 const RegType& GetCaughtExceptionType()
446 REQUIRES_SHARED(Locks::mutator_lock_);
447
448 /*
449 * Resolves a method based on an index and performs access checks to ensure
450 * the referrer can access the resolved method.
451 * Does not throw exceptions.
452 */
453 ArtMethod* ResolveMethodAndCheckAccess(uint32_t method_idx, MethodType method_type)
454 REQUIRES_SHARED(Locks::mutator_lock_);
455
456 /*
457 * Verify the arguments to a method. We're executing in "method", making
458 * a call to the method reference in vB.
459 *
460 * If this is a "direct" invoke, we allow calls to <init>. For calls to
461 * <init>, the first argument may be an uninitialized reference. Otherwise,
462 * calls to anything starting with '<' will be rejected, as will any
463 * uninitialized reference arguments.
464 *
465 * For non-static method calls, this will verify that the method call is
466 * appropriate for the "this" argument.
467 *
468 * The method reference is in vBBBB. The "is_range" parameter determines
469 * whether we use 0-4 "args" values or a range of registers defined by
470 * vAA and vCCCC.
471 *
472 * Widening conversions on integers and references are allowed, but
473 * narrowing conversions are not.
474 *
475 * Returns the resolved method on success, null on failure (with *failure
476 * set appropriately).
477 */
478 ArtMethod* VerifyInvocationArgs(const Instruction* inst, MethodType method_type, bool is_range)
479 REQUIRES_SHARED(Locks::mutator_lock_);
480
481 // Similar checks to the above, but on the proto. Will be used when the method cannot be
482 // resolved.
483 void VerifyInvocationArgsUnresolvedMethod(const Instruction* inst, MethodType method_type,
484 bool is_range)
485 REQUIRES_SHARED(Locks::mutator_lock_);
486
487 template <class T>
488 ArtMethod* VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
489 MethodType method_type, bool is_range,
490 ArtMethod* res_method)
491 REQUIRES_SHARED(Locks::mutator_lock_);
492
493 /*
494 * Verify the arguments present for a call site. Returns "true" if all is well, "false" otherwise.
495 */
496 bool CheckCallSite(uint32_t call_site_idx);
497
498 /*
499 * Verify that the target instruction is not "move-exception". It's important that the only way
500 * to execute a move-exception is as the first instruction of an exception handler.
501 * Returns "true" if all is well, "false" if the target instruction is move-exception.
502 */
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700503 bool CheckNotMoveException(const uint16_t* insns, int insn_idx) {
504 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
505 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
506 return false;
507 }
508 return true;
509 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700510
511 /*
512 * Verify that the target instruction is not "move-result". It is important that we cannot
513 * branch to move-result instructions, but we have to make this a distinct check instead of
514 * adding it to CheckNotMoveException, because it is legal to continue into "move-result"
515 * instructions - as long as the previous instruction was an invoke, which is checked elsewhere.
516 */
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700517 bool CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
518 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
519 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
520 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
521 return false;
522 }
523 return true;
524 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700525
526 /*
527 * Verify that the target instruction is not "move-result" or "move-exception". This is to
528 * be used when checking branch and switch instructions, but not instructions that can
529 * continue.
530 */
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700531 bool CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
532 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
533 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700534
535 /*
536 * Control can transfer to "next_insn". Merge the registers from merge_line into the table at
537 * next_insn, and set the changed flag on the target address if any of the registers were changed.
538 * In the case of fall-through, update the merge line on a change as its the working line for the
539 * next instruction.
540 * Returns "false" if an error is encountered.
541 */
542 bool UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line, bool update_merge_line)
543 REQUIRES_SHARED(Locks::mutator_lock_);
544
545 // Return the register type for the method.
546 const RegType& GetMethodReturnType() REQUIRES_SHARED(Locks::mutator_lock_);
547
548 // Get a type representing the declaring class of the method.
549 const RegType& GetDeclaringClass() REQUIRES_SHARED(Locks::mutator_lock_);
550
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700551 InstructionFlags* CurrentInsnFlags() {
552 return &GetModifiableInstructionFlags(work_insn_idx_);
553 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700554
555 const RegType& DetermineCat1Constant(int32_t value, bool precise)
556 REQUIRES_SHARED(Locks::mutator_lock_);
557
558 // Try to create a register type from the given class. In case a precise type is requested, but
559 // the class is not instantiable, a soft error (of type NO_CLASS) will be enqueued and a
560 // non-precise reference will be returned.
561 // Note: we reuse NO_CLASS as this will throw an exception at runtime, when the failing class is
562 // actually touched.
563 const RegType& FromClass(const char* descriptor, ObjPtr<mirror::Class> klass, bool precise)
564 REQUIRES_SHARED(Locks::mutator_lock_);
565
566 ALWAYS_INLINE bool FailOrAbort(bool condition, const char* error_msg, uint32_t work_insn_idx);
567
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700568 ALWAYS_INLINE InstructionFlags& GetModifiableInstructionFlags(size_t index) {
569 return insn_flags_[index];
570 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700571
572 // Returns the method index of an invoke instruction.
573 uint16_t GetMethodIdxOfInvoke(const Instruction* inst)
574 REQUIRES_SHARED(Locks::mutator_lock_);
575 // Returns the field index of a field access instruction.
576 uint16_t GetFieldIdxOfFieldAccess(const Instruction* inst, bool is_static)
577 REQUIRES_SHARED(Locks::mutator_lock_);
578
579 // Run verification on the method. Returns true if verification completes and false if the input
580 // has an irrecoverable corruption.
581 bool Verify() override REQUIRES_SHARED(Locks::mutator_lock_);
582
583 // Dump the failures encountered by the verifier.
584 std::ostream& DumpFailures(std::ostream& os);
585
586 // Dump the state of the verifier, namely each instruction, what flags are set on it, register
587 // information
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700588 void Dump(std::ostream& os) REQUIRES_SHARED(Locks::mutator_lock_) {
589 VariableIndentationOutputStream vios(&os);
590 Dump(&vios);
591 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700592 void Dump(VariableIndentationOutputStream* vios) REQUIRES_SHARED(Locks::mutator_lock_);
593
Andreas Gampe6087bc22019-06-03 15:52:08 -0700594 bool HandleMoveException(const Instruction* inst) REQUIRES_SHARED(Locks::mutator_lock_);
595
Andreas Gampefc25ae92019-04-19 22:22:57 -0700596 ArtMethod* method_being_verified_; // Its ArtMethod representation if known.
597 const uint32_t method_access_flags_; // Method's access flags.
598 const RegType* return_type_; // Lazily computed return type of the method.
599 // The dex_cache for the declaring class of the method.
600 Handle<mirror::DexCache> dex_cache_ GUARDED_BY(Locks::mutator_lock_);
601 // The class loader for the declaring class of the method.
602 Handle<mirror::ClassLoader> class_loader_ GUARDED_BY(Locks::mutator_lock_);
603 const dex::ClassDef& class_def_; // The class def of the declaring class of the method.
604 const RegType* declaring_class_; // Lazily computed reg type of the method's declaring class.
605
606 // The dex PC of a FindLocksAtDexPc request, -1 otherwise.
607 uint32_t interesting_dex_pc_;
608 // The container into which FindLocksAtDexPc should write the registers containing held locks,
609 // null if we're not doing FindLocksAtDexPc.
610 std::vector<DexLockInfo>* monitor_enter_dex_pcs_;
611
612
613 // An optimization where instead of generating unique RegTypes for constants we use imprecise
614 // constants that cover a range of constants. This isn't good enough for deoptimization that
615 // avoids loading from registers in the case of a constant as the dex instruction set lost the
616 // notion of whether a value should be in a floating point or general purpose register file.
617 const bool need_precise_constants_;
618
619 // Indicates whether we verify to dump the info. In that case we accept quickened instructions
620 // even though we might detect to be a compiler. Should only be set when running
621 // VerifyMethodAndDump.
622 const bool verify_to_dump_;
623
624 // Whether or not we call AllowThreadSuspension periodically, we want a way to disable this for
625 // thread dumping checkpoints since we may get thread suspension at an inopportune time due to
626 // FindLocksAtDexPC, resulting in deadlocks.
627 const bool allow_thread_suspension_;
628
629 // Whether the method seems to be a constructor. Note that this field exists as we can't trust
630 // the flags in the dex file. Some older code does not mark methods named "<init>" and "<clinit>"
631 // correctly.
632 //
633 // Note: this flag is only valid once Verify() has started.
634 bool is_constructor_;
635
Alex Lighte2ddce32019-05-22 17:08:35 +0000636 // Whether to attempt to fill all register lines for (ex) debugger use.
637 bool fill_register_lines_;
638
Andreas Gampefc25ae92019-04-19 22:22:57 -0700639 // API level, for dependent checks. Note: we do not use '0' for unset here, to simplify checks.
640 // Instead, unset level should correspond to max().
641 const uint32_t api_level_;
642
643 friend class ::art::verifier::MethodVerifier;
644
645 DISALLOW_COPY_AND_ASSIGN(MethodVerifier);
646};
647
Andreas Gampe7c038102014-10-27 20:08:46 -0700648// Note: returns true on failure.
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700649template <bool kVerifierDebug>
650inline bool MethodVerifier<kVerifierDebug>::FailOrAbort(bool condition,
651 const char* error_msg,
652 uint32_t work_insn_idx) {
Andreas Gampe7c038102014-10-27 20:08:46 -0700653 if (kIsDebugBuild) {
Andreas Gampef8f36c22016-11-19 14:51:52 -0800654 // In a debug build, abort if the error condition is wrong. Only warn if
655 // we are already aborting (as this verification is likely run to print
656 // lock information).
657 if (LIKELY(gAborting == 0)) {
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700658 DCHECK(condition) << error_msg << work_insn_idx << " "
659 << dex_file_->PrettyMethod(dex_method_idx_);
Andreas Gampef8f36c22016-11-19 14:51:52 -0800660 } else {
661 if (!condition) {
662 LOG(ERROR) << error_msg << work_insn_idx;
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700663 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
Andreas Gampef8f36c22016-11-19 14:51:52 -0800664 return true;
665 }
666 }
Andreas Gampe7c038102014-10-27 20:08:46 -0700667 } else {
668 // In a non-debug build, just fail the class.
669 if (!condition) {
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700670 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
Andreas Gampe7c038102014-10-27 20:08:46 -0700671 return true;
672 }
673 }
674
675 return false;
676}
677
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800678static bool IsLargeMethod(const CodeItemDataAccessor& accessor) {
679 if (!accessor.HasCodeItem()) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700680 return false;
681 }
682
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800683 uint16_t registers_size = accessor.RegistersSize();
684 uint32_t insns_size = accessor.InsnsSizeInCodeUnits();
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700685
686 return registers_size * insns_size > 4*1024*1024;
687}
688
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700689template <bool kVerifierDebug>
690MethodVerifier<kVerifierDebug>::MethodVerifier(Thread* self,
691 const DexFile* dex_file,
692 Handle<mirror::DexCache> dex_cache,
693 Handle<mirror::ClassLoader> class_loader,
694 const dex::ClassDef& class_def,
695 const dex::CodeItem* code_item,
696 uint32_t dex_method_idx,
697 ArtMethod* method,
698 uint32_t method_access_flags,
699 bool can_load_classes,
700 bool allow_soft_failures,
701 bool need_precise_constants,
702 bool verify_to_dump,
703 bool allow_thread_suspension,
Alex Lighte2ddce32019-05-22 17:08:35 +0000704 bool fill_register_lines,
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700705 uint32_t api_level)
Andreas Gampefc25ae92019-04-19 22:22:57 -0700706 : art::verifier::MethodVerifier(self,
707 dex_file,
708 code_item,
709 dex_method_idx,
710 can_load_classes,
711 allow_thread_suspension,
712 allow_soft_failures),
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000713 method_being_verified_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700714 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700715 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800716 dex_cache_(dex_cache),
717 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700718 class_def_(class_def),
Ian Rogers7b078e82014-09-10 14:44:24 -0700719 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700720 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700721 monitor_enter_dex_pcs_(nullptr),
Ian Rogers46960fe2014-05-23 10:43:43 -0700722 need_precise_constants_(need_precise_constants),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800723 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700724 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700725 is_constructor_(false),
Alex Lighte2ddce32019-05-22 17:08:35 +0000726 fill_register_lines_(fill_register_lines),
Andreas Gampe6cc23ac2018-08-24 15:22:43 -0700727 api_level_(api_level == 0 ? std::numeric_limits<uint32_t>::max() : api_level) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700728}
729
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700730template <bool kVerifierDebug>
731void MethodVerifier<kVerifierDebug>::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700732 CHECK(monitor_enter_dex_pcs_ != nullptr);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800733 CHECK(code_item_accessor_.HasCodeItem()); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700734
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800735 // Quick check whether there are any monitor_enter instructions before verifying.
736 for (const DexInstructionPcPair& inst : code_item_accessor_) {
737 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
738 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
739 // verification. In practice, the phase we want relies on data structures set up by all the
740 // earlier passes, so we just run the full method verification and bail out early when we've
741 // got what we wanted.
742 Verify();
743 return;
744 }
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700745 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700746}
747
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700748template <bool kVerifierDebug>
749bool MethodVerifier<kVerifierDebug>::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700750 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
751 // the name.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800752 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Andreas Gampee6215c02015-08-31 18:54:38 -0700753 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
754 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
755 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
756 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
757 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
758 if ((method_access_flags_ & kAccConstructor) != 0) {
759 if (!constructor_by_name) {
760 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
761 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700762 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700763 }
764 is_constructor_ = true;
765 } else if (constructor_by_name) {
David Sehr709b0702016-10-13 09:12:37 -0700766 LOG(WARNING) << "Method " << dex_file_->PrettyMethod(dex_method_idx_)
Andreas Gampee6215c02015-08-31 18:54:38 -0700767 << " not marked as constructor.";
768 is_constructor_ = true;
769 }
770 // If it's a constructor, check whether IsStatic() matches the name.
771 // This should have been rejected by the dex file verifier. Only do in debug build.
772 if (kIsDebugBuild) {
773 if (IsConstructor()) {
774 if (IsStatic() ^ static_constructor_by_name) {
775 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
776 << "constructor name doesn't match static flag";
777 return false;
778 }
jeffhaobdb76512011-09-07 11:43:16 -0700779 }
jeffhaobdb76512011-09-07 11:43:16 -0700780 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700781
782 // Methods may only have one of public/protected/private.
783 // This should have been rejected by the dex file verifier. Only do in debug build.
784 if (kIsDebugBuild) {
785 size_t access_mod_count =
786 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
787 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
788 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
789 if (access_mod_count > 1) {
790 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
791 return false;
792 }
793 }
794
795 // If there aren't any instructions, make sure that's expected, then exit successfully.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800796 if (!code_item_accessor_.HasCodeItem()) {
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700797 // Only native or abstract methods may not have code.
798 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
799 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
800 return false;
801 }
802
Andreas Gampee6215c02015-08-31 18:54:38 -0700803 // This should have been rejected by the dex file verifier. Only do in debug build.
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700804 // Note: the above will also be rejected in the dex file verifier, starting in dex version 37.
Andreas Gampee6215c02015-08-31 18:54:38 -0700805 if (kIsDebugBuild) {
Andreas Gampee6215c02015-08-31 18:54:38 -0700806 if ((method_access_flags_ & kAccAbstract) != 0) {
807 // Abstract methods are not allowed to have the following flags.
808 static constexpr uint32_t kForbidden =
809 kAccPrivate |
810 kAccStatic |
811 kAccFinal |
812 kAccNative |
813 kAccStrict |
814 kAccSynchronized;
815 if ((method_access_flags_ & kForbidden) != 0) {
816 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
817 << "method can't be abstract and private/static/final/native/strict/synchronized";
818 return false;
819 }
820 }
David Brazdil15fc7292016-09-02 14:13:18 +0100821 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700822 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000823 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700824 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000825 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700826 return false;
827 }
828 // In addition to the above, interface methods must not be protected.
829 static constexpr uint32_t kForbidden = kAccProtected;
830 if ((method_access_flags_ & kForbidden) != 0) {
831 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
832 return false;
833 }
834 }
835 // We also don't allow constructors to be abstract or native.
836 if (IsConstructor()) {
837 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
838 return false;
839 }
840 }
841 return true;
842 }
843
844 // This should have been rejected by the dex file verifier. Only do in debug build.
845 if (kIsDebugBuild) {
846 // When there's code, the method must not be native or abstract.
847 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
848 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
849 return false;
850 }
851
David Brazdil15fc7292016-09-02 14:13:18 +0100852 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700853 // Interfaces may always have static initializers for their fields. If we are running with
854 // default methods enabled we also allow other public, static, non-final methods to have code.
855 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700856 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +0000857 if (IsInstanceConstructor()) {
858 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
859 return false;
860 } else if (method_access_flags_ & kAccFinal) {
861 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
862 return false;
Alex Lightb55f1ac2016-04-12 15:50:55 -0700863 } else {
864 uint32_t access_flag_options = kAccPublic;
Mathieu Chartierf6e31472017-12-28 13:32:08 -0800865 if (dex_file_->SupportsDefaultMethods()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -0700866 access_flag_options |= kAccPrivate;
867 }
868 if (!(method_access_flags_ & access_flag_options)) {
869 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
870 << "interfaces may not have protected or package-private members";
871 return false;
872 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700873 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700874 }
875 }
876
877 // Instance constructors must not be synchronized.
878 if (IsInstanceConstructor()) {
879 static constexpr uint32_t kForbidden = kAccSynchronized;
880 if ((method_access_flags_ & kForbidden) != 0) {
881 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
882 return false;
883 }
884 }
885 }
886
Ian Rogersd81871c2011-10-03 13:57:23 -0700887 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800888 if (code_item_accessor_.InsSize() > code_item_accessor_.RegistersSize()) {
889 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins="
890 << code_item_accessor_.InsSize()
891 << " regs=" << code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -0700892 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700893 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700894
Ian Rogersd81871c2011-10-03 13:57:23 -0700895 // Allocate and initialize an array to hold instruction data.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800896 insn_flags_.reset(allocator_.AllocArray<InstructionFlags>(
897 code_item_accessor_.InsnsSizeInCodeUnits()));
Mathieu Chartierde40d472015-10-15 17:47:48 -0700898 DCHECK(insn_flags_ != nullptr);
899 std::uninitialized_fill_n(insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800900 code_item_accessor_.InsnsSizeInCodeUnits(),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700901 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700902 // Run through the instructions and see if the width checks out.
903 bool result = ComputeWidthsAndCountOps();
Andreas Gampebf1cb772017-05-15 15:39:00 -0700904 bool allow_runtime_only_instructions = !Runtime::Current()->IsAotCompiler() || verify_to_dump_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700905 // Flag instructions guarded by a "try" block and check exception handlers.
906 result = result && ScanTryCatchBlocks();
907 // Perform static instruction verification.
Andreas Gampebf1cb772017-05-15 15:39:00 -0700908 result = result && (allow_runtime_only_instructions
909 ? VerifyInstructions<true>()
910 : VerifyInstructions<false>());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700911 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000912 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -0800913
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000914 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700915}
916
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700917template <bool kVerifierDebug>
918void MethodVerifier<kVerifierDebug>::PrependToLastFailMessage(std::string prepend) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700919 size_t failure_num = failure_messages_.size();
920 DCHECK_NE(failure_num, 0U);
921 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
922 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700923 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700924 delete last_fail_message;
925}
926
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700927template <bool kVerifierDebug>
928void MethodVerifier<kVerifierDebug>::AppendToLastFailMessage(const std::string& append) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700929 size_t failure_num = failure_messages_.size();
930 DCHECK_NE(failure_num, 0U);
931 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
932 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800933}
934
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700935template <bool kVerifierDebug>
936bool MethodVerifier<kVerifierDebug>::ComputeWidthsAndCountOps() {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700937 // We can't assume the instruction is well formed, handle the case where calculating the size
938 // goes past the end of the code item.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800939 SafeDexInstructionIterator it(code_item_accessor_.begin(), code_item_accessor_.end());
940 for ( ; !it.IsErrorState() && it < code_item_accessor_.end(); ++it) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700941 // In case the instruction goes past the end of the code item, make sure to not process it.
942 SafeDexInstructionIterator next = it;
943 ++next;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800944 if (next.IsErrorState()) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -0700945 break;
946 }
947 Instruction::Code opcode = it->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700948 switch (opcode) {
949 case Instruction::APUT_OBJECT:
950 case Instruction::CHECK_CAST:
951 has_check_casts_ = true;
952 break;
Ian Rogersa9a82542013-10-04 11:17:26 -0700953 default:
954 break;
jeffhaobdb76512011-09-07 11:43:16 -0700955 }
Andreas Gampe51de69e2019-04-19 15:14:14 -0700956 GetModifiableInstructionFlags(it.DexPc()).SetIsOpcode();
jeffhaobdb76512011-09-07 11:43:16 -0700957 }
958
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800959 if (it != code_item_accessor_.end()) {
960 const size_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaod5347e02012-03-22 17:25:05 -0700961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
Mathieu Chartier2b2bef22017-10-26 17:10:19 -0700962 << it.DexPc() << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700963 return false;
964 }
965
jeffhaobdb76512011-09-07 11:43:16 -0700966 return true;
967}
968
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700969template <bool kVerifierDebug>
970bool MethodVerifier<kVerifierDebug>::ScanTryCatchBlocks() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800971 const uint32_t tries_size = code_item_accessor_.TriesSize();
jeffhaobdb76512011-09-07 11:43:16 -0700972 if (tries_size == 0) {
973 return true;
974 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800975 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800976 for (const dex::TryItem& try_item : code_item_accessor_.TryItems()) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800977 const uint32_t start = try_item.start_addr_;
978 const uint32_t end = start + try_item.insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700979 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700980 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
981 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700982 return false;
983 }
Mathieu Chartierde40d472015-10-15 17:47:48 -0700984 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700985 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
986 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700987 return false;
988 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800989 DexInstructionIterator end_it(code_item_accessor_.Insns(), end);
990 for (DexInstructionIterator it(code_item_accessor_.Insns(), start); it < end_it; ++it) {
Andreas Gampe51de69e2019-04-19 15:14:14 -0700991 GetModifiableInstructionFlags(it.DexPc()).SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700992 }
993 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800994 // Iterate over each of the handlers to verify target addresses.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800995 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
996 const uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700997 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700998 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700999 CatchHandlerIterator iterator(handlers_ptr);
1000 for (; iterator.HasNext(); iterator.Next()) {
Igor Murashkin2ffb7032017-11-08 13:35:21 -08001001 uint32_t dex_pc = iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -07001002 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001003 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1004 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001005 return false;
1006 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001007 if (!CheckNotMoveResult(code_item_accessor_.Insns(), dex_pc)) {
Stephen Kyle9bc61992014-09-22 13:53:15 +01001008 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1009 << "exception handler begins with move-result* (" << dex_pc << ")";
1010 return false;
1011 }
Andreas Gampe51de69e2019-04-19 15:14:14 -07001012 GetModifiableInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001013 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1014 // unresolved exception types will be ignored by exception delivery
Andreas Gampea5b09a62016-11-17 15:21:22 -08001015 if (iterator.GetHandlerTypeIndex().IsValid()) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001016 ObjPtr<mirror::Class> exception_type =
1017 linker->ResolveType(iterator.GetHandlerTypeIndex(), dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07001018 if (exception_type == nullptr) {
1019 DCHECK(self_->IsExceptionPending());
1020 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001021 }
1022 }
jeffhaobdb76512011-09-07 11:43:16 -07001023 }
Ian Rogers0571d352011-11-03 19:51:38 -07001024 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001025 }
jeffhaobdb76512011-09-07 11:43:16 -07001026 return true;
1027}
1028
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001029template <bool kVerifierDebug>
Andreas Gampebf1cb772017-05-15 15:39:00 -07001030template <bool kAllowRuntimeOnlyInstructions>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001031bool MethodVerifier<kVerifierDebug>::VerifyInstructions() {
Andreas Gampe93adcb52019-06-06 20:16:07 -07001032 // Flag the start of the method as a branch target.
Andreas Gampe51de69e2019-04-19 15:14:14 -07001033 GetModifiableInstructionFlags(0).SetBranchTarget();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001034 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001035 const uint32_t dex_pc = inst.DexPc();
1036 if (!VerifyInstruction<kAllowRuntimeOnlyInstructions>(&inst.Inst(), dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001037 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001038 return false;
1039 }
Andreas Gampe93adcb52019-06-06 20:16:07 -07001040 // Flag some interesting instructions.
1041 if (inst->IsReturn()) {
1042 GetModifiableInstructionFlags(dex_pc).SetReturn();
1043 } else if (inst->Opcode() == Instruction::CHECK_CAST) {
1044 // The dex-to-dex compiler wants type information to elide check-casts.
Andreas Gampe51de69e2019-04-19 15:14:14 -07001045 GetModifiableInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -07001046 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001047 }
1048 return true;
1049}
1050
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001051template <bool kVerifierDebug>
Andreas Gampebf1cb772017-05-15 15:39:00 -07001052template <bool kAllowRuntimeOnlyInstructions>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001053bool MethodVerifier<kVerifierDebug>::VerifyInstruction(const Instruction* inst,
1054 uint32_t code_offset) {
Andreas Gampee05cc662017-05-15 10:17:30 -07001055 if (Instruction::kHaveExperimentalInstructions && UNLIKELY(inst->IsExperimental())) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -07001056 // Experimental instructions don't yet have verifier support implementation.
1057 // While it is possible to use them by themselves, when we try to use stable instructions
1058 // with a virtual register that was created by an experimental instruction,
1059 // the data flow analysis will fail.
1060 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
1061 << "experimental instruction is not supported by verifier; skipping verification";
Andreas Gampe43884b22019-06-27 14:05:52 -07001062 flags_.have_pending_experimental_failure_ = true;
Igor Murashkin4d7b75f2015-07-21 17:03:36 -07001063 return false;
1064 }
1065
Ian Rogersd81871c2011-10-03 13:57:23 -07001066 bool result = true;
1067 switch (inst->GetVerifyTypeArgumentA()) {
1068 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -07001069 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001070 break;
1071 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001072 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001073 break;
1074 }
1075 switch (inst->GetVerifyTypeArgumentB()) {
1076 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -07001077 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001078 break;
1079 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001080 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001081 break;
1082 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001083 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001084 break;
1085 case Instruction::kVerifyRegBNewInstance:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001086 result = result && CheckNewInstance(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001087 break;
1088 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001089 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001090 break;
1091 case Instruction::kVerifyRegBType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001092 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001093 break;
1094 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001095 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001096 break;
Orion Hodson2e599942017-09-22 16:17:41 +01001097 case Instruction::kVerifyRegBCallSite:
1098 result = result && CheckCallSiteIndex(inst->VRegB());
1099 break;
1100 case Instruction::kVerifyRegBMethodHandle:
1101 result = result && CheckMethodHandleIndex(inst->VRegB());
1102 break;
1103 case Instruction::kVerifyRegBPrototype:
1104 result = result && CheckPrototypeIndex(inst->VRegB());
1105 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001106 }
1107 switch (inst->GetVerifyTypeArgumentC()) {
1108 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001109 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001110 break;
1111 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001112 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001113 break;
1114 case Instruction::kVerifyRegCNewArray:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001115 result = result && CheckNewArray(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001116 break;
1117 case Instruction::kVerifyRegCType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001118 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001119 break;
1120 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001121 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001122 break;
1123 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001124 switch (inst->GetVerifyTypeArgumentH()) {
1125 case Instruction::kVerifyRegHPrototype:
1126 result = result && CheckPrototypeIndex(inst->VRegH());
1127 break;
1128 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001129 switch (inst->GetVerifyExtraFlags()) {
1130 case Instruction::kVerifyArrayData:
1131 result = result && CheckArrayData(code_offset);
1132 break;
1133 case Instruction::kVerifyBranchTarget:
1134 result = result && CheckBranchTarget(code_offset);
1135 break;
1136 case Instruction::kVerifySwitchTargets:
1137 result = result && CheckSwitchTargets(code_offset);
1138 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001139 case Instruction::kVerifyVarArgNonZero:
1140 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001141 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001142 // Instructions that can actually return a negative value shouldn't have this flag.
1143 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1144 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1145 v_a > Instruction::kMaxVarArgRegs) {
1146 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001147 "non-range invoke";
1148 return false;
1149 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001150
Ian Rogers29a26482014-05-02 15:27:29 -07001151 uint32_t args[Instruction::kMaxVarArgRegs];
1152 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001153 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001154 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001155 }
Andreas Gampec3314312014-06-19 18:13:29 -07001156 case Instruction::kVerifyVarArgRangeNonZero:
1157 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001158 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001159 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1160 inst->VRegA() <= 0) {
1161 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1162 "range invoke";
1163 return false;
1164 }
Ian Rogers29a26482014-05-02 15:27:29 -07001165 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001166 break;
1167 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001168 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001169 result = false;
1170 break;
1171 }
Andreas Gampebf1cb772017-05-15 15:39:00 -07001172 if (!kAllowRuntimeOnlyInstructions && inst->GetVerifyIsRuntimeOnly()) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001173 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1174 result = false;
1175 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001176 return result;
1177}
1178
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001179template <bool kVerifierDebug>
1180inline bool MethodVerifier<kVerifierDebug>::CheckRegisterIndex(uint32_t idx) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001181 if (UNLIKELY(idx >= code_item_accessor_.RegistersSize())) {
jeffhaod5347e02012-03-22 17:25:05 -07001182 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001183 << code_item_accessor_.RegistersSize() << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001184 return false;
1185 }
1186 return true;
1187}
1188
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001189template <bool kVerifierDebug>
1190inline bool MethodVerifier<kVerifierDebug>::CheckWideRegisterIndex(uint32_t idx) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001191 if (UNLIKELY(idx + 1 >= code_item_accessor_.RegistersSize())) {
jeffhaod5347e02012-03-22 17:25:05 -07001192 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001193 << "+1 >= " << code_item_accessor_.RegistersSize() << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001194 return false;
1195 }
1196 return true;
1197}
1198
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001199template <bool kVerifierDebug>
1200inline bool MethodVerifier<kVerifierDebug>::CheckCallSiteIndex(uint32_t idx) {
Orion Hodson2e599942017-09-22 16:17:41 +01001201 uint32_t limit = dex_file_->NumCallSiteIds();
1202 if (UNLIKELY(idx >= limit)) {
1203 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad call site index " << idx << " (max "
1204 << limit << ")";
1205 return false;
1206 }
1207 return true;
1208}
1209
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001210template <bool kVerifierDebug>
1211inline bool MethodVerifier<kVerifierDebug>::CheckFieldIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001212 if (UNLIKELY(idx >= dex_file_->GetHeader().field_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001213 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1214 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001215 return false;
1216 }
1217 return true;
1218}
1219
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001220template <bool kVerifierDebug>
1221inline bool MethodVerifier<kVerifierDebug>::CheckMethodIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001222 if (UNLIKELY(idx >= dex_file_->GetHeader().method_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001223 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1224 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001225 return false;
1226 }
1227 return true;
1228}
1229
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001230template <bool kVerifierDebug>
1231inline bool MethodVerifier<kVerifierDebug>::CheckMethodHandleIndex(uint32_t idx) {
Orion Hodson2e599942017-09-22 16:17:41 +01001232 uint32_t limit = dex_file_->NumMethodHandles();
1233 if (UNLIKELY(idx >= limit)) {
1234 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method handle index " << idx << " (max "
1235 << limit << ")";
1236 return false;
1237 }
1238 return true;
1239}
1240
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001241template <bool kVerifierDebug>
1242inline bool MethodVerifier<kVerifierDebug>::CheckNewInstance(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001243 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001244 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001245 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001246 return false;
1247 }
1248 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001249 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Andreas Gampe29e81422017-05-15 16:29:32 -07001250 if (UNLIKELY(descriptor[0] != 'L')) {
jeffhaod5347e02012-03-22 17:25:05 -07001251 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001252 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001253 } else if (UNLIKELY(strcmp(descriptor, "Ljava/lang/Class;") == 0)) {
Aart Bikdb698f12016-07-25 17:52:22 -07001254 // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an
1255 // exception is thrown when this statement is executed (compiled code would not do that).
1256 Fail(VERIFY_ERROR_INSTANTIATION);
Ian Rogersd81871c2011-10-03 13:57:23 -07001257 }
1258 return true;
1259}
1260
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001261template <bool kVerifierDebug>
1262inline bool MethodVerifier<kVerifierDebug>::CheckPrototypeIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001263 if (UNLIKELY(idx >= dex_file_->GetHeader().proto_ids_size_)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001264 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad prototype index " << idx << " (max "
1265 << dex_file_->GetHeader().proto_ids_size_ << ")";
1266 return false;
1267 }
1268 return true;
1269}
1270
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001271template <bool kVerifierDebug>
1272inline bool MethodVerifier<kVerifierDebug>::CheckStringIndex(uint32_t idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001273 if (UNLIKELY(idx >= dex_file_->GetHeader().string_ids_size_)) {
jeffhaod5347e02012-03-22 17:25:05 -07001274 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1275 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001276 return false;
1277 }
1278 return true;
1279}
1280
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001281template <bool kVerifierDebug>
1282inline bool MethodVerifier<kVerifierDebug>::CheckTypeIndex(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001283 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001284 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001285 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001286 return false;
1287 }
1288 return true;
1289}
1290
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001291template <bool kVerifierDebug>
1292bool MethodVerifier<kVerifierDebug>::CheckNewArray(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001293 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001294 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001295 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001296 return false;
1297 }
1298 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001299 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001300 const char* cp = descriptor;
1301 while (*cp++ == '[') {
1302 bracket_count++;
1303 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001304 if (UNLIKELY(bracket_count == 0)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001305 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001306 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1307 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001308 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001309 } else if (UNLIKELY(bracket_count > 255)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001310 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001311 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1312 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001313 return false;
1314 }
1315 return true;
1316}
1317
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001318template <bool kVerifierDebug>
1319bool MethodVerifier<kVerifierDebug>::CheckArrayData(uint32_t cur_offset) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001320 const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
1321 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001322 const uint16_t* array_data;
1323 int32_t array_data_offset;
1324
1325 DCHECK_LT(cur_offset, insn_count);
1326 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001327 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001328 if (UNLIKELY(static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
1329 cur_offset + array_data_offset + 2 >= insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001330 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001331 << ", data offset " << array_data_offset
1332 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001333 return false;
1334 }
1335 /* offset to array data table is a relative branch-style offset */
1336 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001337 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001338 if (UNLIKELY(!IsAligned<4>(array_data))) {
jeffhaod5347e02012-03-22 17:25:05 -07001339 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1340 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001341 return false;
1342 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001343 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1344 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001345 if (UNLIKELY(!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001346 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1347 << ", data offset " << array_data_offset
1348 << " not correctly visited, probably bad padding.";
1349 return false;
1350 }
1351
Ian Rogersd81871c2011-10-03 13:57:23 -07001352 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001353 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001354 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1355 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001356 if (UNLIKELY(cur_offset + array_data_offset + table_size > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001357 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1358 << ", data offset " << array_data_offset << ", end "
1359 << cur_offset + array_data_offset + table_size
1360 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001361 return false;
1362 }
1363 return true;
1364}
1365
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001366template <bool kVerifierDebug>
1367bool MethodVerifier<kVerifierDebug>::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001368 int32_t offset;
1369 bool isConditional, selfOkay;
1370 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1371 return false;
1372 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001373 if (UNLIKELY(!selfOkay && offset == 0)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001374 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1375 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001376 return false;
1377 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001378 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1379 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Andreas Gampe29e81422017-05-15 16:29:32 -07001380 if (UNLIKELY(((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset))) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001381 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1382 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001383 return false;
1384 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001385 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001386 if (UNLIKELY(abs_offset < 0 ||
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001387 (uint32_t) abs_offset >= code_item_accessor_.InsnsSizeInCodeUnits() ||
Andreas Gampe29e81422017-05-15 16:29:32 -07001388 !GetInstructionFlags(abs_offset).IsOpcode())) {
jeffhaod5347e02012-03-22 17:25:05 -07001389 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001390 << reinterpret_cast<void*>(abs_offset) << ") at "
1391 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001392 return false;
1393 }
Andreas Gampe51de69e2019-04-19 15:14:14 -07001394 GetModifiableInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001395 return true;
1396}
1397
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001398template <bool kVerifierDebug>
1399bool MethodVerifier<kVerifierDebug>::GetBranchOffset(uint32_t cur_offset,
1400 int32_t* pOffset,
1401 bool* pConditional,
1402 bool* selfOkay) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001403 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001404 *pConditional = false;
1405 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001406 switch (*insns & 0xff) {
1407 case Instruction::GOTO:
1408 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001409 break;
1410 case Instruction::GOTO_32:
1411 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001412 *selfOkay = true;
1413 break;
1414 case Instruction::GOTO_16:
1415 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001416 break;
1417 case Instruction::IF_EQ:
1418 case Instruction::IF_NE:
1419 case Instruction::IF_LT:
1420 case Instruction::IF_GE:
1421 case Instruction::IF_GT:
1422 case Instruction::IF_LE:
1423 case Instruction::IF_EQZ:
1424 case Instruction::IF_NEZ:
1425 case Instruction::IF_LTZ:
1426 case Instruction::IF_GEZ:
1427 case Instruction::IF_GTZ:
1428 case Instruction::IF_LEZ:
1429 *pOffset = (int16_t) insns[1];
1430 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001431 break;
1432 default:
1433 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001434 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001435 return true;
1436}
1437
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001438template <bool kVerifierDebug>
1439bool MethodVerifier<kVerifierDebug>::CheckSwitchTargets(uint32_t cur_offset) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001440 const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001441 DCHECK_LT(cur_offset, insn_count);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001442 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001443 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001444 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001445 if (UNLIKELY(static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1446 cur_offset + switch_offset + 2 > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001447 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001448 << ", switch offset " << switch_offset
1449 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001450 return false;
1451 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001452 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001453 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001454 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001455 if (UNLIKELY(!IsAligned<4>(switch_insns))) {
jeffhaod5347e02012-03-22 17:25:05 -07001456 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1457 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001458 return false;
1459 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001460 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1461 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001462 if (UNLIKELY(!GetInstructionFlags(cur_offset + switch_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001463 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1464 << ", switch offset " << switch_offset
1465 << " not correctly visited, probably bad padding.";
1466 return false;
1467 }
1468
David Brazdil5469d342015-09-25 16:57:53 +01001469 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1470
Ian Rogersd81871c2011-10-03 13:57:23 -07001471 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001472 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001473 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001474 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001475 /* 0=sig, 1=count, 2/3=firstKey */
1476 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001477 expected_signature = Instruction::kPackedSwitchSignature;
1478 } else {
1479 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001480 targets_offset = 2 + 2 * switch_count;
1481 expected_signature = Instruction::kSparseSwitchSignature;
1482 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001483 uint32_t table_size = targets_offset + switch_count * 2;
Andreas Gampe29e81422017-05-15 16:29:32 -07001484 if (UNLIKELY(switch_insns[0] != expected_signature)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001485 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1486 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1487 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001488 return false;
1489 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001490 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001491 if (UNLIKELY(cur_offset + switch_offset + table_size > (uint32_t) insn_count)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001492 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1493 << ", switch offset " << switch_offset
1494 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001495 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001496 return false;
1497 }
David Brazdil5469d342015-09-25 16:57:53 +01001498
1499 constexpr int32_t keys_offset = 2;
1500 if (switch_count > 1) {
1501 if (is_packed_switch) {
1502 /* for a packed switch, verify that keys do not overflow int32 */
1503 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1504 int32_t max_first_key =
1505 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
Andreas Gampe29e81422017-05-15 16:29:32 -07001506 if (UNLIKELY(first_key > max_first_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001507 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1508 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001509 return false;
1510 }
David Brazdil5469d342015-09-25 16:57:53 +01001511 } else {
1512 /* for a sparse switch, verify the keys are in ascending order */
1513 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1514 for (uint32_t targ = 1; targ < switch_count; targ++) {
1515 int32_t key =
1516 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1517 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001518 if (UNLIKELY(key <= last_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001519 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1520 << ", this=" << key;
1521 return false;
1522 }
1523 last_key = key;
1524 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001525 }
1526 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001527 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001528 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001529 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1530 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001531 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001532 if (UNLIKELY(abs_offset < 0 ||
1533 abs_offset >= static_cast<int32_t>(insn_count) ||
1534 !GetInstructionFlags(abs_offset).IsOpcode())) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001535 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1536 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1537 << reinterpret_cast<void*>(cur_offset)
1538 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001539 return false;
1540 }
Andreas Gampe51de69e2019-04-19 15:14:14 -07001541 GetModifiableInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001542 }
1543 return true;
1544}
1545
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001546template <bool kVerifierDebug>
1547bool MethodVerifier<kVerifierDebug>::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001548 uint16_t registers_size = code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -07001549 for (uint32_t idx = 0; idx < vA; idx++) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001550 if (UNLIKELY(arg[idx] >= registers_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001551 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1552 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001553 return false;
1554 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001555 }
1556
1557 return true;
1558}
1559
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001560template <bool kVerifierDebug>
1561bool MethodVerifier<kVerifierDebug>::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001562 uint16_t registers_size = code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -07001563 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1564 // integer overflow when adding them here.
Andreas Gampe29e81422017-05-15 16:29:32 -07001565 if (UNLIKELY(vA + vC > registers_size)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001566 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1567 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001568 return false;
1569 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001570 return true;
1571}
1572
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001573template <bool kVerifierDebug>
1574bool MethodVerifier<kVerifierDebug>::VerifyCodeFlow() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001575 const uint16_t registers_size = code_item_accessor_.RegistersSize();
jeffhaobdb76512011-09-07 11:43:16 -07001576
Ian Rogersd81871c2011-10-03 13:57:23 -07001577 /* Create and initialize table holding register status */
Andreas Gampe93adcb52019-06-06 20:16:07 -07001578 RegisterTrackingMode base_mode = Runtime::Current()->IsAotCompiler()
1579 ? kTrackCompilerInterestPoints
1580 : kTrackRegsBranches;
1581 reg_table_.Init(fill_register_lines_ ? kTrackRegsAll : base_mode,
Brian Carlstrom93c33962013-07-26 10:37:43 -07001582 insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001583 code_item_accessor_.InsnsSizeInCodeUnits(),
Brian Carlstrom93c33962013-07-26 10:37:43 -07001584 registers_size,
Andreas Gamped09c0592019-04-19 15:44:05 -07001585 allocator_,
1586 GetRegTypeCache());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001587
Andreas Gamped09c0592019-04-19 15:44:05 -07001588 work_line_.reset(RegisterLine::Create(registers_size, allocator_, GetRegTypeCache()));
1589 saved_line_.reset(RegisterLine::Create(registers_size, allocator_, GetRegTypeCache()));
jeffhaobdb76512011-09-07 11:43:16 -07001590
Ian Rogersd81871c2011-10-03 13:57:23 -07001591 /* Initialize register types of method arguments. */
1592 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001593 DCHECK_NE(failures_.size(), 0U);
1594 std::string prepend("Bad signature in ");
David Sehr709b0702016-10-13 09:12:37 -07001595 prepend += dex_file_->PrettyMethod(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001596 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001597 return false;
1598 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001599 // We may have a runtime failure here, clear.
Andreas Gampe43884b22019-06-27 14:05:52 -07001600 flags_.have_pending_runtime_throw_failure_ = false;
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001601
Ian Rogersd81871c2011-10-03 13:57:23 -07001602 /* Perform code flow verification. */
1603 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001604 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001605 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001606 }
jeffhaobdb76512011-09-07 11:43:16 -07001607 return true;
1608}
1609
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001610template <bool kVerifierDebug>
1611std::ostream& MethodVerifier<kVerifierDebug>::DumpFailures(std::ostream& os) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001612 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001613 for (size_t i = 0; i < failures_.size(); ++i) {
1614 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001615 }
1616 return os;
1617}
1618
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001619template <bool kVerifierDebug>
1620void MethodVerifier<kVerifierDebug>::Dump(VariableIndentationOutputStream* vios) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001621 if (!code_item_accessor_.HasCodeItem()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001622 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001623 return;
jeffhaobdb76512011-09-07 11:43:16 -07001624 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001625 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001626 vios->Stream() << "Register Types:\n";
1627 ScopedIndentation indent1(vios);
1628 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001629 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001630 vios->Stream() << "Dumping instructions and register lines:\n";
1631 ScopedIndentation indent1(vios);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001632
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001633 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001634 const size_t dex_pc = inst.DexPc();
Andreas Gampe077d9db2018-01-19 18:54:14 -08001635
1636 // Might be asked to dump before the table is initialized.
1637 if (reg_table_.IsInitialized()) {
1638 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1639 if (reg_line != nullptr) {
1640 vios->Stream() << reg_line->Dump(this) << "\n";
1641 }
jeffhaobdb76512011-09-07 11:43:16 -07001642 }
Andreas Gampe077d9db2018-01-19 18:54:14 -08001643
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001644 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001645 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001646 const bool kDumpHexOfInstruction = false;
1647 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001648 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001649 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001650 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001651 }
jeffhaobdb76512011-09-07 11:43:16 -07001652}
1653
Ian Rogersd81871c2011-10-03 13:57:23 -07001654static bool IsPrimitiveDescriptor(char descriptor) {
1655 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001656 case 'I':
1657 case 'C':
1658 case 'S':
1659 case 'B':
1660 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001661 case 'F':
1662 case 'D':
1663 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001664 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001665 default:
1666 return false;
1667 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001668}
1669
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001670template <bool kVerifierDebug>
1671bool MethodVerifier<kVerifierDebug>::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001672 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001673
1674 // Should have been verified earlier.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001675 DCHECK_GE(code_item_accessor_.RegistersSize(), code_item_accessor_.InsSize());
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001676
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001677 uint32_t arg_start = code_item_accessor_.RegistersSize() - code_item_accessor_.InsSize();
1678 size_t expected_args = code_item_accessor_.InsSize(); /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001679
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001680 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001681 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001682 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001683 if (expected_args == 0) {
1684 // Expect at least a receiver.
1685 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1686 return false;
1687 }
1688
Ian Rogersd81871c2011-10-03 13:57:23 -07001689 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1690 // argument as uninitialized. This restricts field access until the superclass constructor is
1691 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001692 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001693 if (IsConstructor()) {
1694 if (declaring_class.IsJavaLangObject()) {
1695 // "this" is implicitly initialized.
1696 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001697 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001698 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001699 reg_line->SetRegisterType<LockOp::kClear>(
1700 this,
1701 arg_start + cur_arg,
1702 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001703 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001704 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001705 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001706 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001707 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001708 }
1709
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001710 const dex::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001711 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001712 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001713
1714 for (; iterator.HasNext(); iterator.Next()) {
1715 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001716 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001717 LOG(FATAL) << "Null descriptor";
1718 }
1719 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001720 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1721 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001722 return false;
1723 }
1724 switch (descriptor[0]) {
1725 case 'L':
1726 case '[':
1727 // We assume that reference arguments are initialized. The only way it could be otherwise
1728 // (assuming the caller was verified) is if the current method is <init>, but in that case
1729 // it's effectively considered initialized the instant we reach here (in the sense that we
1730 // can return without doing anything or call virtual methods).
1731 {
Andreas Gampe98be1a92017-08-28 08:25:45 -07001732 // Note: don't check access. No error would be thrown for declaring or passing an
1733 // inaccessible class. Only actual accesses to fields or methods will.
1734 const RegType& reg_type = ResolveClass<CheckAccess::kNo>(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001735 if (!reg_type.IsNonZeroReferenceTypes()) {
1736 DCHECK(HasFailures());
1737 return false;
1738 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001739 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001740 }
1741 break;
1742 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001743 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001744 break;
1745 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001746 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001747 break;
1748 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001749 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001750 break;
1751 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001752 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001753 break;
1754 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001755 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001756 break;
1757 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001758 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001759 break;
1760 case 'J':
1761 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001762 if (cur_arg + 1 >= expected_args) {
1763 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1764 << " args, found more (" << descriptor << ")";
1765 return false;
1766 }
1767
Ian Rogers7b078e82014-09-10 14:44:24 -07001768 const RegType* lo_half;
1769 const RegType* hi_half;
1770 if (descriptor[0] == 'J') {
1771 lo_half = &reg_types_.LongLo();
1772 hi_half = &reg_types_.LongHi();
1773 } else {
1774 lo_half = &reg_types_.DoubleLo();
1775 hi_half = &reg_types_.DoubleHi();
1776 }
1777 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001778 cur_arg++;
1779 break;
1780 }
1781 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001782 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1783 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001784 return false;
1785 }
1786 cur_arg++;
1787 }
1788 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001789 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1790 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001791 return false;
1792 }
1793 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1794 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1795 // format. Only major difference from the method argument format is that 'V' is supported.
1796 bool result;
1797 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1798 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001799 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001800 size_t i = 0;
1801 do {
1802 i++;
1803 } while (descriptor[i] == '['); // process leading [
1804 if (descriptor[i] == 'L') { // object array
1805 do {
1806 i++; // find closing ;
1807 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1808 result = descriptor[i] == ';';
1809 } else { // primitive array
1810 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1811 }
1812 } else if (descriptor[0] == 'L') {
1813 // could be more thorough here, but shouldn't be required
1814 size_t i = 0;
1815 do {
1816 i++;
1817 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1818 result = descriptor[i] == ';';
1819 } else {
1820 result = false;
1821 }
1822 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001823 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1824 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001825 }
1826 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001827}
1828
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001829template <bool kVerifierDebug>
1830bool MethodVerifier<kVerifierDebug>::CodeFlowVerifyMethod() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001831 const uint16_t* insns = code_item_accessor_.Insns();
1832 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaoba5ebb92011-08-25 17:24:37 -07001833
jeffhaobdb76512011-09-07 11:43:16 -07001834 /* Begin by marking the first instruction as "changed". */
Andreas Gampe51de69e2019-04-19 15:14:14 -07001835 GetModifiableInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001836 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001837
jeffhaobdb76512011-09-07 11:43:16 -07001838 /* Continue until no instructions are marked "changed". */
1839 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001840 if (allow_thread_suspension_) {
1841 self_->AllowThreadSuspension();
1842 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001843 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1844 uint32_t insn_idx = start_guess;
1845 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001846 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001847 break;
1848 }
jeffhaobdb76512011-09-07 11:43:16 -07001849 if (insn_idx == insns_size) {
1850 if (start_guess != 0) {
1851 /* try again, starting from the top */
1852 start_guess = 0;
1853 continue;
1854 } else {
1855 /* all flags are clear */
1856 break;
1857 }
1858 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001859 // We carry the working set of registers from instruction to instruction. If this address can
1860 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1861 // "changed" flags, we need to load the set of registers from the table.
1862 // Because we always prefer to continue on to the next instruction, we should never have a
1863 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1864 // target.
1865 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001866 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001867 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001868 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001869 /*
1870 * Sanity check: retrieve the stored register line (assuming
1871 * a full table) and make sure it actually matches.
1872 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001873 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001874 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001875 if (work_line_->CompareLine(register_line) != 0) {
Andreas Gampe4a57d5f2019-04-03 12:31:18 -07001876 Dump(LOG_STREAM(FATAL_WITHOUT_ABORT));
1877 LOG(FATAL_WITHOUT_ABORT) << info_messages_.str();
David Sehr709b0702016-10-13 09:12:37 -07001878 LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001879 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001880 << " work_line=" << work_line_->Dump(this) << "\n"
1881 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001882 }
jeffhaobdb76512011-09-07 11:43:16 -07001883 }
jeffhaobdb76512011-09-07 11:43:16 -07001884 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001885 if (!CodeFlowVerifyInstruction(&start_guess)) {
David Sehr709b0702016-10-13 09:12:37 -07001886 std::string prepend(dex_file_->PrettyMethod(dex_method_idx_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001887 prepend += " failed to verify: ";
1888 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001889 return false;
1890 }
jeffhaobdb76512011-09-07 11:43:16 -07001891 /* Clear "changed" and mark as visited. */
Andreas Gampe51de69e2019-04-19 15:14:14 -07001892 GetModifiableInstructionFlags(insn_idx).SetVisited();
1893 GetModifiableInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001894 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001895
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001896 if (kVerifierDebug) {
jeffhaobdb76512011-09-07 11:43:16 -07001897 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001898 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001899 * (besides the wasted space), but it indicates a flaw somewhere
1900 * down the line, possibly in the verifier.
1901 *
1902 * If we've substituted "always throw" instructions into the stream,
1903 * we are almost certainly going to have some dead code.
1904 */
1905 int dead_start = -1;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001906
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001907 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001908 const uint32_t insn_idx = inst.DexPc();
jeffhaobdb76512011-09-07 11:43:16 -07001909 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001910 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001911 * may or may not be preceded by a padding NOP (for alignment).
1912 */
1913 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1914 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1915 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001916 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001917 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1918 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1919 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Andreas Gampe51de69e2019-04-19 15:14:14 -07001920 GetModifiableInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001921 }
1922
Mathieu Chartierde40d472015-10-15 17:47:48 -07001923 if (!GetInstructionFlags(insn_idx).IsVisited()) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001924 if (dead_start < 0) {
jeffhaobdb76512011-09-07 11:43:16 -07001925 dead_start = insn_idx;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001926 }
jeffhaobdb76512011-09-07 11:43:16 -07001927 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001928 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1929 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001930 dead_start = -1;
1931 }
1932 }
1933 if (dead_start >= 0) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001934 LogVerifyInfo()
1935 << "dead code " << reinterpret_cast<void*>(dead_start)
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001936 << "-" << reinterpret_cast<void*>(code_item_accessor_.InsnsSizeInCodeUnits() - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001937 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001938 // To dump the state of the verify after a method, do something like:
David Sehr709b0702016-10-13 09:12:37 -07001939 // if (dex_file_->PrettyMethod(dex_method_idx_) ==
Ian Rogersc9e463c2013-06-05 16:52:26 -07001940 // "boolean java.lang.String.equals(java.lang.Object)") {
1941 // LOG(INFO) << info_messages_.str();
1942 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001943 }
jeffhaobdb76512011-09-07 11:43:16 -07001944 return true;
1945}
1946
Andreas Gampe68df3202015-06-22 11:35:46 -07001947// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1948// is no such field.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001949static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, dex::TypeIndex type_idx) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001950 const dex::ClassDef* class_def = dex_file.FindClassDef(type_idx);
Andreas Gampe68df3202015-06-22 11:35:46 -07001951 DCHECK(class_def != nullptr);
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -07001952 ClassAccessor accessor(dex_file, *class_def);
1953 for (const ClassAccessor::Field& field : accessor.GetInstanceFields()) {
1954 if (field.IsFinal()) {
1955 return field.GetIndex();
Andreas Gampe68df3202015-06-22 11:35:46 -07001956 }
Andreas Gampe68df3202015-06-22 11:35:46 -07001957 }
Andreas Gampee2abbc62017-09-15 11:59:26 -07001958 return dex::kDexNoIndex;
Andreas Gampe68df3202015-06-22 11:35:46 -07001959}
1960
Andreas Gampea727e372015-08-25 09:22:37 -07001961// Setup a register line for the given return instruction.
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001962template <bool kVerifierDebug>
1963static void AdjustReturnLine(MethodVerifier<kVerifierDebug>* verifier,
Andreas Gampea727e372015-08-25 09:22:37 -07001964 const Instruction* ret_inst,
1965 RegisterLine* line) {
1966 Instruction::Code opcode = ret_inst->Opcode();
1967
1968 switch (opcode) {
1969 case Instruction::RETURN_VOID:
1970 case Instruction::RETURN_VOID_NO_BARRIER:
Andreas Gampe51de69e2019-04-19 15:14:14 -07001971 if (verifier->IsInstanceConstructor()) {
1972 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
1973 line->CheckConstructorReturn(verifier);
1974 }
1975 line->MarkAllRegistersAsConflicts(verifier);
Andreas Gampea727e372015-08-25 09:22:37 -07001976 break;
1977
1978 case Instruction::RETURN:
1979 case Instruction::RETURN_OBJECT:
1980 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1981 break;
1982
1983 case Instruction::RETURN_WIDE:
1984 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1985 break;
1986
1987 default:
1988 LOG(FATAL) << "Unknown return opcode " << opcode;
1989 UNREACHABLE();
1990 }
1991}
1992
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001993template <bool kVerifierDebug>
1994bool MethodVerifier<kVerifierDebug>::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001995 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1996 // We want the state _before_ the instruction, for the case where the dex pc we're
1997 // interested in is itself a monitor-enter instruction (which is a likely place
1998 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001999 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002000 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Andreas Gampeaaf0d382017-11-27 14:10:21 -08002001
2002 std::map<uint32_t, DexLockInfo> depth_to_lock_info;
2003 auto collector = [&](uint32_t dex_reg, uint32_t depth) {
2004 auto insert_pair = depth_to_lock_info.emplace(depth, DexLockInfo(depth));
2005 auto it = insert_pair.first;
2006 auto set_insert_pair = it->second.dex_registers.insert(dex_reg);
2007 DCHECK(set_insert_pair.second);
2008 };
2009 work_line_->IterateRegToLockDepths(collector);
2010 for (auto& pair : depth_to_lock_info) {
2011 monitor_enter_dex_pcs_->push_back(pair.second);
2012 // Map depth to dex PC.
2013 (*monitor_enter_dex_pcs_)[monitor_enter_dex_pcs_->size() - 1].dex_pc =
2014 work_line_->GetMonitorEnterDexPc(pair.second.dex_pc);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07002015 }
2016 }
2017
jeffhaobdb76512011-09-07 11:43:16 -07002018 /*
2019 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07002020 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07002021 * control to another statement:
2022 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002023 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07002024 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07002025 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07002026 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07002027 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07002028 * throw an exception that is handled by an encompassing "try"
2029 * block.
2030 *
2031 * We can also return, in which case there is no successor instruction
2032 * from this point.
2033 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08002034 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07002035 */
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002036 const uint16_t* insns = code_item_accessor_.Insns() + work_insn_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -07002037 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07002038 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07002039
jeffhaobdb76512011-09-07 11:43:16 -07002040 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07002041 bool just_set_result = false;
Andreas Gampe99db7bb2019-04-19 23:05:47 -07002042 if (kVerifierDebug) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002043 // Generate processing back trace to debug verifier
Andreas Gampe2ad6cce2019-04-11 16:17:39 -07002044 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
2045 << work_line_->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07002046 }
jeffhaobdb76512011-09-07 11:43:16 -07002047
2048 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002049 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07002050 * can throw an exception, we will copy/merge this into the "catch"
2051 * address rather than work_line, because we don't want the result
2052 * from the "successful" code path (e.g. a check-cast that "improves"
2053 * a type) to be visible to the exception handler.
2054 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07002055 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002056 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002057 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002058 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07002059 }
Andreas Gampe43884b22019-06-27 14:05:52 -07002060 // Per-instruction flag, should not be set here.
2061 DCHECK(!flags_.have_pending_runtime_throw_failure_);
Andreas Gampe6087bc22019-06-03 15:52:08 -07002062 bool exc_handler_unreachable = false;
jeffhaobdb76512011-09-07 11:43:16 -07002063
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07002064
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002065 // We need to ensure the work line is consistent while performing validation. When we spot a
2066 // peephole pattern we compute a new line for either the fallthrough instruction or the
2067 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08002068 RegisterLineArenaUniquePtr branch_line;
2069 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002070
Sebastien Hertz5243e912013-05-21 10:55:07 +02002071 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07002072 case Instruction::NOP:
2073 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002074 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07002075 * a signature that looks like a NOP; if we see one of these in
2076 * the course of executing code then we have a problem.
2077 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002078 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07002079 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002080 }
2081 break;
2082
2083 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002084 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002085 break;
jeffhaobdb76512011-09-07 11:43:16 -07002086 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002087 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002088 break;
jeffhaobdb76512011-09-07 11:43:16 -07002089 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002090 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002091 break;
2092 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002093 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002094 break;
jeffhaobdb76512011-09-07 11:43:16 -07002095 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002096 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002097 break;
jeffhaobdb76512011-09-07 11:43:16 -07002098 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002099 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07002100 break;
2101 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002102 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002103 break;
jeffhaobdb76512011-09-07 11:43:16 -07002104 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002105 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002106 break;
jeffhaobdb76512011-09-07 11:43:16 -07002107 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002108 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002109 break;
2110
2111 /*
2112 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002113 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002114 * might want to hold the result in an actual CPU register, so the
2115 * Dalvik spec requires that these only appear immediately after an
2116 * invoke or filled-new-array.
2117 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002118 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002119 * redundant with the reset done below, but it can make the debug info
2120 * easier to read in some cases.)
2121 */
2122 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002123 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002124 break;
2125 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002126 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002127 break;
2128 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002129 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002130 break;
2131
Andreas Gampe6087bc22019-06-03 15:52:08 -07002132 case Instruction::MOVE_EXCEPTION:
2133 if (!HandleMoveException(inst)) {
2134 exc_handler_unreachable = true;
Sebastien Hertz270a0e12015-01-16 19:49:09 +01002135 }
jeffhaobdb76512011-09-07 11:43:16 -07002136 break;
Andreas Gampe6087bc22019-06-03 15:52:08 -07002137
jeffhaobdb76512011-09-07 11:43:16 -07002138 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002139 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002140 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002141 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002142 }
jeffhaobdb76512011-09-07 11:43:16 -07002143 }
2144 break;
2145 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002146 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002147 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002148 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002149 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002150 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2151 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002152 } else {
2153 // Compilers may generate synthetic functions that write byte values into boolean fields.
2154 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002155 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002156 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002157 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2158 ((return_type.IsBoolean() || return_type.IsByte() ||
2159 return_type.IsShort() || return_type.IsChar()) &&
2160 src_type.IsInteger()));
2161 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002162 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002163 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002164 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002165 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002166 }
jeffhaobdb76512011-09-07 11:43:16 -07002167 }
2168 }
2169 break;
2170 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002171 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002172 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002173 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002174 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002175 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002176 } else {
2177 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002178 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002179 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002180 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002181 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002182 }
jeffhaobdb76512011-09-07 11:43:16 -07002183 }
2184 }
2185 break;
2186 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002187 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002188 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002189 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002190 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002191 } else {
2192 /* return_type is the *expected* return type, not register value */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002193 DCHECK(!return_type.IsZeroOrNull());
Ian Rogersd81871c2011-10-03 13:57:23 -07002194 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002195 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002196 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002197 // Disallow returning undefined, conflict & uninitialized values and verify that the
2198 // reference in vAA is an instance of the "return_type."
2199 if (reg_type.IsUndefined()) {
2200 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2201 } else if (reg_type.IsConflict()) {
2202 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2203 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002204 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002205 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002206 } else if (!reg_type.IsReferenceTypes()) {
2207 // We really do expect a reference here.
2208 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2209 << reg_type;
David Brazdilca3c8c32016-09-06 14:04:48 +01002210 } else if (!return_type.IsAssignableFrom(reg_type, this)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002211 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2212 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2213 << "' or '" << reg_type << "'";
2214 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002215 bool soft_error = false;
2216 // Check whether arrays are involved. They will show a valid class status, even
2217 // if their components are erroneous.
2218 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
David Brazdilca3c8c32016-09-06 14:04:48 +01002219 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error);
Andreas Gampe16f149c2015-03-23 10:10:20 -07002220 if (soft_error) {
2221 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2222 << reg_type << " vs " << return_type;
2223 }
2224 }
2225
2226 if (!soft_error) {
2227 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2228 << "', but expected from declaration '" << return_type << "'";
2229 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002230 }
jeffhaobdb76512011-09-07 11:43:16 -07002231 }
2232 }
2233 }
2234 break;
2235
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002236 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002237 case Instruction::CONST_4: {
2238 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002239 work_line_->SetRegisterType<LockOp::kClear>(
2240 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002241 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002242 }
2243 case Instruction::CONST_16: {
2244 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002245 work_line_->SetRegisterType<LockOp::kClear>(
2246 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002247 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002248 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002249 case Instruction::CONST: {
2250 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002251 work_line_->SetRegisterType<LockOp::kClear>(
2252 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002253 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002254 }
2255 case Instruction::CONST_HIGH16: {
2256 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002257 work_line_->SetRegisterType<LockOp::kClear>(
2258 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002259 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002260 }
jeffhaobdb76512011-09-07 11:43:16 -07002261 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002262 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002263 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002264 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2265 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002266 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002267 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002268 }
2269 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002270 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002271 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2272 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002273 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002274 break;
2275 }
2276 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002277 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002278 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2279 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002280 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002281 break;
2282 }
2283 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002284 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002285 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2286 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002287 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002288 break;
2289 }
jeffhaobdb76512011-09-07 11:43:16 -07002290 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002291 work_line_->SetRegisterType<LockOp::kClear>(
2292 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002293 break;
jeffhaobdb76512011-09-07 11:43:16 -07002294 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002295 work_line_->SetRegisterType<LockOp::kClear>(
2296 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002297 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002298 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002299 // Get type from instruction if unresolved then we need an access check
2300 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Andreas Gampe98be1a92017-08-28 08:25:45 -07002301 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002302 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002303 work_line_->SetRegisterType<LockOp::kClear>(
2304 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2305 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002306 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002307 }
Orion Hodson2e599942017-09-22 16:17:41 +01002308 case Instruction::CONST_METHOD_HANDLE:
2309 work_line_->SetRegisterType<LockOp::kClear>(
2310 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodHandle());
Orion Hodson2e599942017-09-22 16:17:41 +01002311 break;
2312 case Instruction::CONST_METHOD_TYPE:
2313 work_line_->SetRegisterType<LockOp::kClear>(
2314 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodType());
Orion Hodson2e599942017-09-22 16:17:41 +01002315 break;
jeffhaobdb76512011-09-07 11:43:16 -07002316 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002317 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002318 // Check whether the previous instruction is a move-object with vAA as a source, creating
2319 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002320 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002321 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002322 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002323 prev_idx--;
2324 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002325 const Instruction& prev_inst = code_item_accessor_.InstructionAt(prev_idx);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002326 switch (prev_inst.Opcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002327 case Instruction::MOVE_OBJECT:
2328 case Instruction::MOVE_OBJECT_16:
2329 case Instruction::MOVE_OBJECT_FROM16:
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002330 if (prev_inst.VRegB() == inst->VRegA_11x()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002331 // Redo the copy. This won't change the register types, but update the lock status
2332 // for the aliased register.
2333 work_line_->CopyRegister1(this,
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002334 prev_inst.VRegA(),
2335 prev_inst.VRegB(),
Andreas Gampec1474102015-08-18 08:57:44 -07002336 kTypeCategoryRef);
2337 }
2338 break;
2339
Alexey Grebenkince750492018-05-31 23:42:20 +03002340 // Catch a case of register aliasing when two registers are linked to the same
2341 // java.lang.Class object via two consequent const-class instructions immediately
2342 // preceding monitor-enter called on one of those registers.
2343 case Instruction::CONST_CLASS: {
2344 // Get the second previous instruction.
2345 if (prev_idx == 0 || GetInstructionFlags(prev_idx).IsBranchTarget()) {
2346 break;
2347 }
2348 prev_idx--;
2349 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
2350 prev_idx--;
2351 }
2352 const Instruction& prev2_inst = code_item_accessor_.InstructionAt(prev_idx);
2353
2354 // Match the pattern "const-class; const-class; monitor-enter;"
2355 if (prev2_inst.Opcode() != Instruction::CONST_CLASS) {
2356 break;
2357 }
2358
2359 // Ensure both const-classes are called for the same type_idx.
2360 if (prev_inst.VRegB_21c() != prev2_inst.VRegB_21c()) {
2361 break;
2362 }
2363
2364 // Update the lock status for the aliased register.
2365 if (prev_inst.VRegA() == inst->VRegA_11x()) {
2366 work_line_->CopyRegister1(this,
2367 prev2_inst.VRegA(),
2368 inst->VRegA_11x(),
2369 kTypeCategoryRef);
2370 } else if (prev2_inst.VRegA() == inst->VRegA_11x()) {
2371 work_line_->CopyRegister1(this,
2372 prev_inst.VRegA(),
2373 inst->VRegA_11x(),
2374 kTypeCategoryRef);
2375 }
2376 break;
2377 }
2378
Andreas Gampec1474102015-08-18 08:57:44 -07002379 default: // Other instruction types ignored.
2380 break;
2381 }
2382 }
jeffhaobdb76512011-09-07 11:43:16 -07002383 break;
2384 case Instruction::MONITOR_EXIT:
2385 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002386 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002387 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002388 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002389 * to the need to handle asynchronous exceptions, a now-deprecated
2390 * feature that Dalvik doesn't support.)
2391 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002392 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002393 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002394 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002395 * structured locking checks are working, the former would have
2396 * failed on the -enter instruction, and the latter is impossible.
2397 *
2398 * This is fortunate, because issue 3221411 prevents us from
2399 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002400 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002401 * some catch blocks (which will show up as "dead" code when
2402 * we skip them here); if we can't, then the code path could be
2403 * "live" so we still need to check it.
2404 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002405 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002406 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002407 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002408 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002409 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002410 /*
2411 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2412 * could be a "upcast" -- not expected, so we don't try to address it.)
2413 *
2414 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002415 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002416 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002417 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002418 const dex::TypeIndex type_idx((is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002419 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002420 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002421 // If this is a primitive type, fail HARD.
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002422 ObjPtr<mirror::Class> klass = Runtime::Current()->GetClassLinker()->LookupResolvedType(
2423 type_idx, dex_cache_.Get(), class_loader_.Get());
Andreas Gampe00633eb2014-07-17 16:13:35 -07002424 if (klass != nullptr && klass->IsPrimitive()) {
2425 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2426 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2427 << GetDeclaringClass();
2428 break;
2429 }
2430
Ian Rogersad0b3a32012-04-16 14:50:24 -07002431 DCHECK_NE(failures_.size(), 0U);
2432 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002433 work_line_->SetRegisterType<LockOp::kClear>(this,
2434 inst->VRegA_22c(),
2435 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002436 }
2437 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002438 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002439 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002440 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002441 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002442 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002443 if (is_checkcast) {
2444 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2445 } else {
2446 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2447 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002448 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002449 if (is_checkcast) {
2450 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2451 } else {
2452 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2453 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002454 } else if (orig_type.IsUninitializedTypes()) {
2455 if (is_checkcast) {
2456 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2457 << orig_type_reg;
2458 } else {
2459 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2460 << orig_type_reg;
2461 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002462 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002463 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002464 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002465 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002466 work_line_->SetRegisterType<LockOp::kClear>(this,
2467 inst->VRegA_22c(),
2468 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002469 }
jeffhaobdb76512011-09-07 11:43:16 -07002470 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002471 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002472 }
2473 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002474 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002475 if (res_type.IsReferenceTypes()) {
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002476 if (!res_type.IsArrayTypes() && !res_type.IsZeroOrNull()) {
2477 // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002478 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002479 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002480 work_line_->SetRegisterType<LockOp::kClear>(this,
2481 inst->VRegA_12x(),
2482 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002483 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002484 } else {
2485 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002486 }
2487 break;
2488 }
2489 case Instruction::NEW_INSTANCE: {
Andreas Gampe98be1a92017-08-28 08:25:45 -07002490 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002491 if (res_type.IsConflict()) {
2492 DCHECK_NE(failures_.size(), 0U);
2493 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002494 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002495 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2496 // can't create an instance of an interface or abstract class */
2497 if (!res_type.IsInstantiableTypes()) {
2498 Fail(VERIFY_ERROR_INSTANTIATION)
2499 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002500 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002501 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002502 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002503 // Any registers holding previous allocations from this address that have not yet been
2504 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002505 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002506 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002507 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002508 break;
2509 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002510 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002511 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002512 break;
2513 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002514 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002515 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002516 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002517 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002518 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002519 just_set_result = true; // Filled new array range sets result register
2520 break;
jeffhaobdb76512011-09-07 11:43:16 -07002521 case Instruction::CMPL_FLOAT:
2522 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002523 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002524 break;
2525 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002526 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002527 break;
2528 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002529 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002530 break;
2531 case Instruction::CMPL_DOUBLE:
2532 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002533 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002534 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002535 break;
2536 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002537 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002538 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002539 break;
2540 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002541 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002542 break;
2543 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002544 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002545 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002546 break;
2547 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002548 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002549 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002550 break;
2551 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002552 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002553 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002554 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002555 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
David Brazdilca3c8c32016-09-06 14:04:48 +01002556 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002557 if (res_type.IsUninitializedTypes()) {
2558 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002559 } else if (!res_type.IsReferenceTypes()) {
2560 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002561 } else {
2562 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2563 << "thrown class " << res_type << " not instanceof Throwable";
2564 }
jeffhaobdb76512011-09-07 11:43:16 -07002565 }
2566 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002567 }
jeffhaobdb76512011-09-07 11:43:16 -07002568 case Instruction::GOTO:
2569 case Instruction::GOTO_16:
2570 case Instruction::GOTO_32:
2571 /* no effect on or use of registers */
2572 break;
2573
2574 case Instruction::PACKED_SWITCH:
2575 case Instruction::SPARSE_SWITCH:
2576 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002577 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002578 break;
2579
Ian Rogersd81871c2011-10-03 13:57:23 -07002580 case Instruction::FILL_ARRAY_DATA: {
2581 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002582 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002583 /* array_type can be null if the reg type is Zero */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002584 if (!array_type.IsZeroOrNull()) {
jeffhao457cc512012-02-02 16:55:13 -08002585 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002586 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2587 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002588 } else if (array_type.IsUnresolvedTypes()) {
2589 // If it's an unresolved array type, it must be non-primitive.
2590 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2591 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002592 } else {
Andreas Gampe51de69e2019-04-19 15:14:14 -07002593 const RegType& component_type = reg_types_.GetComponentType(array_type,
2594 class_loader_.Get());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002595 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002596 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002597 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2598 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002599 } else {
jeffhao457cc512012-02-02 16:55:13 -08002600 // Now verify if the element width in the table matches the element width declared in
2601 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002602 const uint16_t* array_data =
2603 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002604 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002605 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002606 } else {
2607 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2608 // Since we don't compress the data in Dex, expect to see equal width of data stored
2609 // in the table and expected from the array class.
2610 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002611 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2612 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002613 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002614 }
2615 }
jeffhaobdb76512011-09-07 11:43:16 -07002616 }
2617 }
2618 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002619 }
jeffhaobdb76512011-09-07 11:43:16 -07002620 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002621 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002622 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2623 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002624 bool mismatch = false;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002625 if (reg_type1.IsZeroOrNull()) { // zero then integral or reference expected
Ian Rogersd81871c2011-10-03 13:57:23 -07002626 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2627 } else if (reg_type1.IsReferenceTypes()) { // both references?
2628 mismatch = !reg_type2.IsReferenceTypes();
2629 } else { // both integral?
2630 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2631 }
2632 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002633 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2634 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002635 }
2636 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002637 }
jeffhaobdb76512011-09-07 11:43:16 -07002638 case Instruction::IF_LT:
2639 case Instruction::IF_GE:
2640 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002641 case Instruction::IF_LE: {
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 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002645 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2646 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002647 }
2648 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002649 }
jeffhaobdb76512011-09-07 11:43:16 -07002650 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002651 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002652 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002653 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002654 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2655 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002656 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002657
2658 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002659 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002660 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002661 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002662 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002663 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002664 }
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -07002665 if (FailOrAbort(GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002666 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2667 work_insn_idx_)) {
2668 break;
2669 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002670 } else {
2671 break;
2672 }
2673
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002674 const Instruction& instance_of_inst = code_item_accessor_.InstructionAt(instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002675
2676 /* Check for peep-hole pattern of:
2677 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002678 * instance-of vX, vY, T;
2679 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002680 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002681 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002682 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002683 * and sharpen the type of vY to be type T.
2684 * Note, this pattern can't be if:
2685 * - if there are other branches to this branch,
2686 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002687 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002688 if (!CurrentInsnFlags()->IsBranchTarget() &&
Vladimir Markod7559b72017-09-28 13:50:37 +01002689 (Instruction::INSTANCE_OF == instance_of_inst.Opcode()) &&
2690 (inst->VRegA_21t() == instance_of_inst.VRegA_22c()) &&
2691 (instance_of_inst.VRegA_22c() != instance_of_inst.VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002692 // Check the type of the instance-of is different than that of registers type, as if they
2693 // are the same there is no work to be done here. Check that the conversion is not to or
2694 // from an unresolved type as type information is imprecise. If the instance-of is to an
2695 // interface then ignore the type information as interfaces can only be treated as Objects
2696 // and we don't want to disallow field and other operations on the object. If the value
2697 // being instance-of checked against is known null (zero) then allow the optimization as
2698 // we didn't have type information. If the merge of the instance-of type with the original
2699 // type is assignable to the original then allow optimization. This check is performed to
2700 // ensure that subsequent merges don't lose type information - such as becoming an
2701 // interface from a class that would lose information relevant to field checks.
Andreas Gamped78122b2019-07-10 14:06:53 -07002702 //
2703 // Note: do not do an access check. This may mark this with a runtime throw that actually
2704 // happens at the instanceof, not the branch (and branches aren't flagged to throw).
Vladimir Markod7559b72017-09-28 13:50:37 +01002705 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst.VRegB_22c());
Andreas Gamped78122b2019-07-10 14:06:53 -07002706 const RegType& cast_type = ResolveClass<CheckAccess::kNo>(
Vladimir Markod7559b72017-09-28 13:50:37 +01002707 dex::TypeIndex(instance_of_inst.VRegC_22c()));
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002708
Ian Rogersebbdd872014-07-07 23:53:08 -07002709 if (!orig_type.Equals(cast_type) &&
2710 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002711 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002712 !cast_type.GetClass()->IsInterface() &&
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002713 (orig_type.IsZeroOrNull() ||
David Brazdilca3c8c32016-09-06 14:04:48 +01002714 orig_type.IsStrictlyAssignableFrom(
2715 cast_type.Merge(orig_type, &reg_types_, this), this))) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002716 RegisterLine* update_line = RegisterLine::Create(code_item_accessor_.RegistersSize(),
Andreas Gamped09c0592019-04-19 15:44:05 -07002717 allocator_,
2718 GetRegTypeCache());
Ian Rogersfae370a2013-06-05 08:33:27 -07002719 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002720 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002721 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002722 branch_line.reset(update_line);
2723 }
2724 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002725 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002726 instance_of_inst.VRegB_22c(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002727 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002728 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002729 // See if instance-of was preceded by a move-object operation, common due to the small
2730 // register encoding space of instance-of, and propagate type information to the source
2731 // of the move-object.
Andreas Gampe43e43252019-01-08 12:06:57 -08002732 // Note: this is only valid if the move source was not clobbered.
Ian Rogers9b360392013-06-06 14:45:07 -07002733 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002734 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002735 move_idx--;
2736 }
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -07002737 if (FailOrAbort(GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002738 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2739 work_insn_idx_)) {
2740 break;
2741 }
Andreas Gampe43e43252019-01-08 12:06:57 -08002742 auto maybe_update_fn = [&instance_of_inst, update_line, this, &cast_type](
2743 uint16_t move_src,
2744 uint16_t move_trg)
2745 REQUIRES_SHARED(Locks::mutator_lock_) {
2746 if (move_trg == instance_of_inst.VRegB_22c() &&
2747 move_src != instance_of_inst.VRegA_22c()) {
2748 update_line->SetRegisterType<LockOp::kKeep>(this, move_src, cast_type);
2749 }
2750 };
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002751 const Instruction& move_inst = code_item_accessor_.InstructionAt(move_idx);
Vladimir Markod7559b72017-09-28 13:50:37 +01002752 switch (move_inst.Opcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002753 case Instruction::MOVE_OBJECT:
Andreas Gampe43e43252019-01-08 12:06:57 -08002754 maybe_update_fn(move_inst.VRegB_12x(), move_inst.VRegA_12x());
Ian Rogers9b360392013-06-06 14:45:07 -07002755 break;
2756 case Instruction::MOVE_OBJECT_FROM16:
Andreas Gampe43e43252019-01-08 12:06:57 -08002757 maybe_update_fn(move_inst.VRegB_22x(), move_inst.VRegA_22x());
Ian Rogers9b360392013-06-06 14:45:07 -07002758 break;
2759 case Instruction::MOVE_OBJECT_16:
Andreas Gampe43e43252019-01-08 12:06:57 -08002760 maybe_update_fn(move_inst.VRegB_32x(), move_inst.VRegA_32x());
Ian Rogers9b360392013-06-06 14:45:07 -07002761 break;
2762 default:
2763 break;
2764 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002765 }
2766 }
2767 }
2768
jeffhaobdb76512011-09-07 11:43:16 -07002769 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002770 }
jeffhaobdb76512011-09-07 11:43:16 -07002771 case Instruction::IF_LTZ:
2772 case Instruction::IF_GEZ:
2773 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002774 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002775 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002776 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002777 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2778 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002779 }
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::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002783 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002784 break;
jeffhaobdb76512011-09-07 11:43:16 -07002785 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002786 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002787 break;
jeffhaobdb76512011-09-07 11:43:16 -07002788 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002789 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002790 break;
jeffhaobdb76512011-09-07 11:43:16 -07002791 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002792 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002793 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002794 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002795 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002796 break;
jeffhaobdb76512011-09-07 11:43:16 -07002797 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002798 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002799 break;
2800 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002801 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002802 break;
2803
Ian Rogersd81871c2011-10-03 13:57:23 -07002804 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002805 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002806 break;
2807 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002808 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002809 break;
2810 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002811 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002812 break;
2813 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002814 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002815 break;
2816 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002817 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002818 break;
2819 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002820 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002821 break;
2822 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002823 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002824 break;
2825
jeffhaobdb76512011-09-07 11:43:16 -07002826 case Instruction::IGET_BOOLEAN:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002827 case Instruction::IGET_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002828 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002829 break;
jeffhaobdb76512011-09-07 11:43:16 -07002830 case Instruction::IGET_BYTE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002831 case Instruction::IGET_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002832 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002833 break;
jeffhaobdb76512011-09-07 11:43:16 -07002834 case Instruction::IGET_CHAR:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002835 case Instruction::IGET_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002836 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002837 break;
jeffhaobdb76512011-09-07 11:43:16 -07002838 case Instruction::IGET_SHORT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002839 case Instruction::IGET_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002840 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002841 break;
2842 case Instruction::IGET:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002843 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002844 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002845 break;
2846 case Instruction::IGET_WIDE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002847 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002848 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002849 break;
2850 case Instruction::IGET_OBJECT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002851 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002852 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2853 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002854 break;
jeffhaobdb76512011-09-07 11:43:16 -07002855
Ian Rogersd81871c2011-10-03 13:57:23 -07002856 case Instruction::IPUT_BOOLEAN:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002857 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002858 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002859 break;
2860 case Instruction::IPUT_BYTE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002861 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002862 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002863 break;
2864 case Instruction::IPUT_CHAR:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002865 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002866 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002867 break;
2868 case Instruction::IPUT_SHORT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002869 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002870 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002871 break;
2872 case Instruction::IPUT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002873 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002874 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002875 break;
2876 case Instruction::IPUT_WIDE:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002877 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002878 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002879 break;
jeffhaobdb76512011-09-07 11:43:16 -07002880 case Instruction::IPUT_OBJECT:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002881 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002882 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2883 false);
jeffhaobdb76512011-09-07 11:43:16 -07002884 break;
2885
jeffhaobdb76512011-09-07 11:43:16 -07002886 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002887 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002888 break;
jeffhaobdb76512011-09-07 11:43:16 -07002889 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002890 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002891 break;
jeffhaobdb76512011-09-07 11:43:16 -07002892 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002893 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002894 break;
jeffhaobdb76512011-09-07 11:43:16 -07002895 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002896 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002897 break;
2898 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002899 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002900 break;
2901 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002902 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002903 break;
2904 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002905 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2906 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002907 break;
2908
2909 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002910 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002911 break;
2912 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002913 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002914 break;
2915 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002916 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002917 break;
2918 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002919 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002920 break;
2921 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002922 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002923 break;
2924 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002925 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002926 break;
2927 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002928 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2929 true);
jeffhaobdb76512011-09-07 11:43:16 -07002930 break;
2931
2932 case Instruction::INVOKE_VIRTUAL:
2933 case Instruction::INVOKE_VIRTUAL_RANGE:
2934 case Instruction::INVOKE_SUPER:
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002935 case Instruction::INVOKE_SUPER_RANGE:
2936 case Instruction::INVOKE_VIRTUAL_QUICK:
2937 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002938 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002939 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE ||
2940 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002941 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2942 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002943 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2944 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002945 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002946 if (called_method != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01002947 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2948 ? called_method->ResolveReturnType()
2949 : called_method->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002950 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002951 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
Vladimir Marko179b7c62019-03-22 13:38:57 +00002952 return_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07002953 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002954 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002955 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2956 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002957 }
2958 }
2959 if (return_type == nullptr) {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002960 uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002961 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002962 dex::TypeIndex return_type_idx =
2963 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002964 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Andreas Gampe51de69e2019-04-19 15:14:14 -07002965 return_type = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002966 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002967 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002968 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002969 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002970 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002971 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002972 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002973 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002974 }
jeffhaobdb76512011-09-07 11:43:16 -07002975 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002976 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002977 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002978 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002979 const char* return_type_descriptor;
2980 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002981 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002982 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002983 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002984 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002985 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Andreas Gampea5b09a62016-11-17 15:21:22 -08002986 dex::TypeIndex return_type_idx =
2987 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers46685432012-06-03 22:26:43 -07002988 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2989 } else {
2990 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002991 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Markob45528c2017-07-27 14:14:28 +01002992 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2993 ? called_method->ResolveReturnType()
2994 : called_method->LookupResolvedReturnType();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002995 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002996 return_type = &FromClass(return_type_descriptor,
Vladimir Markobcf17522018-06-01 13:14:32 +01002997 return_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07002998 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002999 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003000 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3001 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003002 }
Ian Rogers46685432012-06-03 22:26:43 -07003003 }
3004 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07003005 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003006 * Some additional checks when calling a constructor. We know from the invocation arg check
3007 * that the "this" argument is an instance of called_method->klass. Now we further restrict
3008 * that to require that called_method->klass is the same as this->klass or this->super,
3009 * allowing the latter only if the "this" argument is the same as the "this" argument to
3010 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07003011 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003012 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
jeffhaob57e9522012-04-26 18:08:21 -07003013 if (this_type.IsConflict()) // failure.
3014 break;
jeffhaobdb76512011-09-07 11:43:16 -07003015
jeffhaob57e9522012-04-26 18:08:21 -07003016 /* no null refs allowed (?) */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08003017 if (this_type.IsZeroOrNull()) {
jeffhaob57e9522012-04-26 18:08:21 -07003018 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
3019 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07003020 }
jeffhaob57e9522012-04-26 18:08:21 -07003021
3022 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003023 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07003024 // TODO: re-enable constructor type verification
3025 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07003026 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07003027 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
3028 // break;
3029 // }
jeffhaob57e9522012-04-26 18:08:21 -07003030
3031 /* arg must be an uninitialized reference */
3032 if (!this_type.IsUninitializedTypes()) {
3033 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
3034 << this_type;
3035 break;
3036 }
3037
3038 /*
3039 * Replace the uninitialized reference with an initialized one. We need to do this for all
3040 * registers that have the same object instance in them, not just the "this" register.
3041 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00003042 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003043 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07003044 if (return_type == nullptr) {
Andreas Gampe51de69e2019-04-19 15:14:14 -07003045 return_type = &reg_types_.FromDescriptor(class_loader_.Get(),
3046 return_type_descriptor,
3047 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07003048 }
3049 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003050 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003051 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07003052 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003053 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003054 just_set_result = true;
3055 break;
3056 }
3057 case Instruction::INVOKE_STATIC:
3058 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003059 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003060 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003061 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003062 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003063 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003064 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003065 dex::TypeIndex return_type_idx =
3066 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07003067 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003068 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003069 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003070 }
Andreas Gampe51de69e2019-04-19 15:14:14 -07003071 const RegType& return_type = reg_types_.FromDescriptor(class_loader_.Get(),
3072 descriptor,
3073 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003074 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003075 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003076 } else {
3077 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3078 }
jeffhaobdb76512011-09-07 11:43:16 -07003079 just_set_result = true;
3080 }
3081 break;
jeffhaobdb76512011-09-07 11:43:16 -07003082 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07003083 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003084 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003085 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003086 if (abs_method != nullptr) {
Vladimir Markod93e3742018-07-18 10:58:13 +01003087 ObjPtr<mirror::Class> called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003088 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
3089 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
David Sehr709b0702016-10-13 09:12:37 -07003090 << abs_method->PrettyMethod() << "'";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003091 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003092 }
Ian Rogers0d604842012-04-16 14:50:24 -07003093 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003094 /* Get the type of the "this" arg, which should either be a sub-interface of called
3095 * interface or Object (see comments in RegType::JoinClass).
3096 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003097 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08003098 if (this_type.IsZeroOrNull()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003099 /* null pointer always passes (and always fails at runtime) */
3100 } else {
3101 if (this_type.IsUninitializedTypes()) {
3102 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
3103 << this_type;
3104 break;
3105 }
3106 // In the past we have tried to assert that "called_interface" is assignable
3107 // from "this_type.GetClass()", however, as we do an imprecise Join
3108 // (RegType::JoinClass) we don't have full information on what interfaces are
3109 // implemented by "this_type". For example, two classes may implement the same
3110 // interfaces and have a common parent that doesn't implement the interface. The
3111 // join will set "this_type" to the parent class and a test that this implements
3112 // the interface will incorrectly fail.
3113 }
3114 /*
3115 * We don't have an object instance, so we can't find the concrete method. However, all of
3116 * the type information is in the abstract method, so we're good.
3117 */
3118 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003119 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003120 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003121 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003122 dex::TypeIndex return_type_idx =
3123 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003124 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003125 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003126 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003127 }
Andreas Gampe51de69e2019-04-19 15:14:14 -07003128 const RegType& return_type = reg_types_.FromDescriptor(class_loader_.Get(),
3129 descriptor,
3130 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003131 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003132 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003133 } else {
3134 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3135 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003136 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07003137 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003138 }
Narayan Kamath9823e782016-08-03 12:46:58 +01003139 case Instruction::INVOKE_POLYMORPHIC:
3140 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003141 bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
3142 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range);
3143 if (called_method == nullptr) {
3144 // Convert potential soft failures in VerifyInvocationArgs() to hard errors.
3145 if (failure_messages_.size() > 0) {
3146 std::string message = failure_messages_.back()->str();
3147 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message;
3148 } else {
3149 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure.";
3150 }
3151 break;
3152 }
3153 if (!CheckSignaturePolymorphicMethod(called_method) ||
3154 !CheckSignaturePolymorphicReceiver(inst)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00003155 DCHECK(HasFailures());
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003156 break;
3157 }
Orion Hodson06d10a72018-05-14 08:53:38 +01003158 const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
3159 const dex::ProtoIndex proto_idx(vRegH);
Orion Hodsonac141392017-01-13 11:53:47 +00003160 const char* return_descriptor =
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003161 dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx));
3162 const RegType& return_type =
Andreas Gampe51de69e2019-04-19 15:14:14 -07003163 reg_types_.FromDescriptor(class_loader_.Get(), return_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003164 if (!return_type.IsLowHalf()) {
3165 work_line_->SetResultRegisterType(this, return_type);
3166 } else {
3167 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3168 }
Orion Hodsonac141392017-01-13 11:53:47 +00003169 just_set_result = true;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003170 break;
Narayan Kamath9823e782016-08-03 12:46:58 +01003171 }
Orion Hodsonc069a302017-01-18 09:23:12 +00003172 case Instruction::INVOKE_CUSTOM:
3173 case Instruction::INVOKE_CUSTOM_RANGE: {
3174 // Verify registers based on method_type in the call site.
3175 bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE);
3176
3177 // Step 1. Check the call site that produces the method handle for invocation
3178 const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3179 if (!CheckCallSite(call_site_idx)) {
3180 DCHECK(HasFailures());
3181 break;
3182 }
3183
3184 // Step 2. Check the register arguments correspond to the expected arguments for the
3185 // method handle produced by step 1. The dex file verifier has checked ranges for
3186 // the first three arguments and CheckCallSite has checked the method handle type.
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003187 const dex::ProtoIndex proto_idx = dex_file_->GetProtoIndexForCallSite(call_site_idx);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003188 const dex::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00003189 DexFileParameterIterator param_it(*dex_file_, proto_id);
3190 // Treat method as static as it has yet to be determined.
3191 VerifyInvocationArgsFromIterator(&param_it, inst, METHOD_STATIC, is_range, nullptr);
3192 const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
3193
3194 // Step 3. Propagate return type information
3195 const RegType& return_type =
Andreas Gampe51de69e2019-04-19 15:14:14 -07003196 reg_types_.FromDescriptor(class_loader_.Get(), return_descriptor, false);
Orion Hodsonc069a302017-01-18 09:23:12 +00003197 if (!return_type.IsLowHalf()) {
3198 work_line_->SetResultRegisterType(this, return_type);
3199 } else {
3200 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3201 }
3202 just_set_result = true;
Orion Hodsonc069a302017-01-18 09:23:12 +00003203 break;
3204 }
jeffhaobdb76512011-09-07 11:43:16 -07003205 case Instruction::NEG_INT:
3206 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003207 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003208 break;
3209 case Instruction::NEG_LONG:
3210 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003211 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003212 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003213 break;
3214 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003215 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003216 break;
3217 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003218 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003219 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003220 break;
3221 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003222 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003223 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003224 break;
3225 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003226 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003227 break;
3228 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003229 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003230 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003231 break;
3232 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003233 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003234 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003235 break;
3236 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003237 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003238 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003239 break;
3240 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003241 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003242 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003243 break;
3244 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003245 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003246 break;
3247 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003248 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003249 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003250 break;
3251 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003252 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003253 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003254 break;
3255 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003256 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003257 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003258 break;
3259 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003260 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003261 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003262 break;
3263 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003264 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003265 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003266 break;
3267 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003268 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003269 break;
3270 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003271 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003272 break;
3273 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003274 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003275 break;
3276
3277 case Instruction::ADD_INT:
3278 case Instruction::SUB_INT:
3279 case Instruction::MUL_INT:
3280 case Instruction::REM_INT:
3281 case Instruction::DIV_INT:
3282 case Instruction::SHL_INT:
3283 case Instruction::SHR_INT:
3284 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003285 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003286 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003287 break;
3288 case Instruction::AND_INT:
3289 case Instruction::OR_INT:
3290 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003291 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003292 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003293 break;
3294 case Instruction::ADD_LONG:
3295 case Instruction::SUB_LONG:
3296 case Instruction::MUL_LONG:
3297 case Instruction::DIV_LONG:
3298 case Instruction::REM_LONG:
3299 case Instruction::AND_LONG:
3300 case Instruction::OR_LONG:
3301 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003302 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003303 reg_types_.LongLo(), reg_types_.LongHi(),
3304 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003305 break;
3306 case Instruction::SHL_LONG:
3307 case Instruction::SHR_LONG:
3308 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003309 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003310 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003311 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003312 break;
3313 case Instruction::ADD_FLOAT:
3314 case Instruction::SUB_FLOAT:
3315 case Instruction::MUL_FLOAT:
3316 case Instruction::DIV_FLOAT:
3317 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003318 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3319 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003320 break;
3321 case Instruction::ADD_DOUBLE:
3322 case Instruction::SUB_DOUBLE:
3323 case Instruction::MUL_DOUBLE:
3324 case Instruction::DIV_DOUBLE:
3325 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003326 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003327 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3328 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003329 break;
3330 case Instruction::ADD_INT_2ADDR:
3331 case Instruction::SUB_INT_2ADDR:
3332 case Instruction::MUL_INT_2ADDR:
3333 case Instruction::REM_INT_2ADDR:
3334 case Instruction::SHL_INT_2ADDR:
3335 case Instruction::SHR_INT_2ADDR:
3336 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003337 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3338 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003339 break;
3340 case Instruction::AND_INT_2ADDR:
3341 case Instruction::OR_INT_2ADDR:
3342 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003343 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3344 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003345 break;
3346 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003347 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3348 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003349 break;
3350 case Instruction::ADD_LONG_2ADDR:
3351 case Instruction::SUB_LONG_2ADDR:
3352 case Instruction::MUL_LONG_2ADDR:
3353 case Instruction::DIV_LONG_2ADDR:
3354 case Instruction::REM_LONG_2ADDR:
3355 case Instruction::AND_LONG_2ADDR:
3356 case Instruction::OR_LONG_2ADDR:
3357 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003358 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003359 reg_types_.LongLo(), reg_types_.LongHi(),
3360 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003361 break;
3362 case Instruction::SHL_LONG_2ADDR:
3363 case Instruction::SHR_LONG_2ADDR:
3364 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003365 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003366 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003367 break;
3368 case Instruction::ADD_FLOAT_2ADDR:
3369 case Instruction::SUB_FLOAT_2ADDR:
3370 case Instruction::MUL_FLOAT_2ADDR:
3371 case Instruction::DIV_FLOAT_2ADDR:
3372 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003373 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3374 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003375 break;
3376 case Instruction::ADD_DOUBLE_2ADDR:
3377 case Instruction::SUB_DOUBLE_2ADDR:
3378 case Instruction::MUL_DOUBLE_2ADDR:
3379 case Instruction::DIV_DOUBLE_2ADDR:
3380 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003381 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003382 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3383 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003384 break;
3385 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003386 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003387 case Instruction::MUL_INT_LIT16:
3388 case Instruction::DIV_INT_LIT16:
3389 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003390 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3391 true);
jeffhaobdb76512011-09-07 11:43:16 -07003392 break;
3393 case Instruction::AND_INT_LIT16:
3394 case Instruction::OR_INT_LIT16:
3395 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003396 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3397 true);
jeffhaobdb76512011-09-07 11:43:16 -07003398 break;
3399 case Instruction::ADD_INT_LIT8:
3400 case Instruction::RSUB_INT_LIT8:
3401 case Instruction::MUL_INT_LIT8:
3402 case Instruction::DIV_INT_LIT8:
3403 case Instruction::REM_INT_LIT8:
3404 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003405 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003406 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003407 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3408 false);
jeffhaobdb76512011-09-07 11:43:16 -07003409 break;
3410 case Instruction::AND_INT_LIT8:
3411 case Instruction::OR_INT_LIT8:
3412 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003413 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3414 false);
jeffhaobdb76512011-09-07 11:43:16 -07003415 break;
3416
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003417 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003418 case Instruction::RETURN_VOID_NO_BARRIER:
3419 if (IsConstructor() && !IsStatic()) {
Vladimir Markod93e3742018-07-18 10:58:13 +01003420 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003421 if (declaring_class.IsUnresolvedReference()) {
3422 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3423 // manually over the underlying dex file.
3424 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3425 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
Andreas Gampee2abbc62017-09-15 11:59:26 -07003426 if (first_index != dex::kDexNoIndex) {
Andreas Gampe68df3202015-06-22 11:35:46 -07003427 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3428 << first_index;
3429 }
3430 break;
3431 }
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003432 ObjPtr<mirror::Class> klass = declaring_class.GetClass();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003433 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3434 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003435 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
David Sehr709b0702016-10-13 09:12:37 -07003436 << klass->GetInstanceField(i)->PrettyField();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003437 break;
3438 }
3439 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003440 }
Andreas Gampeb2917962015-07-31 13:36:10 -07003441 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3442 // quickened opcodes (otherwise this could be a fall-through).
3443 if (!IsConstructor()) {
3444 if (!GetMethodReturnType().IsConflict()) {
3445 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3446 }
3447 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003448 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003449
Ian Rogersd81871c2011-10-03 13:57:23 -07003450 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003451 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Narayan Kamath8ec3bd22016-08-03 12:46:23 +01003452 case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003453 case Instruction::UNUSED_79:
3454 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003455 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003456 break;
3457
3458 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003459 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003460 * complain if an instruction is missing (which is desirable).
3461 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003462 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003463
Andreas Gampe43884b22019-06-27 14:05:52 -07003464 if (flags_.have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003465 if (Runtime::Current()->IsAotCompiler()) {
3466 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003467 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3468 LOG(ERROR) << "Pending failures:";
3469 for (auto& error : failures_) {
3470 LOG(ERROR) << error;
3471 }
3472 for (auto& error_msg : failure_messages_) {
3473 LOG(ERROR) << error_msg->str();
3474 }
3475 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3476 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003477 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003478 /* immediate failure, reject class */
3479 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3480 return false;
Andreas Gampe43884b22019-06-27 14:05:52 -07003481 } else if (flags_.have_pending_runtime_throw_failure_) {
Andreas Gampe6087bc22019-06-03 15:52:08 -07003482 LogVerifyInfo() << "Elevating opcode flags from " << opcode_flags << " to Throw";
Jeff Haoa3faaf42013-09-03 19:07:00 -07003483 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003484 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003485 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3486 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3487 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003488 }
jeffhaobdb76512011-09-07 11:43:16 -07003489 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003490 * If we didn't just set the result register, clear it out. This ensures that you can only use
3491 * "move-result" immediately after the result is set. (We could check this statically, but it's
3492 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003493 */
3494 if (!just_set_result) {
Andreas Gamped09c0592019-04-19 15:44:05 -07003495 work_line_->SetResultTypeToUnknown(GetRegTypeCache());
jeffhaobdb76512011-09-07 11:43:16 -07003496 }
3497
jeffhaobdb76512011-09-07 11:43:16 -07003498 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003499 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003500 *
3501 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003502 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003503 * somebody could get a reference field, check it for zero, and if the
3504 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003505 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003506 * that, and will reject the code.
3507 *
3508 * TODO: avoid re-fetching the branch target
3509 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003510 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003511 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003512 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003513 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003514 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003515 return false;
3516 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003517 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003518 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(),
3519 work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003520 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003521 }
jeffhaobdb76512011-09-07 11:43:16 -07003522 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003523 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003524 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003525 return false;
3526 }
3527 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003528 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003529 return false;
3530 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003531 }
jeffhaobdb76512011-09-07 11:43:16 -07003532 }
3533
3534 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003535 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003536 *
3537 * We've already verified that the table is structurally sound, so we
3538 * just need to walk through and tag the targets.
3539 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003540 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003541 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003542 const uint16_t* switch_insns = insns + offset_to_switch;
3543 int switch_count = switch_insns[1];
3544 int offset_to_targets, targ;
3545
3546 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3547 /* 0 = sig, 1 = count, 2/3 = first key */
3548 offset_to_targets = 4;
3549 } else {
3550 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003551 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003552 offset_to_targets = 2 + 2 * switch_count;
3553 }
3554
3555 /* verify each switch target */
3556 for (targ = 0; targ < switch_count; targ++) {
3557 int offset;
3558 uint32_t abs_offset;
3559
3560 /* offsets are 32-bit, and only partly endian-swapped */
3561 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003562 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003563 abs_offset = work_insn_idx_ + offset;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003564 DCHECK_LT(abs_offset, code_item_accessor_.InsnsSizeInCodeUnits());
3565 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003566 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003567 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003568 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003569 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003570 }
jeffhaobdb76512011-09-07 11:43:16 -07003571 }
3572 }
3573
3574 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003575 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3576 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003577 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003578 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003579 bool has_catch_all_handler = false;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003580 const dex::TryItem* try_item = code_item_accessor_.FindTryItem(work_insn_idx_);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003581 CHECK(try_item != nullptr);
Mathieu Chartierdc578c72017-12-27 11:51:45 -08003582 CatchHandlerIterator iterator(code_item_accessor_, *try_item);
jeffhaobdb76512011-09-07 11:43:16 -07003583
Andreas Gampef91baf12014-07-18 15:41:00 -07003584 // Need the linker to try and resolve the handled class to check if it's Throwable.
3585 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3586
Ian Rogers0571d352011-11-03 19:51:38 -07003587 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003588 dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex();
3589 if (!handler_type_idx.IsValid()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003590 has_catch_all_handler = true;
3591 } else {
3592 // It is also a catch-all if it is java.lang.Throwable.
Vladimir Marko28e012a2017-12-07 11:22:59 +00003593 ObjPtr<mirror::Class> klass =
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003594 linker->ResolveType(handler_type_idx, dex_cache_, class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003595 if (klass != nullptr) {
Vladimir Markoadbceb72018-05-29 14:34:14 +01003596 if (klass == GetClassRoot<mirror::Throwable>()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003597 has_catch_all_handler = true;
3598 }
3599 } else {
3600 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003601 DCHECK(self_->IsExceptionPending());
3602 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003603 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003604 }
jeffhaobdb76512011-09-07 11:43:16 -07003605 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003606 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3607 * "work_regs", because at runtime the exception will be thrown before the instruction
3608 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003609 */
Andreas Gampebfcca582019-04-19 12:01:55 -07003610 if (kVerifierDebug) {
3611 LogVerifyInfo() << "Updating exception handler 0x"
3612 << std::hex << iterator.GetHandlerAddress();
3613 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003614 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003615 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003616 }
jeffhaobdb76512011-09-07 11:43:16 -07003617 }
3618
3619 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003620 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3621 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003622 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003623 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003624 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003625 * The state in work_line reflects the post-execution state. If the current instruction is a
3626 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003627 * it will do so before grabbing the lock).
3628 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003629 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003630 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003631 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003632 return false;
3633 }
3634 }
3635 }
3636
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003637 /* Handle "continue". Tag the next consecutive instruction.
3638 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3639 * because it changes work_line_ when performing peephole optimization
3640 * and this change should not be used in those cases.
3641 */
Andreas Gampe6087bc22019-06-03 15:52:08 -07003642 if ((opcode_flags & Instruction::kContinue) != 0 && !exc_handler_unreachable) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003643 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003644 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003645 if (next_insn_idx >= code_item_accessor_.InsnsSizeInCodeUnits()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003646 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3647 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003648 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003649 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3650 // next instruction isn't one.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003651 if (!CheckNotMoveException(code_item_accessor_.Insns(), next_insn_idx)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003652 return false;
3653 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003654 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003655 // Make workline consistent with fallthrough computed from peephole optimization.
3656 work_line_->CopyFromLine(fallthrough_line.get());
3657 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003658 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003659 // For returns we only care about the operand to the return, all other registers are dead.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003660 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003661 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003662 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003663 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003664 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003665 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3666 // needed. If the merge changes the state of the registers then the work line will be
3667 // updated.
3668 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003669 return false;
3670 }
3671 } else {
3672 /*
3673 * We're not recording register data for the next instruction, so we don't know what the
3674 * prior state was. We have to assume that something has changed and re-evaluate it.
3675 */
Andreas Gampe51de69e2019-04-19 15:14:14 -07003676 GetModifiableInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003677 }
3678 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003679
jeffhaod1f0fde2011-09-08 17:25:33 -07003680 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003681 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003682 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003683 }
3684
3685 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003686 * Update start_guess. Advance to the next instruction of that's
3687 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003688 * neither of those exists we're in a return or throw; leave start_guess
3689 * alone and let the caller sort it out.
3690 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003691 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003692 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003693 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003694 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003695 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003696 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003697 }
3698
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003699 DCHECK_LT(*start_guess, code_item_accessor_.InsnsSizeInCodeUnits());
Mathieu Chartierde40d472015-10-15 17:47:48 -07003700 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003701
Andreas Gampe43884b22019-06-27 14:05:52 -07003702 if (flags_.have_pending_runtime_throw_failure_) {
3703 flags_.have_any_pending_runtime_throw_failure_ = true;
Andreas Gampea727e372015-08-25 09:22:37 -07003704 // Reset the pending_runtime_throw flag now.
Andreas Gampe43884b22019-06-27 14:05:52 -07003705 flags_.have_pending_runtime_throw_failure_ = false;
Andreas Gampea727e372015-08-25 09:22:37 -07003706 }
3707
jeffhaobdb76512011-09-07 11:43:16 -07003708 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003709} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003710
Andreas Gampe99db7bb2019-04-19 23:05:47 -07003711template <bool kVerifierDebug>
3712template <CheckAccess C>
3713const RegType& MethodVerifier<kVerifierDebug>::ResolveClass(dex::TypeIndex class_idx) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003714 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Marko28e012a2017-12-07 11:22:59 +00003715 ObjPtr<mirror::Class> klass = can_load_classes_
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003716 ? linker->ResolveType(class_idx, dex_cache_, class_loader_)
3717 : linker->LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003718 if (can_load_classes_ && klass == nullptr) {
3719 DCHECK(self_->IsExceptionPending());
3720 self_->ClearException();
3721 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003722 const RegType* result = nullptr;
Vladimir Marko9cb0c462017-04-21 13:31:41 +01003723 if (klass != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003724 bool precise = klass->CannotBeAssignedFromOtherTypes();
3725 if (precise && !IsInstantiableOrPrimitive(klass)) {
3726 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3727 UninstantiableError(descriptor);
3728 precise = false;
3729 }
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003730 result = reg_types_.FindClass(klass, precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003731 if (result == nullptr) {
3732 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003733 result = reg_types_.InsertClass(descriptor, klass, precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003734 }
3735 } else {
3736 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Andreas Gampe51de69e2019-04-19 15:14:14 -07003737 result = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003738 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003739 DCHECK(result != nullptr);
3740 if (result->IsConflict()) {
3741 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3742 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3743 << "' in " << GetDeclaringClass();
3744 return *result;
3745 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003746
3747 // Record result of class resolution attempt.
Vladimir Markobcf17522018-06-01 13:14:32 +01003748 VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass);
David Brazdilca3c8c32016-09-06 14:04:48 +01003749
Andreas Gampe629be512017-08-25 17:09:32 -07003750 // If requested, check if access is allowed. Unresolved types are included in this check, as the
3751 // interpreter only tests whether access is allowed when a class is not pre-verified and runs in
3752 // the access-checks interpreter. If result is primitive, skip the access check.
3753 //
3754 // Note: we do this for unresolved classes to trigger re-verification at runtime.
Andreas Gampe6087bc22019-06-03 15:52:08 -07003755 if (C != CheckAccess::kNo &&
Andreas Gampedc39d322018-09-04 09:26:03 -07003756 result->IsNonZeroReferenceTypes() &&
Andreas Gampe6087bc22019-06-03 15:52:08 -07003757 ((C == CheckAccess::kYes && IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP))
3758 || !result->IsUnresolvedTypes())) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003759 const RegType& referrer = GetDeclaringClass();
David Brazdil2bb2fbd2018-11-13 18:24:26 +00003760 if ((IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP) || !referrer.IsUnresolvedTypes()) &&
3761 !referrer.CanAccess(*result)) {
Andreas Gampe629be512017-08-25 17:09:32 -07003762 Fail(VERIFY_ERROR_ACCESS_CLASS) << "(possibly) illegal class access: '"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +01003763 << referrer << "' -> '" << *result << "'";
Mathieu Chartierde40d472015-10-15 17:47:48 -07003764 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003765 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003766 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003767}
3768
Andreas Gampe99db7bb2019-04-19 23:05:47 -07003769template <bool kVerifierDebug>
Andreas Gampe6087bc22019-06-03 15:52:08 -07003770bool MethodVerifier<kVerifierDebug>::HandleMoveException(const Instruction* inst) {
3771 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
3772 // where one entrypoint to the catch block is not actually an exception path.
3773 if (work_insn_idx_ == 0) {
3774 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
3775 return true;
3776 }
3777 /*
3778 * This statement can only appear as the first instruction in an exception handler. We verify
3779 * that as part of extracting the exception type from the catch block list.
3780 */
3781 auto caught_exc_type_fn = [&]() REQUIRES_SHARED(Locks::mutator_lock_) ->
3782 std::pair<bool, const RegType*> {
3783 const RegType* common_super = nullptr;
3784 if (code_item_accessor_.TriesSize() != 0) {
3785 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
3786 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3787 const RegType* unresolved = nullptr;
3788 for (uint32_t i = 0; i < handlers_size; i++) {
3789 CatchHandlerIterator iterator(handlers_ptr);
3790 for (; iterator.HasNext(); iterator.Next()) {
3791 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3792 if (!iterator.GetHandlerTypeIndex().IsValid()) {
3793 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003794 } else {
Andreas Gampe6087bc22019-06-03 15:52:08 -07003795 // Do access checks only on resolved exception classes.
3796 const RegType& exception =
3797 ResolveClass<CheckAccess::kOnResolvedClass>(iterator.GetHandlerTypeIndex());
3798 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) {
3799 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
3800 if (exception.IsUnresolvedTypes()) {
3801 if (unresolved == nullptr) {
3802 unresolved = &exception;
3803 } else {
3804 unresolved = &unresolved->SafeMerge(exception, &reg_types_, this);
3805 }
3806 } else {
3807 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class "
3808 << exception;
3809 return std::make_pair(true, &reg_types_.Conflict());
3810 }
3811 } else if (common_super == nullptr) {
3812 common_super = &exception;
3813 } else if (common_super->Equals(exception)) {
3814 // odd case, but nothing to do
3815 } else {
3816 common_super = &common_super->Merge(exception, &reg_types_, this);
3817 if (FailOrAbort(reg_types_.JavaLangThrowable(false).IsAssignableFrom(
3818 *common_super, this),
3819 "java.lang.Throwable is not assignable-from common_super at ",
3820 work_insn_idx_)) {
3821 break;
3822 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003823 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003824 }
3825 }
3826 }
Andreas Gampe6087bc22019-06-03 15:52:08 -07003827 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003828 }
Andreas Gampe6087bc22019-06-03 15:52:08 -07003829 if (unresolved != nullptr) {
3830 if (!Runtime::Current()->IsAotCompiler() && common_super == nullptr) {
3831 // This is an unreachable handler.
Andreas Gampe0a7d0b12019-06-04 10:42:29 -07003832
3833 // We need to post a failure. The compiler currently does not handle unreachable
3834 // code correctly.
Andreas Gampe0d87f992019-07-10 13:19:45 -07003835 Fail(VERIFY_ERROR_SKIP_COMPILER, /*pending_exc=*/ false)
3836 << "Unresolved catch handler, fail for compiler";
Andreas Gampe0a7d0b12019-06-04 10:42:29 -07003837
Andreas Gampe6087bc22019-06-03 15:52:08 -07003838 return std::make_pair(false, unresolved);
3839 }
3840 // Soft-fail, but do not handle this with a synthetic throw.
Andreas Gampe0d87f992019-07-10 13:19:45 -07003841 Fail(VERIFY_ERROR_NO_CLASS, /*pending_exc=*/ false) << "Unresolved catch handler";
Andreas Gampe6087bc22019-06-03 15:52:08 -07003842 if (common_super != nullptr) {
3843 unresolved = &unresolved->Merge(*common_super, &reg_types_, this);
3844 }
3845 return std::make_pair(true, unresolved);
3846 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003847 }
Andreas Gampe6087bc22019-06-03 15:52:08 -07003848 if (common_super == nullptr) {
3849 /* no catch blocks, or no catches with classes we can find */
3850 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
3851 return std::make_pair(true, &reg_types_.Conflict());
3852 }
3853 return std::make_pair(true, common_super);
3854 };
3855 auto result = caught_exc_type_fn();
3856 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), *result.second);
3857 return result.first;
Ian Rogersd81871c2011-10-03 13:57:23 -07003858}
3859
Andreas Gampe99db7bb2019-04-19 23:05:47 -07003860template <bool kVerifierDebug>
3861ArtMethod* MethodVerifier<kVerifierDebug>::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003862 uint32_t dex_method_idx, MethodType method_type) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003863 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Andreas Gampe98be1a92017-08-28 08:25:45 -07003864 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003865 if (klass_type.IsConflict()) {
3866 std::string append(" in attempt to access method ");
3867 append += dex_file_->GetMethodName(method_id);
3868 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003869 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003870 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003871 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003872 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003873 }
Vladimir Markoba118822017-06-12 15:41:56 +01003874 ObjPtr<mirror::Class> klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003875 const RegType& referrer = GetDeclaringClass();
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00003876 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3877 PointerSize pointer_size = class_linker->GetImagePointerSize();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003878
Mathieu Chartiere401d142015-04-22 13:56:20 -07003879 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003880 if (res_method == nullptr) {
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00003881 res_method = class_linker->FindResolvedMethod(
3882 klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07003883 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003884
Vladimir Markoba118822017-06-12 15:41:56 +01003885 // Record result of method resolution attempt. The klass resolution has recorded whether
3886 // the class is an interface or not and therefore the type of the lookup performed above.
3887 // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type.
3888 VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_method);
3889
Alex Lightafb66472017-08-01 09:54:49 -07003890 bool must_fail = false;
3891 // This is traditional and helps with screwy bytecode. It will tell you that, yes, a method
3892 // exists, but that it's called incorrectly. This significantly helps debugging, as locally it's
3893 // hard to see the differences.
3894 // If we don't have res_method here we must fail. Just use this bool to make sure of that with a
3895 // DCHECK.
Vladimir Markoba118822017-06-12 15:41:56 +01003896 if (res_method == nullptr) {
Alex Lightafb66472017-08-01 09:54:49 -07003897 must_fail = true;
Vladimir Markoba118822017-06-12 15:41:56 +01003898 // Try to find the method also with the other type for better error reporting below
3899 // but do not store such bogus lookup result in the DexCache or VerifierDeps.
David Brazdil4525e0b2018-04-05 16:57:32 +01003900 res_method = class_linker->FindIncompatibleMethod(
3901 klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx);
Vladimir Markoba118822017-06-12 15:41:56 +01003902 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003903
3904 if (res_method == nullptr) {
3905 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
David Sehr709b0702016-10-13 09:12:37 -07003906 << klass->PrettyDescriptor() << "."
David Brazdilca3c8c32016-09-06 14:04:48 +01003907 << dex_file_->GetMethodName(method_id) << " "
3908 << dex_file_->GetMethodSignature(method_id);
3909 return nullptr;
3910 }
3911
Ian Rogersd81871c2011-10-03 13:57:23 -07003912 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3913 // enforce them here.
3914 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003915 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
David Sehr709b0702016-10-13 09:12:37 -07003916 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003917 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003918 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003919 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003920 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003921 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
David Sehr709b0702016-10-13 09:12:37 -07003922 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003923 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003924 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003925
3926 // Check that interface methods are static or match interface classes.
3927 // We only allow statics if we don't have default methods enabled.
3928 //
3929 // Note: this check must be after the initializer check, as those are required to fail a class,
3930 // while this check implies an IncompatibleClassChangeError.
3931 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003932 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003933 // default methods are supported for the dex file), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003934 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003935 method_type != METHOD_STATIC &&
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003936 (!dex_file_->SupportsDefaultMethods() ||
Alex Lightb55f1ac2016-04-12 15:50:55 -07003937 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003938 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003939 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003940 << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx)
3941 << " is in an interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003942 return nullptr;
3943 }
3944 } else {
3945 if (method_type == METHOD_INTERFACE) {
3946 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003947 << "interface method " << dex_file_->PrettyMethod(dex_method_idx)
3948 << " is in a non-interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003949 return nullptr;
3950 }
3951 }
3952
Alex Lightafb66472017-08-01 09:54:49 -07003953 // Check specifically for non-public object methods being provided for interface dispatch. This
3954 // can occur if we failed to find a method with FindInterfaceMethod but later find one with
3955 // FindClassMethod for error message use.
3956 if (method_type == METHOD_INTERFACE &&
3957 res_method->GetDeclaringClass()->IsObjectClass() &&
3958 !res_method->IsPublic()) {
3959 Fail(VERIFY_ERROR_NO_METHOD) << "invoke-interface " << klass->PrettyDescriptor() << "."
3960 << dex_file_->GetMethodName(method_id) << " "
3961 << dex_file_->GetMethodSignature(method_id) << " resolved to "
3962 << "non-public object method " << res_method->PrettyMethod() << " "
3963 << "but non-public Object methods are excluded from interface "
3964 << "method resolution.";
3965 return nullptr;
3966 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003967 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003968 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07003969 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call "
3970 << res_method->PrettyMethod()
Ian Rogersad0b3a32012-04-16 14:50:24 -07003971 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003972 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003973 }
jeffhaode0d9c92012-02-27 13:58:13 -08003974 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003975 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003976 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
David Sehr709b0702016-10-13 09:12:37 -07003977 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003978 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003979 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003980 // See if the method type implied by the invoke instruction matches the access flags for the
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003981 // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two
3982 // signature polymorphic methods supported by the run-time which are native methods with variable
3983 // arguments.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003984 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003985 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003986 ((method_type == METHOD_SUPER ||
3987 method_type == METHOD_VIRTUAL ||
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003988 method_type == METHOD_INTERFACE) && res_method->IsDirect()) ||
3989 ((method_type == METHOD_POLYMORPHIC) &&
3990 (!res_method->IsNative() || !res_method->IsVarargs()))) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003991 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003992 "type of " << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003993 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003994 }
Alex Lightafb66472017-08-01 09:54:49 -07003995 // Make sure we weren't expecting to fail.
3996 DCHECK(!must_fail) << "invoke type (" << method_type << ")"
3997 << klass->PrettyDescriptor() << "."
3998 << dex_file_->GetMethodName(method_id) << " "
3999 << dex_file_->GetMethodSignature(method_id) << " unexpectedly resolved to "
4000 << res_method->PrettyMethod() << " without error. Initially this method was "
4001 << "not found so we were expecting to fail for some reason.";
jeffhao8cd6dda2012-02-22 10:15:34 -08004002 return res_method;
4003}
4004
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004005template <bool kVerifierDebug>
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004006template <class T>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004007ArtMethod* MethodVerifier<kVerifierDebug>::VerifyInvocationArgsFromIterator(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004008 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampee383d232018-06-19 12:29:51 -07004009 DCHECK_EQ(!is_range, inst->HasVarArgs());
4010
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004011 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4012 // match the call to the signature. Also, we might be calling through an abstract method
4013 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004014 const size_t expected_args = inst->VRegA();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004015 /* caught by static verifier */
4016 DCHECK(is_range || expected_args <= 5);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004017
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004018 if (expected_args > code_item_accessor_.OutsSize()) {
Orion Hodson1cda7c22017-08-10 13:06:45 +01004019 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004020 << ") exceeds outsSize ("
4021 << code_item_accessor_.OutsSize() << ")";
Orion Hodson1cda7c22017-08-10 13:06:45 +01004022 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004023 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004024
4025 /*
4026 * Check the "this" argument, which must be an instance of the class that declared the method.
4027 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4028 * rigorous check here (which is okay since we have to do it at runtime).
4029 */
4030 if (method_type != METHOD_STATIC) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004031 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004032 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Andreas Gampe43884b22019-06-27 14:05:52 -07004033 CHECK(flags_.have_pending_hard_failure_);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004034 return nullptr;
4035 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004036 bool is_init = false;
4037 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004038 if (res_method) {
4039 if (!res_method->IsConstructor()) {
4040 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
4041 return nullptr;
4042 }
4043 } else {
4044 // Check whether the name of the called method is "<init>"
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004045 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Jeff Hao0d087272014-08-04 14:47:17 -07004046 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004047 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
4048 return nullptr;
4049 }
4050 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004051 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004052 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00004053 const RegType& adjusted_type = is_init
4054 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
4055 : actual_arg_type;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004056 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZeroOrNull()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004057 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07004058 // Miranda methods have the declaring interface as their declaring class, not the abstract
4059 // class. It would be wrong to use this for the type check (interface type checks are
4060 // postponed to runtime).
4061 if (res_method != nullptr && !res_method->IsMiranda()) {
Vladimir Markod93e3742018-07-18 10:58:13 +01004062 ObjPtr<mirror::Class> klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004063 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07004064 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
4065 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004066 } else {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004067 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004068 const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07004069 res_method_class = &reg_types_.FromDescriptor(
Andreas Gampe51de69e2019-04-19 15:14:14 -07004070 class_loader_.Get(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07004071 dex_file_->StringByTypeIdx(class_idx),
4072 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004073 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004074 if (!res_method_class->IsAssignableFrom(adjusted_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004075 Fail(adjusted_type.IsUnresolvedTypes()
4076 ? VERIFY_ERROR_NO_CLASS
4077 : VERIFY_ERROR_BAD_CLASS_SOFT)
4078 << "'this' argument '" << actual_arg_type << "' not instance of '"
4079 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004080 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4081 // the compiler.
Andreas Gampe43884b22019-06-27 14:05:52 -07004082 if (flags_.have_pending_hard_failure_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004083 return nullptr;
4084 }
4085 }
4086 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004087 }
4088
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004089 uint32_t arg[5];
4090 if (!is_range) {
4091 inst->GetVarArgs(arg);
4092 }
4093 uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004094 for ( ; it->HasNext(); it->Next()) {
4095 if (sig_registers >= expected_args) {
4096 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004097 " argument registers, method signature has " << sig_registers + 1 << " or more";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004098 return nullptr;
4099 }
4100
4101 const char* param_descriptor = it->GetDescriptor();
4102
4103 if (param_descriptor == nullptr) {
4104 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
4105 "component";
4106 return nullptr;
4107 }
4108
Andreas Gampe51de69e2019-04-19 15:14:14 -07004109 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_.Get(),
4110 param_descriptor,
4111 false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004112 uint32_t get_reg = is_range ? inst->VRegC() + static_cast<uint32_t>(sig_registers) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004113 arg[sig_registers];
4114 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004115 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004116 if (!src_type.IsIntegralTypes()) {
4117 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
4118 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07004119 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004120 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07004121 } else {
4122 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
4123 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4124 // the compiler.
Andreas Gampe43884b22019-06-27 14:05:52 -07004125 if (flags_.have_pending_hard_failure_) {
Andreas Gampeda9badb2015-06-05 20:22:12 -07004126 return nullptr;
4127 }
4128 } else if (reg_type.IsLongOrDoubleTypes()) {
4129 // Check that registers are consecutive (for non-range invokes). Invokes are the only
4130 // instructions not specifying register pairs by the first component, but require them
4131 // nonetheless. Only check when there's an actual register in the parameters. If there's
4132 // none, this will fail below.
4133 if (!is_range && sig_registers + 1 < expected_args) {
4134 uint32_t second_reg = arg[sig_registers + 1];
4135 if (second_reg != get_reg + 1) {
4136 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
4137 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
4138 << second_reg << ".";
4139 return nullptr;
4140 }
4141 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004142 }
4143 }
4144 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
4145 }
4146 if (expected_args != sig_registers) {
4147 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004148 " argument registers, method signature has " << sig_registers;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004149 return nullptr;
4150 }
4151 return res_method;
4152}
4153
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004154template <bool kVerifierDebug>
4155void MethodVerifier<kVerifierDebug>::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
4156 MethodType method_type,
4157 bool is_range) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004158 // As the method may not have been resolved, make this static check against what we expect.
4159 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
4160 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004161 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004162 DexFileParameterIterator it(*dex_file_,
4163 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004164 VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004165}
4166
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004167template <bool kVerifierDebug>
4168bool MethodVerifier<kVerifierDebug>::CheckCallSite(uint32_t call_site_idx) {
Orion Hodson3a842f52017-04-21 15:24:10 +01004169 if (call_site_idx >= dex_file_->NumCallSiteIds()) {
4170 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx
4171 << " >= " << dex_file_->NumCallSiteIds();
4172 return false;
4173 }
4174
Orion Hodsonc069a302017-01-18 09:23:12 +00004175 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
Alex Light1e52a072019-06-25 09:12:04 -07004176 // Check essential arguments are provided. The dex file verifier has verified indices of the
Orion Hodsonc069a302017-01-18 09:23:12 +00004177 // main values (method handle, name, method_type).
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004178 static const size_t kRequiredArguments = 3;
4179 if (it.Size() < kRequiredArguments) {
Orion Hodsonc069a302017-01-18 09:23:12 +00004180 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4181 << " has too few arguments: "
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004182 << it.Size() << " < " << kRequiredArguments;
Orion Hodsonc069a302017-01-18 09:23:12 +00004183 return false;
4184 }
4185
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004186 std::pair<const EncodedArrayValueIterator::ValueType, size_t> type_and_max[kRequiredArguments] =
4187 { { EncodedArrayValueIterator::ValueType::kMethodHandle, dex_file_->NumMethodHandles() },
4188 { EncodedArrayValueIterator::ValueType::kString, dex_file_->NumStringIds() },
4189 { EncodedArrayValueIterator::ValueType::kMethodType, dex_file_->NumProtoIds() }
4190 };
4191 uint32_t index[kRequiredArguments];
4192
4193 // Check arguments have expected types and are within permitted ranges.
4194 for (size_t i = 0; i < kRequiredArguments; ++i) {
4195 if (it.GetValueType() != type_and_max[i].first) {
4196 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx
4197 << " argument " << i << " has wrong type "
4198 << it.GetValueType() << "!=" << type_and_max[i].first;
4199 return false;
4200 }
4201 index[i] = static_cast<uint32_t>(it.GetJavaValue().i);
4202 if (index[i] >= type_and_max[i].second) {
4203 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx
4204 << " argument " << i << " bad index "
4205 << index[i] << " >= " << type_and_max[i].second;
4206 return false;
4207 }
4208 it.Next();
Orion Hodsona5dca522018-02-27 12:42:11 +00004209 }
Orion Hodson3a842f52017-04-21 15:24:10 +01004210
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004211 // Check method handle kind is valid.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004212 const dex::MethodHandleItem& mh = dex_file_->GetMethodHandle(index[0]);
Orion Hodsonc069a302017-01-18 09:23:12 +00004213 if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) {
4214 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
Orion Hodson631827d2017-04-10 14:53:47 +01004215 << " argument 0 method handle type is not InvokeStatic: "
4216 << mh.method_handle_type_;
Orion Hodsonc069a302017-01-18 09:23:12 +00004217 return false;
4218 }
Orion Hodsonc069a302017-01-18 09:23:12 +00004219 return true;
4220}
4221
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004222class MethodParamListDescriptorIterator {
4223 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004224 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004225 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
4226 params_size_(params_ == nullptr ? 0 : params_->Size()) {
4227 }
4228
4229 bool HasNext() {
4230 return pos_ < params_size_;
4231 }
4232
4233 void Next() {
4234 ++pos_;
4235 }
4236
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004237 const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004238 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
4239 }
4240
4241 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004242 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004243 size_t pos_;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004244 const dex::TypeList* params_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004245 const size_t params_size_;
4246};
4247
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004248template <bool kVerifierDebug>
4249ArtMethod* MethodVerifier<kVerifierDebug>::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08004250 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08004251 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
4252 // we're making.
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004253 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Alex Light7268d472016-01-20 15:50:01 -08004254 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004255 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004256 // Check what we can statically.
Andreas Gampe43884b22019-06-27 14:05:52 -07004257 if (!flags_.have_pending_hard_failure_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004258 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
4259 }
4260 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08004261 }
4262
Ian Rogersd81871c2011-10-03 13:57:23 -07004263 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07004264 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08004265 if (method_type == METHOD_SUPER) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004266 dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
David Brazdilca3c8c32016-09-06 14:04:48 +01004267 const RegType& reference_type = reg_types_.FromDescriptor(
Andreas Gampe51de69e2019-04-19 15:14:14 -07004268 class_loader_.Get(),
David Brazdilca3c8c32016-09-06 14:04:48 +01004269 dex_file_->StringByTypeIdx(class_idx),
4270 false);
4271 if (reference_type.IsUnresolvedTypes()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004272 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4273 return nullptr;
4274 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004275 if (reference_type.GetClass()->IsInterface()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004276 // TODO Can we verify anything else.
David Brazdil15fc7292016-09-02 14:13:18 +01004277 if (class_idx == class_def_.class_idx_) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004278 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
Alex Light55ea94d2016-03-15 09:50:26 -07004279 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -08004280 }
4281 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4282 // does.
Alex Light55ea94d2016-03-15 09:50:26 -07004283 if (!GetDeclaringClass().HasClass()) {
4284 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4285 << "interface invoke-super";
4286 return nullptr;
David Brazdilca3c8c32016-09-06 14:04:48 +01004287 } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004288 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07004289 << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass())
4290 << " in method "
4291 << dex_file_->PrettyMethod(dex_method_idx_) << " to method "
4292 << dex_file_->PrettyMethod(method_idx) << " references "
4293 << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass());
Alex Lightfedd91d2016-01-07 14:49:16 -08004294 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004295 }
4296 } else {
4297 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4298 if (super.IsUnresolvedTypes()) {
4299 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004300 << dex_file_->PrettyMethod(dex_method_idx_)
4301 << " to super " << res_method->PrettyMethod();
Alex Light705ad492015-09-21 11:36:30 -07004302 return nullptr;
4303 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004304 if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) ||
Aart Bikf663e342016-04-04 17:28:59 -07004305 (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Alex Light705ad492015-09-21 11:36:30 -07004306 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004307 << dex_file_->PrettyMethod(dex_method_idx_)
Alex Light705ad492015-09-21 11:36:30 -07004308 << " to super " << super
4309 << "." << res_method->GetName()
4310 << res_method->GetSignature();
4311 return nullptr;
4312 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004313 }
4314 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004315
Andreas Gampe74979b12017-05-16 09:28:06 -07004316 if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004317 // Process the signature of the calling site that is invoking the method handle.
Orion Hodson06d10a72018-05-14 08:53:38 +01004318 dex::ProtoIndex proto_idx(inst->VRegH());
4319 DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(proto_idx));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004320 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4321 } else {
4322 // Process the target method's signature.
4323 MethodParamListDescriptorIterator it(res_method);
4324 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4325 }
4326}
4327
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004328template <bool kVerifierDebug>
4329bool MethodVerifier<kVerifierDebug>::CheckSignaturePolymorphicMethod(ArtMethod* method) {
Vladimir Markod93e3742018-07-18 10:58:13 +01004330 ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004331 const char* method_name = method->GetName();
4332
4333 const char* expected_return_descriptor;
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004334 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots =
4335 Runtime::Current()->GetClassLinker()->GetClassRoots();
4336 if (klass == GetClassRoot<mirror::MethodHandle>(class_roots)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00004337 expected_return_descriptor = mirror::MethodHandle::GetReturnTypeDescriptor(method_name);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004338 } else if (klass == GetClassRoot<mirror::VarHandle>(class_roots)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00004339 expected_return_descriptor = mirror::VarHandle::GetReturnTypeDescriptor(method_name);
4340 } else {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004341 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004342 << "Signature polymorphic method in unsuppported class: " << klass->PrettyDescriptor();
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004343 return false;
4344 }
4345
Orion Hodsonfe92d122018-01-02 10:45:17 +00004346 if (expected_return_descriptor == nullptr) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004347 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4348 << "Signature polymorphic method name invalid: " << method_name;
4349 return false;
4350 }
4351
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004352 const dex::TypeList* types = method->GetParameterTypeList();
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004353 if (types->Size() != 1) {
4354 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4355 << "Signature polymorphic method has too many arguments " << types->Size() << " != 1";
4356 return false;
4357 }
4358
4359 const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_;
4360 const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index);
4361 if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) {
4362 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4363 << "Signature polymorphic method has unexpected argument type: " << argument_descriptor;
4364 return false;
4365 }
4366
4367 const char* return_descriptor = method->GetReturnTypeDescriptor();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004368 if (strcmp(return_descriptor, expected_return_descriptor) != 0) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004369 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004370 << "Signature polymorphic method has unexpected return type: " << return_descriptor
4371 << " != " << expected_return_descriptor;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004372 return false;
4373 }
4374
4375 return true;
4376}
4377
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004378template <bool kVerifierDebug>
4379bool MethodVerifier<kVerifierDebug>::CheckSignaturePolymorphicReceiver(const Instruction* inst) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004380 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004381 if (this_type.IsZeroOrNull()) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004382 /* null pointer always passes (and always fails at run time) */
4383 return true;
4384 } else if (!this_type.IsNonZeroReferenceTypes()) {
4385 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4386 << "invoke-polymorphic receiver is not a reference: "
4387 << this_type;
4388 return false;
4389 } else if (this_type.IsUninitializedReference()) {
4390 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4391 << "invoke-polymorphic receiver is uninitialized: "
4392 << this_type;
4393 return false;
4394 } else if (!this_type.HasClass()) {
4395 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4396 << "invoke-polymorphic receiver has no class: "
4397 << this_type;
4398 return false;
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004399 } else {
4400 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots =
4401 Runtime::Current()->GetClassLinker()->GetClassRoots();
4402 if (!this_type.GetClass()->IsSubClass(GetClassRoot<mirror::MethodHandle>(class_roots)) &&
4403 !this_type.GetClass()->IsSubClass(GetClassRoot<mirror::VarHandle>(class_roots))) {
4404 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4405 << "invoke-polymorphic receiver is not a subclass of MethodHandle or VarHandle: "
4406 << this_type;
4407 return false;
4408 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004409 }
4410 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -07004411}
4412
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004413template <bool kVerifierDebug>
4414uint16_t MethodVerifier<kVerifierDebug>::GetMethodIdxOfInvoke(const Instruction* inst) {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004415 switch (inst->Opcode()) {
4416 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
4417 case Instruction::INVOKE_VIRTUAL_QUICK: {
4418 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
4419 << dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_;
4420 DCHECK(method_being_verified_ != nullptr);
4421 uint16_t method_idx = method_being_verified_->GetIndexFromQuickening(work_insn_idx_);
4422 CHECK_NE(method_idx, DexFile::kDexNoIndex16);
4423 return method_idx;
4424 }
4425 default: {
4426 return inst->VRegB();
4427 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004428 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004429}
4430
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004431template <bool kVerifierDebug>
4432uint16_t MethodVerifier<kVerifierDebug>::GetFieldIdxOfFieldAccess(const Instruction* inst,
4433 bool is_static) {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004434 if (is_static) {
4435 return inst->VRegB_21c();
4436 } else if (inst->IsQuickened()) {
4437 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
4438 DCHECK(method_being_verified_ != nullptr);
4439 uint16_t field_idx = method_being_verified_->GetIndexFromQuickening(work_insn_idx_);
4440 CHECK_NE(field_idx, DexFile::kDexNoIndex16);
4441 return field_idx;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004442 } else {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004443 return inst->VRegC_22c();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004444 }
4445}
4446
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004447template <bool kVerifierDebug>
4448void MethodVerifier<kVerifierDebug>::VerifyNewArray(const Instruction* inst,
4449 bool is_filled,
4450 bool is_range) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004451 dex::TypeIndex type_idx;
Sebastien Hertz5243e912013-05-21 10:55:07 +02004452 if (!is_filled) {
4453 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004454 type_idx = dex::TypeIndex(inst->VRegC_22c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004455 } else if (!is_range) {
4456 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004457 type_idx = dex::TypeIndex(inst->VRegB_35c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004458 } else {
4459 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004460 type_idx = dex::TypeIndex(inst->VRegB_3rc());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004461 }
Andreas Gampe98be1a92017-08-28 08:25:45 -07004462 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004463 if (res_type.IsConflict()) { // bad class
4464 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004465 } else {
4466 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4467 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004468 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004469 } else if (!is_filled) {
4470 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004471 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004472 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004473 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004474 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004475 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004476 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004477 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4478 // the list and fail. It's legal, if silly, for arg_count to be zero.
Andreas Gampe51de69e2019-04-19 15:14:14 -07004479 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_.Get());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004480 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4481 uint32_t arg[5];
4482 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004483 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004484 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004485 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004486 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004487 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4488 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004489 return;
4490 }
4491 }
4492 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004493 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004494 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004495 }
4496 }
4497}
4498
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004499template <bool kVerifierDebug>
4500void MethodVerifier<kVerifierDebug>::VerifyAGet(const Instruction* inst,
4501 const RegType& insn_type,
4502 bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004503 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004504 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004505 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004506 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004507 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004508 if (array_type.IsZeroOrNull()) {
Ian Rogers89310de2012-02-01 13:47:30 -08004509 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
Andreas Gampe52f205a2017-12-01 12:16:07 -08004510 // instruction type.
4511 if (!is_primitive) {
4512 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Null());
4513 } else if (insn_type.IsInteger()) {
4514 // Pick a non-zero constant (to distinguish with null) that can fit in any primitive.
4515 // We cannot use 'insn_type' as it could be a float array or an int array.
4516 work_line_->SetRegisterType<LockOp::kClear>(
4517 this, inst->VRegA_23x(), DetermineCat1Constant(1, need_precise_constants_));
4518 } else if (insn_type.IsCategory1Types()) {
4519 // Category 1
4520 // The 'insn_type' is exactly the type we need.
4521 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07004522 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004523 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004524 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4525 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004526 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004527 }
jeffhaofc3144e2012-02-01 17:21:15 -08004528 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004529 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004530 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004531 // Unresolved array types must be reference array types.
4532 if (is_primitive) {
4533 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4534 << " source for category 1 aget";
4535 } else {
4536 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4537 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004538 // Approximate with java.lang.Object[].
4539 work_line_->SetRegisterType<LockOp::kClear>(this,
4540 inst->VRegA_23x(),
4541 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004542 }
Ian Rogers89310de2012-02-01 13:47:30 -08004543 } else {
4544 /* verify the class */
Andreas Gampe51de69e2019-04-19 15:14:14 -07004545 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_.Get());
jeffhaofc3144e2012-02-01 17:21:15 -08004546 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004547 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004548 << " source for aget-object";
4549 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004550 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004551 << " source for category 1 aget";
4552 } else if (is_primitive && !insn_type.Equals(component_type) &&
4553 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004554 (insn_type.IsLong() && component_type.IsDouble()))) {
4555 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4556 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004557 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004558 // Use knowledge of the field type which is stronger than the type inferred from the
4559 // instruction, which can't differentiate object types and ints from floats, longs from
4560 // doubles.
4561 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004562 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004563 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004564 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004565 component_type.HighHalf(&reg_types_));
4566 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004567 }
4568 }
4569 }
4570}
4571
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004572template <bool kVerifierDebug>
4573void MethodVerifier<kVerifierDebug>::VerifyPrimitivePut(const RegType& target_type,
4574 const RegType& insn_type,
4575 const uint32_t vregA) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004576 // Primitive assignability rules are weaker than regular assignability rules.
4577 bool instruction_compatible;
4578 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004579 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004580 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004581 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004582 value_compatible = value_type.IsIntegralTypes();
4583 } else if (target_type.IsFloat()) {
4584 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4585 value_compatible = value_type.IsFloatTypes();
4586 } else if (target_type.IsLong()) {
4587 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004588 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4589 // as target_type depends on the resolved type of the field.
4590 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004591 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004592 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4593 } else {
4594 value_compatible = false;
4595 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004596 } else if (target_type.IsDouble()) {
4597 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004598 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4599 // as target_type depends on the resolved type of the field.
4600 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004601 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004602 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4603 } else {
4604 value_compatible = false;
4605 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004606 } else {
4607 instruction_compatible = false; // reference with primitive store
4608 value_compatible = false; // unused
4609 }
4610 if (!instruction_compatible) {
4611 // This is a global failure rather than a class change failure as the instructions and
4612 // the descriptors for the type should have been consistent within the same file at
4613 // compile time.
4614 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4615 << "' but expected type '" << target_type << "'";
4616 return;
4617 }
4618 if (!value_compatible) {
4619 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4620 << " of type " << value_type << " but expected " << target_type << " for put";
4621 return;
4622 }
4623}
4624
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004625template <bool kVerifierDebug>
4626void MethodVerifier<kVerifierDebug>::VerifyAPut(const Instruction* inst,
4627 const RegType& insn_type,
4628 bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004629 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004630 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004631 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004632 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004633 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004634 if (array_type.IsZeroOrNull()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004635 // Null array type; this code path will fail at runtime.
4636 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004637 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4638 // and fits multiple register types.
4639 const RegType* modified_reg_type = &insn_type;
4640 if ((modified_reg_type == &reg_types_.Integer()) ||
4641 (modified_reg_type == &reg_types_.LongLo())) {
4642 // May be integer or float | long or double. Overwrite insn_type accordingly.
4643 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4644 if (modified_reg_type == &reg_types_.Integer()) {
4645 if (&value_type == &reg_types_.Float()) {
4646 modified_reg_type = &value_type;
4647 }
4648 } else {
4649 if (&value_type == &reg_types_.DoubleLo()) {
4650 modified_reg_type = &value_type;
4651 }
4652 }
4653 }
4654 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004655 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004656 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004657 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004658 // Unresolved array types must be reference array types.
4659 if (is_primitive) {
4660 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4661 << "' but unresolved type '" << array_type << "'";
4662 } else {
4663 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4664 << " because of missing class";
4665 }
Ian Rogers89310de2012-02-01 13:47:30 -08004666 } else {
Andreas Gampe51de69e2019-04-19 15:14:14 -07004667 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_.Get());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004668 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004669 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004670 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004671 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004672 if (!component_type.IsReferenceTypes()) {
4673 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4674 << " source for aput-object";
4675 } else {
4676 // The instruction agrees with the type of array, confirm the value to be stored does too
4677 // Note: we use the instruction type (rather than the component type) for aput-object as
4678 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004679 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004680 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004681 }
4682 }
4683 }
4684}
4685
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004686template <bool kVerifierDebug>
4687ArtField* MethodVerifier<kVerifierDebug>::GetStaticField(int field_idx) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004688 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Ian Rogers90040192011-12-16 08:54:29 -08004689 // Check access to class
Andreas Gampe98be1a92017-08-28 08:25:45 -07004690 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004691 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004692 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4693 field_idx, dex_file_->GetFieldName(field_id),
4694 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004695 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004696 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004697 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004698 // Accessibility checks depend on resolved fields.
David Brazdil2bb2fbd2018-11-13 18:24:26 +00004699 DCHECK(klass_type.Equals(GetDeclaringClass()) ||
4700 !failures_.empty() ||
4701 IsSdkVersionSetAndLessThan(api_level_, SdkVersion::kP));
Andreas Gampe629be512017-08-25 17:09:32 -07004702
Ian Rogers7b078e82014-09-10 14:44:24 -07004703 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004704 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004705 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004706 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004707
4708 // Record result of the field resolution attempt.
4709 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4710
Ian Rogers7b078e82014-09-10 14:44:24 -07004711 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004712 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004713 << dex_file_->GetFieldName(field_id) << ") in "
4714 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004715 DCHECK(self_->IsExceptionPending());
4716 self_->ClearException();
4717 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004718 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4719 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004720 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004721 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004722 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004723 } else if (!field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004724 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004725 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004726 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004727 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004728}
4729
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004730template <bool kVerifierDebug>
4731ArtField* MethodVerifier<kVerifierDebug>::GetInstanceField(const RegType& obj_type, int field_idx) {
Andreas Gampeb34981b2019-05-06 13:00:40 -07004732 if (!obj_type.IsZeroOrNull() && !obj_type.IsReferenceTypes()) {
4733 // Trying to read a field from something that isn't a reference.
4734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4735 << "non-reference type " << obj_type;
4736 return nullptr;
4737 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004738 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Aart Bik31883642016-06-06 15:02:44 -07004739 // Check access to class.
Andreas Gampe98be1a92017-08-28 08:25:45 -07004740 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004741 if (klass_type.IsConflict()) {
4742 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4743 field_idx, dex_file_->GetFieldName(field_id),
4744 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004745 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004746 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004747 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004748 // Accessibility checks depend on resolved fields.
David Brazdil2bb2fbd2018-11-13 18:24:26 +00004749 DCHECK(klass_type.Equals(GetDeclaringClass()) ||
4750 !failures_.empty() ||
4751 IsSdkVersionSetAndLessThan(api_level_, SdkVersion::kP));
Andreas Gampe629be512017-08-25 17:09:32 -07004752
Ian Rogers7b078e82014-09-10 14:44:24 -07004753 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004754 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004755 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004756 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004757
4758 // Record result of the field resolution attempt.
4759 VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
4760
Ian Rogers7b078e82014-09-10 14:44:24 -07004761 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004762 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004763 << dex_file_->GetFieldName(field_id) << ") in "
4764 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004765 DCHECK(self_->IsExceptionPending());
4766 self_->ClearException();
4767 return nullptr;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004768 } else if (obj_type.IsZeroOrNull()) {
Aart Bik31883642016-06-06 15:02:44 -07004769 // Cannot infer and check type, however, access will cause null pointer exception.
4770 // Fall through into a few last soft failure checks below.
Ian Rogerse1758fe2012-04-19 11:31:15 -07004771 } else {
David Brazdil0d638bb2016-07-27 15:29:25 +01004772 std::string temp;
Mathieu Chartier3398c782016-09-30 10:27:43 -07004773 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004774 const RegType& field_klass =
Vladimir Markobcf17522018-06-01 13:14:32 +01004775 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004776 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004777 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004778 // the field is declared in this class.
4779 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4780 // appear in constructors.
4781 if (!obj_type.IsUninitializedThisReference() ||
4782 !IsConstructor() ||
4783 !field_klass.Equals(GetDeclaringClass())) {
David Sehr709b0702016-10-13 09:12:37 -07004784 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField()
David Brazdil68b5c0b2016-01-19 14:25:29 +00004785 << " of a not fully initialized object within the context"
David Sehr709b0702016-10-13 09:12:37 -07004786 << " of " << dex_file_->PrettyMethod(dex_method_idx_);
David Brazdil68b5c0b2016-01-19 14:25:29 +00004787 return nullptr;
4788 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004789 } else if (!field_klass.IsAssignableFrom(obj_type, this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004790 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4791 // of C1. For resolution to occur the declared class of the field must be compatible with
4792 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004793 VerifyError type;
4794 bool is_aot = Runtime::Current()->IsAotCompiler();
4795 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4796 // Compiler & unresolved types involved, retry at runtime.
4797 type = VerifyError::VERIFY_ERROR_NO_CLASS;
4798 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004799 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4800 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004801 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4802 }
David Sehr709b0702016-10-13 09:12:37 -07004803 Fail(type) << "cannot access instance field " << field->PrettyField()
Andreas Gampe66596242016-04-14 10:55:04 -07004804 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004805 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004806 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004807 }
Aart Bik31883642016-06-06 15:02:44 -07004808
4809 // Few last soft failure checks.
4810 if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4811 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004812 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004813 << " from " << GetDeclaringClass();
4814 return nullptr;
4815 } else if (field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004816 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004817 << " to not be static";
4818 return nullptr;
4819 }
4820
4821 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004822}
4823
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004824template <bool kVerifierDebug>
4825template <FieldAccessType kAccType>
4826void MethodVerifier<kVerifierDebug>::VerifyISFieldAccess(const Instruction* inst,
4827 const RegType& insn_type,
4828 bool is_primitive,
4829 bool is_static) {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004830 uint32_t field_idx = GetFieldIdxOfFieldAccess(inst, is_static);
Mathieu Chartierc7853442015-03-27 14:35:38 -07004831 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004832 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004833 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004834 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004835 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004836
4837 // One is not allowed to access fields on uninitialized references, except to write to
4838 // fields in the constructor (before calling another constructor).
4839 // GetInstanceField does an assignability check which will fail for uninitialized types.
4840 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4841 // checks at the same time that we're verifying a constructor).
4842 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4843 object_type.IsUninitializedThisReference();
4844 const RegType& adjusted_type = should_adjust
4845 ? GetRegTypeCache()->FromUninitialized(object_type)
4846 : object_type;
4847 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe43884b22019-06-27 14:05:52 -07004848 if (UNLIKELY(flags_.have_pending_hard_failure_)) {
Andreas Gampe896df402014-10-20 22:25:29 -07004849 return;
4850 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08004851 if (should_adjust) {
4852 if (field == nullptr) {
4853 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
4854 << "to the superclass being initialized in "
David Sehr709b0702016-10-13 09:12:37 -07004855 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004856 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4857 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
David Sehr709b0702016-10-13 09:12:37 -07004858 << field->PrettyField() << " of a not fully initialized "
Alex Light4a2c8fc2016-02-12 11:01:54 -08004859 << "object within the context of "
David Sehr709b0702016-10-13 09:12:37 -07004860 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004861 return;
4862 }
4863 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004864 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004865 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004866 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004867 if (kAccType == FieldAccessType::kAccPut) {
4868 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07004869 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Andreas Gampe896df402014-10-20 22:25:29 -07004870 << " from other class " << GetDeclaringClass();
Aart Bikc2bc2652016-05-23 14:58:49 -07004871 // Keep hunting for possible hard fails.
Andreas Gampe896df402014-10-20 22:25:29 -07004872 }
4873 }
4874
Mathieu Chartier3398c782016-09-30 10:27:43 -07004875 ObjPtr<mirror::Class> field_type_class =
Vladimir Marko208f6702017-12-08 12:00:50 +00004876 can_load_classes_ ? field->ResolveType() : field->LookupResolvedType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004877 if (field_type_class != nullptr) {
Mathieu Chartier3398c782016-09-30 10:27:43 -07004878 field_type = &FromClass(field->GetTypeDescriptor(),
Vladimir Markobcf17522018-06-01 13:14:32 +01004879 field_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004880 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004881 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004882 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4883 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004884 }
David Brazdil2bb2fbd2018-11-13 18:24:26 +00004885 } else if (IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP)) {
Andreas Gampe7da4c402017-08-25 11:30:48 -07004886 // If we don't have the field (it seems we failed resolution) and this is a PUT, we need to
4887 // redo verification at runtime as the field may be final, unless the field id shows it's in
4888 // the same class.
4889 //
4890 // For simplicity, it is OK to not distinguish compile-time vs runtime, and post this an
4891 // ACCESS_FIELD failure at runtime. This has the same effect as NO_FIELD - punting the class
4892 // to the access-checks interpreter.
4893 //
4894 // Note: see b/34966607. This and above may be changed in the future.
4895 if (kAccType == FieldAccessType::kAccPut) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004896 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Andreas Gampe7da4c402017-08-25 11:30:48 -07004897 const char* field_class_descriptor = dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Andreas Gampe51de69e2019-04-19 15:14:14 -07004898 const RegType* field_class_type = &reg_types_.FromDescriptor(class_loader_.Get(),
Andreas Gampe7da4c402017-08-25 11:30:48 -07004899 field_class_descriptor,
4900 false);
4901 if (!field_class_type->Equals(GetDeclaringClass())) {
4902 Fail(VERIFY_ERROR_ACCESS_FIELD) << "could not check field put for final field modify of "
4903 << field_class_descriptor
4904 << "."
4905 << dex_file_->GetFieldName(field_id)
4906 << " from other class "
4907 << GetDeclaringClass();
4908 }
4909 }
Ian Rogers0d604842012-04-16 14:50:24 -07004910 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004911 if (field_type == nullptr) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004912 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004913 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Andreas Gampe51de69e2019-04-19 15:14:14 -07004914 field_type = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004915 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004916 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004917 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004918 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4919 "Unexpected third access type");
4920 if (kAccType == FieldAccessType::kAccPut) {
4921 // sput or iput.
4922 if (is_primitive) {
4923 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004924 } 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 put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004935 return;
4936 }
4937 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004938 }
Andreas Gampe896df402014-10-20 22:25:29 -07004939 } else if (kAccType == FieldAccessType::kAccGet) {
4940 // sget or iget.
4941 if (is_primitive) {
4942 if (field_type->Equals(insn_type) ||
4943 (field_type->IsFloat() && insn_type.IsInteger()) ||
4944 (field_type->IsDouble() && insn_type.IsLong())) {
4945 // expected that read is of the correct primitive type or that int reads are reading
4946 // floats or long reads are reading doubles
4947 } else {
4948 // This is a global failure rather than a class change failure as the instructions and
4949 // the descriptors for the type should have been consistent within the same file at
4950 // compile time
David Sehr709b0702016-10-13 09:12:37 -07004951 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07004952 << " to be of type '" << insn_type
4953 << "' but found type '" << *field_type << "' in get";
4954 return;
4955 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004956 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004957 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004958 // If the field type is not a reference, this is a global failure rather than
4959 // a class change failure as the instructions and the descriptors for the type
4960 // should have been consistent within the same file at compile time.
4961 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4962 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004963 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004964 << " to be compatible with type '" << insn_type
4965 << "' but found type '" << *field_type
4966 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004967 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004968 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004969 }
Andreas Gampe896df402014-10-20 22:25:29 -07004970 return;
4971 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004972 }
Andreas Gampe896df402014-10-20 22:25:29 -07004973 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004974 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004975 } else {
4976 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4977 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004978 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004979 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004980 }
4981}
4982
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004983template <bool kVerifierDebug>
4984bool MethodVerifier<kVerifierDebug>::UpdateRegisters(uint32_t next_insn,
4985 RegisterLine* merge_line,
4986 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004987 bool changed = true;
4988 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004989 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004990 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004991 * We haven't processed this instruction before, and we haven't touched the registers here, so
4992 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4993 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004994 */
Andreas Gampea727e372015-08-25 09:22:37 -07004995 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004996 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004997 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004998 merge_line->VerifyMonitorStackEmpty(this);
4999
Ian Rogersb8c78592013-07-25 23:52:52 +00005000 // For returns we only care about the operand to the return, all other registers are dead.
5001 // Initialize them as conflicts so they don't add to GC and deoptimization information.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08005002 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07005003 AdjustReturnLine(this, ret_inst, target_line);
Aart Bik31883642016-06-06 15:02:44 -07005004 // Directly bail if a hard failure was found.
Andreas Gampe43884b22019-06-27 14:05:52 -07005005 if (flags_.have_pending_hard_failure_) {
Aart Bikb0526322016-06-01 14:06:00 -07005006 return false;
5007 }
Ian Rogersb8c78592013-07-25 23:52:52 +00005008 }
jeffhaobdb76512011-09-07 11:43:16 -07005009 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08005010 RegisterLineArenaUniquePtr copy;
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005011 if (kVerifierDebug) {
Andreas Gamped09c0592019-04-19 15:44:05 -07005012 copy.reset(RegisterLine::Create(target_line->NumRegs(), allocator_, GetRegTypeCache()));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08005013 copy->CopyFromLine(target_line);
5014 }
Ian Rogers7b078e82014-09-10 14:44:24 -07005015 changed = target_line->MergeRegisters(this, merge_line);
Andreas Gampe43884b22019-06-27 14:05:52 -07005016 if (flags_.have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07005017 return false;
jeffhaobdb76512011-09-07 11:43:16 -07005018 }
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005019 if (kVerifierDebug && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07005020 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07005021 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07005022 << copy->Dump(this) << " MERGE\n"
5023 << merge_line->Dump(this) << " ==\n"
Andreas Gampe2ad6cce2019-04-11 16:17:39 -07005024 << target_line->Dump(this);
jeffhaobdb76512011-09-07 11:43:16 -07005025 }
Ian Rogersebbdd872014-07-07 23:53:08 -07005026 if (update_merge_line && changed) {
5027 merge_line->CopyFromLine(target_line);
5028 }
jeffhaobdb76512011-09-07 11:43:16 -07005029 }
Ian Rogersd81871c2011-10-03 13:57:23 -07005030 if (changed) {
Andreas Gampe51de69e2019-04-19 15:14:14 -07005031 GetModifiableInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07005032 }
5033 return true;
5034}
5035
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005036template <bool kVerifierDebug>
5037const RegType& MethodVerifier<kVerifierDebug>::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005038 if (return_type_ == nullptr) {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00005039 if (method_being_verified_ != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01005040 ObjPtr<mirror::Class> return_type_class = can_load_classes_
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00005041 ? method_being_verified_->ResolveReturnType()
5042 : method_being_verified_->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005043 if (return_type_class != nullptr) {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00005044 return_type_ = &FromClass(method_being_verified_->GetReturnTypeDescriptor(),
Vladimir Markobcf17522018-06-01 13:14:32 +01005045 return_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07005046 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005047 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07005048 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
5049 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005050 }
5051 }
5052 if (return_type_ == nullptr) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08005053 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
5054 const dex::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -08005055 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005056 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Andreas Gampe51de69e2019-04-19 15:14:14 -07005057 return_type_ = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07005058 }
5059 }
5060 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005061}
5062
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005063template <bool kVerifierDebug>
5064const RegType& MethodVerifier<kVerifierDebug>::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07005065 if (declaring_class_ == nullptr) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08005066 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07005067 const char* descriptor
5068 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00005069 if (method_being_verified_ != nullptr) {
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01005070 ObjPtr<mirror::Class> klass = method_being_verified_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07005071 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07005072 } else {
Andreas Gampe51de69e2019-04-19 15:14:14 -07005073 declaring_class_ = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07005074 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07005075 }
Ian Rogers637c65b2013-05-31 11:46:00 -07005076 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005077}
5078
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005079template <bool kVerifierDebug>
5080const RegType& MethodVerifier<kVerifierDebug>::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01005081 if (precise) {
5082 // Precise constant type.
5083 return reg_types_.FromCat1Const(value, true);
5084 } else {
5085 // Imprecise constant type.
5086 if (value < -32768) {
5087 return reg_types_.IntConstant();
5088 } else if (value < -128) {
5089 return reg_types_.ShortConstant();
5090 } else if (value < 0) {
5091 return reg_types_.ByteConstant();
5092 } else if (value == 0) {
5093 return reg_types_.Zero();
5094 } else if (value == 1) {
5095 return reg_types_.One();
5096 } else if (value < 128) {
5097 return reg_types_.PosByteConstant();
5098 } else if (value < 32768) {
5099 return reg_types_.PosShortConstant();
5100 } else if (value < 65536) {
5101 return reg_types_.CharConstant();
5102 } else {
5103 return reg_types_.IntConstant();
5104 }
5105 }
5106}
5107
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005108template <bool kVerifierDebug>
5109const RegType& MethodVerifier<kVerifierDebug>::FromClass(const char* descriptor,
5110 ObjPtr<mirror::Class> klass,
5111 bool precise) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005112 DCHECK(klass != nullptr);
5113 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
5114 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
5115 << "non-instantiable klass " << descriptor;
5116 precise = false;
5117 }
5118 return reg_types_.FromClass(descriptor, klass, precise);
5119}
5120
Andreas Gampefc25ae92019-04-19 22:22:57 -07005121} // namespace
5122} // namespace impl
5123
5124MethodVerifier::MethodVerifier(Thread* self,
5125 const DexFile* dex_file,
5126 const dex::CodeItem* code_item,
5127 uint32_t dex_method_idx,
5128 bool can_load_classes,
5129 bool allow_thread_suspension,
5130 bool allow_soft_failures)
5131 : self_(self),
5132 arena_stack_(Runtime::Current()->GetArenaPool()),
5133 allocator_(&arena_stack_),
5134 reg_types_(can_load_classes, allocator_, allow_thread_suspension),
5135 reg_table_(allocator_),
5136 work_insn_idx_(dex::kDexNoIndex),
5137 dex_method_idx_(dex_method_idx),
5138 dex_file_(dex_file),
5139 code_item_accessor_(*dex_file, code_item),
Andreas Gampe43884b22019-06-27 14:05:52 -07005140 flags_({false, false, false, false}),
Andreas Gampefc25ae92019-04-19 22:22:57 -07005141 encountered_failure_types_(0),
5142 can_load_classes_(can_load_classes),
5143 allow_soft_failures_(allow_soft_failures),
5144 has_check_casts_(false),
5145 link_(nullptr) {
5146 self->PushVerifier(this);
5147}
5148
5149MethodVerifier::~MethodVerifier() {
5150 Thread::Current()->PopVerifier(this);
5151 STLDeleteElements(&failure_messages_);
5152}
5153
5154MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
5155 uint32_t method_idx,
5156 const DexFile* dex_file,
5157 Handle<mirror::DexCache> dex_cache,
5158 Handle<mirror::ClassLoader> class_loader,
5159 const dex::ClassDef& class_def,
5160 const dex::CodeItem* code_item,
5161 ArtMethod* method,
5162 uint32_t method_access_flags,
5163 CompilerCallbacks* callbacks,
5164 bool allow_soft_failures,
5165 HardFailLogMode log_level,
5166 bool need_precise_constants,
5167 uint32_t api_level,
5168 std::string* hard_failure_msg) {
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005169 if (VLOG_IS_ON(verifier_debug)) {
5170 return VerifyMethod<true>(self,
5171 method_idx,
5172 dex_file,
5173 dex_cache,
5174 class_loader,
5175 class_def,
5176 code_item,
5177 method,
5178 method_access_flags,
5179 callbacks,
5180 allow_soft_failures,
5181 log_level,
5182 need_precise_constants,
5183 api_level,
5184 hard_failure_msg);
5185 } else {
5186 return VerifyMethod<false>(self,
5187 method_idx,
5188 dex_file,
5189 dex_cache,
5190 class_loader,
5191 class_def,
5192 code_item,
5193 method,
5194 method_access_flags,
5195 callbacks,
5196 allow_soft_failures,
5197 log_level,
5198 need_precise_constants,
5199 api_level,
5200 hard_failure_msg);
5201 }
5202}
5203
5204template <bool kVerifierDebug>
5205MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
5206 uint32_t method_idx,
5207 const DexFile* dex_file,
5208 Handle<mirror::DexCache> dex_cache,
5209 Handle<mirror::ClassLoader> class_loader,
5210 const dex::ClassDef& class_def,
5211 const dex::CodeItem* code_item,
5212 ArtMethod* method,
5213 uint32_t method_access_flags,
5214 CompilerCallbacks* callbacks,
5215 bool allow_soft_failures,
5216 HardFailLogMode log_level,
5217 bool need_precise_constants,
5218 uint32_t api_level,
5219 std::string* hard_failure_msg) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005220 MethodVerifier::FailureData result;
5221 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
5222
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005223 impl::MethodVerifier<kVerifierDebug> verifier(self,
5224 dex_file,
5225 dex_cache,
5226 class_loader,
5227 class_def,
5228 code_item,
5229 method_idx,
5230 method,
5231 method_access_flags,
5232 /* can_load_classes= */ true,
5233 allow_soft_failures,
5234 need_precise_constants,
5235 /* verify to dump */ false,
5236 /* allow_thread_suspension= */ true,
Alex Lighte2ddce32019-05-22 17:08:35 +00005237 /* fill_register_lines= */ false,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005238 api_level);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005239 if (verifier.Verify()) {
5240 // Verification completed, however failures may be pending that didn't cause the verification
5241 // to hard fail.
Andreas Gampe43884b22019-06-27 14:05:52 -07005242 CHECK(!verifier.flags_.have_pending_hard_failure_);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005243
5244 if (code_item != nullptr && callbacks != nullptr) {
5245 // Let the interested party know that the method was verified.
5246 callbacks->MethodVerified(&verifier);
5247 }
5248
5249 if (verifier.failures_.size() != 0) {
5250 if (VLOG_IS_ON(verifier)) {
5251 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
5252 << dex_file->PrettyMethod(method_idx) << "\n");
5253 }
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005254 if (kVerifierDebug) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005255 LOG(INFO) << verifier.info_messages_.str();
5256 verifier.Dump(LOG_STREAM(INFO));
5257 }
5258 result.kind = FailureKind::kSoftFailure;
5259 if (method != nullptr &&
5260 !CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) {
5261 method->SetDontCompile();
5262 }
5263 }
5264 if (method != nullptr) {
5265 if (verifier.HasInstructionThatWillThrow()) {
5266 method->SetDontCompile();
5267 if (Runtime::Current()->IsAotCompiler() &&
5268 (callbacks != nullptr) && !callbacks->IsBootImage()) {
5269 // When compiling apps, make HasInstructionThatWillThrow a soft error to trigger
5270 // re-verification at runtime.
5271 // The dead code after the throw is not verified and might be invalid. This may cause
5272 // the JIT compiler to crash since it assumes that all the code is valid.
5273 //
5274 // There's a strong assumption that the entire boot image is verified and all its dex
5275 // code is valid (even the dead and unverified one). As such this is done only for apps.
5276 // (CompilerDriver DCHECKs in VerifyClassVisitor that methods from boot image are
5277 // fully verified).
5278 result.kind = FailureKind::kSoftFailure;
5279 }
5280 }
5281 if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) {
5282 method->SetMustCountLocks();
5283 }
5284 }
5285 } else {
5286 // Bad method data.
5287 CHECK_NE(verifier.failures_.size(), 0U);
5288
Andreas Gampe43884b22019-06-27 14:05:52 -07005289 if (UNLIKELY(verifier.flags_.have_pending_experimental_failure_)) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005290 // Failed due to being forced into interpreter. This is ok because
5291 // we just want to skip verification.
5292 result.kind = FailureKind::kSoftFailure;
5293 } else {
Andreas Gampe43884b22019-06-27 14:05:52 -07005294 CHECK(verifier.flags_.have_pending_hard_failure_);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005295 if (VLOG_IS_ON(verifier)) {
5296 log_level = std::max(HardFailLogMode::kLogVerbose, log_level);
5297 }
5298 if (log_level >= HardFailLogMode::kLogVerbose) {
5299 LogSeverity severity;
5300 switch (log_level) {
5301 case HardFailLogMode::kLogVerbose:
5302 severity = LogSeverity::VERBOSE;
5303 break;
5304 case HardFailLogMode::kLogWarning:
5305 severity = LogSeverity::WARNING;
5306 break;
5307 case HardFailLogMode::kLogInternalFatal:
5308 severity = LogSeverity::FATAL_WITHOUT_ABORT;
5309 break;
5310 default:
5311 LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level);
5312 UNREACHABLE();
5313 }
5314 verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in "
5315 << dex_file->PrettyMethod(method_idx)
5316 << "\n");
5317 }
5318 if (hard_failure_msg != nullptr) {
5319 CHECK(!verifier.failure_messages_.empty());
5320 *hard_failure_msg =
5321 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
5322 }
5323 result.kind = FailureKind::kHardFailure;
5324
5325 if (callbacks != nullptr) {
5326 // Let the interested party know that we failed the class.
5327 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(class_def));
5328 callbacks->ClassRejected(ref);
5329 }
5330 }
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005331 if (kVerifierDebug || VLOG_IS_ON(verifier)) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005332 LOG(ERROR) << verifier.info_messages_.str();
5333 verifier.Dump(LOG_STREAM(ERROR));
5334 }
5335 // Under verifier-debug, dump the complete log into the error message.
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005336 if (kVerifierDebug && hard_failure_msg != nullptr) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005337 hard_failure_msg->append("\n");
5338 hard_failure_msg->append(verifier.info_messages_.str());
5339 hard_failure_msg->append("\n");
5340 std::ostringstream oss;
5341 verifier.Dump(oss);
5342 hard_failure_msg->append(oss.str());
5343 }
5344 }
5345 if (kTimeVerifyMethod) {
5346 uint64_t duration_ns = NanoTime() - start_ns;
5347 if (duration_ns > MsToNs(Runtime::Current()->GetVerifierLoggingThresholdMs())) {
Andreas Gampeefdd1b02019-05-07 12:30:10 -07005348 double bytecodes_per_second =
5349 verifier.code_item_accessor_.InsnsSizeInCodeUnits() / (duration_ns * 1e-9);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005350 LOG(WARNING) << "Verification of " << dex_file->PrettyMethod(method_idx)
5351 << " took " << PrettyDuration(duration_ns)
Andreas Gampeefdd1b02019-05-07 12:30:10 -07005352 << (impl::IsLargeMethod(verifier.CodeItem()) ? " (large method)" : "")
Andreas Gamped91f8412019-06-13 10:11:14 -07005353 << " (" << StringPrintf("%.2f", bytecodes_per_second) << " bytecodes/s)"
5354 << " (" << verifier.allocator_.ApproximatePeakBytes()
5355 << "B approximate peak alloc)";
Andreas Gampefc25ae92019-04-19 22:22:57 -07005356 }
5357 }
5358 result.types = verifier.encountered_failure_types_;
5359 return result;
5360}
5361
Alex Lighte2ddce32019-05-22 17:08:35 +00005362MethodVerifier* MethodVerifier::CalculateVerificationInfo(
5363 Thread* self,
5364 ArtMethod* method,
5365 Handle<mirror::DexCache> dex_cache,
5366 Handle<mirror::ClassLoader> class_loader) {
5367 std::unique_ptr<impl::MethodVerifier<false>> verifier(
5368 new impl::MethodVerifier<false>(self,
5369 method->GetDexFile(),
5370 dex_cache,
5371 class_loader,
5372 *method->GetDeclaringClass()->GetClassDef(),
5373 method->GetCodeItem(),
5374 method->GetDexMethodIndex(),
5375 method,
5376 method->GetAccessFlags(),
5377 /* can_load_classes= */ false,
5378 /* allow_soft_failures= */ true,
5379 /* need_precise_constants= */ true,
5380 /* verify_to_dump= */ false,
5381 /* allow_thread_suspension= */ false,
5382 /* fill_register_lines= */ true,
Alex Lightaf52cbe2019-05-22 15:05:09 -07005383 // Just use the verifier at the current skd-version.
5384 // This might affect what soft-verifier errors are reported.
5385 // Callers can then filter out relevant errors if needed.
5386 Runtime::Current()->GetTargetSdkVersion()));
Alex Lighte2ddce32019-05-22 17:08:35 +00005387 verifier->Verify();
5388 if (VLOG_IS_ON(verifier)) {
5389 verifier->DumpFailures(VLOG_STREAM(verifier));
5390 VLOG(verifier) << verifier->info_messages_.str();
5391 verifier->Dump(VLOG_STREAM(verifier));
5392 }
Andreas Gampe43884b22019-06-27 14:05:52 -07005393 if (verifier->flags_.have_pending_hard_failure_) {
Alex Lighte2ddce32019-05-22 17:08:35 +00005394 return nullptr;
5395 } else {
5396 return verifier.release();
5397 }
5398}
5399
Andreas Gampefc25ae92019-04-19 22:22:57 -07005400MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
5401 VariableIndentationOutputStream* vios,
5402 uint32_t dex_method_idx,
5403 const DexFile* dex_file,
5404 Handle<mirror::DexCache> dex_cache,
5405 Handle<mirror::ClassLoader> class_loader,
5406 const dex::ClassDef& class_def,
5407 const dex::CodeItem* code_item,
5408 ArtMethod* method,
5409 uint32_t method_access_flags,
5410 uint32_t api_level) {
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005411 impl::MethodVerifier<false>* verifier = new impl::MethodVerifier<false>(
5412 self,
5413 dex_file,
5414 dex_cache,
5415 class_loader,
5416 class_def,
5417 code_item,
5418 dex_method_idx,
5419 method,
5420 method_access_flags,
5421 /* can_load_classes= */ true,
5422 /* allow_soft_failures= */ true,
5423 /* need_precise_constants= */ true,
5424 /* verify_to_dump= */ true,
5425 /* allow_thread_suspension= */ true,
Alex Lighte2ddce32019-05-22 17:08:35 +00005426 /* fill_register_lines= */ false,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005427 api_level);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005428 verifier->Verify();
5429 verifier->DumpFailures(vios->Stream());
5430 vios->Stream() << verifier->info_messages_.str();
5431 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
5432 // and querying any info is dangerous/can abort.
Andreas Gampe43884b22019-06-27 14:05:52 -07005433 if (verifier->flags_.have_pending_hard_failure_) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005434 delete verifier;
5435 return nullptr;
5436 } else {
5437 verifier->Dump(vios);
5438 return verifier;
5439 }
5440}
5441
5442void MethodVerifier::FindLocksAtDexPc(
5443 ArtMethod* m,
5444 uint32_t dex_pc,
5445 std::vector<MethodVerifier::DexLockInfo>* monitor_enter_dex_pcs,
5446 uint32_t api_level) {
5447 StackHandleScope<2> hs(Thread::Current());
5448 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
5449 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005450 impl::MethodVerifier<false> verifier(hs.Self(),
5451 m->GetDexFile(),
5452 dex_cache,
5453 class_loader,
5454 m->GetClassDef(),
5455 m->GetCodeItem(),
5456 m->GetDexMethodIndex(),
5457 m,
5458 m->GetAccessFlags(),
5459 /* can_load_classes= */ false,
5460 /* allow_soft_failures= */ true,
5461 /* need_precise_constants= */ false,
5462 /* verify_to_dump= */ false,
5463 /* allow_thread_suspension= */ false,
Alex Lighte2ddce32019-05-22 17:08:35 +00005464 /* fill_register_lines= */ false,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005465 api_level);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005466 verifier.interesting_dex_pc_ = dex_pc;
5467 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
5468 verifier.FindLocksAtDexPc();
5469}
5470
5471MethodVerifier* MethodVerifier::CreateVerifier(Thread* self,
5472 const DexFile* dex_file,
5473 Handle<mirror::DexCache> dex_cache,
5474 Handle<mirror::ClassLoader> class_loader,
5475 const dex::ClassDef& class_def,
5476 const dex::CodeItem* code_item,
5477 uint32_t method_idx,
5478 ArtMethod* method,
5479 uint32_t access_flags,
5480 bool can_load_classes,
5481 bool allow_soft_failures,
5482 bool need_precise_constants,
5483 bool verify_to_dump,
5484 bool allow_thread_suspension,
5485 uint32_t api_level) {
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005486 return new impl::MethodVerifier<false>(self,
5487 dex_file,
5488 dex_cache,
5489 class_loader,
5490 class_def,
5491 code_item,
5492 method_idx,
5493 method,
5494 access_flags,
5495 can_load_classes,
5496 allow_soft_failures,
5497 need_precise_constants,
5498 verify_to_dump,
5499 allow_thread_suspension,
Alex Lighte2ddce32019-05-22 17:08:35 +00005500 /* fill_register_lines= */ false,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005501 api_level);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005502}
5503
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005504void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005505 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08005506}
5507
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005508void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005509 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08005510}
jeffhaod1224c72012-02-29 13:43:08 -08005511
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005512void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
5513 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07005514}
5515
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005516void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
5517 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08005518}
5519
Andreas Gampe4146e062019-07-10 13:18:04 -07005520std::ostream& MethodVerifier::Fail(VerifyError error, bool pending_exc) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005521 // Mark the error type as encountered.
5522 encountered_failure_types_ |= static_cast<uint32_t>(error);
5523
Andreas Gampe4146e062019-07-10 13:18:04 -07005524 if (pending_exc) {
5525 switch (error) {
5526 case VERIFY_ERROR_NO_CLASS:
5527 case VERIFY_ERROR_NO_FIELD:
5528 case VERIFY_ERROR_NO_METHOD:
5529 case VERIFY_ERROR_ACCESS_CLASS:
5530 case VERIFY_ERROR_ACCESS_FIELD:
5531 case VERIFY_ERROR_ACCESS_METHOD:
5532 case VERIFY_ERROR_INSTANTIATION:
5533 case VERIFY_ERROR_CLASS_CHANGE:
5534 case VERIFY_ERROR_FORCE_INTERPRETER:
5535 case VERIFY_ERROR_LOCKING:
5536 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
5537 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
5538 // class change and instantiation errors into soft verification errors so that we
5539 // re-verify at runtime. We may fail to find or to agree on access because of not yet
5540 // available class loaders, or class loaders that will differ at runtime. In these cases,
5541 // we don't want to affect the soundness of the code being compiled. Instead, the
5542 // generated code runs "slow paths" that dynamically perform the verification and cause
5543 // the behavior to be that akin to an interpreter.
5544 error = VERIFY_ERROR_BAD_CLASS_SOFT;
5545 } else {
5546 // If we fail again at runtime, mark that this instruction would throw and force this
5547 // method to be executed using the interpreter with checks.
5548 flags_.have_pending_runtime_throw_failure_ = true;
Andreas Gampefc25ae92019-04-19 22:22:57 -07005549
Andreas Gampe4146e062019-07-10 13:18:04 -07005550 // We need to save the work_line if the instruction wasn't throwing before. Otherwise
5551 // we'll try to merge garbage.
5552 // Note: this assumes that Fail is called before we do any work_line modifications.
5553 // Note: this can fail before we touch any instruction, for the signature of a method. So
5554 // add a check.
5555 if (work_insn_idx_ < dex::kDexNoIndex) {
5556 const Instruction& inst = code_item_accessor_.InstructionAt(work_insn_idx_);
5557 int opcode_flags = Instruction::FlagsOf(inst.Opcode());
Andreas Gampefc25ae92019-04-19 22:22:57 -07005558
Andreas Gampe4146e062019-07-10 13:18:04 -07005559 if ((opcode_flags & Instruction::kThrow) == 0 &&
5560 GetInstructionFlags(work_insn_idx_).IsInTry()) {
5561 saved_line_->CopyFromLine(work_line_.get());
5562 }
Andreas Gampefc25ae92019-04-19 22:22:57 -07005563 }
5564 }
Andreas Gampe4146e062019-07-10 13:18:04 -07005565 break;
Andreas Gampe7a82acc2019-07-01 13:54:21 -07005566
Andreas Gampe4146e062019-07-10 13:18:04 -07005567 // Indication that verification should be retried at runtime.
5568 case VERIFY_ERROR_BAD_CLASS_SOFT:
5569 if (!allow_soft_failures_) {
5570 flags_.have_pending_hard_failure_ = true;
5571 }
5572 break;
5573
5574 // Hard verification failures at compile time will still fail at runtime, so the class is
5575 // marked as rejected to prevent it from being compiled.
5576 case VERIFY_ERROR_BAD_CLASS_HARD: {
Andreas Gampe4a30f892019-07-10 04:57:58 +00005577 flags_.have_pending_hard_failure_ = true;
Andreas Gampe4146e062019-07-10 13:18:04 -07005578 break;
Andreas Gampe4a30f892019-07-10 04:57:58 +00005579 }
Andreas Gampe4a30f892019-07-10 04:57:58 +00005580
Andreas Gampe0d87f992019-07-10 13:19:45 -07005581 case VERIFY_ERROR_SKIP_COMPILER:
Andreas Gampe4146e062019-07-10 13:18:04 -07005582 // Nothing to do, just remember the failure type.
5583 break;
Andreas Gampe4a30f892019-07-10 04:57:58 +00005584 }
Andreas Gampe4146e062019-07-10 13:18:04 -07005585 } else if (kIsDebugBuild) {
5586 CHECK_NE(error, VERIFY_ERROR_BAD_CLASS_SOFT);
5587 CHECK_NE(error, VERIFY_ERROR_BAD_CLASS_HARD);
Andreas Gampe4a30f892019-07-10 04:57:58 +00005588 }
Andreas Gampe4146e062019-07-10 13:18:04 -07005589
Andreas Gampefc25ae92019-04-19 22:22:57 -07005590 failures_.push_back(error);
5591 std::string location(StringPrintf("%s: [0x%X] ", dex_file_->PrettyMethod(dex_method_idx_).c_str(),
5592 work_insn_idx_));
5593 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
5594 failure_messages_.push_back(failure_message);
5595 return *failure_message;
Andreas Gampef23f33d2015-06-23 14:18:17 -07005596}
5597
Andreas Gampefc25ae92019-04-19 22:22:57 -07005598ScopedNewLine MethodVerifier::LogVerifyInfo() {
5599 ScopedNewLine ret{info_messages_};
5600 ret << "VFY: " << dex_file_->PrettyMethod(dex_method_idx_)
5601 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
5602 return ret;
5603}
5604
5605static FailureKind FailureKindMax(FailureKind fk1, FailureKind fk2) {
5606 static_assert(FailureKind::kNoFailure < FailureKind::kSoftFailure
5607 && FailureKind::kSoftFailure < FailureKind::kHardFailure,
5608 "Unexpected FailureKind order");
5609 return std::max(fk1, fk2);
5610}
5611
5612void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) {
5613 kind = FailureKindMax(kind, fd.kind);
5614 types |= fd.types;
Andreas Gampe51de69e2019-04-19 15:14:14 -07005615}
5616
Ian Rogersd81871c2011-10-03 13:57:23 -07005617} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005618} // namespace art