1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ART_RUNTIME_VERIFIER_METHOD_VERIFIER_H_
#define ART_RUNTIME_VERIFIER_METHOD_VERIFIER_H_
#include <memory>
#include <sstream>
#include <vector>
#include <android-base/logging.h>
#include "base/arena_allocator.h"
#include "base/macros.h"
#include "base/scoped_arena_containers.h"
#include "base/value_object.h"
#include "dex/code_item_accessors.h"
#include "dex/dex_file_types.h"
#include "dex/method_reference.h"
#include "handle.h"
#include "instruction_flags.h"
#include "reg_type_cache.h"
#include "register_line.h"
#include "verifier_enums.h"
namespace art {
class ClassLinker;
class CompilerCallbacks;
class DexFile;
class Instruction;
struct ReferenceMap2Visitor;
class Thread;
class VariableIndentationOutputStream;
namespace dex {
struct ClassDef;
struct CodeItem;
} // namespace dex
namespace mirror {
class DexCache;
} // namespace mirror
namespace verifier {
class MethodVerifier;
class RegisterLine;
using RegisterLineArenaUniquePtr = std::unique_ptr<RegisterLine, RegisterLineArenaDelete>;
class RegType;
struct ScopedNewLine;
// We don't need to store the register data for many instructions, because we either only need
// it at branch points (for verification) or GC points and branches (for verification +
// type-precise register analysis).
enum RegisterTrackingMode {
kTrackRegsBranches,
kTrackCompilerInterestPoints,
kTrackRegsAll,
};
// A class used by the verifier to tell users about what options need to be set for given methods.
class VerifierCallback {
public:
virtual ~VerifierCallback() {}
virtual void SetDontCompile(ArtMethod* method, bool value)
REQUIRES_SHARED(Locks::mutator_lock_) = 0;
virtual void SetMustCountLocks(ArtMethod* method, bool value)
REQUIRES_SHARED(Locks::mutator_lock_) = 0;
};
// A mapping from a dex pc to the register line statuses as they are immediately prior to the
// execution of that instruction.
class PcToRegisterLineTable {
public:
explicit PcToRegisterLineTable(ScopedArenaAllocator& allocator);
~PcToRegisterLineTable();
// Initialize the RegisterTable. Every instruction address can have a different set of information
// about what's in which register, but for verification purposes we only need to store it at
// branch target addresses (because we merge into that).
void Init(RegisterTrackingMode mode,
InstructionFlags* flags,
uint32_t insns_size,
uint16_t registers_size,
ScopedArenaAllocator& allocator,
RegTypeCache* reg_types);
bool IsInitialized() const {
return !register_lines_.empty();
}
RegisterLine* GetLine(size_t idx) const {
return register_lines_[idx].get();
}
private:
ScopedArenaVector<RegisterLineArenaUniquePtr> register_lines_;
DISALLOW_COPY_AND_ASSIGN(PcToRegisterLineTable);
};
// The verifier
class MethodVerifier {
public:
static MethodVerifier* VerifyMethodAndDump(Thread* self,
VariableIndentationOutputStream* vios,
uint32_t method_idx,
const DexFile* dex_file,
Handle<mirror::DexCache> dex_cache,
Handle<mirror::ClassLoader> class_loader,
const dex::ClassDef& class_def,
const dex::CodeItem* code_item, ArtMethod* method,
uint32_t method_access_flags,
uint32_t api_level)
REQUIRES_SHARED(Locks::mutator_lock_);
// Calculates the verification information for every instruction of the given method. The given
// dex-cache and class-loader will be used for lookups. No classes will be loaded. If verification
// fails hard nullptr will be returned. This should only be used if one needs to examine what the
// verifier believes about the registers of a given method.
static MethodVerifier* CalculateVerificationInfo(Thread* self,
ArtMethod* method,
Handle<mirror::DexCache> dex_cache,
Handle<mirror::ClassLoader> class_loader)
REQUIRES_SHARED(Locks::mutator_lock_);
const DexFile& GetDexFile() const {
DCHECK(dex_file_ != nullptr);
return *dex_file_;
}
RegTypeCache* GetRegTypeCache() {
return ®_types_;
}
// Log a verification failure.
std::ostream& Fail(VerifyError error, bool pending_exc = true);
// Log for verification information.
ScopedNewLine LogVerifyInfo();
// Information structure for a lock held at a certain point in time.
struct DexLockInfo {
// The registers aliasing the lock.
std::set<uint32_t> dex_registers;
// The dex PC of the monitor-enter instruction.
uint32_t dex_pc;
explicit DexLockInfo(uint32_t dex_pc_in) {
dex_pc = dex_pc_in;
}
};
// Fills 'monitor_enter_dex_pcs' with the dex pcs of the monitor-enter instructions corresponding
// to the locks held at 'dex_pc' in method 'm'.
// Note: this is the only situation where the verifier will visit quickened instructions.
static void FindLocksAtDexPc(ArtMethod* m,
uint32_t dex_pc,
std::vector<DexLockInfo>* monitor_enter_dex_pcs,
uint32_t api_level)
REQUIRES_SHARED(Locks::mutator_lock_);
static void Init(ClassLinker* class_linker) REQUIRES_SHARED(Locks::mutator_lock_);
static void Shutdown();
virtual ~MethodVerifier();
static void VisitStaticRoots(RootVisitor* visitor)
REQUIRES_SHARED(Locks::mutator_lock_);
void VisitRoots(RootVisitor* visitor, const RootInfo& roots)
REQUIRES_SHARED(Locks::mutator_lock_);
// Accessors used by the compiler via CompilerCallback
const CodeItemDataAccessor& CodeItem() const {
return code_item_accessor_;
}
RegisterLine* GetRegLine(uint32_t dex_pc);
ALWAYS_INLINE const InstructionFlags& GetInstructionFlags(size_t index) const;
MethodReference GetMethodReference() const;
bool HasCheckCasts() const;
bool HasFailures() const;
bool HasInstructionThatWillThrow() const {
return flags_.have_any_pending_runtime_throw_failure_;
}
virtual const RegType& ResolveCheckedClass(dex::TypeIndex class_idx)
REQUIRES_SHARED(Locks::mutator_lock_) = 0;
uint32_t GetEncounteredFailureTypes() {
return encountered_failure_types_;
}
ClassLinker* GetClassLinker() {
return class_linker_;
}
bool IsAotMode() const {
return flags_.aot_mode_;
}
protected:
MethodVerifier(Thread* self,
ClassLinker* class_linker,
ArenaPool* arena_pool,
const DexFile* dex_file,
const dex::CodeItem* code_item,
uint32_t dex_method_idx,
bool can_load_classes,
bool allow_thread_suspension,
bool allow_soft_failures,
bool aot_mode)
REQUIRES_SHARED(Locks::mutator_lock_);
// Verification result for method(s). Includes a (maximum) failure kind, and (the union of)
// all failure types.
struct FailureData : ValueObject {
FailureKind kind = FailureKind::kNoFailure;
uint32_t types = 0U;
// Merge src into this. Uses the most severe failure kind, and the union of types.
void Merge(const FailureData& src);
};
/*
* Perform verification on a single method.
*
* We do this in three passes:
* (1) Walk through all code units, determining instruction locations,
* widths, and other characteristics.
* (2) Walk through all code units, performing static checks on
* operands.
* (3) Iterate through the method, checking type safety and looking
* for code flow problems.
*/
static FailureData VerifyMethod(Thread* self,
ClassLinker* class_linker,
ArenaPool* arena_pool,
uint32_t method_idx,
const DexFile* dex_file,
Handle<mirror::DexCache> dex_cache,
Handle<mirror::ClassLoader> class_loader,
const dex::ClassDef& class_def_idx,
const dex::CodeItem* code_item,
ArtMethod* method,
uint32_t method_access_flags,
CompilerCallbacks* callbacks,
VerifierCallback* verifier_callback,
bool allow_soft_failures,
HardFailLogMode log_level,
bool need_precise_constants,
uint32_t api_level,
bool aot_mode,
std::string* hard_failure_msg)
REQUIRES_SHARED(Locks::mutator_lock_);
template <bool kVerifierDebug>
static FailureData VerifyMethod(Thread* self,
ClassLinker* class_linker,
ArenaPool* arena_pool,
uint32_t method_idx,
const DexFile* dex_file,
Handle<mirror::DexCache> dex_cache,
Handle<mirror::ClassLoader> class_loader,
const dex::ClassDef& class_def_idx,
const dex::CodeItem* code_item,
ArtMethod* method,
uint32_t method_access_flags,
CompilerCallbacks* callbacks,
VerifierCallback* verifier_callback,
bool allow_soft_failures,
HardFailLogMode log_level,
bool need_precise_constants,
uint32_t api_level,
bool aot_mode,
std::string* hard_failure_msg)
REQUIRES_SHARED(Locks::mutator_lock_);
// For VerifierDepsTest. TODO: Refactor.
// Run verification on the method. Returns true if verification completes and false if the input
// has an irrecoverable corruption.
virtual bool Verify() REQUIRES_SHARED(Locks::mutator_lock_) = 0;
static MethodVerifier* CreateVerifier(Thread* self,
const DexFile* dex_file,
Handle<mirror::DexCache> dex_cache,
Handle<mirror::ClassLoader> class_loader,
const dex::ClassDef& class_def,
const dex::CodeItem* code_item,
uint32_t method_idx,
ArtMethod* method,
uint32_t access_flags,
bool can_load_classes,
bool allow_soft_failures,
bool need_precise_constants,
bool verify_to_dump,
bool allow_thread_suspension,
uint32_t api_level)
REQUIRES_SHARED(Locks::mutator_lock_);
// The thread we're verifying on.
Thread* const self_;
// Arena allocator.
ArenaStack arena_stack_;
ScopedArenaAllocator allocator_;
RegTypeCache reg_types_;
PcToRegisterLineTable reg_table_;
// Storage for the register status we're currently working on.
RegisterLineArenaUniquePtr work_line_;
// The address of the instruction we're currently working on, note that this is in 2 byte
// quantities
uint32_t work_insn_idx_;
// Storage for the register status we're saving for later.
RegisterLineArenaUniquePtr saved_line_;
const uint32_t dex_method_idx_; // The method we're working on.
const DexFile* const dex_file_; // The dex file containing the method.
const CodeItemDataAccessor code_item_accessor_;
// Instruction widths and flags, one entry per code unit.
// Owned, but not unique_ptr since insn_flags_ are allocated in arenas.
ArenaUniquePtr<InstructionFlags[]> insn_flags_;
// The types of any error that occurs.
std::vector<VerifyError> failures_;
// Error messages associated with failures.
std::vector<std::ostringstream*> failure_messages_;
struct {
// Is there a pending hard failure?
bool have_pending_hard_failure_ : 1;
// Is there a pending runtime throw failure? A runtime throw failure is when an instruction
// would fail at runtime throwing an exception. Such an instruction causes the following code
// to be unreachable. This is set by Fail and used to ensure we don't process unreachable
// instructions that would hard fail the verification.
// Note: this flag is reset after processing each instruction.
bool have_pending_runtime_throw_failure_ : 1;
// Is there a pending experimental failure?
bool have_pending_experimental_failure_ : 1;
// A version of the above that is not reset and thus captures if there were *any* throw
// failures.
bool have_any_pending_runtime_throw_failure_ : 1;
// Verify in AoT mode?
bool aot_mode_ : 1;
} flags_;
// Info message log use primarily for verifier diagnostics.
std::ostringstream info_messages_;
// Bitset of the encountered failure types. Bits are according to the values in VerifyError.
uint32_t encountered_failure_types_;
const bool can_load_classes_;
// Converts soft failures to hard failures when false. Only false when the compiler isn't
// running and the verifier is called from the class linker.
const bool allow_soft_failures_;
// Indicates the method being verified contains at least one check-cast or aput-object
// instruction. Aput-object operations implicitly check for array-store exceptions, similar to
// check-cast.
bool has_check_casts_;
// Classlinker to use when resolving.
ClassLinker* class_linker_;
// Link, for the method verifier root linked list.
MethodVerifier* link_;
friend class art::Thread;
friend class ClassVerifier;
friend class VerifierDepsTest;
DISALLOW_COPY_AND_ASSIGN(MethodVerifier);
};
} // namespace verifier
} // namespace art
#endif // ART_RUNTIME_VERIFIER_METHOD_VERIFIER_H_
|