cvisor 0.1.0-x86_64-linux-musl

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3e983fc78cc4f7cdb9d04456f9f2060fd9cbd6f2c1be9116cdd056ba0ce25bf6
4
+ data.tar.gz: 0f96222560cfcea664a21c59ce7222a27576fddfe118dd9659c09e9f256398a8
5
+ SHA512:
6
+ metadata.gz: ba864664965840e7212c1661617a254a5dc7b4b0d8c7177fa4fc1a186d675c94c5103a94024eb50d4d524678a26daadb87cae834f187c2c87a7d158697e2cba4
7
+ data.tar.gz: b136f6e692c27db9e8e6861f954eecfcd3f80672fde8ad208ba4a57d3d501aadf8016e7f23ba9fbef2751a557cd69b2ddc118288c5b76bfb6fa49d349e7009ea
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # cVisor — Ruby SDK
2
+
3
+ A `fiddle` FFI wrapper over the `libcvisor` C ABI. Linux-only.
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ require "cvisor"
9
+
10
+ sb = Cvisor::Sandbox.new
11
+ out = sb.run("echo hello")
12
+ puts out.stdout # "hello\n"
13
+ puts out.stderr # ""
14
+ ```
15
+
16
+ `Sandbox#run(cmd)` blocks until the sandboxed command exits and returns an
17
+ `Output` with `#stdout` / `#stderr` (String) and `#stdout_bytes` /
18
+ `#stderr_bytes`.
19
+
20
+ ## Interactive console
21
+
22
+ Launch an IRB session with a live sandbox preloaded:
23
+
24
+ ```bash
25
+ bin/console
26
+ ```
27
+
28
+ ```
29
+ cVisor interactive console
30
+ sb -> a Sandbox instance
31
+ sh("cmd") -> run a shell command in the sandbox, printing stdout/stderr
32
+ Cvisor::Sandbox.new -> create your own
33
+
34
+ irb(main):001> sh("echo hello; uname -n")
35
+ hello
36
+ cvisor
37
+ ```
38
+
39
+ ## Development
40
+
41
+ The SDK loads `libcvisor.so`. Build it from the repo root (`cargo xtask ffi`),
42
+ which drops a copy into `native/`, or point the SDK at one via the `CVISOR_LIB`
43
+ environment variable.
data/bin/console ADDED
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # Interactive cVisor console.
5
+ #
6
+ # Drops you into an IRB session with a live sandbox ready to use:
7
+ #
8
+ # sb -> a Cvisor::Sandbox instance
9
+ # sh("ls /") -> run a shell command in the sandbox, printing stdout/stderr
10
+ # Cvisor::Sandbox.new -> create your own
11
+ #
12
+ # Run with `bin/console` (or `bundle exec console`).
13
+
14
+ $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
15
+
16
+ require "cvisor"
17
+ require "irb"
18
+
19
+ sb = Cvisor::Sandbox.new
20
+
21
+ # Top-level `sh` helper that runs a command in `sb` and prints its output.
22
+ define_singleton_method(:sh) do |command|
23
+ out = sb.run(command)
24
+ $stdout.print(out.stdout)
25
+ $stderr.print(out.stderr)
26
+ out
27
+ end
28
+
29
+ puts <<~BANNER
30
+ cVisor interactive console
31
+ sb -> a Sandbox instance
32
+ sh("cmd") -> run a shell command in the sandbox, printing stdout/stderr
33
+ Cvisor::Sandbox.new -> create your own
34
+ BANNER
35
+
36
+ binding.irb
data/lib/cvisor.rb ADDED
@@ -0,0 +1,92 @@
1
+ # cVisor Ruby SDK — Fiddle FFI wrapper over the libcvisor C ABI. Linux-only.
2
+ #
3
+ # require "cvisor"
4
+ # out = Cvisor::Sandbox.new.run("echo hello")
5
+ # puts out.stdout # "hello\n"
6
+
7
+ require "fiddle"
8
+ require "fiddle/import"
9
+
10
+ module Cvisor
11
+ # Low-level binding to libcvisor via Fiddle.
12
+ module Native
13
+ extend Fiddle::Importer
14
+
15
+ def self.library_path
16
+ return ENV["CVISOR_LIB"] if ENV["CVISOR_LIB"]
17
+ arch = RbConfig::CONFIG["host_cpu"] =~ /(aarch64|arm64)/ ? "aarch64" : "x86_64"
18
+ here = File.expand_path("../native", __dir__)
19
+ [
20
+ File.join(here, "libcvisor-#{arch}.so"),
21
+ File.join(here, "libcvisor.so"),
22
+ ].find { |p| File.exist?(p) } || File.join(here, "libcvisor-#{arch}.so")
23
+ end
24
+
25
+ dlload library_path
26
+
27
+ extern "void* cvisor_sandbox_new(void)"
28
+ extern "void cvisor_sandbox_free(void*)"
29
+ extern "void cvisor_sandbox_set_log_level(void*, int)"
30
+ extern "void* cvisor_run(void*, const char*)"
31
+ extern "void cvisor_output_free(void*)"
32
+ extern "void* cvisor_output_stdout(void*, void*)"
33
+ extern "void* cvisor_output_stderr(void*, void*)"
34
+ extern "void cvisor_bytes_free(void*, size_t)"
35
+ end
36
+
37
+ # Captured output of one run.
38
+ class Output
39
+ attr_reader :stdout_bytes, :stderr_bytes
40
+
41
+ def initialize(stdout_bytes, stderr_bytes)
42
+ @stdout_bytes = stdout_bytes
43
+ @stderr_bytes = stderr_bytes
44
+ end
45
+
46
+ def stdout = @stdout_bytes.force_encoding("UTF-8")
47
+ def stderr = @stderr_bytes.force_encoding("UTF-8")
48
+ end
49
+
50
+ class Sandbox
51
+ def initialize
52
+ @ptr = Native.cvisor_sandbox_new
53
+ raise "failed to create sandbox" if @ptr.null?
54
+ end
55
+
56
+ def set_log_level(level)
57
+ Native.cvisor_sandbox_set_log_level(@ptr, level == "DEBUG" ? 1 : 0)
58
+ end
59
+
60
+ def run(command)
61
+ out = Native.cvisor_run(@ptr, command)
62
+ raise "sandbox run failed" if out.null?
63
+ begin
64
+ Output.new(read_output(out, :cvisor_output_stdout),
65
+ read_output(out, :cvisor_output_stderr))
66
+ ensure
67
+ Native.cvisor_output_free(out)
68
+ end
69
+ end
70
+
71
+ def close
72
+ return unless @ptr && !@ptr.null?
73
+ Native.cvisor_sandbox_free(@ptr)
74
+ @ptr = nil
75
+ end
76
+
77
+ private
78
+
79
+ def read_output(out, accessor)
80
+ len = Fiddle::Pointer.malloc(Fiddle::SIZEOF_SIZE_T, Fiddle::RUBY_FREE)
81
+ ptr = Native.public_send(accessor, out, len)
82
+ n = len[0, Fiddle::SIZEOF_SIZE_T].unpack1(Fiddle::SIZEOF_SIZE_T == 8 ? "Q" : "L")
83
+ return "".b if ptr.null? || n.zero?
84
+
85
+ begin
86
+ ptr[0, n].b
87
+ ensure
88
+ Native.cvisor_bytes_free(ptr, n)
89
+ end
90
+ end
91
+ end
92
+ end
Binary file
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cvisor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: x86_64-linux-musl
6
+ authors:
7
+ - butter.dev
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: A thin Fiddle FFI wrapper over the libcvisor C ABI. Linux-only.
13
+ executables:
14
+ - console
15
+ extensions: []
16
+ extra_rdoc_files: []
17
+ files:
18
+ - README.md
19
+ - bin/console
20
+ - lib/cvisor.rb
21
+ - native/libcvisor-x86_64.so
22
+ homepage: https://github.com/tsirysndr/cVisor
23
+ licenses:
24
+ - MIT
25
+ metadata:
26
+ homepage_uri: https://github.com/tsirysndr/cVisor
27
+ source_code_uri: https://github.com/tsirysndr/cVisor
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '3.0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubygems_version: 4.0.10
43
+ specification_version: 4
44
+ summary: In-process Linux sandbox — Ruby SDK (Fiddle FFI over libcvisor)
45
+ test_files: []