1 // natVMProxy.cc -- Implementation of VMProxy methods.
3 /* Copyright (C) 2006, 2007
4 Free Software Foundation
6 This file is part of libgcj.
8 This software is copyrighted work licensed under the terms of the
9 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
12 // The idea of behind this code is to utilize libffi's ability to
13 // create closures to provide a fast "cut-through" way to generate
14 // proxy classes. Instead of generating bytecode and then
15 // interpreting that, we copy the method definitions for each of the
16 // methods we're supposed to be prxying and generate a libffi closure
21 #include <sysdep/descriptor.h>
29 #include <gcj/javaprims.h>
32 #include <java-threads.h>
33 #include <java-interp.h>
35 #include <execution.h>
36 #include <gcj/method.h>
38 #include <gnu/gcj/runtime/BootClassLoader.h>
39 #include <java/lang/Class.h>
40 #include <java/lang/ClassCastException.h>
41 #include <java/lang/Error.h>
42 #include <java/lang/IllegalArgumentException.h>
43 #include <java/lang/Integer.h>
44 #include <java/lang/StringBuffer.h>
45 #include <java/lang/UnsupportedOperationException.h>
46 #include <java/lang/VMClassLoader.h>
47 #include <java/lang/VMCompiler.h>
48 #include <java/lang/reflect/InvocationHandler.h>
49 #include <java/lang/reflect/Method.h>
50 #include <java/lang/reflect/Proxy$ClassFactory.h>
51 #include <java/lang/reflect/Proxy$ProxyData.h>
52 #include <java/lang/reflect/Proxy.h>
53 #include <java/lang/reflect/UndeclaredThrowableException.h>
54 #include <java/lang/reflect/VMProxy.h>
56 #include <java/lang/Byte.h>
57 #include <java/lang/Short.h>
58 #include <java/lang/Integer.h>
59 #include <java/lang/Long.h>
60 #include <java/lang/Float.h>
61 #include <java/lang/Double.h>
62 #include <java/lang/Boolean.h>
63 #include <java/lang/Character.h>
66 using namespace java::lang::reflect
;
67 using namespace java::lang
;
71 java::lang::reflect::VMProxy::generateProxyClass
72 (ClassLoader
*, Proxy$ProxyData
*)
74 throw new UnsupportedOperationException (
75 JvNewStringLatin1 ("Interpreter not available"));
78 typedef void (*closure_fun
) (ffi_cif
*, void*, void**, void*);
79 static void *ncode (int method_index
, jclass klass
, _Jv_Method
*self
, closure_fun fun
);
80 static void run_proxy (ffi_cif
*, void*, void**, void*);
82 typedef jobject
invoke_t (jobject
, Proxy
*, Method
*, JArray
< jobject
> *);
84 // True if pc points to a proxy frame.
87 _Jv_is_proxy (void *pc
)
89 return pc
== UNWRAP_FUNCTION_DESCRIPTOR ((void*)&run_proxy
);
92 // Generate a proxy class by using libffi closures for each entry
96 java::lang::reflect::VMProxy::generateProxyClass
97 (ClassLoader
*loader
, Proxy$ProxyData
*d
)
99 // If we're precompiling, generate bytecode and allow VMCompiler to
101 if (VMCompiler::precompiles ())
102 return (new Proxy$
ClassFactory(d
))->generate(loader
);
104 jclass klass
= new Class ();
106 // Synchronize on the class, so that it is not attempted initialized
108 JvSynchronize
sync (klass
);
110 klass
->superclass
= &Proxy::class$
;
111 klass
->engine
= &_Jv_soleIndirectCompiledEngine
;
112 klass
->size_in_bytes
= -1;
113 klass
->vtable_method_count
= -1;
115 // Declare private static transient java.lang.reflect.Method[] $Proxy0.m
116 klass
->field_count
= klass
->static_field_count
= 1;
117 klass
->fields
= (_Jv_Field
*)_Jv_AllocRawObj (sizeof (_Jv_Field
));
118 klass
->fields
[0].name
= _Jv_makeUtf8Const ("m");
119 klass
->fields
[0].type
= d
->methods
->getClass();
120 klass
->fields
[0].flags
= (Modifier::PRIVATE
| Modifier::STATIC
121 | Modifier::TRANSIENT
);
123 // Record the defining loader. For the bootstrap class loader,
125 if (loader
!= VMClassLoader::bootLoader
)
126 klass
->loader
= loader
;
129 StringBuffer
*sb
= new StringBuffer();
130 sb
->append(JvNewStringLatin1 ("$Proxy"));
131 sb
->append(Integer::toString (d
->id
));
132 klass
->name
= _Jv_makeUtf8Const (sb
->toString());
135 // Allocate space for the interfaces.
136 klass
->interface_count
= d
->interfaces
->length
;
137 klass
->interfaces
= (jclass
*) _Jv_AllocRawObj (klass
->interface_count
139 for (int i
= 0; i
< klass
->interface_count
; i
++)
140 klass
->interfaces
[i
] = elements(d
->interfaces
)[i
];
142 size_t count
= d
->methods
->length
;
145 size_t total_count
= count
+ Proxy::class$
.method_count
+ 1;
146 if (total_count
>= 65536)
147 throw new IllegalArgumentException ();
148 // Allocate space for the methods. This is a worst case
151 = (_Jv_Method
*) _Jv_AllocRawObj (sizeof (_Jv_Method
)
155 jshort
&method_count
= klass
->method_count
;
157 // Copy all reachable methods from Proxy.
158 for (int i
= 0; i
< Proxy::class$
.method_count
; i
++)
160 if (_Jv_CheckAccess (klass
, &Proxy::class$
,
161 Proxy::class$
.methods
[i
].accflags
))
163 klass
->methods
[method_count
] = Proxy::class$
.methods
[i
];
168 _Jv_Method
*init_method
169 = (_Jv_Linker::search_method_in_class
171 _Jv_makeUtf8Const ("<init>"),
172 _Jv_makeUtf8Const ("(Ljava.lang.reflect.InvocationHandler;)V"),
174 init_method
->accflags
|= Modifier::PUBLIC
;
176 // Create the methods for all of the interfaces.
177 for (size_t i
= 0; i
< count
; i
++)
179 _Jv_Method
&method
= klass
->methods
[method_count
++];
180 const _Jv_Method
&imethod
181 = *_Jv_FromReflectedMethod (elements(d
->methods
)[i
]);
182 // We use a shallow copy of IMETHOD rather than a deep copy;
183 // this means that the pointer fields of METHOD point into the
184 // interface. As long as this subclass of Proxy is reachable,
185 // the interfaces of which it is a proxy will also be reachable,
188 method
.ncode
= ncode (i
, klass
, &method
, run_proxy
);
189 method
.accflags
&= ~Modifier::ABSTRACT
;
192 _Jv_Linker::layout_vtable_methods (klass
);
193 _Jv_RegisterInitiatingLoader (klass
, klass
->loader
);
195 // Set $Proxy0.m to point to the methods arrray
196 java::lang::reflect::Field
*f
197 = klass
->getDeclaredField (JvNewStringLatin1 ("m"));
199 f
->set(NULL
, d
->methods
);
205 // Box things with primitive types.
206 static inline jobject
207 box (void *thing
, jclass klass
, FFI_TYPE type
)
216 case FFI_TYPE_POINTER
:
217 o
= *(jobject
*)thing
;
224 if (klass
== JvPrimClass (byte
))
225 o
= new Byte (*(jbyte
*)thing
);
226 else if (klass
== JvPrimClass (short))
227 o
= new Short (*(jshort
*)thing
);
228 else if (klass
== JvPrimClass (int))
229 o
= new Integer (*(jint
*)thing
);
230 else if (klass
== JvPrimClass (long))
231 o
= new Long (*(jlong
*)thing
);
232 else if (klass
== JvPrimClass (float))
233 o
= new Float (*(jfloat
*)thing
);
234 else if (klass
== JvPrimClass (double))
235 o
= new Double (*(jdouble
*)thing
);
236 else if (klass
== JvPrimClass (boolean
))
237 o
= new Boolean (*(jboolean
*)thing
);
238 else if (klass
== JvPrimClass (char))
239 o
= new Character (*(jchar
*)thing
);
241 JvFail ("Bad ffi type in proxy");
247 // Unbox things with primitive types.
249 unbox (jobject o
, jclass klass
, void *rvalue
, FFI_TYPE type
)
256 case FFI_TYPE_POINTER
:
257 _Jv_CheckCast (klass
, o
);
258 *(jobject
*)rvalue
= o
;
265 // If the value returned ... is null and the interface method's
266 // return type is primitive, then a NullPointerException will be
268 if (klass
== JvPrimClass (byte
))
270 _Jv_CheckCast (&Byte::class$
, o
);
271 *(jbyte
*)rvalue
= ((Byte
*)o
)->byteValue();
273 else if (klass
== JvPrimClass (short))
275 _Jv_CheckCast (&Short::class$
, o
);
276 *(jshort
*)rvalue
= ((Short
*)o
)->shortValue();
278 else if (klass
== JvPrimClass (int))
280 _Jv_CheckCast (&Integer::class$
, o
);
281 *(jint
*)rvalue
= ((Integer
*)o
)->intValue();
283 else if (klass
== JvPrimClass (long))
285 _Jv_CheckCast (&Long::class$
, o
);
286 *(jlong
*)rvalue
= ((Long
*)o
)->longValue();
288 else if (klass
== JvPrimClass (float))
290 _Jv_CheckCast (&Float::class$
, o
);
291 *(jfloat
*)rvalue
= ((Float
*)o
)->floatValue();
293 else if (klass
== JvPrimClass (double))
295 _Jv_CheckCast (&Double::class$
, o
);
296 *(jdouble
*)rvalue
= ((Double
*)o
)->doubleValue();
298 else if (klass
== JvPrimClass (boolean
))
300 _Jv_CheckCast (&Boolean::class$
, o
);
301 *(jboolean
*)rvalue
= ((Boolean
*)o
)->booleanValue();
303 else if (klass
== JvPrimClass (char))
305 _Jv_CheckCast (&Character::class$
, o
);
306 *(jchar
*)rvalue
= ((Character
*)o
)->charValue();
309 JvFail ("Bad ffi type in proxy");
312 // _Jv_getFieldInternal is declared as a friend of reflect.Field in
313 // libjava/headers.txt. This gives us a way to call the private
314 // method Field.get (Class caller, Object obj).
315 extern inline jobject
316 _Jv_getFieldInternal (java::lang::reflect::Field
*f
, jclass c
, jobject o
)
321 // run_proxy is the entry point for all proxy methods. It boxes up
322 // all the arguments and then invokes the invocation handler's invoke()
323 // method. Exceptions are caught and propagated.
327 _Jv_ClosureList list
;
331 ffi_type
*arg_types
[0];
335 run_proxy (ffi_cif
*cif
,
340 using namespace java::lang::reflect
;
342 Proxy
*proxy
= *(Proxy
**)args
[0];
343 ncode_closure
*self
= (ncode_closure
*) user_data
;
345 jclass proxyClass
= proxy
->getClass();
347 // FRAME_DESC registers this particular invocation as the top-most
348 // interpreter frame. This lets the stack tracing code (for
349 // Throwable) print information about the Proxy being run rather
350 // than about Proxy.class itself. FRAME_DESC has a destructor so it
351 // cleans up automatically when this proxy invocation returns.
352 Thread
*thread
= Thread::currentThread();
353 _Jv_InterpFrame
frame_desc (self
->self
, thread
, proxyClass
);
355 // The method to invoke is saved in $Proxy0.m[method_index].
356 // FIXME: We could somewhat improve efficiency by storing a pointer
357 // to the method (rather than its index) in ncode_closure. This
358 // would avoid the lookup, but it probably wouldn't make a huge
359 // difference. We'd still have to save the method array because
360 // ncode structs are not scanned by the gc.
361 Field
*f
= proxyClass
->getDeclaredField (JvNewStringLatin1 ("m"));
362 JArray
<Method
*> *methods
363 = (JArray
<Method
*>*)_Jv_getFieldInternal (f
, proxyClass
, NULL
);
364 Method
*meth
= elements(methods
)[self
->method_index
];
366 JArray
<jclass
> *parameter_types
= meth
->internalGetParameterTypes ();
367 JArray
<jclass
> *exception_types
= meth
->internalGetExceptionTypes ();
369 InvocationHandler
*handler
= proxy
->h
;
370 JArray
<jobject
> *argsArray
= NULL
;
371 jobject
*jargs
= NULL
;
372 if (parameter_types
->length
)
375 = _Jv_NewObjectArray (parameter_types
->length
, &Object::class$
, NULL
);
376 argsArray
= (JArray
<jobject
> *) poo
;
377 jargs
= elements(argsArray
);
380 // FIXME: It must be possible to use fast interface dispatch here,
381 // but I've not quite figured out how to do it.
383 = (invoke_t
*)(_Jv_LookupInterfaceMethod
384 (handler
->getClass (),
385 _Jv_makeUtf8Const ("invoke"),
387 ("(Ljava.lang.Object;Ljava.lang.reflect.Method;[Ljava.lang.Object;)"
388 "Ljava.lang.Object;"))));
390 // Copy and box all the args.
392 for (int i
= 0; i
< parameter_types
->length
; i
++, index
++)
393 jargs
[i
] = box (args
[index
], elements(parameter_types
)[i
],
394 cif
->arg_types
[index
]->type
);
399 ret
= invoke (handler
, proxy
, meth
, argsArray
);
403 if (_Jv_IsInstanceOf (t
, &RuntimeException::class$
)
404 || _Jv_IsInstanceOf (t
, &Error::class$
))
407 Class
**throwables
= elements (exception_types
);
408 for (int i
= 0; i
< exception_types
->length
; i
++)
409 if (_Jv_IsInstanceOf (t
, throwables
[i
]))
412 throw new UndeclaredThrowableException (t
);
415 unbox (ret
, meth
->return_type
, rvalue
, cif
->rtype
->type
);
419 // Given a method and a closure function, create libffi CIF and return
420 // the address of its closure.
423 ncode (int method_index
, jclass klass
, _Jv_Method
*self
, closure_fun fun
)
425 using namespace java::lang::reflect
;
427 jboolean staticp
= (self
->accflags
& Modifier::STATIC
) != 0;
428 int arg_count
= _Jv_count_arguments (self
->signature
, staticp
);
431 ncode_closure
*closure
=
432 (ncode_closure
*)ffi_closure_alloc (sizeof (ncode_closure
)
433 + arg_count
* sizeof (ffi_type
*),
435 closure
->method_index
= method_index
;
436 closure
->list
.registerClosure (klass
, closure
);
438 _Jv_init_cif (self
->signature
,
442 &closure
->arg_types
[0],
444 closure
->self
= self
;
446 JvAssert ((self
->accflags
& Modifier::NATIVE
) == 0);
448 ffi_prep_closure_loc (&closure
->closure
,
458 #endif // INTERPRETER