wasval 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CODE_OF_CONDUCT.md +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +128 -0
- data/Rakefile +11 -0
- data/lib/wasval/config.rb +15 -0
- data/lib/wasval/executor.rb +113 -0
- data/lib/wasval/install/ruby_wasm.rb +107 -0
- data/lib/wasval/result.rb +34 -0
- data/lib/wasval/version.rb +5 -0
- data/lib/wasval.rb +32 -0
- metadata +78 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 0c40e62ec75efb8c895a87bdf907ffe4e339d1954e5e4ea7368e2652732e3156
|
|
4
|
+
data.tar.gz: 964d7ad7932d2fe91a8e27a87a188ac54a0de84eb008407da778bab3c13f6d3a
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: ab1cbfd5146a97b4c06852070840a20060b1101ee6d28a27f98f0c5fdfaa68979c198c03c88abefa0ec7fa9d6483defe8be58002f2a8d2cdc04820ebc900baf9
|
|
7
|
+
data.tar.gz: 9523a2fd9f67374216d3f3d5a5f4d1e8072e821a2563b777522f21ef20d07c8004b3ffeedf0aeec6dad7d19809f2a20140b8d0ec775808f583e8142211f17627
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Code of Conduct
|
|
2
|
+
|
|
3
|
+
"wasval" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
|
|
4
|
+
|
|
5
|
+
* Participants will be tolerant of opposing views.
|
|
6
|
+
* Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
|
|
7
|
+
* When interpreting the words and actions of others, participants should always assume good intentions.
|
|
8
|
+
* Behaviour which can be reasonably considered harassment will not be tolerated.
|
|
9
|
+
|
|
10
|
+
If you have any concerns about behaviour within this project, please contact us at ["yuuji.yaginuma@gmail.com"](mailto:"yuuji.yaginuma@gmail.com").
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Yuji Yaginuma
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# Wasval
|
|
2
|
+
|
|
3
|
+
Wasval is a Ruby sandbox that executes Ruby code safely inside a WebAssembly runtime (via [wasmtime](https://github.com/bytecodealliance/wasmtime-rb)).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bundle add wasval
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Or install directly:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
gem install wasval
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Setup
|
|
18
|
+
|
|
19
|
+
Wasval requires a `ruby.wasm` binary. Install it with the built-in installer:
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
require "wasval"
|
|
23
|
+
|
|
24
|
+
Wasval::Install::RubyWasm.new.install
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
`install` downloads the `ruby.wasm` binary and also generates a pre-compiled `ruby.cwasm` (serialized WebAssembly module) alongside it. Both files are saved to `~/.wasval/` by default (`ruby.wasm` and `ruby.cwasm`). The `.cwasm` file is a pre-compiled module that significantly reduces startup time on subsequent executions, though it is larger in file size than the `.wasm` binary.
|
|
28
|
+
|
|
29
|
+
If you only need the `.wasm` binary without pre-compilation, use `download` instead:
|
|
30
|
+
|
|
31
|
+
```ruby
|
|
32
|
+
Wasval::Install::RubyWasm.new.download
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
The binary is downloaded from the [ruby.wasm releases](https://github.com/ruby/ruby.wasm/releases).
|
|
36
|
+
|
|
37
|
+
By default, Ruby 3.4 is used. You can change the Ruby version via the `WASVAL_RUBY_VERSION` environment variable or the `ruby_version:` option:
|
|
38
|
+
|
|
39
|
+
```ruby
|
|
40
|
+
Wasval::Install::RubyWasm.new(ruby_version: "3.3").install
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
WASVAL_RUBY_VERSION=3.3 bundle exec ruby -e "require 'wasval'; Wasval::Install::RubyWasm.new.install"
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
You can customize the destination via the `WASVAL_RUBY_WASM_PATH` environment variable or the `dest:` option:
|
|
48
|
+
|
|
49
|
+
```ruby
|
|
50
|
+
Wasval::Install::RubyWasm.new(dest: "/path/to/ruby.wasm").download
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Usage
|
|
54
|
+
|
|
55
|
+
Set the `WASVAL_RUBY_WASM_PATH` environment variable to point to the `ruby.wasm` binary, then execute Ruby code:
|
|
56
|
+
|
|
57
|
+
```ruby
|
|
58
|
+
result = Wasval.execute("puts 'hello'")
|
|
59
|
+
|
|
60
|
+
result.status # => :success
|
|
61
|
+
result.output # => "hello\n"
|
|
62
|
+
result.success? # => true
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Configuration
|
|
66
|
+
|
|
67
|
+
```ruby
|
|
68
|
+
Wasval.configure do |config|
|
|
69
|
+
config.timeout = 10 # seconds (default: 5)
|
|
70
|
+
config.memory_limit = 256 # MB (default: 128)
|
|
71
|
+
end
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
You can also pass per-call overrides:
|
|
75
|
+
|
|
76
|
+
```ruby
|
|
77
|
+
result = Wasval.execute(code, timeout: 3, memory_limit: 32)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Result
|
|
81
|
+
|
|
82
|
+
`Wasval.execute` returns a `Wasval::Result` with the following attributes:
|
|
83
|
+
|
|
84
|
+
| Attribute | Description |
|
|
85
|
+
|---|---|
|
|
86
|
+
| `status` | `:success`, `:syntax_error`, `:runtime_error`, `:timeout`, `:memory_limit`, `:sandbox_error` |
|
|
87
|
+
| `output` | Captured stdout |
|
|
88
|
+
| `stderr` | Captured stderr |
|
|
89
|
+
| `error_message` | Human-readable error description (nil on success) |
|
|
90
|
+
|
|
91
|
+
Helper methods: `success?`, `timeout?`, `error_type`.
|
|
92
|
+
|
|
93
|
+
## Performance and Memory Considerations
|
|
94
|
+
|
|
95
|
+
Wasval supports two binary formats for the Ruby WebAssembly runtime:
|
|
96
|
+
|
|
97
|
+
- **`.wasm`** — The standard WebAssembly binary. It is portable and can be used across different WebAssembly runtimes, but requires compilation at load time, which results in higher memory usage and longer startup latency on first use.
|
|
98
|
+
- **`.cwasm`** — A serialized (pre-compiled) module specific to Wasmtime. It is the output of ahead-of-time compilation of the `.wasm` binary, so the runtime can load it directly without recompilation. This eliminates the compilation overhead, significantly reducing both memory usage and first execution latency. Note that `.cwasm` files are larger than their `.wasm` counterparts and are tied to the specific version of Wasmtime used to generate them.
|
|
99
|
+
|
|
100
|
+
When loading a `.wasm` binary at runtime, please be aware of the following:
|
|
101
|
+
|
|
102
|
+
- **Memory usage**: Loading ruby.wasm requires a significant amount of memory. Ensure your environment has sufficient memory available before using this gem.
|
|
103
|
+
- **First execution latency**: The first call to `Wasval.execute` incurs additional overhead due to loading and initializing the WebAssembly runtime. Subsequent calls will be faster.
|
|
104
|
+
|
|
105
|
+
Using `.cwasm` (generated by `Wasval::Install::RubyWasm.new.install`) is recommended for production use, as both concerns above do not apply.
|
|
106
|
+
|
|
107
|
+
## Development
|
|
108
|
+
|
|
109
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, install the ruby.wasm binary:
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
bundle exec ruby -e "require 'wasval'; Wasval::Install::RubyWasm.new.download"
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Set `WASVAL_RUBY_WASM_PATH` and run the tests:
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
export WASVAL_RUBY_WASM_PATH=~/.wasval/ruby.wasm
|
|
119
|
+
bundle exec rake test
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Contributing
|
|
123
|
+
|
|
124
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/y-yagi/wasval.
|
|
125
|
+
|
|
126
|
+
## License
|
|
127
|
+
|
|
128
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Wasval
|
|
4
|
+
class Config
|
|
5
|
+
DEFAULT_TIMEOUT = 5
|
|
6
|
+
DEFAULT_MEMORY_LIMIT = 128
|
|
7
|
+
|
|
8
|
+
attr_accessor :timeout, :memory_limit
|
|
9
|
+
|
|
10
|
+
def initialize
|
|
11
|
+
@timeout = DEFAULT_TIMEOUT
|
|
12
|
+
@memory_limit = DEFAULT_MEMORY_LIMIT
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "wasmtime"
|
|
4
|
+
|
|
5
|
+
module Wasval
|
|
6
|
+
class Executor
|
|
7
|
+
STDOUT_BUFFER_SIZE = 10 * 1024 * 1024 # 10 MB
|
|
8
|
+
STDERR_BUFFER_SIZE = 1 * 1024 * 1024 # 1 MB
|
|
9
|
+
WASM_PATH = ENV["WASVAL_RUBY_WASM_PATH"]
|
|
10
|
+
CWASM_PATH = ENV["WASVAL_RUBY_CWASM_PATH"]
|
|
11
|
+
|
|
12
|
+
def initialize
|
|
13
|
+
@engine = Wasmtime::Engine.new(epoch_interruption: true)
|
|
14
|
+
|
|
15
|
+
if WASM_PATH
|
|
16
|
+
@mod = Wasmtime::Module.from_file(@engine, WASM_PATH)
|
|
17
|
+
elsif CWASM_PATH
|
|
18
|
+
@mod = Wasmtime::Module.deserialize_file(@engine, CWASM_PATH)
|
|
19
|
+
else
|
|
20
|
+
raise ArgumentError.new "Please specify 'WASVAL_RUBY_WASM_PATH' or 'WASVAL_RUBY_CWASM_PATH' env"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def execute(code:, timeout:, memory_limit:)
|
|
25
|
+
if code.nil? || code.strip.empty?
|
|
26
|
+
return Result.new(
|
|
27
|
+
status: :sandbox_error,
|
|
28
|
+
output: "",
|
|
29
|
+
stderr: "",
|
|
30
|
+
error_message: "code must not be nil or empty"
|
|
31
|
+
)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
stdout_buf = +""
|
|
35
|
+
stderr_buf = +""
|
|
36
|
+
|
|
37
|
+
wasi_config = Wasmtime::WasiConfig.new
|
|
38
|
+
.set_argv(["ruby", "-"])
|
|
39
|
+
.set_stdin_string(wrapped_code(code))
|
|
40
|
+
.set_stdout_buffer(stdout_buf, STDOUT_BUFFER_SIZE)
|
|
41
|
+
.set_stderr_buffer(stderr_buf, STDERR_BUFFER_SIZE)
|
|
42
|
+
|
|
43
|
+
store = Wasmtime::Store.new(@engine,
|
|
44
|
+
wasi_p1_config: wasi_config,
|
|
45
|
+
limits: { memory_size: memory_limit * 1024 * 1024 }
|
|
46
|
+
)
|
|
47
|
+
store.set_epoch_deadline(timeout)
|
|
48
|
+
|
|
49
|
+
linker = Wasmtime::Linker.new(@engine)
|
|
50
|
+
Wasmtime::WASI::P1.add_to_linker_sync(linker)
|
|
51
|
+
|
|
52
|
+
@engine.start_epoch_interval(1000)
|
|
53
|
+
|
|
54
|
+
begin
|
|
55
|
+
linker.instantiate(store, @mod).invoke("_start")
|
|
56
|
+
classify_output(stdout_buf, stderr_buf, store)
|
|
57
|
+
rescue Wasmtime::WasiExit
|
|
58
|
+
classify_output(stdout_buf, stderr_buf, store)
|
|
59
|
+
rescue Wasmtime::Trap => e
|
|
60
|
+
if e.code == :interrupt
|
|
61
|
+
Result.new(status: :timeout, output: stdout_buf, stderr: "", error_message: "execution timed out")
|
|
62
|
+
elsif store.linear_memory_limit_hit?
|
|
63
|
+
Result.new(status: :memory_limit, output: stdout_buf, stderr: "", error_message: "memory limit exceeded")
|
|
64
|
+
else
|
|
65
|
+
Result.new(status: :sandbox_error, output: stdout_buf, stderr: stderr_buf, error_message: e.message)
|
|
66
|
+
end
|
|
67
|
+
rescue Wasmtime::Error => e
|
|
68
|
+
if store.linear_memory_limit_hit?
|
|
69
|
+
Result.new(status: :memory_limit, output: stdout_buf, stderr: "", error_message: "memory limit exceeded")
|
|
70
|
+
else
|
|
71
|
+
Result.new(status: :sandbox_error, output: stdout_buf, stderr: stderr_buf, error_message: e.message)
|
|
72
|
+
end
|
|
73
|
+
rescue => e
|
|
74
|
+
Result.new(status: :sandbox_error, output: stdout_buf, stderr: stderr_buf, error_message: e.message)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
private
|
|
79
|
+
|
|
80
|
+
def wrapped_code(code)
|
|
81
|
+
<<~RUBY
|
|
82
|
+
begin
|
|
83
|
+
eval(#{code.inspect}, binding, "(user_code)", 1)
|
|
84
|
+
rescue SyntaxError => e
|
|
85
|
+
$stderr.print "WASVAL:syntax_error:\#{e.message}"
|
|
86
|
+
exit 1
|
|
87
|
+
rescue => e
|
|
88
|
+
$stderr.print "WASVAL:runtime_error:\#{e.class}:\#{e.message}"
|
|
89
|
+
exit 1
|
|
90
|
+
end
|
|
91
|
+
RUBY
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def classify_output(stdout, stderr, store)
|
|
95
|
+
if store.linear_memory_limit_hit?
|
|
96
|
+
return Result.new(status: :memory_limit, output: stdout, stderr: "", error_message: "memory limit exceeded")
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
if (match = stderr.match(/WASVAL:syntax_error:(.+)/m))
|
|
100
|
+
clean_stderr = stderr.gsub(/^WASVAL:.*$/, "").strip
|
|
101
|
+
Result.new(status: :syntax_error, output: stdout, stderr: clean_stderr, error_message: match[1].strip)
|
|
102
|
+
elsif (match = stderr.match(/WASVAL:runtime_error:([^:]+):(.+)/m))
|
|
103
|
+
clean_stderr = stderr.gsub(/^WASVAL:.*$/, "").strip
|
|
104
|
+
error_msg = "#{match[1]}: #{match[2].strip}"
|
|
105
|
+
Result.new(status: :runtime_error, output: stdout, stderr: clean_stderr, error_message: error_msg)
|
|
106
|
+
elsif stderr.match?(/cannot load such file/)
|
|
107
|
+
Result.new(status: :runtime_error, output: stdout, stderr: stderr, error_message: stderr)
|
|
108
|
+
else
|
|
109
|
+
Result.new(status: :success, output: stdout, stderr: stderr, error_message: nil)
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "net/http"
|
|
4
|
+
require "uri"
|
|
5
|
+
require "zlib"
|
|
6
|
+
require "rubygems/package"
|
|
7
|
+
require "fileutils"
|
|
8
|
+
require "tmpdir"
|
|
9
|
+
require "wasmtime"
|
|
10
|
+
|
|
11
|
+
module Wasval
|
|
12
|
+
module Install
|
|
13
|
+
class RubyWasm
|
|
14
|
+
GITHUB_RELEASES_URL = "https://github.com/ruby/ruby.wasm/releases/latest/download"
|
|
15
|
+
TARGET = "wasm32-unknown-wasip1"
|
|
16
|
+
BINARY_PATH_IN_TAR = "usr/local/bin/ruby"
|
|
17
|
+
DEFAULT_INSTALL_DIR = File.expand_path("~/.wasval")
|
|
18
|
+
DEFAULT_BINARY_NAME = "ruby.wasm"
|
|
19
|
+
DEFAULT_SERIALIZED_BINARY_NAME = "ruby.cwasm"
|
|
20
|
+
|
|
21
|
+
attr_reader :dest, :serialized_dest, :ruby_version, :profile
|
|
22
|
+
|
|
23
|
+
def initialize(dest: nil, serialized_dest: nil, ruby_version: nil, profile: :full)
|
|
24
|
+
@ruby_version = ruby_version || ENV["WASVAL_RUBY_VERSION"] || default_ruby_version
|
|
25
|
+
@profile = profile.to_s
|
|
26
|
+
@dest = dest || ENV["WASVAL_RUBY_WASM_PATH"] || File.join(DEFAULT_INSTALL_DIR, DEFAULT_BINARY_NAME)
|
|
27
|
+
@serialized_dest = serialized_dest || ENV["WASVAL_RUBY_CWASM_PATH"] || File.join(File.dirname(@dest), DEFAULT_SERIALIZED_BINARY_NAME)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def download
|
|
31
|
+
FileUtils.mkdir_p(File.dirname(dest))
|
|
32
|
+
Dir.mktmpdir do |tmpdir|
|
|
33
|
+
tarball_path = File.join(tmpdir, tarball_name)
|
|
34
|
+
download_file(download_url, tarball_path)
|
|
35
|
+
extract_binary(tarball_path)
|
|
36
|
+
end
|
|
37
|
+
dest
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def install
|
|
41
|
+
download
|
|
42
|
+
serialize
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def serialize
|
|
46
|
+
engine = Wasmtime::Engine.new(epoch_interruption: true)
|
|
47
|
+
mod = Wasmtime::Module.from_file(engine, dest)
|
|
48
|
+
FileUtils.mkdir_p(File.dirname(serialized_dest))
|
|
49
|
+
File.binwrite(serialized_dest, mod.serialize)
|
|
50
|
+
serialized_dest
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def installed?
|
|
54
|
+
File.exist?(dest)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def tarball_name
|
|
58
|
+
"ruby-#{ruby_version}-#{TARGET}-#{profile}.tar.gz"
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def download_url
|
|
62
|
+
"#{GITHUB_RELEASES_URL}/#{tarball_name}"
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
|
|
67
|
+
def default_ruby_version
|
|
68
|
+
"3.4"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def download_file(url, dest_path, redirect_limit: 10)
|
|
72
|
+
raise "Too many redirects for #{url}" if redirect_limit == 0
|
|
73
|
+
|
|
74
|
+
uri = URI.parse(url)
|
|
75
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http|
|
|
76
|
+
http.request(Net::HTTP::Get.new(uri)) do |response|
|
|
77
|
+
case response
|
|
78
|
+
when Net::HTTPSuccess
|
|
79
|
+
File.open(dest_path, "wb") do |f|
|
|
80
|
+
response.read_body { |chunk| f.write(chunk) }
|
|
81
|
+
end
|
|
82
|
+
when Net::HTTPRedirection
|
|
83
|
+
download_file(response["location"], dest_path, redirect_limit: redirect_limit - 1)
|
|
84
|
+
else
|
|
85
|
+
raise "Failed to download #{url}: #{response.code} #{response.message}"
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def extract_binary(tarball_path)
|
|
92
|
+
Zlib::GzipReader.open(tarball_path) do |gz|
|
|
93
|
+
Gem::Package::TarReader.new(gz) do |tar|
|
|
94
|
+
tar.each do |entry|
|
|
95
|
+
next unless entry.file? && entry.full_name.end_with?(BINARY_PATH_IN_TAR)
|
|
96
|
+
|
|
97
|
+
File.open(dest, "wb") { |f| f.write(entry.read) }
|
|
98
|
+
File.chmod(0o755, dest)
|
|
99
|
+
return
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
raise "Ruby binary not found in tarball (expected path: #{BINARY_PATH_IN_TAR})"
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Wasval
|
|
4
|
+
class Result
|
|
5
|
+
VALID_STATUSES = %i[success syntax_error runtime_error timeout memory_limit sandbox_error].freeze
|
|
6
|
+
|
|
7
|
+
attr_reader :status, :output, :stderr, :error_message
|
|
8
|
+
|
|
9
|
+
def initialize(status:, output:, stderr:, error_message:)
|
|
10
|
+
raise ArgumentError, "invalid status: #{status}" unless VALID_STATUSES.include?(status)
|
|
11
|
+
|
|
12
|
+
@status = status
|
|
13
|
+
@output = output.to_s
|
|
14
|
+
@stderr = stderr.to_s
|
|
15
|
+
@error_message = error_message
|
|
16
|
+
|
|
17
|
+
freeze
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def success?
|
|
21
|
+
@status == :success
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def timeout?
|
|
25
|
+
@status == :timeout
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def error_type
|
|
29
|
+
return nil if @status == :success
|
|
30
|
+
|
|
31
|
+
@status
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
data/lib/wasval.rb
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "wasval/version"
|
|
4
|
+
require_relative "wasval/config"
|
|
5
|
+
require_relative "wasval/result"
|
|
6
|
+
require_relative "wasval/executor"
|
|
7
|
+
require_relative "wasval/install/ruby_wasm"
|
|
8
|
+
|
|
9
|
+
module Wasval
|
|
10
|
+
class Error < StandardError; end
|
|
11
|
+
|
|
12
|
+
class << self
|
|
13
|
+
def execute(code, timeout: nil, memory_limit: nil)
|
|
14
|
+
resolved_timeout = timeout || config.timeout
|
|
15
|
+
resolved_memory = memory_limit || config.memory_limit
|
|
16
|
+
|
|
17
|
+
executor.execute(code: code, timeout: resolved_timeout, memory_limit: resolved_memory)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def configure
|
|
21
|
+
yield config
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def config
|
|
25
|
+
@config ||= Config.new
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def executor
|
|
29
|
+
@executor ||= Executor.new
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: wasval
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Yuji Yaginuma
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: ruby_wasm
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: wasmtime
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
email:
|
|
41
|
+
- yuuji.yaginuma@gmail.com
|
|
42
|
+
executables: []
|
|
43
|
+
extensions: []
|
|
44
|
+
extra_rdoc_files: []
|
|
45
|
+
files:
|
|
46
|
+
- CODE_OF_CONDUCT.md
|
|
47
|
+
- LICENSE.txt
|
|
48
|
+
- README.md
|
|
49
|
+
- Rakefile
|
|
50
|
+
- lib/wasval.rb
|
|
51
|
+
- lib/wasval/config.rb
|
|
52
|
+
- lib/wasval/executor.rb
|
|
53
|
+
- lib/wasval/install/ruby_wasm.rb
|
|
54
|
+
- lib/wasval/result.rb
|
|
55
|
+
- lib/wasval/version.rb
|
|
56
|
+
homepage: https://github.com/y-yagi/wasval/
|
|
57
|
+
licenses:
|
|
58
|
+
- MIT
|
|
59
|
+
metadata:
|
|
60
|
+
homepage_uri: https://github.com/y-yagi/wasval/
|
|
61
|
+
rdoc_options: []
|
|
62
|
+
require_paths:
|
|
63
|
+
- lib
|
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: 3.3.0
|
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
|
+
requirements:
|
|
71
|
+
- - ">="
|
|
72
|
+
- !ruby/object:Gem::Version
|
|
73
|
+
version: '0'
|
|
74
|
+
requirements: []
|
|
75
|
+
rubygems_version: 4.0.3
|
|
76
|
+
specification_version: 4
|
|
77
|
+
summary: Execute 'eval' in a WASM runtime
|
|
78
|
+
test_files: []
|