1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#![deny(
dead_code,
nonstandard_style,
unused_imports,
unused_mut,
unused_variables,
unused_unsafe,
unreachable_patterns
)]
extern crate byteorder;
extern crate structopt;
use structopt::StructOpt;
#[cfg(feature = "loader-kernel")]
use wasmer_singlepass_backend::SinglePassCompiler;
#[cfg(feature = "loader-kernel")]
use std::os::unix::net::{UnixListener, UnixStream};
#[derive(Debug, StructOpt)]
#[structopt(name = "kwasmd", about = "Kernel-mode WebAssembly service.")]
enum CLIOptions {
#[structopt(name = "listen")]
Listen(Listen),
}
#[derive(Debug, StructOpt)]
struct Listen {
#[structopt(long = "socket")]
socket: String,
}
#[cfg(feature = "loader-kernel")]
const CMD_RUN_CODE: u32 = 0x901;
#[cfg(feature = "loader-kernel")]
const CMD_READ_MEMORY: u32 = 0x902;
#[cfg(feature = "loader-kernel")]
const CMD_WRITE_MEMORY: u32 = 0x903;
#[cfg(feature = "loader-kernel")]
fn handle_client(mut stream: UnixStream) {
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::io::{Read, Write};
let binary_size = stream.read_u32::<LittleEndian>().unwrap();
if binary_size > 1048576 * 16 {
println!("binary too large");
return;
}
let mut wasm_binary: Vec<u8> = Vec::with_capacity(binary_size as usize);
unsafe { wasm_binary.set_len(binary_size as usize) };
stream.read_exact(&mut wasm_binary).unwrap();
use wasmer::webassembly;
use wasmer_runtime_core::{
backend::{CompilerConfig, MemoryBoundCheckMode},
loader::Instance,
};
let module = webassembly::compile_with_config_with(
&wasm_binary[..],
CompilerConfig {
symbol_map: None,
memory_bound_check_mode: MemoryBoundCheckMode::Disable,
enforce_stack_check: true,
track_state: false,
features: Default::default(),
},
&SinglePassCompiler::new(),
)
.unwrap();
let mut import_object = wasmer_runtime_core::import::ImportObject::new();
import_object.allow_missing_functions = true;
let instance = module.instantiate(&import_object).unwrap();
let mut ins = instance.load(::wasmer_kernel_loader::KernelLoader).unwrap();
loop {
let cmd = stream.read_u32::<LittleEndian>().unwrap();
match cmd {
CMD_RUN_CODE => {
let func_name_len = stream.read_u32::<LittleEndian>().unwrap();
if func_name_len > 32 {
println!("function name too long");
return;
}
let mut func_name: Vec<u8> = Vec::with_capacity(func_name_len as usize);
unsafe { func_name.set_len(func_name_len as usize) };
stream.read_exact(&mut func_name).unwrap();
let func_name = ::std::str::from_utf8(&func_name).unwrap();
let arg_count = stream.read_u32::<LittleEndian>().unwrap();
if arg_count > 0 {
println!("Too many arguments");
return;
}
use wasmer_runtime::Value;
let mut args: Vec<Value> = Vec::with_capacity(arg_count as usize);
for _ in 0..arg_count {
args.push(Value::I64(stream.read_u64::<LittleEndian>().unwrap() as _));
}
let index = instance.resolve_func(func_name).unwrap();
let ret = ins.call(index, &args);
match ret {
Ok(x) => {
stream.write_u32::<LittleEndian>(1).unwrap();
stream.write_u128::<LittleEndian>(x).unwrap();
}
Err(e) => {
println!("Execution error: {:?}", e);
stream.write_u32::<LittleEndian>(0).unwrap();
}
}
}
CMD_READ_MEMORY => {
let offset = stream.read_u32::<LittleEndian>().unwrap();
let len = stream.read_u32::<LittleEndian>().unwrap();
if len > 1048576 * 16 {
println!("memory size too large");
return;
}
let buf = ins.read_memory(offset, len).unwrap();
stream.write_all(&buf).unwrap();
}
CMD_WRITE_MEMORY => {
let offset = stream.read_u32::<LittleEndian>().unwrap();
let len = stream.read_u32::<LittleEndian>().unwrap();
if len > 1048576 * 16 {
println!("memory size too large");
return;
}
let mut buf: Vec<u8> = Vec::with_capacity(len as usize);
unsafe { buf.set_len(len as usize) };
stream.read_exact(&mut buf).unwrap();
ins.write_memory(offset, len, &buf).unwrap();
}
_ => {
println!("Unknown command");
return;
}
}
}
}
#[cfg(feature = "loader-kernel")]
fn run_listen(opts: Listen) {
let listener = UnixListener::bind(&opts.socket).unwrap();
use std::thread;
for stream in listener.incoming() {
match stream {
Ok(stream) => {
thread::spawn(|| {
match ::std::panic::catch_unwind(::std::panic::AssertUnwindSafe(|| {
handle_client(stream);
})) {
Ok(()) => {}
Err(_) => {}
}
});
}
Err(err) => {
panic!("{:?}", err);
}
}
}
}
#[cfg(feature = "loader-kernel")]
fn main() {
panic!("Kwasm not updated for 128-bit support, yet. Sorry!");
let options = CLIOptions::from_args();
match options {
CLIOptions::Listen(listen) => {
run_listen(listen);
}
}
}
#[cfg(not(feature = "loader-kernel"))]
fn main() {
panic!("Kwasm loader is not enabled during compilation.");
}