The nuts and bolts of WebAssembly by Srikumar K. S. Director, - - PowerPoint PPT Presentation

the nuts and bolts of webassembly
SMART_READER_LITE
LIVE PREVIEW

The nuts and bolts of WebAssembly by Srikumar K. S. Director, - - PowerPoint PPT Presentation

The nuts and bolts of WebAssembly by Srikumar K. S. Director, Technology Pramati Technologies Pvt. Ltd. Mixed Nuts @ Pramati meetup 2019-08-24, Chennai History Web Tech Speed meant plugins ex: Flash , SilverLight SlowJS era '00 '01 '02 '03


slide-1
SLIDE 1

The nuts and bolts of WebAssembly

by Srikumar K. S. Director, Technology Pramati Technologies Pvt. Ltd. Mixed Nuts @ Pramati meetup 2019-08-24, Chennai

slide-2
SLIDE 2

History

slide-3
SLIDE 3

SlowJS era

Speed meant plugins ex: Flash, SilverLight

'00 '01 '02 '03 '04 '05 '06 '07 '08 '09 '10 '11 '12 '13 '14 '15 '16 '17 '18 '19

Web Tech Core Tech

First release

  • f LLVM
slide-4
SLIDE 4

Lex Parse Compile CodeGen Backend 1 CodeGen Backend 2 CodeGen Backend 3 Tokens AST

IR

Optimize Machine Code 1 Machine Code 2 Machine Code 3

Src

LLVM IR

slide-5
SLIDE 5

Truffle announced (towards GraalVM) GraalVM first production release

SlowJS era

(p)NaCl announced asm.js in Firefox 22 (p)NaCl
 axed Meetup @ Pramati Speed meant plugins ex: Flash, SilverLight Safari Nitro applies LLVM for 30% speedup

'00 '01 '02 '03 '04 '05 '06 '07 '08 '09 '10 '11 '12 '13 '14 '15 '16 '17 '18 '19

Web Tech Core Tech

First release

  • f LLVM

stable announced and JIT for Javascript WASM MVP LLVM announces Clang frontend WASM announced Rust-lang

slide-6
SLIDE 6

'17 '18 '19

WebAssembly MVP announced Meetup @ Pramati Firefox 52
 supports WASM WASI announced Cloudflare
 Workers
 support
 WASM wasmer.io launched Chrome 75
 Android supports WASM Safari 11 supports WASM Chrome 57 supports WASM Edge 16 supports WASM iOS Safari 11
 supports WASM Android Browser supports WASM 1.14 supports WASM (emscripten) 1.36 with wasm32-wasi supports wasm32-uk-uk wasi-sdk-1 (clang) Cloudflare
 Workers
 announced (V8 Isolates) announces Terrarium 


  • pen sources

Lucet Golang 1.11 support for WASM

slide-7
SLIDE 7

Walk through

slide-8
SLIDE 8

Setup

  • Install the Emscripten SDK - 


https://emscripten.org/docs/getting_started/downloads.html

  • Install the WASI SDK - 


https://github.com/CraneStation/wasi-sdk/releases


(installs to /opt/wasi-sdk)

  • Install webassembly npm module - 


npm install webassembly

  • Install wabt -


https://github.com/WebAssembly/wabt/releases

  • Install wasmer -


curl https://get.wasmer.io -sSfL | sh

slide-9
SLIDE 9

Baby steps - pythagorus.c

Did you npm install webassembly ?

slide-10
SLIDE 10

Baby steps - pythagorus.js

wa compile pythagorus.c -o pythagorus.wasm

slide-11
SLIDE 11

Baby steps - pythagorus.wasm

wa compile pythagorus.c -o pythagorus.wasm wasm-dis pythagorus.wasm // Like what you see? (haha!) wa compile -O pythagorus.c -o pythagorus.wasm wasm-dis pythagorus.wasm > pythagorus.wast

slide-12
SLIDE 12

Baby steps - pythagorus.wast

x y pythagorus(x,y) x² y² x²+y²

type decl

slide-13
SLIDE 13

S-expressions

(operator arg1 arg2 … argN) (operator arg1 (op2 y1 y2) … argN)

slide-14
SLIDE 14

S-expressions

(sqrt (add (mul x x) (mul y y)))

slide-15
SLIDE 15

S-expressions - evaluation

(op1 arg1 (op2 arg2 arg3)) arg1 arg2 arg3

  • p2
  • p1

Postorder tree traversal !

slide-16
SLIDE 16

S-expressions - evaluation

$0 local.get $0 local.get f64.mul $1 local.get $1 local.get f64.mul f64.add f64.sqrt

slide-17
SLIDE 17

wasm2wat pythagorus.wasm

slide-18
SLIDE 18

Something more useful

slide-19
SLIDE 19

WASM machine model

( )

Memory

Funcref Index 0x1135762 1 0x9498627 2 0x9282463 3

funcref table

WASM CODE

Name Fn crunch 2 mumble 7 dig 11

Exports / Imports

slide-20
SLIDE 20

Getting real with WASI

ucase.c

slide-21
SLIDE 21

Getting real with WASI

SYSROOT=/home/srikumar/wasi-sdk-6.0/opt/wasi-sdk/share/wasi-sysroot/

clang -O3 --sysroot $SYSROOT ucase.c -o ucase.wasm

Set <SYSROOT> to the sysroot folder of your wasi-sdk,

  • r omit --sysroot parameter if you installed

wasi-sdk at the default location - /opt

wasmer run ucase.wasm ucase.c /tmp/out.c

wasmer run --dir=. --mapdir=/tmp:output ucase.wasm ucase.c /tmp/out.c

Input denied!

slide-22
SLIDE 22

Where is all this heading?

slide-23
SLIDE 23

Signal processing in the browser

(shameless plug - patantara.com) Opus codec - https://github.com/srikumarks/webopus

slide-24
SLIDE 24

Core cloud principles

  • Fast deployment
  • Fast startup time
  • Fast runtime
  • Low memory overhead
  • Secure execution
slide-25
SLIDE 25

Capability based security

Google Fuchsia OS

sandstorm.io

You can ONLY access resources you’ve been handed!

slide-26
SLIDE 26

Capability based security

You can ONLY access resources you’ve been handed!

694-8673 239-4867 348-4687 239-4867 348-4687

Anitha Balu Chandra

694-8673

slide-27
SLIDE 27

CloudABI

https://cloudabi.org

  • Capability based runtime for UNIX programs
  • No command line arguments
  • No environment variables
  • Cannot take ANY action that affects global system
  • => WebAssembly and CloudABI are MFEO!
slide-28
SLIDE 28

CloudABI

https://cloudabi.org cloudabi_errno_t cloudabi_sys_file_open( cloudabi_lookup_t dirfd, const char *path, size_t path_len, cloudabi_oflags_t oflags, const cloudabi_fdstat_t *fds, cloudabi_fd_t *fd
 );

slide-29
SLIDE 29

CloudABI

https://cloudabi.org cloudabi_errno_t cloudabi_sys_sock_recv( cloudabi_fd_t sock, const cloudabi_recv_in_t *in, cloudabi_recv_out_t *out); cloudabi_errno_t cloudabi_sys_sock_send( cloudabi_fd_t sock, const cloudabi_send_in_t *in, cloudabi_send_out_t *out); cloudabi_errno_t cloudabi_sys_sock_shutdown( cloudabi_fd_t sock, cloudabi_sdflags_t how);

No sock_create() !!

slide-30
SLIDE 30

Running code at the edge

Workers Terrarium / Lucet

Javascript WebAssembly

slide-31
SLIDE 31

Running code in the kernel

https://github.com/wasmerio/kernel-wasm eBPF in WASM? Faster IO than user mode.

slide-32
SLIDE 32

Trusted computing

eWASM WASM for Ethereum https://github.com/project-oak/ Isolated secure enclaves with e2e encrypted communication.

slide-33
SLIDE 33

Wrapping up

slide-34
SLIDE 34

Possible efficiencies

  • Pre-compiled code is fast to start
  • Prepare initialized memory once and reuse it
  • Cache friendliness - use languages where you control memory
  • One-time optimizations worth the cost
slide-35
SLIDE 35

Possible inefficiencies

  • Data needs to be copied in and out.


(Improvements in the works, though)

  • No vectorization (yet). SIMD support is coming.
  • No threads (yet) … though we can use workers
  • Not 64-bit enabled (yet)
slide-36
SLIDE 36

Where to use

  • Fast startup/shutdown times
  • Large compute relative to data transfer
  • Strong security guarantees needed, without full VM overhead
  • Portability of existing desktop software and libraries for use within

browsers is important - ex: simulated development environments, KrakenD API gateway.

slide-37
SLIDE 37

Where not to use

  • If security is not a concern for running plug-n-play code, consider JS. Its

pretty darned good.

  • Relatively little compute to compensate for the extra data copy.


(There are ways to avoid the copy)

  • If binary portability doesn’t buy you much - ex: known server

environments and trusted code - consider native code.

slide-38
SLIDE 38

Other possibilities

  • WASM for deploying machine learning models?
  • WASM code as Kafka stream processors?
  • WASM for database UDFs?
  • WASM for FPGA (https://github.com/piranna/wasmachine)
  • WASM in network controllers?
  • WASM for web applications (duh!) (https://github.com/yewstack/yew)
  • WASM on bare metal, hypervisors?

Speculative

slide-39
SLIDE 39

Happy assembling!