gear_core/code/
instrumented.rs

1// This file is part of Gear.
2
3// Copyright (C) 2024-2025 Gear Technologies Inc.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Module for instrumented code.
20
21use crate::{
22    code::{Code, CodeAndId},
23    ids::CodeId,
24    message::DispatchKind,
25    pages::{WasmPage, WasmPagesAmount},
26};
27use alloc::{collections::BTreeSet, vec::Vec};
28use scale_info::{
29    scale::{Decode, Encode},
30    TypeInfo,
31};
32
33/// Instantiated section sizes for charging during module instantiation.
34/// By "instantiated sections sizes" we mean the size of the section representation in the executor
35/// during module instantiation.
36#[derive(Clone, Debug, PartialEq, Eq, Decode, Encode, TypeInfo)]
37pub struct InstantiatedSectionSizes {
38    /// Code section size in bytes.
39    pub code_section: u32,
40    /// Data section size in bytes based on the number of heuristic memory pages
41    /// used during data section instantiation (see `GENERIC_OS_PAGE_SIZE`).
42    pub data_section: u32,
43    /// Global section size in bytes.
44    pub global_section: u32,
45    /// Table section size in bytes.
46    pub table_section: u32,
47    /// Element section size in bytes.
48    pub element_section: u32,
49    /// Type section size in bytes.
50    pub type_section: u32,
51}
52
53impl InstantiatedSectionSizes {
54    /// Returns an empty instance of the section sizes.
55    pub const EMPTY: Self = Self {
56        code_section: 0,
57        data_section: 0,
58        global_section: 0,
59        table_section: 0,
60        element_section: 0,
61        type_section: 0,
62    };
63}
64
65/// The newtype contains the instrumented code and the corresponding id (hash).
66#[derive(Clone, Debug, Decode, Encode, TypeInfo)]
67pub struct InstrumentedCode {
68    pub(crate) code: Vec<u8>,
69    pub(crate) original_code_len: u32,
70    pub(crate) exports: BTreeSet<DispatchKind>,
71    pub(crate) static_pages: WasmPagesAmount,
72    pub(crate) stack_end: Option<WasmPage>,
73    pub(crate) instantiated_section_sizes: InstantiatedSectionSizes,
74    pub(crate) version: u32,
75}
76
77impl InstrumentedCode {
78    /// Creates a new instance of the instrumented code.
79    ///
80    /// # Safety
81    /// The caller must ensure that the `code` is a valid wasm binary,
82    /// and other parameters are valid and suitable for the wasm binary.
83    pub unsafe fn new_unchecked(
84        code: Vec<u8>,
85        original_code_len: u32,
86        exports: BTreeSet<DispatchKind>,
87        static_pages: WasmPagesAmount,
88        stack_end: Option<WasmPage>,
89        instantiated_section_sizes: InstantiatedSectionSizes,
90        version: u32,
91    ) -> Self {
92        Self {
93            code,
94            original_code_len,
95            exports,
96            static_pages,
97            stack_end,
98            instantiated_section_sizes,
99            version,
100        }
101    }
102
103    /// Returns reference to the instrumented binary code.
104    pub fn code(&self) -> &[u8] {
105        &self.code
106    }
107
108    /// Returns the length of the original binary code.
109    pub fn original_code_len(&self) -> u32 {
110        self.original_code_len
111    }
112
113    /// Returns instruction weights version.
114    pub fn instruction_weights_version(&self) -> u32 {
115        self.version
116    }
117
118    /// Returns wasm module exports.
119    pub fn exports(&self) -> &BTreeSet<DispatchKind> {
120        &self.exports
121    }
122
123    /// Returns initial memory size from memory import.
124    pub fn static_pages(&self) -> WasmPagesAmount {
125        self.static_pages
126    }
127
128    /// Returns stack end page if exists.
129    pub fn stack_end(&self) -> Option<WasmPage> {
130        self.stack_end
131    }
132
133    /// Returns instantiated section sizes used for charging during module instantiation.
134    pub fn instantiated_section_sizes(&self) -> &InstantiatedSectionSizes {
135        &self.instantiated_section_sizes
136    }
137
138    /// Consumes the instance and returns the instrumented code.
139    pub fn into_code(self) -> Vec<u8> {
140        self.code
141    }
142}
143
144impl From<Code> for InstrumentedCode {
145    fn from(code: Code) -> Self {
146        code.into_parts().0
147    }
148}
149
150/// The newtype contains the instrumented code and the corresponding id (hash).
151#[derive(Clone, Debug)]
152pub struct InstrumentedCodeAndId {
153    code: InstrumentedCode,
154    code_id: CodeId,
155}
156
157impl InstrumentedCodeAndId {
158    /// Returns reference to the instrumented code.
159    pub fn code(&self) -> &InstrumentedCode {
160        &self.code
161    }
162
163    /// Returns corresponding id (hash) for the code.
164    pub fn code_id(&self) -> CodeId {
165        self.code_id
166    }
167
168    /// Consumes the instance and returns the instrumented code.
169    pub fn into_parts(self) -> (InstrumentedCode, CodeId) {
170        (self.code, self.code_id)
171    }
172}
173
174impl From<CodeAndId> for InstrumentedCodeAndId {
175    fn from(code_and_id: CodeAndId) -> Self {
176        let (code, code_id) = code_and_id.into_parts();
177        let (code, _) = code.into_parts();
178        Self { code, code_id }
179    }
180}