Skip to content

How to create a block using the C API? mrb_yield keeps crashing! #5932

Closed

Description

I'm trying to create a block using the C API, but I keep getting a segfault. This is the code below:

mrb_value proc_test(mrb_state *mrb, mrb_value self) {
        return mrb_false_value();                                            
}

struct RProc *p = mrb_proc_new_cfunc(mrb, proc_test);

mrb_value b = mrb_obj_value(p);

mrb_yield(mrb, b, mrb_nil_value());

The problem seems to the variable mrb_value b. I have no idea why!

Even when I use mrb_p trying to see if I'm getting the expected object value it just crashes! What's going on here?

Valgrind gives me this output in regard to the error:

Invalid read of size 8
0x12DE5A: mrb_yield.cold (vm.c:999)
0x130C45: data_manager_parse (MRB_DataManager.c:54)
0x15A42B: mrb_vm_exec (vm.c:1792)
0x162CF5: mrb_vm_run (vm.c:1282)
0x1630AD: mrb_run (vm.c:3100)
0x1630AD: mrb_funcall_with_block (vm.c:681)
0x1636E9: mrb_funcall_with_block (vm.c:637)
0x163AAD: mrb_funcall_argv (vm.c:692)
0x163AAD: mrb_funcall (vm.c:484)
0x1307C5: RunGame (CORE_Main.c:142)
0x1308FB: CORE_Main (CORE_Main.c:45)
0x12E068: main (Main.c:17)
Address 0x18 is not stack'd, malloc'd or (recently) free'd

Why is it an invalid read?? And what is mrb_yield.cold??

Can I get an example of how to create a block using the C API?

Activity

  1. ghost closed this as completedon Feb 24, 2023
  2. matz commented on Feb 25, 2023

    @matz
    Member

    Since your code snippet is incomplete, I can only guess the situation. Probably, you passed the wrong value to mrb_yield function.

  3. dearblue commented on Mar 14, 2023

    @dearblue
    Contributor

    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_value
    reporter(mrb_state *mrb, mrb_value self)
    {
      mrb_value o = mrb_get_arg1(mrb);
      mrb_p(mrb, o);
      return mrb_nil_value();
    }
    
    int
    main(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_exec
    zsh: segmentation fault (core dumped)  ./a.out 1
  4. added a commit that references this issue on Mar 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions