Commit Graph

18161 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto ad5301970f Merge pull request #6694 from katafrakt/mirb-cosmopolitan 2026-01-08 12:58:09 +09:00
Yukihiro "Matz" Matsumoto 9a48049109 Merge pull request #6696 from khasinski/fix-required-kwarg-parsing 2026-01-08 11:02:18 +09:00
Yukihiro "Matz" Matsumoto 7bc1c44c3b Merge pull request #6695 from jbampton/add-dependabot-cooldown 2026-01-08 08:56:43 +09:00
Chris Hasiński 1e932dd161 Fix parse error with required kwargs and omitted parens
When defining a method with a required keyword argument without
parentheses, mruby incorrectly parsed the next line as the default
value:

    def foo arg:
      123
    end

Was parsed as: def foo(arg: 123); end  (optional kwarg, empty body)
Should be:     def foo(arg:); 123; end (required kwarg, body returns 123)

The fix sets EXPR_ARG lexer state after parsing f_label, making
newlines significant. This prevents the parser from consuming
expressions across line boundaries as default values for keyword
arguments.

Also fixes a pre-existing bug in f_label where tNUMPARAM (type <num>)
was implicitly assigned to $$ (type <id>) without conversion. Now
explicitly uses intern_numparam() to convert numbered parameters to
symbols.

Fixes https://github.com/mruby/mruby/issues/6268
2026-01-08 00:50:55 +01:00
Yukihiro "Matz" Matsumoto 7fe5c2e260 gc.c: rename mrb_alloca() to mrb_temp_alloc() and fix memory leaks
rename mrb_alloca() to mrb_temp_alloc() for clearer naming - the new name
better describes its purpose as GC-managed temporary allocation. keep
mrb_alloca() as a macro alias for backward compatibility.

apply mrb_temp_alloc() to fix potential memory leaks in:
- mruby-strftime: if mrb_str_cat() raises, allocated buffers now cleaned by GC
- mruby-io File.readlink: if mrb_str_new() raises, buffer now cleaned by GC

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-08 08:23:51 +09:00
Yukihiro "Matz" Matsumoto c9e3af60e1 mruby-set: fix memory leak caused by recursive hash computation
when a Set contains itself (directly or indirectly), computing its hash
would cause infinite recursion leading to SystemStackError. the exception
during khash rebuild leaked memory.

add recursion detection flag to Set#hash that returns 0 for recursive
references, similar to Ruby's behavior.

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-08 08:23:43 +09:00
John Bampton 6d5de7b3c1 [CI] Dependabot: add a cooldown period for new releases
Enforces security best practices by requiring a minimum age for new dependency releases before they are automatically updated by Dependabot.

This practice, known as a "cooldown period," helps mitigate supply chain attacks by allowing time for frequently published malicious packages to be identified.

https://docs.github.com/en/code-security/dependabot/working-with-dependabot/dependabot-options-reference#cooldown-
2026-01-08 01:02:15 +10:00
Yukihiro "Matz" Matsumoto 0e42c95df2 mruby-bigint: add exponent size check in mrb_bint_pow
prevent resource exhaustion when computing power with extremely large
exponents (e.g., 81.pow(51742871469327219)). the check estimates the
result size and raises RangeError if it would exceed 1 million bits.

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-06 07:51:07 +09:00
Yukihiro "Matz" Matsumoto bbcadd6bf9 mruby-rational: fix left shift overflow in rational_new_f
Shifting 1 left by MRB_INT_BIT-1 (e.g., 63 on 64-bit) bits into the sign
bit is undefined behavior. Change the overflow check from >= MRB_INT_BIT
to >= MRB_INT_BIT-1 to prevent this.

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-05 17:47:57 +09:00
Yukihiro "Matz" Matsumoto d5c7a906f9 mruby-time: fix integer overflow in time_mktime
When year value is close to MRB_INT_MIN, subtracting TM_YEAR_BASE (1900)
causes signed integer overflow. Add underflow check before the subtraction.

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-05 12:35:09 +09:00
Paweł Świątkowski 1881a904a4 Add Cosmopolitan build to CI 2026-01-05 01:04:21 +01:00
Yukihiro "Matz" Matsumoto 5a1123ed22 mruby-io: remove unused flock function
The local flock() function for Windows is now dead code since the
HAL refactoring. The Windows implementation is in hal-win-io which
provides mrb_hal_io_flock().

Fixes warning: 'flock' defined but not used [-Wunused-function]

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-05 08:55:53 +09:00
Paweł Świątkowski f856cf811f Require sys/socket.h (for Cosmopolitan)
Compilation of mirb with Cosmopolitan fails because of missing include
(Cosmopolitan seems to be more strict than "traditional" compilers.
2026-01-04 19:52:11 +01:00
Yukihiro "Matz" Matsumoto ee06bbb417 vm.c: replace type assertions with runtime checks
Replace mrb_assert with mrb_ensure_*_type for VM opcodes that require
specific types:

- OP_ARYCAT: mrb_ensure_array_type
- OP_ARYPUSH: mrb_ensure_array_type
- OP_ASET: mrb_ensure_array_type (also fixed: was checking wrong register)
- OP_INTERN: mrb_ensure_string_type
- OP_HASHCAT: mrb_ensure_hash_type

These checks catch codegen bugs with clear error messages in both
debug and release builds.

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-04 15:16:03 +09:00
Yukihiro "Matz" Matsumoto 6b482ee3f8 vm.c: add runtime type check for OP_STRCAT
Replace mrb_assert with mrb_ensure_string_type to catch codegen bugs
even in release builds. This prevents null-dereference crashes when
OP_STRCAT receives a non-string first operand due to compiler bugs.

Consistent with OP_HASH which uses mrb_ensure_hash_type for similar
type safety.

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-04 14:09:57 +09:00
Yukihiro "Matz" Matsumoto 2e4a8e8edd mruby-compiler: fix sp tracking in pattern match failure path
After pattern matching code generation, the sp (stack pointer) must
be restored to match the success path value. The failure path (after
RAISEIF) left sp in a different state, causing incorrect register
allocation in subsequent code like string interpolation.

This caused OP_STRCAT to use the wrong register, leading to
null-dereference when trying to modify a non-string value as a string.

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-04 09:12:35 +09:00
Yukihiro "Matz" Matsumoto 099d2c4771 array.c: fix heap-use-after-free in insertion_sort
The key variable in insertion_sort temporarily holds an array element
that's been removed from its slot during the sorting process. When
sort_cmp yields to a block that triggers GC, key wasn't protected
and could be collected.

Use arena save/restore around the loop to avoid arena overflow for
large arrays.

Test case from oss-fuzz: sort! with block containing rescue.

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-03 20:41:47 +09:00
Yukihiro "Matz" Matsumoto af3f9b65f1 mruby-compiler: fix sp imbalance in pattern matching with rescue
The => pattern matching codegen was doing push() after RAISEIF, even though
RAISEIF never returns. This caused sp to be off by 1 when success and failure
paths joined, resulting in wrong register allocation for subsequent operations.

For string interpolation like "#{ expr => pattern rescue body }", the base
string would be at R2 but STRCAT would incorrectly use R3, causing memory
corruption and crashes.

Test case: %{#{.=>.,. rescue def .()end}} (from oss-fuzz)

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-03 20:21:44 +09:00
Yukihiro "Matz" Matsumoto a9825e92df mruby-rational: fix crash in rational_new_f with negative exponent
rational_new_b() expects both arguments to be bigints, but rational_new_f()
was passing an integer value for the numerator when the exponent was negative.
This caused a segfault in mrb_bint_reduce() which called RBIGINT() on the
integer value.

Test case: 5r**-92 (from oss-fuzz)

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-03 13:13:02 +09:00
Yukihiro "Matz" Matsumoto 53a25bab14 mruby-sleep, hal-posix-socket: fix amalgamation compatibility
mruby-sleep: declare slp_tm before #ifdef _WIN32 block to fix
undeclared variable error in non-Windows branch.

hal-posix-socket: use #if defined(HAVE_SA_LEN) && HAVE_SA_LEN instead
of #ifdef HAVE_SA_LEN, since mruby-socket defines HAVE_SA_LEN to 0 on
non-BSD platforms.

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-02 18:28:31 +09:00
Yukihiro "Matz" Matsumoto 64f1436323 amalgamation.md: add mruby-rational/complex, clarify gem defines
Co-authored-by: Claude <noreply@anthropic.com>
2026-01-02 18:04:39 +09:00
Yukihiro "Matz" Matsumoto f78ac530c8 amalgam.rb: support gems with core-affecting defines
Gems like mruby-task add preprocessor defines (MRB_USE_TASK_SCHEDULER)
that affect mrb_state structure. The amalgamation generator now detects
these defines from the build configuration and adds them at the top of
mruby.h before struct definitions are encountered.

Supported define patterns: MRB_USE_*, MRB_UTF8_*, HAVE_MRUBY_*

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-02 17:35:31 +09:00
Yukihiro "Matz" Matsumoto 07cd188264 README.md: update document index
Co-authored-by: Claude <noreply@anthropic.com>
2026-01-02 17:02:12 +09:00
Yukihiro "Matz" Matsumoto 29113b490e doc: add amalgamation guide
Co-authored-by: Claude <noreply@anthropic.com>
2026-01-02 17:01:53 +09:00
Yukihiro "Matz" Matsumoto 835561c9e4 README.md: add amalgamation section
Co-authored-by: Claude <noreply@anthropic.com>
2026-01-02 17:00:02 +09:00
Yukihiro "Matz" Matsumoto 037a9b3c6d TODO.md: remove amalgamation (implemented)
Co-authored-by: Claude <noreply@anthropic.com>
2026-01-02 16:58:39 +09:00
Yukihiro "Matz" Matsumoto d995ca2910 build: add amalgamation support via rake amalgam task
Add ability to generate combined mruby.h and mruby.c files for
single-file embedding, similar to SQLite's amalgamation.

Usage: rake amalgam
Output: build/<target>/amalgam/mruby.{h,c}

Features:
- Headers concatenated in dependency order with guards stripped
- Sources concatenated with proper ordering (core, gems, mrblib)
- X-macro headers (ops.h) inlined at each include point
- Local includes automatically inlined
- Handles both src/ and core/ gem directory conventions

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-02 08:38:51 +09:00
Yukihiro "Matz" Matsumoto 94e831cc70 init.c, mruby-io: undef DONE macro
Add #undef DONE after last usage to prevent macro redefinition
warnings in amalgamation builds.

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-01 13:18:33 +09:00
Yukihiro "Matz" Matsumoto a263c43adb vm.c, codedump.c: undef CASE macro
Add #undef CASE at end of files to prevent macro redefinition
warnings in amalgamation builds.

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-01 12:35:09 +09:00
Yukihiro "Matz" Matsumoto d1178ec8eb hash.c, symbol.c, string.c, mruby-string-ext: undef lesser macro
Add #undef lesser after last usage to prevent macro redefinition
warnings when files are amalgamated into a single translation unit.

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-01 10:23:21 +09:00
Yukihiro "Matz" Matsumoto c7463af767 mruby-io, hal-posix-io: fix amalgamation compatibility
Remove unused mrb_stat typedef from file.c that conflicted with the
mrb_stat() function in file_test.c when compiled as a single
translation unit.

Fix convert_stat() in hal-posix-io to handle st_atime macro correctly
in both normal and amalgamated builds by extracting time values before
undefining the macros.

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-01 09:01:15 +09:00
Yukihiro "Matz" Matsumoto 510ebd738d mruby-compiler: terminate parsing early after too many errors
When parsing malformed input with many syntax errors (e.g., via eval
with a long garbage string), the parser would continue until the end
of input, causing long execution times.

Add an early termination check in the lexer that returns EOF once
the error count exceeds 10 (same as error_buffer size). This prevents
DoS from inputs like eval("garbage" * 1000).

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-31 09:11:44 +09:00
Yukihiro "Matz" Matsumoto 53ee1a7826 bigint.c: fix memory leak in mpz_div_2exp
mpz_div_2exp() was calling mpz_init_heap() on output parameter z
without first freeing z's existing memory. When called from
mpz_barrett_reduce() with pre-allocated temporaries, this caused
memory leaks.

Add mpz_clear(ctx, z) before mpz_init_heap() in both affected code
paths, matching the pattern already used in mpz_mod_2exp().

Fixes ClusterFuzz issue detected with input "8.pow 7*2515881+186,8 ^4>>-509".

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-30 18:20:41 +09:00
Yukihiro "Matz" Matsumoto dcd4fe9fb0 range.rb: fix Range#hash for endless/beginless ranges
Use self.begin/self.end instead of first/last to compute hash for
ranges. The first/last methods raise RangeError for endless/beginless
ranges, but the internal begin/end accessors return nil safely.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-30 17:43:59 +09:00
Yukihiro "Matz" Matsumoto 8f259fb560 mruby-bigint, mruby-numeric-ext: fix Integer#pow with negative modulus
Support negative modulus in Integer#pow(exp, mod) with proper Ruby
semantics. Previously, negative modulus caused an infinite loop in
Barrett reduction. Now:

- Use absolute value of modulus for computation
- Apply signed modulo adjustment (result + m for non-zero result
  when m is negative)
- Add early return for zero base with positive exponent (0^n = 0)

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-30 16:25:49 +09:00
Yukihiro "Matz" Matsumoto 54fbf6c3ec bigint.c: fix buffer overflow in mpz_div_2exp with large shift
When right-shifting by more bits than the number contains, the loop
condition `i < x->sz - digs` would underflow (since size_t is unsigned),
causing out-of-bounds memory access.

Fixed by checking if digs >= x->sz upfront and returning zero in that
case, since shifting right by more bits than the number has always
yields zero.

Discovered via ClusterFuzz with input "7<<78<<-772".

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-30 14:46:26 +09:00
Yukihiro "Matz" Matsumoto 5eca2fae1e rational.c: fix undefined behavior from large shift exponents
In rational_new_f(), the code performed ((mrb_int)1)<<exp without
checking if exp >= MRB_INT_BIT. Shifting by a value >= bit width
is undefined behavior in C.

Also fixed the negative exponent case which incorrectly used
deno >>= exp (right-shift by negative is UB). The correct logic
is deno <<= -exp to multiply denominator by 2^(-exp).

Both cases now check for overflow before shifting and fall back
to bigint operations when necessary.

Discovered via ClusterFuzz with input "92r**11".

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-30 14:28:44 +09:00
Yukihiro "Matz" Matsumoto ff0e20453f bigint.c: fix GCD infinite loop and size handling bugs
Fixed three bugs that caused infinite loops in GCD calculations:

1. mpz_set_int() didn't shrink sz when setting a smaller value.
   mpz_realloc() only grows allocations, so setting a 1-limb value
   to an mpz_t with sz=3 would leave sz=3, breaking algorithms
   that depend on correct sz values.

2. mpz_set_uint64() had the same issue.

3. mpz_gcd() used mpz_init_set() which preserves the sign.
   GCD should work with absolute values since gcd(a,b) = gcd(|a|,|b|).
   With negative inputs, the sign would oscillate during mod operations,
   preventing the Euclidean algorithm from converging.

4. mpz_div_2exp() when e==0 and z==x would corrupt data by calling
   mpz_init_heap() which overwrites z->p before copying from x.

These bugs were discovered via ClusterFuzz with complex rational
number calculations.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-30 12:04:56 +09:00
Yukihiro "Matz" Matsumoto 27c9037d48 bigint.c: fix FPE caused by inconsistent zero sign state
Fix two functions that could create bigints with sn != 0 but value of 0:

- mpz_mod_limb: single-limb case set r->sn = x->sn even when result was 0
- mpz_mul_2exp: set z->sn = sn unconditionally after zero-producing ops

This inconsistent state caused GCD loop (!zero_p(&b)) to continue with
a zero divisor, eventually causing FPE in mpz_mod_limb with m = 0.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-29 23:39:30 +09:00
Yukihiro "Matz" Matsumoto 9f0950da13 codegen.c: fix stack tracking in pattern match branching code
When generating code for pattern matching with potential failures, the
success and failure paths both need to pop the matched value. At
runtime, only one path executes. But during codegen, both pop() calls
affected the compile-time stack pointer (cursp), corrupting register
allocation and causing heap-buffer-overflow when accessing symbol
tables with wrong indices.

Fix by saving/restoring the stack pointer around the branch point, so
each path correctly tracks the stack state independently.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-29 22:28:45 +09:00
Yukihiro "Matz" Matsumoto da2d652ec9 codegen.c: fix NODE_MATCH_PAT to push result when val is true
Pattern matching expressions were not pushing a result value in several
code paths when used in value context (e.g., string interpolation).
This caused crashes when the result was expected on the stack.

Fix all code paths in NODE_MATCH_PAT to push the appropriate value:
- 'in' pattern returns true/false
- '=>' pattern returns nil (matches CRuby behavior)

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-29 19:09:06 +09:00
Yukihiro "Matz" Matsumoto f6f8124406 codegen.c: fix crash in parallel assignment optimization
The direct literal generation optimization for parallel assignment was
using the RHS count as the loop bound but only filling registers for
LHS variables. When RHS has more elements than LHS (e.g., `a,=1,2`),
this caused uninitialized register indices to be used, generating
garbage opcodes that crashed the VM.

Fix by counting LHS variables and only applying the optimization when
LHS and RHS counts match exactly.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-29 18:19:58 +09:00
Yukihiro "Matz" Matsumoto 225cdaa16a mruby-compiler: eliminate bison shift-reduce conflict for 'in'
Add precedence declarations to resolve the ambiguity between:
- One-line pattern match: `arg in pattern`
- Case/in clause: `case expr; in pattern; end`

When seeing `arg in`, the parser should shift to parse `arg in pattern`
as a complete expression (matching CRuby behavior), not reduce `arg`
to start a case clause.

Changes:
- Add keyword_in to %nonassoc precedence declarations
- Add %prec tLOWEST to the plain `arg` reduction rule

This eliminates all bison shift-reduce conflicts (was 2, now 0).

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-29 17:56:54 +09:00
Yukihiro "Matz" Matsumoto a91ffcfe0d mruby-compiler: fix as-pattern parsing with symbol values
Remove non-standard `symbol tASSOC p_as` rule from hash pattern
elements. This rule conflicted with the as-pattern rule and caused
`:foo => x` to be incorrectly parsed as a hash pattern instead of
an as-pattern.

CRuby only supports label syntax (foo:) for hash pattern keys,
not hashrocket syntax (:foo =>). This change aligns mruby with
CRuby behavior and reduces bison shift-reduce conflicts from 2 to 1.

Before: `case :foo; in :foo => x; end` raised NoMethodError
After:  `case :foo; in :foo => x; end` binds x to :foo

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-29 13:42:19 +09:00
Yukihiro "Matz" Matsumoto 4fc81e8ea0 mruby-compiler: fix crash in pattern matching with string literal
The p_value grammar rule passed raw tSTRING token (a (len . str) cons
cell) directly to new_pat_value() without wrapping it as a proper AST
node. When codegen processed this malformed node, it read the length
field as the node type, causing misinterpretation and crash.

Wrap tSTRING with new_str(p, list1($1)) to create a proper NODE_STR,
consistent with how the primary:string rule handles strings.

Found by ClusterFuzz (oss-fuzz/mruby_fuzzer).

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-29 13:34:13 +09:00
Yukihiro "Matz" Matsumoto e19b107642 mruby-random: fix FPE in rand_i() for large ranges
When rand is called with a range exceeding UINT32_MAX (e.g.,
rand(2..4294967297)), the span value could overflow when cast
to uint32_t, causing division by zero in the modulo operation.

Add 64-bit path for MRB_INT64 builds that combines two 32-bit
randoms when the range exceeds 32 bits.

Found by ClusterFuzz (oss-fuzz/mruby_fuzzer).

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-29 12:54:32 +09:00
Yukihiro "Matz" Matsumoto b8f05f0752 benchmark: optimize bm_so_mandelbrot.rb
Replace for..in loops with while loops to avoid closure overhead.
Cache constants in local variables to avoid repeated lookups.
59% faster (7.4s -> 3.0s).

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-29 12:28:02 +09:00
Yukihiro "Matz" Matsumoto 8a5283cf4e benchmark: terminal version of mandelbrot
Optimized to avoid Math.sqrt by squaring the threshold
(sqrt(x) < 1000 => x < 1000000) and caching zr*zr/zi*zi
to avoid redundant computation. 29% faster than naive version.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-29 11:56:58 +09:00
Yukihiro "Matz" Matsumoto 624272b15d mruby-bin-mirb: add syntax highlighting for keywords and strings
Add syntax highlighting to mirb's multi-line editor with support for:
- keywords (def, if, class, end, etc.) in magenta
- strings ("...", '...', %q{...}) in green
- comments (#...) in gray
- numbers (42, 3.14, 0xff) in cyan
- symbols (:foo) in yellow
- constants (Array, Foo) in bold yellow
- instance variables (@var) in blue
- global variables ($var) in bold blue

Features:
- auto-detects light/dark theme via COLORFGBG env var
- MIRB_THEME=light/dark for explicit override
- method calls like obj.class correctly not highlighted as keywords
- enabled automatically when terminal supports color

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-29 11:56:58 +09:00
Yukihiro "Matz" Matsumoto a866a5b0e6 mruby-sprintf: improve initial buffer size estimation
Estimate initial buffer size based on format string to reduce
reallocations. The new formula uses format string length plus
120 bytes base, plus 24 bytes per format specifier, capped at 4096.

This reduces reallocations by ~60% in typical use cases and
improves performance by 2-21% depending on output size.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-29 11:56:58 +09:00