Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ gear-workspace-hack.workspace = true
[dev-dependencies]
tracing-subscriber.workspace = true
wat.workspace = true
wasm-encoder.workspace = true
proptest.workspace = true
rand = { workspace = true, features = ["std", "std_rng"] }
numerated = { workspace = true, features = ["mock"] }
Expand Down
69 changes: 68 additions & 1 deletion core/src/code/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ mod utils;
pub use errors::*;
pub use instrumented::*;
pub use metadata::*;
pub use utils::{ALLOWED_EXPORTS, MAX_WASM_PAGES_AMOUNT, REQUIRED_EXPORTS};
pub use utils::{
ALLOWED_EXPORTS, MAX_WASM_PAGES_AMOUNT, REQUIRED_EXPORTS, get_custom_section_data,
};

use utils::CodeTypeSectionSizes;

Expand Down Expand Up @@ -1245,4 +1247,69 @@ mod tests {
))
));
}

mod custom_section_tests {
use alloc::{vec, vec::Vec};
use crate::code::get_custom_section_data;

fn make_wasm_with_custom_section(name: &str, data: &[u8]) -> Vec<u8> {
let mut module = wasm_encoder::Module::new();
// Add a minimal type section so it's a valid module
let mut types = wasm_encoder::TypeSection::new();
types.ty().function(vec![], vec![]);
module.section(&types);
// Add the custom section
module.section(&wasm_encoder::CustomSection {
name: name.into(),
data: data.into(),
});
module.finish()
}

#[test]
fn section_found() {
let wasm = make_wasm_with_custom_section("sails:idl", b"hello idl");
let result = get_custom_section_data(&wasm, "sails:idl");
assert_eq!(result.unwrap().unwrap(), b"hello idl");
}

#[test]
fn section_not_found() {
let wasm = make_wasm_with_custom_section("other", b"data");
let result = get_custom_section_data(&wasm, "sails:idl");
assert_eq!(result.unwrap(), None);
}

#[test]
fn first_match_returned() {
let mut module = wasm_encoder::Module::new();
let mut types = wasm_encoder::TypeSection::new();
types.ty().function(vec![], vec![]);
module.section(&types);
module.section(&wasm_encoder::CustomSection {
name: "sails:idl".into(),
data: b"first".into(),
});
module.section(&wasm_encoder::CustomSection {
name: "sails:idl".into(),
data: b"second".into(),
});
let wasm = module.finish();

let result = get_custom_section_data(&wasm, "sails:idl");
assert_eq!(result.unwrap().unwrap(), b"first");
}

#[test]
fn invalid_wasm() {
let result = get_custom_section_data(b"not wasm at all", "sails:idl");
assert!(result.is_err());
}

#[test]
fn empty_input() {
let result = get_custom_section_data(b"", "sails:idl");
assert!(result.is_err());
}
}
}
29 changes: 29 additions & 0 deletions core/src/code/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,32 @@ pub fn get_code_type_sections_sizes(
type_section: type_section_size,
})
}

/// Extract data of a custom section by name from raw WASM bytes.
///
/// Returns `Ok(Some(data))` if the section is found, `Ok(None)` if the WASM
/// is valid but the section is not present, and `Err` if the WASM is malformed.
pub fn get_custom_section_data<'a>(
wasm: &'a [u8],
section_name: &str,
) -> Result<Option<&'a [u8]>, &'static str> {
let parser = wasmparser::Parser::new(0);
Comment thread
ukint-vs marked this conversation as resolved.
Outdated
let mut saw_version = false;

for payload in parser.parse_all(wasm) {
let payload = payload.map_err(|_| "invalid wasm binary")?;
match payload {
Payload::Version { .. } => saw_version = true,
Payload::CustomSection(section) if section.name() == section_name => {
return Ok(Some(section.data()));
}
_ => {}
}
}

if !saw_version {
return Err("invalid wasm binary");
}

Ok(None)
}
3 changes: 3 additions & 0 deletions node/service/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use sp_keystore::KeystorePtr;

mod gear_events;
mod runtime_info;
mod wasm_section;

/// Extra dependencies for BABE.
pub struct BabeDeps {
Expand Down Expand Up @@ -138,6 +139,7 @@ where
use pallet_gear_staking_rewards_rpc::{GearStakingRewards, GearStakingRewardsApiServer};
use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};
use runtime_info::{RuntimeInfoApi, RuntimeInfoServer};
use wasm_section::{WasmSectionApi, WasmSectionServer};
use sc_consensus_babe_rpc::{Babe, BabeApiServer};
use sc_consensus_grandpa_rpc::{Grandpa, GrandpaApiServer};
use sc_rpc::dev::{Dev, DevApiServer};
Expand Down Expand Up @@ -224,6 +226,7 @@ where
}

io.merge(RuntimeInfoApi::<C, Block, B>::new(client.clone()).into_rpc())?;
io.merge(WasmSectionApi::<C, Block, B>::new(client.clone()).into_rpc())?;

io.merge(GearStakingRewards::new(client.clone()).into_rpc())?;

Expand Down
121 changes: 121 additions & 0 deletions node/service/src/rpc/wasm_section.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// This file is part of Gear.

// Copyright (C) 2021-2025 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! RPC endpoint for reading custom sections from program WASM original code.

use jsonrpsee::{core::RpcResult, proc_macros::rpc, types::ErrorObjectOwned};
use sc_client_api::StorageProvider;
use sp_blockchain::HeaderBackend;
use sp_core::{Bytes, H256, twox_128};
use sp_runtime::traits::Block as BlockT;
use sp_storage::StorageKey;
use std::{marker::PhantomData, sync::Arc};

#[rpc(server)]
pub(crate) trait WasmSection<BlockHash> {
/// Read a custom section from the original WASM code stored on-chain.
///
/// This is commonly used to retrieve the Sails IDL embedded in the
/// `sails:idl` custom section. The returned bytes are raw section data;
/// for `sails:idl`, clients must parse the envelope (version + flags)
/// and decompress the payload.
///
/// Returns `null` if the code is not found or the section does not exist.
#[method(name = "gear_readWasmCustomSection")]
fn read_wasm_custom_section(
&self,
code_id: H256,
section_name: String,
at: Option<BlockHash>,
) -> RpcResult<Option<Bytes>>;
}

pub(crate) struct WasmSectionApi<C, Block, Backend> {
client: Arc<C>,
original_code_prefix: Vec<u8>,
_marker1: PhantomData<Block>,
_marker2: PhantomData<Backend>,
}

impl<C, Block, Backend> WasmSectionApi<C, Block, Backend> {
pub(crate) fn new(client: Arc<C>) -> Self {
let mut original_code_prefix = twox_128(b"GearProgram").to_vec();
Comment thread
ukint-vs marked this conversation as resolved.
Outdated
original_code_prefix.extend_from_slice(&twox_128(b"OriginalCodeStorage"));

Self {
client,
original_code_prefix,
_marker1: PhantomData,
_marker2: PhantomData,
}
}
}

impl<C, Block, Backend> WasmSectionServer<<Block as BlockT>::Hash>
for WasmSectionApi<C, Block, Backend>
where
C: HeaderBackend<Block> + StorageProvider<Block, Backend> + Send + Sync + 'static,
Block: BlockT,
Backend: sc_client_api::Backend<Block> + Send + Sync + 'static,
{
fn read_wasm_custom_section(
&self,
code_id: H256,
section_name: String,
at: Option<Block::Hash>,
) -> RpcResult<Option<Bytes>> {
let at = at.unwrap_or_else(|| self.client.info().best_hash);

// Construct storage key: prefix ++ Identity(code_id)
let mut storage_key = self.original_code_prefix.clone();
storage_key.extend_from_slice(code_id.as_bytes());
Comment thread
ukint-vs marked this conversation as resolved.
Outdated
Comment on lines +81 to +84
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move impl inside pallet gear, to use normally CodeStorage instead of this raw code


let wasm_data = self
.client
.storage(at, &StorageKey(storage_key))
.map_err(map_err_into_rpc_err)?;

let Some(wasm_data) = wasm_data else {
return Ok(None);
};

// The storage value is SCALE-encoded Vec<u8>, so we need to decode it.
let wasm_bytes: Vec<u8> =
parity_scale_codec::Decode::decode(&mut wasm_data.0.as_slice())
Comment thread
ukint-vs marked this conversation as resolved.
Outdated
.map_err(|e| rpc_err("Failed to decode stored WASM", Some(format!("{e:?}"))))?;

match gear_core::code::get_custom_section_data(&wasm_bytes, &section_name) {
Comment thread
ukint-vs marked this conversation as resolved.
Outdated
Ok(Some(data)) => Ok(Some(Bytes(data.to_vec()))),
Ok(None) => Ok(None),
Err(e) => Err(rpc_err(
"Failed to parse stored WASM",
Some(e.to_string()),
)),
}
}
}

fn map_err_into_rpc_err(err: impl std::fmt::Debug) -> ErrorObjectOwned {
rpc_err("WASM section read error", Some(format!("{err:?}")))
}

fn rpc_err(message: &str, data: Option<String>) -> ErrorObjectOwned {
use jsonrpsee::types::error::ErrorObject;

ErrorObject::owned(9000, message, data)
}