1 // natVMProxy.cc -- Implementation of VMProxy methods.
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/VMClassLoader.h>
46 #include <java/lang/VMCompiler.h>
47 #include <java/lang/reflect/InvocationHandler.h>
48 #include <java/lang/reflect/Method.h>
49 #include <java/lang/reflect/Proxy$ClassFactory.h>
50 #include <java/lang/reflect/Proxy$ProxyData.h>
51 #include <java/lang/reflect/Proxy.h>
52 #include <java/lang/reflect/UndeclaredThrowableException.h>
53 #include <java/lang/reflect/VMProxy.h>
55 #include <java/lang/Byte.h>
56 #include <java/lang/Short.h>
57 #include <java/lang/Integer.h>
58 #include <java/lang/Long.h>
59 #include <java/lang/Float.h>
60 #include <java/lang/Double.h>
61 #include <java/lang/Boolean.h>
62 #include <java/lang/Character.h>
65 using namespace java::lang::reflect;
66 using namespace java::lang;
68 typedef void (*closure_fun) (ffi_cif*, void*, void**, void*);
69 static void *ncode (_Jv_Method *self, closure_fun fun);
70 static void run_proxy (ffi_cif*, void*, void**, void*);
72 typedef jobject invoke_t (jobject, Proxy *, Method *, JArray< jobject > *);
74 // True if pc points to a proxy frame.
77 _Jv_is_proxy (void *pc)
79 return pc == UNWRAP_FUNCTION_DESCRIPTOR ((void*)&run_proxy);
82 // Generate a proxy class by using libffi closures for each entry
86 java::lang::reflect::VMProxy::generateProxyClass
87 (ClassLoader *loader, Proxy$ProxyData *d)
89 // If we're precompiling, generate bytecode and allow VMCompiler to
91 if (VMCompiler::precompiles ())
92 return (new Proxy$ClassFactory(d))->generate(loader);
94 jclass klass = new Class ();
95 klass->superclass = &Proxy::class$;
96 klass->engine = &_Jv_soleIndirectCompiledEngine;
97 klass->size_in_bytes = Proxy::class$.size_in_bytes;
98 klass->vtable_method_count = -1;
100 // Synchronize on the class, so that it is not attempted initialized
102 JvSynchronize sync (klass);
104 // Record the defining loader. For the bootstrap class loader,
106 if (loader != VMClassLoader::bootLoader)
107 klass->loader = loader;
110 StringBuffer *sb = new StringBuffer();
111 sb->append(JvNewStringLatin1 ("$Proxy"));
112 sb->append(Integer::toString (d->id));
113 klass->name = _Jv_makeUtf8Const (sb->toString());
116 // Allocate space for the interfaces.
117 klass->interface_count = d->interfaces->length;
118 klass->interfaces = (jclass*) _Jv_AllocRawObj (klass->interface_count
120 for (int i = 0; i < klass->interface_count; i++)
121 klass->interfaces[i] = elements(d->interfaces)[i];
123 size_t count = d->methods->length;
126 size_t total_count = count + Proxy::class$.method_count + 1;
127 if (total_count >= 65536)
128 throw new IllegalArgumentException ();
129 // Allocate space for the methods. This is a worst case
132 = (_Jv_Method *) _Jv_AllocRawObj (sizeof (_Jv_Method)
136 jshort &method_count = klass->method_count;
138 // Copy all reachable methods from Proxy.
139 for (int i = 0; i < Proxy::class$.method_count; i++)
141 if (_Jv_CheckAccess (klass, &Proxy::class$,
142 Proxy::class$.methods[i].accflags))
144 klass->methods[method_count] = Proxy::class$.methods[i];
149 _Jv_Method *init_method
150 = (_Jv_Linker::search_method_in_class
152 _Jv_makeUtf8Const ("<init>"),
153 _Jv_makeUtf8Const ("(Ljava.lang.reflect.InvocationHandler;)V"),
155 init_method->accflags |= Modifier::PUBLIC;
157 // Create the methods for all of the interfaces.
158 for (size_t i = 0; i < count; i++)
160 _Jv_Method &method = klass->methods[method_count++];
161 const _Jv_Method &imethod = *_Jv_FromReflectedMethod (elements(d->methods)[i]);
162 // We use a shallow copy of IMETHOD rather than a deep copy;
163 // this means that the pointer fields of METHOD point into the
164 // interface. As long as this subclass of Proxy is reachable,
165 // the interfaces of which it is a proxy will also be reachable,
168 method.ncode = ncode (&method, run_proxy);
169 method.accflags &= ~Modifier::ABSTRACT;
172 _Jv_Linker::layout_vtable_methods (klass);
173 _Jv_RegisterInitiatingLoader (klass, klass->loader);
179 // Box things with primitive types.
180 static inline jobject
181 box (void *thing, jclass klass, FFI_TYPE type)
190 case FFI_TYPE_POINTER:
191 o = *(jobject*)thing;
198 if (klass == JvPrimClass (byte))
199 o = new Byte (*(jbyte*)thing);
200 else if (klass == JvPrimClass (short))
201 o = new Short (*(jshort*)thing);
202 else if (klass == JvPrimClass (int))
203 o = new Integer (*(jint*)thing);
204 else if (klass == JvPrimClass (long))
205 o = new Long (*(jlong*)thing);
206 else if (klass == JvPrimClass (float))
207 o = new Float (*(jfloat*)thing);
208 else if (klass == JvPrimClass (double))
209 o = new Double (*(jdouble*)thing);
210 else if (klass == JvPrimClass (boolean))
211 o = new Boolean (*(jboolean*)thing);
212 else if (klass == JvPrimClass (char))
213 o = new Character (*(jchar*)thing);
215 JvFail ("Bad ffi type in proxy");
221 // Unbox things with primitive types.
223 unbox (jobject o, jclass klass, void *rvalue, FFI_TYPE type)
230 case FFI_TYPE_POINTER:
231 _Jv_CheckCast (klass, o);
232 *(jobject*)rvalue = o;
239 // If the value returned ... is null and the interface method's
240 // return type is primitive, then a NullPointerException will be
242 if (klass == JvPrimClass (byte))
244 _Jv_CheckCast (&Byte::class$, o);
245 *(jbyte*)rvalue = ((Byte*)o)->byteValue();
247 else if (klass == JvPrimClass (short))
249 _Jv_CheckCast (&Short::class$, o);
250 *(jshort*)rvalue = ((Short*)o)->shortValue();
252 else if (klass == JvPrimClass (int))
254 _Jv_CheckCast (&Integer::class$, o);
255 *(jint*)rvalue = ((Integer*)o)->intValue();
257 else if (klass == JvPrimClass (long))
259 _Jv_CheckCast (&Long::class$, o);
260 *(jlong*)rvalue = ((Long*)o)->longValue();
262 else if (klass == JvPrimClass (float))
264 _Jv_CheckCast (&Float::class$, o);
265 *(jfloat*)rvalue = ((Float*)o)->floatValue();
267 else if (klass == JvPrimClass (double))
269 _Jv_CheckCast (&Double::class$, o);
270 *(jdouble*)rvalue = ((Double*)o)->doubleValue();
272 else if (klass == JvPrimClass (boolean))
274 _Jv_CheckCast (&Boolean::class$, o);
275 *(jboolean*)rvalue = ((Boolean*)o)->booleanValue();
277 else if (klass == JvPrimClass (char))
279 _Jv_CheckCast (&Character::class$, o);
280 *(jchar*)rvalue = ((Character*)o)->charValue();
283 JvFail ("Bad ffi type in proxy");
286 // run_proxy is the entry point for all proxy methods. It boxes up
287 // all the arguments and then invokes the invocation handler's invoke()
288 // method. Exceptions are caught and propagated.
294 ffi_type *arg_types[0];
298 run_proxy (ffi_cif *cif,
303 Proxy *proxy = *(Proxy**)args[0];
304 ncode_closure *self = (ncode_closure *) user_data;
306 // FRAME_DESC registers this particular invocation as the top-most
307 // interpreter frame. This lets the stack tracing code (for
308 // Throwable) print information about the Proxy being run rather
309 // than about Proxy.class itself. FRAME_DESC has a destructor so it
310 // cleans up automatically when this proxy invocation returns.
311 Thread *thread = Thread::currentThread();
312 _Jv_InterpFrame frame_desc (self->self, thread, proxy->getClass());
314 Method *meth = _Jv_GetReflectedMethod (proxy->getClass(),
316 self->self->signature);
317 JArray<jclass> *parameter_types = meth->internalGetParameterTypes ();
318 JArray<jclass> *exception_types = meth->internalGetExceptionTypes ();
320 InvocationHandler *handler = proxy->h;
322 = _Jv_NewObjectArray (parameter_types->length, &Object::class$, NULL);
323 JArray<jobject> *argsArray = (JArray<jobject> *) poo;
324 jobject *jargs = elements(argsArray);
326 // FIXME: It must be possible to use fast interface dispatch here,
327 // but I've not quite figured out how to do it.
329 = (invoke_t *)(_Jv_LookupInterfaceMethod
330 (handler->getClass (),
331 _Jv_makeUtf8Const ("invoke"),
333 ("(Ljava.lang.Object;Ljava.lang.reflect.Method;[Ljava.lang.Object;)"
334 "Ljava.lang.Object;"))));
336 // Copy and box all the args.
338 for (int i = 0; i < parameter_types->length; i++, index++)
339 jargs[i] = box (args[index], elements(parameter_types)[i],
340 cif->arg_types[index]->type);
345 ret = invoke (handler, proxy, meth, argsArray);
349 if (_Jv_IsInstanceOf (t, &RuntimeException::class$)
350 || _Jv_IsInstanceOf (t, &Error::class$))
353 Class **throwables = elements (exception_types);
354 for (int i = 0; i < exception_types->length; i++)
355 if (_Jv_IsInstanceOf (t, throwables[i]))
358 throw new UndeclaredThrowableException (t);
361 unbox (ret, meth->return_type, rvalue, cif->rtype->type);
365 // Given a method and a closure function, create libffi CIF and return
366 // the address of its closure.
369 ncode (_Jv_Method *self, closure_fun fun)
371 using namespace java::lang::reflect;
373 jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
374 int arg_count = _Jv_count_arguments (self->signature, staticp);
376 ncode_closure *closure =
377 (ncode_closure*)_Jv_AllocBytes (sizeof (ncode_closure)
378 + arg_count * sizeof (ffi_type*));
380 _Jv_init_cif (self->signature,
384 &closure->arg_types[0],
386 closure->self = self;
388 JvAssert ((self->accflags & Modifier::NATIVE) == 0);
390 ffi_prep_closure (&closure->closure,
395 self->ncode = (void*)closure;