-
Notifications
You must be signed in to change notification settings - Fork 206
/
compatability.rs
479 lines (435 loc) Β· 17.4 KB
/
compatability.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
use parity_wasm::elements::{deserialize_buffer, External, ImportEntry, Module};
use std::collections::BTreeSet;
use std::collections::HashSet;
use std::iter::FromIterator;
use crate::errors::{VmError, VmResult};
use crate::features::required_features_from_module;
/// Lists all imports we provide upon instantiating the instance in Instance::from_module()
/// This should be updated when new imports are added
const SUPPORTED_IMPORTS: &[&str] = &[
"env.db_read",
"env.db_write",
"env.db_remove",
"env.canonicalize_address",
"env.humanize_address",
"env.query_chain",
#[cfg(feature = "iterator")]
"env.db_scan",
#[cfg(feature = "iterator")]
"env.db_next",
];
/// Lists all entry points we expect to be present when calling a contract.
/// Basically, anything that is used in calls.rs
/// This is unlikely to change much, must be frozen at 1.0 to avoid breaking existing contracts
const REQUIRED_EXPORTS: &[&str] = &[
"cosmwasm_vm_version_3",
"query",
"init",
"handle",
"allocate",
"deallocate",
];
const MEMORY_LIMIT: u32 = 512; // in pages
/// Checks if the data is valid wasm and compatibility with the CosmWasm API (imports and exports)
pub fn check_wasm(wasm_code: &[u8], supported_features: &HashSet<String>) -> VmResult<()> {
let module = match deserialize_buffer(&wasm_code) {
Ok(deserialized) => deserialized,
Err(err) => {
return Err(VmError::static_validation_err(format!(
"Wasm bytecode could not be deserialized. Deserialization error: \"{}\"",
err
)));
}
};
check_wasm_memories(&module)?;
check_wasm_exports(&module)?;
check_wasm_imports(&module)?;
check_wasm_features(&module, supported_features)?;
Ok(())
}
fn check_wasm_memories(module: &Module) -> VmResult<()> {
let section = match module.memory_section() {
Some(section) => section,
None => {
return Err(VmError::static_validation_err(
"Wasm contract doesn't have a memory section",
));
}
};
let memories = section.entries();
if memories.len() != 1 {
return Err(VmError::static_validation_err(
"Wasm contract must contain exactly one memory",
));
}
let memory = memories[0];
// println!("Memory: {:?}", memory);
let limits = memory.limits();
if limits.initial() > MEMORY_LIMIT {
return Err(VmError::static_validation_err(format!(
"Wasm contract memory's minimum must not exceed {} pages.",
MEMORY_LIMIT
)));
}
if limits.maximum() != None {
return Err(VmError::static_validation_err(
"Wasm contract memory's maximum must be unset. The host will set it for you.",
));
}
Ok(())
}
fn check_wasm_exports(module: &Module) -> VmResult<()> {
let available_exports: Vec<String> = module.export_section().map_or(vec![], |export_section| {
export_section
.entries()
.iter()
.map(|entry| entry.field().to_string())
.collect()
});
for required_export in REQUIRED_EXPORTS {
if !available_exports.iter().any(|x| x == required_export) {
return Err(VmError::static_validation_err(format!(
"Wasm contract doesn't have required export: \"{}\". Exports required by VM: {:?}. Contract version too old for this VM?",
required_export, REQUIRED_EXPORTS
)));
}
}
Ok(())
}
/// Checks if the import requirements of the contract are satisfied.
/// When this is not the case, we either have an incompatibility between contract and VM
/// or a error in the contract.
fn check_wasm_imports(module: &Module) -> VmResult<()> {
let required_imports: Vec<ImportEntry> = module
.import_section()
.map_or(vec![], |import_section| import_section.entries().to_vec());
for required_import in required_imports {
let full_name = format!("{}.{}", required_import.module(), required_import.field());
if !SUPPORTED_IMPORTS.contains(&full_name.as_str()) {
return Err(VmError::static_validation_err(format!(
"Wasm contract requires unsupported import: \"{}\". Imports supported by VM: {:?}. Contract version too new for this VM?",
full_name, SUPPORTED_IMPORTS
)));
}
match required_import.external() {
External::Function(_) => {}, // ok
_ => return Err(VmError::static_validation_err(format!(
"Wasm contract requires non-function import: \"{}\". Right now, all supported imports are functions.",
full_name
))),
};
}
Ok(())
}
fn check_wasm_features(module: &Module, supported_features: &HashSet<String>) -> VmResult<()> {
let required_features = required_features_from_module(module);
if !required_features.is_subset(supported_features) {
// We switch to BTreeSet to get a sorted error message
let unsupported = BTreeSet::from_iter(required_features.difference(&supported_features));
return Err(VmError::static_validation_err(format!(
"Wasm contract requires unsupported features: {:?}",
unsupported
)));
}
Ok(())
}
#[cfg(test)]
mod test {
use super::*;
use crate::errors::VmError;
use std::iter::FromIterator;
use wabt::wat2wasm;
static CONTRACT_0_6: &[u8] = include_bytes!("../testdata/contract_0.6.wasm");
static CONTRACT_0_7: &[u8] = include_bytes!("../testdata/contract_0.7.wasm");
static CONTRACT: &[u8] = include_bytes!("../testdata/contract.wasm");
static CORRUPTED: &[u8] = include_bytes!("../testdata/corrupted.wasm");
fn default_features() -> HashSet<String> {
HashSet::from_iter(["staking".to_string()].iter().cloned())
}
#[test]
fn test_check_wasm() {
// this is our reference check, must pass
check_wasm(CONTRACT, &default_features()).unwrap();
}
#[test]
fn test_check_wasm_old_contract() {
match check_wasm(CONTRACT_0_7, &default_features()) {
Err(VmError::StaticValidationErr { msg, .. }) => assert!(msg.starts_with(
"Wasm contract doesn't have required export: \"cosmwasm_vm_version_3\""
)),
Err(e) => panic!("Unexpected error {:?}", e),
Ok(_) => panic!("This must not succeeed"),
};
match check_wasm(CONTRACT_0_6, &default_features()) {
Err(VmError::StaticValidationErr { msg, .. }) => assert!(msg.starts_with(
"Wasm contract doesn't have required export: \"cosmwasm_vm_version_3\""
)),
Err(e) => panic!("Unexpected error {:?}", e),
Ok(_) => panic!("This must not succeeed"),
};
}
#[test]
fn test_check_wasm_corrupted_data() {
match check_wasm(CORRUPTED, &default_features()) {
Err(VmError::StaticValidationErr { msg, .. }) => {
assert!(msg.starts_with("Wasm bytecode could not be deserialized."))
}
Err(e) => panic!("Unexpected error {:?}", e),
Ok(_) => panic!("This must not succeeed"),
}
}
#[test]
fn test_check_wasm_memories_ok() {
let wasm = wat2wasm("(module (memory 1))").unwrap();
check_wasm_memories(&deserialize_buffer(&wasm).unwrap()).unwrap()
}
#[test]
fn test_check_wasm_memories_no_memory() {
let wasm = wat2wasm("(module)").unwrap();
match check_wasm_memories(&deserialize_buffer(&wasm).unwrap()) {
Err(VmError::StaticValidationErr { msg, .. }) => {
assert!(msg.starts_with("Wasm contract doesn't have a memory section"));
}
Err(e) => panic!("Unexpected error {:?}", e),
Ok(_) => panic!("Didn't reject wasm with invalid api"),
}
}
#[test]
fn test_check_wasm_memories_two_memories() {
// Generated manually because wat2wasm protects us from creating such Wasm:
// "error: only one memory block allowed"
let wasm = hex::decode(concat!(
"0061736d", // magic bytes
"01000000", // binary version (uint32)
"05", // section type (memory)
"05", // section length
"02", // number of memories
"0009", // element of type "resizable_limits", min=9, max=unset
"0009", // element of type "resizable_limits", min=9, max=unset
))
.unwrap();
match check_wasm_memories(&deserialize_buffer(&wasm).unwrap()) {
Err(VmError::StaticValidationErr { msg, .. }) => {
assert!(msg.starts_with("Wasm contract must contain exactly one memory"));
}
Err(e) => panic!("Unexpected error {:?}", e),
Ok(_) => panic!("Didn't reject wasm with invalid api"),
}
}
#[test]
fn test_check_wasm_memories_zero_memories() {
// Generated manually because wat2wasm would not create an empty memory section
let wasm = hex::decode(concat!(
"0061736d", // magic bytes
"01000000", // binary version (uint32)
"05", // section type (memory)
"01", // section length
"00", // number of memories
))
.unwrap();
match check_wasm_memories(&deserialize_buffer(&wasm).unwrap()) {
Err(VmError::StaticValidationErr { msg, .. }) => {
assert!(msg.starts_with("Wasm contract must contain exactly one memory"));
}
Err(e) => panic!("Unexpected error {:?}", e),
Ok(_) => panic!("Didn't reject wasm with invalid api"),
}
}
#[test]
fn test_check_wasm_memories_initial_size() {
let wasm_ok = wat2wasm("(module (memory 512))").unwrap();
check_wasm_memories(&deserialize_buffer(&wasm_ok).unwrap()).unwrap();
let wasm_too_big = wat2wasm("(module (memory 513))").unwrap();
match check_wasm_memories(&deserialize_buffer(&wasm_too_big).unwrap()) {
Err(VmError::StaticValidationErr { msg, .. }) => {
assert!(msg.starts_with("Wasm contract memory's minimum must not exceed 512 pages"));
}
Err(e) => panic!("Unexpected error {:?}", e),
Ok(_) => panic!("Didn't reject wasm with invalid api"),
}
}
#[test]
fn test_check_wasm_memories_maximum_size() {
let wasm_max = wat2wasm("(module (memory 1 5))").unwrap();
match check_wasm_memories(&deserialize_buffer(&wasm_max).unwrap()) {
Err(VmError::StaticValidationErr { msg, .. }) => {
assert!(msg.starts_with("Wasm contract memory's maximum must be unset"));
}
Err(e) => panic!("Unexpected error {:?}", e),
Ok(_) => panic!("Didn't reject wasm with invalid api"),
}
}
#[test]
fn test_check_wasm_exports() {
// this is invalid, as it doesn't contain all required exports
const WAT_MISSING_EXPORTS: &'static str = r#"
(module
(type $t0 (func (param i32) (result i32)))
(func $add_one (export "add_one") (type $t0) (param $p0 i32) (result i32)
get_local $p0
i32.const 1
i32.add))
"#;
let wasm_missing_exports = wat2wasm(WAT_MISSING_EXPORTS).unwrap();
let module = deserialize_buffer(&wasm_missing_exports).unwrap();
match check_wasm_exports(&module) {
Err(VmError::StaticValidationErr { msg, .. }) => {
assert!(msg.starts_with(
"Wasm contract doesn't have required export: \"cosmwasm_vm_version_3\""
));
}
Err(e) => panic!("Unexpected error {:?}", e),
Ok(_) => panic!("Didn't reject wasm with invalid api"),
}
}
#[test]
fn test_check_wasm_exports_of_old_contract() {
let module = deserialize_buffer(CONTRACT_0_7).unwrap();
match check_wasm_exports(&module) {
Err(VmError::StaticValidationErr { msg, .. }) => {
assert!(msg.starts_with(
"Wasm contract doesn't have required export: \"cosmwasm_vm_version_3\""
));
}
Err(e) => panic!("Unexpected error {:?}", e),
Ok(_) => panic!("Didn't reject wasm with invalid api"),
}
}
#[test]
fn check_wasm_imports_ok() {
let wasm = wat2wasm(
r#"(module
(import "env" "db_read" (func (param i32 i32) (result i32)))
(import "env" "db_write" (func (param i32 i32) (result i32)))
(import "env" "db_remove" (func (param i32) (result i32)))
(import "env" "canonicalize_address" (func (param i32 i32) (result i32)))
(import "env" "humanize_address" (func (param i32 i32) (result i32)))
)"#,
)
.unwrap();
check_wasm_imports(&deserialize_buffer(&wasm).unwrap()).unwrap();
}
#[test]
fn test_check_wasm_imports_of_old_contract() {
let module = deserialize_buffer(CONTRACT_0_7).unwrap();
match check_wasm_imports(&module) {
Err(VmError::StaticValidationErr { msg, .. }) => {
assert!(
msg.starts_with("Wasm contract requires unsupported import: \"env.read_db\"")
);
}
Err(e) => panic!("Unexpected error {:?}", e),
Ok(_) => panic!("Didn't reject wasm with invalid api"),
}
}
#[test]
fn test_check_wasm_imports_wrong_type() {
let wasm = wat2wasm(r#"(module (import "env" "db_read" (memory 1 1)))"#).unwrap();
match check_wasm_imports(&deserialize_buffer(&wasm).unwrap()) {
Err(VmError::StaticValidationErr { msg, .. }) => {
assert!(
msg.starts_with("Wasm contract requires non-function import: \"env.db_read\"")
);
}
Err(e) => panic!("Unexpected error {:?}", e),
Ok(_) => panic!("Didn't reject wasm with invalid api"),
}
}
#[test]
fn check_wasm_features_ok() {
let wasm = wat2wasm(
r#"(module
(type (func))
(func (type 0) nop)
(export "requires_water" (func 0))
(export "requires_" (func 0))
(export "requires_nutrients" (func 0))
(export "require_milk" (func 0))
(export "REQUIRES_air" (func 0))
(export "requires_sun" (func 0))
)"#,
)
.unwrap();
let module = deserialize_buffer(&wasm).unwrap();
let supported = HashSet::from_iter(
[
"water".to_string(),
"nutrients".to_string(),
"sun".to_string(),
"freedom".to_string(),
]
.iter()
.cloned(),
);
check_wasm_features(&module, &supported).unwrap();
}
#[test]
fn check_wasm_features_fails_for_missing() {
let wasm = wat2wasm(
r#"(module
(type (func))
(func (type 0) nop)
(export "requires_water" (func 0))
(export "requires_" (func 0))
(export "requires_nutrients" (func 0))
(export "require_milk" (func 0))
(export "REQUIRES_air" (func 0))
(export "requires_sun" (func 0))
)"#,
)
.unwrap();
let module = deserialize_buffer(&wasm).unwrap();
// Support set 1
let supported = HashSet::from_iter(
[
"water".to_string(),
"nutrients".to_string(),
"freedom".to_string(),
]
.iter()
.cloned(),
);
match check_wasm_features(&module, &supported).unwrap_err() {
VmError::StaticValidationErr { msg, .. } => assert_eq!(
msg,
"Wasm contract requires unsupported features: {\"sun\"}"
),
_ => panic!("Got unexpected error"),
}
// Support set 2
let supported = HashSet::from_iter(
[
"nutrients".to_string(),
"freedom".to_string(),
"Water".to_string(), // features are case sensitive (and lowercase by convention)
]
.iter()
.cloned(),
);
match check_wasm_features(&module, &supported).unwrap_err() {
VmError::StaticValidationErr { msg, .. } => assert_eq!(
msg,
"Wasm contract requires unsupported features: {\"sun\", \"water\"}"
),
_ => panic!("Got unexpected error"),
}
// Support set 3
let supported = HashSet::from_iter(["freedom".to_string()].iter().cloned());
match check_wasm_features(&module, &supported).unwrap_err() {
VmError::StaticValidationErr { msg, .. } => assert_eq!(
msg,
"Wasm contract requires unsupported features: {\"nutrients\", \"sun\", \"water\"}"
),
_ => panic!("Got unexpected error"),
}
// Support set 4
let supported = HashSet::from_iter([].iter().cloned());
match check_wasm_features(&module, &supported).unwrap_err() {
VmError::StaticValidationErr { msg, .. } => assert_eq!(
msg,
"Wasm contract requires unsupported features: {\"nutrients\", \"sun\", \"water\"}"
),
_ => panic!("Got unexpected error"),
}
}
}