summaryrefslogtreecommitdiffstats
path: root/libjava/java/lang/reflect/natVMProxy.cc
blob: 5704049909a4ff7b36909b60131f3dcacb6aac4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// natVMProxy.cc -- Implementation of VMProxy methods.

/* Copyright (C) 2006, 2007
   Free Software Foundation

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

// The idea of behind this code is to utilize libffi's ability to
// create closures to provide a fast "cut-through" way to generate
// proxy classes.  Instead of generating bytecode and then
// interpreting that, we copy the method definitions for each of the
// methods we're supposed to be prxying and generate a libffi closure
// for each one.

#include <config.h>
#include <platform.h>
#include <sysdep/descriptor.h>

#include <limits.h>
#include <string.h>
#include <stddef.h>
#include <stdio.h>

#include <gcj/cni.h>
#include <gcj/javaprims.h>
#include <jvm.h>
#include <jni.h>
#include <java-threads.h>
#include <java-interp.h>
#include <ffi.h>
#include <execution.h>
#include <gcj/method.h>

#include <gnu/gcj/runtime/BootClassLoader.h>
#include <java/lang/Class.h>
#include <java/lang/ClassCastException.h>
#include <java/lang/Error.h>
#include <java/lang/IllegalArgumentException.h>
#include <java/lang/Integer.h>
#include <java/lang/StringBuffer.h>
#include <java/lang/VMClassLoader.h>
#include <java/lang/VMCompiler.h>
#include <java/lang/reflect/InvocationHandler.h>
#include <java/lang/reflect/Method.h>
#include <java/lang/reflect/Proxy$ClassFactory.h>
#include <java/lang/reflect/Proxy$ProxyData.h>
#include <java/lang/reflect/Proxy.h>
#include <java/lang/reflect/UndeclaredThrowableException.h>
#include <java/lang/reflect/VMProxy.h>

#include <java/lang/Byte.h>
#include <java/lang/Short.h>
#include <java/lang/Integer.h>
#include <java/lang/Long.h>
#include <java/lang/Float.h>
#include <java/lang/Double.h>
#include <java/lang/Boolean.h>
#include <java/lang/Character.h>


using namespace java::lang::reflect;
using namespace java::lang;

typedef void (*closure_fun) (ffi_cif*, void*, void**, void*);
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 > *);

// True if pc points to a proxy frame.

bool 
_Jv_is_proxy (void *pc)
{
  return pc == UNWRAP_FUNCTION_DESCRIPTOR ((void*)&run_proxy);
}

// Generate a proxy class by using libffi closures for each entry
// point.

jclass
java::lang::reflect::VMProxy::generateProxyClass 
  (ClassLoader *loader, Proxy$ProxyData *d)
{
  // If we're precompiling, generate bytecode and allow VMCompiler to
  // precompile it.
  if (VMCompiler::precompiles ())
    return (new Proxy$ClassFactory(d))->generate(loader);

  jclass klass = new Class ();
  klass->superclass = &Proxy::class$;
  klass->engine = &_Jv_soleIndirectCompiledEngine;
  klass->size_in_bytes = Proxy::class$.size_in_bytes;
  klass->vtable_method_count = -1;

  // Synchronize on the class, so that it is not attempted initialized
  // until we're done.
  JvSynchronize sync (klass);

  // Record the defining loader.  For the bootstrap class loader,
  // we record NULL.
  if (loader != VMClassLoader::bootLoader)
    klass->loader = loader;

  {
    StringBuffer *sb = new StringBuffer();
    sb->append(JvNewStringLatin1 ("$Proxy"));
    sb->append(Integer::toString (d->id));
    klass->name = _Jv_makeUtf8Const (sb->toString());
  }

  // Allocate space for the interfaces.
  klass->interface_count = d->interfaces->length;
  klass->interfaces = (jclass*) _Jv_AllocRawObj (klass->interface_count
						 *sizeof (jclass));
  for (int i = 0; i < klass->interface_count; i++)
    klass->interfaces[i] = elements(d->interfaces)[i];

  size_t count = d->methods->length;

  {
    size_t total_count = count + Proxy::class$.method_count + 1;
    if (total_count >= 65536)
      throw new IllegalArgumentException ();
    // Allocate space for the methods.  This is a worst case
    // estimate.
    klass->methods 
      = (_Jv_Method *) _Jv_AllocRawObj (sizeof (_Jv_Method) 
					* total_count);
  }

  jshort &method_count = klass->method_count;

  // Copy all reachable methods from Proxy.
  for (int i = 0; i < Proxy::class$.method_count; i++)
    {
      if (_Jv_CheckAccess (klass, &Proxy::class$,
			   Proxy::class$.methods[i].accflags))
	{
	  klass->methods[method_count] = Proxy::class$.methods[i];
	  method_count++;
	}
    }

  _Jv_Method *init_method 
    = (_Jv_Linker::search_method_in_class 
       (klass, klass,
	_Jv_makeUtf8Const ("<init>"),
	_Jv_makeUtf8Const ("(Ljava.lang.reflect.InvocationHandler;)V"),
	false));  
  init_method->accflags |= Modifier::PUBLIC;

  // Create the methods for all of the interfaces.
  for (size_t i = 0; i < count; i++)
    {
      _Jv_Method &method = klass->methods[method_count++];
      const _Jv_Method &imethod = *_Jv_FromReflectedMethod (elements(d->methods)[i]);
      // We use a shallow copy of IMETHOD rather than a deep copy;
      // this means that the pointer fields of METHOD point into the
      // interface.  As long as this subclass of Proxy is reachable,
      // the interfaces of which it is a proxy will also be reachable,
      // so this is safe.
      method = imethod;
      method.ncode = ncode (klass, &method, run_proxy);
      method.accflags &= ~Modifier::ABSTRACT;
    }

  _Jv_Linker::layout_vtable_methods (klass);
  _Jv_RegisterInitiatingLoader (klass, klass->loader);

  return klass;
}


// Box things with primitive types.
static inline jobject
box (void *thing, jclass klass, FFI_TYPE type)
{
  jobject o;

  switch (type)
    {
    case FFI_TYPE_VOID:
      return NULL;
      
    case FFI_TYPE_POINTER:
      o = *(jobject*)thing;
      return o;

    default:
      ;
    }

  if (klass == JvPrimClass (byte))
    o = new Byte (*(jbyte*)thing);
  else if (klass == JvPrimClass (short))
    o = new Short (*(jshort*)thing);
  else if (klass == JvPrimClass (int))
    o = new Integer (*(jint*)thing);
  else if (klass == JvPrimClass (long))
    o = new Long (*(jlong*)thing);
  else if (klass == JvPrimClass (float))
    o = new Float (*(jfloat*)thing);
  else if (klass == JvPrimClass (double))
    o = new Double (*(jdouble*)thing);
  else if (klass == JvPrimClass (boolean))
    o = new Boolean (*(jboolean*)thing);
  else if (klass == JvPrimClass (char))
    o = new Character (*(jchar*)thing);
  else
    JvFail ("Bad ffi type in proxy");

  return o;
}  


// Unbox things with primitive types.
static inline void
unbox (jobject o, jclass klass, void *rvalue, FFI_TYPE type)
{
  switch (type)
    {
    case FFI_TYPE_VOID:
      return;
      
    case FFI_TYPE_POINTER:
      _Jv_CheckCast (klass, o);
      *(jobject*)rvalue = o;
      return;

    default:
      ;
    }

  // If the value returned ... is null and the interface method's
  // return type is primitive, then a NullPointerException will be
  // thrown ...
  if (klass == JvPrimClass (byte))
    {
      _Jv_CheckCast (&Byte::class$, o);
      *(jbyte*)rvalue = ((Byte*)o)->byteValue();
    }
  else if (klass == JvPrimClass (short))
    {
      _Jv_CheckCast (&Short::class$, o);
      *(jshort*)rvalue = ((Short*)o)->shortValue();
    }
  else if (klass == JvPrimClass (int))
    {
      _Jv_CheckCast (&Integer::class$, o);
      *(jint*)rvalue = ((Integer*)o)->intValue();
    }
  else if (klass == JvPrimClass (long))
    {
      _Jv_CheckCast (&Long::class$, o);
      *(jlong*)rvalue = ((Long*)o)->longValue();
    }
  else if (klass == JvPrimClass (float))
    {
      _Jv_CheckCast (&Float::class$, o);
      *(jfloat*)rvalue = ((Float*)o)->floatValue();
    }
  else if (klass == JvPrimClass (double))
    {
      _Jv_CheckCast (&Double::class$, o);
      *(jdouble*)rvalue = ((Double*)o)->doubleValue();
    }
  else if (klass == JvPrimClass (boolean))
    {
      _Jv_CheckCast (&Boolean::class$, o);
      *(jboolean*)rvalue = ((Boolean*)o)->booleanValue();
    }
  else if (klass == JvPrimClass (char))
    {
      _Jv_CheckCast (&Character::class$, o);
      *(jchar*)rvalue = ((Character*)o)->charValue();
    }
  else
    JvFail ("Bad ffi type in proxy");
}

// run_proxy is the entry point for all proxy methods.  It boxes up
// all the arguments and then invokes the invocation handler's invoke()
// method.  Exceptions are caught and propagated.

typedef struct {
  ffi_closure  closure;
  _Jv_ClosureList list;
  ffi_cif   cif;
  _Jv_Method *self;
  ffi_type *arg_types[0];
} ncode_closure;

static void
run_proxy (ffi_cif *cif,
	   void *rvalue,
	   void **args,
	   void*user_data)
{
  Proxy *proxy = *(Proxy**)args[0];
  ncode_closure *self = (ncode_closure *) user_data;

  // FRAME_DESC registers this particular invocation as the top-most
  // interpreter frame.  This lets the stack tracing code (for
  // Throwable) print information about the Proxy being run rather
  // than about Proxy.class itself.  FRAME_DESC has a destructor so it
  // cleans up automatically when this proxy invocation returns.
  Thread *thread = Thread::currentThread();
  _Jv_InterpFrame frame_desc (self->self, thread, proxy->getClass());

  Method *meth = _Jv_GetReflectedMethod (proxy->getClass(), 
					 self->self->name,
					 self->self->signature);
  JArray<jclass> *parameter_types = meth->internalGetParameterTypes ();
  JArray<jclass> *exception_types = meth->internalGetExceptionTypes ();

  InvocationHandler *handler = proxy->h;
  void *poo 
    = _Jv_NewObjectArray (parameter_types->length, &Object::class$, NULL);
  JArray<jobject> *argsArray = (JArray<jobject> *) poo;
  jobject *jargs = elements(argsArray);

  // FIXME: It must be possible to use fast interface dispatch here,
  // but I've not quite figured out how to do it.
  invoke_t *invoke
    = (invoke_t *)(_Jv_LookupInterfaceMethod 
		   (handler->getClass (), 
		    _Jv_makeUtf8Const ("invoke"),
		    (_Jv_makeUtf8Const 
		     ("(Ljava.lang.Object;Ljava.lang.reflect.Method;[Ljava.lang.Object;)"
		      "Ljava.lang.Object;"))));

  // Copy and box all the args.
  int index = 1;
  for (int i = 0; i < parameter_types->length; i++, index++)
    jargs[i] = box (args[index], elements(parameter_types)[i],
		    cif->arg_types[index]->type);
  
  jobject ret;
  try
    {
      ret = invoke (handler, proxy, meth, argsArray);
    }
  catch (Throwable *t)
    {
      if (_Jv_IsInstanceOf (t, &RuntimeException::class$)
	  || _Jv_IsInstanceOf (t, &Error::class$))
	throw t;

      Class **throwables = elements (exception_types);
      for (int i = 0; i < exception_types->length; i++)
	if (_Jv_IsInstanceOf (t, throwables[i]))
	  throw t;

      throw new UndeclaredThrowableException (t);
    }

  unbox (ret, meth->return_type, rvalue, cif->rtype->type);
}


// Given a method and a closure function, create libffi CIF and return
// the address of its closure.

static void *
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*)ffi_closure_alloc (sizeof (ncode_closure)
				       + arg_count * sizeof (ffi_type*),
				       &code);
  closure->list.registerClosure (klass, closure);

  _Jv_init_cif (self->signature,
		arg_count,
		staticp,
		&closure->cif,
		&closure->arg_types[0],
		NULL);
  closure->self = self;

  JvAssert ((self->accflags & Modifier::NATIVE) == 0);

  ffi_prep_closure_loc (&closure->closure,
			&closure->cif,
			fun,
			code,
			code);

  self->ncode = code;
  return self->ncode;
}
OpenPOWER on IntegriCloud