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

refactor(backend): refine approach of funcs forbidding #4409

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
14 changes: 10 additions & 4 deletions core-backend/src/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,18 +225,22 @@ impl<F> RawSyscall<F> {
}
}

impl<T, F, Caller> Syscall<Caller, T> for RawSyscall<F>
impl<T, F, Caller, Ext> Syscall<Caller, T> for RawSyscall<F>
where
F: FnOnce(&mut CallerWrap<Caller>) -> Result<(Gas, T), HostError>,
Caller: AsContextExt<State = HostState<Ext, BackendMemory<ExecutorMemory>>>,
Ext: BackendExternalities + 'static,
{
type Context = ();

fn execute(
self,
caller: &mut CallerWrap<Caller>,
(): Self::Context,
_syscall_name: SyscallName,
syscall_name: SyscallName,
) -> Result<(Gas, T), HostError> {
caller.check_func_forbiddenness(syscall_name)?;

(self.0)(caller)
}
}
Expand Down Expand Up @@ -291,7 +295,8 @@ where
) -> Result<(Gas, ()), HostError> {
let Self { token, f, .. } = self;
let FallibleSyscallContext { gas, res_ptr } = context;
caller.run_fallible::<T, _, E>(gas, res_ptr, token, syscall_name, f)
caller.check_func_forbiddenness(syscall_name)?;
caller.run_fallible::<T, _, E>(gas, res_ptr, token, f)
}
}

Expand Down Expand Up @@ -336,7 +341,8 @@ where
) -> Result<(Gas, T), HostError> {
let Self { token, f } = self;
let InfallibleSyscallContext { gas } = ctx;
caller.run_any::<T, _>(gas, token, syscall_name, f)
caller.check_func_forbiddenness(syscall_name)?;
caller.run_any::<T, _>(gas, token, f)
}
}

Expand Down
7 changes: 3 additions & 4 deletions core-backend/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use gear_core::{
gas::{ChargeError, CounterType, CountersOwner, GasAmount, GasCounter, GasLeft},
ids::{MessageId, ProgramId, ReservationId},
memory::{Memory, MemoryInterval},
message::{HandlePacket, InitPacket, ReplyPacket},
message::{DispatchKind, HandlePacket, InitPacket, ReplyPacket},
pages::WasmPage,
};
use gear_core_errors::{ReplyCode, SignalCode};
Expand Down Expand Up @@ -74,7 +74,6 @@ pub struct MockExt {
reads: Vec<MemoryInterval>,
writes: Vec<MemoryInterval>,
_forbidden_funcs: BTreeSet<SyscallName>,
_endpoint_forbidden_funcs: BTreeSet<SyscallName>,
}

impl MockExt {
Expand Down Expand Up @@ -243,8 +242,8 @@ impl Externalities for MockExt {
fn forbidden_funcs(&self) -> &BTreeSet<SyscallName> {
&self._forbidden_funcs
}
fn endpoint_forbidden_funcs(&self) -> &BTreeSet<SyscallName> {
&self._endpoint_forbidden_funcs
fn endpoint_dispatch_kind(&self) -> DispatchKind {
Default::default()
}
fn reserve_gas(
&mut self,
Expand Down
37 changes: 16 additions & 21 deletions core-backend/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,30 +79,12 @@ where
Ext: BackendExternalities + 'static,
{
#[track_caller]
pub fn run_any<U, F>(
&mut self,
gas: u64,
token: CostToken,
syscall_name: SyscallName,
f: F,
) -> Result<(u64, U), HostError>
pub fn run_any<U, F>(&mut self, gas: u64, token: CostToken, f: F) -> Result<(u64, U), HostError>
where
F: FnOnce(&mut Self) -> Result<U, UndefinedTerminationReason>,
{
self.state_mut().ext.decrease_current_counter_to(gas);

if self.ext_mut().forbidden_funcs().contains(&syscall_name)
|| self
.ext_mut()
.endpoint_forbidden_funcs()
.contains(&syscall_name)
{
self.set_termination_reason(
ActorTerminationReason::Trap(TrapExplanation::ForbiddenFunction).into(),
);
return Err(HostError);
}

let run = || {
self.state_mut().ext.charge_gas_for_token(token)?;
f(self)
Expand All @@ -122,7 +104,6 @@ where
gas: u64,
res_ptr: u32,
token: CostToken,
syscall_name: SyscallName,
f: F,
) -> Result<(u64, ()), HostError>
where
Expand All @@ -132,7 +113,6 @@ where
self.run_any(
gas,
token,
syscall_name,
|ctx: &mut Self| -> Result<_, UndefinedTerminationReason> {
let res = f(ctx);
let res = ctx.process_fallible_func_result(res)?;
Expand Down Expand Up @@ -184,4 +164,19 @@ where
},
}
}

pub fn check_func_forbiddenness(&mut self, syscall_name: SyscallName) -> Result<(), HostError> {
let endpoint_forbidden_funcs = self.ext_mut().endpoint_dispatch_kind().forbidden_funcs();
breathx marked this conversation as resolved.
Show resolved Hide resolved

if self.ext_mut().forbidden_funcs().contains(&syscall_name)
|| endpoint_forbidden_funcs.contains(&syscall_name)
{
self.set_termination_reason(
ActorTerminationReason::Trap(TrapExplanation::ForbiddenFunction).into(),
);
return Err(HostError);
}

Ok(())
}
}
12 changes: 2 additions & 10 deletions core-processor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ where
);
let value_counter = ValueCounter::new(value_available);

let endpoint_forbidden_funcs = kind.forbidden_funcs();

let context = ProcessorContext {
gas_counter,
gas_allowance_counter,
Expand All @@ -130,13 +128,13 @@ where
program_id: program.id,
program_candidates_data: Default::default(),
forbidden_funcs: settings.forbidden_funcs,
endpoint_dispatch_kind: kind,
breathx marked this conversation as resolved.
Show resolved Hide resolved
reserve_for: settings.reserve_for,
random_data: settings.random_data,
gas_multiplier: settings.gas_multiplier,
existential_deposit: settings.existential_deposit,
mailbox_threshold: settings.mailbox_threshold,
costs: settings.ext_costs,
endpoint_forbidden_funcs,
};

// Creating externalities.
Expand Down Expand Up @@ -310,12 +308,6 @@ where
)
.ok_or("Incorrect message store context: out of outgoing bytes limit")?;

let endpoint_forbidden_funcs = function
.try_into_kind()
.as_mut()
.map(|kind| kind.forbidden_funcs())
.unwrap_or_default();

let context = ProcessorContext {
gas_counter: GasCounter::new(gas_limit),
gas_allowance_counter: GasAllowanceCounter::new(gas_limit),
Expand All @@ -342,7 +334,7 @@ where
existential_deposit: Default::default(),
mailbox_threshold: Default::default(),
costs: Default::default(),
endpoint_forbidden_funcs,
endpoint_dispatch_kind: function.try_into_kind().unwrap_or_default(),
};

// Creating externalities.
Expand Down
10 changes: 5 additions & 5 deletions core-processor/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ pub struct ProcessorContext {
pub program_candidates_data: BTreeMap<CodeId, Vec<(MessageId, ProgramId)>>,
/// Functions forbidden to be called.
pub forbidden_funcs: BTreeSet<SyscallName>,
/// Functions forbidden to be called with this endpoint
pub endpoint_forbidden_funcs: BTreeSet<SyscallName>,
/// Endpoint dispatch kind.
pub endpoint_dispatch_kind: DispatchKind,
/// Reserve for parameter of scheduling.
pub reserve_for: u32,
/// Output from Randomness.
Expand Down Expand Up @@ -139,7 +139,7 @@ impl ProcessorContext {
program_id: Default::default(),
program_candidates_data: Default::default(),
forbidden_funcs: Default::default(),
endpoint_forbidden_funcs: Default::default(),
endpoint_dispatch_kind: Default::default(),
reserve_for: 0,
random_data: ([0u8; 32].to_vec(), 0),
gas_multiplier: gsys::GasMultiplier::from_value_per_gas(1),
Expand Down Expand Up @@ -1425,8 +1425,8 @@ impl<LP: LazyPagesInterface> Externalities for Ext<LP> {
&self.context.forbidden_funcs
}

fn endpoint_forbidden_funcs(&self) -> &BTreeSet<SyscallName> {
&self.context.endpoint_forbidden_funcs
fn endpoint_dispatch_kind(&self) -> DispatchKind {
self.context.endpoint_dispatch_kind
}
}

Expand Down
6 changes: 3 additions & 3 deletions core/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
env_vars::EnvVars,
ids::{MessageId, ProgramId, ReservationId},
memory::Memory,
message::{HandlePacket, InitPacket, MessageContext, Payload, ReplyPacket},
message::{DispatchKind, HandlePacket, InitPacket, MessageContext, Payload, ReplyPacket},
pages::WasmPage,
};
use alloc::collections::BTreeSet;
Expand Down Expand Up @@ -392,6 +392,6 @@ pub trait Externalities {
/// Return the set of functions that are forbidden to be called.
fn forbidden_funcs(&self) -> &BTreeSet<SyscallName>;

/// Return the set of functions that are forbidden to be called in this endpoint.
fn endpoint_forbidden_funcs(&self) -> &BTreeSet<SyscallName>;
/// Return the current dispatch kind.
fn endpoint_dispatch_kind(&self) -> DispatchKind;
}
Loading