237 Commits
Author SHA1 Message Date
Nobuyoshi Nakada 7d4461e162 [Misc #22206] Generate dependencies without compiler builds
Replace `tool/update-deps` with a Ruby source scanner so dependency
updates no longer require GNU make, a configured build, or compiler
preprocessor output.

Keep compact source mappings and scanner declarations in dependency
files, and expand complete rules into the build directory.  Avoid the
temporary fixture headers required by the compiler-driven prototype.

Use the generated rules from GNU make, BSD make, and NMake builds.
Support out-of-tree and read-only source trees, and retain expanded
dependencies shipped in release archives when baseruby is unavailable.
2026-07-23 00:04:47 +09:00
Nobuyoshi Nakada 4b638eecd1 Provide a fallback implementation of memrchr
Move the local fallback out of string.c so other source files can use
memrchr on platforms that do not provide it.
2026-07-15 13:28:39 +09:00
Koichi SasadaandClaude Fable 5 39564b8957 dtoa.c: load the 5-powers cache pointers atomically
pow5mult's b_cache publishes new cache nodes with ATOMIC_PTR_CAS (release),
but both readers -- the fast-path check and the recheck before allocating --
were plain loads racing with that CAS: a data race in the C11 sense, and
what TSan reports as `race:pow5mult` under parallel Ractor float<->string
conversion. Harmless on current targets (the reader dereferences through
the loaded pointer, so address dependency orders the accesses), but easy
to make formally correct: read the cache head/next through
RUBY_ATOMIC_PTR_LOAD. The lock-free CAS scheme is unchanged.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-15 04:57:14 +09:00
Nobuyoshi Nakada 44693ee329 Fix a possible memory leak in dtoa
Fix GH-15061
2025-12-24 14:05:41 +09:00
Nobuyoshi Nakada 624538ba92 dtoa.c: Check integer underflow
Reported at https://hackerone.com/reports/3288162

This underflow does not occur in Ruby because:

* This function is `static` and not accessible other than from ruby
  internal.

* Ruby uses mode 0 when calling this function directly.

* For `%f` in vsnprintf.c using mode 3, this parameter comes from the
  precision, but negative precision is meaningless and ignored.
2025-09-10 16:45:25 +09:00
Nobuyoshi Nakada f047174cf1 dtoa.c: Check memory allocation failures 2025-09-10 15:34:30 +09:00
Nobuyoshi Nakada d0c966adcd dtoa.c: Extract macro to update 5powers Bigint cache atomically 2025-09-10 15:34:30 +09:00
Nobuyoshi Nakada 0887d82406 dtoa.c: Add shortcut if arguments are zero 2025-09-10 15:34:29 +09:00
John Hawthorn b3b1d7b295 Remove spinlock in dtoa's Balloc, use xmalloc
The spinlock here performs poorly when there are multiple Ractors. The
improvement on single threaded performance doesn't seem that
significant, so I think we should just use malloc.
2025-04-07 16:56:44 -07:00
Nobuyoshi Nakada d17edf3a17 [Bug #20705] Update strtod implementation
The absence of either the integer or fractional part should be
allowed.
2024-10-05 23:29:42 +09:00
Peter Zhu e7d20623cb Add comments in setproctitle.c 2024-05-02 10:12:58 -04:00
Peter Zhu 4f69d318b8 Keep track of the originally allocated environ
We need to keep a pointer to the originally allocated environ because
adding more environment variables can cause it to be changed to something
else.

For example:

    100.times do |i|
      ENV["FOO#{i}"] = "1"
    end

Causes Valgrind to report:

    312 bytes in 1 blocks are definitely lost in loss record 9 of 13
      at 0x484D444: calloc (vg_replace_malloc.c:1340)
      by 0x1884F8: calloc1 (gc.c:1844)
      by 0x1884F8: objspace_xcalloc (gc.c:12202)
      by 0x1884F8: ruby_xcalloc_body (gc.c:12209)
      by 0x4204DD: ruby_init_setproctitle (setproctitle.c:119)
      by 0x27DDF4: ruby_process_options (ruby.c:3101)
      by 0x160BD1: ruby_options (eval.c:117)
      by 0x15B96E: rb_main (main.c:40)
      by 0x15B96E: main (main.c:59)
2024-05-02 10:12:58 -04:00
Peter Zhu d1f14bafb0 Fix leak reported by Valgrind when setting environment variables
The call to setenv replaces the string in the environ array, which causes
the original string to be reported as a memory leak.

This commit allocates another copy of environ called alloc_environ that
contains the strings allocated by ruby_init_setproctitle. Then in
ruby_free_proctitle it frees all the strings in alloc_environ.

For example:

    ENV.each { |k, v| ENV[k] = v.dup }

Valgrind reports:

    3,321 bytes in 39 blocks are definitely lost in loss record 3 of 3
      at 0x484884F: malloc (vg_replace_malloc.c:393)
      by 0x25CB5B: objspace_xmalloc0 (gc.c:11972)
      by 0x40344E: ruby_strdup (util.c:540)
      by 0x59C854: ruby_init_setproctitle (setproctitle.c:143)
      by 0x38CC44: ruby_process_options (ruby.c:3101)
      by 0x234DB1: ruby_options (eval.c:117)
      by 0x15B92E: rb_main (main.c:40)
      by 0x15B92E: main (main.c:59)
2024-05-01 08:54:45 -04:00
Peter Zhu 206388b19e Fix ruby_free_proctitle
It is undefined behaviour to free environ as it is managed by the system.
This caused RUBY_FREE_AT_EXIT to double free on systems like Linux. This
commit changes it to only free orig_environ, which is enough to make
both Valgrind and macOS leaks tools to not detect memory leaks.
2024-01-12 16:13:42 -05:00
Peter Zhu 057df4379f Free environ when RUBY_FREE_AT_EXIT
The environ is malloc'd, so it gets reported as a memory leak. This
commit adds ruby_free_proctitle which frees it during shutdown when
RUBY_FREE_AT_EXIT is set.

    STACK OF 1 INSTANCE OF 'ROOT LEAK: <calloc in ruby_init_setproctitle>':
    5   dyld                                  0x18b7090e0 start + 2360
    4   ruby                                  0x10000e3a8 main + 100  main.c:58
    3   ruby                                  0x1000b4dfc ruby_options + 180  eval.c:121
    2   ruby                                  0x1001c5f70 ruby_process_options + 200  ruby.c:3014
    1   ruby                                  0x10035c9fc ruby_init_setproctitle + 76  setproctitle.c:105
    0   libsystem_malloc.dylib                0x18b8c7b78 _malloc_zone_calloc_instrumented_or_legacy + 100
2024-01-11 10:09:53 -05:00
6816e8efcf Free everything at shutdown
when the RUBY_FREE_ON_SHUTDOWN environment variable is set, manually free memory at shutdown.

Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
Co-authored-by: Peter Zhu <peter@peterzhu.ca>
2023-12-07 15:52:35 -05:00
Nobuyoshi Nakada 5eef125afe Dump backtraces to an arbitrary stream when using libprocstat 2023-09-26 09:02:37 +09:00
Nobuyoshi Nakada 78c5bb1136 Remove duplicate #include <string.h> [ci skip] 2023-08-27 23:21:20 +09:00
Nobuyoshi Nakada bc8cc68aef Make dtoa.c buildable alone 2023-07-14 18:35:23 +09:00
Nobuyoshi Nakada f42230ff22 Adjust styles [ci skip] 2022-07-27 18:42:27 +09:00
Nobuyoshi Nakada d0a822eec5 Fix dtoa buffer overrun
https://hackerone.com/reports/1248108
2022-04-12 21:30:49 +09:00
Yuta Saito 7ee786388a [wasm] wasm/missing.{c,h}: add missing libc stubs for wasi-libc 2022-01-19 11:19:06 +09:00
Nobuyoshi Nakada 33844f3096 Prefer ANSI-style prototypes over old K&R-style definitions 2021-10-27 10:16:52 +09:00
Nobuyoshi Nakada 80c1faf076 Use C99-defined signbit macro 2021-08-27 12:42:23 +09:00
Nobuyoshi Nakada 04be8e84db Use C99-defined macros to classify a floating-point number 2021-08-27 12:41:30 +09:00
Nobuyoshi Nakada 2d67027448 Removed missing/dup2.c
This function should be always available, as POSIX-compliant or
Windows platform are required since 1.9.  Also the code in this
file is MT-unsafe.
2021-05-10 15:59:23 +09:00
Nobuyoshi NakadaandKoichi Sasada 3acc81d9e4 Fixed race in dtoa [Bug #17612]
Fixed the race condition when replacing `freelist` entry with its
chained next element.  At acquiring an entry, hold the entry once
with the special value, then release by replacing it with the next
element again after acquired.  If another thread is holding the
same entry at that time, spinning until the entry gets released.

Co-Authored-By: Koichi Sasada <ko1@atdot.net>
2021-02-10 19:44:47 +09:00
Nobuyoshi Nakada 2adbf01ae1 dtoa.c: make thread-safe by using atomic CAS 2021-01-10 18:19:53 +09:00
Nobuyoshi Nakada 34d02631e7 dtoa.c: constified
clang seems to locate never modified local data in the const
segment implicitly.
2021-01-10 16:42:36 +09:00
Nobuyoshi Nakada 63abb5c227 dtoa.c: make compilable independently
Except for `-Dxmalloc=malloc -Dxfree=free`.
2021-01-10 16:28:58 +09:00
Nobuyoshi Nakada 4d2ad8d737 Removed obsolete autoconf checks
Use regular `AC_CHECK_MEMBERS` instead of:
* `AC_STRUCT_ST_BLKSIZE`
* `AC_STRUCT_ST_BLOCKS`
* `AC_STRUCT_ST_RDEV`
2020-12-12 17:23:44 +09:00
卜部昌平 9e41a75255 sed -i 's|ruby/impl|ruby/internal|'
To fix build failures.
2020-05-11 09:24:08 +09:00
卜部昌平 d7f4d732c1 sed -i s|ruby/3|ruby/impl|g
This shall fix compile errors.
2020-05-11 09:24:08 +09:00
Sorah Fukumori 7ccb7a992d [DOC] explicit_bzero exists in modern Linux
[ci skip]
2020-04-26 02:00:45 +09:00
卜部昌平 4ff3f20540 add #include guard hack
According to MSVC manual (*1), cl.exe can skip including a header file
when that:

- contains #pragma once, or
- starts with #ifndef, or
- starts with #if ! defined.

GCC has a similar trick (*2), but it acts more stricter (e. g. there
must be _no tokens_ outside of #ifndef...#endif).

Sun C lacked #pragma once for a looong time.  Oracle Developer Studio
12.5 finally implemented it, but we cannot assume such recent version.

This changeset modifies header files so that each of them include
strictly one #ifndef...#endif.  I believe this is the most portable way
to trigger compiler optimizations. [Bug #16770]

*1: https://docs.microsoft.com/en-us/cpp/preprocessor/once
*2: https://gcc.gnu.org/onlinedocs/cppinternals/Guard-Macros.html
2020-04-13 16:06:00 +09:00
卜部昌平 9e6e39c351 Merge pull request #2991 from shyouhei/ruby.h
Split ruby.h
2020-04-08 13:28:13 +09:00
卜部昌平 8184adabe5 internal/stdbool.h rework
Noticed that internal/stdbool.h and addr2line.c are the only two place
where missing/stdbool.h is included.  Why not delete the file so that
we can merge internal/stdbool.h and missing/stdbool.h into one.
2019-12-26 20:45:12 +09:00
Nobuyoshi Nakada e954be14d0 Get rid of false positive misspellings
[Bug #16437]
2019-12-24 10:33:32 +09:00
Nobuyoshi Nakada 209164e44f Renamed assembly file like as e64f71f812 2019-11-18 18:42:22 +09:00
Yusuke Endoh cb14c4a535 missing/setproctitle.c: remove nonsense NULL check
If fmt is NULL, ptitle is uninitialized and used.
SETPROCTITLE(3bsd) says "If fmt is NULL, the process title is restored",
but looks like the feature is not implemented in missing/setproctitle.c.
At least the source code of ruby does not pass NULL to the function.
So I assume this function requires non-NULL fmt.

This issue was found by Coverity Scan.
2019-10-12 21:14:20 +09:00
Yusuke Endoh d76be10df1 missing/memcmp.c: suppress a -Wparentheses warning 2019-08-19 14:43:55 +09:00
David CARLIER 28267cea08 NetBSD native support of explicit_bzero's like feature (#2145) 2019-08-17 14:17:30 +09:00
nobu a67788b2f3 Split procstat_vm.c
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67024 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-02-07 08:53:13 +00:00
nobu 18365f9e83 Split mt19937.c
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67023 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-02-07 08:53:12 +00:00
nobu 867338c36e Split dtoa.c
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67022 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-02-07 08:53:11 +00:00
k0kubun d8931259d0 internal.h: unify our own stdtbool.h implementation
because we had another one in missing/stdbool.h as well.
Assuming that _Bool does not exist when stdbool.h is absent, I'm in
favor of r57462 implementation rather than r61326.

Note that Visual Studio 2013 has stdbool.h and thus r57462's commit
message is somewhat misleading. This missing/stdbool.h is for Visual
Studio 2012 or older, and for Oracle Solaris Studio 12.2 or older because
it's added in 12.3 https://docs.oracle.com/cd/E24457_01/html/E21987/gkeza.html.

missing/stdbool.h: ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66739 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-01-06 14:53:43 +00:00
shyouhei 4680cecd57 [ci skip] tabify
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61972 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-20 04:18:09 +00:00
shyouhei 782236e3bd add missing/nan.c
instead of scattering #ifdef HAVE_NANF here and there define our
own nan() unless defined elsewhere.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61971 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-20 03:16:59 +00:00
ngoto b75828fdfa Support C89 if possible
* addr2line.c: Because stdbool.h is a C99 feature, compile error
  occurs with some old compilers without specifying C99 options.
  Fix compile error with Oracle Solaris Studio 12.4 on Solaris 10.
  [Bug #14200] [ruby-dev:50366]

* missing/stdbool.h: Alternative of stdbool.h for C89 compilers.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61326 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-12-18 16:17:17 +00:00
rhe 0b845a8458 string.c: fix memory leak in String#crypt
Use ALLOCV to allocate struct crypt_data for slightly cleaner and less
error-prone code. It is currently possible it leaks when an invalid
argument is passed to String#crypt or rb_str_new_cstr() fails to
allocate memory.

SIZEOF_CRYPT_DATA macro in missing/crypt.h is removed since it is not
used any longer.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60748 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-11-12 15:55:04 +00:00