Open usuyuki opened 11 months ago
❯ wasm-objdump helloworld.wasm -d
helloworld.wasm: file format wasm 0x1
Code Disassembly:
000095 func[2] <_start>:
000096: 41 80 90 80 80 00 | i32.const 2048
00009c: 41 1c | i32.const 28
00009e: 10 84 80 80 80 00 | call 4
0000a4: 03 40 | loop
0000a6: 41 84 07 | i32.const 900
0000a9: 10 83 80 80 80 00 | call 3
0000af: 41 9c 90 80 80 00 | i32.const 2076
0000b5: 41 0c | i32.const 12
0000b7: 10 85 80 80 80 00 | call 5
0000bd: 0c 00 | br 0
0000bf: 0b | end
0000c0: 0b | end
0000c2 func[3]:
0000c3: 20 00 | local.get 0
0000c5: 10 80 80 80 80 00 | call 0 <arduino.delay>
0000cb: 0b | end
0000cd func[4]:
0000ce: 20 00 | local.get 0
0000d0: 20 01 | local.get 1
0000d2: 10 81 80 80 80 00 | call 1 <serial.print>
0000d8: 0b | end
0000da func[5]:
0000db: 20 00 | local.get 0
0000dd: 20 01 | local.get 1
0000df: 10 81 80 80 80 00 | call 1 <serial.print>
0000e5: 41 a8 90 80 80 00 | i32.const 2088
0000eb: 41 01 | i32.const 1
0000ed: 10 81 80 80 80 00 | call 1 <serial.print>
0000f3: 0b | end
❯ wasm-interp helloworld.wasm
error initializing module: invalid import "arduino.delay"
type checkとかより前にエラーになる。
❯ wasm-decompile helloworld.wasm
export memory memory(initial: 1, max: 0);
global g_a:int = 2048;
export global data_end:int = 2089;
export global heap_base:int = 2096;
table T_a:funcref(min: 1, max: 1);
data d_hellomahiwawritteninRusthell(offset: 2048) =
"hello mahiwa written in Rusthello mahiwa\0a";
import function arduino_delay(a:int);
import function serial_print(a:int, b:int);
export function start() {
f_e(2048, 28);
loop L_a {
f_d(900);
f_f(2076, 12);
continue L_a;
}
}
function f_d(a:int) {
arduino_delay(a)
}
function f_e(a:int, b:int) {
serial_print(a, b)
}
function f_f(a:int, b:int) {
serial_print(a, b);
serial_print(2088, 1);
}
translate from the binary format to readable C-like syntax.
になっている。
❯ wat-desugar helloworld.wasm
helloworld.wasm:1:1: error: unexpected char
helloworld.wasm:1:5: error: unexpected char
helloworld.wasm:1:6: error: unexpected char
helloworld.wasm:1:7: error: unexpected char
helloworld.wasm:1:8: error: unexpected char
helloworld.wasm:1:9: error: unexpected char
helloworld.wasm:1:11: error: unexpected char
helloworld.wasm:1:2: error: unexpected token "asm", expected a module field or a command.
helloworld.wasm:1:12: error: unexpected token `, expected EOF.
長すぎ
verbose付けないと正常時は何も出ない
❯ wasm-validate helloworld.wasm -v
BeginModule(version: 1)
BeginTypeSection(13)
OnTypeCount(3)
OnFuncType(index: 0, params: [i32], results: [])
OnFuncType(index: 1, params: [i32, i32], results: [])
OnFuncType(index: 2, params: [], results: [])
EndTypeSection
BeginImportSection(32)
OnImportCount(2)
OnImport(index: 0, kind: func, module: "arduino", field: "delay")
OnImportFunc(import_index: 0, func_index: 0, sig_index: 0)
OnImport(index: 1, kind: func, module: "serial", field: "print")
OnImportFunc(import_index: 1, func_index: 1, sig_index: 1)
EndImportSection
BeginFunctionSection(5)
OnFunctionCount(4)
OnFunction(index: 2, sig_index: 2)
OnFunction(index: 3, sig_index: 0)
OnFunction(index: 4, sig_index: 1)
OnFunction(index: 5, sig_index: 1)
EndFunctionSection
BeginTableSection(5)
OnTableCount(1)
OnTable(index: 0, elem_type: funcref, initial: 1, max: 1)
EndTableSection
BeginMemorySection(3)
OnMemoryCount(1)
OnMemory(index: 0, initial: 1)
EndMemorySection
BeginGlobalSection(19)
OnGlobalCount(3)
BeginGlobal(index: 0, type: i32, mutable: true)
BeginGlobalInitExpr(0)
OnI32ConstExpr(2048 (0x800))
OnEndExpr
EndGlobalInitExpr(0)
EndGlobal(0)
BeginGlobal(index: 1, type: i32, mutable: false)
BeginGlobalInitExpr(1)
OnI32ConstExpr(2089 (0x829))
OnEndExpr
EndGlobalInitExpr(1)
EndGlobal(1)
BeginGlobal(index: 2, type: i32, mutable: false)
BeginGlobalInitExpr(2)
OnI32ConstExpr(2096 (0x830))
OnEndExpr
EndGlobalInitExpr(2)
EndGlobal(2)
EndGlobalSection
BeginExportSection(46)
OnExportCount(4)
OnExport(index: 0, kind: memory, item_index: 0, name: "memory")
OnExport(index: 1, kind: func, item_index: 2, name: "_start")
OnExport(index: 2, kind: global, item_index: 1, name: "__data_end")
OnExport(index: 3, kind: global, item_index: 2, name: "__heap_base")
EndExportSection
BeginCodeSection(97)
OnFunctionBodyCount(4)
BeginFunctionBody(2, size:44)
OnLocalDeclCount(0)
OnI32ConstExpr(2048 (0x800))
OnI32ConstExpr(28 (0x1c))
OnCallExpr(func_index: 4)
OnLoopExpr(sig: void)
OnI32ConstExpr(900 (0x384))
OnCallExpr(func_index: 3)
OnI32ConstExpr(2076 (0x81c))
OnI32ConstExpr(12 (0xc))
OnCallExpr(func_index: 5)
OnBrExpr(depth: 0)
OnEndExpr
OnEndExpr
EndFunctionBody(2)
BeginFunctionBody(3, size:10)
OnLocalDeclCount(0)
OnLocalGetExpr(index: 0)
OnCallExpr(func_index: 0)
OnEndExpr
EndFunctionBody(3)
BeginFunctionBody(4, size:12)
OnLocalDeclCount(0)
OnLocalGetExpr(index: 0)
OnLocalGetExpr(index: 1)
OnCallExpr(func_index: 1)
OnEndExpr
EndFunctionBody(4)
BeginFunctionBody(5, size:26)
OnLocalDeclCount(0)
OnLocalGetExpr(index: 0)
OnLocalGetExpr(index: 1)
OnCallExpr(func_index: 1)
OnI32ConstExpr(2088 (0x828))
OnI32ConstExpr(1 (0x1))
OnCallExpr(func_index: 1)
OnEndExpr
EndFunctionBody(5)
EndCodeSection
BeginDataSection(48)
OnDataSegmentCount(1)
BeginDataSegment(index: 0, memory_index: 0, flags: 0)
BeginDataSegmentInitExpr(0)
OnI32ConstExpr(2048 (0x800))
OnEndExpr
EndDataSegmentInitExpr(0)
OnDataSegmentData(index:0, size:41)
EndDataSegment(0)
EndDataSection
EndModule
❯ wast2json helloworld.wasm
helloworld.wasm:1:1: error: unexpected char
``` arduinodelayserialprintp...
^
helloworld.wasm:1:5: error: unexpected char
``` arduinodelayserialprintp...
^
helloworld.wasm:1:6: error: unexpected char
``` arduinodelayserialprintp...
^
helloworld.wasm:1:7: error: unexpected char
``` arduinodelayserialprintp...
^
helloworld.wasm:1:8: error: unexpected char
``` arduinodelayserialprintp...
^
helloworld.wasm:1:9: error: unexpected char
``` arduinodelayserialprintp...
^
helloworld.wasm:1:11: error: unexpected char
``` arduinodelayserialprintp...
^
helloworld.wasm:1:2: error: unexpected token "asm", expected a module field or a command.
``` arduinodelayserialprintp...
^^^
helloworld.wasm:1:12: error: unexpected token `, expected EOF.
``` arduinodelayserialprintp...
❯ ll
Permissions Size User Date Modified Git Name
drwxr-xr-x - usuyuki 4 12月 22:44 -- .cargo/
.rw-r--r-- 7 usuyuki 4 12月 22:44 -- .gitignore
.rw-r--r-- 259 usuyuki 8 12月 20:32 -I Cargo.lock
.rw-r--r-- 230 usuyuki 4 12月 22:44 -- Cargo.toml
.rwxr-xr-x 294 usuyuki 4 12月 22:44 -- helloworld.wasm*
.rw-r--r-- 1.1k usuyuki 4 12月 22:44 -- helloworld.wat
.rw-r--r-- 456 usuyuki 4 12月 22:44 -- README.md
drwxr-xr-x - usuyuki 4 12月 22:44 -- src/
drwxr-xr-x - usuyuki 8 12月 20:32 -I target/
❯ wasm-strip helloworld.wasm
❯ ll
Permissions Size User Date Modified Git Name
drwxr-xr-x - usuyuki 4 12月 22:44 -- .cargo/
.rw-r--r-- 7 usuyuki 4 12月 22:44 -- .gitignore
.rw-r--r-- 259 usuyuki 8 12月 20:32 -I Cargo.lock
.rw-r--r-- 230 usuyuki 4 12月 22:44 -- Cargo.toml
.rwxr-xr-x 294 usuyuki 14 12月 10:21 -- helloworld.wasm*
.rw-r--r-- 1.1k usuyuki 4 12月 22:44 -- helloworld.wat
.rw-r--r-- 456 usuyuki 4 12月 22:44 -- README.md
drwxr-xr-x - usuyuki 4 12月 22:44 -- src/
drwxr-xr-x - usuyuki 8 12月 20:32 -I target/
容量変わらない。(Rustで作ったwasmは最初から最適化されている説がある)
✨ 概要
https://github.com/WebAssembly/wabt
なぜ必要
備考
コメントにて,ツールの動作結果を示す.