verifier.hpp revision 0
0N/A/*
0N/A * Copyright 1998-2006 Sun Microsystems, Inc. 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 *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A *
0N/A */
0N/A
0N/A// The verifier class
0N/Aclass Verifier : AllStatic {
0N/A public:
0N/A enum { STACKMAP_ATTRIBUTE_MAJOR_VERSION = 50 };
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 */
0N/A static bool verify(instanceKlassHandle klass, Mode mode, TRAPS);
0N/A
0N/A // Return false if the class is loaded by the bootstrap loader.
0N/A static bool should_verify_for(oop class_loader);
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:
0N/A static bool is_eligible_for_verification(instanceKlassHandle klass);
0N/A static symbolHandle 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.
0N/A// StackMapFrame are resource allocated. There is one ResourceMark
0N/A// for each method.
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// Care needs to be taken to make sure resource objects don't outlive
0N/A// the lifetime of their ResourceMark.
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
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;
0N/A symbolHandle _exception_type;
0N/A char* _message;
0N/A size_t _message_buffer_len;
0N/A
0N/A void verify_method(methodHandle method, TRAPS);
0N/A char* generate_code_data(methodHandle m, u4 code_length, TRAPS);
0N/A void verify_exception_handler_table(u4 code_length, char* code_data, 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,
0N/A symbolOop field_name, symbolOop field_sig, bool is_method);
0N/A
0N/A void verify_cp_index(constantPoolHandle cp, int index, TRAPS);
0N/A void verify_cp_type(
0N/A int index, constantPoolHandle cp, unsigned int types, TRAPS);
0N/A void verify_cp_class_type(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(
0N/A RawBytecodeStream* bcs, 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);
0N/A void verify_anewarray(
0N/A u2 index, constantPoolHandle cp, StackMapFrame* current_frame, TRAPS);
0N/A void verify_return_value(
0N/A VerificationType return_type, VerificationType type, u2 offset, 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
0N/A bool name_in_supers(symbolOop ref_name, instanceKlassHandle current);
0N/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
0N/A public:
0N/A enum {
0N/A BYTECODE_OFFSET = 1,
0N/A NEW_OFFSET = 2
0N/A };
0N/A
0N/A // constructor
0N/A ClassVerifier(instanceKlassHandle klass, char* msg, size_t msg_len, 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
0N/A symbolHandle result() const { return _exception_type; }
0N/A bool has_error() const { return !(result().is_null()); }
0N/A
0N/A // Called when verify or class format errors are encountered.
0N/A // May throw an exception based upon the mode.
0N/A void verify_error(u2 offset, const char* fmt, ...);
0N/A void verify_error(const char* fmt, ...);
0N/A void class_format_error(const char* fmt, ...);
0N/A void format_error_message(const char* fmt, int offset, va_list args);
0N/A
0N/A klassOop load_class(symbolHandle 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) {
0N/A return VerificationType::reference_type(
0N/A symbolHandle(THREAD, cp->klass_name_at(index)));
0N/A }
0N/A
0N/A static bool _verify_verbose; // for debugging
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 {
0N/A symbolOop name = sig_type->as_symbol(CHECK_0);
0N/A *inference_type =
0N/A VerificationType::reference_type(symbolHandle(THREAD, name));
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}