707 Commits

Author SHA1 Message Date
Peter Zhu
55ea3ec00f Fix strict aliasing warning in rb_int128_to_numeric
If we don't have uint128, then rb_int128_to_numeric emits a strict
aliasing warning:

    numeric.c:3641:39: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
    3641 |         return rb_uint128_to_numeric(*(rb_uint128_t*)&n);
         |                                       ^~~~~~~~~~~~~~~~~
2025-12-08 15:01:05 -08:00
Nobuyoshi Nakada
a82aa08fe0
Make ruby_reset_leap_second_info internal
It is exported only for the extension library to test, but the method
is no longer used since 29e31e72fb5a14194a78ec974c4ba56c33ad8d45.
2025-12-08 12:11:58 +09:00
Nobuyoshi Nakada
6a1f5b68cb
Make ruby_reset_timezone internal
It is used only in hash.c, when `ENV['TZ']` is set.
2025-12-08 12:11:58 +09:00
Kazuki Yamaguchi
f2eece7199 Remove the internal-only attribute from ruby_reset_timezone()
The #ifdef is currently not taken because include/ruby/backward.h is
not included at this point. The attribute is unnecessary in an internal
header, so remove it.
2025-12-08 11:47:39 +09:00
Samuel Williams
a7dc53b91c
Add support for u128, U128, s128 and S128 integers to IO::Buffer. (#15399) 2025-12-06 15:55:32 +13:00
John Hawthorn
b0c9286d98 Use VWA for bignum
Previously we only allocated bignums from the 40 byte sizepool, and
embedded bignum used a fixed size.
2025-12-05 06:21:17 -08:00
Nobuyoshi Nakada
7f41c3e7b1
Add rb_eval_cmd_call_kw to shortcut 2025-12-04 18:07:49 +09:00
Jean Boussier
8c3909935e Handle NEWOBJ tracepoints settings fields
[Bug #21710]

- struct.c: `struct_alloc`

It is possible for a `NEWOBJ` tracepoint call back to write fields
into a newly allocated object before `struct_alloc` had the time
to set the `RSTRUCT_GEN_FIELDS` flags and such.

Hence we can't blindly initialize the `fields_obj` reference to `0`
we first need to check no fields were added yet.

- object.c: `rb_class_allocate_instance`

Similarly, if a `NEWOBJ` tracepoint tries to set fields on the object,
the `shape_id` must already be set, as it's required on T_OBJECT to
know where to write fields.

`NEWOBJ_OF` had to be refactored to accept a `shape_id`.
2025-12-03 08:14:56 +01:00
Satoshi Tagomori
9eafeaed67 Box: Free rb_classext_t struct for a box when the box is GCed 2025-12-02 23:49:49 +09:00
Max Bernstein
a25196395e Add BOP_GTGT
This will help JITs (and maybe later the interpreter) optimize
Integer#>>.
2025-12-01 15:19:26 -08:00
John Hawthorn
ff1d23eccb Use a serial to keep track of Mutex-owning Fiber
Previously this held a pointer to the Fiber itself, which requires
marking it (which was only implemented recently, prior to that it was
buggy). Using a monotonically increasing integer instead allows us to
avoid having a free function and keeps everything simpler.

My main motivations in making this change are that the root fiber lazily
allocates self, which makes the writebarrier implementation challenging
to do correctly, and wanting to avoid sending Mutexes to the remembered
set when locked by a short-lived Fiber.
2025-11-20 14:06:33 -08:00
Nobuyoshi Nakada
e31dc5f193
Fix a typo 2025-11-19 16:05:12 +09:00
Nobuyoshi Nakada
cdb9893c55 Win32: Drop support for older than MSVC 8.0/_MSC_VER 1400
Visual C++ 2005 (8.0):
- _MSC_VER: 1400
- MSVCRT_VERSION: 80
2025-11-19 11:03:42 +09:00
isuckatcs
6e5bbbc598 Remove include prefix from include paths 2025-11-14 09:37:49 +09:00
Luke Gruber
148fde2754
Revert "ns_subclasses refcount accesses need to be atomic (#15083)" (#15138)
This reverts commit 2998c8d6b99ec49925ebea42198b29c3e27b34a7.

We need to find a better way to fix this bug. Even with this refcount
change, errors were still being seen in CI. For now we need to remove
this failing test.
2025-11-11 02:52:43 +00:00
John Hawthorn
6238b6f53e Remove unused subclass methods 2025-11-10 15:55:38 -08:00
Satoshi Tagomori
c4691ef061 Rename Namespace to Ruby::Box 2025-11-07 13:14:54 +09:00
Satoshi Tagomori
d2a587c791 renaming internal data structures and functions from namespace to box 2025-11-07 13:14:54 +09:00
Satoshi Tagomori
aaa1234702 update referenced filenames from namespace to box 2025-11-07 13:14:54 +09:00
Satoshi Tagomori
50b9d9d355 rename namespace.c (and others) to box.c 2025-11-07 13:14:54 +09:00
Luke Gruber
2998c8d6b9
ns_subclasses refcount accesses need to be atomic (#15083)
We were seeing errors like:

```
* thread #8, stop reason = EXC_BAD_ACCESS (code=1, address=0x803)
  * frame #0: 0x00000001001fe944 ruby`rb_st_lookup(tab=0x00000000000007fb, key=1, value=0x00000001305b7490) at st.c:1066:22
    frame #1: 0x000000010002d658 ruby`remove_class_from_subclasses [inlined] class_get_subclasses_for_ns(tbl=0x00000000000007fb, ns_id=1) at class.c:604:9
    frame #2: 0x000000010002d650 ruby`remove_class_from_subclasses(tbl=0x00000000000007fb, ns_id=1, klass=4754039232) at class.c:620:34
    frame #3: 0x000000010002c8a8 ruby`rb_class_classext_free_subclasses(ext=0x000000011b5ce1d8, klass=4754039232, replacing=<unavailable>) at class.c:700:9
    frame #4: 0x000000010002c760 ruby`rb_class_classext_free(klass=4754039232, ext=0x000000011b5ce1d8, is_prime=true) at class.c:105:5
    frame #5: 0x00000001000e770c ruby`classext_free(ext=<unavailable>, is_prime=<unavailable>, namespace=<unavailable>, arg=<unavailable>) at gc.c:1231:5 [artificial]
    frame #6: 0x000000010002d178 ruby`rb_class_classext_foreach(klass=<unavailable>, func=(ruby`classext_free at gc.c:1228), arg=0x00000001305b75c0) at class.c:518:5
    frame #7: 0x00000001000e745c ruby`rb_gc_obj_free(objspace=0x000000012500c400, obj=4754039232) at gc.c:1282:9
    frame #8: 0x00000001000e70d4 ruby`gc_sweep_plane(objspace=0x000000012500c400, heap=<unavailable>, p=4754039232, bitset=4095, ctx=0x00000001305b76e8) at default.c:3482:21
    frame #9: 0x00000001000e6e9c ruby`gc_sweep_page(objspace=0x000000012500c400, heap=0x000000012500c540, ctx=0x00000001305b76e8) at default.c:3567:13
    frame #10: 0x00000001000e51d0 ruby`gc_sweep_step(objspace=0x000000012500c400, heap=0x000000012500c540) at default.c:3848:9
    frame #11: 0x00000001000e1880 ruby`gc_continue [inlined] gc_sweep_continue(objspace=0x000000012500c400, sweep_heap=0x000000012500c540) at default.c:3931:13
    frame #12: 0x00000001000e1754 ruby`gc_continue(objspace=0x000000012500c400, heap=0x000000012500c540) at default.c:2037:9
    frame #13: 0x00000001000e10bc ruby`newobj_cache_miss [inlined] heap_prepare(objspace=0x000000012500c400, heap=0x000000012500c540) at default.c:2056:5
    frame #14: 0x00000001000e1074 ruby`newobj_cache_miss [inlined] heap_next_free_page(objspace=0x000000012500c400, heap=0x000000012500c540) at default.c:2280:9
    frame #15: 0x00000001000e106c ruby`newobj_cache_miss(objspace=0x000000012500c400, cache=0x0000600001b00300, heap_idx=2, vm_locked=false) at default.c:2387:38
    frame #16: 0x00000001000e0d28 ruby`newobj_alloc(objspace=<unavailable>, cache=<unavailable>, heap_idx=<unavailable>, vm_locked=<unavailable>) at default.c:2411:15 [artificial]
    frame #17: 0x00000001000d7214 ruby`newobj_of [inlined] rb_gc_impl_new_obj(objspace_ptr=<unavailable>, cache_ptr=<unavailable>, klass=<unavailable>, flags=<unavailable>, wb_protected=<unavailable>, alloc_size=<unavailable>) at default.c:2490:15
    frame #18: 0x00000001000d719c ruby`newobj_of(cr=<unavailable>, klass=4313971728, flags=258, wb_protected=<unavailable>, size=<unavailable>) at gc.c:995:17
    frame #19: 0x00000001000d73ec ruby`rb_wb_protected_newobj_of(ec=<unavailable>, klass=<unavailable>, flags=<unavailable>, size=<unavailable>) at gc.c:1044:12 [artificial]
    frame #20: 0x0000000100032d34 ruby`class_alloc0(type=<unavailable>, klass=4313971728, namespaceable=<unavailable>) at class.c:803:5
```
2025-11-06 16:32:20 -05:00
Satoshi Tagomori
f53b8194cd Stop deleting the reference from superclass when replacing classext.
Calling the usual rb_iclass_classext_free() causes SEGV because
duplicating a newer classext of iclass had set the reference from superclass
to the newer classext, but calling rb_iclass_classext_free() deletes it.
2025-10-26 17:49:39 +09:00
Koichi Sasada
bc00c4468e use SET_SHAREABLE
to adopt strict shareable rule.

* (basically) shareable objects only refer shareable objects
* (exception) shareable objects can refere unshareable objects
  but should not leak reference to unshareable objects to Ruby world
2025-10-23 13:08:26 +09:00
Koichi Sasada
45907b1b00 add SET_SHAREABLE macros
* `RB_OBJ_SET_SHAREABLE(obj)` makes obj shareable.
  All of reachable objects from `obj` should be shareable.
* `RB_OBJ_SET_FROZEN_SHAREABLE(obj)` same as above
  but freeze `obj` before making it shareable.

Also `rb_gc_verify_shareable(obj)` is introduced to check
the `obj` does not violate shareable rule (an shareable object
only refers shareable objects) strictly.

The rule has some exceptions (some shareable objects can refer to
unshareable objects, such as a Ractor object (which is a shareable
object) can refer to the Ractor local objects.
To handle such case, `check_shareable` flag is also introduced.

`STRICT_VERIFY_SHAREABLE` macro is also introduced to verify
the strict shareable rule at `SET_SHAREABLE`.
2025-10-23 13:08:26 +09:00
Peter Zhu
4a23b6a89f Fix memory leak in RCLASS_SET_NAMESPACE_CLASSEXT
The st_insert in RCLASS_SET_NAMESPACE_CLASSEXT may overwrite an existing
rb_classext_t, causing it to leak memory. This commit changes it to use
st_update to free the existing one before overwriting it.
2025-10-21 18:42:17 -04:00
Peter Zhu
cd42096f5a Move rb_class_classext_free to class.c 2025-10-21 18:42:17 -04:00
Étienne Barrié
79b2685675 [DOC] Fix typos
Inspired by 42ba82424d908c290a4a34ced8853f0a403b734b, I looked for other
occurrences of "the the".
2025-10-13 15:21:36 -04:00
Satoshi Tagomori
9a0e857c35 Stop displaying current namespace when it crashed
To avoid crashes during displaying crash reports.
2025-10-07 22:18:29 +09:00
Satoshi Tagomori
2548c476a3 Add namespace debug methods and assertions 2025-10-07 14:19:26 +09:00
Peter Zhu
1858233ffa Free the native thread of the main thread on FREE_AT_EXIT 2025-10-04 18:21:13 -04:00
Satoshi Tagomori
2100826243 Fix wrong way to check an object is an instance of rb_cNamespace 2025-09-29 01:15:38 +09:00
Satoshi Tagomori
4f47327287 Update current namespace management by using control frames and lexical contexts
to fix inconsistent and wrong current namespace detections.

This includes:
* Moving load_path and related things from rb_vm_t to rb_namespace_t to simplify
  accessing those values via namespace (instead of accessing either vm or ns)
* Initializing root_namespace earlier and consolidate builtin_namespace into root_namespace
* Adding VM_FRAME_FLAG_NS_REQUIRE for checkpoints to detect a namespace to load/require files
* Removing implicit refinements in the root namespace which was used to determine
  the namespace to be loaded (replaced by VM_FRAME_FLAG_NS_REQUIRE)
* Removing namespaces from rb_proc_t because its namespace can be identified by lexical context
* Starting to use ep[VM_ENV_DATA_INDEX_SPECVAL] to store the current namespace when
  the frame type is MAGIC_TOP or MAGIC_CLASS (block handlers don't exist in this case)
2025-09-29 01:15:38 +09:00
Peter Zhu
809dfb861e Don't export rb_imemo_new
Nothing needs rb_imemo_new exported anymore.
2025-09-16 08:35:45 -04:00
John Hawthorn
e4f09a8c94 Remove next field and unused method from tmpbuf
These used to be used by the parser
2025-09-15 16:08:13 -07:00
Peter Zhu
1e3e04cd65 Move rb_imemo_tmpbuf_new to imemo.c 2025-09-15 11:43:05 -04:00
Peter Zhu
b0ce1fd549 Combine rb_imemo_tmpbuf_auto_free_pointer and rb_imemo_tmpbuf_new 2025-09-15 09:25:20 -04:00
Peter Zhu
a9b35b3a07 Remove useless field in rb_imemo_tmpbuf_t 2025-09-12 14:13:59 -04:00
Peter Zhu
729d602e29 Assert that RARRAY_AREF is within bounds
We should assert that i is within bounds to prevent buffer overflows.
2025-09-11 08:14:25 -04:00
Jean Boussier
4992d2c298 YJIT: rb_ivar_get_at skip ractor checks
Using `assume_single_ractor_mode` we can skip all ractor safety
checks if we're in single ractor mode.

```
compare-ruby: ruby 3.5.0dev (2025-08-27T14:58:58Z merge-vm-setivar-d.. 5b749d8e53) +YJIT +PRISM [arm64-darwin24]
built-ruby: ruby 3.5.0dev (2025-08-28T21:23:38Z yjit-get-exivar 3cc21b76d4) +YJIT +PRISM [arm64-darwin24]

|                           |compare-ruby|built-ruby|
|:--------------------------|-----------:|---------:|
|vm_ivar_get_on_obj         |     975.981|   975.772|
|                           |       1.00x|         -|
|vm_ivar_get_on_class       |     136.214|   470.912|
|                           |           -|     3.46x|
|vm_ivar_get_on_generic     |     148.315|   299.122|
|                           |           -|     2.02x|
```
2025-08-29 00:02:29 +02:00
Jean Boussier
b6d4882c05 YJIT: getinstancevariable cache indexes for types other than T_OBJECT
While accessing the ivars of other types is too complicated to
realistically generate the ASM for it, we can at least provide
the ivar index as to not have to lookup the shape tree every
time.

```
compare-ruby: ruby 3.5.0dev (2025-08-27T14:58:58Z merge-vm-setivar-d.. 5b749d8e53) +YJIT +PRISM [arm64-darwin24]
built-ruby: ruby 3.5.0dev (2025-08-28T17:58:32Z yjit-get-exivar efaa8c9b09) +YJIT +PRISM [arm64-darwin24]

|                           |compare-ruby|built-ruby|
|:--------------------------|-----------:|---------:|
|vm_ivar_get_on_obj         |     930.458|   936.865|
|                           |           -|     1.01x|
|vm_ivar_get_on_class       |     134.471|   431.622|
|                           |           -|     3.21x|
|vm_ivar_get_on_generic     |     146.679|   284.408|
|                           |           -|     1.94x|
```

Co-Authored-By: Aaron Patterson <tenderlove@ruby-lang.org>
2025-08-29 00:02:29 +02:00
Jean Boussier
e7fb87ee3a Populate ivar caches for types other than T_OBJECT
`vm_setinstancevariable` had a codepath to try to match the inline
cache for types other than T_OBJECT, but the cache population path
in `vm_setivar_slowpath` was exclusive to T_OBJECT, so `vm_setivar_default`
would never match anything.

This commit improves `vm_setivar_slowpath` so that it is capable of
filling the cache for all types, and adds a `vm_setivar_class` codepath
for `T_CLASS` and `T_MODULE`.

`vm_setivar`, `vm_setivar_default` and `vm_setivar_class` could be unified,
but based on the very explicit `NOINLINE` I assume they were split to minimize
codesize.

```
compare-ruby: ruby 3.5.0dev (2025-08-27T14:58:58Z merge-vm-setivar-d.. 5b749d8e53) +PRISM [arm64-darwin24]
built-ruby: ruby 3.5.0dev (2025-08-27T16:30:31Z setivar-cache-gene.. 4fe78ff296) +PRISM [arm64-darwin24]

|                         |compare-ruby|built-ruby|
|:------------------------|-----------:|---------:|
|vm_ivar_set_on_instance  |     161.809|   164.688|
|                         |           -|     1.02x|
|vm_ivar_set_on_generic   |      58.769|   115.638|
|                         |           -|     1.97x|
|vm_ivar_set_on_class     |      70.034|   141.042|
|                         |           -|     2.01x|
```
2025-08-28 09:25:51 +02:00
Takashi Kokubun
76810fc349
ZJIT: Implement side exit stats (#14357) 2025-08-27 10:01:07 -07:00
Jean Boussier
5257e1298c Replace ROBJECT_EMBED by ROBJECT_HEAP
The embed layout is way more common than the heap one,
especially since WVA.

I think it makes for more readable code to inverse the
flag.
2025-08-27 12:41:07 +02:00
Jean Boussier
14bdf4b57d Ensure T_OBJECT and T_IMEMO/fields have identical layout 2025-08-26 13:44:59 +02:00
Jean Boussier
e535b0da48 Get rid of obj_ivar_set_transition_too_complex 2025-08-25 13:45:51 +02:00
Luke Gruber
9db54a1a98 Fixes to encoding/transcoding for ractors.
Not all ractor-related encoding issues were fixed by 1afc07e815051e2f73493f055f2130cb642ba12a.
I found more by running my test-all branch with 3 ractors for each test.
2025-08-22 10:49:44 -07:00
John Hawthorn
feb8331673 Atomic CC table set in cache_callable_method_entry 2025-08-21 09:17:31 +02:00
Daniel Colson
fc5ee247d5
ZJIT: Compile toregexp (#14200)
`toregexp` is fairly similar to `concatstrings`, so this commit extracts
a helper for pushing and popping operands on the native stack.

There's probably opportunity to move some of this into lir (e.g. Alan
suggested a push_many that could use STP on ARM to push 2 at a time),
but I might save that for another day.
2025-08-19 10:02:13 -04:00
Jean Boussier
10aa4134d4 imemo_fields: store owner object in RBasic.klass
It is much more convenient than storing the klass, especially
when dealing with `object_id` as it allows to update the id2ref
table without having to dereference the owner, which may be
garbage at that point.
2025-08-13 19:53:18 +02:00
Jean Boussier
85c52079aa set.c: Store set_table->bins at the end of set_table->entries
This saves one pointer in `struct set_table`, which would allow
`Set` objects to still fit in 80B TypedData slots even if RTypedData
goes from 32B to 40B large.

The existing set benchmark seem to show this doesn't have a very
significant impact. Smaller sets are a bit faster, larger sets
a bit slower.

It seem consistent over multiple runs, but it's unclear how much
of that is just error margin.

```
compare-ruby: ruby 3.5.0dev (2025-08-12T02:14:57Z master 428937a536) +YJIT +PRISM [arm64-darwin24]
built-ruby: ruby 3.5.0dev (2025-08-12T07:22:26Z set-entries-bounds da30024fdc) +YJIT +PRISM [arm64-darwin24]
warming up........

|                         |compare-ruby|built-ruby|
|:------------------------|-----------:|---------:|
|new_0                    |     15.459M|   15.823M|
|                         |           -|     1.02x|
|new_10                   |      3.484M|    3.574M|
|                         |           -|     1.03x|
|new_100                  |    546.992k|  564.679k|
|                         |           -|     1.03x|
|new_1000                 |     49.391k|   48.169k|
|                         |       1.03x|         -|
|aref_0                   |     18.643M|   19.350M|
|                         |           -|     1.04x|
|aref_10                  |      5.941M|    6.006M|
|                         |           -|     1.01x|
|aref_100                 |    822.197k|  814.219k|
|                         |       1.01x|         -|
|aref_1000                |     83.230k|   79.411k|
|                         |       1.05x|         -|
```
2025-08-12 21:56:57 +02:00