Rubyã¢ããã³ãã«ã¬ã³ãã¼ï¼ã«ã¬ã³ãã¼2ï¼ã®2æ¥ç®ã§ãã
ä»è¦ããã«ã¬ã³ãã¼2ãåå以ä¸åã¾ã£ã¦ã¦å¬ãã... Ruby is alive now!!!
ã¨ãããã¨ã§ãRubyKaigi takeout 2021ã®æ¯ãè¿ããå ¼ãã¦ä½ã£ããã®ã®è©±ã§ãã
Rucy: A Ruby Compiler
Rucyã¯Rubyã®ã¹ã¯ãªãããã³ã³ãã¤ã«ãã¦ç´æ¥ç¹å®ã®ãã©ã¼ãããã®ãã¤ããªãåãåºããã¼ã«ã§ããä»ã®ã¨ããBPFã®ãã¤ããªã«ã®ã¿å¯¾å¿ãã¦ãã¾ãã
ã¡ãªã¿ã« "Ru"by "C"ompiler -> RuC -> Rucy ã¨ããçµç·¯ã§ãRucyã¨ãã綴ãããããªãã«Rubyã¨ç©ºç®ããç¹å«ãã¦ï¼ï¼ï¼ æ°ã«å ¥ã£ãåä»ãã§ãã
Rucyã«ã¤ãã¦ææ¡ããåã«BPFã®èª¬æããããªãã¨ãããªãã®ã§ãããè¦ããã«Linuxã®æ©è½ã®ä¸ã¤ã§ãã«ã¼ãã«ã®ä¸ã§å®å ¨ãã¤é«éã«ã¦ã¼ã¶ã®ä½æããããã°ã©ã ãåä½ãããä»çµã¿ã§ããéã«ããã¨ãã£ã«ã¿ãªã³ã°ãå¾æï¼Packet Filterã§ãããï¼ãªã®ã§ãé«éã«iptablesçãªãã®ãä½ã£ããããããã¯ã«ã¼ãã«ã®é¢æ°å¼ã³åºãã®æ å ±ãéãã¦ããã©ã¼ãã³ã¹è¨æ¸¬ããããªã©ã«ä½¿ãã¾ãã
@chikuwait ããã«ããã¹ã©ã¤ããããããããã®ã§è©³ç´°ã¯ãã¡ãã«è²ãã¾ãã
ãã¤ã³ãã¯ããã¦ã¼ã¶ã®ä½æããããã°ã©ã ãã¯BPFã®ããã®ãã¤ãã³ã¼ã*1ã§ããå¿
è¦ãããç¹ã§ãããã®ãã¤ãã³ã¼ããå«ããã¤ããªã¯ä»ã®ã¨ãã clang ã¾ã㯠gcc ãç¨ããCè¨èªã®ã½ã¼ã¹ã³ã¼ãã bpf
ã¿ã¼ã²ããã§ã³ã³ãã¤ã«ãããã¨ã§ä½æããã®ãæ®éã§ãã
ãã®BPFã®ãã¤ããªãã«ã¼ãã«ã«ãã¼ãããããããããã¯å®è¡ããçµæãä½ãããã®å½¢ã§åãåºãå å·¥ããããã°ã©ã ã¯ã©ã®è¨èªã§ãæ¸ãã¾ãããããã£ã¦Rubyãå¾æãªãRubyã§æ¸ãã¦æ§ãã¾ãã*2ã
ä¸æ¹ã§ãã®ããã¤ããªèªä½ããRubyã®ã³ã¼ããæ¸ãã ãã§ä½ãããå¬ããã ããã¨ãããã¨ã§ãRucyãéçºãã¦ã¿ããã¨ã«ãã¾ããã
ã¡ãªã¿ã«ãã®BPFã®ããã®ãã¤ããªã¯ELFå½¢å¼ï¼ãªãã±ã¼ã¿ãã«ãã¡ã¤ã«æ±ãï¼ã§ãtexté åã«BPFãã¤ãã³ã¼ããä¿æãããããªå½¢ã«ãªãã¾ããä¸ææELFãã¡ã¤ã«ã®ãã¨ããã¤ãã調ã¹ã¦ãã®ã¯Rucyã®ããã§ããã
Rucy ãæ¯ããæè¡
ã¹ã©ã¤ã ã«ãããã®ã§ããããã£ãããRubyã¹ã¯ãªãããâãmrubyã®ãã¤ãã³ã¼ããâãBPFã®ãã¤ãã³ã¼ããâãELFãã¤ããªãã¨ããçµè·¯ã§å¤æãã¦ãã¾ãã
ãRubyã¹ã¯ãªãããâãmrubyã®ãã¤ãã³ã¼ãã
ã®ç®æã¯å½ç¶ã¨ããããmrbcã®ä¸ã§ä½¿ããã¦ããCã®å½è©²é¢æ°ãåå©ç¨ãã¦ãã¾ãã
ãã ããã®å
ã® ãmrubyã®ãã¤ãã³ã¼ããâãBPFã®ãã¤ãã³ã¼ãã
ã®é¨åã¾ã§Cè¨èªã§æ¸ãã»ã©Cè¨èªãä¿¡ç¨ã§ããªãã£ãããããã®Passã¯Rustãç¨ãã¦å¤æãã¸ãã¯ãå®è£
ãã¦ãã¾ãããªã®ã§ãå®ã¯mrubyã®ãã¤ãã³ã¼ããå½ä»¤åä½ã«åºåãå¦çã¯èªåã§åå®è£
ãã¦ããããã¾ãã
ã¾ããmrubyã®VMï¼ã¬ã¸ã¹ã¿ãããã使ããï¼ã¨BPFã®VMï¼ã¬ã¸ã¹ã¿10åã¾ã§ãä¸é¨ã¯ç¨éãæå®ï¼ã¯ãåãã¬ã¸ã¹ã¿åVMã¨ã¯ããææ³ãè²ã éããæã£ã以ä¸ã«ã翻訳ãã¯è¦å´ãã¾ããã
ã¨ã¯ããPoCã¬ãã«ã¯åä½ããããã«ãªããçºè¡¨ã§ã¯è¨åãã¦ãã¾ããã§ããããæä½éã®æååã®åãè¾¼ã¿ãã BPF_CALL
ã®ãµãã¼ããã¤ã¾ãBPFãã«ãã¼é¢æ°å¼ã³åºããåãããã«ãã¦ãã¾ããã¤ã¾ã bpf_trace_printk
ã使ã£ã¦ãã£ã«ã¿ããçµæãã¦ã¼ã¶ã¹ãã¼ã¹ã«ãã£ã¼ãããã¯ãããã§ããããã«ãªã£ã¦ãã¾ããããã§çãå°½ããã¨ããã°ããã§ãã...ã
ãBPFã®ãã¤ãã³ã¼ããâãELFãã¤ããªã
ã®é¨åã¯libelfãç¨ãã¦ãã¾ããlibelfã¯ãã¾ãããã¥ã¡ã³ãããªãã£ãã®ã§è¦å´ãã¾ãããããããã¨ããããåãã¦ãã®ã§ãã£ã¨å¤§ä¸å¤«ã
åæç¥èãå¤ãè¦å´ã¯ãã¾ããããã¨ã¯ããããããã®Passã¯ãããã®ã使ã£ããå®è£ ä¸ã®é¢å¿ãã¨ãæ¸ãããã¨ãã§ããã®ã§ãå ¨ä½ã¨ãã¦RubyKaigiã¾ã§ã«ãªãã¨ãªãåããã®ãå®æãã¾ããããã¨Rustã®FFIã¯ç´ æ´ãããã
ããããã³ã³ãã¤ã©*3å®è£ ã®ã¢ã¤ãã¢ãæãã¤ããã®ã¯ãå½æå人çã«èª¿ã¹ã¦ããè¨èªå®è£ ãã³ã³ãã¤ã©ã®åä½ã®ä»çµã¿ã®ç¥èã大ãã«å½¹ç«ã£ãæãããã¾ãã
åä½ã®æ§å
RubyKaigi takeoutã®é²ç» ããããã¯Twitterã«ã¢ãããããã¢ãªã©ãã©ããã
Rucyã®demoåç»æ®ã£ã #rubykaigihttps://t.co/xtiFLHXwMS pic.twitter.com/9Lhi4aGOrF
— Uchio Kondoð (@udzura) August 30, 2021
ã¢ã¯ã»ã¹ããããã¤ã¹ã®majorã1ãminorã9ï¼ /dev/urandom
ï¼ã®ã¨ãã ããªã¸ã§ã¯ããããããªãã¢ã§ããCã§æ¸ãã¦ããããªå¤ãããã§ããã©...ã
ã¨ã«ããã¡ããã¨åããã¤ããªãåãåºããã¨ãã§ãã¾ãï¼ï¼
ä»å¾
å¾ç¶ã¨æ¸ãã¾ããããä»å¾è²ã ãããããã¨èªä½ã¯ããã¾ã...ã
- BPFãã¼ã«ã¨ãã¦ç¾å®ã§ä½¿ããç¨åº¦ã®æ©è½ãæããã
ãã¾ã®ã¨ãããBPF Mapï¼perf event mapå«ãï¼ãä½ã£ãããrodataçµç±ã§ãã©ã¡ã¼ã¿ãæ³¨å ¥ããããå ¨ã¦ã®BPFãã«ãã¼é¢æ°ããã¹ããããã¨ãããã¨ã¯å®ç¾ãã¦ããªãã®ã§ãããªãéãããç¨éã®ã¿ã«ãªãã¾ã...ãã¾ããããã®æ©è½ã使ãã¨ããã©ãããBPFãã¤ããªãå¿ è¦ã«ãªããã調ã¹ãã¨ããããã§ãããå°éã«é²ãã¦ãããããªãã
- Rubyã ãã§BPF CO-RE対å¿ã§ããããã«ãã
æ¨ä»ã®BPFãã¼ã«ã¯BPF CO-REã¨ãã ãã«ã¼ãã«ã®ãã¼ã¸ã§ã³å·®ç°ãå¸åãã¤ã¤ã©ãã§ãåããããªãã¤ããªãã©ã¼ãããã«åºããæµãã«ãªã£ã¦ãã¾ãã
Infrared *4 ã¨ããããã¸ã§ã¯ããç«ã¡ä¸ãã¦ãRucyã¨Rubyã ãã§ã¾ãã¯ã¯ã³ãã¤ããªãä½ããããã«ããã§ããã°Rubyã¨ã³ã·ã¹ãã ã®ããã©ã¼ãã³ã¹æ¸¬å®ãªã©ã«å¯ä¸ãããã¨æã£ã¦ã¯ããã®ã§ããã壮大ãªæ§æ³ã§ãªããªã...ã
ã²ã¨ã¾ãmrubyã§libbpfã®bindingãä½ããskelãããæãã«çæãããã°ç°¡åãªãã¼ã«ã¯æ¸ããããã«ãªããã¨æãã¾ããç°¡åã«è¨ã£ã¦ã¿ããã®ã®ããã ãã§ãææ°ã¯å¤ããããããããã«CO-REã®æ å ±ãELFãã¡ã¤ã«ã«åãè¾¼ãå¿ è¦ãããã®ã§ã...ã
- ããã¯ã¨ã³ããå¢ãã
å人çã«WebAssemblyã®ãµãã¼ããå ¥ãã¦ã¿ããã£ããã§ããã MRI to WebAssembly ã¯é½è¤ããããã£ã¦ãã ãã ã£ã½ãã®ã§åªå 度ã¯ä½ããªãã¾ãããã»ãã¯ãLLVM-IRã¨ãï¼ãé¢ç½ãã¤ããªãã©ã¼ããããããã°ç¥ãããã§ãã
- Rubyã¹ã¯ãªããã®ãã¼ã¹ã¨ä¸éå½¢å¼ãèªåã®ãã®ã«ãã
mrubyã®ãã¤ãã³ã¼ãããã¼ã¹ã«ãããã¨ã§è²ã ã¹ãããã§ããã®ã¯äºå®ã§ãããããããBPFã®ããã®ãã©ã¼ãããã§ã¯ãªãã®ã§ç¡çããã¦ããç®æãããï¼ç¹ã«ã¬ã¸ã¹ã¿ã®ä½¿ãæ¹ãç´°ããè¦ç´ãéã£ã¦ãã¦å¤§å¤ï¼ãmrubyã«é ¼ã£ã¦ããç®æãå¾ã ã«èªåã«ã§ããªããã¨èãã¦ãã¾ãããã ããã®è¾ºã®å·®ç°ãåããã®ã¯ãä¾ãã°æé©åã«ã¤ãã¦ç§ãå¦ç¿ãã¦ããã°ãªãã¨ã対å¦ã§ããã¬ãã«ããããã¾ããããããã«ããåå¼·ããªãã¨ãããªãç¯å²ã¯åºãã
æå¾ã«
Rucy ãå®è£ ããã«ããã£ã¦ã以ä¸ã«æããçæ§ã«ãä¸è©±ã«ãªãã¾ãããã礼ãç³ãä¸ãã¾ãã
- ããã°ã©ãã³ã°è¨èªå¦çç³»ã好ããªäººã®éã¾ã ã®å®ä¾ãªã³ã©ã¤ã³ãã¼ãã¢ããã§æ§æ³ãã話ãããæ©ä¼ãå¾ã¦ããã®å ´ã§è²ã ãæè¦ãããã ãã¾ãããã¨ã«ãããã¡ããã¡ãåºæ¿ã«ãªãSlackãªã®ã§ã¿ããªå ¥ãã¾ãããã
- ãããã¿ã¤ãã®ç¶æ ï¼ãããä»ãï¼ï¼ãããã¡ã½ãããåæ¦å çãä¹å·å¤§å¦ã®å¶åå çï¼ãµã¤ãã¼ã»ãã¥ãªãã£ã»ã³ã¿ã¼ï¼ãç¬ åå çï¼æ å ±åºç¤ç 究éçºã»ã³ã¿ã¼ï¼ã«ã覧ããã ãæ©ä¼ãå¾ã¦ãè²ã ã¨ã¢ããã¤ã¹ãææ³ãããã ãã¾ããã大å¤å©ãã«ãªãã¾ããã
æè¿ã¯BPFã¨ããããã¾ããVMã£ã¦ã©ããã£ã¦ä½ããã ããï¼ãã¨æã£ã¦ Crafting Interpreters ããCISPããLuaã®å®è£ ããã«æãåºãã¦ãã¾ã£ã¦åæ¾ãã¤ããªãæãããã¾ãããè²ã¦ã¦ããããã§ãã...ã
ã¨ãããããã¯ãRubyãã«ã¬ã³ãã¼ã®è¨äºãªã®ãï¼ãã¾ããããã
ææ¥ã¯ #1 ã¯@hasumikin師å ã #2 ã¯@Reichardtããã§ãã
*1:BPFã¯VMãæã¡ãç¹å®ã®å½ä»¤ã»ãããæã£ã¦ããä¸ç¨®ã®ãã¤ãã³ã¼ãè¨èªã§ã
*2:以åä½æãã RbBCC ã¯ããããã³ã³ã»ããã§ã https://github.com/udzura/rbbcc
*3:ãããã£ã¦èª¬æããã¨ãå²ã¨æ®éã«ã³ã³ãã¤ã©ãã¦ã¾ãã...
*4:ï¼èµ¤å¤ç·