LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module...

44
LLVM on the Web Using Portable Native Client to run Clang/LLVM in the Browser Brad Nelson @flagxor February 1, 2015

Transcript of LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module...

Page 1: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

LLVM on the WebUsing Portable Native Client

to run Clang/LLVM in the Browser

Brad Nelson@flagxorFebruary 1, 2015

Page 2: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Motivation

Author Ron Reuter (aka MainByte) I created the photo/file and it is released to the public domain.

Page 3: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

● I hate configuring new computers● I don’t trust all the programs I run● I’m a programmer

Motivation

Page 4: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Goals

● Develop in the Browser● Make it SECURE● Make it FAST

Page 5: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

What I Need

Vim

Git

Bash

Python

GCC

LLVM

Page 6: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

What I Need

Native Code

Page 7: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Native Code on the Web?

Page 8: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Native Client

● Native code as secure as JavaScript● Verifiably safe● Portable

Page 9: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Jargon

Native Client → NaClPortable Native Client → PNaClPepper Plugin API → PPAPI

Page 10: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Native Client

foo.h

foo.c

NaClToolchain foo.nexe

foo.html

User

Page 11: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Two Sandboxes!

● Outer Process Sandbox● Inner Static Verification Sandbox

Page 12: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Process Sandbox

OS RendererProcess

RendererProcess

RendererProcess

NaClProcess

Page 13: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Software Fault Isolation

● Restrict what code can execute(Control Flow Integrity → CFI)

● Restrict what data can be accessed(Data Integrity)

● Restricted I/O interface

Page 14: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

NaCl Address Space

Code

Data

0

256MB

1GB / 4GB

Page 15: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Code Flow Integrity

● Be able to disassemble all code● Disassembly must be unambiguous● Know where all direct jumps can lead● Know all indirect jumps (including returns)

are safe

Page 16: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Bundle Alignment

1 instruction

0

32

64

96

Conventional

NOPs

NOPs

0

32

64

96

NaCl

Page 17: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Code Flow / Data Integrity

● Architecture specific restrictions on valid regions (segments)

● Mask jumps / memory access

Page 18: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Syscalls

● Ban all the dangerous instructions● Do syscalls through exit trampolines

Page 19: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

PPAPI

● Asynchronous API for:○ Graphics○ Sound○ Network Access○ Clipboard○ Browser local files

Page 20: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

PPAPI

Native Client Process

air_mech_x86_64.nexe

NaCl runtime

PPAPIair_mech.postMessage(...);instance_->PostMessage( pp::Var(...));

JavaScript C++

Page 21: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Chrome Web Store

AirMech

From Dust

Page 22: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Chrome Web Store

Page 23: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

nacl_io

● POSIX compatibility on top of PPAPI● Virtual filesystem mounts:

○ Memory○ DOM Storage○ HTTP○ FUSE

● POSIX like sockets● Symbolic links (coming soon)

Page 24: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

nacl-spawn

● POSIX like processes● PIDs● spawnve, vfork, exec, waitpid● Pipes (coming soon)

Page 25: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

nacl-spawn

naclprocess.js

NaCl Module(process)

NaCl Module(process)

NaCl Module(process)

5 6 7

Page 26: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

hterm

Page 27: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Portability

● Most of what we want is C code● But we want low level performance● Move part of the compile to the client?

Page 28: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Portable Native Client

● High level optimizations ahead of time(PNaCl Toolchain)

● Low level optimizations on the client(PNaCl Translator)

● Transmit portable type-reduced LLVM-IR (PEXE)

Page 29: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

The PNaCl Translator"ssh.pexe"

no translation exists

Native Client Process

llc.nexe

Send .nexefrom cache

Native Client Process

ssh_x86_64.nexe

TranslationCache

Stream .pexe

from URL

ssh_x86_64.nexe

Write .nexe

Save ssh_x86_64.nexe

Page 30: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

PNaCl

● Custom compiler driver● ABI simplification passes before and after

optimization● Translator running sandboxed in browser

Page 31: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Naclports

● Free / Open Source ports to NaCl● Stored as build scripts + patches● Dependencies● Upstream source mirroring● Continuous build and test

Page 32: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

200+ Ported Packages!agg agg-demo alut-demo apr apr-util bash bdftopcf bigreqsproto binutils blackbox blis bochs boost boringssl box2d bullet busybox bzip2 cairo cfitsio civetweb clapack compositeproto coreutils curl damageproto devenv devenv-latest devil dosbox dreadthread drod emacs emacs-x expat faac faad2 ffmpeg fftw fftw-float fixesproto flac font-adobe-100dpi font-adobe-75dpi font-adobe-utopia-100dpi font-adobe-utopia-75dpi font-adobe-utopia-type1 font-alias font-bh-100dpi font-bh-75dpi font-bh-lucidatypewriter-100dpi font-bh-lucidatypewriter-75dpi font-bh-ttf font-bh-type1 font-bitstream-100dpi font-bitstream-75dpi font-bitstream-type1 font-cronyx-cyrillic font-cursor-misc font-daewoo-misc font-dec-misc font-ibm-type1 font-isas-misc font-jis-misc font-micro-misc font-misc-cyrillic font-misc-ethiopic font-misc-meltho font-misc-misc font-mutt-misc font-schumacher-misc font-screen-cyrillic font-sony-misc font-sun-misc font-util font-winitzki-cyrillic font-xfree86-type1 fontconfig fontsproto freealut freeimage freetype fvwm gc gcc gdb gettext geturl gforth giflib git glib glibc-compat glproto gmock gmp grep gsl gtest hdf5 icu imagemagick inputproto ipython-ppapi jpeg6b jpeg8d jsoncpp kbproto lame lcms leveldb libarchive libarchive-dev libatomic-ops libav libffi libfontenc libgit2 libgit2-demo libhangul libice libiconv libmikmod libmng libmodplug libogg liboggz libpciaccess libpng libpng12 libsm libsodium libssh libssh2 libtar libtheora libtomcrypt libtommath libtool libunistring libuuid libvorbis libwebp libx11 libxau libxaw libxcb libxext libxfont libxi libxinerama libxkbfile libxml2 libxmu libxpm libxrender libxt libxtst libyuv lua-ppapi lua5.1 lua5.2 m4 make mesa mesa-demo metakit mingn mongoose mp4v2 mpc mpfr mpg123 nacl-spawn nano ncurses netcat nethack ninja openal-ogg-demo openal-soft opencv openjpeg openscenegraph openssh openssl pango pcre physfs pixman protobuf protobuf-c protobuf25 python python-host python-ppapi python-static python3 python3-ppapi python_modules quakespasm randrproto readline recordproto regal renderproto resourceproto ruby ruby-ppapi scrnsaverproto scummvm sdl sdl-image sdl-mixer sdl-net sdl-tests sdl-ttf sdl2 sdl2-gfx sdl2-image sdl2-tests sdl2-ttf sdlquake snes9x speex sqlite subversion tar tcl texlive thttpd tiff tinyxml tk toybox twm unzip videoproto vim x264 xaos xbitmaps xcb-proto xcb-util xcmiscproto xextproto xeyes xfonts xineramaproto xkbcomp xkeyboard-config xorg-server xproto xtrans yajl zeromq zlib

Page 33: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Continuous Build

Page 34: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

NaCl Dev Environment

● Mini-Unix● Bash● Editors (Vim, Nano, Emacs)● Interpreters (Python, Ruby, Lua, Tcl/Tk,

GForth)● Compilers (GCC, LLVM)

Page 35: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

NaCl Dev Environment

Page 36: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

PNaCl Toolchain

● LLVM● Clang● Binutils● Libraries

Page 37: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Porting Challenges

● Newlib LibC○ glibc-compat

● Static Linking only● Gold Linker plugin● Compiler driver in Bash + Python● Change main to nacl_main

Page 38: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Static Linking

● Linker plugin (as used with LTO)● We only need one plugin● Modify gold to reference a single statically

linked plugin● Link LLVM + gold

Page 39: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Compiler Drivers

● Bash + Python Compiler Driver● Use nacl-spawn to create subprocess in:

○ Bash○ Python○ Clang

Page 40: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Results

● 47 MB PEXE → 54MB NEXE (opt)● 15 secs to compile a small sample

○ Why so slow?○ DOM fs

● Can build fire example in NaCl Dev Env

Page 41: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Possible Improvements

● Speed up DOM file system● Replace DOM file system with ISO for

headers● Ditch our Python driver scripts

Page 42: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Future Directions

● In browser development● Cross-compile to native

Page 43: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Thanks to awesome folks● Sam Clegg (naclports + nacl_io)● Ben Smith (naclports + nacl_io)● Shinichiro Hamaji (gcc port + nacl-spawn)● Pete Williamson (emacs port)● Robert Ginda (hterm)● Sergii Pylypenko (xserver on sdl)● Austin Benson, Torin Rudeen, Channing Huang

(awesome interns)● Many more...

Page 44: LLVM on the Webllvm.org/devmtg/2015-02/slides/brad-pnacl.pdf · naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7. hterm. Portability Most of

Demo + Questionshttp://gonacl.com/fire