./make

./make

Share this post

./make
./make
Get in loser. We're rewinding the stack.
Copy link
Facebook
Email
Notes
More

Get in loser. We're rewinding the stack.

Sandboxing Perl with WebAssembly - Part 2.

Andrew Sampson
Feb 11, 2025
8

Share this post

./make
./make
Get in loser. We're rewinding the stack.
Copy link
Facebook
Email
Notes
More
1
Share

In my last post, I expressed frustration at how the lack of exnref support in most WebAssembly runtimes made zeroperl effectively unusable. However, complaining alone doesn’t solve problems—if something is broken, fix it. Don’t accept the status quo or let it derail your goals.

Using libsetjmp from the WASI SDK for setjmp/longjmp breaks compatibility across WebAssembly runtimes, so I decided to implement it myself. Binaryen has an Asyncify feature, which provides more than enough functionality to build a setjmp implementation from scratch.1


First, we remove the official library


Next, we write some (web)assembly


Then, we run wasm-opt

wasm-opt zeroperl_unopt -O3 \  
--strip-dwarf \  
--enable-bulk-memory \  
--asyncify \  
-o zeroperl.wasm  

And the moment of truth…


zeroperl now runs in Wasmer, Wasmtime, and every other WebAssembly runtime I tested.

Enjoy your fully sandboxed and self-contained Perl. All in one tiny WebAssembly module. Source code here.

See part three for how I got asynchronous web APIs working.

1

Okay, not from scratch, I found a solution in the Ruby repo of all places (which itself just mirrors how Emscripten implements setjmp)

8

Share this post

./make
./make
Get in loser. We're rewinding the stack.
Copy link
Facebook
Email
Notes
More
1
Share

Discussion about this post

Will Braswell
Feb 11

Top notch work, let's meet up and discuss! :-)

Expand full comment
Reply
Share

No posts

Ready for more?

© 2025 Andrew Sampson
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great culture

Share

Copy link
Facebook
Email
Notes
More