LuaのVM - Tsukuba.pm #3 · •Lua/MoonScriptとは Lua 弱い動的型付け言語 ←Perl {...

Post on 19-Aug-2020

2 views 0 download

Transcript of LuaのVM - Tsukuba.pm #3 · •Lua/MoonScriptとは Lua 弱い動的型付け言語 ←Perl {...

Lua VMTsukuba.pm #3

May 14, 2016

•自己紹介〜

◮ 大学 4年滞在中 3年生◮ 大学 来 ン 始 ン◮ Lua/MoonScript く書く

Figure: 白い大 花 咲 い 百合

自己紹介 1/26

•Lua/MoonScriptとは

◮ Lua

弱い動的型付け 言語←Perl · · · · · · ?産!! 軽量!! 関数 ー 扱え !!

文法 簡単 予約語 22個 少 い◮ MoonScript

JS 対 CoffeeScript い やend地獄 解消 内包表記 ー OOP 文法 強化

Lua .Lua/MoonScript 2/26

•Lua/MoonScriptとは

Perl · · · · · ·

Lua .Lua/MoonScript 3/26

•Luaとは?!

◮ 軽量2 NetBSD Wiresharkや (LuaLATEX) 組 込

ー VM上 動く (PUC-Lua)◮ 関数 ー高階関数 扱う CPS 用い 例外処理機構 あ Lua ン

*1 実装◮ 唯一 ー 構造 table

◮ 簡単 言う 連想配列←Perl · · · · · · ? 数字 キー◮ 関数 数値 文字列 table自身 全部詰 込◮ ー いう機能 用い 演算子 ーバー ー や

ー 指向 ン

*1 https://github.com/nymphium/llixLua .Lua ?! 4/26

•Luaとは?!

Perl · · · · · · い

Lua .Lua ?! 5/26

•Lua VMについて

◮ ネ ・ 大学 開発 Lua処理系 ( 本家,PUC-Lua) VM

ソー →バ ー →VM 実行

調査 解析

Lua5.2 見 (LuaLATEX v5.2 ー )

Lua VM 調査.Lua VM い 6/26

•Lua VMについて1環境 ( ン ン 関数 ー ) (slots, constantsupvalues locals functions) 用意

function hello()print("hello")

end

slots: 2個 (print "hello" セッ )constants: 2個 ("print" "hello")upvalues: 1個 (_ENV*2)functions: 0個

locals: 0個

*2 ーバ 変数 _ENV table 持 くLua VM 調査.Lua VM い 7/26

•バイ コー

◮ luac ン バ ー 生成◮ luac -l bytecode.out いい感 出力◮ luac -l -l bytecode.out 情報 増え

Lua VM 調査. バ ー 8/26

•バイ コー標準入力 渡

cat <<LUACODE | luac -l -print("hello, world")LUACODE

main <stdin:0,0> (4 instructions at 0x2268790)0+ params, 2 slots, 1 upvalue, 0 locals, 2 constants, 0 functions

1 [1] GETTABUP 0 0 -1 ; _ENV "print"2 [1] LOADK 1 -2 ; "hello, world"3 [1] CALL 0 2 14 [1] RETURN 0 1

Lua VM 調査. バ ー 9/26

•バイ コーLuaLATEX 書い

Code 1: codes/texutils/dumpdemo.lua1 tex.print([==[\begin{lstlisting}[numbers=none]]==])2 local function foo() end -- 関数内 環境 書 出3 local LEN = 314 local acc, cnt = "", 05 for c in string.dump(foo):gmatch(".") do6 acc = acc .. ("%02X"):format(c:byte())7 cnt = cnt + 18 if cnt % LEN < 1 then9 tex.print(acc)

10 acc = ""11 else acc = acc .. " "12 end13 end14 if #acc > 0 then tex.print(acc) end15 tex.print([[\end{lstlisting}]])

1B 4C 75 61 52 00 01 04 08 04 08 00 19 93 0D 0A 1A 0A 02 00 00 00 02 00 00 00 00 00 02 01 0000 00 1F 00 80 00 00 00 00 00 00 00 00 00 00 00 00 00 1D 00 00 00 00 00 00 00 40 63 6F 64 6573 2F 74 65 78 75 74 69 6C 73 2F 64 75 6D 70 64 65 6D 6F 2E 6C 75 61 00 01 00 00 00 02 00 0000 00 00 00 00 00 00 00 00

Lua VM 調査. バ ー 10/26

•読 方

1B 4C 75 61 52 00 01 04 08 04 08 00 19 93 0D 0A 1A 0A 02 00 00 00 02 00 00 00 00 00 02 01 0000 00 1F 00 80 00 00 00 00 00 00 00 00 00 00 00 00 00 1D 00 00 00 00 00 00 00 40 63 6F 64 6573 2F 74 65 78 75 74 69 6C 73 2F 64 75 6D 70 64 65 6D 6F 2E 6C 75 61 00 01 00 00 00 02 00 0000 00 00 00 00 00 00 00 00

Lua VM 調査. 読 方 11/26

•header block1B 4C 75 61 52 00 01 04 08 04 08 00 19 93 0D 0A 1A 0Aheader block

◮ 4bytesheader signature (ESC Lua )

◮ 1byteLua Version(例 52 Lua 5.2)

◮ 1byteFormat Version(0 = official(default))

◮ 1byteEndianness flag(1 = little endian(default), 0 = bigendian)

◮ 1bytesize of int (C lang)

◮ 1bytesize of size_t (C lang)

◮ 1bytesize of instruction

◮ 1bytesize of lua_Number*3(C lang)

◮ 1byteIntegral flag(0 = floating point(default), 1 = integralnumber type)

*3 32bit integerLua VM 調査. 読 方 12/26

•header block

1B 4C 75 61 52 00 01 04 08 04 08 00 19 93 0D 0A 1A 0A◮ 6bytesLUAC_DATA, data to catch conversion errors (ソー ン )

◮ 19 93Lua1.0 ー 年

◮ 0D 0ADOS 改行 (CR LF) DOS→UNIX 改行 ー 変換 検知

◮ 1ASUB

◮ 0AUNIX 改行 (LF) UNIX→DOS 改行 ー 変換 検知

Lua VM 調査. 読 方 13/26

•header block

Lua5.2

1B 4C 75 61 52 00 01 04 08 04 08 00 19 93 0D 0A 1A 0A⇓

Lua5.3header signature

1B 4C 75 61 version53 format version00LUAC_DATA

19 93 0D 0A 1A 0Aobjects sizes

04 08 04 08 08LUAC_INT*4

78 56 00 00 00 00 00 00LUAC_NUM*5

00 00 00 00 00 00 00 28 77 40

長い

*4 Endianness 検査 0x5678 dump い 場合 ン ン*5 IEEE754 float format 検査

Lua VM 調査. 読 方 14/26

•function block

02 00 00 00 02 00 00 00 00 00 02 01 00· · · · · ·function block

◮ intline defined

◮ intlast line defined

◮ 1bytenumber of parameters

◮ 1byteis_vararg

◮ 1bytenumber of registers used by the function

◮ Listnumber/list of instructions

◮ Listnumber/list of constants

◮ Listnumber/list of upvalues

◮ Listdebug info

Lua VM 調査. 読 方 15/26

1B 4C 75 61 52 00 01 04 08 04 08 00 19 93 0D 0A 1A 0A 02 00 00 00 02 00 00 00 00 00 02 01 0000 00 1F 00 80 00 00 00 00 00 00 00 00 00 00 00 00 00 1D 00 00 00 00 00 00 00 40 63 6F 64 6573 2F 74 65 78 75 74 69 6C 73 2F 64 75 6D 70 64 65 6D 6F 2E 6C 75 61 00 01 00 00 00 02 00 0000 00 00 00 00 00 00 00 00

header block

function block

Lua VM 調査. 読 方 16/26

•VM instructions32bit 3 形式

◮ iABCopcode R(A) R(B) R(C)

◮ iABxopcode R(A) (unsigned integer)Bx

◮ iAsBxopcode R(A) (signed integer)Bx

31 0 bitiABC B:9 C:9 A:8 Opcode:6

iABx Bx:18 A:8 Opcode:6

iAsBx sBx:18 A:8 Opcode:6

Lua VM 調査.VM instructions 17/26

•example

Q. 命令 ?1F 00 80 00 (funciton foo() )

1. endian 考慮 bitッ 情報 ン ン

1F 00 80 00→ 00 80 00 1F⇓

B0000000 1 C0000000 00 A000000 00RETURN0111112. Opcode 0b11111 RETURN3. RETURN iABC形式 ( R(C) not used)

A. RETURN 0 1 (0)ン 終わ 必 読 便利!!

Lua VM 調査.VM instructions 18/26

•example

Q. 命令 バ ー 直 ? (little endian)CALL 0 2 1

1. CALL Opcode 0b111012. iABC形式 32bit 以下 う

B0000001 0 C0000000 01 A000000 00 CALL0111013. little endian 16進数 わ

A. 1d 40 00 01

Lua VM 調査.VM instructions 19/26

•解析

いhttps://gist.github.com/Nymphium/d47385929fd0d23e98207670f9c588c3

◮ Lua 5.2バ ー ー ッ◮ header/function block constants 解析◮ MoonScript 実装

バ 一 け · · · · · ·

◮ 5.1 バ ー 資料見 実装 い 5.2 変わホン キ

◮ 5.2 5.3 い い違 キ (2)

解析 20/26

•解析

解析 21/26

•解析

1. header block 情報 読 取

2. 読 取 情報 基 functoin block 読 取

解析 22/26

•解析

う◮ 最適化最適化 い ほ い (TAICALL number 即値演算程度)い い う

◮ 型 ッconstants pool 逆 命令 見 いけ いけ

◮ 可視化 ( ー )

解析 23/26

• とめ

◮ Lua MV バ ー 読◮ Lua バ 解析◮ Lua 気持 理解◮ わ バー ン 互換 い◮ Lua 高速化 部分 あ

24/26

•参考文献

◮ Lua 5.2 Bytecode and Virtual Machine (Web site)http://files.catwell.info/misc/mirror/lua-5.2-bytecode-vm-dirk-laurie/lua52vm.html

◮ A No-Frills Introduction to Lua 5.1 VM Instructions (pdf)http://luaforge.net/docman/83/98/ANoFrillsIntroToLua51VMInstructions.pdf

◮ Source code◮ on web

http://www.lua.org/source/5.2/◮ tar

https://www.lua.org/ftp/lua-5.2.4.tar.gz

https://www.lua.org/ftp/lua-5.3.2.tar.gz (stable latest)

参考文献 25/26

26/26