blob: 8a4d6993f2f1d363668a3e2101c09128f676aa5a [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Andreas Gampe4a57d5f2019-04-03 12:31:18 -070019#include <ostream>
Elliott Hughes1f359b02011-07-17 14:27:17 -070020
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021#include "android-base/stringprintf.h"
22
Mathieu Chartierc7853442015-03-27 14:35:38 -070023#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070024#include "art_method-inl.h"
Andreas Gampe39b378c2017-12-07 15:44:13 -080025#include "base/aborting.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070026#include "base/enums.h"
David Sehr67bf42e2018-02-26 16:43:04 -080027#include "base/leb128.h"
David Sehr9c4a0152018-04-05 12:23:54 -070028#include "base/indenter.h"
Andreas Gampe57943812017-12-06 21:39:13 -080029#include "base/logging.h" // For VLOG.
Ian Rogers637c65b2013-05-31 11:46:00 -070030#include "base/mutex-inl.h"
David Brazdil2bb2fbd2018-11-13 18:24:26 +000031#include "base/sdk_version.h"
Vladimir Marko637ee0b2015-09-04 12:47:41 +010032#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080033#include "base/systrace.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010034#include "base/time_utils.h"
David Sehrc431b9d2018-03-02 12:01:51 -080035#include "base/utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070036#include "class_linker.h"
Vladimir Marko5868ada2020-05-12 11:50:34 +010037#include "class_root-inl.h"
Mathieu Chartier1f1cb9f2018-06-04 09:22:46 -070038#include "dex/class_accessor-inl.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080039#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080040#include "dex/dex_file-inl.h"
41#include "dex/dex_file_exception_helpers.h"
42#include "dex/dex_instruction-inl.h"
43#include "dex/dex_instruction_utils.h"
Alex Lighteb7c1442015-08-31 13:17:42 -070044#include "experimental_flags.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070045#include "gc/accounting/card_table-inl.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070046#include "handle_scope-inl.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070047#include "intern_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048#include "mirror/class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070049#include "mirror/class.h"
Andreas Gampe51de69e2019-04-19 15:14:14 -070050#include "mirror/class_loader.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070051#include "mirror/dex_cache-inl.h"
Orion Hodsoncfa325e2016-10-13 10:25:54 +010052#include "mirror/method_handle_impl.h"
Orion Hodson2e599942017-09-22 16:17:41 +010053#include "mirror/method_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054#include "mirror/object-inl.h"
55#include "mirror/object_array-inl.h"
Orion Hodsonfe92d122018-01-02 10:45:17 +000056#include "mirror/var_handle.h"
Andreas Gampe51de69e2019-04-19 15:14:14 -070057#include "obj_ptr-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070058#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070059#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070060#include "runtime.h"
Andreas Gampe2ad6cce2019-04-11 16:17:39 -070061#include "scoped_newline.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070062#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070063#include "stack.h"
Nicolas Geoffrayb041a402017-11-13 15:16:22 +000064#include "vdex_file.h"
Alex Lighte2ddce32019-05-22 17:08:35 +000065#include "verifier/method_verifier.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070066#include "verifier_deps.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070067
68namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070069namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070070
Andreas Gampe46ee31b2016-12-14 10:11:49 -080071using android::base::StringPrintf;
72
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070073static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogers2c8a8572011-10-24 17:11:36 -070074
Vladimir Marko69d310e2017-10-09 14:12:23 +010075PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& allocator)
76 : register_lines_(allocator.Adapter(kArenaAllocVerifier)) {}
Mathieu Chartierde40d472015-10-15 17:47:48 -070077
Nicolas Geoffray2cb25272021-07-15 14:24:29 +010078void PcToRegisterLineTable::Init(InstructionFlags* flags,
Andreas Gamped09c0592019-04-19 15:44:05 -070079 uint32_t insns_size,
80 uint16_t registers_size,
81 ScopedArenaAllocator& allocator,
Nicolas Geoffray2cb25272021-07-15 14:24:29 +010082 RegTypeCache* reg_types,
83 uint32_t interesting_dex_pc) {
Ian Rogersd81871c2011-10-03 13:57:23 -070084 DCHECK_GT(insns_size, 0U);
Mathieu Chartierde40d472015-10-15 17:47:48 -070085 register_lines_.resize(insns_size);
Ian Rogersd81871c2011-10-03 13:57:23 -070086 for (uint32_t i = 0; i < insns_size; i++) {
Nicolas Geoffray2cb25272021-07-15 14:24:29 +010087 if ((i == interesting_dex_pc) || flags[i].IsBranchTarget()) {
Andreas Gamped09c0592019-04-19 15:44:05 -070088 register_lines_[i].reset(RegisterLine::Create(registers_size, allocator, reg_types));
Ian Rogersd0fbd852013-09-24 18:17:04 -070089 }
90 }
91}
92
Mathieu Chartierde40d472015-10-15 17:47:48 -070093PcToRegisterLineTable::~PcToRegisterLineTable() {}
Ian Rogersd81871c2011-10-03 13:57:23 -070094
Andreas Gampefc25ae92019-04-19 22:22:57 -070095namespace impl {
96namespace {
97
Andreas Gampe99db7bb2019-04-19 23:05:47 -070098enum class CheckAccess {
Andreas Gampe99db7bb2019-04-19 23:05:47 -070099 kNo,
Andreas Gampe6087bc22019-06-03 15:52:08 -0700100 kOnResolvedClass,
101 kYes,
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700102};
103
104enum class FieldAccessType {
105 kAccGet,
106 kAccPut
107};
108
Andreas Gampec6582782019-07-10 15:06:18 -0700109// Instruction types that are not marked as throwing (because they normally would not), but for
110// historical reasons may do so. These instructions cannot be marked kThrow as that would introduce
111// a general flow that is unwanted.
112//
113// Note: Not implemented as Instruction::Flags value as that set is full and we'd need to increase
114// the struct size (making it a non-power-of-two) for a single element.
115//
116// Note: This should eventually be removed.
117constexpr bool IsCompatThrow(Instruction::Code opcode) {
Mathieu Chartiere8ed8662020-03-13 13:55:44 -0700118 return opcode == Instruction::Code::RETURN_OBJECT || opcode == Instruction::Code::MOVE_EXCEPTION;
Andreas Gampec6582782019-07-10 15:06:18 -0700119}
120
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700121template <bool kVerifierDebug>
Andreas Gampefc25ae92019-04-19 22:22:57 -0700122class MethodVerifier final : public ::art::verifier::MethodVerifier {
123 public:
124 bool IsInstanceConstructor() const {
125 return IsConstructor() && !IsStatic();
126 }
127
128 const RegType& ResolveCheckedClass(dex::TypeIndex class_idx) override
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700129 REQUIRES_SHARED(Locks::mutator_lock_) {
130 DCHECK(!HasFailures());
131 const RegType& result = ResolveClass<CheckAccess::kYes>(class_idx);
132 DCHECK(!HasFailures());
133 return result;
134 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700135
136 void FindLocksAtDexPc() REQUIRES_SHARED(Locks::mutator_lock_);
137
138 private:
139 MethodVerifier(Thread* self,
Andreas Gampee0bbab92019-07-25 12:28:22 -0700140 ClassLinker* class_linker,
Andreas Gampef1468b52019-07-26 09:22:39 -0700141 ArenaPool* arena_pool,
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +0000142 VerifierDeps* verifier_deps,
Andreas Gampefc25ae92019-04-19 22:22:57 -0700143 const DexFile* dex_file,
Andreas Gampe0134a282019-07-29 11:34:55 -0700144 const dex::CodeItem* code_item,
145 uint32_t method_idx,
146 bool can_load_classes,
147 bool allow_thread_suspension,
148 bool allow_soft_failures,
149 bool aot_mode,
Andreas Gampefc25ae92019-04-19 22:22:57 -0700150 Handle<mirror::DexCache> dex_cache,
151 Handle<mirror::ClassLoader> class_loader,
152 const dex::ClassDef& class_def,
Andreas Gampefc25ae92019-04-19 22:22:57 -0700153 uint32_t access_flags,
Andreas Gampefc25ae92019-04-19 22:22:57 -0700154 bool verify_to_dump,
Andreas Gampe86bf0682019-07-29 11:22:44 -0700155 uint32_t api_level) REQUIRES_SHARED(Locks::mutator_lock_)
156 : art::verifier::MethodVerifier(self,
157 class_linker,
158 arena_pool,
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +0000159 verifier_deps,
Andreas Gampe86bf0682019-07-29 11:22:44 -0700160 dex_file,
Nicolas Geoffray1960c422020-11-04 08:45:32 +0000161 class_def,
Andreas Gampe86bf0682019-07-29 11:22:44 -0700162 code_item,
163 method_idx,
164 can_load_classes,
165 allow_thread_suspension,
166 allow_soft_failures,
167 aot_mode),
Andreas Gampe86bf0682019-07-29 11:22:44 -0700168 method_access_flags_(access_flags),
169 return_type_(nullptr),
170 dex_cache_(dex_cache),
171 class_loader_(class_loader),
Andreas Gampe86bf0682019-07-29 11:22:44 -0700172 declaring_class_(nullptr),
173 interesting_dex_pc_(-1),
174 monitor_enter_dex_pcs_(nullptr),
Andreas Gampe86bf0682019-07-29 11:22:44 -0700175 verify_to_dump_(verify_to_dump),
176 allow_thread_suspension_(allow_thread_suspension),
177 is_constructor_(false),
Andreas Gampe86bf0682019-07-29 11:22:44 -0700178 api_level_(api_level == 0 ? std::numeric_limits<uint32_t>::max() : api_level) {
179 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700180
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700181 void UninstantiableError(const char* descriptor) {
182 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
183 << "non-instantiable klass " << descriptor;
184 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700185 static bool IsInstantiableOrPrimitive(ObjPtr<mirror::Class> klass)
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700186 REQUIRES_SHARED(Locks::mutator_lock_) {
187 return klass->IsInstantiable() || klass->IsPrimitive();
188 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700189
190 // Is the method being verified a constructor? See the comment on the field.
191 bool IsConstructor() const {
192 return is_constructor_;
193 }
194
195 // Is the method verified static?
196 bool IsStatic() const {
197 return (method_access_flags_ & kAccStatic) != 0;
198 }
199
200 // Adds the given string to the beginning of the last failure message.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700201 void PrependToLastFailMessage(std::string prepend) {
202 size_t failure_num = failure_messages_.size();
203 DCHECK_NE(failure_num, 0U);
204 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
205 prepend += last_fail_message->str();
206 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
207 delete last_fail_message;
208 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700209
210 // Adds the given string to the end of the last failure message.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700211 void AppendToLastFailMessage(const std::string& append) {
212 size_t failure_num = failure_messages_.size();
213 DCHECK_NE(failure_num, 0U);
214 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
215 (*last_fail_message) << append;
216 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700217
Andreas Gampefc25ae92019-04-19 22:22:57 -0700218 /*
219 * Compute the width of the instruction at each address in the instruction stream, and store it in
220 * insn_flags_. Addresses that are in the middle of an instruction, or that are part of switch
221 * table data, are not touched (so the caller should probably initialize "insn_flags" to zero).
222 *
223 * The "new_instance_count_" and "monitor_enter_count_" fields in vdata are also set.
224 *
225 * Performs some static checks, notably:
226 * - opcode of first instruction begins at index 0
227 * - only documented instructions may appear
228 * - each instruction follows the last
229 * - last byte of last instruction is at (code_length-1)
230 *
231 * Logs an error and returns "false" on failure.
232 */
233 bool ComputeWidthsAndCountOps();
234
235 /*
236 * Set the "in try" flags for all instructions protected by "try" statements. Also sets the
237 * "branch target" flags for exception handlers.
238 *
239 * Call this after widths have been set in "insn_flags".
240 *
241 * Returns "false" if something in the exception table looks fishy, but we're expecting the
Orion Hodsonaacf9772020-07-22 21:51:00 +0100242 * exception table to be valid.
Andreas Gampefc25ae92019-04-19 22:22:57 -0700243 */
244 bool ScanTryCatchBlocks() REQUIRES_SHARED(Locks::mutator_lock_);
245
246 /*
247 * Perform static verification on all instructions in a method.
248 *
249 * Walks through instructions in a method calling VerifyInstruction on each.
250 */
251 template <bool kAllowRuntimeOnlyInstructions>
252 bool VerifyInstructions();
253
254 /*
255 * Perform static verification on an instruction.
256 *
257 * As a side effect, this sets the "branch target" flags in InsnFlags.
258 *
259 * "(CF)" items are handled during code-flow analysis.
260 *
261 * v3 4.10.1
262 * - target of each jump and branch instruction must be valid
263 * - targets of switch statements must be valid
264 * - operands referencing constant pool entries must be valid
265 * - (CF) operands of getfield, putfield, getstatic, putstatic must be valid
266 * - (CF) operands of method invocation instructions must be valid
267 * - (CF) only invoke-direct can call a method starting with '<'
268 * - (CF) <clinit> must never be called explicitly
269 * - operands of instanceof, checkcast, new (and variants) must be valid
270 * - new-array[-type] limited to 255 dimensions
271 * - can't use "new" on an array class
272 * - (?) limit dimensions in multi-array creation
273 * - local variable load/store register values must be in valid range
274 *
275 * v3 4.11.1.2
276 * - branches must be within the bounds of the code array
277 * - targets of all control-flow instructions are the start of an instruction
278 * - register accesses fall within range of allocated registers
279 * - (N/A) access to constant pool must be of appropriate type
280 * - code does not end in the middle of an instruction
281 * - execution cannot fall off the end of the code
282 * - (earlier) for each exception handler, the "try" area must begin and
283 * end at the start of an instruction (end can be at the end of the code)
284 * - (earlier) for each exception handler, the handler must start at a valid
285 * instruction
286 */
287 template <bool kAllowRuntimeOnlyInstructions>
288 bool VerifyInstruction(const Instruction* inst, uint32_t code_offset);
289
290 /* Ensure that the register index is valid for this code item. */
Andreas Gampe86bf0682019-07-29 11:22:44 -0700291 bool CheckRegisterIndex(uint32_t idx) {
292 if (UNLIKELY(idx >= code_item_accessor_.RegistersSize())) {
293 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
294 << code_item_accessor_.RegistersSize() << ")";
295 return false;
296 }
297 return true;
298 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700299
300 /* Ensure that the wide register index is valid for this code item. */
Andreas Gampe86bf0682019-07-29 11:22:44 -0700301 bool CheckWideRegisterIndex(uint32_t idx) {
302 if (UNLIKELY(idx + 1 >= code_item_accessor_.RegistersSize())) {
303 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
304 << "+1 >= " << code_item_accessor_.RegistersSize() << ")";
305 return false;
306 }
307 return true;
308 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700309
310 // Perform static checks on an instruction referencing a CallSite. All we do here is ensure that
311 // the call site index is in the valid range.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700312 bool CheckCallSiteIndex(uint32_t idx) {
313 uint32_t limit = dex_file_->NumCallSiteIds();
314 if (UNLIKELY(idx >= limit)) {
315 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad call site index " << idx << " (max "
316 << limit << ")";
317 return false;
318 }
319 return true;
320 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700321
322 // Perform static checks on a field Get or set instruction. All we do here is ensure that the
323 // field index is in the valid range.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700324 bool CheckFieldIndex(uint32_t idx) {
325 if (UNLIKELY(idx >= dex_file_->GetHeader().field_ids_size_)) {
326 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
327 << dex_file_->GetHeader().field_ids_size_ << ")";
328 return false;
329 }
330 return true;
331 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700332
333 // Perform static checks on a method invocation instruction. All we do here is ensure that the
334 // method index is in the valid range.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700335 bool CheckMethodIndex(uint32_t idx) {
336 if (UNLIKELY(idx >= dex_file_->GetHeader().method_ids_size_)) {
337 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
338 << dex_file_->GetHeader().method_ids_size_ << ")";
339 return false;
340 }
341 return true;
342 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700343
344 // Perform static checks on an instruction referencing a constant method handle. All we do here
345 // is ensure that the method index is in the valid range.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700346 bool CheckMethodHandleIndex(uint32_t idx) {
347 uint32_t limit = dex_file_->NumMethodHandles();
348 if (UNLIKELY(idx >= limit)) {
349 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method handle index " << idx << " (max "
350 << limit << ")";
351 return false;
352 }
353 return true;
354 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700355
356 // Perform static checks on a "new-instance" instruction. Specifically, make sure the class
357 // reference isn't for an array class.
358 bool CheckNewInstance(dex::TypeIndex idx);
359
360 // Perform static checks on a prototype indexing instruction. All we do here is ensure that the
361 // prototype index is in the valid range.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700362 bool CheckPrototypeIndex(uint32_t idx) {
363 if (UNLIKELY(idx >= dex_file_->GetHeader().proto_ids_size_)) {
364 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad prototype index " << idx << " (max "
365 << dex_file_->GetHeader().proto_ids_size_ << ")";
366 return false;
367 }
368 return true;
369 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700370
371 /* Ensure that the string index is in the valid range. */
Andreas Gampe86bf0682019-07-29 11:22:44 -0700372 bool CheckStringIndex(uint32_t idx) {
373 if (UNLIKELY(idx >= dex_file_->GetHeader().string_ids_size_)) {
374 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
375 << dex_file_->GetHeader().string_ids_size_ << ")";
376 return false;
377 }
378 return true;
379 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700380
381 // Perform static checks on an instruction that takes a class constant. Ensure that the class
382 // index is in the valid range.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700383 bool CheckTypeIndex(dex::TypeIndex idx) {
384 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
385 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
386 << dex_file_->GetHeader().type_ids_size_ << ")";
387 return false;
388 }
389 return true;
390 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700391
392 // Perform static checks on a "new-array" instruction. Specifically, make sure they aren't
393 // creating an array of arrays that causes the number of dimensions to exceed 255.
394 bool CheckNewArray(dex::TypeIndex idx);
395
396 // Verify an array data table. "cur_offset" is the offset of the fill-array-data instruction.
397 bool CheckArrayData(uint32_t cur_offset);
398
399 // Verify that the target of a branch instruction is valid. We don't expect code to jump directly
400 // into an exception handler, but it's valid to do so as long as the target isn't a
401 // "move-exception" instruction. We verify that in a later stage.
402 // The dex format forbids certain instructions from branching to themselves.
403 // Updates "insn_flags_", setting the "branch target" flag.
404 bool CheckBranchTarget(uint32_t cur_offset);
405
406 // Verify a switch table. "cur_offset" is the offset of the switch instruction.
407 // Updates "insn_flags_", setting the "branch target" flag.
408 bool CheckSwitchTargets(uint32_t cur_offset);
409
410 // Check the register indices used in a "vararg" instruction, such as invoke-virtual or
411 // filled-new-array.
412 // - vA holds word count (0-5), args[] have values.
413 // There are some tests we don't do here, e.g. we don't try to verify that invoking a method that
414 // takes a double is done with consecutive registers. This requires parsing the target method
415 // signature, which we will be doing later on during the code flow analysis.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700416 bool CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
417 uint16_t registers_size = code_item_accessor_.RegistersSize();
418 for (uint32_t idx = 0; idx < vA; idx++) {
419 if (UNLIKELY(arg[idx] >= registers_size)) {
420 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
421 << ") in non-range invoke (>= " << registers_size << ")";
422 return false;
423 }
424 }
425
426 return true;
427 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700428
429 // Check the register indices used in a "vararg/range" instruction, such as invoke-virtual/range
430 // or filled-new-array/range.
431 // - vA holds word count, vC holds index of first reg.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700432 bool CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
433 uint16_t registers_size = code_item_accessor_.RegistersSize();
434 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
435 // integer overflow when adding them here.
436 if (UNLIKELY(vA + vC > registers_size)) {
437 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
438 << " in range invoke (> " << registers_size << ")";
439 return false;
440 }
441 return true;
442 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700443
444 // Checks the method matches the expectations required to be signature polymorphic.
445 bool CheckSignaturePolymorphicMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
446
447 // Checks the invoked receiver matches the expectations for signature polymorphic methods.
448 bool CheckSignaturePolymorphicReceiver(const Instruction* inst) REQUIRES_SHARED(Locks::mutator_lock_);
449
450 // Extract the relative offset from a branch instruction.
451 // Returns "false" on failure (e.g. this isn't a branch instruction).
452 bool GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
453 bool* selfOkay);
454
455 /* Perform detailed code-flow analysis on a single method. */
456 bool VerifyCodeFlow() REQUIRES_SHARED(Locks::mutator_lock_);
457
458 // Set the register types for the first instruction in the method based on the method signature.
459 // This has the side-effect of validating the signature.
460 bool SetTypesFromSignature() REQUIRES_SHARED(Locks::mutator_lock_);
461
462 /*
463 * Perform code flow on a method.
464 *
465 * The basic strategy is as outlined in v3 4.11.1.2: set the "changed" bit on the first
466 * instruction, process it (setting additional "changed" bits), and repeat until there are no
467 * more.
468 *
469 * v3 4.11.1.1
470 * - (N/A) operand stack is always the same size
471 * - operand stack [registers] contain the correct types of values
472 * - local variables [registers] contain the correct types of values
473 * - methods are invoked with the appropriate arguments
474 * - fields are assigned using values of appropriate types
475 * - opcodes have the correct type values in operand registers
476 * - there is never an uninitialized class instance in a local variable in code protected by an
477 * exception handler (operand stack is okay, because the operand stack is discarded when an
478 * exception is thrown) [can't know what's a local var w/o the debug info -- should fall out of
479 * register typing]
480 *
481 * v3 4.11.1.2
482 * - execution cannot fall off the end of the code
483 *
484 * (We also do many of the items described in the "static checks" sections, because it's easier to
485 * do them here.)
486 *
487 * We need an array of RegType values, one per register, for every instruction. If the method uses
488 * monitor-enter, we need extra data for every register, and a stack for every "interesting"
489 * instruction. In theory this could become quite large -- up to several megabytes for a monster
490 * function.
491 *
492 * NOTE:
493 * The spec forbids backward branches when there's an uninitialized reference in a register. The
494 * idea is to prevent something like this:
495 * loop:
496 * move r1, r0
497 * new-instance r0, MyClass
498 * ...
499 * if-eq rN, loop // once
500 * initialize r0
501 *
502 * This leaves us with two different instances, both allocated by the same instruction, but only
503 * one is initialized. The scheme outlined in v3 4.11.1.4 wouldn't catch this, so they work around
504 * it by preventing backward branches. We achieve identical results without restricting code
505 * reordering by specifying that you can't execute the new-instance instruction if a register
506 * contains an uninitialized instance created by that same instruction.
507 */
Andreas Gampeb90cad52019-07-31 16:15:41 -0700508 template <bool kMonitorDexPCs>
Andreas Gampefc25ae92019-04-19 22:22:57 -0700509 bool CodeFlowVerifyMethod() REQUIRES_SHARED(Locks::mutator_lock_);
510
511 /*
512 * Perform verification for a single instruction.
513 *
514 * This requires fully decoding the instruction to determine the effect it has on registers.
515 *
516 * Finds zero or more following instructions and sets the "changed" flag if execution at that
517 * point needs to be (re-)evaluated. Register changes are merged into "reg_types_" at the target
518 * addresses. Does not set or clear any other flags in "insn_flags_".
519 */
520 bool CodeFlowVerifyInstruction(uint32_t* start_guess)
521 REQUIRES_SHARED(Locks::mutator_lock_);
522
523 // Perform verification of a new array instruction
524 void VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range)
525 REQUIRES_SHARED(Locks::mutator_lock_);
526
527 // Helper to perform verification on puts of primitive type.
528 void VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
529 const uint32_t vregA) REQUIRES_SHARED(Locks::mutator_lock_);
530
531 // Perform verification of an aget instruction. The destination register's type will be set to
532 // be that of component type of the array unless the array type is unknown, in which case a
533 // bottom type inferred from the type of instruction is used. is_primitive is false for an
534 // aget-object.
535 void VerifyAGet(const Instruction* inst, const RegType& insn_type,
536 bool is_primitive) REQUIRES_SHARED(Locks::mutator_lock_);
537
538 // Perform verification of an aput instruction.
539 void VerifyAPut(const Instruction* inst, const RegType& insn_type,
540 bool is_primitive) REQUIRES_SHARED(Locks::mutator_lock_);
541
542 // Lookup instance field and fail for resolution violations
543 ArtField* GetInstanceField(const RegType& obj_type, int field_idx)
544 REQUIRES_SHARED(Locks::mutator_lock_);
545
546 // Lookup static field and fail for resolution violations
547 ArtField* GetStaticField(int field_idx) REQUIRES_SHARED(Locks::mutator_lock_);
548
549 // Perform verification of an iget/sget/iput/sput instruction.
Andreas Gampefc25ae92019-04-19 22:22:57 -0700550 template <FieldAccessType kAccType>
551 void VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
552 bool is_primitive, bool is_static)
553 REQUIRES_SHARED(Locks::mutator_lock_);
554
Andreas Gampefc25ae92019-04-19 22:22:57 -0700555 // Resolves a class based on an index and, if C is kYes, performs access checks to ensure
556 // the referrer can access the resolved class.
557 template <CheckAccess C>
558 const RegType& ResolveClass(dex::TypeIndex class_idx)
559 REQUIRES_SHARED(Locks::mutator_lock_);
560
561 /*
562 * For the "move-exception" instruction at "work_insn_idx_", which must be at an exception handler
563 * address, determine the Join of all exceptions that can land here. Fails if no matching
564 * exception handler can be found or if the Join of exception types fails.
565 */
566 const RegType& GetCaughtExceptionType()
567 REQUIRES_SHARED(Locks::mutator_lock_);
568
569 /*
570 * Resolves a method based on an index and performs access checks to ensure
571 * the referrer can access the resolved method.
572 * Does not throw exceptions.
573 */
574 ArtMethod* ResolveMethodAndCheckAccess(uint32_t method_idx, MethodType method_type)
575 REQUIRES_SHARED(Locks::mutator_lock_);
576
577 /*
578 * Verify the arguments to a method. We're executing in "method", making
579 * a call to the method reference in vB.
580 *
581 * If this is a "direct" invoke, we allow calls to <init>. For calls to
582 * <init>, the first argument may be an uninitialized reference. Otherwise,
583 * calls to anything starting with '<' will be rejected, as will any
584 * uninitialized reference arguments.
585 *
586 * For non-static method calls, this will verify that the method call is
587 * appropriate for the "this" argument.
588 *
589 * The method reference is in vBBBB. The "is_range" parameter determines
590 * whether we use 0-4 "args" values or a range of registers defined by
591 * vAA and vCCCC.
592 *
593 * Widening conversions on integers and references are allowed, but
594 * narrowing conversions are not.
595 *
596 * Returns the resolved method on success, null on failure (with *failure
597 * set appropriately).
598 */
599 ArtMethod* VerifyInvocationArgs(const Instruction* inst, MethodType method_type, bool is_range)
600 REQUIRES_SHARED(Locks::mutator_lock_);
601
602 // Similar checks to the above, but on the proto. Will be used when the method cannot be
603 // resolved.
604 void VerifyInvocationArgsUnresolvedMethod(const Instruction* inst, MethodType method_type,
605 bool is_range)
606 REQUIRES_SHARED(Locks::mutator_lock_);
607
608 template <class T>
609 ArtMethod* VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
610 MethodType method_type, bool is_range,
611 ArtMethod* res_method)
612 REQUIRES_SHARED(Locks::mutator_lock_);
613
614 /*
615 * Verify the arguments present for a call site. Returns "true" if all is well, "false" otherwise.
616 */
617 bool CheckCallSite(uint32_t call_site_idx);
618
619 /*
620 * Verify that the target instruction is not "move-exception". It's important that the only way
621 * to execute a move-exception is as the first instruction of an exception handler.
622 * Returns "true" if all is well, "false" if the target instruction is move-exception.
623 */
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700624 bool CheckNotMoveException(const uint16_t* insns, int insn_idx) {
625 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
626 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
627 return false;
628 }
629 return true;
630 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700631
632 /*
633 * Verify that the target instruction is not "move-result". It is important that we cannot
634 * branch to move-result instructions, but we have to make this a distinct check instead of
635 * adding it to CheckNotMoveException, because it is legal to continue into "move-result"
636 * instructions - as long as the previous instruction was an invoke, which is checked elsewhere.
637 */
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700638 bool CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
639 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
640 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
641 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
642 return false;
643 }
644 return true;
645 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700646
647 /*
648 * Verify that the target instruction is not "move-result" or "move-exception". This is to
649 * be used when checking branch and switch instructions, but not instructions that can
650 * continue.
651 */
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700652 bool CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
653 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
654 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700655
656 /*
657 * Control can transfer to "next_insn". Merge the registers from merge_line into the table at
658 * next_insn, and set the changed flag on the target address if any of the registers were changed.
659 * In the case of fall-through, update the merge line on a change as its the working line for the
660 * next instruction.
661 * Returns "false" if an error is encountered.
662 */
663 bool UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line, bool update_merge_line)
664 REQUIRES_SHARED(Locks::mutator_lock_);
665
666 // Return the register type for the method.
667 const RegType& GetMethodReturnType() REQUIRES_SHARED(Locks::mutator_lock_);
668
669 // Get a type representing the declaring class of the method.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700670 const RegType& GetDeclaringClass() REQUIRES_SHARED(Locks::mutator_lock_) {
671 if (declaring_class_ == nullptr) {
672 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
673 const char* descriptor
674 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Nicolas Geoffray2ec38232021-07-02 16:36:29 +0100675 declaring_class_ = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
Andreas Gampe86bf0682019-07-29 11:22:44 -0700676 }
677 return *declaring_class_;
678 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700679
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700680 InstructionFlags* CurrentInsnFlags() {
681 return &GetModifiableInstructionFlags(work_insn_idx_);
682 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700683
Nicolas Geoffray2cb25272021-07-15 14:24:29 +0100684 const RegType& DetermineCat1Constant(int32_t value)
Andreas Gampefc25ae92019-04-19 22:22:57 -0700685 REQUIRES_SHARED(Locks::mutator_lock_);
686
687 // Try to create a register type from the given class. In case a precise type is requested, but
688 // the class is not instantiable, a soft error (of type NO_CLASS) will be enqueued and a
689 // non-precise reference will be returned.
690 // Note: we reuse NO_CLASS as this will throw an exception at runtime, when the failing class is
691 // actually touched.
692 const RegType& FromClass(const char* descriptor, ObjPtr<mirror::Class> klass, bool precise)
Andreas Gampe86bf0682019-07-29 11:22:44 -0700693 REQUIRES_SHARED(Locks::mutator_lock_) {
694 DCHECK(klass != nullptr);
695 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
696 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
697 << "non-instantiable klass " << descriptor;
698 precise = false;
699 }
700 return reg_types_.FromClass(descriptor, klass, precise);
701 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700702
703 ALWAYS_INLINE bool FailOrAbort(bool condition, const char* error_msg, uint32_t work_insn_idx);
704
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700705 ALWAYS_INLINE InstructionFlags& GetModifiableInstructionFlags(size_t index) {
706 return insn_flags_[index];
707 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700708
709 // Returns the method index of an invoke instruction.
710 uint16_t GetMethodIdxOfInvoke(const Instruction* inst)
Andreas Gampe86bf0682019-07-29 11:22:44 -0700711 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray4924ea92021-03-23 08:25:31 +0000712 return inst->VRegB();
Andreas Gampe86bf0682019-07-29 11:22:44 -0700713 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700714 // Returns the field index of a field access instruction.
715 uint16_t GetFieldIdxOfFieldAccess(const Instruction* inst, bool is_static)
Andreas Gampe86bf0682019-07-29 11:22:44 -0700716 REQUIRES_SHARED(Locks::mutator_lock_) {
717 if (is_static) {
718 return inst->VRegB_21c();
Andreas Gampe86bf0682019-07-29 11:22:44 -0700719 } else {
720 return inst->VRegC_22c();
721 }
722 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700723
724 // Run verification on the method. Returns true if verification completes and false if the input
725 // has an irrecoverable corruption.
726 bool Verify() override REQUIRES_SHARED(Locks::mutator_lock_);
727
Nicolas Geoffray44dc8a32021-06-21 15:23:49 +0100728 // For app-compatibility, code after a runtime throw is treated as dead code
729 // for apps targeting <= S.
Nicolas Geoffraycd133d82021-06-25 16:10:39 +0100730 // Returns whether the current instruction was marked as throwing.
731 bool PotentiallyMarkRuntimeThrow() override;
Nicolas Geoffray44dc8a32021-06-21 15:23:49 +0100732
Andreas Gampefc25ae92019-04-19 22:22:57 -0700733 // Dump the failures encountered by the verifier.
Andreas Gampe86bf0682019-07-29 11:22:44 -0700734 std::ostream& DumpFailures(std::ostream& os) {
735 DCHECK_EQ(failures_.size(), failure_messages_.size());
Andreas Gampe4ec4d482019-08-01 10:00:23 -0700736 for (const auto* stream : failure_messages_) {
737 os << stream->str() << "\n";
Andreas Gampe86bf0682019-07-29 11:22:44 -0700738 }
739 return os;
740 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700741
742 // Dump the state of the verifier, namely each instruction, what flags are set on it, register
743 // information
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700744 void Dump(std::ostream& os) REQUIRES_SHARED(Locks::mutator_lock_) {
745 VariableIndentationOutputStream vios(&os);
746 Dump(&vios);
747 }
Andreas Gampefc25ae92019-04-19 22:22:57 -0700748 void Dump(VariableIndentationOutputStream* vios) REQUIRES_SHARED(Locks::mutator_lock_);
749
Andreas Gampe6087bc22019-06-03 15:52:08 -0700750 bool HandleMoveException(const Instruction* inst) REQUIRES_SHARED(Locks::mutator_lock_);
751
Andreas Gampefc25ae92019-04-19 22:22:57 -0700752 const uint32_t method_access_flags_; // Method's access flags.
753 const RegType* return_type_; // Lazily computed return type of the method.
754 // The dex_cache for the declaring class of the method.
755 Handle<mirror::DexCache> dex_cache_ GUARDED_BY(Locks::mutator_lock_);
756 // The class loader for the declaring class of the method.
757 Handle<mirror::ClassLoader> class_loader_ GUARDED_BY(Locks::mutator_lock_);
Andreas Gampefc25ae92019-04-19 22:22:57 -0700758 const RegType* declaring_class_; // Lazily computed reg type of the method's declaring class.
759
760 // The dex PC of a FindLocksAtDexPc request, -1 otherwise.
761 uint32_t interesting_dex_pc_;
762 // The container into which FindLocksAtDexPc should write the registers containing held locks,
763 // null if we're not doing FindLocksAtDexPc.
764 std::vector<DexLockInfo>* monitor_enter_dex_pcs_;
765
Andreas Gampefc25ae92019-04-19 22:22:57 -0700766 // Indicates whether we verify to dump the info. In that case we accept quickened instructions
767 // even though we might detect to be a compiler. Should only be set when running
768 // VerifyMethodAndDump.
769 const bool verify_to_dump_;
770
771 // Whether or not we call AllowThreadSuspension periodically, we want a way to disable this for
772 // thread dumping checkpoints since we may get thread suspension at an inopportune time due to
773 // FindLocksAtDexPC, resulting in deadlocks.
774 const bool allow_thread_suspension_;
775
776 // Whether the method seems to be a constructor. Note that this field exists as we can't trust
777 // the flags in the dex file. Some older code does not mark methods named "<init>" and "<clinit>"
778 // correctly.
779 //
780 // Note: this flag is only valid once Verify() has started.
781 bool is_constructor_;
782
783 // API level, for dependent checks. Note: we do not use '0' for unset here, to simplify checks.
784 // Instead, unset level should correspond to max().
785 const uint32_t api_level_;
786
787 friend class ::art::verifier::MethodVerifier;
788
789 DISALLOW_COPY_AND_ASSIGN(MethodVerifier);
790};
791
Andreas Gampe7c038102014-10-27 20:08:46 -0700792// Note: returns true on failure.
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700793template <bool kVerifierDebug>
794inline bool MethodVerifier<kVerifierDebug>::FailOrAbort(bool condition,
795 const char* error_msg,
796 uint32_t work_insn_idx) {
Andreas Gampe7c038102014-10-27 20:08:46 -0700797 if (kIsDebugBuild) {
Andreas Gampef8f36c22016-11-19 14:51:52 -0800798 // In a debug build, abort if the error condition is wrong. Only warn if
799 // we are already aborting (as this verification is likely run to print
800 // lock information).
801 if (LIKELY(gAborting == 0)) {
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700802 DCHECK(condition) << error_msg << work_insn_idx << " "
803 << dex_file_->PrettyMethod(dex_method_idx_);
Andreas Gampef8f36c22016-11-19 14:51:52 -0800804 } else {
805 if (!condition) {
806 LOG(ERROR) << error_msg << work_insn_idx;
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700807 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
Andreas Gampef8f36c22016-11-19 14:51:52 -0800808 return true;
809 }
810 }
Andreas Gampe7c038102014-10-27 20:08:46 -0700811 } else {
812 // In a non-debug build, just fail the class.
813 if (!condition) {
Mathieu Chartierc2a2bda2018-03-12 11:21:52 -0700814 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
Andreas Gampe7c038102014-10-27 20:08:46 -0700815 return true;
816 }
817 }
818
819 return false;
820}
821
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800822static bool IsLargeMethod(const CodeItemDataAccessor& accessor) {
823 if (!accessor.HasCodeItem()) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700824 return false;
825 }
826
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800827 uint16_t registers_size = accessor.RegistersSize();
828 uint32_t insns_size = accessor.InsnsSizeInCodeUnits();
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700829
830 return registers_size * insns_size > 4*1024*1024;
831}
832
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700833template <bool kVerifierDebug>
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700834void MethodVerifier<kVerifierDebug>::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700835 CHECK(monitor_enter_dex_pcs_ != nullptr);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800836 CHECK(code_item_accessor_.HasCodeItem()); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700837
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800838 // Quick check whether there are any monitor_enter instructions before verifying.
839 for (const DexInstructionPcPair& inst : code_item_accessor_) {
840 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
841 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
842 // verification. In practice, the phase we want relies on data structures set up by all the
843 // earlier passes, so we just run the full method verification and bail out early when we've
844 // got what we wanted.
845 Verify();
846 return;
847 }
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700848 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700849}
850
Andreas Gampe99db7bb2019-04-19 23:05:47 -0700851template <bool kVerifierDebug>
852bool MethodVerifier<kVerifierDebug>::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700853 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
854 // the name.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800855 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Andreas Gampee6215c02015-08-31 18:54:38 -0700856 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
857 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
858 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
859 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
860 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
861 if ((method_access_flags_ & kAccConstructor) != 0) {
862 if (!constructor_by_name) {
863 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
864 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700865 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700866 }
867 is_constructor_ = true;
868 } else if (constructor_by_name) {
David Sehr709b0702016-10-13 09:12:37 -0700869 LOG(WARNING) << "Method " << dex_file_->PrettyMethod(dex_method_idx_)
Andreas Gampee6215c02015-08-31 18:54:38 -0700870 << " not marked as constructor.";
871 is_constructor_ = true;
872 }
873 // If it's a constructor, check whether IsStatic() matches the name.
874 // This should have been rejected by the dex file verifier. Only do in debug build.
875 if (kIsDebugBuild) {
876 if (IsConstructor()) {
877 if (IsStatic() ^ static_constructor_by_name) {
878 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
879 << "constructor name doesn't match static flag";
880 return false;
881 }
jeffhaobdb76512011-09-07 11:43:16 -0700882 }
jeffhaobdb76512011-09-07 11:43:16 -0700883 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700884
885 // Methods may only have one of public/protected/private.
886 // This should have been rejected by the dex file verifier. Only do in debug build.
887 if (kIsDebugBuild) {
888 size_t access_mod_count =
889 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
890 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
891 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
892 if (access_mod_count > 1) {
893 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
894 return false;
895 }
896 }
897
898 // If there aren't any instructions, make sure that's expected, then exit successfully.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -0800899 if (!code_item_accessor_.HasCodeItem()) {
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700900 // Only native or abstract methods may not have code.
901 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
902 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
903 return false;
904 }
905
Nicolas Geoffray5e82b182020-12-08 08:57:36 +0000906 // Test FastNative and CriticalNative annotations. We do this in the
907 // verifier for convenience.
908 if ((method_access_flags_ & kAccNative) != 0) {
909 // Fetch the flags from the annotations: the class linker hasn't processed
910 // them yet.
911 uint32_t native_access_flags = annotations::GetNativeMethodAnnotationAccessFlags(
912 *dex_file_, class_def_, dex_method_idx_);
913 if ((native_access_flags & kAccFastNative) != 0) {
914 if ((method_access_flags_ & kAccSynchronized) != 0) {
915 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "fast native methods cannot be synchronized";
916 return false;
917 }
918 }
919 if ((native_access_flags & kAccCriticalNative) != 0) {
920 if ((method_access_flags_ & kAccSynchronized) != 0) {
921 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "critical native methods cannot be synchronized";
922 return false;
923 }
924 if ((method_access_flags_ & kAccStatic) == 0) {
925 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "critical native methods must be static";
926 return false;
927 }
928 const char* shorty = dex_file_->GetMethodShorty(method_id);
929 for (size_t i = 0, len = strlen(shorty); i < len; ++i) {
930 if (Primitive::GetType(shorty[i]) == Primitive::kPrimNot) {
931 Fail(VERIFY_ERROR_BAD_CLASS_HARD) <<
932 "critical native methods must not have references as arguments or return type";
933 return false;
934 }
935 }
936 }
937 }
938
Andreas Gampee6215c02015-08-31 18:54:38 -0700939 // This should have been rejected by the dex file verifier. Only do in debug build.
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700940 // Note: the above will also be rejected in the dex file verifier, starting in dex version 37.
Andreas Gampee6215c02015-08-31 18:54:38 -0700941 if (kIsDebugBuild) {
Andreas Gampee6215c02015-08-31 18:54:38 -0700942 if ((method_access_flags_ & kAccAbstract) != 0) {
943 // Abstract methods are not allowed to have the following flags.
944 static constexpr uint32_t kForbidden =
945 kAccPrivate |
946 kAccStatic |
947 kAccFinal |
948 kAccNative |
949 kAccStrict |
950 kAccSynchronized;
951 if ((method_access_flags_ & kForbidden) != 0) {
952 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
953 << "method can't be abstract and private/static/final/native/strict/synchronized";
954 return false;
955 }
956 }
David Brazdil15fc7292016-09-02 14:13:18 +0100957 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700958 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000959 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700960 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700962 return false;
963 }
964 // In addition to the above, interface methods must not be protected.
965 static constexpr uint32_t kForbidden = kAccProtected;
966 if ((method_access_flags_ & kForbidden) != 0) {
967 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
968 return false;
969 }
970 }
971 // We also don't allow constructors to be abstract or native.
972 if (IsConstructor()) {
973 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
974 return false;
975 }
976 }
977 return true;
978 }
979
980 // This should have been rejected by the dex file verifier. Only do in debug build.
981 if (kIsDebugBuild) {
982 // When there's code, the method must not be native or abstract.
983 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
984 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
985 return false;
986 }
987
David Brazdil15fc7292016-09-02 14:13:18 +0100988 if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700989 // Interfaces may always have static initializers for their fields. If we are running with
990 // default methods enabled we also allow other public, static, non-final methods to have code.
991 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700992 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +0000993 if (IsInstanceConstructor()) {
994 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
995 return false;
996 } else if (method_access_flags_ & kAccFinal) {
997 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
998 return false;
Alex Lightb55f1ac2016-04-12 15:50:55 -0700999 } else {
1000 uint32_t access_flag_options = kAccPublic;
Mathieu Chartierf6e31472017-12-28 13:32:08 -08001001 if (dex_file_->SupportsDefaultMethods()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07001002 access_flag_options |= kAccPrivate;
1003 }
1004 if (!(method_access_flags_ & access_flag_options)) {
1005 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1006 << "interfaces may not have protected or package-private members";
1007 return false;
1008 }
Alex Lighteb7c1442015-08-31 13:17:42 -07001009 }
Andreas Gampee6215c02015-08-31 18:54:38 -07001010 }
1011 }
1012
1013 // Instance constructors must not be synchronized.
1014 if (IsInstanceConstructor()) {
1015 static constexpr uint32_t kForbidden = kAccSynchronized;
1016 if ((method_access_flags_ & kForbidden) != 0) {
1017 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
1018 return false;
1019 }
1020 }
1021 }
1022
David Srbecky346fd962020-07-27 16:51:00 +01001023 // Consistency-check of the register counts.
1024 // ins + locals = registers, so make sure that ins <= registers.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001025 if (code_item_accessor_.InsSize() > code_item_accessor_.RegistersSize()) {
1026 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins="
1027 << code_item_accessor_.InsSize()
1028 << " regs=" << code_item_accessor_.RegistersSize();
Ian Rogersd81871c2011-10-03 13:57:23 -07001029 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001030 }
Alex Lighteb7c1442015-08-31 13:17:42 -07001031
Ian Rogersd81871c2011-10-03 13:57:23 -07001032 // Allocate and initialize an array to hold instruction data.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001033 insn_flags_.reset(allocator_.AllocArray<InstructionFlags>(
1034 code_item_accessor_.InsnsSizeInCodeUnits()));
Mathieu Chartierde40d472015-10-15 17:47:48 -07001035 DCHECK(insn_flags_ != nullptr);
1036 std::uninitialized_fill_n(insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001037 code_item_accessor_.InsnsSizeInCodeUnits(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07001038 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -07001039 // Run through the instructions and see if the width checks out.
1040 bool result = ComputeWidthsAndCountOps();
Andreas Gampefef91cc2019-07-25 14:13:23 -07001041 bool allow_runtime_only_instructions = !IsAotMode() || verify_to_dump_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001042 // Flag instructions guarded by a "try" block and check exception handlers.
1043 result = result && ScanTryCatchBlocks();
1044 // Perform static instruction verification.
Andreas Gampebf1cb772017-05-15 15:39:00 -07001045 result = result && (allow_runtime_only_instructions
1046 ? VerifyInstructions<true>()
1047 : VerifyInstructions<false>());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001048 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +00001049 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -08001050
Vladimir Marko2b5eaa22013-12-13 13:59:30 +00001051 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -07001052}
1053
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001054template <bool kVerifierDebug>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001055bool MethodVerifier<kVerifierDebug>::ComputeWidthsAndCountOps() {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -07001056 // We can't assume the instruction is well formed, handle the case where calculating the size
1057 // goes past the end of the code item.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001058 SafeDexInstructionIterator it(code_item_accessor_.begin(), code_item_accessor_.end());
1059 for ( ; !it.IsErrorState() && it < code_item_accessor_.end(); ++it) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -07001060 // In case the instruction goes past the end of the code item, make sure to not process it.
1061 SafeDexInstructionIterator next = it;
1062 ++next;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001063 if (next.IsErrorState()) {
Mathieu Chartieraf7c9022017-10-27 09:42:46 -07001064 break;
1065 }
Andreas Gampe51de69e2019-04-19 15:14:14 -07001066 GetModifiableInstructionFlags(it.DexPc()).SetIsOpcode();
jeffhaobdb76512011-09-07 11:43:16 -07001067 }
1068
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001069 if (it != code_item_accessor_.end()) {
1070 const size_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaod5347e02012-03-22 17:25:05 -07001071 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001072 << it.DexPc() << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001073 return false;
1074 }
Andreas Gampe147a9112019-07-31 16:17:10 -07001075 DCHECK(GetInstructionFlags(0).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07001076
jeffhaobdb76512011-09-07 11:43:16 -07001077 return true;
1078}
1079
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001080template <bool kVerifierDebug>
1081bool MethodVerifier<kVerifierDebug>::ScanTryCatchBlocks() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001082 const uint32_t tries_size = code_item_accessor_.TriesSize();
jeffhaobdb76512011-09-07 11:43:16 -07001083 if (tries_size == 0) {
1084 return true;
1085 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001086 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001087 for (const dex::TryItem& try_item : code_item_accessor_.TryItems()) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001088 const uint32_t start = try_item.start_addr_;
1089 const uint32_t end = start + try_item.insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001090 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001091 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
1092 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001093 return false;
1094 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001095 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001096 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1097 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001098 return false;
1099 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001100 DexInstructionIterator end_it(code_item_accessor_.Insns(), end);
1101 for (DexInstructionIterator it(code_item_accessor_.Insns(), start); it < end_it; ++it) {
Andreas Gampe51de69e2019-04-19 15:14:14 -07001102 GetModifiableInstructionFlags(it.DexPc()).SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -07001103 }
1104 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001105 // Iterate over each of the handlers to verify target addresses.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001106 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
1107 const uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Andreas Gampee0bbab92019-07-25 12:28:22 -07001108 ClassLinker* linker = GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001109 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001110 CatchHandlerIterator iterator(handlers_ptr);
1111 for (; iterator.HasNext(); iterator.Next()) {
Igor Murashkin2ffb7032017-11-08 13:35:21 -08001112 uint32_t dex_pc = iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -07001113 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001114 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1115 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001116 return false;
1117 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001118 if (!CheckNotMoveResult(code_item_accessor_.Insns(), dex_pc)) {
Stephen Kyle9bc61992014-09-22 13:53:15 +01001119 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1120 << "exception handler begins with move-result* (" << dex_pc << ")";
1121 return false;
1122 }
Andreas Gampe51de69e2019-04-19 15:14:14 -07001123 GetModifiableInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001124 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1125 // unresolved exception types will be ignored by exception delivery
Andreas Gampea5b09a62016-11-17 15:21:22 -08001126 if (iterator.GetHandlerTypeIndex().IsValid()) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001127 ObjPtr<mirror::Class> exception_type =
1128 linker->ResolveType(iterator.GetHandlerTypeIndex(), dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07001129 if (exception_type == nullptr) {
1130 DCHECK(self_->IsExceptionPending());
1131 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001132 }
1133 }
jeffhaobdb76512011-09-07 11:43:16 -07001134 }
Ian Rogers0571d352011-11-03 19:51:38 -07001135 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001136 }
jeffhaobdb76512011-09-07 11:43:16 -07001137 return true;
1138}
1139
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001140template <bool kVerifierDebug>
Andreas Gampebf1cb772017-05-15 15:39:00 -07001141template <bool kAllowRuntimeOnlyInstructions>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001142bool MethodVerifier<kVerifierDebug>::VerifyInstructions() {
Andreas Gampe93adcb52019-06-06 20:16:07 -07001143 // Flag the start of the method as a branch target.
Andreas Gampe51de69e2019-04-19 15:14:14 -07001144 GetModifiableInstructionFlags(0).SetBranchTarget();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001145 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001146 const uint32_t dex_pc = inst.DexPc();
1147 if (!VerifyInstruction<kAllowRuntimeOnlyInstructions>(&inst.Inst(), dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001148 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001149 return false;
1150 }
Andreas Gampe93adcb52019-06-06 20:16:07 -07001151 // Flag some interesting instructions.
1152 if (inst->IsReturn()) {
1153 GetModifiableInstructionFlags(dex_pc).SetReturn();
1154 } else if (inst->Opcode() == Instruction::CHECK_CAST) {
1155 // The dex-to-dex compiler wants type information to elide check-casts.
Andreas Gampe51de69e2019-04-19 15:14:14 -07001156 GetModifiableInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -07001157 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001158 }
1159 return true;
1160}
1161
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001162template <bool kVerifierDebug>
Andreas Gampebf1cb772017-05-15 15:39:00 -07001163template <bool kAllowRuntimeOnlyInstructions>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001164bool MethodVerifier<kVerifierDebug>::VerifyInstruction(const Instruction* inst,
1165 uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001166 bool result = true;
1167 switch (inst->GetVerifyTypeArgumentA()) {
1168 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -07001169 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001170 break;
1171 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001172 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001173 break;
1174 }
1175 switch (inst->GetVerifyTypeArgumentB()) {
1176 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -07001177 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001178 break;
1179 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001180 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001181 break;
1182 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001183 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001184 break;
1185 case Instruction::kVerifyRegBNewInstance:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001186 result = result && CheckNewInstance(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001187 break;
1188 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001189 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001190 break;
1191 case Instruction::kVerifyRegBType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001192 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegB()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001193 break;
1194 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001195 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001196 break;
Orion Hodson2e599942017-09-22 16:17:41 +01001197 case Instruction::kVerifyRegBCallSite:
1198 result = result && CheckCallSiteIndex(inst->VRegB());
1199 break;
1200 case Instruction::kVerifyRegBMethodHandle:
1201 result = result && CheckMethodHandleIndex(inst->VRegB());
1202 break;
1203 case Instruction::kVerifyRegBPrototype:
1204 result = result && CheckPrototypeIndex(inst->VRegB());
1205 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001206 }
1207 switch (inst->GetVerifyTypeArgumentC()) {
1208 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001209 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001210 break;
1211 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001212 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001213 break;
1214 case Instruction::kVerifyRegCNewArray:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001215 result = result && CheckNewArray(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001216 break;
1217 case Instruction::kVerifyRegCType:
Andreas Gampea5b09a62016-11-17 15:21:22 -08001218 result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegC()));
Ian Rogersd81871c2011-10-03 13:57:23 -07001219 break;
1220 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001221 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001222 break;
1223 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01001224 switch (inst->GetVerifyTypeArgumentH()) {
1225 case Instruction::kVerifyRegHPrototype:
1226 result = result && CheckPrototypeIndex(inst->VRegH());
1227 break;
1228 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001229 switch (inst->GetVerifyExtraFlags()) {
1230 case Instruction::kVerifyArrayData:
1231 result = result && CheckArrayData(code_offset);
1232 break;
1233 case Instruction::kVerifyBranchTarget:
1234 result = result && CheckBranchTarget(code_offset);
1235 break;
1236 case Instruction::kVerifySwitchTargets:
1237 result = result && CheckSwitchTargets(code_offset);
1238 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001239 case Instruction::kVerifyVarArgNonZero:
1240 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001241 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001242 // Instructions that can actually return a negative value shouldn't have this flag.
1243 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1244 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1245 v_a > Instruction::kMaxVarArgRegs) {
1246 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001247 "non-range invoke";
1248 return false;
1249 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001250
Ian Rogers29a26482014-05-02 15:27:29 -07001251 uint32_t args[Instruction::kMaxVarArgRegs];
1252 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001253 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001254 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001255 }
Andreas Gampec3314312014-06-19 18:13:29 -07001256 case Instruction::kVerifyVarArgRangeNonZero:
1257 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001258 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001259 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1260 inst->VRegA() <= 0) {
1261 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1262 "range invoke";
1263 return false;
1264 }
Ian Rogers29a26482014-05-02 15:27:29 -07001265 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001266 break;
1267 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001268 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001269 result = false;
1270 break;
1271 }
Andreas Gampebf1cb772017-05-15 15:39:00 -07001272 if (!kAllowRuntimeOnlyInstructions && inst->GetVerifyIsRuntimeOnly()) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001273 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1274 result = false;
1275 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001276 return result;
1277}
1278
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001279template <bool kVerifierDebug>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001280inline bool MethodVerifier<kVerifierDebug>::CheckNewInstance(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001281 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001282 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001283 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001284 return false;
1285 }
1286 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001287 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Andreas Gampe29e81422017-05-15 16:29:32 -07001288 if (UNLIKELY(descriptor[0] != 'L')) {
jeffhaod5347e02012-03-22 17:25:05 -07001289 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001290 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001291 } else if (UNLIKELY(strcmp(descriptor, "Ljava/lang/Class;") == 0)) {
Aart Bikdb698f12016-07-25 17:52:22 -07001292 // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an
1293 // exception is thrown when this statement is executed (compiled code would not do that).
1294 Fail(VERIFY_ERROR_INSTANTIATION);
Ian Rogersd81871c2011-10-03 13:57:23 -07001295 }
1296 return true;
1297}
1298
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001299template <bool kVerifierDebug>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001300bool MethodVerifier<kVerifierDebug>::CheckNewArray(dex::TypeIndex idx) {
Andreas Gampe29e81422017-05-15 16:29:32 -07001301 if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001302 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
jeffhaod5347e02012-03-22 17:25:05 -07001303 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001304 return false;
1305 }
1306 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001307 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001308 const char* cp = descriptor;
1309 while (*cp++ == '[') {
1310 bracket_count++;
1311 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001312 if (UNLIKELY(bracket_count == 0)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001313 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001314 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1315 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001316 return false;
Andreas Gampe29e81422017-05-15 16:29:32 -07001317 } else if (UNLIKELY(bracket_count > 255)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001318 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001319 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1320 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001321 return false;
1322 }
1323 return true;
1324}
1325
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001326template <bool kVerifierDebug>
1327bool MethodVerifier<kVerifierDebug>::CheckArrayData(uint32_t cur_offset) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001328 const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
1329 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001330 const uint16_t* array_data;
1331 int32_t array_data_offset;
1332
1333 DCHECK_LT(cur_offset, insn_count);
1334 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001335 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001336 if (UNLIKELY(static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
1337 cur_offset + array_data_offset + 2 >= insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001338 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001339 << ", data offset " << array_data_offset
1340 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001341 return false;
1342 }
1343 /* offset to array data table is a relative branch-style offset */
1344 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001345 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001346 if (UNLIKELY(!IsAligned<4>(array_data))) {
jeffhaod5347e02012-03-22 17:25:05 -07001347 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1348 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001349 return false;
1350 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001351 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1352 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001353 if (UNLIKELY(!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001354 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1355 << ", data offset " << array_data_offset
1356 << " not correctly visited, probably bad padding.";
1357 return false;
1358 }
1359
Ian Rogersd81871c2011-10-03 13:57:23 -07001360 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001361 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001362 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1363 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001364 if (UNLIKELY(cur_offset + array_data_offset + table_size > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001365 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1366 << ", data offset " << array_data_offset << ", end "
1367 << cur_offset + array_data_offset + table_size
1368 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001369 return false;
1370 }
1371 return true;
1372}
1373
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001374template <bool kVerifierDebug>
1375bool MethodVerifier<kVerifierDebug>::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001376 int32_t offset;
1377 bool isConditional, selfOkay;
1378 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1379 return false;
1380 }
Andreas Gampe29e81422017-05-15 16:29:32 -07001381 if (UNLIKELY(!selfOkay && offset == 0)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001382 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1383 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001384 return false;
1385 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001386 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1387 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Andreas Gampe29e81422017-05-15 16:29:32 -07001388 if (UNLIKELY(((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset))) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001389 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1390 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001391 return false;
1392 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001393 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001394 if (UNLIKELY(abs_offset < 0 ||
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001395 (uint32_t) abs_offset >= code_item_accessor_.InsnsSizeInCodeUnits() ||
Andreas Gampe29e81422017-05-15 16:29:32 -07001396 !GetInstructionFlags(abs_offset).IsOpcode())) {
jeffhaod5347e02012-03-22 17:25:05 -07001397 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001398 << reinterpret_cast<void*>(abs_offset) << ") at "
1399 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001400 return false;
1401 }
Andreas Gampe51de69e2019-04-19 15:14:14 -07001402 GetModifiableInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001403 return true;
1404}
1405
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001406template <bool kVerifierDebug>
1407bool MethodVerifier<kVerifierDebug>::GetBranchOffset(uint32_t cur_offset,
1408 int32_t* pOffset,
1409 bool* pConditional,
1410 bool* selfOkay) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001411 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001412 *pConditional = false;
1413 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001414 switch (*insns & 0xff) {
1415 case Instruction::GOTO:
1416 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001417 break;
1418 case Instruction::GOTO_32:
1419 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001420 *selfOkay = true;
1421 break;
1422 case Instruction::GOTO_16:
1423 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001424 break;
1425 case Instruction::IF_EQ:
1426 case Instruction::IF_NE:
1427 case Instruction::IF_LT:
1428 case Instruction::IF_GE:
1429 case Instruction::IF_GT:
1430 case Instruction::IF_LE:
1431 case Instruction::IF_EQZ:
1432 case Instruction::IF_NEZ:
1433 case Instruction::IF_LTZ:
1434 case Instruction::IF_GEZ:
1435 case Instruction::IF_GTZ:
1436 case Instruction::IF_LEZ:
1437 *pOffset = (int16_t) insns[1];
1438 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001439 break;
1440 default:
1441 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001442 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001443 return true;
1444}
1445
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001446template <bool kVerifierDebug>
1447bool MethodVerifier<kVerifierDebug>::CheckSwitchTargets(uint32_t cur_offset) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001448 const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001449 DCHECK_LT(cur_offset, insn_count);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001450 const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001451 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001452 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001453 if (UNLIKELY(static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1454 cur_offset + switch_offset + 2 > insn_count)) {
jeffhaod5347e02012-03-22 17:25:05 -07001455 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001456 << ", switch offset " << switch_offset
1457 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001458 return false;
1459 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001460 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001461 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001462 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
Andreas Gampe29e81422017-05-15 16:29:32 -07001463 if (UNLIKELY(!IsAligned<4>(switch_insns))) {
jeffhaod5347e02012-03-22 17:25:05 -07001464 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1465 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001466 return false;
1467 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001468 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1469 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Andreas Gampe29e81422017-05-15 16:29:32 -07001470 if (UNLIKELY(!GetInstructionFlags(cur_offset + switch_offset).IsOpcode())) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001471 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1472 << ", switch offset " << switch_offset
1473 << " not correctly visited, probably bad padding.";
1474 return false;
1475 }
1476
David Brazdil5469d342015-09-25 16:57:53 +01001477 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1478
Ian Rogersd81871c2011-10-03 13:57:23 -07001479 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001480 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001481 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001482 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001483 /* 0=sig, 1=count, 2/3=firstKey */
1484 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001485 expected_signature = Instruction::kPackedSwitchSignature;
1486 } else {
1487 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001488 targets_offset = 2 + 2 * switch_count;
1489 expected_signature = Instruction::kSparseSwitchSignature;
1490 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001491 uint32_t table_size = targets_offset + switch_count * 2;
Andreas Gampe29e81422017-05-15 16:29:32 -07001492 if (UNLIKELY(switch_insns[0] != expected_signature)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001493 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1494 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1495 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001496 return false;
1497 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001498 /* make sure the end of the switch is in range */
Andreas Gampe29e81422017-05-15 16:29:32 -07001499 if (UNLIKELY(cur_offset + switch_offset + table_size > (uint32_t) insn_count)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001500 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1501 << ", switch offset " << switch_offset
1502 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001503 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001504 return false;
1505 }
David Brazdil5469d342015-09-25 16:57:53 +01001506
1507 constexpr int32_t keys_offset = 2;
1508 if (switch_count > 1) {
1509 if (is_packed_switch) {
1510 /* for a packed switch, verify that keys do not overflow int32 */
1511 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1512 int32_t max_first_key =
1513 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
Andreas Gampe29e81422017-05-15 16:29:32 -07001514 if (UNLIKELY(first_key > max_first_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001515 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1516 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001517 return false;
1518 }
David Brazdil5469d342015-09-25 16:57:53 +01001519 } else {
1520 /* for a sparse switch, verify the keys are in ascending order */
1521 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1522 for (uint32_t targ = 1; targ < switch_count; targ++) {
1523 int32_t key =
1524 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1525 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
Andreas Gampe29e81422017-05-15 16:29:32 -07001526 if (UNLIKELY(key <= last_key)) {
David Brazdil5469d342015-09-25 16:57:53 +01001527 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1528 << ", this=" << key;
1529 return false;
1530 }
1531 last_key = key;
1532 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001533 }
1534 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001535 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001536 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001537 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1538 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001539 int32_t abs_offset = cur_offset + offset;
Andreas Gampe29e81422017-05-15 16:29:32 -07001540 if (UNLIKELY(abs_offset < 0 ||
1541 abs_offset >= static_cast<int32_t>(insn_count) ||
1542 !GetInstructionFlags(abs_offset).IsOpcode())) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001543 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1544 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1545 << reinterpret_cast<void*>(cur_offset)
1546 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001547 return false;
1548 }
Andreas Gampe51de69e2019-04-19 15:14:14 -07001549 GetModifiableInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001550 }
1551 return true;
1552}
1553
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001554template <bool kVerifierDebug>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001555bool MethodVerifier<kVerifierDebug>::VerifyCodeFlow() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001556 const uint16_t registers_size = code_item_accessor_.RegistersSize();
jeffhaobdb76512011-09-07 11:43:16 -07001557
Ian Rogersd81871c2011-10-03 13:57:23 -07001558 /* Create and initialize table holding register status */
Nicolas Geoffray2cb25272021-07-15 14:24:29 +01001559 reg_table_.Init(insn_flags_.get(),
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001560 code_item_accessor_.InsnsSizeInCodeUnits(),
Brian Carlstrom93c33962013-07-26 10:37:43 -07001561 registers_size,
Andreas Gamped09c0592019-04-19 15:44:05 -07001562 allocator_,
Nicolas Geoffray2cb25272021-07-15 14:24:29 +01001563 GetRegTypeCache(),
1564 interesting_dex_pc_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001565
Andreas Gamped09c0592019-04-19 15:44:05 -07001566 work_line_.reset(RegisterLine::Create(registers_size, allocator_, GetRegTypeCache()));
1567 saved_line_.reset(RegisterLine::Create(registers_size, allocator_, GetRegTypeCache()));
jeffhaobdb76512011-09-07 11:43:16 -07001568
Ian Rogersd81871c2011-10-03 13:57:23 -07001569 /* Initialize register types of method arguments. */
1570 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001571 DCHECK_NE(failures_.size(), 0U);
1572 std::string prepend("Bad signature in ");
David Sehr709b0702016-10-13 09:12:37 -07001573 prepend += dex_file_->PrettyMethod(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001574 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001575 return false;
1576 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001577 // We may have a runtime failure here, clear.
Andreas Gampe43884b22019-06-27 14:05:52 -07001578 flags_.have_pending_runtime_throw_failure_ = false;
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001579
Ian Rogersd81871c2011-10-03 13:57:23 -07001580 /* Perform code flow verification. */
Andreas Gampeb90cad52019-07-31 16:15:41 -07001581 bool res = LIKELY(monitor_enter_dex_pcs_ == nullptr)
1582 ? CodeFlowVerifyMethod</*kMonitorDexPCs=*/ false>()
1583 : CodeFlowVerifyMethod</*kMonitorDexPCs=*/ true>();
1584 if (UNLIKELY(!res)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001585 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001586 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001587 }
jeffhaobdb76512011-09-07 11:43:16 -07001588 return true;
1589}
1590
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001591template <bool kVerifierDebug>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001592void MethodVerifier<kVerifierDebug>::Dump(VariableIndentationOutputStream* vios) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001593 if (!code_item_accessor_.HasCodeItem()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001594 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001595 return;
jeffhaobdb76512011-09-07 11:43:16 -07001596 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001597 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001598 vios->Stream() << "Register Types:\n";
1599 ScopedIndentation indent1(vios);
1600 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001601 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001602 vios->Stream() << "Dumping instructions and register lines:\n";
1603 ScopedIndentation indent1(vios);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001604
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001605 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001606 const size_t dex_pc = inst.DexPc();
Andreas Gampe077d9db2018-01-19 18:54:14 -08001607
1608 // Might be asked to dump before the table is initialized.
1609 if (reg_table_.IsInitialized()) {
1610 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1611 if (reg_line != nullptr) {
1612 vios->Stream() << reg_line->Dump(this) << "\n";
1613 }
jeffhaobdb76512011-09-07 11:43:16 -07001614 }
Andreas Gampe077d9db2018-01-19 18:54:14 -08001615
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001616 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001617 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001618 const bool kDumpHexOfInstruction = false;
1619 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001620 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001621 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001622 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001623 }
jeffhaobdb76512011-09-07 11:43:16 -07001624}
1625
Ian Rogersd81871c2011-10-03 13:57:23 -07001626static bool IsPrimitiveDescriptor(char descriptor) {
1627 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001628 case 'I':
1629 case 'C':
1630 case 'S':
1631 case 'B':
1632 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001633 case 'F':
1634 case 'D':
1635 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001636 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001637 default:
1638 return false;
1639 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001640}
1641
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001642template <bool kVerifierDebug>
1643bool MethodVerifier<kVerifierDebug>::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001644 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001645
1646 // Should have been verified earlier.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001647 DCHECK_GE(code_item_accessor_.RegistersSize(), code_item_accessor_.InsSize());
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001648
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001649 uint32_t arg_start = code_item_accessor_.RegistersSize() - code_item_accessor_.InsSize();
1650 size_t expected_args = code_item_accessor_.InsSize(); /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001651
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001652 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001653 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001654 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001655 if (expected_args == 0) {
1656 // Expect at least a receiver.
1657 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1658 return false;
1659 }
1660
Ian Rogersd81871c2011-10-03 13:57:23 -07001661 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1662 // argument as uninitialized. This restricts field access until the superclass constructor is
1663 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001664 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001665 if (IsConstructor()) {
1666 if (declaring_class.IsJavaLangObject()) {
1667 // "this" is implicitly initialized.
1668 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001669 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001670 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001671 reg_line->SetRegisterType<LockOp::kClear>(
1672 this,
1673 arg_start + cur_arg,
1674 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001675 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001676 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001677 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001678 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001679 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001680 }
1681
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001682 const dex::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001683 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001684 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001685
1686 for (; iterator.HasNext(); iterator.Next()) {
1687 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001688 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001689 LOG(FATAL) << "Null descriptor";
1690 }
1691 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001692 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1693 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001694 return false;
1695 }
1696 switch (descriptor[0]) {
1697 case 'L':
1698 case '[':
1699 // We assume that reference arguments are initialized. The only way it could be otherwise
1700 // (assuming the caller was verified) is if the current method is <init>, but in that case
1701 // it's effectively considered initialized the instant we reach here (in the sense that we
1702 // can return without doing anything or call virtual methods).
1703 {
Andreas Gampe98be1a92017-08-28 08:25:45 -07001704 // Note: don't check access. No error would be thrown for declaring or passing an
1705 // inaccessible class. Only actual accesses to fields or methods will.
1706 const RegType& reg_type = ResolveClass<CheckAccess::kNo>(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001707 if (!reg_type.IsNonZeroReferenceTypes()) {
1708 DCHECK(HasFailures());
1709 return false;
1710 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001711 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001712 }
1713 break;
1714 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001715 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001716 break;
1717 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001718 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001719 break;
1720 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001721 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001722 break;
1723 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001724 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001725 break;
1726 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001727 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001728 break;
1729 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001730 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001731 break;
1732 case 'J':
1733 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001734 if (cur_arg + 1 >= expected_args) {
1735 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1736 << " args, found more (" << descriptor << ")";
1737 return false;
1738 }
1739
Ian Rogers7b078e82014-09-10 14:44:24 -07001740 const RegType* lo_half;
1741 const RegType* hi_half;
1742 if (descriptor[0] == 'J') {
1743 lo_half = &reg_types_.LongLo();
1744 hi_half = &reg_types_.LongHi();
1745 } else {
1746 lo_half = &reg_types_.DoubleLo();
1747 hi_half = &reg_types_.DoubleHi();
1748 }
1749 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001750 cur_arg++;
1751 break;
1752 }
1753 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001754 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1755 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001756 return false;
1757 }
1758 cur_arg++;
1759 }
1760 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001761 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1762 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001763 return false;
1764 }
1765 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1766 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1767 // format. Only major difference from the method argument format is that 'V' is supported.
1768 bool result;
1769 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1770 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001771 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001772 size_t i = 0;
1773 do {
1774 i++;
1775 } while (descriptor[i] == '['); // process leading [
1776 if (descriptor[i] == 'L') { // object array
1777 do {
1778 i++; // find closing ;
1779 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1780 result = descriptor[i] == ';';
1781 } else { // primitive array
1782 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1783 }
1784 } else if (descriptor[0] == 'L') {
1785 // could be more thorough here, but shouldn't be required
1786 size_t i = 0;
1787 do {
1788 i++;
1789 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1790 result = descriptor[i] == ';';
1791 } else {
1792 result = false;
1793 }
1794 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001795 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1796 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001797 }
1798 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001799}
1800
Andreas Gampeb90cad52019-07-31 16:15:41 -07001801COLD_ATTR
1802void HandleMonitorDexPcsWorkLine(
1803 std::vector<::art::verifier::MethodVerifier::DexLockInfo>* monitor_enter_dex_pcs,
1804 RegisterLine* work_line) {
1805 monitor_enter_dex_pcs->clear(); // The new work line is more accurate than the previous one.
1806
1807 std::map<uint32_t, ::art::verifier::MethodVerifier::DexLockInfo> depth_to_lock_info;
1808 auto collector = [&](uint32_t dex_reg, uint32_t depth) {
1809 auto insert_pair = depth_to_lock_info.emplace(
1810 depth, ::art::verifier::MethodVerifier::DexLockInfo(depth));
1811 auto it = insert_pair.first;
1812 auto set_insert_pair = it->second.dex_registers.insert(dex_reg);
1813 DCHECK(set_insert_pair.second);
1814 };
1815 work_line->IterateRegToLockDepths(collector);
1816 for (auto& pair : depth_to_lock_info) {
1817 monitor_enter_dex_pcs->push_back(pair.second);
1818 // Map depth to dex PC.
Andreas Gampe4ec4d482019-08-01 10:00:23 -07001819 monitor_enter_dex_pcs->back().dex_pc = work_line->GetMonitorEnterDexPc(pair.second.dex_pc);
Andreas Gampeb90cad52019-07-31 16:15:41 -07001820 }
1821}
1822
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001823template <bool kVerifierDebug>
Andreas Gampeb90cad52019-07-31 16:15:41 -07001824template <bool kMonitorDexPCs>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001825bool MethodVerifier<kVerifierDebug>::CodeFlowVerifyMethod() {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001826 const uint16_t* insns = code_item_accessor_.Insns();
1827 const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
jeffhaoba5ebb92011-08-25 17:24:37 -07001828
jeffhaobdb76512011-09-07 11:43:16 -07001829 /* Begin by marking the first instruction as "changed". */
Andreas Gampe51de69e2019-04-19 15:14:14 -07001830 GetModifiableInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001831 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001832
jeffhaobdb76512011-09-07 11:43:16 -07001833 /* Continue until no instructions are marked "changed". */
1834 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001835 if (allow_thread_suspension_) {
1836 self_->AllowThreadSuspension();
1837 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001838 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1839 uint32_t insn_idx = start_guess;
1840 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001841 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001842 break;
1843 }
jeffhaobdb76512011-09-07 11:43:16 -07001844 if (insn_idx == insns_size) {
1845 if (start_guess != 0) {
1846 /* try again, starting from the top */
1847 start_guess = 0;
1848 continue;
1849 } else {
1850 /* all flags are clear */
1851 break;
1852 }
1853 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001854 // We carry the working set of registers from instruction to instruction. If this address can
1855 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1856 // "changed" flags, we need to load the set of registers from the table.
1857 // Because we always prefer to continue on to the next instruction, we should never have a
1858 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1859 // target.
1860 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001861 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001862 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001863 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001864 /*
David Srbecky346fd962020-07-27 16:51:00 +01001865 * Consistency check: retrieve the stored register line (assuming
jeffhaobdb76512011-09-07 11:43:16 -07001866 * a full table) and make sure it actually matches.
1867 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001868 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001869 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001870 if (work_line_->CompareLine(register_line) != 0) {
Andreas Gampe4a57d5f2019-04-03 12:31:18 -07001871 Dump(LOG_STREAM(FATAL_WITHOUT_ABORT));
1872 LOG(FATAL_WITHOUT_ABORT) << info_messages_.str();
David Sehr709b0702016-10-13 09:12:37 -07001873 LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001874 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001875 << " work_line=" << work_line_->Dump(this) << "\n"
1876 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001877 }
jeffhaobdb76512011-09-07 11:43:16 -07001878 }
jeffhaobdb76512011-09-07 11:43:16 -07001879 }
Andreas Gampeb90cad52019-07-31 16:15:41 -07001880
1881 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1882 // We want the state _before_ the instruction, for the case where the dex pc we're
1883 // interested in is itself a monitor-enter instruction (which is a likely place
1884 // for a thread to be suspended).
1885 if (kMonitorDexPCs && UNLIKELY(work_insn_idx_ == interesting_dex_pc_)) {
1886 HandleMonitorDexPcsWorkLine(monitor_enter_dex_pcs_, work_line_.get());
1887 }
1888
Ian Rogersd81871c2011-10-03 13:57:23 -07001889 if (!CodeFlowVerifyInstruction(&start_guess)) {
David Sehr709b0702016-10-13 09:12:37 -07001890 std::string prepend(dex_file_->PrettyMethod(dex_method_idx_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001891 prepend += " failed to verify: ";
1892 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001893 return false;
1894 }
jeffhaobdb76512011-09-07 11:43:16 -07001895 /* Clear "changed" and mark as visited. */
Andreas Gampe51de69e2019-04-19 15:14:14 -07001896 GetModifiableInstructionFlags(insn_idx).SetVisited();
1897 GetModifiableInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001898 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001899
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001900 if (kVerifierDebug) {
jeffhaobdb76512011-09-07 11:43:16 -07001901 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001902 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001903 * (besides the wasted space), but it indicates a flaw somewhere
1904 * down the line, possibly in the verifier.
1905 *
1906 * If we've substituted "always throw" instructions into the stream,
1907 * we are almost certainly going to have some dead code.
1908 */
1909 int dead_start = -1;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001910
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001911 for (const DexInstructionPcPair& inst : code_item_accessor_) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001912 const uint32_t insn_idx = inst.DexPc();
jeffhaobdb76512011-09-07 11:43:16 -07001913 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001914 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001915 * may or may not be preceded by a padding NOP (for alignment).
1916 */
1917 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1918 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1919 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001920 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001921 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1922 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1923 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Andreas Gampe51de69e2019-04-19 15:14:14 -07001924 GetModifiableInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001925 }
1926
Mathieu Chartierde40d472015-10-15 17:47:48 -07001927 if (!GetInstructionFlags(insn_idx).IsVisited()) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001928 if (dead_start < 0) {
jeffhaobdb76512011-09-07 11:43:16 -07001929 dead_start = insn_idx;
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001930 }
jeffhaobdb76512011-09-07 11:43:16 -07001931 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001932 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1933 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001934 dead_start = -1;
1935 }
1936 }
1937 if (dead_start >= 0) {
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07001938 LogVerifyInfo()
1939 << "dead code " << reinterpret_cast<void*>(dead_start)
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08001940 << "-" << reinterpret_cast<void*>(code_item_accessor_.InsnsSizeInCodeUnits() - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001941 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001942 // To dump the state of the verify after a method, do something like:
David Sehr709b0702016-10-13 09:12:37 -07001943 // if (dex_file_->PrettyMethod(dex_method_idx_) ==
Ian Rogersc9e463c2013-06-05 16:52:26 -07001944 // "boolean java.lang.String.equals(java.lang.Object)") {
1945 // LOG(INFO) << info_messages_.str();
1946 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001947 }
jeffhaobdb76512011-09-07 11:43:16 -07001948 return true;
1949}
1950
Andreas Gampea727e372015-08-25 09:22:37 -07001951// Setup a register line for the given return instruction.
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001952template <bool kVerifierDebug>
1953static void AdjustReturnLine(MethodVerifier<kVerifierDebug>* verifier,
Andreas Gampea727e372015-08-25 09:22:37 -07001954 const Instruction* ret_inst,
1955 RegisterLine* line) {
1956 Instruction::Code opcode = ret_inst->Opcode();
1957
1958 switch (opcode) {
1959 case Instruction::RETURN_VOID:
Andreas Gampe51de69e2019-04-19 15:14:14 -07001960 if (verifier->IsInstanceConstructor()) {
1961 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
1962 line->CheckConstructorReturn(verifier);
1963 }
1964 line->MarkAllRegistersAsConflicts(verifier);
Andreas Gampea727e372015-08-25 09:22:37 -07001965 break;
1966
1967 case Instruction::RETURN:
1968 case Instruction::RETURN_OBJECT:
1969 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1970 break;
1971
1972 case Instruction::RETURN_WIDE:
1973 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1974 break;
1975
1976 default:
1977 LOG(FATAL) << "Unknown return opcode " << opcode;
1978 UNREACHABLE();
1979 }
1980}
1981
Andreas Gampe99db7bb2019-04-19 23:05:47 -07001982template <bool kVerifierDebug>
1983bool MethodVerifier<kVerifierDebug>::CodeFlowVerifyInstruction(uint32_t* start_guess) {
jeffhaobdb76512011-09-07 11:43:16 -07001984 /*
1985 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001986 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001987 * control to another statement:
1988 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001989 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001990 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001991 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001992 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001993 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001994 * throw an exception that is handled by an encompassing "try"
1995 * block.
1996 *
1997 * We can also return, in which case there is no successor instruction
1998 * from this point.
1999 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08002000 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07002001 */
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002002 const uint16_t* insns = code_item_accessor_.Insns() + work_insn_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -07002003 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07002004 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07002005
jeffhaobdb76512011-09-07 11:43:16 -07002006 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07002007 bool just_set_result = false;
Andreas Gampe99db7bb2019-04-19 23:05:47 -07002008 if (kVerifierDebug) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002009 // Generate processing back trace to debug verifier
Andreas Gampe2ad6cce2019-04-11 16:17:39 -07002010 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
2011 << work_line_->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07002012 }
jeffhaobdb76512011-09-07 11:43:16 -07002013
2014 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002015 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07002016 * can throw an exception, we will copy/merge this into the "catch"
2017 * address rather than work_line, because we don't want the result
2018 * from the "successful" code path (e.g. a check-cast that "improves"
2019 * a type) to be visible to the exception handler.
2020 */
Andreas Gampec6582782019-07-10 15:06:18 -07002021 if (((opcode_flags & Instruction::kThrow) != 0 || IsCompatThrow(inst->Opcode())) &&
2022 CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002023 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002024 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002025 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07002026 }
Andreas Gampe43884b22019-06-27 14:05:52 -07002027 // Per-instruction flag, should not be set here.
2028 DCHECK(!flags_.have_pending_runtime_throw_failure_);
Andreas Gampe6087bc22019-06-03 15:52:08 -07002029 bool exc_handler_unreachable = false;
jeffhaobdb76512011-09-07 11:43:16 -07002030
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07002031
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002032 // We need to ensure the work line is consistent while performing validation. When we spot a
2033 // peephole pattern we compute a new line for either the fallthrough instruction or the
2034 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08002035 RegisterLineArenaUniquePtr branch_line;
2036 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002037
Sebastien Hertz5243e912013-05-21 10:55:07 +02002038 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07002039 case Instruction::NOP:
2040 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002041 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07002042 * a signature that looks like a NOP; if we see one of these in
2043 * the course of executing code then we have a problem.
2044 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002045 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07002046 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002047 }
2048 break;
2049
2050 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002051 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002052 break;
jeffhaobdb76512011-09-07 11:43:16 -07002053 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002054 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002055 break;
jeffhaobdb76512011-09-07 11:43:16 -07002056 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002057 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002058 break;
2059 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002060 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002061 break;
jeffhaobdb76512011-09-07 11:43:16 -07002062 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002063 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002064 break;
jeffhaobdb76512011-09-07 11:43:16 -07002065 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002066 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07002067 break;
2068 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002069 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002070 break;
jeffhaobdb76512011-09-07 11:43:16 -07002071 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002072 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002073 break;
jeffhaobdb76512011-09-07 11:43:16 -07002074 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002075 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002076 break;
2077
2078 /*
2079 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002080 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002081 * might want to hold the result in an actual CPU register, so the
2082 * Dalvik spec requires that these only appear immediately after an
2083 * invoke or filled-new-array.
2084 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002085 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002086 * redundant with the reset done below, but it can make the debug info
2087 * easier to read in some cases.)
2088 */
2089 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002090 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002091 break;
2092 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002093 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002094 break;
2095 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002096 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002097 break;
2098
Andreas Gampe6087bc22019-06-03 15:52:08 -07002099 case Instruction::MOVE_EXCEPTION:
2100 if (!HandleMoveException(inst)) {
2101 exc_handler_unreachable = true;
Sebastien Hertz270a0e12015-01-16 19:49:09 +01002102 }
jeffhaobdb76512011-09-07 11:43:16 -07002103 break;
Andreas Gampe6087bc22019-06-03 15:52:08 -07002104
jeffhaobdb76512011-09-07 11:43:16 -07002105 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002106 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002107 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002108 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002109 }
jeffhaobdb76512011-09-07 11:43:16 -07002110 }
2111 break;
2112 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002113 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002114 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002115 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002116 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002117 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2118 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002119 } else {
2120 // Compilers may generate synthetic functions that write byte values into boolean fields.
2121 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002122 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002123 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002124 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2125 ((return_type.IsBoolean() || return_type.IsByte() ||
2126 return_type.IsShort() || return_type.IsChar()) &&
2127 src_type.IsInteger()));
2128 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002129 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002130 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002131 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002132 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002133 }
jeffhaobdb76512011-09-07 11:43:16 -07002134 }
2135 }
2136 break;
2137 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002138 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002139 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002140 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002141 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002142 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002143 } else {
2144 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002145 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002146 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002147 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002148 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002149 }
jeffhaobdb76512011-09-07 11:43:16 -07002150 }
2151 }
2152 break;
2153 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002154 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002155 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002156 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002157 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002158 } else {
2159 /* return_type is the *expected* return type, not register value */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002160 DCHECK(!return_type.IsZeroOrNull());
Ian Rogersd81871c2011-10-03 13:57:23 -07002161 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002162 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002163 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002164 // Disallow returning undefined, conflict & uninitialized values and verify that the
2165 // reference in vAA is an instance of the "return_type."
2166 if (reg_type.IsUndefined()) {
2167 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2168 } else if (reg_type.IsConflict()) {
2169 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2170 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002171 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002172 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002173 } else if (!reg_type.IsReferenceTypes()) {
2174 // We really do expect a reference here.
2175 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2176 << reg_type;
David Brazdilca3c8c32016-09-06 14:04:48 +01002177 } else if (!return_type.IsAssignableFrom(reg_type, this)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002178 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
Nicolas Geoffray04ea42c2021-01-22 10:04:56 +00002179 Fail(api_level_ > 29u
2180 ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_UNRESOLVED_TYPE_CHECK)
Andreas Gampebc919af2019-07-10 14:09:51 -07002181 << " can't resolve returned type '" << return_type << "' or '" << reg_type << "'";
Jeff Haoa3faaf42013-09-03 19:07:00 -07002182 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002183 bool soft_error = false;
2184 // Check whether arrays are involved. They will show a valid class status, even
2185 // if their components are erroneous.
2186 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
David Brazdilca3c8c32016-09-06 14:04:48 +01002187 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error);
Andreas Gampe16f149c2015-03-23 10:10:20 -07002188 if (soft_error) {
2189 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2190 << reg_type << " vs " << return_type;
2191 }
2192 }
2193
2194 if (!soft_error) {
2195 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2196 << "', but expected from declaration '" << return_type << "'";
2197 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002198 }
jeffhaobdb76512011-09-07 11:43:16 -07002199 }
2200 }
2201 }
2202 break;
2203
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002204 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002205 case Instruction::CONST_4: {
2206 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002207 work_line_->SetRegisterType<LockOp::kClear>(
Nicolas Geoffray2cb25272021-07-15 14:24:29 +01002208 this, inst->VRegA_11n(), DetermineCat1Constant(val));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002209 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002210 }
2211 case Instruction::CONST_16: {
2212 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002213 work_line_->SetRegisterType<LockOp::kClear>(
Nicolas Geoffray2cb25272021-07-15 14:24:29 +01002214 this, inst->VRegA_21s(), DetermineCat1Constant(val));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002215 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002216 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002217 case Instruction::CONST: {
2218 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002219 work_line_->SetRegisterType<LockOp::kClear>(
Nicolas Geoffray2cb25272021-07-15 14:24:29 +01002220 this, inst->VRegA_31i(), DetermineCat1Constant(val));
jeffhaobdb76512011-09-07 11:43:16 -07002221 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002222 }
2223 case Instruction::CONST_HIGH16: {
2224 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002225 work_line_->SetRegisterType<LockOp::kClear>(
Nicolas Geoffray2cb25272021-07-15 14:24:29 +01002226 this, inst->VRegA_21h(), DetermineCat1Constant(val));
jeffhaobdb76512011-09-07 11:43:16 -07002227 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002228 }
jeffhaobdb76512011-09-07 11:43:16 -07002229 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002230 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002231 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002232 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2233 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002234 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002235 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002236 }
2237 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002238 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002239 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2240 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002241 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002242 break;
2243 }
2244 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002245 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002246 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2247 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002248 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002249 break;
2250 }
2251 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002252 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002253 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2254 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002255 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002256 break;
2257 }
jeffhaobdb76512011-09-07 11:43:16 -07002258 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002259 work_line_->SetRegisterType<LockOp::kClear>(
2260 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002261 break;
jeffhaobdb76512011-09-07 11:43:16 -07002262 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002263 work_line_->SetRegisterType<LockOp::kClear>(
2264 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002265 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002266 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002267 // Get type from instruction if unresolved then we need an access check
2268 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Andreas Gampe98be1a92017-08-28 08:25:45 -07002269 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002270 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002271 work_line_->SetRegisterType<LockOp::kClear>(
2272 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2273 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002274 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002275 }
Orion Hodson2e599942017-09-22 16:17:41 +01002276 case Instruction::CONST_METHOD_HANDLE:
2277 work_line_->SetRegisterType<LockOp::kClear>(
2278 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodHandle());
Orion Hodson2e599942017-09-22 16:17:41 +01002279 break;
2280 case Instruction::CONST_METHOD_TYPE:
2281 work_line_->SetRegisterType<LockOp::kClear>(
2282 this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodType());
Orion Hodson2e599942017-09-22 16:17:41 +01002283 break;
jeffhaobdb76512011-09-07 11:43:16 -07002284 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002285 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002286 // Check whether the previous instruction is a move-object with vAA as a source, creating
2287 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002288 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002289 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002290 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002291 prev_idx--;
2292 }
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002293 const Instruction& prev_inst = code_item_accessor_.InstructionAt(prev_idx);
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002294 switch (prev_inst.Opcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002295 case Instruction::MOVE_OBJECT:
2296 case Instruction::MOVE_OBJECT_16:
2297 case Instruction::MOVE_OBJECT_FROM16:
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002298 if (prev_inst.VRegB() == inst->VRegA_11x()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002299 // Redo the copy. This won't change the register types, but update the lock status
2300 // for the aliased register.
2301 work_line_->CopyRegister1(this,
Mathieu Chartier1d2d4ff2017-09-23 16:11:06 -07002302 prev_inst.VRegA(),
2303 prev_inst.VRegB(),
Andreas Gampec1474102015-08-18 08:57:44 -07002304 kTypeCategoryRef);
2305 }
2306 break;
2307
Alexey Grebenkince750492018-05-31 23:42:20 +03002308 // Catch a case of register aliasing when two registers are linked to the same
2309 // java.lang.Class object via two consequent const-class instructions immediately
2310 // preceding monitor-enter called on one of those registers.
2311 case Instruction::CONST_CLASS: {
2312 // Get the second previous instruction.
2313 if (prev_idx == 0 || GetInstructionFlags(prev_idx).IsBranchTarget()) {
2314 break;
2315 }
2316 prev_idx--;
2317 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
2318 prev_idx--;
2319 }
2320 const Instruction& prev2_inst = code_item_accessor_.InstructionAt(prev_idx);
2321
2322 // Match the pattern "const-class; const-class; monitor-enter;"
2323 if (prev2_inst.Opcode() != Instruction::CONST_CLASS) {
2324 break;
2325 }
2326
2327 // Ensure both const-classes are called for the same type_idx.
2328 if (prev_inst.VRegB_21c() != prev2_inst.VRegB_21c()) {
2329 break;
2330 }
2331
2332 // Update the lock status for the aliased register.
2333 if (prev_inst.VRegA() == inst->VRegA_11x()) {
2334 work_line_->CopyRegister1(this,
2335 prev2_inst.VRegA(),
2336 inst->VRegA_11x(),
2337 kTypeCategoryRef);
2338 } else if (prev2_inst.VRegA() == inst->VRegA_11x()) {
2339 work_line_->CopyRegister1(this,
2340 prev_inst.VRegA(),
2341 inst->VRegA_11x(),
2342 kTypeCategoryRef);
2343 }
2344 break;
2345 }
2346
Andreas Gampec1474102015-08-18 08:57:44 -07002347 default: // Other instruction types ignored.
2348 break;
2349 }
2350 }
jeffhaobdb76512011-09-07 11:43:16 -07002351 break;
2352 case Instruction::MONITOR_EXIT:
2353 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002354 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002355 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002356 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002357 * to the need to handle asynchronous exceptions, a now-deprecated
2358 * feature that Dalvik doesn't support.)
2359 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002360 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002361 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002362 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002363 * structured locking checks are working, the former would have
2364 * failed on the -enter instruction, and the latter is impossible.
2365 *
2366 * This is fortunate, because issue 3221411 prevents us from
2367 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002368 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002369 * some catch blocks (which will show up as "dead" code when
2370 * we skip them here); if we can't, then the code path could be
2371 * "live" so we still need to check it.
2372 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002373 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002374 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002375 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002376 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002377 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002378 /*
2379 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2380 * could be a "upcast" -- not expected, so we don't try to address it.)
2381 *
2382 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002383 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002384 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002385 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002386 const dex::TypeIndex type_idx((is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c());
Andreas Gampe98be1a92017-08-28 08:25:45 -07002387 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002388 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002389 // If this is a primitive type, fail HARD.
Andreas Gampee0bbab92019-07-25 12:28:22 -07002390 ObjPtr<mirror::Class> klass = GetClassLinker()->LookupResolvedType(
Vladimir Marko666ee3d2017-12-11 18:37:36 +00002391 type_idx, dex_cache_.Get(), class_loader_.Get());
Andreas Gampe00633eb2014-07-17 16:13:35 -07002392 if (klass != nullptr && klass->IsPrimitive()) {
2393 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2394 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2395 << GetDeclaringClass();
2396 break;
2397 }
2398
Ian Rogersad0b3a32012-04-16 14:50:24 -07002399 DCHECK_NE(failures_.size(), 0U);
2400 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002401 work_line_->SetRegisterType<LockOp::kClear>(this,
2402 inst->VRegA_22c(),
2403 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002404 }
2405 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002406 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002407 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002408 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002409 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002410 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002411 if (is_checkcast) {
2412 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2413 } else {
2414 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2415 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002416 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002417 if (is_checkcast) {
2418 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2419 } else {
2420 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2421 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002422 } else if (orig_type.IsUninitializedTypes()) {
2423 if (is_checkcast) {
2424 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2425 << orig_type_reg;
2426 } else {
2427 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2428 << orig_type_reg;
2429 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002430 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002431 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002432 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002433 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002434 work_line_->SetRegisterType<LockOp::kClear>(this,
2435 inst->VRegA_22c(),
2436 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002437 }
jeffhaobdb76512011-09-07 11:43:16 -07002438 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002439 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002440 }
2441 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002442 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002443 if (res_type.IsReferenceTypes()) {
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002444 if (!res_type.IsArrayTypes() && !res_type.IsZeroOrNull()) {
2445 // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002446 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002447 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002448 work_line_->SetRegisterType<LockOp::kClear>(this,
2449 inst->VRegA_12x(),
2450 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002451 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002452 } else {
2453 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002454 }
2455 break;
2456 }
2457 case Instruction::NEW_INSTANCE: {
Andreas Gampe98be1a92017-08-28 08:25:45 -07002458 const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
Ian Rogersad0b3a32012-04-16 14:50:24 -07002459 if (res_type.IsConflict()) {
2460 DCHECK_NE(failures_.size(), 0U);
2461 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002462 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002463 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2464 // can't create an instance of an interface or abstract class */
2465 if (!res_type.IsInstantiableTypes()) {
2466 Fail(VERIFY_ERROR_INSTANTIATION)
2467 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002468 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002469 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002470 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002471 // Any registers holding previous allocations from this address that have not yet been
2472 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002473 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002474 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002475 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002476 break;
2477 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002478 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002479 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002480 break;
2481 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002482 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002483 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002484 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002485 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002486 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002487 just_set_result = true; // Filled new array range sets result register
2488 break;
jeffhaobdb76512011-09-07 11:43:16 -07002489 case Instruction::CMPL_FLOAT:
2490 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002491 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002492 break;
2493 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002494 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002495 break;
2496 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002497 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002498 break;
2499 case Instruction::CMPL_DOUBLE:
2500 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002501 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002502 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002503 break;
2504 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002505 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002506 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002507 break;
2508 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002509 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002510 break;
2511 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002512 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002513 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002514 break;
2515 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002516 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002517 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002518 break;
2519 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002520 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002521 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002522 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002523 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
David Brazdilca3c8c32016-09-06 14:04:48 +01002524 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002525 if (res_type.IsUninitializedTypes()) {
2526 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002527 } else if (!res_type.IsReferenceTypes()) {
2528 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002529 } else {
Nicolas Geoffray04ea42c2021-01-22 10:04:56 +00002530 Fail(res_type.IsUnresolvedTypes()
2531 ? VERIFY_ERROR_UNRESOLVED_TYPE_CHECK : VERIFY_ERROR_BAD_CLASS_SOFT)
David Brazdil68b5c0b2016-01-19 14:25:29 +00002532 << "thrown class " << res_type << " not instanceof Throwable";
2533 }
jeffhaobdb76512011-09-07 11:43:16 -07002534 }
2535 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002536 }
jeffhaobdb76512011-09-07 11:43:16 -07002537 case Instruction::GOTO:
2538 case Instruction::GOTO_16:
2539 case Instruction::GOTO_32:
2540 /* no effect on or use of registers */
2541 break;
2542
2543 case Instruction::PACKED_SWITCH:
2544 case Instruction::SPARSE_SWITCH:
2545 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002546 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002547 break;
2548
Ian Rogersd81871c2011-10-03 13:57:23 -07002549 case Instruction::FILL_ARRAY_DATA: {
2550 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002551 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002552 /* array_type can be null if the reg type is Zero */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002553 if (!array_type.IsZeroOrNull()) {
jeffhao457cc512012-02-02 16:55:13 -08002554 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002555 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2556 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002557 } else if (array_type.IsUnresolvedTypes()) {
2558 // If it's an unresolved array type, it must be non-primitive.
2559 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2560 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002561 } else {
Andreas Gampe51de69e2019-04-19 15:14:14 -07002562 const RegType& component_type = reg_types_.GetComponentType(array_type,
2563 class_loader_.Get());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002564 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002565 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002566 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2567 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002568 } else {
jeffhao457cc512012-02-02 16:55:13 -08002569 // Now verify if the element width in the table matches the element width declared in
2570 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002571 const uint16_t* array_data =
2572 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002573 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002574 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002575 } else {
2576 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2577 // Since we don't compress the data in Dex, expect to see equal width of data stored
2578 // in the table and expected from the array class.
2579 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002580 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2581 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002582 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002583 }
2584 }
jeffhaobdb76512011-09-07 11:43:16 -07002585 }
2586 }
2587 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002588 }
jeffhaobdb76512011-09-07 11:43:16 -07002589 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002590 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002591 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2592 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002593 bool mismatch = false;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002594 if (reg_type1.IsZeroOrNull()) { // zero then integral or reference expected
Ian Rogersd81871c2011-10-03 13:57:23 -07002595 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2596 } else if (reg_type1.IsReferenceTypes()) { // both references?
2597 mismatch = !reg_type2.IsReferenceTypes();
2598 } else { // both integral?
2599 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2600 }
2601 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002602 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2603 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002604 }
2605 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002606 }
jeffhaobdb76512011-09-07 11:43:16 -07002607 case Instruction::IF_LT:
2608 case Instruction::IF_GE:
2609 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002610 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002611 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2612 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002613 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002614 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2615 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002616 }
2617 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002618 }
jeffhaobdb76512011-09-07 11:43:16 -07002619 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002620 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002621 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002622 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002623 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2624 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002625 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002626
2627 // Find previous instruction - its existence is a precondition to peephole optimization.
Andreas Gampe147a9112019-07-31 16:17:10 -07002628 if (UNLIKELY(0 == work_insn_idx_)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002629 break;
2630 }
Andreas Gampe147a9112019-07-31 16:17:10 -07002631 uint32_t instance_of_idx = work_insn_idx_ - 1;
2632 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
2633 instance_of_idx--;
2634 }
2635 // Dex index 0 must be an opcode.
2636 DCHECK(GetInstructionFlags(instance_of_idx).IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002637
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002638 const Instruction& instance_of_inst = code_item_accessor_.InstructionAt(instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002639
2640 /* Check for peep-hole pattern of:
2641 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002642 * instance-of vX, vY, T;
2643 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002644 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002645 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002646 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002647 * and sharpen the type of vY to be type T.
2648 * Note, this pattern can't be if:
2649 * - if there are other branches to this branch,
2650 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002651 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002652 if (!CurrentInsnFlags()->IsBranchTarget() &&
Vladimir Markod7559b72017-09-28 13:50:37 +01002653 (Instruction::INSTANCE_OF == instance_of_inst.Opcode()) &&
2654 (inst->VRegA_21t() == instance_of_inst.VRegA_22c()) &&
2655 (instance_of_inst.VRegA_22c() != instance_of_inst.VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002656 // Check the type of the instance-of is different than that of registers type, as if they
2657 // are the same there is no work to be done here. Check that the conversion is not to or
2658 // from an unresolved type as type information is imprecise. If the instance-of is to an
2659 // interface then ignore the type information as interfaces can only be treated as Objects
2660 // and we don't want to disallow field and other operations on the object. If the value
2661 // being instance-of checked against is known null (zero) then allow the optimization as
2662 // we didn't have type information. If the merge of the instance-of type with the original
2663 // type is assignable to the original then allow optimization. This check is performed to
2664 // ensure that subsequent merges don't lose type information - such as becoming an
2665 // interface from a class that would lose information relevant to field checks.
Andreas Gamped78122b2019-07-10 14:06:53 -07002666 //
2667 // Note: do not do an access check. This may mark this with a runtime throw that actually
2668 // happens at the instanceof, not the branch (and branches aren't flagged to throw).
Vladimir Markod7559b72017-09-28 13:50:37 +01002669 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst.VRegB_22c());
Andreas Gamped78122b2019-07-10 14:06:53 -07002670 const RegType& cast_type = ResolveClass<CheckAccess::kNo>(
Vladimir Markod7559b72017-09-28 13:50:37 +01002671 dex::TypeIndex(instance_of_inst.VRegC_22c()));
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002672
Ian Rogersebbdd872014-07-07 23:53:08 -07002673 if (!orig_type.Equals(cast_type) &&
2674 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002675 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002676 !cast_type.GetClass()->IsInterface() &&
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002677 (orig_type.IsZeroOrNull() ||
David Brazdilca3c8c32016-09-06 14:04:48 +01002678 orig_type.IsStrictlyAssignableFrom(
2679 cast_type.Merge(orig_type, &reg_types_, this), this))) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002680 RegisterLine* update_line = RegisterLine::Create(code_item_accessor_.RegistersSize(),
Andreas Gamped09c0592019-04-19 15:44:05 -07002681 allocator_,
2682 GetRegTypeCache());
Ian Rogersfae370a2013-06-05 08:33:27 -07002683 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002684 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002685 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002686 branch_line.reset(update_line);
2687 }
2688 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002689 update_line->SetRegisterType<LockOp::kKeep>(this,
Vladimir Markod7559b72017-09-28 13:50:37 +01002690 instance_of_inst.VRegB_22c(),
Andreas Gampead238ce2015-08-24 21:13:08 -07002691 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002692 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002693 // See if instance-of was preceded by a move-object operation, common due to the small
2694 // register encoding space of instance-of, and propagate type information to the source
2695 // of the move-object.
Andreas Gampe43e43252019-01-08 12:06:57 -08002696 // Note: this is only valid if the move source was not clobbered.
Ian Rogers9b360392013-06-06 14:45:07 -07002697 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002698 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002699 move_idx--;
2700 }
Andreas Gampe147a9112019-07-31 16:17:10 -07002701 DCHECK(GetInstructionFlags(move_idx).IsOpcode());
Andreas Gampe43e43252019-01-08 12:06:57 -08002702 auto maybe_update_fn = [&instance_of_inst, update_line, this, &cast_type](
2703 uint16_t move_src,
2704 uint16_t move_trg)
2705 REQUIRES_SHARED(Locks::mutator_lock_) {
2706 if (move_trg == instance_of_inst.VRegB_22c() &&
2707 move_src != instance_of_inst.VRegA_22c()) {
2708 update_line->SetRegisterType<LockOp::kKeep>(this, move_src, cast_type);
2709 }
2710 };
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08002711 const Instruction& move_inst = code_item_accessor_.InstructionAt(move_idx);
Vladimir Markod7559b72017-09-28 13:50:37 +01002712 switch (move_inst.Opcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002713 case Instruction::MOVE_OBJECT:
Andreas Gampe43e43252019-01-08 12:06:57 -08002714 maybe_update_fn(move_inst.VRegB_12x(), move_inst.VRegA_12x());
Ian Rogers9b360392013-06-06 14:45:07 -07002715 break;
2716 case Instruction::MOVE_OBJECT_FROM16:
Andreas Gampe43e43252019-01-08 12:06:57 -08002717 maybe_update_fn(move_inst.VRegB_22x(), move_inst.VRegA_22x());
Ian Rogers9b360392013-06-06 14:45:07 -07002718 break;
2719 case Instruction::MOVE_OBJECT_16:
Andreas Gampe43e43252019-01-08 12:06:57 -08002720 maybe_update_fn(move_inst.VRegB_32x(), move_inst.VRegA_32x());
Ian Rogers9b360392013-06-06 14:45:07 -07002721 break;
2722 default:
2723 break;
2724 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002725 }
2726 }
2727 }
2728
jeffhaobdb76512011-09-07 11:43:16 -07002729 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002730 }
jeffhaobdb76512011-09-07 11:43:16 -07002731 case Instruction::IF_LTZ:
2732 case Instruction::IF_GEZ:
2733 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002734 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002735 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002736 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002737 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2738 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002739 }
jeffhaobdb76512011-09-07 11:43:16 -07002740 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002741 }
jeffhaobdb76512011-09-07 11:43:16 -07002742 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002743 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002744 break;
jeffhaobdb76512011-09-07 11:43:16 -07002745 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002746 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002747 break;
jeffhaobdb76512011-09-07 11:43:16 -07002748 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002749 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002750 break;
jeffhaobdb76512011-09-07 11:43:16 -07002751 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002752 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002753 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002754 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002755 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002756 break;
jeffhaobdb76512011-09-07 11:43:16 -07002757 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002758 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002759 break;
2760 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002761 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002762 break;
2763
Ian Rogersd81871c2011-10-03 13:57:23 -07002764 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002765 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002766 break;
2767 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002768 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002769 break;
2770 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002771 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002772 break;
2773 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002774 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002775 break;
2776 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002777 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002778 break;
2779 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002780 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002781 break;
2782 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002783 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002784 break;
2785
jeffhaobdb76512011-09-07 11:43:16 -07002786 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002787 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002788 break;
jeffhaobdb76512011-09-07 11:43:16 -07002789 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002790 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002791 break;
jeffhaobdb76512011-09-07 11:43:16 -07002792 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002793 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002794 break;
jeffhaobdb76512011-09-07 11:43:16 -07002795 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002796 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002797 break;
2798 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002799 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002800 break;
2801 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002802 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002803 break;
2804 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002805 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2806 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002807 break;
jeffhaobdb76512011-09-07 11:43:16 -07002808
Ian Rogersd81871c2011-10-03 13:57:23 -07002809 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002810 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002811 break;
2812 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002813 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002814 break;
2815 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002816 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 break;
2818 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002819 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002820 break;
2821 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002822 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002823 break;
2824 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002825 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002826 break;
jeffhaobdb76512011-09-07 11:43:16 -07002827 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002828 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2829 false);
jeffhaobdb76512011-09-07 11:43:16 -07002830 break;
2831
jeffhaobdb76512011-09-07 11:43:16 -07002832 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002833 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002834 break;
jeffhaobdb76512011-09-07 11:43:16 -07002835 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002836 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002837 break;
jeffhaobdb76512011-09-07 11:43:16 -07002838 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002839 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002840 break;
jeffhaobdb76512011-09-07 11:43:16 -07002841 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002842 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002843 break;
2844 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002845 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002846 break;
2847 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002848 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002849 break;
2850 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002851 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2852 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002853 break;
2854
2855 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002856 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002857 break;
2858 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002859 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002860 break;
2861 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002862 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002863 break;
2864 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002865 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002866 break;
2867 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002868 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002869 break;
2870 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002871 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002872 break;
2873 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002874 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2875 true);
jeffhaobdb76512011-09-07 11:43:16 -07002876 break;
2877
2878 case Instruction::INVOKE_VIRTUAL:
2879 case Instruction::INVOKE_VIRTUAL_RANGE:
2880 case Instruction::INVOKE_SUPER:
David Srbeckyc5cd5892021-03-23 08:09:15 +00002881 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002882 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
David Srbeckyc5cd5892021-03-23 08:09:15 +00002883 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002884 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2885 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002886 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2887 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002888 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002889 if (called_method != nullptr) {
Vladimir Markob45528c2017-07-27 14:14:28 +01002890 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2891 ? called_method->ResolveReturnType()
2892 : called_method->LookupResolvedReturnType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002893 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002894 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
Vladimir Marko179b7c62019-03-22 13:38:57 +00002895 return_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07002896 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002897 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002898 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2899 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002900 }
2901 }
2902 if (return_type == nullptr) {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00002903 uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002904 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08002905 dex::TypeIndex return_type_idx =
2906 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002907 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Andreas Gampe51de69e2019-04-19 15:14:14 -07002908 return_type = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002909 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002910 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002911 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002912 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002913 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002914 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002915 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002916 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002917 }
jeffhaobdb76512011-09-07 11:43:16 -07002918 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002919 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002920 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002921 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002922 const char* return_type_descriptor;
2923 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002924 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002925 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002926 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002927 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002928 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Andreas Gampea5b09a62016-11-17 15:21:22 -08002929 dex::TypeIndex return_type_idx =
2930 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers46685432012-06-03 22:26:43 -07002931 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2932 } else {
2933 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002934 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Markob45528c2017-07-27 14:14:28 +01002935 ObjPtr<mirror::Class> return_type_class = can_load_classes_
2936 ? called_method->ResolveReturnType()
2937 : called_method->LookupResolvedReturnType();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002938 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002939 return_type = &FromClass(return_type_descriptor,
Vladimir Markobcf17522018-06-01 13:14:32 +01002940 return_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07002941 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002942 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002943 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2944 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002945 }
Ian Rogers46685432012-06-03 22:26:43 -07002946 }
2947 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002948 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002949 * Some additional checks when calling a constructor. We know from the invocation arg check
2950 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2951 * that to require that called_method->klass is the same as this->klass or this->super,
2952 * allowing the latter only if the "this" argument is the same as the "this" argument to
2953 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002954 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01002955 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
jeffhaob57e9522012-04-26 18:08:21 -07002956 if (this_type.IsConflict()) // failure.
2957 break;
jeffhaobdb76512011-09-07 11:43:16 -07002958
jeffhaob57e9522012-04-26 18:08:21 -07002959 /* no null refs allowed (?) */
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08002960 if (this_type.IsZeroOrNull()) {
jeffhaob57e9522012-04-26 18:08:21 -07002961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2962 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002963 }
jeffhaob57e9522012-04-26 18:08:21 -07002964
2965 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002966 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002967 // TODO: re-enable constructor type verification
2968 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002969 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002970 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2971 // break;
2972 // }
jeffhaob57e9522012-04-26 18:08:21 -07002973
2974 /* arg must be an uninitialized reference */
2975 if (!this_type.IsUninitializedTypes()) {
2976 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2977 << this_type;
2978 break;
2979 }
2980
2981 /*
2982 * Replace the uninitialized reference with an initialized one. We need to do this for all
2983 * registers that have the same object instance in them, not just the "this" register.
2984 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00002985 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002986 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002987 if (return_type == nullptr) {
Andreas Gampe51de69e2019-04-19 15:14:14 -07002988 return_type = &reg_types_.FromDescriptor(class_loader_.Get(),
2989 return_type_descriptor,
2990 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002991 }
2992 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002993 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002994 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002995 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002996 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002997 just_set_result = true;
2998 break;
2999 }
3000 case Instruction::INVOKE_STATIC:
3001 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003002 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003003 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003004 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003005 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003006 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003007 const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003008 dex::TypeIndex return_type_idx =
3009 dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07003010 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07003011 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003012 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07003013 }
Andreas Gampe51de69e2019-04-19 15:14:14 -07003014 const RegType& return_type = reg_types_.FromDescriptor(class_loader_.Get(),
3015 descriptor,
3016 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003017 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003018 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003019 } else {
3020 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3021 }
jeffhaobdb76512011-09-07 11:43:16 -07003022 just_set_result = true;
3023 }
3024 break;
jeffhaobdb76512011-09-07 11:43:16 -07003025 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07003026 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003027 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08003028 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003029 if (abs_method != nullptr) {
Vladimir Markod93e3742018-07-18 10:58:13 +01003030 ObjPtr<mirror::Class> called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003031 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
3032 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
David Sehr709b0702016-10-13 09:12:37 -07003033 << abs_method->PrettyMethod() << "'";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003034 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003035 }
Ian Rogers0d604842012-04-16 14:50:24 -07003036 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003037 /* Get the type of the "this" arg, which should either be a sub-interface of called
3038 * interface or Object (see comments in RegType::JoinClass).
3039 */
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003040 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08003041 if (this_type.IsZeroOrNull()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003042 /* null pointer always passes (and always fails at runtime) */
3043 } else {
3044 if (this_type.IsUninitializedTypes()) {
3045 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
3046 << this_type;
3047 break;
3048 }
3049 // In the past we have tried to assert that "called_interface" is assignable
3050 // from "this_type.GetClass()", however, as we do an imprecise Join
3051 // (RegType::JoinClass) we don't have full information on what interfaces are
3052 // implemented by "this_type". For example, two classes may implement the same
3053 // interfaces and have a common parent that doesn't implement the interface. The
3054 // join will set "this_type" to the parent class and a test that this implements
3055 // the interface will incorrectly fail.
3056 }
3057 /*
3058 * We don't have an object instance, so we can't find the concrete method. However, all of
3059 * the type information is in the abstract method, so we're good.
3060 */
3061 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003062 if (abs_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 Chartierde40d472015-10-15 17:47:48 -07003067 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003068 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003069 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -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 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003079 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07003080 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003081 }
Narayan Kamath9823e782016-08-03 12:46:58 +01003082 case Instruction::INVOKE_POLYMORPHIC:
3083 case Instruction::INVOKE_POLYMORPHIC_RANGE: {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003084 bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
3085 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range);
3086 if (called_method == nullptr) {
3087 // Convert potential soft failures in VerifyInvocationArgs() to hard errors.
3088 if (failure_messages_.size() > 0) {
3089 std::string message = failure_messages_.back()->str();
3090 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message;
3091 } else {
3092 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure.";
3093 }
3094 break;
3095 }
3096 if (!CheckSignaturePolymorphicMethod(called_method) ||
3097 !CheckSignaturePolymorphicReceiver(inst)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00003098 DCHECK(HasFailures());
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003099 break;
3100 }
Orion Hodson06d10a72018-05-14 08:53:38 +01003101 const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
3102 const dex::ProtoIndex proto_idx(vRegH);
Orion Hodsonac141392017-01-13 11:53:47 +00003103 const char* return_descriptor =
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003104 dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx));
3105 const RegType& return_type =
Andreas Gampe51de69e2019-04-19 15:14:14 -07003106 reg_types_.FromDescriptor(class_loader_.Get(), return_descriptor, false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003107 if (!return_type.IsLowHalf()) {
3108 work_line_->SetResultRegisterType(this, return_type);
3109 } else {
3110 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3111 }
Orion Hodsonac141392017-01-13 11:53:47 +00003112 just_set_result = true;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003113 break;
Narayan Kamath9823e782016-08-03 12:46:58 +01003114 }
Orion Hodsonc069a302017-01-18 09:23:12 +00003115 case Instruction::INVOKE_CUSTOM:
3116 case Instruction::INVOKE_CUSTOM_RANGE: {
3117 // Verify registers based on method_type in the call site.
3118 bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE);
3119
3120 // Step 1. Check the call site that produces the method handle for invocation
3121 const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3122 if (!CheckCallSite(call_site_idx)) {
3123 DCHECK(HasFailures());
3124 break;
3125 }
3126
3127 // Step 2. Check the register arguments correspond to the expected arguments for the
3128 // method handle produced by step 1. The dex file verifier has checked ranges for
3129 // the first three arguments and CheckCallSite has checked the method handle type.
Orion Hodson4c8e12e2018-05-18 08:33:20 +01003130 const dex::ProtoIndex proto_idx = dex_file_->GetProtoIndexForCallSite(call_site_idx);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003131 const dex::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00003132 DexFileParameterIterator param_it(*dex_file_, proto_id);
3133 // Treat method as static as it has yet to be determined.
3134 VerifyInvocationArgsFromIterator(&param_it, inst, METHOD_STATIC, is_range, nullptr);
3135 const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
3136
3137 // Step 3. Propagate return type information
3138 const RegType& return_type =
Andreas Gampe51de69e2019-04-19 15:14:14 -07003139 reg_types_.FromDescriptor(class_loader_.Get(), return_descriptor, false);
Orion Hodsonc069a302017-01-18 09:23:12 +00003140 if (!return_type.IsLowHalf()) {
3141 work_line_->SetResultRegisterType(this, return_type);
3142 } else {
3143 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3144 }
3145 just_set_result = true;
Orion Hodsonc069a302017-01-18 09:23:12 +00003146 break;
3147 }
jeffhaobdb76512011-09-07 11:43:16 -07003148 case Instruction::NEG_INT:
3149 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003150 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003151 break;
3152 case Instruction::NEG_LONG:
3153 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003154 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003155 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003156 break;
3157 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003158 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003159 break;
3160 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003161 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003162 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003163 break;
3164 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003165 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003166 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003167 break;
3168 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003169 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003170 break;
3171 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003172 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003173 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003174 break;
3175 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003176 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003177 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003178 break;
3179 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003180 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003181 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003182 break;
3183 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003184 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003185 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003186 break;
3187 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003188 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003189 break;
3190 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003191 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003192 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003193 break;
3194 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003195 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003196 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003197 break;
3198 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003199 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003200 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003201 break;
3202 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003203 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003204 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003205 break;
3206 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003207 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003208 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003209 break;
3210 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003211 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003212 break;
3213 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003214 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003215 break;
3216 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003217 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003218 break;
3219
3220 case Instruction::ADD_INT:
3221 case Instruction::SUB_INT:
3222 case Instruction::MUL_INT:
3223 case Instruction::REM_INT:
3224 case Instruction::DIV_INT:
3225 case Instruction::SHL_INT:
3226 case Instruction::SHR_INT:
3227 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003228 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003229 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003230 break;
3231 case Instruction::AND_INT:
3232 case Instruction::OR_INT:
3233 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003234 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003235 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003236 break;
3237 case Instruction::ADD_LONG:
3238 case Instruction::SUB_LONG:
3239 case Instruction::MUL_LONG:
3240 case Instruction::DIV_LONG:
3241 case Instruction::REM_LONG:
3242 case Instruction::AND_LONG:
3243 case Instruction::OR_LONG:
3244 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003245 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003246 reg_types_.LongLo(), reg_types_.LongHi(),
3247 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003248 break;
3249 case Instruction::SHL_LONG:
3250 case Instruction::SHR_LONG:
3251 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003252 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003253 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003254 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003255 break;
3256 case Instruction::ADD_FLOAT:
3257 case Instruction::SUB_FLOAT:
3258 case Instruction::MUL_FLOAT:
3259 case Instruction::DIV_FLOAT:
3260 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003261 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3262 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003263 break;
3264 case Instruction::ADD_DOUBLE:
3265 case Instruction::SUB_DOUBLE:
3266 case Instruction::MUL_DOUBLE:
3267 case Instruction::DIV_DOUBLE:
3268 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003269 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003270 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3271 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003272 break;
3273 case Instruction::ADD_INT_2ADDR:
3274 case Instruction::SUB_INT_2ADDR:
3275 case Instruction::MUL_INT_2ADDR:
3276 case Instruction::REM_INT_2ADDR:
3277 case Instruction::SHL_INT_2ADDR:
3278 case Instruction::SHR_INT_2ADDR:
3279 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003280 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3281 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003282 break;
3283 case Instruction::AND_INT_2ADDR:
3284 case Instruction::OR_INT_2ADDR:
3285 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003286 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3287 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003288 break;
3289 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003290 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3291 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003292 break;
3293 case Instruction::ADD_LONG_2ADDR:
3294 case Instruction::SUB_LONG_2ADDR:
3295 case Instruction::MUL_LONG_2ADDR:
3296 case Instruction::DIV_LONG_2ADDR:
3297 case Instruction::REM_LONG_2ADDR:
3298 case Instruction::AND_LONG_2ADDR:
3299 case Instruction::OR_LONG_2ADDR:
3300 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003301 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003302 reg_types_.LongLo(), reg_types_.LongHi(),
3303 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003304 break;
3305 case Instruction::SHL_LONG_2ADDR:
3306 case Instruction::SHR_LONG_2ADDR:
3307 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003308 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003309 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003310 break;
3311 case Instruction::ADD_FLOAT_2ADDR:
3312 case Instruction::SUB_FLOAT_2ADDR:
3313 case Instruction::MUL_FLOAT_2ADDR:
3314 case Instruction::DIV_FLOAT_2ADDR:
3315 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003316 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3317 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003318 break;
3319 case Instruction::ADD_DOUBLE_2ADDR:
3320 case Instruction::SUB_DOUBLE_2ADDR:
3321 case Instruction::MUL_DOUBLE_2ADDR:
3322 case Instruction::DIV_DOUBLE_2ADDR:
3323 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003324 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003325 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3326 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003327 break;
3328 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003329 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003330 case Instruction::MUL_INT_LIT16:
3331 case Instruction::DIV_INT_LIT16:
3332 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003333 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3334 true);
jeffhaobdb76512011-09-07 11:43:16 -07003335 break;
3336 case Instruction::AND_INT_LIT16:
3337 case Instruction::OR_INT_LIT16:
3338 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003339 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3340 true);
jeffhaobdb76512011-09-07 11:43:16 -07003341 break;
3342 case Instruction::ADD_INT_LIT8:
3343 case Instruction::RSUB_INT_LIT8:
3344 case Instruction::MUL_INT_LIT8:
3345 case Instruction::DIV_INT_LIT8:
3346 case Instruction::REM_INT_LIT8:
3347 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003348 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003349 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003350 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3351 false);
jeffhaobdb76512011-09-07 11:43:16 -07003352 break;
3353 case Instruction::AND_INT_LIT8:
3354 case Instruction::OR_INT_LIT8:
3355 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003356 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3357 false);
jeffhaobdb76512011-09-07 11:43:16 -07003358 break;
3359
Ian Rogersd81871c2011-10-03 13:57:23 -07003360 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003361 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
David Srbeckyc5cd5892021-03-23 08:09:15 +00003362 case Instruction::UNUSED_E3 ... Instruction::UNUSED_F9:
David Srbecky61c62422021-04-27 16:22:48 +01003363 case Instruction::UNUSED_73:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003364 case Instruction::UNUSED_79:
3365 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003366 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003367 break;
3368
3369 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003370 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003371 * complain if an instruction is missing (which is desirable).
3372 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003373 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003374
Andreas Gampe43884b22019-06-27 14:05:52 -07003375 if (flags_.have_pending_hard_failure_) {
Andreas Gampefef91cc2019-07-25 14:13:23 -07003376 if (IsAotMode()) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003377 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003378 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3379 LOG(ERROR) << "Pending failures:";
3380 for (auto& error : failures_) {
3381 LOG(ERROR) << error;
3382 }
3383 for (auto& error_msg : failure_messages_) {
3384 LOG(ERROR) << error_msg->str();
3385 }
3386 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3387 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003388 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003389 /* immediate failure, reject class */
3390 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3391 return false;
Andreas Gampe43884b22019-06-27 14:05:52 -07003392 } else if (flags_.have_pending_runtime_throw_failure_) {
Andreas Gampe6087bc22019-06-03 15:52:08 -07003393 LogVerifyInfo() << "Elevating opcode flags from " << opcode_flags << " to Throw";
Jeff Haoa3faaf42013-09-03 19:07:00 -07003394 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003395 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003396 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3397 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3398 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003399 }
jeffhaobdb76512011-09-07 11:43:16 -07003400 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003401 * If we didn't just set the result register, clear it out. This ensures that you can only use
3402 * "move-result" immediately after the result is set. (We could check this statically, but it's
3403 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003404 */
3405 if (!just_set_result) {
Andreas Gamped09c0592019-04-19 15:44:05 -07003406 work_line_->SetResultTypeToUnknown(GetRegTypeCache());
jeffhaobdb76512011-09-07 11:43:16 -07003407 }
3408
jeffhaobdb76512011-09-07 11:43:16 -07003409 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003410 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003411 *
3412 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003413 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003414 * somebody could get a reference field, check it for zero, and if the
3415 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003416 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003417 * that, and will reject the code.
3418 *
3419 * TODO: avoid re-fetching the branch target
3420 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003421 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003422 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003423 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003424 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003425 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003426 return false;
3427 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003428 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003429 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(),
3430 work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003431 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003432 }
jeffhaobdb76512011-09-07 11:43:16 -07003433 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003434 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003435 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003436 return false;
3437 }
3438 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003439 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003440 return false;
3441 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003442 }
jeffhaobdb76512011-09-07 11:43:16 -07003443 }
3444
3445 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003446 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003447 *
3448 * We've already verified that the table is structurally sound, so we
3449 * just need to walk through and tag the targets.
3450 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003451 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003452 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003453 const uint16_t* switch_insns = insns + offset_to_switch;
3454 int switch_count = switch_insns[1];
3455 int offset_to_targets, targ;
3456
3457 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3458 /* 0 = sig, 1 = count, 2/3 = first key */
3459 offset_to_targets = 4;
3460 } else {
3461 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003462 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003463 offset_to_targets = 2 + 2 * switch_count;
3464 }
3465
3466 /* verify each switch target */
3467 for (targ = 0; targ < switch_count; targ++) {
3468 int offset;
3469 uint32_t abs_offset;
3470
3471 /* offsets are 32-bit, and only partly endian-swapped */
3472 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003473 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003474 abs_offset = work_insn_idx_ + offset;
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003475 DCHECK_LT(abs_offset, code_item_accessor_.InsnsSizeInCodeUnits());
3476 if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003477 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003478 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003479 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003480 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003481 }
jeffhaobdb76512011-09-07 11:43:16 -07003482 }
3483 }
3484
3485 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003486 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3487 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003488 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003489 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003490 bool has_catch_all_handler = false;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003491 const dex::TryItem* try_item = code_item_accessor_.FindTryItem(work_insn_idx_);
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003492 CHECK(try_item != nullptr);
Mathieu Chartierdc578c72017-12-27 11:51:45 -08003493 CatchHandlerIterator iterator(code_item_accessor_, *try_item);
jeffhaobdb76512011-09-07 11:43:16 -07003494
Andreas Gampef91baf12014-07-18 15:41:00 -07003495 // Need the linker to try and resolve the handled class to check if it's Throwable.
Andreas Gampee0bbab92019-07-25 12:28:22 -07003496 ClassLinker* linker = GetClassLinker();
Andreas Gampef91baf12014-07-18 15:41:00 -07003497
Ian Rogers0571d352011-11-03 19:51:38 -07003498 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003499 dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex();
3500 if (!handler_type_idx.IsValid()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003501 has_catch_all_handler = true;
3502 } else {
3503 // It is also a catch-all if it is java.lang.Throwable.
Vladimir Marko28e012a2017-12-07 11:22:59 +00003504 ObjPtr<mirror::Class> klass =
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003505 linker->ResolveType(handler_type_idx, dex_cache_, class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003506 if (klass != nullptr) {
Vladimir Markoadbceb72018-05-29 14:34:14 +01003507 if (klass == GetClassRoot<mirror::Throwable>()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003508 has_catch_all_handler = true;
3509 }
3510 } else {
3511 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003512 DCHECK(self_->IsExceptionPending());
3513 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003514 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003515 }
jeffhaobdb76512011-09-07 11:43:16 -07003516 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003517 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3518 * "work_regs", because at runtime the exception will be thrown before the instruction
3519 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003520 */
Andreas Gampebfcca582019-04-19 12:01:55 -07003521 if (kVerifierDebug) {
3522 LogVerifyInfo() << "Updating exception handler 0x"
3523 << std::hex << iterator.GetHandlerAddress();
3524 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003525 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003526 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003527 }
jeffhaobdb76512011-09-07 11:43:16 -07003528 }
3529
3530 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003531 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3532 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003533 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003534 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003535 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003536 * The state in work_line reflects the post-execution state. If the current instruction is a
3537 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003538 * it will do so before grabbing the lock).
3539 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003540 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003541 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003542 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003543 return false;
3544 }
3545 }
3546 }
3547
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003548 /* Handle "continue". Tag the next consecutive instruction.
3549 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3550 * because it changes work_line_ when performing peephole optimization
3551 * and this change should not be used in those cases.
3552 */
Andreas Gampe6087bc22019-06-03 15:52:08 -07003553 if ((opcode_flags & Instruction::kContinue) != 0 && !exc_handler_unreachable) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003554 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003555 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003556 if (next_insn_idx >= code_item_accessor_.InsnsSizeInCodeUnits()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003557 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3558 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003559 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003560 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3561 // next instruction isn't one.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003562 if (!CheckNotMoveException(code_item_accessor_.Insns(), next_insn_idx)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003563 return false;
3564 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003565 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003566 // Make workline consistent with fallthrough computed from peephole optimization.
3567 work_line_->CopyFromLine(fallthrough_line.get());
3568 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003569 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003570 // For returns we only care about the operand to the return, all other registers are dead.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003571 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003572 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003573 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003574 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003575 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003576 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3577 // needed. If the merge changes the state of the registers then the work line will be
3578 // updated.
3579 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003580 return false;
3581 }
3582 } else {
3583 /*
3584 * We're not recording register data for the next instruction, so we don't know what the
3585 * prior state was. We have to assume that something has changed and re-evaluate it.
3586 */
Andreas Gampe51de69e2019-04-19 15:14:14 -07003587 GetModifiableInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003588 }
3589 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003590
jeffhaod1f0fde2011-09-08 17:25:33 -07003591 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003592 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003593 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003594 }
3595
3596 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003597 * Update start_guess. Advance to the next instruction of that's
3598 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003599 * neither of those exists we're in a return or throw; leave start_guess
3600 * alone and let the caller sort it out.
3601 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003602 if ((opcode_flags & Instruction::kContinue) != 0) {
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003603 DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
Ian Rogers7b078e82014-09-10 14:44:24 -07003604 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003605 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003606 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003607 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003608 }
3609
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003610 DCHECK_LT(*start_guess, code_item_accessor_.InsnsSizeInCodeUnits());
Mathieu Chartierde40d472015-10-15 17:47:48 -07003611 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003612
Andreas Gampe43884b22019-06-27 14:05:52 -07003613 if (flags_.have_pending_runtime_throw_failure_) {
Nicolas Geoffray2ec38232021-07-02 16:36:29 +01003614 Fail(VERIFY_ERROR_RUNTIME_THROW, /* pending_exc= */ false);
Andreas Gampea727e372015-08-25 09:22:37 -07003615 // Reset the pending_runtime_throw flag now.
Andreas Gampe43884b22019-06-27 14:05:52 -07003616 flags_.have_pending_runtime_throw_failure_ = false;
Andreas Gampea727e372015-08-25 09:22:37 -07003617 }
3618
jeffhaobdb76512011-09-07 11:43:16 -07003619 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003620} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003621
Andreas Gampe99db7bb2019-04-19 23:05:47 -07003622template <bool kVerifierDebug>
3623template <CheckAccess C>
3624const RegType& MethodVerifier<kVerifierDebug>::ResolveClass(dex::TypeIndex class_idx) {
Andreas Gampee0bbab92019-07-25 12:28:22 -07003625 ClassLinker* linker = GetClassLinker();
Vladimir Marko28e012a2017-12-07 11:22:59 +00003626 ObjPtr<mirror::Class> klass = can_load_classes_
Vladimir Marko666ee3d2017-12-11 18:37:36 +00003627 ? linker->ResolveType(class_idx, dex_cache_, class_loader_)
3628 : linker->LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get());
Vladimir Marko8d6768d2017-03-14 10:13:21 +00003629 if (can_load_classes_ && klass == nullptr) {
3630 DCHECK(self_->IsExceptionPending());
3631 self_->ClearException();
3632 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003633 const RegType* result = nullptr;
Vladimir Marko9cb0c462017-04-21 13:31:41 +01003634 if (klass != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003635 bool precise = klass->CannotBeAssignedFromOtherTypes();
3636 if (precise && !IsInstantiableOrPrimitive(klass)) {
3637 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3638 UninstantiableError(descriptor);
3639 precise = false;
3640 }
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003641 result = reg_types_.FindClass(klass, precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003642 if (result == nullptr) {
3643 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Vladimir Marko2cffc5d2018-05-29 15:40:56 +01003644 result = reg_types_.InsertClass(descriptor, klass, precise);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003645 }
3646 } else {
3647 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Andreas Gampe51de69e2019-04-19 15:14:14 -07003648 result = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003649 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003650 DCHECK(result != nullptr);
3651 if (result->IsConflict()) {
3652 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3653 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3654 << "' in " << GetDeclaringClass();
3655 return *result;
3656 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003657
Andreas Gampe629be512017-08-25 17:09:32 -07003658 // If requested, check if access is allowed. Unresolved types are included in this check, as the
3659 // interpreter only tests whether access is allowed when a class is not pre-verified and runs in
3660 // the access-checks interpreter. If result is primitive, skip the access check.
3661 //
3662 // Note: we do this for unresolved classes to trigger re-verification at runtime.
Andreas Gampe6087bc22019-06-03 15:52:08 -07003663 if (C != CheckAccess::kNo &&
Andreas Gampedc39d322018-09-04 09:26:03 -07003664 result->IsNonZeroReferenceTypes() &&
Andreas Gampe6087bc22019-06-03 15:52:08 -07003665 ((C == CheckAccess::kYes && IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP))
3666 || !result->IsUnresolvedTypes())) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07003667 const RegType& referrer = GetDeclaringClass();
David Brazdil2bb2fbd2018-11-13 18:24:26 +00003668 if ((IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP) || !referrer.IsUnresolvedTypes()) &&
3669 !referrer.CanAccess(*result)) {
Andreas Gampe629be512017-08-25 17:09:32 -07003670 Fail(VERIFY_ERROR_ACCESS_CLASS) << "(possibly) illegal class access: '"
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +01003671 << referrer << "' -> '" << *result << "'";
Mathieu Chartierde40d472015-10-15 17:47:48 -07003672 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003673 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003674 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003675}
3676
Andreas Gampe99db7bb2019-04-19 23:05:47 -07003677template <bool kVerifierDebug>
Andreas Gampe6087bc22019-06-03 15:52:08 -07003678bool MethodVerifier<kVerifierDebug>::HandleMoveException(const Instruction* inst) {
3679 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
3680 // where one entrypoint to the catch block is not actually an exception path.
3681 if (work_insn_idx_ == 0) {
3682 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
3683 return true;
3684 }
3685 /*
3686 * This statement can only appear as the first instruction in an exception handler. We verify
3687 * that as part of extracting the exception type from the catch block list.
3688 */
3689 auto caught_exc_type_fn = [&]() REQUIRES_SHARED(Locks::mutator_lock_) ->
3690 std::pair<bool, const RegType*> {
3691 const RegType* common_super = nullptr;
3692 if (code_item_accessor_.TriesSize() != 0) {
3693 const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
3694 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3695 const RegType* unresolved = nullptr;
3696 for (uint32_t i = 0; i < handlers_size; i++) {
3697 CatchHandlerIterator iterator(handlers_ptr);
3698 for (; iterator.HasNext(); iterator.Next()) {
3699 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3700 if (!iterator.GetHandlerTypeIndex().IsValid()) {
3701 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003702 } else {
Andreas Gampe6087bc22019-06-03 15:52:08 -07003703 // Do access checks only on resolved exception classes.
3704 const RegType& exception =
3705 ResolveClass<CheckAccess::kOnResolvedClass>(iterator.GetHandlerTypeIndex());
3706 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) {
3707 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
3708 if (exception.IsUnresolvedTypes()) {
3709 if (unresolved == nullptr) {
3710 unresolved = &exception;
3711 } else {
3712 unresolved = &unresolved->SafeMerge(exception, &reg_types_, this);
3713 }
3714 } else {
3715 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class "
3716 << exception;
3717 return std::make_pair(true, &reg_types_.Conflict());
3718 }
3719 } else if (common_super == nullptr) {
3720 common_super = &exception;
3721 } else if (common_super->Equals(exception)) {
3722 // odd case, but nothing to do
3723 } else {
3724 common_super = &common_super->Merge(exception, &reg_types_, this);
3725 if (FailOrAbort(reg_types_.JavaLangThrowable(false).IsAssignableFrom(
3726 *common_super, this),
3727 "java.lang.Throwable is not assignable-from common_super at ",
3728 work_insn_idx_)) {
3729 break;
3730 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003731 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003732 }
3733 }
3734 }
Andreas Gampe6087bc22019-06-03 15:52:08 -07003735 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003736 }
Andreas Gampe6087bc22019-06-03 15:52:08 -07003737 if (unresolved != nullptr) {
Andreas Gampe6087bc22019-06-03 15:52:08 -07003738 // Soft-fail, but do not handle this with a synthetic throw.
Nicolas Geoffray04ea42c2021-01-22 10:04:56 +00003739 Fail(VERIFY_ERROR_UNRESOLVED_TYPE_CHECK, /*pending_exc=*/ false)
3740 << "Unresolved catch handler";
Nicolas Geoffraycd133d82021-06-25 16:10:39 +01003741 bool should_continue = true;
Andreas Gampe6087bc22019-06-03 15:52:08 -07003742 if (common_super != nullptr) {
3743 unresolved = &unresolved->Merge(*common_super, &reg_types_, this);
Nicolas Geoffraycd133d82021-06-25 16:10:39 +01003744 } else {
3745 should_continue = !PotentiallyMarkRuntimeThrow();
Andreas Gampe6087bc22019-06-03 15:52:08 -07003746 }
Nicolas Geoffraycd133d82021-06-25 16:10:39 +01003747 return std::make_pair(should_continue, unresolved);
Andreas Gampe6087bc22019-06-03 15:52:08 -07003748 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003749 }
Andreas Gampe6087bc22019-06-03 15:52:08 -07003750 if (common_super == nullptr) {
3751 /* no catch blocks, or no catches with classes we can find */
3752 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
3753 return std::make_pair(true, &reg_types_.Conflict());
3754 }
3755 return std::make_pair(true, common_super);
3756 };
3757 auto result = caught_exc_type_fn();
3758 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), *result.second);
3759 return result.first;
Ian Rogersd81871c2011-10-03 13:57:23 -07003760}
3761
Andreas Gampe99db7bb2019-04-19 23:05:47 -07003762template <bool kVerifierDebug>
3763ArtMethod* MethodVerifier<kVerifierDebug>::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003764 uint32_t dex_method_idx, MethodType method_type) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003765 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Andreas Gampe98be1a92017-08-28 08:25:45 -07003766 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003767 if (klass_type.IsConflict()) {
3768 std::string append(" in attempt to access method ");
3769 append += dex_file_->GetMethodName(method_id);
3770 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003771 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003772 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003773 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003774 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003775 }
Vladimir Markoba118822017-06-12 15:41:56 +01003776 ObjPtr<mirror::Class> klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003777 const RegType& referrer = GetDeclaringClass();
Andreas Gampee0bbab92019-07-25 12:28:22 -07003778 ClassLinker* class_linker = GetClassLinker();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003779
David Srbecky5de5efe2021-02-15 21:23:00 +00003780 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003781 if (res_method == nullptr) {
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00003782 res_method = class_linker->FindResolvedMethod(
3783 klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07003784 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003785
Alex Lightafb66472017-08-01 09:54:49 -07003786 bool must_fail = false;
3787 // This is traditional and helps with screwy bytecode. It will tell you that, yes, a method
3788 // exists, but that it's called incorrectly. This significantly helps debugging, as locally it's
3789 // hard to see the differences.
3790 // If we don't have res_method here we must fail. Just use this bool to make sure of that with a
3791 // DCHECK.
Vladimir Markoba118822017-06-12 15:41:56 +01003792 if (res_method == nullptr) {
Alex Lightafb66472017-08-01 09:54:49 -07003793 must_fail = true;
Vladimir Markoba118822017-06-12 15:41:56 +01003794 // Try to find the method also with the other type for better error reporting below
3795 // but do not store such bogus lookup result in the DexCache or VerifierDeps.
David Brazdil4525e0b2018-04-05 16:57:32 +01003796 res_method = class_linker->FindIncompatibleMethod(
3797 klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx);
Vladimir Markoba118822017-06-12 15:41:56 +01003798 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003799
3800 if (res_method == nullptr) {
3801 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
David Sehr709b0702016-10-13 09:12:37 -07003802 << klass->PrettyDescriptor() << "."
David Brazdilca3c8c32016-09-06 14:04:48 +01003803 << dex_file_->GetMethodName(method_id) << " "
3804 << dex_file_->GetMethodSignature(method_id);
3805 return nullptr;
3806 }
3807
Ian Rogersd81871c2011-10-03 13:57:23 -07003808 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3809 // enforce them here.
3810 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003811 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
David Sehr709b0702016-10-13 09:12:37 -07003812 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003813 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003814 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003815 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003816 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003817 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
David Sehr709b0702016-10-13 09:12:37 -07003818 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003819 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003820 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003821
3822 // Check that interface methods are static or match interface classes.
3823 // We only allow statics if we don't have default methods enabled.
3824 //
3825 // Note: this check must be after the initializer check, as those are required to fail a class,
3826 // while this check implies an IncompatibleClassChangeError.
3827 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003828 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003829 // default methods are supported for the dex file), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003830 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003831 method_type != METHOD_STATIC &&
Mathieu Chartierf6e31472017-12-28 13:32:08 -08003832 (!dex_file_->SupportsDefaultMethods() ||
Alex Lightb55f1ac2016-04-12 15:50:55 -07003833 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003834 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003835 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003836 << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx)
3837 << " is in an interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003838 return nullptr;
3839 }
3840 } else {
3841 if (method_type == METHOD_INTERFACE) {
3842 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07003843 << "interface method " << dex_file_->PrettyMethod(dex_method_idx)
3844 << " is in a non-interface class " << klass->PrettyClass();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003845 return nullptr;
3846 }
3847 }
3848
Alex Lightafb66472017-08-01 09:54:49 -07003849 // Check specifically for non-public object methods being provided for interface dispatch. This
3850 // can occur if we failed to find a method with FindInterfaceMethod but later find one with
3851 // FindClassMethod for error message use.
3852 if (method_type == METHOD_INTERFACE &&
3853 res_method->GetDeclaringClass()->IsObjectClass() &&
3854 !res_method->IsPublic()) {
3855 Fail(VERIFY_ERROR_NO_METHOD) << "invoke-interface " << klass->PrettyDescriptor() << "."
3856 << dex_file_->GetMethodName(method_id) << " "
3857 << dex_file_->GetMethodSignature(method_id) << " resolved to "
3858 << "non-public object method " << res_method->PrettyMethod() << " "
3859 << "but non-public Object methods are excluded from interface "
3860 << "method resolution.";
3861 return nullptr;
3862 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003863 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003864 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07003865 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call "
3866 << res_method->PrettyMethod()
Ian Rogersad0b3a32012-04-16 14:50:24 -07003867 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003868 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003869 }
jeffhaode0d9c92012-02-27 13:58:13 -08003870 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003871 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003872 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
David Sehr709b0702016-10-13 09:12:37 -07003873 << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003874 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003875 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003876 // See if the method type implied by the invoke instruction matches the access flags for the
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003877 // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two
3878 // signature polymorphic methods supported by the run-time which are native methods with variable
3879 // arguments.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003880 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003881 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003882 ((method_type == METHOD_SUPER ||
3883 method_type == METHOD_VIRTUAL ||
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003884 method_type == METHOD_INTERFACE) && res_method->IsDirect()) ||
3885 ((method_type == METHOD_POLYMORPHIC) &&
3886 (!res_method->IsNative() || !res_method->IsVarargs()))) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003887 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003888 "type of " << res_method->PrettyMethod();
Ian Rogers7b078e82014-09-10 14:44:24 -07003889 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003890 }
Alex Lightafb66472017-08-01 09:54:49 -07003891 // Make sure we weren't expecting to fail.
3892 DCHECK(!must_fail) << "invoke type (" << method_type << ")"
3893 << klass->PrettyDescriptor() << "."
3894 << dex_file_->GetMethodName(method_id) << " "
3895 << dex_file_->GetMethodSignature(method_id) << " unexpectedly resolved to "
3896 << res_method->PrettyMethod() << " without error. Initially this method was "
3897 << "not found so we were expecting to fail for some reason.";
jeffhao8cd6dda2012-02-22 10:15:34 -08003898 return res_method;
3899}
3900
Andreas Gampe99db7bb2019-04-19 23:05:47 -07003901template <bool kVerifierDebug>
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003902template <class T>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07003903ArtMethod* MethodVerifier<kVerifierDebug>::VerifyInvocationArgsFromIterator(
Mathieu Chartiere401d142015-04-22 13:56:20 -07003904 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampee383d232018-06-19 12:29:51 -07003905 DCHECK_EQ(!is_range, inst->HasVarArgs());
3906
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003907 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3908 // match the call to the signature. Also, we might be calling through an abstract method
3909 // definition (which doesn't have register count values).
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003910 const size_t expected_args = inst->VRegA();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003911 /* caught by static verifier */
3912 DCHECK(is_range || expected_args <= 5);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003913
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003914 if (expected_args > code_item_accessor_.OutsSize()) {
Orion Hodson1cda7c22017-08-10 13:06:45 +01003915 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08003916 << ") exceeds outsSize ("
3917 << code_item_accessor_.OutsSize() << ")";
Orion Hodson1cda7c22017-08-10 13:06:45 +01003918 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003919 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003920
3921 /*
3922 * Check the "this" argument, which must be an instance of the class that declared the method.
3923 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3924 * rigorous check here (which is okay since we have to do it at runtime).
3925 */
3926 if (method_type != METHOD_STATIC) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003927 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003928 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Andreas Gampe43884b22019-06-27 14:05:52 -07003929 CHECK(flags_.have_pending_hard_failure_);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003930 return nullptr;
3931 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003932 bool is_init = false;
3933 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003934 if (res_method) {
3935 if (!res_method->IsConstructor()) {
3936 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3937 return nullptr;
3938 }
3939 } else {
3940 // Check whether the name of the called method is "<init>"
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00003941 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Jeff Hao0d087272014-08-04 14:47:17 -07003942 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003943 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3944 return nullptr;
3945 }
3946 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003947 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003948 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003949 const RegType& adjusted_type = is_init
3950 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
3951 : actual_arg_type;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08003952 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZeroOrNull()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003953 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003954 // Miranda methods have the declaring interface as their declaring class, not the abstract
3955 // class. It would be wrong to use this for the type check (interface type checks are
3956 // postponed to runtime).
3957 if (res_method != nullptr && !res_method->IsMiranda()) {
Vladimir Markod93e3742018-07-18 10:58:13 +01003958 ObjPtr<mirror::Class> klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003959 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003960 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3961 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003962 } else {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00003963 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampea5b09a62016-11-17 15:21:22 -08003964 const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003965 res_method_class = &reg_types_.FromDescriptor(
Andreas Gampe51de69e2019-04-19 15:14:14 -07003966 class_loader_.Get(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07003967 dex_file_->StringByTypeIdx(class_idx),
3968 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003969 }
David Brazdilca3c8c32016-09-06 14:04:48 +01003970 if (!res_method_class->IsAssignableFrom(adjusted_type, this)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003971 Fail(adjusted_type.IsUnresolvedTypes()
Nicolas Geoffray04ea42c2021-01-22 10:04:56 +00003972 ? VERIFY_ERROR_UNRESOLVED_TYPE_CHECK
David Brazdil68b5c0b2016-01-19 14:25:29 +00003973 : VERIFY_ERROR_BAD_CLASS_SOFT)
3974 << "'this' argument '" << actual_arg_type << "' not instance of '"
3975 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003976 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3977 // the compiler.
Andreas Gampe43884b22019-06-27 14:05:52 -07003978 if (flags_.have_pending_hard_failure_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003979 return nullptr;
3980 }
3981 }
3982 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003983 }
3984
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003985 uint32_t arg[5];
3986 if (!is_range) {
3987 inst->GetVarArgs(arg);
3988 }
3989 uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003990 for ( ; it->HasNext(); it->Next()) {
3991 if (sig_registers >= expected_args) {
3992 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01003993 " argument registers, method signature has " << sig_registers + 1 << " or more";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003994 return nullptr;
3995 }
3996
3997 const char* param_descriptor = it->GetDescriptor();
3998
3999 if (param_descriptor == nullptr) {
4000 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
4001 "component";
4002 return nullptr;
4003 }
4004
Andreas Gampe51de69e2019-04-19 15:14:14 -07004005 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_.Get(),
4006 param_descriptor,
4007 false);
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004008 uint32_t get_reg = is_range ? inst->VRegC() + static_cast<uint32_t>(sig_registers) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004009 arg[sig_registers];
4010 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004011 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004012 if (!src_type.IsIntegralTypes()) {
4013 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
4014 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07004015 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004016 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07004017 } else {
4018 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
4019 // Continue on soft failures. We need to find possible hard failures to avoid problems in
4020 // the compiler.
Andreas Gampe43884b22019-06-27 14:05:52 -07004021 if (flags_.have_pending_hard_failure_) {
Andreas Gampeda9badb2015-06-05 20:22:12 -07004022 return nullptr;
4023 }
4024 } else if (reg_type.IsLongOrDoubleTypes()) {
4025 // Check that registers are consecutive (for non-range invokes). Invokes are the only
4026 // instructions not specifying register pairs by the first component, but require them
4027 // nonetheless. Only check when there's an actual register in the parameters. If there's
4028 // none, this will fail below.
4029 if (!is_range && sig_registers + 1 < expected_args) {
4030 uint32_t second_reg = arg[sig_registers + 1];
4031 if (second_reg != get_reg + 1) {
4032 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
4033 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
4034 << second_reg << ".";
4035 return nullptr;
4036 }
4037 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004038 }
4039 }
4040 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
4041 }
4042 if (expected_args != sig_registers) {
4043 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004044 " argument registers, method signature has " << sig_registers;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004045 return nullptr;
4046 }
4047 return res_method;
4048}
4049
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004050template <bool kVerifierDebug>
4051void MethodVerifier<kVerifierDebug>::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
4052 MethodType method_type,
4053 bool is_range) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004054 // As the method may not have been resolved, make this static check against what we expect.
4055 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
4056 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004057 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004058 DexFileParameterIterator it(*dex_file_,
4059 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004060 VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004061}
4062
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004063template <bool kVerifierDebug>
4064bool MethodVerifier<kVerifierDebug>::CheckCallSite(uint32_t call_site_idx) {
Orion Hodson3a842f52017-04-21 15:24:10 +01004065 if (call_site_idx >= dex_file_->NumCallSiteIds()) {
4066 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx
4067 << " >= " << dex_file_->NumCallSiteIds();
4068 return false;
4069 }
4070
Orion Hodsonc069a302017-01-18 09:23:12 +00004071 CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
Alex Light1e52a072019-06-25 09:12:04 -07004072 // Check essential arguments are provided. The dex file verifier has verified indices of the
Orion Hodsonc069a302017-01-18 09:23:12 +00004073 // main values (method handle, name, method_type).
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004074 static const size_t kRequiredArguments = 3;
4075 if (it.Size() < kRequiredArguments) {
Orion Hodsonc069a302017-01-18 09:23:12 +00004076 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
4077 << " has too few arguments: "
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004078 << it.Size() << " < " << kRequiredArguments;
Orion Hodsonc069a302017-01-18 09:23:12 +00004079 return false;
4080 }
4081
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004082 std::pair<const EncodedArrayValueIterator::ValueType, size_t> type_and_max[kRequiredArguments] =
4083 { { EncodedArrayValueIterator::ValueType::kMethodHandle, dex_file_->NumMethodHandles() },
4084 { EncodedArrayValueIterator::ValueType::kString, dex_file_->NumStringIds() },
4085 { EncodedArrayValueIterator::ValueType::kMethodType, dex_file_->NumProtoIds() }
4086 };
4087 uint32_t index[kRequiredArguments];
4088
4089 // Check arguments have expected types and are within permitted ranges.
4090 for (size_t i = 0; i < kRequiredArguments; ++i) {
4091 if (it.GetValueType() != type_and_max[i].first) {
4092 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx
4093 << " argument " << i << " has wrong type "
4094 << it.GetValueType() << "!=" << type_and_max[i].first;
4095 return false;
4096 }
4097 index[i] = static_cast<uint32_t>(it.GetJavaValue().i);
4098 if (index[i] >= type_and_max[i].second) {
4099 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx
4100 << " argument " << i << " bad index "
4101 << index[i] << " >= " << type_and_max[i].second;
4102 return false;
4103 }
4104 it.Next();
Orion Hodsona5dca522018-02-27 12:42:11 +00004105 }
Orion Hodson3a842f52017-04-21 15:24:10 +01004106
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004107 // Check method handle kind is valid.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004108 const dex::MethodHandleItem& mh = dex_file_->GetMethodHandle(index[0]);
Orion Hodsonc069a302017-01-18 09:23:12 +00004109 if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) {
4110 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
Orion Hodson631827d2017-04-10 14:53:47 +01004111 << " argument 0 method handle type is not InvokeStatic: "
4112 << mh.method_handle_type_;
Orion Hodsonc069a302017-01-18 09:23:12 +00004113 return false;
4114 }
Orion Hodsonc069a302017-01-18 09:23:12 +00004115 return true;
4116}
4117
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004118class MethodParamListDescriptorIterator {
4119 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004120 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004121 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
4122 params_size_(params_ == nullptr ? 0 : params_->Size()) {
4123 }
4124
4125 bool HasNext() {
4126 return pos_ < params_size_;
4127 }
4128
4129 void Next() {
4130 ++pos_;
4131 }
4132
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07004133 const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004134 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
4135 }
4136
4137 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004138 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004139 size_t pos_;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004140 const dex::TypeList* params_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004141 const size_t params_size_;
4142};
4143
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004144template <bool kVerifierDebug>
4145ArtMethod* MethodVerifier<kVerifierDebug>::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08004146 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08004147 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
4148 // we're making.
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004149 const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
Alex Light7268d472016-01-20 15:50:01 -08004150 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004151 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004152 // Check what we can statically.
Andreas Gampe43884b22019-06-27 14:05:52 -07004153 if (!flags_.have_pending_hard_failure_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004154 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
4155 }
4156 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08004157 }
4158
Ian Rogersd81871c2011-10-03 13:57:23 -07004159 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07004160 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08004161 if (method_type == METHOD_SUPER) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004162 dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
David Brazdilca3c8c32016-09-06 14:04:48 +01004163 const RegType& reference_type = reg_types_.FromDescriptor(
Andreas Gampe51de69e2019-04-19 15:14:14 -07004164 class_loader_.Get(),
David Brazdilca3c8c32016-09-06 14:04:48 +01004165 dex_file_->StringByTypeIdx(class_idx),
4166 false);
4167 if (reference_type.IsUnresolvedTypes()) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004168 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4169 return nullptr;
4170 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004171 if (reference_type.GetClass()->IsInterface()) {
Alex Light55ea94d2016-03-15 09:50:26 -07004172 if (!GetDeclaringClass().HasClass()) {
4173 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4174 << "interface invoke-super";
4175 return nullptr;
David Brazdilca3c8c32016-09-06 14:04:48 +01004176 } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004177 Fail(VERIFY_ERROR_CLASS_CHANGE)
David Sehr709b0702016-10-13 09:12:37 -07004178 << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass())
4179 << " in method "
4180 << dex_file_->PrettyMethod(dex_method_idx_) << " to method "
4181 << dex_file_->PrettyMethod(method_idx) << " references "
4182 << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass());
Alex Lightfedd91d2016-01-07 14:49:16 -08004183 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004184 }
4185 } else {
4186 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4187 if (super.IsUnresolvedTypes()) {
4188 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004189 << dex_file_->PrettyMethod(dex_method_idx_)
4190 << " to super " << res_method->PrettyMethod();
Alex Light705ad492015-09-21 11:36:30 -07004191 return nullptr;
4192 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004193 if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) ||
Aart Bikf663e342016-04-04 17:28:59 -07004194 (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Alex Light705ad492015-09-21 11:36:30 -07004195 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
David Sehr709b0702016-10-13 09:12:37 -07004196 << dex_file_->PrettyMethod(dex_method_idx_)
Alex Light705ad492015-09-21 11:36:30 -07004197 << " to super " << super
4198 << "." << res_method->GetName()
4199 << res_method->GetSignature();
4200 return nullptr;
4201 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004202 }
4203 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004204
Andreas Gampe74979b12017-05-16 09:28:06 -07004205 if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004206 // Process the signature of the calling site that is invoking the method handle.
Orion Hodson06d10a72018-05-14 08:53:38 +01004207 dex::ProtoIndex proto_idx(inst->VRegH());
4208 DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(proto_idx));
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004209 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4210 } else {
4211 // Process the target method's signature.
4212 MethodParamListDescriptorIterator it(res_method);
4213 return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
4214 }
4215}
4216
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004217template <bool kVerifierDebug>
4218bool MethodVerifier<kVerifierDebug>::CheckSignaturePolymorphicMethod(ArtMethod* method) {
Vladimir Markod93e3742018-07-18 10:58:13 +01004219 ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004220 const char* method_name = method->GetName();
4221
4222 const char* expected_return_descriptor;
Andreas Gampee0bbab92019-07-25 12:28:22 -07004223 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = GetClassLinker()->GetClassRoots();
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004224 if (klass == GetClassRoot<mirror::MethodHandle>(class_roots)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00004225 expected_return_descriptor = mirror::MethodHandle::GetReturnTypeDescriptor(method_name);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004226 } else if (klass == GetClassRoot<mirror::VarHandle>(class_roots)) {
Orion Hodsonfe92d122018-01-02 10:45:17 +00004227 expected_return_descriptor = mirror::VarHandle::GetReturnTypeDescriptor(method_name);
4228 } else {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004229 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004230 << "Signature polymorphic method in unsuppported class: " << klass->PrettyDescriptor();
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004231 return false;
4232 }
4233
Orion Hodsonfe92d122018-01-02 10:45:17 +00004234 if (expected_return_descriptor == nullptr) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004235 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4236 << "Signature polymorphic method name invalid: " << method_name;
4237 return false;
4238 }
4239
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004240 const dex::TypeList* types = method->GetParameterTypeList();
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004241 if (types->Size() != 1) {
4242 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4243 << "Signature polymorphic method has too many arguments " << types->Size() << " != 1";
4244 return false;
4245 }
4246
4247 const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_;
4248 const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index);
4249 if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) {
4250 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4251 << "Signature polymorphic method has unexpected argument type: " << argument_descriptor;
4252 return false;
4253 }
4254
4255 const char* return_descriptor = method->GetReturnTypeDescriptor();
Orion Hodsonfe92d122018-01-02 10:45:17 +00004256 if (strcmp(return_descriptor, expected_return_descriptor) != 0) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004257 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Orion Hodsonfe92d122018-01-02 10:45:17 +00004258 << "Signature polymorphic method has unexpected return type: " << return_descriptor
4259 << " != " << expected_return_descriptor;
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004260 return false;
4261 }
4262
4263 return true;
4264}
4265
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004266template <bool kVerifierDebug>
4267bool MethodVerifier<kVerifierDebug>::CheckSignaturePolymorphicReceiver(const Instruction* inst) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004268 const RegType& this_type = work_line_->GetInvocationThis(this, inst);
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004269 if (this_type.IsZeroOrNull()) {
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004270 /* null pointer always passes (and always fails at run time) */
4271 return true;
4272 } else if (!this_type.IsNonZeroReferenceTypes()) {
4273 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4274 << "invoke-polymorphic receiver is not a reference: "
4275 << this_type;
4276 return false;
4277 } else if (this_type.IsUninitializedReference()) {
4278 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4279 << "invoke-polymorphic receiver is uninitialized: "
4280 << this_type;
4281 return false;
4282 } else if (!this_type.HasClass()) {
4283 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4284 << "invoke-polymorphic receiver has no class: "
4285 << this_type;
4286 return false;
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004287 } else {
Andreas Gampee0bbab92019-07-25 12:28:22 -07004288 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = GetClassLinker()->GetClassRoots();
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01004289 if (!this_type.GetClass()->IsSubClass(GetClassRoot<mirror::MethodHandle>(class_roots)) &&
4290 !this_type.GetClass()->IsSubClass(GetClassRoot<mirror::VarHandle>(class_roots))) {
4291 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
4292 << "invoke-polymorphic receiver is not a subclass of MethodHandle or VarHandle: "
4293 << this_type;
4294 return false;
4295 }
Orion Hodsoncfa325e2016-10-13 10:25:54 +01004296 }
4297 return true;
Ian Rogersd81871c2011-10-03 13:57:23 -07004298}
4299
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004300template <bool kVerifierDebug>
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004301void MethodVerifier<kVerifierDebug>::VerifyNewArray(const Instruction* inst,
4302 bool is_filled,
4303 bool is_range) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004304 dex::TypeIndex type_idx;
Sebastien Hertz5243e912013-05-21 10:55:07 +02004305 if (!is_filled) {
4306 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004307 type_idx = dex::TypeIndex(inst->VRegC_22c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004308 } else if (!is_range) {
4309 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004310 type_idx = dex::TypeIndex(inst->VRegB_35c());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004311 } else {
4312 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
Andreas Gampea5b09a62016-11-17 15:21:22 -08004313 type_idx = dex::TypeIndex(inst->VRegB_3rc());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004314 }
Andreas Gampe98be1a92017-08-28 08:25:45 -07004315 const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004316 if (res_type.IsConflict()) { // bad class
4317 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004318 } else {
4319 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4320 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004321 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004322 } else if (!is_filled) {
4323 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004324 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004325 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004326 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004327 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004328 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004329 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004330 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4331 // the list and fail. It's legal, if silly, for arg_count to be zero.
Andreas Gampe51de69e2019-04-19 15:14:14 -07004332 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_.Get());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004333 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4334 uint32_t arg[5];
4335 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004336 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004337 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004338 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004339 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004340 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4341 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004342 return;
4343 }
4344 }
4345 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004346 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004347 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004348 }
4349 }
4350}
4351
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004352template <bool kVerifierDebug>
4353void MethodVerifier<kVerifierDebug>::VerifyAGet(const Instruction* inst,
4354 const RegType& insn_type,
4355 bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004356 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004357 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004358 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004359 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004360 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004361 if (array_type.IsZeroOrNull()) {
Ian Rogers89310de2012-02-01 13:47:30 -08004362 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
Andreas Gampe52f205a2017-12-01 12:16:07 -08004363 // instruction type.
4364 if (!is_primitive) {
4365 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Null());
4366 } else if (insn_type.IsInteger()) {
4367 // Pick a non-zero constant (to distinguish with null) that can fit in any primitive.
4368 // We cannot use 'insn_type' as it could be a float array or an int array.
4369 work_line_->SetRegisterType<LockOp::kClear>(
Nicolas Geoffray2cb25272021-07-15 14:24:29 +01004370 this, inst->VRegA_23x(), DetermineCat1Constant(1));
Andreas Gampe52f205a2017-12-01 12:16:07 -08004371 } else if (insn_type.IsCategory1Types()) {
4372 // Category 1
4373 // The 'insn_type' is exactly the type we need.
4374 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07004375 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004376 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004377 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4378 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004379 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004380 }
jeffhaofc3144e2012-02-01 17:21:15 -08004381 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004382 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004383 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004384 // Unresolved array types must be reference array types.
4385 if (is_primitive) {
4386 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4387 << " source for category 1 aget";
4388 } else {
4389 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4390 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004391 // Approximate with java.lang.Object[].
4392 work_line_->SetRegisterType<LockOp::kClear>(this,
4393 inst->VRegA_23x(),
4394 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004395 }
Ian Rogers89310de2012-02-01 13:47:30 -08004396 } else {
4397 /* verify the class */
Andreas Gampe51de69e2019-04-19 15:14:14 -07004398 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_.Get());
jeffhaofc3144e2012-02-01 17:21:15 -08004399 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004400 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004401 << " source for aget-object";
4402 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004403 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004404 << " source for category 1 aget";
4405 } else if (is_primitive && !insn_type.Equals(component_type) &&
4406 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004407 (insn_type.IsLong() && component_type.IsDouble()))) {
4408 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4409 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004410 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004411 // Use knowledge of the field type which is stronger than the type inferred from the
4412 // instruction, which can't differentiate object types and ints from floats, longs from
4413 // doubles.
4414 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004415 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004416 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004417 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004418 component_type.HighHalf(&reg_types_));
4419 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004420 }
4421 }
4422 }
4423}
4424
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004425template <bool kVerifierDebug>
4426void MethodVerifier<kVerifierDebug>::VerifyPrimitivePut(const RegType& target_type,
4427 const RegType& insn_type,
4428 const uint32_t vregA) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004429 // Primitive assignability rules are weaker than regular assignability rules.
4430 bool instruction_compatible;
4431 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004432 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004433 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004434 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004435 value_compatible = value_type.IsIntegralTypes();
4436 } else if (target_type.IsFloat()) {
4437 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4438 value_compatible = value_type.IsFloatTypes();
4439 } else if (target_type.IsLong()) {
4440 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004441 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4442 // as target_type depends on the resolved type of the field.
4443 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004444 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004445 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4446 } else {
4447 value_compatible = false;
4448 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004449 } else if (target_type.IsDouble()) {
4450 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004451 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4452 // as target_type depends on the resolved type of the field.
4453 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004454 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004455 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4456 } else {
4457 value_compatible = false;
4458 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004459 } else {
4460 instruction_compatible = false; // reference with primitive store
4461 value_compatible = false; // unused
4462 }
4463 if (!instruction_compatible) {
4464 // This is a global failure rather than a class change failure as the instructions and
4465 // the descriptors for the type should have been consistent within the same file at
4466 // compile time.
4467 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4468 << "' but expected type '" << target_type << "'";
4469 return;
4470 }
4471 if (!value_compatible) {
4472 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4473 << " of type " << value_type << " but expected " << target_type << " for put";
4474 return;
4475 }
4476}
4477
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004478template <bool kVerifierDebug>
4479void MethodVerifier<kVerifierDebug>::VerifyAPut(const Instruction* inst,
4480 const RegType& insn_type,
4481 bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004482 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004483 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004484 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004485 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004486 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004487 if (array_type.IsZeroOrNull()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004488 // Null array type; this code path will fail at runtime.
4489 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004490 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4491 // and fits multiple register types.
4492 const RegType* modified_reg_type = &insn_type;
4493 if ((modified_reg_type == &reg_types_.Integer()) ||
4494 (modified_reg_type == &reg_types_.LongLo())) {
4495 // May be integer or float | long or double. Overwrite insn_type accordingly.
4496 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4497 if (modified_reg_type == &reg_types_.Integer()) {
4498 if (&value_type == &reg_types_.Float()) {
4499 modified_reg_type = &value_type;
4500 }
4501 } else {
4502 if (&value_type == &reg_types_.DoubleLo()) {
4503 modified_reg_type = &value_type;
4504 }
4505 }
4506 }
4507 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004508 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004509 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004510 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004511 // Unresolved array types must be reference array types.
4512 if (is_primitive) {
4513 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4514 << "' but unresolved type '" << array_type << "'";
4515 } else {
4516 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4517 << " because of missing class";
4518 }
Ian Rogers89310de2012-02-01 13:47:30 -08004519 } else {
Andreas Gampe51de69e2019-04-19 15:14:14 -07004520 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_.Get());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004521 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004522 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004523 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004524 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004525 if (!component_type.IsReferenceTypes()) {
4526 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4527 << " source for aput-object";
4528 } else {
4529 // The instruction agrees with the type of array, confirm the value to be stored does too
4530 // Note: we use the instruction type (rather than the component type) for aput-object as
4531 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004532 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004533 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004534 }
4535 }
4536 }
4537}
4538
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004539template <bool kVerifierDebug>
4540ArtField* MethodVerifier<kVerifierDebug>::GetStaticField(int field_idx) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004541 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Ian Rogers90040192011-12-16 08:54:29 -08004542 // Check access to class
Andreas Gampe98be1a92017-08-28 08:25:45 -07004543 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004544 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004545 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4546 field_idx, dex_file_->GetFieldName(field_id),
4547 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004548 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004549 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004550 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004551 // Accessibility checks depend on resolved fields.
David Brazdil2bb2fbd2018-11-13 18:24:26 +00004552 DCHECK(klass_type.Equals(GetDeclaringClass()) ||
4553 !failures_.empty() ||
4554 IsSdkVersionSetAndLessThan(api_level_, SdkVersion::kP));
Andreas Gampe629be512017-08-25 17:09:32 -07004555
Ian Rogers7b078e82014-09-10 14:44:24 -07004556 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004557 }
Andreas Gampee0bbab92019-07-25 12:28:22 -07004558 ClassLinker* class_linker = GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004559 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004560
Ian Rogers7b078e82014-09-10 14:44:24 -07004561 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004562 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004563 << dex_file_->GetFieldName(field_id) << ") in "
4564 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004565 DCHECK(self_->IsExceptionPending());
4566 self_->ClearException();
4567 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004568 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4569 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004570 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField()
Ian Rogersad0b3a32012-04-16 14:50:24 -07004571 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004572 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004573 } else if (!field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004574 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004575 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004576 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004577 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004578}
4579
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004580template <bool kVerifierDebug>
4581ArtField* MethodVerifier<kVerifierDebug>::GetInstanceField(const RegType& obj_type, int field_idx) {
Andreas Gampeb34981b2019-05-06 13:00:40 -07004582 if (!obj_type.IsZeroOrNull() && !obj_type.IsReferenceTypes()) {
4583 // Trying to read a field from something that isn't a reference.
4584 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4585 << "non-reference type " << obj_type;
4586 return nullptr;
4587 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004588 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Aart Bik31883642016-06-06 15:02:44 -07004589 // Check access to class.
Andreas Gampe98be1a92017-08-28 08:25:45 -07004590 const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004591 if (klass_type.IsConflict()) {
4592 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4593 field_idx, dex_file_->GetFieldName(field_id),
4594 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004595 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004596 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004597 if (klass_type.IsUnresolvedTypes()) {
Andreas Gampe629be512017-08-25 17:09:32 -07004598 // Accessibility checks depend on resolved fields.
David Brazdil2bb2fbd2018-11-13 18:24:26 +00004599 DCHECK(klass_type.Equals(GetDeclaringClass()) ||
4600 !failures_.empty() ||
4601 IsSdkVersionSetAndLessThan(api_level_, SdkVersion::kP));
Andreas Gampe629be512017-08-25 17:09:32 -07004602
Ian Rogers7b078e82014-09-10 14:44:24 -07004603 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004604 }
Andreas Gampee0bbab92019-07-25 12:28:22 -07004605 ClassLinker* class_linker = GetClassLinker();
Vladimir Markoe11dd502017-12-08 14:09:45 +00004606 ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
David Brazdilca3c8c32016-09-06 14:04:48 +01004607
Ian Rogers7b078e82014-09-10 14:44:24 -07004608 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004609 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004610 << dex_file_->GetFieldName(field_id) << ") in "
4611 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004612 DCHECK(self_->IsExceptionPending());
4613 self_->ClearException();
4614 return nullptr;
Andreas Gampeeed3a5d2017-11-29 14:58:34 -08004615 } else if (obj_type.IsZeroOrNull()) {
Aart Bik31883642016-06-06 15:02:44 -07004616 // Cannot infer and check type, however, access will cause null pointer exception.
4617 // Fall through into a few last soft failure checks below.
Ian Rogerse1758fe2012-04-19 11:31:15 -07004618 } else {
David Brazdil0d638bb2016-07-27 15:29:25 +01004619 std::string temp;
Mathieu Chartier3398c782016-09-30 10:27:43 -07004620 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004621 const RegType& field_klass =
Vladimir Markobcf17522018-06-01 13:14:32 +01004622 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004623 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004624 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004625 // the field is declared in this class.
4626 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4627 // appear in constructors.
4628 if (!obj_type.IsUninitializedThisReference() ||
4629 !IsConstructor() ||
4630 !field_klass.Equals(GetDeclaringClass())) {
David Sehr709b0702016-10-13 09:12:37 -07004631 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField()
David Brazdil68b5c0b2016-01-19 14:25:29 +00004632 << " of a not fully initialized object within the context"
David Sehr709b0702016-10-13 09:12:37 -07004633 << " of " << dex_file_->PrettyMethod(dex_method_idx_);
David Brazdil68b5c0b2016-01-19 14:25:29 +00004634 return nullptr;
4635 }
David Brazdilca3c8c32016-09-06 14:04:48 +01004636 } else if (!field_klass.IsAssignableFrom(obj_type, this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004637 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4638 // of C1. For resolution to occur the declared class of the field must be compatible with
4639 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004640 VerifyError type;
Andreas Gampefef91cc2019-07-25 14:13:23 -07004641 bool is_aot = IsAotMode();
Andreas Gampe66596242016-04-14 10:55:04 -07004642 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4643 // Compiler & unresolved types involved, retry at runtime.
Nicolas Geoffray04ea42c2021-01-22 10:04:56 +00004644 type = VerifyError::VERIFY_ERROR_UNRESOLVED_TYPE_CHECK;
Andreas Gampe66596242016-04-14 10:55:04 -07004645 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004646 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4647 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004648 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4649 }
David Sehr709b0702016-10-13 09:12:37 -07004650 Fail(type) << "cannot access instance field " << field->PrettyField()
Andreas Gampe66596242016-04-14 10:55:04 -07004651 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004652 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004653 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004654 }
Aart Bik31883642016-06-06 15:02:44 -07004655
4656 // Few last soft failure checks.
4657 if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4658 field->GetAccessFlags())) {
David Sehr709b0702016-10-13 09:12:37 -07004659 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004660 << " from " << GetDeclaringClass();
4661 return nullptr;
4662 } else if (field->IsStatic()) {
David Sehr709b0702016-10-13 09:12:37 -07004663 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField()
Aart Bik31883642016-06-06 15:02:44 -07004664 << " to not be static";
4665 return nullptr;
4666 }
4667
4668 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004669}
4670
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004671template <bool kVerifierDebug>
4672template <FieldAccessType kAccType>
4673void MethodVerifier<kVerifierDebug>::VerifyISFieldAccess(const Instruction* inst,
4674 const RegType& insn_type,
4675 bool is_primitive,
4676 bool is_static) {
Nicolas Geoffray69b1cf12018-03-21 10:44:58 +00004677 uint32_t field_idx = GetFieldIdxOfFieldAccess(inst, is_static);
Mathieu Chartierc7853442015-03-27 14:35:38 -07004678 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004679 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004680 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004681 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004682 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004683
4684 // One is not allowed to access fields on uninitialized references, except to write to
4685 // fields in the constructor (before calling another constructor).
4686 // GetInstanceField does an assignability check which will fail for uninitialized types.
4687 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4688 // checks at the same time that we're verifying a constructor).
4689 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4690 object_type.IsUninitializedThisReference();
4691 const RegType& adjusted_type = should_adjust
4692 ? GetRegTypeCache()->FromUninitialized(object_type)
4693 : object_type;
4694 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe43884b22019-06-27 14:05:52 -07004695 if (UNLIKELY(flags_.have_pending_hard_failure_)) {
Andreas Gampe896df402014-10-20 22:25:29 -07004696 return;
4697 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08004698 if (should_adjust) {
4699 if (field == nullptr) {
4700 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
4701 << "to the superclass being initialized in "
David Sehr709b0702016-10-13 09:12:37 -07004702 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004703 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4704 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
David Sehr709b0702016-10-13 09:12:37 -07004705 << field->PrettyField() << " of a not fully initialized "
Alex Light4a2c8fc2016-02-12 11:01:54 -08004706 << "object within the context of "
David Sehr709b0702016-10-13 09:12:37 -07004707 << dex_file_->PrettyMethod(dex_method_idx_);
Alex Light4a2c8fc2016-02-12 11:01:54 -08004708 return;
4709 }
4710 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004711 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004712 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004713 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004714 if (kAccType == FieldAccessType::kAccPut) {
4715 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
David Sehr709b0702016-10-13 09:12:37 -07004716 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
Andreas Gampe896df402014-10-20 22:25:29 -07004717 << " from other class " << GetDeclaringClass();
Aart Bikc2bc2652016-05-23 14:58:49 -07004718 // Keep hunting for possible hard fails.
Andreas Gampe896df402014-10-20 22:25:29 -07004719 }
4720 }
4721
Mathieu Chartier3398c782016-09-30 10:27:43 -07004722 ObjPtr<mirror::Class> field_type_class =
Vladimir Marko208f6702017-12-08 12:00:50 +00004723 can_load_classes_ ? field->ResolveType() : field->LookupResolvedType();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004724 if (field_type_class != nullptr) {
Mathieu Chartier3398c782016-09-30 10:27:43 -07004725 field_type = &FromClass(field->GetTypeDescriptor(),
Vladimir Markobcf17522018-06-01 13:14:32 +01004726 field_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004727 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004728 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004729 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4730 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004731 }
David Brazdil2bb2fbd2018-11-13 18:24:26 +00004732 } else if (IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP)) {
Andreas Gampe7da4c402017-08-25 11:30:48 -07004733 // If we don't have the field (it seems we failed resolution) and this is a PUT, we need to
4734 // redo verification at runtime as the field may be final, unless the field id shows it's in
4735 // the same class.
4736 //
4737 // For simplicity, it is OK to not distinguish compile-time vs runtime, and post this an
4738 // ACCESS_FIELD failure at runtime. This has the same effect as NO_FIELD - punting the class
4739 // to the access-checks interpreter.
4740 //
4741 // Note: see b/34966607. This and above may be changed in the future.
4742 if (kAccType == FieldAccessType::kAccPut) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004743 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Andreas Gampe7da4c402017-08-25 11:30:48 -07004744 const char* field_class_descriptor = dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Andreas Gampe51de69e2019-04-19 15:14:14 -07004745 const RegType* field_class_type = &reg_types_.FromDescriptor(class_loader_.Get(),
Andreas Gampe7da4c402017-08-25 11:30:48 -07004746 field_class_descriptor,
4747 false);
4748 if (!field_class_type->Equals(GetDeclaringClass())) {
4749 Fail(VERIFY_ERROR_ACCESS_FIELD) << "could not check field put for final field modify of "
4750 << field_class_descriptor
4751 << "."
4752 << dex_file_->GetFieldName(field_id)
4753 << " from other class "
4754 << GetDeclaringClass();
4755 }
4756 }
Ian Rogers0d604842012-04-16 14:50:24 -07004757 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004758 if (field_type == nullptr) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08004759 const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004760 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Andreas Gampe51de69e2019-04-19 15:14:14 -07004761 field_type = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004762 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004763 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004764 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004765 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4766 "Unexpected third access type");
4767 if (kAccType == FieldAccessType::kAccPut) {
4768 // sput or iput.
4769 if (is_primitive) {
4770 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004771 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004772 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004773 // If the field type is not a reference, this is a global failure rather than
4774 // a class change failure as the instructions and the descriptors for the type
4775 // should have been consistent within the same file at compile time.
4776 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4777 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004778 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004779 << " to be compatible with type '" << insn_type
4780 << "' but found type '" << *field_type
4781 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004782 return;
4783 }
4784 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004785 }
Andreas Gampe896df402014-10-20 22:25:29 -07004786 } else if (kAccType == FieldAccessType::kAccGet) {
4787 // sget or iget.
4788 if (is_primitive) {
4789 if (field_type->Equals(insn_type) ||
4790 (field_type->IsFloat() && insn_type.IsInteger()) ||
4791 (field_type->IsDouble() && insn_type.IsLong())) {
4792 // expected that read is of the correct primitive type or that int reads are reading
4793 // floats or long reads are reading doubles
4794 } else {
4795 // This is a global failure rather than a class change failure as the instructions and
4796 // the descriptors for the type should have been consistent within the same file at
4797 // compile time
David Sehr709b0702016-10-13 09:12:37 -07004798 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
Andreas Gampe896df402014-10-20 22:25:29 -07004799 << " to be of type '" << insn_type
4800 << "' but found type '" << *field_type << "' in get";
4801 return;
4802 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004803 } else {
David Brazdilca3c8c32016-09-06 14:04:48 +01004804 if (!insn_type.IsAssignableFrom(*field_type, this)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004805 // If the field type is not a reference, this is a global failure rather than
4806 // a class change failure as the instructions and the descriptors for the type
4807 // should have been consistent within the same file at compile time.
4808 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4809 : VERIFY_ERROR_BAD_CLASS_HARD;
David Sehr709b0702016-10-13 09:12:37 -07004810 Fail(error) << "expected field " << ArtField::PrettyField(field)
Vladimir Marko414000e2015-06-23 17:45:21 +01004811 << " to be compatible with type '" << insn_type
4812 << "' but found type '" << *field_type
4813 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004814 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004815 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004816 }
Andreas Gampe896df402014-10-20 22:25:29 -07004817 return;
4818 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004819 }
Andreas Gampe896df402014-10-20 22:25:29 -07004820 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004821 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004822 } else {
4823 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4824 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004825 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004826 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004827 }
4828}
4829
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004830template <bool kVerifierDebug>
4831bool MethodVerifier<kVerifierDebug>::UpdateRegisters(uint32_t next_insn,
4832 RegisterLine* merge_line,
4833 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004834 bool changed = true;
4835 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004836 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004837 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004838 * We haven't processed this instruction before, and we haven't touched the registers here, so
4839 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4840 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004841 */
Andreas Gampea727e372015-08-25 09:22:37 -07004842 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004843 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004844 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004845 merge_line->VerifyMonitorStackEmpty(this);
4846
Ian Rogersb8c78592013-07-25 23:52:52 +00004847 // For returns we only care about the operand to the return, all other registers are dead.
4848 // Initialize them as conflicts so they don't add to GC and deoptimization information.
Mathieu Chartier3da1d0f2017-11-06 20:02:24 -08004849 const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004850 AdjustReturnLine(this, ret_inst, target_line);
Aart Bik31883642016-06-06 15:02:44 -07004851 // Directly bail if a hard failure was found.
Andreas Gampe43884b22019-06-27 14:05:52 -07004852 if (flags_.have_pending_hard_failure_) {
Aart Bikb0526322016-06-01 14:06:00 -07004853 return false;
4854 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004855 }
jeffhaobdb76512011-09-07 11:43:16 -07004856 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08004857 RegisterLineArenaUniquePtr copy;
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004858 if (kVerifierDebug) {
Andreas Gamped09c0592019-04-19 15:44:05 -07004859 copy.reset(RegisterLine::Create(target_line->NumRegs(), allocator_, GetRegTypeCache()));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004860 copy->CopyFromLine(target_line);
4861 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004862 changed = target_line->MergeRegisters(this, merge_line);
Andreas Gampe43884b22019-06-27 14:05:52 -07004863 if (flags_.have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004864 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004865 }
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004866 if (kVerifierDebug && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004867 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004868 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004869 << copy->Dump(this) << " MERGE\n"
4870 << merge_line->Dump(this) << " ==\n"
Andreas Gampe2ad6cce2019-04-11 16:17:39 -07004871 << target_line->Dump(this);
jeffhaobdb76512011-09-07 11:43:16 -07004872 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004873 if (update_merge_line && changed) {
4874 merge_line->CopyFromLine(target_line);
4875 }
jeffhaobdb76512011-09-07 11:43:16 -07004876 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004877 if (changed) {
Andreas Gampe51de69e2019-04-19 15:14:14 -07004878 GetModifiableInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004879 }
4880 return true;
4881}
4882
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004883template <bool kVerifierDebug>
4884const RegType& MethodVerifier<kVerifierDebug>::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004885 if (return_type_ == nullptr) {
Nicolas Geoffray2ec38232021-07-02 16:36:29 +01004886 const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4887 const dex::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4888 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
4889 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
4890 return_type_ = &reg_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004891 }
4892 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004893}
4894
Andreas Gampe99db7bb2019-04-19 23:05:47 -07004895template <bool kVerifierDebug>
Nicolas Geoffray2cb25272021-07-15 14:24:29 +01004896const RegType& MethodVerifier<kVerifierDebug>::DetermineCat1Constant(int32_t value) {
4897 // Imprecise constant type.
4898 if (value < -32768) {
4899 return reg_types_.IntConstant();
4900 } else if (value < -128) {
4901 return reg_types_.ShortConstant();
4902 } else if (value < 0) {
4903 return reg_types_.ByteConstant();
4904 } else if (value == 0) {
4905 return reg_types_.Zero();
4906 } else if (value == 1) {
4907 return reg_types_.One();
4908 } else if (value < 128) {
4909 return reg_types_.PosByteConstant();
4910 } else if (value < 32768) {
4911 return reg_types_.PosShortConstant();
4912 } else if (value < 65536) {
4913 return reg_types_.CharConstant();
Sebastien Hertz849600b2013-12-20 10:28:08 +01004914 } else {
Nicolas Geoffray2cb25272021-07-15 14:24:29 +01004915 return reg_types_.IntConstant();
Sebastien Hertz849600b2013-12-20 10:28:08 +01004916 }
4917}
4918
Nicolas Geoffray44dc8a32021-06-21 15:23:49 +01004919template <bool kVerifierDebug>
Nicolas Geoffraycd133d82021-06-25 16:10:39 +01004920bool MethodVerifier<kVerifierDebug>::PotentiallyMarkRuntimeThrow() {
Nicolas Geoffray44dc8a32021-06-21 15:23:49 +01004921 if (IsAotMode() || IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kT)) {
Nicolas Geoffraycd133d82021-06-25 16:10:39 +01004922 return false;
Nicolas Geoffray44dc8a32021-06-21 15:23:49 +01004923 }
Nicolas Geoffraycd133d82021-06-25 16:10:39 +01004924 // Compatibility mode: we treat the following code unreachable and the verifier
4925 // will not analyze it.
Nicolas Geoffray44dc8a32021-06-21 15:23:49 +01004926 // The verifier may fail before we touch any instruction, for the signature of a method. So
4927 // add a check.
4928 if (work_insn_idx_ < dex::kDexNoIndex) {
4929 const Instruction& inst = code_item_accessor_.InstructionAt(work_insn_idx_);
4930 Instruction::Code opcode = inst.Opcode();
Nicolas Geoffraycd133d82021-06-25 16:10:39 +01004931 if (opcode == Instruction::MOVE_EXCEPTION) {
4932 // This is an unreachable handler. The instruction doesn't throw, but we
4933 // mark the method as having a pending runtime throw failure so that
4934 // the compiler does not try to compile it.
Nicolas Geoffray2ec38232021-07-02 16:36:29 +01004935 Fail(VERIFY_ERROR_RUNTIME_THROW, /* pending_exc= */ false);
Nicolas Geoffraycd133d82021-06-25 16:10:39 +01004936 return true;
4937 }
4938 // How to handle runtime failures for instructions that are not flagged kThrow.
Nicolas Geoffray44dc8a32021-06-21 15:23:49 +01004939 if ((Instruction::FlagsOf(opcode) & Instruction::kThrow) == 0 &&
4940 !impl::IsCompatThrow(opcode) &&
4941 GetInstructionFlags(work_insn_idx_).IsInTry()) {
4942 if (Runtime::Current()->IsVerifierMissingKThrowFatal()) {
4943 LOG(FATAL) << "Unexpected throw: " << std::hex << work_insn_idx_ << " " << opcode;
4944 UNREACHABLE();
4945 }
4946 // We need to save the work_line if the instruction wasn't throwing before. Otherwise
4947 // we'll try to merge garbage.
4948 // Note: this assumes that Fail is called before we do any work_line modifications.
4949 saved_line_->CopyFromLine(work_line_.get());
4950 }
4951 }
Nicolas Geoffraycd133d82021-06-25 16:10:39 +01004952 flags_.have_pending_runtime_throw_failure_ = true;
4953 return true;
Nicolas Geoffray44dc8a32021-06-21 15:23:49 +01004954}
4955
Andreas Gampefc25ae92019-04-19 22:22:57 -07004956} // namespace
4957} // namespace impl
4958
4959MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07004960 ClassLinker* class_linker,
Andreas Gampef1468b52019-07-26 09:22:39 -07004961 ArenaPool* arena_pool,
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +00004962 VerifierDeps* verifier_deps,
Andreas Gampefc25ae92019-04-19 22:22:57 -07004963 const DexFile* dex_file,
Nicolas Geoffray1960c422020-11-04 08:45:32 +00004964 const dex::ClassDef& class_def,
Andreas Gampefc25ae92019-04-19 22:22:57 -07004965 const dex::CodeItem* code_item,
4966 uint32_t dex_method_idx,
4967 bool can_load_classes,
4968 bool allow_thread_suspension,
Andreas Gampefef91cc2019-07-25 14:13:23 -07004969 bool allow_soft_failures,
4970 bool aot_mode)
Andreas Gampefc25ae92019-04-19 22:22:57 -07004971 : self_(self),
Andreas Gampef1468b52019-07-26 09:22:39 -07004972 arena_stack_(arena_pool),
Andreas Gampefc25ae92019-04-19 22:22:57 -07004973 allocator_(&arena_stack_),
Andreas Gampee0bbab92019-07-25 12:28:22 -07004974 reg_types_(class_linker, can_load_classes, allocator_, allow_thread_suspension),
Andreas Gampefc25ae92019-04-19 22:22:57 -07004975 reg_table_(allocator_),
4976 work_insn_idx_(dex::kDexNoIndex),
4977 dex_method_idx_(dex_method_idx),
4978 dex_file_(dex_file),
Nicolas Geoffray1960c422020-11-04 08:45:32 +00004979 class_def_(class_def),
Andreas Gampefc25ae92019-04-19 22:22:57 -07004980 code_item_accessor_(*dex_file, code_item),
Andreas Gampefef91cc2019-07-25 14:13:23 -07004981 // TODO: make it designated initialization when we compile as C++20.
Nicolas Geoffray2ec38232021-07-02 16:36:29 +01004982 flags_({false, false, aot_mode}),
Andreas Gampefc25ae92019-04-19 22:22:57 -07004983 encountered_failure_types_(0),
4984 can_load_classes_(can_load_classes),
4985 allow_soft_failures_(allow_soft_failures),
Andreas Gampee0bbab92019-07-25 12:28:22 -07004986 class_linker_(class_linker),
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +00004987 verifier_deps_(verifier_deps),
Andreas Gampefc25ae92019-04-19 22:22:57 -07004988 link_(nullptr) {
4989 self->PushVerifier(this);
4990}
4991
4992MethodVerifier::~MethodVerifier() {
4993 Thread::Current()->PopVerifier(this);
4994 STLDeleteElements(&failure_messages_);
4995}
4996
4997MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07004998 ClassLinker* class_linker,
Andreas Gampef1468b52019-07-26 09:22:39 -07004999 ArenaPool* arena_pool,
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +00005000 VerifierDeps* verifier_deps,
Andreas Gampefc25ae92019-04-19 22:22:57 -07005001 uint32_t method_idx,
5002 const DexFile* dex_file,
5003 Handle<mirror::DexCache> dex_cache,
5004 Handle<mirror::ClassLoader> class_loader,
5005 const dex::ClassDef& class_def,
5006 const dex::CodeItem* code_item,
Andreas Gampefc25ae92019-04-19 22:22:57 -07005007 uint32_t method_access_flags,
Andreas Gampefc25ae92019-04-19 22:22:57 -07005008 bool allow_soft_failures,
5009 HardFailLogMode log_level,
Andreas Gampefc25ae92019-04-19 22:22:57 -07005010 uint32_t api_level,
Andreas Gampefef91cc2019-07-25 14:13:23 -07005011 bool aot_mode,
Andreas Gampefc25ae92019-04-19 22:22:57 -07005012 std::string* hard_failure_msg) {
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005013 if (VLOG_IS_ON(verifier_debug)) {
5014 return VerifyMethod<true>(self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005015 class_linker,
Andreas Gampef1468b52019-07-26 09:22:39 -07005016 arena_pool,
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +00005017 verifier_deps,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005018 method_idx,
5019 dex_file,
5020 dex_cache,
5021 class_loader,
5022 class_def,
5023 code_item,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005024 method_access_flags,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005025 allow_soft_failures,
5026 log_level,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005027 api_level,
Andreas Gampefef91cc2019-07-25 14:13:23 -07005028 aot_mode,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005029 hard_failure_msg);
5030 } else {
5031 return VerifyMethod<false>(self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005032 class_linker,
Andreas Gampef1468b52019-07-26 09:22:39 -07005033 arena_pool,
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +00005034 verifier_deps,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005035 method_idx,
5036 dex_file,
5037 dex_cache,
5038 class_loader,
5039 class_def,
5040 code_item,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005041 method_access_flags,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005042 allow_soft_failures,
5043 log_level,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005044 api_level,
Andreas Gampefef91cc2019-07-25 14:13:23 -07005045 aot_mode,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005046 hard_failure_msg);
5047 }
5048}
5049
Nicolas Geoffray1715efa2020-06-05 18:34:49 +01005050// Return whether the runtime knows how to execute a method without needing to
Nicolas Geoffraybd570592020-12-23 16:37:44 +00005051// re-verify it at runtime (and therefore save on first use of the class).
Nicolas Geoffray1715efa2020-06-05 18:34:49 +01005052// The AOT/JIT compiled code is not affected.
5053static inline bool CanRuntimeHandleVerificationFailure(uint32_t encountered_failure_types) {
5054 constexpr uint32_t unresolved_mask =
Nicolas Geoffrayd1728bf2021-01-12 14:02:29 +00005055 verifier::VerifyError::VERIFY_ERROR_UNRESOLVED_TYPE_CHECK |
Nicolas Geoffray04ea42c2021-01-22 10:04:56 +00005056 verifier::VerifyError::VERIFY_ERROR_NO_CLASS |
Nicolas Geoffrayb677aff2020-12-23 17:07:10 +00005057 verifier::VerifyError::VERIFY_ERROR_CLASS_CHANGE |
Nicolas Geoffray54ed0152020-12-23 17:29:32 +00005058 verifier::VerifyError::VERIFY_ERROR_INSTANTIATION |
Nicolas Geoffray1715efa2020-06-05 18:34:49 +01005059 verifier::VerifyError::VERIFY_ERROR_ACCESS_CLASS |
5060 verifier::VerifyError::VERIFY_ERROR_ACCESS_FIELD |
Nicolas Geoffraybd570592020-12-23 16:37:44 +00005061 verifier::VerifyError::VERIFY_ERROR_NO_METHOD |
Nicolas Geoffray2ec38232021-07-02 16:36:29 +01005062 verifier::VerifyError::VERIFY_ERROR_ACCESS_METHOD |
5063 verifier::VerifyError::VERIFY_ERROR_RUNTIME_THROW;
Nicolas Geoffray1715efa2020-06-05 18:34:49 +01005064 return (encountered_failure_types & (~unresolved_mask)) == 0;
5065}
5066
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005067template <bool kVerifierDebug>
5068MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005069 ClassLinker* class_linker,
Andreas Gampef1468b52019-07-26 09:22:39 -07005070 ArenaPool* arena_pool,
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +00005071 VerifierDeps* verifier_deps,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005072 uint32_t method_idx,
5073 const DexFile* dex_file,
5074 Handle<mirror::DexCache> dex_cache,
5075 Handle<mirror::ClassLoader> class_loader,
5076 const dex::ClassDef& class_def,
5077 const dex::CodeItem* code_item,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005078 uint32_t method_access_flags,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005079 bool allow_soft_failures,
5080 HardFailLogMode log_level,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005081 uint32_t api_level,
Andreas Gampefef91cc2019-07-25 14:13:23 -07005082 bool aot_mode,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005083 std::string* hard_failure_msg) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005084 MethodVerifier::FailureData result;
5085 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
5086
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005087 impl::MethodVerifier<kVerifierDebug> verifier(self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005088 class_linker,
Andreas Gampef1468b52019-07-26 09:22:39 -07005089 arena_pool,
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +00005090 verifier_deps,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005091 dex_file,
Andreas Gampe0134a282019-07-29 11:34:55 -07005092 code_item,
5093 method_idx,
Alex Lightc2d0c962019-10-23 14:14:25 -07005094 /* can_load_classes= */ true,
5095 /* allow_thread_suspension= */ true,
Andreas Gampe0134a282019-07-29 11:34:55 -07005096 allow_soft_failures,
5097 aot_mode,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005098 dex_cache,
5099 class_loader,
5100 class_def,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005101 method_access_flags,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005102 /* verify to dump */ false,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005103 api_level);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005104 if (verifier.Verify()) {
5105 // Verification completed, however failures may be pending that didn't cause the verification
5106 // to hard fail.
Andreas Gampe43884b22019-06-27 14:05:52 -07005107 CHECK(!verifier.flags_.have_pending_hard_failure_);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005108
Andreas Gampefc25ae92019-04-19 22:22:57 -07005109 if (verifier.failures_.size() != 0) {
5110 if (VLOG_IS_ON(verifier)) {
5111 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
5112 << dex_file->PrettyMethod(method_idx) << "\n");
5113 }
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005114 if (kVerifierDebug) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005115 LOG(INFO) << verifier.info_messages_.str();
5116 verifier.Dump(LOG_STREAM(INFO));
5117 }
Nicolas Geoffray1715efa2020-06-05 18:34:49 +01005118 if (CanRuntimeHandleVerificationFailure(verifier.encountered_failure_types_)) {
Nicolas Geoffrayd1728bf2021-01-12 14:02:29 +00005119 if (verifier.encountered_failure_types_ & VERIFY_ERROR_UNRESOLVED_TYPE_CHECK) {
5120 result.kind = FailureKind::kTypeChecksFailure;
5121 } else {
5122 result.kind = FailureKind::kAccessChecksFailure;
5123 }
Nicolas Geoffray1715efa2020-06-05 18:34:49 +01005124 } else {
5125 result.kind = FailureKind::kSoftFailure;
5126 }
Andreas Gampefc25ae92019-04-19 22:22:57 -07005127 }
5128 } else {
5129 // Bad method data.
5130 CHECK_NE(verifier.failures_.size(), 0U);
Nicolas Geoffrayf8b52882021-06-25 17:00:30 +01005131 CHECK(verifier.flags_.have_pending_hard_failure_);
5132 if (VLOG_IS_ON(verifier)) {
5133 log_level = std::max(HardFailLogMode::kLogVerbose, log_level);
5134 }
5135 if (log_level >= HardFailLogMode::kLogVerbose) {
5136 LogSeverity severity;
5137 switch (log_level) {
5138 case HardFailLogMode::kLogVerbose:
5139 severity = LogSeverity::VERBOSE;
5140 break;
5141 case HardFailLogMode::kLogWarning:
5142 severity = LogSeverity::WARNING;
5143 break;
5144 case HardFailLogMode::kLogInternalFatal:
5145 severity = LogSeverity::FATAL_WITHOUT_ABORT;
5146 break;
5147 default:
5148 LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level);
5149 UNREACHABLE();
5150 }
5151 verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in "
5152 << dex_file->PrettyMethod(method_idx)
5153 << "\n");
5154 }
5155 if (hard_failure_msg != nullptr) {
5156 CHECK(!verifier.failure_messages_.empty());
5157 *hard_failure_msg =
5158 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
5159 }
5160 result.kind = FailureKind::kHardFailure;
Andreas Gampefc25ae92019-04-19 22:22:57 -07005161
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005162 if (kVerifierDebug || VLOG_IS_ON(verifier)) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005163 LOG(ERROR) << verifier.info_messages_.str();
5164 verifier.Dump(LOG_STREAM(ERROR));
5165 }
5166 // Under verifier-debug, dump the complete log into the error message.
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005167 if (kVerifierDebug && hard_failure_msg != nullptr) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005168 hard_failure_msg->append("\n");
5169 hard_failure_msg->append(verifier.info_messages_.str());
5170 hard_failure_msg->append("\n");
5171 std::ostringstream oss;
5172 verifier.Dump(oss);
5173 hard_failure_msg->append(oss.str());
5174 }
5175 }
5176 if (kTimeVerifyMethod) {
5177 uint64_t duration_ns = NanoTime() - start_ns;
5178 if (duration_ns > MsToNs(Runtime::Current()->GetVerifierLoggingThresholdMs())) {
Andreas Gampeefdd1b02019-05-07 12:30:10 -07005179 double bytecodes_per_second =
5180 verifier.code_item_accessor_.InsnsSizeInCodeUnits() / (duration_ns * 1e-9);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005181 LOG(WARNING) << "Verification of " << dex_file->PrettyMethod(method_idx)
5182 << " took " << PrettyDuration(duration_ns)
Andreas Gampeefdd1b02019-05-07 12:30:10 -07005183 << (impl::IsLargeMethod(verifier.CodeItem()) ? " (large method)" : "")
Andreas Gamped91f8412019-06-13 10:11:14 -07005184 << " (" << StringPrintf("%.2f", bytecodes_per_second) << " bytecodes/s)"
5185 << " (" << verifier.allocator_.ApproximatePeakBytes()
5186 << "B approximate peak alloc)";
Andreas Gampefc25ae92019-04-19 22:22:57 -07005187 }
5188 }
5189 result.types = verifier.encountered_failure_types_;
5190 return result;
5191}
5192
Alex Lighte2ddce32019-05-22 17:08:35 +00005193MethodVerifier* MethodVerifier::CalculateVerificationInfo(
5194 Thread* self,
5195 ArtMethod* method,
Nicolas Geoffray2cb25272021-07-15 14:24:29 +01005196 uint32_t dex_pc) {
5197 StackHandleScope<2> hs(self);
5198 Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache()));
5199 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(method->GetClassLoader()));
Alex Lighte2ddce32019-05-22 17:08:35 +00005200 std::unique_ptr<impl::MethodVerifier<false>> verifier(
5201 new impl::MethodVerifier<false>(self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005202 Runtime::Current()->GetClassLinker(),
Andreas Gampef1468b52019-07-26 09:22:39 -07005203 Runtime::Current()->GetArenaPool(),
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +00005204 /* verifier_deps= */ nullptr,
Alex Lighte2ddce32019-05-22 17:08:35 +00005205 method->GetDexFile(),
Andreas Gampe0134a282019-07-29 11:34:55 -07005206 method->GetCodeItem(),
5207 method->GetDexMethodIndex(),
5208 /* can_load_classes= */ false,
5209 /* allow_thread_suspension= */ false,
5210 /* allow_soft_failures= */ true,
5211 Runtime::Current()->IsAotCompiler(),
Alex Lighte2ddce32019-05-22 17:08:35 +00005212 dex_cache,
5213 class_loader,
5214 *method->GetDeclaringClass()->GetClassDef(),
Alex Lighte2ddce32019-05-22 17:08:35 +00005215 method->GetAccessFlags(),
Alex Lighte2ddce32019-05-22 17:08:35 +00005216 /* verify_to_dump= */ false,
Alex Lightaf52cbe2019-05-22 15:05:09 -07005217 // Just use the verifier at the current skd-version.
5218 // This might affect what soft-verifier errors are reported.
5219 // Callers can then filter out relevant errors if needed.
5220 Runtime::Current()->GetTargetSdkVersion()));
Nicolas Geoffray2cb25272021-07-15 14:24:29 +01005221 verifier->interesting_dex_pc_ = dex_pc;
Alex Lighte2ddce32019-05-22 17:08:35 +00005222 verifier->Verify();
5223 if (VLOG_IS_ON(verifier)) {
5224 verifier->DumpFailures(VLOG_STREAM(verifier));
5225 VLOG(verifier) << verifier->info_messages_.str();
5226 verifier->Dump(VLOG_STREAM(verifier));
5227 }
Andreas Gampe43884b22019-06-27 14:05:52 -07005228 if (verifier->flags_.have_pending_hard_failure_) {
Alex Lighte2ddce32019-05-22 17:08:35 +00005229 return nullptr;
5230 } else {
5231 return verifier.release();
5232 }
5233}
5234
Andreas Gampefc25ae92019-04-19 22:22:57 -07005235MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
5236 VariableIndentationOutputStream* vios,
5237 uint32_t dex_method_idx,
5238 const DexFile* dex_file,
5239 Handle<mirror::DexCache> dex_cache,
5240 Handle<mirror::ClassLoader> class_loader,
5241 const dex::ClassDef& class_def,
5242 const dex::CodeItem* code_item,
Andreas Gampefc25ae92019-04-19 22:22:57 -07005243 uint32_t method_access_flags,
5244 uint32_t api_level) {
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005245 impl::MethodVerifier<false>* verifier = new impl::MethodVerifier<false>(
5246 self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005247 Runtime::Current()->GetClassLinker(),
Andreas Gampef1468b52019-07-26 09:22:39 -07005248 Runtime::Current()->GetArenaPool(),
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +00005249 /* verifier_deps= */ nullptr,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005250 dex_file,
Andreas Gampe0134a282019-07-29 11:34:55 -07005251 code_item,
5252 dex_method_idx,
5253 /* can_load_classes= */ true,
5254 /* allow_thread_suspension= */ true,
5255 /* allow_soft_failures= */ true,
5256 Runtime::Current()->IsAotCompiler(),
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005257 dex_cache,
5258 class_loader,
5259 class_def,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005260 method_access_flags,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005261 /* verify_to_dump= */ true,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005262 api_level);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005263 verifier->Verify();
5264 verifier->DumpFailures(vios->Stream());
5265 vios->Stream() << verifier->info_messages_.str();
5266 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
5267 // and querying any info is dangerous/can abort.
Andreas Gampe43884b22019-06-27 14:05:52 -07005268 if (verifier->flags_.have_pending_hard_failure_) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005269 delete verifier;
5270 return nullptr;
5271 } else {
5272 verifier->Dump(vios);
5273 return verifier;
5274 }
5275}
5276
5277void MethodVerifier::FindLocksAtDexPc(
5278 ArtMethod* m,
5279 uint32_t dex_pc,
5280 std::vector<MethodVerifier::DexLockInfo>* monitor_enter_dex_pcs,
5281 uint32_t api_level) {
5282 StackHandleScope<2> hs(Thread::Current());
5283 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
5284 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005285 impl::MethodVerifier<false> verifier(hs.Self(),
Andreas Gampee0bbab92019-07-25 12:28:22 -07005286 Runtime::Current()->GetClassLinker(),
Andreas Gampef1468b52019-07-26 09:22:39 -07005287 Runtime::Current()->GetArenaPool(),
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +00005288 /* verifier_deps= */ nullptr,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005289 m->GetDexFile(),
Andreas Gampe0134a282019-07-29 11:34:55 -07005290 m->GetCodeItem(),
5291 m->GetDexMethodIndex(),
5292 /* can_load_classes= */ false,
5293 /* allow_thread_suspension= */ false,
5294 /* allow_soft_failures= */ true,
5295 Runtime::Current()->IsAotCompiler(),
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005296 dex_cache,
5297 class_loader,
5298 m->GetClassDef(),
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005299 m->GetAccessFlags(),
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005300 /* verify_to_dump= */ false,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005301 api_level);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005302 verifier.interesting_dex_pc_ = dex_pc;
5303 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
5304 verifier.FindLocksAtDexPc();
5305}
5306
5307MethodVerifier* MethodVerifier::CreateVerifier(Thread* self,
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +00005308 VerifierDeps* verifier_deps,
Andreas Gampefc25ae92019-04-19 22:22:57 -07005309 const DexFile* dex_file,
5310 Handle<mirror::DexCache> dex_cache,
5311 Handle<mirror::ClassLoader> class_loader,
5312 const dex::ClassDef& class_def,
5313 const dex::CodeItem* code_item,
5314 uint32_t method_idx,
Andreas Gampefc25ae92019-04-19 22:22:57 -07005315 uint32_t access_flags,
5316 bool can_load_classes,
5317 bool allow_soft_failures,
Andreas Gampefc25ae92019-04-19 22:22:57 -07005318 bool verify_to_dump,
5319 bool allow_thread_suspension,
5320 uint32_t api_level) {
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005321 return new impl::MethodVerifier<false>(self,
Andreas Gampee0bbab92019-07-25 12:28:22 -07005322 Runtime::Current()->GetClassLinker(),
Andreas Gampef1468b52019-07-26 09:22:39 -07005323 Runtime::Current()->GetArenaPool(),
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +00005324 verifier_deps,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005325 dex_file,
Andreas Gampe0134a282019-07-29 11:34:55 -07005326 code_item,
5327 method_idx,
5328 can_load_classes,
5329 allow_thread_suspension,
5330 allow_soft_failures,
5331 Runtime::Current()->IsAotCompiler(),
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005332 dex_cache,
5333 class_loader,
5334 class_def,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005335 access_flags,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005336 verify_to_dump,
Andreas Gampe99db7bb2019-04-19 23:05:47 -07005337 api_level);
Andreas Gampefc25ae92019-04-19 22:22:57 -07005338}
5339
Andreas Gampee0bbab92019-07-25 12:28:22 -07005340void MethodVerifier::Init(ClassLinker* class_linker) {
5341 art::verifier::RegTypeCache::Init(class_linker);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08005342}
5343
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005344void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005345 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08005346}
jeffhaod1224c72012-02-29 13:43:08 -08005347
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005348void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
5349 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07005350}
5351
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005352void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
5353 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08005354}
5355
Andreas Gampe4146e062019-07-10 13:18:04 -07005356std::ostream& MethodVerifier::Fail(VerifyError error, bool pending_exc) {
Andreas Gampefc25ae92019-04-19 22:22:57 -07005357 // Mark the error type as encountered.
5358 encountered_failure_types_ |= static_cast<uint32_t>(error);
5359
Andreas Gampe4146e062019-07-10 13:18:04 -07005360 if (pending_exc) {
5361 switch (error) {
5362 case VERIFY_ERROR_NO_CLASS:
Nicolas Geoffray04ea42c2021-01-22 10:04:56 +00005363 case VERIFY_ERROR_UNRESOLVED_TYPE_CHECK:
Andreas Gampe4146e062019-07-10 13:18:04 -07005364 case VERIFY_ERROR_NO_METHOD:
5365 case VERIFY_ERROR_ACCESS_CLASS:
5366 case VERIFY_ERROR_ACCESS_FIELD:
5367 case VERIFY_ERROR_ACCESS_METHOD:
5368 case VERIFY_ERROR_INSTANTIATION:
Nicolas Geoffray44dc8a32021-06-21 15:23:49 +01005369 case VERIFY_ERROR_CLASS_CHANGE: {
5370 PotentiallyMarkRuntimeThrow();
Andreas Gampe4146e062019-07-10 13:18:04 -07005371 break;
Nicolas Geoffray44dc8a32021-06-21 15:23:49 +01005372 }
Andreas Gampe7a82acc2019-07-01 13:54:21 -07005373
Nicolas Geoffray35a37202021-01-29 17:53:32 +00005374 case VERIFY_ERROR_LOCKING:
Nicolas Geoffray44dc8a32021-06-21 15:23:49 +01005375 PotentiallyMarkRuntimeThrow();
Nicolas Geoffray35a37202021-01-29 17:53:32 +00005376 // This will be reported to the runtime as a soft failure.
5377 break;
5378
Nicolas Geoffrayd1728bf2021-01-12 14:02:29 +00005379 // Indication that verification should be retried at runtime.
Andreas Gampe4146e062019-07-10 13:18:04 -07005380 case VERIFY_ERROR_BAD_CLASS_SOFT:
5381 if (!allow_soft_failures_) {
5382 flags_.have_pending_hard_failure_ = true;
5383 }
5384 break;
5385
Nicolas Geoffray35a37202021-01-29 17:53:32 +00005386 // Hard verification failures at compile time will still fail at runtime, so the class is
5387 // marked as rejected to prevent it from being compiled.
Andreas Gampe4146e062019-07-10 13:18:04 -07005388 case VERIFY_ERROR_BAD_CLASS_HARD: {
Andreas Gampe4a30f892019-07-10 04:57:58 +00005389 flags_.have_pending_hard_failure_ = true;
Andreas Gampe4146e062019-07-10 13:18:04 -07005390 break;
Andreas Gampe4a30f892019-07-10 04:57:58 +00005391 }
Nicolas Geoffray2ec38232021-07-02 16:36:29 +01005392
5393 case VERIFY_ERROR_RUNTIME_THROW: {
5394 LOG(FATAL) << "UNREACHABLE";
5395 }
Andreas Gampe4a30f892019-07-10 04:57:58 +00005396 }
Andreas Gampe4146e062019-07-10 13:18:04 -07005397 } else if (kIsDebugBuild) {
5398 CHECK_NE(error, VERIFY_ERROR_BAD_CLASS_SOFT);
5399 CHECK_NE(error, VERIFY_ERROR_BAD_CLASS_HARD);
Andreas Gampe4a30f892019-07-10 04:57:58 +00005400 }
Andreas Gampe4146e062019-07-10 13:18:04 -07005401
Andreas Gampefc25ae92019-04-19 22:22:57 -07005402 failures_.push_back(error);
5403 std::string location(StringPrintf("%s: [0x%X] ", dex_file_->PrettyMethod(dex_method_idx_).c_str(),
5404 work_insn_idx_));
5405 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
5406 failure_messages_.push_back(failure_message);
5407 return *failure_message;
Andreas Gampef23f33d2015-06-23 14:18:17 -07005408}
5409
Andreas Gampefc25ae92019-04-19 22:22:57 -07005410ScopedNewLine MethodVerifier::LogVerifyInfo() {
5411 ScopedNewLine ret{info_messages_};
5412 ret << "VFY: " << dex_file_->PrettyMethod(dex_method_idx_)
5413 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
5414 return ret;
5415}
5416
5417static FailureKind FailureKindMax(FailureKind fk1, FailureKind fk2) {
5418 static_assert(FailureKind::kNoFailure < FailureKind::kSoftFailure
5419 && FailureKind::kSoftFailure < FailureKind::kHardFailure,
5420 "Unexpected FailureKind order");
5421 return std::max(fk1, fk2);
5422}
5423
5424void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) {
5425 kind = FailureKindMax(kind, fd.kind);
5426 types |= fd.types;
Andreas Gampe51de69e2019-04-19 15:14:14 -07005427}
5428
Ian Rogersd81871c2011-10-03 13:57:23 -07005429} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005430} // namespace art