From d04c085b3c39d7e3893620c8c64aaf89269d2d2c Mon Sep 17 00:00:00 2001 From: normal Date: Tue, 18 Jul 2017 02:29:59 +0000 Subject: 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 --- hash.c | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) (limited to 'hash.c') diff --git a/hash.c b/hash.c index c47f21ee9f..6ae13ba1b4 100644 --- a/hash.c +++ b/hash.c @@ -18,7 +18,6 @@ #include "probes.h" #include "id.h" #include "symbol.h" -#include "gc.h" #ifdef __APPLE__ # ifdef HAVE_CRT_EXTERNS_H @@ -1516,33 +1515,13 @@ hash_aset(st_data_t *key, st_data_t *val, struct update_arg *arg, int existing) return ST_CONTINUE; } -static VALUE -fstring_existing_str(VALUE str) -{ - st_data_t fstr; - st_table *tbl = rb_vm_fstring_table(); - - if (st_lookup(tbl, str, &fstr)) { - if (rb_objspace_garbage_object_p(fstr)) { - return rb_fstring(str); - } - else { - return (VALUE)fstr; - } - } - else { - return Qnil; - } -} - static int hash_aset_str(st_data_t *key, st_data_t *val, struct update_arg *arg, int existing) { if (!existing && !RB_OBJ_FROZEN(*key)) { VALUE k; - if (!RB_OBJ_TAINTED(*key) && - (k = fstring_existing_str(*key)) != Qnil) { + if ((k = rb_fstring_existing(*key)) != Qnil) { *key = k; } else { -- cgit v1.2.3