0N/A/*
3955N/A * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#ifndef SHARE_VM_CLASSFILE_VERIFIER_HPP
1879N/A#define SHARE_VM_CLASSFILE_VERIFIER_HPP
1879N/A
1879N/A#include "classfile/verificationType.hpp"
1879N/A#include "memory/gcLocker.hpp"
1879N/A#include "oops/klass.hpp"
1879N/A#include "oops/methodOop.hpp"
1879N/A#include "runtime/handles.hpp"
1879N/A#include "utilities/exceptions.hpp"
1879N/A
0N/A// The verifier class
0N/Aclass Verifier : AllStatic {
0N/A public:
1522N/A enum {
1522N/A STACKMAP_ATTRIBUTE_MAJOR_VERSION = 50,
1522N/A INVOKEDYNAMIC_MAJOR_VERSION = 51
1522N/A };
0N/A typedef enum { ThrowException, NoException } Mode;
0N/A
0N/A /**
0N/A * Verify the bytecodes for a class. If 'throw_exception' is true
0N/A * then the appropriate VerifyError or ClassFormatError will be thrown.
0N/A * Otherwise, no exception is thrown and the return indicates the
0N/A * error.
0N/A */
973N/A static bool verify(instanceKlassHandle klass, Mode mode, bool should_verify_class, TRAPS);
0N/A
973N/A // Return false if the class is loaded by the bootstrap loader,
973N/A // or if defineClass was called requesting skipping verification
973N/A // -Xverify:all/none override this value
973N/A static bool should_verify_for(oop class_loader, bool should_verify_class);
0N/A
0N/A // Relax certain verifier checks to enable some broken 1.1 apps to run on 1.2.
0N/A static bool relax_verify_for(oop class_loader);
0N/A
0N/A private:
973N/A static bool is_eligible_for_verification(instanceKlassHandle klass, bool should_verify_class);
2062N/A static Symbol* inference_verify(
0N/A instanceKlassHandle klass, char* msg, size_t msg_len, TRAPS);
0N/A};
0N/A
0N/Aclass RawBytecodeStream;
0N/Aclass StackMapFrame;
0N/Aclass StackMapTable;
0N/A
0N/A// Summary of verifier's memory usage:
0N/A// StackMapTable is stack allocated.
2062N/A// StackMapFrame are resource allocated. There is only one ResourceMark
2062N/A// for each class verification, which is created at the top level.
0N/A// There is one mutable StackMapFrame (current_frame) which is updated
0N/A// by abstract bytecode interpretation. frame_in_exception_handler() returns
0N/A// a frame that has a mutable one-item stack (ready for pushing the
0N/A// catch type exception object). All the other StackMapFrame's
0N/A// are immutable (including their locals and stack arrays) after
0N/A// their constructions.
0N/A// locals/stack arrays in StackMapFrame are resource allocated.
0N/A// locals/stack arrays can be shared between StackMapFrame's, except
0N/A// the mutable StackMapFrame (current_frame).
0N/A
0N/A// These macros are used similarly to CHECK macros but also check
0N/A// the status of the verifier and return if that has an error.
0N/A#define CHECK_VERIFY(verifier) \
0N/A CHECK); if ((verifier)->has_error()) return; (0
0N/A#define CHECK_VERIFY_(verifier, result) \
0N/A CHECK_(result)); if ((verifier)->has_error()) return (result); (0
0N/A
3955N/Aclass TypeOrigin VALUE_OBJ_CLASS_SPEC {
3955N/A private:
3955N/A typedef enum {
3955N/A CF_LOCALS, // Comes from the current frame locals
3955N/A CF_STACK, // Comes from the current frame expression stack
3955N/A SM_LOCALS, // Comes from stackmap locals
3955N/A SM_STACK, // Comes from stackmap expression stack
3955N/A CONST_POOL, // Comes from the constant pool
3955N/A SIG, // Comes from method signature
3955N/A IMPLICIT, // Comes implicitly from code or context
3955N/A BAD_INDEX, // No type, but the index is bad
3955N/A FRAME_ONLY, // No type, context just contains the frame
3955N/A NONE
3955N/A } Origin;
3955N/A
3955N/A Origin _origin;
3955N/A u2 _index; // local, stack, or constant pool index
3955N/A StackMapFrame* _frame; // source frame if CF or SM
3955N/A VerificationType _type; // The actual type
3955N/A
3955N/A TypeOrigin(
3955N/A Origin origin, u2 index, StackMapFrame* frame, VerificationType type)
3955N/A : _origin(origin), _index(index), _frame(frame), _type(type) {}
3955N/A
3955N/A public:
3955N/A TypeOrigin() : _origin(NONE), _index(0), _frame(NULL) {}
3955N/A
3955N/A static TypeOrigin null();
3955N/A static TypeOrigin local(u2 index, StackMapFrame* frame);
3955N/A static TypeOrigin stack(u2 index, StackMapFrame* frame);
3955N/A static TypeOrigin sm_local(u2 index, StackMapFrame* frame);
3955N/A static TypeOrigin sm_stack(u2 index, StackMapFrame* frame);
3955N/A static TypeOrigin cp(u2 index, VerificationType vt);
3955N/A static TypeOrigin signature(VerificationType vt);
3955N/A static TypeOrigin bad_index(u2 index);
3955N/A static TypeOrigin implicit(VerificationType t);
3955N/A static TypeOrigin frame(StackMapFrame* frame);
3955N/A
3955N/A void reset_frame();
3955N/A void details(outputStream* ss) const;
3955N/A void print_frame(outputStream* ss) const;
3955N/A const StackMapFrame* frame() const { return _frame; }
3955N/A bool is_valid() const { return _origin != NONE; }
3955N/A u2 index() const { return _index; }
3955N/A
3955N/A#ifdef ASSERT
3955N/A void print_on(outputStream* str) const;
3955N/A#endif
3955N/A};
3955N/A
3955N/Aclass ErrorContext VALUE_OBJ_CLASS_SPEC {
3955N/A private:
3955N/A typedef enum {
3955N/A INVALID_BYTECODE, // There was a problem with the bytecode
3955N/A WRONG_TYPE, // Type value was not as expected
3955N/A FLAGS_MISMATCH, // Frame flags are not assignable
3955N/A BAD_CP_INDEX, // Invalid constant pool index
3955N/A BAD_LOCAL_INDEX, // Invalid local index
3955N/A LOCALS_SIZE_MISMATCH, // Frames have differing local counts
3955N/A STACK_SIZE_MISMATCH, // Frames have different stack sizes
3955N/A STACK_OVERFLOW, // Attempt to push onto a full expression stack
3955N/A STACK_UNDERFLOW, // Attempt to pop and empty expression stack
3955N/A MISSING_STACKMAP, // No stackmap for this location and there should be
3955N/A BAD_STACKMAP, // Format error in stackmap
3955N/A NO_FAULT, // No error
3955N/A UNKNOWN
3955N/A } FaultType;
3955N/A
3955N/A int _bci;
3955N/A FaultType _fault;
3955N/A TypeOrigin _type;
3955N/A TypeOrigin _expected;
3955N/A
3955N/A ErrorContext(int bci, FaultType fault) :
3955N/A _bci(bci), _fault(fault) {}
3955N/A ErrorContext(int bci, FaultType fault, TypeOrigin type) :
3955N/A _bci(bci), _fault(fault), _type(type) {}
3955N/A ErrorContext(int bci, FaultType fault, TypeOrigin type, TypeOrigin exp) :
3955N/A _bci(bci), _fault(fault), _type(type), _expected(exp) {}
3955N/A
3955N/A public:
3955N/A ErrorContext() : _bci(-1), _fault(NO_FAULT) {}
3955N/A
3955N/A static ErrorContext bad_code(u2 bci) {
3955N/A return ErrorContext(bci, INVALID_BYTECODE);
3955N/A }
3955N/A static ErrorContext bad_type(u2 bci, TypeOrigin type) {
3955N/A return ErrorContext(bci, WRONG_TYPE, type);
3955N/A }
3955N/A static ErrorContext bad_type(u2 bci, TypeOrigin type, TypeOrigin exp) {
3955N/A return ErrorContext(bci, WRONG_TYPE, type, exp);
3955N/A }
3955N/A static ErrorContext bad_flags(u2 bci, StackMapFrame* frame) {
3955N/A return ErrorContext(bci, FLAGS_MISMATCH, TypeOrigin::frame(frame));
3955N/A }
3955N/A static ErrorContext bad_flags(u2 bci, StackMapFrame* cur, StackMapFrame* sm) {
3955N/A return ErrorContext(bci, FLAGS_MISMATCH,
3955N/A TypeOrigin::frame(cur), TypeOrigin::frame(sm));
3955N/A }
3955N/A static ErrorContext bad_cp_index(u2 bci, u2 index) {
3955N/A return ErrorContext(bci, BAD_CP_INDEX, TypeOrigin::bad_index(index));
3955N/A }
3955N/A static ErrorContext bad_local_index(u2 bci, u2 index) {
3955N/A return ErrorContext(bci, BAD_LOCAL_INDEX, TypeOrigin::bad_index(index));
3955N/A }
3955N/A static ErrorContext locals_size_mismatch(
3955N/A u2 bci, StackMapFrame* frame0, StackMapFrame* frame1) {
3955N/A return ErrorContext(bci, LOCALS_SIZE_MISMATCH,
3955N/A TypeOrigin::frame(frame0), TypeOrigin::frame(frame1));
3955N/A }
3955N/A static ErrorContext stack_size_mismatch(
3955N/A u2 bci, StackMapFrame* frame0, StackMapFrame* frame1) {
3955N/A return ErrorContext(bci, STACK_SIZE_MISMATCH,
3955N/A TypeOrigin::frame(frame0), TypeOrigin::frame(frame1));
3955N/A }
3955N/A static ErrorContext stack_overflow(u2 bci, StackMapFrame* frame) {
3955N/A return ErrorContext(bci, STACK_OVERFLOW, TypeOrigin::frame(frame));
3955N/A }
3955N/A static ErrorContext stack_underflow(u2 bci, StackMapFrame* frame) {
3955N/A return ErrorContext(bci, STACK_UNDERFLOW, TypeOrigin::frame(frame));
3955N/A }
3955N/A static ErrorContext missing_stackmap(u2 bci) {
3955N/A return ErrorContext(bci, MISSING_STACKMAP);
3955N/A }
3955N/A static ErrorContext bad_stackmap(int index, StackMapFrame* frame) {
3955N/A return ErrorContext(0, BAD_STACKMAP, TypeOrigin::frame(frame));
3955N/A }
3955N/A
3955N/A bool is_valid() const { return _fault != NO_FAULT; }
3955N/A int bci() const { return _bci; }
3955N/A
3955N/A void reset_frames() {
3955N/A _type.reset_frame();
3955N/A _expected.reset_frame();
3955N/A }
3955N/A
3955N/A void details(outputStream* ss, methodOop method) const;
3955N/A
3955N/A#ifdef ASSERT
3955N/A void print_on(outputStream* str) const {
3955N/A str->print("error_context(%d, %d,", _bci, _fault);
3955N/A _type.print_on(str);
3955N/A str->print(",");
3955N/A _expected.print_on(str);
3955N/A str->print(")");
3955N/A }
3955N/A#endif
3955N/A
3955N/A private:
3955N/A void location_details(outputStream* ss, methodOop method) const;
3955N/A void reason_details(outputStream* ss) const;
3955N/A void frame_details(outputStream* ss) const;
3955N/A void bytecode_details(outputStream* ss, methodOop method) const;
3955N/A void handler_details(outputStream* ss, methodOop method) const;
3955N/A void stackmap_details(outputStream* ss, methodOop method) const;
3955N/A};
3955N/A
0N/A// A new instance of this class is created for each class being verified
0N/Aclass ClassVerifier : public StackObj {
0N/A private:
0N/A Thread* _thread;
3955N/A GrowableArray<Symbol*>* _symbols; // keep a list of symbols created
3955N/A
2062N/A Symbol* _exception_type;
0N/A char* _message;
3955N/A
3955N/A ErrorContext _error_context; // contains information about an error
0N/A
0N/A void verify_method(methodHandle method, TRAPS);
0N/A char* generate_code_data(methodHandle m, u4 code_length, TRAPS);
3955N/A void verify_exception_handler_table(u4 code_length, char* code_data,
3955N/A int& min, int& max, TRAPS);
0N/A void verify_local_variable_table(u4 code_length, char* code_data, TRAPS);
0N/A
0N/A VerificationType cp_ref_index_to_type(
0N/A int index, constantPoolHandle cp, TRAPS) {
0N/A return cp_index_to_type(cp->klass_ref_index_at(index), cp, THREAD);
0N/A }
0N/A
0N/A bool is_protected_access(
0N/A instanceKlassHandle this_class, klassOop target_class,
2062N/A Symbol* field_name, Symbol* field_sig, bool is_method);
0N/A
3955N/A void verify_cp_index(u2 bci, constantPoolHandle cp, int index, TRAPS);
3955N/A void verify_cp_type(u2 bci, int index, constantPoolHandle cp,
3955N/A unsigned int types, TRAPS);
3955N/A void verify_cp_class_type(u2 bci, int index, constantPoolHandle cp, TRAPS);
0N/A
0N/A u2 verify_stackmap_table(
0N/A u2 stackmap_index, u2 bci, StackMapFrame* current_frame,
0N/A StackMapTable* stackmap_table, bool no_control_flow, TRAPS);
0N/A
0N/A void verify_exception_handler_targets(
0N/A u2 bci, bool this_uninit, StackMapFrame* current_frame,
0N/A StackMapTable* stackmap_table, TRAPS);
0N/A
0N/A void verify_ldc(
0N/A int opcode, u2 index, StackMapFrame *current_frame,
0N/A constantPoolHandle cp, u2 bci, TRAPS);
0N/A
0N/A void verify_switch(
0N/A RawBytecodeStream* bcs, u4 code_length, char* code_data,
0N/A StackMapFrame* current_frame, StackMapTable* stackmap_table, TRAPS);
0N/A
0N/A void verify_field_instructions(
0N/A RawBytecodeStream* bcs, StackMapFrame* current_frame,
0N/A constantPoolHandle cp, TRAPS);
0N/A
0N/A void verify_invoke_init(
3955N/A RawBytecodeStream* bcs, u2 ref_index, VerificationType ref_class_type,
0N/A StackMapFrame* current_frame, u4 code_length, bool* this_uninit,
0N/A constantPoolHandle cp, TRAPS);
0N/A
0N/A void verify_invoke_instructions(
0N/A RawBytecodeStream* bcs, u4 code_length, StackMapFrame* current_frame,
0N/A bool* this_uninit, VerificationType return_type,
0N/A constantPoolHandle cp, TRAPS);
0N/A
0N/A VerificationType get_newarray_type(u2 index, u2 bci, TRAPS);
3955N/A void verify_anewarray(u2 bci, u2 index, constantPoolHandle cp,
3955N/A StackMapFrame* current_frame, TRAPS);
0N/A void verify_return_value(
3955N/A VerificationType return_type, VerificationType type, u2 offset,
3955N/A StackMapFrame* current_frame, TRAPS);
0N/A
0N/A void verify_iload (u2 index, StackMapFrame* current_frame, TRAPS);
0N/A void verify_lload (u2 index, StackMapFrame* current_frame, TRAPS);
0N/A void verify_fload (u2 index, StackMapFrame* current_frame, TRAPS);
0N/A void verify_dload (u2 index, StackMapFrame* current_frame, TRAPS);
0N/A void verify_aload (u2 index, StackMapFrame* current_frame, TRAPS);
0N/A void verify_istore(u2 index, StackMapFrame* current_frame, TRAPS);
0N/A void verify_lstore(u2 index, StackMapFrame* current_frame, TRAPS);
0N/A void verify_fstore(u2 index, StackMapFrame* current_frame, TRAPS);
0N/A void verify_dstore(u2 index, StackMapFrame* current_frame, TRAPS);
0N/A void verify_astore(u2 index, StackMapFrame* current_frame, TRAPS);
0N/A void verify_iinc (u2 index, StackMapFrame* current_frame, TRAPS);
0N/A
2062N/A bool name_in_supers(Symbol* ref_name, instanceKlassHandle current);
0N/A
1862N/A VerificationType object_type() const;
1862N/A
0N/A instanceKlassHandle _klass; // the class being verified
0N/A methodHandle _method; // current method being verified
0N/A VerificationType _this_type; // the verification type of the current class
0N/A
1490N/A // Some recursive calls from the verifier to the name resolver
1490N/A // can cause the current class to be re-verified and rewritten.
1490N/A // If this happens, the original verification should not continue,
1490N/A // because constant pool indexes will have changed.
1490N/A // The rewriter is preceded by the verifier. If the verifier throws
1490N/A // an error, rewriting is prevented. Also, rewriting always precedes
1490N/A // bytecode execution or compilation. Thus, is_rewritten implies
1490N/A // that a class has been verified and prepared for execution.
1490N/A bool was_recursively_verified() { return _klass->is_rewritten(); }
1490N/A
0N/A public:
0N/A enum {
0N/A BYTECODE_OFFSET = 1,
0N/A NEW_OFFSET = 2
0N/A };
0N/A
0N/A // constructor
3955N/A ClassVerifier(instanceKlassHandle klass, TRAPS);
0N/A
0N/A // destructor
0N/A ~ClassVerifier();
0N/A
0N/A Thread* thread() { return _thread; }
0N/A methodHandle method() { return _method; }
0N/A instanceKlassHandle current_class() const { return _klass; }
0N/A VerificationType current_type() const { return _this_type; }
0N/A
0N/A // Verifies the class. If a verify or class file format error occurs,
0N/A // the '_exception_name' symbols will set to the exception name and
0N/A // the message_buffer will be filled in with the exception message.
0N/A void verify_class(TRAPS);
0N/A
0N/A // Return status modes
2062N/A Symbol* result() const { return _exception_type; }
2062N/A bool has_error() const { return result() != NULL; }
3955N/A char* exception_message() {
3955N/A stringStream ss;
3955N/A ss.print(_message);
3955N/A _error_context.details(&ss, _method());
3955N/A return ss.as_string();
3955N/A }
0N/A
0N/A // Called when verify or class format errors are encountered.
0N/A // May throw an exception based upon the mode.
3955N/A void verify_error(ErrorContext ctx, const char* fmt, ...);
0N/A void class_format_error(const char* fmt, ...);
0N/A
2062N/A klassOop load_class(Symbol* name, TRAPS);
0N/A
0N/A int change_sig_to_verificationType(
0N/A SignatureStream* sig_type, VerificationType* inference_type, TRAPS);
0N/A
0N/A VerificationType cp_index_to_type(int index, constantPoolHandle cp, TRAPS) {
2062N/A return VerificationType::reference_type(cp->klass_name_at(index));
0N/A }
0N/A
2062N/A // Keep a list of temporary symbols created during verification because
2062N/A // their reference counts need to be decrememented when the verifier object
2062N/A // goes out of scope. Since these symbols escape the scope in which they're
2062N/A // created, we can't use a TempNewSymbol.
3955N/A Symbol* create_temporary_symbol(
3955N/A const Symbol* s, int begin, int end, TRAPS);
2062N/A Symbol* create_temporary_symbol(const char *s, int length, TRAPS);
2062N/A
3955N/A TypeOrigin ref_ctx(const char* str, TRAPS);
0N/A};
0N/A
0N/Ainline int ClassVerifier::change_sig_to_verificationType(
0N/A SignatureStream* sig_type, VerificationType* inference_type, TRAPS) {
0N/A BasicType bt = sig_type->type();
0N/A switch (bt) {
0N/A case T_OBJECT:
0N/A case T_ARRAY:
0N/A {
2062N/A Symbol* name = sig_type->as_symbol(CHECK_0);
2062N/A // Create another symbol to save as signature stream unreferences
2062N/A // this symbol.
2062N/A Symbol* name_copy =
2062N/A create_temporary_symbol(name, 0, name->utf8_length(), CHECK_0);
2062N/A assert(name_copy == name, "symbols don't match");
0N/A *inference_type =
2062N/A VerificationType::reference_type(name_copy);
0N/A return 1;
0N/A }
0N/A case T_LONG:
0N/A *inference_type = VerificationType::long_type();
0N/A *++inference_type = VerificationType::long2_type();
0N/A return 2;
0N/A case T_DOUBLE:
0N/A *inference_type = VerificationType::double_type();
0N/A *++inference_type = VerificationType::double2_type();
0N/A return 2;
0N/A case T_INT:
0N/A case T_BOOLEAN:
0N/A case T_BYTE:
0N/A case T_CHAR:
0N/A case T_SHORT:
0N/A *inference_type = VerificationType::integer_type();
0N/A return 1;
0N/A case T_FLOAT:
0N/A *inference_type = VerificationType::float_type();
0N/A return 1;
0N/A default:
0N/A ShouldNotReachHere();
0N/A return 1;
0N/A }
0N/A}
1879N/A
1879N/A#endif // SHARE_VM_CLASSFILE_VERIFIER_HPP