check_code.c revision 502
0N/A/*
502N/A * Copyright 1994-2008 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. Sun designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Sun in the LICENSE file that accompanied this code.
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 * Verify that the code within a method block doesn't exploit any
0N/A * security holes.
0N/A */
0N/A/*
0N/A Exported function:
0N/A
0N/A jboolean
0N/A VerifyClass(JNIEnv *env, jclass cb, char *message_buffer,
0N/A jint buffer_length)
0N/A jboolean
0N/A VerifyClassForMajorVersion(JNIEnv *env, jclass cb, char *message_buffer,
0N/A jint buffer_length, jint major_version)
0N/A
0N/A This file now only uses the standard JNI and the following VM functions
0N/A exported in jvm.h:
0N/A
0N/A JVM_FindClassFromClass
0N/A JVM_IsInterface
0N/A JVM_GetClassNameUTF
0N/A JVM_GetClassCPEntriesCount
0N/A JVM_GetClassCPTypes
0N/A JVM_GetClassFieldsCount
0N/A JVM_GetClassMethodsCount
0N/A
0N/A JVM_GetFieldIxModifiers
0N/A
0N/A JVM_GetMethodIxModifiers
0N/A JVM_GetMethodIxExceptionTableLength
0N/A JVM_GetMethodIxLocalsCount
0N/A JVM_GetMethodIxArgsSize
0N/A JVM_GetMethodIxMaxStack
0N/A JVM_GetMethodIxNameUTF
0N/A JVM_GetMethodIxSignatureUTF
0N/A JVM_GetMethodIxExceptionsCount
0N/A JVM_GetMethodIxExceptionIndexes
0N/A JVM_GetMethodIxByteCodeLength
0N/A JVM_GetMethodIxByteCode
0N/A JVM_GetMethodIxExceptionTableEntry
0N/A JVM_IsConstructorIx
0N/A
0N/A JVM_GetCPClassNameUTF
0N/A JVM_GetCPFieldNameUTF
0N/A JVM_GetCPMethodNameUTF
0N/A JVM_GetCPFieldSignatureUTF
0N/A JVM_GetCPMethodSignatureUTF
0N/A JVM_GetCPFieldClassNameUTF
0N/A JVM_GetCPMethodClassNameUTF
0N/A JVM_GetCPFieldModifiers
0N/A JVM_GetCPMethodModifiers
0N/A
0N/A JVM_ReleaseUTF
0N/A JVM_IsSameClassPackage
0N/A
0N/A */
0N/A
0N/A#include <string.h>
0N/A#include <setjmp.h>
0N/A#include <assert.h>
0N/A#include <limits.h>
0N/A#include <stdlib.h>
0N/A
0N/A#include "jni.h"
0N/A#include "jvm.h"
502N/A#include "classfile_constants.h"
0N/A#include "opcodes.in_out"
0N/A
0N/A#define MAX_ARRAY_DIMENSIONS 255
0N/A/* align byte code */
0N/A#ifndef ALIGN_UP
0N/A#define ALIGN_UP(n,align_grain) (((n) + ((align_grain) - 1)) & ~((align_grain)-1))
0N/A#endif /* ALIGN_UP */
0N/A#define UCALIGN(n) ((unsigned char *)ALIGN_UP((uintptr_t)(n),sizeof(int)))
0N/A
0N/A#ifdef DEBUG
0N/A
0N/Aint verify_verbose = 0;
0N/Astatic struct context_type *GlobalContext;
0N/A#endif
0N/A
0N/Aenum {
0N/A ITEM_Bogus,
0N/A ITEM_Void, /* only as a function return value */
0N/A ITEM_Integer,
0N/A ITEM_Float,
0N/A ITEM_Double,
0N/A ITEM_Double_2, /* 2nd word of double in register */
0N/A ITEM_Long,
0N/A ITEM_Long_2, /* 2nd word of long in register */
0N/A ITEM_Array,
0N/A ITEM_Object, /* Extra info field gives name. */
0N/A ITEM_NewObject, /* Like object, but uninitialized. */
0N/A ITEM_InitObject, /* "this" is init method, before call
0N/A to super() */
0N/A ITEM_ReturnAddress, /* Extra info gives instr # of start pc */
0N/A /* The following three are only used within array types.
0N/A * Normally, we use ITEM_Integer, instead. */
0N/A ITEM_Byte,
0N/A ITEM_Short,
0N/A ITEM_Char
0N/A};
0N/A
0N/A
0N/A#define UNKNOWN_STACK_SIZE -1
0N/A#define UNKNOWN_REGISTER_COUNT -1
0N/A#define UNKNOWN_RET_INSTRUCTION -1
0N/A
0N/A#undef MAX
0N/A#undef MIN
0N/A#define MAX(a, b) ((a) > (b) ? (a) : (b))
0N/A#define MIN(a, b) ((a) < (b) ? (a) : (b))
0N/A
0N/A#define BITS_PER_INT (CHAR_BIT * sizeof(int)/sizeof(char))
0N/A#define SET_BIT(flags, i) (flags[(i)/BITS_PER_INT] |= \
0N/A ((unsigned)1 << ((i) % BITS_PER_INT)))
0N/A#define IS_BIT_SET(flags, i) (flags[(i)/BITS_PER_INT] & \
0N/A ((unsigned)1 << ((i) % BITS_PER_INT)))
0N/A
0N/Atypedef unsigned int fullinfo_type;
0N/Atypedef unsigned int *bitvector;
0N/A
0N/A#define GET_ITEM_TYPE(thing) ((thing) & 0x1F)
0N/A#define GET_INDIRECTION(thing) (((thing) & 0xFFFF) >> 5)
0N/A#define GET_EXTRA_INFO(thing) ((thing) >> 16)
0N/A#define WITH_ZERO_INDIRECTION(thing) ((thing) & ~(0xFFE0))
0N/A#define WITH_ZERO_EXTRA_INFO(thing) ((thing) & 0xFFFF)
0N/A
0N/A#define MAKE_FULLINFO(type, indirect, extra) \
0N/A ((type) + ((indirect) << 5) + ((extra) << 16))
0N/A
0N/A#define MAKE_Object_ARRAY(indirect) \
0N/A (context->object_info + ((indirect) << 5))
0N/A
0N/A#define NULL_FULLINFO MAKE_FULLINFO(ITEM_Object, 0, 0)
0N/A
502N/A/* JVM_OPC_invokespecial calls to <init> need to be treated special */
502N/A#define JVM_OPC_invokeinit 0x100
0N/A
0N/A/* A hash mechanism used by the verifier.
0N/A * Maps class names to unique 16 bit integers.
0N/A */
0N/A
0N/A#define HASH_TABLE_SIZE 503
0N/A
0N/A/* The buckets are managed as a 256 by 256 matrix. We allocate an entire
0N/A * row (256 buckets) at a time to minimize fragmentation. Rows are
0N/A * allocated on demand so that we don't waste too much space.
0N/A */
0N/A
0N/A#define MAX_HASH_ENTRIES 65536
0N/A#define HASH_ROW_SIZE 256
0N/A
0N/Atypedef struct hash_bucket_type {
0N/A char *name;
0N/A unsigned int hash;
0N/A jclass class;
0N/A unsigned short ID;
0N/A unsigned short next;
0N/A unsigned loadable:1; /* from context->class loader */
0N/A} hash_bucket_type;
0N/A
0N/Atypedef struct {
0N/A hash_bucket_type **buckets;
0N/A unsigned short *table;
0N/A int entries_used;
0N/A} hash_table_type;
0N/A
0N/A#define GET_BUCKET(class_hash, ID)\
0N/A (class_hash->buckets[ID / HASH_ROW_SIZE] + ID % HASH_ROW_SIZE)
0N/A
0N/A/*
0N/A * There are currently two types of resources that we need to keep
0N/A * track of (in addition to the CCalloc pool).
0N/A */
0N/Aenum {
0N/A VM_STRING_UTF, /* VM-allocated UTF strings */
0N/A VM_MALLOC_BLK /* malloc'ed blocks */
0N/A};
0N/A
0N/A#define LDC_CLASS_MAJOR_VERSION 49
0N/A
0N/A#define ALLOC_STACK_SIZE 16 /* big enough */
0N/A
0N/Atypedef struct alloc_stack_type {
0N/A void *ptr;
0N/A int kind;
0N/A struct alloc_stack_type *next;
0N/A} alloc_stack_type;
0N/A
0N/A/* The context type encapsulates the current invocation of the byte
0N/A * code verifier.
0N/A */
0N/Astruct context_type {
0N/A
0N/A JNIEnv *env; /* current JNIEnv */
0N/A
0N/A /* buffers etc. */
0N/A char *message;
0N/A jint message_buf_len;
0N/A jboolean err_code;
0N/A
0N/A alloc_stack_type *allocated_memory; /* all memory blocks that we have not
0N/A had a chance to free */
0N/A /* Store up to ALLOC_STACK_SIZE number of handles to allocated memory
0N/A blocks here, to save mallocs. */
0N/A alloc_stack_type alloc_stack[ALLOC_STACK_SIZE];
0N/A int alloc_stack_top;
0N/A
0N/A /* these fields are per class */
0N/A jclass class; /* current class */
0N/A jint major_version;
0N/A jint nconstants;
0N/A unsigned char *constant_types;
0N/A hash_table_type class_hash;
0N/A
0N/A fullinfo_type object_info; /* fullinfo for java/lang/Object */
0N/A fullinfo_type string_info; /* fullinfo for java/lang/String */
0N/A fullinfo_type throwable_info; /* fullinfo for java/lang/Throwable */
0N/A fullinfo_type cloneable_info; /* fullinfo for java/lang/Cloneable */
0N/A fullinfo_type serializable_info; /* fullinfo for java/io/Serializable */
0N/A
0N/A fullinfo_type currentclass_info; /* fullinfo for context->class */
0N/A fullinfo_type superclass_info; /* fullinfo for superclass */
0N/A
0N/A /* these fields are per method */
0N/A int method_index; /* current method */
0N/A unsigned short *exceptions; /* exceptions */
0N/A unsigned char *code; /* current code object */
0N/A jint code_length;
0N/A int *code_data; /* offset to instruction number */
0N/A struct instruction_data_type *instruction_data; /* info about each */
0N/A struct handler_info_type *handler_info;
0N/A fullinfo_type *superclasses; /* null terminated superclasses */
0N/A int instruction_count; /* number of instructions */
0N/A fullinfo_type return_type; /* function return type */
0N/A fullinfo_type swap_table[4]; /* used for passing information */
0N/A int bitmask_size; /* words needed to hold bitmap of arguments */
0N/A
0N/A /* these fields are per field */
0N/A int field_index;
0N/A
0N/A /* Used by the space allocator */
0N/A struct CCpool *CCroot, *CCcurrent;
0N/A char *CCfree_ptr;
0N/A int CCfree_size;
0N/A
0N/A /* Jump here on any error. */
0N/A jmp_buf jump_buffer;
0N/A
0N/A#ifdef DEBUG
0N/A /* keep track of how many global refs are allocated. */
0N/A int n_globalrefs;
0N/A#endif
0N/A};
0N/A
0N/Astruct stack_info_type {
0N/A struct stack_item_type *stack;
0N/A int stack_size;
0N/A};
0N/A
0N/Astruct register_info_type {
0N/A int register_count; /* number of registers used */
0N/A fullinfo_type *registers;
0N/A int mask_count; /* number of masks in the following */
0N/A struct mask_type *masks;
0N/A};
0N/A
0N/Astruct mask_type {
0N/A int entry;
0N/A int *modifies;
0N/A};
0N/A
0N/Atypedef unsigned short flag_type;
0N/A
0N/Astruct instruction_data_type {
502N/A int opcode; /* may turn into "canonical" opcode */
0N/A unsigned changed:1; /* has it changed */
0N/A unsigned protected:1; /* must accessor be a subclass of "this" */
0N/A union {
0N/A int i; /* operand to the opcode */
0N/A int *ip;
0N/A fullinfo_type fi;
0N/A } operand, operand2;
0N/A fullinfo_type p;
0N/A struct stack_info_type stack_info;
0N/A struct register_info_type register_info;
0N/A#define FLAG_REACHED 0x01 /* instruction reached */
0N/A#define FLAG_NEED_CONSTRUCTOR 0x02 /* must call this.<init> or super.<init> */
0N/A#define FLAG_NO_RETURN 0x04 /* must throw out of method */
0N/A flag_type or_flags; /* true for at least one path to this inst */
0N/A#define FLAG_CONSTRUCTED 0x01 /* this.<init> or super.<init> called */
0N/A flag_type and_flags; /* true for all paths to this instruction */
0N/A};
0N/A
0N/Astruct handler_info_type {
0N/A int start, end, handler;
0N/A struct stack_info_type stack_info;
0N/A};
0N/A
0N/Astruct stack_item_type {
0N/A fullinfo_type item;
0N/A struct stack_item_type *next;
0N/A};
0N/A
0N/Atypedef struct context_type context_type;
0N/Atypedef struct instruction_data_type instruction_data_type;
0N/Atypedef struct stack_item_type stack_item_type;
0N/Atypedef struct register_info_type register_info_type;
0N/Atypedef struct stack_info_type stack_info_type;
0N/Atypedef struct mask_type mask_type;
0N/A
0N/Astatic void read_all_code(context_type *context, jclass cb, int num_methods,
0N/A int** code_lengths, unsigned char*** code);
0N/Astatic void verify_method(context_type *context, jclass cb, int index,
0N/A int code_length, unsigned char* code);
0N/Astatic void free_all_code(int num_methods, int* lengths, unsigned char** code);
0N/Astatic void verify_field(context_type *context, jclass cb, int index);
0N/A
0N/Astatic void verify_opcode_operands (context_type *, unsigned int inumber, int offset);
502N/Astatic void set_protected(context_type *, unsigned int inumber, int key, int);
0N/Astatic jboolean is_superclass(context_type *, fullinfo_type);
0N/A
0N/Astatic void initialize_exception_table(context_type *);
0N/Astatic int instruction_length(unsigned char *iptr, unsigned char *end);
0N/Astatic jboolean isLegalTarget(context_type *, int offset);
0N/Astatic void verify_constant_pool_type(context_type *, int, unsigned);
0N/A
0N/Astatic void initialize_dataflow(context_type *);
0N/Astatic void run_dataflow(context_type *context);
0N/Astatic void check_register_values(context_type *context, unsigned int inumber);
0N/Astatic void check_flags(context_type *context, unsigned int inumber);
0N/Astatic void pop_stack(context_type *, unsigned int inumber, stack_info_type *);
0N/Astatic void update_registers(context_type *, unsigned int inumber, register_info_type *);
0N/Astatic void update_flags(context_type *, unsigned int inumber,
0N/A flag_type *new_and_flags, flag_type *new_or_flags);
0N/Astatic void push_stack(context_type *, unsigned int inumber, stack_info_type *stack);
0N/A
0N/Astatic void merge_into_successors(context_type *, unsigned int inumber,
0N/A register_info_type *register_info,
0N/A stack_info_type *stack_info,
0N/A flag_type and_flags, flag_type or_flags);
0N/Astatic void merge_into_one_successor(context_type *context,
0N/A unsigned int from_inumber,
0N/A unsigned int inumber,
0N/A register_info_type *register_info,
0N/A stack_info_type *stack_info,
0N/A flag_type and_flags, flag_type or_flags,
0N/A jboolean isException);
0N/Astatic void merge_stack(context_type *, unsigned int inumber,
0N/A unsigned int to_inumber, stack_info_type *);
0N/Astatic void merge_registers(context_type *, unsigned int inumber,
0N/A unsigned int to_inumber,
0N/A register_info_type *);
0N/Astatic void merge_flags(context_type *context, unsigned int from_inumber,
0N/A unsigned int to_inumber,
0N/A flag_type new_and_flags, flag_type new_or_flags);
0N/A
0N/Astatic stack_item_type *copy_stack(context_type *, stack_item_type *);
0N/Astatic mask_type *copy_masks(context_type *, mask_type *masks, int mask_count);
0N/Astatic mask_type *add_to_masks(context_type *, mask_type *, int , int);
0N/A
0N/Astatic fullinfo_type decrement_indirection(fullinfo_type);
0N/A
0N/Astatic fullinfo_type merge_fullinfo_types(context_type *context,
0N/A fullinfo_type a,
0N/A fullinfo_type b,
0N/A jboolean assignment);
0N/Astatic jboolean isAssignableTo(context_type *,
0N/A fullinfo_type a,
0N/A fullinfo_type b);
0N/A
0N/Astatic jclass object_fullinfo_to_classclass(context_type *, fullinfo_type);
0N/A
0N/A
0N/A#define NEW(type, count) \
0N/A ((type *)CCalloc(context, (count)*(sizeof(type)), JNI_FALSE))
0N/A#define ZNEW(type, count) \
0N/A ((type *)CCalloc(context, (count)*(sizeof(type)), JNI_TRUE))
0N/A
0N/Astatic void CCinit(context_type *context);
0N/Astatic void CCreinit(context_type *context);
0N/Astatic void CCdestroy(context_type *context);
0N/Astatic void *CCalloc(context_type *context, int size, jboolean zero);
0N/A
0N/Astatic fullinfo_type cp_index_to_class_fullinfo(context_type *, int, int);
0N/A
0N/Astatic char signature_to_fieldtype(context_type *context,
0N/A const char **signature_p, fullinfo_type *info);
0N/A
0N/Astatic void CCerror (context_type *, char *format, ...);
0N/Astatic void CFerror (context_type *, char *format, ...);
0N/Astatic void CCout_of_memory (context_type *);
0N/A
0N/A/* Because we can longjmp any time, we need to be very careful about
0N/A * remembering what needs to be freed. */
0N/A
0N/Astatic void check_and_push(context_type *context, const void *ptr, int kind);
0N/Astatic void pop_and_free(context_type *context);
0N/A
0N/Astatic int signature_to_args_size(const char *method_signature);
0N/A
0N/A#ifdef DEBUG
0N/Astatic void print_stack (context_type *, stack_info_type *stack_info);
0N/Astatic void print_registers(context_type *, register_info_type *register_info);
0N/Astatic void print_flags(context_type *, flag_type, flag_type);
0N/Astatic void print_formatted_fieldname(context_type *context, int index);
0N/Astatic void print_formatted_methodname(context_type *context, int index);
0N/A#endif
0N/A
0N/Avoid initialize_class_hash(context_type *context)
0N/A{
0N/A hash_table_type *class_hash = &(context->class_hash);
0N/A class_hash->buckets = (hash_bucket_type **)
0N/A calloc(MAX_HASH_ENTRIES / HASH_ROW_SIZE, sizeof(hash_bucket_type *));
0N/A class_hash->table = (unsigned short *)
0N/A calloc(HASH_TABLE_SIZE, sizeof(unsigned short));
0N/A if (class_hash->buckets == 0 ||
0N/A class_hash->table == 0)
0N/A CCout_of_memory(context);
0N/A class_hash->entries_used = 0;
0N/A}
0N/A
0N/Astatic void finalize_class_hash(context_type *context)
0N/A{
0N/A hash_table_type *class_hash = &(context->class_hash);
0N/A JNIEnv *env = context->env;
0N/A int i;
0N/A /* 4296677: bucket index starts from 1. */
0N/A for (i=1;i<=class_hash->entries_used;i++) {
0N/A hash_bucket_type *bucket = GET_BUCKET(class_hash, i);
0N/A assert(bucket != NULL);
0N/A free(bucket->name);
0N/A if (bucket->class) {
0N/A (*env)->DeleteGlobalRef(env, bucket->class);
0N/A#ifdef DEBUG
0N/A context->n_globalrefs--;
0N/A#endif
0N/A }
0N/A }
0N/A if (class_hash->buckets) {
0N/A for (i=0;i<MAX_HASH_ENTRIES / HASH_ROW_SIZE; i++) {
0N/A if (class_hash->buckets[i] == 0)
0N/A break;
0N/A free(class_hash->buckets[i]);
0N/A }
0N/A }
0N/A free(class_hash->buckets);
0N/A free(class_hash->table);
0N/A}
0N/A
0N/Astatic hash_bucket_type *
0N/Anew_bucket(context_type *context, unsigned short *pID)
0N/A{
0N/A hash_table_type *class_hash = &(context->class_hash);
0N/A int i = *pID = class_hash->entries_used + 1;
0N/A int row = i / HASH_ROW_SIZE;
0N/A if (i >= MAX_HASH_ENTRIES)
0N/A CCerror(context, "Exceeded verifier's limit of 65535 referred classes");
0N/A if (class_hash->buckets[row] == 0) {
0N/A class_hash->buckets[row] = (hash_bucket_type*)
0N/A calloc(HASH_ROW_SIZE, sizeof(hash_bucket_type));
0N/A if (class_hash->buckets[row] == 0)
0N/A CCout_of_memory(context);
0N/A }
0N/A class_hash->entries_used++; /* only increment when we are sure there
0N/A is no overflow. */
0N/A return GET_BUCKET(class_hash, i);
0N/A}
0N/A
0N/Astatic unsigned int
0N/Aclass_hash_fun(const char *s)
0N/A{
0N/A int i;
0N/A unsigned raw_hash;
0N/A for (raw_hash = 0; (i = *s) != '\0'; ++s)
0N/A raw_hash = raw_hash * 37 + i;
0N/A return raw_hash;
0N/A}
0N/A
0N/A/*
0N/A * Find a class using the defining loader of the current class
0N/A * and return a local reference to it.
0N/A */
0N/Astatic jclass load_class_local(context_type *context,const char *classname)
0N/A{
0N/A jclass cb = JVM_FindClassFromClass(context->env, classname,
0N/A JNI_FALSE, context->class);
0N/A if (cb == 0)
0N/A CCerror(context, "Cannot find class %s", classname);
0N/A return cb;
0N/A}
0N/A
0N/A/*
0N/A * Find a class using the defining loader of the current class
0N/A * and return a global reference to it.
0N/A */
0N/Astatic jclass load_class_global(context_type *context, const char *classname)
0N/A{
0N/A JNIEnv *env = context->env;
0N/A jclass local, global;
0N/A
0N/A local = load_class_local(context, classname);
0N/A global = (*env)->NewGlobalRef(env, local);
0N/A if (global == 0)
0N/A CCout_of_memory(context);
0N/A#ifdef DEBUG
0N/A context->n_globalrefs++;
0N/A#endif
0N/A (*env)->DeleteLocalRef(env, local);
0N/A return global;
0N/A}
0N/A
0N/A/*
0N/A * Return a unique ID given a local class reference. The loadable
0N/A * flag is true if the defining class loader of context->class
0N/A * is known to be capable of loading the class.
0N/A */
0N/Astatic unsigned short
0N/Aclass_to_ID(context_type *context, jclass cb, jboolean loadable)
0N/A{
0N/A JNIEnv *env = context->env;
0N/A hash_table_type *class_hash = &(context->class_hash);
0N/A unsigned int hash;
0N/A hash_bucket_type *bucket;
0N/A unsigned short *pID;
0N/A const char *name = JVM_GetClassNameUTF(env, cb);
0N/A
0N/A check_and_push(context, name, VM_STRING_UTF);
0N/A hash = class_hash_fun(name);
0N/A pID = &(class_hash->table[hash % HASH_TABLE_SIZE]);
0N/A while (*pID) {
0N/A bucket = GET_BUCKET(class_hash, *pID);
0N/A if (bucket->hash == hash && strcmp(name, bucket->name) == 0) {
0N/A /*
0N/A * There is an unresolved entry with our name
0N/A * so we're forced to load it in case it matches us.
0N/A */
0N/A if (bucket->class == 0) {
0N/A assert(bucket->loadable == JNI_TRUE);
0N/A bucket->class = load_class_global(context, name);
0N/A }
0N/A
0N/A /*
0N/A * It's already in the table. Update the loadable
0N/A * state if it's known and then we're done.
0N/A */
0N/A if ((*env)->IsSameObject(env, cb, bucket->class)) {
0N/A if (loadable && !bucket->loadable)
0N/A bucket->loadable = JNI_TRUE;
0N/A goto done;
0N/A }
0N/A }
0N/A pID = &bucket->next;
0N/A }
0N/A bucket = new_bucket(context, pID);
0N/A bucket->next = 0;
0N/A bucket->hash = hash;
0N/A bucket->name = malloc(strlen(name) + 1);
0N/A if (bucket->name == 0)
0N/A CCout_of_memory(context);
0N/A strcpy(bucket->name, name);
0N/A bucket->loadable = loadable;
0N/A bucket->class = (*env)->NewGlobalRef(env, cb);
0N/A if (bucket->class == 0)
0N/A CCout_of_memory(context);
0N/A#ifdef DEBUG
0N/A context->n_globalrefs++;
0N/A#endif
0N/A
0N/Adone:
0N/A pop_and_free(context);
0N/A return *pID;
0N/A}
0N/A
0N/A/*
0N/A * Return a unique ID given a class name from the constant pool.
0N/A * All classes are lazily loaded from the defining loader of
0N/A * context->class.
0N/A */
0N/Astatic unsigned short
0N/Aclass_name_to_ID(context_type *context, const char *name)
0N/A{
0N/A hash_table_type *class_hash = &(context->class_hash);
0N/A unsigned int hash = class_hash_fun(name);
0N/A hash_bucket_type *bucket;
0N/A unsigned short *pID;
0N/A jboolean force_load = JNI_FALSE;
0N/A
0N/A pID = &(class_hash->table[hash % HASH_TABLE_SIZE]);
0N/A while (*pID) {
0N/A bucket = GET_BUCKET(class_hash, *pID);
0N/A if (bucket->hash == hash && strcmp(name, bucket->name) == 0) {
0N/A if (bucket->loadable)
0N/A goto done;
0N/A force_load = JNI_TRUE;
0N/A }
0N/A pID = &bucket->next;
0N/A }
0N/A
0N/A if (force_load) {
0N/A /*
0N/A * We found at least one matching named entry for a class that
0N/A * was not known to be loadable through the defining class loader
0N/A * of context->class. We must load our named class and update
0N/A * the hash table in case one these entries matches our class.
0N/A */
0N/A JNIEnv *env = context->env;
0N/A jclass cb = load_class_local(context, name);
0N/A unsigned short id = class_to_ID(context, cb, JNI_TRUE);
0N/A (*env)->DeleteLocalRef(env, cb);
0N/A return id;
0N/A }
0N/A
0N/A bucket = new_bucket(context, pID);
0N/A bucket->next = 0;
0N/A bucket->class = 0;
0N/A bucket->loadable = JNI_TRUE; /* name-only IDs are implicitly loadable */
0N/A bucket->hash = hash;
0N/A bucket->name = malloc(strlen(name) + 1);
0N/A if (bucket->name == 0)
0N/A CCout_of_memory(context);
0N/A strcpy(bucket->name, name);
0N/A
0N/Adone:
0N/A return *pID;
0N/A}
0N/A
0N/Astatic const char *
0N/AID_to_class_name(context_type *context, unsigned short ID)
0N/A{
0N/A hash_table_type *class_hash = &(context->class_hash);
0N/A hash_bucket_type *bucket = GET_BUCKET(class_hash, ID);
0N/A return bucket->name;
0N/A}
0N/A
0N/Astatic jclass
0N/AID_to_class(context_type *context, unsigned short ID)
0N/A{
0N/A hash_table_type *class_hash = &(context->class_hash);
0N/A hash_bucket_type *bucket = GET_BUCKET(class_hash, ID);
0N/A if (bucket->class == 0) {
0N/A assert(bucket->loadable == JNI_TRUE);
0N/A bucket->class = load_class_global(context, bucket->name);
0N/A }
0N/A return bucket->class;
0N/A}
0N/A
0N/Astatic fullinfo_type
0N/Amake_loadable_class_info(context_type *context, jclass cb)
0N/A{
0N/A return MAKE_FULLINFO(ITEM_Object, 0,
0N/A class_to_ID(context, cb, JNI_TRUE));
0N/A}
0N/A
0N/Astatic fullinfo_type
0N/Amake_class_info(context_type *context, jclass cb)
0N/A{
0N/A return MAKE_FULLINFO(ITEM_Object, 0,
0N/A class_to_ID(context, cb, JNI_FALSE));
0N/A}
0N/A
0N/Astatic fullinfo_type
0N/Amake_class_info_from_name(context_type *context, const char *name)
0N/A{
0N/A return MAKE_FULLINFO(ITEM_Object, 0,
0N/A class_name_to_ID(context, name));
0N/A}
0N/A
0N/A/* RETURNS
0N/A * 1: on success chosen to be consistent with previous VerifyClass
0N/A * 0: verify error
0N/A * 2: out of memory
0N/A * 3: class format error
0N/A *
0N/A * Called by verify_class. Verify the code of each of the methods
0N/A * in a class. Note that this function apparently can't be JNICALL,
0N/A * because if it is the dynamic linker doesn't appear to be able to
0N/A * find it on Win32.
0N/A */
0N/A
0N/A#define CC_OK 1
0N/A#define CC_VerifyError 0
0N/A#define CC_OutOfMemory 2
0N/A#define CC_ClassFormatError 3
0N/A
0N/AJNIEXPORT jboolean
0N/AVerifyClassForMajorVersion(JNIEnv *env, jclass cb, char *buffer, jint len,
0N/A jint major_version)
0N/A{
0N/A context_type context_structure;
0N/A context_type *context = &context_structure;
0N/A jboolean result = CC_OK;
0N/A int i;
0N/A int num_methods;
0N/A int* code_lengths;
0N/A unsigned char** code;
0N/A
0N/A#ifdef DEBUG
0N/A GlobalContext = context;
0N/A#endif
0N/A
0N/A memset(context, 0, sizeof(context_type));
0N/A context->message = buffer;
0N/A context->message_buf_len = len;
0N/A
0N/A context->env = env;
0N/A context->class = cb;
0N/A
0N/A /* Set invalid method/field index of the context, in case anyone
0N/A calls CCerror */
0N/A context->method_index = -1;
0N/A context->field_index = -1;
0N/A
0N/A /* Don't call CCerror or anything that can call it above the setjmp! */
0N/A if (!setjmp(context->jump_buffer)) {
0N/A jclass super;
0N/A
0N/A CCinit(context); /* initialize heap; may throw */
0N/A
0N/A initialize_class_hash(context);
0N/A
0N/A context->major_version = major_version;
0N/A context->nconstants = JVM_GetClassCPEntriesCount(env, cb);
0N/A context->constant_types = (unsigned char *)
0N/A malloc(sizeof(unsigned char) * context->nconstants + 1);
0N/A
0N/A if (context->constant_types == 0)
0N/A CCout_of_memory(context);
0N/A
0N/A JVM_GetClassCPTypes(env, cb, context->constant_types);
0N/A
0N/A if (context->constant_types == 0)
0N/A CCout_of_memory(context);
0N/A
0N/A context->object_info =
0N/A make_class_info_from_name(context, "java/lang/Object");
0N/A context->string_info =
0N/A make_class_info_from_name(context, "java/lang/String");
0N/A context->throwable_info =
0N/A make_class_info_from_name(context, "java/lang/Throwable");
0N/A context->cloneable_info =
0N/A make_class_info_from_name(context, "java/lang/Cloneable");
0N/A context->serializable_info =
0N/A make_class_info_from_name(context, "java/io/Serializable");
0N/A
0N/A context->currentclass_info = make_loadable_class_info(context, cb);
0N/A
0N/A super = (*env)->GetSuperclass(env, cb);
0N/A
0N/A if (super != 0) {
0N/A fullinfo_type *gptr;
0N/A int i = 0;
0N/A
0N/A context->superclass_info = make_loadable_class_info(context, super);
0N/A
0N/A while(super != 0) {
0N/A jclass tmp_cb = (*env)->GetSuperclass(env, super);
0N/A (*env)->DeleteLocalRef(env, super);
0N/A super = tmp_cb;
0N/A i++;
0N/A }
0N/A (*env)->DeleteLocalRef(env, super);
0N/A super = 0;
0N/A
0N/A /* Can't go on context heap since it survives more than
0N/A one method */
0N/A context->superclasses = gptr =
0N/A malloc(sizeof(fullinfo_type)*(i + 1));
0N/A if (gptr == 0) {
0N/A CCout_of_memory(context);
0N/A }
0N/A
0N/A super = (*env)->GetSuperclass(env, context->class);
0N/A while(super != 0) {
0N/A jclass tmp_cb;
0N/A *gptr++ = make_class_info(context, super);
0N/A tmp_cb = (*env)->GetSuperclass(env, super);
0N/A (*env)->DeleteLocalRef(env, super);
0N/A super = tmp_cb;
0N/A }
0N/A *gptr = 0;
0N/A } else {
0N/A context->superclass_info = 0;
0N/A }
0N/A
0N/A (*env)->DeleteLocalRef(env, super);
0N/A
0N/A /* Look at each method */
0N/A for (i = JVM_GetClassFieldsCount(env, cb); --i >= 0;)
0N/A verify_field(context, cb, i);
0N/A num_methods = JVM_GetClassMethodsCount(env, cb);
0N/A read_all_code(context, cb, num_methods, &code_lengths, &code);
0N/A for (i = num_methods - 1; i >= 0; --i)
0N/A verify_method(context, cb, i, code_lengths[i], code[i]);
0N/A free_all_code(num_methods, code_lengths, code);
0N/A result = CC_OK;
0N/A } else {
0N/A result = context->err_code;
0N/A }
0N/A
0N/A /* Cleanup */
0N/A finalize_class_hash(context);
0N/A
0N/A while(context->allocated_memory)
0N/A pop_and_free(context);
0N/A
0N/A#ifdef DEBUG
0N/A GlobalContext = 0;
0N/A#endif
0N/A
0N/A if (context->exceptions)
0N/A free(context->exceptions);
0N/A
0N/A if (context->code)
0N/A free(context->code);
0N/A
0N/A if (context->constant_types)
0N/A free(context->constant_types);
0N/A
0N/A if (context->superclasses)
0N/A free(context->superclasses);
0N/A
0N/A#ifdef DEBUG
0N/A /* Make sure all global refs created in the verifier are freed */
0N/A assert(context->n_globalrefs == 0);
0N/A#endif
0N/A
0N/A CCdestroy(context); /* destroy heap */
0N/A return result;
0N/A}
0N/A
0N/A#define OLD_FORMAT_MAX_MAJOR_VERSION 48
0N/A
0N/AJNIEXPORT jboolean
0N/AVerifyClass(JNIEnv *env, jclass cb, char *buffer, jint len)
0N/A{
0N/A static int warned = 0;
0N/A if (!warned) {
0N/A jio_fprintf(stdout, "Warning! An old version of jvm is used. This is not supported.\n");
0N/A warned = 1;
0N/A }
0N/A return VerifyClassForMajorVersion(env, cb, buffer, len,
0N/A OLD_FORMAT_MAX_MAJOR_VERSION);
0N/A}
0N/A
0N/Astatic void
0N/Averify_field(context_type *context, jclass cb, int field_index)
0N/A{
0N/A JNIEnv *env = context->env;
0N/A int access_bits = JVM_GetFieldIxModifiers(env, cb, field_index);
0N/A context->field_index = field_index;
0N/A
0N/A if ( ((access_bits & JVM_ACC_PUBLIC) != 0) &&
0N/A ((access_bits & (JVM_ACC_PRIVATE | JVM_ACC_PROTECTED)) != 0)) {
0N/A CCerror(context, "Inconsistent access bits.");
0N/A }
0N/A context->field_index = -1;
0N/A}
0N/A
0N/A
0N/A/**
0N/A * We read all of the class's methods' code because it is possible that
0N/A * the verification of one method could resulting in linking further
0N/A * down the stack (due to class loading), which could end up rewriting
0N/A * some of the bytecode of methods we haven't verified yet. Since we
0N/A * don't want to see the rewritten bytecode, cache all the code and
0N/A * operate only on that.
0N/A */
0N/Astatic void
0N/Aread_all_code(context_type* context, jclass cb, int num_methods,
0N/A int** lengths_addr, unsigned char*** code_addr)
0N/A{
0N/A int* lengths = malloc(sizeof(int) * num_methods);
0N/A unsigned char** code = malloc(sizeof(unsigned char*) * num_methods);
0N/A
0N/A *(lengths_addr) = lengths;
0N/A *(code_addr) = code;
0N/A
0N/A if (lengths == 0 || code == 0) {
0N/A CCout_of_memory(context);
0N/A } else {
0N/A int i;
0N/A for (i = 0; i < num_methods; ++i) {
0N/A lengths[i] = JVM_GetMethodIxByteCodeLength(context->env, cb, i);
0N/A if (lengths[i] != 0) {
0N/A code[i] = malloc(sizeof(unsigned char) * (lengths[i] + 1));
0N/A if (code[i] == NULL) {
0N/A CCout_of_memory(context);
0N/A } else {
0N/A JVM_GetMethodIxByteCode(context->env, cb, i, code[i]);
0N/A }
0N/A } else {
0N/A code[i] = NULL;
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/Astatic void
0N/Afree_all_code(int num_methods, int* lengths, unsigned char** code)
0N/A{
0N/A int i;
0N/A for (i = 0; i < num_methods; ++i) {
0N/A free(code[i]);
0N/A }
0N/A free(lengths);
0N/A free(code);
0N/A}
0N/A
0N/A/* Verify the code of one method */
0N/Astatic void
0N/Averify_method(context_type *context, jclass cb, int method_index,
0N/A int code_length, unsigned char* code)
0N/A{
0N/A JNIEnv *env = context->env;
0N/A int access_bits = JVM_GetMethodIxModifiers(env, cb, method_index);
0N/A int *code_data;
0N/A instruction_data_type *idata = 0;
0N/A int instruction_count;
0N/A int i, offset;
0N/A unsigned int inumber;
0N/A jint nexceptions;
0N/A
0N/A if ((access_bits & (JVM_ACC_NATIVE | JVM_ACC_ABSTRACT)) != 0) {
0N/A /* not much to do for abstract and native methods */
0N/A return;
0N/A }
0N/A
0N/A context->code_length = code_length;
0N/A context->code = code;
0N/A
0N/A /* CCerror can give method-specific info once this is set */
0N/A context->method_index = method_index;
0N/A
0N/A CCreinit(context); /* initial heap */
0N/A code_data = NEW(int, code_length);
0N/A
0N/A#ifdef DEBUG
0N/A if (verify_verbose) {
0N/A const char *classname = JVM_GetClassNameUTF(env, cb);
0N/A const char *methodname =
0N/A JVM_GetMethodIxNameUTF(env, cb, method_index);
0N/A const char *signature =
0N/A JVM_GetMethodIxSignatureUTF(env, cb, method_index);
0N/A jio_fprintf(stdout, "Looking at %s.%s%s\n",
0N/A (classname ? classname : ""),
0N/A (methodname ? methodname : ""),
0N/A (signature ? signature : ""));
0N/A JVM_ReleaseUTF(classname);
0N/A JVM_ReleaseUTF(methodname);
0N/A JVM_ReleaseUTF(signature);
0N/A }
0N/A#endif
0N/A
0N/A if (((access_bits & JVM_ACC_PUBLIC) != 0) &&
0N/A ((access_bits & (JVM_ACC_PRIVATE | JVM_ACC_PROTECTED)) != 0)) {
0N/A CCerror(context, "Inconsistent access bits.");
0N/A }
0N/A
0N/A /* Run through the code. Mark the start of each instruction, and give
0N/A * the instruction a number */
0N/A for (i = 0, offset = 0; offset < code_length; i++) {
0N/A int length = instruction_length(&code[offset], code + code_length);
0N/A int next_offset = offset + length;
0N/A if (length <= 0)
0N/A CCerror(context, "Illegal instruction found at offset %d", offset);
0N/A if (next_offset > code_length)
0N/A CCerror(context, "Code stops in the middle of instruction "
0N/A " starting at offset %d", offset);
0N/A code_data[offset] = i;
0N/A while (++offset < next_offset)
0N/A code_data[offset] = -1; /* illegal location */
0N/A }
0N/A instruction_count = i; /* number of instructions in code */
0N/A
0N/A /* Allocate a structure to hold info about each instruction. */
0N/A idata = NEW(instruction_data_type, instruction_count);
0N/A
0N/A /* Initialize the heap, and other info in the context structure. */
0N/A context->code = code;
0N/A context->instruction_data = idata;
0N/A context->code_data = code_data;
0N/A context->instruction_count = instruction_count;
0N/A context->handler_info =
0N/A NEW(struct handler_info_type,
0N/A JVM_GetMethodIxExceptionTableLength(env, cb, method_index));
0N/A context->bitmask_size =
0N/A (JVM_GetMethodIxLocalsCount(env, cb, method_index)
0N/A + (BITS_PER_INT - 1))/BITS_PER_INT;
0N/A
0N/A if (instruction_count == 0)
0N/A CCerror(context, "Empty code");
0N/A
0N/A for (inumber = 0, offset = 0; offset < code_length; inumber++) {
0N/A int length = instruction_length(&code[offset], code + code_length);
0N/A instruction_data_type *this_idata = &idata[inumber];
0N/A this_idata->opcode = code[offset];
0N/A this_idata->stack_info.stack = NULL;
0N/A this_idata->stack_info.stack_size = UNKNOWN_STACK_SIZE;
0N/A this_idata->register_info.register_count = UNKNOWN_REGISTER_COUNT;
0N/A this_idata->changed = JNI_FALSE; /* no need to look at it yet. */
0N/A this_idata->protected = JNI_FALSE; /* no need to look at it yet. */
0N/A this_idata->and_flags = (flag_type) -1; /* "bottom" and value */
0N/A this_idata->or_flags = 0; /* "bottom" or value*/
0N/A /* This also sets up this_data->operand. It also makes the
0N/A * xload_x and xstore_x instructions look like the generic form. */
0N/A verify_opcode_operands(context, inumber, offset);
0N/A offset += length;
0N/A }
0N/A
0N/A
0N/A /* make sure exception table is reasonable. */
0N/A initialize_exception_table(context);
0N/A /* Set up first instruction, and start of exception handlers. */
0N/A initialize_dataflow(context);
0N/A /* Run data flow analysis on the instructions. */
0N/A run_dataflow(context);
0N/A
0N/A /* verify checked exceptions, if any */
0N/A nexceptions = JVM_GetMethodIxExceptionsCount(env, cb, method_index);
0N/A context->exceptions = (unsigned short *)
0N/A malloc(sizeof(unsigned short) * nexceptions + 1);
0N/A if (context->exceptions == 0)
0N/A CCout_of_memory(context);
0N/A JVM_GetMethodIxExceptionIndexes(env, cb, method_index,
0N/A context->exceptions);
0N/A for (i = 0; i < nexceptions; i++) {
0N/A /* Make sure the constant pool item is JVM_CONSTANT_Class */
0N/A verify_constant_pool_type(context, (int)context->exceptions[i],
0N/A 1 << JVM_CONSTANT_Class);
0N/A }
0N/A free(context->exceptions);
0N/A context->exceptions = 0;
0N/A context->code = 0;
0N/A context->method_index = -1;
0N/A}
0N/A
0N/A
0N/A/* Look at a single instruction, and verify its operands. Also, for
0N/A * simplicity, move the operand into the ->operand field.
0N/A * Make sure that branches don't go into the middle of nowhere.
0N/A */
0N/A
0N/Astatic jint ntohl(jint n)
0N/A{
0N/A unsigned char *p = (unsigned char *)&n;
0N/A return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
0N/A}
0N/A
0N/Astatic void
0N/Averify_opcode_operands(context_type *context, unsigned int inumber, int offset)
0N/A{
0N/A JNIEnv *env = context->env;
0N/A instruction_data_type *idata = context->instruction_data;
0N/A instruction_data_type *this_idata = &idata[inumber];
0N/A int *code_data = context->code_data;
0N/A int mi = context->method_index;
0N/A unsigned char *code = context->code;
502N/A int opcode = this_idata->opcode;
0N/A int var;
0N/A
0N/A /*
0N/A * Set the ip fields to 0 not the i fields because the ip fields
0N/A * are 64 bits on 64 bit architectures, the i field is only 32
0N/A */
0N/A this_idata->operand.ip = 0;
0N/A this_idata->operand2.ip = 0;
0N/A
0N/A switch (opcode) {
0N/A
502N/A case JVM_OPC_jsr:
0N/A /* instruction of ret statement */
0N/A this_idata->operand2.i = UNKNOWN_RET_INSTRUCTION;
0N/A /* FALLTHROUGH */
502N/A case JVM_OPC_ifeq: case JVM_OPC_ifne: case JVM_OPC_iflt:
502N/A case JVM_OPC_ifge: case JVM_OPC_ifgt: case JVM_OPC_ifle:
502N/A case JVM_OPC_ifnull: case JVM_OPC_ifnonnull:
502N/A case JVM_OPC_if_icmpeq: case JVM_OPC_if_icmpne: case JVM_OPC_if_icmplt:
502N/A case JVM_OPC_if_icmpge: case JVM_OPC_if_icmpgt: case JVM_OPC_if_icmple:
502N/A case JVM_OPC_if_acmpeq: case JVM_OPC_if_acmpne:
502N/A case JVM_OPC_goto: {
0N/A /* Set the ->operand to be the instruction number of the target. */
0N/A int jump = (((signed char)(code[offset+1])) << 8) + code[offset+2];
0N/A int target = offset + jump;
0N/A if (!isLegalTarget(context, target))
0N/A CCerror(context, "Illegal target of jump or branch");
0N/A this_idata->operand.i = code_data[target];
0N/A break;
0N/A }
0N/A
502N/A case JVM_OPC_jsr_w:
0N/A /* instruction of ret statement */
0N/A this_idata->operand2.i = UNKNOWN_RET_INSTRUCTION;
0N/A /* FALLTHROUGH */
502N/A case JVM_OPC_goto_w: {
0N/A /* Set the ->operand to be the instruction number of the target. */
0N/A int jump = (((signed char)(code[offset+1])) << 24) +
0N/A (code[offset+2] << 16) + (code[offset+3] << 8) +
0N/A (code[offset + 4]);
0N/A int target = offset + jump;
0N/A if (!isLegalTarget(context, target))
0N/A CCerror(context, "Illegal target of jump or branch");
0N/A this_idata->operand.i = code_data[target];
0N/A break;
0N/A }
0N/A
502N/A case JVM_OPC_tableswitch:
502N/A case JVM_OPC_lookupswitch: {
0N/A /* Set the ->operand to be a table of possible instruction targets. */
0N/A int *lpc = (int *) UCALIGN(code + offset + 1);
0N/A int *lptr;
0N/A int *saved_operand;
0N/A int keys;
0N/A int k, delta;
0N/A /* 4639449, 4647081: Padding bytes must be zero. */
0N/A unsigned char* bptr = (unsigned char*) (code + offset + 1);
0N/A for (; bptr < (unsigned char*)lpc; bptr++) {
0N/A if (*bptr != 0) {
0N/A CCerror(context, "Non zero padding bytes in switch");
0N/A }
0N/A }
502N/A if (opcode == JVM_OPC_tableswitch) {
0N/A keys = ntohl(lpc[2]) - ntohl(lpc[1]) + 1;
0N/A delta = 1;
0N/A } else {
0N/A keys = ntohl(lpc[1]); /* number of pairs */
0N/A delta = 2;
0N/A /* Make sure that the tableswitch items are sorted */
0N/A for (k = keys - 1, lptr = &lpc[2]; --k >= 0; lptr += 2) {
0N/A int this_key = ntohl(lptr[0]); /* NB: ntohl may be unsigned */
0N/A int next_key = ntohl(lptr[2]);
0N/A if (this_key >= next_key) {
0N/A CCerror(context, "Unsorted lookup switch");
0N/A }
0N/A }
0N/A }
0N/A saved_operand = NEW(int, keys + 2);
0N/A if (!isLegalTarget(context, offset + ntohl(lpc[0])))
0N/A CCerror(context, "Illegal default target in switch");
0N/A saved_operand[keys + 1] = code_data[offset + ntohl(lpc[0])];
0N/A for (k = keys, lptr = &lpc[3]; --k >= 0; lptr += delta) {
0N/A int target = offset + ntohl(lptr[0]);
0N/A if (!isLegalTarget(context, target))
502N/A CCerror(context, "Illegal branch in tableswitch");
0N/A saved_operand[k + 1] = code_data[target];
0N/A }
0N/A saved_operand[0] = keys + 1; /* number of successors */
0N/A this_idata->operand.ip = saved_operand;
0N/A break;
0N/A }
0N/A
502N/A case JVM_OPC_ldc: {
0N/A /* Make sure the constant pool item is the right type. */
0N/A int key = code[offset + 1];
0N/A int types = (1 << JVM_CONSTANT_Integer) | (1 << JVM_CONSTANT_Float) |
0N/A (1 << JVM_CONSTANT_String);
0N/A if (context->major_version >= LDC_CLASS_MAJOR_VERSION) {
0N/A types |= 1 << JVM_CONSTANT_Class;
0N/A }
0N/A this_idata->operand.i = key;
0N/A verify_constant_pool_type(context, key, types);
0N/A break;
0N/A }
0N/A
502N/A case JVM_OPC_ldc_w: {
0N/A /* Make sure the constant pool item is the right type. */
0N/A int key = (code[offset + 1] << 8) + code[offset + 2];
0N/A int types = (1 << JVM_CONSTANT_Integer) | (1 << JVM_CONSTANT_Float) |
0N/A (1 << JVM_CONSTANT_String);
0N/A if (context->major_version >= LDC_CLASS_MAJOR_VERSION) {
0N/A types |= 1 << JVM_CONSTANT_Class;
0N/A }
0N/A this_idata->operand.i = key;
0N/A verify_constant_pool_type(context, key, types);
0N/A break;
0N/A }
0N/A
502N/A case JVM_OPC_ldc2_w: {
0N/A /* Make sure the constant pool item is the right type. */
0N/A int key = (code[offset + 1] << 8) + code[offset + 2];
0N/A int types = (1 << JVM_CONSTANT_Double) | (1 << JVM_CONSTANT_Long);
0N/A this_idata->operand.i = key;
0N/A verify_constant_pool_type(context, key, types);
0N/A break;
0N/A }
0N/A
502N/A case JVM_OPC_getfield: case JVM_OPC_putfield:
502N/A case JVM_OPC_getstatic: case JVM_OPC_putstatic: {
0N/A /* Make sure the constant pool item is the right type. */
0N/A int key = (code[offset + 1] << 8) + code[offset + 2];
0N/A this_idata->operand.i = key;
0N/A verify_constant_pool_type(context, key, 1 << JVM_CONSTANT_Fieldref);
502N/A if (opcode == JVM_OPC_getfield || opcode == JVM_OPC_putfield)
0N/A set_protected(context, inumber, key, opcode);
0N/A break;
0N/A }
0N/A
502N/A case JVM_OPC_invokevirtual:
502N/A case JVM_OPC_invokespecial:
502N/A case JVM_OPC_invokestatic:
502N/A case JVM_OPC_invokeinterface: {
0N/A /* Make sure the constant pool item is the right type. */
0N/A int key = (code[offset + 1] << 8) + code[offset + 2];
0N/A const char *methodname;
0N/A jclass cb = context->class;
0N/A fullinfo_type clazz_info;
0N/A int is_constructor, is_internal;
502N/A int kind = (opcode == JVM_OPC_invokeinterface
0N/A ? 1 << JVM_CONSTANT_InterfaceMethodref
0N/A : 1 << JVM_CONSTANT_Methodref);
0N/A /* Make sure the constant pool item is the right type. */
0N/A verify_constant_pool_type(context, key, kind);
0N/A methodname = JVM_GetCPMethodNameUTF(env, cb, key);
0N/A check_and_push(context, methodname, VM_STRING_UTF);
0N/A is_constructor = !strcmp(methodname, "<init>");
0N/A is_internal = methodname[0] == '<';
0N/A pop_and_free(context);
0N/A
0N/A clazz_info = cp_index_to_class_fullinfo(context, key,
0N/A JVM_CONSTANT_Methodref);
0N/A this_idata->operand.i = key;
0N/A this_idata->operand2.fi = clazz_info;
0N/A if (is_constructor) {
502N/A if (opcode != JVM_OPC_invokespecial) {
0N/A CCerror(context,
0N/A "Must call initializers using invokespecial");
0N/A }
502N/A this_idata->opcode = JVM_OPC_invokeinit;
0N/A } else {
0N/A if (is_internal) {
0N/A CCerror(context, "Illegal call to internal method");
0N/A }
502N/A if (opcode == JVM_OPC_invokespecial
0N/A && clazz_info != context->currentclass_info
0N/A && clazz_info != context->superclass_info) {
0N/A int not_found = 1;
0N/A
0N/A jclass super = (*env)->GetSuperclass(env, context->class);
0N/A while(super != 0) {
0N/A jclass tmp_cb;
0N/A fullinfo_type new_info = make_class_info(context, super);
0N/A if (clazz_info == new_info) {
0N/A not_found = 0;
0N/A break;
0N/A }
0N/A tmp_cb = (*env)->GetSuperclass(env, super);
0N/A (*env)->DeleteLocalRef(env, super);
0N/A super = tmp_cb;
0N/A }
0N/A (*env)->DeleteLocalRef(env, super);
0N/A
0N/A /* The optimizer make cause this to happen on local code */
0N/A if (not_found) {
0N/A#ifdef BROKEN_JAVAC
0N/A jobject loader = JVM_GetClassLoader(env, context->class);
0N/A int has_loader = (loader != 0);
0N/A (*env)->DeleteLocalRef(env, loader);
0N/A if (has_loader)
0N/A#endif /* BROKEN_JAVAC */
0N/A CCerror(context,
0N/A "Illegal use of nonvirtual function call");
0N/A }
0N/A }
0N/A }
502N/A if (opcode == JVM_OPC_invokeinterface) {
0N/A unsigned int args1;
0N/A unsigned int args2;
0N/A const char *signature =
0N/A JVM_GetCPMethodSignatureUTF(env, context->class, key);
0N/A check_and_push(context, signature, VM_STRING_UTF);
0N/A args1 = signature_to_args_size(signature) + 1;
0N/A args2 = code[offset + 3];
0N/A if (args1 != args2) {
0N/A CCerror(context,
502N/A "Inconsistent args_size for invokeinterface");
0N/A }
0N/A if (code[offset + 4] != 0) {
0N/A CCerror(context,
0N/A "Fourth operand byte of invokeinterface must be zero");
0N/A }
0N/A pop_and_free(context);
502N/A } else if (opcode == JVM_OPC_invokevirtual
502N/A || opcode == JVM_OPC_invokespecial)
0N/A set_protected(context, inumber, key, opcode);
0N/A break;
0N/A }
0N/A
0N/A
502N/A case JVM_OPC_instanceof:
502N/A case JVM_OPC_checkcast:
502N/A case JVM_OPC_new:
502N/A case JVM_OPC_anewarray:
502N/A case JVM_OPC_multianewarray: {
0N/A /* Make sure the constant pool item is a class */
0N/A int key = (code[offset + 1] << 8) + code[offset + 2];
0N/A fullinfo_type target;
0N/A verify_constant_pool_type(context, key, 1 << JVM_CONSTANT_Class);
0N/A target = cp_index_to_class_fullinfo(context, key, JVM_CONSTANT_Class);
0N/A if (GET_ITEM_TYPE(target) == ITEM_Bogus)
0N/A CCerror(context, "Illegal type");
0N/A switch(opcode) {
502N/A case JVM_OPC_anewarray:
0N/A if ((GET_INDIRECTION(target)) >= MAX_ARRAY_DIMENSIONS)
0N/A CCerror(context, "Array with too many dimensions");
0N/A this_idata->operand.fi = MAKE_FULLINFO(GET_ITEM_TYPE(target),
0N/A GET_INDIRECTION(target) + 1,
0N/A GET_EXTRA_INFO(target));
0N/A break;
502N/A case JVM_OPC_new:
0N/A if (WITH_ZERO_EXTRA_INFO(target) !=
0N/A MAKE_FULLINFO(ITEM_Object, 0, 0))
0N/A CCerror(context, "Illegal creation of multi-dimensional array");
0N/A /* operand gets set to the "unitialized object". operand2 gets
0N/A * set to what the value will be after it's initialized. */
0N/A this_idata->operand.fi = MAKE_FULLINFO(ITEM_NewObject, 0, inumber);
0N/A this_idata->operand2.fi = target;
0N/A break;
502N/A case JVM_OPC_multianewarray:
0N/A this_idata->operand.fi = target;
0N/A this_idata->operand2.i = code[offset + 3];
502N/A if ( (this_idata->operand2.i > (int)GET_INDIRECTION(target))
0N/A || (this_idata->operand2.i == 0))
0N/A CCerror(context, "Illegal dimension argument");
0N/A break;
0N/A default:
0N/A this_idata->operand.fi = target;
0N/A }
0N/A break;
0N/A }
0N/A
502N/A case JVM_OPC_newarray: {
502N/A /* Cache the result of the JVM_OPC_newarray into the operand slot */
0N/A fullinfo_type full_info;
0N/A switch (code[offset + 1]) {
0N/A case JVM_T_INT:
0N/A full_info = MAKE_FULLINFO(ITEM_Integer, 1, 0); break;
0N/A case JVM_T_LONG:
0N/A full_info = MAKE_FULLINFO(ITEM_Long, 1, 0); break;
0N/A case JVM_T_FLOAT:
0N/A full_info = MAKE_FULLINFO(ITEM_Float, 1, 0); break;
0N/A case JVM_T_DOUBLE:
0N/A full_info = MAKE_FULLINFO(ITEM_Double, 1, 0); break;
0N/A case JVM_T_BYTE: case JVM_T_BOOLEAN:
0N/A full_info = MAKE_FULLINFO(ITEM_Byte, 1, 0); break;
0N/A case JVM_T_CHAR:
0N/A full_info = MAKE_FULLINFO(ITEM_Char, 1, 0); break;
0N/A case JVM_T_SHORT:
0N/A full_info = MAKE_FULLINFO(ITEM_Short, 1, 0); break;
0N/A default:
0N/A full_info = 0; /* Keep lint happy */
502N/A CCerror(context, "Bad type passed to newarray");
0N/A }
0N/A this_idata->operand.fi = full_info;
0N/A break;
0N/A }
0N/A
0N/A /* Fudge iload_x, aload_x, etc to look like their generic cousin. */
502N/A case JVM_OPC_iload_0: case JVM_OPC_iload_1: case JVM_OPC_iload_2: case JVM_OPC_iload_3:
502N/A this_idata->opcode = JVM_OPC_iload;
502N/A var = opcode - JVM_OPC_iload_0;
0N/A goto check_local_variable;
0N/A
502N/A case JVM_OPC_fload_0: case JVM_OPC_fload_1: case JVM_OPC_fload_2: case JVM_OPC_fload_3:
502N/A this_idata->opcode = JVM_OPC_fload;
502N/A var = opcode - JVM_OPC_fload_0;
0N/A goto check_local_variable;
0N/A
502N/A case JVM_OPC_aload_0: case JVM_OPC_aload_1: case JVM_OPC_aload_2: case JVM_OPC_aload_3:
502N/A this_idata->opcode = JVM_OPC_aload;
502N/A var = opcode - JVM_OPC_aload_0;
0N/A goto check_local_variable;
0N/A
502N/A case JVM_OPC_lload_0: case JVM_OPC_lload_1: case JVM_OPC_lload_2: case JVM_OPC_lload_3:
502N/A this_idata->opcode = JVM_OPC_lload;
502N/A var = opcode - JVM_OPC_lload_0;
0N/A goto check_local_variable2;
0N/A
502N/A case JVM_OPC_dload_0: case JVM_OPC_dload_1: case JVM_OPC_dload_2: case JVM_OPC_dload_3:
502N/A this_idata->opcode = JVM_OPC_dload;
502N/A var = opcode - JVM_OPC_dload_0;
0N/A goto check_local_variable2;
0N/A
502N/A case JVM_OPC_istore_0: case JVM_OPC_istore_1: case JVM_OPC_istore_2: case JVM_OPC_istore_3:
502N/A this_idata->opcode = JVM_OPC_istore;
502N/A var = opcode - JVM_OPC_istore_0;
0N/A goto check_local_variable;
0N/A
502N/A case JVM_OPC_fstore_0: case JVM_OPC_fstore_1: case JVM_OPC_fstore_2: case JVM_OPC_fstore_3:
502N/A this_idata->opcode = JVM_OPC_fstore;
502N/A var = opcode - JVM_OPC_fstore_0;
0N/A goto check_local_variable;
0N/A
502N/A case JVM_OPC_astore_0: case JVM_OPC_astore_1: case JVM_OPC_astore_2: case JVM_OPC_astore_3:
502N/A this_idata->opcode = JVM_OPC_astore;
502N/A var = opcode - JVM_OPC_astore_0;
0N/A goto check_local_variable;
0N/A
502N/A case JVM_OPC_lstore_0: case JVM_OPC_lstore_1: case JVM_OPC_lstore_2: case JVM_OPC_lstore_3:
502N/A this_idata->opcode = JVM_OPC_lstore;
502N/A var = opcode - JVM_OPC_lstore_0;
0N/A goto check_local_variable2;
0N/A
502N/A case JVM_OPC_dstore_0: case JVM_OPC_dstore_1: case JVM_OPC_dstore_2: case JVM_OPC_dstore_3:
502N/A this_idata->opcode = JVM_OPC_dstore;
502N/A var = opcode - JVM_OPC_dstore_0;
0N/A goto check_local_variable2;
0N/A
502N/A case JVM_OPC_wide:
0N/A this_idata->opcode = code[offset + 1];
0N/A var = (code[offset + 2] << 8) + code[offset + 3];
0N/A switch(this_idata->opcode) {
502N/A case JVM_OPC_lload: case JVM_OPC_dload:
502N/A case JVM_OPC_lstore: case JVM_OPC_dstore:
0N/A goto check_local_variable2;
0N/A default:
0N/A goto check_local_variable;
0N/A }
0N/A
502N/A case JVM_OPC_iinc: /* the increment amount doesn't matter */
502N/A case JVM_OPC_ret:
502N/A case JVM_OPC_aload: case JVM_OPC_iload: case JVM_OPC_fload:
502N/A case JVM_OPC_astore: case JVM_OPC_istore: case JVM_OPC_fstore:
0N/A var = code[offset + 1];
0N/A check_local_variable:
0N/A /* Make sure that the variable number isn't illegal. */
0N/A this_idata->operand.i = var;
0N/A if (var >= JVM_GetMethodIxLocalsCount(env, context->class, mi))
0N/A CCerror(context, "Illegal local variable number");
0N/A break;
0N/A
502N/A case JVM_OPC_lload: case JVM_OPC_dload: case JVM_OPC_lstore: case JVM_OPC_dstore:
0N/A var = code[offset + 1];
0N/A check_local_variable2:
0N/A /* Make sure that the variable number isn't illegal. */
0N/A this_idata->operand.i = var;
0N/A if ((var + 1) >= JVM_GetMethodIxLocalsCount(env, context->class, mi))
0N/A CCerror(context, "Illegal local variable number");
0N/A break;
0N/A
0N/A default:
502N/A if (opcode > JVM_OPC_MAX)
0N/A CCerror(context, "Quick instructions shouldn't appear yet.");
0N/A break;
0N/A } /* of switch */
0N/A}
0N/A
0N/A
0N/Astatic void
502N/Aset_protected(context_type *context, unsigned int inumber, int key, int opcode)
0N/A{
0N/A JNIEnv *env = context->env;
0N/A fullinfo_type clazz_info;
502N/A if (opcode != JVM_OPC_invokevirtual && opcode != JVM_OPC_invokespecial) {
0N/A clazz_info = cp_index_to_class_fullinfo(context, key,
0N/A JVM_CONSTANT_Fieldref);
0N/A } else {
0N/A clazz_info = cp_index_to_class_fullinfo(context, key,
0N/A JVM_CONSTANT_Methodref);
0N/A }
0N/A if (is_superclass(context, clazz_info)) {
0N/A jclass calledClass =
0N/A object_fullinfo_to_classclass(context, clazz_info);
0N/A int access;
0N/A /* 4734966: JVM_GetCPFieldModifiers() or JVM_GetCPMethodModifiers() only
0N/A searches the referenced field or method in calledClass. The following
0N/A while loop is added to search up the superclass chain to make this
0N/A symbolic resolution consistent with the field/method resolution
0N/A specified in VM spec 5.4.3. */
0N/A calledClass = (*env)->NewLocalRef(env, calledClass);
0N/A do {
0N/A jclass tmp_cb;
502N/A if (opcode != JVM_OPC_invokevirtual && opcode != JVM_OPC_invokespecial) {
0N/A access = JVM_GetCPFieldModifiers
0N/A (env, context->class, key, calledClass);
0N/A } else {
0N/A access = JVM_GetCPMethodModifiers
0N/A (env, context->class, key, calledClass);
0N/A }
0N/A if (access != -1) {
0N/A break;
0N/A }
0N/A tmp_cb = (*env)->GetSuperclass(env, calledClass);
0N/A (*env)->DeleteLocalRef(env, calledClass);
0N/A calledClass = tmp_cb;
0N/A } while (calledClass != 0);
0N/A
0N/A if (access == -1) {
0N/A /* field/method not found, detected at runtime. */
0N/A } else if (access & JVM_ACC_PROTECTED) {
0N/A if (!JVM_IsSameClassPackage(env, calledClass, context->class))
0N/A context->instruction_data[inumber].protected = JNI_TRUE;
0N/A }
0N/A (*env)->DeleteLocalRef(env, calledClass);
0N/A }
0N/A}
0N/A
0N/A
0N/Astatic jboolean
0N/Ais_superclass(context_type *context, fullinfo_type clazz_info) {
0N/A fullinfo_type *fptr = context->superclasses;
0N/A
0N/A if (fptr == 0)
0N/A return JNI_FALSE;
0N/A for (; *fptr != 0; fptr++) {
0N/A if (*fptr == clazz_info)
0N/A return JNI_TRUE;
0N/A }
0N/A return JNI_FALSE;
0N/A}
0N/A
0N/A
0N/A/* Look through each item on the exception table. Each of the fields must
0N/A * refer to a legal instruction.
0N/A */
0N/Astatic void
0N/Ainitialize_exception_table(context_type *context)
0N/A{
0N/A JNIEnv *env = context->env;
0N/A int mi = context->method_index;
0N/A struct handler_info_type *handler_info = context->handler_info;
0N/A int *code_data = context->code_data;
0N/A int code_length = context->code_length;
0N/A int max_stack_size = JVM_GetMethodIxMaxStack(env, context->class, mi);
0N/A int i = JVM_GetMethodIxExceptionTableLength(env, context->class, mi);
0N/A if (max_stack_size < 1 && i > 0) {
0N/A // If the method contains exception handlers, it must have room
0N/A // on the expression stack for the exception that the VM could push
0N/A CCerror(context, "Stack size too large");
0N/A }
0N/A for (; --i >= 0; handler_info++) {
0N/A JVM_ExceptionTableEntryType einfo;
0N/A stack_item_type *stack_item = NEW(stack_item_type, 1);
0N/A
0N/A JVM_GetMethodIxExceptionTableEntry(env, context->class, mi,
0N/A i, &einfo);
0N/A
0N/A if (!(einfo.start_pc < einfo.end_pc &&
0N/A einfo.start_pc >= 0 &&
0N/A isLegalTarget(context, einfo.start_pc) &&
0N/A (einfo.end_pc == code_length ||
0N/A isLegalTarget(context, einfo.end_pc)))) {
0N/A CFerror(context, "Illegal exception table range");
0N/A }
0N/A if (!((einfo.handler_pc > 0) &&
0N/A isLegalTarget(context, einfo.handler_pc))) {
0N/A CFerror(context, "Illegal exception table handler");
0N/A }
0N/A
0N/A handler_info->start = code_data[einfo.start_pc];
0N/A /* einfo.end_pc may point to one byte beyond the end of bytecodes. */
0N/A handler_info->end = (einfo.end_pc == context->code_length) ?
0N/A context->instruction_count : code_data[einfo.end_pc];
0N/A handler_info->handler = code_data[einfo.handler_pc];
0N/A handler_info->stack_info.stack = stack_item;
0N/A handler_info->stack_info.stack_size = 1;
0N/A stack_item->next = NULL;
0N/A if (einfo.catchType != 0) {
0N/A const char *classname;
0N/A /* Constant pool entry type has been checked in format checker */
0N/A classname = JVM_GetCPClassNameUTF(env,
0N/A context->class,
0N/A einfo.catchType);
0N/A check_and_push(context, classname, VM_STRING_UTF);
0N/A stack_item->item = make_class_info_from_name(context, classname);
0N/A if (!isAssignableTo(context,
0N/A stack_item->item,
0N/A context->throwable_info))
0N/A CCerror(context, "catch_type not a subclass of Throwable");
0N/A pop_and_free(context);
0N/A } else {
0N/A stack_item->item = context->throwable_info;
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A/* Given a pointer to an instruction, return its length. Use the table
0N/A * opcode_length[] which is automatically built.
0N/A */
0N/Astatic int instruction_length(unsigned char *iptr, unsigned char *end)
0N/A{
502N/A static unsigned char opcode_length[] = JVM_OPCODE_LENGTH_INITIALIZER;
0N/A int instruction = *iptr;
0N/A switch (instruction) {
502N/A case JVM_OPC_tableswitch: {
0N/A int *lpc = (int *)UCALIGN(iptr + 1);
0N/A int index;
0N/A if (lpc + 2 >= (int *)end) {
0N/A return -1; /* do not read pass the end */
0N/A }
0N/A index = ntohl(lpc[2]) - ntohl(lpc[1]);
0N/A if ((index < 0) || (index > 65535)) {
0N/A return -1; /* illegal */
0N/A } else {
0N/A return (unsigned char *)(&lpc[index + 4]) - iptr;
0N/A }
0N/A }
0N/A
502N/A case JVM_OPC_lookupswitch: {
0N/A int *lpc = (int *) UCALIGN(iptr + 1);
0N/A int npairs;
0N/A if (lpc + 1 >= (int *)end)
0N/A return -1; /* do not read pass the end */
0N/A npairs = ntohl(lpc[1]);
0N/A /* There can't be more than 64K labels because of the limit
0N/A * on per-method byte code length.
0N/A */
0N/A if (npairs < 0 || npairs >= 65536)
0N/A return -1;
0N/A else
0N/A return (unsigned char *)(&lpc[2 * (npairs + 1)]) - iptr;
0N/A }
0N/A
502N/A case JVM_OPC_wide:
0N/A if (iptr + 1 >= end)
0N/A return -1; /* do not read pass the end */
0N/A switch(iptr[1]) {
502N/A case JVM_OPC_ret:
502N/A case JVM_OPC_iload: case JVM_OPC_istore:
502N/A case JVM_OPC_fload: case JVM_OPC_fstore:
502N/A case JVM_OPC_aload: case JVM_OPC_astore:
502N/A case JVM_OPC_lload: case JVM_OPC_lstore:
502N/A case JVM_OPC_dload: case JVM_OPC_dstore:
0N/A return 4;
502N/A case JVM_OPC_iinc:
0N/A return 6;
0N/A default:
0N/A return -1;
0N/A }
0N/A
0N/A default: {
0N/A /* A length of 0 indicates an error. */
0N/A int length = opcode_length[instruction];
0N/A return (length <= 0) ? -1 : length;
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A/* Given the target of a branch, make sure that it's a legal target. */
0N/Astatic jboolean
0N/AisLegalTarget(context_type *context, int offset)
0N/A{
0N/A int code_length = context->code_length;
0N/A int *code_data = context->code_data;
0N/A return (offset >= 0 && offset < code_length && code_data[offset] >= 0);
0N/A}
0N/A
0N/A
0N/A/* Make sure that an element of the constant pool really is of the indicated
0N/A * type.
0N/A */
0N/Astatic void
0N/Averify_constant_pool_type(context_type *context, int index, unsigned mask)
0N/A{
0N/A int nconstants = context->nconstants;
0N/A unsigned char *type_table = context->constant_types;
0N/A unsigned type;
0N/A
0N/A if ((index <= 0) || (index >= nconstants))
0N/A CCerror(context, "Illegal constant pool index");
0N/A
0N/A type = type_table[index];
0N/A if ((mask & (1 << type)) == 0)
0N/A CCerror(context, "Illegal type in constant pool");
0N/A}
0N/A
0N/A
0N/Astatic void
0N/Ainitialize_dataflow(context_type *context)
0N/A{
0N/A JNIEnv *env = context->env;
0N/A instruction_data_type *idata = context->instruction_data;
0N/A int mi = context->method_index;
0N/A jclass cb = context->class;
0N/A int args_size = JVM_GetMethodIxArgsSize(env, cb, mi);
0N/A fullinfo_type *reg_ptr;
0N/A fullinfo_type full_info;
0N/A const char *p;
0N/A const char *signature;
0N/A
0N/A /* Initialize the function entry, since we know everything about it. */
0N/A idata[0].stack_info.stack_size = 0;
0N/A idata[0].stack_info.stack = NULL;
0N/A idata[0].register_info.register_count = args_size;
0N/A idata[0].register_info.registers = NEW(fullinfo_type, args_size);
0N/A idata[0].register_info.mask_count = 0;
0N/A idata[0].register_info.masks = NULL;
0N/A idata[0].and_flags = 0; /* nothing needed */
0N/A idata[0].or_flags = FLAG_REACHED; /* instruction reached */
0N/A reg_ptr = idata[0].register_info.registers;
0N/A
0N/A if ((JVM_GetMethodIxModifiers(env, cb, mi) & JVM_ACC_STATIC) == 0) {
0N/A /* A non static method. If this is an <init> method, the first
0N/A * argument is an uninitialized object. Otherwise it is an object of
0N/A * the given class type. java.lang.Object.<init> is special since
0N/A * we don't call its superclass <init> method.
0N/A */
0N/A if (JVM_IsConstructorIx(env, cb, mi)
0N/A && context->currentclass_info != context->object_info) {
0N/A *reg_ptr++ = MAKE_FULLINFO(ITEM_InitObject, 0, 0);
0N/A idata[0].or_flags |= FLAG_NEED_CONSTRUCTOR;
0N/A } else {
0N/A *reg_ptr++ = context->currentclass_info;
0N/A }
0N/A }
0N/A signature = JVM_GetMethodIxSignatureUTF(env, cb, mi);
0N/A check_and_push(context, signature, VM_STRING_UTF);
0N/A /* Fill in each of the arguments into the registers. */
0N/A for (p = signature + 1; *p != JVM_SIGNATURE_ENDFUNC; ) {
0N/A char fieldchar = signature_to_fieldtype(context, &p, &full_info);
0N/A switch (fieldchar) {
0N/A case 'D': case 'L':
0N/A *reg_ptr++ = full_info;
0N/A *reg_ptr++ = full_info + 1;
0N/A break;
0N/A default:
0N/A *reg_ptr++ = full_info;
0N/A break;
0N/A }
0N/A }
0N/A p++; /* skip over right parenthesis */
0N/A if (*p == 'V') {
0N/A context->return_type = MAKE_FULLINFO(ITEM_Void, 0, 0);
0N/A } else {
0N/A signature_to_fieldtype(context, &p, &full_info);
0N/A context->return_type = full_info;
0N/A }
0N/A pop_and_free(context);
0N/A /* Indicate that we need to look at the first instruction. */
0N/A idata[0].changed = JNI_TRUE;
0N/A}
0N/A
0N/A
0N/A/* Run the data flow analysis, as long as there are things to change. */
0N/Astatic void
0N/Arun_dataflow(context_type *context) {
0N/A JNIEnv *env = context->env;
0N/A int mi = context->method_index;
0N/A jclass cb = context->class;
0N/A int max_stack_size = JVM_GetMethodIxMaxStack(env, cb, mi);
0N/A instruction_data_type *idata = context->instruction_data;
502N/A unsigned int icount = context->instruction_count;
0N/A jboolean work_to_do = JNI_TRUE;
0N/A unsigned int inumber;
0N/A
0N/A /* Run through the loop, until there is nothing left to do. */
0N/A while (work_to_do) {
0N/A work_to_do = JNI_FALSE;
0N/A for (inumber = 0; inumber < icount; inumber++) {
0N/A instruction_data_type *this_idata = &idata[inumber];
0N/A if (this_idata->changed) {
0N/A register_info_type new_register_info;
0N/A stack_info_type new_stack_info;
0N/A flag_type new_and_flags, new_or_flags;
0N/A
0N/A this_idata->changed = JNI_FALSE;
0N/A work_to_do = JNI_TRUE;
0N/A#ifdef DEBUG
0N/A if (verify_verbose) {
0N/A int opcode = this_idata->opcode;
0N/A jio_fprintf(stdout, "Instruction %d: ", inumber);
0N/A print_stack(context, &this_idata->stack_info);
0N/A print_registers(context, &this_idata->register_info);
0N/A print_flags(context,
0N/A this_idata->and_flags, this_idata->or_flags);
0N/A fflush(stdout);
0N/A }
0N/A#endif
0N/A /* Make sure the registers and flags are appropriate */
0N/A check_register_values(context, inumber);
0N/A check_flags(context, inumber);
0N/A
0N/A /* Make sure the stack can deal with this instruction */
0N/A pop_stack(context, inumber, &new_stack_info);
0N/A
0N/A /* Update the registers and flags */
0N/A update_registers(context, inumber, &new_register_info);
0N/A update_flags(context, inumber, &new_and_flags, &new_or_flags);
0N/A
0N/A /* Update the stack. */
0N/A push_stack(context, inumber, &new_stack_info);
0N/A
0N/A if (new_stack_info.stack_size > max_stack_size)
0N/A CCerror(context, "Stack size too large");
0N/A#ifdef DEBUG
0N/A if (verify_verbose) {
0N/A jio_fprintf(stdout, " ");
0N/A print_stack(context, &new_stack_info);
0N/A print_registers(context, &new_register_info);
0N/A print_flags(context, new_and_flags, new_or_flags);
0N/A fflush(stdout);
0N/A }
0N/A#endif
0N/A /* Add the new stack and register information to any
0N/A * instructions that can follow this instruction. */
0N/A merge_into_successors(context, inumber,
0N/A &new_register_info, &new_stack_info,
0N/A new_and_flags, new_or_flags);
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A/* Make sure that the registers contain a legitimate value for the given
0N/A * instruction.
0N/A*/
0N/A
0N/Astatic void
0N/Acheck_register_values(context_type *context, unsigned int inumber)
0N/A{
0N/A instruction_data_type *idata = context->instruction_data;
0N/A instruction_data_type *this_idata = &idata[inumber];
502N/A int opcode = this_idata->opcode;
0N/A int operand = this_idata->operand.i;
0N/A int register_count = this_idata->register_info.register_count;
0N/A fullinfo_type *registers = this_idata->register_info.registers;
0N/A jboolean double_word = JNI_FALSE; /* default value */
0N/A int type;
0N/A
0N/A switch (opcode) {
0N/A default:
0N/A return;
502N/A case JVM_OPC_iload: case JVM_OPC_iinc:
0N/A type = ITEM_Integer; break;
502N/A case JVM_OPC_fload:
0N/A type = ITEM_Float; break;
502N/A case JVM_OPC_aload:
0N/A type = ITEM_Object; break;
502N/A case JVM_OPC_ret:
0N/A type = ITEM_ReturnAddress; break;
502N/A case JVM_OPC_lload:
0N/A type = ITEM_Long; double_word = JNI_TRUE; break;
502N/A case JVM_OPC_dload:
0N/A type = ITEM_Double; double_word = JNI_TRUE; break;
0N/A }
0N/A if (!double_word) {
0N/A fullinfo_type reg;
0N/A /* Make sure we don't have an illegal register or one with wrong type */
0N/A if (operand >= register_count) {
0N/A CCerror(context,
0N/A "Accessing value from uninitialized register %d", operand);
0N/A }
0N/A reg = registers[operand];
0N/A
502N/A if (WITH_ZERO_EXTRA_INFO(reg) == (unsigned)MAKE_FULLINFO(type, 0, 0)) {
0N/A /* the register is obviously of the given type */
0N/A return;
0N/A } else if (GET_INDIRECTION(reg) > 0 && type == ITEM_Object) {
0N/A /* address type stuff be used on all arrays */
0N/A return;
0N/A } else if (GET_ITEM_TYPE(reg) == ITEM_ReturnAddress) {
0N/A CCerror(context, "Cannot load return address from register %d",
0N/A operand);
0N/A /* alternatively
0N/A (GET_ITEM_TYPE(reg) == ITEM_ReturnAddress)
502N/A && (opcode == JVM_OPC_iload)
0N/A && (type == ITEM_Object || type == ITEM_Integer)
0N/A but this never occurs
0N/A */
0N/A } else if (reg == ITEM_InitObject && type == ITEM_Object) {
0N/A return;
0N/A } else if (WITH_ZERO_EXTRA_INFO(reg) ==
0N/A MAKE_FULLINFO(ITEM_NewObject, 0, 0) &&
0N/A type == ITEM_Object) {
0N/A return;
0N/A } else {
0N/A CCerror(context, "Register %d contains wrong type", operand);
0N/A }
0N/A } else {
0N/A /* Make sure we don't have an illegal register or one with wrong type */
0N/A if ((operand + 1) >= register_count) {
0N/A CCerror(context,
0N/A "Accessing value from uninitialized register pair %d/%d",
0N/A operand, operand+1);
0N/A } else {
502N/A if ((registers[operand] == (unsigned)MAKE_FULLINFO(type, 0, 0)) &&
502N/A (registers[operand + 1] == (unsigned)MAKE_FULLINFO(type + 1, 0, 0))) {
0N/A return;
0N/A } else {
0N/A CCerror(context, "Register pair %d/%d contains wrong type",
0N/A operand, operand+1);
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A/* Make sure the flags contain legitimate values for this instruction.
0N/A*/
0N/A
0N/Astatic void
0N/Acheck_flags(context_type *context, unsigned int inumber)
0N/A{
0N/A instruction_data_type *idata = context->instruction_data;
0N/A instruction_data_type *this_idata = &idata[inumber];
502N/A int opcode = this_idata->opcode;
0N/A switch (opcode) {
502N/A case JVM_OPC_return:
0N/A /* We need a constructor, but we aren't guaranteed it's called */
0N/A if ((this_idata->or_flags & FLAG_NEED_CONSTRUCTOR) &&
0N/A !(this_idata->and_flags & FLAG_CONSTRUCTED))
0N/A CCerror(context, "Constructor must call super() or this()");
0N/A /* fall through */
502N/A case JVM_OPC_ireturn: case JVM_OPC_lreturn:
502N/A case JVM_OPC_freturn: case JVM_OPC_dreturn: case JVM_OPC_areturn:
0N/A if (this_idata->or_flags & FLAG_NO_RETURN)
0N/A /* This method cannot exit normally */
0N/A CCerror(context, "Cannot return normally");
0N/A default:
0N/A break; /* nothing to do. */
0N/A }
0N/A}
0N/A
0N/A/* Make sure that the top of the stack contains reasonable values for the
0N/A * given instruction. The post-pop values of the stack and its size are
0N/A * returned in *new_stack_info.
0N/A */
0N/A
0N/Astatic void
0N/Apop_stack(context_type *context, unsigned int inumber, stack_info_type *new_stack_info)
0N/A{
0N/A instruction_data_type *idata = context->instruction_data;
0N/A instruction_data_type *this_idata = &idata[inumber];
502N/A int opcode = this_idata->opcode;
0N/A stack_item_type *stack = this_idata->stack_info.stack;
0N/A int stack_size = this_idata->stack_info.stack_size;
0N/A char *stack_operands, *p;
0N/A char buffer[257]; /* for holding manufactured argument lists */
0N/A fullinfo_type stack_extra_info_buffer[256]; /* save info popped off stack */
0N/A fullinfo_type *stack_extra_info = &stack_extra_info_buffer[256];
0N/A fullinfo_type full_info; /* only used in case of invoke instructions */
502N/A fullinfo_type put_full_info; /* only used in case JVM_OPC_putstatic and JVM_OPC_putfield */
0N/A
0N/A switch(opcode) {
0N/A default:
0N/A /* For most instructions, we just use a built-in table */
0N/A stack_operands = opcode_in_out[opcode][0];
0N/A break;
0N/A
502N/A case JVM_OPC_putstatic: case JVM_OPC_putfield: {
0N/A /* The top thing on the stack depends on the signature of
0N/A * the object. */
0N/A int operand = this_idata->operand.i;
0N/A const char *signature =
0N/A JVM_GetCPFieldSignatureUTF(context->env,
0N/A context->class,
0N/A operand);
0N/A char *ip = buffer;
0N/A check_and_push(context, signature, VM_STRING_UTF);
0N/A#ifdef DEBUG
0N/A if (verify_verbose) {
0N/A print_formatted_fieldname(context, operand);
0N/A }
0N/A#endif
502N/A if (opcode == JVM_OPC_putfield)
0N/A *ip++ = 'A'; /* object for putfield */
0N/A *ip++ = signature_to_fieldtype(context, &signature, &put_full_info);
0N/A *ip = '\0';
0N/A stack_operands = buffer;
0N/A pop_and_free(context);
0N/A break;
0N/A }
0N/A
502N/A case JVM_OPC_invokevirtual: case JVM_OPC_invokespecial:
502N/A case JVM_OPC_invokeinit: /* invokespecial call to <init> */
502N/A case JVM_OPC_invokestatic: case JVM_OPC_invokeinterface: {
0N/A /* The top stuff on the stack depends on the method signature */
0N/A int operand = this_idata->operand.i;
0N/A const char *signature =
0N/A JVM_GetCPMethodSignatureUTF(context->env,
0N/A context->class,
0N/A operand);
0N/A char *ip = buffer;
0N/A const char *p;
0N/A check_and_push(context, signature, VM_STRING_UTF);
0N/A#ifdef DEBUG
0N/A if (verify_verbose) {
0N/A print_formatted_methodname(context, operand);
0N/A }
0N/A#endif
502N/A if (opcode != JVM_OPC_invokestatic)
0N/A /* First, push the object */
502N/A *ip++ = (opcode == JVM_OPC_invokeinit ? '@' : 'A');
0N/A for (p = signature + 1; *p != JVM_SIGNATURE_ENDFUNC; ) {
0N/A *ip++ = signature_to_fieldtype(context, &p, &full_info);
0N/A if (ip >= buffer + sizeof(buffer) - 1)
0N/A CCerror(context, "Signature %s has too many arguments",
0N/A signature);
0N/A }
0N/A *ip = 0;
0N/A stack_operands = buffer;
0N/A pop_and_free(context);
0N/A break;
0N/A }
0N/A
502N/A case JVM_OPC_multianewarray: {
0N/A /* Count can't be larger than 255. So can't overflow buffer */
0N/A int count = this_idata->operand2.i; /* number of ints on stack */
0N/A memset(buffer, 'I', count);
0N/A buffer[count] = '\0';
0N/A stack_operands = buffer;
0N/A break;
0N/A }
0N/A
0N/A } /* of switch */
0N/A
0N/A /* Run through the list of operands >>backwards<< */
0N/A for ( p = stack_operands + strlen(stack_operands);
0N/A p > stack_operands;
0N/A stack = stack->next) {
0N/A int type = *--p;
0N/A fullinfo_type top_type = stack ? stack->item : 0;
0N/A int size = (type == 'D' || type == 'L') ? 2 : 1;
0N/A *--stack_extra_info = top_type;
0N/A if (stack == NULL)
0N/A CCerror(context, "Unable to pop operand off an empty stack");
0N/A
0N/A switch (type) {
0N/A case 'I':
0N/A if (top_type != MAKE_FULLINFO(ITEM_Integer, 0, 0))
0N/A CCerror(context, "Expecting to find integer on stack");
0N/A break;
0N/A
0N/A case 'F':
0N/A if (top_type != MAKE_FULLINFO(ITEM_Float, 0, 0))
0N/A CCerror(context, "Expecting to find float on stack");
0N/A break;
0N/A
0N/A case 'A': /* object or array */
0N/A if ( (GET_ITEM_TYPE(top_type) != ITEM_Object)
0N/A && (GET_INDIRECTION(top_type) == 0)) {
0N/A /* The thing isn't an object or an array. Let's see if it's
0N/A * one of the special cases */
0N/A if ( (WITH_ZERO_EXTRA_INFO(top_type) ==
0N/A MAKE_FULLINFO(ITEM_ReturnAddress, 0, 0))
502N/A && (opcode == JVM_OPC_astore))
0N/A break;
0N/A if ( (GET_ITEM_TYPE(top_type) == ITEM_NewObject
0N/A || (GET_ITEM_TYPE(top_type) == ITEM_InitObject))
502N/A && ((opcode == JVM_OPC_astore) || (opcode == JVM_OPC_aload)
502N/A || (opcode == JVM_OPC_ifnull) || (opcode == JVM_OPC_ifnonnull)))
0N/A break;
0N/A /* The 2nd edition VM of the specification allows field
0N/A * initializations before the superclass initializer,
0N/A * if the field is defined within the current class.
0N/A */
0N/A if ( (GET_ITEM_TYPE(top_type) == ITEM_InitObject)
502N/A && (opcode == JVM_OPC_putfield)) {
0N/A int operand = this_idata->operand.i;
0N/A int access_bits = JVM_GetCPFieldModifiers(context->env,
0N/A context->class,
0N/A operand,
0N/A context->class);
0N/A /* Note: This relies on the fact that
0N/A * JVM_GetCPFieldModifiers retrieves only local fields,
0N/A * and does not respect inheritance.
0N/A */
0N/A if (access_bits != -1) {
0N/A if ( cp_index_to_class_fullinfo(context, operand, JVM_CONSTANT_Fieldref) ==
0N/A context->currentclass_info ) {
0N/A top_type = context->currentclass_info;
0N/A *stack_extra_info = top_type;
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A CCerror(context, "Expecting to find object/array on stack");
0N/A }
0N/A break;
0N/A
0N/A case '@': { /* unitialized object, for call to <init> */
0N/A int item_type = GET_ITEM_TYPE(top_type);
0N/A if (item_type != ITEM_NewObject && item_type != ITEM_InitObject)
0N/A CCerror(context,
0N/A "Expecting to find unitialized object on stack");
0N/A break;
0N/A }
0N/A
0N/A case 'O': /* object, not array */
0N/A if (WITH_ZERO_EXTRA_INFO(top_type) !=
0N/A MAKE_FULLINFO(ITEM_Object, 0, 0))
0N/A CCerror(context, "Expecting to find object on stack");
0N/A break;
0N/A
0N/A case 'a': /* integer, object, or array */
0N/A if ( (top_type != MAKE_FULLINFO(ITEM_Integer, 0, 0))
0N/A && (GET_ITEM_TYPE(top_type) != ITEM_Object)
0N/A && (GET_INDIRECTION(top_type) == 0))
0N/A CCerror(context,
0N/A "Expecting to find object, array, or int on stack");
0N/A break;
0N/A
0N/A case 'D': /* double */
0N/A if (top_type != MAKE_FULLINFO(ITEM_Double, 0, 0))
0N/A CCerror(context, "Expecting to find double on stack");
0N/A break;
0N/A
0N/A case 'L': /* long */
0N/A if (top_type != MAKE_FULLINFO(ITEM_Long, 0, 0))
0N/A CCerror(context, "Expecting to find long on stack");
0N/A break;
0N/A
0N/A case ']': /* array of some type */
0N/A if (top_type == NULL_FULLINFO) {
0N/A /* do nothing */
0N/A } else switch(p[-1]) {
0N/A case 'I': /* array of integers */
0N/A if (top_type != MAKE_FULLINFO(ITEM_Integer, 1, 0) &&
0N/A top_type != NULL_FULLINFO)
0N/A CCerror(context,
0N/A "Expecting to find array of ints on stack");
0N/A break;
0N/A
0N/A case 'L': /* array of longs */
0N/A if (top_type != MAKE_FULLINFO(ITEM_Long, 1, 0))
0N/A CCerror(context,
0N/A "Expecting to find array of longs on stack");
0N/A break;
0N/A
0N/A case 'F': /* array of floats */
0N/A if (top_type != MAKE_FULLINFO(ITEM_Float, 1, 0))
0N/A CCerror(context,
0N/A "Expecting to find array of floats on stack");
0N/A break;
0N/A
0N/A case 'D': /* array of doubles */
0N/A if (top_type != MAKE_FULLINFO(ITEM_Double, 1, 0))
0N/A CCerror(context,
0N/A "Expecting to find array of doubles on stack");
0N/A break;
0N/A
0N/A case 'A': { /* array of addresses (arrays or objects) */
0N/A int indirection = GET_INDIRECTION(top_type);
0N/A if ((indirection == 0) ||
0N/A ((indirection == 1) &&
0N/A (GET_ITEM_TYPE(top_type) != ITEM_Object)))
0N/A CCerror(context,
0N/A "Expecting to find array of objects or arrays "
0N/A "on stack");
0N/A break;
0N/A }
0N/A
0N/A case 'B': /* array of bytes */
0N/A if (top_type != MAKE_FULLINFO(ITEM_Byte, 1, 0))
0N/A CCerror(context,
0N/A "Expecting to find array of bytes on stack");
0N/A break;
0N/A
0N/A case 'C': /* array of characters */
0N/A if (top_type != MAKE_FULLINFO(ITEM_Char, 1, 0))
0N/A CCerror(context,
0N/A "Expecting to find array of chars on stack");
0N/A break;
0N/A
0N/A case 'S': /* array of shorts */
0N/A if (top_type != MAKE_FULLINFO(ITEM_Short, 1, 0))
0N/A CCerror(context,
0N/A "Expecting to find array of shorts on stack");
0N/A break;
0N/A
0N/A case '?': /* any type of array is okay */
0N/A if (GET_INDIRECTION(top_type) == 0)
0N/A CCerror(context,
0N/A "Expecting to find array on stack");
0N/A break;
0N/A
0N/A default:
0N/A CCerror(context, "Internal error #1");
0N/A break;
0N/A }
0N/A p -= 2; /* skip over [ <char> */
0N/A break;
0N/A
0N/A case '1': case '2': case '3': case '4': /* stack swapping */
0N/A if (top_type == MAKE_FULLINFO(ITEM_Double, 0, 0)
0N/A || top_type == MAKE_FULLINFO(ITEM_Long, 0, 0)) {
0N/A if ((p > stack_operands) && (p[-1] == '+')) {
0N/A context->swap_table[type - '1'] = top_type + 1;
0N/A context->swap_table[p[-2] - '1'] = top_type;
0N/A size = 2;
0N/A p -= 2;
0N/A } else {
0N/A CCerror(context,
0N/A "Attempt to split long or double on the stack");
0N/A }
0N/A } else {
0N/A context->swap_table[type - '1'] = stack->item;
0N/A if ((p > stack_operands) && (p[-1] == '+'))
0N/A p--; /* ignore */
0N/A }
0N/A break;
0N/A case '+': /* these should have been caught. */
0N/A default:
0N/A CCerror(context, "Internal error #2");
0N/A }
0N/A stack_size -= size;
0N/A }
0N/A
0N/A /* For many of the opcodes that had an "A" in their field, we really
0N/A * need to go back and do a little bit more accurate testing. We can, of
0N/A * course, assume that the minimal type checking has already been done.
0N/A */
0N/A switch (opcode) {
0N/A default: break;
502N/A case JVM_OPC_aastore: { /* array index object */
0N/A fullinfo_type array_type = stack_extra_info[0];
0N/A fullinfo_type object_type = stack_extra_info[2];
0N/A fullinfo_type target_type = decrement_indirection(array_type);
0N/A if ((GET_ITEM_TYPE(object_type) != ITEM_Object)
0N/A && (GET_INDIRECTION(object_type) == 0)) {
0N/A CCerror(context, "Expecting reference type on operand stack in aastore");
0N/A }
0N/A if ((GET_ITEM_TYPE(target_type) != ITEM_Object)
0N/A && (GET_INDIRECTION(target_type) == 0)) {
0N/A CCerror(context, "Component type of the array must be reference type in aastore");
0N/A }
0N/A break;
0N/A }
0N/A
502N/A case JVM_OPC_putfield:
502N/A case JVM_OPC_getfield:
502N/A case JVM_OPC_putstatic: {
0N/A int operand = this_idata->operand.i;
0N/A fullinfo_type stack_object = stack_extra_info[0];
502N/A if (opcode == JVM_OPC_putfield || opcode == JVM_OPC_getfield) {
0N/A if (!isAssignableTo
0N/A (context,
0N/A stack_object,
0N/A cp_index_to_class_fullinfo
0N/A (context, operand, JVM_CONSTANT_Fieldref))) {
0N/A CCerror(context,
0N/A "Incompatible type for getting or setting field");
0N/A }
0N/A if (this_idata->protected &&
0N/A !isAssignableTo(context, stack_object,
0N/A context->currentclass_info)) {
0N/A CCerror(context, "Bad access to protected data");
0N/A }
0N/A }
502N/A if (opcode == JVM_OPC_putfield || opcode == JVM_OPC_putstatic) {
502N/A int item = (opcode == JVM_OPC_putfield ? 1 : 0);
0N/A if (!isAssignableTo(context,
0N/A stack_extra_info[item], put_full_info)) {
0N/A CCerror(context, "Bad type in putfield/putstatic");
0N/A }
0N/A }
0N/A break;
0N/A }
0N/A
502N/A case JVM_OPC_athrow:
0N/A if (!isAssignableTo(context, stack_extra_info[0],
0N/A context->throwable_info)) {
0N/A CCerror(context, "Can only throw Throwable objects");
0N/A }
0N/A break;
0N/A
502N/A case JVM_OPC_aaload: { /* array index */
0N/A /* We need to pass the information to the stack updater */
0N/A fullinfo_type array_type = stack_extra_info[0];
0N/A context->swap_table[0] = decrement_indirection(array_type);
0N/A break;
0N/A }
0N/A
502N/A case JVM_OPC_invokevirtual: case JVM_OPC_invokespecial:
502N/A case JVM_OPC_invokeinit:
502N/A case JVM_OPC_invokeinterface: case JVM_OPC_invokestatic: {
0N/A int operand = this_idata->operand.i;
0N/A const char *signature =
0N/A JVM_GetCPMethodSignatureUTF(context->env,
0N/A context->class,
0N/A operand);
0N/A int item;
0N/A const char *p;
0N/A check_and_push(context, signature, VM_STRING_UTF);
502N/A if (opcode == JVM_OPC_invokestatic) {
0N/A item = 0;
502N/A } else if (opcode == JVM_OPC_invokeinit) {
0N/A fullinfo_type init_type = this_idata->operand2.fi;
0N/A fullinfo_type object_type = stack_extra_info[0];
0N/A context->swap_table[0] = object_type; /* save value */
0N/A if (GET_ITEM_TYPE(stack_extra_info[0]) == ITEM_NewObject) {
0N/A /* We better be calling the appropriate init. Find the
502N/A * inumber of the "JVM_OPC_new" instruction", and figure
0N/A * out what the type really is.
0N/A */
0N/A unsigned int new_inumber = GET_EXTRA_INFO(stack_extra_info[0]);
0N/A fullinfo_type target_type = idata[new_inumber].operand2.fi;
0N/A context->swap_table[1] = target_type;
0N/A
0N/A if (target_type != init_type) {
0N/A CCerror(context, "Call to wrong initialization method");
0N/A }
0N/A if (this_idata->protected
0N/A && context->major_version > LDC_CLASS_MAJOR_VERSION
0N/A && !isAssignableTo(context, object_type,
0N/A context->currentclass_info)) {
0N/A CCerror(context, "Bad access to protected data");
0N/A }
0N/A } else {
0N/A /* We better be calling super() or this(). */
0N/A if (init_type != context->superclass_info &&
0N/A init_type != context->currentclass_info) {
0N/A CCerror(context, "Call to wrong initialization method");
0N/A }
0N/A context->swap_table[1] = context->currentclass_info;
0N/A }
0N/A item = 1;
0N/A } else {
0N/A fullinfo_type target_type = this_idata->operand2.fi;
0N/A fullinfo_type object_type = stack_extra_info[0];
0N/A if (!isAssignableTo(context, object_type, target_type)){
0N/A CCerror(context,
0N/A "Incompatible object argument for function call");
0N/A }
502N/A if (opcode == JVM_OPC_invokespecial
0N/A && !isAssignableTo(context, object_type,
0N/A context->currentclass_info)) {
0N/A /* Make sure object argument is assignment compatible to current class */
0N/A CCerror(context,
0N/A "Incompatible object argument for invokespecial");
0N/A }
0N/A if (this_idata->protected
0N/A && !isAssignableTo(context, object_type,
0N/A context->currentclass_info)) {
0N/A /* This is ugly. Special dispensation. Arrays pretend to
0N/A implement public Object clone() even though they don't */
0N/A const char *utfName =
0N/A JVM_GetCPMethodNameUTF(context->env,
0N/A context->class,
0N/A this_idata->operand.i);
0N/A int is_clone = utfName && (strcmp(utfName, "clone") == 0);
0N/A JVM_ReleaseUTF(utfName);
0N/A
0N/A if ((target_type == context->object_info) &&
0N/A (GET_INDIRECTION(object_type) > 0) &&
0N/A is_clone) {
0N/A } else {
0N/A CCerror(context, "Bad access to protected data");
0N/A }
0N/A }
0N/A item = 1;
0N/A }
0N/A for (p = signature + 1; *p != JVM_SIGNATURE_ENDFUNC; item++)
0N/A if (signature_to_fieldtype(context, &p, &full_info) == 'A') {
0N/A if (!isAssignableTo(context,
0N/A stack_extra_info[item], full_info)) {
0N/A CCerror(context, "Incompatible argument to function");
0N/A }
0N/A }
0N/A
0N/A pop_and_free(context);
0N/A break;
0N/A }
0N/A
502N/A case JVM_OPC_return:
0N/A if (context->return_type != MAKE_FULLINFO(ITEM_Void, 0, 0))
0N/A CCerror(context, "Wrong return type in function");
0N/A break;
0N/A
502N/A case JVM_OPC_ireturn: case JVM_OPC_lreturn: case JVM_OPC_freturn:
502N/A case JVM_OPC_dreturn: case JVM_OPC_areturn: {
0N/A fullinfo_type target_type = context->return_type;
0N/A fullinfo_type object_type = stack_extra_info[0];
0N/A if (!isAssignableTo(context, object_type, target_type)) {
0N/A CCerror(context, "Wrong return type in function");
0N/A }
0N/A break;
0N/A }
0N/A
502N/A case JVM_OPC_new: {
0N/A /* Make sure that nothing on the stack already looks like what
0N/A * we want to create. I can't image how this could possibly happen
0N/A * but we should test for it anyway, since if it could happen, the
0N/A * result would be an unitialized object being able to masquerade
0N/A * as an initialized one.
0N/A */
0N/A stack_item_type *item;
0N/A for (item = stack; item != NULL; item = item->next) {
0N/A if (item->item == this_idata->operand.fi) {
0N/A CCerror(context,
0N/A "Uninitialized object on stack at creating point");
0N/A }
0N/A }
0N/A /* Info for update_registers */
0N/A context->swap_table[0] = this_idata->operand.fi;
0N/A context->swap_table[1] = MAKE_FULLINFO(ITEM_Bogus, 0, 0);
0N/A
0N/A break;
0N/A }
0N/A }
0N/A new_stack_info->stack = stack;
0N/A new_stack_info->stack_size = stack_size;
0N/A}
0N/A
0N/A
0N/A/* We've already determined that the instruction is legal. Perform the
0N/A * operation on the registers, and return the updated results in
0N/A * new_register_count_p and new_registers.
0N/A */
0N/A
0N/Astatic void
0N/Aupdate_registers(context_type *context, unsigned int inumber,
0N/A register_info_type *new_register_info)
0N/A{
0N/A instruction_data_type *idata = context->instruction_data;
0N/A instruction_data_type *this_idata = &idata[inumber];
502N/A int opcode = this_idata->opcode;
0N/A int operand = this_idata->operand.i;
0N/A int register_count = this_idata->register_info.register_count;
0N/A fullinfo_type *registers = this_idata->register_info.registers;
0N/A stack_item_type *stack = this_idata->stack_info.stack;
0N/A int mask_count = this_idata->register_info.mask_count;
0N/A mask_type *masks = this_idata->register_info.masks;
0N/A
0N/A /* Use these as default new values. */
0N/A int new_register_count = register_count;
0N/A int new_mask_count = mask_count;
0N/A fullinfo_type *new_registers = registers;
0N/A mask_type *new_masks = masks;
0N/A
0N/A enum { ACCESS_NONE, ACCESS_SINGLE, ACCESS_DOUBLE } access = ACCESS_NONE;
0N/A int i;
0N/A
0N/A /* Remember, we've already verified the type at the top of the stack. */
0N/A switch (opcode) {
0N/A default: break;
502N/A case JVM_OPC_istore: case JVM_OPC_fstore: case JVM_OPC_astore:
0N/A access = ACCESS_SINGLE;
0N/A goto continue_store;
0N/A
502N/A case JVM_OPC_lstore: case JVM_OPC_dstore:
0N/A access = ACCESS_DOUBLE;
0N/A goto continue_store;
0N/A
0N/A continue_store: {
0N/A /* We have a modification to the registers. Copy them if needed. */
0N/A fullinfo_type stack_top_type = stack->item;
0N/A int max_operand = operand + ((access == ACCESS_DOUBLE) ? 1 : 0);
0N/A
0N/A if ( max_operand < register_count
0N/A && registers[operand] == stack_top_type
0N/A && ((access == ACCESS_SINGLE) ||
0N/A (registers[operand + 1]== stack_top_type + 1)))
0N/A /* No changes have been made to the registers. */
0N/A break;
0N/A new_register_count = MAX(max_operand + 1, register_count);
0N/A new_registers = NEW(fullinfo_type, new_register_count);
0N/A for (i = 0; i < register_count; i++)
0N/A new_registers[i] = registers[i];
0N/A for (i = register_count; i < new_register_count; i++)
0N/A new_registers[i] = MAKE_FULLINFO(ITEM_Bogus, 0, 0);
0N/A new_registers[operand] = stack_top_type;
0N/A if (access == ACCESS_DOUBLE)
0N/A new_registers[operand + 1] = stack_top_type + 1;
0N/A break;
0N/A }
0N/A
502N/A case JVM_OPC_iload: case JVM_OPC_fload: case JVM_OPC_aload:
502N/A case JVM_OPC_iinc: case JVM_OPC_ret:
0N/A access = ACCESS_SINGLE;
0N/A break;
0N/A
502N/A case JVM_OPC_lload: case JVM_OPC_dload:
0N/A access = ACCESS_DOUBLE;
0N/A break;
0N/A
502N/A case JVM_OPC_jsr: case JVM_OPC_jsr_w:
0N/A for (i = 0; i < new_mask_count; i++)
0N/A if (new_masks[i].entry == operand)
0N/A CCerror(context, "Recursive call to jsr entry");
0N/A new_masks = add_to_masks(context, masks, mask_count, operand);
0N/A new_mask_count++;
0N/A break;
0N/A
502N/A case JVM_OPC_invokeinit:
502N/A case JVM_OPC_new: {
0N/A /* For invokeinit, an uninitialized object has been initialized.
0N/A * For new, all previous occurrences of an uninitialized object
0N/A * from the same instruction must be made bogus.
0N/A * We find all occurrences of swap_table[0] in the registers, and
0N/A * replace them with swap_table[1];
0N/A */
0N/A fullinfo_type from = context->swap_table[0];
0N/A fullinfo_type to = context->swap_table[1];
0N/A
0N/A int i;
0N/A for (i = 0; i < register_count; i++) {
0N/A if (new_registers[i] == from) {
0N/A /* Found a match */
0N/A break;
0N/A }
0N/A }
0N/A if (i < register_count) { /* We broke out loop for match */
0N/A /* We have to change registers, and possibly a mask */
0N/A jboolean copied_mask = JNI_FALSE;
0N/A int k;
0N/A new_registers = NEW(fullinfo_type, register_count);
0N/A memcpy(new_registers, registers,
0N/A register_count * sizeof(registers[0]));
0N/A for ( ; i < register_count; i++) {
0N/A if (new_registers[i] == from) {
0N/A new_registers[i] = to;
0N/A for (k = 0; k < new_mask_count; k++) {
0N/A if (!IS_BIT_SET(new_masks[k].modifies, i)) {
0N/A if (!copied_mask) {
0N/A new_masks = copy_masks(context, new_masks,
0N/A mask_count);
0N/A copied_mask = JNI_TRUE;
0N/A }
0N/A SET_BIT(new_masks[k].modifies, i);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A break;
0N/A }
0N/A } /* of switch */
0N/A
0N/A if ((access != ACCESS_NONE) && (new_mask_count > 0)) {
0N/A int i, j;
0N/A for (i = 0; i < new_mask_count; i++) {
0N/A int *mask = new_masks[i].modifies;
0N/A if ((!IS_BIT_SET(mask, operand)) ||
0N/A ((access == ACCESS_DOUBLE) &&
0N/A !IS_BIT_SET(mask, operand + 1))) {
0N/A new_masks = copy_masks(context, new_masks, mask_count);
0N/A for (j = i; j < new_mask_count; j++) {
0N/A SET_BIT(new_masks[j].modifies, operand);
0N/A if (access == ACCESS_DOUBLE)
0N/A SET_BIT(new_masks[j].modifies, operand + 1);
0N/A }
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A
0N/A new_register_info->register_count = new_register_count;
0N/A new_register_info->registers = new_registers;
0N/A new_register_info->masks = new_masks;
0N/A new_register_info->mask_count = new_mask_count;
0N/A}
0N/A
0N/A
0N/A
0N/A/* We've already determined that the instruction is legal, and have updated
0N/A * the registers. Update the flags, too.
0N/A */
0N/A
0N/A
0N/Astatic void
0N/Aupdate_flags(context_type *context, unsigned int inumber,
0N/A flag_type *new_and_flags, flag_type *new_or_flags)
0N/A
0N/A{
0N/A instruction_data_type *idata = context->instruction_data;
0N/A instruction_data_type *this_idata = &idata[inumber];
0N/A flag_type and_flags = this_idata->and_flags;
0N/A flag_type or_flags = this_idata->or_flags;
0N/A
0N/A /* Set the "we've done a constructor" flag */
502N/A if (this_idata->opcode == JVM_OPC_invokeinit) {
0N/A fullinfo_type from = context->swap_table[0];
0N/A if (from == MAKE_FULLINFO(ITEM_InitObject, 0, 0))
0N/A and_flags |= FLAG_CONSTRUCTED;
0N/A }
0N/A *new_and_flags = and_flags;
0N/A *new_or_flags = or_flags;
0N/A}
0N/A
0N/A
0N/A
0N/A/* We've already determined that the instruction is legal. Perform the
0N/A * operation on the stack;
0N/A *
0N/A * new_stack_size_p and new_stack_p point to the results after the pops have
0N/A * already been done. Do the pushes, and then put the results back there.
0N/A */
0N/A
0N/Astatic void
0N/Apush_stack(context_type *context, unsigned int inumber, stack_info_type *new_stack_info)
0N/A{
0N/A instruction_data_type *idata = context->instruction_data;
0N/A instruction_data_type *this_idata = &idata[inumber];
502N/A int opcode = this_idata->opcode;
0N/A int operand = this_idata->operand.i;
0N/A
0N/A int stack_size = new_stack_info->stack_size;
0N/A stack_item_type *stack = new_stack_info->stack;
0N/A char *stack_results;
0N/A
0N/A fullinfo_type full_info = 0;
0N/A char buffer[5], *p; /* actually [2] is big enough */
0N/A
0N/A /* We need to look at all those opcodes in which either we can't tell the
0N/A * value pushed onto the stack from the opcode, or in which the value
0N/A * pushed onto the stack is an object or array. For the latter, we need
0N/A * to make sure that full_info is set to the right value.
0N/A */
0N/A switch(opcode) {
0N/A default:
0N/A stack_results = opcode_in_out[opcode][1];
0N/A break;
0N/A
502N/A case JVM_OPC_ldc: case JVM_OPC_ldc_w: case JVM_OPC_ldc2_w: {
0N/A /* Look to constant pool to determine correct result. */
0N/A unsigned char *type_table = context->constant_types;
0N/A switch (type_table[operand]) {
0N/A case JVM_CONSTANT_Integer:
0N/A stack_results = "I"; break;
0N/A case JVM_CONSTANT_Float:
0N/A stack_results = "F"; break;
0N/A case JVM_CONSTANT_Double:
0N/A stack_results = "D"; break;
0N/A case JVM_CONSTANT_Long:
0N/A stack_results = "L"; break;
0N/A case JVM_CONSTANT_String:
0N/A stack_results = "A";
0N/A full_info = context->string_info;
0N/A break;
0N/A case JVM_CONSTANT_Class:
0N/A if (context->major_version < LDC_CLASS_MAJOR_VERSION)
0N/A CCerror(context, "Internal error #3");
0N/A stack_results = "A";
0N/A full_info = make_class_info_from_name(context,
0N/A "java/lang/Class");
0N/A break;
0N/A default:
0N/A CCerror(context, "Internal error #3");
0N/A stack_results = ""; /* Never reached: keep lint happy */
0N/A }
0N/A break;
0N/A }
0N/A
502N/A case JVM_OPC_getstatic: case JVM_OPC_getfield: {
0N/A /* Look to signature to determine correct result. */
0N/A int operand = this_idata->operand.i;
0N/A const char *signature = JVM_GetCPFieldSignatureUTF(context->env,
0N/A context->class,
0N/A operand);
0N/A check_and_push(context, signature, VM_STRING_UTF);
0N/A#ifdef DEBUG
0N/A if (verify_verbose) {
0N/A print_formatted_fieldname(context, operand);
0N/A }
0N/A#endif
0N/A buffer[0] = signature_to_fieldtype(context, &signature, &full_info);
0N/A buffer[1] = '\0';
0N/A stack_results = buffer;
0N/A pop_and_free(context);
0N/A break;
0N/A }
0N/A
502N/A case JVM_OPC_invokevirtual: case JVM_OPC_invokespecial:
502N/A case JVM_OPC_invokeinit:
502N/A case JVM_OPC_invokestatic: case JVM_OPC_invokeinterface: {
0N/A /* Look to signature to determine correct result. */
0N/A int operand = this_idata->operand.i;
0N/A const char *signature = JVM_GetCPMethodSignatureUTF(context->env,
0N/A context->class,
0N/A operand);
0N/A const char *result_signature;
0N/A check_and_push(context, signature, VM_STRING_UTF);
0N/A result_signature = strchr(signature, JVM_SIGNATURE_ENDFUNC) + 1;
0N/A if (result_signature[0] == JVM_SIGNATURE_VOID) {
0N/A stack_results = "";
0N/A } else {
0N/A buffer[0] = signature_to_fieldtype(context, &result_signature,
0N/A &full_info);
0N/A buffer[1] = '\0';
0N/A stack_results = buffer;
0N/A }
0N/A pop_and_free(context);
0N/A break;
0N/A }
0N/A
502N/A case JVM_OPC_aconst_null:
0N/A stack_results = opcode_in_out[opcode][1];
0N/A full_info = NULL_FULLINFO; /* special NULL */
0N/A break;
0N/A
502N/A case JVM_OPC_new:
502N/A case JVM_OPC_checkcast:
502N/A case JVM_OPC_newarray:
502N/A case JVM_OPC_anewarray:
502N/A case JVM_OPC_multianewarray:
0N/A stack_results = opcode_in_out[opcode][1];
0N/A /* Conveniently, this result type is stored here */
0N/A full_info = this_idata->operand.fi;
0N/A break;
0N/A
502N/A case JVM_OPC_aaload:
0N/A stack_results = opcode_in_out[opcode][1];
0N/A /* pop_stack() saved value for us. */
0N/A full_info = context->swap_table[0];
0N/A break;
0N/A
502N/A case JVM_OPC_aload:
0N/A stack_results = opcode_in_out[opcode][1];
0N/A /* The register hasn't been modified, so we can use its value. */
0N/A full_info = this_idata->register_info.registers[operand];
0N/A break;
0N/A } /* of switch */
0N/A
0N/A for (p = stack_results; *p != 0; p++) {
0N/A int type = *p;
0N/A stack_item_type *new_item = NEW(stack_item_type, 1);
0N/A new_item->next = stack;
0N/A stack = new_item;
0N/A switch (type) {
0N/A case 'I':
0N/A stack->item = MAKE_FULLINFO(ITEM_Integer, 0, 0); break;
0N/A case 'F':
0N/A stack->item = MAKE_FULLINFO(ITEM_Float, 0, 0); break;
0N/A case 'D':
0N/A stack->item = MAKE_FULLINFO(ITEM_Double, 0, 0);
0N/A stack_size++; break;
0N/A case 'L':
0N/A stack->item = MAKE_FULLINFO(ITEM_Long, 0, 0);
0N/A stack_size++; break;
0N/A case 'R':
0N/A stack->item = MAKE_FULLINFO(ITEM_ReturnAddress, 0, operand);
0N/A break;
0N/A case '1': case '2': case '3': case '4': {
0N/A /* Get the info saved in the swap_table */
0N/A fullinfo_type stype = context->swap_table[type - '1'];
0N/A stack->item = stype;
0N/A if (stype == MAKE_FULLINFO(ITEM_Long, 0, 0) ||
0N/A stype == MAKE_FULLINFO(ITEM_Double, 0, 0)) {
0N/A stack_size++; p++;
0N/A }
0N/A break;
0N/A }
0N/A case 'A':
0N/A /* full_info should have the appropriate value. */
0N/A assert(full_info != 0);
0N/A stack->item = full_info;
0N/A break;
0N/A default:
0N/A CCerror(context, "Internal error #4");
0N/A
0N/A } /* switch type */
0N/A stack_size++;
0N/A } /* outer for loop */
0N/A
502N/A if (opcode == JVM_OPC_invokeinit) {
0N/A /* If there are any instances of "from" on the stack, we need to
0N/A * replace it with "to", since calling <init> initializes all versions
0N/A * of the object, obviously. */
0N/A fullinfo_type from = context->swap_table[0];
0N/A stack_item_type *ptr;
0N/A for (ptr = stack; ptr != NULL; ptr = ptr->next) {
0N/A if (ptr->item == from) {
0N/A fullinfo_type to = context->swap_table[1];
0N/A stack = copy_stack(context, stack);
0N/A for (ptr = stack; ptr != NULL; ptr = ptr->next)
0N/A if (ptr->item == from) ptr->item = to;
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A
0N/A new_stack_info->stack_size = stack_size;
0N/A new_stack_info->stack = stack;
0N/A}
0N/A
0N/A
0N/A/* We've performed an instruction, and determined the new registers and stack
0N/A * value. Look at all of the possibly subsequent instructions, and merge
0N/A * this stack value into theirs.
0N/A */
0N/A
0N/Astatic void
0N/Amerge_into_successors(context_type *context, unsigned int inumber,
0N/A register_info_type *register_info,
0N/A stack_info_type *stack_info,
0N/A flag_type and_flags, flag_type or_flags)
0N/A{
0N/A instruction_data_type *idata = context->instruction_data;
0N/A instruction_data_type *this_idata = &idata[inumber];
502N/A int opcode = this_idata->opcode;
0N/A int operand = this_idata->operand.i;
0N/A struct handler_info_type *handler_info = context->handler_info;
0N/A int handler_info_length =
0N/A JVM_GetMethodIxExceptionTableLength(context->env,
0N/A context->class,
0N/A context->method_index);
0N/A
0N/A
0N/A int buffer[2]; /* default value for successors */
0N/A int *successors = buffer; /* table of successors */
0N/A int successors_count;
0N/A int i;
0N/A
0N/A switch (opcode) {
0N/A default:
0N/A successors_count = 1;
0N/A buffer[0] = inumber + 1;
0N/A break;
0N/A
502N/A case JVM_OPC_ifeq: case JVM_OPC_ifne: case JVM_OPC_ifgt:
502N/A case JVM_OPC_ifge: case JVM_OPC_iflt: case JVM_OPC_ifle:
502N/A case JVM_OPC_ifnull: case JVM_OPC_ifnonnull:
502N/A case JVM_OPC_if_icmpeq: case JVM_OPC_if_icmpne: case JVM_OPC_if_icmpgt:
502N/A case JVM_OPC_if_icmpge: case JVM_OPC_if_icmplt: case JVM_OPC_if_icmple:
502N/A case JVM_OPC_if_acmpeq: case JVM_OPC_if_acmpne:
0N/A successors_count = 2;
0N/A buffer[0] = inumber + 1;
0N/A buffer[1] = operand;
0N/A break;
0N/A
502N/A case JVM_OPC_jsr: case JVM_OPC_jsr_w:
0N/A if (this_idata->operand2.i != UNKNOWN_RET_INSTRUCTION)
0N/A idata[this_idata->operand2.i].changed = JNI_TRUE;
0N/A /* FALLTHROUGH */
502N/A case JVM_OPC_goto: case JVM_OPC_goto_w:
0N/A successors_count = 1;
0N/A buffer[0] = operand;
0N/A break;
0N/A
0N/A
502N/A case JVM_OPC_ireturn: case JVM_OPC_lreturn: case JVM_OPC_return:
502N/A case JVM_OPC_freturn: case JVM_OPC_dreturn: case JVM_OPC_areturn:
502N/A case JVM_OPC_athrow:
0N/A /* The testing for the returns is handled in pop_stack() */
0N/A successors_count = 0;
0N/A break;
0N/A
502N/A case JVM_OPC_ret: {
0N/A /* This is slightly slow, but good enough for a seldom used instruction.
0N/A * The EXTRA_ITEM_INFO of the ITEM_ReturnAddress indicates the
0N/A * address of the first instruction of the subroutine. We can return
0N/A * to 1 after any instruction that jsr's to that instruction.
0N/A */
0N/A if (this_idata->operand2.ip == NULL) {
0N/A fullinfo_type *registers = this_idata->register_info.registers;
0N/A int called_instruction = GET_EXTRA_INFO(registers[operand]);
0N/A int i, count, *ptr;;
0N/A for (i = context->instruction_count, count = 0; --i >= 0; ) {
502N/A if (((idata[i].opcode == JVM_OPC_jsr) ||
502N/A (idata[i].opcode == JVM_OPC_jsr_w)) &&
0N/A (idata[i].operand.i == called_instruction))
0N/A count++;
0N/A }
0N/A this_idata->operand2.ip = ptr = NEW(int, count + 1);
0N/A *ptr++ = count;
0N/A for (i = context->instruction_count, count = 0; --i >= 0; ) {
502N/A if (((idata[i].opcode == JVM_OPC_jsr) ||
502N/A (idata[i].opcode == JVM_OPC_jsr_w)) &&
0N/A (idata[i].operand.i == called_instruction))
0N/A *ptr++ = i + 1;
0N/A }
0N/A }
0N/A successors = this_idata->operand2.ip; /* use this instead */
0N/A successors_count = *successors++;
0N/A break;
0N/A
0N/A }
0N/A
502N/A case JVM_OPC_tableswitch:
502N/A case JVM_OPC_lookupswitch:
0N/A successors = this_idata->operand.ip; /* use this instead */
0N/A successors_count = *successors++;
0N/A break;
0N/A }
0N/A
0N/A#ifdef DEBUG
0N/A if (verify_verbose) {
0N/A jio_fprintf(stdout, " [");
0N/A for (i = handler_info_length; --i >= 0; handler_info++)
0N/A if (handler_info->start <= inumber && handler_info->end > inumber)
0N/A jio_fprintf(stdout, "%d* ", handler_info->handler);
0N/A for (i = 0; i < successors_count; i++)
0N/A jio_fprintf(stdout, "%d ", successors[i]);
0N/A jio_fprintf(stdout, "]\n");
0N/A }
0N/A#endif
0N/A
0N/A handler_info = context->handler_info;
0N/A for (i = handler_info_length; --i >= 0; handler_info++) {
502N/A if (handler_info->start <= (int)inumber && handler_info->end > (int)inumber) {
0N/A int handler = handler_info->handler;
502N/A if (opcode != JVM_OPC_invokeinit) {
0N/A merge_into_one_successor(context, inumber, handler,
0N/A &this_idata->register_info, /* old */
0N/A &handler_info->stack_info,
0N/A (flag_type) (and_flags
0N/A & this_idata->and_flags),
0N/A (flag_type) (or_flags
0N/A | this_idata->or_flags),
0N/A JNI_TRUE);
0N/A } else {
0N/A /* We need to be a little bit more careful with this
0N/A * instruction. Things could either be in the state before
0N/A * the instruction or in the state afterwards */
0N/A fullinfo_type from = context->swap_table[0];
0N/A flag_type temp_or_flags = or_flags;
0N/A if (from == MAKE_FULLINFO(ITEM_InitObject, 0, 0))
0N/A temp_or_flags |= FLAG_NO_RETURN;
0N/A merge_into_one_successor(context, inumber, handler,
0N/A &this_idata->register_info, /* old */
0N/A &handler_info->stack_info,
0N/A this_idata->and_flags,
0N/A this_idata->or_flags,
0N/A JNI_TRUE);
0N/A merge_into_one_successor(context, inumber, handler,
0N/A register_info,
0N/A &handler_info->stack_info,
0N/A and_flags, temp_or_flags, JNI_TRUE);
0N/A }
0N/A }
0N/A }
0N/A for (i = 0; i < successors_count; i++) {
0N/A int target = successors[i];
0N/A if (target >= context->instruction_count)
0N/A CCerror(context, "Falling off the end of the code");
0N/A merge_into_one_successor(context, inumber, target,
0N/A register_info, stack_info, and_flags, or_flags,
0N/A JNI_FALSE);
0N/A }
0N/A}
0N/A
0N/A/* We have a new set of registers and stack values for a given instruction.
0N/A * Merge this new set into the values that are already there.
0N/A */
0N/A
0N/Astatic void
0N/Amerge_into_one_successor(context_type *context,
0N/A unsigned int from_inumber, unsigned int to_inumber,
0N/A register_info_type *new_register_info,
0N/A stack_info_type *new_stack_info,
0N/A flag_type new_and_flags, flag_type new_or_flags,
0N/A jboolean isException)
0N/A{
0N/A instruction_data_type *idata = context->instruction_data;
0N/A register_info_type register_info_buf;
0N/A stack_info_type stack_info_buf;
0N/A#ifdef DEBUG
0N/A instruction_data_type *this_idata = &idata[to_inumber];
0N/A register_info_type old_reg_info;
0N/A stack_info_type old_stack_info;
0N/A flag_type old_and_flags, old_or_flags;
0N/A#endif
0N/A
0N/A#ifdef DEBUG
0N/A if (verify_verbose) {
0N/A old_reg_info = this_idata->register_info;
0N/A old_stack_info = this_idata->stack_info;
0N/A old_and_flags = this_idata->and_flags;
0N/A old_or_flags = this_idata->or_flags;
0N/A }
0N/A#endif
0N/A
0N/A /* All uninitialized objects are set to "bogus" when jsr and
0N/A * ret are executed. Thus uninitialized objects can't propagate
0N/A * into or out of a subroutine.
0N/A */
502N/A if (idata[from_inumber].opcode == JVM_OPC_ret ||
502N/A idata[from_inumber].opcode == JVM_OPC_jsr ||
502N/A idata[from_inumber].opcode == JVM_OPC_jsr_w) {
0N/A int new_register_count = new_register_info->register_count;
0N/A fullinfo_type *new_registers = new_register_info->registers;
0N/A int i;
0N/A stack_item_type *item;
0N/A
0N/A for (item = new_stack_info->stack; item != NULL; item = item->next) {
0N/A if (GET_ITEM_TYPE(item->item) == ITEM_NewObject) {
0N/A /* This check only succeeds for hand-contrived code.
0N/A * Efficiency is not an issue.
0N/A */
0N/A stack_info_buf.stack = copy_stack(context,
0N/A new_stack_info->stack);
0N/A stack_info_buf.stack_size = new_stack_info->stack_size;
0N/A new_stack_info = &stack_info_buf;
0N/A for (item = new_stack_info->stack; item != NULL;
0N/A item = item->next) {
0N/A if (GET_ITEM_TYPE(item->item) == ITEM_NewObject) {
0N/A item->item = MAKE_FULLINFO(ITEM_Bogus, 0, 0);
0N/A }
0N/A }
0N/A break;
0N/A }
0N/A }
0N/A for (i = 0; i < new_register_count; i++) {
0N/A if (GET_ITEM_TYPE(new_registers[i]) == ITEM_NewObject) {
0N/A /* This check only succeeds for hand-contrived code.
0N/A * Efficiency is not an issue.
0N/A */
0N/A fullinfo_type *new_set = NEW(fullinfo_type,
0N/A new_register_count);
0N/A for (i = 0; i < new_register_count; i++) {
0N/A fullinfo_type t = new_registers[i];
0N/A new_set[i] = GET_ITEM_TYPE(t) != ITEM_NewObject ?
0N/A t : MAKE_FULLINFO(ITEM_Bogus, 0, 0);
0N/A }
0N/A register_info_buf.register_count = new_register_count;
0N/A register_info_buf.registers = new_set;
0N/A register_info_buf.mask_count = new_register_info->mask_count;
0N/A register_info_buf.masks = new_register_info->masks;
0N/A new_register_info = &register_info_buf;
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /* Returning from a subroutine is somewhat ugly. The actual thing
0N/A * that needs to get merged into the new instruction is a joining
0N/A * of info from the ret instruction with stuff in the jsr instruction
0N/A */
502N/A if (idata[from_inumber].opcode == JVM_OPC_ret && !isException) {
0N/A int new_register_count = new_register_info->register_count;
0N/A fullinfo_type *new_registers = new_register_info->registers;
0N/A int new_mask_count = new_register_info->mask_count;
0N/A mask_type *new_masks = new_register_info->masks;
0N/A int operand = idata[from_inumber].operand.i;
0N/A int called_instruction = GET_EXTRA_INFO(new_registers[operand]);
0N/A instruction_data_type *jsr_idata = &idata[to_inumber - 1];
0N/A register_info_type *jsr_reginfo = &jsr_idata->register_info;
502N/A if (jsr_idata->operand2.i != (int)from_inumber) {
0N/A if (jsr_idata->operand2.i != UNKNOWN_RET_INSTRUCTION)
0N/A CCerror(context, "Multiple returns to single jsr");
0N/A jsr_idata->operand2.i = from_inumber;
0N/A }
0N/A if (jsr_reginfo->register_count == UNKNOWN_REGISTER_COUNT) {
0N/A /* We don't want to handle the returned-to instruction until
0N/A * we've dealt with the jsr instruction. When we get to the
0N/A * jsr instruction (if ever), we'll re-mark the ret instruction
0N/A */
0N/A ;
0N/A } else {
0N/A int register_count = jsr_reginfo->register_count;
0N/A fullinfo_type *registers = jsr_reginfo->registers;
0N/A int max_registers = MAX(register_count, new_register_count);
0N/A fullinfo_type *new_set = NEW(fullinfo_type, max_registers);
0N/A int *return_mask;
0N/A struct register_info_type new_new_register_info;
0N/A int i;
0N/A /* Make sure the place we're returning from is legal! */
0N/A for (i = new_mask_count; --i >= 0; )
0N/A if (new_masks[i].entry == called_instruction)
0N/A break;
0N/A if (i < 0)
0N/A CCerror(context, "Illegal return from subroutine");
0N/A /* pop the masks down to the indicated one. Remember the mask
0N/A * we're popping off. */
0N/A return_mask = new_masks[i].modifies;
0N/A new_mask_count = i;
0N/A for (i = 0; i < max_registers; i++) {
0N/A if (IS_BIT_SET(return_mask, i))
0N/A new_set[i] = i < new_register_count ?
0N/A new_registers[i] : MAKE_FULLINFO(ITEM_Bogus, 0, 0);
0N/A else
0N/A new_set[i] = i < register_count ?
0N/A registers[i] : MAKE_FULLINFO(ITEM_Bogus, 0, 0);
0N/A }
0N/A new_new_register_info.register_count = max_registers;
0N/A new_new_register_info.registers = new_set;
0N/A new_new_register_info.mask_count = new_mask_count;
0N/A new_new_register_info.masks = new_masks;
0N/A
0N/A
0N/A merge_stack(context, from_inumber, to_inumber, new_stack_info);
0N/A merge_registers(context, to_inumber - 1, to_inumber,
0N/A &new_new_register_info);
0N/A merge_flags(context, from_inumber, to_inumber, new_and_flags, new_or_flags);
0N/A }
0N/A } else {
0N/A merge_stack(context, from_inumber, to_inumber, new_stack_info);
0N/A merge_registers(context, from_inumber, to_inumber, new_register_info);
0N/A merge_flags(context, from_inumber, to_inumber,
0N/A new_and_flags, new_or_flags);
0N/A }
0N/A
0N/A#ifdef DEBUG
0N/A if (verify_verbose && idata[to_inumber].changed) {
0N/A register_info_type *register_info = &this_idata->register_info;
0N/A stack_info_type *stack_info = &this_idata->stack_info;
0N/A if (memcmp(&old_reg_info, register_info, sizeof(old_reg_info)) ||
0N/A memcmp(&old_stack_info, stack_info, sizeof(old_stack_info)) ||
0N/A (old_and_flags != this_idata->and_flags) ||
0N/A (old_or_flags != this_idata->or_flags)) {
0N/A jio_fprintf(stdout, " %2d:", to_inumber);
0N/A print_stack(context, &old_stack_info);
0N/A print_registers(context, &old_reg_info);
0N/A print_flags(context, old_and_flags, old_or_flags);
0N/A jio_fprintf(stdout, " => ");
0N/A print_stack(context, &this_idata->stack_info);
0N/A print_registers(context, &this_idata->register_info);
0N/A print_flags(context, this_idata->and_flags, this_idata->or_flags);
0N/A jio_fprintf(stdout, "\n");
0N/A }
0N/A }
0N/A#endif
0N/A
0N/A}
0N/A
0N/Astatic void
0N/Amerge_stack(context_type *context, unsigned int from_inumber,
0N/A unsigned int to_inumber, stack_info_type *new_stack_info)
0N/A{
0N/A instruction_data_type *idata = context->instruction_data;
0N/A instruction_data_type *this_idata = &idata[to_inumber];
0N/A
0N/A int new_stack_size = new_stack_info->stack_size;
0N/A stack_item_type *new_stack = new_stack_info->stack;
0N/A
0N/A int stack_size = this_idata->stack_info.stack_size;
0N/A
0N/A if (stack_size == UNKNOWN_STACK_SIZE) {
0N/A /* First time at this instruction. Just copy. */
0N/A this_idata->stack_info.stack_size = new_stack_size;
0N/A this_idata->stack_info.stack = new_stack;
0N/A this_idata->changed = JNI_TRUE;
0N/A } else if (new_stack_size != stack_size) {
0N/A CCerror(context, "Inconsistent stack height %d != %d",
0N/A new_stack_size, stack_size);
0N/A } else {
0N/A stack_item_type *stack = this_idata->stack_info.stack;
0N/A stack_item_type *old, *new;
0N/A jboolean change = JNI_FALSE;
0N/A for (old = stack, new = new_stack; old != NULL;
0N/A old = old->next, new = new->next) {
0N/A if (!isAssignableTo(context, new->item, old->item)) {
0N/A change = JNI_TRUE;
0N/A break;
0N/A }
0N/A }
0N/A if (change) {
0N/A stack = copy_stack(context, stack);
0N/A for (old = stack, new = new_stack; old != NULL;
0N/A old = old->next, new = new->next) {
0N/A if (new == NULL) {
0N/A break;
0N/A }
0N/A old->item = merge_fullinfo_types(context, old->item, new->item,
0N/A JNI_FALSE);
0N/A if (GET_ITEM_TYPE(old->item) == ITEM_Bogus) {
0N/A CCerror(context, "Mismatched stack types");
0N/A }
0N/A }
0N/A if (old != NULL || new != NULL) {
0N/A CCerror(context, "Mismatched stack types");
0N/A }
0N/A this_idata->stack_info.stack = stack;
0N/A this_idata->changed = JNI_TRUE;
0N/A }
0N/A }
0N/A}
0N/A
0N/Astatic void
0N/Amerge_registers(context_type *context, unsigned int from_inumber,
0N/A unsigned int to_inumber, register_info_type *new_register_info)
0N/A{
0N/A instruction_data_type *idata = context->instruction_data;
0N/A instruction_data_type *this_idata = &idata[to_inumber];
0N/A register_info_type *this_reginfo = &this_idata->register_info;
0N/A
0N/A int new_register_count = new_register_info->register_count;
0N/A fullinfo_type *new_registers = new_register_info->registers;
0N/A int new_mask_count = new_register_info->mask_count;
0N/A mask_type *new_masks = new_register_info->masks;
0N/A
0N/A
0N/A if (this_reginfo->register_count == UNKNOWN_REGISTER_COUNT) {
0N/A this_reginfo->register_count = new_register_count;
0N/A this_reginfo->registers = new_registers;
0N/A this_reginfo->mask_count = new_mask_count;
0N/A this_reginfo->masks = new_masks;
0N/A this_idata->changed = JNI_TRUE;
0N/A } else {
0N/A /* See if we've got new information on the register set. */
0N/A int register_count = this_reginfo->register_count;
0N/A fullinfo_type *registers = this_reginfo->registers;
0N/A int mask_count = this_reginfo->mask_count;
0N/A mask_type *masks = this_reginfo->masks;
0N/A
0N/A jboolean copy = JNI_FALSE;
0N/A int i, j;
0N/A if (register_count > new_register_count) {
0N/A /* Any register larger than new_register_count is now bogus */
0N/A this_reginfo->register_count = new_register_count;
0N/A register_count = new_register_count;
0N/A this_idata->changed = JNI_TRUE;
0N/A }
0N/A for (i = 0; i < register_count; i++) {
0N/A fullinfo_type prev_value = registers[i];
0N/A if ((i < new_register_count)
0N/A ? (!isAssignableTo(context, new_registers[i], prev_value))
0N/A : (prev_value != MAKE_FULLINFO(ITEM_Bogus, 0, 0))) {
0N/A copy = JNI_TRUE;
0N/A break;
0N/A }
0N/A }
0N/A
0N/A if (copy) {
0N/A /* We need a copy. So do it. */
0N/A fullinfo_type *new_set = NEW(fullinfo_type, register_count);
0N/A for (j = 0; j < i; j++)
0N/A new_set[j] = registers[j];
0N/A for (j = i; j < register_count; j++) {
0N/A if (i >= new_register_count)
0N/A new_set[j] = MAKE_FULLINFO(ITEM_Bogus, 0, 0);
0N/A else
0N/A new_set[j] = merge_fullinfo_types(context,
0N/A new_registers[j],
0N/A registers[j], JNI_FALSE);
0N/A }
0N/A /* Some of the end items might now be bogus. This step isn't
0N/A * necessary, but it may save work later. */
0N/A while ( register_count > 0
0N/A && GET_ITEM_TYPE(new_set[register_count-1]) == ITEM_Bogus)
0N/A register_count--;
0N/A this_reginfo->register_count = register_count;
0N/A this_reginfo->registers = new_set;
0N/A this_idata->changed = JNI_TRUE;
0N/A }
0N/A if (mask_count > 0) {
0N/A /* If the target instruction already has a sequence of masks, then
0N/A * we need to merge new_masks into it. We want the entries on
0N/A * the mask to be the longest common substring of the two.
0N/A * (e.g. a->b->d merged with a->c->d should give a->d)
0N/A * The bits set in the mask should be the or of the corresponding
0N/A * entries in each of the original masks.
0N/A */
0N/A int i, j, k;
0N/A int matches = 0;
0N/A int last_match = -1;
0N/A jboolean copy_needed = JNI_FALSE;
0N/A for (i = 0; i < mask_count; i++) {
0N/A int entry = masks[i].entry;
0N/A for (j = last_match + 1; j < new_mask_count; j++) {
0N/A if (new_masks[j].entry == entry) {
0N/A /* We have a match */
0N/A int *prev = masks[i].modifies;
0N/A int *new = new_masks[j].modifies;
0N/A matches++;
0N/A /* See if new_mask has bits set for "entry" that
0N/A * weren't set for mask. If so, need to copy. */
0N/A for (k = context->bitmask_size - 1;
0N/A !copy_needed && k >= 0;
0N/A k--)
0N/A if (~prev[k] & new[k])
0N/A copy_needed = JNI_TRUE;
0N/A last_match = j;
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A if ((matches < mask_count) || copy_needed) {
0N/A /* We need to make a copy for the new item, since either the
0N/A * size has decreased, or new bits are set. */
0N/A mask_type *copy = NEW(mask_type, matches);
0N/A for (i = 0; i < matches; i++) {
0N/A copy[i].modifies = NEW(int, context->bitmask_size);
0N/A }
0N/A this_reginfo->masks = copy;
0N/A this_reginfo->mask_count = matches;
0N/A this_idata->changed = JNI_TRUE;
0N/A matches = 0;
0N/A last_match = -1;
0N/A for (i = 0; i < mask_count; i++) {
0N/A int entry = masks[i].entry;
0N/A for (j = last_match + 1; j < new_mask_count; j++) {
0N/A if (new_masks[j].entry == entry) {
0N/A int *prev1 = masks[i].modifies;
0N/A int *prev2 = new_masks[j].modifies;
0N/A int *new = copy[matches].modifies;
0N/A copy[matches].entry = entry;
0N/A for (k = context->bitmask_size - 1; k >= 0; k--)
0N/A new[k] = prev1[k] | prev2[k];
0N/A matches++;
0N/A last_match = j;
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/Astatic void
0N/Amerge_flags(context_type *context, unsigned int from_inumber,
0N/A unsigned int to_inumber,
0N/A flag_type new_and_flags, flag_type new_or_flags)
0N/A{
0N/A /* Set this_idata->and_flags &= new_and_flags
0N/A this_idata->or_flags |= new_or_flags
0N/A */
0N/A instruction_data_type *idata = context->instruction_data;
0N/A instruction_data_type *this_idata = &idata[to_inumber];
0N/A flag_type this_and_flags = this_idata->and_flags;
0N/A flag_type this_or_flags = this_idata->or_flags;
0N/A flag_type merged_and = this_and_flags & new_and_flags;
0N/A flag_type merged_or = this_or_flags | new_or_flags;
0N/A
0N/A if ((merged_and != this_and_flags) || (merged_or != this_or_flags)) {
0N/A this_idata->and_flags = merged_and;
0N/A this_idata->or_flags = merged_or;
0N/A this_idata->changed = JNI_TRUE;
0N/A }
0N/A}
0N/A
0N/A
0N/A/* Make a copy of a stack */
0N/A
0N/Astatic stack_item_type *
0N/Acopy_stack(context_type *context, stack_item_type *stack)
0N/A{
0N/A int length;
0N/A stack_item_type *ptr;
0N/A
0N/A /* Find the length */
0N/A for (ptr = stack, length = 0; ptr != NULL; ptr = ptr->next, length++);
0N/A
0N/A if (length > 0) {
0N/A stack_item_type *new_stack = NEW(stack_item_type, length);
0N/A stack_item_type *new_ptr;
0N/A for ( ptr = stack, new_ptr = new_stack;
0N/A ptr != NULL;
0N/A ptr = ptr->next, new_ptr++) {
0N/A new_ptr->item = ptr->item;
0N/A new_ptr->next = new_ptr + 1;
0N/A }
0N/A new_stack[length - 1].next = NULL;
0N/A return new_stack;
0N/A } else {
0N/A return NULL;
0N/A }
0N/A}
0N/A
0N/A
0N/Astatic mask_type *
0N/Acopy_masks(context_type *context, mask_type *masks, int mask_count)
0N/A{
0N/A mask_type *result = NEW(mask_type, mask_count);
0N/A int bitmask_size = context->bitmask_size;
0N/A int *bitmaps = NEW(int, mask_count * bitmask_size);
0N/A int i;
0N/A for (i = 0; i < mask_count; i++) {
0N/A result[i].entry = masks[i].entry;
0N/A result[i].modifies = &bitmaps[i * bitmask_size];
0N/A memcpy(result[i].modifies, masks[i].modifies, bitmask_size * sizeof(int));
0N/A }
0N/A return result;
0N/A}
0N/A
0N/A
0N/Astatic mask_type *
0N/Aadd_to_masks(context_type *context, mask_type *masks, int mask_count, int d)
0N/A{
0N/A mask_type *result = NEW(mask_type, mask_count + 1);
0N/A int bitmask_size = context->bitmask_size;
0N/A int *bitmaps = NEW(int, (mask_count + 1) * bitmask_size);
0N/A int i;
0N/A for (i = 0; i < mask_count; i++) {
0N/A result[i].entry = masks[i].entry;
0N/A result[i].modifies = &bitmaps[i * bitmask_size];
0N/A memcpy(result[i].modifies, masks[i].modifies, bitmask_size * sizeof(int));
0N/A }
0N/A result[mask_count].entry = d;
0N/A result[mask_count].modifies = &bitmaps[mask_count * bitmask_size];
0N/A memset(result[mask_count].modifies, 0, bitmask_size * sizeof(int));
0N/A return result;
0N/A}
0N/A
0N/A
0N/A
0N/A/* We create our own storage manager, since we malloc lots of little items,
0N/A * and I don't want to keep trace of when they become free. I sure wish that
0N/A * we had heaps, and I could just free the heap when done.
0N/A */
0N/A
0N/A#define CCSegSize 2000
0N/A
0N/Astruct CCpool { /* a segment of allocated memory in the pool */
0N/A struct CCpool *next;
0N/A int segSize; /* almost always CCSegSize */
0N/A int poolPad;
0N/A char space[CCSegSize];
0N/A};
0N/A
0N/A/* Initialize the context's heap. */
0N/Astatic void CCinit(context_type *context)
0N/A{
0N/A struct CCpool *new = (struct CCpool *) malloc(sizeof(struct CCpool));
0N/A /* Set context->CCroot to 0 if new == 0 to tell CCdestroy to lay off */
0N/A context->CCroot = context->CCcurrent = new;
0N/A if (new == 0) {
0N/A CCout_of_memory(context);
0N/A }
0N/A new->next = NULL;
0N/A new->segSize = CCSegSize;
0N/A context->CCfree_size = CCSegSize;
0N/A context->CCfree_ptr = &new->space[0];
0N/A}
0N/A
0N/A
0N/A/* Reuse all the space that we have in the context's heap. */
0N/Astatic void CCreinit(context_type *context)
0N/A{
0N/A struct CCpool *first = context->CCroot;
0N/A context->CCcurrent = first;
0N/A context->CCfree_size = CCSegSize;
0N/A context->CCfree_ptr = &first->space[0];
0N/A}
0N/A
0N/A/* Destroy the context's heap. */
0N/Astatic void CCdestroy(context_type *context)
0N/A{
0N/A struct CCpool *this = context->CCroot;
0N/A while (this) {
0N/A struct CCpool *next = this->next;
0N/A free(this);
0N/A this = next;
0N/A }
0N/A /* These two aren't necessary. But can't hurt either */
0N/A context->CCroot = context->CCcurrent = NULL;
0N/A context->CCfree_ptr = 0;
0N/A}
0N/A
0N/A/* Allocate an object of the given size from the context's heap. */
0N/Astatic void *
0N/ACCalloc(context_type *context, int size, jboolean zero)
0N/A{
0N/A
0N/A register char *p;
0N/A /* Round CC to the size of a pointer */
0N/A size = (size + (sizeof(void *) - 1)) & ~(sizeof(void *) - 1);
0N/A
0N/A if (context->CCfree_size < size) {
0N/A struct CCpool *current = context->CCcurrent;
0N/A struct CCpool *new;
0N/A if (size > CCSegSize) { /* we need to allocate a special block */
0N/A new = (struct CCpool *)malloc(sizeof(struct CCpool) +
0N/A (size - CCSegSize));
0N/A if (new == 0) {
0N/A CCout_of_memory(context);
0N/A }
0N/A new->next = current->next;
0N/A new->segSize = size;
0N/A current->next = new;
0N/A } else {
0N/A new = current->next;
0N/A if (new == NULL) {
0N/A new = (struct CCpool *) malloc(sizeof(struct CCpool));
0N/A if (new == 0) {
0N/A CCout_of_memory(context);
0N/A }
0N/A current->next = new;
0N/A new->next = NULL;
0N/A new->segSize = CCSegSize;
0N/A }
0N/A }
0N/A context->CCcurrent = new;
0N/A context->CCfree_ptr = &new->space[0];
0N/A context->CCfree_size = new->segSize;
0N/A }
0N/A p = context->CCfree_ptr;
0N/A context->CCfree_ptr += size;
0N/A context->CCfree_size -= size;
0N/A if (zero)
0N/A memset(p, 0, size);
0N/A return p;
0N/A}
0N/A
0N/A/* Get the class associated with a particular field or method or class in the
0N/A * constant pool. If is_field is true, we've got a field or method. If
0N/A * false, we've got a class.
0N/A */
0N/Astatic fullinfo_type
0N/Acp_index_to_class_fullinfo(context_type *context, int cp_index, int kind)
0N/A{
0N/A JNIEnv *env = context->env;
0N/A fullinfo_type result;
0N/A const char *classname;
0N/A switch (kind) {
0N/A case JVM_CONSTANT_Class:
0N/A classname = JVM_GetCPClassNameUTF(env,
0N/A context->class,
0N/A cp_index);
0N/A break;
0N/A case JVM_CONSTANT_Methodref:
0N/A classname = JVM_GetCPMethodClassNameUTF(env,
0N/A context->class,
0N/A cp_index);
0N/A break;
0N/A case JVM_CONSTANT_Fieldref:
0N/A classname = JVM_GetCPFieldClassNameUTF(env,
0N/A context->class,
0N/A cp_index);
0N/A break;
0N/A default:
0N/A classname = NULL;
0N/A CCerror(context, "Internal error #5");
0N/A }
0N/A
0N/A check_and_push(context, classname, VM_STRING_UTF);
0N/A if (classname[0] == JVM_SIGNATURE_ARRAY) {
0N/A /* This make recursively call us, in case of a class array */
0N/A signature_to_fieldtype(context, &classname, &result);
0N/A } else {
0N/A result = make_class_info_from_name(context, classname);
0N/A }
0N/A pop_and_free(context);
0N/A return result;
0N/A}
0N/A
0N/A
0N/Astatic int
0N/Aprint_CCerror_info(context_type *context)
0N/A{
0N/A JNIEnv *env = context->env;
0N/A jclass cb = context->class;
0N/A const char *classname = JVM_GetClassNameUTF(env, cb);
0N/A const char *name = 0;
0N/A const char *signature = 0;
0N/A int n = 0;
0N/A if (context->method_index != -1) {
0N/A name = JVM_GetMethodIxNameUTF(env, cb, context->method_index);
0N/A signature =
0N/A JVM_GetMethodIxSignatureUTF(env, cb, context->method_index);
0N/A n += jio_snprintf(context->message, context->message_buf_len,
0N/A "(class: %s, method: %s signature: %s) ",
0N/A (classname ? classname : ""),
0N/A (name ? name : ""),
0N/A (signature ? signature : ""));
0N/A } else if (context->field_index != -1 ) {
0N/A name = JVM_GetMethodIxNameUTF(env, cb, context->field_index);
0N/A n += jio_snprintf(context->message, context->message_buf_len,
0N/A "(class: %s, field: %s) ",
0N/A (classname ? classname : 0),
0N/A (name ? name : 0));
0N/A } else {
0N/A n += jio_snprintf(context->message, context->message_buf_len,
0N/A "(class: %s) ", classname ? classname : "");
0N/A }
0N/A JVM_ReleaseUTF(classname);
0N/A JVM_ReleaseUTF(name);
0N/A JVM_ReleaseUTF(signature);
0N/A return n;
0N/A}
0N/A
0N/Astatic void
0N/ACCerror (context_type *context, char *format, ...)
0N/A{
0N/A int n = print_CCerror_info(context);
0N/A va_list args;
0N/A if (n >= 0 && n < context->message_buf_len) {
0N/A va_start(args, format);
0N/A jio_vsnprintf(context->message + n, context->message_buf_len - n,
0N/A format, args);
0N/A va_end(args);
0N/A }
0N/A context->err_code = CC_VerifyError;
0N/A longjmp(context->jump_buffer, 1);
0N/A}
0N/A
0N/Astatic void
0N/ACCout_of_memory(context_type *context)
0N/A{
0N/A int n = print_CCerror_info(context);
0N/A context->err_code = CC_OutOfMemory;
0N/A longjmp(context->jump_buffer, 1);
0N/A}
0N/A
0N/Astatic void
0N/ACFerror(context_type *context, char *format, ...)
0N/A{
0N/A int n = print_CCerror_info(context);
0N/A va_list args;
0N/A if (n >= 0 && n < context->message_buf_len) {
0N/A va_start(args, format);
0N/A jio_vsnprintf(context->message + n, context->message_buf_len - n,
0N/A format, args);
0N/A va_end(args);
0N/A }
0N/A context->err_code = CC_ClassFormatError;
0N/A longjmp(context->jump_buffer, 1);
0N/A}
0N/A
0N/Astatic char
0N/Asignature_to_fieldtype(context_type *context,
0N/A const char **signature_p, fullinfo_type *full_info_p)
0N/A{
0N/A const char *p = *signature_p;
0N/A fullinfo_type full_info = MAKE_FULLINFO(0, 0, 0);
0N/A char result;
0N/A int array_depth = 0;
0N/A
0N/A for (;;) {
0N/A switch(*p++) {
0N/A default:
0N/A full_info = MAKE_FULLINFO(ITEM_Bogus, 0, 0);
0N/A result = 0;
0N/A break;
0N/A
0N/A case JVM_SIGNATURE_BOOLEAN: case JVM_SIGNATURE_BYTE:
0N/A full_info = (array_depth > 0)
0N/A ? MAKE_FULLINFO(ITEM_Byte, 0, 0)
0N/A : MAKE_FULLINFO(ITEM_Integer, 0, 0);
0N/A result = 'I';
0N/A break;
0N/A
0N/A case JVM_SIGNATURE_CHAR:
0N/A full_info = (array_depth > 0)
0N/A ? MAKE_FULLINFO(ITEM_Char, 0, 0)
0N/A : MAKE_FULLINFO(ITEM_Integer, 0, 0);
0N/A result = 'I';
0N/A break;
0N/A
0N/A case JVM_SIGNATURE_SHORT:
0N/A full_info = (array_depth > 0)
0N/A ? MAKE_FULLINFO(ITEM_Short, 0, 0)
0N/A : MAKE_FULLINFO(ITEM_Integer, 0, 0);
0N/A result = 'I';
0N/A break;
0N/A
0N/A case JVM_SIGNATURE_INT:
0N/A full_info = MAKE_FULLINFO(ITEM_Integer, 0, 0);
0N/A result = 'I';
0N/A break;
0N/A
0N/A case JVM_SIGNATURE_FLOAT:
0N/A full_info = MAKE_FULLINFO(ITEM_Float, 0, 0);
0N/A result = 'F';
0N/A break;
0N/A
0N/A case JVM_SIGNATURE_DOUBLE:
0N/A full_info = MAKE_FULLINFO(ITEM_Double, 0, 0);
0N/A result = 'D';
0N/A break;
0N/A
0N/A case JVM_SIGNATURE_LONG:
0N/A full_info = MAKE_FULLINFO(ITEM_Long, 0, 0);
0N/A result = 'L';
0N/A break;
0N/A
0N/A case JVM_SIGNATURE_ARRAY:
0N/A array_depth++;
0N/A continue; /* only time we ever do the loop > 1 */
0N/A
0N/A case JVM_SIGNATURE_CLASS: {
0N/A char buffer_space[256];
0N/A char *buffer = buffer_space;
0N/A char *finish = strchr(p, JVM_SIGNATURE_ENDCLASS);
0N/A int length = finish - p;
502N/A if (length + 1 > (int)sizeof(buffer_space)) {
0N/A buffer = malloc(length + 1);
0N/A check_and_push(context, buffer, VM_MALLOC_BLK);
0N/A }
0N/A memcpy(buffer, p, length);
0N/A buffer[length] = '\0';
0N/A full_info = make_class_info_from_name(context, buffer);
0N/A result = 'A';
0N/A p = finish + 1;
0N/A if (buffer != buffer_space)
0N/A pop_and_free(context);
0N/A break;
0N/A }
0N/A } /* end of switch */
0N/A break;
0N/A }
0N/A *signature_p = p;
0N/A if (array_depth == 0 || result == 0) {
0N/A /* either not an array, or result is bogus */
0N/A *full_info_p = full_info;
0N/A return result;
0N/A } else {
0N/A if (array_depth > MAX_ARRAY_DIMENSIONS)
0N/A CCerror(context, "Array with too many dimensions");
0N/A *full_info_p = MAKE_FULLINFO(GET_ITEM_TYPE(full_info),
0N/A array_depth,
0N/A GET_EXTRA_INFO(full_info));
0N/A return 'A';
0N/A }
0N/A}
0N/A
0N/A
0N/A/* Given an array type, create the type that has one less level of
0N/A * indirection.
0N/A */
0N/A
0N/Astatic fullinfo_type
0N/Adecrement_indirection(fullinfo_type array_info)
0N/A{
0N/A if (array_info == NULL_FULLINFO) {
0N/A return NULL_FULLINFO;
0N/A } else {
0N/A int type = GET_ITEM_TYPE(array_info);
0N/A int indirection = GET_INDIRECTION(array_info) - 1;
0N/A int extra_info = GET_EXTRA_INFO(array_info);
0N/A if ( (indirection == 0)
0N/A && ((type == ITEM_Short || type == ITEM_Byte || type == ITEM_Char)))
0N/A type = ITEM_Integer;
0N/A return MAKE_FULLINFO(type, indirection, extra_info);
0N/A }
0N/A}
0N/A
0N/A
0N/A/* See if we can assign an object of the "from" type to an object
0N/A * of the "to" type.
0N/A */
0N/A
0N/Astatic jboolean isAssignableTo(context_type *context,
0N/A fullinfo_type from, fullinfo_type to)
0N/A{
0N/A return (merge_fullinfo_types(context, from, to, JNI_TRUE) == to);
0N/A}
0N/A
0N/A/* Given two fullinfo_type's, find their lowest common denominator. If
0N/A * the assignable_p argument is non-null, we're really just calling to find
0N/A * out if "<target> := <value>" is a legitimate assignment.
0N/A *
0N/A * We treat all interfaces as if they were of type java/lang/Object, since the
0N/A * runtime will do the full checking.
0N/A */
0N/Astatic fullinfo_type
0N/Amerge_fullinfo_types(context_type *context,
0N/A fullinfo_type value, fullinfo_type target,
0N/A jboolean for_assignment)
0N/A{
0N/A JNIEnv *env = context->env;
0N/A if (value == target) {
0N/A /* If they're identical, clearly just return what we've got */
0N/A return value;
0N/A }
0N/A
0N/A /* Both must be either arrays or objects to go further */
0N/A if (GET_INDIRECTION(value) == 0 && GET_ITEM_TYPE(value) != ITEM_Object)
0N/A return MAKE_FULLINFO(ITEM_Bogus, 0, 0);
0N/A if (GET_INDIRECTION(target) == 0 && GET_ITEM_TYPE(target) != ITEM_Object)
0N/A return MAKE_FULLINFO(ITEM_Bogus, 0, 0);
0N/A
0N/A /* If either is NULL, return the other. */
0N/A if (value == NULL_FULLINFO)
0N/A return target;
0N/A else if (target == NULL_FULLINFO)
0N/A return value;
0N/A
0N/A /* If either is java/lang/Object, that's the result. */
0N/A if (target == context->object_info)
0N/A return target;
0N/A else if (value == context->object_info) {
0N/A /* Minor hack. For assignments, Interface := Object, return Interface
0N/A * rather than Object, so that isAssignableTo() will get the right
0N/A * result. */
0N/A if (for_assignment && (WITH_ZERO_EXTRA_INFO(target) ==
0N/A MAKE_FULLINFO(ITEM_Object, 0, 0))) {
0N/A jclass cb = object_fullinfo_to_classclass(context,
0N/A target);
0N/A int is_interface = cb && JVM_IsInterface(env, cb);
0N/A if (is_interface)
0N/A return target;
0N/A }
0N/A return value;
0N/A }
0N/A if (GET_INDIRECTION(value) > 0 || GET_INDIRECTION(target) > 0) {
0N/A /* At least one is an array. Neither is java/lang/Object or NULL.
0N/A * Moreover, the types are not identical.
0N/A * The result must either be Object, or an array of some object type.
0N/A */
0N/A fullinfo_type value_base, target_base;
0N/A int dimen_value = GET_INDIRECTION(value);
0N/A int dimen_target = GET_INDIRECTION(target);
0N/A
0N/A if (target == context->cloneable_info ||
0N/A target == context->serializable_info) {
0N/A return target;
0N/A }
0N/A
0N/A if (value == context->cloneable_info ||
0N/A value == context->serializable_info) {
0N/A return value;
0N/A }
0N/A
0N/A /* First, if either item's base type isn't ITEM_Object, promote it up
0N/A * to an object or array of object. If either is elemental, we can
0N/A * punt.
0N/A */
0N/A if (GET_ITEM_TYPE(value) != ITEM_Object) {
0N/A if (dimen_value == 0)
0N/A return MAKE_FULLINFO(ITEM_Bogus, 0, 0);
0N/A dimen_value--;
0N/A value = MAKE_Object_ARRAY(dimen_value);
0N/A
0N/A }
0N/A if (GET_ITEM_TYPE(target) != ITEM_Object) {
0N/A if (dimen_target == 0)
0N/A return MAKE_FULLINFO(ITEM_Bogus, 0, 0);
0N/A dimen_target--;
0N/A target = MAKE_Object_ARRAY(dimen_target);
0N/A }
0N/A /* Both are now objects or arrays of some sort of object type */
0N/A value_base = WITH_ZERO_INDIRECTION(value);
0N/A target_base = WITH_ZERO_INDIRECTION(target);
0N/A if (dimen_value == dimen_target) {
0N/A /* Arrays of the same dimension. Merge their base types. */
0N/A fullinfo_type result_base =
0N/A merge_fullinfo_types(context, value_base, target_base,
0N/A for_assignment);
0N/A if (result_base == MAKE_FULLINFO(ITEM_Bogus, 0, 0))
0N/A /* bogus in, bogus out */
0N/A return result_base;
0N/A return MAKE_FULLINFO(ITEM_Object, dimen_value,
0N/A GET_EXTRA_INFO(result_base));
0N/A } else {
0N/A /* Arrays of different sizes. If the smaller dimension array's base
0N/A * type is java/lang/Cloneable or java/io/Serializable, return it.
0N/A * Otherwise return java/lang/Object with a dimension of the smaller
0N/A * of the two */
0N/A if (dimen_value < dimen_target) {
0N/A if (value_base == context->cloneable_info ||
0N/A value_base == context ->serializable_info) {
0N/A return value;
0N/A }
0N/A return MAKE_Object_ARRAY(dimen_value);
0N/A } else {
0N/A if (target_base == context->cloneable_info ||
0N/A target_base == context->serializable_info) {
0N/A return target;
0N/A }
0N/A return MAKE_Object_ARRAY(dimen_target);
0N/A }
0N/A }
0N/A } else {
0N/A /* Both are non-array objects. Neither is java/lang/Object or NULL */
0N/A jclass cb_value, cb_target, cb_super_value, cb_super_target;
0N/A fullinfo_type result_info;
0N/A
0N/A /* Let's get the classes corresponding to each of these. Treat
0N/A * interfaces as if they were java/lang/Object. See hack note above. */
0N/A cb_target = object_fullinfo_to_classclass(context, target);
0N/A if (cb_target == 0)
0N/A return MAKE_FULLINFO(ITEM_Bogus, 0, 0);
0N/A if (JVM_IsInterface(env, cb_target))
0N/A return for_assignment ? target : context->object_info;
0N/A cb_value = object_fullinfo_to_classclass(context, value);
0N/A if (cb_value == 0)
0N/A return MAKE_FULLINFO(ITEM_Bogus, 0, 0);
0N/A if (JVM_IsInterface(env, cb_value))
0N/A return context->object_info;
0N/A
0N/A /* If this is for assignment of target := value, we just need to see if
0N/A * cb_target is a superclass of cb_value. Save ourselves a lot of
0N/A * work.
0N/A */
0N/A if (for_assignment) {
0N/A cb_super_value = (*env)->GetSuperclass(env, cb_value);
0N/A while (cb_super_value != 0) {
0N/A jclass tmp_cb;
0N/A if ((*env)->IsSameObject(env, cb_super_value, cb_target)) {
0N/A (*env)->DeleteLocalRef(env, cb_super_value);
0N/A return target;
0N/A }
0N/A tmp_cb = (*env)->GetSuperclass(env, cb_super_value);
0N/A (*env)->DeleteLocalRef(env, cb_super_value);
0N/A cb_super_value = tmp_cb;
0N/A }
0N/A (*env)->DeleteLocalRef(env, cb_super_value);
0N/A return context->object_info;
0N/A }
0N/A
0N/A /* Find out whether cb_value or cb_target is deeper in the class
0N/A * tree by moving both toward the root, and seeing who gets there
0N/A * first. */
0N/A cb_super_value = (*env)->GetSuperclass(env, cb_value);
0N/A cb_super_target = (*env)->GetSuperclass(env, cb_target);
0N/A while((cb_super_value != 0) &&
0N/A (cb_super_target != 0)) {
0N/A jclass tmp_cb;
0N/A /* Optimization. If either hits the other when going up looking
0N/A * for a parent, then might as well return the parent immediately */
0N/A if ((*env)->IsSameObject(env, cb_super_value, cb_target)) {
0N/A (*env)->DeleteLocalRef(env, cb_super_value);
0N/A (*env)->DeleteLocalRef(env, cb_super_target);
0N/A return target;
0N/A }
0N/A if ((*env)->IsSameObject(env, cb_super_target, cb_value)) {
0N/A (*env)->DeleteLocalRef(env, cb_super_value);
0N/A (*env)->DeleteLocalRef(env, cb_super_target);
0N/A return value;
0N/A }
0N/A tmp_cb = (*env)->GetSuperclass(env, cb_super_value);
0N/A (*env)->DeleteLocalRef(env, cb_super_value);
0N/A cb_super_value = tmp_cb;
0N/A
0N/A tmp_cb = (*env)->GetSuperclass(env, cb_super_target);
0N/A (*env)->DeleteLocalRef(env, cb_super_target);
0N/A cb_super_target = tmp_cb;
0N/A }
0N/A cb_value = (*env)->NewLocalRef(env, cb_value);
0N/A cb_target = (*env)->NewLocalRef(env, cb_target);
0N/A /* At most one of the following two while clauses will be executed.
0N/A * Bring the deeper of cb_target and cb_value to the depth of the
0N/A * shallower one.
0N/A */
0N/A while (cb_super_value != 0) {
0N/A /* cb_value is deeper */
0N/A jclass cb_tmp;
0N/A
0N/A cb_tmp = (*env)->GetSuperclass(env, cb_super_value);
0N/A (*env)->DeleteLocalRef(env, cb_super_value);
0N/A cb_super_value = cb_tmp;
0N/A
0N/A cb_tmp = (*env)->GetSuperclass(env, cb_value);
0N/A (*env)->DeleteLocalRef(env, cb_value);
0N/A cb_value = cb_tmp;
0N/A }
0N/A while (cb_super_target != 0) {
0N/A /* cb_target is deeper */
0N/A jclass cb_tmp;
0N/A
0N/A cb_tmp = (*env)->GetSuperclass(env, cb_super_target);
0N/A (*env)->DeleteLocalRef(env, cb_super_target);
0N/A cb_super_target = cb_tmp;
0N/A
0N/A cb_tmp = (*env)->GetSuperclass(env, cb_target);
0N/A (*env)->DeleteLocalRef(env, cb_target);
0N/A cb_target = cb_tmp;
0N/A }
0N/A
0N/A /* Walk both up, maintaining equal depth, until a join is found. We
0N/A * know that we will find one. */
0N/A while (!(*env)->IsSameObject(env, cb_value, cb_target)) {
0N/A jclass cb_tmp;
0N/A cb_tmp = (*env)->GetSuperclass(env, cb_value);
0N/A (*env)->DeleteLocalRef(env, cb_value);
0N/A cb_value = cb_tmp;
0N/A cb_tmp = (*env)->GetSuperclass(env, cb_target);
0N/A (*env)->DeleteLocalRef(env, cb_target);
0N/A cb_target = cb_tmp;
0N/A }
0N/A result_info = make_class_info(context, cb_value);
0N/A (*env)->DeleteLocalRef(env, cb_value);
0N/A (*env)->DeleteLocalRef(env, cb_super_value);
0N/A (*env)->DeleteLocalRef(env, cb_target);
0N/A (*env)->DeleteLocalRef(env, cb_super_target);
0N/A return result_info;
0N/A } /* both items are classes */
0N/A}
0N/A
0N/A
0N/A/* Given a fullinfo_type corresponding to an Object, return the jclass
0N/A * of that type.
0N/A *
0N/A * This function always returns a global reference!
0N/A */
0N/A
0N/Astatic jclass
0N/Aobject_fullinfo_to_classclass(context_type *context, fullinfo_type classinfo)
0N/A{
0N/A unsigned short info = GET_EXTRA_INFO(classinfo);
0N/A return ID_to_class(context, info);
0N/A}
0N/A
0N/Astatic void free_block(void *ptr, int kind)
0N/A{
0N/A switch (kind) {
0N/A case VM_STRING_UTF:
0N/A JVM_ReleaseUTF(ptr);
0N/A break;
0N/A case VM_MALLOC_BLK:
0N/A free(ptr);
0N/A break;
0N/A }
0N/A}
0N/A
0N/Astatic void check_and_push(context_type *context, const void *ptr, int kind)
0N/A{
0N/A alloc_stack_type *p;
0N/A if (ptr == 0)
0N/A CCout_of_memory(context);
0N/A if (context->alloc_stack_top < ALLOC_STACK_SIZE)
0N/A p = &(context->alloc_stack[context->alloc_stack_top++]);
0N/A else {
0N/A /* Otherwise we have to malloc */
0N/A p = malloc(sizeof(alloc_stack_type));
0N/A if (p == 0) {
0N/A /* Make sure we clean up. */
0N/A free_block((void *)ptr, kind);
0N/A CCout_of_memory(context);
0N/A }
0N/A }
0N/A p->kind = kind;
0N/A p->ptr = (void *)ptr;
0N/A p->next = context->allocated_memory;
0N/A context->allocated_memory = p;
0N/A}
0N/A
0N/Astatic void pop_and_free(context_type *context)
0N/A{
0N/A alloc_stack_type *p = context->allocated_memory;
0N/A context->allocated_memory = p->next;
0N/A free_block(p->ptr, p->kind);
0N/A if (p < context->alloc_stack + ALLOC_STACK_SIZE &&
0N/A p >= context->alloc_stack)
0N/A context->alloc_stack_top--;
0N/A else
0N/A free(p);
0N/A}
0N/A
0N/Astatic int signature_to_args_size(const char *method_signature)
0N/A{
0N/A const char *p;
0N/A int args_size = 0;
0N/A for (p = method_signature; *p != JVM_SIGNATURE_ENDFUNC; p++) {
0N/A switch (*p) {
0N/A case JVM_SIGNATURE_BOOLEAN:
0N/A case JVM_SIGNATURE_BYTE:
0N/A case JVM_SIGNATURE_CHAR:
0N/A case JVM_SIGNATURE_SHORT:
0N/A case JVM_SIGNATURE_INT:
0N/A case JVM_SIGNATURE_FLOAT:
0N/A args_size += 1;
0N/A break;
0N/A case JVM_SIGNATURE_CLASS:
0N/A args_size += 1;
0N/A while (*p != JVM_SIGNATURE_ENDCLASS) p++;
0N/A break;
0N/A case JVM_SIGNATURE_ARRAY:
0N/A args_size += 1;
0N/A while ((*p == JVM_SIGNATURE_ARRAY)) p++;
0N/A /* If an array of classes, skip over class name, too. */
0N/A if (*p == JVM_SIGNATURE_CLASS) {
0N/A while (*p != JVM_SIGNATURE_ENDCLASS)
0N/A p++;
0N/A }
0N/A break;
0N/A case JVM_SIGNATURE_DOUBLE:
0N/A case JVM_SIGNATURE_LONG:
0N/A args_size += 2;
0N/A break;
0N/A case JVM_SIGNATURE_FUNC: /* ignore initial (, if given */
0N/A break;
0N/A default:
0N/A /* Indicate an error. */
0N/A return 0;
0N/A }
0N/A }
0N/A return args_size;
0N/A}
0N/A
0N/A#ifdef DEBUG
0N/A
0N/A/* Below are for debugging. */
0N/A
0N/Astatic void print_fullinfo_type(context_type *, fullinfo_type, jboolean);
0N/A
0N/Astatic void
0N/Aprint_stack(context_type *context, stack_info_type *stack_info)
0N/A{
0N/A stack_item_type *stack = stack_info->stack;
0N/A if (stack_info->stack_size == UNKNOWN_STACK_SIZE) {
0N/A jio_fprintf(stdout, "x");
0N/A } else {
0N/A jio_fprintf(stdout, "(");
0N/A for ( ; stack != 0; stack = stack->next)
0N/A print_fullinfo_type(context, stack->item,
0N/A (jboolean)(verify_verbose > 1 ? JNI_TRUE : JNI_FALSE));
0N/A jio_fprintf(stdout, ")");
0N/A }
0N/A}
0N/A
0N/Astatic void
0N/Aprint_registers(context_type *context, register_info_type *register_info)
0N/A{
0N/A int register_count = register_info->register_count;
0N/A if (register_count == UNKNOWN_REGISTER_COUNT) {
0N/A jio_fprintf(stdout, "x");
0N/A } else {
0N/A fullinfo_type *registers = register_info->registers;
0N/A int mask_count = register_info->mask_count;
0N/A mask_type *masks = register_info->masks;
0N/A int i, j;
0N/A
0N/A jio_fprintf(stdout, "{");
0N/A for (i = 0; i < register_count; i++)
0N/A print_fullinfo_type(context, registers[i],
0N/A (jboolean)(verify_verbose > 1 ? JNI_TRUE : JNI_FALSE));
0N/A jio_fprintf(stdout, "}");
0N/A for (i = 0; i < mask_count; i++) {
0N/A char *separator = "";
0N/A int *modifies = masks[i].modifies;
0N/A jio_fprintf(stdout, "<%d: ", masks[i].entry);
0N/A for (j = 0;
0N/A j < JVM_GetMethodIxLocalsCount(context->env,
0N/A context->class,
0N/A context->method_index);
0N/A j++)
0N/A if (IS_BIT_SET(modifies, j)) {
0N/A jio_fprintf(stdout, "%s%d", separator, j);
0N/A separator = ",";
0N/A }
0N/A jio_fprintf(stdout, ">");
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/Astatic void
0N/Aprint_flags(context_type *context, flag_type and_flags, flag_type or_flags)
0N/A{
0N/A if (and_flags != ((flag_type)-1) || or_flags != 0) {
0N/A jio_fprintf(stdout, "<%x %x>", and_flags, or_flags);
0N/A }
0N/A}
0N/A
0N/Astatic void
0N/Aprint_fullinfo_type(context_type *context, fullinfo_type type, jboolean verbose)
0N/A{
0N/A int i;
0N/A int indirection = GET_INDIRECTION(type);
0N/A for (i = indirection; i-- > 0; )
0N/A jio_fprintf(stdout, "[");
0N/A switch (GET_ITEM_TYPE(type)) {
0N/A case ITEM_Integer:
0N/A jio_fprintf(stdout, "I"); break;
0N/A case ITEM_Float:
0N/A jio_fprintf(stdout, "F"); break;
0N/A case ITEM_Double:
0N/A jio_fprintf(stdout, "D"); break;
0N/A case ITEM_Double_2:
0N/A jio_fprintf(stdout, "d"); break;
0N/A case ITEM_Long:
0N/A jio_fprintf(stdout, "L"); break;
0N/A case ITEM_Long_2:
0N/A jio_fprintf(stdout, "l"); break;
0N/A case ITEM_ReturnAddress:
0N/A jio_fprintf(stdout, "a"); break;
0N/A case ITEM_Object:
0N/A if (!verbose) {
0N/A jio_fprintf(stdout, "A");
0N/A } else {
0N/A unsigned short extra = GET_EXTRA_INFO(type);
0N/A if (extra == 0) {
0N/A jio_fprintf(stdout, "/Null/");
0N/A } else {
0N/A const char *name = ID_to_class_name(context, extra);
0N/A const char *name2 = strrchr(name, '/');
0N/A jio_fprintf(stdout, "/%s/", name2 ? name2 + 1 : name);
0N/A }
0N/A }
0N/A break;
0N/A case ITEM_Char:
0N/A jio_fprintf(stdout, "C"); break;
0N/A case ITEM_Short:
0N/A jio_fprintf(stdout, "S"); break;
0N/A case ITEM_Byte:
0N/A jio_fprintf(stdout, "B"); break;
0N/A case ITEM_NewObject:
0N/A if (!verbose) {
0N/A jio_fprintf(stdout, "@");
0N/A } else {
0N/A int inum = GET_EXTRA_INFO(type);
0N/A fullinfo_type real_type =
0N/A context->instruction_data[inum].operand2.fi;
0N/A jio_fprintf(stdout, ">");
0N/A print_fullinfo_type(context, real_type, JNI_TRUE);
0N/A jio_fprintf(stdout, "<");
0N/A }
0N/A break;
0N/A case ITEM_InitObject:
0N/A jio_fprintf(stdout, verbose ? ">/this/<" : "@");
0N/A break;
0N/A
0N/A default:
0N/A jio_fprintf(stdout, "?"); break;
0N/A }
0N/A for (i = indirection; i-- > 0; )
0N/A jio_fprintf(stdout, "]");
0N/A}
0N/A
0N/A
0N/Astatic void
0N/Aprint_formatted_fieldname(context_type *context, int index)
0N/A{
0N/A JNIEnv *env = context->env;
0N/A jclass cb = context->class;
0N/A const char *classname = JVM_GetCPFieldClassNameUTF(env, cb, index);
0N/A const char *fieldname = JVM_GetCPFieldNameUTF(env, cb, index);
0N/A jio_fprintf(stdout, " <%s.%s>",
0N/A classname ? classname : "", fieldname ? fieldname : "");
0N/A JVM_ReleaseUTF(classname);
0N/A JVM_ReleaseUTF(fieldname);
0N/A}
0N/A
0N/Astatic void
0N/Aprint_formatted_methodname(context_type *context, int index)
0N/A{
0N/A JNIEnv *env = context->env;
0N/A jclass cb = context->class;
0N/A const char *classname = JVM_GetCPMethodClassNameUTF(env, cb, index);
0N/A const char *methodname = JVM_GetCPMethodNameUTF(env, cb, index);
0N/A jio_fprintf(stdout, " <%s.%s>",
0N/A classname ? classname : "", methodname ? methodname : "");
0N/A JVM_ReleaseUTF(classname);
0N/A JVM_ReleaseUTF(methodname);
0N/A}
0N/A
0N/A#endif /*DEBUG*/