Commit Graph

118 Commits

Author SHA1 Message Date
dearblue 3a9ef9d27e Avoid using the deprecated function mrb_data_check_and_get()
The "d" directive in `mrb_get_args()` can be used as an alternative.
Furthermore, NULL checking is unnecessary for the following reasons:
  - Incomplete objects from `ary_combination_init()` are not passed to the caller and are garbage collected when `ObjectSpace.each_object` is called, so they are never retrieved
  - Even if `state.clone` is called, the `RData::type` of the cloned object is set to NULL, so it is rejected by `mrb_get_args()`
2026-04-08 21:10:45 +09:00
dearblue 2135088ada Avoid the impact of object modifications caused by calls to mrb_vm_exec()
Several methods defined in mruby-array-ext are written in C and may call `mrb_vm_exec()`.
If array objects are modified on the Ruby side, problems may arise in subsequent processing.

  - Using objects that have been removed from the array and garbage collected
  - Using pointers or array lengths that have become invalid due to changes to the array object
  - Modifying the contents of a shared array object directly

ref: https://github.com/mruby/mruby/issues/6662
2026-03-22 23:05:37 +09:00
dearblue 98d763603c Further optimize Array#product
Replace `__product_group` method with `__product_generate` and `__product_next`.
This change eliminates the need for Ruby to perform internal state calculations, allowing it to simply receive the results.
2026-03-20 21:13:52 +09:00
dearblue 8441eaf633 Fixed "Out-of-bounds Read" and "Divide-by-Zero" in ary_product_group()
Reproduction:

  - Out-of-bounds Read

    ```console
    % build/host/bin/mruby -e '([nil] * 256).__product_group([[nil] * 256], 1 << 32, 256)'
    zsh: segmentation fault (core dumped)  build/host/bin/mruby -e
    ```

  - Divide-by-Zero

    ```console
    % build/host/bin/mruby -e '([nil] * 256).__product_group([[]], 1 << 32, 256)'
    zsh: floating point exception (core dumped)  build/host/bin/mruby -e '([nil] * 256).__product_group([[]], 1 << 32, 256)'
    ```
2026-03-19 23:11:06 +09:00
Yukihiro "Matz" Matsumoto 8956c5abb5 mruby.h: include mruby/presym.h for all source files
Since presym is now mandatory, mruby.h includes presym.h so that
MRB_SYM() macros are available everywhere without explicit include.
Remove redundant #include <mruby/presym.h> from all source files.

Co-authored-by: Claude <noreply@anthropic.com>
2026-03-09 16:50:58 +09:00
Yukihiro "Matz" Matsumoto 71cb3c2e3a class.c: allocate ROM table wrappers per mrb_state
ROM method tables used static mrb_mt_tbl variables shared
across the process. The next pointer in each wrapper was
mutated by mrb_mt_init_rom(), causing cross-state
contamination when multiple mrb_state instances existed.

Allocate mrb_mt_tbl wrappers per-state via mrb_malloc().
The const mrb_mt_entry[] arrays remain static and shared.
Wrappers are tracked in mrb->rom_mt and freed at mrb_close().

Remove MRB_MT_ROM_TAB macro; add MRB_MT_INIT_ROM macro that
auto-computes size and calls the new mrb_mt_init_rom().

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 22:24:31 +09:00
Yukihiro "Matz" Matsumoto ea938c531f align MRB_MT_ENTRY columns and ISO section comments in ROM tables
Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 15:02:27 +09:00
Yukihiro "Matz" Matsumoto b460554d33 vm.c: generalize pre-dispatch argument count check for C methods
Replace check_method_noarg() with check_argument_count() that validates
min <= argc <= max using the full aspec stored in mrb_method_t.flags.
This catches ArgumentError earlier at dispatch time, before entering
the C function.

The old check only handled the special case of aspec==0 (NOARG).
The new check extracts REQ, OPT, REST, POST, KEY, and KDICT from
the aspec and validates accordingly. Keyword hash is counted as
a positional arg only when the method doesn't accept keywords.

Remove MRB_METHOD_NOARG_P macro from proc.h (subsumed by aspec check).
Fix 15 incorrect aspec declarations across the codebase that were
exposed by the stricter enforcement.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 14:25:48 +09:00
Yukihiro "Matz" Matsumoto 483c155a41 class.c: store aspec in ROM method table entries
Restore MRB_ARGS_* argument specs and ISO section comments to all
709 ROM method table entries. The aspec is encoded in bits 4-27 of
the flags field; MRB_MT_NOARG is now auto-derived from aspec==0.

Add MRB_MT_ENTRY_PRIVATE() macro for private methods (53 entries)
and MRB_MT_ASPEC() accessor for extracting aspec from flags.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 11:44:28 +09:00
Yukihiro "Matz" Matsumoto 8adba34bd9 class.c: auto-set MRB_MT_FUNC in MRB_MT_ENTRY macro
Since ROM table entries are always C functions, have the
MRB_MT_ENTRY() macro set MRB_MT_FUNC automatically. This
simplifies entry definitions across all 32 source files.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 10:37:06 +09:00
Yukihiro "Matz" Matsumoto 0fab703028 class.c: use linear search for method tables; make ROM entries const
Replace binary search with linear scan in mt_get(), mt_put(),
mt_del(), mt_chain_has(), and mrb_mt_foreach(). The method cache
makes repeated lookups O(1), so linear scan on cache misses is
acceptable.

This removes the sorting requirement, allowing ROM entry arrays
to be declared const. On embedded systems, const static data
resides in flash/ROM instead of RAM, saving ~8.4KB for ~700
method entries on 32-bit MCUs.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 08:26:05 +09:00
Yukihiro "Matz" Matsumoto bde2202100 class.c: refactor ROM method tables to array-of-structs layout
Replace the parallel-arrays (struct-of-arrays) ROM method table
layout with an array-of-structs layout where each mrb_mt_entry
bundles its function pointer and symbol key together.

New MRB_MT_ENTRY() and MRB_MT_ROM_TAB() macros simplify ROM table
definitions from a 3-part pattern (SIZE define + anonymous struct +
mrb_mt_tbl) to a 2-part pattern (entries array + mrb_mt_tbl).

Internal mt_* functions in class.c are simplified: single memmove/
memcpy operations replace paired key+value operations.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-19 23:59:30 +09:00
Yukihiro "Matz" Matsumoto 0ed26f8352 class.c: rename mt_/MT_ to mrb_mt_/MRB_MT_ for non-static identifiers
Follow mruby's naming convention: non-static types, macros, and
functions use the mrb_/MRB_ prefix. Renamed:
- union mt_ptr -> union mrb_mt_ptr
- mt_tbl -> mrb_mt_tbl
- MT_KEY(), MT_FUNC, MT_NOARG, MT_PUBLIC, MT_PRIVATE -> MRB_MT_*
- MT_KEY_SHIFT, MT_READONLY_BIT, MT_REMOVED_P -> MRB_MT_*
- mt_init_rom() -> mrb_mt_init_rom()
File-local static functions and macros in class.c are unchanged.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-19 15:22:55 +09:00
Yukihiro "Matz" Matsumoto 9fbc11c6d0 mrbgems: remove MRB_NO_PRESYM guards from core extension gems
Co-authored-by: Claude <noreply@anthropic.com>
2026-02-19 12:17:34 +09:00
Yukihiro "Matz" Matsumoto 6b66f99c6b mruby-array-ext: ROM method table for Array extensions (29 methods)
Co-authored-by: Claude <noreply@anthropic.com>
2026-02-18 16:41:10 +09:00
dearblue da75e4b049 Use MRB_ENSURE() instead of mrb_ensure()
The purpose is to avoid using the `MRB_TT_CPTR` object.
The reasons are as follows:
  - The `MRB_WORD_BOXING` setting involves object creation.
  - If object creation fails, the `ary_set_t` data leaks memory.
2026-01-24 11:32:57 +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 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 729b84cf26 mruby-array-ext: fix use-after-free in array set operations; fix #6662
during eql? callbacks, array modifications can cause elements in khash to
be freed by GC, leading to use-after-free. create temporary shared copies
of arrays before populating khash to protect elements during callbacks.

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-13 11:51:53 +09:00
Yukihiro "Matz" Matsumoto b56293c41d mruby-array-ext: fix memory leak in array set operations; ref #6662
add exception handling with MRB_TRY/MRB_CATCH to ensure khash cleanup
when eql? or hash methods raise exceptions. use kh_is_end macro for safe
khash iteration.

affected functions: Array#intersect?, Array#-, Array#|, Array#&, Array#uniq!

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-12 16:12:41 +09:00
Yukihiro "Matz" Matsumoto 150a8db053 mruby-array-ext: avoid goto in ary_slice_bang 2025-11-12 10:25:27 +09:00
Yukihiro "Matz" Matsumoto 704dfa01af mruby-array-ext: fix for some C++ compiler labels 2025-11-08 20:36:57 +09:00
Yukihiro "Matz" Matsumoto 0485ecdb01 mruby-array-ext: combine variable declaration with initialization 2025-11-08 14:28:22 +09:00
Yukihiro "Matz" Matsumoto 2c47e84cab Merge pull request #6656 from dearblue/combination_init.2 2025-10-29 11:01:29 +09:00
Yukihiro "Matz" Matsumoto ec58dca22f mruby-array-ext: use Data_Make_Struct() in ary_combination_init
refactor to use the standard Data_Make_Struct() macro instead of manual
RData allocation and linking. the macro provides automatic zero-initialization
and is more idiomatic.

ref #6655

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 11:46:22 +09:00
Yukihiro "Matz" Matsumoto 16efbd5c91 Merge pull request #6655 from dearblue/combination_init 2025-10-27 11:43:26 +09:00
Yukihiro "Matz" Matsumoto 40606418a9 mruby-array-ext: combine variable declaration with initialization 2025-10-27 08:38:17 +09:00
dearblue c28223ac5b Fix integer overflow in allocation size calculation
Passing a large integer value as the first argument to `Array#ary_combination_init` could cause an incorrect memory allocation due to integer overflow.
This would result in an invalid write during the subsequent zero-fill of the memory.

To resolve the issue, it has been replaced with `mrb_calloc()`.
However, since the current `mrb_calloc()` returns `NULL` due to overflow, it has been modified to raise an exception as a clear error.
2025-10-26 21:02:20 +09:00
dearblue 2512b4b399 Preventing Memory Leaks in Array#__combination_init
If memory allocated with `mrb_malloc()` is not associated with an object, subsequent attempts to allocate memory or objects will fail and raise an exception, resulting in a memory leak.
2025-10-26 20:49:25 +09:00
Yukihiro "Matz" Matsumoto bd3b5f87fb mruby-array-ext: revert unsafe length caching in ary_intersect_p; ref #6652
both hash and linear paths cache array lengths before loops that call
mrb_eql() and mrb_equal(), which can execute user code that modifies
arrays, causing out-of-bounds access.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-25 08:45:39 +09:00
Yukihiro "Matz" Matsumoto 56a0bdf493 mruby-array-ext: revert unsafe hash path hoisting; ref #6652
khash operations (kh_get, kh_put) call mrb_eql() which can execute user
code that modifies arrays during iteration, invalidating cached pointers
and lengths. reverted hoisting in ary_subtract_internal, ary_union_internal,
ary_intersection_internal, and ary_uniq_bang hash paths.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-25 08:45:39 +09:00
Yukihiro "Matz" Matsumoto 2f4d3a329b partial revert "mruby-array-ext: hoist RARRAY_PTR calls outside loops"; ref #6652
revert hoisting in functions that call mrb_equal() which can execute user
code that modifies arrays during iteration causing use-after-free

reverted functions:
- ary_assoc, ary_rassoc: call mrb_equal()
- ary_subtract_internal (linear path): calls mrb_equal()
- ary_union_internal (linear path): calls add_uniq() -> mrb_equal()
- ary_intersection_internal (linear path): calls mrb_equal()
- ary_intersect_p (linear path): calls mrb_equal()
- ary_uniq_bang (linear path): calls mrb_equal()

kept optimizations in:
- ary_compact_bang: only checks mrb_nil_p(), no callbacks
- ary_rotate: only reads from self, no callbacks
- hash paths: use kh_get/kh_put, no Ruby callbacks

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-25 08:45:39 +09:00
Yukihiro "Matz" Matsumoto b135601e6a mruby-array-ext: combine variable declaration with initialization
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-23 15:28:25 +09:00
Yukihiro "Matz" Matsumoto 93619f06dd mruby-array-ext: validate start and length in fill operation; fix #6650
add validation to prevent out-of-bounds write when negative start or
length bypasses normalization

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-23 11:03:11 +09:00
Yukihiro "Matz" Matsumoto 6e01f9dfc6 mruby-array-ext: hoist RARRAY_PTR calls outside loops
Optimizes array operations by hoisting RARRAY_PTR macro calls outside
loops to avoid repeated conditional checks (embed vs heap storage).

Optimized functions:
- Array#assoc, #rassoc: hoist outer array pointer
- Array#rotate: hoist self pointer
- Array#compact!: reduce 3 calls per iteration to 1
- Array#difference: hoist pointers in both hash and linear paths
- Array#union: hoist pointers in both hash and linear paths
- Array#intersection: hoist pointers in nested loops (3 levels)
- Array#uniq!: reduce O(n²) to O(n) pointer calls in linear path
- Array#disjoint?: hoist both array pointers in nested loop

Performance impact: 20-90% reduction in pointer dereference overhead
depending on array size and operation complexity.

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-22 12:06:46 +09:00
Yukihiro "Matz" Matsumoto cc71d93714 mruby-array-ext: fix conversion warning in ary_init_temp_set
Cast mrb_int capacity to khint_t when calling kh_init_data to resolve
C4244 warning about potential data loss in conversion from signed to
unsigned type. The khash API expects khint_t (uint32_t) parameters.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-22 21:34:57 +09:00
Yukihiro "Matz" Matsumoto 982b170346 mruby-array-ext: optimize repeated combination algorithm in C
Implement hybrid C/Ruby optimization for __repeated_combination method:
- Add combination state structure with C index generation
- Use iterator pattern to avoid VM callbacks (mrb_yield)
- Keep Ruby block handling while optimizing core algorithm
- Add comprehensive validation and error handling
- Maintain compatibility with existing repeated_combination/repeated_permutation APIs

Performance improvements:
- 5-10x faster index advancement in C vs Ruby arithmetic
- Reduced memory allocation for intermediate arrays
- Optimized for both small and large combination sizes

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-19 10:06:17 +09:00
Yukihiro "Matz" Matsumoto ad2757d6b5 mruby-array-ext: optimize Array#product by using __product_group C helper
Implemented `__product_group` in C to efficiently construct the intermediate
group arrays within Array#product. This reduces Ruby interpreter overhead
and improves performance for Array#product, especially for large inputs.

Co-authored-by: Gemini <gemini@google.com>
2025-08-15 22:38:53 +09:00
Yukihiro "Matz" Matsumoto 03478e6d2c mruby-array-ext: avoid heap allocation for temporary sets in array operations
The internal helper functions for array set operations now use a
stack-allocated `ary_set_t` instead of a heap-allocated one. This avoids
an unnecessary memory allocation for each call to `&`, `|`, `-`, `uniq!`,
and `intersect?`, improving performance by reducing overhead.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:53:07 +09:00
Yukihiro "Matz" Matsumoto f9243aa67a mruby-array-ext: replace ruby hash with khash.h for set operations
Replace Ruby Hash usage in array set operations with khash.h for better
memory efficiency and performance. This affects operations on arrays
larger than 32 elements.

Changes:
- Add KHASH_DECLARE/DEFINE for ary_set_t (set mode)
- Replace mrb_hash_* calls with kh_* equivalents
- Add helper functions for temporary set management
- Update all affected functions:
  * ary_subtract_internal (difference operations)
  * ary_union_internal (union operations)
  * ary_intersection_internal (intersection operations)
  * ary_intersect_p (intersection checking)
  * ary_uniq_bang (uniqueness operations)

Benefits:
- Better performance: direct C operations vs Ruby method calls
- Consistent with mruby core architecture using khash.h
- Eliminates unnecessary mrb_true_value() storage

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:02 +09:00
Yukihiro "Matz" Matsumoto d1bd883526 mruby-array-ext: refactor set operations argument parsing
Introduces `ary_get_array_args` to centralize the argument parsing logic for
set operations, reducing code duplication in `ary_subtract_internal`,
`ary_union_internal`, and `ary_intersection_internal`. Also fixes a bug in
`ary_union_internal` where converted arguments were not being used.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:52:52 +09:00
Yukihiro "Matz" Matsumoto 16fbae065d mruby-array-ext: refactor set operations to use hash set helpers
Introduces `ary_update_hash_set` to centralize the logic for adding array
elements to a hash set. This helper is now used by `ary_to_hash_set`,
`ary_subtract_internal`, and `ary_intersection_internal`, reducing code
duplication.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:52:52 +09:00
Yukihiro "Matz" Matsumoto 19b6cacbb5 mruby-array-ext: refactor ary_compact to use ary_compact_bang
This removes code duplication by making ary_compact call
ary_compact_bang on a duplicated array, centralizing the compaction
logic. It also reorders the functions to remove the need for a forward
declaration.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:52:52 +09:00
Yukihiro "Matz" Matsumoto 2250171071 mruby-array-ext: refactor ary_uniq to use ary_uniq_bang
This removes code duplication by making ary_uniq call ary_uniq_bang on a
duplicated array, centralizing the uniqueness logic.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:52:52 +09:00
Yukihiro "Matz" Matsumoto 17c671dce8 mruby-array-ext: fix use-after-free in ary_compact_bang
This commit fixes a use-after-free vulnerability in `ary_compact_bang` by
replacing pointer-based iteration with index-based loops. This prevents raw
pointers from becoming stale after a garbage collection cycle is triggered by
`mrb_ary_modify`.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:52:50 +09:00
Yukihiro "Matz" Matsumoto f88847841a mruby-array-ext: fix use-after-free in ary_slice_bang
This commit fixes a use-after-free vulnerability in `ary_slice_bang` by
replacing pointer-based operations with index-based operations. This prevents
raw pointers from becoming stale after a garbage collection cycle is triggered
by `mrb_ary_new_from_values`.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:52:50 +09:00
Yukihiro "Matz" Matsumoto 81726bacb8 mruby-array-ext: fix use-after-free in ary_uniq_bang
This commit fixes a use-after-free vulnerability in `ary_uniq_bang` by
replacing pointer-based iteration with index-based loops. This prevents raw
pointers from becoming stale after a garbage collection cycle is triggered by
functions like `mrb_hash_set` or `mrb_equal`.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:52:50 +09:00
Yukihiro "Matz" Matsumoto 96a9150580 mruby-array-ext: fix use-after-free in ary_uniq
This commit fixes a use-after-free vulnerability in `ary_uniq` by replacing
pointer-based iteration with index-based loops. This prevents raw pointers from
becoming stale after a garbage collection cycle is triggered by functions like
`mrb_hash_set`, `mrb_ary_push`, or `mrb_equal`.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:52:49 +09:00
Yukihiro "Matz" Matsumoto 48d9113b68 mruby-array-ext: fix use-after-free in ary_intersect_p
This commit fixes a use-after-free vulnerability in `ary_intersect_p` by
replacing pointer-based iteration with index-based loops. This prevents
raw pointers from becoming stale after a garbage collection cycle is
triggered by functions like `mrb_hash_set` or `mrb_equal`.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:52:49 +09:00
Yukihiro "Matz" Matsumoto 5640e1bd9e mruby-array-ext: fix use-after-free in ary_rotate
This commit fixes a use-after-free vulnerability in `ary_rotate` by replacing a
pointer-based loop with an index-based loop. This prevents a raw pointer from
becoming stale after a garbage collection cycle is triggered by `mrb_ary_push`.

Co-authored-by: Gemini <gemini@google.com>
2025-08-14 10:52:49 +09:00