Commit Graph

17981 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto c136a476ee Merge pull request #6677 from jbampton/pre-commit-color-always 2025-12-06 21:56:26 +09:00
John Bampton 58264bedee gha: run pre-commit with --color=always
Minor enhancement for the GHA CI.

refs https://github.com/apache/cloudstack/pull/11977
2025-12-05 23:21:58 +10:00
Yukihiro "Matz" Matsumoto 4dc234c7a2 AUTHORS: update entries [ci skip] 2025-12-02 23:25:09 +09:00
Yukihiro "Matz" Matsumoto 127ef7f406 TODO.md: update profiler/benchmarking entry
benchmarking tools (mruby-benchmark) have been implemented.
update entry to focus on remaining profiler features:
method call tracing, stack profiling, and detailed memory analysis.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-02 23:12:37 +09:00
Yukihiro "Matz" Matsumoto 2336534552 mruby-benchmark: add comprehensive test suite
add 24 test cases covering all benchmark functionality:
- Benchmark.measure and Benchmark.realtime
- Benchmark::Tms class and its methods (total, to_s, format)
- Benchmark.bm for formatted comparison reports
- Benchmark::Report class
- memory tracking with ObjectSpace integration
- consistency and realistic usage scenarios

suppress output during tests by temporarily setting $stdout to nil
for tests that call Benchmark.bm or Report#report to avoid printing
garbage during test execution.

all tests pass successfully.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-02 23:12:37 +09:00
Yukihiro "Matz" Matsumoto ef3a37b9b7 limitation.md: document current pattern matching limitations; close #6674
add pattern matching section to limitations.md clarifying that only
rightward assignment (expr => var) is currently supported, while
case/in syntax and other pattern types are not yet implemented.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-02 23:12:37 +09:00
Yukihiro "Matz" Matsumoto 2494a712dd vm.c: extract alias resolution logic to MRB_PROC_RESOLVE_ALIAS macro
reduce code duplication by introducing MRB_PROC_RESOLVE_ALIAS macro
to handle alias proc resolution in a consistent way across 5 locations.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-02 23:12:36 +09:00
Yukihiro "Matz" Matsumoto 13e2ce4249 mruby-benchmark: fix implementation for mruby environment
fix implementation to work correctly in mruby:
- use String#% instead of sprintf for formatting
- use $stdout directly for output instead of bare print/puts
- add nil check for $stdout to handle test environments
- use Object.const_defined? instead of defined? keyword
- create new Tms instance with label instead of instance_variable_set
- add dependencies: mruby-sprintf and mruby-io

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-02 23:12:36 +09:00
Yukihiro "Matz" Matsumoto 2f40f3d170 mruby-benchmark: add benchmarking gem
add initial implementation of mruby-benchmark gem providing:
- Benchmark.measure for detailed timing with Tms results
- Benchmark.realtime for simple wall-clock measurements
- Benchmark.bm for formatted comparison reports
- optional memory tracking via ObjectSpace integration

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-02 23:12:36 +09:00
Yukihiro "Matz" Matsumoto ea221a5562 TODO.md: add performance, tooling, and embedded features
add future work items:
- method inline caching improvements
- built-in profiler/benchmarking tools
- improved REPL (mirb) features
- configurable memory pools
- suspend/resume VM state
- CMake build support

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-02 23:12:36 +09:00
Yukihiro "Matz" Matsumoto ee3b7e0faf TODO.md: add pattern matching to future work
add full pattern matching implementation (case/in syntax, array/hash
patterns, guards, etc.) to the todo list for after mruby 3.4.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-02 23:12:36 +09:00
Yukihiro "Matz" Matsumoto 1fcf93e18e TODO.md: update version to 3.4
update "After mruby 3.1" to "After mruby 3.4" to reflect the current
version.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-02 23:12:36 +09:00
Yukihiro "Matz" Matsumoto e144962590 TODO.md: remove completed variable sized AST node item
variable sized AST node has been implemented, so remove it from the
todo list.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-02 23:12:36 +09:00
Yukihiro "Matz" Matsumoto 5af58bd7cc Merge pull request #6676 from mruby/dependabot/github_actions/super-linter/super-linter-8.3.0 2025-12-02 23:12:18 +09:00
dependabot[bot] b7b61fdbe5 build(deps): bump super-linter/super-linter from 8.2.1 to 8.3.0
Bumps [super-linter/super-linter](https://github.com/super-linter/super-linter) from 8.2.1 to 8.3.0.
- [Release notes](https://github.com/super-linter/super-linter/releases)
- [Changelog](https://github.com/super-linter/super-linter/blob/main/CHANGELOG.md)
- [Commits](https://github.com/super-linter/super-linter/compare/v8.2.1...v8.3.0)

---
updated-dependencies:
- dependency-name: super-linter/super-linter
  dependency-version: 8.3.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-12-02 14:01:29 +00:00
Yukihiro "Matz" Matsumoto 40644ad96c Merge pull request #6589 from jbampton/add-no-zip-hook 2025-12-02 08:34:39 +09:00
Yukihiro "Matz" Matsumoto ffd3252dd0 proc.c: fix Method#== for aliased methods and comparison bug; fix #6668
follow alias chains in mrb_proc_eql() to compare underlying procs,
making Method#== return true for aliased methods as in CRuby.

also fix typo where p1 was checked instead of p2 in CFUNC comparison.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-01 18:20:45 +09:00
Yukihiro "Matz" Matsumoto 29b3405283 bigint.c: handle in-place operation in mpz_mod_2exp()
when mpz_mod_2exp() is called with z == x (in-place operation), the
function was calling mpz_clear(ctx, z) which freed x's memory, then
attempting to access x->p[i] - reading freed memory. this caused
Barrett reduction to produce incorrect results in modular
exponentiation.

the fix checks if z == x and handles in-place modification by
adjusting the size and masking directly, without clearing. this is
similar to the memory leak fix for pool→heap transitions.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-01 18:20:44 +09:00
Yukihiro "Matz" Matsumoto a5e25393b8 mruby-bigint: fix memory leak in mpz_mod_2exp by clearing before reinit
mpz_mod_2exp() was reinitializing its output parameter without clearing
existing heap memory. When the parameter contained heap allocations from
pool->heap transitions in mpz_mul()->mpz_realloc(), reinitializing would
overwrite the pointer and leak memory. Added mpz_clear() before each
mpz_init() or mpz_init_heap() call to properly free existing heap memory.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-01 18:20:44 +09:00
Yukihiro "Matz" Matsumoto 5ed0cd4ecb bigint.c: increase karatsuba scratch allocation safety margin
the previous fixed safety margin of 8 limbs was insufficient for certain
edge cases involving deep recursion levels in karatsuba multiplication,
as discovered by oss-fuzz. changed to proportional margin (~12.5% plus
fixed overhead of 16) that scales with input size.

this prevents potential buffer overruns in deeply nested karatsuba
multiplications while maintaining efficiency for typical cases.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-01 18:20:44 +09:00
Yukihiro "Matz" Matsumoto 857de45036 mruby-time: normalize microseconds before converting to nanoseconds
when converting microseconds to nanoseconds, multiplying very large
usec values by 1000 can cause signed integer overflow. for example,
Time.at(0, 9999999999990768) would trigger ASAN runtime error.

fixed by normalizing microseconds >= 1000000 (or <= -1000000) to
seconds before the multiplication, preventing overflow while maintaining
correct time representation. this normalization converts excess
microseconds to seconds, leaving only the fractional part for
multiplication.

applied fix to both time_alloc() and mrb_time_at() functions.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-01 18:20:44 +09:00
Yukihiro "Matz" Matsumoto 78f9b5c52f string.c: simplify memcmp guard with ternary operator
refactored the NULL pointer guard in mrb_str_cmp() from an if-else
block to a more concise ternary operator. functionality remains the
same: avoids undefined behavior by skipping memcmp() when comparing
zero-length strings.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-01 18:20:43 +09:00
Yukihiro "Matz" Matsumoto 6122fdfee9 string.c: guard memcmp() call to avoid undefined behavior with NULL pointers
passing NULL pointers to memcmp() is undefined behavior per C standard,
even when size is 0. memcmp() is declared with nonnull attributes,
and ASAN can detect this violation.

in mrb_str_cmp(), when comparing two empty strings or when the minimum
length is 0, we now skip the memcmp() call and directly set retval to 0.
this avoids the undefined behavior while maintaining correct comparison
semantics.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-01 18:20:43 +09:00
Yukihiro "Matz" Matsumoto db3d754261 mruby-array-ext: use mrb_ensure() instead of direct throw.h calls
refactored five functions to use mrb_ensure() instead of MRB_TRY/MRB_CATCH:
- ary_subtract_internal(): body/ensure pattern for set cleanup
- ary_union_internal(): body/ensure pattern for set cleanup
- ary_intersection_internal(): body/ensure pattern for set cleanup
- ary_intersect_p(): body/ensure pattern for set cleanup
- ary_uniq_bang(): body/ensure pattern for set cleanup

each function now uses a context struct containing set pointer and other
necessary data, with separate body and ensure functions that guarantee
cleanup on exception. this allows array-ext to compile as pure C without
requiring C++ compiler when enable_cxx_exception is set.

added mruby-error dependency to access mrb_ensure(). changed include
from throw.h to error.h. fix #6667.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-01 18:20:42 +09:00
Yukihiro "Matz" Matsumoto 61437126cd Merge pull request #6673 from jbampton/fix-links-clean-up 2025-11-29 12:40:15 +09:00
John Bampton 8558ad40ec Adjust broken license links; clean up Markdown 2025-11-29 01:46:54 +10:00
John Bampton 8623215c69 Add pre-commit hook check-zip-file-is-not-committed
Zip files should not be allowed in the repository as they are hard to track and have security implications.
2025-11-28 23:58:42 +10:00
Yukihiro "Matz" Matsumoto 209ae86e77 Merge pull request #6670 from dearblue/vm 2025-11-28 21:50:55 +09:00
Yukihiro "Matz" Matsumoto 271428002a Merge pull request #6599 from jbampton/manual-hooks 2025-11-28 14:28:11 +09:00
John Bampton 88bc3c130f Merge branch 'master' into manual-hooks 2025-11-28 05:43:37 +10:00
Yukihiro "Matz" Matsumoto 9e95646f76 Merge pull request #6669 from mruby/dependabot/github_actions/actions/checkout-6 2025-11-24 23:56:13 +09:00
dearblue 50ecc1fd2c Arranging VM dispatch macros
Consolidate duplicate common code.
2025-11-23 14:45:22 +09:00
dependabot[bot] f05a284cab build(deps): bump actions/checkout from 5 to 6
Bumps [actions/checkout](https://github.com/actions/checkout) from 5 to 6.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/v5...v6)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-11-21 14:01:22 +00:00
Yukihiro "Matz" Matsumoto 82180d040b codegen.c: fix regexp literal with encoding to pass nil for options; fix #6666
restore correct argument passing for Regexp.compile when encoding is
present but flags are not. regexp literals like /a/n should compile to
Regexp.compile("a", nil, "n") with 3 arguments, not
Regexp.compile("a", "n") with 2 arguments.

the bug was introduced during refactoring when the nil-insertion logic
for the options parameter was accidentally omitted. now properly inserts
OP_LOADNIL when flags are absent but encoding is present.

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-20 14:08:21 +09:00
Yukihiro "Matz" Matsumoto 2da01c607f mruby-pack: avoid integer overflow in pack_hex buffer calculation
rewrite ceiling division to avoid signed overflow. the expression
(count + 1) / 2 triggers undefined behavior when count == INT_MAX.
use count / 2 + (count & 1) instead, which computes the same result
without intermediate overflow.

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-19 15:34:32 +09:00
Yukihiro "Matz" Matsumoto 3f2611ebcd bigint.c: fix buffer overflow in uadd with mismatched operand sizes
fix out-of-bounds read when adding bigints of different sizes. the
unrolled loop accessed both operands up to the size of x without
checking if y had enough limbs. when y->sz < x->sz, this caused reads
beyond y's allocation. now use min(x->sz, y->sz) for the overlap
region and handle remaining limbs from the larger operand separately.

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-19 08:54:30 +09:00
Yukihiro "Matz" Matsumoto 2993302b8a mruby-pack: fix buffer overflow in pack_uu encoding
fix buffer size calculation for UU-encoding to account for per-line
padding. each line encodes separately, causing additional padding when
line length is not divisible by 3. the previous calculation treated
all input as one block, underestimating the required buffer size when
using small count values.

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-18 21:50:34 +09:00
Yukihiro "Matz" Matsumoto 281b38c0b6 bigint.c: add missing mrb_as_bint calls in mod and rem
mrb_bint_mod() and mrb_bint_rem() were missing conversion of the first
operand x to bigint before calling bint_as_mpz(). this caused crashes
when x was not already a bigint. added mrb_as_bint(mrb, x) calls to
ensure both operands are properly converted.

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-18 10:14:13 +09:00
Yukihiro "Matz" Matsumoto 8113c0d24a bigint.c: fix division by zero in udiv normalization
after left-shifting the divisor in udiv(), trailing zero limbs could
remain, causing division by zero. added trim(&y) after ulshift() to
remove zero limbs, and safety check to handle edge cases where divisor
becomes zero after normalization.

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-18 09:33:24 +09:00
Yukihiro "Matz" Matsumoto a6b55e741e mruby-set: fix use-after-free by adding write barriers
young objects stored in old Set instances were being freed during GC
because write barriers were missing. added mrb_field_write_barrier_value()
calls after all kset_put() operations. introduced kset_to_rset() macro
using container-of pattern to obtain RSet pointer from embedded kset_t
without adding function parameters.

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-17 20:08:57 +09:00
Yukihiro "Matz" Matsumoto 431d4bb51d mruby-array-ext: add type check in __product_group to prevent crash
the internal method __product_group assumes all elements in the arys
argument are Arrays, but when called directly (e.g., via send or fuzzing),
non-array values can cause segfault. add type check before accessing with
RARRAY_LEN to convert crash to proper TypeError.

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-17 08:50:40 +09:00
Yukihiro "Matz" Matsumoto d570ef257c CONTRIBUTING.md: add security reporting guidance with link to SECURITY.md
added a new "Security Issues" section that summarizes the security reporting
process: email for RCE vulnerabilities, issue tracker for VM crashes. links
to SECURITY.md for complete details on what qualifies as a security issue.

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-17 08:39:11 +09:00
Yukihiro "Matz" Matsumoto 62ac7e4c43 SECURITY.md: clarify security scope with three-tier priority system
restructured the security policy to reduce misunderstandings:
- high priority: remote code execution (RCE) vulnerabilities only
- lower priority: VM crashes from valid Ruby code (accepted but preferably
  reported as bugs on issue tracker)
- out of scope: resource exhaustion, malformed bytecode, C API misuse,
  theoretical undefined behavior, allocation warnings

added detailed rationale and examples for each category, explaining mruby's
role as an embeddable interpreter and the host application's responsibility
for sandboxing and resource management.

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-17 08:17:55 +09:00
Yukihiro "Matz" Matsumoto 1c7a0d4e96 vm.c: add type check before calling mrb_hash_size() on keyword dict
the keyword argument handling code was checking if kdict is not nil
before calling mrb_hash_size(), but didn't verify it's actually a hash.
malformed bytecode could cause a non-hash value to be stored in the
keyword dictionary register, leading to a NULL pointer dereference in
h_size(). add mrb_hash_p() check to prevent the crash.

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-16 19:38:33 +09:00
Yukihiro "Matz" Matsumoto 40b0cb98f7 mruby.h: add MRB_OPEN_FAILURE() macro and refactor MRB_OPEN_SUCCESS()
since all current uses check for failure (!MRB_OPEN_SUCCESS), add
MRB_OPEN_FAILURE() as the primary macro for better readability. define
MRB_OPEN_SUCCESS() in terms of MRB_OPEN_FAILURE() to avoid duplication
and optimize the common case. update all usage sites to use the clearer
MRB_OPEN_FAILURE() form.

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-16 06:53:02 +09:00
Yukihiro "Matz" Matsumoto 74c0769319 mruby-bigint: fix memory leak in mpz_mod() when reusing initialized mpz_t
mpz_mod() was calling mpz_init_heap() on its output parameter, assuming it
was uninitialized. However, callers like mpz_powm_i() pass already-
initialized variables, causing the old allocations to leak. Changed to use
mpz_realloc() which properly handles both cases.

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-15 22:40:29 +09:00
Yukihiro "Matz" Matsumoto 9133124bef mruby-bigint: add safety margin to karatsuba scratch allocation
prevents buffer overrun in karatsuba multiplication scratch space due to
rounding errors in recursive partitioning. empirically determined 8-limb
margin fixes valgrind-detected overrun with large exponentiations.

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-15 22:40:21 +09:00
Yukihiro "Matz" Matsumoto 8e50a45f3e mrb_print_error: handle NULL gracefully to simplify error checking
made mrb_print_error() handle NULL by printing "Failed to allocate
mrb_state" when mrb is NULL. since mrb_close() already handles NULL,
this allows simplified error checking pattern:

  if (!MRB_OPEN_SUCCESS(mrb)) {
    mrb_print_error(mrb);  // handles NULL
    mrb_close(mrb);        // handles NULL
    return EXIT_FAILURE;
  }

updated all binary tools (mruby, mirb, mrdb, mrbtest) to use this
simplified pattern, removing nested if checks.

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-14 00:49:55 +09:00
Yukihiro "Matz" Matsumoto 05ffe0c441 mrb_open: return mrb_state with exc set on init failure
changed mrb_open() and mrb_open_core() to return mrb_state with mrb->exc
set (instead of NULL) when initialization fails. this allows callers to
programmatically inspect error details, which is essential for embedded
systems without stderr. return NULL only for true allocation failure.

added MRB_OPEN_SUCCESS(mrb) macro to check initialization success, since
mrb != NULL no longer guarantees success. updated all binary tools
(mruby, mirb, mrdb, mrbtest) to use new pattern: check MRB_OPEN_SUCCESS,
print exception details via mrb_print_error if available, then mrb_close.

mrb_core_init_protect now preserves exception in mrb->exc instead of
printing and clearing it, giving caller control over error handling.

breaking change: callers must use MRB_OPEN_SUCCESS(mrb) or check both
mrb != NULL && mrb->exc == NULL. old NULL-only checks will miss
initialization failures.

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-13 19:10:46 +09:00
Yukihiro "Matz" Matsumoto 92640097ab variable.c: skip const_added hook during bootstrapping; fix #6613
dd96afd added const_added hook call to mrb_const_set(), but calling
mrb_funcall_argv() during core initialization (before bootstrapping
completes) fails on bare metal platforms where VM is not fully ready.
skip hook during mrb->bootstrapping phase, matching pattern used in
class.c for method cache clearing.

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-13 16:46:50 +09:00