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

Adapt to wasmi::ResourceLimiter, replacing mem_fuel metering #946

Closed
wants to merge 4 commits into from
Closed
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
13 changes: 3 additions & 10 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ default-features = false
package = "soroban-wasmi"
version = "0.30.0-soroban"
git = "https://github.com/stellar/wasmi"
rev = "3dc639fde3bebf0bf364a9fa4ac2f0efb7ee9995"
rev = "284c963ba080703061797e2a3cba0853edee0dd4"

[workspace.dependencies.stellar-strkey]
version = "0.0.7"
Expand Down
4 changes: 3 additions & 1 deletion soroban-env-common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ impl From<wasmi::core::TrapCode> for Error {

wasmi::core::TrapCode::BadSignature => ScErrorCode::UnexpectedType,

wasmi::core::TrapCode::StackOverflow | wasmi::core::TrapCode::OutOfFuel => {
wasmi::core::TrapCode::StackOverflow
| wasmi::core::TrapCode::OutOfFuel
| wasmi::core::TrapCode::GrowthOperationLimited => {
return Error::from_type_and_code(ScErrorType::Budget, ScErrorCode::ExceededLimit)
}
};
Expand Down
63 changes: 13 additions & 50 deletions soroban-env-host/src/budget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl HostCostModel for ContractCostParamEntry {
}
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone)]
pub struct BudgetDimension {
/// A set of cost models that map input values (eg. event counts, object
/// sizes) from some CostType to whatever concrete resource type is being
Expand Down Expand Up @@ -204,7 +204,7 @@ impl BudgetDimension {
/// doesn't derive all the traits we want. These fields (coarsely) define the
/// relative costs of different wasm instruction types and are for wasmi internal
/// fuel metering use only. Units are in "fuels".
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone)]
pub(crate) struct FuelConfig {
/// The base fuel costs for all instructions.
pub base: u64,
Expand Down Expand Up @@ -249,7 +249,7 @@ impl FuelConfig {
}
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone)]
pub(crate) struct BudgetImpl {
pub cpu_insns: BudgetDimension,
pub mem_bytes: BudgetDimension,
Expand Down Expand Up @@ -408,7 +408,7 @@ impl Display for BudgetImpl {
}
}

#[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Default, Clone)]
pub struct Budget(pub(crate) Rc<RefCell<BudgetImpl>>);

impl Debug for Budget {
Expand Down Expand Up @@ -469,7 +469,13 @@ impl Budget {
f(self.0.try_borrow_mut_or_err()?)
}

fn charge_in_bulk(
/// Performs a bulk charge to the budget under the specified [`CostType`].
/// The `iterations` is the batch size. The caller needs to ensure:
/// 1. the batched charges have identical costs (having the same
/// [`CostType`] and `input`)
/// 2. The input passed in (Some/None) is consistent with the [`CostModel`]
/// underneath the [`CostType`] (linear/constant).
pub fn bulk_charge(
&self,
ty: ContractCostType,
iterations: u64,
Expand Down Expand Up @@ -520,27 +526,7 @@ impl Budget {
/// Otherwise it is a linear model. The caller needs to ensure the input
/// passed is consistent with the inherent model underneath.
pub fn charge(&self, ty: ContractCostType, input: Option<u64>) -> Result<(), HostError> {
self.charge_in_bulk(ty, 1, input)
}

pub fn apply_wasmi_fuels(&self, cpu_fuel: u64, mem_fuel: u64) -> Result<(), HostError> {
self.charge_in_bulk(ContractCostType::WasmInsnExec, cpu_fuel, None)?;
self.charge_in_bulk(ContractCostType::WasmMemAlloc, mem_fuel, None)
}

/// Performs a bulk charge to the budget under the specified [`CostType`].
/// The `iterations` is the batch size. The caller needs to ensure:
/// 1. the batched charges have identical costs (having the same
/// [`CostType`] and `input`)
/// 2. The input passed in (Some/None) is consistent with the [`CostModel`]
/// underneath the [`CostType`] (linear/constant).
pub fn batched_charge(
&self,
ty: ContractCostType,
iterations: u64,
input: Option<u64>,
) -> Result<(), HostError> {
self.charge_in_bulk(ty, iterations, input)
self.bulk_charge(ty, 1, input)
}

pub fn with_free_budget<F, T>(&self, f: F) -> Result<T, HostError>
Expand Down Expand Up @@ -649,7 +635,7 @@ impl Budget {
Ok(())
}

fn get_cpu_insns_remaining_as_fuel(&self) -> Result<u64, HostError> {
pub(crate) fn get_cpu_insns_remaining_as_fuel(&self) -> Result<u64, HostError> {
let cpu_remaining = self.get_cpu_insns_remaining()?;
let cpu_per_fuel = self
.0
Expand All @@ -675,29 +661,6 @@ impl Budget {
Ok(cpu_remaining / cpu_per_fuel)
}

fn get_mem_bytes_remaining_as_fuel(&self) -> Result<u64, HostError> {
let bytes_remaining = self.get_mem_bytes_remaining()?;
let bytes_per_fuel = self
.0
.try_borrow_or_err()?
.mem_bytes
.get_cost_model(ContractCostType::WasmMemAlloc)
.linear_term;

if bytes_per_fuel < 0 {
return Err((ScErrorType::Context, ScErrorCode::InvalidInput).into());
}
let bytes_per_fuel = (bytes_per_fuel as u64).max(1);
// See comment about rounding above.
Ok(bytes_remaining / bytes_per_fuel)
}

pub fn get_fuels_budget(&self) -> Result<(u64, u64), HostError> {
let cpu_fuel = self.get_cpu_insns_remaining_as_fuel()?;
let mem_fuel = self.get_mem_bytes_remaining_as_fuel()?;
Ok((cpu_fuel, mem_fuel))
}

// generate a wasmi fuel cost schedule based on our calibration
pub fn wasmi_fuel_costs(&self) -> Result<FuelCosts, HostError> {
let config = &self.0.try_borrow_or_err()?.fuel_config;
Expand Down
Loading