* Allow access to a struct's underlying memory with `struct[offset, length]`.
* Make accessing a struct's underlying memory more convenient.
* refactor memory access unit tests for improved clarity
https://github.com/ruby/fiddle/commit/c082c81bb5
If a module has an origin, and that module is included in another
module or class, previously the iclass created for the module had
an origin pointer to the module's origin instead of the iclass's
origin.
Setting the origin pointer correctly requires using a stack, since
the origin iclass is not created until after the iclass itself.
Use a hidden ruby array to implement that stack.
Correctly assigning the origin pointers in the iclass caused a
use-after-free in GC. If a module with an origin is included
in a class, the iclass shares a method table with the module
and the iclass origin shares a method table with module origin.
Mark iclass origin with a flag that notes that even though the
iclass is an origin, it shares a method table, so the method table
should not be garbage collected. The shared method table will be
garbage collected when the module origin is garbage collected.
I've tested that this does not introduce a memory leak.
This change caused a VM assertion failure, which was traced to callable
method entries using the incorrect defined_class. Update
rb_vm_check_redefinition_opt_method and find_defined_class_by_owner
to treat iclass origins different than class origins to avoid this
issue.
This also includes a fix for Module#included_modules to skip
iclasses with origins.
Fixes [Bug #16736]
If a module has an origin, and that module is included in another
module or class, previously the iclass created for the module had
an origin pointer to the module's origin instead of the iclass's
origin.
Setting the origin pointer correctly requires using a stack, since
the origin iclass is not created until after the iclass itself.
Use a hidden ruby array to implement that stack.
Correctly assigning the origin pointers in the iclass caused a
use-after-free in GC. If a module with an origin is included
in a class, the iclass shares a method table with the module
and the iclass origin shares a method table with module origin.
Mark iclass origin with a flag that notes that even though the
iclass is an origin, it shares a method table, so the method table
should not be garbage collected. The shared method table will be
garbage collected when the module origin is garbage collected.
I've tested that this does not introduce a memory leak.
This also includes a fix for Module#included_modules to skip
iclasses with origins.
Fixes [Bug #16736]
rb_callable_method_entry() creates ccs entry in cc_tbl, but this
code overwrite by insert newly created ccs and overwrote ccs never
freed.
[Bug #16900]
instead of :TOTAL of ObjectSpace.count_objects.
This test had failed very occasionally:
https://rubyci.org/logs/rubyci.s3.amazonaws.com/centos8/ruby-master/log/20200521T033004Z.fail.html.gz
```
1) Failure:
TestRubyOptimization#test_block_parameter_should_not_create_objects [/home/chkbuild/chkbuild/tmp/build/20200521T033004Z/ruby/test/ruby/test_optimization.rb:713]:
<0> expected but was
<407>.
```
This test of lazy proc creation checks if no object is created during a
method call. However, calling a method itself increases the count of
objects because method cache is now an object (T_MEMO).
The reason why this test rarely fails is because the test was buggy; it
checked the count of :TOTAL, but :TOTAL count changes only when the GC
heap is expanded. Creating one object rarely causes heap expansion.
The test must have checked not only :TOTAL but also the count of :FREE.
Instead, this change more directly checks :T_DATA. Note that a Proc
object is T_DATA.
Ruby's GC is incremental, meaning that during the mark phase (and also
the sweep phase) programs are allowed to run. This means that programs
can allocate objects before the mark or sweep phase have actually
completed. Those objects may not have had a chance to be marked, so we
can't know if they are movable or not. Something that references the
newly created object might have called the pinning function during the
mark phase, but since the mark phase hasn't run we can't know if there
is a "pinning" relationship.
To be conservative, we must only allow objects that are not pinned but
also marked to move.
The GC will not disassemble incomplete instruction sequences. So it is
important that when instructions are being assembled, any objects the
instructions point at should not be moved.
This patch implements a fixed width array that pins its references.
When the instructions are done being assembled, the pinning array goes
away and the objects inside the iseqs are allowed to move.
We observed combination of multiple __builtin_assume() can cause
strange compile results on clang-6 (-DNDEBUG exposed this issue).
To avoid this problem, __builtin_assume() is from clang-7.
https://github.com/ruby/ruby/pull/3120#issuecomment-630821333