forked from OISF/suricata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime_util.rs
242 lines (227 loc) · 8.65 KB
/
runtime_util.rs
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* Copyright (C) 2020 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
// written by Pierre Chifflier <chifflier@wzdftpd.net>
use crate::wasm::CallEnv;
use std::cell::RefCell;
use std::mem;
use std::rc::Rc;
use wasmtime::{Caller, Extern, Func, Global, Memory, Store, Trap, WasmTy};
// wrap a function with 0 arguments to add call environment
pub(crate) fn wrap_env0<F, R>(store: &Store, call_env: Rc<RefCell<CallEnv>>, f: F) -> Func
where
F: Fn(Caller<'_>, &CallEnv) -> Result<R, Trap> + 'static,
R: WasmTy,
{
let f = move |caller: Caller<'_>| -> Result<R, Trap> {
let env = call_env
// safety: must not be called recursively
.borrow();
f(caller, &env)
};
Func::wrap(store, f).into()
}
// wrap a function with 1 arguments to add call environment
pub(crate) fn wrap_env1<F, A1, R>(store: &Store, call_env: Rc<RefCell<CallEnv>>, f: F) -> Func
where
F: Fn(Caller<'_>, &CallEnv, A1) -> Result<R, Trap> + 'static,
A1: WasmTy,
R: WasmTy,
{
let f = move |caller: Caller<'_>, a1: A1| -> Result<R, Trap> {
let env = call_env
// safety: must not be called recursively
.borrow();
f(caller, &env, a1)
};
Func::wrap(store, f).into()
}
// wrap a function with 2 arguments to add call environment
pub(crate) fn wrap_env2<F, A1, A2, R>(store: &Store, call_env: Rc<RefCell<CallEnv>>, f: F) -> Func
where
F: Fn(Caller<'_>, &CallEnv, A1, A2) -> Result<R, Trap> + 'static,
A1: WasmTy,
A2: WasmTy,
R: WasmTy,
{
let f = move |caller: Caller<'_>, a1: A1, a2: A2| -> Result<R, Trap> {
let env = call_env
// safety: must not be called recursively
.borrow();
f(caller, &env, a1, a2)
};
Func::wrap(store, f).into()
}
#[inline]
pub(crate) fn wasm_get_caller_memory(caller: &Caller) -> Result<Memory, Trap> {
match caller.get_export("memory") {
Some(Extern::Memory(mem)) => Ok(mem),
_ => return Err(Trap::new("failed to find host memory")),
}
}
#[inline]
pub(crate) fn wasm_get_caller_function(caller: &Caller, name: &str) -> Result<Func, Trap> {
match caller.get_export(name) {
Some(Extern::Func(f)) => Ok(f),
_ => return Err(Trap::new("failed to find guest function")),
}
}
pub(crate) fn get_global_str<'a>(mem: &'a Memory, global: &Global) -> Result<&'a str, Trap> {
let offset = global.get().i32().ok_or(Trap::new(
"Global has wrong type (expected pointer to string)",
))?;
let s = memory_read_serialized_slice(&mem, offset as u32)?;
let s = std::str::from_utf8(s).map_err(|_| Trap::new("Invalid UTF-8 encoding"))?;
Ok(s)
}
// helper function to get a string from memory area, knowing start and length
pub(crate) fn get_memory_str<'a>(mem: &'a Memory, ptr: u32, len: u32) -> Result<&'a str, Trap> {
let s = get_memory_slice(mem, ptr, len)?;
std::str::from_utf8(s).map_err(|_| Trap::new("Invalid UTF-8 encoding"))
}
// helper function to get a slice from memory area, knowing start and length
pub(crate) fn get_memory_slice<'a>(mem: &'a Memory, ptr: u32, len: u32) -> Result<&'a [u8], Trap> {
debug_assert!(mem.data_size() >= (ptr + len) as usize);
// We're reading raw wasm memory here so we need `unsafe`. Note
// though that this should be safe because we don't reenter wasm
// while we're reading wasm memory, nor should we clash with
// any other memory accessors (assuming they're well-behaved
// too).
unsafe {
let data = mem
.data_unchecked()
.get(ptr as usize..)
.and_then(|arr| arr.get(..len as usize));
match data {
Some(data) => Ok(data),
None => return Err(Trap::new("pointer/length out of bounds")),
}
}
}
// helper function to get memory area
pub(crate) fn get_memory_slice_mut<'a>(
mem: &'a Memory, ptr: u32, len: u32,
) -> Result<&'a mut [u8], Trap> {
debug_assert!(mem.data_size() >= (ptr + len) as usize);
// We're reading raw wasm memory here so we need `unsafe`. Note
// though that this should be safe because we don't reenter wasm
// while we're reading wasm memory, nor should we clash with
// any other memory accessors (assuming they're well-behaved
// too).
unsafe {
let data = mem
.data_unchecked_mut()
.get_mut(ptr as usize..)
.and_then(|arr| arr.get_mut(..len as usize));
match data {
Some(data) => Ok(data),
None => return Err(Trap::new("pointer/length out of bounds")),
}
}
}
// helper function to read a serialized slice from memory
// a slice is written as [ ptr, len ] (both fields as u32)
pub(crate) fn memory_read_serialized_slice<'a>(
mem: &'a Memory, ptr: u32,
) -> Result<&'a [u8], Trap> {
let s_ptr = get_memory_as::<u32>(&mem, ptr as u32)?;
let s_len = get_memory_as::<u32>(&mem, ptr as u32 + 4)?;
get_memory_slice(&mem, *s_ptr, *s_len)
}
// Allocate space in guest memory, copy data and return index in guest memory
pub(crate) fn copy_data_to_memory(
caller: &Caller, ptr: *const u8, len: usize,
) -> Result<u32, Trap> {
//
let mem = wasm_get_caller_memory(&caller)?;
let alloc = wasm_get_caller_function(&caller, "sc_allocate")?;
let alloc = alloc.get1::<u32, u32>()?;
//
let buffer = alloc(len as u32)?;
let dst = get_memory_slice_mut(&mem, buffer, len as u32)?;
// safety:, pointers and length are valid (from slices), no alignment requirement (pointer to u8)
unsafe {
std::ptr::copy_nonoverlapping(ptr, dst.as_mut_ptr() as *mut _, len);
}
// dst.copy_from_slice(src);
Ok(buffer)
}
// helper function to get memory area
pub(crate) fn get_memory_as<'a, T: Sized>(mem: &'a Memory, ptr: u32) -> Result<&'a T, Trap> {
let len = mem::size_of::<T>();
if mem.data_size() < ptr as usize + len {
println!("Request for data outside memory");
return Err(Trap::new("Request for data outside memory"));
}
// pointer must be aligned
if ptr as usize % len != 0 {
println!("Unaligned pointer cast 0x{:x}", ptr as usize);
return Err(Trap::new("Unaligned pointer cast"));
}
// We're reading raw wasm memory here so we need `unsafe`. Note
// though that this should be safe because we don't reenter wasm
// while we're reading wasm memory, nor should we clash with
// any other memory accessors (assuming they're well-behaved
// too).
unsafe {
let data = mem
.data_unchecked_mut()
.get(ptr as usize..)
.and_then(|arr| arr.get(..len));
match data {
Some(data) => {
// safety: size tested at function start
// ptr should be aligned, too XXX
let data = data.as_ptr() as *const T;
Ok(&*data)
}
None => return Err(Trap::new("pointer/length out of bounds")),
}
}
}
// helper function to get memory area
pub(crate) fn get_memory_as_mut<'a, T: Sized>(
mem: &'a Memory, ptr: u32,
) -> Result<&'a mut T, Trap> {
let len = mem::size_of::<T>();
if mem.data_size() < ptr as usize + len {
return Err(Trap::new("Request for data outside memory"));
}
// pointer must be aligned
if ptr as usize % len != 0 {
return Err(Trap::new("Unaligned pointer cast"));
}
// We're reading raw wasm memory here so we need `unsafe`. Note
// though that this should be safe because we don't reenter wasm
// while we're reading wasm memory, nor should we clash with
// any other memory accessors (assuming they're well-behaved
// too).
unsafe {
let data = mem
.data_unchecked_mut()
.get_mut(ptr as usize..)
.and_then(|arr| arr.get_mut(..len));
match data {
Some(data) => {
// safety: size tested at function start
// ptr should be aligned, too XXX
let data = data.as_ptr() as *mut T;
Ok(&mut *data)
}
None => return Err(Trap::new("pointer/length out of bounds")),
}
}
}