diff options
author | Alexandre Oliva <aoliva@redhat.com> | 2007-03-07 07:27:25 +0000 |
---|---|---|
committer | Alexandre Oliva <aoliva@gcc.gnu.org> | 2007-03-07 07:27:25 +0000 |
commit | 18fa3240db054b98ef9464cbcedb186d819217fd (patch) | |
tree | 65dc1e16680788d96a4fb13f1406c83419164f7c /libjava | |
parent | dd77833100a12f219e7278685059dd82196c78f4 (diff) | |
download | gcc-18fa3240db054b98ef9464cbcedb186d819217fd.tar.gz |
ffi.h.in (ffi_closure_alloc, [...]): New.
libffi/ChangeLog:
* include/ffi.h.in (ffi_closure_alloc, ffi_closure_free): New.
(ffi_prep_closure_loc): New.
(ffi_prep_raw_closure_loc): New.
(ffi_prep_java_raw_closure_loc): New.
* src/closures.c: New file.
* src/dlmalloc.c [FFI_MMAP_EXEC_WRIT] (struct malloc_segment):
Replace sflags with exec_offset.
[FFI_MMAP_EXEC_WRIT] (mmap_exec_offset, add_segment_exec_offset,
sub_segment_exec_offset): New macros.
(get_segment_flags, set_segment_flags, check_segment_merge): New
macros.
(is_mmapped_segment, is_extern_segment): Use get_segment_flags.
(add_segment, sys_alloc, create_mspace, create_mspace_with_base,
destroy_mspace): Use new macros.
(sys_alloc): Silence warning.
* Makefile.am (libffi_la_SOURCES): Add src/closures.c.
* Makefile.in: Rebuilt.
* src/prep_cif [FFI_CLOSURES] (ffi_prep_closure): Implement in
terms of ffi_prep_closure_loc.
* src/raw_api.c (ffi_prep_raw_closure_loc): Renamed and adjusted
from...
(ffi_prep_raw_closure): ... this. Re-implement in terms of the
renamed version.
* src/java_raw_api (ffi_prep_java_raw_closure_loc): Renamed and
adjusted from...
(ffi_prep_java_raw_closure): ... this. Re-implement in terms of
the renamed version.
* src/alpha/ffi.c (ffi_prep_closure_loc): Renamed from
(ffi_prep_closure): ... this.
* src/pa/ffi.c: Likewise.
* src/cris/ffi.c: Likewise. Adjust.
* src/frv/ffi.c: Likewise.
* src/ia64/ffi.c: Likewise.
* src/mips/ffi.c: Likewise.
* src/powerpc/ffi_darwin.c: Likewise.
* src/s390/ffi.c: Likewise.
* src/sh/ffi.c: Likewise.
* src/sh64/ffi.c: Likewise.
* src/sparc/ffi.c: Likewise.
* src/x86/ffi64.c: Likewise.
* src/x86/ffi.c: Likewise.
(FFI_INIT_TRAMPOLINE): Adjust.
(ffi_prep_raw_closure_loc): Renamed and adjusted from...
(ffi_prep_raw_closure): ... this.
* src/powerpc/ffi.c (ffi_prep_closure_loc): Renamed from
(ffi_prep_closure): ... this.
(flush_icache): Adjust.
boehm-gc/ChangeLog:
* include/gc.h (GC_REGISTER_FINALIZER_UNREACHABLE): New.
(GC_register_finalizer_unreachable): Declare.
(GC_debug_register_finalizer_unreachable): Declare.
* finalize.c (GC_unreachable_finalize_mark_proc): New.
(GC_register_finalizer_unreachable): New.
(GC_finalize): Handle it.
* dbg_mlc.c (GC_debug_register_finalizer_unreachable): New.
(GC_debug_register_finalizer_no_order): Fix whitespace.
libjava/ChangeLog:
* include/jvm.h (_Jv_ClosureListFinalizer): New.
(_Jv_Linker::create_error_method): Adjust.
* boehm.cc (_Jv_ClosureListFinalizer): New.
* nogc.cc (_Jv_ClosureListFinalizer): New.
* java/lang/Class.h (class _Jv_ClosureList): New.
(class java::lang::Class): Declare it as friend.
* java/lang/natClass.cc (_Jv_ClosureList::releaseClosures): New.
(_Jv_ClosureList::registerClousure): New.
* include/execution.h (_Jv_ExecutionEngine): Add get_closure_list.
(_Jv_CompiledEngine::do_get_closure_list): New.
(_Jv_CompiledEngine::_Jv_CompiledEngine): Use it.
(_Jv_IndirectCompiledClass): Add closures.
(_Jv_IndirectCompiledEngine::get_aux_info): New.
(_Jv_IndirectCompiledEngine::do_allocate_field_initializers): Use
it.
(_Jv_IndirectCompiledEngine::do_get_closure_list): New.
(_Jv_IndirectCompiledEngine::_Jv_IndirectCompiledEngine): Use it.
(_Jv_InterpreterEngine::do_get_closure_list): Declare.
(_Jv_InterpreterEngine::_Jv_InterpreterEngine): Use it.
* interpret.cc (FFI_PREP_RAW_CLOSURE): Use _loc variants.
(node_closure): Add closure list.
(_Jv_InterpMethod::ncode): Add jclass argument. Use
ffi_closure_alloc and the separate code pointer. Register the
closure for finalization.
(_Jv_JNIMethod::ncode): Likewise.
(_Jv_InterpreterEngine::do_create_ncode): Pass klass to ncode.
(_Jv_InterpreterEngine::do_get_closure_list): New.
* include/java-interp.h (_Jv_InterpMethod::ncode): Adjust.
(_Jv_InterpClass): Add closures field.
(_Jv_JNIMethod::ncode): Adjust.
* defineclass.cc (_Jv_ClassReader::handleCodeAttribute): Adjust.
(_Jv_ClassReader::handleMethodsEnd): Likewise.
* link.cc (struct method_closure): Add closure list.
(_Jv_Linker::create_error_method): Add jclass argument. Use
ffi_closure_alloc and the separate code pointer. Register the
closure for finalization.
(_Jv_Linker::link_symbol_table): Remove outdated comment about
sharing of otable and atable. Adjust.
* java/lang/reflect/natVMProxy.cc (ncode_closure): Add closure
list.
(ncode): Add jclass argument. Use ffi_closure_alloc and the
separate code pointer. Register the closure for finalization.
(java::lang::reflect::VMProxy::generateProxyClass): Adjust.
* testsuite/libjava.jar/TestClosureGC.java: New.
* testsuite/libjava.jar/TestClosureGC.out: New.
* testsuite/libjava.jar/TestClosureGC.xfail: New.
* testsuite/libjava.jar/TestClosureGC.jar: New.
From-SVN: r122652
Diffstat (limited to 'libjava')
-rw-r--r-- | libjava/ChangeLog | 50 | ||||
-rw-r--r-- | libjava/boehm.cc | 25 | ||||
-rw-r--r-- | libjava/defineclass.cc | 7 | ||||
-rw-r--r-- | libjava/include/execution.h | 49 | ||||
-rw-r--r-- | libjava/include/java-interp.h | 5 | ||||
-rw-r--r-- | libjava/include/jvm.h | 6 | ||||
-rw-r--r-- | libjava/interpret.cc | 49 | ||||
-rw-r--r-- | libjava/java/lang/Class.h | 10 | ||||
-rw-r--r-- | libjava/java/lang/natClass.cc | 22 | ||||
-rw-r--r-- | libjava/java/lang/reflect/natVMProxy.cc | 27 | ||||
-rw-r--r-- | libjava/link.cc | 35 | ||||
-rw-r--r-- | libjava/nogc.cc | 11 | ||||
-rw-r--r-- | libjava/testsuite/libjava.jar/TestClosureGC.jar | bin | 0 -> 3997 bytes | |||
-rw-r--r-- | libjava/testsuite/libjava.jar/TestClosureGC.java | 116 | ||||
-rw-r--r-- | libjava/testsuite/libjava.jar/TestClosureGC.out | 1 | ||||
-rw-r--r-- | libjava/testsuite/libjava.jar/TestClosureGC.xfail | 1 |
16 files changed, 364 insertions, 50 deletions
diff --git a/libjava/ChangeLog b/libjava/ChangeLog index 278642f62f4..23bf0a401f6 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,53 @@ +2007-03-07 Alexandre Oliva <aoliva@redhat.com> + + * include/jvm.h (_Jv_ClosureListFinalizer): New. + (_Jv_Linker::create_error_method): Adjust. + * boehm.cc (_Jv_ClosureListFinalizer): New. + * nogc.cc (_Jv_ClosureListFinalizer): New. + * java/lang/Class.h (class _Jv_ClosureList): New. + (class java::lang::Class): Declare it as friend. + * java/lang/natClass.cc (_Jv_ClosureList::releaseClosures): New. + (_Jv_ClosureList::registerClousure): New. + * include/execution.h (_Jv_ExecutionEngine): Add get_closure_list. + (_Jv_CompiledEngine::do_get_closure_list): New. + (_Jv_CompiledEngine::_Jv_CompiledEngine): Use it. + (_Jv_IndirectCompiledClass): Add closures. + (_Jv_IndirectCompiledEngine::get_aux_info): New. + (_Jv_IndirectCompiledEngine::do_allocate_field_initializers): Use + it. + (_Jv_IndirectCompiledEngine::do_get_closure_list): New. + (_Jv_IndirectCompiledEngine::_Jv_IndirectCompiledEngine): Use it. + (_Jv_InterpreterEngine::do_get_closure_list): Declare. + (_Jv_InterpreterEngine::_Jv_InterpreterEngine): Use it. + * interpret.cc (FFI_PREP_RAW_CLOSURE): Use _loc variants. + (node_closure): Add closure list. + (_Jv_InterpMethod::ncode): Add jclass argument. Use + ffi_closure_alloc and the separate code pointer. Register the + closure for finalization. + (_Jv_JNIMethod::ncode): Likewise. + (_Jv_InterpreterEngine::do_create_ncode): Pass klass to ncode. + (_Jv_InterpreterEngine::do_get_closure_list): New. + * include/java-interp.h (_Jv_InterpMethod::ncode): Adjust. + (_Jv_InterpClass): Add closures field. + (_Jv_JNIMethod::ncode): Adjust. + * defineclass.cc (_Jv_ClassReader::handleCodeAttribute): Adjust. + (_Jv_ClassReader::handleMethodsEnd): Likewise. + * link.cc (struct method_closure): Add closure list. + (_Jv_Linker::create_error_method): Add jclass argument. Use + ffi_closure_alloc and the separate code pointer. Register the + closure for finalization. + (_Jv_Linker::link_symbol_table): Remove outdated comment about + sharing of otable and atable. Adjust. + * java/lang/reflect/natVMProxy.cc (ncode_closure): Add closure + list. + (ncode): Add jclass argument. Use ffi_closure_alloc and the + separate code pointer. Register the closure for finalization. + (java::lang::reflect::VMProxy::generateProxyClass): Adjust. + * testsuite/libjava.jar/TestClosureGC.java: New. + * testsuite/libjava.jar/TestClosureGC.out: New. + * testsuite/libjava.jar/TestClosureGC.xfail: New. + * testsuite/libjava.jar/TestClosureGC.jar: New. + 2007-03-06 Kyle Galloway <kgallowa@redhat.com> * classpath/gnu/classpath/jdwp/exception/AbsentInformationException.java: New file. diff --git a/libjava/boehm.cc b/libjava/boehm.cc index 19d7e53133e..66860dd50da 100644 --- a/libjava/boehm.cc +++ b/libjava/boehm.cc @@ -1,6 +1,6 @@ // boehm.cc - interface between libjava and Boehm GC. -/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 +/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation This file is part of libgcj. @@ -380,6 +380,29 @@ _Jv_AllocRawObj (jsize size) return (void *) GC_MALLOC (size ? size : 1); } +typedef _Jv_ClosureList *closure_list_pointer; + +/* Release closures in a _Jv_ClosureList. */ +static void +finalize_closure_list (GC_PTR obj, GC_PTR) +{ + _Jv_ClosureList **clpp = (_Jv_ClosureList **)obj; + _Jv_ClosureList::releaseClosures (clpp); +} + +/* Allocate a double-indirect pointer to a _Jv_ClosureList that will + get garbage-collected after this double-indirect pointer becomes + unreachable by any other objects, including finalizable ones. */ +_Jv_ClosureList ** +_Jv_ClosureListFinalizer () +{ + _Jv_ClosureList **clpp; + clpp = (_Jv_ClosureList **)_Jv_AllocBytes (sizeof (*clpp)); + GC_REGISTER_FINALIZER_UNREACHABLE (clpp, finalize_closure_list, + NULL, NULL, NULL); + return clpp; +} + static void call_finalizer (GC_PTR obj, GC_PTR client_data) { diff --git a/libjava/defineclass.cc b/libjava/defineclass.cc index c66fff84b7f..0f0da77d6eb 100644 --- a/libjava/defineclass.cc +++ b/libjava/defineclass.cc @@ -1,6 +1,7 @@ // defineclass.cc - defining a class from .class format. -/* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation +/* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 + Free Software Foundation This file is part of libgcj. @@ -1695,7 +1696,7 @@ void _Jv_ClassReader::handleCodeAttribute // call a static method of an interpreted class from precompiled // code without first resolving the class (that will happen // during class initialization instead). - method->self->ncode = method->ncode (); + method->self->ncode = method->ncode (def); } } @@ -1740,7 +1741,7 @@ void _Jv_ClassReader::handleMethodsEnd () // interpreted class from precompiled code without // first resolving the class (that will happen // during class initialization instead). - method->ncode = m->ncode (); + method->ncode = m->ncode (def); } } } diff --git a/libjava/include/execution.h b/libjava/include/execution.h index 279e9c1b872..eac6133c5b7 100644 --- a/libjava/include/execution.h +++ b/libjava/include/execution.h @@ -1,6 +1,6 @@ // execution.h - Execution engines. -*- c++ -*- -/* Copyright (C) 2004, 2006 Free Software Foundation +/* Copyright (C) 2004, 2006, 2007 Free Software Foundation This file is part of libgcj. @@ -29,6 +29,7 @@ struct _Jv_ExecutionEngine _Jv_ResolvedMethod *(*resolve_method) (_Jv_Method *, jclass, jboolean); void (*post_miranda_hook) (jclass); + _Jv_ClosureList **(*get_closure_list) (jclass); }; // This handles gcj-compiled code except that compiled with @@ -77,6 +78,11 @@ struct _Jv_CompiledEngine : public _Jv_ExecutionEngine // Not needed. } + static _Jv_ClosureList **do_get_closure_list (jclass) + { + return NULL; + } + _Jv_CompiledEngine () { unregister = do_unregister; @@ -87,6 +93,7 @@ struct _Jv_CompiledEngine : public _Jv_ExecutionEngine create_ncode = do_create_ncode; resolve_method = do_resolve_method; post_miranda_hook = do_post_miranda_hook; + get_closure_list = do_get_closure_list; } // These operators make it so we don't have to link in libstdc++. @@ -105,6 +112,7 @@ class _Jv_IndirectCompiledClass { public: void **field_initializers; + _Jv_ClosureList **closures; }; // This handles gcj-compiled code compiled with -findirect-classes. @@ -114,14 +122,32 @@ struct _Jv_IndirectCompiledEngine : public _Jv_CompiledEngine { allocate_static_fields = do_allocate_static_fields; allocate_field_initializers = do_allocate_field_initializers; + get_closure_list = do_get_closure_list; } + static _Jv_IndirectCompiledClass *get_aux_info (jclass klass) + { + _Jv_IndirectCompiledClass *aux = + (_Jv_IndirectCompiledClass*)klass->aux_info; + if (!aux) + { + aux = (_Jv_IndirectCompiledClass*) + _Jv_AllocRawObj (sizeof (_Jv_IndirectCompiledClass)); + klass->aux_info = aux; + } + + return aux; + } + static void do_allocate_field_initializers (jclass klass) { - _Jv_IndirectCompiledClass *aux - = (_Jv_IndirectCompiledClass*) - _Jv_AllocRawObj (sizeof (_Jv_IndirectCompiledClass)); - klass->aux_info = aux; + _Jv_IndirectCompiledClass *aux = get_aux_info (klass); + if (!aux) + { + aux = (_Jv_IndirectCompiledClass*) + _Jv_AllocRawObj (sizeof (_Jv_IndirectCompiledClass)); + klass->aux_info = aux; + } aux->field_initializers = (void **)_Jv_Malloc (klass->field_count * sizeof (void*)); @@ -172,6 +198,16 @@ struct _Jv_IndirectCompiledEngine : public _Jv_CompiledEngine } _Jv_Free (aux->field_initializers); } + + static _Jv_ClosureList **do_get_closure_list (jclass klass) + { + _Jv_IndirectCompiledClass *aux = get_aux_info (klass); + + if (!aux->closures) + aux->closures = _Jv_ClosureListFinalizer (); + + return aux->closures; + } }; @@ -203,6 +239,8 @@ class _Jv_InterpreterEngine : public _Jv_ExecutionEngine static void do_post_miranda_hook (jclass); + static _Jv_ClosureList **do_get_closure_list (jclass klass); + _Jv_InterpreterEngine () { unregister = do_unregister; @@ -213,6 +251,7 @@ class _Jv_InterpreterEngine : public _Jv_ExecutionEngine create_ncode = do_create_ncode; resolve_method = do_resolve_method; post_miranda_hook = do_post_miranda_hook; + get_closure_list = do_get_closure_list; } // These operators make it so we don't have to link in libstdc++. diff --git a/libjava/include/java-interp.h b/libjava/include/java-interp.h index e23baab9bb2..c294cc81896 100644 --- a/libjava/include/java-interp.h +++ b/libjava/include/java-interp.h @@ -202,7 +202,7 @@ class _Jv_InterpMethod : public _Jv_MethodBase } // return the method's invocation pointer (a stub). - void *ncode (); + void *ncode (jclass); void compile (const void * const *); static void run_normal (ffi_cif*, void*, ffi_raw*, void*); @@ -293,6 +293,7 @@ class _Jv_InterpClass _Jv_MethodBase **interpreted_methods; _Jv_ushort *field_initializers; jstring source_file_name; + _Jv_ClosureList **closures; friend class _Jv_ClassReader; friend class _Jv_InterpMethod; @@ -341,7 +342,7 @@ class _Jv_JNIMethod : public _Jv_MethodBase // This function is used when making a JNI call from the interpreter. static void call (ffi_cif *, void *, ffi_raw *, void *); - void *ncode (); + void *ncode (jclass); friend class _Jv_ClassReader; friend class _Jv_InterpreterEngine; diff --git a/libjava/include/jvm.h b/libjava/include/jvm.h index 9a99a954b28..02974e8c7ea 100644 --- a/libjava/include/jvm.h +++ b/libjava/include/jvm.h @@ -288,7 +288,7 @@ private: _Jv_Utf8Const *method_signature, jclass *found_class, bool check_perms = true); - static void *create_error_method(_Jv_Utf8Const *); + static void *create_error_method(_Jv_Utf8Const *, jclass); /* The least significant bit of the signature pointer in a symbol table is set to 1 by the compiler if the reference is "special", @@ -341,6 +341,10 @@ void *_Jv_AllocBytes (jsize size) __attribute__((__malloc__)); /* Allocate space for a new non-Java object, which does not have the usual Java object header but may contain pointers to other GC'ed objects. */ void *_Jv_AllocRawObj (jsize size) __attribute__((__malloc__)); +/* Allocate a double-indirect pointer to a _Jv_ClosureList such that + the _Jv_ClosureList gets automatically finalized when it is no + longer reachable, not even by other finalizable objects. */ +_Jv_ClosureList **_Jv_ClosureListFinalizer (void) __attribute__((__malloc__)); /* Explicitly throw an out-of-memory exception. */ void _Jv_ThrowNoMemory() __attribute__((__noreturn__)); /* Allocate an object with a single pointer. The first word is reserved diff --git a/libjava/interpret.cc b/libjava/interpret.cc index 79276258c3d..ac23b060240 100644 --- a/libjava/interpret.cc +++ b/libjava/interpret.cc @@ -1255,10 +1255,10 @@ _Jv_init_cif (_Jv_Utf8Const* signature, } #if FFI_NATIVE_RAW_API -# define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure +# define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure_loc # define FFI_RAW_SIZE ffi_raw_size #else -# define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure +# define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure_loc # define FFI_RAW_SIZE ffi_java_raw_size #endif @@ -1269,6 +1269,7 @@ _Jv_init_cif (_Jv_Utf8Const* signature, typedef struct { ffi_raw_closure closure; + _Jv_ClosureList list; ffi_cif cif; ffi_type *arg_types[0]; } ncode_closure; @@ -1276,7 +1277,7 @@ typedef struct { typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_raw*,void*); void * -_Jv_InterpMethod::ncode () +_Jv_InterpMethod::ncode (jclass klass) { using namespace java::lang::reflect; @@ -1286,9 +1287,12 @@ _Jv_InterpMethod::ncode () jboolean staticp = (self->accflags & Modifier::STATIC) != 0; int arg_count = _Jv_count_arguments (self->signature, staticp); + void *code; ncode_closure *closure = - (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure) - + arg_count * sizeof (ffi_type*)); + (ncode_closure*)ffi_closure_alloc (sizeof (ncode_closure) + + arg_count * sizeof (ffi_type*), + &code); + closure->list.registerClosure (klass, closure); _Jv_init_cif (self->signature, arg_count, @@ -1341,9 +1345,11 @@ _Jv_InterpMethod::ncode () FFI_PREP_RAW_CLOSURE (&closure->closure, &closure->cif, fun, - (void*)this); + (void*)this, + code); + + self->ncode = code; - self->ncode = (void*)closure; return self->ncode; } @@ -1540,7 +1546,7 @@ _Jv_InterpMethod::set_insn (jlong index, pc_t insn) } void * -_Jv_JNIMethod::ncode () +_Jv_JNIMethod::ncode (jclass klass) { using namespace java::lang::reflect; @@ -1550,9 +1556,12 @@ _Jv_JNIMethod::ncode () jboolean staticp = (self->accflags & Modifier::STATIC) != 0; int arg_count = _Jv_count_arguments (self->signature, staticp); + void *code; ncode_closure *closure = - (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure) - + arg_count * sizeof (ffi_type*)); + (ncode_closure*)ffi_closure_alloc (sizeof (ncode_closure) + + arg_count * sizeof (ffi_type*), + &code); + closure->list.registerClosure (klass, closure); ffi_type *rtype; _Jv_init_cif (self->signature, @@ -1594,9 +1603,10 @@ _Jv_JNIMethod::ncode () FFI_PREP_RAW_CLOSURE (&closure->closure, &closure->cif, fun, - (void*) this); + (void*) this, + code); - self->ncode = (void *) closure; + self->ncode = code; return self->ncode; } @@ -1657,16 +1667,27 @@ _Jv_InterpreterEngine::do_create_ncode (jclass klass) // cases. Well, we can't, because we don't allocate these // objects using `new', and thus they don't get a vtable. _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth); - klass->methods[i].ncode = jnim->ncode (); + klass->methods[i].ncode = jnim->ncode (klass); } else if (imeth != 0) // it could be abstract { _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth); - klass->methods[i].ncode = im->ncode (); + klass->methods[i].ncode = im->ncode (klass); } } } +_Jv_ClosureList ** +_Jv_InterpreterEngine::do_get_closure_list (jclass klass) +{ + _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info; + + if (!iclass->closures) + iclass->closures = _Jv_ClosureListFinalizer (); + + return iclass->closures; +} + void _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass, int pointer_size, diff --git a/libjava/java/lang/Class.h b/libjava/java/lang/Class.h index 3ea26ab85cf..80c410009ea 100644 --- a/libjava/java/lang/Class.h +++ b/libjava/java/lang/Class.h @@ -105,6 +105,15 @@ class _Jv_InterpClass; class _Jv_InterpMethod; #endif +class _Jv_ClosureList +{ + _Jv_ClosureList *next; + void *ptr; +public: + void registerClosure (jclass klass, void *ptr); + static void releaseClosures (_Jv_ClosureList **closures); +}; + struct _Jv_Constants { jint size; @@ -632,6 +641,7 @@ private: friend class ::_Jv_CompiledEngine; friend class ::_Jv_IndirectCompiledEngine; friend class ::_Jv_InterpreterEngine; + friend class ::_Jv_ClosureList; friend void ::_Jv_sharedlib_register_hook (jclass klass); diff --git a/libjava/java/lang/natClass.cc b/libjava/java/lang/natClass.cc index cec519fd502..79fa59989d3 100644 --- a/libjava/java/lang/natClass.cc +++ b/libjava/java/lang/natClass.cc @@ -670,6 +670,28 @@ java::lang::Class::finalize (void) engine->unregister(this); } +void +_Jv_ClosureList::releaseClosures (_Jv_ClosureList **closures) +{ + if (!closures) + return; + + while (_Jv_ClosureList *current = *closures) + { + *closures = current->next; + ffi_closure_free (current->ptr); + } +} + +void +_Jv_ClosureList::registerClosure (jclass klass, void *ptr) +{ + _Jv_ClosureList **closures = klass->engine->get_closure_list (klass); + this->ptr = ptr; + this->next = *closures; + *closures = this; +} + // This implements the initialization process for a class. From Spec // section 12.4.2. void diff --git a/libjava/java/lang/reflect/natVMProxy.cc b/libjava/java/lang/reflect/natVMProxy.cc index c83880ccf1e..5704049909a 100644 --- a/libjava/java/lang/reflect/natVMProxy.cc +++ b/libjava/java/lang/reflect/natVMProxy.cc @@ -1,6 +1,6 @@ // natVMProxy.cc -- Implementation of VMProxy methods. -/* Copyright (C) 2006 +/* Copyright (C) 2006, 2007 Free Software Foundation This file is part of libgcj. @@ -66,7 +66,7 @@ using namespace java::lang::reflect; using namespace java::lang; typedef void (*closure_fun) (ffi_cif*, void*, void**, void*); -static void *ncode (_Jv_Method *self, closure_fun fun); +static void *ncode (jclass klass, _Jv_Method *self, closure_fun fun); static void run_proxy (ffi_cif*, void*, void**, void*); typedef jobject invoke_t (jobject, Proxy *, Method *, JArray< jobject > *); @@ -165,7 +165,7 @@ java::lang::reflect::VMProxy::generateProxyClass // the interfaces of which it is a proxy will also be reachable, // so this is safe. method = imethod; - method.ncode = ncode (&method, run_proxy); + method.ncode = ncode (klass, &method, run_proxy); method.accflags &= ~Modifier::ABSTRACT; } @@ -289,6 +289,7 @@ unbox (jobject o, jclass klass, void *rvalue, FFI_TYPE type) typedef struct { ffi_closure closure; + _Jv_ClosureList list; ffi_cif cif; _Jv_Method *self; ffi_type *arg_types[0]; @@ -366,16 +367,19 @@ run_proxy (ffi_cif *cif, // the address of its closure. static void * -ncode (_Jv_Method *self, closure_fun fun) +ncode (jclass klass, _Jv_Method *self, closure_fun fun) { using namespace java::lang::reflect; jboolean staticp = (self->accflags & Modifier::STATIC) != 0; int arg_count = _Jv_count_arguments (self->signature, staticp); + void *code; ncode_closure *closure = - (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure) - + arg_count * sizeof (ffi_type*)); + (ncode_closure*)ffi_closure_alloc (sizeof (ncode_closure) + + arg_count * sizeof (ffi_type*), + &code); + closure->list.registerClosure (klass, closure); _Jv_init_cif (self->signature, arg_count, @@ -387,11 +391,12 @@ ncode (_Jv_Method *self, closure_fun fun) JvAssert ((self->accflags & Modifier::NATIVE) == 0); - ffi_prep_closure (&closure->closure, - &closure->cif, - fun, - (void*)closure); + ffi_prep_closure_loc (&closure->closure, + &closure->cif, + fun, + code, + code); - self->ncode = (void*)closure; + self->ncode = code; return self->ncode; } diff --git a/libjava/link.cc b/libjava/link.cc index 006676c3f4c..4ea548ec905 100644 --- a/libjava/link.cc +++ b/libjava/link.cc @@ -1022,15 +1022,17 @@ struct method_closure // be the same as the address of the overall structure. This is due // to disabling interior pointers in the GC. ffi_closure closure; + _Jv_ClosureList list; ffi_cif cif; ffi_type *arg_types[1]; }; void * -_Jv_Linker::create_error_method (_Jv_Utf8Const *class_name) +_Jv_Linker::create_error_method (_Jv_Utf8Const *class_name, jclass klass) { + void *code; method_closure *closure - = (method_closure *) _Jv_AllocBytes(sizeof (method_closure)); + = (method_closure *)ffi_closure_alloc (sizeof (method_closure), &code); closure->arg_types[0] = &ffi_type_void; @@ -1042,13 +1044,18 @@ _Jv_Linker::create_error_method (_Jv_Utf8Const *class_name) 1, &ffi_type_void, closure->arg_types) == FFI_OK - && ffi_prep_closure (&closure->closure, - &closure->cif, - _Jv_ThrowNoClassDefFoundErrorTrampoline, - class_name) == FFI_OK) - return &closure->closure; + && ffi_prep_closure_loc (&closure->closure, + &closure->cif, + _Jv_ThrowNoClassDefFoundErrorTrampoline, + class_name, + code) == FFI_OK) + { + closure->list.registerClosure (klass, closure); + return code; + } else { + ffi_closure_free (closure); java::lang::StringBuffer *buffer = new java::lang::StringBuffer(); buffer->append(JvNewStringLatin1("Error setting up FFI closure" " for static method of" @@ -1059,7 +1066,7 @@ _Jv_Linker::create_error_method (_Jv_Utf8Const *class_name) } #else void * -_Jv_Linker::create_error_method (_Jv_Utf8Const *) +_Jv_Linker::create_error_method (_Jv_Utf8Const *, jclass) { // Codepath for platforms which do not support (or want) libffi. // You have to accept that it is impossible to provide the name @@ -1090,8 +1097,6 @@ static bool debug_link = false; // at the corresponding position in the virtual method offset table // (klass->otable). -// The same otable and atable may be shared by many classes. - // This must be called while holding the class lock. void @@ -1242,13 +1247,15 @@ _Jv_Linker::link_symbol_table (jclass klass) // NullPointerException klass->atable->addresses[index] = NULL; + bool use_error_method = false; + // If the target class is missing we prepare a function call // that throws a NoClassDefFoundError and store the address of // that newly prepared method in the atable. The user can run // code in classes where the missing class is part of the // execution environment as long as it is never referenced. if (target_class == NULL) - klass->atable->addresses[index] = create_error_method(sym.class_name); + use_error_method = true; // We're looking for a static field or a static method, and we // can tell which is needed by looking at the signature. else if (signature->first() == '(' && signature->len() >= 2) @@ -1296,12 +1303,16 @@ _Jv_Linker::link_symbol_table (jclass klass) } } else + use_error_method = true; + + if (use_error_method) klass->atable->addresses[index] - = create_error_method(sym.class_name); + = create_error_method(sym.class_name, klass); continue; } + // Try fields only if the target class exists. if (target_class != NULL) { diff --git a/libjava/nogc.cc b/libjava/nogc.cc index f25037aae0e..126e4de5c2b 100644 --- a/libjava/nogc.cc +++ b/libjava/nogc.cc @@ -1,6 +1,7 @@ // nogc.cc - Implement null garbage collector. -/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2006 Free Software Foundation +/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2006, 2007 + Free Software Foundation This file is part of libgcj. @@ -71,6 +72,14 @@ _Jv_AllocRawObj (jsize size) return calloc (size, 1); } +_Jv_ClosureList ** +_Jv_ClosureListFinalizer () +{ + _Jv_ClosureList **clpp; + clpp = (_Jv_ClosureList **)_Jv_AllocBytes (sizeof (*clpp)); + return clpp; +} + void _Jv_RegisterFinalizer (void *, _Jv_FinalizerFunc *) { diff --git a/libjava/testsuite/libjava.jar/TestClosureGC.jar b/libjava/testsuite/libjava.jar/TestClosureGC.jar Binary files differnew file mode 100644 index 00000000000..60d948ad58b --- /dev/null +++ b/libjava/testsuite/libjava.jar/TestClosureGC.jar diff --git a/libjava/testsuite/libjava.jar/TestClosureGC.java b/libjava/testsuite/libjava.jar/TestClosureGC.java new file mode 100644 index 00000000000..69a325a9994 --- /dev/null +++ b/libjava/testsuite/libjava.jar/TestClosureGC.java @@ -0,0 +1,116 @@ +/* Verify that libffi closures aren't deallocated too early. + + Copyright (C) 2007 Free Software Foundation, Inc + Contributed by Alexandre Oliva <aoliva@redhat.com> + + If libffi closures are released too early, we lose. + */ + +import java.util.HashSet; + +public class TestClosureGC { + public static String objId (Object obj) { + return obj + "/" + + Integer.toHexString(obj.getClass().getClassLoader().hashCode()); + } + public static class cld extends java.net.URLClassLoader { + static final Object obj = new cl0(); + public cld () throws Exception { + super(new java.net.URL[] { }); + /* System.out.println (objId (this) + " created"); */ + } + public void finalize () { + /* System.out.println (objId (this) + " finalized"); */ + } + public String toString () { + return this.getClass().getName() + "@" + + Integer.toHexString (hashCode ()); + } + public Class loadClass (String name) throws ClassNotFoundException { + try { + java.io.InputStream IS = getSystemResourceAsStream + (name + ".class"); + int maxsz = 1024, readsz = 0; + byte buf[] = new byte[maxsz]; + for(;;) { + int readnow = IS.read (buf, readsz, maxsz - readsz); + if (readnow <= 0) + break; + readsz += readnow; + if (readsz == maxsz) { + byte newbuf[] = new byte[maxsz *= 2]; + System.arraycopy (buf, 0, newbuf, 0, readsz); + buf = newbuf; + } + } + return defineClass (name, buf, 0, readsz); + } catch (Exception e) { + return super.loadClass (name); + } + } + } + public static class cl0 { + public cl0 () { + /* System.out.println (objId (this) + " created"); */ + } + public void finalize () { + /* System.out.println (objId (this) + " finalized"); */ + } + } + public static class cl1 { + final HashSet hs; + static final Object obj = new cl0(); + public cl1 (final HashSet hs) { + this.hs = hs; + /* System.out.println (objId (this) + " created"); */ + } + public void finalize () { + /* System.out.println (objId (this) + " finalized"); */ + } + } + public static class cl2 { + final HashSet hs; + static final Object obj = new cl0(); + public cl2 (final HashSet hs) { + this.hs = hs; + /* System.out.println (objId (this) + " created"); */ + } + public void finalize () { + /* System.out.println (objId (this) + " finalized"); */ + hs.add(this); + hs.add(new cl0()); + } + } + static final HashSet hs = new HashSet(); + static final Object obj = new cl0(); + public static void main(String[] argv) throws Exception { + { + Class[] hscs = { HashSet.class }; + Object[] hsos = { hs }; + new cld().loadClass ("TestClosureGC$cl1"). + getConstructor (hscs).newInstance (hsos); + new cld().loadClass ("TestClosureGC$cl2"). + getConstructor (hscs).newInstance (hsos); + new cld().loadClass ("TestClosureGC$cl1"). + getConstructor (hscs).newInstance (hsos); + new cld().loadClass ("TestClosureGC$cl1"). + getConstructor (hscs).newInstance (hsos); + } + for (int i = 1; i <= 5; i++) { + /* System.out.println ("Will run GC and finalization " + i); */ + System.gc (); + Thread.sleep (100); + System.runFinalization (); + Thread.sleep (100); + if (hs.isEmpty ()) + continue; + java.util.Iterator it = hs.iterator (); + while (it.hasNext ()) { + Object obj = it.next(); + /* System.out.println (objId (obj) + " in ht, removing"); */ + it.remove (); + } + } + System.out.println ("ok"); + } +} diff --git a/libjava/testsuite/libjava.jar/TestClosureGC.out b/libjava/testsuite/libjava.jar/TestClosureGC.out new file mode 100644 index 00000000000..9766475a418 --- /dev/null +++ b/libjava/testsuite/libjava.jar/TestClosureGC.out @@ -0,0 +1 @@ +ok diff --git a/libjava/testsuite/libjava.jar/TestClosureGC.xfail b/libjava/testsuite/libjava.jar/TestClosureGC.xfail new file mode 100644 index 00000000000..963b35a7c70 --- /dev/null +++ b/libjava/testsuite/libjava.jar/TestClosureGC.xfail @@ -0,0 +1 @@ +main=TestClosureGC |