You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to create a block using the C API? mrb_yield keeps crashing! #5932
I have come to the conclusion that this issue should be addressed.
This is because functions defined as methods can cause SIGSEGV if they use mrb_yield() or mrb_yield_argv() to call block arguments.
The block object to be given to such a method is created by mrb_proc_new_cfunc() as in this issue.
The place where SIGSEGV occurs is MRB_PROC_ENV(p)->stack[0] in the mrb_yield() function.
This is because MRB_PROC_ENV(p) is NULL for block objects created by mrb_proc_new_cfunc().
The following is an example for reference:
% cat 5932a.c#include <mruby.h>#include <mruby/compile.h>#include <mruby/proc.h>#include <stdlib.h>static mrb_valuereporter(mrb_state *mrb, mrb_value self){ mrb_value o = mrb_get_arg1(mrb); mrb_p(mrb, o); return mrb_nil_value();}intmain(int argc, char *argv[]){ int mode = (argc > 1) ? strtol(argv[1], NULL, 0) : 0; mrb_state *mrb = mrb_open(); mrb_value b; if (mode == 0) { // Generate block objects that wrap C functions b = mrb_obj_value(mrb_proc_new_cfunc(mrb, reporter)); } else { // Generate block object as top-level scope mrbc_context *mrbc = mrbc_context_new(mrb); mrbc->no_exec = TRUE; mrb_load_string_cxt(mrb, "a = nil", mrbc); // Allocate as the first argument b = mrb_load_string_cxt(mrb, "p a", mrbc); mrbc_context_free(mrb, mrbc); } mrb_value objspace = mrb_obj_value(mrb_module_get(mrb, "ObjectSpace")); mrb_sym mid = mrb_intern_lit(mrb, "each_object"); mrb_funcall_with_block(mrb, objspace, mid, 0, NULL, b); if (mrb->exc) { mrb_print_error(mrb); } mrb_close(mrb); return 0;}
% $(bin/mruby-config --cc --cflags) 5932a.c $(bin/mruby-config --ldflags --libs)
% ./a.out 0 # case using mrb_proc_new_cfunc()zsh: segmentation fault (core dumped) ./a.out 0
% ./a.out 1 # case using mrb_load_string_cxt() with mrbc_context::no_execzsh: segmentation fault (core dumped) ./a.out 1
Activity
matz commentedon Feb 25, 2023
Since your code snippet is incomplete, I can only guess the situation. Probably, you passed the wrong value to
mrb_yieldfunction.dearblue commentedon Mar 14, 2023
I have come to the conclusion that this issue should be addressed.
This is because functions defined as methods can cause
SIGSEGVif they usemrb_yield()ormrb_yield_argv()to call block arguments.The block object to be given to such a method is created by
mrb_proc_new_cfunc()as in this issue.The place where
SIGSEGVoccurs isMRB_PROC_ENV(p)->stack[0]in themrb_yield()function.This is because
MRB_PROC_ENV(p)isNULLfor block objects created bymrb_proc_new_cfunc().The following is an example for reference:
Safely retrieve `self` from proc
Safely retrieve `self` from proc in `mrb_yield()`
Safely retrieve `self` from proc in `mrb_yield()`