SlideShare a Scribd company logo
1 of 19
Download to read offline
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 WebAPIsJulian Viereck
 
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.jsYoann 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 Overviewjessesanford
 
XPages Binary Output
XPages Binary OutputXPages Binary Output
XPages Binary OutputJohnFoldager
 
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 GoRodolfo 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önigGR8Conf
 
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.jssoft-shake.ch
 
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 DriverMongoDB
 
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/2011Jimmy 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

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 ItVMware 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 2023VMware 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 ScaleVMware Tanzu
 
Spring Update | July 2023
Spring Update | July 2023Spring Update | July 2023
Spring Update | July 2023VMware 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 ProductVMware Tanzu
 
Building Cloud Ready Apps
Building Cloud Ready AppsBuilding Cloud Ready Apps
Building Cloud Ready AppsVMware Tanzu
 
Spring Boot 3 And Beyond
Spring Boot 3 And BeyondSpring Boot 3 And Beyond
Spring Boot 3 And BeyondVMware 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.pdfVMware 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 2023VMware 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 2023VMware Tanzu
 
tanzu_developer_connect.pptx
tanzu_developer_connect.pptxtanzu_developer_connect.pptx
tanzu_developer_connect.pptxVMware Tanzu
 
Tanzu Virtual Developer Connect Workshop - French
Tanzu Virtual Developer Connect Workshop - FrenchTanzu Virtual Developer Connect Workshop - French
Tanzu Virtual Developer Connect Workshop - FrenchVMware Tanzu
 
Tanzu Developer Connect Workshop - English
Tanzu Developer Connect Workshop - EnglishTanzu Developer Connect Workshop - English
Tanzu Developer Connect Workshop - EnglishVMware Tanzu
 
Virtual Developer Connect Workshop - English
Virtual Developer Connect Workshop - EnglishVirtual Developer Connect Workshop - English
Virtual Developer Connect Workshop - EnglishVMware Tanzu
 
Tanzu Developer Connect - French
Tanzu Developer Connect - FrenchTanzu Developer Connect - French
Tanzu Developer Connect - FrenchVMware 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 2023VMware 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 BootVMware Tanzu
 
SpringOne Tour: The Influential Software Engineer
SpringOne Tour: The Influential Software EngineerSpringOne Tour: The Influential Software Engineer
SpringOne Tour: The Influential Software EngineerVMware 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 PracticeVMware Tanzu
 
SpringOne Tour: Spring Recipes: A Collection of Common-Sense Solutions
SpringOne Tour: Spring Recipes: A Collection of Common-Sense SolutionsSpringOne Tour: Spring Recipes: A Collection of Common-Sense Solutions
SpringOne Tour: Spring Recipes: A Collection of Common-Sense SolutionsVMware Tanzu
 

More from VMware Tanzu (20)

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
 
SpringOne Tour: Spring Recipes: A Collection of Common-Sense Solutions
SpringOne Tour: Spring Recipes: A Collection of Common-Sense SolutionsSpringOne Tour: Spring Recipes: A Collection of Common-Sense Solutions
SpringOne Tour: Spring Recipes: A Collection of Common-Sense Solutions
 

Recently uploaded

UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxAndreas Kunz
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...Bert Jan Schrijver
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingShane Coughlan
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4jGraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4jNeo4j
 
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdfAndrey Devyatkin
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?Alexandre Beguel
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slidesvaideheekore1
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLionel Briand
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...OnePlan Solutions
 
Patterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencePatterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencessuser9e7c64
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecturerahul_net
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesKrzysztofKkol1
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogueitservices996
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolsosttopstonverter
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfRTS corp
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...OnePlan Solutions
 

Recently uploaded (20)

UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4jGraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
 
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slides
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and Repair
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
 
Patterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencePatterns for automating API delivery. API conference
Patterns for automating API delivery. API conference
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecture
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogue
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration tools
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
 

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/