Ruby Issue Tracking System: Issueshttps://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112015-03-10T15:45:40ZRuby Issue Tracking System
Redmine Ruby master - Bug #10959 (Closed): rb_iseq_load: problems with hash containing symbolic keyshttps://redmine.ruby-lang.org/issues/109592015-03-10T15:45:40Zalvoskov (Alexey Voskov)alvoskov@gmail.com
<p>Hello!<br>
I've found a bug in the rb_iseq_load function. When I'm trying to load the next code:</p>
<p><code>def func @basis << {:a => 1} end</code></p>
<p>compiled by <code>RubyVM::InstructionSequence.compile_file(filename).to_a</code> call it throws an exception "wrong argument type false (expected Symbol). If :a is replaced to "a" the precompiled code loads correctly. All files required for reproducing this bug is attached to the report.</p> Ruby master - Bug #8543 (Closed): new rb_iseq_load crashhttps://redmine.ruby-lang.org/issues/85432013-06-19T21:39:35Zalvoskov (Alexey Voskov)alvoskov@gmail.com
<p>I noticed an unusual behaviour of undocumented rb_iseq_load function.<br>
Its work differs in different Ruby versions. I'm trying to protect some Ruby<br>
source code by its conversion to YARV p-code and using the next strategy:</p>
<ol>
<li>
<p>Convert code to array</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">data</span> <span class="o">=</span> <span class="no">RubyVM</span><span class="o">::</span><span class="no">InstructionSequence</span><span class="p">.</span><span class="nf">compile_file</span><span class="p">(</span><span class="s1">'hello.rb'</span><span class="p">).</span><span class="nf">to_a</span>
</code></pre>
</li>
<li>
<p>Pass a compiled source to the rb_iseq_load function and evaluate it</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">iseq</span> <span class="o">=</span> <span class="n">iseq_load</span><span class="o">.</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">iseq</span><span class="p">.</span><span class="nf">eval</span>
</code></pre>
</li>
</ol>
<p>Sample programs are supplied in the attachments.<br>
"hello.rb"</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">puts</span> <span class="s2">"tralivali"</span>
<span class="k">def</span> <span class="nf">funct</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">)</span>
<span class="n">a</span><span class="o">**</span><span class="n">b</span>
<span class="k">end</span>
<span class="mi">3</span><span class="p">.</span><span class="nf">times</span> <span class="p">{</span> <span class="o">|</span><span class="n">i</span><span class="o">|</span>
<span class="nb">puts</span> <span class="s2">"Hello, world</span><span class="si">#{</span><span class="n">funct</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="n">i</span><span class="p">)</span><span class="si">}</span><span class="s2">!"</span>
<span class="p">}</span>
</code></pre>
<p>The differences<br>
Ruby 1.9.3 (ruby 1.9.3p194 (2012-04-20) [i386-mingw32])<br>
Correct work. Output:</p>
<pre><code>tralivali
Hello, world1!
Hello, world2!
Hello, world4!
</code></pre>
<p>Ruby 2.0.0 (ruby 2.0.0p193 (2013-05-14) [x64-mingw32])<br>
Incorrect work (omits the code inside code blocks). Output</p>
<pre><code>tralivali
</code></pre>
<p>Attempts of loading bigger programs by means of rb_iseq_load in Ruby 2.0.0 usually ends with a segmentation fault.</p>
<p>Such behaviour also can be reproduced by means of iseq Ruby extension ("for iseq freaks")<br>
<a href="https://github.com/wanabe/iseq" class="external">https://github.com/wanabe/iseq</a></p>
<p>P.S. I understand that it is an undocumented feature.</p>