Symbol GC

Narihiro Nakamura
Narihiro Nakamura株式会社ネットワーク応用通信研究所(NaCl)
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!
1 of 64

Recommended

全文検索サーバ Fess 〜 全文検索システム構築時の悩みどころ by
全文検索サーバ Fess 〜 全文検索システム構築時の悩みどころ全文検索サーバ Fess 〜 全文検索システム構築時の悩みどころ
全文検索サーバ Fess 〜 全文検索システム構築時の悩みどころShinsuke Sugaya
19K views31 slides
MQ入門 by
MQ入門MQ入門
MQ入門HIRA
7.3K views28 slides
15分でわかるGit入門 by
15分でわかるGit入門15分でわかるGit入門
15分でわかるGit入門to_ueda
55.8K views34 slides
Tokyo Video Tech #2 動画配信の課題とCMAF活用のメリットデメリット by
Tokyo Video Tech #2 動画配信の課題とCMAF活用のメリットデメリットTokyo Video Tech #2 動画配信の課題とCMAF活用のメリットデメリット
Tokyo Video Tech #2 動画配信の課題とCMAF活用のメリットデメリットMasashi Ito
4.1K views44 slides
Yahoo! JAPANのデータパイプラインで起きた障害とチューニング - Apache Kafka Meetup Japan #5 - by
Yahoo! JAPANのデータパイプラインで起きた障害とチューニング - Apache Kafka Meetup Japan #5 -Yahoo! JAPANのデータパイプラインで起きた障害とチューニング - Apache Kafka Meetup Japan #5 -
Yahoo! JAPANのデータパイプラインで起きた障害とチューニング - Apache Kafka Meetup Japan #5 -Yahoo!デベロッパーネットワーク
2.3K views26 slides
Scalaで型クラス入門 by
Scalaで型クラス入門Scalaで型クラス入門
Scalaで型クラス入門Makoto Fukuhara
6.4K views30 slides

More Related Content

What's hot

Kinesis→Redshift連携を、KCLからFirehoseに切り替えたお話 by
Kinesis→Redshift連携を、KCLからFirehoseに切り替えたお話Kinesis→Redshift連携を、KCLからFirehoseに切り替えたお話
Kinesis→Redshift連携を、KCLからFirehoseに切り替えたお話Hajime Sano
6.7K views24 slides
プログラム組んだら負け!実はHTML/CSSだけでできること2015夏 by
プログラム組んだら負け!実はHTML/CSSだけでできること2015夏プログラム組んだら負け!実はHTML/CSSだけでできること2015夏
プログラム組んだら負け!実はHTML/CSSだけでできること2015夏Yusuke Hirao
82.1K views88 slides
サイボウズのフロントエンド開発 現在とこれからの挑戦 by
サイボウズのフロントエンド開発 現在とこれからの挑戦サイボウズのフロントエンド開発 現在とこれからの挑戦
サイボウズのフロントエンド開発 現在とこれからの挑戦Teppei Sato
20.6K views75 slides
バッチは地味だが役に立つ by
バッチは地味だが役に立つバッチは地味だが役に立つ
バッチは地味だが役に立つapkiban
2.7K views72 slides
TF에서 팀 빌딩까지 9개월의 기록 : 성장하는 조직을 만드는 여정 by
TF에서 팀 빌딩까지 9개월의 기록 : 성장하는 조직을 만드는 여정TF에서 팀 빌딩까지 9개월의 기록 : 성장하는 조직을 만드는 여정
TF에서 팀 빌딩까지 9개월의 기록 : 성장하는 조직을 만드는 여정Seongyun Byeon
14.7K views65 slides
FlinkML: Large Scale Machine Learning with Apache Flink by
FlinkML: Large Scale Machine Learning with Apache FlinkFlinkML: Large Scale Machine Learning with Apache Flink
FlinkML: Large Scale Machine Learning with Apache FlinkTheodoros Vasiloudis
2.4K views84 slides

What's hot(20)

Kinesis→Redshift連携を、KCLからFirehoseに切り替えたお話 by Hajime Sano
Kinesis→Redshift連携を、KCLからFirehoseに切り替えたお話Kinesis→Redshift連携を、KCLからFirehoseに切り替えたお話
Kinesis→Redshift連携を、KCLからFirehoseに切り替えたお話
Hajime Sano6.7K views
プログラム組んだら負け!実はHTML/CSSだけでできること2015夏 by Yusuke Hirao
プログラム組んだら負け!実はHTML/CSSだけでできること2015夏プログラム組んだら負け!実はHTML/CSSだけでできること2015夏
プログラム組んだら負け!実はHTML/CSSだけでできること2015夏
Yusuke Hirao82.1K views
サイボウズのフロントエンド開発 現在とこれからの挑戦 by Teppei Sato
サイボウズのフロントエンド開発 現在とこれからの挑戦サイボウズのフロントエンド開発 現在とこれからの挑戦
サイボウズのフロントエンド開発 現在とこれからの挑戦
Teppei Sato20.6K views
バッチは地味だが役に立つ by apkiban
バッチは地味だが役に立つバッチは地味だが役に立つ
バッチは地味だが役に立つ
apkiban2.7K views
TF에서 팀 빌딩까지 9개월의 기록 : 성장하는 조직을 만드는 여정 by Seongyun Byeon
TF에서 팀 빌딩까지 9개월의 기록 : 성장하는 조직을 만드는 여정TF에서 팀 빌딩까지 9개월의 기록 : 성장하는 조직을 만드는 여정
TF에서 팀 빌딩까지 9개월의 기록 : 성장하는 조직을 만드는 여정
Seongyun Byeon14.7K views
FlinkML: Large Scale Machine Learning with Apache Flink by Theodoros Vasiloudis
FlinkML: Large Scale Machine Learning with Apache FlinkFlinkML: Large Scale Machine Learning with Apache Flink
FlinkML: Large Scale Machine Learning with Apache Flink
오픈 소스를 활용한 캐쥬얼 게임 서버 프레임워크 개발 by 주항 박
오픈 소스를 활용한 캐쥬얼 게임 서버 프레임워크 개발오픈 소스를 활용한 캐쥬얼 게임 서버 프레임워크 개발
오픈 소스를 활용한 캐쥬얼 게임 서버 프레임워크 개발
주항 박12.1K views
Travailler avec git et eclipse by Francois ANDRE
Travailler avec git et eclipseTravailler avec git et eclipse
Travailler avec git et eclipse
Francois ANDRE2.3K views
ホットペッパービューティーにおけるモバイルアプリ向けAPIのBFF/Backend分割 by Recruit Lifestyle Co., Ltd.
ホットペッパービューティーにおけるモバイルアプリ向けAPIのBFF/Backend分割ホットペッパービューティーにおけるモバイルアプリ向けAPIのBFF/Backend分割
ホットペッパービューティーにおけるモバイルアプリ向けAPIのBFF/Backend分割
Firefoxの倒し方 by 西村 宗晃 (にしむねあ) by CODE BLUE
Firefoxの倒し方 by 西村 宗晃 (にしむねあ)Firefoxの倒し方 by 西村 宗晃 (にしむねあ)
Firefoxの倒し方 by 西村 宗晃 (にしむねあ)
CODE BLUE3.3K views
Building a turn-based game prototype using ECS - Unite Copenhagen 2019 by Unity Technologies
Building a turn-based game prototype using ECS - Unite Copenhagen 2019Building a turn-based game prototype using ECS - Unite Copenhagen 2019
Building a turn-based game prototype using ECS - Unite Copenhagen 2019
Unity Technologies3.3K views
DeClang 誕生!Clang ベースのハッキング対策コンパイラ【DeNA TechCon 2020 ライブ配信】 by DeNA
DeClang 誕生!Clang ベースのハッキング対策コンパイラ【DeNA TechCon 2020 ライブ配信】DeClang 誕生!Clang ベースのハッキング対策コンパイラ【DeNA TechCon 2020 ライブ配信】
DeClang 誕生!Clang ベースのハッキング対策コンパイラ【DeNA TechCon 2020 ライブ配信】
DeNA3K views
Real-Time Streaming Data Solution on AWS with Beeswax by Amazon Web Services
Real-Time Streaming Data Solution on AWS with BeeswaxReal-Time Streaming Data Solution on AWS with Beeswax
Real-Time Streaming Data Solution on AWS with Beeswax
Amazon Web Services1.7K views
Grapics debugging with RenderDoc by Matias Lavik
Grapics debugging with RenderDocGrapics debugging with RenderDoc
Grapics debugging with RenderDoc
Matias Lavik1.8K views
NDC12_Lockless게임서버설계와구현 by noerror
NDC12_Lockless게임서버설계와구현NDC12_Lockless게임서버설계와구현
NDC12_Lockless게임서버설계와구현
noerror7.7K views
並行処理初心者のためのAkka入門 by Yoshimura Soichiro
並行処理初心者のためのAkka入門並行処理初心者のためのAkka入門
並行処理初心者のためのAkka入門
Yoshimura Soichiro49.9K views
Python과 Git으로 만드는 모바일 게임 패치 시스템 by Youngtaek Oh
Python과 Git으로 만드는 모바일 게임 패치 시스템Python과 Git으로 만드는 모바일 게임 패치 시스템
Python과 Git으로 만드는 모바일 게임 패치 시스템
Youngtaek Oh15.8K views

Viewers also liked

Javaのプログラムはどうやって動いているの? GC編 by
Javaのプログラムはどうやって動いているの? GC編Javaのプログラムはどうやって動いているの? GC編
Javaのプログラムはどうやって動いているの? GC編Yuichi Sakuraba
18.8K views43 slides
GC FAQ by
GC FAQGC FAQ
GC FAQNarihiro Nakamura
1.8K views52 slides
Ruby's GC 20 by
Ruby's GC 20Ruby's GC 20
Ruby's GC 20Narihiro Nakamura
5.8K views77 slides
G1GCへ伸びていた「いばらの道」 by
G1GCへ伸びていた「いばらの道」G1GCへ伸びていた「いばらの道」
G1GCへ伸びていた「いばらの道」Narihiro Nakamura
4.1K views112 slides
Fxxking gc.c by
Fxxking gc.cFxxking gc.c
Fxxking gc.cNarihiro Nakamura
1.9K views41 slides
GC黄金時代 by
GC黄金時代GC黄金時代
GC黄金時代Narihiro Nakamura
2.7K views191 slides

Viewers also liked(20)

Javaのプログラムはどうやって動いているの? GC編 by Yuichi Sakuraba
Javaのプログラムはどうやって動いているの? GC編Javaのプログラムはどうやって動いているの? GC編
Javaのプログラムはどうやって動いているの? GC編
Yuichi Sakuraba18.8K views
G1GCへ伸びていた「いばらの道」 by Narihiro Nakamura
G1GCへ伸びていた「いばらの道」G1GCへ伸びていた「いばらの道」
G1GCへ伸びていた「いばらの道」
Narihiro Nakamura4.1K views
Java hotspot vmに おけるGCの振る舞い by Di Ai
Java hotspot vmにおけるGCの振る舞いJava hotspot vmにおけるGCの振る舞い
Java hotspot vmに おけるGCの振る舞い
Di Ai1.8K views
GC本をGCしないための100の方法 by Narihiro Nakamura
GC本をGCしないための100の方法GC本をGCしないための100の方法
GC本をGCしないための100の方法
Narihiro Nakamura5.2K views
第七回 渋谷Java - Apache Shiroを使ってみた by chonaso
第七回 渋谷Java - Apache Shiroを使ってみた第七回 渋谷Java - Apache Shiroを使ってみた
第七回 渋谷Java - Apache Shiroを使ってみた
chonaso4K views
われわれは、GCをX倍遅くできる by Narihiro Nakamura
われわれは、GCをX倍遅くできるわれわれは、GCをX倍遅くできる
われわれは、GCをX倍遅くできる
Narihiro Nakamura2.3K views
第九回渋谷Java RaspberryPi+Javaを試してみる by chonaso
第九回渋谷Java RaspberryPi+Javaを試してみる第九回渋谷Java RaspberryPi+Javaを試してみる
第九回渋谷Java RaspberryPi+Javaを試してみる
chonaso2.9K views
第六回渋谷Java Java8のJVM監視を考える by chonaso
第六回渋谷Java Java8のJVM監視を考える第六回渋谷Java Java8のJVM監視を考える
第六回渋谷Java Java8のJVM監視を考える
chonaso15.4K views

Similar to Symbol GC

There and Back Again by
There and Back AgainThere and Back Again
There and Back AgainJoshua Ballanco
993 views77 slides
SIL for the first time by
SIL for the first timeSIL for the first time
SIL for the first timeYusuke Kita
2.1K views67 slides
SFO15-500: VIXL by
SFO15-500: VIXLSFO15-500: VIXL
SFO15-500: VIXLLinaro
1.2K views30 slides
Hacking parse.y (RubyConf 2009) by
Hacking parse.y (RubyConf 2009)Hacking parse.y (RubyConf 2009)
Hacking parse.y (RubyConf 2009)ujihisa
2.3K views54 slides
A Blink Into The Rails Magic by
A Blink Into The Rails MagicA Blink Into The Rails Magic
A Blink Into The Rails MagicNikos Dimitrakopoulos
1.1K views77 slides
OSCON Presentation: Developing High Performance Websites and Modern Apps with... by
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
4.1K views50 slides

Similar to Symbol GC(20)

SIL for the first time by Yusuke Kita
SIL for the first timeSIL for the first time
SIL for the first time
Yusuke Kita2.1K views
SFO15-500: VIXL by Linaro
SFO15-500: VIXLSFO15-500: VIXL
SFO15-500: VIXL
Linaro1.2K views
Hacking parse.y (RubyConf 2009) by ujihisa
Hacking parse.y (RubyConf 2009)Hacking parse.y (RubyConf 2009)
Hacking parse.y (RubyConf 2009)
ujihisa2.3K views
OSCON Presentation: Developing High Performance Websites and Modern Apps with... by Doris Chen
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 Chen4.1K views
Dip Your Toes in the Sea of Security (CoderCruise 2017) by James Titcumb
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 Titcumb335 views
Deterministic Simulation - What modern online games can learn from the Game B... by David Salz
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 Salz1.4K views
Dip Your Toes in the Sea of Security (ConFoo YVR 2017) by James Titcumb
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 Titcumb244 views
Symfony & Javascript. Combining the best of two worlds by Ignacio Martín
Symfony & Javascript. Combining the best of two worldsSymfony & Javascript. Combining the best of two worlds
Symfony & Javascript. Combining the best of two worlds
Ignacio Martín19.3K views
Building reusable libraries by Felix Morgner
Building reusable librariesBuilding reusable libraries
Building reusable libraries
Felix Morgner184 views
ECMAScript.Next ECMAScipt 6 by Kevin DeRudder
ECMAScript.Next ECMAScipt 6ECMAScript.Next ECMAScipt 6
ECMAScript.Next ECMAScipt 6
Kevin DeRudder4.3K views
MMT 29: "Hab Dich!" -- Wie Angreifer ganz ohne JavaScript an Deine wertvollen... by MMT - Multimediatreff
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...
Rust tutorial from Boston Meetup 2015-07-22 by nikomatsakis
Rust tutorial from Boston Meetup 2015-07-22Rust tutorial from Boston Meetup 2015-07-22
Rust tutorial from Boston Meetup 2015-07-22
nikomatsakis1.1K views
Serializing Ruby Objects in Redis by Brian Kaney
Serializing Ruby Objects in RedisSerializing Ruby Objects in Redis
Serializing Ruby Objects in Redis
Brian Kaney9K views

More from Narihiro Nakamura

桐島、Rubyやめるってよ by
桐島、Rubyやめるってよ桐島、Rubyやめるってよ
桐島、RubyやめるってよNarihiro Nakamura
21.3K views149 slides
Parallel worlds of CRuby's GC by
Parallel worlds of CRuby's GCParallel worlds of CRuby's GC
Parallel worlds of CRuby's GCNarihiro Nakamura
35.4K views208 slides
シャイなRubyistがRubyKaigiでできること by
シャイなRubyistがRubyKaigiでできることシャイなRubyistがRubyKaigiでできること
シャイなRubyistがRubyKaigiでできることNarihiro Nakamura
1.2K views59 slides
GC生誕50周年を祝って by
GC生誕50周年を祝ってGC生誕50周年を祝って
GC生誕50周年を祝ってNarihiro Nakamura
2K views100 slides
GC本のツクリカタ by
GC本のツクリカタGC本のツクリカタ
GC本のツクリカタNarihiro Nakamura
2.2K views203 slides
シャイなRubyistにできること by
シャイなRubyistにできることシャイなRubyistにできること
シャイなRubyistにできることNarihiro Nakamura
2K views103 slides

More from Narihiro Nakamura(15)

シャイなRubyistがRubyKaigiでできること by Narihiro Nakamura
シャイなRubyistがRubyKaigiでできることシャイなRubyistがRubyKaigiでできること
シャイなRubyistがRubyKaigiでできること
Narihiro Nakamura1.2K views
Railsハイパー実践講座-第35回NaCl勉強会 by Narihiro Nakamura
Railsハイパー実践講座-第35回NaCl勉強会Railsハイパー実践講座-第35回NaCl勉強会
Railsハイパー実践講座-第35回NaCl勉強会
Narihiro Nakamura1.7K views
Androidの中身-第26回NaCl社内勉強会 by Narihiro Nakamura
Androidの中身-第26回NaCl社内勉強会Androidの中身-第26回NaCl社内勉強会
Androidの中身-第26回NaCl社内勉強会
Narihiro Nakamura1.2K views
RubyのGC改善による私のエコライフ by Narihiro Nakamura
RubyのGC改善による私のエコライフRubyのGC改善による私のエコライフ
RubyのGC改善による私のエコライフ
Narihiro Nakamura3.3K views
本当は怖いObjectSpace.each_object by Narihiro Nakamura
本当は怖いObjectSpace.each_object本当は怖いObjectSpace.each_object
本当は怖いObjectSpace.each_object
Narihiro Nakamura3.9K views

Recently uploaded

SAP Automation Using Bar Code and FIORI.pdf by
SAP Automation Using Bar Code and FIORI.pdfSAP Automation Using Bar Code and FIORI.pdf
SAP Automation Using Bar Code and FIORI.pdfVirendra Rai, PMP
23 views38 slides
Vertical User Stories by
Vertical User StoriesVertical User Stories
Vertical User StoriesMoisés Armani Ramírez
14 views16 slides
Serverless computing with Google Cloud (2023-24) by
Serverless computing with Google Cloud (2023-24)Serverless computing with Google Cloud (2023-24)
Serverless computing with Google Cloud (2023-24)wesley chun
11 views33 slides
STPI OctaNE CoE Brochure.pdf by
STPI OctaNE CoE Brochure.pdfSTPI OctaNE CoE Brochure.pdf
STPI OctaNE CoE Brochure.pdfmadhurjyapb
14 views1 slide
Zero to Automated in Under a Year by
Zero to Automated in Under a YearZero to Automated in Under a Year
Zero to Automated in Under a YearNetwork Automation Forum
15 views23 slides
Ransomware is Knocking your Door_Final.pdf by
Ransomware is Knocking your Door_Final.pdfRansomware is Knocking your Door_Final.pdf
Ransomware is Knocking your Door_Final.pdfSecurity Bootcamp
59 views46 slides

Recently uploaded(20)

SAP Automation Using Bar Code and FIORI.pdf by Virendra Rai, PMP
SAP Automation Using Bar Code and FIORI.pdfSAP Automation Using Bar Code and FIORI.pdf
SAP Automation Using Bar Code and FIORI.pdf
Serverless computing with Google Cloud (2023-24) by wesley chun
Serverless computing with Google Cloud (2023-24)Serverless computing with Google Cloud (2023-24)
Serverless computing with Google Cloud (2023-24)
wesley chun11 views
STPI OctaNE CoE Brochure.pdf by madhurjyapb
STPI OctaNE CoE Brochure.pdfSTPI OctaNE CoE Brochure.pdf
STPI OctaNE CoE Brochure.pdf
madhurjyapb14 views
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N... by James Anderson
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
James Anderson92 views
Case Study Copenhagen Energy and Business Central.pdf by Aitana
Case Study Copenhagen Energy and Business Central.pdfCase Study Copenhagen Energy and Business Central.pdf
Case Study Copenhagen Energy and Business Central.pdf
Aitana16 views
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ... by Jasper Oosterveld
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas... by Bernd Ruecker
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
Bernd Ruecker40 views
"Running students' code in isolation. The hard way", Yurii Holiuk by Fwdays
"Running students' code in isolation. The hard way", Yurii Holiuk "Running students' code in isolation. The hard way", Yurii Holiuk
"Running students' code in isolation. The hard way", Yurii Holiuk
Fwdays17 views
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors by sugiuralab
TouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective SensorsTouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective Sensors
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors
sugiuralab21 views
Piloting & Scaling Successfully With Microsoft Viva by Richard Harbridge
Piloting & Scaling Successfully With Microsoft VivaPiloting & Scaling Successfully With Microsoft Viva
Piloting & Scaling Successfully With Microsoft Viva
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive by Network Automation Forum
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLiveAutomating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive

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