Commit graph

20692 commits

Author SHA1 Message Date
Peter Zhu
7464514ca5 Fix memory leak in String#start_with? when regexp times out
[Bug #20653]

This commit refactors how Onigmo handles timeout. Instead of raising a
timeout error, onig_search will return a ONIGERR_TIMEOUT which the
caller can free memory, and then raise a timeout error.

This fixes a memory leak in String#start_with when the regexp times out.
For example:

    regex = Regexp.new("^#{"(a*)" * 10_000}x$", timeout: 0.000001)
    str = "a" * 1000000 + "x"

    10.times do
      100.times do
        str.start_with?(regex)
      rescue
      end

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

Before:

    33216
    51936
    71152
    81728
    97152
    103248
    120384
    133392
    133520
    133616

After:

    14912
    15376
    15824
    15824
    16128
    16128
    16144
    16144
    16160
    16160
2024-07-26 08:42:38 -04:00
Hiroshi SHIBATA
83a99bdbe6 [ruby/net-http] Removed needless NullWriter class
ddb2a81aed
2024-07-26 01:45:02 +00:00
Peter Zhu
54ff3d6996 Fix test_kwarg_eval_memory_leak
Hash.new no longer takes keyword arguments except for capacity, so we
should just use a method that takes keyword arguments instead.
2024-07-25 15:25:37 -04:00
Kevin Newton
24770c30f8 [ruby/prism] Sync from ruby/ruby
280517c325
2024-07-25 18:59:10 +00:00
Peter Zhu
10574857ce Fix memory leak in Regexp capture group when timeout
[Bug #20650]

The capture group allocates memory that is leaked when it times out.

For example:

    re = Regexp.new("^#{"(a*)" * 10_000}x$", timeout: 0.000001)
    str = "a" * 1000000 + "x"

    10.times do
      100.times do
        re =~ str
      rescue Regexp::TimeoutError
      end

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

Before:

    34688
    56416
    78288
    100368
    120784
    140704
    161904
    183568
    204320
    224800

After:

    16288
    16288
    16880
    16896
    16912
    16928
    16944
    17184
    17184
    17200
2024-07-25 09:23:49 -04:00
Yusuke Endoh
5f6a1c2e22 Omit TestFile#test_stat when the machine is stupidly slow
GitHub Actions macos-14 machine is stupid.

https://app.launchableinc.com/organizations/ruby/workspaces/ruby/data/test-paths/file%3Dtest%2Fruby%2Ftest_file.rb%23%23%23class%3DTestFile%23%23%23testcase%3Dtest_stat?organizationId=ruby&workspaceId=ruby&testPathId=file%3Dtest%2Fruby%2Ftest_file.rb%23%23%23class%3DTestFile%23%23%23testcase%3Dtest_stat&testSessionStatus=flake
2024-07-25 13:47:51 +09: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
Nobuyoshi Nakada
e642ddf7ae
[Bug #20647] Disallow return directly within a singleton class 2024-07-24 14:44:32 +09:00
Kevin Newton
cb863907d8 [ruby/prism] Single line method bodies should not be marked as newline
18a8597276
2024-07-23 18:44:31 +00:00
yui-knk
57b11be15a Implement UNLESS NODE keyword locations 2024-07-23 14:35:23 +09:00
yui-knk
f23485a8d6 [Feature #20624] Enhance RubyVM::AbstractSyntaxTree::Node#locations
This commit introduce `RubyVM::AbstractSyntaxTree::Node#locations` method
and `RubyVM::AbstractSyntaxTree::Location` class.

Ruby AST node will hold multiple locations information.
`RubyVM::AbstractSyntaxTree::Node#locations` provides a way to access
these locations information.

`RubyVM::AbstractSyntaxTree::Location` is a class which holds these location information:

* `#first_lineno`
* `#first_column`
* `#last_lineno`
* `#last_column`
2024-07-23 12:36:00 +09:00
Kevin Newton
2effa98b6f [ruby/prism] Implement mismatched indentation warning
5d5bf92be8
2024-07-22 18:57:10 +00:00
Jean Boussier
82aee1a946 bundled_gems.rb: Add a fast path
[Bug #20641] `Gem::BUNDLED_GEMS.warning?` adds a lot of extra
work on top of `require`. When the call end up atually loading code
the overhead is somewhat marginal.

However it's not uncommon for code to go some late `require` in some
paths, so it's expected that calling `require` with something already
required is somewhat fast, and `bundled_gems.rb` breaks this assumption.

To avoid this, we can have a fast path that in most case allow to
short-circuit all the heavy computations. If we extract the feature
basename and it doesn't match any of the bundled gems we care about
we can return very early.

With this change `require 'date'` is now only 1.33x slower on Ruby
3.3.3, than it was on Ruby 3.2.2, whereas before this change it
was at least 100x slower.
2024-07-22 10:33:07 +02:00
Nobuyoshi Nakada
c544f26726
Fix a typo in setup of block-after-blockcall tests
Unparenthesize the argument and make `command_call` when calling with
`do`-block.
2024-07-21 13:00:38 +09:00
yui-knk
11e5ebaba7 Fix SEGV on method call with empty args and brace block for do block command call 2024-07-21 11:02:38 +09:00
Jeremy Evans
e7dda08617 Do not set Enumerator::Lazy#zip to use packed format
Enumerator#zip yields a single array, not multiple arguments,
so Enumerator::Lazy#zip should do the same.

Fixes [#20623]
2024-07-20 18:16:42 -07:00
Jeremy Evans
6428ce80f0 Avoid array allocation for f(*r2k_ary) when def f(x)
When calling a method that does not accept a positional splat
parameter with a splatted array with a ruby2_keywords flagged hash,
there is no need to duplicate the splatted array.  Previously,
Ruby would duplicate the splatted array and potentially modify
it before flattening it to the VM stack

Use a similar approach as the f(*ary, **hash) optimization,
flattening the splatted array to the VM stack without modifying
it, and make any modifications needed to the VM stack.
2024-07-18 22:17:21 -07:00
Jeremy Evans
1cc5a64dd8 Avoid hash allocation for f(*r2k_ary) when def f(kw: 1)
When calling a method that accepts keywords but not a keyword
splat with a splatted array with a ruby2_keywords flagged hash,
there is no need to duplicate the ruby2_keywords flagged hash,
since it will be accessed to get the keyword values, but it will
not be modified.
2024-07-18 22:17:21 -07:00
Jeremy Evans
4a49b060ae Check for and remove duplicate checks in test_allocation 2024-07-18 22:17:21 -07:00
Jeremy Evans
94e7d26643 Avoid array allocation for f(*empty_ary, **hash) when def f(x)
This avoids an array allocation when calling a method that does
not accept a positional splat or keywords with both a positional
splat and keywords.  Previously, Ruby would dup the positional
splat to append the keyword splat to it. Then it would flatten
the dupped positional splat array to the VM stack.

This flattens the given positional splat to the VM stack, then
adds the keyword splat hash after the last positional splat
element on the VM stack, avoiding the need to modify
the positional splat array.
2024-07-18 22:17:21 -07:00
Jeremy Evans
3de20efc30 Avoid unnecessary array allocations for f(arg, *arg, **arg, **arg), f(*arg, a: lvar), and other calls
The `f(arg, *arg, **arg, **arg)` case was previously not optimized.
The optimizer didn't optimize this case because of the multiple
keyword splats, and the compiler didn't optimize it because the
`f(*arg, **arg, **arg)` optimization added in
0ee3960685 didn't apply.

I found it difficult to apply this optimization without changing
the `setup_args_core` API, since by the time you get to the ARGSCAT
case, you don't know whether you were called recursively or directly,
so I'm not sure if it was possible to know at that point whether the
array allocation could be avoided.

This changes the dup_rest argument in `setup_args_core` from an int
to a pointer to int.  This allows us to track whether we have allocated
a caller side array for multiple splats or splat+post across
recursive calls. Check the pointed value (*dup_rest) to determine the
`splatarray` argument. If dup_rest is 1, then use `splatarray true`
(caller-side array allocation), then set *dup_rest back to 0, ensuring
only a single `splatarray true` per method call.

Before calling `setup_args_core`, check whether the array allocation
can be avoided safely using `splatarray false`.  Optimizable cases are:

```
// f(*arg)
SPLAT

// f(1, *arg)
ARGSCAT
 LIST

// f(*arg, **arg)
ARGSPUSH
 SPLAT
 HASH nd_brace=0

// f(1, *arg, **arg)
ARGSPUSH
  ARGSCAT
   LIST
  HASH nd_brace=0
```

If so, dup_rest is set to 0 instead of 1 to avoid the allocation.

After calling `setup_args_core`, check the flag. If the flag
includes `VM_CALL_ARGS_SPLAT`, and the pointed value has changed,
indicating `splatarray true` was used, then also set
`VM_CALL_ARGS_SPLAT_MUT` in the flag.

My initial attempt at this broke the `f(*ary, &ary.pop)` test,
because we were not duplicating the ary in the splat even though
it was modified later (evaluation order issue). The initial attempt
would also break `f(*ary, **ary.pop)` or `f(*ary, kw: ary.pop)` cases
for the same reason. I added test cases for those evaluation
order issues.

Add setup_args_dup_rest_p static function that checks that a given
node is safe.  Call that on the block pass node to determine if
the block pass node is safe.  Also call it on each of the hash
key/value nodes to test that they are safe.  If any are not safe,
then set dup_rest = 1 so that `splatarray true` will be used to
avoid the evaluation order issue.

This new approach has the affect of optimizing most cases of
literal keywords after positional splats.  Previously, only
static keyword hashes after positional splats avoided array
allocation for the splat.  Now, most dynamic keyword hashes
after positional splats also avoid array allocation.

Add allocation tests for dynamic keyword keyword hashes after
positional splats.

setup_args_dup_rest_p is currently fairly conservative. It
could definitely be expanded to handle additional node types
to reduce allocations in additional cases.
2024-07-18 22:17:21 -07:00
alpha0x00
3222c67262 [rubygems/rubygems] Fix line comment issue for map
7ca06e139b
2024-07-19 03:03:16 +00:00
Kevin Newton
1c81d1a69d [PRISM] Refactor parser support into its own module 2024-07-18 21:15:54 -04:00
Hiroshi SHIBATA
8db2325a11 [ruby/uri] Also support URI::PATTERN with switch-back
823697edb4
2024-07-19 00:50:38 +00:00
Hiroshi SHIBATA
082335494b [ruby/uri] Added test for constant definition and remove URI::REGEXP when using RFC3986_PARSER
6f616d97fc
2024-07-19 00:50:37 +00:00
Hiroshi SHIBATA
862041d054 [ruby/uri] Rename and switch RFC2396_PARSER test
2e0f73f05e
2024-07-19 00:50:36 +00:00
Hiroshi SHIBATA
ce4da88a57 [ruby/uri] Switch to inspect with default parser
0ab9abbf08
2024-07-19 00:50:36 +00:00
David Rodríguez
50e7c8f051 [rubygems/rubygems] Fix unused variable warning when running RubyGems tests
155d8fd051
2024-07-18 19:23:06 +00:00
Kevin Newton
76ea5cde2a Refactor RUBY_DESCRIPTION assertions in test_rubyoptions 2024-07-18 13:03:17 -04:00
tomoya ishida
c304bf13b5 [ruby/irb] Clear ENV["XDG_CONFIG_HOME"] to avoid loading
user-defined irbrc in TestIRB::ConfigValidationTest
(https://github.com/ruby/irb/pull/982)

632da0ff29
2024-07-18 10:56:19 +00:00
David Rodríguez
86c99a8d14 [rubygems/rubygems] Fix gemspec require_paths type validation
It was not properly being detected as an Array attribute, and thus not
properly validated.

Fixing this allows us to remove a strange `rescue` clause in Bundler.

4121a32408
2024-07-18 09:25:17 +00:00
David Rodríguez
f78a8761c4 [rubygems/rubygems] Remove unnecessary Windows test skip
946180f5c1
2024-07-18 09:25:16 +00:00
Hartley McGuire
d0c17cbd09
Require space between hash/content in ATX heading (#1140)
While writing some Markdown documentation for Rails, I came across an
interesting case where trying to link to an instance method at the start
of a line would instead parse as an H1 heading:

```markdown
#response_body=
```

Expected:

```html
<a href=""><code>#response_body=</code></a>
```

Actual:

```html
<h1>response_body=</h1>
```

According to the CommonMark spec:

> At least one space or tab is required between the # characters and the
> heading’s contents, unless the heading is empty. Note that many
> implementations currently do not require the space. However, the space
> was required by the original ATX implementation, and it helps prevent
> things like the following from being parsed as headings:
>
> Example 64

So while some implementations do not follow this requirement, I believe
RDoc should because it makes it easy to write text similar to Example 64
(which was used in the new test) and it also enables automatically
linking to instance methods at the start of a line.
2024-07-18 09:40:01 +09:00
Kevin Newton
e77e4aa608 [ruby/prism] Have parse_stream handle NUL bytes
4a41d298c8
2024-07-17 19:44:32 +00:00
Nobuyoshi Nakada
278bbd7b45 Revert pending EVENT_RETURN tests
* "Allow ambiguosity of `return` line"
  65b991bc85
* "Move to test/.excludes-prism"
  3b4ff810d2
* "Pending `EVENT_RETURN` settracefunc tests with Prism"
  a7f33c99c6
2024-07-17 14:06:11 -04:00
Nobuyoshi Nakada
644424941a [Bug #20457] [Prism] Remove redundant return flag 2024-07-17 14:06:11 -04:00
Hiroshi SHIBATA
58aebcbce1 [rubygems/rubygems] Applied rubocop
c26054e7e9
2024-07-17 10:17:57 +00:00
Yusuke Endoh
3f65df48bd Report a TracePoint log when the TracePoint tests fail 2024-07-17 18:41:39 +09:00
Hiroshi SHIBATA
af60317861 Removed needless block arguments 2024-07-17 17:37:57 +09:00
Hiroshi SHIBATA
2a12e4ffec To avoid fd leak with fetch request for SSL server 2024-07-17 17:37:57 +09:00
Hiroshi SHIBATA
e55bae4a67 Close leaked TCPServer socket 2024-07-17 17:37:57 +09:00
Hiroshi SHIBATA
76386ba1c0 Close leaked SSLServer socket 2024-07-17 17:37:57 +09:00
Hiroshi SHIBATA
f4c642edb1 Fixed fd leak from TCPServer
```
Leaked file descriptor: HTTPSProxyTest#test_https_proxy_ssl_connection: 8 : #<TCPServer:fd 8, AF_INET, 127.0.0.1, 63104>
```
2024-07-17 17:37:57 +09:00
Kevin Newton
e3c5d73e0f
[PRISM] Omit two more ast tests that will not work without RubyVM::Ast 2024-07-16 14:41:42 -04:00
Stan Lo
4a4e1bf357 [ruby/irb] Group class methods under class << self
(https://github.com/ruby/irb/pull/981)

cdaa356df2
2024-07-16 15:58:15 +00:00
Shugo Maeda
e048a073a3 Add MatchData#bytebegin and MatchData#byteend
These methods return the byte-based offset of the beginning or end of the specified match.

[Feature #20576]
2024-07-16 14:48:06 +09:00
dependabot[bot]
3f679c02a9 [rubygems/rubygems] Bump rb-sys
Bumps [rb-sys](https://github.com/oxidize-rb/rb-sys) from 0.9.97 to 0.9.98.
- [Release notes](https://github.com/oxidize-rb/rb-sys/releases)
- [Commits](https://github.com/oxidize-rb/rb-sys/compare/v0.9.97...v0.9.98)

---
updated-dependencies:
- dependency-name: rb-sys
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

291a5ad53c
2024-07-15 20:17:37 +00:00
Kevin Newton
c06f79c575 [ruby/prism] Fix up implicit flags
f4152c1f50
2024-07-15 18:12:37 +00:00
Kevin Newton
6298667ff5 [ruby/prism] When parsing an invalid write, still add to the local table
a54abc4d1b
2024-07-12 13:50:00 +00:00