SlideShare a Scribd company logo
Running Untrusted Code in Spring
with WebAssembly
Dave Syer (2022)
@david_syer dsyer@vmware.com
What is WebAssembly?
● Host = application code (e.g. browser)
● Guest = WASM, compiled from C, C#, AssemblyScript, Rust, etc.
● Spec: https://github.com/WebAssembly/spec
● Originally targeted at browsers, so JavaScript is most common host
● Other hosts include Rust, Go, Python, C#, Java
● Sandbox - flexible with secure defaults
Host
Guest
Show me Some Code
(module
(func (export "add") (param i32) (param i32)
(result i32)
local.get 0
local.get 1
i32.add
)
)
Example playground:
https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/Numeric/Addition
Linear Memory
(module
(memory (export "memory") 1)
(func (export "get") (param i32)
(result i32)
(i32.load (local.get 0))
)
)
1 page of memory is exported and accessed externally via the “get” function
Imports and Exports
(module
(import "env" "get" (func $get (result i32)))
(func (export "echo") (result i32)
(call $get)
)
)
The result of the “get” function is echoed back to the caller of “echo”
Options:
● Emscripten: https://github.com/emscripten-core/emscripten
● Wasi SDK: https://github.com/WebAssembly/wasi-sdk
● Binaryen: https://github.com/WebAssembly/binaryen
● Llvm/Clang: https://github.com/llvm/llvm-project
Echo Guest: C
int get();
int echo() {
return get();
}
Try it out at https://wasdk.github.io/WasmFiddle/
Echo Guest: AssemblyScript
// @ts-ignore: decorator
@external("env", "get")
declare function get(): i32
export function echo() : i32 {
return get();
}
Echo Guest: Rust
extern "C" {
pub fn get() -> i32;
}
#[no_mangle]
pub extern "C" fn echo() -> i32 {
get()
}
Echo Guest: Java
Options:
● TeamVM: https://github.com/konsoletyper/teavm
● JSweet: https://github.com/cincheo/jsweet
● J2cl: https://github.com/google/j2cl
public class HelloWorld {
public static void main(String[] args) throws Exception {}
@Export(name = "echo")
public static int echo() { return get(); }
@Import(module = "env", name = "get")
public static native int get();
}
Echo Host: JavaScript
var wasmModule = new WebAssembly.Module(wasmCode);
var wasmInstance = new WebAssembly.Instance(
wasmModule,
{"env": {"get": () => 1234}}
);
log(wasmInstance.exports.echo());
byte array
Echo Host: Java
try (Store<Void> store = Store.withoutData();
Engine engine = store.engine();
Module module = new Module(engine, wasmCode);
Linker linker = new Linker(store.engine())) {
linker.define("env", "get", Extern.fromFunc(WasmFunctions.wrap(store, I32, () -> 1234)));
linker.module(store, "", module);
try (Func func = linker.get(store, "", "echo").get().func()) {
Function0<Integer> echo = WasmFunctions.func(store, func, I32);
int result = echo.call();
System.out.println(result);
}
}
Using https://github.com/kawamuray/wasmtime-java
Something Less Trivial?
● Strings
● POJOs
● JSON
Exchanging Data Between Host and Guest
Host
Guest
memory
Exchanging Data Between Host and Guest
● Choose a binary format, e.g. Protobuf, Avro, MessagePack, JSON string
● Convert input and copy into shared memory
● Call WASM function with [ptr, len] tuple
● Output is another [ptr, len] tuple
● Copy output from shared memory and convert
ptr (input)
len
ptr (output)
len
Application Binary Interface (ABI)
Contract for exchanging data:
● Allocate and free memory (host and guest have to agree on location)
● Binary encoding format, e.g. Protobuf definitions
● Signature for exports - structure of input and output pointers
● (Optional as necessary) signature of imports
Draft spec for standardization: https://github.com/WebAssembly/component-model
Message Exchange Host: JavaScript
var encoded = encode(msg);
const bytes = malloc(encoded.length);
new Uint8Array(memory.buffer).set(encoded, bytes);
const output = malloc(8);
const input = malloc(8);
new Uint32Array(memory.buffer, input, 2).set([bytes, encoded.length]);
wasm.instance.exports.call(output, input);
var buffer = new Uint32Array(memory.buffer, output, 2).slice();
var result = message.SpringMessage.deserializeBinary(new Uint8Array(memory.buffer, buffer[0],
buffer[1]));
free(output);
free(input);
return decode(result);
input object
memory management (imported from wasm)
Message Exchange Host: Java
var buffer = memory.buffer(store);
try (var input = new Wrapper(buffer, message);
var output = new Wrapper(buffer)) {
linker.get(store, "", "call").get().func().call(store, Val.fromI32(output.ptr()),
Val.fromI32(input.ptr()));
return output.get(SpringMessage.class);
}
input object
memory management
Spring Host Ideas
● Some glue code for boilerplate WASM host stuff
● Spring Cloud Gateway - predicates and filters
● Spring Cloud Function - generic data transformation
● Kubernetes operator - webhook or controller, e.g. Cartographer
Demos:
● https://github.com/dsyer/spring-wasm-demo (client: C; host: Spring)
● https://github.com/dsyer/async-wasm (client: C, AS, Rust; host: javascript)
Links
● https://github.com/dsyer/spring-wasm-demo
● https://developer.mozilla.org/en-US/docs/WebAssembly/Reference - MDN docs with
WAT/Javascript playground
● https://mbebenita.github.io/WasmExplorer/ - playground with C/C++/WAT/assembly
● https://github.com/WebAssembly/component-model
● https://cartographer.sh/

More Related Content

Similar to Running Untrusted Code in Spring with WebAssembly

Implementing new WebAPIs
Implementing new WebAPIsImplementing new WebAPIs
Implementing new WebAPIs
Julian Viereck
 
Book
BookBook
Book
luis_lmro
 
Nodejs
NodejsNodejs
Introduction to REST API with Node.js
Introduction to REST API with Node.jsIntroduction to REST API with Node.js
Introduction to REST API with Node.js
Yoann Gotthilf
 
Fantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptFantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptKamil Toman
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?
Doug Hawkins
 
Dragoncraft Architectural Overview
Dragoncraft Architectural OverviewDragoncraft Architectural Overview
Dragoncraft Architectural Overview
jessesanford
 
Nodejs Intro Part One
Nodejs Intro Part OneNodejs Intro Part One
Nodejs Intro Part One
Budh Ram Gurung
 
Pysec
PysecPysec
XPages Binary Output
XPages Binary OutputXPages Binary Output
XPages Binary Output
JohnFoldager
 
Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX Go
Rodolfo Carvalho
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing UpDavid Padbury
 
Parse cloud code
Parse cloud codeParse cloud code
Parse cloud code維佋 唐
 
GR8Conf 2009: Groovy Usage Patterns by Dierk König
GR8Conf 2009: Groovy Usage Patterns by Dierk KönigGR8Conf 2009: Groovy Usage Patterns by Dierk König
GR8Conf 2009: Groovy Usage Patterns by Dierk König
GR8Conf
 
soft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.js
soft-shake.ch
 
Introduction to Apache Beam
Introduction to Apache BeamIntroduction to Apache Beam
Introduction to Apache Beam
Jean-Baptiste Onofré
 
How Secure Are Docker Containers?
How Secure Are Docker Containers?How Secure Are Docker Containers?
How Secure Are Docker Containers?
Ben Hall
 
ECMAScript 6 and the Node Driver
ECMAScript 6 and the Node DriverECMAScript 6 and the Node Driver
ECMAScript 6 and the Node Driver
MongoDB
 
Iron Languages - NYC CodeCamp 2/19/2011
Iron Languages - NYC CodeCamp 2/19/2011Iron Languages - NYC CodeCamp 2/19/2011
Iron Languages - NYC CodeCamp 2/19/2011
Jimmy Schementi
 

Similar to Running Untrusted Code in Spring with WebAssembly (20)

Implementing New Web
Implementing New WebImplementing New Web
Implementing New Web
 
Implementing new WebAPIs
Implementing new WebAPIsImplementing new WebAPIs
Implementing new WebAPIs
 
Book
BookBook
Book
 
Nodejs
NodejsNodejs
Nodejs
 
Introduction to REST API with Node.js
Introduction to REST API with Node.jsIntroduction to REST API with Node.js
Introduction to REST API with Node.js
 
Fantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptFantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and Javascript
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?
 
Dragoncraft Architectural Overview
Dragoncraft Architectural OverviewDragoncraft Architectural Overview
Dragoncraft Architectural Overview
 
Nodejs Intro Part One
Nodejs Intro Part OneNodejs Intro Part One
Nodejs Intro Part One
 
Pysec
PysecPysec
Pysec
 
XPages Binary Output
XPages Binary OutputXPages Binary Output
XPages Binary Output
 
Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX Go
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
 
Parse cloud code
Parse cloud codeParse cloud code
Parse cloud code
 
GR8Conf 2009: Groovy Usage Patterns by Dierk König
GR8Conf 2009: Groovy Usage Patterns by Dierk KönigGR8Conf 2009: Groovy Usage Patterns by Dierk König
GR8Conf 2009: Groovy Usage Patterns by Dierk König
 
soft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.js
 
Introduction to Apache Beam
Introduction to Apache BeamIntroduction to Apache Beam
Introduction to Apache Beam
 
How Secure Are Docker Containers?
How Secure Are Docker Containers?How Secure Are Docker Containers?
How Secure Are Docker Containers?
 
ECMAScript 6 and the Node Driver
ECMAScript 6 and the Node DriverECMAScript 6 and the Node Driver
ECMAScript 6 and the Node Driver
 
Iron Languages - NYC CodeCamp 2/19/2011
Iron Languages - NYC CodeCamp 2/19/2011Iron Languages - NYC CodeCamp 2/19/2011
Iron Languages - NYC CodeCamp 2/19/2011
 

More from VMware Tanzu

Spring into AI presented by Dan Vega 5/14
Spring into AI presented by Dan Vega 5/14Spring into AI presented by Dan Vega 5/14
Spring into AI presented by Dan Vega 5/14
VMware Tanzu
 
What AI Means For Your Product Strategy And What To Do About It
What AI Means For Your Product Strategy And What To Do About ItWhat AI Means For Your Product Strategy And What To Do About It
What AI Means For Your Product Strategy And What To Do About It
VMware Tanzu
 
Make the Right Thing the Obvious Thing at Cardinal Health 2023
Make the Right Thing the Obvious Thing at Cardinal Health 2023Make the Right Thing the Obvious Thing at Cardinal Health 2023
Make the Right Thing the Obvious Thing at Cardinal Health 2023
VMware Tanzu
 
Enhancing DevEx and Simplifying Operations at Scale
Enhancing DevEx and Simplifying Operations at ScaleEnhancing DevEx and Simplifying Operations at Scale
Enhancing DevEx and Simplifying Operations at Scale
VMware Tanzu
 
Spring Update | July 2023
Spring Update | July 2023Spring Update | July 2023
Spring Update | July 2023
VMware Tanzu
 
Platforms, Platform Engineering, & Platform as a Product
Platforms, Platform Engineering, & Platform as a ProductPlatforms, Platform Engineering, & Platform as a Product
Platforms, Platform Engineering, & Platform as a Product
VMware Tanzu
 
Building Cloud Ready Apps
Building Cloud Ready AppsBuilding Cloud Ready Apps
Building Cloud Ready Apps
VMware Tanzu
 
Spring Boot 3 And Beyond
Spring Boot 3 And BeyondSpring Boot 3 And Beyond
Spring Boot 3 And Beyond
VMware Tanzu
 
Spring Cloud Gateway - SpringOne Tour 2023 Charles Schwab.pdf
Spring Cloud Gateway - SpringOne Tour 2023 Charles Schwab.pdfSpring Cloud Gateway - SpringOne Tour 2023 Charles Schwab.pdf
Spring Cloud Gateway - SpringOne Tour 2023 Charles Schwab.pdf
VMware Tanzu
 
Simplify and Scale Enterprise Apps in the Cloud | Boston 2023
Simplify and Scale Enterprise Apps in the Cloud | Boston 2023Simplify and Scale Enterprise Apps in the Cloud | Boston 2023
Simplify and Scale Enterprise Apps in the Cloud | Boston 2023
VMware Tanzu
 
Simplify and Scale Enterprise Apps in the Cloud | Seattle 2023
Simplify and Scale Enterprise Apps in the Cloud | Seattle 2023Simplify and Scale Enterprise Apps in the Cloud | Seattle 2023
Simplify and Scale Enterprise Apps in the Cloud | Seattle 2023
VMware Tanzu
 
tanzu_developer_connect.pptx
tanzu_developer_connect.pptxtanzu_developer_connect.pptx
tanzu_developer_connect.pptx
VMware Tanzu
 
Tanzu Virtual Developer Connect Workshop - French
Tanzu Virtual Developer Connect Workshop - FrenchTanzu Virtual Developer Connect Workshop - French
Tanzu Virtual Developer Connect Workshop - French
VMware Tanzu
 
Tanzu Developer Connect Workshop - English
Tanzu Developer Connect Workshop - EnglishTanzu Developer Connect Workshop - English
Tanzu Developer Connect Workshop - English
VMware Tanzu
 
Virtual Developer Connect Workshop - English
Virtual Developer Connect Workshop - EnglishVirtual Developer Connect Workshop - English
Virtual Developer Connect Workshop - English
VMware Tanzu
 
Tanzu Developer Connect - French
Tanzu Developer Connect - FrenchTanzu Developer Connect - French
Tanzu Developer Connect - French
VMware Tanzu
 
Simplify and Scale Enterprise Apps in the Cloud | Dallas 2023
Simplify and Scale Enterprise Apps in the Cloud | Dallas 2023Simplify and Scale Enterprise Apps in the Cloud | Dallas 2023
Simplify and Scale Enterprise Apps in the Cloud | Dallas 2023
VMware Tanzu
 
SpringOne Tour: Deliver 15-Factor Applications on Kubernetes with Spring Boot
SpringOne Tour: Deliver 15-Factor Applications on Kubernetes with Spring BootSpringOne Tour: Deliver 15-Factor Applications on Kubernetes with Spring Boot
SpringOne Tour: Deliver 15-Factor Applications on Kubernetes with Spring Boot
VMware Tanzu
 
SpringOne Tour: The Influential Software Engineer
SpringOne Tour: The Influential Software EngineerSpringOne Tour: The Influential Software Engineer
SpringOne Tour: The Influential Software Engineer
VMware Tanzu
 
SpringOne Tour: Domain-Driven Design: Theory vs Practice
SpringOne Tour: Domain-Driven Design: Theory vs PracticeSpringOne Tour: Domain-Driven Design: Theory vs Practice
SpringOne Tour: Domain-Driven Design: Theory vs Practice
VMware Tanzu
 

More from VMware Tanzu (20)

Spring into AI presented by Dan Vega 5/14
Spring into AI presented by Dan Vega 5/14Spring into AI presented by Dan Vega 5/14
Spring into AI presented by Dan Vega 5/14
 
What AI Means For Your Product Strategy And What To Do About It
What AI Means For Your Product Strategy And What To Do About ItWhat AI Means For Your Product Strategy And What To Do About It
What AI Means For Your Product Strategy And What To Do About It
 
Make the Right Thing the Obvious Thing at Cardinal Health 2023
Make the Right Thing the Obvious Thing at Cardinal Health 2023Make the Right Thing the Obvious Thing at Cardinal Health 2023
Make the Right Thing the Obvious Thing at Cardinal Health 2023
 
Enhancing DevEx and Simplifying Operations at Scale
Enhancing DevEx and Simplifying Operations at ScaleEnhancing DevEx and Simplifying Operations at Scale
Enhancing DevEx and Simplifying Operations at Scale
 
Spring Update | July 2023
Spring Update | July 2023Spring Update | July 2023
Spring Update | July 2023
 
Platforms, Platform Engineering, & Platform as a Product
Platforms, Platform Engineering, & Platform as a ProductPlatforms, Platform Engineering, & Platform as a Product
Platforms, Platform Engineering, & Platform as a Product
 
Building Cloud Ready Apps
Building Cloud Ready AppsBuilding Cloud Ready Apps
Building Cloud Ready Apps
 
Spring Boot 3 And Beyond
Spring Boot 3 And BeyondSpring Boot 3 And Beyond
Spring Boot 3 And Beyond
 
Spring Cloud Gateway - SpringOne Tour 2023 Charles Schwab.pdf
Spring Cloud Gateway - SpringOne Tour 2023 Charles Schwab.pdfSpring Cloud Gateway - SpringOne Tour 2023 Charles Schwab.pdf
Spring Cloud Gateway - SpringOne Tour 2023 Charles Schwab.pdf
 
Simplify and Scale Enterprise Apps in the Cloud | Boston 2023
Simplify and Scale Enterprise Apps in the Cloud | Boston 2023Simplify and Scale Enterprise Apps in the Cloud | Boston 2023
Simplify and Scale Enterprise Apps in the Cloud | Boston 2023
 
Simplify and Scale Enterprise Apps in the Cloud | Seattle 2023
Simplify and Scale Enterprise Apps in the Cloud | Seattle 2023Simplify and Scale Enterprise Apps in the Cloud | Seattle 2023
Simplify and Scale Enterprise Apps in the Cloud | Seattle 2023
 
tanzu_developer_connect.pptx
tanzu_developer_connect.pptxtanzu_developer_connect.pptx
tanzu_developer_connect.pptx
 
Tanzu Virtual Developer Connect Workshop - French
Tanzu Virtual Developer Connect Workshop - FrenchTanzu Virtual Developer Connect Workshop - French
Tanzu Virtual Developer Connect Workshop - French
 
Tanzu Developer Connect Workshop - English
Tanzu Developer Connect Workshop - EnglishTanzu Developer Connect Workshop - English
Tanzu Developer Connect Workshop - English
 
Virtual Developer Connect Workshop - English
Virtual Developer Connect Workshop - EnglishVirtual Developer Connect Workshop - English
Virtual Developer Connect Workshop - English
 
Tanzu Developer Connect - French
Tanzu Developer Connect - FrenchTanzu Developer Connect - French
Tanzu Developer Connect - French
 
Simplify and Scale Enterprise Apps in the Cloud | Dallas 2023
Simplify and Scale Enterprise Apps in the Cloud | Dallas 2023Simplify and Scale Enterprise Apps in the Cloud | Dallas 2023
Simplify and Scale Enterprise Apps in the Cloud | Dallas 2023
 
SpringOne Tour: Deliver 15-Factor Applications on Kubernetes with Spring Boot
SpringOne Tour: Deliver 15-Factor Applications on Kubernetes with Spring BootSpringOne Tour: Deliver 15-Factor Applications on Kubernetes with Spring Boot
SpringOne Tour: Deliver 15-Factor Applications on Kubernetes with Spring Boot
 
SpringOne Tour: The Influential Software Engineer
SpringOne Tour: The Influential Software EngineerSpringOne Tour: The Influential Software Engineer
SpringOne Tour: The Influential Software Engineer
 
SpringOne Tour: Domain-Driven Design: Theory vs Practice
SpringOne Tour: Domain-Driven Design: Theory vs PracticeSpringOne Tour: Domain-Driven Design: Theory vs Practice
SpringOne Tour: Domain-Driven Design: Theory vs Practice
 

Recently uploaded

AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
RISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent EnterpriseRISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent Enterprise
Srikant77
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 

Recently uploaded (20)

AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
RISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent EnterpriseRISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent Enterprise
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 

Running Untrusted Code in Spring with WebAssembly

  • 1. Running Untrusted Code in Spring with WebAssembly Dave Syer (2022) @david_syer dsyer@vmware.com
  • 2. What is WebAssembly? ● Host = application code (e.g. browser) ● Guest = WASM, compiled from C, C#, AssemblyScript, Rust, etc. ● Spec: https://github.com/WebAssembly/spec ● Originally targeted at browsers, so JavaScript is most common host ● Other hosts include Rust, Go, Python, C#, Java ● Sandbox - flexible with secure defaults Host Guest
  • 3. Show me Some Code (module (func (export "add") (param i32) (param i32) (result i32) local.get 0 local.get 1 i32.add ) ) Example playground: https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/Numeric/Addition
  • 4. Linear Memory (module (memory (export "memory") 1) (func (export "get") (param i32) (result i32) (i32.load (local.get 0)) ) ) 1 page of memory is exported and accessed externally via the “get” function
  • 5. Imports and Exports (module (import "env" "get" (func $get (result i32))) (func (export "echo") (result i32) (call $get) ) ) The result of the “get” function is echoed back to the caller of “echo”
  • 6. Options: ● Emscripten: https://github.com/emscripten-core/emscripten ● Wasi SDK: https://github.com/WebAssembly/wasi-sdk ● Binaryen: https://github.com/WebAssembly/binaryen ● Llvm/Clang: https://github.com/llvm/llvm-project Echo Guest: C int get(); int echo() { return get(); } Try it out at https://wasdk.github.io/WasmFiddle/
  • 7. Echo Guest: AssemblyScript // @ts-ignore: decorator @external("env", "get") declare function get(): i32 export function echo() : i32 { return get(); }
  • 8. Echo Guest: Rust extern "C" { pub fn get() -> i32; } #[no_mangle] pub extern "C" fn echo() -> i32 { get() }
  • 9. Echo Guest: Java Options: ● TeamVM: https://github.com/konsoletyper/teavm ● JSweet: https://github.com/cincheo/jsweet ● J2cl: https://github.com/google/j2cl public class HelloWorld { public static void main(String[] args) throws Exception {} @Export(name = "echo") public static int echo() { return get(); } @Import(module = "env", name = "get") public static native int get(); }
  • 10. Echo Host: JavaScript var wasmModule = new WebAssembly.Module(wasmCode); var wasmInstance = new WebAssembly.Instance( wasmModule, {"env": {"get": () => 1234}} ); log(wasmInstance.exports.echo()); byte array
  • 11. Echo Host: Java try (Store<Void> store = Store.withoutData(); Engine engine = store.engine(); Module module = new Module(engine, wasmCode); Linker linker = new Linker(store.engine())) { linker.define("env", "get", Extern.fromFunc(WasmFunctions.wrap(store, I32, () -> 1234))); linker.module(store, "", module); try (Func func = linker.get(store, "", "echo").get().func()) { Function0<Integer> echo = WasmFunctions.func(store, func, I32); int result = echo.call(); System.out.println(result); } } Using https://github.com/kawamuray/wasmtime-java
  • 12. Something Less Trivial? ● Strings ● POJOs ● JSON
  • 13. Exchanging Data Between Host and Guest Host Guest memory
  • 14. Exchanging Data Between Host and Guest ● Choose a binary format, e.g. Protobuf, Avro, MessagePack, JSON string ● Convert input and copy into shared memory ● Call WASM function with [ptr, len] tuple ● Output is another [ptr, len] tuple ● Copy output from shared memory and convert ptr (input) len ptr (output) len
  • 15. Application Binary Interface (ABI) Contract for exchanging data: ● Allocate and free memory (host and guest have to agree on location) ● Binary encoding format, e.g. Protobuf definitions ● Signature for exports - structure of input and output pointers ● (Optional as necessary) signature of imports Draft spec for standardization: https://github.com/WebAssembly/component-model
  • 16. Message Exchange Host: JavaScript var encoded = encode(msg); const bytes = malloc(encoded.length); new Uint8Array(memory.buffer).set(encoded, bytes); const output = malloc(8); const input = malloc(8); new Uint32Array(memory.buffer, input, 2).set([bytes, encoded.length]); wasm.instance.exports.call(output, input); var buffer = new Uint32Array(memory.buffer, output, 2).slice(); var result = message.SpringMessage.deserializeBinary(new Uint8Array(memory.buffer, buffer[0], buffer[1])); free(output); free(input); return decode(result); input object memory management (imported from wasm)
  • 17. Message Exchange Host: Java var buffer = memory.buffer(store); try (var input = new Wrapper(buffer, message); var output = new Wrapper(buffer)) { linker.get(store, "", "call").get().func().call(store, Val.fromI32(output.ptr()), Val.fromI32(input.ptr())); return output.get(SpringMessage.class); } input object memory management
  • 18. Spring Host Ideas ● Some glue code for boilerplate WASM host stuff ● Spring Cloud Gateway - predicates and filters ● Spring Cloud Function - generic data transformation ● Kubernetes operator - webhook or controller, e.g. Cartographer Demos: ● https://github.com/dsyer/spring-wasm-demo (client: C; host: Spring) ● https://github.com/dsyer/async-wasm (client: C, AS, Rust; host: javascript)
  • 19. Links ● https://github.com/dsyer/spring-wasm-demo ● https://developer.mozilla.org/en-US/docs/WebAssembly/Reference - MDN docs with WAT/Javascript playground ● https://mbebenita.github.io/WasmExplorer/ - playground with C/C++/WAT/assembly ● https://github.com/WebAssembly/component-model ● https://cartographer.sh/