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
// This file is part of Gear.

// Copyright (C) 2021-2023 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// 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
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{
    common::{
        ActorExecutionError, ActorExecutionErrorReplyReason, DispatchResult, DispatchResultKind,
        ExecutionError, SystemExecutionError, WasmExecutionContext,
    },
    configs::{BlockInfo, ExecutionSettings},
    ext::{ProcessorContext, ProcessorExternalities},
};
use alloc::{
    collections::{BTreeMap, BTreeSet},
    format,
    string::{String, ToString},
    vec::Vec,
};
use gear_backend_common::{
    lazy_pages::{GlobalsAccessConfig, LazyPagesWeights, Status},
    ActorTerminationReason, BackendExternalities, BackendReport, BackendSyscallError, Environment,
    EnvironmentError, TerminationReason, TrapExplanation,
};
use gear_core::{
    code::InstrumentedCode,
    env::Externalities,
    gas::{GasAllowanceCounter, GasCounter, ValueCounter},
    ids::ProgramId,
    memory::{AllocationsContext, Memory, MemoryError, PageBuf},
    message::{
        ContextSettings, DispatchKind, IncomingDispatch, IncomingMessage, MessageContext,
        WasmEntryPoint,
    },
    pages::{GearPage, PageU32Size, WasmPage},
    program::Program,
    reservation::GasReserver,
};
use scale_info::{
    scale::{self, Decode, Encode},
    TypeInfo,
};

#[derive(Debug, derive_more::Display, derive_more::From)]
pub enum PrepareMemoryError {
    #[display(fmt = "{_0}")]
    Actor(ActorPrepareMemoryError),
    #[display(fmt = "{_0}")]
    System(SystemPrepareMemoryError),
}

/// Prepare memory error
#[derive(Encode, Decode, TypeInfo, Debug, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)]
#[codec(crate = scale)]
pub enum ActorPrepareMemoryError {
    /// Stack end page, which value is specified in WASM code, cannot be bigger than static memory size.
    #[display(fmt = "Stack end page {_0:?} is bigger then WASM static memory size {_1:?}")]
    StackEndPageBiggerWasmMemSize(WasmPage, WasmPage),
    /// It's not allowed to set initial data for stack memory pages, if they are specified in WASM code.
    #[display(fmt = "Set initial data for stack pages is restricted")]
    StackPagesHaveInitialData,
    /// Stack is not aligned to WASM page size
    #[display(fmt = "Stack end addr {_0:#x} must be aligned to WASM page size")]
    StackIsNotAligned(u32),
}

#[derive(Debug, Eq, PartialEq, derive_more::Display)]
pub enum SystemPrepareMemoryError {
    /// Mem size less then static pages num
    #[display(fmt = "Mem size less then static pages num")]
    InsufficientMemorySize,
    /// Page with data is not allocated for program
    #[display(fmt = "{_0:?} is not allocated for program")]
    PageIsNotAllocated(GearPage),
    /// Cannot read initial memory data from wasm memory.
    #[display(fmt = "Cannot read data for {_0:?}: {_1}")]
    InitialMemoryReadFailed(GearPage, MemoryError),
    /// Cannot write initial data to wasm memory.
    #[display(fmt = "Cannot write initial data for {_0:?}: {_1}")]
    InitialDataWriteFailed(GearPage, MemoryError),
    /// Initial pages data must be empty in lazy pages mode
    #[display(fmt = "Initial pages data must be empty when execute with lazy pages")]
    InitialPagesContainsDataInLazyPagesMode,
}

/// Make checks that everything with memory goes well.
fn check_memory<'a>(
    allocations: &BTreeSet<WasmPage>,
    pages_with_data: impl Iterator<Item = &'a GearPage>,
    static_pages: WasmPage,
    memory_size: WasmPage,
) -> Result<(), SystemPrepareMemoryError> {
    if memory_size < static_pages {
        log::error!(
            "Mem size less then static pages num: mem_size = {:?}, static_pages = {:?}",
            memory_size,
            static_pages
        );
        return Err(SystemPrepareMemoryError::InsufficientMemorySize);
    }

    // Checks that all pages with data are in allocations set.
    for page in pages_with_data {
        let wasm_page = page.to_page();
        if wasm_page >= static_pages && !allocations.contains(&wasm_page) {
            return Err(SystemPrepareMemoryError::PageIsNotAllocated(*page));
        }
    }

    Ok(())
}

fn lazy_pages_check_initial_data(
    initial_pages_data: &BTreeMap<GearPage, PageBuf>,
) -> Result<(), SystemPrepareMemoryError> {
    initial_pages_data
        .is_empty()
        .then_some(())
        .ok_or(SystemPrepareMemoryError::InitialPagesContainsDataInLazyPagesMode)
}

/// Writes initial pages data to memory and prepare memory for execution.
fn prepare_memory<ProcessorExt: ProcessorExternalities, EnvMem: Memory>(
    mem: &mut EnvMem,
    program_id: ProgramId,
    pages_data: &mut BTreeMap<GearPage, PageBuf>,
    static_pages: WasmPage,
    stack_end: Option<u32>,
    globals_config: GlobalsAccessConfig,
    lazy_pages_weights: LazyPagesWeights,
) -> Result<(), PrepareMemoryError> {
    let stack_end = if let Some(stack_end) = stack_end {
        let stack_end = (stack_end % WasmPage::size() == 0)
            .then_some(WasmPage::from_offset(stack_end))
            .ok_or(ActorPrepareMemoryError::StackIsNotAligned(stack_end))?;

        if stack_end > static_pages {
            return Err(ActorPrepareMemoryError::StackEndPageBiggerWasmMemSize(
                stack_end,
                static_pages,
            )
            .into());
        }

        Some(stack_end)
    } else {
        None
    };

    // Set initial data for pages
    for (page, data) in pages_data.iter_mut() {
        mem.write(page.offset(), data)
            .map_err(|err| SystemPrepareMemoryError::InitialDataWriteFailed(*page, err))?;
    }

    if ProcessorExt::LAZY_PAGES_ENABLED {
        lazy_pages_check_initial_data(pages_data)?;

        ProcessorExt::lazy_pages_init_for_program(
            mem,
            program_id,
            stack_end,
            globals_config,
            lazy_pages_weights,
        );
    } else {
        // If we executes without lazy pages, then we have to save all initial data for static pages,
        // in order to be able to identify pages, which has been changed during execution.
        // Skip stack page if they are specified.
        let begin = stack_end.unwrap_or_default();

        if pages_data.keys().any(|&p| p < begin.to_page()) {
            return Err(ActorPrepareMemoryError::StackPagesHaveInitialData.into());
        }

        let non_stack_pages = begin.iter_end(static_pages).unwrap_or_else(|err| {
            unreachable!(
                "We have already checked that `stack_end` is <= `static_pages`, but get: {}",
                err
            )
        });
        for page in non_stack_pages.flat_map(|p| p.to_pages_iter()) {
            if pages_data.contains_key(&page) {
                // This page already has initial data
                continue;
            }
            let mut data = PageBuf::new_zeroed();
            mem.read(page.offset(), &mut data)
                .map_err(|err| SystemPrepareMemoryError::InitialMemoryReadFailed(page, err))?;
            pages_data.insert(page, data);
        }
    }
    Ok(())
}

/// Returns pages and their new data, which must be updated or uploaded to storage.
fn get_pages_to_be_updated<ProcessorExt: ProcessorExternalities>(
    old_pages_data: BTreeMap<GearPage, PageBuf>,
    new_pages_data: BTreeMap<GearPage, PageBuf>,
    static_pages: WasmPage,
) -> BTreeMap<GearPage, PageBuf> {
    if ProcessorExt::LAZY_PAGES_ENABLED {
        // In lazy pages mode we update some page data in storage,
        // when it has been write accessed, so no need to compare old and new page data.
        new_pages_data.keys().for_each(|page| {
            log::trace!("{:?} has been write accessed, update it in storage", page)
        });
        return new_pages_data;
    }

    let mut page_update = BTreeMap::new();
    let mut old_pages_data = old_pages_data;
    let static_gear_pages = static_pages.to_page();
    for (page, new_data) in new_pages_data {
        let initial_data = if let Some(initial_data) = old_pages_data.remove(&page) {
            initial_data
        } else {
            // If it's static page without initial data,
            // then it's stack page and we skip this page update.
            if page < static_gear_pages {
                continue;
            }

            // If page has no data in `pages_initial_data` then data is zeros.
            // Because it's default data for wasm pages which is not static,
            // and for all static pages we save data in `pages_initial_data` in E::new.
            PageBuf::new_zeroed()
        };

        if new_data != initial_data {
            page_update.insert(page, new_data);
            log::trace!("{page:?} has been changed - will be updated in storage");
        }
    }
    page_update
}

/// Execute wasm with dispatch and return dispatch result.
pub fn execute_wasm<E>(
    balance: u128,
    dispatch: IncomingDispatch,
    context: WasmExecutionContext,
    settings: ExecutionSettings,
    msg_ctx_settings: ContextSettings,
) -> Result<DispatchResult, ExecutionError>
where
    E: Environment,
    E::Ext: ProcessorExternalities + BackendExternalities + 'static,
    <E::Ext as Externalities>::UnrecoverableError: BackendSyscallError,
{
    let WasmExecutionContext {
        gas_counter,
        gas_allowance_counter,
        gas_reserver,
        program,
        mut pages_initial_data,
        memory_size,
    } = context;

    let program_id = program.id();
    let kind = dispatch.kind();

    log::debug!("Executing program {}", program_id);
    log::debug!("Executing dispatch {:?}", dispatch);

    let static_pages = program.static_pages();
    let allocations = program.allocations();

    check_memory(
        allocations,
        pages_initial_data.keys(),
        static_pages,
        memory_size,
    )
    .map_err(SystemExecutionError::PrepareMemory)?;

    // Creating allocations context.
    let allocations_context =
        AllocationsContext::new(allocations.clone(), static_pages, settings.max_pages);

    // Creating message context.
    let message_context = MessageContext::new(dispatch.clone(), program_id, msg_ctx_settings);

    // Creating value counter.
    let value_counter = ValueCounter::new(balance + dispatch.value());

    let context = ProcessorContext {
        gas_counter,
        gas_allowance_counter,
        gas_reserver,
        system_reservation: None,
        value_counter,
        allocations_context,
        message_context,
        block_info: settings.block_info,
        max_pages: settings.max_pages,
        page_costs: settings.page_costs,
        existential_deposit: settings.existential_deposit,
        program_id,
        program_candidates_data: Default::default(),
        program_rents: Default::default(),
        host_fn_weights: settings.host_fn_weights,
        forbidden_funcs: settings.forbidden_funcs,
        mailbox_threshold: settings.mailbox_threshold,
        waitlist_cost: settings.waitlist_cost,
        dispatch_hold_cost: settings.dispatch_hold_cost,
        reserve_for: settings.reserve_for,
        reservation: settings.reservation,
        random_data: settings.random_data,
        rent_cost: settings.rent_cost,
    };

    let lazy_pages_weights = context.page_costs.lazy_pages_weights();

    // Creating externalities.
    let ext = E::Ext::new(context);

    // Execute program in backend env.
    let execute = || {
        let env = E::new(
            ext,
            program.raw_code(),
            kind,
            program.code().exports().clone(),
            memory_size,
        )
        .map_err(EnvironmentError::from_infallible)?;
        env.execute(|memory, stack_end, globals_config| {
            prepare_memory::<E::Ext, E::Memory>(
                memory,
                program_id,
                &mut pages_initial_data,
                static_pages,
                stack_end,
                globals_config,
                lazy_pages_weights,
            )
        })
    };
    let (termination, memory, ext) = match execute() {
        Ok(report) => {
            let BackendReport {
                termination_reason,
                memory_wrap: mut memory,
                ext,
            } = report;

            let mut termination = match termination_reason {
                TerminationReason::Actor(reason) => reason,
                TerminationReason::System(reason) => {
                    return Err(ExecutionError::System(reason.into()))
                }
            };

            // released pages initial data will be added to `pages_initial_data` after execution.
            if E::Ext::LAZY_PAGES_ENABLED {
                E::Ext::lazy_pages_post_execution_actions(&mut memory);

                match E::Ext::lazy_pages_status() {
                    Status::Normal => (),
                    Status::GasLimitExceeded => {
                        termination =
                            ActorTerminationReason::Trap(TrapExplanation::GasLimitExceeded);
                    }
                    Status::GasAllowanceExceeded => {
                        termination = ActorTerminationReason::GasAllowanceExceeded;
                    }
                }
            }

            (termination, memory, ext)
        }
        Err(EnvironmentError::System(e)) => {
            return Err(ExecutionError::System(SystemExecutionError::Environment(
                e.to_string(),
            )))
        }
        Err(EnvironmentError::PrepareMemory(gas_amount, PrepareMemoryError::Actor(e))) => {
            return Err(ExecutionError::Actor(ActorExecutionError {
                gas_amount,
                reason: ActorExecutionErrorReplyReason::PrepareMemory(e),
            }))
        }
        Err(EnvironmentError::PrepareMemory(_gas_amount, PrepareMemoryError::System(e))) => {
            return Err(ExecutionError::System(e.into()));
        }
        Err(EnvironmentError::Actor(gas_amount, err)) => {
            return Err(ExecutionError::Actor(ActorExecutionError {
                gas_amount,
                reason: ActorExecutionErrorReplyReason::Environment(err.into()),
            }))
        }
    };

    log::debug!("Termination reason: {:?}", termination);

    let info = ext
        .into_ext_info(&memory)
        .map_err(SystemExecutionError::IntoExtInfo)?;

    if E::Ext::LAZY_PAGES_ENABLED {
        lazy_pages_check_initial_data(&pages_initial_data)
            .map_err(SystemExecutionError::PrepareMemory)?;
    }

    // Parsing outcome.
    let kind = match termination {
        ActorTerminationReason::Exit(value_dest) => DispatchResultKind::Exit(value_dest),
        ActorTerminationReason::Leave | ActorTerminationReason::Success => {
            DispatchResultKind::Success
        }
        ActorTerminationReason::Trap(explanation) => {
            log::debug!("💥 Trap during execution of {program_id}\n📔 Explanation: {explanation}");
            DispatchResultKind::Trap(explanation)
        }
        ActorTerminationReason::Wait(duration, waited_type) => {
            DispatchResultKind::Wait(duration, waited_type)
        }
        ActorTerminationReason::GasAllowanceExceeded => DispatchResultKind::GasAllowanceExceed,
    };

    let page_update =
        get_pages_to_be_updated::<E::Ext>(pages_initial_data, info.pages_data, static_pages);

    // Getting new programs that are scheduled to be initialized (respected messages are in `generated_dispatches` collection)
    let program_candidates = info.program_candidates_data;

    // Output
    Ok(DispatchResult {
        kind,
        dispatch,
        program_id,
        context_store: info.context_store,
        generated_dispatches: info.generated_dispatches,
        awakening: info.awakening,
        reply_deposits: info.reply_deposits,
        program_candidates,
        program_rents: info.program_rents,
        gas_amount: info.gas_amount,
        gas_reserver: Some(info.gas_reserver),
        system_reservation_context: info.system_reservation_context,
        page_update,
        allocations: info.allocations,
    })
}

/// !!! FOR TESTING / INFORMATIONAL USAGE ONLY
#[allow(clippy::too_many_arguments)]
pub fn execute_for_reply<E, EP>(
    function: EP,
    instrumented_code: InstrumentedCode,
    pages_initial_data: Option<BTreeMap<GearPage, PageBuf>>,
    allocations: Option<BTreeSet<WasmPage>>,
    program_id: Option<ProgramId>,
    payload: Vec<u8>,
    gas_limit: u64,
    block_info: BlockInfo,
) -> Result<Vec<u8>, String>
where
    E: Environment<EP>,
    E::Ext: ProcessorExternalities + BackendExternalities + 'static,
    <E::Ext as Externalities>::UnrecoverableError: BackendSyscallError,
    EP: WasmEntryPoint,
{
    let program = Program::new(program_id.unwrap_or_default(), instrumented_code);
    let mut pages_initial_data: BTreeMap<GearPage, PageBuf> =
        pages_initial_data.unwrap_or_default();
    let static_pages = program.static_pages();
    let allocations = allocations.unwrap_or_else(|| program.allocations().clone());

    let memory_size = if let Some(page) = allocations.iter().next_back() {
        page.inc()
            .map_err(|err| err.to_string())
            .expect("Memory size overflow, impossible")
    } else if static_pages != WasmPage::from(0) {
        static_pages
    } else {
        0.into()
    };

    let context = ProcessorContext {
        gas_counter: GasCounter::new(gas_limit),
        gas_allowance_counter: GasAllowanceCounter::new(gas_limit),
        gas_reserver: GasReserver::new(&Default::default(), Default::default(), Default::default()),
        value_counter: ValueCounter::new(Default::default()),
        allocations_context: AllocationsContext::new(allocations, static_pages, 512.into()),
        message_context: MessageContext::new(
            IncomingDispatch::new(
                DispatchKind::Handle,
                IncomingMessage::new(
                    Default::default(),
                    Default::default(),
                    payload
                        .try_into()
                        .map_err(|e| format!("Failed to create payload: {e:?}"))?,
                    gas_limit,
                    Default::default(),
                    Default::default(),
                ),
                None,
            ),
            program.id(),
            ContextSettings::new(0, 0, 0, 0, 0, 0),
        ),
        block_info,
        max_pages: 512.into(),
        page_costs: Default::default(),
        existential_deposit: Default::default(),
        program_id: program.id(),
        program_candidates_data: Default::default(),
        program_rents: Default::default(),
        host_fn_weights: Default::default(),
        forbidden_funcs: Default::default(),
        mailbox_threshold: Default::default(),
        waitlist_cost: Default::default(),
        dispatch_hold_cost: Default::default(),
        reserve_for: Default::default(),
        reservation: Default::default(),
        random_data: Default::default(),
        system_reservation: Default::default(),
        rent_cost: Default::default(),
    };

    let lazy_pages_weights = context.page_costs.lazy_pages_weights();

    // Creating externalities.
    let ext = E::Ext::new(context);

    // Execute program in backend env.
    let f = || {
        let env = E::new(
            ext,
            program.raw_code(),
            function,
            program.code().exports().clone(),
            memory_size,
        )
        .map_err(EnvironmentError::from_infallible)?;
        env.execute(|memory, stack_end, globals_config| {
            prepare_memory::<E::Ext, E::Memory>(
                memory,
                program.id(),
                &mut pages_initial_data,
                static_pages,
                stack_end,
                globals_config,
                lazy_pages_weights,
            )
        })
    };

    let (termination, memory, ext) = match f() {
        Ok(report) => {
            let BackendReport {
                termination_reason,
                memory_wrap,
                ext,
            } = report;

            let termination_reason = match termination_reason {
                TerminationReason::Actor(reason) => reason,
                TerminationReason::System(reason) => {
                    return Err(format!("Backend error: {reason}"))
                }
            };

            (termination_reason, memory_wrap, ext)
        }
        Err(e) => return Err(format!("Backend error: {e}")),
    };

    match termination {
        ActorTerminationReason::Exit(_)
        | ActorTerminationReason::Leave
        | ActorTerminationReason::Wait(_, _) => {
            return Err("Execution has incorrect termination reason".into())
        }
        ActorTerminationReason::Success => (),
        ActorTerminationReason::Trap(explanation) => {
            return Err(format!(
                "Program execution failed with error: {explanation}"
            ));
        }
        ActorTerminationReason::GasAllowanceExceeded => return Err("Unreachable".into()),
    };

    let info = ext
        .into_ext_info(&memory)
        .map_err(|e| format!("Backend postprocessing error: {e:?}"))?;

    for (dispatch, _, _) in info.generated_dispatches {
        if matches!(dispatch.kind(), DispatchKind::Reply) {
            return Ok(dispatch.payload_bytes().to_vec());
        }
    }

    Err("Reply not found".into())
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::vec::Vec;
    use gear_backend_common::lazy_pages::Status;
    use gear_core::{
        memory::PageBufInner,
        pages::{PageNumber, WasmPage},
    };

    struct TestExt;
    struct LazyTestExt;

    impl ProcessorExternalities for TestExt {
        const LAZY_PAGES_ENABLED: bool = false;
        fn new(_context: ProcessorContext) -> Self {
            Self
        }

        fn lazy_pages_init_for_program(
            _mem: &mut impl Memory,
            _prog_id: ProgramId,
            _stack_end: Option<WasmPage>,
            _globals_config: GlobalsAccessConfig,
            _lazy_pages_weights: LazyPagesWeights,
        ) {
        }

        fn lazy_pages_post_execution_actions(_mem: &mut impl Memory) {}
        fn lazy_pages_status() -> Status {
            Status::Normal
        }
    }

    impl ProcessorExternalities for LazyTestExt {
        const LAZY_PAGES_ENABLED: bool = true;

        fn new(_context: ProcessorContext) -> Self {
            Self
        }

        fn lazy_pages_init_for_program(
            _mem: &mut impl Memory,
            _prog_id: ProgramId,
            _stack_end: Option<WasmPage>,
            _globals_config: GlobalsAccessConfig,
            _lazy_pages_weights: LazyPagesWeights,
        ) {
        }

        fn lazy_pages_post_execution_actions(_mem: &mut impl Memory) {}
        fn lazy_pages_status() -> Status {
            Status::Normal
        }
    }

    fn prepare_pages_and_allocs() -> (Vec<GearPage>, BTreeSet<WasmPage>) {
        let data = [0u16, 1, 2, 8, 18, 25, 27, 28, 93, 146, 240, 518];
        let pages = data.map(Into::into);
        (pages.to_vec(), pages.map(|p| p.to_page()).into())
    }

    fn prepare_pages() -> BTreeMap<GearPage, PageBuf> {
        let mut pages = BTreeMap::new();
        for i in 0..=255 {
            let buffer = PageBufInner::filled_with(i);
            pages.insert((i as u16).into(), PageBuf::from_inner(buffer));
        }
        pages
    }

    #[test]
    fn check_memory_insufficient() {
        let res = check_memory(&[].into(), [].iter(), 8.into(), 4.into());
        assert_eq!(res, Err(SystemPrepareMemoryError::InsufficientMemorySize));
    }

    #[test]
    fn check_memory_not_allocated() {
        let (pages, mut allocs) = prepare_pages_and_allocs();
        let last = *allocs.iter().last().unwrap();
        allocs.remove(&last);
        let res = check_memory(&allocs, pages.iter(), 2.into(), 4.into());
        assert_eq!(
            res,
            Err(SystemPrepareMemoryError::PageIsNotAllocated(
                *pages.last().unwrap()
            ))
        );
    }

    #[test]
    fn check_memory_ok() {
        let (pages, allocs) = prepare_pages_and_allocs();
        check_memory(&allocs, pages.iter(), 4.into(), 8.into()).unwrap();
    }

    #[test]
    fn lazy_pages_to_update() {
        let new_pages = prepare_pages();
        let res =
            get_pages_to_be_updated::<LazyTestExt>(Default::default(), new_pages.clone(), 0.into());
        // All touched pages are to be updated in lazy mode
        assert_eq!(res, new_pages);
    }

    #[test]
    fn no_pages_to_update() {
        let old_pages = prepare_pages();
        let mut new_pages = old_pages.clone();
        let static_pages = 4;
        let res =
            get_pages_to_be_updated::<TestExt>(old_pages, new_pages.clone(), static_pages.into());
        assert_eq!(res, Default::default());

        // Change static pages
        for i in 0..static_pages {
            let buffer = PageBufInner::filled_with(42);
            new_pages.insert(i.into(), PageBuf::from_inner(buffer));
        }
        // Do not include non-static pages
        let new_pages = new_pages
            .into_iter()
            .take(WasmPage::from(static_pages).to_page::<GearPage>().raw() as _)
            .collect();
        let res =
            get_pages_to_be_updated::<TestExt>(Default::default(), new_pages, static_pages.into());
        assert_eq!(res, Default::default());
    }

    #[test]
    fn pages_to_update() {
        let old_pages = prepare_pages();
        let mut new_pages = old_pages.clone();

        let page_with_zero_data = WasmPage::from(30).to_page();
        let changes: BTreeMap<GearPage, PageBuf> = [
            (
                WasmPage::from(1).to_page(),
                PageBuf::from_inner(PageBufInner::filled_with(42u8)),
            ),
            (
                WasmPage::from(5).to_page(),
                PageBuf::from_inner(PageBufInner::filled_with(84u8)),
            ),
            (page_with_zero_data, PageBuf::new_zeroed()),
        ]
        .into_iter()
        .collect();
        new_pages.extend(changes.clone().into_iter());

        // Change pages
        let static_pages = 4.into();
        let res = get_pages_to_be_updated::<TestExt>(old_pages, new_pages.clone(), static_pages);
        assert_eq!(res, changes);

        // There was no any old page
        let res =
            get_pages_to_be_updated::<TestExt>(Default::default(), new_pages.clone(), static_pages);

        // The result is all pages except the static ones
        for page in static_pages.to_page::<GearPage>().iter_from_zero() {
            new_pages.remove(&page);
        }

        // Remove page with zero data, because it must not be updated.
        new_pages.remove(&page_with_zero_data);

        assert_eq!(res, new_pages);
    }
}