Commit graph

16 commits

Author SHA1 Message Date
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
Adam Hess
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
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
nobu
9b454bfaf3 rubystub
* rubystub.c: generalize win32/stub.c.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55973 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-08-20 02:20:34 +00:00
odaira
f3754f57cb missing/setproctitle.c: Avoid invalidating argv[1], argv[2], etc. until the first call to Process.setproctitle, because the ps command of AIX refers to the argv array. [Bug #10090]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47852 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-10-08 22:31:53 +00:00
charliesome
af35f2a61d * configure.in: check for the whether crt_externs.h is present when compiling
for darwin (this header is missing in the iOS SDK)
* eval_intern.h: check HAVE_CRT_EXTERNS_H before including crt_externs.h, if
  not defined, include missing/crt_externs.h instead
* hash.c: ditto
* missing/setproctitle.c: ditto
* missing/crt_externs.h: declare _NSGetEnviron() function and define environ
  for iOS

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38644 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-12-28 14:23:25 +00:00
nobu
2741a598ff setproctitle.c: _NSGetEnviron
* missing/setproctitle.c (environ): use (*_NSGetEnviron()) instead of
  environ on Darwin for namespace cleanness, same as [ruby-core:00537].
  [ruby-core:45615] [Bug #6576]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36151 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-06-20 07:36:23 +00:00
nobu
b3e2baa3b5 * missing/setproctitle.c (ruby_init_setproctitle): changed prefix.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34326 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-01-17 08:13:50 +00:00
kosaki
887063f44b fix mswin32 build error.
* missing/setproctitle.c: oadd #ifdef HAVE_UNISTD_H.
* win32/Makefile.sub (MISSING): add setproctitle.obj



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31569 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-05-14 16:01:28 +00:00
kosaki
d19cda5fa6 * missing/setproctitle.c: add to include "ruby/util.h".
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31568 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-05-14 13:45:58 +00:00
kosaki
1780714844 * missing/setproctitle.c (compat_init_setproctitle): use
ruby_strdup() instead of strdup().



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31566 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-05-14 10:51:16 +00:00
kosaki
3ad44e0aa1 introduce missing/setproctitle.c
* include/ruby/missing.h: add setproctitle() declaration.
* missing/setproctitle.c: added.
* configure.in: add check for missing/setproctitle.c.

* ruby.c (ruby_process_options): add to call compat_init_setproctitle().
* ruby.c (set_arg0): remove all platform specific code. it's
  moved to missing/setproctitle.c.
* ruby.c (origarg): remove len field. It's no longer used.
* ruby.c (get_arglen): removed.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31565 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-05-14 10:49:47 +00:00