Commit graph

9327 commits

Author SHA1 Message Date
Nobuyoshi Nakada
b7bca0ebdc
win32/sspi: Fix Win32::SSPI::SSPIResult#== with Integer
The values of `@@map` are `Symbol`s and `@value` should be an
`Integer` since unpacked as unsigned long, so this comparison should
be false always.  Probably comparison with `Symbol` was intended.
2024-09-29 21:00:26 +09:00
Nobuyoshi Nakada
1179c86384
win32/sspi: Already Fixnum has been removed years ago 2024-09-29 21:00:25 +09:00
Nobuyoshi Nakada
a3fd24e22e [ruby/digest] Import patches for old macOS from MacPorts
07a5db2f77
2024-09-26 04:56:36 +00:00
Peter Zhu
407f8b8716 Fix memory leak in Ripper for indented heredocs
The allocated parser string is never freed, which causes a memory leak.

The following code leaks memory:

    Ripper.sexp_raw(DATA.read)

    __END__
    <<~EOF
      a
        #{1}
      a
    EOF
2024-09-25 08:56:14 -04:00
Josh Cooper
0d16c36d0a [win32/registry] Fallback to UTF-8 for unknown codepages
There are some codepages like cp708 for which no ruby encoding exists:

    $ ruby -e "Encoding.find('cp708')"
    Traceback (most recent call last):
    	1: from -e:1:in `<main>'
    -e:1:in `find': unknown encoding name - cp708 (ArgumentError)

win32/registry uses ENCODING to transcode error messages and expand environment
variables from UTF-16LE, so using UTF-8 seems like the best choice and is better
than a hard failure.

This should resolve [Bug #13831]
2024-09-25 10:43:31 +09:00
Jean Boussier
d31378dc91 [ruby/psych] Use String#match? over String#=~ when applicable
Save on allocating useless `MatchData` instances.

b2d9f16e58
2024-09-24 19:25:40 +00:00
S-H-GAMELINKS
95d26ee41e Reuse dedent_string function in rb_ruby_ripper_dedent_string function
This change is reduce Ruby C API dependency for Universal Parser.
Reuse dedent_string functions in rb_ruby_ripper_dedent_string functions and remove dependencies on rb_str_modify and rb_str_set_len from the parser.
2024-09-22 12:22:20 +09:00
NAITOH Jun
d81b0588bb
[ruby/strscan] Accept String as a pattern at non head
(https://github.com/ruby/strscan/pull/106)

It supports non-head match cases such as StringScanner#scan_until.

If we use a String as a pattern, we can improve match performance.
Here is a result of the including benchmark.

## CRuby

It shows String as a pattern is 1.18x faster than Regexp as a pattern.

```
$ benchmark-driver benchmark/check_until.yaml
Warming up --------------------------------------
              regexp     9.403M i/s -      9.548M times in 1.015459s (106.35ns/i)
          regexp_var     9.162M i/s -      9.248M times in 1.009479s (109.15ns/i)
              string     8.966M i/s -      9.274M times in 1.034343s (111.54ns/i)
          string_var    11.051M i/s -     11.190M times in 1.012538s (90.49ns/i)
Calculating -------------------------------------
              regexp    10.319M i/s -     28.209M times in 2.733707s (96.91ns/i)
          regexp_var    10.032M i/s -     27.485M times in 2.739807s (99.68ns/i)
              string     9.681M i/s -     26.897M times in 2.778397s (103.30ns/i)
          string_var    12.162M i/s -     33.154M times in 2.726046s (82.22ns/i)

Comparison:
          string_var:  12161920.6 i/s
              regexp:  10318949.7 i/s - 1.18x  slower
          regexp_var:  10031617.6 i/s - 1.21x  slower
              string:   9680843.7 i/s - 1.26x  slower
```

## JRuby

It shows String as a pattern is 2.11x faster than Regexp as a pattern.

```
$ benchmark-driver benchmark/check_until.yaml
Warming up --------------------------------------
              regexp     7.591M i/s -      7.544M times in 0.993780s (131.74ns/i)
          regexp_var     6.143M i/s -      6.125M times in 0.997038s (162.77ns/i)
              string    14.135M i/s -     14.079M times in 0.996067s (70.75ns/i)
          string_var    14.079M i/s -     14.057M times in 0.998420s (71.03ns/i)
Calculating -------------------------------------
              regexp     9.409M i/s -     22.773M times in 2.420268s (106.28ns/i)
          regexp_var    10.116M i/s -     18.430M times in 1.821820s (98.85ns/i)
              string    21.389M i/s -     42.404M times in 1.982519s (46.75ns/i)
          string_var    20.897M i/s -     42.237M times in 2.021187s (47.85ns/i)

Comparison:
              string:  21389191.1 i/s
          string_var:  20897327.5 i/s - 1.02x  slower
          regexp_var:  10116464.7 i/s - 2.11x  slower
              regexp:   9409222.3 i/s - 2.27x  slower
```

See:
be7815ec02/core/src/main/java/org/jruby/util/StringSupport.java (L1706-L1736)

---------

f9d96c446a

Co-authored-by: Sutou Kouhei <kou@clear-code.com>
2024-09-17 15:12:25 +09:00
Yusuke Endoh
0f3dc2f958 Prevent warnings "the block passed to ... may be ignored" 2024-09-13 16:52:38 +09:00
John Meade
efc77d535b [ruby/psych] Ensure strings with only underscores are not processed as Integer
A string similar to "0x____" should be treated as a string.
Currently it is processed as an Integer.

This alters the regex specified by http://yaml.org/type/int.html
to ensure at least one numerical symbol is present in the string
before converting to Integer.

81479b203e
2024-09-11 17:19:18 +00:00
Ivan Kuchin
6c16598a72 [ruby/pathname] use delete_prefix instead of sub in find method
delete_prefix with a string is easier to read than a regular expression
also it should be faster. It is available since ruby 2.5 and the gem requires
ruby 2.7.

0070f43f19
2024-09-11 04:49:08 +00:00
Nobuyoshi Nakada
f622548800 [ruby/resolv] Add spec extensions
3189d16b69
2024-09-10 08:33:32 +00:00
Hiroshi SHIBATA
2d4fdafa64 [ruby/psych] Bump up 5.2.0.beta1
a8b73bb80e
2024-09-09 06:46:51 +00:00
Juanjo Bazán
74872109be [ruby/psych] Unlimited line_width with -1
3b63a93dfc
2024-09-09 06:44:40 +00:00
Hiroshi SHIBATA
dbfabafe96 [ruby/psych] Make to load stringio lazily
9f5392d180
2024-09-09 06:43:35 +00:00
Gareth Jones
aed8e46118 [ruby/psych] docs: specify correct default fallback value
ce7946981d
2024-09-09 06:40:59 +00:00
Nobuyoshi Nakada
37712c1ed6
Prefer constants for shutdown over magic numbers 2024-09-06 21:58:01 +09:00
Nobuyoshi Nakada
7387a09791 [ruby/digest] Suppress md5 deprecation warnings by gcc as well as clang
76878e3201
2024-09-06 05:58:21 +00:00
Jean Boussier
63cbe3f6ac Proof of Concept: Allow to prevent fork from happening in known fork unsafe API
[Feature #20590]

For better of for worse, fork(2) remain the primary provider of
parallelism in Ruby programs. Even though it's frowned uppon in
many circles, and a lot of literature will simply state that only
async-signal safe APIs are safe to use after `fork()`, in practice
most APIs work well as long as you are careful about not forking
while another thread is holding a pthread mutex.

One of the APIs that is known cause fork safety issues is `getaddrinfo`.
If you fork while another thread is inside `getaddrinfo`, a mutex
may be left locked in the child, with no way to unlock it.

I think we could reduce the impact of these problem by preventing
in for the most notorious and common cases, by locking around
`fork(2)` and known unsafe APIs with a read-write lock.
2024-09-05 11:43:46 +02:00
Jean Boussier
d612f9fd34 [flori/json] Remove outdated ifdef checks
`json` requires Ruby 2.3, so `HAVE_RUBY_ENCODING_H` and `HAVE_RB_ENC_RAISE`
are always true.

5c8dc6b70a
2024-09-03 11:51:51 +09:00
Nobuyoshi Nakada
185602e696 [ruby/io-console] Fix mixing declarations and code for older versions
504292b487
2024-09-01 03:46:44 +00:00
Nobuyoshi Nakada
37db194c02 [ruby/io-console] Support older rubies
cad8169568
2024-09-01 03:46:43 +00:00
Nobuyoshi Nakada
afd12873e0
Update dependencies of io-console 2024-09-01 12:00:12 +09:00
Nobuyoshi Nakada
e69945fc57 [ruby/io-console] Workaround for old TruffleRuby
f10c946ac7
2024-08-31 08:43:59 +00:00
Nobuyoshi Nakada
4a1ea9b63a [ruby/io-console] Store console IO in Ractor-local storage
Ractor requires a shareable class has shareable constants only, but IO
is not shareable unless frozen.

65e0ff895c
2024-08-31 08:43:59 +00:00
Nobuyoshi Nakada
365ededfdf [ruby/io-console] io-console is considered Ractor-safe
b1adc15af7
2024-08-31 05:46:05 +00:00
Hiroshi SHIBATA
32f134bb85
Added pre-release suffix for development version of default gems
https://github.com/ruby/stringio/issues/81
2024-08-31 14:22:17 +09:00
Takashi Kokubun
818e3037ed [ruby/zlib] Bump up 3.1.1
d756bb0a0f
2024-08-31 05:04:30 +00:00
Hiroshi SHIBATA
c48e5959de
[ruby/fiddle] Removed libffi patchs for old Ruby
(https://github.com/ruby/fiddle/pull/143)

Pick
92865d8760
from `ruby/ruby` repo.

---------

aad5a3bc79

Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
2024-08-23 11:44:38 +09:00
Jeremy Evans
a3f5a043fa Handle getlogin failure in PTY.spawn
getlogin is only called if USER environment variable is not set,
but if getlogin returns NULL in that case, then do not call
getpwnam, and assume /bin/sh as shell.

Mentioned in comment to bug 20586.
2024-08-22 11:20:47 +09:00
KJ Tsanaktsidis
927a44b43f Rewrite #test_redefinition_mismatch to use a dedicated test class
This test is checking what happens if you try and define a class in a C
extension where that constant is already not a class. It was doing this
by overriding ::Date and then trying to require 'date. The issue with
this is that if we ever add 'date' as a dependency for the test runner,
this test will break because the test runner files get implicitly
required in an `assert_separately` block.

Better use an explicit class for this purpose which can't be accidentally
required elsewhere.
2024-08-20 18:36:16 +09:00
Peter Zhu
568d7ab7f5 Fix memory leak reported in -test-/random/loop.c
RUBY_TYPED_DEFAULT_FREE will only free the rand_loop_t, but it will cause
the buf to be leaked. This commit fixes the memory leak by implementing
a free function for the rand_loop_t type.
2024-08-12 09:33:20 -04:00
Nobuyoshi Nakada
1db8586279
Show mkmf.log when failed 2024-08-11 15:57:56 +09:00
Peter Zhu
7b7dde37f5 [ruby/psych] Guard from memory leak in Psych::Emitter#start_document
When an exception is raised, it can leak memory in `head`. There are two
places that can leak memory:

1. `Check_Type(tuple, T_ARRAY)` can leak memory if `tuple` is not an
   array.
2. `StringValue(name)` and `StringValue(value)` if they are not strings
   and the call to `to_str` does not return a string.

This commit fixes these memory leaks by wrapping the code around a
rb_ensure so that the memory is freed in all cases.

The following code demonstrates the memory leak:

    emitter = Psych::Emitter.new(StringIO.new)
    nil_to_string_tags = [[nil, "tag:TALOS"]] + ([1] * 1000)
    expected_array_tags = [1] * 1000

    10.times do
      1_000.times do
        # Raises `no implicit conversion of nil into String`
        emitter.start_document([], nil_to_string_tags, 0)
      rescue TypeError
      end

      1_000.times do
        # Raises `wrong argument type Integer (expected Array)`
        emitter.start_document([], expected_array_tags, 0)
      rescue TypeError
      end

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

Before:

    47248
    79728
    111968
    144224
    176480
    208896
    241104
    273280
    305472
    337664

After:

    14832
    15088
    15344
    15344
    15360
    15632
    15632
    15632
    15648
    15648

053af73818
2024-08-09 20:28:53 +00:00
Peter Zhu
712ac99e4d [ruby/psych] Convert missed tabs to spaces in C files
74a6b4d226
2024-08-09 19:35:28 +00:00
Peter Zhu
e63a2115f6 [ruby/psych] Convert tabs to spaces in C files
e7d64c9848
2024-08-09 18:29:46 +00:00
Nobuyoshi Nakada
12a5400a88 [ruby/io-console] Remove no longer used variable
651797ff8a
2024-07-31 13:20:01 +00:00
Nobuyoshi Nakada
92865d8760 Remove files to build libffi in mswin
These files were to build libffi from the bundled source, but are no
longer used since we stopped bundling the libffi sources in commit
e4f5296f06.

The gemspec file is unchanged because fiddle gem itself still supports
ruby 2.5.
2024-07-31 11:19:52 +09:00
Satoshi Tagomori
19ec803179 Reset the counter for two consecutive runs 2024-07-31 10:59:51 +09:00
Satoshi Tagomori
50a0552bd7 Fix test code and extension to avoid using gvars and Kernel methods 2024-07-30 15:31:24 +09:00
Satoshi Tagomori
c884db0b5b [BUG #20655] Add tests to use rb_ensure and call cont.call 2024-07-30 15:31:24 +09:00
Misaki Shioi
b3baa11ee9
Improve Socket.tcp (#11187)
[Feature #20646]Improve Socket.tcp

This is a proposed improvement to `Socket.tcp`, which has implemented Happy Eyeballs version 2 (RFC8305) in PR9374.

1. Background
I implemented Happy Eyeballs version 2 (HEv2) for Socket.tcp in PR9374, but several issues have been identified:

- `IO.select` waits for name resolution or connection establishment in v46w, but it does not consider the case where both events occur simultaneously when it returns a value.
  - In this case, Socket.tcp can only capture one event and needs to execute an unnecessary loop to capture the other one, calling `IO.select` one extra time.
- `IO.select` waits for both IPv6/IPv4 name resolution (in start), but when it returns a value, it doesn't consider the case where name resolution for both address families is complete.
  - In this case, `Socket.tcp` can only obtain the addresses of one address family and needs to execute an unnecessary loop obtain the other addresses, calling `IO.select` one extra time.
- The consideration for `connect_timeout` was insufficient. After initiating one or more connections, it raises a 'user specified timeout' after the `connect_timeout` period even if there were addresses that have been resolved and have not yet tried to connect.
- It does not retry with another address in case of a connection failure.
- It executes unnecessary state transitions even when an IP address is passed as the `host` argument.
- The regex for IP addresses did not correctly specify the start and end.

2. Proposal & Outcome
To overcome the aforementioned issues, this PR introduces the following changes:

- Previously, each loop iteration represented a single state transition. This has been changed to execute all processes that meet the execution conditions within a single loop iteration.
  - This prevents unnecessary repeated loops and calling `IO.select`
- Introduced logic to determine the timeout value set for `IO.select`. During the Resolution Delay and Connection Attempt Delay, the user-specified timeout is ignored. Otherwise, the timeout value is set to the larger of `resolv_timeout` and `connect_timeout`.
  - This ensures that the `connect_timeout` is only detected after attempting to connect to all resolved addresses.
- Retry with another address in case of a connection failure.
  - This prevents unnecessary repeated loops upon connection failure.
- Call `tcp_without_fast_fallback` when an IP address is passed as the host argument.
  - This prevents unnecessary state transitions when an IP address is passed.
- Fixed regex for IP addresses.

Additionally, the code has been reduced by over 100 lines, and redundancy has been minimized, which is expected to improve readability.

3. Performance
No significant performance changes were observed in the happy case before and after the improvement.
However, improvements in state transition deficiencies are expected to enhance performance in edge cases.

```ruby
require 'socket'
require 'benchmark'

Benchmark.bmbm do |x|
  x.report('fast_fallback: true') do
    30.times { Socket.tcp("www.ruby-lang.org", 80) }
  end

  x.report('fast_fallback: false') do # Ruby3.3時点と同じ
    30.times { Socket.tcp("www.ruby-lang.org", 80, fast_fallback: false) }
  end
end
```

Before:

```
~/s/build ❯❯❯ ../install/bin/ruby ../ruby/test.rb

                           user     system      total        real
fast_fallback: true    0.021315   0.040723   0.062038 (  0.504866)
fast_fallback: false   0.007553   0.026248   0.033801 (  0.533211)
```

After:

```
~/s/build ❯❯❯ ../install/bin/ruby ../ruby/test.rb

                           user     system      total        real
fast_fallback: true    0.023081   0.040525   0.063606 (  0.406219)
fast_fallback: false   0.007302   0.025515   0.032817 (  0.418680)
```
2024-07-30 12:58:31 +09:00
Samuel Giddins
0d3ce31234 [ruby/openssl] Set time directly on the x509 store
(https://github.com/ruby/openssl/pull/770)

Instead of an ivar, so other ossl functions that take a store will use the correct time when verifying

21aadc66ae
2024-07-24 16:51:00 +00:00
Kazuki Yamaguchi
1388945f0d [ruby/openssl] asn1: make ossl_asn1_get_asn1type() private
The function is not used anywhere outside of ossl_asn1.c.

5392b79941
2024-07-24 16:50:01 +00:00
Kazuki Yamaguchi
a1cf39bd36 [ruby/openssl] x509attr: avoid using OpenSSL::ASN1 internals in #value=
OpenSSL::ASN1 is being rewritten in Ruby. To make it easier, let's
remove dependency to the instance variables and the internal-use
function ossl_asn1_get_asn1type() outside OpenSSL::ASN1.

This also fixes the insufficient validation of the passed value with
its tagging.

35a157462e
2024-07-24 16:50:01 +00:00
Nobuyoshi Nakada
97449338d6 [Bug #20649] Allow nil as 2nd argument of assign_error
Fallback to the last token element in that case, for the backward
compatibilities.
2024-07-24 22:18:36 +09:00
Jean Boussier
30f2d69825 Don't call Kernel#require in hot loop
Ref: https://bugs.ruby-lang.org/issues/20641

Even without the reference bug, `require 'date'` isn't cheap.

```ruby

require "benchmark/ips"
require "yaml"
require "date"

100.times do |i|
  $LOAD_PATH.unshift("/tmp/does/not/exist/#{i}")
end
payload = 100.times.map { Date.today }.to_yaml

Benchmark.ips do |x|
  x.report("100 dates") { YAML.unsafe_load(payload) }
end
```

Before:
```
$ ruby /tmp/bench-yaml.rb
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin22]
Warming up --------------------------------------
           100 dates   416.000 i/100ms
Calculating -------------------------------------
           100 dates      4.309k (± 1.2%) i/s -     21.632k in   5.021003s
```

After:
```
$ ruby -Ilib /tmp/bench-yaml.rb
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin22]
Warming up --------------------------------------
           100 dates   601.000 i/100ms
Calculating -------------------------------------
           100 dates      5.993k (± 1.8%) i/s -     30.050k in   5.016079s
```
2024-07-19 20:37:20 +00:00
Nobuyoshi Nakada
94b87084a6 [ruby/win32ole] win32ole.c: repeated code
* ext/win32ole/win32ole.c (ole_variant2val): reduce repeated code
  between byref and byval.

e753c6abdd
2024-07-12 09:45:06 +00:00
Ivo Anjo
ef563a696d Minor: Fix typo in bug name
This confused me for a few minutes -- the testcase for
https://bugs.ruby-lang.org/issues/14834 was mistyped in the file name,
as well as once in the source.

E.g. in some cases it was
`one-four-three-eight-four` instead of
`one-four-eight-three-four`.
2024-07-11 15:18:40 -04:00
Peter Zhu
51bd816517 [Feature #20470] Split GC into gc_impl.c
This commit splits gc.c into two files:

- gc.c now only contains code not specific to Ruby GC. This includes
  code to mark objects (which the GC implementation may choose not to
  use) and wrappers for internal APIs that the implementation may need
  to use (e.g. locking the VM).

- gc_impl.c now contains the implementation of Ruby's GC. This includes
  marking, sweeping, compaction, and statistics. Most importantly,
  gc_impl.c only uses public APIs in Ruby and a limited set of functions
  exposed in gc.c. This allows us to build gc_impl.c independently of
  Ruby and plug Ruby's GC into itself.
2024-07-03 09:03:40 -04:00