Share
Embed
WebAssemblyの現状と展望 ~言語ツールチェインからWASIまで~
Infra Study 2nd #4「セキュリティエンジニアリングの世界」 https://forkwell.connpass.com/event/219136/
mathetake
August 24, 2021
Transcript
52 slides
-
• Takeshi Yoneda (Ϛελέ) / Twitter, Github: @mathetake • Software
Engineer at Tetrate.io • OSS dev: Envoy, Istio, Proxy-Wasm. • Stable maintainer of Envoy • C++ maintainer of Proxy-Wasm project • Creator of Go, Zig SDK for Proxy-Wasm • Contributor of V8, Ziglang, TinyGo, etc. whoami -
1. Introduction to WebAssembly (Wasm) 2. WasmͱݴޠπʔϧνΣΠϯ 3. Proxy-Wasm ABI:
Envoy with Wasm VMs 4. Wasmͷ͜Ε͔Β Agenda -
WebAssembly design goals • Safe • UntrustedͳίʔυΛ҆શʹ࣮ߦՄೳ • Language-independent (Polyglot)
• Portable • ҙͷҰൠతͳϋʔυΣΞ্ͰαϙʔτՄ • Platform-independent • ϒϥβ͚ͩͰͳ͘ҙͷڥ(e.g. OS)ʹຒΊࠐΊΔ • Open • WebAssemblyͱϗετڥͷؒͷAPI͕Φʔϓϯ https://webassembly.github.io/spec/core/intro/introduction.html -
Why Wasm is innovative? Why WebAssembly is innovative even outside
the browser: https://www.tetrate.io/blog/wasm-outside-the-browser/ -
• Stack-basedͳԾϚγϯͱόΠφϦͷ༷ • ݩʑJavaScriptͷߴԽ͕త • asm.js -> WebAssembly(Wasm)ͱਐԽ • ༷ΛಡΊ͔Δ͕Ұݴ
“host” ͷཁٻ͕ͳ͍ • Portable, Platform-agnostic, Open-ended • Run at near-native speed: ΊͬͪΌ͍(※࣮ʹΑΔ) • Security: ελοΫ͕ϓϩάϥϜ͔Βݟ͑ͳ͍ͱ͔ͦ͏͍͏ͷ WebAssembly 101 -
• Import Functions: ผ໊Hostؔ • WasmͷϓϩάϥϜΛ࣮ߦ͢Δϗε τଆͰ࣮(ఏڙ)͞ΕΔؔ • Export Functions
• WasmͷϓϩάϥϜͰ࣮͞Εɺ ϗετଆͰར༻Մೳͳؔ WebAssembly 101: Import / Export functions -
WebAssembly in browsers V8 engine my.wasm Export function void changevalue()
Export function int64_t myvalue() mycode.js* *ٖࣅίʔυͰ͢ WasmίϯύΠϧ -
WebAssembly in browsers V8 engine my.wasm Import function void console_print(int64_t)
Import function int64_t get_timestamp() mycode.js* Export function void myfunc() WasmίϯύΠϧ *ٖࣅίʔυͰ͢ -
• Import/Export functionsͷ༷Λࡦఆ => ༷ʑͳϗετʹຒΊࠐΈՄೳ • POSIX APIͷΑ͏ͳImport functionsͷ༷Λ࡞ͬͨΒศརͦ͏ •
WasmͷίϯύΠϥ/ඪ४ϥΠϒϥϦͷPlatformλʔήοτ • WasmͷϓϩάϥϜ͕ී௨ͷlinux/amd64όΠφϦͷΑ͏ʹৼΔ͑Δ WebAssembly 101: Import / Export functions -
• WASIΛ࣮ͨ͠ϥϯλΠϜ͕ొ • WAVM, Wasmtime, Wasmer, Lucet, ʑ • EnvoyWASIͷҰ෦system
callΛαϙʔτ • “ී௨ͷϓϩηε”ͱಉ͡Α͏ʹWasm͕ಈ͘Α͏ʹͳΔ • KrustletͳͲͷ৺తϓϩδΣΫτͷొ Wasm/WASI Runtimes -
• ͓͞Β͍: Import/Export functionsͷ༷Α༷ͬͯʑͳHostڥΛ࡞ΕΔ • ༷ = ABI (Application Binary
Interface)ͱݺͿ • WASI = OS/SystemcallपΓͷABI • Compileͷඪ४తͳλʔήοτͱͳΔ: linux/amd64 <> wasi/wasm32 Wasm outside the web browsers WASI Linux / Darwin / Windows / … Wasm Virtual Machine -
• WASIʹݶΒ༷ͣʑͳABI͕ొ • Blockchain༻ͷABI, k8s֦ு༻ͷABI, Proxy-Wasm, etc. • ѥछABIͷRuntimeWASI࣮͍ͯ͠Δ͜ͱ͕ଟ͍ •
E.g. Envoy wasi/wasm32͚ͷWasmόΠφϦΛαϙʔτ Wasm outside the web browsers WASI Proxy-Wasm Envoy / Nginx / ATS / … Wasm Virtual Machine -
• Rust, Zig • ެࣜͷݴޠπʔϧνΣΠϯ͕wasm32-{unknown,wasi}Λαϙʔτ • C/C++ • Clang: wasm32-{unknown,wasi}λʔήοτΛαϙʔτ
• Emscripten: ࣮࣭Clangͷwrapper, ศརϨΠϠʔͷՃ Toolchains - Rust, C, C++, Zig -
• C++ exception͕͑ͳ͍: “-fno-exceptions” flag͕ඞਢ • Emscriptenಛ༗ͷABI͕ଘࡏ͢ΔͷͰαϙʔτՄͰ͋Δ • Hostଆͷtry-catchΛͬͯtranslate •
ͯ͢ͷtry-catchʹରͯ͠HostؔΛݺͿͷͰoverheadେ • ཧ༝ޙड़ Toolchains - C++ Limitations -
• 2ͭͷίϯύΠϥ͕͑Δ: ެࣜ, TinyGo • ެࣜ: GOOS=js GOARCH=wasmͷΈαϙʔτ: ❌WASI •
GoಠࣗͷJavaScript ABIΛϗετଆͰαϙʔτ͠ͳ͍ͱ͍͚ͳ͍ • GoͷݴޠϥϯλΠϜ͕JavaScriptͷ࣮ʹີ݁߹ (e.g. Goroutineͷ࣮) • TinyGo: wasm32-{unknown, wasi}ͲͪΒαϙʔτ • Runtime͕ඇৗʹ͍ܰͷͰόΠφϦαΠζ͕খ͍͞ Toolchains - Go -
• ݱঢ়JSڥલఏͷWasmλʔήοτ • WASIͳόΠφϦੜͰ͖ͳ͍ • Baremetalવແཧ = GoͷϥϯλΠϜͷ͍ͤ • WASIαϙʔτʹ͚ͨIssue:
#25612, #31105, #38248 • ॴײ: Ұੜαϙʔτ͞Εͳ͍ͷͰ(෦తʹ͋ͱ2ʙ4ͳΒߦ͚ͦ͏) • ෦త = WASIʹ2ͭͷABI͕ଘࡏ, ยํαϙʔτ͞ΕΔͷͰ? • ͏ҰํݪཧతʹGoroutine/Channel͕࣮ෆՄೳͳͷͰແཧͩͱ༧ Toolchains - Go: Official WASI support -
• ͑ͳ͍ύοέʔδ͕ଟʑ͋Δ: e.g. encoding/json • ݪҼ: Reflectionͷ࣮ͷҧ͍(=ݴޠͷ࣮͕ҧ͏ͷͰͨΓલ) • ݱࡏਐߦܥͰ͍Ζ͍Ζվળ͞Ε͍ͯΔ •
recover()͕ະ࣮: panic͔Β෮چͰ͖ͳ͍ • ࣮͞Εͨͱͯ͠, C++ྫ֎ͱಉ༷ͷཧ༝ͰWasmର֎(ޙड़) • WASIͷABIʹΑͬͯGoroutine/channel͕͑ͳ͍(ޙड़) • Goroutine/channel(ਖ਼֬ʹscheduler)Λdisableͯ͠compile͕Ͱ͖Δͷ͕ྑ͍ Toolchains - TinyGo limitations -
• WASIʹ2ͭͷ“Application ABI”͕ଘࡏ • WASI Command: ௨ৗͷexecutableͱಉ֓͡೦ • _start symbol͕Runtimeʹ࣮ߦ͞ΕΔ:
ݴޠ࣮తʹͦͷதͰuser definedͳmainؔΛݺͿ • exitͨ͠ΒͦͷVMഁغ • WASI Reactor: Event drivenͳruntimeͰΘΕΔ (e.g. Envoy proxy) • _initialize symbol͕Runtimeʹ࣮ߦ͞ΕVM෦ॳظԽ͢Δ(C++ͷctorsݺͼग़͠ͱ͔) • _initializeΛൈ͚ͨ͋ͱVMΛഁغ͠ͳ͍ • export͞ΕͨؔΛRuntime͕ୟ͘͜ͱͰԿVMʹcall in͢Δ WASI command vs WASI reactor https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md -
• TinyGoCommand/Reactorͱͯ͠ͷόΠφϦΛు͚Δ* • Reactorͱͯ͠ΘΕΔ߹: • schedulerͷ֓೦͕ͿͬඈͿͷͰchannel, goroutine͕͑ͳ͍ • ެࣜGoCommandͱͯ͠ͷWASIͰ͋Εαϙʔτ͢Δະདྷ͕དྷΔ͔? •
Reactorͷ߹scheduler͕ͳ͍, ݴޠ༷(?)Λຬͨͤͳ͍ • CommandͩͱҙͷؔΛexportͯ͠ϗετͱ͓Γͯ͠ɺͱ͍͏ͷ͕Ͱ͖ͳ͍ • Ϣʔεέʔε͕ݶΒΕ͍ͯΔ Toolchains - Go *ਖ਼֬ʹcommand͕ͩexit͠ͳ͍ͱ͍͏ಛघͳ_startؔͷ࣮ʹͳ͍ͬͯΔ(͕ͦ͏࣮ͯ͠͠·ͬͨͷͰͨ͠΄͏͕͍͍ΜͩΑͳ͋…) -
• https://github.com/WebAssembly/wasi-libc • A libc for WebAssembly based on WASI
system calls. • Rust, C, C++, Zig, TinyGowasi-libcͱ(fully-)static linkͯ͠όΠφϦΛੜ • ެࣜϏϧυdlmalloc͕allocatorͱ͍͖ͯͭͯͯ͠͠·͏ • ΧελϜallocatorͱซ༻ෆՄ: Heap͕Ԛછ͞ΕΔ • WASI SDK: https://github.com/WebAssembly/wasi-sdk • C/C++͚ͷϏϧυࡁΈWASI libc Toolchains - WASI libc -
• lldͷWasm͚ͷport • Rust, C/C++, TinyGo, Zig෦తʹwasm-ldΛͬͯ࠷ऴόΠφϦΛੜ • ΦϒδΣΫτϑΥʔϚοτWasmࣗମͷ༷ͷதʹͳ͍ •
https://github.com/WebAssembly/tool-conventions/blob/main/Linking.md Toolchains - wasm-ld -
• Wasmͷσόά͍͠ • ݱߦͷ༷ͰUserۭ͔ؒΒStack͕ݟ͑ͳ͍ • StacktraceΛऔಘͨ͠Γunwindingͨ͠Γ͕ݴޠϨϕϧͰ࣮ෆՄೳ • Proposal͕ਐߦத͕ͩΨοπϦॻ͖͞ΕͨΓͯ͠Δ: ·ͩ·͔͔ͩ࣌ؒΓͦ͏ •
͜Ε͕C++ྫ֎Rustͷpanic::*, Goͷrecover͕࣮ෆՄೳͳཧ༝ • DWARFͷWasm͚ͷ༷͕ެ։͞ΕͯΔ • DWARFͷ༷ͷΞυϨεͷҙຯΛม͑ͨͷͳͷͰ΄΅ಉҰ • ݱঢ়αϙʔτͰ͖ͯΔϗετڥ΄΅ͳ͍: Chrome͙Β͍? Debugging Wasm -
• Proxy-Wasm: Envoy/IstioൃͷϓϩδΣΫτ: https://github.com/proxy-wasm/spec • ϓϩΩγαʔόΛ֦ு͢ΔͨΊͷImport/Exportؔͷ༷ • ྫ: httpͷΠϕϯτຖʹWasm͔Βexport͞Εͨؔʹ͔͚͠Δ •
Proxy-WasmͷhostWASI࣮ => Compileλʔήοτͱͯ͠wasm32-wasi Proxy-Wasm: Wasm ABI for network proxies WASI Proxy-Wasm Envoy / Nginx / ATS / … Wasm Virtual Machine -
• ݱঢ়5ͭͷݴޠͷSDK͕ଘࡏ • C++, Rust by Google • Go (TinyGo)
by Tetrate • ͷझຯϓϩδΣΫτ͕ࣄʹͳΓ·ͨ͠ • AssemblyScript by Solo.io • Ziglang by Proxy-Wasm: Wasm ABI for network proxies -
Wasmͷ͜Ε͔Β • πʔϧνΣΠϯͷख़ • ެࣜGoͷαϙʔτ • JITͷߴԽ! • ֤छProposalͷਐల •
Threads, Exceptions, GC, External Ref, etc. -
Wasmͷ͜Ε͔Β • “ίϯςφ” ͱͯ͠ͷར༻ • Krustlet: https://github.com/krustlet/krustlet • WasmtimeΛར༻ͯ͠wasm32-wasiͳϓϩάϥϜΛK8sʹdeploy •
ϢʔβʔۭؒͰ࣮͞ΕͨΧʔωϧͱͯ͠ػೳ͢ΔͷͰ gVisorʹ͍ۙ -
·ͱΊ • Wasmྑ͍ͱ͜ͲΓͷԾϚγϯ+όΠφϦϑΥʔϚοτͷ༷ • Export/Import functionʹΑΓՄೳੑແݶେ • WASIʹΑΓ࣮ࡍͷOSͰಈ͘Α͏ͳϓϩάϥϜ͕ॻ͚Δ • Wasmࣗମ/πʔϧνΣΠϯ·ͩ·ͩൃల్্
• ͑Δݴޠػೳʹ੍ݶɿݱঢ়ؾʹ͢Δ͖ϙΠϯτ͕ͨ͘͞Μ • Proxy-WasmWasmΛͬͨNetwork proxyͷ֦ுػೳͷϓϩδΣΫτ • ϓϥάΠϯػߏͱͯ͠ͷWasmར༻͏ՄೳͳϑΣʔζ