ruby/wasm
Yuta Saito 0e59d91eed [wasm] Avoid malloc during longjmp
`longjmp` can be called to raise `NoMemoryError` or to trigger GC when
`malloc` fails to allocate memory in `ruby_xmalloc` family. In such
case, `malloc` call in `longjmp` will fail again, and Asyncify unwinding
operation corrupts the memory space by using the failed pointer as
Asyncify buffer. This commit uses statically allocated buffer to avoid
such situation.
2023-11-23 02:15:42 +09:00
..
tests [wasm] add unit test suite for fiber, register scan, sjlj in platform dir 2022-01-19 11:19:06 +09:00
GNUmakefile.in [wasm] add unit test suite for fiber, register scan, sjlj in platform dir 2022-01-19 11:19:06 +09:00
README.md wasm/README.md: Add a note about the Ruby built for wasm. [ci skip] 2022-11-11 07:57:25 +09:00
asyncify.h [wasm] vm.c: stop unwinding to main for every vm_exec call by setjmp 2022-02-18 18:28:18 +09:00
fiber.c
fiber.h
machine.c [wasm] Scan machine stack based on `ec->machine.stack_{start,end}` 2022-11-06 05:03:21 +09:00
machine.h [wasm] Scan machine stack based on `ec->machine.stack_{start,end}` 2022-11-06 05:03:21 +09:00
machine_core.S
missing.c
runtime.c [wasm] Fix Asyncify loop exit condition for normal return (#9007) 2023-11-22 16:59:54 +00:00
setjmp.c [wasm] Avoid malloc during longjmp 2023-11-23 02:15:42 +09:00
setjmp.h [wasm] Allocate asyncify buffer on heap to save stack usage 2023-05-16 03:52:59 +09:00
setjmp_core.S
wasm-opt

README.md

WebAssembly / WASI port of Ruby

How to cross-build

Requirement

  • Ruby (the same version as the building target version) (baseruby)
  • GNU make
  • WASI SDK 14.0 or later
  • Binaryen version 106 or later
  • Linux or macOS build machine

Steps

  1. Download a prebuilt WASI SDK package from WASI SDK release page.
  2. Set WASI_SDK_PATH environment variable to the root directory of the WASI SDK package.
$ export WASI_SDK_PATH=/path/to/wasi-sdk-X.Y
  1. Download a prebuilt binaryen from Binaryen release page
  2. Set PATH environment variable to lookup binaryen tools
$ export PATH=path/to/binaryen:$PATH
  1. Download the latest config.guess with WASI support, and run ./autogen.sh to generate configure when you are building from the source checked out from Git repository
$ ruby tool/downloader.rb -d tool -e gnu config.guess config.sub
$ ./autogen.sh
  1. Configure
  • You can select which extensions you want to build.
  • If you got Out of bounds memory access while running the produced ruby, you may need to increase the maximum size of stack.
$ ./configure LDFLAGS="-Xlinker -zstack-size=16777216" \
  --host wasm32-unknown-wasi \
  --with-destdir=./ruby-wasm32-wasi \
  --with-static-linked-ext \
  --with-ext=ripper,monitor
  1. Make
$ make install

Now you have a WASI compatible ruby binary. You can run it by any WebAssembly runtime like wasmtime, wasmer, Node.js, or browser with WASI polyfill.

Note: it may take a long time (~20 sec) for the first time for JIT compilation

$ wasmtime ruby-wasm32-wasi/usr/local/bin/ruby --mapdir /::./ruby-wasm32-wasi/ -- -e 'puts RUBY_PLATFORM'
wasm32-wasi

Note: you cannot run the built ruby without a WebAssembly runtime, because of the difference of the binary file type.

$ ruby-wasm32-wasi/usr/local/bin/ruby -e 'puts "a"'
bash: ruby-wasm32-wasi/usr/local/bin/ruby: cannot execute binary file: Exec format error

$ file ruby-wasm32-wasi/usr/local/bin/ruby
ruby-wasm32-wasi/usr/local/bin/ruby: WebAssembly (wasm) binary module version 0x1 (MVP)

Current Limitation

  • No Thread support for now.
  • Spawning a new process is not supported. e.g. Kernel.spawn and Kernel.system