MJIT 㧠dlopen 使ããã« ELF ãªãã¸ã§ã¯ããç´æ¥ãã¼ããã話
MJIT ã¨ããã®ã Ruby ã«å ¥ã£ãã®ã¯èãã¦ãã¦ãããããã¨ãããªãã¨æã£ã¦ããã§ãããå®éã« Ruby Kaigi ã§è©±ãèãã¦å°ãéãã§ã¿ãããªã£ãã®ã§ãããããããã° https://turingcomplete.fm/5 ã®æã«ãMJITã«ã¤ãã¦ã©ãæããèãã¦ããã¦ä¸ãããã¨ããªã¯ã¨ã¹ããã¦ãããã®ã«èãã¦ãããªãã£ãã®ã§ãã£ãã
https://k0kubun.hatenablog.com/entry/ruby26-jit
ããããã¨ãããªãã¨æã£ãã®ã¯ãã®ææ³ã§ã C çæã㦠dlopen ã¨ãããããéè«ã¨ãã§è¨ã話ã§ã¯ãããã©ãå®éåºã使ãããç¨éã§ä½¿ãããã®ã¯è¦ããã¨ãç¡ãã£ã(ICFPCã¨ãã§ã¯è¦ããã¨ããã)ã®ã§ãããããªã¨ã
ä¸æ¹ã§ã dlopen ããããããã¨ãããã¤ãã®æå³ã§ãªã¼ããããããããã¨æãããããããã¨ã§ãã¤ã¯ããã³ãã«åºãªãã¨ããã§å¤§å¤ã ã£ããããã ãããªãâ¦â¦ç¹ã«ã¡ã¢ãªã®ãã¼ã«ãªãã£çãªãã¨ãæãã¤ã¤èãã¦ããã¨ããã¯ãã¡ã¢ãªã®ãã£ãã·ã¥ãããçãæ¸ãã¯ãããã¨æªãå½±é¿ãâ¦â¦ã¨ãã話ã«ãªãã¾ãããä¸ã¤ä¸ã¤ã® C ã¬ãã«é¢æ°ã¯æããå°ããã®ã§ã¯ãªãããªãã¨æã£ã¦ããã®ã§ãã³ã¼ããµã¤ãºã«ãã¦ä¾ãã° 100-1000 ãã¤ããããã®é¢æ°ã®ããã«ã dlopen ããã¨å°ãªãã¨ãå¿ è¦ã§ããã r-x ã® .text ãã¼ã¸ã¨ GOT ã®ããã® r-- (or rw-) ã®ãã㧠8192 ãã¤ãã使ããã¨ã«ãªãã®ã§ã¯ãªãããªãâ¦â¦ã¨æã£ã¦ããã®ã§ããã
Ruby Kaigi ä¸ã«è³ªåããã®ã§ããããä¸ã¤ã®é¢æ°ãã¨ã«æä½ 8 or 12kB 使ãããªâ¦â¦ãã¨æã£ã¦ããã¨ããã«ã2MB/methodï¼ãã¿ãããªã¹ã©ã¤ããè¦ã¦ããªãããããã¨æã£ãããã§ããããã¨ã§ ã¹ã©ã¤ã ãããçºããã¨ã .text 㨠.rodata ã®éã®ããããªããã¤ãã«ã¦ã³ããã¦ãã ãã¨æ°ä»ãã¾ããã 2MB ã®ä»®æ³ã¢ãã¬ã¹ãåã£ã¡ããã®ãã©ããªãã ã£ã¦è©±ã¯ãããããããªãã§ããããããã3ã¤ã®ãã¼ã¸ã«åå²ãããã®ãæ¬è³ªçãªåé¡ã ã¨æãã¾ãããã¤ã MJIT ã®åãã¦ãã³ã¼ããè¦ãéã .data 㨠.rodata ã¯ä½¿ããªãããªã®ã§ãå®éã«è§¦ããã¼ã¸ã¯ .text ç¨ã®ãã¤ã¨ .got ã®ãã¤ã§2æã ãã§ãªããã¨æãã¾ãã
ãã¦ããã®åé¡ã«ã¤ãã¦ã®æ£ãã解決çã¯ãé©åãªåä½ã§è¤æ°ã®ã¡ã½ãããã¾ã¨ãã¦ã³ã³ãã¤ã«ãããã¨ãããã®ãããªããã¨æãã¾ããã§ãã¤ããã¯ææ¡ããã¦ããææ³ãªã®ã§è¯ãã¨æãã¾ããã¨ã¯ãã .got ã®ããã« .so ä¸ã¤ãã¨ã«ç¡é§ã« 4kB 使ãã¨ããã®ããªãâ¦â¦ãªã©ã¨ãæãã¾ããã§ã .got 㨠.text ãã¾ã¨ãã¦åããã¼ã¸ã«çªã£è¾¼ãã§ãããããªãªã³ã«ãªãã·ã§ã³ç¡ããâ¦â¦ã¨æ¢ããã(ãªãã£ã)ããããã RELRO 㦠.got ã RX ã«ãããã¨ã§ããã®â¦â¦ã¨ãã¼ãçºããã(ã§ããªãã£ã)ãã¾ããã
ãªã¼ãã¦ãããã¨ãèãã¦ãããã¨ããããã ããã¯ã¶ãã ããªã .so ããã¼ãã«èªã¾ããã®ã§ã¯ãªãã¦ã .o ãèªåã§é©å½ã«ãã¼ãããã°ãããããã¨ãããã¨ã§ããããããã®ãã§ã£ã¡ããã¾ããã
https://github.com/shinh/ruby/tree/objfcn
ã¾ãâ¦â¦ä½¿ãããããã®ã§ã¯ãªãã¨æãã¾ããæ¬è³ªçã« MJIT ã®ãã㪠.so ãèªã以å¤ã®ææ³ã§æ¸ããã JIT ã¨ã³ã¸ã³ã«ããåé¡ã§ããããã¯ããããã°ã¯å¤§å¤ã§ãããèªåã§ã¡ããã¨ç©ºããã¡ã¢ãªã解æ¾ããã¨ããé¢åã§ãã(ç¾ç¶1GBåºå®ã¢ãã±ã¼ãããã¢ã¼ãã¨é ãæ¹æ³)ãã¾ããããªãããªâ¦â¦
ãã ã C ã³ã¼ãåãã¦ã³ã³ãã¤ã«ãã¦ãã¼ããããã£ã¦æ¹éãå¤ãããªãã®ã§ããã°ããã¼ãã«é¢ãã¦ã¯æéãªæ¹æ³ã ã¨æãã®ã§ããã£ã¨é©åãªæ¹æ³ã§ããããè¤æ°ã®ã³ã¼ããã¾ã¨ãã¦ã³ã³ãã¤ã«ãããã¨ããæ¹æ³ã§çè«éçã«è¿ä»ãã¦ããã©ããã¯ãç§ã®ã³ã¼ãã¨æ¯è¼ããã°è©ä¾¡ã§ããã®ã§ã¯ãªããã¨æã£ã¦ãã¾ãã
ãã¨æåã«ãã¼ã¨æ¸ããã³ã¼ãã¯2ã¤ã¯ã©ãã·ã¥ãããã°ããã£ã¦ããããããªãã»ã©ãªãã¨æã£ãã®ã§æ¸ãã¦ããã¾ãã
ä¸ã¤ç®ã¯ mmap ãåãã£ã¦ããéã« PLT/GOT ã®ããã«çæããã³ã¼ãã®ãµã¤ãºãè¨ç®ã«å ¥ãã¦ãªããã¨ãããã®ã§ããããã㯠tinycc ã®æãã©ããããããã¨æã£ãè¨æ¶ãããã¾ããâ¦â¦
äºã¤ç®ã¯ã»ã¯ã·ã§ã³ãããã«æå®ããã¦ãã¢ã©ã¤ã³ã¡ã³ããé©åã«åã£ã¦ããªããã¨ãããã®ã§ãããå ·ä½çã«ã¯ SSE å½ä»¤ã§ data é åãèªãã ãããã³ã¼ãã® SSE ã¢ã©ã¤ã³ã¡ã³ããå£ãã¦ããã¨ãã話ã§ããããªããä½ã¬ã¤ã¤ãªã¨ãããã£ã¦ã㨠SSE ã¢ã©ã¤ã³ã¡ã³ãã¯å¿ ããã°ãã¾ãããã¹ã¿ãã¯ãã¢ã©ã¤ã³åãã¦ãªãç¶æ 㧠printf å¼ãã§æ»ãã ãã¨ãâ¦â¦
ã¨ãã¨ãããªãã§ããããããªæ¹æ³ã MRI ã§æ¡ç¨ãã¹ãã¨ã¯æã£ã¦ãªãã®ã§ããããã ãã®ææ³ã® JIT ã¨ã³ã¸ã³ã®æã¤ãããã¤ããããªã¼ãã¼ãããæºã®ä¸ã¤ã§ããããã¡ã¢ãªããã¼ã¸å¢çã§åæããã¦ãã¾ã£ã¦ãã¼ã«ãªãã£ãè¯ããªããã¨ãããã¤ãã©ã®ãããæªãããã¦ããã®è©ä¾¡ç¨åº¦ã«ã¯ä½¿ããã®ã§ã¯ãªããã¨æã£ã¦ãã¾ãã