350 Commits
Author SHA1 Message Date
Benoit DalozeandClaude Opus 5 091399f637 Move prism_compile.h include out of iseq.h
iseq.h does not reference any of the pm_* types or functions declared in
prism_compile.h, so include the latter from the files that actually use
it instead. Only 6 of the 19 files including iseq.h need the prism
declarations, so this keeps prism/prism.h out of the include closure of
ast.c, box.c, builtin.c, cont.c, eval.c, jit.c, proc.c, thread.c,
vm_dump.c, vm_trace.c, yjit.c, zjit.c and internal/jit.h.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-10-14 16:20:26 +02:00
Hiroshi SHIBATAandClaude Opus 5 224dc5c110 Use rb_len_t for path lengths
The file, dir, load and ruby.c changes go together because rb_get_expanded_load_path() returns the maximum path length through a pointer. Lengths passed to the Windows string conversion helpers that take `long *` stay `long`.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-10-09 14:00:15 +09:00
Peter Zhu b20a2caf1c Fix memory leak reported in TestRequire#test_loading_fifo_threading_raise
The test TestRequire#test_loading_fifo_threading_raise fails with the
following error in LSAN because pm_load_parse_file could raise which
will cause the pm_parse_result_t to leak.

    Direct leak of 104 byte(s) in 1 object(s) allocated from:
        #0 0x64ee1a50385d in calloc build-llvm/tools/clang/stage2-bins/runtimes/runtimes-bins/compiler-rt/lib/asan/asan_malloc_linux.cpp:74:3
        #1 0x64ee1a578206 in calloc1 gc/default/default.c:2000:12
        #2 0x64ee1a578206 in rb_gc_impl_calloc gc/default/default.c:11069:5
        #3 0x64ee1a578206 in ruby_xcalloc_body gc.c:6146:12
        #4 0x64ee1a578206 in ruby_xcalloc gc.c:6140:34
        #5 0x64ee1acfaed1 in pm_parse_result_init prism_compile.c:10639:23
        #6 0x64ee1a67bbd1 in load_iseq_eval load.c:748:13
        #7 0x64ee1a674cd4 in rb_load_internal load.c:866:9
        #8 0x64ee1a6754d6 in rb_load_entrypoint load.c:908:5
        #9 0x64ee1a6790a5 in rb_f_load load.c:950:12
        #10 0x64ee1a97043b in vm_call_cfunc_with_frame_ vm_insnhelper.c:3899:11
        #11 0x64ee1a957d0b in vm_call_method_each_type vm_insnhelper.c:4891:16
        #12 0x64ee1a95779b in vm_call_method vm_insnhelper.c
        #13 0x64ee1a91a9ad in vm_sendish vm_insnhelper.c:6213:15
        #14 0x64ee1a91a9ad in vm_exec_core insns.def:909:11
        #15 0x64ee1a9083bd in rb_vm_exec vm.c:2875:22
        #16 0x64ee1a54bccc in rb_ec_exec_node eval.c:299:9
        #17 0x64ee1a54b993 in ruby_run_node eval.c:337:30
        #18 0x64ee1a547f50 in rb_main main.c:42:12
        #19 0x64ee1a547f50 in main main.c:62:12
        #20 0x7f8507a2a1c9 in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
        #21 0x7f8507a2a28a in __libc_start_main csu/../csu/libc-start.c:360:3
        #22 0x64ee1a45fb74 in _start (ruby+0x197b74) (BuildId: 5b18a734908cec0a5f93267f1f350850b0f3fb6d)

    Direct leak of 16 byte(s) in 1 object(s) allocated from:
        #0 0x64ee1a50385d in calloc build-llvm/tools/clang/stage2-bins/runtimes/runtimes-bins/compiler-rt/lib/asan/asan_malloc_linux.cpp:74:3
        #1 0x64ee1a578206 in calloc1 gc/default/default.c:2000:12
        #2 0x64ee1a578206 in rb_gc_impl_calloc gc/default/default.c:11069:5
        #3 0x64ee1a578206 in ruby_xcalloc_body gc.c:6146:12
        #4 0x64ee1a578206 in ruby_xcalloc gc.c:6140:34
        #5 0x64ee1acfaeb9 in pm_parse_result_init prism_compile.c:10638:21
        #6 0x64ee1a67bbd1 in load_iseq_eval load.c:748:13
        #7 0x64ee1a674cd4 in rb_load_internal load.c:866:9
        #8 0x64ee1a6754d6 in rb_load_entrypoint load.c:908:5
        #9 0x64ee1a6790a5 in rb_f_load load.c:950:12
        #10 0x64ee1a97043b in vm_call_cfunc_with_frame_ vm_insnhelper.c:3899:11
        #11 0x64ee1a957d0b in vm_call_method_each_type vm_insnhelper.c:4891:16
        #12 0x64ee1a95779b in vm_call_method vm_insnhelper.c
        #13 0x64ee1a91a9ad in vm_sendish vm_insnhelper.c:6213:15
        #14 0x64ee1a91a9ad in vm_exec_core insns.def:909:11
        #15 0x64ee1a9083bd in rb_vm_exec vm.c:2875:22
        #16 0x64ee1a54bccc in rb_ec_exec_node eval.c:299:9
        #17 0x64ee1a54b993 in ruby_run_node eval.c:337:30
        #18 0x64ee1a547f50 in rb_main main.c:42:12
        #19 0x64ee1a547f50 in main main.c:62:12
        #20 0x7f8507a2a1c9 in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
        #21 0x7f8507a2a28a in __libc_start_main csu/../csu/libc-start.c:360:3
        #22 0x64ee1a45fb74 in _start (ruby+0x197b74) (BuildId: 5b18a734908cec0a5f93267f1f350850b0f3fb6d)
2026-09-09 13:41:38 +09:00
Peter Zhu eb2dc8cbe4 Make rb_f_require_relative static 2026-09-04 15:27:10 +09:00
Hiroshi SHIBATAandClaude Fable 5 104c0f06dd Copy box-local extensions into a process-private directory
Flattening the full source path into a single file name made the copy
fail with ENAMETOOLONG when the extension lives under a deep path such
as a long BUNDLE_PATH, since the name exceeds NAME_MAX. Name each copy
with a short per-process sequence instead, in a 0700 directory with an
unpredictable name so that other local users cannot occupy the copy
destination in a shared TMPDIR.

https://bugs.ruby-lang.org/issues/22110

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-31 14:43:42 +09:00
Hiroshi SHIBATAandClaude Fable 5 1c664d8024 Defer unloading box-local extension DLLs on Windows
box_entry_free could run before other T_DATA objects are finalized in
rb_objspace_call_finalizer, and FreeLibrary there unmapped the
rb_data_type_t and dfree functions that later finalization still
dereferences, crashing any boxed process that leaves extension objects
alive at exit. Keep the copied DLLs loaded until ruby_vm_destruct, then
unload and delete them.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-27 10:42:13 +09:00
Nobuyoshi Nakada c2883ddf97 Avoid NUL-terminated extension checks in require
Search and compare feature extensions within the Ruby string length so
require does not rely on a terminator while classifying paths.
2026-07-15 13:28:39 +09:00
Jean Boussier d17f8ecd19 file.c: right size the rb_find_file_ext & rb_find_file buffer
It generally end up frozen in `$LOADED_FEATURES` so it's preferable
to predict its size and only deal with an embedded string most of
the time.

It saves on having to allocate a `MAXPATHLEN + 2` (1026) bytes long
buffer and then realloc it later to shink it.
2026-06-19 21:20:07 +02:00
Nobuyoshi Nakada efd35db39c Make rb_load_entrypoint accept two arguments directly
`rb_load_entrypoint` is used only by `rb_box_load`; it checks that the
argument has two-elements (implicitly assuming it is an array),
extracts its contents, and then simply discards the argument.
2026-05-19 19:21:18 +09:00
Étienne Barrié 42b3cdc51a Remove discard qualifiers warning with C23 strchr
C23 has qualifier-preserving standard library functions, so calling
strchr with a `const char *` will return a `const char *`. We can change
the type of the local variables because we don't mutate the strings.
2026-04-22 16:45:35 +09:00
Kevin Newton 78ab3a7ced Leverage new Prism APIs
Prism changed structure quite a bit. Most of the previously-public
structs are now opaque. This requires quite a bit of changes
internally.

It also triggered some unrelated changes, which were necessary
because Prism's main header used to pull in standard headers. So
box.c and jit.c are now no longer transitively getting all the
headers they needed.
2026-03-20 09:37:43 -04:00
Jean Boussier 53351a4363 Embed vm->static_ext_inits 2026-03-16 15:04:38 +01:00
Samuel Williams f0ff0df584 Introduce suport for autoload_relative. (#16148) 2026-02-13 12:21:59 +13:00
Jean Boussier cbf6e775ee darray.h: use ruby_sized_xfree 2026-02-08 12:47:40 +01:00
Nobuyoshi Nakada 3636277dc5 Add NUM2PTR and PTR2NUM macros
These macros have been defined here and there, so collect them.
2025-12-10 12:09:50 +09:00
Nobuyoshi Nakada 573896a40a Box: remove copied extension files 2025-12-09 23:41:50 +09:00
Satoshi Tagomori 03ed220cb0 Box: load_wrapping is not needed now
Top level constants are defined on the Object class's constant table,
and those constants can be referred as box::CONST_NAME from outside box.
So load_wrapping() is not needed now.
2025-12-02 23:49:49 +09:00
Stan Lo 4cd6661e18 Reorganize page documentations (#15154)
Re-organize page docs
2025-11-27 20:12:24 +00:00
Nobuyoshi Nakada c65f8b6370 Prevent the path for copied extension from GC 2025-11-08 10:17:07 +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 961683bcb1 Run .so init functions in namespaces to be loaded 2025-10-31 22:42:07 +09:00
Nobuyoshi Nakada 2bb6fe3854 [Bug #21629] Initialize struct RString 2025-10-08 18:19:47 +09:00
Nobuyoshi Nakada 5b2ec0eb1b Save ns that may be clobbered by setjmp/longjmp 2025-10-03 17:59:54 +09:00
Yusuke Endoh 7ae67e8f6a load.c: Fix dest and src of MEMMOVE
When multiple files with the same name are required, the features_index
hash stores the indexes in `$LOADED_FEATURES` array into a darray.
The dest and src arguments for `MEMMOVE` were wrongly reversed when
inserting a new index in the darray.

[Bug #21568]
2025-10-02 20:00:41 +09:00
Satoshi Tagomori 4644d14990 Fix the missed vm_ns during rebase to follow the change b227a942b205c89fdb5adc85acdf029b9b83faf1 2025-09-29 01:15:38 +09:00
Satoshi Tagomori 140bf4d8a0 localize rb_vm_t and minimize times of GET_VM() calls 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 f164e1c03a Fix out-of-bounds read in require when $LOADED_FEATURES is modified
The following script causes an out-of-bounds read on the $LOADED_FEATURES
array when it is modified by another thread:

    require "tempfile"
    PATH = Tempfile.create(["test", ".rb"]).path

    2.times.map do
      Thread.new do
        20.times do
          require PATH
          $LOADED_FEATURES.delete_if { |p| p == PATH }
        end
      end
    end.each(&:join)

Crashes with:

    internal/array.h:143: Assertion Failed: RARRAY_AREF:i < RARRAY_LEN(ary)
    ruby 3.5.0dev (2025-09-10T18:47:06Z array-aref-assert-.. 765a3fd01c) +PRISM [arm64-darwin24]

    -- Crash Report log information --------------------------------------------
      See Crash Report log file in one of the following locations:
        * ~/Library/Logs/DiagnosticReports
        * /Library/Logs/DiagnosticReports
      for more details.
    Don't forget to include the above Crash Report log file in bug reports.

    -- Control frame information -----------------------------------------------
    c:0005 p:---- s:0019 e:000018 CFUNC  :require
    c:0004 p:0005 s:0014 e:000013 BLOCK  test.rb:19
    c:0003 p:0024 s:0011 e:000010 METHOD <internal:numeric>:257
    c:0002 p:0005 s:0006 e:000005 BLOCK  test.rb:18 [FINISH]
    c:0001 p:---- s:0003 e:000002 DUMMY  [FINISH]

    -- Ruby level backtrace information ----------------------------------------
    test.rb:18:in 'block (2 levels) in <main>'
    <internal:numeric>:257:in 'times'
    test.rb:19:in 'block (3 levels) in <main>'
    test.rb:19:in 'require'

    -- Threading information ---------------------------------------------------
    Total ractor count: 1
    Ruby thread count for this ractor: 2

    -- C level backtrace information -------------------------------------------
    miniruby(rb_vm_bugreport+0xb88) [0x100f3f1d4] vm_dump.c:1175
    miniruby(rb_vm_bugreport) (null):0
    miniruby(rb_assert_failure_detail+0xd4) [0x10108d920] error.c:1215
    miniruby(rb_assert_failure_detail+0x0) [0x10108d84c] error.c:1191
    miniruby(rb_assert_failure) (null):0
    miniruby(rb_ary_pop.cold.9+0x0) [0x101087198] internal/array.h:143
    miniruby(RARRAY_AREF) (null):0
    miniruby(rb_ary_pop.cold.7) array.c:1443
    miniruby(rb_feature_p+0x720) [0x100dbe28c] internal/array.h:143
    miniruby(search_required+0x2cc) [0x100dbcb78] load.c:1203
    miniruby(require_internal+0x144) [0x100dbd108] load.c:1434
    miniruby(rb_require_string_internal+0x78) [0x100dbc6bc] load.c:1581
    miniruby(rb_require_string+0x20) [0x100dbc56c] load.c:1567
    miniruby(rb_f_require) load.c:1160
    miniruby(vm_call_cfunc_with_frame_+0xe8) [0x100f2e998] vm_insnhelper.c:3873
    miniruby(vm_sendish+0x718) [0x100f08b20]
    miniruby(vm_exec_core+0x6044) [0x100f10a94]
    miniruby(rb_vm_exec+0x170) [0x100f08e3c] vm.c:2639
    miniruby(vm_invoke_proc+0x200) [0x100f1f564] vm.c:1669
    miniruby(thread_do_start_proc+0x2f4) [0x100ed8420] thread.c:605
    miniruby(thread_start_func_2+0x37c) [0x100ed7714] thread.c:622
    miniruby(call_thread_start_func_2+0x18) [0x100eda144] thread_pthread.c:2234
    miniruby(nt_start) thread_pthread.c:2279
    /usr/lib/system/libsystem_pthread.dylib(_pthread_start+0x88) [0x19c0e7c0c]
2025-09-11 08:14:25 -04:00
Peter Zhu 928fea3bfa Fix crash when $LOADED_FEATURES is modified during require
[Bug #21567]

When we require an object that is not a string, it will attempt to convert
it to a string by calling to_str on it. If we modify the $LOADED_FEATURES
array while it calls to_str, Ruby can crash because it can end up inserting
the string in the wrong index in the array.

For example, the following script crashes:

    require "tempfile"

    class MyString
      def initialize(path)
        @path = path
      end

      def to_str
        $LOADED_FEATURES.clear
        @path
      end

      def to_path = @path
    end

    def create_ruby_file = Tempfile.create(["test", ".rb"]).path

    require MyString.new(create_ruby_file)
    $LOADED_FEATURES.unshift(create_ruby_file)
    $LOADED_FEATURES << MyString.new(create_ruby_file)
    require create_ruby_file

Crash log:

    test.rb:21: [BUG] Segmentation fault at 0x0000000000000004
    ruby 3.5.0dev (2025-09-09T09:29:35Z master ce94add7fb) +PRISM [arm64-darwin24]

    -- Crash Report log information --------------------------------------------
      See Crash Report log file in one of the following locations:
        * ~/Library/Logs/DiagnosticReports
        * /Library/Logs/DiagnosticReports
      for more details.
    Don't forget to include the above Crash Report log file in bug reports.

    -- Control frame information -----------------------------------------------
    c:0003 p:---- s:0011 e:000010 CFUNC  :require
    c:0002 p:0076 s:0006 e:000005 EVAL   test.rb:21 [FINISH]
    c:0001 p:0000 s:0003 E:0001b0 DUMMY  [FINISH]

    -- Ruby level backtrace information ----------------------------------------
    test.rb:21:in '<main>'
    test.rb:21:in 'require'

    -- Threading information ---------------------------------------------------
    Total ractor count: 1
    Ruby thread count for this ractor: 1

    -- Machine register context ------------------------------------------------
      x0: 0x0000000000000004  x1: 0x000000000000c800  x2: 0x0000000000000000
      x3: 0x0000000000000000  x4: 0x0000000000000205  x5: 0x0000000000000000
      x6: 0x0000000000000000  x7: 0x0000000000000001 x18: 0x0000000000000000
    x19: 0x0000000209dfc0b0 x20: 0x0000000209dfc018 x21: 0x000000016ee8ab58
    x22: 0x0fffffff0009d71d x23: 0x0000000209dfc018 x24: 0x0000000209dfc150
    x25: 0x000000016ee8acc0 x26: 0x0000000000000000 x27: 0x0000000000000000
    x28: 0x0000000000000000  lr: 0x0000000101244140  fp: 0x000000016ee887f0
      sp: 0x000000016ee887d0

    -- C level backtrace information -------------------------------------------
    miniruby(rb_print_backtrace+0x24) [0x101317b08] vm_dump.c:843
    miniruby(rb_print_backtrace) (null):0
    miniruby(rb_vm_bugreport+0x26c) [0x101317d94] vm_dump.c:1175
    miniruby(rb_bug_for_fatal_signal+0xa4) [0x10105ddac] error.c:1130
    miniruby(sig_do_nothing+0x0) [0x1012278c0] signal.c:948
    miniruby(sigsegv) (null):0
    /usr/lib/system/libsystem_platform.dylib(_sigtramp+0x38) [0x19c1216a4]
    miniruby(rb_str_new_frozen+0x1c) [0x101244140] string.c:1495
    miniruby(rb_check_realpath_internal+0x68) [0x101077804] file.c:4679
    miniruby(rb_check_realpath+0x2c) [0x101077aa4] file.c:4765
    miniruby(get_loaded_features_index+0x37c) [0x1010f9c94] load.c:467
    miniruby(rb_feature_p+0xd0) [0x1010f8174] load.c:582
    miniruby(search_required+0xac) [0x1010f6ad4] load.c:1193
    miniruby(require_internal+0x274) [0x1010f7518] load.c:1424
    miniruby(rb_require_string_internal+0x94) [0x1010f6830] load.c:1571
    miniruby(rb_require_string+0x58) [0x1010f66e8] load.c:1557
    miniruby(rb_f_require+0x1c) [0x1010f6684] load.c:1150
    miniruby(ractor_safe_call_cfunc_1+0x38) [0x101306c28] vm_insnhelper.c:3696
    miniruby(vm_call_cfunc_with_frame_+0x250) [0x1012f857c] vm_insnhelper.c:3873
    miniruby(vm_call_cfunc_with_frame+0x6c) [0x1012f8834] vm_insnhelper.c:3919
    miniruby(vm_sendish+0x1a8) [0x1012c990c] vm_insnhelper.c:6087
    miniruby(vm_exec_core+0x4050) [0x1012cfb48] insns.def:900
    miniruby(vm_exec_loop+0x80) [0x1012e5448] vm.c:2666
    miniruby(rb_vm_exec+0x134) [0x1012c9b40] vm.c:2645
    miniruby(rb_iseq_eval_main+0x34) [0x1012e5628] vm.c:2919
    miniruby(rb_ec_exec_node+0xe4) [0x10106d094] eval.c:282
    miniruby(ruby_run_node+0x94) [0x10106cf64] eval.c:320
    miniruby(rb_main+0x40) [0x100f7499c] main.c:42
    miniruby(main+0x60) [0x100f74928] main.c:62
2025-09-10 11:41:24 +02:00
Burdette Lamar 306df12949 [DOC] New .md file to replace doc/globals.rdoc 2025-08-11 22:02:59 -04:00
ArtSin a6aaeb9acf load.c: fix prev_ext_config clobbering in require_internal
The variable `prev_ext_config` is modified by `ext_config_push` between
`setjmp` and `longjmp` calls. Since `ext_config_push` and `ext_config_pop`
are small and likely to be inlined, `prev_ext_config` can be allocated on
a register and get clobbered. Fix by making it `volatile`.

This bug can be observed by adding a check for values greater than 1 in
`th2->ext_config.ractor_safe` after `ext_config_pop` and building with Clang.
2025-08-05 22:19:55 +09:00
Jean Boussier 482f4cad82 Autoload encodings on the main ractor
None of the datastructures involved in the require process are
safe to call on a secondary ractor, however when autoloading
encodings, we do so from the current ractor.

So all sorts of corruption can happen when using an autoloaded
encoding for the first time from a secondary ractor.
2025-07-07 12:44:21 +02:00
Nobuyoshi Nakada 2e3f81838c Align styles [ci skip] 2025-05-15 17:48:40 +09:00
Satoshi Tagomori 294b52fb9b Follow the code style about else 2025-05-11 23:32:50 +09:00
Satoshi Tagomori 382645d440 namespace on read 2025-05-11 23:32:50 +09:00
Yusuke Endoh ee1f39ef88 Add a document to autoload
Users are responsible for avoiding circular autoload.

[Misc #21154]
2025-03-13 22:08:01 +09:00
Jean Boussier d4a1a2780c rb_feature_p: skip get_expanded_load_path for absolute paths
Ref: https://github.com/fxn/zeitwerk/pull/308

```ruby
require 'benchmark'

$LOAD_PATH << 'relative-path'

autoload :FOO, '/tmp/foo.rb'

puts Benchmark.realtime {
  500_000.times do
    Object.autoload?(:FOO)
  end
}
```

The above script takes 2.5 seconds on `master`, and only
50ms on this branch.

When we're looking for a feature with an absolute path, we don't
need to call the expensive `get_expanded_load_path`.
2025-01-26 09:52:22 +01:00
Peter Zhu 36966456c7 GC guard lookup_name
When searching for native extensions, if the name does not end in ".so"
then we create a new string and append ".so" so it. If the native extension
is in static_ext_inits, then we could trigger a GC in the rb_filesystem_str_new_cstr.
This could cuase the GC to free lookup_name since we don't use the local
variable anymore.

This bug was caught in this ASAN build:
http://ci.rvm.jp/results/trunk_asan@ruby-sp1/5479182

    ==435614==ERROR: AddressSanitizer: use-after-poison on address 0x715a63022da0 at pc 0x5e7463873e4e bp 0x7fff383c8b00 sp 0x7fff383c82c0
    READ of size 14 at 0x715a63022da0 thread T0
        #0 0x5e7463873e4d in __asan_memcpy (/tmp/ruby/build/trunk_asan/ruby+0x214e4d) (BuildId: 607411c0626a2f66b4c20c02179b346aace20898)
        #1 0x5e7463b50a82 in memcpy /usr/include/x86_64-linux-gnu/bits/string_fortified.h:29:10
        #2 0x5e7463b50a82 in ruby_nonempty_memcpy /tmp/ruby/src/trunk_asan/include/ruby/internal/memory.h:671:16
        #3 0x5e7463b50a82 in str_enc_new /tmp/ruby/src/trunk_asan/string.c:1035:9
        #4 0x5e74639b97dd in search_required /tmp/ruby/src/trunk_asan/load.c:1126:21
        #5 0x5e74639b97dd in require_internal /tmp/ruby/src/trunk_asan/load.c:1274:17
        #6 0x5e74639b83c1 in rb_require_string_internal /tmp/ruby/src/trunk_asan/load.c:1401:22
        #7 0x5e74639b83c1 in rb_require_string /tmp/ruby/src/trunk_asan/load.c:1387:12
2024-12-20 16:52:36 -05:00
Peter Zhu 51ffef2819 Fix memory leak in prism when syntax error in iseq compilation
If there's a syntax error during iseq compilation then prism would leak
memory because it would not free the pm_parse_result_t.

This commit changes pm_iseq_new_with_opt to have a rb_protect to catch
when an error is raised, and return NULL and set error_state to a value
that can be raised by calling rb_jump_tag after memory has been freed.

For example:

    10.times do
      10_000.times do
        eval("/[/=~s")
      rescue SyntaxError
      end

      puts `ps -o rss= -p #{$$}`
    end

Before:

    39280
    68736
    99232
    128864
    158896
    188208
    217344
    246304
    275376
    304592

After:

    12192
    13200
    14256
    14848
    16000
    16000
    16000
    16064
    17232
    17952
2024-11-08 15:43:41 -05:00
Koichi Sasada aa63699d10 support require in non-main Ractors
Many libraries should be loaded on the main ractor because of
setting constants with unshareable objects and so on.

This patch allows to call `requore` on non-main Ractors by
asking the main ractor to call `require` on it. The calling ractor
waits for the result of `require` from the main ractor.

If the `require` call failed with some reasons, an exception
objects will be deliverred from the main ractor to the calling ractor
if it is copy-able.

Same on `require_relative` and `require` by `autoload`.

Now `Ractor.new{pp obj}` works well (the first call of `pp` requires
`pp` library implicitly).

[Feature #20627]
2024-11-08 18:02:46 +09:00
Nobuyoshi Nakada 3e1021b144 Make default parser enum and define getter/setter 2024-10-02 20:43:40 +09:00
Nikolay Ponomarev bb2d24731e [DOC] Fix autoload method formatting 2024-09-17 12:52:31 +09:00
Kevin Newton 371432b2d7 [PRISM] Handle RubyVM.keep_script_lines 2024-08-29 20:27:01 -04:00
d25b74b32c Resize arrays in rb_ary_freeze and use it for freezing arrays
While working on a separate issue we found that in some cases
`ary_heap_realloc` was being called on frozen arrays. To fix this, this
change does the following:

1) Updates `rb_ary_freeze` to assert the type is an array, return if
already frozen, and shrink the capacity if it is not embedded, shared
or a shared root.
2) Replaces `rb_obj_freeze` with `rb_ary_freeze` when the object is
always an array.
3) In `ary_heap_realloc`, ensure the new capa is set with
`ARY_SET_CAPA`. Previously the change in capa was not set.
4) Adds an assertion to `ary_heap_realloc` that the array is not frozen.

Some of this work was originally done in
https://github.com/ruby/ruby/pull/2640, referencing this issue
https://bugs.ruby-lang.org/issues/16291. There didn't appear to be any
objections to this PR, it appears to have simply lost traction.

The original PR made changes to arrays and strings at the same time,
this PR only does arrays. Also it was old enough that rather than revive
that branch I've made a new one. I added Lourens as co-author in addtion
to Aaron who helped me with this patch.

The original PR made this change for performance reasons, and while
that's still true for this PR, the goal of this PR is to avoid
calling `ary_heap_realloc` on frozen arrays. The capacity should be
shrunk _before_ the array is frozen, not after.

Co-authored-by: Aaron Patterson <tenderlove@ruby-lang.org>
Co-Authored-By: methodmissing <lourens@methodmissing.com>
2024-07-02 10:34:23 -07:00
Nobuyoshi Nakada da69c9235f Fix -Wclobbered warnings 2024-05-29 17:38:26 +09:00
Kevin Newton a708b6aa65 [PRISM] Respect eval coverage setting 2024-05-20 12:28:47 -04:00
yui-knk 899d9f79dd Rename vast to ast_value
There is an English word "vast".
This commit changes the name to be more clear name to avoid confusion.
2024-05-03 12:40:35 +09:00
HASUMI HitoshiandNobuyoshi Nakada 2244c58b00 [Universal parser] Decouple IMEMO from rb_ast_t
This patch removes the `VALUE flags` member from the `rb_ast_t` structure making `rb_ast_t` no longer an IMEMO object.

## Background

We are trying to make the Ruby parser generated from parse.y a universal parser that can be used by other implementations such as mruby.
To achieve this, it is necessary to exclude VALUE and IMEMO from parse.y, AST, and NODE.

## Summary (file by file)

- `rubyparser.h`
  - Remove the `VALUE flags` member from `rb_ast_t`
- `ruby_parser.c` and `internal/ruby_parser.h`
  - Use TypedData_Make_Struct VALUE which wraps `rb_ast_t` `in ast_alloc()` so that GC can manage it
    - You can retrieve `rb_ast_t` from the VALUE by `rb_ruby_ast_data_get()`
  - Change the return type of `rb_parser_compile_XXXX()` functions from `rb_ast_t *` to `VALUE`
  - rb_ruby_ast_new() which internally `calls ast_alloc()` is to create VALUE vast outside ruby_parser.c
- `iseq.c` and `vm_core.h`
  - Amend the first parameter of `rb_iseq_new_XXXX()` functions from `rb_ast_body_t *` to `VALUE`
  - This keeps the VALUE of AST on the machine stack to prevent being removed by GC
- `ast.c`
  - Almost all change is replacement `rb_ast_t *ast` with `VALUE vast` (sorry for the big diff)
  - Fix `node_memsize()`
    - Now it includes `rb_ast_local_table_link`, `tokens` and script_lines
- `compile.c`, `load.c`, `node.c`, `parse.y`, `proc.c`, `ruby.c`, `template/prelude.c.tmpl`, `vm.c` and `vm_eval.c`
  - Follow-up due to the above changes
- `imemo.{c|h}`
  - If an object with `imemo_ast` appears, considers it a bug

Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
2024-04-26 11:21:08 +09:00
1984f9aedc Specify Kernel#autoload? uses current namespace
Because Kernel#autoload? uses the current namespace, it can lead to
potentially confusing results. We should make it clearer that modules
count as separate namespaces to lookup in.

Co-authored-by: Jeremy Evans <code@jeremyevans.net>
Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
2024-04-15 10:55:14 -07:00