-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdynamic_link.rs
288 lines (268 loc) · 8.97 KB
/
dynamic_link.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
use std::collections::HashMap;
use std::convert::TryInto;
use std::panic::{catch_unwind, AssertUnwindSafe};
use cosmwasm_std::{Addr, Binary};
use cosmwasm_vm::{
read_region_vals_from_env, set_callee_permission, write_value_to_env, Backend, Cache, Checksum,
InstanceOptions, WasmerVal,
};
use serde_json::{from_slice, to_vec};
use wasmer::{ExportType, FunctionType};
use crate::api::GoApi;
use crate::args::{CACHE_ARG, CHECKSUM_ARG, GAS_USED_ARG};
use crate::cache::{cache_t, to_cache};
use crate::db::Db;
use crate::error::{handle_c_error_default, Error};
use crate::memory::{ByteSliceView, UnmanagedVector};
use crate::querier::GoQuerier;
use crate::storage::GoStorage;
// A mibi (mega binary)
const MI: usize = 1024 * 1024;
// limit of sum of regions length dynamic link's input/output
// these are defined as enough big size
// input size is also limited by instantiate gas cost
const MAX_REGIONS_LENGTH_OUTPUT: usize = 64 * MI;
fn into_backend(db: Db, api: GoApi, querier: GoQuerier) -> Backend<GoApi, GoStorage, GoQuerier> {
Backend {
api,
storage: GoStorage::new(db),
querier,
}
}
// gas_used: used gas excepted instantiate cost of the callee instance
// callstack: serialized `Vec<Addr>`. It needs to contain the caller
// args: serialized `Vec<Vec<u8>>`.
//
// This function returns empty vec if the function returns nothing
#[no_mangle]
pub extern "C" fn call_callable_point(
name: ByteSliceView,
cache: *mut cache_t,
checksum: ByteSliceView,
is_readonly: bool,
callstack: ByteSliceView,
env: ByteSliceView,
args: ByteSliceView,
db: Db,
api: GoApi,
querier: GoQuerier,
gas_limit: u64,
print_debug: bool,
gas_used: Option<&mut u64>,
events: Option<&mut UnmanagedVector>,
attributes: Option<&mut UnmanagedVector>,
error_msg: Option<&mut UnmanagedVector>,
) -> UnmanagedVector {
let r = match to_cache(cache) {
Some(c) => catch_unwind(AssertUnwindSafe(move || {
do_call_callable_point(
name,
c,
checksum,
is_readonly,
callstack,
env,
args,
db,
api,
querier,
gas_limit,
print_debug,
events,
attributes,
gas_used,
)
}))
.unwrap_or_else(|_| Err(Error::panic())),
None => Err(Error::unset_arg(CACHE_ARG)),
};
let option_data = handle_c_error_default(r, error_msg);
let data = match to_vec(&option_data) {
Ok(v) => v,
// Unexpected
Err(_) => Vec::<u8>::new(),
};
UnmanagedVector::new(Some(data))
}
fn do_call_callable_point(
name: ByteSliceView,
cache: &mut Cache<GoApi, GoStorage, GoQuerier>,
checksum: ByteSliceView,
is_readonly: bool,
callstack: ByteSliceView,
env: ByteSliceView,
args: ByteSliceView,
db: Db,
api: GoApi,
querier: GoQuerier,
gas_limit: u64,
print_debug: bool,
events: Option<&mut UnmanagedVector>,
attributes: Option<&mut UnmanagedVector>,
gas_used: Option<&mut u64>,
) -> Result<Option<Binary>, Error> {
let name: String =
from_slice(name.read().ok_or_else(|| Error::unset_arg("name"))?).map_err(|e| {
Error::serde_err(format!(
"Error during deserializing callable point's `name` to call: {}",
e
))
})?;
let args: Vec<Binary> = from_slice(args.read().ok_or_else(|| Error::unset_arg("args"))?)
.map_err(|e| {
Error::serde_err(format!(
"Error during deserializing `args` for the callable point: {}",
e
))
})?;
let gas_used = gas_used.ok_or_else(|| Error::empty_arg(GAS_USED_ARG))?;
let checksum: Checksum = checksum
.read()
.ok_or_else(|| Error::unset_arg(CHECKSUM_ARG))?
.try_into()?;
let callstack: Vec<Addr> = from_slice(
callstack
.read()
.ok_or_else(|| Error::unset_arg("callstack"))?,
)
.map_err(|e| {
Error::serde_err(format!(
"Error during deserializing `callstack` of calling callable points: {}",
e
))
})?;
let backend = into_backend(db, api, querier);
let options = InstanceOptions {
gas_limit,
print_debug,
};
let env_u8 = env.read().ok_or_else(|| Error::unset_arg("env"))?;
// make instance
let mut instance = cache.get_instance(&checksum, backend, options)?;
instance.env.set_serialized_env(env_u8);
instance.env.set_dynamic_callstack(callstack)?;
// set permission
match set_callee_permission(&mut instance, &name, is_readonly) {
Ok(v) => v,
Err(e) => {
*gas_used = instance.create_gas_report().used_internally;
return Err(e.into());
}
};
// prepare inputs
let mut arg_ptrs = Vec::<WasmerVal>::with_capacity(args.len() + 1);
let env_ptr = write_value_to_env(&instance.env, env_u8)?;
arg_ptrs.push(env_ptr);
for arg in &args {
let ptr = write_value_to_env(&instance.env, arg.as_slice())?;
arg_ptrs.push(ptr);
}
let call_result = match instance.call_function(&name, &arg_ptrs) {
Ok(results) => {
let result_datas = read_region_vals_from_env(
&instance.env,
&results,
MAX_REGIONS_LENGTH_OUTPUT,
true,
)?;
match result_datas.len() {
0 => Ok(None),
1 => Ok(Some(Binary(result_datas[0].clone()))),
_ => Err(Error::dynamic_link_err(
"unexpected more than 1 returning values",
)),
}
}
Err(e) => Err(Error::dynamic_link_err(format!(
r#"Error during calling callable point "{}": {}"#,
name, e
))),
}?;
// events
if !is_readonly {
let e = events.ok_or_else(|| Error::empty_arg("events"))?;
let a = attributes.ok_or_else(|| Error::empty_arg("attributes"))?;
let (events, attributes) = instance.get_events_attributes();
let events_vec = match to_vec(&events) {
Ok(v) => v,
Err(e) => return Err(Error::invalid_events(e.to_string())),
};
let attributes_vec = match to_vec(&attributes) {
Ok(v) => v,
Err(e) => return Err(Error::invalid_attributes(e.to_string())),
};
*e = UnmanagedVector::new(Some(events_vec));
*a = UnmanagedVector::new(Some(attributes_vec));
};
// gas
*gas_used = instance.create_gas_report().used_internally;
Ok(call_result)
}
// returning value: serialized `Option<String>`
// `None` : true
// `Some(e)`: false and `e` is the reason
#[no_mangle]
pub extern "C" fn validate_interface(
cache: *mut cache_t,
checksum: ByteSliceView,
expected_interface: ByteSliceView,
error_msg: Option<&mut UnmanagedVector>,
) -> UnmanagedVector {
let r = match to_cache(cache) {
Some(c) => catch_unwind(AssertUnwindSafe(move || {
do_validate_interface(c, checksum, expected_interface)
}))
.unwrap_or_else(|_| Err(Error::panic())),
None => Err(Error::unset_arg(CACHE_ARG)),
};
let option_data = handle_c_error_default(r, error_msg);
let data = match to_vec(&option_data) {
Ok(v) => v,
// Unexpected
Err(_) => Vec::<u8>::new(),
};
UnmanagedVector::new(Some(data))
}
// returning `Option<Sting>`: `None` if true, `Some(e)` if false and `e` is the reason
fn do_validate_interface(
cache: &mut Cache<GoApi, GoStorage, GoQuerier>,
checksum: ByteSliceView,
expected_interface: ByteSliceView,
) -> Result<Option<String>, Error> {
let checksum: Checksum = checksum
.read()
.ok_or_else(|| Error::unset_arg(CHECKSUM_ARG))?
.try_into()?;
let expected_interface: Vec<ExportType<FunctionType>> = from_slice(
expected_interface
.read()
.ok_or_else(|| Error::unset_arg("expected_interface"))?,
)?;
let module = cache.get_module(&checksum)?;
let mut exported_fns: HashMap<String, FunctionType> = HashMap::new();
for f in module.exports().functions() {
exported_fns.insert(f.name().to_string(), f.ty().clone());
}
// No gas fee for comparison now
let mut err_msg = "The following functions are not implemented: ".to_string();
let mut is_err = false;
for expected_fn in expected_interface.iter() {
// if not expected
if !exported_fns
.get(expected_fn.name())
.map_or(false, |t| t == expected_fn.ty())
{
if is_err {
err_msg.push_str(", ");
};
err_msg.push_str(&format!("{}: {}", expected_fn.name(), expected_fn.ty()));
is_err = true;
}
}
if is_err {
Ok(Some(err_msg))
} else {
// as expected
Ok(None)
}
}