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