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::code::Code;
22use alloc::vec::Vec;
23use scale_info::{
24    TypeInfo,
25    scale::{Decode, Encode},
26};
27
28/// Instantiated section sizes for charging during module instantiation.
29/// By "instantiated sections sizes" we mean the size of the section representation in the executor
30/// during module instantiation.
31#[derive(Clone, Debug, PartialEq, Eq, Decode, Encode, TypeInfo, Hash)]
32pub struct InstantiatedSectionSizes {
33    /// Code section size in bytes.
34    code_section: u32,
35    /// Data section size in bytes based on the number of heuristic memory pages
36    /// used during data section instantiation (see `GENERIC_OS_PAGE_SIZE`).
37    data_section: u32,
38    /// Global section size in bytes.
39    global_section: u32,
40    /// Table section size in bytes.
41    table_section: u32,
42    /// Element section size in bytes.
43    element_section: u32,
44    /// Type section size in bytes.
45    type_section: u32,
46}
47
48impl InstantiatedSectionSizes {
49    /// Creates a new instance of the section sizes.
50    pub fn new(
51        code_section: u32,
52        data_section: u32,
53        global_section: u32,
54        table_section: u32,
55        element_section: u32,
56        type_section: u32,
57    ) -> Self {
58        Self {
59            code_section,
60            data_section,
61            global_section,
62            table_section,
63            element_section,
64            type_section,
65        }
66    }
67
68    /// Returns the code section size in bytes.
69    pub fn code_section(&self) -> u32 {
70        self.code_section
71    }
72
73    /// Returns the data section size in bytes.
74    pub fn data_section(&self) -> u32 {
75        self.data_section
76    }
77
78    /// Returns the global section size in bytes.
79    pub fn global_section(&self) -> u32 {
80        self.global_section
81    }
82
83    /// Returns the table section size in bytes.
84    pub fn table_section(&self) -> u32 {
85        self.table_section
86    }
87
88    /// Returns the element section size in bytes.
89    pub fn element_section(&self) -> u32 {
90        self.element_section
91    }
92
93    /// Returns the type section size in bytes.
94    pub fn type_section(&self) -> u32 {
95        self.type_section
96    }
97}
98
99/// The newtype contains the instrumented code and the corresponding id (hash).
100#[derive(Clone, Debug, Decode, Encode, TypeInfo, PartialEq, Eq, Hash)]
101pub struct InstrumentedCode {
102    /// Code instrumented with the latest schedule.
103    bytes: Vec<u8>,
104    /// Instantiated section sizes used for charging during module instantiation.
105    instantiated_section_sizes: InstantiatedSectionSizes,
106}
107
108impl InstrumentedCode {
109    /// Creates a new instance of the instrumented code.
110    pub fn new(bytes: Vec<u8>, instantiated_section_sizes: InstantiatedSectionSizes) -> Self {
111        Self {
112            bytes,
113            instantiated_section_sizes,
114        }
115    }
116
117    /// Returns reference to the instrumented binary code.
118    pub fn bytes(&self) -> &[u8] {
119        &self.bytes
120    }
121
122    /// Returns instantiated section sizes used for charging during module instantiation.
123    pub fn instantiated_section_sizes(&self) -> &InstantiatedSectionSizes {
124        &self.instantiated_section_sizes
125    }
126
127    /// Consumes the instance and returns the instrumented code.
128    pub fn into_bytes(self) -> Vec<u8> {
129        self.bytes
130    }
131}
132
133impl From<Code> for InstrumentedCode {
134    fn from(code: Code) -> Self {
135        code.into_parts().1
136    }
137}