Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug #20451] merge revision(s) a4ad6bd9aac564e93219284c912b26a72f9e82fc: #10696

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 43 additions & 13 deletions ext/fiddle/closure.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,16 @@ allocate(VALUE klass)
return i;
}

typedef struct {
VALUE self;
int argc;
VALUE *argv;
} initialize_data;

static VALUE
initialize(int rbargc, VALUE argv[], VALUE self)
initialize_body(VALUE user_data)
{
initialize_data *data = (initialize_data *)user_data;
VALUE ret;
VALUE args;
VALUE normalized_args;
Expand All @@ -237,14 +244,14 @@ initialize(int rbargc, VALUE argv[], VALUE self)
ffi_status result;
int i, argc;

if (2 == rb_scan_args(rbargc, argv, "21", &ret, &args, &abi))
abi = INT2NUM(FFI_DEFAULT_ABI);
if (2 == rb_scan_args(data->argc, data->argv, "21", &ret, &args, &abi))
abi = INT2NUM(FFI_DEFAULT_ABI);

Check_Type(args, T_ARRAY);

argc = RARRAY_LENINT(args);

TypedData_Get_Struct(self, fiddle_closure, &closure_data_type, cl);
TypedData_Get_Struct(data->self, fiddle_closure, &closure_data_type, cl);

cl->argv = (ffi_type **)xcalloc(argc + 1, sizeof(ffi_type *));

Expand All @@ -257,8 +264,8 @@ initialize(int rbargc, VALUE argv[], VALUE self)
cl->argv[argc] = NULL;

ret = rb_fiddle_type_ensure(ret);
rb_iv_set(self, "@ctype", ret);
rb_iv_set(self, "@args", normalized_args);
rb_iv_set(data->self, "@ctype", ret);
rb_iv_set(data->self, "@args", normalized_args);

cif = &cl->cif;
pcl = cl->pcl;
Expand All @@ -269,25 +276,48 @@ initialize(int rbargc, VALUE argv[], VALUE self)
rb_fiddle_int_to_ffi_type(NUM2INT(ret)),
cl->argv);

if (FFI_OK != result)
rb_raise(rb_eRuntimeError, "error prepping CIF %d", result);
if (FFI_OK != result) {
rb_raise(rb_eRuntimeError, "error prepping CIF %d", result);
}

#if USE_FFI_CLOSURE_ALLOC
result = ffi_prep_closure_loc(pcl, cif, callback,
(void *)self, cl->code);
(void *)(data->self), cl->code);
#else
result = ffi_prep_closure(pcl, cif, callback, (void *)(data->self));
cl->code = (void *)pcl;
i = mprotect(pcl, sizeof(*pcl), PROT_READ | PROT_EXEC);
if (i) {
rb_sys_fail("mprotect");
rb_sys_fail("mprotect");
}
#endif

if (FFI_OK != result)
rb_raise(rb_eRuntimeError, "error prepping closure %d", result);
if (FFI_OK != result) {
rb_raise(rb_eRuntimeError, "error prepping closure %d", result);
}

return data->self;
}

return self;
static VALUE
initialize_rescue(VALUE user_data, VALUE exception)
{
initialize_data *data = (initialize_data *)user_data;
dealloc(RTYPEDDATA_DATA(data->self));
RTYPEDDATA_DATA(data->self) = NULL;
rb_exc_raise(exception);
return data->self;
}

static VALUE
initialize(int argc, VALUE *argv, VALUE self)
{
initialize_data data;
data.self = self;
data.argc = argc;
data.argv = argv;
return rb_rescue(initialize_body, (VALUE)&data,
initialize_rescue, (VALUE)&data);
}

static VALUE
Expand Down
6 changes: 3 additions & 3 deletions version.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
#define RUBY_VERSION_TEENY 5
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
#define RUBY_PATCHLEVEL 252
#define RUBY_PATCHLEVEL 253

#define RUBY_RELEASE_YEAR 2024
#define RUBY_RELEASE_MONTH 4
#define RUBY_RELEASE_DAY 23
#define RUBY_RELEASE_MONTH 5
#define RUBY_RELEASE_DAY 2

#include "ruby/version.h"

Expand Down