Forum: Ruby-core [PATCH] ruby_bind_stack() to embed Ruby in coroutine

79f9616c279f40ef953f366dcfe38dc8?d=identicon&s=25 Suraj Kurapati (sunaku)
on 2013-02-13 06:50
(Received via mailing list)
Issue #2294 has been updated by sunaku (Suraj Kurapati).


ko1 (Koichi Sasada) wrote:
> > 1.  Cannot bind Ruby to a pre-allocated stack address range.
>
> I'm not sure what you are saying.

Changeset r38905 did not include my ruby_bind_stack() function.

Without that function (or something similar), I cannot tell Ruby
2.0.0-rc2
about the stack address range of the coroutine which is hosting the
Ruby.

And if Ruby is not told that information, it might make a fatal mistake:
Ruby might go beyond the coroutine's pre-allocated stack address range.

This can cause memory corruption, segfault crashes, and undefined
behavior.

> `ucontext' version do `pre-allocation'.

Yes, in my ruby-coroutine-example application, all versions do
pre-allocation.

However, Ruby must also be told (bound to) the coroutine's stack address
range.

For your reference:

* libpcl coroutine is bound to pre-allocated stack here:
  https://github.com/sunaku/ruby-coroutine-example/b...

* pthread coroutine is bound to pre-allocated stack here:
  https://github.com/sunaku/ruby-coroutine-example/b...

* ucontext coroutine is bound to pre-allocated stack here:
  https://github.com/sunaku/ruby-coroutine-example/b...

* Ruby must _also_ be bound to coroutine's pre-allocated stack here:
  https://github.com/sunaku/ruby-coroutine-example/b...

> > 2.  In my coroutine example, only ucontext works correctly:
>
> I checked pthread and it okay...
>
> I'll check again soon. Please wait.

Sure, thanks for your consideration.
----------------------------------------
Feature #2294: [PATCH] ruby_bind_stack() to embed Ruby in coroutine
https://bugs.ruby-lang.org/issues/2294#change-36197

Author: sunaku (Suraj Kurapati)
Status: Assigned
Priority: High
Assignee: ko1 (Koichi Sasada)
Category: core
Target version: 2.0.0


=begin
 Hi,

 I am attaching a "ruby_bind_stack.patch" patch file
 that adds a ruby_bind_stack() function to the Ruby C API.

 This function allows me to inform the GC about the stack
 boundaries of the coroutine inside which Ruby is embedded:

   void ruby_bind_stack(void *lower, void *upper);

 I am also attaching tarballs containing code examples that
 embed Ruby inside two different coroutine environments:
 UNIX System V contexts[1] and libpcl[2] coroutines.

 Each tarball has an "output.log" file which contains the
 result of running `script -c ./run.sh output.log` on my
 machine:

 Linux yantram 2.6.31-ARCH #1 SMP PREEMPT Tue Oct 13 13:36:23 CEST 2009
i686 Intel(R) Pentium(R) D CPU 3.00GHz GenuineIntel GNU/Linux

 The last section in "output.log" corresponds to Ruby @ SVN
 trunk that is patched with the "ruby_bind_stack.patch"
 patch file that is attached to this issue.

 Thanks for your consideration.

 [1]: http://www.gnu.org/s/libc/manual/html_node/System-...
 [2]: http://www.xmailserver.org/libpcl.html

 See also:
 * http://redmine.ruby-lang.org/issues/show/2258
 * http://redmine.ruby-lang.org/issues/show/2126
=end
308cbef6e86dfc49cce3b2d4cf42aedc?d=identicon&s=25 SASADA Koichi (Guest)
on 2013-02-13 07:36
(Received via mailing list)
Hi,

(2013/02/13 14:49), sunaku (Suraj Kurapati) wrote:
> And if Ruby is not told that information, it might make a fatal mistake:
> Ruby might go beyond the coroutine's pre-allocated stack address range.
>
> This can cause memory corruption, segfault crashes, and undefined behavior.
>
>> `ucontext' version do `pre-allocation'.
>
> Yes, in my ruby-coroutine-example application, all versions do pre-allocation.
>
> However, Ruby must also be told (bound to) the coroutine's stack address range.

I think you got misunderstanding.

(1) Ruby interpreter must know machine stack start, not a `range'.
(2) Machine stack start can be grabbed without telling an address
    range explicitly.
    See a r38905.

NOTE: On Ruby 1.8, may run fine on co-routines.
      The patch revert to this behavior.

Without machine-stack-end, we can't detect machine stack overflow. This
is an issue. But not a critical issue.

However, the implantation of `ruby_bind_stack()' has several problems:

- Designed on misunderstanding
- Depend on specific architecture

I agree to add another interface to tell the stack range. But
`ruby_bind_stack()' is not acceptable now. At least Ruby 2.0.0 shouldn't
include it.

Maybe it will be a parameter of an initialize function of the Ruby
interpreter.


> Ruby might go beyond the coroutine's pre-allocated stack address range.

Yes. That's right.


>>> 2.  In my coroutine example, only ucontext works correctly:
>>
>> I checked pthread and it okay...
>>
>> I'll check again soon. Please wait.
>
> Sure, thanks for your consideration.

I believe last patch will solve this issue.
So the problem is based on my misunderstanding or a bug.
I want to solve this issue before 2.0.0 release.

I'll check it soon.
This topic is locked and can not be replied to.