Skip to content

actor upgrade error refactor #1910

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

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 7 additions & 13 deletions fvm/src/call_manager/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,6 @@ where
ExecutionEvent::CallError(SyscallError::new(ErrorNumber::Forbidden, "fatal"))
}
Err(ExecutionError::Syscall(s)) => ExecutionEvent::CallError(s.clone()),
Err(ExecutionError::Abort(_)) => {
ExecutionEvent::CallError(SyscallError::new(ErrorNumber::Forbidden, "aborted"))
}
});
}

Expand Down Expand Up @@ -723,9 +720,10 @@ where

// From this point on, there are no more syscall errors, only aborts.
let result: std::result::Result<BlockId, Abort> = (|| {
let code = &state.code;
// Instantiate the module.
let instance = engine
.instantiate(&mut store, &state.code)?
.instantiate(&mut store, code)?
.context("actor not found")
.map_err(Abort::Fatal)?;

Expand All @@ -740,7 +738,11 @@ where
let func = match instance.get_func(&mut store, entrypoint.func_name()) {
Some(func) => func,
None => {
return Err(Abort::EntrypointNotFound);
return Err(Abort::Exit(
ExitCode::SYS_INVALID_RECEIVER,
format!("cannot upgrade to {code}"),
0,
));
}
};

Expand Down Expand Up @@ -812,14 +814,6 @@ where
Err(ExecutionError::Fatal(anyhow!(e))),
),
},
Abort::EntrypointNotFound => (
ExitCode::USR_FORBIDDEN,
"entrypoint not found".to_owned(),
Err(ExecutionError::Syscall(SyscallError::new(
ErrorNumber::Forbidden,
"entrypoint not found",
))),
),
Abort::OutOfGas => (
ExitCode::SYS_OUT_OF_GAS,
"out of gas".to_owned(),
Expand Down
9 changes: 4 additions & 5 deletions fvm/src/call_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ use fvm_shared::{ActorID, MethodNum};

use crate::engine::Engine;
use crate::gas::{Gas, GasCharge, GasTimer, GasTracker, PriceList};
use crate::kernel::{self, BlockRegistry, Result};
use crate::kernel::{self, BlockRegistry, ClassifyResult, Context, Result};
use crate::machine::{Machine, MachineContext};
use crate::state_tree::ActorState;
use crate::syscalls::error::Abort;
use crate::Kernel;

pub mod backtrace;
Expand Down Expand Up @@ -235,9 +234,9 @@ impl Entrypoint {
match self {
Entrypoint::Invoke(_) => Ok(Vec::new()),
Entrypoint::Upgrade(ui) => {
let ui_params = to_vec(&ui).map_err(|e| {
Abort::Fatal(anyhow::anyhow!("failed to serialize upgrade params: {}", e))
})?;
let ui_params = to_vec(&ui)
.or_fatal()
.context("failed to serialize upgrade params")?;
// This is CBOR instead of DAG_CBOR because these params are not reachable
let block_id = br.put_reachable(kernel::Block::new(CBOR, ui_params, Vec::new()))?;
Ok(vec![wasmtime::Val::I32(block_id as i32)])
Expand Down
3 changes: 0 additions & 3 deletions fvm/src/executor/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,6 @@ where
events_root,
}
}

Err(ExecutionError::Abort(e)) => return Err(anyhow!("actor aborted: {}", e)),
Copy link
Member Author

Choose a reason for hiding this comment

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

This was one of the cases I was worried about. Aborting with a fatal error here isn't really correct.


Err(ExecutionError::OutOfGas) => Receipt {
exit_code: ExitCode::SYS_OUT_OF_GAS,
return_data: Default::default(),
Expand Down
34 changes: 14 additions & 20 deletions fvm/src/kernel/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::convert::{TryFrom, TryInto};
use std::panic::{self, UnwindSafe};
use std::path::PathBuf;

use crate::syscalls::error::Abort;
use anyhow::{anyhow, Context as _};
use cid::Cid;
use filecoin_proofs_api::{self as proofs, ProverId, PublicReplicaInfo, SectorId};
Expand Down Expand Up @@ -874,7 +873,11 @@ where
.create_actor(code_id, actor_id, delegated_address)
}

fn upgrade_actor<K: Kernel>(&mut self, new_code_cid: Cid, params_id: BlockId) -> Result<u32> {
fn upgrade_actor<K: Kernel>(
&mut self,
new_code_cid: Cid,
params_id: BlockId,
) -> Result<SendResult> {
if self.read_only {
return Err(
syscall_error!(ReadOnly, "upgrade_actor cannot be called while read-only").into(),
Expand Down Expand Up @@ -938,27 +941,18 @@ where
});

match result {
Ok(InvocationResult {
exit_code: ExitCode::OK,
value,
}) => {
let block_id = match value {
None => NO_DATA_BLOCK_ID,
Some(block) => self.blocks.put_reachable(block).unwrap_or_else(|_| {
log::error!("failed to write to kernel block registry");
NO_DATA_BLOCK_ID
}),
Ok(InvocationResult { exit_code, value }) => {
let (block_stat, block_id) = match value {
None => (BlockStat { codec: 0, size: 0 }, NO_DATA_BLOCK_ID),
// TODO: Check error cases. At a minimum, we could run out of gas here!
Some(block) => (block.stat(), self.blocks.put_reachable(block)?),
Comment on lines +947 to +948
Copy link
Contributor

Choose a reason for hiding this comment

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

hmm, I don't see put_reachable ever checking if we run out of gas

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm. You're right... It looks like the only failure case is the LimitExceeded case which... likely shouldn't fail.

But I'd avoid casting the error if possible.

Copy link
Member Author

Choose a reason for hiding this comment

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

(and remove my todo)

};
Err(ExecutionError::Abort(Abort::Exit(
ExitCode::OK,
"actor upgraded".to_string(),
Ok(SendResult {
block_id,
)))
block_stat,
exit_code,
})
}
Ok(InvocationResult {
exit_code,
value: _,
}) => Ok(exit_code.value()),
Err(err) => Err(err),
}
}
Expand Down
14 changes: 1 addition & 13 deletions fvm/src/kernel/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use std::fmt::Display;
use derive_more::Display;
use fvm_shared::error::ErrorNumber;

use crate::syscalls::error::Abort;

/// Execution result.
pub type Result<T> = std::result::Result<T, ExecutionError>;

Expand Down Expand Up @@ -37,7 +35,6 @@ pub enum ExecutionError {
OutOfGas,
Syscall(SyscallError),
Fatal(anyhow::Error),
Abort(Abort),
}

impl ExecutionError {
Expand All @@ -55,7 +52,6 @@ impl ExecutionError {
match self {
Fatal(_) => true,
OutOfGas | Syscall(_) => false,
Abort(_) => true,
}
}

Expand All @@ -65,7 +61,7 @@ impl ExecutionError {
use ExecutionError::*;
match self {
Syscall(_) => true,
OutOfGas | Fatal(_) | Abort(_) => false,
OutOfGas | Fatal(_) => false,
}
}
}
Expand All @@ -78,12 +74,6 @@ impl From<SyscallError> for ExecutionError {
}
}

impl From<Abort> for ExecutionError {
fn from(e: Abort) -> Self {
ExecutionError::Abort(e)
}
}

pub trait ClassifyResult: Sized {
type Value;
type Error;
Expand Down Expand Up @@ -158,7 +148,6 @@ impl Context for ExecutionError {
Syscall(e) => Syscall(SyscallError(format!("{}: {}", context, e.0), e.1)),
Fatal(e) => Fatal(e.context(context.to_string())),
OutOfGas => OutOfGas, // no reason necessary
Abort(_) => self,
}
}

Expand All @@ -179,7 +168,6 @@ impl From<ExecutionError> for anyhow::Error {
OutOfGas => anyhow::anyhow!("out of gas"),
Syscall(err) => anyhow::anyhow!(err.0),
Fatal(err) => err,
Abort(e) => anyhow::anyhow!("aborted: {}", e),
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion fvm/src/kernel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,11 @@ pub trait ActorOps {
delegated_address: Option<Address>,
) -> Result<()>;

fn upgrade_actor<K: Kernel>(&mut self, new_code_cid: Cid, params_id: BlockId) -> Result<u32>;
fn upgrade_actor<K: Kernel>(
&mut self,
new_code_cid: Cid,
params_id: BlockId,
) -> Result<SendResult>;

/// Installs actor code pointed by cid
#[cfg(feature = "m2-native")]
Expand Down
33 changes: 28 additions & 5 deletions fvm/src/syscalls/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
use anyhow::{anyhow, Context as _};
use fvm_shared::{sys, ActorID};

use super::bind::ControlFlow;
use super::error::Abort;
use super::Context;
use crate::kernel::{ClassifyResult, Result};
use crate::kernel::{ClassifyResult, Result, SendResult};
use crate::{syscall_error, Kernel};

pub fn resolve_address(
Expand Down Expand Up @@ -113,10 +115,31 @@ pub fn upgrade_actor<K: Kernel>(
context: Context<'_, K>,
new_code_cid_off: u32,
params_id: u32,
) -> Result<u32> {
let cid = context.memory.read_cid(new_code_cid_off)?;

context.kernel.upgrade_actor::<K>(cid, params_id)
) -> ControlFlow<sys::out::send::Send> {
let cid = match context.memory.read_cid(new_code_cid_off) {
Ok(cid) => cid,
Err(err) => return err.into(),
};

match context.kernel.upgrade_actor::<K>(cid, params_id) {
Ok(SendResult {
block_id,
block_stat,
exit_code,
}) => {
if exit_code.is_success() {
ControlFlow::Abort(Abort::Exit(exit_code, String::new(), block_id))
} else {
ControlFlow::Return(sys::out::send::Send {
exit_code: exit_code.value(),
return_id: block_id,
return_codec: block_stat.codec,
return_size: block_stat.size,
})
}
Comment on lines +130 to +139
Copy link
Contributor

Choose a reason for hiding this comment

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

plus having it here, especially with the explicit naming makes it very easy to read and maintain

}
Err(err) => err.into(),
}
}

pub fn get_builtin_actor_type(
Expand Down
Loading