Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wasm-builder): Check all gear requirements for code at compile time #3649

Merged
merged 21 commits into from
Jan 26, 2024
Merged
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
9 changes: 7 additions & 2 deletions core/src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,19 @@ fn check_code(module: &Module, config: &TryNewCodeConfig) -> Result<(), CodeErro
}

if config.check_imports {
let syscalls = SyscallName::instrumentable_map();
let syscalls = SyscallName::all_map();
StackOverflowExcept1on marked this conversation as resolved.
Show resolved Hide resolved
for import in imports {
if let External::Function(i) = import.external() {
let Type::Function(types) = &types[*i as usize];
// We can likely improve this by adding some helper function in SyscallName
let syscall = syscalls
.get(import.field())
.ok_or(CodeError::UnknownImport)?;

if syscall == SyscallName::SystemBreak {
continue;
}

let signature = syscall.signature();

let params = signature
Expand Down Expand Up @@ -416,7 +421,7 @@ impl Default for TryNewCodeConfig {
stack_height: None,
export_stack_height: false,
check_exports: true,
check_imports: true,
check_imports: false,
check_and_canonize_stack_end: true,
check_mut_global_exports: true,
check_start_section: true,
Expand Down
9 changes: 6 additions & 3 deletions utils/wasm-builder/src/optimize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,14 @@ impl Optimizer {
.map_err(BuilderError::CodeCheckFailed)?,
// validate wasm code
// see `pallet_gear::pallet::Pallet::upload_program(...)`
OptType::Opt => Code::try_new(
OptType::Opt => Code::try_new_mock_with_rules(
original_code,
1,
|_| CustomConstantCostRules::default(),
None,
TryNewCodeConfig {
version: 1,
check_imports: true,
..Default::default()
},
)
.map(|_| ())
.map_err(BuilderError::CodeCheckFailed)?,
Expand Down
16 changes: 8 additions & 8 deletions utils/wasm-instrument/src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ impl SyscallName {
Self::all().count()
}

/// Returns map of all syscall string values to syscall names.
pub fn all_map() -> BTreeMap<String, SyscallName> {
Self::all()
.into_iter()
.map(|n| (n.to_str().to_string(), n))
.collect()
}

/// Returns list of all syscall names (actually supported by this module syscalls).
pub fn instrumentable() -> BTreeSet<Self> {
[
Expand Down Expand Up @@ -242,14 +250,6 @@ impl SyscallName {
.into()
}

/// Returns map of all syscall string values to syscall names (actually supported by this module syscalls).
pub fn instrumentable_map() -> BTreeMap<String, SyscallName> {
Self::instrumentable()
.into_iter()
.map(|n| (n.to_str().to_string(), n))
.collect()
}

/// Returns signature for syscall by name.
pub fn signature(self) -> SyscallSignature {
use RegularParamType::*;
Expand Down
Loading