Skip to content

Add alias support for mruby-method and mruby-proc-ext#6879

Merged
matz merged 2 commits into
mruby:masterfrom
Asmod4n:method_alias
Jun 4, 2026
Merged

Add alias support for mruby-method and mruby-proc-ext#6879
matz merged 2 commits into
mruby:masterfrom
Asmod4n:method_alias

Conversation

@Asmod4n

@Asmod4n Asmod4n commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Ive hit a Segfault when using mruby-method with an aliased C mruby function definition.
This fixed it, i also added some tests to verify the fix works.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes potential segmentation faults and misaligned reads by resolving alias procs to their underlying original procs before accessing their properties in mrb_proc_parameters and mrb_proc_arity. It also adds comprehensive test coverage for aliased and C-defined methods. The review feedback correctly identifies that several other functions, such as proc_inspect, mrb_proc_local_variables, and mrb_proc_copy, still access body.irep directly without resolving aliases, which could lead to similar crashes or memory corruption. It is highly recommended to apply the same alias resolution guards to these functions.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +192 to +195
while (MRB_PROC_ALIAS_P(proc)) {
proc = proc->upper;
if (!proc) return mrb_ary_new(mrb);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

While resolving aliases in mrb_proc_parameters successfully prevents a SEGV here, proc_inspect (lines 103–132) in the same file suffers from the exact same vulnerability. In proc_inspect, if !MRB_PROC_CFUNC_P(p) is true, it directly accesses p->body.irep and passes it to mrb_debug_get_position. For an alias proc, body.irep is actually body.mid (a symbol), which will cause a misaligned read or SEGV when dereferenced.

Please consider resolving aliases in proc_inspect as well, for example:

  while (p && MRB_PROC_ALIAS_P(p)) {
    p = p->upper;
  }

Comment thread src/proc.c
Comment on lines +426 to +429
while (p && MRB_PROC_ALIAS_P(p)) {
p = p->upper;
}
if (!p) return 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Resolving aliases in mrb_proc_arity is a great fix. However, there are other functions in src/proc.c that access proc->body.irep directly without resolving aliases:

  1. mrb_proc_local_variables (lines 469–498) accesses proc->body.irep directly in its loop. If an alias proc is passed, it will treat body.mid (a symbol) as an mrb_irep* and dereference irep->lv, leading to a SEGV.
  2. mrb_proc_copy (lines 276–290) accesses b->body.irep and calls mrb_irep_incref on it if !MRB_PROC_CFUNC_P(b) is true. For an alias proc, this will corrupt memory by incrementing a reference count on a symbol value.

Please consider adding alias resolution guards to mrb_proc_local_variables and mrb_proc_copy to prevent these potential crashes and memory corruptions.

@Asmod4n

Asmod4n commented Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

@matz didn't see the issues in the other functions gemini flags.

Wouldn't a global helper method be better here for accessing this which does the alias resolution itself?
That would be out of scope for this PR i guess since it would touch much more code.

@matz matz merged commit 24f428a into mruby:master Jun 4, 2026
19 checks passed
@matz

matz commented Jun 4, 2026

Copy link
Copy Markdown
Member

Thanks, this is a clean fix and the tests are thorough. Confirmed the segfault on master and that this resolves it.

On the helper idea: yes, the while (MRB_PROC_ALIAS_P(p)) p = p->upper idiom now repeats across several places (mrb_proc_eql, mrb_proc_source_location, and now arity/parameters), so factoring it into a small internal mrb_proc_resolve_alias (or similar) makes sense. I agree it is out of scope for this PR. I will merge this as-is; a follow-up that introduces the helper and converts the existing sites would be welcome.

@Asmod4n Asmod4n deleted the method_alias branch June 5, 2026 05:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants