#! /bin/sh -e # DP: Reimplement loading constraints (taken from the trunk). dir= if [ $# -eq 3 -a "$2" = '-d' ]; then pdir="-d $3" dir="$3/" elif [ $# -ne 1 ]; then echo >&2 "`basename $0`: script expects -patch|-unpatch as argument" exit 1 fi case "$1" in -patch) patch $pdir -f --no-backup-if-mismatch -p0 < $0 uudecode $0 tar -xv -f class-loader-classes.tar.bz2 -C ${dir}libjava rm -f class-loader-classes.tar.bz2* ;; -unpatch) patch $pdir -f --no-backup-if-mismatch -R -p0 < $0 ;; *) echo >&2 "`basename $0`: script expects -patch|-unpatch as argument" exit 1 esac exit 0 2008-03-13 Tom Tromey * java/lang/natClassLoader.cc (_Jv_RegisterInitiatingLoader): Check loading constraints. (_Jv_CheckOrCreateLoadingConstraint): New function. * java/lang/ClassLoader.java (loadingConstraints): New field. * link.cc (_Jv_Linker::find_field): Use _Jv_CheckOrCreateLoadingConstraint. (_Jv_Linker::check_loading_constraints): New function. (_Jv_Linker::resolve_method_entry): Use check_loading_constraints. (_Jv_Linker::append_partial_itable): Likewise. (_Jv_Linker::layout_vtable_methods): Likewise. * include/jvm.h (_Jv_Linker::check_loading_constraints): Declare. (_Jv_CheckOrCreateLoadingConstraint): Declare. --- libjava/include/jvm.h.orig 2008-03-21 22:09:54.249100837 +0100 +++ libjava/include/jvm.h 2008-03-21 22:10:20.739500439 +0100 @@ -1,6 +1,6 @@ // jvm.h - Header file for private implementation information. -*- c++ -*- -/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation +/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation This file is part of libgcj. @@ -265,6 +265,7 @@ jclass, jclass *); static _Jv_Field *find_field(jclass, jclass, jclass *, _Jv_Utf8Const *, _Jv_Utf8Const *); + static void check_loading_constraints (_Jv_Method *, jclass, jclass); static void prepare_constant_time_tables(jclass); static jshort get_interfaces(jclass, _Jv_ifaces *); static void link_symbol_table(jclass); @@ -557,6 +558,9 @@ jboolean is_jni_call = true, jclass iface = NULL); +extern void _Jv_CheckOrCreateLoadingConstraint (jclass, + java::lang::ClassLoader *); + extern jobject _Jv_NewMultiArray (jclass, jint ndims, jint* dims) __attribute__((__malloc__)); --- libjava/java/lang/ClassLoader.h.orig 2008-03-21 22:09:54.249100837 +0100 +++ libjava/java/lang/ClassLoader.h 2008-03-21 22:10:20.749500589 +0100 @@ -86,6 +86,7 @@ void checkInitialized(); public: // actually package-private ::java::util::HashMap * __attribute__((aligned(__alignof__( ::java::lang::Object)))) loadedClasses; + ::java::util::HashMap * loadingConstraints; ::java::util::HashMap * definedPackages; private: ::java::lang::ClassLoader * parent; --- libjava/java/lang/ClassLoader.java.orig 2008-03-21 22:09:54.249100837 +0100 +++ libjava/java/lang/ClassLoader.java 2008-03-21 22:10:20.749500589 +0100 @@ -1,5 +1,5 @@ /* ClassLoader.java -- responsible for loading classes into the VM - Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc. + Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -45,6 +45,7 @@ import java.io.IOException; import java.io.InputStream; +import java.lang.ref.WeakReference; import java.net.URL; import java.nio.ByteBuffer; import java.security.CodeSource; @@ -130,6 +131,15 @@ final HashMap loadedClasses = new HashMap(); /** + * Loading constraints registered with this classloader. This maps + * a class name to a weak reference to a class. When the reference + * is non-null, it means that a reference to the name must resolve + * to the indicated class. + */ + final HashMap> loadingConstraints + = new HashMap>(); + + /** * All packages defined by this classloader. It is not private in order to * allow native code (and trusted subclasses) access to this field. */ --- libjava/java/lang/natClassLoader.cc.orig 2008-03-21 22:09:54.249100837 +0100 +++ libjava/java/lang/natClassLoader.cc 2008-03-21 22:10:20.749500589 +0100 @@ -1,6 +1,6 @@ // natClassLoader.cc - Implementation of java.lang.ClassLoader native methods. -/* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation +/* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2008 Free Software Foundation This file is part of libgcj. @@ -41,6 +41,7 @@ #include #include #include +#include #include #include #include @@ -143,7 +144,21 @@ // them later. return; } - loader->loadedClasses->put(klass->name->toString(), klass); + + JvSynchronize sync (loader->loadingConstraints); + + using namespace java::lang::ref; + + jstring name = klass->getName(); + WeakReference *ref = (WeakReference *) loader->loadingConstraints->get (name); + if (ref) + { + jclass constraint = (jclass) ref->get(); + if (constraint && constraint != klass) + throw new java::lang::LinkageError(JvNewStringLatin1("loading constraint violated")); + } + loader->loadingConstraints->put(name, new WeakReference(klass)); + loader->loadedClasses->put(name, klass); } // If we found an error while defining an interpreted class, we must @@ -156,6 +171,46 @@ loader->loadedClasses->remove(klass->name->toString()); } +// Check a loading constraint. In particular check that, if there is +// a constraint for the name of KLASS in LOADER, that it maps to +// KLASS. If there is no such constraint, make a new one. If the +// constraint is violated, throw an exception. Do nothing for +// primitive types. +void +_Jv_CheckOrCreateLoadingConstraint (jclass klass, + java::lang::ClassLoader *loader) +{ + // Strip arrays. + while (klass->isArray()) + klass = klass->getComponentType(); + // Ignore primitive types. + if (klass->isPrimitive()) + return; + + if (! loader) + loader = java::lang::VMClassLoader::bootLoader; + jstring name = klass->getName(); + + JvSynchronize sync (loader->loadingConstraints); + + using namespace java::lang::ref; + + WeakReference *ref = (WeakReference *) loader->loadingConstraints->get (name); + if (ref) + { + jclass constraint = (jclass) ref->get(); + if (constraint) + { + if (klass != constraint) + throw new java::lang::LinkageError(JvNewStringLatin1("loading constraint violated")); + // Otherwise, all is ok. + return; + } + } + // No constraint (or old constraint GC'd). Make a new one. + loader->loadingConstraints->put(name, new WeakReference(klass)); +} + // Class registration. // --- libjava/link.cc.orig 2008-03-21 22:09:54.249100837 +0100 +++ libjava/link.cc 2008-03-21 22:24:51.692638568 +0100 @@ -246,13 +246,9 @@ if (_Jv_CheckAccess (klass, *found_class, the_field->flags)) { // Note that the field returned by find_field_helper is always - // resolved. There's no point checking class loaders here, - // since we already did the work to look up all the types. - // FIXME: being lazy here would be nice. - if (the_field->type != field_type) - throw new java::lang::LinkageError - (JvNewStringLatin1 - ("field type mismatch with different loaders")); + // resolved. However, we still use the constraint mechanism + // because this may affect other lookups. + _Jv_CheckOrCreateLoadingConstraint (klass, (*found_class)->loader); } else { @@ -269,6 +265,23 @@ return the_field; } +// Check loading constraints for method. +void +_Jv_Linker::check_loading_constraints (_Jv_Method *method, jclass self_class, + jclass other_class) +{ + JArray *klass_args; + jclass klass_return; + + _Jv_GetTypesFromSignature (method, self_class, &klass_args, &klass_return); + jclass *klass_arg = elements (klass_args); + java::lang::ClassLoader *found_loader = other_class->loader; + + _Jv_CheckOrCreateLoadingConstraint (klass_return, found_loader); + for (int i = 0; i < klass_args->length; i++) + _Jv_CheckOrCreateLoadingConstraint (*(klass_arg++), found_loader); +} + _Jv_Method * _Jv_Linker::resolve_method_entry (jclass klass, jclass &found_class, int class_index, int name_and_type_index, @@ -359,6 +372,11 @@ throw new java::lang::NoSuchMethodError (sb->toString()); } + // if (found_class->loader != klass->loader), then we must actually + // check that the types of arguments correspond. JVMS 5.4.3.3. + if (found_class->loader != klass->loader) + check_loading_constraints (the_method, klass, found_class); + return the_method; } @@ -891,7 +909,8 @@ continue; meth = NULL; - for (jclass cl = klass; cl; cl = cl->getSuperclass()) + jclass cl; + for (cl = klass; cl; cl = cl->getSuperclass()) { meth = _Jv_GetMethodLocal (cl, iface->methods[j].name, iface->methods[j].signature); @@ -913,6 +932,9 @@ itable[pos] = (void *) &_Jv_ThrowAbstractMethodError; else itable[pos] = meth->ncode; + + if (cl->loader != iface->loader) + check_loading_constraints (meth, cl, iface); } else { @@ -1467,6 +1489,11 @@ sb->append(_Jv_GetMethodString(declarer, super_meth)); throw new VerifyError(sb->toString()); } + else if (declarer->loader != klass->loader) + { + // JVMS 5.4.2. + check_loading_constraints (meth, klass, declarer); + } } } begin 644 class-loader-classes.tar.bz2 M0EIH.3%!62936;AC>/P`$B'_____________________________________ M________X!6?>GEKK>IQ%5[=.%0070O6<+;>];;V[[P&^V:]RKXV;8WK.('; M!H:"@#;-,'G=N]V!J:30!&B:8AJGFF0-)HGFI/0Q/5/*;3:IY3U-M1,FI^J9 MXE/`U/5!^J>IYJ>IIIM)LBGIE-IZ)F13]3U3>I-/94:>4>F9)IZDT]0]IJ@V MIXH&TGE-/3T:HTQ/4]&C1IJ/4:`T031-,"9":8FIZ&2GM)IZ3$TR:FU&C(GM M*/4]1F:4]$>H>4]1X4-I!M1Z&4-!ZC1D]1H]"!B:'H3`:3(,F:FT0>4TQ#0! MB-/2:;4T&Q)IZ@:$:!!-11ZGA1C4](V4-`]3(/:H]1Z@`!D`!H:`:`#3T0`` M!H`!H````]0#30T``]0&@-`-`--`:`)-20C4*>1HRIZGJ#3:GFE,F::@PAZ0 M'J#)Z@TTT!D;4/1-J>IH!ZAH:`T/*`-#0```!Z@`&FC0````&@!D!H`$```` M``````!,```````````F``````````````````$P$P)$DT)IH),-)IDTT(VF M(1Z*>TIZ3R3PIDT&(#33$T>D&C0R-`-`!H````!H`:``&F@````````:`+40 MP,AXAHV[384=&=*:I_H[F;&$@=O37@`S\@]IP%T.,X'0,ER*QC?1QSRUW6N" M,PD32^QGX&7VT)L,"HQLLV_A$&=,*!-;&"*90NL**ZQZ$2E+7^&68,XD MZ2L-J&VEA="HDKM#=(RL%]F7&*"*JQ;(5';J(%2;Q.A.*U0,-@1P8$.WD]@LZ*(+8/Q MM2^67GXUDF$[MV*91J,-Q06UU8-K,$JF";`4%N=>X=8:D)$PT3#&JK1PF M`6FCDVM\`OR89!US!(FP$DN'GR:J3I?3Z^*W$,PN@9,1IT2%8*V!L&,O3JI5 M9V7FG=(-/TD2LS&X4'>0H"1[(-_EJ1(\?O,8Y'5]&9\O%]=JW;YJ->]TMO&3 MUW0O>4K#;_1LB0M"2)(89D*IVLG-0=09=D.?N@!8^15(J,.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