LLVM on the Web Using Portable Native Client to run Clang/LLVM in - - PowerPoint PPT Presentation

llvm on the web
SMART_READER_LITE
LIVE PREVIEW

LLVM on the Web Using Portable Native Client to run Clang/LLVM in - - PowerPoint PPT Presentation

LLVM on the Web Using Portable Native Client to run Clang/LLVM in the Browser Brad Nelson February 1, 2015 @flagxor Motivation Author Ron Reuter (aka MainByte) I created the photo/file and it is released to the public domain. Motivation


slide-1
SLIDE 1

LLVM on the Web

Using Portable Native Client to run Clang/LLVM in the Browser

Brad Nelson @flagxor February 1, 2015

slide-2
SLIDE 2

Motivation

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

slide-3
SLIDE 3
  • I hate configuring new computers
  • I don’t trust all the programs I run
  • I’m a programmer

Motivation

slide-4
SLIDE 4

Goals

  • Develop in the Browser
  • Make it SECURE
  • Make it FAST
slide-5
SLIDE 5

What I Need

Vim Git Bash Python GCC LLVM

slide-6
SLIDE 6

What I Need Native Code

slide-7
SLIDE 7

Native Code on the Web?

slide-8
SLIDE 8

Native Client

  • Native code as secure as JavaScript
  • Verifiably safe
  • Portable
slide-9
SLIDE 9

Jargon

Native Client → NaCl Portable Native Client → PNaCl Pepper Plugin API → PPAPI

slide-10
SLIDE 10

Native Client

foo.h foo.c NaCl Toolchain foo.nexe foo.html User

slide-11
SLIDE 11

Two Sandboxes!

  • Outer Process Sandbox
  • Inner Static Verification Sandbox
slide-12
SLIDE 12

Process Sandbox

OS Renderer Process Renderer Process Renderer Process NaCl Process

slide-13
SLIDE 13

Software Fault Isolation

  • Restrict what code can execute

(Control Flow Integrity → CFI)

  • Restrict what data can be accessed

(Data Integrity)

  • Restricted I/O interface
slide-14
SLIDE 14

NaCl Address Space

Code Data

256MB 1GB / 4GB

slide-15
SLIDE 15

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

slide-16
SLIDE 16

Bundle Alignment

1 instruction 32 64 96 Conventional NOPs NOPs 32 64 96 NaCl

slide-17
SLIDE 17

Code Flow / Data Integrity

  • Architecture specific restrictions on valid

regions (segments)

  • Mask jumps / memory access
slide-18
SLIDE 18

Syscalls

  • Ban all the dangerous instructions
  • Do syscalls through exit trampolines
slide-19
SLIDE 19

PPAPI

  • Asynchronous API for:

○ Graphics ○ Sound ○ Network Access ○ Clipboard ○ Browser local files

slide-20
SLIDE 20

PPAPI

Native Client Process

air_mech_x86_64.nexe

NaCl runtime

PPAPI

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

JavaScript

C++

slide-21
SLIDE 21

Chrome Web Store

AirMech From Dust

slide-22
SLIDE 22

Chrome Web Store

slide-23
SLIDE 23

nacl_io

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

○ Memory ○ DOM Storage ○ HTTP ○ FUSE

  • POSIX like sockets
  • Symbolic links (coming soon)
slide-24
SLIDE 24

nacl-spawn

  • POSIX like processes
  • PIDs
  • spawnve, vfork, exec, waitpid
  • Pipes (coming soon)
slide-25
SLIDE 25

nacl-spawn

naclprocess.js NaCl Module (process) NaCl Module (process) NaCl Module (process) 5 6 7

slide-26
SLIDE 26

hterm

slide-27
SLIDE 27

Portability

  • Most of what we want is C code
  • But we want low level performance
  • Move part of the compile to the client?
slide-28
SLIDE 28

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)

slide-29
SLIDE 29

The PNaCl Translator

"ssh.pexe"

no translation exists

Native Client Process llc.nexe

Send .nexe from cache

Native Client Process

ssh_x86_64.nexe Translation Cache

Stream . pexe from URL

ssh_x86_64.nexe Write . nexe Save ssh_x86_64.nexe

slide-30
SLIDE 30

PNaCl

  • Custom compiler driver
  • ABI simplification passes before and after
  • ptimization
  • Translator running sandboxed in browser
slide-31
SLIDE 31

Naclports

  • Free / Open Source ports to NaCl
  • Stored as build scripts + patches
  • Dependencies
  • Upstream source mirroring
  • Continuous build and test
slide-32
SLIDE 32

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

  • penscenegraph 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

slide-33
SLIDE 33

Continuous Build

slide-34
SLIDE 34

NaCl Dev Environment

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

GForth)

  • Compilers (GCC, LLVM)
slide-35
SLIDE 35

NaCl Dev Environment

slide-36
SLIDE 36

PNaCl Toolchain

  • LLVM
  • Clang
  • Binutils
  • Libraries
slide-37
SLIDE 37

Porting Challenges

  • Newlib LibC

○ glibc-compat

  • Static Linking only
  • Gold Linker plugin
  • Compiler driver in Bash + Python
  • Change main to nacl_main
slide-38
SLIDE 38

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
slide-39
SLIDE 39

Compiler Drivers

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

○ Bash ○ Python ○ Clang

slide-40
SLIDE 40

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
slide-41
SLIDE 41

Possible Improvements

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

headers

  • Ditch our Python driver scripts
slide-42
SLIDE 42

Future Directions

  • In browser development
  • Cross-compile to native
slide-43
SLIDE 43

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...
slide-44
SLIDE 44

Demo + Questions

http://gonacl.com/fire