-
Notifications
You must be signed in to change notification settings - Fork 986
/
run.rs
1404 lines (1291 loc) · 49.8 KB
/
run.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Wasm runners
use std::collections::BTreeSet;
use std::marker::PhantomData;
use borsh::BorshDeserialize;
use namada_core::ledger::gas::{GasMetering, TxGasMeter, WASM_MEMORY_PAGE_GAS};
use namada_core::ledger::storage::write_log::StorageModification;
use namada_core::types::transaction::TxSentinel;
use namada_core::types::validity_predicate::VpSentinel;
use parity_wasm::elements;
use thiserror::Error;
use wasmer::{BaseTunables, Module, Store};
use super::memory::{Limit, WasmMemory};
use super::TxCache;
use crate::ledger::gas::VpGasMeter;
use crate::ledger::storage::write_log::WriteLog;
use crate::ledger::storage::{self, Storage, StorageHasher};
use crate::proto::{Commitment, Section, Tx};
use crate::types::address::Address;
use crate::types::hash::{Error as TxHashError, Hash};
use crate::types::internal::HostEnvResult;
use crate::types::storage::{Key, TxIndex};
use crate::vm::host_env::{TxVmEnv, VpCtx, VpEvaluator, VpVmEnv};
use crate::vm::prefix_iter::PrefixIterators;
use crate::vm::types::VpInput;
use crate::vm::wasm::host_env::{tx_imports, vp_imports};
use crate::vm::wasm::{memory, Cache, CacheName, VpCache};
use crate::vm::{
validate_untrusted_wasm, WasmCacheAccess, WasmValidationError,
};
const TX_ENTRYPOINT: &str = "_apply_tx";
const VP_ENTRYPOINT: &str = "_validate_tx";
const WASM_STACK_LIMIT: u32 = u16::MAX as u32;
#[allow(missing_docs)]
#[derive(Error, Debug)]
pub enum Error {
#[error("Missing tx section: {0}")]
MissingSection(String),
#[error("Memory error: {0}")]
MemoryError(memory::Error),
#[error("Unable to inject stack limiter")]
StackLimiterInjection,
#[error("Wasm deserialization error: {0}")]
DeserializationError(elements::Error),
#[error("Wasm serialization error: {0}")]
SerializationError(elements::Error),
#[error("Unable to inject gas meter")]
GasMeterInjection,
#[error("Wasm compilation error: {0}")]
CompileError(wasmer::CompileError),
#[error("Missing wasm memory export, failed with: {0}")]
MissingModuleMemory(wasmer::ExportError),
#[error("Missing wasm entrypoint: {0}")]
MissingModuleEntrypoint(wasmer::ExportError),
#[error("Failed running wasm with: {0}")]
RuntimeError(wasmer::RuntimeError),
#[error("Failed instantiating wasm module with: {0}")]
// Boxed cause it's 128b
InstantiationError(Box<wasmer::InstantiationError>),
#[error(
"Unexpected module entrypoint interface {entrypoint}, failed with: \
{error}"
)]
UnexpectedModuleEntrypointInterface {
entrypoint: &'static str,
error: wasmer::RuntimeError,
},
#[error("Wasm validation error: {0}")]
ValidationError(WasmValidationError),
#[error("Wasm code hash error: {0}")]
CodeHash(TxHashError),
#[error("Unable to load wasm code: {0}")]
LoadWasmCode(String),
#[error("Unable to find compiled wasm code")]
NoCompiledWasmCode,
#[error("Gas error: {0}")]
GasError(String),
#[error("Failed type conversion: {0}")]
ConversionError(String),
#[error("Invalid transaction signature")]
InvalidTxSignature,
}
/// Result for functions that may fail
pub type Result<T> = std::result::Result<T, Error>;
/// Execute a transaction code. Returns the set verifiers addresses requested by
/// the transaction.
#[allow(clippy::too_many_arguments)]
pub fn tx<DB, H, CA>(
storage: &Storage<DB, H>,
write_log: &mut WriteLog,
gas_meter: &mut TxGasMeter,
tx_index: &TxIndex,
tx: &Tx,
vp_wasm_cache: &mut VpCache<CA>,
tx_wasm_cache: &mut TxCache<CA>,
) -> Result<BTreeSet<Address>>
where
DB: 'static + storage::DB + for<'iter> storage::DBIter<'iter>,
H: 'static + StorageHasher,
CA: 'static + WasmCacheAccess,
{
let tx_code = tx
.get_section(tx.code_sechash())
.and_then(|x| Section::code_sec(x.as_ref()))
.ok_or(Error::MissingSection(tx.code_sechash().to_string()))?;
// If the transaction code has a tag, ensure that the tag hash equals the
// transaction code's hash.
if let Some(tag) = &tx_code.tag {
// Get the WASM code hash corresponding to the tag from storage
let hash_key = Key::wasm_hash(tag);
let hash_value = match storage
.read(&hash_key)
.map_err(|e| {
Error::LoadWasmCode(format!(
"Read wasm code hash failed from storage: key {}, error {}",
hash_key, e
))
})?
.0
{
Some(v) => Hash::try_from_slice(&v)
.map_err(|e| Error::ConversionError(e.to_string()))?,
None => {
return Err(Error::LoadWasmCode(format!(
"No wasm code hash in storage: key {}",
hash_key
)));
}
};
// Ensure that the queried code hash equals the transaction's code hash
let tx_code_hash = tx_code.code.hash();
if tx_code_hash != hash_value {
return Err(Error::LoadWasmCode(format!(
"Transaction code hash does not correspond to tag: tx hash \
{}, tag {}, tag hash {}",
tx_code_hash, tag, hash_value,
)));
}
}
let (module, store) = fetch_or_compile(
tx_wasm_cache,
&tx_code.code,
write_log,
storage,
gas_meter,
)?;
let mut iterators: PrefixIterators<'_, DB> = PrefixIterators::default();
let mut verifiers = BTreeSet::new();
let mut result_buffer: Option<Vec<u8>> = None;
let mut sentinel = TxSentinel::default();
let env = TxVmEnv::new(
WasmMemory::default(),
storage,
write_log,
&mut iterators,
gas_meter,
&mut sentinel,
tx,
tx_index,
&mut verifiers,
&mut result_buffer,
vp_wasm_cache,
tx_wasm_cache,
);
let initial_memory =
memory::prepare_tx_memory(&store).map_err(Error::MemoryError)?;
let imports = tx_imports(&store, initial_memory, env);
// Instantiate the wasm module
let instance = wasmer::Instance::new(&module, &imports)
.map_err(|e| Error::InstantiationError(Box::new(e)))?;
// We need to write the inputs in the memory exported from the wasm
// module
let memory = instance
.exports
.get_memory("memory")
.map_err(Error::MissingModuleMemory)?;
let memory::TxCallInput {
tx_data_ptr,
tx_data_len,
} = memory::write_tx_inputs(memory, tx).map_err(Error::MemoryError)?;
// Get the module's entrypoint to be called
let apply_tx = instance
.exports
.get_function(TX_ENTRYPOINT)
.map_err(Error::MissingModuleEntrypoint)?
.native::<(u64, u64), ()>()
.map_err(|error| Error::UnexpectedModuleEntrypointInterface {
entrypoint: TX_ENTRYPOINT,
error,
})?;
apply_tx.call(tx_data_ptr, tx_data_len).map_err(|err| {
tracing::debug!("Tx WASM failed with {}", err);
match sentinel {
TxSentinel::None => Error::RuntimeError(err),
TxSentinel::OutOfGas => Error::GasError(err.to_string()),
TxSentinel::InvalidCommitment => {
Error::MissingSection(err.to_string())
}
}
})?;
Ok(verifiers)
}
/// Execute a validity predicate code. Returns whether the validity
/// predicate accepted storage modifications performed by the transaction
/// that triggered the execution.
#[allow(clippy::too_many_arguments)]
pub fn vp<DB, H, CA>(
vp_code_hash: Hash,
tx: &Tx,
tx_index: &TxIndex,
address: &Address,
storage: &Storage<DB, H>,
write_log: &WriteLog,
gas_meter: &mut VpGasMeter,
keys_changed: &BTreeSet<Key>,
verifiers: &BTreeSet<Address>,
mut vp_wasm_cache: VpCache<CA>,
) -> Result<bool>
where
DB: 'static + storage::DB + for<'iter> storage::DBIter<'iter>,
H: 'static + StorageHasher,
CA: 'static + WasmCacheAccess,
{
// Compile the wasm module
let (module, store) = fetch_or_compile(
&mut vp_wasm_cache,
&Commitment::Hash(vp_code_hash),
write_log,
storage,
gas_meter,
)?;
let mut iterators: PrefixIterators<'_, DB> = PrefixIterators::default();
let mut result_buffer: Option<Vec<u8>> = None;
let eval_runner = VpEvalWasm {
db: PhantomData,
hasher: PhantomData,
cache_access: PhantomData,
};
let mut sentinel = VpSentinel::default();
let env = VpVmEnv::new(
WasmMemory::default(),
address,
storage,
write_log,
gas_meter,
&mut sentinel,
tx,
tx_index,
&mut iterators,
verifiers,
&mut result_buffer,
keys_changed,
&eval_runner,
&mut vp_wasm_cache,
);
let initial_memory =
memory::prepare_vp_memory(&store).map_err(Error::MemoryError)?;
let imports = vp_imports(&store, initial_memory, env);
match run_vp(
module,
imports,
&vp_code_hash,
tx,
address,
keys_changed,
verifiers,
gas_meter,
) {
Ok(accept) => {
if sentinel.is_invalid_signature() {
if accept {
// This is unexpected, if the signature is invalid the vp
// should have rejected the tx. Something must be wrong with
// the VP logic and we take the signature verification
// result as the reference. In this case we override the vp
// result and log the issue
tracing::warn!(
"VP of {address} accepted the transaction but \
signaled that the signature was invalid. Overriding \
the vp result to reject the transaction..."
);
}
Err(Error::InvalidTxSignature)
} else {
Ok(accept)
}
}
Err(err) => {
if sentinel.is_out_of_gas() {
Err(Error::GasError(err.to_string()))
} else {
Err(err)
}
}
}
}
#[allow(clippy::too_many_arguments)]
fn run_vp(
module: wasmer::Module,
vp_imports: wasmer::ImportObject,
_vp_code_hash: &Hash,
input_data: &Tx,
address: &Address,
keys_changed: &BTreeSet<Key>,
verifiers: &BTreeSet<Address>,
_gas_meter: &mut VpGasMeter,
) -> Result<bool> {
let input: VpInput = VpInput {
addr: address,
data: input_data,
keys_changed,
verifiers,
};
// Instantiate the wasm module
let instance = wasmer::Instance::new(&module, &vp_imports)
.map_err(|e| Error::InstantiationError(Box::new(e)))?;
// We need to write the inputs in the memory exported from the wasm
// module
let memory = instance
.exports
.get_memory("memory")
.map_err(Error::MissingModuleMemory)?;
let memory::VpCallInput {
addr_ptr,
addr_len,
data_ptr,
data_len,
keys_changed_ptr,
keys_changed_len,
verifiers_ptr,
verifiers_len,
} = memory::write_vp_inputs(memory, input).map_err(Error::MemoryError)?;
// Get the module's entrypoint to be called
let validate_tx = instance
.exports
.get_function(VP_ENTRYPOINT)
.map_err(Error::MissingModuleEntrypoint)?
.native::<(u64, u64, u64, u64, u64, u64, u64, u64), u64>()
.map_err(|error| Error::UnexpectedModuleEntrypointInterface {
entrypoint: VP_ENTRYPOINT,
error,
})?;
let is_valid = validate_tx
.call(
addr_ptr,
addr_len,
data_ptr,
data_len,
keys_changed_ptr,
keys_changed_len,
verifiers_ptr,
verifiers_len,
)
.map_err(Error::RuntimeError)?;
tracing::debug!("is_valid {}", is_valid);
Ok(is_valid == 1)
}
/// Validity predicate wasm evaluator for `eval` host function calls.
#[derive(Default, Debug)]
pub struct VpEvalWasm<DB, H, CA>
where
DB: storage::DB + for<'iter> storage::DBIter<'iter>,
H: StorageHasher,
CA: WasmCacheAccess,
{
/// Phantom type for DB
pub db: PhantomData<*const DB>,
/// Phantom type for DB Hasher
pub hasher: PhantomData<*const H>,
/// Phantom type for WASM compilation cache access
pub cache_access: PhantomData<*const CA>,
}
impl<DB, H, CA> VpEvaluator for VpEvalWasm<DB, H, CA>
where
DB: 'static + storage::DB + for<'iter> storage::DBIter<'iter>,
H: 'static + StorageHasher,
CA: WasmCacheAccess,
{
type CA = CA;
type Db = DB;
type Eval = Self;
type H = H;
fn eval(
&self,
ctx: VpCtx<'static, DB, H, Self, CA>,
vp_code_hash: Hash,
input_data: Tx,
) -> HostEnvResult {
match self.eval_native_result(ctx, vp_code_hash, input_data) {
Ok(ok) => HostEnvResult::from(ok),
Err(err) => {
tracing::warn!("VP eval error {}", err);
HostEnvResult::Fail
}
}
}
}
impl<DB, H, CA> VpEvalWasm<DB, H, CA>
where
DB: 'static + storage::DB + for<'iter> storage::DBIter<'iter>,
H: 'static + StorageHasher,
CA: WasmCacheAccess,
{
/// Evaluate the given VP.
pub fn eval_native_result(
&self,
ctx: VpCtx<'static, DB, H, Self, CA>,
vp_code_hash: Hash,
input_data: Tx,
) -> Result<bool> {
let address = unsafe { ctx.address.get() };
let keys_changed = unsafe { ctx.keys_changed.get() };
let verifiers = unsafe { ctx.verifiers.get() };
let vp_wasm_cache = unsafe { ctx.vp_wasm_cache.get() };
let write_log = unsafe { ctx.write_log.get() };
let storage = unsafe { ctx.storage.get() };
let gas_meter = unsafe { ctx.gas_meter.get() };
let env = VpVmEnv {
memory: WasmMemory::default(),
ctx,
};
// Compile the wasm module
let (module, store) = fetch_or_compile(
vp_wasm_cache,
&Commitment::Hash(vp_code_hash),
write_log,
storage,
gas_meter,
)?;
let initial_memory =
memory::prepare_vp_memory(&store).map_err(Error::MemoryError)?;
let imports = vp_imports(&store, initial_memory, env);
run_vp(
module,
imports,
&vp_code_hash,
&input_data,
address,
keys_changed,
verifiers,
gas_meter,
)
}
}
/// Prepare a wasm store for untrusted code.
pub fn untrusted_wasm_store(limit: Limit<BaseTunables>) -> wasmer::Store {
// Use Singlepass compiler with the default settings
let compiler = wasmer_compiler_singlepass::Singlepass::default();
wasmer::Store::new_with_tunables(
&wasmer_engine_universal::Universal::new(compiler).engine(),
limit,
)
}
/// Inject gas counter and stack-height limiter into the given wasm code
pub fn prepare_wasm_code<T: AsRef<[u8]>>(code: T) -> Result<Vec<u8>> {
let module: elements::Module = elements::deserialize_buffer(code.as_ref())
.map_err(Error::DeserializationError)?;
let module = wasm_instrument::gas_metering::inject(
module,
wasm_instrument::gas_metering::host_function::Injector::new(
"env", "gas",
),
&get_gas_rules(),
)
.map_err(|_original_module| Error::GasMeterInjection)?;
let module =
wasm_instrument::inject_stack_limiter(module, WASM_STACK_LIMIT)
.map_err(|_original_module| Error::StackLimiterInjection)?;
elements::serialize(module).map_err(Error::SerializationError)
}
// Fetch or compile a WASM code from the cache or storage. Account for the
// loading and code compilation gas costs.
fn fetch_or_compile<DB, H, CN, CA>(
wasm_cache: &mut Cache<CN, CA>,
code_or_hash: &Commitment,
write_log: &WriteLog,
storage: &Storage<DB, H>,
gas_meter: &mut dyn GasMetering,
) -> Result<(Module, Store)>
where
DB: 'static + storage::DB + for<'iter> storage::DBIter<'iter>,
H: 'static + StorageHasher,
CN: 'static + CacheName,
CA: 'static + WasmCacheAccess,
{
match code_or_hash {
Commitment::Hash(code_hash) => {
let (module, store, tx_len) = match wasm_cache.fetch(code_hash)? {
Some((module, store)) => {
// Gas accounting even if the compiled module is in cache
let key = Key::wasm_code_len(code_hash);
let tx_len = match write_log.read(&key).0 {
Some(StorageModification::Write { value }) => {
u64::try_from_slice(value).map_err(|e| {
Error::ConversionError(e.to_string())
})
}
_ => match storage
.read(&key)
.map_err(|e| {
Error::LoadWasmCode(format!(
"Read wasm code length failed from \
storage: key {}, error {}",
key, e
))
})?
.0
{
Some(v) => u64::try_from_slice(&v).map_err(|e| {
Error::ConversionError(e.to_string())
}),
None => Err(Error::LoadWasmCode(format!(
"No wasm code length in storage: key {}",
key
))),
},
}?;
(module, store, tx_len)
}
None => {
let key = Key::wasm_code(code_hash);
let code = match write_log.read(&key).0 {
Some(StorageModification::Write { value }) => {
value.clone()
}
_ => match storage
.read(&key)
.map_err(|e| {
Error::LoadWasmCode(format!(
"Read wasm code failed from storage: key \
{}, error {}",
key, e
))
})?
.0
{
Some(v) => v,
None => {
return Err(Error::LoadWasmCode(format!(
"No wasm code in storage: key {}",
key
)));
}
},
};
let tx_len = u64::try_from(code.len())
.map_err(|e| Error::ConversionError(e.to_string()))?;
match wasm_cache.compile_or_fetch(code)? {
Some((module, store)) => (module, store, tx_len),
None => return Err(Error::NoCompiledWasmCode),
}
}
};
gas_meter
.add_wasm_load_from_storage_gas(tx_len)
.map_err(|e| Error::GasError(e.to_string()))?;
gas_meter
.add_compiling_gas(tx_len)
.map_err(|e| Error::GasError(e.to_string()))?;
Ok((module, store))
}
Commitment::Id(code) => {
let tx_len = code.len() as u64;
gas_meter
.add_wasm_validation_gas(tx_len)
.map_err(|e| Error::GasError(e.to_string()))?;
validate_untrusted_wasm(code).map_err(Error::ValidationError)?;
gas_meter
.add_compiling_gas(tx_len)
.map_err(|e| Error::GasError(e.to_string()))?;
match wasm_cache.compile_or_fetch(code)? {
Some((module, store)) => Ok((module, store)),
None => Err(Error::NoCompiledWasmCode),
}
}
}
}
/// Get the gas rules used to meter wasm operations
fn get_gas_rules() -> wasm_instrument::gas_metering::ConstantCostRules {
// NOTE: costs set to 0 don't actually trigger the injection of a call to
// the gas host function (no useless instructions are injected)
let instruction_cost = 0;
let memory_grow_cost = WASM_MEMORY_PAGE_GAS;
let call_per_local_cost = 0;
wasm_instrument::gas_metering::ConstantCostRules::new(
instruction_cost,
memory_grow_cost,
call_per_local_cost,
)
}
#[cfg(test)]
mod tests {
use borsh_ext::BorshSerializeExt;
use itertools::Either;
use namada_test_utils::TestWasms;
use test_log::test;
use wasmer_vm::TrapCode;
use super::*;
use crate::ledger::storage::testing::TestStorage;
use crate::proto::{Code, Data};
use crate::types::hash::Hash;
use crate::types::transaction::TxType;
use crate::types::validity_predicate::EvalVp;
use crate::vm::wasm;
const TX_GAS_LIMIT: u64 = 10_000_000_000;
/// Test that when a transaction wasm goes over the stack-height limit, the
/// execution is aborted.
#[test]
// NB: Disabled on aarch64 macOS since a fix for
// https://github.com/wasmerio/wasmer/issues/4072
// reduced the available stack space on mac
#[cfg_attr(all(target_arch = "aarch64", target_os = "macos"), ignore)]
fn test_tx_stack_limiter() {
// Because each call into `$loop` inside the wasm consumes 5 stack
// heights except for the terminal call, this should hit the stack
// limit.
let loops = WASM_STACK_LIMIT / 5 - 1;
let error = loop_in_tx_wasm(loops).expect_err(&format!(
"Expecting runtime error \"unreachable\" caused by stack-height \
overflow, loops {}. Got",
loops,
));
assert_stack_overflow(&error);
// one less loop shouldn't go over the limit
let result = loop_in_tx_wasm(loops - 1);
assert!(result.is_ok(), "Expected success. Got {:?}", result);
}
/// Test that when a VP wasm goes over the stack-height limit, the execution
/// is aborted.
#[test]
// NB: Disabled on aarch64 macOS since a fix for
// https://github.com/wasmerio/wasmer/issues/4072
// reduced the available stack space on mac
#[cfg_attr(all(target_arch = "aarch64", target_os = "macos"), ignore)]
fn test_vp_stack_limiter() {
// Because each call into `$loop` inside the wasm consumes 5 stack
// heights except for the terminal call, this should hit the stack
// limit.
let loops = WASM_STACK_LIMIT / 5 - 1;
let error = loop_in_vp_wasm(loops).expect_err(
"Expecting runtime error caused by stack-height overflow. Got",
);
assert_stack_overflow(&error);
// one less loop shouldn't go over the limit
let result = loop_in_vp_wasm(loops - 1);
assert!(result.is_ok(), "Expected success. Got {:?}", result);
}
/// Test that when a transaction wasm goes over the memory limit inside the
/// wasm execution, the execution is aborted.
#[test]
fn test_tx_memory_limiter_in_guest() {
let storage = TestStorage::default();
let mut write_log = WriteLog::default();
let mut gas_meter = TxGasMeter::new_from_sub_limit(TX_GAS_LIMIT.into());
let tx_index = TxIndex::default();
// This code will allocate memory of the given size
let tx_code = TestWasms::TxMemoryLimit.read_bytes();
// store the wasm code
let code_hash = Hash::sha256(&tx_code);
let key = Key::wasm_code(&code_hash);
let len_key = Key::wasm_code_len(&code_hash);
let code_len = (tx_code.len() as u64).serialize_to_vec();
write_log.write(&key, tx_code.clone()).unwrap();
write_log.write(&len_key, code_len).unwrap();
// Assuming 200 pages, 12.8 MiB limit
assert_eq!(memory::TX_MEMORY_MAX_PAGES, 200);
// Allocating `2^23` (8 MiB) should be below the memory limit and
// shouldn't fail
let tx_data = 2_usize.pow(23).serialize_to_vec();
let (mut vp_cache, _) =
wasm::compilation_cache::common::testing::cache();
let (mut tx_cache, _) =
wasm::compilation_cache::common::testing::cache();
let mut outer_tx = Tx::from_type(TxType::Raw);
outer_tx.set_code(Code::new(tx_code.clone(), None));
outer_tx.set_data(Data::new(tx_data));
let result = tx(
&storage,
&mut write_log,
&mut gas_meter,
&tx_index,
&outer_tx,
&mut vp_cache,
&mut tx_cache,
);
assert!(result.is_ok(), "Expected success, got {:?}", result);
// Allocating `2^24` (16 MiB) should be above the memory limit and
// should fail
let tx_data = 2_usize.pow(24).serialize_to_vec();
let mut outer_tx = Tx::from_type(TxType::Raw);
outer_tx.set_code(Code::new(tx_code, None));
outer_tx.set_data(Data::new(tx_data));
let error = tx(
&storage,
&mut write_log,
&mut gas_meter,
&tx_index,
&outer_tx,
&mut vp_cache,
&mut tx_cache,
)
.expect_err("Expected to run out of memory");
assert_stack_overflow(&error);
}
/// Test that when a validity predicate wasm goes over the memory limit
/// inside the wasm execution when calling `eval` host function, the `eval`
/// fails and hence returns `false`.
#[test]
fn test_vp_memory_limiter_in_guest_calling_eval() {
let mut storage = TestStorage::default();
let addr = storage.address_gen.generate_address("rng seed");
let write_log = WriteLog::default();
let mut gas_meter = VpGasMeter::new_from_tx_meter(
&TxGasMeter::new_from_sub_limit(TX_GAS_LIMIT.into()),
);
let keys_changed = BTreeSet::new();
let verifiers = BTreeSet::new();
let tx_index = TxIndex::default();
// This code will call `eval` with the other VP below
let vp_eval = TestWasms::VpEval.read_bytes();
// store the wasm code
let code_hash = Hash::sha256(&vp_eval);
let key = Key::wasm_code(&code_hash);
let len_key = Key::wasm_code_len(&code_hash);
let code_len = (vp_eval.len() as u64).serialize_to_vec();
storage.write(&key, vp_eval).unwrap();
storage.write(&len_key, code_len).unwrap();
// This code will allocate memory of the given size
let vp_memory_limit = TestWasms::VpMemoryLimit.read_bytes();
// store the wasm code
let limit_code_hash = Hash::sha256(&vp_memory_limit);
let key = Key::wasm_code(&limit_code_hash);
let len_key = Key::wasm_code_len(&limit_code_hash);
let code_len = (vp_memory_limit.len() as u64).serialize_to_vec();
storage.write(&key, vp_memory_limit).unwrap();
storage.write(&len_key, code_len).unwrap();
// Assuming 200 pages, 12.8 MiB limit
assert_eq!(memory::VP_MEMORY_MAX_PAGES, 200);
// Allocating `2^23` (8 MiB) should be below the memory limit and
// shouldn't fail
let input = 2_usize.pow(23).serialize_to_vec();
let mut tx = Tx::new(storage.chain_id.clone(), None);
tx.add_code(vec![], None).add_serialized_data(input);
let eval_vp = EvalVp {
vp_code_hash: limit_code_hash,
input: tx,
};
let mut outer_tx = Tx::new(storage.chain_id.clone(), None);
outer_tx.add_code(vec![], None).add_data(eval_vp);
let (vp_cache, _) = wasm::compilation_cache::common::testing::cache();
// When the `eval`ed VP doesn't run out of memory, it should return
// `true`
let passed = vp(
code_hash,
&outer_tx,
&tx_index,
&addr,
&storage,
&write_log,
&mut gas_meter,
&keys_changed,
&verifiers,
vp_cache.clone(),
)
.unwrap();
assert!(passed);
// Allocating `2^24` (16 MiB) should be above the memory limit and
// should fail
let input = 2_usize.pow(24).serialize_to_vec();
let mut tx = Tx::new(storage.chain_id.clone(), None);
tx.add_code(vec![], None).add_data(input);
let eval_vp = EvalVp {
vp_code_hash: limit_code_hash,
input: tx,
};
let mut outer_tx = Tx::new(storage.chain_id.clone(), None);
outer_tx.add_code(vec![], None).add_data(eval_vp);
// When the `eval`ed VP runs out of memory, its result should be
// `false`, hence we should also get back `false` from the VP that
// called `eval`.
let passed = vp(
code_hash,
&outer_tx,
&tx_index,
&addr,
&storage,
&write_log,
&mut gas_meter,
&keys_changed,
&verifiers,
vp_cache,
)
.unwrap();
assert!(!passed);
}
/// Test that when a validity predicate wasm goes over the memory limit
/// inside the wasm execution, the execution is aborted.
#[test]
fn test_vp_memory_limiter_in_guest() {
let mut storage = TestStorage::default();
let addr = storage.address_gen.generate_address("rng seed");
let write_log = WriteLog::default();
let mut gas_meter = VpGasMeter::new_from_tx_meter(
&TxGasMeter::new_from_sub_limit(TX_GAS_LIMIT.into()),
);
let keys_changed = BTreeSet::new();
let verifiers = BTreeSet::new();
let tx_index = TxIndex::default();
// This code will allocate memory of the given size
let vp_code = TestWasms::VpMemoryLimit.read_bytes();
// store the wasm code
let code_hash = Hash::sha256(&vp_code);
let code_len = (vp_code.len() as u64).serialize_to_vec();
let key = Key::wasm_code(&code_hash);
let len_key = Key::wasm_code_len(&code_hash);
storage.write(&key, vp_code).unwrap();
storage.write(&len_key, code_len).unwrap();
// Assuming 200 pages, 12.8 MiB limit
assert_eq!(memory::VP_MEMORY_MAX_PAGES, 200);
// Allocating `2^23` (8 MiB) should be below the memory limit and
// shouldn't fail
let tx_data = 2_usize.pow(23).serialize_to_vec();
let mut outer_tx = Tx::from_type(TxType::Raw);
outer_tx.header.chain_id = storage.chain_id.clone();
outer_tx.set_data(Data::new(tx_data));
outer_tx.set_code(Code::new(vec![], None));
let (vp_cache, _) = wasm::compilation_cache::common::testing::cache();
let result = vp(
code_hash,
&outer_tx,
&tx_index,
&addr,
&storage,
&write_log,
&mut gas_meter,
&keys_changed,
&verifiers,
vp_cache.clone(),
);
assert!(result.is_ok(), "Expected success, got {:?}", result);
// Allocating `2^24` (16 MiB) should be above the memory limit and
// should fail
let tx_data = 2_usize.pow(24).serialize_to_vec();
let mut outer_tx = Tx::from_type(TxType::Raw);
outer_tx.header.chain_id = storage.chain_id.clone();
outer_tx.set_data(Data::new(tx_data));
let error = vp(
code_hash,
&outer_tx,
&tx_index,
&addr,
&storage,
&write_log,
&mut gas_meter,
&keys_changed,
&verifiers,
vp_cache,
)
.expect_err("Expected to run out of memory");
assert_stack_overflow(&error);
}
/// Test that when a transaction wasm goes over the wasm memory limit in the
/// host input, the execution fails.
#[test]
fn test_tx_memory_limiter_in_host_input() {
let storage = TestStorage::default();
let mut write_log = WriteLog::default();
let mut gas_meter = TxGasMeter::new_from_sub_limit(TX_GAS_LIMIT.into());
let tx_index = TxIndex::default();
let tx_no_op = TestWasms::TxNoOp.read_bytes();
// store the wasm code
let code_hash = Hash::sha256(&tx_no_op);
let key = Key::wasm_code(&code_hash);
let len_key = Key::wasm_code_len(&code_hash);
let code_len = (tx_no_op.len() as u64).serialize_to_vec();
write_log.write(&key, tx_no_op.clone()).unwrap();
write_log.write(&len_key, code_len).unwrap();
// Assuming 200 pages, 12.8 MiB limit
assert_eq!(memory::TX_MEMORY_MAX_PAGES, 200);
// Allocating `2^24` (16 MiB) for the input should be above the memory
// limit and should fail
let len = 2_usize.pow(24);
let tx_data: Vec<u8> = vec![6_u8; len];
let (mut vp_cache, _) =
wasm::compilation_cache::common::testing::cache();
let (mut tx_cache, _) =
wasm::compilation_cache::common::testing::cache();
let mut outer_tx = Tx::from_type(TxType::Raw);
outer_tx.set_code(Code::new(tx_no_op, None));
outer_tx.set_data(Data::new(tx_data));
let result = tx(
&storage,
&mut write_log,
&mut gas_meter,
&tx_index,
&outer_tx,
&mut vp_cache,
&mut tx_cache,
);
// Depending on platform, we get a different error from the running out
// of memory
match result {
// Dylib engine error (used anywhere except mac)
Err(Error::MemoryError(memory::Error::MemoryOutOfBounds(
wasmer::MemoryError::CouldNotGrow { .. },
))) => {}
Err(error) => {
let trap_code = get_trap_code(&error);
// Universal engine error (currently used on mac)
assert_eq!(
trap_code,
Either::Left(wasmer_vm::TrapCode::HeapAccessOutOfBounds)
);
}
_ => panic!("Expected to run out of memory, got {:?}", result),
}
}
/// Test that when a validity predicate wasm goes over the wasm memory limit
/// in the host input, the execution fails.
#[test]
fn test_vp_memory_limiter_in_host_input() {
let mut storage = TestStorage::default();