14 Commits

Author SHA1 Message Date
Peter Zhu
12102d101a Fix crash in WeakMap during compaction
WeakMap can crash during compaction because the st_insert could allocate
memory.
2023-09-06 14:20:23 -04:00
Peter Zhu
9a8398a18f Introduce rb_gc_remove_weak
If we're during incremental marking, then Ruby code can execute that
deallocates certain memory buffers that have been called with
rb_gc_mark_weak, which can cause use-after-free bugs.
2023-09-05 14:32:15 -04:00
Peter Zhu
06a1d16dc2 Reuse allocated buffer in WeakMap
If the key exists in WeakMap and WeakKeyMap, then we can reuse the
buffer and we can avoid an allocation.
2023-09-05 14:32:15 -04:00
Peter Zhu
f5c8bdaa8a Implement WeakKeyMap using weak references 2023-08-25 09:01:21 -04:00
Peter Zhu
ee9cc8e32e Implement WeakMap using weak references 2023-08-25 09:01:21 -04:00
Burdette Lamar
8c5b9ebf71
[DOC] Improve doc guide compliance (#8221) 2023-08-15 14:43:58 -04:00
Jean Boussier
52449b5b75 Implement ObjectSpace::WeakMap#delete and ObjectSpace::WeakKeyMap#delete
[Feature #19561]

It's useful to be able to remove references from weak maps.
2023-04-15 16:29:46 +02:00
Jean Boussier
a1db5ecd93 Add specs for ObjectSpace::WeakKeyMap
[Feature #18498]
2023-04-15 02:10:38 +02:00
Jean Boussier
3592b24cdc ObjectSpace::WeakMap: clean inverse reference when an entry is re-assigned
[Bug #19531]

```ruby
wmap[1] = "A"
wmap[1] = "B"
```

In the example above, we need to remove the `"A" => 1` inverse reference
so that when `"A"` is GCed the `1` key isn't deleted.
2023-03-17 17:50:08 +00:00
Peter Zhu
e0cf80d666 Fix incorrect size of WeakMap buffer
In wmap_final_func, j is the number of elements + 1 (since j also
includes the length at the 0th index), so we should resize the buffer
to size j and the new length is j - 1.
2023-03-16 10:00:02 -04:00
Peter Zhu
3dc8cde700 Fix crash during compaction
[Bug #19529]

The fix for [Bug #19529] in commit 548086b contained a bug that crashes
on the following script:

```
wm = ObjectSpace::WeakMap.new
obj = Object.new
100.times do
  wm[Object.new] = obj
  GC.start
end
GC.compact
```
2023-03-14 23:18:11 -04:00
Jean Boussier
548086b34e ObjectSpace::WeakMap: fix compaction support
[Bug #19529]

`rb_gc_update_tbl_refs` can't be used on `w->obj2wmap` because it's
not a `VALUE -> VALUE` table, but a `VALUE -> VALUE *` table, so
we need some dedicated iterator.
2023-03-14 16:49:23 +01:00
Jean Boussier
9bb4397875 Mark weak maps as write barrier protected
For both we mark the lambda finalizer.

ObjectSpace::WeakMap doesn't mark any other reference, so we can just add the flag.

ObjectSpace::WeakKeyMap only ever add new refs in `wkmap_aset`, so we can just trigger the write barrier there.
2023-03-10 21:15:20 +01:00
Peter Zhu
f98a7fd28d Move WeakMap and WeakKeyMap code to weakmap.c
These classes don't belong in gc.c as they're not actually part of the
GC. This commit refactors the code by moving all the code into a
weakmap.c file.
2023-03-10 09:32:10 -05:00