Issue #12237 has been updated by Tomoyuki Chikanaga.

Backport changed from 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN to 2.1: REQUIRED, 2.2: REQUIRED, 2.3: REQUIRED

----------------------------------------
Bug #12237: Coverage keeps tracking counts even after Coverage.result
https://bugs.ruby-lang.org/issues/12237#change-57901

* Author: Benoit Daloze
* Status: Closed
* Priority: Normal
* Assignee: Yusuke Endoh
* ruby -v: ruby 2.4.0dev (2016-03-31 trunk 54454) [x86_64-linux]
* Backport: 2.1: REQUIRED, 2.2: REQUIRED, 2.3: REQUIRED
----------------------------------------
While looking at #12220, I found a bug.
Attached is a patch that reveals it.

Running
$ make test-all TESTS="test/coverage -n /test_restarting_coverage/"
gives
[1/1] TestCoverage#test_restarting_coverage
Updating cov for /tmp/d20160331-27149-q8o6co/test.rb:1 : 1 size=3 # OK
Updating cov for /tmp/d20160331-27149-q8o6co/test.rb:2 : 1 size=0 # This did RARAAY_AREF(,2) on an array of size 0!
{"/tmp/d20160331-27149-q8o6co/test.rb"=>[]}
Updating cov for /tmp/d20160331-27149-q8o6co/test.rb:2 : 2 size=0 # This did RARAAY_AREF(,4) on an array of size 0!
Updating cov for /tmp/d20160331-27149-q8o6co/test2.rb:1 : 1 size=4 # OK
{"/tmp/d20160331-27149-q8o6co/test.rb"=>[], "/tmp/d20160331-27149-q8o6co/test2.rb"=>[1, nil, 0, nil]}

Coverage tracking of files after Coverage.result (= reset) should just disable itself.
Since the current strategy is to rb_ary_clear() these arrays referenced by the iseq, one should check for bounds instead, as in the commented code.

Ideally, it could even rewrite the trace coverage instruction to a no-op, but that's more involved and might have few real use-cases.
Note that these arrays also forever leak and it would seem rb_ary_clear() does not always free the storage but just set the size to 0.

Can I commit the fix in thread.c?
I think it should be back-ported as well as it could be accessing arrays out-of-bounds.

---Files--------------------------------
0001-Demonstrate-Coverage-bug.patch (2.42 KB)
0001-thread.c-update_coverage-Do-not-track-coverage-in-lo.patch (1.88 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>