Skip to content

Commit

Permalink
refactor(core-errors)!: New sys-call error representation (#2839)
Browse files Browse the repository at this point in the history
  • Loading branch information
ark0f authored Jun 20, 2023
1 parent a118104 commit ba15f03
Show file tree
Hide file tree
Showing 34 changed files with 585 additions and 759 deletions.
12 changes: 6 additions & 6 deletions core-backend/codegen/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl HostFn {
}

let cost = self.meta.runtime_costs.clone();
let err_len = self.meta.err_len.clone();
let err = self.meta.err.clone();
let inner_block = self.item.block.clone();
let inputs = self.build_inputs();

Expand All @@ -129,7 +129,7 @@ impl HostFn {
}
CallType::Fallible => {
parse_quote! {
ctx.run_fallible::<_, _, #err_len>(err_mid_ptr, #cost, |ctx| {
ctx.run_fallible::<_, _, #err>(err_mid_ptr, #cost, |ctx| {
#inner_block
})
}
Expand Down Expand Up @@ -186,15 +186,15 @@ pub struct HostFnMeta {
/// The runtime costs of the host function.
runtime_costs: Expr,
/// The length of the error.
pub err_len: Expr,
pub err: Expr,
}

impl Parse for HostFnMeta {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let mut call_type = Default::default();
let mut wgas = false;
let mut runtime_costs = parse_quote!(RuntimeCosts::Null);
let mut err_len = parse_quote!(LengthWithHash);
let mut err = parse_quote!(ErrorWithHash);

let meta_list = Punctuated::<Meta, Token![,]>::parse_terminated(input)?;
for meta in meta_list {
Expand All @@ -203,7 +203,7 @@ impl Parse for HostFnMeta {
"fallible" => call_type = CallType::Fallible,
"wgas" => wgas = true,
"cost" => runtime_costs = meta.require_name_value()?.value.clone(),
"err_len" => err_len = meta.require_name_value()?.value.clone(),
"err" => err = meta.require_name_value()?.value.clone(),
_ => {}
}
}
Expand All @@ -212,7 +212,7 @@ impl Parse for HostFnMeta {
call_type,
wgas,
runtime_costs,
err_len,
err,
})
}
}
Expand Down
6 changes: 3 additions & 3 deletions core-backend/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mod host;
/// * `fallible` - if the host function executes fallible call.
/// * `wgas` - if the host function supports with-gas version.
/// * `cost` - RuntimeCosts definition, for example `#[host(cost = RuntimeCosts::Null)]`
/// * `err_len` - Error length definition, for example `#[host(err_len = LengthBytes)]`
/// * `err` - Structure definition with error code, for example `#[host(err = ErrorWithHash)]`
///
/// # Example
///
Expand Down Expand Up @@ -62,7 +62,7 @@ mod host;
/// ) -> Result<(), R::Error> {
/// syscall_trace!("reply", payload_ptr, len, value_ptr, delay, err_mid_ptr);
///
/// ctx.run_fallible::<_, _, LengthWithHash>(err_mid_ptr, RuntimeCosts::Reply(len), |ctx| {
/// ctx.run_fallible::<_, _, ErrorWithHash>(err_mid_ptr, RuntimeCosts::Reply(len), |ctx| {
/// // ...
/// })
/// }
Expand All @@ -77,7 +77,7 @@ mod host;
/// ) -> Result<(), R::Error> {
/// syscall_trace!("reply_wgas", payload_ptr, len, gas_limit, value_ptr, delay, err_mid_ptr);
///
/// ctx.run_fallible::<_, _, LengthWithHash>(err_mid_ptr, RuntimeCosts::ReplyWGas(len), |ctx| {
/// ctx.run_fallible::<_, _, ErrorWithHash>(err_mid_ptr, RuntimeCosts::ReplyWGas(len), |ctx| {
/// // ...
/// })
/// }
Expand Down
68 changes: 24 additions & 44 deletions core-backend/common/src/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ use gear_core::{
memory::{PageU32Size, WasmPage},
message::{HandlePacket, InitPacket, ReplyPacket},
};
use gear_core_errors::{ExtError, ReplyCode, SignalCode};
use gear_core_errors::{ReplyCode, SignalCode};
use gsys::{
BlockNumberWithHash, Hash, HashWithValue, LengthBytes, LengthWithBlockNumberAndValue,
LengthWithGas, LengthWithHandle, LengthWithHash, LengthWithReplyCode, LengthWithSignalCode,
LengthWithTwoHashes, TwoHashesWithValue,
BlockNumberWithHash, ErrorBytes, ErrorWithBlockNumberAndValue, ErrorWithGas, ErrorWithHandle,
ErrorWithHash, ErrorWithReplyCode, ErrorWithSignalCode, ErrorWithTwoHashes, Hash,
HashWithValue, TwoHashesWithValue,
};
use parity_scale_codec::Encode;

pub struct FuncsHandler<Ext: Externalities + 'static, Runtime> {
_phantom: PhantomData<(Ext, Runtime)>,
Expand Down Expand Up @@ -107,12 +106,12 @@ where
.map_err(Into::into)
}

#[host(fallible, cost = RuntimeCosts::SendInit, err_len = LengthWithHandle)]
#[host(fallible, cost = RuntimeCosts::SendInit, err = ErrorWithHandle)]
pub fn send_init(ctx: &mut R) -> Result<(), R::Error> {
ctx.ext_mut().send_init().map_err(Into::into)
}

#[host(fallible, cost = RuntimeCosts::SendPush(len), err_len = LengthBytes)]
#[host(fallible, cost = RuntimeCosts::SendPush(len), err = ErrorBytes)]
pub fn send_push(ctx: &mut R, handle: u32, payload_ptr: u32, len: u32) -> Result<(), R::Error> {
let read_payload = ctx.register_read(payload_ptr, len);
let payload = ctx.read(read_payload)?;
Expand Down Expand Up @@ -172,7 +171,7 @@ where
.map_err(Into::into)
}

#[host(fallible, cost = RuntimeCosts::Read, err_len = LengthBytes)]
#[host(fallible, cost = RuntimeCosts::Read, err = ErrorBytes)]
pub fn read(ctx: &mut R, at: u32, len: u32, buffer_ptr: u32) -> Result<(), R::Error> {
let payload_lock = ctx.ext_mut().lock_payload(at, len)?;
payload_lock
Expand Down Expand Up @@ -203,7 +202,7 @@ where
Err(ActorTerminationReason::Exit(inheritor_id).into())
}

#[host(fallible, cost = RuntimeCosts::ReplyCode, err_len = LengthWithReplyCode)]
#[host(fallible, cost = RuntimeCosts::ReplyCode, err = ErrorWithReplyCode)]
pub fn reply_code(ctx: &mut R) -> Result<(), R::Error> {
ctx.ext_mut()
.reply_code()
Expand All @@ -212,7 +211,7 @@ where
}

// TODO: write proper benchmark #2825
#[host(fallible, cost = RuntimeCosts::ReplyCode, err_len = LengthWithSignalCode)]
#[host(fallible, cost = RuntimeCosts::ReplyCode, err = ErrorWithSignalCode)]
pub fn signal_code(ctx: &mut R) -> Result<(), R::Error> {
ctx.ext_mut()
.signal_code()
Expand Down Expand Up @@ -361,7 +360,7 @@ where
ctx.ext_mut().signal_from().map_err(Into::into)
}

#[host(fallible, cost = RuntimeCosts::ReplyPush(len), err_len = LengthBytes)]
#[host(fallible, cost = RuntimeCosts::ReplyPush(len), err = ErrorBytes)]
pub fn reply_push(ctx: &mut R, payload_ptr: u32, len: u32) -> Result<(), R::Error> {
let read_payload = ctx.register_read(payload_ptr, len);
let payload = ctx.read(read_payload)?;
Expand All @@ -383,7 +382,7 @@ where
f().map_err(Into::into)
}

#[host(fallible, cost = RuntimeCosts::ReplyPushInput, err_len = LengthBytes)]
#[host(fallible, cost = RuntimeCosts::ReplyPushInput, err = ErrorBytes)]
pub fn reply_push_input(ctx: &mut R, offset: u32, len: u32) -> Result<(), R::Error> {
ctx.ext_mut()
.reply_push_input(offset, len)
Expand Down Expand Up @@ -418,7 +417,7 @@ where
f().map_err(Into::into)
}

#[host(fallible, cost = RuntimeCosts::SendPushInput, err_len = LengthBytes)]
#[host(fallible, cost = RuntimeCosts::SendPushInput, err = ErrorBytes)]
pub fn send_push_input(
ctx: &mut R,
handle: u32,
Expand Down Expand Up @@ -461,23 +460,17 @@ where
ctx.ext_mut().reserve_gas(gas, duration).map_err(Into::into)
}

pub fn reply_deposit(
ctx: &mut R,
message_id_ptr: u32,
gas: u64,
err_mid_ptr: u32,
) -> Result<(), R::Error> {
ctx.run_fallible::<_, _, LengthBytes>(err_mid_ptr, RuntimeCosts::ReplyDeposit, |ctx| {
let read_message_id = ctx.register_read_decoded(message_id_ptr);
let message_id = ctx.read_decoded(read_message_id)?;
#[host(fallible, cost = RuntimeCosts::ReplyDeposit, err = ErrorBytes)]
pub fn reply_deposit(ctx: &mut R, message_id_ptr: u32, gas: u64) -> Result<(), R::Error> {
let read_message_id = ctx.register_read_decoded(message_id_ptr);
let message_id = ctx.read_decoded(read_message_id)?;

ctx.ext_mut()
.reply_deposit(message_id, gas)
.map_err(Into::into)
})
ctx.ext_mut()
.reply_deposit(message_id, gas)
.map_err(Into::into)
}

#[host(fallible, cost = RuntimeCosts::UnreserveGas, err_len = LengthWithGas)]
#[host(fallible, cost = RuntimeCosts::UnreserveGas, err = ErrorWithGas)]
pub fn unreserve_gas(ctx: &mut R, reservation_id_ptr: u32) -> Result<(), R::Error> {
let read_reservation_id = ctx.register_read_decoded(reservation_id_ptr);
let reservation_id = ctx.read_decoded(read_reservation_id)?;
Expand All @@ -487,7 +480,7 @@ where
.map_err(Into::into)
}

#[host(fallible, cost = RuntimeCosts::SystemReserveGas, err_len = LengthBytes)]
#[host(fallible, cost = RuntimeCosts::SystemReserveGas, err = ErrorBytes)]
pub fn system_reserve_gas(ctx: &mut R, gas: u64) -> Result<(), R::Error> {
ctx.ext_mut().system_reserve_gas(gas).map_err(Into::into)
}
Expand Down Expand Up @@ -519,7 +512,7 @@ where
.map_err(Into::into)
}

#[host(fallible, cost = RuntimeCosts::PayProgramRent, err_len = LengthWithBlockNumberAndValue)]
#[host(fallible, cost = RuntimeCosts::PayProgramRent, err = ErrorWithBlockNumberAndValue)]
pub fn pay_program_rent(ctx: &mut R, rent_pid_ptr: u32) -> Result<(), R::Error> {
let read_rent_pid = ctx.register_read_as(rent_pid_ptr);

Expand Down Expand Up @@ -587,7 +580,7 @@ where
Err(ActorTerminationReason::Wait(Some(duration), waited_type).into())
}

#[host(fallible, cost = RuntimeCosts::Wake, err_len = LengthBytes)]
#[host(fallible, cost = RuntimeCosts::Wake, err = ErrorBytes)]
pub fn wake(ctx: &mut R, message_id_ptr: u32, delay: u32) -> Result<(), R::Error> {
let read_message_id = ctx.register_read_decoded(message_id_ptr);
let message_id = ctx.read_decoded(read_message_id)?;
Expand All @@ -596,7 +589,7 @@ where
}

#[allow(clippy::too_many_arguments)]
#[host(fallible, wgas, cost = RuntimeCosts::CreateProgram(payload_len, salt_len), err_len = LengthWithTwoHashes)]
#[host(fallible, wgas, cost = RuntimeCosts::CreateProgram(payload_len, salt_len), err = ErrorWithTwoHashes)]
pub fn create_program(
ctx: &mut R,
cid_value_ptr: u32,
Expand All @@ -621,19 +614,6 @@ where
.map_err(Into::into)
}

// `error_bytes_ptr` is ptr for buffer of an error
#[host(fallible, cost = RuntimeCosts::Error, err_len = LengthBytes)]
pub fn error(ctx: &mut R, error_bytes_ptr: u32) -> Result<(), R::Error> {
if let Some(err) = ctx.fallible_syscall_error().as_ref() {
let err = err.encode();
let write_error_bytes = ctx.register_write(error_bytes_ptr, err.len() as u32);
ctx.write(write_error_bytes, err.as_ref())?;
Ok(())
} else {
Err(ActorTerminationReason::Trap(TrapExplanation::Ext(ExtError::SyscallUsage)).into())
}
}

pub fn forbidden(ctx: &mut R) -> Result<(), R::Error> {
syscall_trace!("forbidden");

Expand Down
10 changes: 3 additions & 7 deletions core-backend/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ use gear_core::{
use gear_core_errors::{ExecutionError, ExtError, MemoryError, MessageError};
use lazy_pages::GlobalsAccessConfig;
use memory::ProcessAccessError;
use scale_info::{
scale::{self, Decode, Encode},
TypeInfo,
};
use scale_info::scale::{self, Decode, Encode};

pub use crate::utils::{LimitedStr, TrimmedString};
pub use log;
Expand Down Expand Up @@ -164,7 +161,6 @@ pub struct SystemTerminationReason;
#[derive(
Decode,
Encode,
TypeInfo,
Debug,
Clone,
PartialEq,
Expand Down Expand Up @@ -360,10 +356,10 @@ pub trait BackendState {
TrapExplanation::Ext(ext_err),
)) = err
{
let len = ext_err.encoded_size() as u32;
let code = ext_err.to_u32();
log::trace!(target: "syscalls", "fallible syscall error: {ext_err}");
self.set_fallible_syscall_error(ext_err);
Ok(Err(len))
Ok(Err(code))
} else {
Err(err)
}
Expand Down
1 change: 0 additions & 1 deletion core-backend/sandbox/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ where
builder.add_func(Debug, wrap_common_func!(FuncsHandler::debug, (2) -> ()));
builder.add_func(Panic, wrap_common_func!(FuncsHandler::panic, (2) -> ()));
builder.add_func(OomPanic, wrap_common_func!(FuncsHandler::oom_panic, () -> ()));
builder.add_func(Error, wrap_common_func!(FuncsHandler::error, (2) -> ()));
builder.add_func(Exit, wrap_common_func!(FuncsHandler::exit, (1) -> ()));
builder.add_func(ReplyCode, wrap_common_func!(FuncsHandler::reply_code, (1) -> ()));
builder.add_func(SignalCode, wrap_common_func!(FuncsHandler::signal_code, (1) -> ()));
Expand Down
1 change: 0 additions & 1 deletion core-backend/wasmi/src/funcs_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ where
f.build(Wake, |forbidden| wrap_common_func!(CommonFuncsHandler::wake, (3) -> ())(store, forbidden, memory)),
f.build(CreateProgram, |forbidden| wrap_common_func!(CommonFuncsHandler::create_program, (7) -> ())(store, forbidden, memory)),
f.build(CreateProgramWGas, |forbidden| wrap_common_func!(CommonFuncsHandler::create_program_wgas, (8) -> ())(store, forbidden, memory)),
f.build(Error, |forbidden| wrap_common_func!(CommonFuncsHandler::error, (2) -> ())(store, forbidden, memory)),
f.build(ReserveGas, |forbidden| wrap_common_func!(CommonFuncsHandler::reserve_gas, (3) -> ())(store, forbidden, memory)),
f.build(ReplyDeposit, |forbidden| wrap_common_func!(CommonFuncsHandler::reply_deposit, (3) -> ())(store, forbidden, memory)),
f.build(UnreserveGas, |forbidden| wrap_common_func!(CommonFuncsHandler::unreserve_gas, (2) -> ())(store, forbidden, memory)),
Expand Down
Loading

0 comments on commit ba15f03

Please sign in to comment.