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

// Copyright (C) 2021-2024 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/>.

//! Module for programs.

use crate::{code::InstrumentedCode, ids::ProgramId, pages::WasmPage};
use alloc::collections::BTreeSet;
use scale_info::{
    scale::{Decode, Encode},
    TypeInfo,
};

/// Struct defines infix of memory pages storage.
#[derive(Clone, Copy, Debug, Default, Decode, Encode, PartialEq, Eq, TypeInfo)]
pub struct MemoryInfix(u32);

impl MemoryInfix {
    /// Constructing function from u32 number.
    pub const fn new(value: u32) -> Self {
        Self(value)
    }

    /// Return inner u32 value.
    pub fn inner(&self) -> u32 {
        self.0
    }
}

/// Program.
#[derive(Clone, Debug)]
pub struct Program {
    id: ProgramId,
    memory_infix: MemoryInfix,
    code: InstrumentedCode,
    /// Wasm pages allocated by program.
    allocations: BTreeSet<WasmPage>,
}

impl Program {
    /// New program with specific `id` and `code`.
    pub fn new(id: ProgramId, memory_infix: MemoryInfix, code: InstrumentedCode) -> Self {
        Program {
            id,
            memory_infix,
            code,
            allocations: Default::default(),
        }
    }

    /// New program from stored data
    pub fn from_parts(
        id: ProgramId,
        memory_infix: MemoryInfix,
        code: InstrumentedCode,
        allocations: BTreeSet<WasmPage>,
    ) -> Self {
        Self {
            id,
            memory_infix,
            code,
            allocations,
        }
    }

    /// Reference to [`InstrumentedCode`] of this program.
    pub fn code(&self) -> &InstrumentedCode {
        &self.code
    }

    /// Reference to raw binary code of this program.
    pub fn code_bytes(&self) -> &[u8] {
        self.code.code()
    }

    /// Get the [`ProgramId`] of this program.
    pub fn id(&self) -> ProgramId {
        self.id
    }

    /// Get the [`MemoryInfix`] of this program.
    pub fn memory_infix(&self) -> MemoryInfix {
        self.memory_infix
    }

    /// Get initial memory size for this program.
    pub fn static_pages(&self) -> WasmPage {
        self.code.static_pages()
    }

    /// Get stack end page if exists.
    pub fn stack_end(&self) -> Option<WasmPage> {
        self.code.stack_end()
    }

    /// Get allocations as a set of page numbers.
    pub fn allocations(&self) -> &BTreeSet<WasmPage> {
        &self.allocations
    }

    /// Set allocations as a set of page numbers.
    pub fn set_allocations(&mut self, allocations: BTreeSet<WasmPage>) {
        self.allocations = allocations;
    }
}

#[cfg(test)]
mod tests {
    use super::Program;
    use crate::{code::Code, ids::ProgramId};
    use alloc::vec::Vec;
    use gear_wasm_instrument::gas_metering::CustomConstantCostRules;

    fn parse_wat(source: &str) -> Vec<u8> {
        let module_bytes = wabt::Wat2Wasm::new()
            .validate(false)
            .convert(source)
            .expect("failed to parse module")
            .as_ref()
            .to_vec();
        module_bytes
    }

    #[test]
    #[should_panic(expected = "Identifier must be 32 length")]
    /// Test that ProgramId's `from_slice(...)` constructor causes panic
    /// when the argument has the wrong length
    fn program_id_from_slice_error_implementation() {
        let bytes = "foobar";
        let _: ProgramId = bytes.as_bytes().into();
    }

    #[test]
    /// Test static pages.
    fn program_memory() {
        let wat = r#"
            (module
                (import "env" "gr_reply_to"  (func $gr_reply_to (param i32)))
                (import "env" "memory" (memory 2))
                (export "handle" (func $handle))
                (export "handle_reply" (func $handle))
                (export "init" (func $init))
                (func $handle
                    i32.const 65536
                    call $gr_reply_to
                )
                (func $handle_reply
                    i32.const 65536
                    call $gr_reply_to
                )
                (func $init)
            )"#;

        let binary: Vec<u8> = parse_wat(wat);

        let code = Code::try_new(binary, 1, |_| CustomConstantCostRules::default(), None).unwrap();
        let (code, _) = code.into_parts();
        let program = Program::new(ProgramId::from(1), Default::default(), code);

        // 2 static pages
        assert_eq!(program.static_pages(), 2.into());

        // Has no allocations because we do not set them in new
        assert_eq!(program.allocations().len(), 0);
    }
}