1 /* go-reflect-call.c -- call reflection support for Go.
2 
3    Copyright 2009 The Go Authors. All rights reserved.
4    Use of this source code is governed by a BSD-style
5    license that can be found in the LICENSE file.  */
6 
7 #include <stdio.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 
11 #include "runtime.h"
12 #include "go-alloc.h"
13 #include "go-assert.h"
14 #include "go-type.h"
15 
16 #ifdef USE_LIBFFI
17 
18 #include "ffi.h"
19 
20 /* The functions in this file are only called from reflect_call.  As
21    reflect_call calls a libffi function, which will be compiled
22    without -fsplit-stack, it will always run with a large stack.  */
23 
24 static ffi_type *go_array_to_ffi (const struct __go_array_type *)
25   __attribute__ ((no_split_stack));
26 static ffi_type *go_slice_to_ffi (const struct __go_slice_type *)
27   __attribute__ ((no_split_stack));
28 static ffi_type *go_struct_to_ffi (const struct __go_struct_type *)
29   __attribute__ ((no_split_stack));
30 static ffi_type *go_string_to_ffi (void) __attribute__ ((no_split_stack));
31 static ffi_type *go_interface_to_ffi (void) __attribute__ ((no_split_stack));
32 static ffi_type *go_complex_to_ffi (ffi_type *)
33   __attribute__ ((no_split_stack, unused));
34 static ffi_type *go_type_to_ffi (const struct __go_type_descriptor *)
35   __attribute__ ((no_split_stack));
36 static ffi_type *go_func_return_ffi (const struct __go_func_type *)
37   __attribute__ ((no_split_stack));
38 static void go_func_to_cif (const struct __go_func_type *, _Bool, _Bool,
39 			    ffi_cif *)
40   __attribute__ ((no_split_stack));
41 static size_t go_results_size (const struct __go_func_type *)
42   __attribute__ ((no_split_stack));
43 static void go_set_results (const struct __go_func_type *, unsigned char *,
44 			    void **)
45   __attribute__ ((no_split_stack));
46 
47 /* Return an ffi_type for a Go array type.  The libffi library does
48    not have any builtin support for passing arrays as values.  We work
49    around this by pretending that the array is a struct.  */
50 
51 static ffi_type *
go_array_to_ffi(const struct __go_array_type * descriptor)52 go_array_to_ffi (const struct __go_array_type *descriptor)
53 {
54   ffi_type *ret;
55   uintptr_t len;
56   ffi_type *element;
57   uintptr_t i;
58 
59   ret = (ffi_type *) __go_alloc (sizeof (ffi_type));
60   ret->type = FFI_TYPE_STRUCT;
61   len = descriptor->__len;
62   ret->elements = (ffi_type **) __go_alloc ((len + 1) * sizeof (ffi_type *));
63   element = go_type_to_ffi (descriptor->__element_type);
64   for (i = 0; i < len; ++i)
65     ret->elements[i] = element;
66   ret->elements[len] = NULL;
67   return ret;
68 }
69 
70 /* Return an ffi_type for a Go slice type.  This describes the
71    __go_open_array type defines in array.h.  */
72 
73 static ffi_type *
go_slice_to_ffi(const struct __go_slice_type * descriptor)74 go_slice_to_ffi (
75     const struct __go_slice_type *descriptor __attribute__ ((unused)))
76 {
77   ffi_type *ret;
78   ffi_type *ffi_intgo;
79 
80   ret = (ffi_type *) __go_alloc (sizeof (ffi_type));
81   ret->type = FFI_TYPE_STRUCT;
82   ret->elements = (ffi_type **) __go_alloc (4 * sizeof (ffi_type *));
83   ret->elements[0] = &ffi_type_pointer;
84   ffi_intgo = sizeof (intgo) == 4 ? &ffi_type_sint32 : &ffi_type_sint64;
85   ret->elements[1] = ffi_intgo;
86   ret->elements[2] = ffi_intgo;
87   ret->elements[3] = NULL;
88   return ret;
89 }
90 
91 /* Return an ffi_type for a Go struct type.  */
92 
93 static ffi_type *
go_struct_to_ffi(const struct __go_struct_type * descriptor)94 go_struct_to_ffi (const struct __go_struct_type *descriptor)
95 {
96   ffi_type *ret;
97   int field_count;
98   const struct __go_struct_field *fields;
99   int i;
100 
101   field_count = descriptor->__fields.__count;
102   if (field_count == 0) {
103     return &ffi_type_void;
104   }
105   ret = (ffi_type *) __go_alloc (sizeof (ffi_type));
106   ret->type = FFI_TYPE_STRUCT;
107   fields = (const struct __go_struct_field *) descriptor->__fields.__values;
108   ret->elements = (ffi_type **) __go_alloc ((field_count + 1)
109 					    * sizeof (ffi_type *));
110   for (i = 0; i < field_count; ++i)
111     ret->elements[i] = go_type_to_ffi (fields[i].__type);
112   ret->elements[field_count] = NULL;
113   return ret;
114 }
115 
116 /* Return an ffi_type for a Go string type.  This describes the String
117    struct.  */
118 
119 static ffi_type *
go_string_to_ffi(void)120 go_string_to_ffi (void)
121 {
122   ffi_type *ret;
123   ffi_type *ffi_intgo;
124 
125   ret = (ffi_type *) __go_alloc (sizeof (ffi_type));
126   ret->type = FFI_TYPE_STRUCT;
127   ret->elements = (ffi_type **) __go_alloc (3 * sizeof (ffi_type *));
128   ret->elements[0] = &ffi_type_pointer;
129   ffi_intgo = sizeof (intgo) == 4 ? &ffi_type_sint32 : &ffi_type_sint64;
130   ret->elements[1] = ffi_intgo;
131   ret->elements[2] = NULL;
132   return ret;
133 }
134 
135 /* Return an ffi_type for a Go interface type.  This describes the
136    __go_interface and __go_empty_interface structs.  */
137 
138 static ffi_type *
go_interface_to_ffi(void)139 go_interface_to_ffi (void)
140 {
141   ffi_type *ret;
142 
143   ret = (ffi_type *) __go_alloc (sizeof (ffi_type));
144   ret->type = FFI_TYPE_STRUCT;
145   ret->elements = (ffi_type **) __go_alloc (3 * sizeof (ffi_type *));
146   ret->elements[0] = &ffi_type_pointer;
147   ret->elements[1] = &ffi_type_pointer;
148   ret->elements[2] = NULL;
149   return ret;
150 }
151 
152 /* Return an ffi_type for a Go complex type.  */
153 
154 static ffi_type *
go_complex_to_ffi(ffi_type * float_type)155 go_complex_to_ffi (ffi_type *float_type)
156 {
157   ffi_type *ret;
158 
159   ret = (ffi_type *) __go_alloc (sizeof (ffi_type));
160   ret->type = FFI_TYPE_STRUCT;
161   ret->elements = (ffi_type **) __go_alloc (3 * sizeof (ffi_type *));
162   ret->elements[0] = float_type;
163   ret->elements[1] = float_type;
164   ret->elements[2] = NULL;
165   return ret;
166 }
167 
168 /* Return an ffi_type for a type described by a
169    __go_type_descriptor.  */
170 
171 static ffi_type *
go_type_to_ffi(const struct __go_type_descriptor * descriptor)172 go_type_to_ffi (const struct __go_type_descriptor *descriptor)
173 {
174   switch (descriptor->__code & GO_CODE_MASK)
175     {
176     case GO_BOOL:
177       if (sizeof (_Bool) == 1)
178 	return &ffi_type_uint8;
179       else if (sizeof (_Bool) == sizeof (int))
180 	return &ffi_type_uint;
181       abort ();
182     case GO_FLOAT32:
183       if (sizeof (float) == 4)
184 	return &ffi_type_float;
185       abort ();
186     case GO_FLOAT64:
187       if (sizeof (double) == 8)
188 	return &ffi_type_double;
189       abort ();
190     case GO_COMPLEX64:
191 #ifdef __alpha__
192       runtime_throw("the libffi library does not support Complex64 type with "
193 		    "reflect.Call or runtime.SetFinalizer");
194 #else
195       if (sizeof (float) == 4)
196 	return go_complex_to_ffi (&ffi_type_float);
197       abort ();
198 #endif
199     case GO_COMPLEX128:
200 #ifdef __alpha__
201       runtime_throw("the libffi library does not support Complex128 type with "
202 		    "reflect.Call or runtime.SetFinalizer");
203 #else
204       if (sizeof (double) == 8)
205 	return go_complex_to_ffi (&ffi_type_double);
206       abort ();
207 #endif
208     case GO_INT16:
209       return &ffi_type_sint16;
210     case GO_INT32:
211       return &ffi_type_sint32;
212     case GO_INT64:
213       return &ffi_type_sint64;
214     case GO_INT8:
215       return &ffi_type_sint8;
216     case GO_INT:
217       return sizeof (intgo) == 4 ? &ffi_type_sint32 : &ffi_type_sint64;
218     case GO_UINT16:
219       return &ffi_type_uint16;
220     case GO_UINT32:
221       return &ffi_type_uint32;
222     case GO_UINT64:
223       return &ffi_type_uint64;
224     case GO_UINT8:
225       return &ffi_type_uint8;
226     case GO_UINT:
227       return sizeof (uintgo) == 4 ? &ffi_type_uint32 : &ffi_type_uint64;
228     case GO_UINTPTR:
229       if (sizeof (void *) == 2)
230 	return &ffi_type_uint16;
231       else if (sizeof (void *) == 4)
232 	return &ffi_type_uint32;
233       else if (sizeof (void *) == 8)
234 	return &ffi_type_uint64;
235       abort ();
236     case GO_ARRAY:
237       return go_array_to_ffi ((const struct __go_array_type *) descriptor);
238     case GO_SLICE:
239       return go_slice_to_ffi ((const struct __go_slice_type *) descriptor);
240     case GO_STRUCT:
241       return go_struct_to_ffi ((const struct __go_struct_type *) descriptor);
242     case GO_STRING:
243       return go_string_to_ffi ();
244     case GO_INTERFACE:
245       return go_interface_to_ffi ();
246     case GO_CHAN:
247     case GO_FUNC:
248     case GO_MAP:
249     case GO_PTR:
250     case GO_UNSAFE_POINTER:
251       /* These types are always pointers, and for FFI purposes nothing
252 	 else matters.  */
253       return &ffi_type_pointer;
254     default:
255       abort ();
256     }
257 }
258 
259 /* Return the return type for a function, given the number of out
260    parameters and their types.  */
261 
262 static ffi_type *
go_func_return_ffi(const struct __go_func_type * func)263 go_func_return_ffi (const struct __go_func_type *func)
264 {
265   int count;
266   const struct __go_type_descriptor **types;
267   ffi_type *ret;
268   int i;
269 
270   count = func->__out.__count;
271   if (count == 0)
272     return &ffi_type_void;
273 
274   types = (const struct __go_type_descriptor **) func->__out.__values;
275 
276   if (count == 1)
277     return go_type_to_ffi (types[0]);
278 
279   ret = (ffi_type *) __go_alloc (sizeof (ffi_type));
280   ret->type = FFI_TYPE_STRUCT;
281   ret->elements = (ffi_type **) __go_alloc ((count + 1) * sizeof (ffi_type *));
282   for (i = 0; i < count; ++i)
283     ret->elements[i] = go_type_to_ffi (types[i]);
284   ret->elements[count] = NULL;
285   return ret;
286 }
287 
288 /* Build an ffi_cif structure for a function described by a
289    __go_func_type structure.  */
290 
291 static void
go_func_to_cif(const struct __go_func_type * func,_Bool is_interface,_Bool is_method,ffi_cif * cif)292 go_func_to_cif (const struct __go_func_type *func, _Bool is_interface,
293 		_Bool is_method, ffi_cif *cif)
294 {
295   int num_params;
296   const struct __go_type_descriptor **in_types;
297   size_t num_args;
298   ffi_type **args;
299   int off;
300   int i;
301   ffi_type *rettype;
302   ffi_status status;
303 
304   num_params = func->__in.__count;
305   in_types = ((const struct __go_type_descriptor **)
306 	      func->__in.__values);
307 
308   num_args = num_params + (is_interface ? 1 : 0);
309   args = (ffi_type **) __go_alloc (num_args * sizeof (ffi_type *));
310   i = 0;
311   off = 0;
312   if (is_interface)
313     {
314       args[0] = &ffi_type_pointer;
315       off = 1;
316     }
317   else if (is_method)
318     {
319       args[0] = &ffi_type_pointer;
320       i = 1;
321     }
322   for (; i < num_params; ++i)
323     args[i + off] = go_type_to_ffi (in_types[i]);
324 
325   rettype = go_func_return_ffi (func);
326 
327   status = ffi_prep_cif (cif, FFI_DEFAULT_ABI, num_args, rettype, args);
328   __go_assert (status == FFI_OK);
329 }
330 
331 /* Get the total size required for the result parameters of a
332    function.  */
333 
334 static size_t
go_results_size(const struct __go_func_type * func)335 go_results_size (const struct __go_func_type *func)
336 {
337   int count;
338   const struct __go_type_descriptor **types;
339   size_t off;
340   size_t maxalign;
341   int i;
342 
343   count = func->__out.__count;
344   if (count == 0)
345     return 0;
346 
347   types = (const struct __go_type_descriptor **) func->__out.__values;
348 
349   /* A single integer return value is always promoted to a full
350      word.  */
351   if (count == 1)
352     {
353       switch (types[0]->__code & GO_CODE_MASK)
354 	{
355 	case GO_BOOL:
356 	case GO_INT8:
357 	case GO_INT16:
358 	case GO_INT32:
359 	case GO_UINT8:
360 	case GO_UINT16:
361 	case GO_UINT32:
362 	case GO_INT:
363 	case GO_UINT:
364 	  return sizeof (ffi_arg);
365 
366 	default:
367 	  break;
368 	}
369     }
370 
371   off = 0;
372   maxalign = 0;
373   for (i = 0; i < count; ++i)
374     {
375       size_t align;
376 
377       align = types[i]->__field_align;
378       if (align > maxalign)
379 	maxalign = align;
380       off = (off + align - 1) & ~ (align - 1);
381       off += types[i]->__size;
382     }
383 
384   off = (off + maxalign - 1) & ~ (maxalign - 1);
385 
386   return off;
387 }
388 
389 /* Copy the results of calling a function via FFI from CALL_RESULT
390    into the addresses in RESULTS.  */
391 
392 static void
go_set_results(const struct __go_func_type * func,unsigned char * call_result,void ** results)393 go_set_results (const struct __go_func_type *func, unsigned char *call_result,
394 		void **results)
395 {
396   int count;
397   const struct __go_type_descriptor **types;
398   size_t off;
399   int i;
400 
401   count = func->__out.__count;
402   if (count == 0)
403     return;
404 
405   types = (const struct __go_type_descriptor **) func->__out.__values;
406 
407   /* A single integer return value is always promoted to a full
408      word.  */
409   if (count == 1)
410     {
411       switch (types[0]->__code & GO_CODE_MASK)
412 	{
413 	case GO_BOOL:
414 	case GO_INT8:
415 	case GO_INT16:
416 	case GO_INT32:
417 	case GO_UINT8:
418 	case GO_UINT16:
419 	case GO_UINT32:
420 	case GO_INT:
421 	case GO_UINT:
422 	  {
423 	    union
424 	    {
425 	      unsigned char buf[sizeof (ffi_arg)];
426 	      ffi_arg v;
427 	    } u;
428 	    ffi_arg v;
429 
430 	    __builtin_memcpy (&u.buf, call_result, sizeof (ffi_arg));
431 	    v = u.v;
432 
433 	    switch (types[0]->__size)
434 	      {
435 	      case 1:
436 		{
437 		  uint8_t b;
438 
439 		  b = (uint8_t) v;
440 		  __builtin_memcpy (results[0], &b, 1);
441 		}
442 		break;
443 
444 	      case 2:
445 		{
446 		  uint16_t s;
447 
448 		  s = (uint16_t) v;
449 		  __builtin_memcpy (results[0], &s, 2);
450 		}
451 		break;
452 
453 	      case 4:
454 		{
455 		  uint32_t w;
456 
457 		  w = (uint32_t) v;
458 		  __builtin_memcpy (results[0], &w, 4);
459 		}
460 		break;
461 
462 	      case 8:
463 		{
464 		  uint64_t d;
465 
466 		  d = (uint64_t) v;
467 		  __builtin_memcpy (results[0], &d, 8);
468 		}
469 		break;
470 
471 	      default:
472 		abort ();
473 	      }
474 	  }
475 	  return;
476 
477 	default:
478 	  break;
479 	}
480     }
481 
482   off = 0;
483   for (i = 0; i < count; ++i)
484     {
485       size_t align;
486       size_t size;
487 
488       align = types[i]->__field_align;
489       size = types[i]->__size;
490       off = (off + align - 1) & ~ (align - 1);
491       __builtin_memcpy (results[i], call_result + off, size);
492       off += size;
493     }
494 }
495 
496 /* Call a function.  The type of the function is FUNC_TYPE, and the
497    closure is FUNC_VAL.  PARAMS is an array of parameter addresses.
498    RESULTS is an array of result addresses.
499 
500    If IS_INTERFACE is true this is a call to an interface method and
501    the first argument is the receiver, which is always a pointer.
502    This argument, the receiver, is not described in FUNC_TYPE.
503 
504    If IS_METHOD is true this is a call to a method expression.  The
505    first argument is the receiver.  It is described in FUNC_TYPE, but
506    regardless of FUNC_TYPE, it is passed as a pointer.
507 
508    If neither IS_INTERFACE nor IS_METHOD is true then we are calling a
509    function indirectly, and we must pass a closure pointer via
510    __go_set_closure.  The pointer to pass is simply FUNC_VAL.  */
511 
512 void
reflect_call(const struct __go_func_type * func_type,FuncVal * func_val,_Bool is_interface,_Bool is_method,void ** params,void ** results)513 reflect_call (const struct __go_func_type *func_type, FuncVal *func_val,
514 	      _Bool is_interface, _Bool is_method, void **params,
515 	      void **results)
516 {
517   ffi_cif cif;
518   unsigned char *call_result;
519 
520   __go_assert ((func_type->__common.__code & GO_CODE_MASK) == GO_FUNC);
521   go_func_to_cif (func_type, is_interface, is_method, &cif);
522 
523   call_result = (unsigned char *) malloc (go_results_size (func_type));
524 
525   if (!is_interface && !is_method)
526     __go_set_closure (func_val);
527   ffi_call (&cif, func_val->fn, call_result, params);
528 
529   /* Some day we may need to free result values if RESULTS is
530      NULL.  */
531   if (results != NULL)
532     go_set_results (func_type, call_result, results);
533 
534   free (call_result);
535 }
536 
537 #else /* !defined(USE_LIBFFI) */
538 
539 void
reflect_call(const struct __go_func_type * func_type,FuncVal * func_val,_Bool is_interface,_Bool is_method,void ** params,void ** results)540 reflect_call (const struct __go_func_type *func_type __attribute__ ((unused)),
541 	      FuncVal *func_val __attribute__ ((unused)),
542 	      _Bool is_interface __attribute__ ((unused)),
543 	      _Bool is_method __attribute__ ((unused)),
544 	      void **params __attribute__ ((unused)),
545 	      void **results __attribute__ ((unused)))
546 {
547   /* Without FFI there is nothing we can do.  */
548   runtime_throw("libgo built without FFI does not support "
549 		"reflect.Call or runtime.SetFinalizer");
550 }
551 
552 #endif /* !defined(USE_LIBFFI) */
553