Skip to content

Commit

Permalink
InstrumentedCode is made a member of the Code, changed method names f…
Browse files Browse the repository at this point in the history
…or better consistency
  • Loading branch information
Lazark0x committed Oct 1, 2024
1 parent e250089 commit 1d9f69f
Show file tree
Hide file tree
Showing 17 changed files with 330 additions and 272 deletions.
18 changes: 9 additions & 9 deletions common/src/code_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,28 @@ pub trait CodeStorage {

fn add_code(code_and_id: CodeAndId, metadata: CodeMetadata) -> Result<(), Error> {
let (code, code_id) = code_and_id.into_parts();
let (code, original_code) = code.into_parts();
let (instrumented_code, original_code) = code.into_parts();

Self::InstrumentedCodeStorage::mutate(code_id, |maybe| {
if maybe.is_some() {
return Err(CodeStorageError::DuplicateItem);
}

Self::InstrumentedLenStorage::insert(code_id, code.code().len() as u32);
Self::InstrumentedLenStorage::insert(code_id, instrumented_code.code().len() as u32);
Self::OriginalCodeStorage::insert(code_id, original_code);
Self::MetadataStorage::insert(code_id, metadata);

*maybe = Some(code);
*maybe = Some(instrumented_code);
Ok(())
})
}

/// Update the corresponding code in the storage.
fn update_code(code_and_id: InstrumentedCodeAndId) {
let (code, code_id) = code_and_id.into_parts();
fn update_instrumented_code(code_and_id: InstrumentedCodeAndId) {
let (instrumented_code, code_id) = code_and_id.into_parts();

Self::InstrumentedLenStorage::insert(code_id, code.code().len() as u32);
Self::InstrumentedCodeStorage::insert(code_id, code);
Self::InstrumentedLenStorage::insert(code_id, instrumented_code.code().len() as u32);
Self::InstrumentedCodeStorage::insert(code_id, instrumented_code);
}

fn exists(code_id: CodeId) -> bool {
Expand All @@ -89,11 +89,11 @@ pub trait CodeStorage {
})
}

fn get_code(code_id: CodeId) -> Option<InstrumentedCode> {
fn get_instrumented_code(code_id: CodeId) -> Option<InstrumentedCode> {
Self::InstrumentedCodeStorage::get(&code_id)
}

fn get_code_len(code_id: CodeId) -> Option<u32> {
fn get_instrumented_code_len(code_id: CodeId) -> Option<u32> {
Self::InstrumentedLenStorage::get(&code_id)
}

Expand Down
12 changes: 6 additions & 6 deletions core-processor/src/precharge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,27 +422,27 @@ pub fn precharge_for_module_instantiation(

charger.charge_gas_for_section_instantiation(
SectionName::Function,
section_sizes.code_section.into(),
section_sizes.code_section().into(),
)?;
charger.charge_gas_for_section_instantiation(
SectionName::Data,
section_sizes.data_section.into(),
section_sizes.data_section().into(),
)?;
charger.charge_gas_for_section_instantiation(
SectionName::Global,
section_sizes.global_section.into(),
section_sizes.global_section().into(),
)?;
charger.charge_gas_for_section_instantiation(
SectionName::Table,
section_sizes.table_section.into(),
section_sizes.table_section().into(),
)?;
charger.charge_gas_for_section_instantiation(
SectionName::Element,
section_sizes.element_section.into(),
section_sizes.element_section().into(),
)?;
charger.charge_gas_for_section_instantiation(
SectionName::Type,
section_sizes.type_section.into(),
section_sizes.type_section().into(),
)?;

Ok(memory_size)
Expand Down
60 changes: 60 additions & 0 deletions core/src/code/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/// Configuration for `Code::try_new_mock_`.
/// By default all checks enabled.
pub struct TryNewCodeConfig {
/// Instrumentation version
pub version: u32,
/// Stack height limit
pub stack_height: Option<u32>,
/// Limit of data section amount
pub data_segments_amount_limit: Option<u32>,
/// Limit on the number of tables.
pub table_number_limit: Option<u32>,
/// Export `STACK_HEIGHT_EXPORT_NAME` global
pub export_stack_height: bool,
/// Check exports (wasm contains init or handle exports)
pub check_exports: bool,
/// Check imports (check that all imports are valid syscalls with correct signature)
pub check_imports: bool,
/// Check and canonize stack end
pub check_and_canonize_stack_end: bool,
/// Check mutable global exports
pub check_mut_global_exports: bool,
/// Check start section (not allowed for programs)
pub check_start_section: bool,
/// Check data section
pub check_data_section: bool,
/// Check table section
pub check_table_section: bool,
/// Make wasmparser validation
pub make_validation: bool,
}

impl TryNewCodeConfig {
/// New default config without exports checks.
pub fn new_no_exports_check() -> Self {
Self {
check_exports: false,
..Default::default()
}
}
}

impl Default for TryNewCodeConfig {
fn default() -> Self {
Self {
version: 1,
stack_height: None,
data_segments_amount_limit: None,
table_number_limit: None,
export_stack_height: false,
check_exports: true,
check_imports: true,
check_and_canonize_stack_end: true,
check_mut_global_exports: true,
check_start_section: true,
check_data_section: true,
check_table_section: true,
make_validation: true,
}
}
}
143 changes: 112 additions & 31 deletions core/src/code/instrumented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,126 @@ use scale_info::{
#[derive(Clone, Debug, PartialEq, Eq, Decode, Encode, TypeInfo)]
pub struct InstantiatedSectionSizes {
/// Code section size in bytes.
pub code_section: u32,
code_section: u32,
/// Data section size in bytes based on the number of heuristic memory pages
/// used during data section instantiation (see `GENERIC_OS_PAGE_SIZE`).
pub data_section: u32,
data_section: u32,
/// Global section size in bytes.
pub global_section: u32,
global_section: u32,
/// Table section size in bytes.
pub table_section: u32,
table_section: u32,
/// Element section size in bytes.
pub element_section: u32,
element_section: u32,
/// Type section size in bytes.
pub type_section: u32,
type_section: u32,
}

impl InstantiatedSectionSizes {
/// Returns an empty instance of the section sizes.
pub const EMPTY: Self = Self {
code_section: 0,
data_section: 0,
global_section: 0,
table_section: 0,
element_section: 0,
type_section: 0,
};
/// Creates a new instance of the section sizes.
pub fn new(
code_section: u32,
data_section: u32,
global_section: u32,
table_section: u32,
element_section: u32,
type_section: u32,
) -> Self {
Self {
code_section,
data_section,
global_section,
table_section,
element_section,
type_section,
}
}

/// Creates an empty instance of the section sizes.
///
/// # Safety
/// This method is unsafe because it is used for testing purposes only.
pub const unsafe fn zero() -> Self {
Self {
code_section: 0,
data_section: 0,
global_section: 0,
table_section: 0,
element_section: 0,
type_section: 0,
}
}

/// Returns the code section size in bytes.
pub fn code_section(&self) -> u32 {
self.code_section
}

/// Returns the data section size in bytes.
pub fn data_section(&self) -> u32 {
self.data_section
}

/// Returns the global section size in bytes.
pub fn global_section(&self) -> u32 {
self.global_section
}

/// Returns the table section size in bytes.
pub fn table_section(&self) -> u32 {
self.table_section
}

/// Returns the element section size in bytes.
pub fn element_section(&self) -> u32 {
self.element_section
}

/// Returns the type section size in bytes.
pub fn type_section(&self) -> u32 {
self.type_section
}
}

/// The newtype contains the instrumented code and the corresponding id (hash).
#[derive(Clone, Debug, Decode, Encode, TypeInfo)]
#[derive(Clone, Debug, Decode, Encode, TypeInfo, PartialEq, Eq)]
pub struct InstrumentedCode {
pub(crate) code: Vec<u8>,
pub(crate) original_code_len: u32,
pub(crate) exports: BTreeSet<DispatchKind>,
pub(crate) static_pages: WasmPagesAmount,
pub(crate) stack_end: Option<WasmPage>,
pub(crate) instantiated_section_sizes: InstantiatedSectionSizes,
pub(crate) version: u32,
/// Code instrumented with the latest schedule.
code: Vec<u8>,
/// Original code length.
original_code_len: u32,
/// Exports of the wasm module.
exports: BTreeSet<DispatchKind>,
/// Static pages count from memory import.
static_pages: WasmPagesAmount,
/// Stack end page.
stack_end: Option<WasmPage>,
/// Instruction weights version.
instruction_weights_version: u32,
/// Instantiated section sizes used for charging during module instantiation.
instantiated_section_sizes: InstantiatedSectionSizes,
}

impl InstrumentedCode {
pub(crate) fn new(
code: Vec<u8>,
original_code_len: u32,
exports: BTreeSet<DispatchKind>,
static_pages: WasmPagesAmount,
stack_end: Option<WasmPage>,
instantiated_section_sizes: InstantiatedSectionSizes,
instruction_weights_version: u32,
) -> Self {
Self {
code,
original_code_len,
exports,
static_pages,
stack_end,
instantiated_section_sizes,
instruction_weights_version,
}
}

/// Creates a new instance of the instrumented code.
///
/// # Safety
Expand All @@ -87,7 +168,7 @@ impl InstrumentedCode {
static_pages: WasmPagesAmount,
stack_end: Option<WasmPage>,
instantiated_section_sizes: InstantiatedSectionSizes,
version: u32,
instruction_weights_version: u32,
) -> Self {
Self {
code,
Expand All @@ -96,7 +177,7 @@ impl InstrumentedCode {
static_pages,
stack_end,
instantiated_section_sizes,
version,
instruction_weights_version,
}
}

Expand All @@ -105,16 +186,11 @@ impl InstrumentedCode {
&self.code
}

/// Returns the length of the original binary code.
/// Returns original code length.
pub fn original_code_len(&self) -> u32 {
self.original_code_len
}

/// Returns instruction weights version.
pub fn instruction_weights_version(&self) -> u32 {
self.version
}

/// Returns wasm module exports.
pub fn exports(&self) -> &BTreeSet<DispatchKind> {
&self.exports
Expand All @@ -135,6 +211,11 @@ impl InstrumentedCode {
&self.instantiated_section_sizes
}

/// Returns instruction weights version.
pub fn instruction_weights_version(&self) -> u32 {
self.instruction_weights_version
}

/// Consumes the instance and returns the instrumented code.
pub fn into_code(self) -> Vec<u8> {
self.code
Expand Down
Loading

0 comments on commit 1d9f69f

Please sign in to comment.