Skip to main content

gear_core/code/
instrumented.rs

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