ruby/test
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
..
-ext- str_duplicate: Don't share with a frozen shared string 2019-05-09 10:04:19 +09:00
base64
benchmark
bigdecimal Import bigdecimal-1.4.0.pre.20181220a 2018-12-20 23:29:49 +00:00
cgi
coverage Introduce pattern matching [EXPERIMENTAL] 2019-04-17 06:48:03 +00:00
csv Import CSV 3.0.9 2019-04-15 02:05:03 +00:00
date date: support for Reiwa, new Japanese era 2019-04-09 01:27:36 +00:00
dbm
digest
drb ignore test_RangeError 2019-05-04 23:32:13 +09:00
dtrace test/dtrace: use TracePoint.__enable 2018-12-11 09:49:31 +00:00
erb erb.rb: prohibit marshaling [EXPERIMENTAL] 2019-03-28 03:46:48 +00:00
etc
excludes Isolate TestGCCompact from JIT testing 2019-04-27 12:28:52 +09:00
fiddle test/fiddle/test_function.rb: loosen timeout 2018-12-23 01:40:33 +00:00
fileutils Guard out the test when it is run under root permission 2019-03-14 01:04:53 +00:00
gdbm
io test_io_wait.rb: try to stabilize test_wait_readable 2018-12-31 12:54:40 +00:00
irb Use Ripper for IRB 2019-04-30 14:40:06 +09:00
json Merge json-2.2.0 from flori/json. 2019-02-23 03:43:58 +00:00
lib Make only `mesg` can be assigned with default `fname` 2019-04-29 13:42:50 +09:00
logger
matrix lib/matrix.rb: Make Matrix & Vector mutable. Add #[]=, #map!. 2018-11-02 17:52:51 +00:00
minitest Use github url instead of ViewVC. 2019-04-22 20:00:32 +09:00
misc
mkmf test/mkmf/test_flags.rb: try to suppress random CI failure 2018-12-27 16:24:46 +00:00
monitor lib/monitor.rb: avoid race conditions by Thread.handle_interrupt 2018-11-28 01:51:44 +00:00
net test/net/fixtures/Makefile: moved from test/net/imap 2019-01-02 04:18:34 +00:00
nkf
objspace fix imemo entry description. 2019-02-13 04:24:59 +00:00
open-uri
openssl ext/openssl/ossl_bn.c (ossl_bn_initialize): get rid of SEGV 2019-04-10 12:57:39 +00:00
optparse
ostruct ostruct.rb: Accept block for to_h [#15451]. 2018-12-22 17:05:03 +00:00
pathname STATX_BTIME depends on filesystems 2019-02-22 06:43:11 +00:00
psych Make rubygems follow the upstream of psych 2018-11-11 00:20:27 +00:00
rdoc Fix a typo in an assertion message 2019-03-30 12:35:13 +00:00
readline Skip on Windows now when using reline because it causes hang of whole tests 2019-04-30 19:45:44 +09:00
reline Silence a (probable) debug print 2019-05-01 20:55:08 +09:00
resolv Fix a typo [ci skip] 2018-11-11 11:58:38 +00:00
rexml Really ensure loading REXML in tests 2018-12-20 04:03:10 +00:00
rinda Add debug message for test_rinda crash 2019-04-07 08:18:09 +00:00
ripper require 'stringio' 2019-05-09 09:23:44 +09:00
rss Upgrade RSS to 0.2.8 2019-01-25 06:35:04 +00:00
ruby Avoid triggering autoload in Module#const_defined?(String) 2019-05-07 21:20:01 +09:00
rubygems Update rubygems with latest upstream changes 2019-04-28 11:07:45 +09:00
scanf
sdbm
shell
socket disable non-blocking pipes and sockets by default 2018-11-29 20:00:00 +00:00
stringio setbyte / ungetbyte allow out-of-range integers 2019-01-15 06:41:58 +00:00
strscan
syslog
testunit Trim MJIT output from TestHideSkip 2019-05-08 17:30:15 +09:00
uri
webrick Added to check MJIT constant for the Ruby 2.3-2.5. 2019-03-11 10:09:48 +00:00
win32ole test_win32ole_event.rb: use better retry strategy 2018-12-24 03:50:39 +00:00
yaml
zlib zlib (gzfile_write_raw): do not resize string after .write call 2018-12-07 07:09:31 +00:00
colors
runner.rb
test_abbrev.rb
test_cmath.rb
test_delegate.rb
test_extlibs.rb
test_find.rb
test_forwardable.rb
test_ipaddr.rb Import ipaddr 1.2.2 2018-12-18 05:09:08 +00:00
test_mutex_m.rb
test_observer.rb
test_open3.rb use :chdir option to avoid fd 3 to work with Windows. 2018-12-13 08:53:15 +00:00
test_pp.rb test_pp.rb: show the node when failed 2018-12-03 00:57:06 +00:00
test_prettyprint.rb
test_prime.rb
test_pstore.rb
test_pty.rb
test_rbconfig.rb
test_securerandom.rb
test_set.rb
test_shellwords.rb
test_singleton.rb
test_sync.rb
test_syslog.rb
test_tempfile.rb Windows simply causes an error to open invalid path 2019-05-01 17:38:45 +09:00
test_time.rb Test for "%p" in Time.strptime 2019-01-08 07:45:04 +00:00
test_timeout.rb
test_tmpdir.rb tmpdir.rb: permission of user given directory 2019-01-23 06:06:47 +00:00
test_tracer.rb
test_tsort.rb
test_unicode_normalize.rb update to Unicode Version 12.1.0 (beta) 2019-04-05 00:58:51 +00:00
test_weakref.rb Pin weakmap references 2019-04-09 23:03:40 +00:00
test_win32api.rb