Main Page | Namespace List | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

classfile.c File Reference


Detailed Description

Implementation of The Java Virtual Machine Specification, version 2 Chapter 4, The Class File Format.

Manipulation of attributes is performed in cfattrib.c. All other work is done here.

The JVM specification is available from Sun Microsystems' web site at http://java.sun.com/docs/books/vmspec/index.html and may be read online at http://java.sun.com/docs/books/vmspec/2nd-edition/html/VMSpecTOC.doc.html

The Java 5 class file format is available as a PDF file separately at http://java.sun.com/docs/books/vmspec/2nd-edition/ClassFileFormat-final-draft.pdf and was the basis for the ClassFile structure of this implementation.

Todo:
Per spec section 5.4.1, need to verify the contents of the file read in before initializing the class, else throw VerifyError.
Todo:
Need to verify which web document for the Java 5 class file definition is either "official", actually correct, or is the de facto standard.

Control

$URL: https://svn.apache.org/path/name/classfile.c $ $Id: classfile.c 0 09/28/2005 dlydick $

Copyright 2005 The Apache Software Foundation or its licensors, as applicable.

Licensed under the Apache License, Version 2.0 ("the License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and limitations under the License.

Version:
$LastChangedRevision: 0 $
Date:
$LastChangedDate: 09/28/2005 $
Author:
$LastChangedBy: dlydick $ Original code contributed by Daniel Lydick on 09/28/2005.

Reference

Definition in file classfile.c.

#include "arch.h"
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include "jvmcfg.h"
#include "cfmacros.h"
#include "classfile.h"
#include "classpath.h"
#include "exit.h"
#include "jvm.h"
#include "native.h"
#include "util.h"

Go to the source code of this file.

Range check of constant_pool indices.

Range check constant_pool cpidx against max index, with/without typed pointer.

(Testing cpidx < 0 is not checked, since cpidx is an unsigned integer.)

Parameters:
type CONSTANT_xxx_info pointer of data type to be checked
pcfs ClassFile area to be checked
cpidx constant_pool index to be checked
msg Error message to display at failure
Returns:
rvoid


#define CPIDX_RANGE_CHECK(pcfs, cpidx, msg)
 Range check a simple index against the constant_pool_count value.
#define CPTYPEIDX_RANGE_CHECK(type, pcfs, cpidx, msg)
 Range check a typed pointer against the constant_pool_count value.

Read Java object code (class data) from disk files.

Parameters:
filename Null-terminated ASCII string, pathname of JAR file or class file.
Returns:
Pointer to memory area containing class file data. If error detected, rnull is returned and perror("msg") may be called to report the system call problem that caused the particular failure more.


u1classfile_readclassfile (rchar *filename)
 Read a JVM class file.
u1classfile_readjarfile (rchar *filename)
 Read an entire JAR file into temporary disk area and load up one class file from it.

Defines

#define ALLOC_CF_ITEM(spec_typedef, pbytes, pcfsi, binding_struct)
 Allocate either a field_info or method_info structure.
#define ALLOC_CP_INFO(spec_typedef, binding_struct)
 Allocate a cp_info_dup structure containing any generic type of constant_pool entry.
#define READ_SYSCALL_FAILURE(expr, msg)
 Report an error rnull pointer if a system call fails.

Functions

ClassFileclassfile_allocate_primative (jvm_basetype basetype)
 Build up empty JVM class file structure for use by Java primative data types.
static void classfile_c_dummy (void)
ClassFileclassfile_loadclassdata (u1 *pclassfile_image)
 Parse an in-memory JVM class file, create structures to point to various parts of it, per JVM spec pseuco-code structures.
rvoid classfile_unloadclassdata (ClassFile *pcfs)
 Release all heap allocated to a fully loaded ClassFile structure.

Variables

static char * classfile_c_copyright = "\0" "$URL: https://svn.apache.org/path/name/classfile.c $ $Id: classfile.c 0 09/28/2005 dlydick $" " " "Copyright 2005 The Apache Software Foundation or its licensors, as applicable."


Define Documentation

#define ALLOC_CP_INFO spec_typedef,
binding_struct   ) 
 

Value:

misc_adj = sizeof(u1) * CP_INFO_NUM_EMPTIES;                       \
                                                                       \
    cf_item_size = sizeof(spec_typedef)-sizeof(struct binding_struct); \
    pcpd = HEAP_GET_METHOD(misc_adj + sizeof(spec_typedef), rfalse);   \
    memcpy(((rbyte *) pcpd) + misc_adj,pcpbytes,cf_item_size);         \
                                                                       \
    pcpd->empty[0] = FILL_INFO_DUP0;                                   \
    pcpd->empty[1] = FILL_INFO_DUP1;                                   \
    pcpd->empty[2] = FILL_INFO_DUP2;
Allocate a cp_info_dup structure containing any generic type of constant_pool entry.

Allocate space from heap, populate from class file data, and fill in initial pad bytes.

Parameters:
spec_typedef Structure CONSTANT_xxx_info type definition of one of the constant_pool types.
binding_struct Name of local binding structure LOCAL_xxx_binding associated with spec_typedef
Returns:
rvoid

Definition at line 216 of file classfile.c.

Referenced by classfile_loadclassdata().

#define ALLOC_CF_ITEM spec_typedef,
pbytes,
pcfsi,
binding_struct   ) 
 

Value:

cf_item_size = sizeof(spec_typedef) -                          \
                   sizeof(struct binding_struct) -                 \
                   sizeof(attribute_info_dup **);                  \
    pcfsi = HEAP_GET_METHOD(sizeof(spec_typedef), rfalse);  \
    memcpy(((rbyte *) pcfsi),pbytes,cf_item_size);
Allocate either a field_info or method_info structure.

Allocate space from heap and populate from class file data,

Parameters:
spec_typedef Structure field_info or method_info type definition.
pbytes Name (jbyte *) pointer into class file data being parsed pointing to where this structure is found.
pcfsi (spec_typedef *) array[] pointer to the field_info or method_info table slot where the parsed data will be stored.
binding_struct Structure LOCAL_field_binding or LOCAL_method_binding for appropriate spec_typedef type definition.

Definition at line 252 of file classfile.c.

Referenced by classfile_loadclassdata().

#define CPTYPEIDX_RANGE_CHECK type,
pcfs,
cpidx,
msg   ) 
 

Value:

LOAD_SYSCALL_FAILURE(( /* ((((type *) &pcpd->cp)->cpidx) < 0) || */\
                          ((((type *) &pcpd->cp)->cpidx) >=            \
                                          pcfs->constant_pool_count)), \
                         msg,                                          \
                         rnull,                                        \
                         rnull);
Range check a typed pointer against the constant_pool_count value.

Definition at line 290 of file classfile.c.

Referenced by classfile_loadclassdata().

#define CPIDX_RANGE_CHECK pcfs,
cpidx,
msg   ) 
 

Value:

LOAD_SYSCALL_FAILURE((/* (cpidx < 0) || */                      \
                             (cpidx >= pcfs->constant_pool_count)), \
                         msg,                                       \
                         rnull,                                     \
                         rnull);
Range check a simple index against the constant_pool_count value.

Definition at line 303 of file classfile.c.

Referenced by classfile_loadclassdata().

#define READ_SYSCALL_FAILURE expr,
msg   ) 
 

Value:

GENERIC_FAILURE_PTR((expr),                    \
                        DMLMIN,                    \
                        "classfile_readclassfile", \
                        msg,                       \
                        rvoid,                     \
                        rnull,                     \
                        rnull)
Report an error rnull pointer if a system call fails.

Parameters:
expr Any logical expression that returns rtrue or rfalse.
msg Text to display to sysDbgMsg() upon failure
Returns:
If expr is rtrue, return a rnull pointer to the calling function, cast as (rvoid *), else continue with inline code.

Definition at line 1666 of file classfile.c.


Function Documentation

static void classfile_c_dummy void   )  [static]
 

Definition at line 63 of file classfile.c.

ClassFile* classfile_allocate_primative jvm_basetype  basetype  ) 
 

Build up empty JVM class file structure for use by Java primative data types.

Each primative data type needs a minimal class definition to avoid having special cases for class processing logic. Also, treating primatives like classes can provide convenient information on data size types, like (jlong) versus (jint). This can be provided by the base type information, which is passed in here when creating these minimal classes.

The constant pool for such a minima class looks like this:

Todo:
Need to take a hard look at the requirements for java.lang.Class and see if this is sufficient or even accurately implemented.
Parameters:
basetype One of the primative base types BASETYPE_CHAR_x
Returns:
(ClassFile *) to heap-allocated area, used throughtout life of JVM, then released.

For internal use only.

INITIALIZE TO ZEROES all fields so there are automatic rnull pointers in case of failure along the way.

< Magic number for class files

Since java.lang.Object is implied, don't need this slot.

< Declared synthetic ; not present in the source code.

Todo:
Needs more thought
Todo:
Is this assumption valid/meaningful?

Definition at line 121 of file classfile.c.

References ACC_SYNTHETIC, ClassFile::access_flags, ClassFile::attributes, ClassFile::attributes_count, CONSTANT_Utf8_info::bytes, CLASSFILE_MAGIC, CONSTANT_Class, ClassFile::constant_pool, ClassFile::constant_pool_count, CONSTANT_Utf8, ClassFile::fields, ClassFile::fields_count, HEAP_GET_DATA, ClassFile::interfaces, ClassFile::interfaces_count, CONSTANT_Utf8_info::length, ClassFile::magic, ClassFile::major_version, ClassFile::methods, ClassFile::methods_count, ClassFile::minor_version, CONSTANT_Class_info::name_index, PTR_THIS_CP_Class, PTR_THIS_CP_Utf8, rfalse, rnull, rtrue, ClassFile::super_class, CONSTANT_Utf8_info::tag, CONSTANT_Class_info::tag, ClassFile::this_class, VERSION_MAJOR_JDK2, and VERSION_MINOR_DEFAULT.

ClassFile* classfile_loadclassdata u1 pclassfile_image  ) 
 

Parse an in-memory JVM class file, create structures to point to various parts of it, per JVM spec pseuco-code structures.

Interpret the class file data and load up the data structures which access it, such as the fully qualified class names, the code area, etc.

Todo:
Need a much better way to free partially built class structure when an error occurs. The current scheme is only piecemeal and will leave orphaned memory blocks lying around when something is freed that has the pointer to it, case in point, the constant_pool[] table when freeing the main ClassFile structure block on error. The partial solutions of adding heap pointer parameters to LOAD_SYSCALL_FAILURE() and GENERIC_FAILURExxx() macros is not useful here since so many allocations are done in this function.
Parameters:
pclassfile_image Null-terminated ASCII string, pathname of file.
Returns:
Complete class file structure, fully loaded with the real instantiation of the JVM spec pseudo-code structures. If error detected, rnull is returned and perror("msg") may be called to report the system call problem that caused the particular failure more. Status of heap areas will be undefined since a failure like this should be fatal to program execution.
< Convenient alias for DML5

< Magic number for class files

< Magic number for class files

< Convenient alias for DML1

< Convenient alias for DML5

< Convenient alias for DML5

< Convenient alias for DML1

< Convenient alias for DML1

< Convenient alias for DML1

Todo:
Throw UnsupportedClassVersionError for bad versions
< Convenient alias for DML5

< References java.lang.Object < Ref first constant item

< Ref first constant item

< Size of padding for cp_info structures insize of cp_info_dup structure

< Convenient alias for DML1

< Size of padding for cp_info structures insize of cp_info_dup structure

< Convenient alias for DML1

< Invalid basetype due to malformed tag or other error (not in spec)

< Size of padding for cp_info structures insize of cp_info_dup structure

< Convenient alias for DML1

< Convenient alias for DML1

< Size of padding for cp_info structures insize of cp_info_dup structure

< Convenient alias for DML1

< Convenient alias for DML1

< Size of padding for cp_info structures insize of cp_info_dup structure

< Convenient alias for DML1

< Size of padding for cp_info structures insize of cp_info_dup structure

< Size of padding for cp_info structures insize of cp_info_dup structure

< Size of padding for cp_info structures insize of cp_info_dup structure

< Size of padding for cp_info structures insize of cp_info_dup structure

< Size of padding for cp_info structures insize of cp_info_dup structure

< Convenient alias for DML1

< Convenient alias for DML1

< Size of padding for cp_info structures insize of cp_info_dup structure

< Size of padding for cp_info structures insize of cp_info_dup structure

Todo:
Need better and more complete heap free here
< Convenient alias for DML5

< Declared public; may be accessed from outside its package.

< Declared final;no subclasses allowed

< Treat superclass methods specially when invoked by the INVOKESPECIAL instruction

< Is an interface, not a class.

< Declared abstract ; may not be instantiated.

< Declared synthetic ; not present in the source code.

< Declared as an annotation type

< Declared as an enum type

< Convenient alias for DML5

Todo:
Need to free constant_pool[0..n] also if failure
< Convenient alias for DML1

< Convenient alias for DML5

< Convenient alias for DML1

Todo:
HEAP_FREE_METHOD(pcfs->constan_pool[0..n]);
< Convenient alias for DML5

< Convenient alias for DML5

< Declared public; may be accessed from outside its package.

< Declared private ; usable only within the defining class.

< Declared protected ; may be accessed within subclasses.

< Declared static .

< Declared final;no subclasses allowed

< Declared volatile ; cannot be cached

< Declared transient ; not written or read by a persistent object manager

< Declared synthetic ; not present in the source code.

< Declared as an enum type

< Convenient alias for DML1

< Convenient alias for DML1

< Convenient alias for DML1

< Convenient alias for DML5

< Declared public; may be accessed from outside its package.

< Declared private ; usable only within the defining class.

< Declared protected ; may be accessed within subclasses.

< Declared static .

< Declared final;no subclasses allowed

< Declared synchronized ; invokation is wrapped in a monitor lock.

< A bridge method, generated by the compiler.

< Declared with variable number of arguments.

< Declared native ; implemented in a language other than Java.

< Declared abstract ; may not be instantiated.

< Declared strictfp ; floating-point mode is FP-strict

< Declared synthetic ; not present in the source code.

< Convenient alias for DML1

< Convenient alias for DML1

< Convenient alias for DML1

< Convenient alias for DML5

< Convenient alias for DML1

Todo:
Throw VerifyError for classes w/ questionable contents

Definition at line 345 of file classfile.c.

References ACC_ABSTRACT, ACC_ANNOTATION, ACC_BRIDGE, ACC_ENUM, ACC_FINAL, ACC_INTERFACE, ACC_NATIVE, ACC_PRIVATE, ACC_PROTECTED, ACC_PUBLIC, ACC_STATIC, ACC_STRICT, ACC_SUPER, ACC_SYNCHRONIZED, ACC_SYNTHETIC, ACC_TRANSIENT, ACC_VARARGS, ACC_VOLATILE, ClassFile::access_flags, field_info::access_flags, method_info::access_flags, attribute_info_dup::ai, ALLOC_CF_ITEM, ALLOC_CP_INFO, attribute_info::attribute_name_index, field_info::attributes, method_info::attributes, ClassFile::attributes, field_info::attributes_count, method_info::attributes_count, ClassFile::attributes_count, cfattrib_atr2enum(), cfattrib_loadattribute(), cfmsgs_show_constant_pool(), cfmsgs_typemsg(), CLASSFILE_MAGIC, method_info::LOCAL_method_binding::codeatridxJVM, CONSTANT_Class, CONSTANT_CP_DEFAULT_INDEX, CONSTANT_CP_START_INDEX, CONSTANT_Double, CONSTANT_Fieldref, CONSTANT_Float, CONSTANT_Integer, CONSTANT_InterfaceMethodref, CONSTANT_Long, CONSTANT_Methodref, CONSTANT_NameAndType, ClassFile::constant_pool, ClassFile::constant_pool_count, CONSTANT_String, CONSTANT_Utf8, CP_INFO_NUM_EMPTIES, CP_ITEM_SWAP_U2, CP_ITEM_SWAP_U4, CP_TAG, CPIDX_RANGE_CHECK, CPTYPEIDX_RANGE_CHECK, field_info::descriptor_index, method_info::descriptor_index, DMLNORM, method_info::LOCAL_method_binding::excpatridxJVM, ClassFile::fields, ClassFile::fields_count, FILL_INFO_DUP0, FILL_INFO_DUP1, FILL_INFO_DUP2, field_info::LOCAL_field_binding::fluidxJVM, GENERIC_FAILURE1_PTR, GETRI4, GETRS2, HEAP_FREE_METHOD, HEAP_GET_METHOD, ClassFile::interfaces, ClassFile::interfaces_count, jvm_attribute_index_bad, jvm_class_index_null, jvm_field_index_bad, jvm_method_index_bad, jvm_native_method_ordinal_null, LOAD_SYSCALL_FAILURE, LOCAL_ANNOTATIONDEFAULT_ATTRIBUTE, LOCAL_BASETYPE_ERROR, LOCAL_CODE_ATTRIBUTE, LOCAL_CONSTANTVALUE_ATTRIBUTE, LOCAL_DEPRECATED_ATTRIBUTE, LOCAL_ENCLOSINGMETHOD_ATTRIBUTE, LOCAL_EXCEPTIONS_ATTRIBUTE, field_info::LOCAL_field_binding, LOCAL_INNERCLASSES_ATTRIBUTE, LOCAL_LINENUMBERTABLE_ATTRIBUTE, LOCAL_LOCALVARIABLETABLE_ATTRIBUTE, LOCAL_LOCALVARIABLETYPETABLE_ATTRIBUTE, method_info::LOCAL_method_binding, LOCAL_RUNTIMEINVISIBLEANNOTATIONS_ATTRIBUTE, LOCAL_RUNTIMEINVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE, LOCAL_RUNTIMEVISIBLEANNOTATIONS_ATTRIBUTE, LOCAL_RUNTIMEVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE, LOCAL_SIGNATURE_ATTRIBUTE, LOCAL_SOURCEFILE_ATTRIBUTE, LOCAL_SYNTHETIC_ATTRIBUTE, LOCAL_UNKNOWN_ATTRIBUTE, MACHINE_JINT_SWAP, MACHINE_JSHORT_SWAP, ClassFile::magic, ClassFile::major_version, MAKE_PU2, MAKE_PU4, ClassFile::methods, ClassFile::methods_count, ClassFile::minor_version, field_info::name_index, method_info::name_index, method_info::LOCAL_method_binding::nmordJVM, PTR_THIS_CP_Class, PTR_THIS_CP_Fieldref, PTR_THIS_CP_InterfaceMethodref, PTR_THIS_CP_Methodref, PTR_THIS_CP_Utf8, rfalse, rneither_true_nor_false, rnull, rtrue, ClassFile::super_class, sysDbgMsg(), ClassFile::this_class, VERSION_MAJOR_HIGH, VERSION_MAJOR_LOW, and VERSION_MINOR_HIGH.

rvoid classfile_unloadclassdata ClassFile pcfs  ) 
 

Release all heap allocated to a fully loaded ClassFile structure.

Parameters:
pcfs Pointer to a ClassFile structure with all its pieces
Returns:
rvoid Whether it succeeds or fails, returning anything does not make much sense. This is similar to free(3) not returning anything even when a bad pointer was passed in.
< References java.lang.Object

Definition at line 1493 of file classfile.c.

u1* classfile_readclassfile rchar filename  ) 
 

Read a JVM class file.

If a valid class file is read, return pointer to memory area containing its Java class image.

< Convenient alias for DML1

< Convenient alias for DML1

< Convenient alias for DML1

< Convenient alias for DML5

Definition at line 1700 of file classfile.c.

u1* classfile_readjarfile rchar filename  ) 
 

Read an entire JAR file into temporary disk area and load up one class file from it.

If a valid JAR file is read, return pointer to memory area containing the JAva class image of the startup class that was specified in Jar Manifest file. In the future, all classes in the JAR file will be available for loading from the temporary disk area via CLASSPATH.

< Convenient size for a system(3) call

< Convenient size for a system(3) call

< Convenient size for a system(3) call

Todo:
Need a version of this that works on MS Windows

Todo:
Check `pwd` overflow and rnull returned

Definition at line 1763 of file classfile.c.


Variable Documentation

char* classfile_c_copyright = "\0" "$URL: https://svn.apache.org/path/name/classfile.c $ $Id: classfile.c 0 09/28/2005 dlydick $" " " "Copyright 2005 The Apache Software Foundation or its licensors, as applicable." [static]
 

Definition at line 63 of file classfile.c.


Generated on Fri Sep 30 18:49:34 2005 by  doxygen 1.4.4