SlideShare uma empresa Scribd logo
1 de 64
Baixar para ler offline
Symbol GC 
#rubykaigi 2014 
Narihiro Nakamura - @nari3
Self introduction
Self introduction 
✔ Nari, @nari3, authorNari 
✔ A CRuby committer. 
✔ I work at NaCl. 
✔ “Nakamura” 
✔ is the most powerful clan in Ruby World.
Author 
http://tatsu-zziinnee..ccoomm//bbooookkss//ggccbbooookk
An unmotivated rubyist.
Today's topic 
obj = Object.new 
100_000.times do |i| 
obj.respond_to?("sym#{i}".to_sym) 
end 
GC.start 
puts"symbol : #{Symbol.all_symbols.size}" 
$ ruby-2.1.2 a.rb 
symbol : 102416 
$ ruby-trunk 
symbol : 2833
What is Symbol?
Symbol 
✔ A symbol is a primitive data type whose 
instances have a unique human-readable 
form. 
✔ Symbols can be used as identifiers.
:symbol
A pitfall of Symbol
A pitfall of Symbol 
✔ All symbols are not garbage collected. 
✔ Many beginners don't know this fact. 
✔ Make a mistake even good rubyists. 
✔ Prone to vulnerability 
✔ User input → symbol 
✔ Compress the memory
Simple cases 
✖ if user.respond_to(params[:method].to_sym) 
Is this method callable? 
NG: params[:method] is user input 
✖ params[params[:attr].to_sym] 
Get a value of a hash via a symbol key. 
NG: params[:attr] is user input.
Rails DoS Vulnerability 
CVE-2012-3424 
HTTP Request: GET 
…. 
WWW-Authenticate: 
Digest 
digest = { 
to_sym :realm => “..”, 
to_sym :nonce => “..”, 
realm="..", 
nonce="...", 
algorithm=MD5, 
qop="auth" Parse to a hash 
} 
, 
foo=”xxx”, 
.., 
:foo => “..”, 
to_sym 
. . .,
We want Symbol GC 
✔ There is this request from long time ago. 
✔ Sasada-san has an idea. 
✔ I will implement this idea.
Are symbols in other 
programming 
languages garbage 
collectable?
Programming languages 
which supported for Symbol 
✔ Too Many Parentheses Languages 
✔ Erlang 
✔ Smalltalk 
✔ Scala
Symbol GC support 
Language Symbol GC 
Erlang ✖ 
Gauche ✖ 
Clojure ○ 
EmacsLisp ✖ 
VisualWorks(Smalltalk) ○ 
Scala ○
Implementation dependency? 
✔ Not unified. 
✔ Symbol GC is undocumented in 
programing language specifications. 
✔ Implementation = Specification?
EmacsLisp 
✔ Function: unintern 
✔ (unintern 'foo) 
✔ Declare an unnecessary symbol. 
✔ It's like manual memory management.
Scala 
Java main.scala 
01: val a = 'sym 
Symbol Table String 
“sym”
Scala 
Java main.scala 
01: val a = 'sym 
02: a = null 
String 
“sym” 
Symbol Table 
Weak Reference 
GCG SCT ASRTATRT
Details of CRuby's 
Symbol
“sym”.to_sym 
C Ruby 
global_symbols ““ssyymm”” 
“sym” 
String 
sym_id(hash) 
・ 
・・ 
last_id(long) 
1000 
“sym” freeze 
freeze String 
“sym” 
1001 
Frozen String
“sym”.to_sym 
C Ruby 
global_symbols ““ssyymm”” 
“sym” 
String 
sym_id(hash) 
1001 
・ 
・・ 
last_id(long) 
“sym” freeze 
freeze String 
“sym” 
1001 
ID: 1001 
ID2SYM(ID) 
SYMBOL 
(VALUE) 
:sym 
Frozen String
ID 
✔ ID: Used by C Level. 
✔ Store ID to a method table or a variable table. 
✔ An unique number that corresponds to a symbol. 
✔ Created by rb_intern(“foo”) of C API. 
✔ :sym == :sym → 1001 == 1001
SYMBOL(VALUE) 
✔ SYMBOL(VALUE): Used by Ruby Level. 
✔ An raw data of :sym or ”sym”.to_sym 
✔ Uncollectable.
Why can't collect 
garbage symbols.
For example, it stores ID to the static 
area of the C extension 
C Ruby 
global_symbols 
sym_id(hash) 
“foo” 
1001 
・ 
・・ 
last_id(long) 
1001 
SYMBOL 
(VALUE) 
:foo 
Ruby's C extension 
static public ID id; 
SYM2ID(:foo) 1001
If :foo is collected, 
ID in sym_id will be deleted. 
C Ruby 
global_symbols 
sym_id(hash) 
“foo” 
1001 
・ 
・・ 
last_id(long) 
1001 
SYMBOL 
(VALUE) 
:foo 
Ruby's C extension 
static public ID id; 
1001 GC START
Then “foo”.to_sym is called. 
:foo == :foo but different ID 
C Ruby 
global_symbols 
sym_id(hash) 
“foo” 
1002 
・ 
・・ 
last_id(long) 
1001 
SYMBOL 
(VALUE) 
:foo 
Ruby's C extension 
static public ID id; 
1001 
1002 
Different 
SYM2ID(:foo) != id
Why can't collect 
garbage symbols 
✔ Problem: ID remaining in the C side. 
✔ We can't detect and manage all IDs in C extension. 
✔ Same symbol but different ID 
✔ It will create an inconsistent ID.
In Ruby world 
RRIIPP.. AA ssyymmbbooll iiss ddeeaadd...... 
Photo by MIKI Yoshihito, https://www.flickr.com/pphhoottooss//mmuujjiittrraa//77557711002222449900
In C world 
WWRRRRRRYYYYYYYYYY!!!!!! 
II''mm ssttiillll aalliivvee........!! 
IIDD 
Photo by Zufallsfaktor, https://www.flickr.com/photos/zzuuffaallllssffaakkttoorr//55991111333388995599
How do you create 
Symbol GC?
Idea
Separates into two types of symbols 
Immortal 
Symbol 
Mortal 
Symbol 
CC WWoorrlldd RRuubbyy WWoorrlldd
Immortal Symbol 
✔ These symbols have the ID corresponding 
✔ e.g. method name, variable name, constant name, etc... 
✔ use in C-level mainly 
✔ Uncollectable 
✔ Symbol stay alive after numbering the ID 
once 
✔ There is no transition to Mortal Symbol.
def foo; end 
C Ruby 
global_symbols 
sym_id(hash) 
“foo” 
1001 
・ 
・・ 
last_id(long) 
1001 
Frozen String 
“foo”
Store an ID to the method table 
C Ruby 
global_symbols 
sym_id(hash) 
“foo” 
1001 
・ 
・・ 
last_id(long) 
1001 
Frozen String 
“foo” 
Method table 
1001 def foo; end
ID2SYM(ID) → VALUE 
C Ruby 
global_symbols 
sym_id(hash) 
“foo” 
1001 
・ 
・・ 
last_id(long) 
1001 
“foo” 
ID: 1001 
ID2SYM(ID) 
Immortal 
Symbol 
(VALUE) 
:foo 
Frozen String 
Method table 
1001 def foo; end
Mortal Symbol 
✔ These symbols don't have ID 
✔ “sym”.to_sym → Mortal Symbol 
✔ use in Ruby-level mainly 
✔ Collectable 
✔ Unreachable symbols are collected. 
✔ There is transition to Immortal Symbol.
“bar”.to_sym 
C Ruby 
global_symbols 
sym_id(hash) 
“foo” 
1001 
・ 
・・ 
last_id(long) 
1001 
“foo” 
ID: 1001 
ID2SYM(ID) 
Immortal 
Symbol 
(VALUE) 
:foo 
Frozen String 
Mortal 
Symbol 
(VALUE) 
:bar 
Frozen String 
“bar” 
“bar” :bar
Splits uncollectable or 
collectable objects 
C Ruby 
global_symbols 
sym_id(hash) 
“foo” 
1001 
・ 
・・ 
last_id(long) 
1001 
“foo” 
ID: 1001 
Uncollectable 
ID2SYM(ID) 
Immortal 
Symbol 
(VALUE) 
:foo 
Frozen String 
Mortal 
Symbol 
(VALUE) 
:bar 
Collectable 
Frozen String 
“bar” 
“bar” :bar
:bar will be collected 
C Ruby 
global_symbols 
sym_id(hash) 
“foo” 
1001 
・ 
・・ 
last_id(long) 
1001 
“foo” 
ID: 1001 
ID2SYM(ID) 
Immortal 
Symbol 
(VALUE) 
:foo 
Frozen String 
Mortal 
Symbol 
(VALUE) 
:bar 
Frozen String 
“bar” 
“bar” :bar
If you already have 
Immortal Symbol of 
the same name
def foo; end 
C Ruby 
global_symbols 
sym_id(hash) 
“foo” 
1001 
・ 
・・ 
last_id(long) 
1001 
“foo” 
ID: 1001 
ID2SYM(ID) 
Immortal 
Symbol 
(VALUE) 
:foo 
Frozen String
“foo”.to_sym 
C Ruby 
global_symbols 
sym_id(hash) 
“foo” 
1001 
・ 
・・ 
last_id(long) 
1001 
“foo” 
ID:1001 
ID2SYM(ID) 
Immortal 
Symbol 
(VALUE) 
:foo 
Frozen String 
Mortal 
Symbol 
(VALUE) 
:foo 
Check 
Use this one
From Mortal Symbol 
to Immortal Symbol
define_method(“foo”.to_sym){} 
C Ruby 
Mortal 
Symbol 
(VALUE) 
:foo 
global_symbols 
sym_id(hash) 
“foo” 
:foo
define_method(“foo”.to_sym){} 
C Ruby 
Immortal 
Mortal 
Symbol 
(VALUE) 
:foo 
global_symbols 
sym_id(hash) 
Method table 
0x2c8d0 def foo; end 
SYM2ID(VALUE) 
0x2c8d0 
Pin down 
UUnnccoolllleeccttaabbllee 
Address = ID 
“foo” 
:foo
CAUTION
A new pitfall is 
coming!
Immortal Symbol 
✔ All symbols are garbage collected. 
✔ Immortal symbols are not garbage 
collected. 
✔ Mortal → Immortal symbol when 
numbering an ID. 
✔ This still lead to vulnerability!
A new pitfall 
✔ Immortal Symbol is increase 
unintentionally. 
✔ For instance: Get a name from a symbol 
✔ rb_id2str(SYM2ID(sym)) 
✔ Mortal → Immortal 
✔ Please use rb_sym2str() 
✔ Please attention to unconsidered SYM2ID().
Please keep to monitor 
✔ Check Symbol.all_symbols.size 
✔ Please report a bug to ruby-core or library author if 
increase number of symbols. 
✔ It's a transition period now. 
✔ It will get better gradually.
Details of 
implementation 
(for CRuby Hackers)
Static Symbol, 
Dynamic Symbol 
✔ Static Symbol = Immediate value 
✔ Immortal 
✔ Dynamic Symbol = RVALUE 
✔ Mortal or Immortal 
✔ Change to immortal symbol when needs ID. 
✔ Similar to Float and FLONUM
Details of RSymbol struct 
struct RSymbol { 
struct RBasic basic; 
VALUE fstr; 
ID type; 
}; 
Frozen String 
“foo” 
ID_LOCAL 0b00000 
ID_INSTANCE 0b00010 
ID_GLOBAL 0b00110 
ID_ATTRSET 0b01000 
・・ 
・
ID Structure 
0bxxx.....xxx 000 
High-order 61 bits = Counter Low-order 3 bits = ID type 
0bxxx.....xx 000 
1 
Low-order 1 bit = Static Symbol Flag
Fast recognize ID 
✔ Low-order 1bit = 1 → Static Symbol 
✔ Dynamic Symbol ID = RVALUE address 
✔ Low order 1 bit = 0 
✔ It's only check of the lower 1 bit.
Conclusion
Conclusion 
✔ Most symbols will be garbage collected. 
✔ But some symbols won't be garbage 
collected. 
✔ “sym”.to_sym → OK 
✔ define_method(“sym”.to_sym){} → NG
Acknowledgments 
✔ Sasada-san 
✔ Teaches me an idea of Symbol GC. 
✔ Refines code of Symbol GC. 
✔ Nakada-san, Tsujimoto-san, U.Nakamura-san, 
etc... 
✔ Fixes many bugs. 
✔ NaCl members
Thank you!

Mais conteúdo relacionado

Mais procurados

FlutterでGraphQLを扱う
FlutterでGraphQLを扱うFlutterでGraphQLを扱う
FlutterでGraphQLを扱うIgaHironobu
 
CyberChefの使い方(HamaCTF2019 WriteUp編)
CyberChefの使い方(HamaCTF2019 WriteUp編)CyberChefの使い方(HamaCTF2019 WriteUp編)
CyberChefの使い方(HamaCTF2019 WriteUp編)Shota Shinogi
 
いまさら聞けないselectあれこれ
いまさら聞けないselectあれこれいまさら聞けないselectあれこれ
いまさら聞けないselectあれこれlestrrat
 
MCC CTF講習会 pwn編
MCC CTF講習会 pwn編MCC CTF講習会 pwn編
MCC CTF講習会 pwn編hama7230
 
DDD x CQRS 更新系と参照系で異なるORMを併用して上手くいった話
DDD x CQRS   更新系と参照系で異なるORMを併用して上手くいった話DDD x CQRS   更新系と参照系で異なるORMを併用して上手くいった話
DDD x CQRS 更新系と参照系で異なるORMを併用して上手くいった話Koichiro Matsuoka
 
muCon 2016: Authentication in Microservice Systems By David Borsos
muCon 2016: Authentication in Microservice Systems By David BorsosmuCon 2016: Authentication in Microservice Systems By David Borsos
muCon 2016: Authentication in Microservice Systems By David BorsosOpenCredo
 
PHP AST 徹底解説
PHP AST 徹底解説PHP AST 徹底解説
PHP AST 徹底解説do_aki
 
ネットストーカー御用達OSINTツールBlackBirdを触ってみた.pptx
ネットストーカー御用達OSINTツールBlackBirdを触ってみた.pptxネットストーカー御用達OSINTツールBlackBirdを触ってみた.pptx
ネットストーカー御用達OSINTツールBlackBirdを触ってみた.pptxShota Shinogi
 
クラウド時代だからSpring-Retryフレームワーク
クラウド時代だからSpring-Retryフレームワーククラウド時代だからSpring-Retryフレームワーク
クラウド時代だからSpring-RetryフレームワークY Watanabe
 
SQLアンチパターン - ナイーブツリー
SQLアンチパターン - ナイーブツリーSQLアンチパターン - ナイーブツリー
SQLアンチパターン - ナイーブツリーke-m kamekoopa
 
メルカリ・ソウゾウでは どうGoを活用しているのか?
メルカリ・ソウゾウでは どうGoを活用しているのか?メルカリ・ソウゾウでは どうGoを活用しているのか?
メルカリ・ソウゾウでは どうGoを活用しているのか?Takuya Ueda
 
root権限無しでKubernetesを動かす
root権限無しでKubernetesを動かす root権限無しでKubernetesを動かす
root権限無しでKubernetesを動かす Akihiro Suda
 
Constexpr 中3女子テクニック
Constexpr 中3女子テクニックConstexpr 中3女子テクニック
Constexpr 中3女子テクニックGenya Murakami
 
ソフトウェア技術者から見たFPGAの魅力と可能性
ソフトウェア技術者から見たFPGAの魅力と可能性ソフトウェア技術者から見たFPGAの魅力と可能性
ソフトウェア技術者から見たFPGAの魅力と可能性Kenichiro MITSUDA
 
そんなトランザクションマネージャで大丈夫か?
そんなトランザクションマネージャで大丈夫か?そんなトランザクションマネージャで大丈夫か?
そんなトランザクションマネージャで大丈夫か?takezoe
 
HamaCTF WriteUp (Unpack category)
HamaCTF WriteUp (Unpack category)HamaCTF WriteUp (Unpack category)
HamaCTF WriteUp (Unpack category)Shota Shinogi
 

Mais procurados (20)

FlutterでGraphQLを扱う
FlutterでGraphQLを扱うFlutterでGraphQLを扱う
FlutterでGraphQLを扱う
 
CyberChefの使い方(HamaCTF2019 WriteUp編)
CyberChefの使い方(HamaCTF2019 WriteUp編)CyberChefの使い方(HamaCTF2019 WriteUp編)
CyberChefの使い方(HamaCTF2019 WriteUp編)
 
いまさら聞けないselectあれこれ
いまさら聞けないselectあれこれいまさら聞けないselectあれこれ
いまさら聞けないselectあれこれ
 
MCC CTF講習会 pwn編
MCC CTF講習会 pwn編MCC CTF講習会 pwn編
MCC CTF講習会 pwn編
 
DDD x CQRS 更新系と参照系で異なるORMを併用して上手くいった話
DDD x CQRS   更新系と参照系で異なるORMを併用して上手くいった話DDD x CQRS   更新系と参照系で異なるORMを併用して上手くいった話
DDD x CQRS 更新系と参照系で異なるORMを併用して上手くいった話
 
muCon 2016: Authentication in Microservice Systems By David Borsos
muCon 2016: Authentication in Microservice Systems By David BorsosmuCon 2016: Authentication in Microservice Systems By David Borsos
muCon 2016: Authentication in Microservice Systems By David Borsos
 
PHP AST 徹底解説
PHP AST 徹底解説PHP AST 徹底解説
PHP AST 徹底解説
 
ネットストーカー御用達OSINTツールBlackBirdを触ってみた.pptx
ネットストーカー御用達OSINTツールBlackBirdを触ってみた.pptxネットストーカー御用達OSINTツールBlackBirdを触ってみた.pptx
ネットストーカー御用達OSINTツールBlackBirdを触ってみた.pptx
 
TLS, HTTP/2演習
TLS, HTTP/2演習TLS, HTTP/2演習
TLS, HTTP/2演習
 
クラウド時代だからSpring-Retryフレームワーク
クラウド時代だからSpring-Retryフレームワーククラウド時代だからSpring-Retryフレームワーク
クラウド時代だからSpring-Retryフレームワーク
 
SQLアンチパターン - ナイーブツリー
SQLアンチパターン - ナイーブツリーSQLアンチパターン - ナイーブツリー
SQLアンチパターン - ナイーブツリー
 
メルカリ・ソウゾウでは どうGoを活用しているのか?
メルカリ・ソウゾウでは どうGoを活用しているのか?メルカリ・ソウゾウでは どうGoを活用しているのか?
メルカリ・ソウゾウでは どうGoを活用しているのか?
 
root権限無しでKubernetesを動かす
root権限無しでKubernetesを動かす root権限無しでKubernetesを動かす
root権限無しでKubernetesを動かす
 
Spring tools4
Spring tools4Spring tools4
Spring tools4
 
Constexpr 中3女子テクニック
Constexpr 中3女子テクニックConstexpr 中3女子テクニック
Constexpr 中3女子テクニック
 
ソフトウェア技術者から見たFPGAの魅力と可能性
ソフトウェア技術者から見たFPGAの魅力と可能性ソフトウェア技術者から見たFPGAの魅力と可能性
ソフトウェア技術者から見たFPGAの魅力と可能性
 
競プロでGo!
競プロでGo!競プロでGo!
競プロでGo!
 
そんなトランザクションマネージャで大丈夫か?
そんなトランザクションマネージャで大丈夫か?そんなトランザクションマネージャで大丈夫か?
そんなトランザクションマネージャで大丈夫か?
 
Hive on Tezのベストプラクティス
Hive on TezのベストプラクティスHive on Tezのベストプラクティス
Hive on Tezのベストプラクティス
 
HamaCTF WriteUp (Unpack category)
HamaCTF WriteUp (Unpack category)HamaCTF WriteUp (Unpack category)
HamaCTF WriteUp (Unpack category)
 

Destaque

Javaのプログラムはどうやって動いているの? GC編
Javaのプログラムはどうやって動いているの? GC編Javaのプログラムはどうやって動いているの? GC編
Javaのプログラムはどうやって動いているの? GC編Yuichi Sakuraba
 
G1GCへ伸びていた「いばらの道」
G1GCへ伸びていた「いばらの道」G1GCへ伸びていた「いばらの道」
G1GCへ伸びていた「いばらの道」Narihiro Nakamura
 
Java hotspot vmに おけるGCの振る舞い
Java hotspot vmにおけるGCの振る舞いJava hotspot vmにおけるGCの振る舞い
Java hotspot vmに おけるGCの振る舞いDi Ai
 
GC本をGCしないための100の方法
GC本をGCしないための100の方法GC本をGCしないための100の方法
GC本をGCしないための100の方法Narihiro Nakamura
 
第七回 渋谷Java - Apache Shiroを使ってみた
第七回 渋谷Java - Apache Shiroを使ってみた第七回 渋谷Java - Apache Shiroを使ってみた
第七回 渋谷Java - Apache Shiroを使ってみたchonaso
 
円環の理(Garbage Collection)
円環の理(Garbage Collection)円環の理(Garbage Collection)
円環の理(Garbage Collection)Narihiro Nakamura
 
われわれは、GCをX倍遅くできる
われわれは、GCをX倍遅くできるわれわれは、GCをX倍遅くできる
われわれは、GCをX倍遅くできるNarihiro Nakamura
 
地獄のGC本スピンオフ
地獄のGC本スピンオフ地獄のGC本スピンオフ
地獄のGC本スピンオフNarihiro Nakamura
 
Parallel worlds of CRuby's GC
Parallel worlds of CRuby's GCParallel worlds of CRuby's GC
Parallel worlds of CRuby's GCNarihiro Nakamura
 
第九回渋谷Java RaspberryPi+Javaを試してみる
第九回渋谷Java RaspberryPi+Javaを試してみる第九回渋谷Java RaspberryPi+Javaを試してみる
第九回渋谷Java RaspberryPi+Javaを試してみるchonaso
 
第六回渋谷Java Java8のJVM監視を考える
第六回渋谷Java Java8のJVM監視を考える第六回渋谷Java Java8のJVM監視を考える
第六回渋谷Java Java8のJVM監視を考えるchonaso
 

Destaque (20)

Javaのプログラムはどうやって動いているの? GC編
Javaのプログラムはどうやって動いているの? GC編Javaのプログラムはどうやって動いているの? GC編
Javaのプログラムはどうやって動いているの? GC編
 
GC FAQ
GC FAQGC FAQ
GC FAQ
 
Ruby's GC 20
Ruby's GC 20Ruby's GC 20
Ruby's GC 20
 
G1GCへ伸びていた「いばらの道」
G1GCへ伸びていた「いばらの道」G1GCへ伸びていた「いばらの道」
G1GCへ伸びていた「いばらの道」
 
Fxxking gc.c
Fxxking gc.cFxxking gc.c
Fxxking gc.c
 
GC黄金時代
GC黄金時代GC黄金時代
GC黄金時代
 
CRubyGCの並列世界
CRubyGCの並列世界CRubyGCの並列世界
CRubyGCの並列世界
 
RUBYLAND
RUBYLANDRUBYLAND
RUBYLAND
 
Java hotspot vmに おけるGCの振る舞い
Java hotspot vmにおけるGCの振る舞いJava hotspot vmにおけるGCの振る舞い
Java hotspot vmに おけるGCの振る舞い
 
GC本をGCしないための100の方法
GC本をGCしないための100の方法GC本をGCしないための100の方法
GC本をGCしないための100の方法
 
第七回 渋谷Java - Apache Shiroを使ってみた
第七回 渋谷Java - Apache Shiroを使ってみた第七回 渋谷Java - Apache Shiroを使ってみた
第七回 渋谷Java - Apache Shiroを使ってみた
 
Rubyによる本気のGC
Rubyによる本気のGCRubyによる本気のGC
Rubyによる本気のGC
 
円環の理(Garbage Collection)
円環の理(Garbage Collection)円環の理(Garbage Collection)
円環の理(Garbage Collection)
 
われわれは、GCをX倍遅くできる
われわれは、GCをX倍遅くできるわれわれは、GCをX倍遅くできる
われわれは、GCをX倍遅くできる
 
地獄のGC本スピンオフ
地獄のGC本スピンオフ地獄のGC本スピンオフ
地獄のGC本スピンオフ
 
Parallel worlds of CRuby's GC
Parallel worlds of CRuby's GCParallel worlds of CRuby's GC
Parallel worlds of CRuby's GC
 
第九回渋谷Java RaspberryPi+Javaを試してみる
第九回渋谷Java RaspberryPi+Javaを試してみる第九回渋谷Java RaspberryPi+Javaを試してみる
第九回渋谷Java RaspberryPi+Javaを試してみる
 
GCが止まらない
GCが止まらないGCが止まらない
GCが止まらない
 
第六回渋谷Java Java8のJVM監視を考える
第六回渋谷Java Java8のJVM監視を考える第六回渋谷Java Java8のJVM監視を考える
第六回渋谷Java Java8のJVM監視を考える
 
Java8勉強会
Java8勉強会Java8勉強会
Java8勉強会
 

Semelhante a Symbol GC

SIL for the first time
SIL for the first timeSIL for the first time
SIL for the first timeYusuke Kita
 
SFO15-500: VIXL
SFO15-500: VIXLSFO15-500: VIXL
SFO15-500: VIXLLinaro
 
Hacking parse.y (RubyConf 2009)
Hacking parse.y (RubyConf 2009)Hacking parse.y (RubyConf 2009)
Hacking parse.y (RubyConf 2009)ujihisa
 
OSCON Presentation: Developing High Performance Websites and Modern Apps with...
OSCON Presentation: Developing High Performance Websites and Modern Apps with...OSCON Presentation: Developing High Performance Websites and Modern Apps with...
OSCON Presentation: Developing High Performance Websites and Modern Apps with...Doris Chen
 
Dip Your Toes in the Sea of Security (CoderCruise 2017)
Dip Your Toes in the Sea of Security (CoderCruise 2017)Dip Your Toes in the Sea of Security (CoderCruise 2017)
Dip Your Toes in the Sea of Security (CoderCruise 2017)James Titcumb
 
Deterministic Simulation - What modern online games can learn from the Game B...
Deterministic Simulation - What modern online games can learn from the Game B...Deterministic Simulation - What modern online games can learn from the Game B...
Deterministic Simulation - What modern online games can learn from the Game B...David Salz
 
Dip Your Toes in the Sea of Security (ConFoo YVR 2017)
Dip Your Toes in the Sea of Security (ConFoo YVR 2017)Dip Your Toes in the Sea of Security (ConFoo YVR 2017)
Dip Your Toes in the Sea of Security (ConFoo YVR 2017)James Titcumb
 
Encryption Boot Camp on the JVM
Encryption Boot Camp on the JVMEncryption Boot Camp on the JVM
Encryption Boot Camp on the JVMMatthew McCullough
 
Symfony & Javascript. Combining the best of two worlds
Symfony & Javascript. Combining the best of two worldsSymfony & Javascript. Combining the best of two worlds
Symfony & Javascript. Combining the best of two worldsIgnacio Martín
 
Building reusable libraries
Building reusable librariesBuilding reusable libraries
Building reusable librariesFelix Morgner
 
ECMAScript.Next ECMAScipt 6
ECMAScript.Next ECMAScipt 6ECMAScript.Next ECMAScipt 6
ECMAScript.Next ECMAScipt 6Kevin DeRudder
 
MMT 29: "Hab Dich!" -- Wie Angreifer ganz ohne JavaScript an Deine wertvollen...
MMT 29: "Hab Dich!" -- Wie Angreifer ganz ohne JavaScript an Deine wertvollen...MMT 29: "Hab Dich!" -- Wie Angreifer ganz ohne JavaScript an Deine wertvollen...
MMT 29: "Hab Dich!" -- Wie Angreifer ganz ohne JavaScript an Deine wertvollen...MMT - Multimediatreff
 
Rust tutorial from Boston Meetup 2015-07-22
Rust tutorial from Boston Meetup 2015-07-22Rust tutorial from Boston Meetup 2015-07-22
Rust tutorial from Boston Meetup 2015-07-22nikomatsakis
 
Serializing Ruby Objects in Redis
Serializing Ruby Objects in RedisSerializing Ruby Objects in Redis
Serializing Ruby Objects in RedisBrian Kaney
 

Semelhante a Symbol GC (20)

There and Back Again
There and Back AgainThere and Back Again
There and Back Again
 
SIL for the first time
SIL for the first timeSIL for the first time
SIL for the first time
 
SFO15-500: VIXL
SFO15-500: VIXLSFO15-500: VIXL
SFO15-500: VIXL
 
Hacking parse.y (RubyConf 2009)
Hacking parse.y (RubyConf 2009)Hacking parse.y (RubyConf 2009)
Hacking parse.y (RubyConf 2009)
 
A Blink Into The Rails Magic
A Blink Into The Rails MagicA Blink Into The Rails Magic
A Blink Into The Rails Magic
 
OSCON Presentation: Developing High Performance Websites and Modern Apps with...
OSCON Presentation: Developing High Performance Websites and Modern Apps with...OSCON Presentation: Developing High Performance Websites and Modern Apps with...
OSCON Presentation: Developing High Performance Websites and Modern Apps with...
 
Dip Your Toes in the Sea of Security (CoderCruise 2017)
Dip Your Toes in the Sea of Security (CoderCruise 2017)Dip Your Toes in the Sea of Security (CoderCruise 2017)
Dip Your Toes in the Sea of Security (CoderCruise 2017)
 
Deterministic Simulation - What modern online games can learn from the Game B...
Deterministic Simulation - What modern online games can learn from the Game B...Deterministic Simulation - What modern online games can learn from the Game B...
Deterministic Simulation - What modern online games can learn from the Game B...
 
Dip Your Toes in the Sea of Security (ConFoo YVR 2017)
Dip Your Toes in the Sea of Security (ConFoo YVR 2017)Dip Your Toes in the Sea of Security (ConFoo YVR 2017)
Dip Your Toes in the Sea of Security (ConFoo YVR 2017)
 
Encryption Boot Camp on the JVM
Encryption Boot Camp on the JVMEncryption Boot Camp on the JVM
Encryption Boot Camp on the JVM
 
Symfony & Javascript. Combining the best of two worlds
Symfony & Javascript. Combining the best of two worldsSymfony & Javascript. Combining the best of two worlds
Symfony & Javascript. Combining the best of two worlds
 
Building reusable libraries
Building reusable librariesBuilding reusable libraries
Building reusable libraries
 
ECMAScript.Next ECMAScipt 6
ECMAScript.Next ECMAScipt 6ECMAScript.Next ECMAScipt 6
ECMAScript.Next ECMAScipt 6
 
Es.next
Es.nextEs.next
Es.next
 
MMT 29: "Hab Dich!" -- Wie Angreifer ganz ohne JavaScript an Deine wertvollen...
MMT 29: "Hab Dich!" -- Wie Angreifer ganz ohne JavaScript an Deine wertvollen...MMT 29: "Hab Dich!" -- Wie Angreifer ganz ohne JavaScript an Deine wertvollen...
MMT 29: "Hab Dich!" -- Wie Angreifer ganz ohne JavaScript an Deine wertvollen...
 
The_Borrow_Checker.pdf
The_Borrow_Checker.pdfThe_Borrow_Checker.pdf
The_Borrow_Checker.pdf
 
Rust tutorial from Boston Meetup 2015-07-22
Rust tutorial from Boston Meetup 2015-07-22Rust tutorial from Boston Meetup 2015-07-22
Rust tutorial from Boston Meetup 2015-07-22
 
Serializing Ruby Objects in Redis
Serializing Ruby Objects in RedisSerializing Ruby Objects in Redis
Serializing Ruby Objects in Redis
 
Java Performance MythBusters
Java Performance MythBustersJava Performance MythBusters
Java Performance MythBusters
 
Sorbet at Grailed
Sorbet at GrailedSorbet at Grailed
Sorbet at Grailed
 

Mais de Narihiro Nakamura

桐島、Rubyやめるってよ
桐島、Rubyやめるってよ桐島、Rubyやめるってよ
桐島、RubyやめるってよNarihiro Nakamura
 
Parallel worlds of CRuby's GC
Parallel worlds of CRuby's GCParallel worlds of CRuby's GC
Parallel worlds of CRuby's GCNarihiro Nakamura
 
シャイなRubyistがRubyKaigiでできること
シャイなRubyistがRubyKaigiでできることシャイなRubyistがRubyKaigiでできること
シャイなRubyistがRubyKaigiでできることNarihiro Nakamura
 
GC生誕50周年を祝って
GC生誕50周年を祝ってGC生誕50周年を祝って
GC生誕50周年を祝ってNarihiro Nakamura
 
シャイなRubyistにできること
シャイなRubyistにできることシャイなRubyistにできること
シャイなRubyistにできることNarihiro Nakamura
 
Railsハイパー実践講座-第35回NaCl勉強会
Railsハイパー実践講座-第35回NaCl勉強会Railsハイパー実践講座-第35回NaCl勉強会
Railsハイパー実践講座-第35回NaCl勉強会Narihiro Nakamura
 
Androidの中身-第26回NaCl社内勉強会
Androidの中身-第26回NaCl社内勉強会Androidの中身-第26回NaCl社内勉強会
Androidの中身-第26回NaCl社内勉強会Narihiro Nakamura
 
RubyのGC改善による私のエコライフ
RubyのGC改善による私のエコライフRubyのGC改善による私のエコライフ
RubyのGC改善による私のエコライフNarihiro Nakamura
 
本当は怖いObjectSpace.each_object
本当は怖いObjectSpace.each_object本当は怖いObjectSpace.each_object
本当は怖いObjectSpace.each_objectNarihiro Nakamura
 
Talk In Point Of Gc Once In While
Talk In Point Of Gc Once In WhileTalk In Point Of Gc Once In While
Talk In Point Of Gc Once In WhileNarihiro Nakamura
 
Rubyはゲームの夢を見るか
Rubyはゲームの夢を見るかRubyはゲームの夢を見るか
Rubyはゲームの夢を見るかNarihiro Nakamura
 

Mais de Narihiro Nakamura (15)

桐島、Rubyやめるってよ
桐島、Rubyやめるってよ桐島、Rubyやめるってよ
桐島、Rubyやめるってよ
 
Parallel worlds of CRuby's GC
Parallel worlds of CRuby's GCParallel worlds of CRuby's GC
Parallel worlds of CRuby's GC
 
シャイなRubyistがRubyKaigiでできること
シャイなRubyistがRubyKaigiでできることシャイなRubyistがRubyKaigiでできること
シャイなRubyistがRubyKaigiでできること
 
GC生誕50周年を祝って
GC生誕50周年を祝ってGC生誕50周年を祝って
GC生誕50周年を祝って
 
GC本のツクリカタ
GC本のツクリカタGC本のツクリカタ
GC本のツクリカタ
 
シャイなRubyistにできること
シャイなRubyistにできることシャイなRubyistにできること
シャイなRubyistにできること
 
Railsハイパー実践講座-第35回NaCl勉強会
Railsハイパー実践講座-第35回NaCl勉強会Railsハイパー実践講座-第35回NaCl勉強会
Railsハイパー実践講座-第35回NaCl勉強会
 
Androidの中身-第26回NaCl社内勉強会
Androidの中身-第26回NaCl社内勉強会Androidの中身-第26回NaCl社内勉強会
Androidの中身-第26回NaCl社内勉強会
 
RubyのGC改善による私のエコライフ
RubyのGC改善による私のエコライフRubyのGC改善による私のエコライフ
RubyのGC改善による私のエコライフ
 
絶対復習について
絶対復習について絶対復習について
絶対復習について
 
AlgorithmDesign01
AlgorithmDesign01AlgorithmDesign01
AlgorithmDesign01
 
make of MiniGC
make of MiniGCmake of MiniGC
make of MiniGC
 
本当は怖いObjectSpace.each_object
本当は怖いObjectSpace.each_object本当は怖いObjectSpace.each_object
本当は怖いObjectSpace.each_object
 
Talk In Point Of Gc Once In While
Talk In Point Of Gc Once In WhileTalk In Point Of Gc Once In While
Talk In Point Of Gc Once In While
 
Rubyはゲームの夢を見るか
Rubyはゲームの夢を見るかRubyはゲームの夢を見るか
Rubyはゲームの夢を見るか
 

Último

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 

Último (20)

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 

Symbol GC

  • 1. Symbol GC #rubykaigi 2014 Narihiro Nakamura - @nari3
  • 3. Self introduction ✔ Nari, @nari3, authorNari ✔ A CRuby committer. ✔ I work at NaCl. ✔ “Nakamura” ✔ is the most powerful clan in Ruby World.
  • 6. Today's topic obj = Object.new 100_000.times do |i| obj.respond_to?("sym#{i}".to_sym) end GC.start puts"symbol : #{Symbol.all_symbols.size}" $ ruby-2.1.2 a.rb symbol : 102416 $ ruby-trunk symbol : 2833
  • 8. Symbol ✔ A symbol is a primitive data type whose instances have a unique human-readable form. ✔ Symbols can be used as identifiers.
  • 10. A pitfall of Symbol
  • 11. A pitfall of Symbol ✔ All symbols are not garbage collected. ✔ Many beginners don't know this fact. ✔ Make a mistake even good rubyists. ✔ Prone to vulnerability ✔ User input → symbol ✔ Compress the memory
  • 12. Simple cases ✖ if user.respond_to(params[:method].to_sym) Is this method callable? NG: params[:method] is user input ✖ params[params[:attr].to_sym] Get a value of a hash via a symbol key. NG: params[:attr] is user input.
  • 13. Rails DoS Vulnerability CVE-2012-3424 HTTP Request: GET …. WWW-Authenticate: Digest digest = { to_sym :realm => “..”, to_sym :nonce => “..”, realm="..", nonce="...", algorithm=MD5, qop="auth" Parse to a hash } , foo=”xxx”, .., :foo => “..”, to_sym . . .,
  • 14. We want Symbol GC ✔ There is this request from long time ago. ✔ Sasada-san has an idea. ✔ I will implement this idea.
  • 15. Are symbols in other programming languages garbage collectable?
  • 16. Programming languages which supported for Symbol ✔ Too Many Parentheses Languages ✔ Erlang ✔ Smalltalk ✔ Scala
  • 17. Symbol GC support Language Symbol GC Erlang ✖ Gauche ✖ Clojure ○ EmacsLisp ✖ VisualWorks(Smalltalk) ○ Scala ○
  • 18. Implementation dependency? ✔ Not unified. ✔ Symbol GC is undocumented in programing language specifications. ✔ Implementation = Specification?
  • 19. EmacsLisp ✔ Function: unintern ✔ (unintern 'foo) ✔ Declare an unnecessary symbol. ✔ It's like manual memory management.
  • 20. Scala Java main.scala 01: val a = 'sym Symbol Table String “sym”
  • 21. Scala Java main.scala 01: val a = 'sym 02: a = null String “sym” Symbol Table Weak Reference GCG SCT ASRTATRT
  • 23. “sym”.to_sym C Ruby global_symbols ““ssyymm”” “sym” String sym_id(hash) ・ ・・ last_id(long) 1000 “sym” freeze freeze String “sym” 1001 Frozen String
  • 24. “sym”.to_sym C Ruby global_symbols ““ssyymm”” “sym” String sym_id(hash) 1001 ・ ・・ last_id(long) “sym” freeze freeze String “sym” 1001 ID: 1001 ID2SYM(ID) SYMBOL (VALUE) :sym Frozen String
  • 25. ID ✔ ID: Used by C Level. ✔ Store ID to a method table or a variable table. ✔ An unique number that corresponds to a symbol. ✔ Created by rb_intern(“foo”) of C API. ✔ :sym == :sym → 1001 == 1001
  • 26. SYMBOL(VALUE) ✔ SYMBOL(VALUE): Used by Ruby Level. ✔ An raw data of :sym or ”sym”.to_sym ✔ Uncollectable.
  • 27. Why can't collect garbage symbols.
  • 28. For example, it stores ID to the static area of the C extension C Ruby global_symbols sym_id(hash) “foo” 1001 ・ ・・ last_id(long) 1001 SYMBOL (VALUE) :foo Ruby's C extension static public ID id; SYM2ID(:foo) 1001
  • 29. If :foo is collected, ID in sym_id will be deleted. C Ruby global_symbols sym_id(hash) “foo” 1001 ・ ・・ last_id(long) 1001 SYMBOL (VALUE) :foo Ruby's C extension static public ID id; 1001 GC START
  • 30. Then “foo”.to_sym is called. :foo == :foo but different ID C Ruby global_symbols sym_id(hash) “foo” 1002 ・ ・・ last_id(long) 1001 SYMBOL (VALUE) :foo Ruby's C extension static public ID id; 1001 1002 Different SYM2ID(:foo) != id
  • 31. Why can't collect garbage symbols ✔ Problem: ID remaining in the C side. ✔ We can't detect and manage all IDs in C extension. ✔ Same symbol but different ID ✔ It will create an inconsistent ID.
  • 32. In Ruby world RRIIPP.. AA ssyymmbbooll iiss ddeeaadd...... Photo by MIKI Yoshihito, https://www.flickr.com/pphhoottooss//mmuujjiittrraa//77557711002222449900
  • 33. In C world WWRRRRRRYYYYYYYYYY!!!!!! II''mm ssttiillll aalliivvee........!! IIDD Photo by Zufallsfaktor, https://www.flickr.com/photos/zzuuffaallllssffaakkttoorr//55991111333388995599
  • 34. How do you create Symbol GC?
  • 35. Idea
  • 36. Separates into two types of symbols Immortal Symbol Mortal Symbol CC WWoorrlldd RRuubbyy WWoorrlldd
  • 37. Immortal Symbol ✔ These symbols have the ID corresponding ✔ e.g. method name, variable name, constant name, etc... ✔ use in C-level mainly ✔ Uncollectable ✔ Symbol stay alive after numbering the ID once ✔ There is no transition to Mortal Symbol.
  • 38. def foo; end C Ruby global_symbols sym_id(hash) “foo” 1001 ・ ・・ last_id(long) 1001 Frozen String “foo”
  • 39. Store an ID to the method table C Ruby global_symbols sym_id(hash) “foo” 1001 ・ ・・ last_id(long) 1001 Frozen String “foo” Method table 1001 def foo; end
  • 40. ID2SYM(ID) → VALUE C Ruby global_symbols sym_id(hash) “foo” 1001 ・ ・・ last_id(long) 1001 “foo” ID: 1001 ID2SYM(ID) Immortal Symbol (VALUE) :foo Frozen String Method table 1001 def foo; end
  • 41. Mortal Symbol ✔ These symbols don't have ID ✔ “sym”.to_sym → Mortal Symbol ✔ use in Ruby-level mainly ✔ Collectable ✔ Unreachable symbols are collected. ✔ There is transition to Immortal Symbol.
  • 42. “bar”.to_sym C Ruby global_symbols sym_id(hash) “foo” 1001 ・ ・・ last_id(long) 1001 “foo” ID: 1001 ID2SYM(ID) Immortal Symbol (VALUE) :foo Frozen String Mortal Symbol (VALUE) :bar Frozen String “bar” “bar” :bar
  • 43. Splits uncollectable or collectable objects C Ruby global_symbols sym_id(hash) “foo” 1001 ・ ・・ last_id(long) 1001 “foo” ID: 1001 Uncollectable ID2SYM(ID) Immortal Symbol (VALUE) :foo Frozen String Mortal Symbol (VALUE) :bar Collectable Frozen String “bar” “bar” :bar
  • 44. :bar will be collected C Ruby global_symbols sym_id(hash) “foo” 1001 ・ ・・ last_id(long) 1001 “foo” ID: 1001 ID2SYM(ID) Immortal Symbol (VALUE) :foo Frozen String Mortal Symbol (VALUE) :bar Frozen String “bar” “bar” :bar
  • 45. If you already have Immortal Symbol of the same name
  • 46. def foo; end C Ruby global_symbols sym_id(hash) “foo” 1001 ・ ・・ last_id(long) 1001 “foo” ID: 1001 ID2SYM(ID) Immortal Symbol (VALUE) :foo Frozen String
  • 47. “foo”.to_sym C Ruby global_symbols sym_id(hash) “foo” 1001 ・ ・・ last_id(long) 1001 “foo” ID:1001 ID2SYM(ID) Immortal Symbol (VALUE) :foo Frozen String Mortal Symbol (VALUE) :foo Check Use this one
  • 48. From Mortal Symbol to Immortal Symbol
  • 49. define_method(“foo”.to_sym){} C Ruby Mortal Symbol (VALUE) :foo global_symbols sym_id(hash) “foo” :foo
  • 50. define_method(“foo”.to_sym){} C Ruby Immortal Mortal Symbol (VALUE) :foo global_symbols sym_id(hash) Method table 0x2c8d0 def foo; end SYM2ID(VALUE) 0x2c8d0 Pin down UUnnccoolllleeccttaabbllee Address = ID “foo” :foo
  • 52. A new pitfall is coming!
  • 53. Immortal Symbol ✔ All symbols are garbage collected. ✔ Immortal symbols are not garbage collected. ✔ Mortal → Immortal symbol when numbering an ID. ✔ This still lead to vulnerability!
  • 54. A new pitfall ✔ Immortal Symbol is increase unintentionally. ✔ For instance: Get a name from a symbol ✔ rb_id2str(SYM2ID(sym)) ✔ Mortal → Immortal ✔ Please use rb_sym2str() ✔ Please attention to unconsidered SYM2ID().
  • 55. Please keep to monitor ✔ Check Symbol.all_symbols.size ✔ Please report a bug to ruby-core or library author if increase number of symbols. ✔ It's a transition period now. ✔ It will get better gradually.
  • 56. Details of implementation (for CRuby Hackers)
  • 57. Static Symbol, Dynamic Symbol ✔ Static Symbol = Immediate value ✔ Immortal ✔ Dynamic Symbol = RVALUE ✔ Mortal or Immortal ✔ Change to immortal symbol when needs ID. ✔ Similar to Float and FLONUM
  • 58. Details of RSymbol struct struct RSymbol { struct RBasic basic; VALUE fstr; ID type; }; Frozen String “foo” ID_LOCAL 0b00000 ID_INSTANCE 0b00010 ID_GLOBAL 0b00110 ID_ATTRSET 0b01000 ・・ ・
  • 59. ID Structure 0bxxx.....xxx 000 High-order 61 bits = Counter Low-order 3 bits = ID type 0bxxx.....xx 000 1 Low-order 1 bit = Static Symbol Flag
  • 60. Fast recognize ID ✔ Low-order 1bit = 1 → Static Symbol ✔ Dynamic Symbol ID = RVALUE address ✔ Low order 1 bit = 0 ✔ It's only check of the lower 1 bit.
  • 62. Conclusion ✔ Most symbols will be garbage collected. ✔ But some symbols won't be garbage collected. ✔ “sym”.to_sym → OK ✔ define_method(“sym”.to_sym){} → NG
  • 63. Acknowledgments ✔ Sasada-san ✔ Teaches me an idea of Symbol GC. ✔ Refines code of Symbol GC. ✔ Nakada-san, Tsujimoto-san, U.Nakamura-san, etc... ✔ Fixes many bugs. ✔ NaCl members