summaryrefslogtreecommitdiff
path: root/gc.c
AgeCommit message (Collapse)Author
2019-11-08Fix passing actual object_id to finalizerJohn Hawthorn
Previously we were passing the memory_id. This was broken previously if compaction was run (which changes the memory_id) and now that object_id is a monotonically increasing number it was always broken. This commit fixes this by defering removal from the object_id table until finalizers have run (for objects with finalizers) and also copying the SEEN_OBJ_ID flag onto the zombie objects. Notes: Merged: https://github.com/ruby/ruby/pull/2658
2019-11-08Renamed `load_*.inc` as `*.rbinc` to utilize a suffix ruleNobuyoshi Nakada
2019-11-08use builtins for GC.Koichi Sasada
Define a part of GC in gc.rb.
2019-11-07Add a counter for compactionAaron Patterson
Keep track of the number of times the compactor ran. I would like to use this as a way to keep track of inline cache reference updates.
2019-11-07Use a monotonically increasing number for object_idJohn Hawthorn
This changes object_id from being based on the objects location in memory (or a nearby memory location in the case of a conflict) to be based on an always increasing number. This number is a Ruby Integer which allows it to overflow the size of a pointer without issue (very unlikely to happen in real programs especially on 64-bit, but a nice guarantee). This changes obj_to_id_tbl and id_to_obj_tbl to both be maps of Ruby objects to Ruby objects (previously they were Ruby object to C integer) which simplifies updating them after compaction as we can run them through gc_update_table_refs. Co-authored-by: Aaron Patterson <tenderlove@ruby-lang.org>
2019-11-06Remove duplicate codeAaron Patterson
These functions are the same, so remove one. Co-authored-by: John Hawthorn <john@hawthorn.email>
2019-11-06Revert "Use a monotonically increasing number for object_id"Aaron Patterson
This reverts commit bd2b314a05ae9192b3143e1e678a37c370d8a9ce.
2019-11-06Use a monotonically increasing number for object_idJohn Hawthorn
This changes object_id from being based on the objects location in memory (or a nearby memory location in the case of a conflict) to be based on an always increasing number. This number is a Ruby Integer which allows it to overflow the size of a pointer without issue (very unlikely to happen in real programs especially on 64-bit, but a nice guarantee). This changes obj_to_id_tbl and id_to_obj_tbl to both be maps of Ruby objects to Ruby objects (previously they were Ruby object to C integer) which simplifies updating them after compaction as we can run them through gc_update_table_refs. Co-authored-by: Aaron Patterson <tenderlove@ruby-lang.org> Notes: Merged: https://github.com/ruby/ruby/pull/2638
2019-11-04Fix zero free objects assertionAaron Patterson
This commit is to attempt fixing this error: http://ci.rvm.jp/results/trunk-gc-asserts@ruby-sky1/2353281 Each non-full heap_page struct contains a reference to the next page that contains free slots. Compaction could fill any page, including pages that happen to be linked to as "pages which contain free slots". To fix this, we'll iterate each page, and rebuild the "free page list" depending on the number of actual free slots on that page. If there are no free slots on the page, we'll set the free_next pointer to NULL. Finally we'll pop one page off the "free page list" and set it as the "using page" for the next allocation.
2019-11-01ruby_mimmalloc can return NULL卜部昌平
malloc can fail. Should treat such situations.
2019-10-30Revert "Fix zero free objects assertion"Aaron Patterson
This reverts commit e1bf29314feee6aaf535917da0178e868e7ff3fa. I'm not sure why this broke stuff, I need to investigate later.
2019-10-30Fix zero free objects assertionAaron Patterson
This commit is to attempt fixing this error: http://ci.rvm.jp/results/trunk-gc-asserts@ruby-sky1/2353281 Each non-full heap_page struct contains a reference to the next page that contains free slots. Compaction could fill any page, including pages that happen to be linked to as "pages which contain free slots". To fix this, we'll iterate each page, and rebuild the "free page list" depending on the number of actual free slots on that page. If there are no free slots on the page, we'll set the free_next pointer to NULL. Finally we'll pop one page off the "free page list" and set it as the "using page" for the next allocation.
2019-10-29Compacting the heap can cause GC, so disable itAaron Patterson
When we compact the heap, various st tables are updated, particularly the table that contains the object id map. Updating an st table can cause a GC to occur, and we need to prevent any GC from happening while moving or updating references.
2019-10-28Revert "Protect finalizer references during execution"Aaron Patterson
This reverts commit 60a7f9f446604571f8a81499080c57c47baf0e6b. We can't have Ruby objects pointing at T_ZOMBIE objects otherwise we get an error in the GC. We need to find a different way to update references.
2019-10-28Protect finalizer references during executionAaron Patterson
When we run finalizers we have to copy all of the finalizers to a new data structure because a finalizer could add another finalizer and we need to keep draining the "real" finalizer table until it's empty. We don't want Ruby programs to mutate the finalizers that we're iterating over as well. Before this commit we would copy the finalizers in to a linked list. The problem with this approach is that if compaction happens, the linked list will need to be updated. But the GC doesn't know about the existence of the linked list, so it could not update references. This commit changes the linked list to be a Ruby array so that when compaction happens, the arrays will automatically be updated and all references remain valid.
2019-10-28Marshal is calling functions that should pin thingsAaron Patterson
2019-10-18Make weakmap finalizer an ifunc lambdaNobuyoshi Nakada
Simple comparison between proc/ifunc/method invocations: ``` proc 15.209M (± 1.6%) i/s - 76.138M in 5.007413s ifunc 15.195M (± 1.7%) i/s - 76.257M in 5.020106s method 9.836M (± 1.2%) i/s - 49.272M in 5.009984s ``` As `proc` and `ifunc` have no significant difference, chosen the latter for arity check.
2019-10-18Use identhash as WeakMapNobuyoshi Nakada
As ObjectSpace::WeakMap allows FLONUM as a key, needs the special deal for its hash. [Feature #16035]
2019-10-10make rb_raise a GVL-only function again卜部昌平
Requested by ko1 that ability of calling rb_raise from anywhere outside of GVL is "too much". Give up that part, move the GVL aquisition routine into gc.c, and make our new gc_raise().
2019-10-10negative_size_allocation_error never returnsNobuyoshi Nakada
2019-10-10allow rb_raise from outside of GVL卜部昌平
Now that allocation routines like ALLOC_N() can raise exceptions on integer overflows. This is a problem when the calling thread has no GVL. Memory allocations has been allowed without it, but can still fail. Let's just relax rb_raise's restriction so that we can call it with or without GVL. With GVL the behaviour is unchanged. With no GVL, wait for it. Also, integer overflows can theoretically occur during GC when we expand the object space. We cannot do so much then. Call rb_memerror and let that routine abort the process.
2019-10-10fix memory corruption in old GCC卜部昌平
This typo introduced memory corruption when __builtin_add_overflow is not available but uint128_t is. GCC before 5 are one of such situatins. See also https://rubyci.org/logs/rubyci.s3.amazonaws.com/opensuseleap/ruby-master/log/20191009T120004Z.log.html.gz
2019-10-09Prefer st_is_member over st_lookup with 0Ben Woosley
The st_is_member DEFINE has simpler semantics, for more readable code. Notes: Merged: https://github.com/ruby/ruby/pull/1622
2019-10-09avoid returning NULL from xrealloc卜部昌平
This changeset is to kill future possibility of bugs similar to CVE-2019-11932. The vulnerability occurs when reallocarray(3) (which is a variant of realloc(3) and roughly resembles our ruby_xmalloc2()) returns NULL. In our C API, ruby_xmalloc() never returns NULL to raise NoMemoryError instead. ruby_xfree() does not return NULL by definition. ruby_xrealloc() on the other hand, _did_ return NULL, _and_ also raised sometimes. It is very confusing. Let's not do that. x-series APIs shall raise on error and shall not return NULL. Notes: Merged: https://github.com/ruby/ruby/pull/2540
2019-10-09avoid overflow in integer multiplication卜部昌平
This changeset basically replaces `ruby_xmalloc(x * y)` into `ruby_xmalloc2(x, y)`. Some convenient functions are also provided for instance `rb_xmalloc_mul_add(x, y, z)` which allocates x * y + z byes. Notes: Merged: https://github.com/ruby/ruby/pull/2540
2019-10-07Do not free too many pages.Aaron Patterson
Sweep step checks `heap_pages_freeable_pages`, so compaction should do the same.
2019-10-07Move empty pages to the tombAaron Patterson
I think we need to be moving empty pages to the tomb after they become empty.
2019-10-07Eliminate second GC pass for eliminating T_MOVEDAaron Patterson
`T_MOVED` is a linked list, so we can just iterate through the `T_MOVED` objects, clearing them out and adding them to respective free lists.
2019-10-04IMEMO objects don't have a class, so return earlyAaron Patterson
IMEMO objects don't have a class field to update, so we need to return early, otherwise it can cause a segv.
2019-10-04Don't allocate objects in `gc_compact`Aaron Patterson
I'd like to call `gc_compact` after major GC, but before the GC finishes. This means we can't allocate any objects inside `gc_compact`. So in this commit I'm just pulling the compaction statistics allocation outside the `gc_compact` function so we can safely call it.
2019-10-05Fix potential memory leaks by `rb_imemo_tmpbuf_auto_free_pointer`Nobuyoshi Nakada
This function has been used wrongly always at first, "allocate a buffer then wrap it with tmpbuf". This order can cause a memory leak, as tmpbuf creation also can raise a NoMemoryError exception. The right order is "create a tmpbuf then allocate&wrap a buffer". So the argument of this function is rather harmful than just useless. TODO: * Rename this function to more proper name, as it is not used "temporary" (function local) purpose. * Allocate and wrap at once safely, like `ALLOCV`.
2019-10-03Revert https://github.com/ruby/ruby/pull/2486卜部昌平
This reverts commits: 10d6a3aca7 8ba48c1b85 fba8627dc1 dd883de5ba 6c6a25feca 167e6b48f1 7cb96d41a5 3207979278 595b3c4fdd 1521f7cf89 c11c5e69ac cf33608203 3632a812c0 f56506be0d 86427a3219 . The reason for the revert is that we observe ABA problem around inline method cache. When a cache misshits, we search for a method entry. And if the entry is identical to what was cached before, we reuse the cache. But the commits we are reverting here introduced situations where a method entry is freed, then the identical memory region is used for another method entry. An inline method cache cannot detect that ABA. Here is a code that reproduce such situation: ```ruby require 'prime' class << Integer alias org_sqrt sqrt def sqrt(n) raise end GC.stress = true Prime.each(7*37){} rescue nil # <- Here we populate CC class << Object.new; end # These adjacent remove-then-alias maneuver # frees a method entry, then immediately # reuses it for another. remove_method :sqrt alias sqrt org_sqrt end Prime.each(7*37).to_a # <- SEGV ```
2019-09-30refactor constify most of rb_method_entry_t卜部昌平
Now that we have eliminated most destructive operations over the rb_method_entry_t / rb_callable_method_entry_t, let's make them mostly immutabe and mark them const. One exception is rb_export_method(), which destructively modifies visibilities of method entries. I have left that operation as is because I suspect that destructiveness is the nature of that function. Notes: Merged: https://github.com/ruby/ruby/pull/2486
2019-09-30refactor constify most of rb_method_definition_t卜部昌平
Most (if not all) of the fields of rb_method_definition_t are never meant to be modified once after they are stored. Marking them const makes it possible for compilers to warn on unintended modifications. Notes: Merged: https://github.com/ruby/ruby/pull/2486
2019-09-27Adjusted spaces [ci skip]Nobuyoshi Nakada
2019-09-26Add compaction support to `rb_ast_t`Aaron Patterson
This commit adds compaction support to `rb_ast_t`.
2019-08-29Allow non-finalizable objects in ObjectSpace::WeakMapJean Boussier
[feature #16035] This goes one step farther than what nobu did in [feature #13498] With this patch, special objects such as static symbols, integers, etc can be used as either key or values inside WeakMap. They simply don't have a finalizer defined on them. This is useful if you need to deduplicate value objects Notes: Merged: https://github.com/ruby/ruby/pull/2313
2019-08-29drop-in type check for rb_define_singleton_method卜部昌平
We can check the function pointer passed to rb_define_singleton_method like how we do so in rb_define_method. Doing so revealed many arity mismatches.
2019-08-27st_foreach now free from ANYARGS卜部昌平
After 5e86b005c0f2ef30df2f9906c7e2f3abefe286a2, I now think ANYARGS is dangerous and should be extinct. This commit deletes ANYARGS from st_foreach. I strongly believe that this commit should have had come with b0af0592fdd9e9d4e4b863fde006d67ccefeac21, which added extra parameter to st_foreach callbacks.
2019-08-27rb_proc_new / rb_fiber_new now free from ANYARGS卜部昌平
After 5e86b005c0f2ef30df2f9906c7e2f3abefe286a2, I now think ANYARGS is dangerous and should be extinct. This commit deletes ANYARGS from rb_proc_new / rb_fiber_new, and applies RB_BLOCK_CALL_FUNC_ARGLIST wherever necessary.
2019-08-27rb_ensure now free from ANYARGS卜部昌平
After 5e86b005c0f2ef30df2f9906c7e2f3abefe286a2, I now think ANYARGS is dangerous and should be extinct. This commit deletes ANYARGS from rb_ensure, which also revealed many arity / type mismatches.
2019-08-26this iv table should also use the new update functionAaron Patterson
2019-08-26Try only updating hash value referencesAaron Patterson
I'm afraid the keys to this hash are just integers, and those integers may look like VALUE pointers when they are not. Since we don't mark the keys to this hash, it's probably safe to say that none of them have moved, so we shouldn't try to update the references either.
2019-08-26Make `gc_update_table_refs` match `mark_tbl_no_pin` a little more closelyAaron Patterson
This commit just makes `gc_update_table_refs` match `mark_tbl_no_pin` more closely.
2019-08-21`rp(obj)` shows func, file and line. (#2394)Koichi Sasada
rp() macro for debug also shows file location and function name such as: [OBJ_INFO:rb_call_inits@inits.c:73] 0x000056147741b248 ... Notes: Merged-By: ko1
2019-08-18Fix document of `GC.start` (#2382)Masataka Pocke Kuwabara
2019-08-13* expand tabs.git
2019-08-13Removed non-VM_OBJSPACE codeNobuyoshi Nakada
It has not been used for 4 years, since r60856, e33b1690d06f867e45750bd8e3e8b06d78b5bc26.
2019-08-13Refactored `objspace_each_objects`Nobuyoshi Nakada
As `rb_objspace_each_objects_without_setup` doesn't reset and restore `dont_incremental` flag, renamed the bare iterator as `objspace_each_objects_without_setup`. `objspace_each_objects` calls it when called with the flag disabled, wrap the arguments otherwise only.
2019-08-13Move rb_objspace_t* in objspace_reachable_objects_from_root to an argumentNobuyoshi Nakada