Commit graph

9132 commits

Author SHA1 Message Date
Nobuyoshi Nakada
e90b447655
[Bug #20924] Fix reading with delimiter in wide character encodings 2024-12-10 23:57:57 +09:00
Kouhei Yanagita
3422bfcab6 [Bug #20936] Fix #size for Range#reverse_each 2024-12-10 23:12:27 +09:00
Nobuyoshi Nakada
1c15f641cc [Bug #20929] Win32: Encode timezone name in UTF-8 2024-12-06 18:59:38 +09:00
Nobuyoshi Nakada
78762b5218 [Bug #20929] Fix assert_zone_encoding
The default internal encoding is not taken into account to encode
timezone name.
2024-12-06 18:59:38 +09:00
Peter Zhu
ce1ad1b816 Standardize on the name "modular GC"
We have name fragmentation for this feature, including "shared GC",
"modular GC", and "external GC". This commit standardizes the feature
name to "modular GC" and the implementation to "GC library".
2024-12-05 10:33:26 -05:00
John Hawthorn
f1dda5ed01 Warn when redefining __id__ as well as object_id
[Feature #20912]
2024-11-29 20:41:00 -08:00
viralpraxis
660b995365 [Bug #20915] Fix SEGV with TracePoint#parameters and aliased C method
The following snippet results with a SEGV:

```ruby
C = Class.new do
  alias_method :new_to_s, :to_s
end

TracePoint.new(:c_call, &:parameters).enable { C.new.new_to_s }
```

at MRI 3.3.6 and ruby 3.4.0dev

The root cause of the issue lies in the `rb_tracearg_parameters` function
within the `RUBY_EVENT_C_RETURN` branch. Specifically, when the invoked
method is an alias for a C function,
`rb_method_entry_without_refinements(..., trace_arg->called_id, ...)`
may return NULL. In that case we can fallback to `trace_arg->id`.
2024-11-29 18:42:48 -05:00
Yusuke Endoh
50a34637a4 Avoid illegal pointer
When loading a crafted marshal data of Random, a pointer to an illegal
address was created. I don't think there is any harm since the data is
normalized before access, but just to be safe, I add a check to make it
an error.
2024-11-29 03:01:46 +09:00
Randy Stauner
1dd40ec18a
Optimize instructions when creating an array just to call include? (#12123)
* Add opt_duparray_send insn to skip the allocation on `#include?`

If the method isn't going to modify the array we don't need to copy it.
This avoids the allocation / array copy for things like `[:a, :b].include?(x)`.

This adds a BOP for include? and tracks redefinition for it on Array.

Co-authored-by: Andrew Novoselac <andrew.novoselac@shopify.com>

* YJIT: Implement opt_duparray_send include_p

Co-authored-by: Andrew Novoselac <andrew.novoselac@shopify.com>

* Update opt_newarray_send to support simple forms of include?(arg)

Similar to opt_duparray_send but for non-static arrays.

* YJIT: Implement opt_newarray_send include_p

---------

Co-authored-by: Andrew Novoselac <andrew.novoselac@shopify.com>
2024-11-26 14:31:08 -05:00
Nobuyoshi Nakada
6b4f8945d6 Many of Oniguruma functions need valid encoding strings 2024-11-26 11:46:34 +09:00
Nobuyoshi Nakada
02b70256b5 Check negative integer underflow 2024-11-26 11:46:34 +09:00
Yusuke Endoh
f78f1e927f Prevent a warning: setting Encoding.default_external 2024-11-25 15:45:00 +09:00
Jeremy Evans
3b7892b6e4 Fix a bug in rb_include_module that stops nested inclusion into module subclasses
This bug was present since the code was originally added by me
in 3556a834a2.

Fixes [Bug #20871]
2024-11-20 07:59:31 -08:00
Shugo Maeda
7ed027a833 Remove deprecated method Refinement#refined_class
[Feature #20901]
2024-11-19 14:33:38 +09:00
Nobuyoshi Nakada
9c777f282f
[Bug #20900] Warn deprecated constant when removing 2024-11-19 12:43:38 +09:00
Matt Valentine-House
42501015b4 rb_raise when attempting to set the GC implementation name
Instead of silently ignoring the key, we should raise to clearly tell
the user that this key is read-only.
2024-11-14 10:46:36 +00:00
Matt Valentine-House
ee290c94a3 Include the currently active GC in RUBY_DESCRIPTION
This will add +MOD_GC to the version string and Ruby description when
Ruby is compiled with shared gc support.

When shared GC support is compiled in and a GC module has been loaded
using RUBY_GC_LIBRARY, the version string will include the name of
the currently active GC as reported by the rb_gc_active_gc_name function
in the form

+MOD_GC[gc_name]

[Feature #20794]
2024-11-14 10:46:36 +00:00
Matt Valentine-House
fa10441981 Expose GC.config[:implementation], to query the currently active GC
And a default and readonly key to the GC.config hash that names the
current GC implementation.

This is provided by each implementation by the API function
rb_gc_impl_active_gc_name
2024-11-14 10:46:36 +00:00
Takashi Kokubun
01db456196 Move Array#map to Ruby
Co-Authored-By: Aaron Patterson <tenderlove@ruby-lang.org>
2024-11-13 12:27:26 -08:00
John Hawthorn
8409edc497 Fix regex timeout double-free after stack_double
As of 10574857ce, it's possible to crash
on a double free due to `stk_alloc` AKA `msa->stack_p` being freed
twice, once at the end of match_at and a second time in `FREE_MATCH_ARG`
in the parent caller.

Fixes [Bug #20886]
2024-11-11 23:33:21 -08:00
KJ Tsanaktsidis
2694585fb3 Fix race condition in test_self_stop
This test was relying on a sleep to synchronise the parent and child
processes. By having the child be the process that stops itself with
SIGSTOP, instead of the parent, we can actually properly wait for that
using waitpid2 and be notified of the stop.

This use of sleep to synchronise processes is potentially flaky and
caused failures under rr's `--chaos` mode.
2024-11-08 22:13:15 -07:00
John Hawthorn
1f6dd9071c Fix update_coderange for binary strings
Although a binary (aka ASCII-8BIT) string will never have a broken
coderange, it still has to differentiate between "valid" and "7bit".

On Ruby 3.4/trunk this problem is masked because we now clear the
coderange more agressively in rb_str_resize, and we happened to always
be strinking this string, but we should not assume that.

On Ruby 3.3 this created strings where `ascii_only?` was true in cases
it shouldn't be as well as other problems.

Fixes [Bug #20883]

Co-authored-by: Daniel Colson <danieljamescolson@gmail.com>
Co-authored-by: Matthew Draper <matthew@trebex.net>
2024-11-08 17:39:19 -08: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
Yusuke Endoh
fde6d189b2 Avoid calculating large powers
... for slow CI machines like macOS.
2024-11-08 20:53:21 +09:00
Yusuke Endoh
45cd4a8296 Do not round a**b to infinity
... instead, just calculate the value unless it is too big.
Also, this change raises an ArgumentError if it is expected to exceed
16 GB in a 64-bit environment.

(It is possible to calculate it straightforward, but it would likely be
out-of-memory, so I didn't think it would make sense.)

[Feature #20811]
2024-11-08 19:48:56 +09:00
Nobuyoshi Nakada
ee29aade1a
[Bug #20869] Discard read buffer and encoding converters at seeking 2024-11-07 21:03:37 +09:00
Nobuyoshi Nakada
1701ddea73
[Bug #20802] Copy encodings at reopen as well 2024-11-07 19:33:41 +09:00
Yusuke Endoh
f87ec81e08 Prevent method redefinition warnings in test 2024-11-07 13:18:57 +09:00
Kasumi Hanazuki
35bf660337
io_buffer.c: Allow copies between overlapping buffers with #copy and #set_string (#11640)
The current implementation of `IO::Buffer#copy` and `#set_string` has
an undefined behavior when the source and destination memory overlaps,
due to the underlying use of the `memcpy` C function.

This patch guarantees the methods to be safe even when copying between
overlapping buffers by replacing `memcpy` with `memmove`,

Fixes: [Bug #20745]
2024-11-06 20:55:39 +00:00
Peter Zhu
56ecc243e2 [Bug #20868] Fix Method#hash to not change after compaction
The hash value of a Method must remain constant after a compaction, otherwise
it may not work as the key in a hash table.

For example:

    def a; end

    # Need this method here because otherwise the iseq may be on the C stack
    # which would get pinned and not move during compaction
    def get_hash
      method(:a).hash
    end

    puts get_hash # => 2993401401091578131

    GC.verify_compaction_references(expand_heap: true, toward: :empty)

    puts get_hash # => -2162775864511574135
2024-11-06 10:34:20 -05:00
KJ Tsanaktsidis
96e695ad00 Fix flakiness in TestGc#test_thrashing_for_young_objects
I caught a reproduction of this test failing under rr, and was able to
replay it to isolate the failure. The call to
`before_stat_heap = GC.stat_heap` is itself allocating a hash, which in
unlucky circumstances can result in a new page being allocated and thus
`before_stats[:heap_allocated_pages]` no longer equals
`after_stats[:heap_allocated_pages]`.

The solution is to use the form of GC.stat/stat_heap which takes a hash
as an argument, and thus needs to perform no Ruby allocations itself.
2024-11-06 09:58:47 -05:00
Hiroshi SHIBATA
a2dd471d9e
macOS 15 is the stable version today 2024-11-06 15:45:32 +09:00
Nobuyoshi Nakada
d71be7274b
[Bug #20873] Consider -FIXNUM_MIN overflow
`-FIXNUM_MIN` is usually greater than `FIXNUM_MAX` on platforms using
two's complement representation.
2024-11-06 13:31:38 +09:00
Koichi Sasada
ab7ab9e450 Warning[:strict_unused_block]
to show unused block warning strictly.

```ruby
class C
  def f = nil
end

class D
  def f = yield
end

[C.new, D.new].each{|obj| obj.f{}}
```

In this case, `D#f` accepts a block. However `C#f` doesn't
accept a block. There are some cases passing a block with
`obj.f{}` where `obj` is `C` or `D`. To avoid warnings on
such cases, "unused block warning" will be warned only if
there is not same name which accepts a block.
On the above example, `C.new.f{}` doesn't show any warnings
because there is a same name `D#f` which accepts a block.

We call this default behavior as "relax mode".

`strict_unused_block` new warning category changes from
"relax mode" to "strict mode", we don't check same name
methods and `C.new.f{}` will be warned.

[Feature #15554]
2024-11-06 11:06:18 +09:00
Takashi Kokubun
478e0fc710
YJIT: Replace Array#each only when YJIT is enabled (#11955)
* YJIT: Replace Array#each only when YJIT is enabled

* Add comments about BUILTIN_ATTR_C_TRACE

* Make Ruby Array#each available with --yjit as well

* Fix all paths that expect a C location

* Use method_basic_definition_p to detect patches

* Copy a comment about C_TRACE flag to compilers

* Rephrase a comment about add_yjit_hook

* Give METHOD_ENTRY_BASIC flag to Array#each

* Add --yjit-c-builtin option

* Allow inconsistent source_location in test-spec

* Refactor a check of BUILTIN_ATTR_C_TRACE

* Set METHOD_ENTRY_BASIC without touching vm->running
2024-11-04 11:14:28 -05:00
Nobuyoshi Nakada
4dc120cb7f
Fix Dir.mktmpdir argument
The temporary directory is the second argument.
2024-11-04 23:32:25 +09:00
Peter Zhu
3e2ee99057 Skip TestProc#test_hash_does_not_change_after_compaction if compaction is not supported 2024-11-02 17:25:16 -04:00
Peter Zhu
c7708d22c3 Improve coverage of TestProc#test_hash_uniqueness 2024-11-01 10:49:50 -04:00
Peter Zhu
813286762c Add TestProc#test_hash_equal 2024-11-01 10:49:50 -04:00
Peter Zhu
53b3fac6d2 Rename test_hash to test_hash_uniqueness 2024-11-01 10:49:50 -04:00
Peter Zhu
29c480dd6f [Bug #20853] Fix Proc#hash to not change after compaction
The hash value of a Proc must remain constant after a compaction, otherwise
it may not work as the key in a hash table.
2024-11-01 10:49:50 -04:00
Koichi Sasada
783dde2159 alias should not set defined_class for Modules
`me->defined_class` should be 0 for method entries of
Modules.

This patch checks this condition
and fix https://github.com/ruby/ruby/pull/11965#issuecomment-2448291790
2024-11-01 11:50:00 +09:00
Peter Zhu
843b4f49ee Fix assertion when envval of proc is Qundef
The following code crashes with assertions enabled because envval could
be Qundef:

    {}.to_proc.dup
2024-10-31 13:52:24 -04:00
Peter Zhu
66afde9cea Fix indentation in TestProc#test_hash [ci skip] 2024-10-31 10:58:30 -04:00
Étienne Barrié
257f78fb67 Show where mutated chilled strings were allocated
[Feature #20205]

The warning now suggests running with --debug-frozen-string-literal:

```
test.rb:3: warning: literal string will be frozen in the future (run with --debug-frozen-string-literal for more information)
```

When using --debug-frozen-string-literal, the location where the string
was created is shown:

```
test.rb:3: warning: literal string will be frozen in the future
test.rb:1: info: the string was created here
```

When resurrecting strings and debug mode is not enabled, the overhead is a simple FL_TEST_RAW.
When mutating chilled strings and deprecation warnings are not enabled,
the overhead is a simple warning category enabled check.

Co-authored-by: Jean Boussier <byroot@ruby-lang.org>
Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
Co-authored-by: Jean Boussier <byroot@ruby-lang.org>
2024-10-21 12:33:02 +02:00
Peter Zhu
90aa6aefc4 Fix memory leak in syntax error in prism
If there is a syntax error, there could be an ast_node in the result.
This could get leaked if there is a syntax error so parsing could not
complete (parsed is not set to true).

For example, the following script leaks memory:

    10.times do
      10_000.times do
        eval("def foo(...) super(...) {}; end")
      rescue SyntaxError
      end

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

Before:

    31328
    42768
    53856
    65120
    76208
    86768
    97856
    109120
    120208
    131296

After:

    20944
    20944
    20944
    20944
    20944
    20944
    20944
    20944
    20944
    20944
2024-10-16 14:52:46 -04:00
Kevin Newton
5eca11ca5e RubyVM::InstructionSequence.of Thread::Backtrace::Location
This would be useful for debugging.
2024-10-16 14:31:26 -04:00
Peter Zhu
ebfe615a0c Skip GC compaction tests in test_yjit.rb when not supported 2024-10-16 10:17:54 -04:00
Peter Zhu
3da3cabf98 Remove a puts in test_finalizer_thread_raise 2024-10-15 09:10:35 -04:00
Nobuyoshi Nakada
9611c619ac
[Bug #20797] Check seconds in UTC offset as well as minutes 2024-10-14 13:55:55 +09:00