2010-05-06 02:59:24 -04:00
|
|
|
#include <fiddle.h>
|
2015-11-23 16:20:56 -05:00
|
|
|
#include <ruby/thread.h>
|
2010-05-06 02:59:24 -04:00
|
|
|
|
2014-01-15 03:16:44 -05:00
|
|
|
#ifdef PRIsVALUE
|
|
|
|
# define RB_OBJ_CLASSNAME(obj) rb_obj_class(obj)
|
|
|
|
# define RB_OBJ_STRING(obj) (obj)
|
|
|
|
#else
|
|
|
|
# define PRIsVALUE "s"
|
|
|
|
# define RB_OBJ_CLASSNAME(obj) rb_obj_classname(obj)
|
|
|
|
# define RB_OBJ_STRING(obj) StringValueCStr(obj)
|
|
|
|
#endif
|
|
|
|
|
2010-05-06 02:59:24 -04:00
|
|
|
VALUE cFiddleFunction;
|
|
|
|
|
2014-03-08 23:51:17 -05:00
|
|
|
#define MAX_ARGS (SIZE_MAX / (sizeof(void *) + sizeof(fiddle_generic)) - 1)
|
|
|
|
|
|
|
|
#define Check_Max_Args(name, len) \
|
2015-12-13 04:35:58 -05:00
|
|
|
Check_Max_Args_(name, len, "")
|
|
|
|
#define Check_Max_Args_Long(name, len) \
|
|
|
|
Check_Max_Args_(name, len, "l")
|
|
|
|
#define Check_Max_Args_(name, len, fmt) \
|
2014-03-08 23:51:17 -05:00
|
|
|
if ((size_t)(len) < MAX_ARGS) { \
|
|
|
|
/* OK */ \
|
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
rb_raise(rb_eTypeError, \
|
2015-12-13 04:35:58 -05:00
|
|
|
name" is so large that it can cause integer overflow (%"fmt"d)", \
|
2014-03-08 23:51:17 -05:00
|
|
|
(len)); \
|
|
|
|
}
|
|
|
|
|
2010-05-06 02:59:24 -04:00
|
|
|
static void
|
|
|
|
deallocate(void *p)
|
|
|
|
{
|
|
|
|
ffi_cif *ptr = p;
|
|
|
|
if (ptr->arg_types) xfree(ptr->arg_types);
|
|
|
|
xfree(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
function_memsize(const void *p)
|
|
|
|
{
|
|
|
|
/* const */ffi_cif *ptr = (ffi_cif *)p;
|
|
|
|
size_t size = 0;
|
|
|
|
|
2015-12-08 20:01:17 -05:00
|
|
|
size += sizeof(*ptr);
|
2010-05-06 02:59:24 -04:00
|
|
|
#if !defined(FFI_NO_RAW_API) || !FFI_NO_RAW_API
|
2015-12-08 20:01:17 -05:00
|
|
|
size += ffi_raw_size(ptr);
|
2010-05-06 02:59:24 -04:00
|
|
|
#endif
|
2015-12-08 20:01:17 -05:00
|
|
|
|
2010-05-06 02:59:24 -04:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
const rb_data_type_t function_data_type = {
|
|
|
|
"fiddle/function",
|
2010-07-20 05:23:08 -04:00
|
|
|
{0, deallocate, function_memsize,},
|
2010-05-06 02:59:24 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
allocate(VALUE klass)
|
|
|
|
{
|
|
|
|
ffi_cif * cif;
|
|
|
|
|
|
|
|
return TypedData_Make_Struct(klass, ffi_cif, &function_data_type, cif);
|
|
|
|
}
|
|
|
|
|
2012-11-27 14:54:50 -05:00
|
|
|
VALUE
|
|
|
|
rb_fiddle_new_function(VALUE address, VALUE arg_types, VALUE ret_type)
|
|
|
|
{
|
|
|
|
VALUE argv[3];
|
|
|
|
|
|
|
|
argv[0] = address;
|
|
|
|
argv[1] = arg_types;
|
|
|
|
argv[2] = ret_type;
|
|
|
|
|
|
|
|
return rb_class_new_instance(3, argv, cFiddleFunction);
|
|
|
|
}
|
|
|
|
|
2012-12-11 05:41:00 -05:00
|
|
|
static int
|
|
|
|
parse_keyword_arg_i(VALUE key, VALUE value, VALUE self)
|
|
|
|
{
|
2014-01-15 03:16:32 -05:00
|
|
|
if (key == ID2SYM(rb_intern("name"))) {
|
|
|
|
rb_iv_set(self, "@name", value);
|
|
|
|
} else {
|
2014-01-15 03:16:44 -05:00
|
|
|
rb_raise(rb_eArgError, "unknown keyword: %"PRIsVALUE,
|
|
|
|
RB_OBJ_STRING(key));
|
2014-01-15 03:16:32 -05:00
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
2012-12-11 05:41:00 -05:00
|
|
|
}
|
|
|
|
|
2010-05-06 02:59:24 -04:00
|
|
|
static VALUE
|
|
|
|
initialize(int argc, VALUE argv[], VALUE self)
|
|
|
|
{
|
|
|
|
ffi_cif * cif;
|
2015-12-13 04:35:58 -05:00
|
|
|
ffi_type **arg_types, *rtype;
|
2010-05-06 02:59:24 -04:00
|
|
|
ffi_status result;
|
2015-12-13 05:25:01 -05:00
|
|
|
VALUE ptr, args, ret_type, abi, kwds, ary;
|
2015-12-13 05:57:51 -05:00
|
|
|
int i, len;
|
2015-12-13 04:35:58 -05:00
|
|
|
int nabi;
|
|
|
|
void *cfunc;
|
2010-05-06 02:59:24 -04:00
|
|
|
|
2012-12-11 05:41:00 -05:00
|
|
|
rb_scan_args(argc, argv, "31:", &ptr, &args, &ret_type, &abi, &kwds);
|
2015-12-13 04:35:58 -05:00
|
|
|
ptr = rb_Integer(ptr);
|
|
|
|
cfunc = NUM2PTR(ptr);
|
|
|
|
PTR2NUM(cfunc);
|
|
|
|
nabi = NIL_P(abi) ? FFI_DEFAULT_ABI : NUM2INT(abi);
|
|
|
|
abi = INT2FIX(nabi);
|
|
|
|
i = NUM2INT(ret_type);
|
|
|
|
rtype = INT2FFI_TYPE(i);
|
|
|
|
ret_type = INT2FIX(i);
|
2010-05-06 02:59:24 -04:00
|
|
|
|
|
|
|
Check_Type(args, T_ARRAY);
|
2015-12-13 04:35:58 -05:00
|
|
|
len = RARRAY_LENINT(args);
|
2015-12-13 05:57:51 -05:00
|
|
|
Check_Max_Args("args", len);
|
|
|
|
ary = rb_ary_subseq(args, 0, len);
|
2015-12-13 04:35:58 -05:00
|
|
|
for (i = 0; i < RARRAY_LEN(args); i++) {
|
|
|
|
VALUE a = RARRAY_PTR(args)[i];
|
|
|
|
int type = NUM2INT(a);
|
|
|
|
(void)INT2FFI_TYPE(type); /* raise */
|
|
|
|
if (INT2FIX(type) != a) rb_ary_store(ary, i, INT2FIX(type));
|
|
|
|
}
|
|
|
|
OBJ_FREEZE(ary);
|
2010-05-06 02:59:24 -04:00
|
|
|
|
|
|
|
rb_iv_set(self, "@ptr", ptr);
|
|
|
|
rb_iv_set(self, "@args", args);
|
|
|
|
rb_iv_set(self, "@return_type", ret_type);
|
|
|
|
rb_iv_set(self, "@abi", abi);
|
|
|
|
|
2012-12-11 05:41:00 -05:00
|
|
|
if (!NIL_P(kwds)) rb_hash_foreach(kwds, parse_keyword_arg_i, self);
|
|
|
|
|
2010-05-06 02:59:24 -04:00
|
|
|
TypedData_Get_Struct(self, ffi_cif, &function_data_type, cif);
|
|
|
|
|
2015-12-13 04:35:58 -05:00
|
|
|
arg_types = xcalloc(len + 1, sizeof(ffi_type *));
|
2010-05-06 02:59:24 -04:00
|
|
|
|
|
|
|
for (i = 0; i < RARRAY_LEN(args); i++) {
|
2015-11-04 20:09:17 -05:00
|
|
|
int type = NUM2INT(RARRAY_AREF(args, i));
|
2010-05-06 02:59:24 -04:00
|
|
|
arg_types[i] = INT2FFI_TYPE(type);
|
|
|
|
}
|
2015-12-13 04:35:58 -05:00
|
|
|
arg_types[len] = NULL;
|
2010-05-06 02:59:24 -04:00
|
|
|
|
2015-12-13 04:35:58 -05:00
|
|
|
result = ffi_prep_cif(cif, nabi, len, rtype, arg_types);
|
2010-05-06 02:59:24 -04:00
|
|
|
|
|
|
|
if (result)
|
|
|
|
rb_raise(rb_eRuntimeError, "error creating CIF %d", result);
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-11-23 16:20:56 -05:00
|
|
|
struct nogvl_ffi_call_args {
|
|
|
|
ffi_cif *cif;
|
|
|
|
void (*fn)(void);
|
|
|
|
void **values;
|
|
|
|
fiddle_generic retval;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void *
|
|
|
|
nogvl_ffi_call(void *ptr)
|
|
|
|
{
|
|
|
|
struct nogvl_ffi_call_args *args = ptr;
|
|
|
|
|
|
|
|
ffi_call(args->cif, args->fn, &args->retval, args->values);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-05-06 02:59:24 -04:00
|
|
|
static VALUE
|
|
|
|
function_call(int argc, VALUE argv[], VALUE self)
|
|
|
|
{
|
2015-11-23 16:20:56 -05:00
|
|
|
struct nogvl_ffi_call_args args = { 0 };
|
2010-05-06 02:59:24 -04:00
|
|
|
fiddle_generic *generic_args;
|
|
|
|
VALUE cfunc, types, cPointer;
|
|
|
|
int i;
|
2014-03-08 23:51:38 -05:00
|
|
|
VALUE alloc_buffer = 0;
|
2010-05-06 02:59:24 -04:00
|
|
|
|
|
|
|
cfunc = rb_iv_get(self, "@ptr");
|
|
|
|
types = rb_iv_get(self, "@args");
|
|
|
|
cPointer = rb_const_get(mFiddle, rb_intern("Pointer"));
|
|
|
|
|
2014-03-08 23:51:17 -05:00
|
|
|
Check_Max_Args("number of arguments", argc);
|
2015-10-24 20:43:06 -04:00
|
|
|
if (argc != (i = RARRAY_LENINT(types))) {
|
|
|
|
rb_error_arity(argc, i, i);
|
2010-05-06 02:59:24 -04:00
|
|
|
}
|
|
|
|
|
2015-11-23 16:20:56 -05:00
|
|
|
TypedData_Get_Struct(self, ffi_cif, &function_data_type, args.cif);
|
2010-05-06 02:59:24 -04:00
|
|
|
|
2013-05-14 07:06:58 -04:00
|
|
|
if (rb_safe_level() >= 1) {
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
VALUE src = argv[i];
|
|
|
|
if (OBJ_TAINTED(src)) {
|
|
|
|
rb_raise(rb_eSecurityError, "tainted parameter not allowed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-08 23:51:38 -05:00
|
|
|
generic_args = ALLOCV(alloc_buffer,
|
|
|
|
(size_t)(argc + 1) * sizeof(void *) + (size_t)argc * sizeof(fiddle_generic));
|
2015-11-23 16:20:56 -05:00
|
|
|
args.values = (void **)((char *)generic_args +
|
|
|
|
(size_t)argc * sizeof(fiddle_generic));
|
2010-05-06 02:59:24 -04:00
|
|
|
|
|
|
|
for (i = 0; i < argc; i++) {
|
2015-11-04 20:09:17 -05:00
|
|
|
VALUE type = RARRAY_AREF(types, i);
|
2010-05-06 02:59:24 -04:00
|
|
|
VALUE src = argv[i];
|
2015-12-13 04:35:58 -05:00
|
|
|
int argtype = FIX2INT(type);
|
2010-05-06 02:59:24 -04:00
|
|
|
|
2015-12-13 04:35:58 -05:00
|
|
|
if (argtype == TYPE_VOIDP) {
|
2010-05-06 02:59:24 -04:00
|
|
|
if(NIL_P(src)) {
|
2013-03-05 11:31:19 -05:00
|
|
|
src = INT2FIX(0);
|
2010-05-06 02:59:24 -04:00
|
|
|
} else if(cPointer != CLASS_OF(src)) {
|
2013-03-05 11:31:19 -05:00
|
|
|
src = rb_funcall(cPointer, rb_intern("[]"), 1, src);
|
2010-05-06 02:59:24 -04:00
|
|
|
}
|
|
|
|
src = rb_Integer(src);
|
|
|
|
}
|
|
|
|
|
2015-12-13 04:35:58 -05:00
|
|
|
VALUE2GENERIC(argtype, src, &generic_args[i]);
|
2015-11-23 16:20:56 -05:00
|
|
|
args.values[i] = (void *)&generic_args[i];
|
2010-05-06 02:59:24 -04:00
|
|
|
}
|
2015-11-23 16:20:56 -05:00
|
|
|
args.values[argc] = NULL;
|
2015-12-13 04:35:58 -05:00
|
|
|
args.fn = NUM2PTR(cfunc);
|
2010-05-06 02:59:24 -04:00
|
|
|
|
2015-11-23 16:20:56 -05:00
|
|
|
(void)rb_thread_call_without_gvl(nogvl_ffi_call, &args, 0, 0);
|
2010-05-06 02:59:24 -04:00
|
|
|
|
|
|
|
rb_funcall(mFiddle, rb_intern("last_error="), 1, INT2NUM(errno));
|
2011-01-04 22:13:18 -05:00
|
|
|
#if defined(_WIN32)
|
2010-05-06 02:59:24 -04:00
|
|
|
rb_funcall(mFiddle, rb_intern("win32_last_error="), 1, INT2NUM(errno));
|
|
|
|
#endif
|
|
|
|
|
2014-03-08 23:51:38 -05:00
|
|
|
ALLOCV_END(alloc_buffer);
|
2010-05-06 02:59:24 -04:00
|
|
|
|
2015-11-23 16:20:56 -05:00
|
|
|
return GENERIC2VALUE(rb_iv_get(self, "@return_type"), args.retval);
|
2010-05-06 02:59:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Init_fiddle_function(void)
|
|
|
|
{
|
2011-08-15 19:48:59 -04:00
|
|
|
/*
|
|
|
|
* Document-class: Fiddle::Function
|
|
|
|
*
|
|
|
|
* == Description
|
|
|
|
*
|
|
|
|
* A representation of a C function
|
|
|
|
*
|
|
|
|
* == Examples
|
|
|
|
*
|
|
|
|
* === 'strcpy'
|
|
|
|
*
|
2012-11-27 18:26:07 -05:00
|
|
|
* @libc = Fiddle.dlopen "/lib/libc.so.6"
|
|
|
|
* #=> #<Fiddle::Handle:0x00000001d7a8d8>
|
|
|
|
* f = Fiddle::Function.new(
|
|
|
|
* @libc['strcpy'],
|
|
|
|
* [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP],
|
|
|
|
* Fiddle::TYPE_VOIDP)
|
|
|
|
* #=> #<Fiddle::Function:0x00000001d8ee00>
|
2011-08-15 19:48:59 -04:00
|
|
|
* buff = "000"
|
2012-11-27 18:26:07 -05:00
|
|
|
* #=> "000"
|
2011-08-15 19:48:59 -04:00
|
|
|
* str = f.call(buff, "123")
|
2012-11-27 18:26:07 -05:00
|
|
|
* #=> #<Fiddle::Pointer:0x00000001d0c380 ptr=0x000000018a21b8 size=0 free=0x00000000000000>
|
2011-08-15 19:48:59 -04:00
|
|
|
* str.to_s
|
|
|
|
* => "123"
|
|
|
|
*
|
|
|
|
* === ABI check
|
|
|
|
*
|
2014-10-31 17:13:09 -04:00
|
|
|
* @libc = Fiddle.dlopen "/lib/libc.so.6"
|
2012-11-27 18:26:07 -05:00
|
|
|
* #=> #<Fiddle::Handle:0x00000001d7a8d8>
|
2011-08-15 19:48:59 -04:00
|
|
|
* f = Fiddle::Function.new(@libc['strcpy'], [TYPE_VOIDP, TYPE_VOIDP], TYPE_VOIDP)
|
2012-11-27 18:26:07 -05:00
|
|
|
* #=> #<Fiddle::Function:0x00000001d8ee00>
|
2011-08-15 19:48:59 -04:00
|
|
|
* f.abi == Fiddle::Function::DEFAULT
|
2012-11-27 18:26:07 -05:00
|
|
|
* #=> true
|
2011-08-15 19:48:59 -04:00
|
|
|
*/
|
2010-05-06 02:59:24 -04:00
|
|
|
cFiddleFunction = rb_define_class_under(mFiddle, "Function", rb_cObject);
|
|
|
|
|
2011-08-15 19:48:59 -04:00
|
|
|
/*
|
|
|
|
* Document-const: DEFAULT
|
|
|
|
*
|
|
|
|
* Default ABI
|
|
|
|
*
|
|
|
|
*/
|
2010-05-06 02:59:24 -04:00
|
|
|
rb_define_const(cFiddleFunction, "DEFAULT", INT2NUM(FFI_DEFAULT_ABI));
|
|
|
|
|
2012-11-30 10:57:21 -05:00
|
|
|
#ifdef HAVE_CONST_FFI_STDCALL
|
2011-08-15 19:48:59 -04:00
|
|
|
/*
|
|
|
|
* Document-const: STDCALL
|
|
|
|
*
|
|
|
|
* FFI implementation of WIN32 stdcall convention
|
|
|
|
*
|
|
|
|
*/
|
2010-05-06 02:59:24 -04:00
|
|
|
rb_define_const(cFiddleFunction, "STDCALL", INT2NUM(FFI_STDCALL));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
rb_define_alloc_func(cFiddleFunction, allocate);
|
|
|
|
|
2011-08-15 19:48:59 -04:00
|
|
|
/*
|
|
|
|
* Document-method: call
|
|
|
|
*
|
2015-12-11 02:43:09 -05:00
|
|
|
* Calls the constructed Function, with +args+.
|
|
|
|
* Caller must ensure the underlying function is called in a
|
|
|
|
* thread-safe manner if running in a multi-threaded process.
|
2011-08-15 19:48:59 -04:00
|
|
|
*
|
|
|
|
* For an example see Fiddle::Function
|
|
|
|
*
|
|
|
|
*/
|
2010-05-06 02:59:24 -04:00
|
|
|
rb_define_method(cFiddleFunction, "call", function_call, -1);
|
2011-08-15 19:48:59 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: new
|
2012-12-06 23:35:43 -05:00
|
|
|
* call-seq: new(ptr, args, ret_type, abi = DEFAULT)
|
2011-08-15 19:48:59 -04:00
|
|
|
*
|
|
|
|
* Constructs a Function object.
|
2012-11-27 18:26:07 -05:00
|
|
|
* * +ptr+ is a referenced function, of a Fiddle::Handle
|
2011-08-15 19:48:59 -04:00
|
|
|
* * +args+ is an Array of arguments, passed to the +ptr+ function
|
|
|
|
* * +ret_type+ is the return type of the function
|
|
|
|
* * +abi+ is the ABI of the function
|
|
|
|
*
|
|
|
|
*/
|
2010-05-06 02:59:24 -04:00
|
|
|
rb_define_method(cFiddleFunction, "initialize", initialize, -1);
|
|
|
|
}
|
|
|
|
/* vim: set noet sws=4 sw=4: */
|