diff options
author | normal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-07-18 02:29:59 +0000 |
---|---|---|
committer | normal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-07-18 02:29:59 +0000 |
commit | d04c085b3c39d7e3893620c8c64aaf89269d2d2c (patch) | |
tree | 187b42aea525bc09a90e51111c161aee5cc0043f /test/ruby/test_optimization.rb | |
parent | ed831a6c5b6a4f7331ffba5f8b4a2f1d079217ab (diff) |
hash: keep fstrings of tainted strings for string keys
The same hash keys may be loaded from tainted data sources
frequently (e.g. parsing headers from socket or loading
YAML data from a file). If a non-tainted fstring already
exists (because the application expects the hash key),
cache and deduplicate the tainted version in the new
tainted_frozen_strings table.
For non-embedded strings, this also allows sharing with the
underlying malloc-ed data.
* vm_core.h (rb_vm_struct): add tainted_frozen_strings
* vm.c (ruby_vm_destruct): free tainted_frozen_strings
(Init_vm_objects): initialize tainted_frozen_strings
(rb_vm_tfstring_table): accessor for tainted_frozen_strings
* internal.h: declare rb_fstring_existing, rb_vm_tfstring_table
* hash.c (fstring_existing_str): remove (moved to string.c)
(hash_aset_str): use rb_fstring_existing
* string.c (rb_fstring_existing): new, based on fstring_existing_str
(tainted_fstr_update): new
(rb_fstring_existing0): new, based on fstring_existing_str
(rb_tainted_fstring_existing): new, special case for tainted strings
(rb_str_free): delete from tainted_frozen_strings table
* test/ruby/test_optimization.rb (test_hash_reuse_fstring): new test
[ruby-core:82012] [Bug #13737]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59354 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby/test_optimization.rb')
-rw-r--r-- | test/ruby/test_optimization.rb | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/test/ruby/test_optimization.rb b/test/ruby/test_optimization.rb index 2d0eec02fd..0d14c9c932 100644 --- a/test/ruby/test_optimization.rb +++ b/test/ruby/test_optimization.rb @@ -181,6 +181,37 @@ class TestRubyOptimization < Test::Unit::TestCase assert_redefine_method('Hash', 'empty?', 'assert_nil({}.empty?); assert_nil({1=>1}.empty?)') end + def test_hash_reuse_fstring + embed = -'h e l l o' + shared = -'this string is too long to be embedded and should be shared' + assert_not_equal GC::INTERNAL_CONSTANTS[:RVALUE_SIZE], + ObjectSpace.memsize_of(shared), + 'update this test if string embedding changed' + + [ embed, shared ].each do |exp| + key = exp.split(' ').join(' ') + h = {} + h[key] = true + assert_not_same key, h.keys[0] + assert_predicate h.keys[0], :frozen? + assert_same exp, h.keys[0] + + h1 = {} + h2 = {} + key.taint + h1[key] = true + h2[key] = true + k1 = h1.keys[0] + k2 = h2.keys[0] + assert_same k1, k2 + assert_predicate k1, :tainted? + + assert_equal GC::INTERNAL_CONSTANTS[:RVALUE_SIZE], + ObjectSpace.memsize_of(k1), + 'tainted string should share with untainted fstring' + end + end + def test_hash_aref_with h = { "foo" => 1 } assert_equal 1, h["foo"] |