2262 Commits
Author SHA1 Message Date
Hiroshi SHIBATA b03e500193 Reject a compiled program too big for its offset type
Concatenated large repeats reach a program past INT_MAX from a few hundred
kilobytes of source, and the offsets emitted into it wrap.  Cap the size
while the program is emitted, which does not depend on the length pass that
overflows along with it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
(cherry picked from commit cf35145fc4)
2026-10-23 12:00:20 +09:00
Hiroya Fujinami 63eda2681e Reject overflowed repeat/null-check IDs in huge regexps (#16801)
Add explicit errors for excessive range-repeat and null-check IDs;
map new errors in regerror; add regression tests for oversized generated
patterns.

(cherry picked from commit 493d26dcde)
2026-10-23 12:00:20 +09:00
nagachika 61a9c450f4 merge revision(s) 9d44cb0b2b, 367ddd445c, 48dce7874f: [Backport #21655]
[PATCH] Remove rbimpl_rstring_getmem() usage as workaround for GCC 15.2.1 optimization bug. [Bug #21655]

	[PATCH] include/ruby/internal/core/rstring.h: Remove rbimpl_rstring_getmem() definition.

	[PATCH] simplify RSRING_GETMEM() definition.
2025-11-29 12:30:30 +09:00
nagachika 5a8d764216 merge revision(s) 43dbb9a93f, 2bb6fe3854, 7c9dd0ecff: [Backport #21629]
[PATCH] [Bug #21629] Enable `nonstring` attribute on clang 21

	[PATCH] [Bug #21629] Initialize `struct RString`

	[PATCH] [Bug #21629] Initialize `struct RArray`
2025-10-08 22:56:02 +09:00
nagachika 407f6c9d05 merge revision(s) b42afa1dbc, 67b91e7807, f1f0cc14cc, 543e3a1896, ed2806117a, 46e4c86737: [Backport #21497]
Suppress gcc 15 unterminated-string-initialization warnings

	Drop an ignored attribute
	MIME-Version: 1.0
	Content-Type: text/plain; charset=UTF-8
	Content-Transfer-Encoding: 8bit

	GCC 13.3.0 (Ubuntu 24.04) emits the following warning:

	../symbol.c: In function ‘rb_id_attrset’:
	../symbol.c:175:9: warning: ‘nonstring’ attribute ignored on objects of type ‘const char[][8]’ [-Wattributes]
	  175 |         RBIMPL_ATTR_NONSTRING() static const char id_types[][8] = {
	      |         ^~~~~~~~~~~~~~~~~~~~~

	Separate `__has_attribute` from `defined(__has_attribute)`

	Fix Visual C warnings:
	```
	regenc.h(121): warning C4067: unexpected tokens following preprocessor directive - expected a newline
	```

	Cast up `int` instruction code to `VALUE`

	Fix Visual C warnings:
	```
	iseq.c(3793): warning C4312: 'type cast': conversion from 'int' to 'void *' of greater size
	iseq.c(3794): warning C4312: 'type cast': conversion from 'int' to 'void *' of greater size
	```

	Do not let files depend on a phony target

	Detect `clock_gettime` and `clock_getres` for winpthreads
2025-07-20 18:32:03 +09:00
nagachika 5de0c228a3 merge revision(s) 2297afda7f: [Backport #21340]
Include stdbool.h without checking with autoconf

	As reported in <https://bugs.ruby-lang.org/issues/21340>, older autoconf
	have an AC_HEADER_STDBOOL that's incompatible with C23. Autoconf 2.72
	fixed the macro, but also mentions that it's obsolescent since all
	current compilers have this header.

	Since we require C99 [1] and VS 2015 [2], we might actually be able take
	that suggestion and include stdbool.h without a check. I want to try
	this on rubyci.org and will revert if this cause any issues. Not
	touching AC_HEADER_STDBOOL in configure.ac for now.

	[1]: https://bugs.ruby-lang.org/issues/15347
	[2]: https://bugs.ruby-lang.org/issues/19982
2025-06-14 11:41:49 +09:00
nagachika a3adc05a4e merge revision(s) 3e47e7a499, 46e4c86737:
Fix redefinition of `clock_gettime` and `clock_getres`

	winpthreads-git 12.0.0.r720 provides `clock_gettime` and
	`clock_getres` as inline functions.

	Detect `clock_gettime` and `clock_getres` for winpthreads
2025-05-17 15:41:00 +09:00
nagachika ffd469bd6d merge revision(s) 719486a642: [Backport #21286]
Fix C23 (GCC 15) WIN32 compatibility for rb_define_* functions

	Fixes [Bug #21286]
2025-05-06 11:29:37 +09:00
Peter Zhu 7571ad42f4 [Bug #20650] Fix memory leak in Regexp capture group when timeout (#11244)
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:14:26 -07:00
Takashi Kokubun b2eb7f47b3 merge revision(s) 1faeb44dfcf777ace28321e80d0ebf942161a0a7,7f87ad9fc4bc45faf8cd33602a025f27c094b2fd: [Backport #20431]
Check if macros are defined before using

	Assume macros with the same prefix would be defined together.

	Refer autoconfigured endian macro (#10572)

	Remove the case `RB_IO_BUFFER_HOST_ENDIAN` is not defined.
2024-05-29 16:50:13 -07:00
Samuel Williams 260bf60e52 Correctly release the underlying file mapping. (#9340)
* Avoiding using `Tempfile` which was retaining the file preventing it from unlinking.
2023-12-25 14:20:53 +13:00
Samuel Williams 37753f163e IO::Buffer improvements and documentation. (#9329)
* Restore experimental warnings.

* Documentation and code structure improvements.

* Improved validation of flags, clarified documentation of argument handling.

* Remove inconsistent use of `Example:` and add example to `null?`.

* Expose `IO::Buffer#private?` and add test.
2023-12-25 02:03:36 +13:00
Alan Wu 0c05551f58 Typo fixes for public headers [ci skip] 2023-12-21 20:34:49 -05:00
John HawthornandDustin Brown 1710eb9367 [DOC] Fix rb_postponed_job_register_once typo
Co-authored-by: Dustin Brown <dbrown9@gmail.com>
2023-12-21 09:17:22 -08:00
Satoshi Tagomori e51f9e9f75 rb_ext_resolve_symbol: C API to resolve and return externed symbols [Feature #20005]
This is a C API for extensions to resolve and get function symbols of other extensions.
Extensions can check the expected symbol is correctly loaded and accessible, and
use it if it is available.
Otherwise, extensions can raise their own error to guide users to setup their
environments correctly and what's missing.
2023-12-14 17:39:42 +09:00
KJ Tsanaktsidis 626daa73ec Small doc improvements for rb_postponed_job API 2023-12-13 13:35:05 +11:00
Koichi Sasada c4c39082af add flags to rb_postponed_job_preregister
for future extensions.
2023-12-10 15:39:06 +09:00
KJ Tsanaktsidis f8effa209a Change the semantics of rb_postponed_job_register
Our current implementation of rb_postponed_job_register suffers from
some safety issues that can lead to interpreter crashes (see bug #1991).
Essentially, the issue is that jobs can be called with the wrong
arguments.

We made two attempts to fix this whilst keeping the promised semantics,
but:
  * The first one involved masking/unmasking when flushing jobs, which
    was believed to be too expensive
  * The second one involved a lock-free, multi-producer, single-consumer
    ringbuffer, which was too complex

The critical insight behind this third solution is that essentially the
only user of these APIs are a) internal, or b) profiling gems.

For a), none of the usages actually require variable data; they will
work just fine with the preregistration interface.

For b), generally profiling gems only call a single callback with a
single piece of data (which is actually usually just zero) for the life
of the program. The ringbuffer is complex because it needs to support
multi-word inserts of job & data (which can't be atomic); but nobody
actually even needs that functionality, really.

So, this comit:
  * Introduces a pre-registration API for jobs, with a GVL-requiring
    rb_postponed_job_prereigster, which returns a handle which can be
    used with an async-signal-safe rb_postponed_job_trigger.
  * Deprecates rb_postponed_job_register (and re-implements it on top of
    the preregister function for compatability)
  * Moves all the internal usages of postponed job register
    pre-registration
2023-12-10 15:00:37 +09:00
KJ Tsanaktsidis aecbd66742 Add RUBY_ATOMIC_{PTR_,}FETCH macros for atomic loads
This can already be emulated by doing an atomic fetch_add of zero, but
this is more explicit.

[Bug #19994]
2023-12-10 15:00:37 +09:00
卜部昌平 51ab9ebca1 [ci skip] comment for commit be1bbd5b7d 2023-12-08 13:17:56 +09:00
Koichi Sasada 352a885a0f Thread specific storage APIs
This patch introduces thread specific storage APIs
for tools which use `rb_internal_thread_event_hook` APIs.

* `rb_internal_thread_specific_key_create()` to create a tool specific
  thread local storage key and allocate the storage if not available.
* `rb_internal_thread_specific_set()` sets a data to thread and tool
  specific storage.
* `rb_internal_thread_specific_get()` gets a data in thread and tool
  specific storage.

Note that `rb_internal_thread_specific_get|set(thread_val, key)`
can be called without GVL and safe for async signal and safe for
multi-threading (native threads). So you can call it in any internal
thread event hooks. Further more you can call it from other native
threads. Of course `thread_val` should be living while accessing the
data from this function.

Note that you should not forget to clean up the set data.
2023-12-08 13:16:19 +09:00
Nobuyoshi Nakada 0cdad3b92a Add RUBY_REFERENCES
Instead of `RUBY_REFERENCES_START` and `RUBY_REFERENCES_END`, so that
auto-indent works well.
2023-11-30 21:39:28 +09:00
Nobuyoshi Nakada 30f7b7a053 Prefix REF_EDGE and REFS_LIST_PTR with RUBY_
Also move `struct` so that `typedef`-ed names can be used.
2023-11-30 21:39:28 +09:00
23a7714343 Refactor and fix the GVL instrumentation API
This entirely changes how it is tested. Rather than to use counters
we now record the timeline of events with associated threads which
makes it much easier to assert that certains events are only preceded
by a specific event, and makes it much easier to debug unexpected
timelines.

Co-Authored-By: Étienne Barrié <etienne.barrie@gmail.com>
Co-Authored-By: JP Camara <jp@jpcamara.com>
Co-Authored-By: John Hawthorn <john@hawthorn.email>
2023-11-27 17:37:57 +01:00
Nobuyoshi Nakada e81c380c0f Constify RUBY_REFERENCES_START tables 2023-11-26 16:04:23 +09:00
Jean Boussier 9ca41e9991 GVL Instrumentation: pass thread->self as part of event data
Context: https://github.com/ivoanjo/gvl-tracing/pull/4

Some hooks may want to collect data on a per thread basis.
Right now the only way to identify the concerned thread is to
use `rb_nativethread_self()` or similar, but even then because
of the thread cache or MaNy, two distinct Ruby threads may report
the same native thread id.

By passing `thread->self`, hooks can use it as a key to store
the metadata.

NB: Most hooks are executed outside the GVL, so such data collection
need to use a thread-safe data-structure, and shouldn't use the
reference in other ways from inside the hook.

They must also either pin that value or handle compaction.
2023-11-13 08:45:20 +01:00
Nobuyoshi Nakada 64f03460ba [DOC] Update comment for DECIMAL_SIZE_OF_BITS 2023-11-11 19:26:13 +09:00
Jean Boussier 7efe0669ae TypedData_Make_Struct0: cast RTYPEDDATA_GET_DATA return pointer
Fixes:

```
/usr/local/ruby/include/ruby-3.3.0+0/ruby/internal/core/rtypeddata.h:467:33:
error: invalid conversion from ‘void*’ to ‘parser_t*’ [-fpermissive]
  467 |     (sval) = RTYPEDDATA_GET_DATA(result); \
      |              ~~~~~~~~~~~~~~~~~~~^~~~~~~~
      |                                 |
      |                                 void*
```
2023-11-08 11:33:01 +01:00
Peter ZhuandJean Boussier 392238e3fd Implement embedded TypedData objects
This commit adds a new flag RUBY_TYPED_EMBEDDABLE that allows the data
of a TypedData object to be embedded after the object itself. This will
improve cache locality and allow us to save the 8 byte data pointer.

Co-Authored-By: Jean Boussier <byroot@ruby-lang.org>
2023-11-07 15:48:06 -05:00
Nobuyoshi Nakada ee90a7f981 [DOC] Update the document for FilePathStringValue 2023-11-02 10:31:13 +09:00
Daisuke AritomoandMike Perham 4adf418be9 [Feature #10602] Add new API rb_profile_thread_frames()
Add a new API rb_profile_thread_frames(), which is essentialy a
per-thread version of rb_profile_frames().

While the original rb_profile_frames() always returns results about the
current active thread obtained by GET_EC(), this new API takes a Thread
to be profiled as an argument.

This should come in handy when profiling I/O-bound programs such as
webapps, since this new API allows us to learn about Threads performing
I/O (which do not have the GVL).

Profiling worker threads (such as Sidekiq workers) may be another
application.

Implements [Feature #10602]

Co-authored-by: Mike Perham <mike@perham.net>
2023-10-31 11:16:18 +09:00
Koichi Sasada be1bbd5b7d M:N thread scheduler for Ractors
This patch introduce M:N thread scheduler for Ractor system.

In general, M:N thread scheduler employs N native threads (OS threads)
to manage M user-level threads (Ruby threads in this case).
On the Ruby interpreter, 1 native thread is provided for 1 Ractor
and all Ruby threads are managed by the native thread.

From Ruby 1.9, the interpreter uses 1:1 thread scheduler which means
1 Ruby thread has 1 native thread. M:N scheduler change this strategy.

Because of compatibility issue (and stableness issue of the implementation)
main Ractor doesn't use M:N scheduler on default. On the other words,
threads on the main Ractor will be managed with 1:1 thread scheduler.

There are additional settings by environment variables:

`RUBY_MN_THREADS=1` enables M:N thread scheduler on the main ractor.
Note that non-main ractors use the M:N scheduler without this
configuration. With this configuration, single ractor applications
run threads on M:1 thread scheduler (green threads, user-level threads).

`RUBY_MAX_CPU=n` specifies maximum number of native threads for
M:N scheduler (default: 8).

This patch will be reverted soon if non-easy issues are found.

[Bug #19842]
2023-10-12 14:47:01 +09:00
Daisuke Aritomo 6d28f96986 [DOC] Fix description for rb_postponed_job_register_one()
The current documentation for `rb_postponed_job_register_one()` is
explaining the differences with itself, where it should be explaining
the differences with `rb_postponed_job_register()`.
2023-10-04 14:12:50 +09:00
Benoit Daloze 4468b6ef3c Fix documentation for rb_warn() and friends
* rb_warn() does not warn if $VERBOSE is nil, the "always" is wrong.
* Talk about $VERBOSE and not -W since $VERBOSE can be changed at runtime.
2023-09-29 12:53:40 +02:00
Nobuyoshi Nakada 50520cc193 [DOC] Missing comment markers 2023-09-27 16:18:05 +09:00
John Hawthorn 380b42fe6a Fix comment for rb_enc_str_new [ci skip] 2023-09-16 11:29:40 -07:00
Sutou Kouhei 90dad2b128 memory_view: Avoid using bit field
Bit field's memory layout is implementation-defined.

See also:
https://wiki.sei.cmu.edu/confluence/display/c/EXP11-C.+Do+not+make+assumptions+regarding+the+layout+of+structures+with+bit-fields

If memory layout is implementation-defined, it's difficult to use from
FFI library such as Ruby-FFI.
2023-09-09 07:30:04 +09:00
Jean Boussier 2d37b44603 Document that thread event hooks are called without the GVL
Except for the `RESUMED` event.
2023-09-07 18:31:16 +02:00
Samuel Williams e46e48d690 Expose rb_process_status_wait and hide rb_process_status_waitv. (#8316) 2023-08-29 22:24:55 +12:00
Samuel Williams caf48487ca Restore HAVE_RB_IO_T macro for compatibility with kgio, unicorn, etc. (#8286) 2023-08-28 21:50:05 +09:00
卜部昌平 fa2712981f workaround clang-17 -Wc2x-extensions
cf: https://github.com/llvm/llvm-project/commit/874217f99b99ab3c9026dc3b7bd84cd2beebde6e
2023-08-25 17:27:53 +09:00
Takashi Kokubun d405410e3c YJIT: Move ROBJECT_OFFSET_* to yjit.c (#8157) 2023-08-02 10:15:29 -04:00
Koichi Sasada d68c01fd31 support rescue event for TracePoint
fix [Feature #19572]
2023-08-01 22:46:17 +09:00
Takashi Kokubun 9721972175 Resurrect rb_reg_prepare_re C API
Existing strscan releases rely on this C API. It means that the current
Ruby master doesn't work if your Gemfile.lock has strscan unless it's
locked to 3.0.7, which is not released yet.

To fix it, let's not remove the C API we've exposed to users.
2023-07-27 15:30:10 -07:00
Peter Zhu 7193b404a1 Add function rb_reg_onig_match
rb_reg_onig_match performs preparation, error handling, and cleanup for
matching a regex against a string. This reduces repetitive code and
removes the need for StringScanner to access internal data of regex.
2023-07-27 13:33:40 -04:00
Nobuyoshi Nakada 1faeb44dfc Check if macros are defined before using
Assume macros with the same prefix would be defined together.
2023-07-24 23:59:50 +09:00
Nobuyoshi Nakada 14d1540768 RString NULL ptr check only when RUBY_DEBUG
Since edf01d4e82, fake string treats
NULL as an empty string.
2023-07-24 23:57:28 +09:00
Kunshan Wang 639aa76e82 Embed struct rmatch into GC slot (#8097) 2023-07-20 14:17:38 -04:00
Nobuyoshi Nakada 1c4a523006 Move posix_signal declaration internal with prefix ruby_ 2023-07-17 21:31:59 +09:00
Peter Zhu 3223181284 Remove RARRAY_CONST_PTR_TRANSIENT
RARRAY_CONST_PTR now does the same things as RARRAY_CONST_PTR_TRANSIENT.
2023-07-13 14:48:14 -04:00