1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/test/-ext-
Alan Wu c06ddfee87
str_duplicate: Don't share with a frozen shared string
This is a follow up for 3f9562015e.
Before this commit, it was possible to create a shared string which
shares with another shared string by passing a frozen shared string
to `str_duplicate`.

Such string looks like:

```
 --------                    -----------------
 | root | ------ owns -----> | root's buffer |
 --------                    -----------------
     ^                             ^   ^
 -----------                       |   |
 | shared1 | ------ references -----   |
 -----------                           |
     ^                                 |
 -----------                           |
 | shared2 | ------ references ---------
 -----------
```

This is bad news because `rb_fstring(shared2)` can make `shared1`
independent, which severs the reference from `shared1` to `root`:

```c
/* from fstr_update_callback() */
str = str_new_frozen(rb_cString, shared2);  /* can return shared1 */
if (STR_SHARED_P(str)) { /* shared1 is also a shared string */
    str_make_independent(str);  /* no frozen check */
}
```

If `shared1` was the only reference to `root`, then `root` can be
reclaimed by the GC, leaving `shared2` in a corrupted state:

```
 -----------                         --------------------
 | shared1 | -------- owns --------> | shared1's buffer |
 -----------                         --------------------
      ^
      |
 -----------                         -------------------------
 | shared2 | ------ references ----> | root's buffer (freed) |
 -----------                         -------------------------
```

Here is a reproduction script for the situation this commit fixes.

```ruby
a = ('a' * 24).strip.freeze.strip
-a
p a
4.times { GC.start }
p a
```

 - string.c (str_duplicate): always share with the root string when
   the original is a shared string.
 - test_rb_str_dup.rb: specifically test `rb_str_dup` to make
   sure it does not try to share with a shared string.

[Bug #15792]

Closes: https://github.com/ruby/ruby/pull/2159
2019-05-09 10:04:19 +09:00
..
arith_seq Add test cases of rb_arithmetic_sequence_extract 2018-12-12 21:17:04 +00:00
array
bignum
bug_reporter test_bug_reporter.rb: make it work with --jit 2018-04-28 09:02:56 +00:00
class
debug * remove trailing spaces. 2018-09-21 06:41:09 +00:00
exception
file
float
funcall
gvl introduce rb_nogvl C-API to mark ubf as async-signal-safe 2019-01-04 13:14:11 +00:00
hash
integer numeric.c: fix infinite loop 2019-03-11 01:37:16 +00:00
iseq_load test_iseq_load.rb: increase timeout for stressful roundtrip 2018-12-29 01:53:10 +00:00
iter
load
marshal
method
num2int
path_to_class
popen_deadlock
postponed_job
proc
rational
st
string str_duplicate: Don't share with a frozen shared string 2019-05-09 10:04:19 +09:00
struct
symbol Symbol creation is not a problem now unless pinned down 2019-03-06 11:49:53 +00:00
thread_fd_close revert revert of r65285 2018-10-21 23:20:32 +00:00
time
tracepoint
typeddata
vm
wait_for_single_fd test_wait_for_single_fd.rb: remove with_pipe helper 2018-05-14 23:51:39 +00:00
win32
test_bug-3571.rb
test_bug-5832.rb
test_notimplement.rb Add more assertions for NotImplementedError of instance method 2018-08-21 14:57:56 +00:00
test_printf.rb
test_recursion.rb
test_scan_args.rb The combination of non-Symbol keys and Symbol keys is now allowed again 2019-03-11 12:48:33 +00:00