[BC ABI] Type assertions
Andrew Haley
aph@redhat.com
Mon Aug 30 11:00:00 GMT 2004
This patch adds type assertions to the ouput generated by gcj when
compiling with -findirect-dispatch.
There's a new field, Class.verify, which points to executable code.
When a class is loaded, this code is called. In turn, this calls
_Jv_CheckAssignment for every assertion.
Bryce has pointed out that this would be much more space efficient
with a table of {source, dest} pairs. While true, if this were the
only assertion form it would preclude more complex assertions, and I
want to leave that possibility open for a little while.
This does far too much checking, in that even when we can prove at
compile time that an assigment is legal, we still output the
assertions. Some optimization required.
Minimally tested.
Committed.
Andrew.
2004-08-27 Andrew Haley <aph@redhat.com>
* java/lang/natClassLoader.cc (_Jv_FindClassInCache): Call
klass->verify().
* java/lang/natClass.cc (_Jv_LinkSymbolTable): Add debugging.
(_Jv_LinkSymbolTable): Call Jv_LayoutClass on target class.
2004-08-27 Tom Tromey <tromey@redhat.com>
* java/lang/Class.h (_Jv_IsAssignableFromSlow): Declare as
friend.
* java/lang/natClass.cc (_Jv_CheckAssignment): New function.
(_Jv_IsAssignableFromSlow): New function.
Include VerifyError.h.
* include/jvm.h (_Jv_CheckAssignment): Declare.
Index: libjava/java/lang/Class.h
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/Class.h,v
retrieving revision 1.63.12.6
diff -p -2 -c -w -r1.63.12.6 Class.h
*** libjava/java/lang/Class.h 12 Aug 2004 23:16:27 -0000 1.63.12.6
--- libjava/java/lang/Class.h 27 Aug 2004 19:28:04 -0000
*************** private:
*** 292,295 ****
--- 292,296 ----
friend void *_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface,
int method_idx);
+ friend jboolean _Jv_IsAssignableFromSlow(jclass, jclass);
friend void _Jv_ResolveClassRef (jclass, jclass *);
*************** private:
*** 474,477 ****
--- 476,481 ----
// Security Domain to which this class belongs (or null).
java::security::ProtectionDomain *protectionDomain;
+ // Pointer to verify method for this class.
+ void (*verify)(java::lang::ClassLoader *loader);
// Signers of this class (or null).
JArray<jobject> *hack_signers;
Index: libjava/java/lang/natClass.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/natClass.cc,v
retrieving revision 1.75.2.9
diff -p -2 -c -w -r1.75.2.9 natClass.cc
*** libjava/java/lang/natClass.cc 13 Aug 2004 20:26:33 -0000 1.75.2.9
--- libjava/java/lang/natClass.cc 27 Aug 2004 19:28:04 -0000
*************** details. */
*** 58,61 ****
--- 58,62 ----
#include <gnu/gcj/runtime/MethodRef.h>
#include <gnu/gcj/RawData.h>
+ #include <java/lang/VerifyError.h>
#include <java-cpool.h>
*************** _Jv_CheckArrayStore (jobject arr, jobjec
*** 1167,1170 ****
--- 1168,1236 ----
}
+
+ jboolean
+ _Jv_IsAssignableFromSlow (jclass target, jclass source)
+ {
+ // First, strip arrays.
+ while (target->isArray ())
+ {
+ // If target is array, source must be as well.
+ if (! source->isArray ())
+ return false;
+ target = target->getComponentType ();
+ source = source->getComponentType ();
+ }
+
+ // Quick success.
+ if (target == &java::lang::Object::class$)
+ return true;
+
+ do
+ {
+ if (source == target)
+ return true;
+
+ if (target->isPrimitive () || source->isPrimitive ())
+ return false;
+
+ if (target->isInterface ())
+ {
+ for (int i = 0; i < source->interface_count; ++i)
+ {
+ // We use a recursive call because we also need to
+ // check superinterfaces.
+ if (_Jv_IsAssignableFromSlow (target, source->getInterface (i)))
+ return true;
+ }
+ }
+ source = source->getSuperclass ();
+ }
+ while (source != NULL);
+
+ return false;
+ }
+
+ // Check that SOURCE is assignment compatible with DEST. Both are
+ // loaded with the indicated class loader. Neither is prepared.
+ // Throws appropriate exception, usually VerifyError, on failure.
+ void
+ _Jv_CheckAssignment (java::lang::ClassLoader *loader,
+ _Jv_Utf8Const *source_name,
+ _Jv_Utf8Const *dest_name)
+ {
+ jclass source = _Jv_FindClassFromSignature (source_name->data, loader);
+ jclass dest = _Jv_FindClassFromSignature (dest_name->data, loader);
+
+ if (! _Jv_IsAssignableFromSlow (dest, source))
+ {
+ using namespace java::lang;
+ StringBuffer *sb = new StringBuffer();
+ sb->append(dest->getName());
+ sb->append(JvNewStringLatin1(" isn't assignable from "));
+ sb->append(source->getName());
+ throw new VerifyError(sb->toString());
+ }
+ }
+
#define INITIAL_IOFFSETS_LEN 4
#define INITIAL_IFACES_LEN 4
*************** _Jv_LinkSymbolTable(jclass klass)
*** 1867,1870 ****
--- 1933,1941 ----
// {
+ if (_Jv_isBinaryCompatible (cls))
+ {
+ _Jv_LayoutClass(cls);
+ }
+
if (!field->isResolved ())
_Jv_ResolveField (field, cls->loader);
*************** _Jv_LinkSymbolTable(jclass klass)
*** 1879,1882 ****
--- 1950,1960 ----
// throw new java::lang::LinkageError
// (JvNewStringLatin1
+ if (debug_link)
+ fprintf (stderr, " offsets[%d] = %d (class %s@%p : %s)\n",
+ index,
+ field->u.boffset,
+ (const char*)cls->name->data,
+ cls,
+ (const char*)field->name->data);
// ("field type mismatch with different loaders"));
Index: libjava/java/lang/natClassLoader.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/natClassLoader.cc,v
retrieving revision 1.64.8.6
diff -p -2 -c -w -r1.64.8.6 natClassLoader.cc
*** libjava/java/lang/natClassLoader.cc 12 Aug 2004 23:16:27 -0000 1.64.8.6
--- libjava/java/lang/natClassLoader.cc 27 Aug 2004 19:28:04 -0000
*************** _Jv_FindClassInCache (_Jv_Utf8Const *nam
*** 258,261 ****
--- 258,269 ----
}
}
+ else
+ {
+ // FIXME Is this the right place? dunno.
+ JvSynchronize sync (klass);
+ if (klass->verify)
+ klass->verify (loader);
+ klass->verify = NULL;
+ }
return klass;
Index: libjava/include/jvm.h
===================================================================
RCS file: /cvs/gcc/gcc/libjava/include/jvm.h,v
retrieving revision 1.63.6.2
diff -p -2 -c -w -r1.63.6.2 jvm.h
*** libjava/include/jvm.h 12 Aug 2004 19:29:29 -0000 1.63.6.2
--- libjava/include/jvm.h 27 Aug 2004 19:28:05 -0000
*************** extern "C" void *_Jv_LookupInterfaceMeth
*** 402,405 ****
--- 402,407 ----
int meth_idx);
extern "C" void _Jv_CheckArrayStore (jobject array, jobject obj);
+ extern "C" void _Jv_CheckAssignment (java::lang::ClassLoader *,
+ _Jv_Utf8Const *, _Jv_Utf8Const *);
extern "C" void _Jv_RegisterClass (jclass klass);
extern "C" void _Jv_RegisterClasses (jclass *classes);
2004-08-27 Andrew Haley <aph@redhat.com>
* java-tree.h (enum java_tree_index): Add JTI_VERIFY_IDENTIFIER_NODE.
(verify_identifier_node): New.
(TYPE_VERIFY_METHOD): New.
(struct type_assertion): New type.
* expr.c (type_assertion_eq): New function.
(type_assertion_hash): New function.
(add_type_assertion): New function.
(can_widen_reference_to): Call add_type_assertion().
* decl.c (java_init_decl_processing): Add verify_identifier_node.
* class.c (make_class_data): Initialize TYPE_VERIFY_METHOD (type).
(finish_class): Output TYPE_VERIFY_METHOD (type).
* decl.c (end_java_method): Nullify unused fields.
Index: gcc/java/class.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/class.c,v
retrieving revision 1.180.2.9
diff -p -2 -c -w -r1.180.2.9 class.c
*** gcc/java/class.c 17 Aug 2004 13:42:20 -0000 1.180.2.9
--- gcc/java/class.c 27 Aug 2004 19:29:35 -0000
*************** The Free Software Foundation is independ
*** 44,47 ****
--- 44,48 ----
#include "target.h"
#include "except.h"
+ #include "cgraph.h"
/* DOS brain-damage */
*************** make_class_data (tree type)
*** 1787,1790 ****
--- 1788,1800 ----
PUSH_FIELD_VALUE (cons, "arrayclass", null_pointer_node);
PUSH_FIELD_VALUE (cons, "protectionDomain", null_pointer_node);
+ {
+ tree verify_method = TYPE_VERIFY_METHOD (type);
+ tree verify_method_ref
+ = (verify_method
+ ? build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (verify_method)),
+ verify_method)
+ : null_pointer_node);
+ PUSH_FIELD_VALUE (cons, "verify" , verify_method_ref);
+ }
PUSH_FIELD_VALUE (cons, "hack_signers", null_pointer_node);
PUSH_FIELD_VALUE (cons, "chain", null_pointer_node);
*************** void
*** 1809,1812 ****
--- 1819,1829 ----
finish_class (void)
{
+ if (TYPE_VERIFY_METHOD (output_class))
+ {
+ java_genericize (TYPE_VERIFY_METHOD (current_class));
+ cgraph_finalize_function (TYPE_VERIFY_METHOD (current_class), false);
+ TYPE_ASSERTIONS (current_class) = NULL;
+ }
+
java_expand_catch_classes (current_class);
Index: gcc/java/decl.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/decl.c,v
retrieving revision 1.178.4.5
diff -p -2 -c -w -r1.178.4.5 decl.c
*** gcc/java/decl.c 21 Jul 2004 16:00:45 -0000 1.178.4.5
--- gcc/java/decl.c 27 Aug 2004 19:29:36 -0000
*************** java_init_decl_processing (void)
*** 754,757 ****
--- 754,758 ----
finit_identifier_node = get_identifier ("finit$");
instinit_identifier_node = get_identifier ("instinit$");
+ verify_identifier_node = get_identifier ("<verify>");
void_signature_node = get_identifier ("()V");
length_identifier_node = get_identifier ("length");
*************** java_init_decl_processing (void)
*** 841,844 ****
--- 842,846 ----
PUSH_FIELD (class_type_node, field, "arrayclass", ptr_type_node);
PUSH_FIELD (class_type_node, field, "protectionDomain", ptr_type_node);
+ PUSH_FIELD (class_type_node, field, "verify", ptr_type_node);
PUSH_FIELD (class_type_node, field, "hack_signers", ptr_type_node);
PUSH_FIELD (class_type_node, field, "chain", ptr_type_node);
*************** java_init_decl_processing (void)
*** 1002,1005 ****
--- 1004,1013 ----
build_function_type (boolean_type_node, t),
0, NOT_BUILT_IN, NULL, NULL_TREE);
+ t = tree_cons (NULL_TREE, ptr_type_node,
+ tree_cons (NULL_TREE, ptr_type_node, endlink));
+ soft_check_assignment_node
+ = builtin_function ("_Jv_CheckAssignment",
+ build_function_type (void_type_node, t),
+ 0, NOT_BUILT_IN, NULL, NULL_TREE);
t = tree_cons (NULL_TREE, object_ptr_type_node,
tree_cons (NULL_TREE, object_ptr_type_node, endlink));
*************** end_java_method (void)
*** 1937,1948 ****
DECL_INITIAL (fndecl) = NULL_TREE;
}
! if (! flag_unit_at_a_time)
! {
! /* Nulling these fields when we no longer need them saves
! memory. */
! DECL_SAVED_TREE (fndecl) = NULL;
! DECL_STRUCT_FUNCTION (fndecl) = NULL;
! DECL_INITIAL (fndecl) = NULL_TREE;
! }
current_function_decl = NULL_TREE;
}
--- 1945,1949 ----
DECL_INITIAL (fndecl) = NULL_TREE;
}
!
current_function_decl = NULL_TREE;
}
Index: gcc/java/expr.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/expr.c,v
retrieving revision 1.185.2.8
diff -p -2 -c -w -r1.185.2.8 expr.c
*** gcc/java/expr.c 17 Aug 2004 13:42:19 -0000 1.185.2.8
--- gcc/java/expr.c 27 Aug 2004 19:29:37 -0000
*************** pop_type (tree type)
*** 393,396 ****
--- 393,489 ----
}
+
+ /* Return true if two type assertions are equal. */
+
+ static int
+ type_assertion_eq (const void * k1_p, const void * k2_p)
+ {
+ type_assertion k1 = *(type_assertion *)k1_p;
+ type_assertion k2 = *(type_assertion *)k2_p;
+ return (k1.source_type == k2.source_type
+ && k1.target_type == k2.target_type);
+ }
+
+ /* Hash a type assertion. */
+
+ static hashval_t
+ type_assertion_hash (const void *p)
+ {
+ const type_assertion *k_p = p;
+ hashval_t hash = iterative_hash (&k_p->target_type, sizeof k_p->target_type, 0);
+ return iterative_hash (&k_p->source_type, sizeof k_p->source_type, hash);
+ }
+
+ /* Add an assertion of the form "source_type is a subclass/
+ subinterface of target_type" to the "__verify" function of the
+ current class. */
+
+ static void
+ add_type_assertion (tree source_type, tree target_type)
+ {
+ tree verify_method = TYPE_VERIFY_METHOD (output_class);
+ tree itype = TREE_TYPE (TREE_TYPE (soft_instanceof_node));
+ tree arg;
+
+ if (! verify_method)
+ {
+ arg = build_decl (PARM_DECL, get_identifier ("classLoader"), ptr_type_node);
+ DECL_CONTEXT (arg) = verify_method;
+ DECL_ARG_TYPE (arg) = ptr_type_node;
+ verify_method
+ = build_decl (FUNCTION_DECL, get_identifier ("__verify"),
+ build_function_type (ptr_type_node, end_params_node));
+ DECL_ARGUMENTS (verify_method) = arg;
+ DECL_ARTIFICIAL (verify_method) = 1;
+ TREE_PUBLIC (verify_method) = 0;
+ DECL_EXTERNAL (verify_method) = 0;
+ TREE_PRIVATE (verify_method) = 1;
+ TREE_STATIC (verify_method) = 1;
+ TYPE_VERIFY_METHOD (output_class) = verify_method;
+ build_result_decl (verify_method);
+ DECL_INITIAL (verify_method) = build (BLOCK, void_type_node);
+ DECL_CONTEXT (verify_method) = output_class;
+ TYPE_ASSERTIONS (output_class)
+ = htab_create_ggc (42, type_assertion_hash, type_assertion_eq, NULL);
+ }
+
+ {
+ /* Don't emit the same type assertion twice. */
+ type_assertion as;
+ void **as_pp;
+ as.source_type = source_type;
+ as.target_type = target_type;
+ as_pp = htab_find_slot (TYPE_ASSERTIONS (output_class), &as, true);
+ if (*as_pp)
+ return;
+ *as_pp = ggc_alloc (sizeof (type_assertion));
+ **(type_assertion **)as_pp = as;
+ }
+
+ {
+ tree source_ref, target_ref;
+ tree source_sig = build_java_signature(source_type);
+ tree target_sig = build_java_signature(target_type);
+
+ source_ref
+ = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (source_sig),
+ IDENTIFIER_LENGTH (source_sig)));
+ target_ref
+ = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (target_sig),
+ IDENTIFIER_LENGTH (target_sig)));
+
+ tree args = tree_cons (NULL_TREE, source_ref,
+ build_tree_list (NULL_TREE, target_ref));
+ args = chainon (build_tree_list (NULL_TREE, DECL_ARGUMENTS (verify_method)), args);
+ tree expr = build (CALL_EXPR, itype,
+ build_address_of (soft_check_assignment_node),
+ args, NULL_TREE);
+ DECL_SAVED_TREE (verify_method)
+ = add_stmt_to_compound (DECL_SAVED_TREE (verify_method), itype, expr);
+ }
+ }
+
+
+
/* Return 1 if SOURCE_TYPE can be safely widened to TARGET_TYPE.
Handles array types and interfaces. */
*************** can_widen_reference_to (tree source_type
*** 411,414 ****
--- 504,514 ----
return 1;
+ /* FIXME: This is very pessimistic, in that it checks everything,
+ even if we already know that the types are compatible. If we're
+ to support full Java class loader semantics, we need this.
+ However, we could do something more optimal. */
+ if (! flag_verify_invocations)
+ add_type_assertion (source_type, target_type);
+
if (TYPE_DUMMY (source_type) || TYPE_DUMMY (target_type))
{
Index: gcc/java/java-tree.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/java-tree.h,v
retrieving revision 1.197.2.5
diff -p -2 -c -w -r1.197.2.5 java-tree.h
*** gcc/java/java-tree.h 21 Jul 2004 16:00:45 -0000 1.197.2.5
--- gcc/java/java-tree.h 27 Aug 2004 19:29:38 -0000
*************** enum java_tree_index
*** 335,338 ****
--- 335,339 ----
JTI_FINIT_IDENTIFIER_NODE,
JTI_INSTINIT_IDENTIFIER_NODE,
+ JTI_VERIFY_IDENTIFIER_NODE,
JTI_VOID_SIGNATURE_NODE,
JTI_LENGTH_IDENTIFIER_NODE,
*************** enum java_tree_index
*** 388,391 ****
--- 389,393 ----
JTI_SOFT_INSTANCEOF_NODE,
JTI_SOFT_CHECKCAST_NODE,
+ JTI_SOFT_CHECK_ASSIGNMENT_NODE,
JTI_SOFT_INITCLASS_NODE,
JTI_SOFT_NEWARRAY_NODE,
*************** extern GTY(()) tree java_global_trees[JT
*** 532,535 ****
--- 534,539 ----
#define instinit_identifier_node \
java_global_trees[JTI_INSTINIT_IDENTIFIER_NODE] /* "instinit$" */
+ #define verify_identifier_node \
+ java_global_trees[JTI_VERIFY_IDENTIFIER_NODE] /* "<verify><" */
#define void_signature_node \
java_global_trees[JTI_VOID_SIGNATURE_NODE] /* "()V" */
*************** extern GTY(()) tree java_global_trees[JT
*** 634,637 ****
--- 638,643 ----
#define soft_instanceof_node \
java_global_trees[JTI_SOFT_INSTANCEOF_NODE]
+ #define soft_check_assignment_node \
+ java_global_trees[JTI_SOFT_CHECK_ASSIGNMENT_NODE]
#define soft_checkcast_node \
java_global_trees[JTI_SOFT_CHECKCAST_NODE]
*************** struct treetreehash_entry GTY(())
*** 1003,1006 ****
--- 1009,1018 ----
};
+ typedef struct type_assertion GTY(())
+ {
+ tree source_type;
+ tree target_type;
+ } type_assertion;
+
extern tree java_treetreehash_find (htab_t, tree);
extern tree * java_treetreehash_new (htab_t, tree);
*************** struct lang_decl GTY(())
*** 1076,1081 ****
--- 1088,1095 ----
#define TYPE_CTABLE_DECL(T) (TYPE_LANG_SPECIFIC (T)->ctable_decl)
#define TYPE_CATCH_CLASSES(T) (TYPE_LANG_SPECIFIC (T)->catch_classes)
+ #define TYPE_VERIFY_METHOD(T) (TYPE_LANG_SPECIFIC (T)->verify_method)
#define TYPE_TO_RUNTIME_MAP(T) (TYPE_LANG_SPECIFIC (T)->type_to_runtime_map)
+ #define TYPE_ASSERTIONS(T) (TYPE_LANG_SPECIFIC (T)->type_assertions)
struct lang_type GTY(())
*************** struct lang_type GTY(())
*** 1110,1117 ****
--- 1124,1136 ----
tree catch_classes;
+ tree verify_method; /* The verify method for this class.
+ Used in split verification. */
+
htab_t GTY ((param_is (struct treetreehash_entry))) type_to_runtime_map;
/* The mapping of classes to exception region
markers. */
+ htab_t GTY ((param_is (struct type_assertion))) type_assertions;
+
unsigned pic:1; /* Private Inner Class. */
unsigned poic:1; /* Protected Inner Class. */
More information about the Java-patches
mailing list