2012-03-27 23:13:14 +00:00
|
|
|
/* go-reflect-call.c -- call reflection support for Go.
|
|
|
|
|
|
|
|
Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
Use of this source code is governed by a BSD-style
|
|
|
|
license that can be found in the LICENSE file. */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
#include "runtime.h"
|
2012-03-27 23:13:14 +00:00
|
|
|
#include "go-assert.h"
|
2017-10-07 00:16:47 +00:00
|
|
|
|
|
|
|
#ifdef USE_LIBFFI
|
|
|
|
#include "ffi.h"
|
|
|
|
#endif
|
2012-03-27 23:13:14 +00:00
|
|
|
|
2015-08-28 15:33:40 +00:00
|
|
|
#if defined(USE_LIBFFI) && FFI_GO_CLOSURES
|
2012-03-27 23:13:14 +00:00
|
|
|
|
|
|
|
/* The functions in this file are only called from reflect_call. As
|
|
|
|
reflect_call calls a libffi function, which will be compiled
|
|
|
|
without -fsplit-stack, it will always run with a large stack. */
|
|
|
|
|
2022-10-27 18:55:19 +00:00
|
|
|
static size_t go_results_size (const struct functype *)
|
2012-03-27 23:13:14 +00:00
|
|
|
__attribute__ ((no_split_stack));
|
2022-10-27 18:55:19 +00:00
|
|
|
static void go_set_results (const struct functype *, unsigned char *, void **)
|
2012-03-27 23:13:14 +00:00
|
|
|
__attribute__ ((no_split_stack));
|
|
|
|
|
|
|
|
/* Get the total size required for the result parameters of a
|
|
|
|
function. */
|
|
|
|
|
|
|
|
static size_t
|
2022-10-27 18:55:19 +00:00
|
|
|
go_results_size (const struct functype *func)
|
2012-03-27 23:13:14 +00:00
|
|
|
{
|
|
|
|
int count;
|
2022-10-27 18:55:19 +00:00
|
|
|
const struct _type **types;
|
2012-03-27 23:13:14 +00:00
|
|
|
size_t off;
|
|
|
|
size_t maxalign;
|
|
|
|
int i;
|
|
|
|
|
2022-10-27 18:55:19 +00:00
|
|
|
count = func->out.__count;
|
2012-03-27 23:13:14 +00:00
|
|
|
if (count == 0)
|
|
|
|
return 0;
|
|
|
|
|
2022-10-27 18:55:19 +00:00
|
|
|
types = (const struct _type **) func->out.__values;
|
2012-03-27 23:13:14 +00:00
|
|
|
|
2022-10-27 18:55:19 +00:00
|
|
|
/* A single integer return value is always promoted to a full word.
|
|
|
|
There is similar code below and in libgo/go/reflect/makefunc_ffi.go.*/
|
2012-03-27 23:13:14 +00:00
|
|
|
if (count == 1)
|
|
|
|
{
|
2022-10-27 18:55:19 +00:00
|
|
|
switch (types[0]->kind & kindMask)
|
2012-03-27 23:13:14 +00:00
|
|
|
{
|
2022-10-27 18:55:19 +00:00
|
|
|
case kindBool:
|
|
|
|
case kindInt8:
|
|
|
|
case kindInt16:
|
|
|
|
case kindInt32:
|
|
|
|
case kindUint8:
|
|
|
|
case kindUint16:
|
|
|
|
case kindUint32:
|
2012-03-27 23:13:14 +00:00
|
|
|
return sizeof (ffi_arg);
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
off = 0;
|
|
|
|
maxalign = 0;
|
|
|
|
for (i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
size_t align;
|
|
|
|
|
2022-10-27 18:55:19 +00:00
|
|
|
align = types[i]->fieldAlign;
|
2012-03-27 23:13:14 +00:00
|
|
|
if (align > maxalign)
|
|
|
|
maxalign = align;
|
|
|
|
off = (off + align - 1) & ~ (align - 1);
|
2022-10-27 18:55:19 +00:00
|
|
|
off += types[i]->size;
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
off = (off + maxalign - 1) & ~ (maxalign - 1);
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
// The libffi library doesn't understand a struct with no fields.
|
|
|
|
// We generate a struct with a single field of type void. When used
|
|
|
|
// as a return value, libffi will think that requires a byte.
|
|
|
|
if (off == 0)
|
|
|
|
off = 1;
|
|
|
|
|
2012-03-27 23:13:14 +00:00
|
|
|
return off;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the results of calling a function via FFI from CALL_RESULT
|
|
|
|
into the addresses in RESULTS. */
|
|
|
|
|
|
|
|
static void
|
2022-10-27 18:55:19 +00:00
|
|
|
go_set_results (const struct functype *func, unsigned char *call_result,
|
2012-03-27 23:13:14 +00:00
|
|
|
void **results)
|
|
|
|
{
|
|
|
|
int count;
|
2022-10-27 18:55:19 +00:00
|
|
|
const struct _type **types;
|
2012-03-27 23:13:14 +00:00
|
|
|
size_t off;
|
|
|
|
int i;
|
|
|
|
|
2022-10-27 18:55:19 +00:00
|
|
|
count = func->out.__count;
|
2012-03-27 23:13:14 +00:00
|
|
|
if (count == 0)
|
|
|
|
return;
|
|
|
|
|
2022-10-27 18:55:19 +00:00
|
|
|
types = (const struct _type **) func->out.__values;
|
2012-03-27 23:13:14 +00:00
|
|
|
|
2022-10-27 18:55:19 +00:00
|
|
|
/* A single integer return value is always promoted to a full word.
|
|
|
|
There is similar code above and in libgo/go/reflect/makefunc_ffi.go.*/
|
2012-03-27 23:13:14 +00:00
|
|
|
if (count == 1)
|
|
|
|
{
|
2022-10-27 18:55:19 +00:00
|
|
|
switch (types[0]->kind & kindMask)
|
2012-03-27 23:13:14 +00:00
|
|
|
{
|
2022-10-27 18:55:19 +00:00
|
|
|
case kindBool:
|
|
|
|
case kindInt8:
|
|
|
|
case kindInt16:
|
|
|
|
case kindInt32:
|
|
|
|
case kindUint8:
|
|
|
|
case kindUint16:
|
|
|
|
case kindUint32:
|
2012-03-27 23:13:14 +00:00
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
unsigned char buf[sizeof (ffi_arg)];
|
|
|
|
ffi_arg v;
|
|
|
|
} u;
|
|
|
|
ffi_arg v;
|
|
|
|
|
|
|
|
__builtin_memcpy (&u.buf, call_result, sizeof (ffi_arg));
|
|
|
|
v = u.v;
|
|
|
|
|
2022-10-27 18:55:19 +00:00
|
|
|
switch (types[0]->size)
|
2012-03-27 23:13:14 +00:00
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
uint8_t b;
|
|
|
|
|
|
|
|
b = (uint8_t) v;
|
|
|
|
__builtin_memcpy (results[0], &b, 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
uint16_t s;
|
|
|
|
|
|
|
|
s = (uint16_t) v;
|
|
|
|
__builtin_memcpy (results[0], &s, 2);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
{
|
|
|
|
uint32_t w;
|
|
|
|
|
|
|
|
w = (uint32_t) v;
|
|
|
|
__builtin_memcpy (results[0], &w, 4);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
{
|
|
|
|
uint64_t d;
|
|
|
|
|
|
|
|
d = (uint64_t) v;
|
|
|
|
__builtin_memcpy (results[0], &d, 8);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
off = 0;
|
|
|
|
for (i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
size_t align;
|
|
|
|
size_t size;
|
|
|
|
|
2022-10-27 18:55:19 +00:00
|
|
|
align = types[i]->fieldAlign;
|
|
|
|
size = types[i]->size;
|
2012-03-27 23:13:14 +00:00
|
|
|
off = (off + align - 1) & ~ (align - 1);
|
|
|
|
__builtin_memcpy (results[i], call_result + off, size);
|
|
|
|
off += size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
/* The code that converts the Go type to an FFI type is written in Go,
|
|
|
|
so that it can allocate Go heap memory. */
|
2022-10-27 18:55:19 +00:00
|
|
|
extern void ffiFuncToCIF(const struct functype*, _Bool, _Bool, ffi_cif*)
|
2017-10-07 00:16:47 +00:00
|
|
|
__asm__ ("runtime.ffiFuncToCIF");
|
|
|
|
|
2012-03-27 23:13:14 +00:00
|
|
|
/* Call a function. The type of the function is FUNC_TYPE, and the
|
2014-09-21 17:33:12 +00:00
|
|
|
closure is FUNC_VAL. PARAMS is an array of parameter addresses.
|
|
|
|
RESULTS is an array of result addresses.
|
|
|
|
|
|
|
|
If IS_INTERFACE is true this is a call to an interface method and
|
|
|
|
the first argument is the receiver, which is always a pointer.
|
|
|
|
This argument, the receiver, is not described in FUNC_TYPE.
|
|
|
|
|
|
|
|
If IS_METHOD is true this is a call to a method expression. The
|
|
|
|
first argument is the receiver. It is described in FUNC_TYPE, but
|
2015-08-28 15:33:40 +00:00
|
|
|
regardless of FUNC_TYPE, it is passed as a pointer. */
|
2012-03-27 23:13:14 +00:00
|
|
|
|
|
|
|
void
|
2022-10-27 18:55:19 +00:00
|
|
|
reflect_call (const struct functype *func_type, FuncVal *func_val,
|
2012-03-27 23:13:14 +00:00
|
|
|
_Bool is_interface, _Bool is_method, void **params,
|
|
|
|
void **results)
|
|
|
|
{
|
|
|
|
ffi_cif cif;
|
|
|
|
unsigned char *call_result;
|
|
|
|
|
2022-10-27 18:55:19 +00:00
|
|
|
__go_assert ((func_type->typ.kind & kindMask) == kindFunc);
|
2017-10-07 00:16:47 +00:00
|
|
|
ffiFuncToCIF (func_type, is_interface, is_method, &cif);
|
2012-03-27 23:13:14 +00:00
|
|
|
|
|
|
|
call_result = (unsigned char *) malloc (go_results_size (func_type));
|
|
|
|
|
2019-06-02 15:48:37 +00:00
|
|
|
ffi_call_go (&cif, (void (*)(void)) func_val->fn, call_result, params,
|
|
|
|
func_val);
|
2012-03-27 23:13:14 +00:00
|
|
|
|
|
|
|
/* Some day we may need to free result values if RESULTS is
|
|
|
|
NULL. */
|
|
|
|
if (results != NULL)
|
|
|
|
go_set_results (func_type, call_result, results);
|
|
|
|
|
|
|
|
free (call_result);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !defined(USE_LIBFFI) */
|
|
|
|
|
|
|
|
void
|
2022-10-27 18:55:19 +00:00
|
|
|
reflect_call (const struct functype *func_type __attribute__ ((unused)),
|
2014-09-21 17:33:12 +00:00
|
|
|
FuncVal *func_val __attribute__ ((unused)),
|
2012-03-27 23:13:14 +00:00
|
|
|
_Bool is_interface __attribute__ ((unused)),
|
|
|
|
_Bool is_method __attribute__ ((unused)),
|
|
|
|
void **params __attribute__ ((unused)),
|
|
|
|
void **results __attribute__ ((unused)))
|
|
|
|
{
|
|
|
|
/* Without FFI there is nothing we can do. */
|
|
|
|
runtime_throw("libgo built without FFI does not support "
|
|
|
|
"reflect.Call or runtime.SetFinalizer");
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !defined(USE_LIBFFI) */
|