Skip to content

Commit 09be48b

Browse files
committed
make InstrEncoder aware if fuel metering is enabled
1 parent d5e99be commit 09be48b

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

crates/wasmi/src/engine/translator/func2/instrs.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ pub struct InstrEncoder {
1414
/// The list of constructed instructions and their parameters.
1515
instrs: Vec<Instruction>,
1616
/// The fuel costs of instructions.
17-
fuel_costs: FuelCostsProvider,
17+
///
18+
/// This is `Some` if fuel metering is enabled, otherwise `None`.
19+
fuel_costs: Option<FuelCostsProvider>,
1820
}
1921

2022
impl ReusableAllocations for InstrEncoder {
@@ -49,9 +51,14 @@ impl Reset for InstrEncoder {
4951
impl InstrEncoder {
5052
/// Creates a new [`InstrEncoder`].
5153
pub fn new(engine: &Engine, alloc: InstrEncoderAllocations) -> Self {
54+
let config = engine.config();
55+
let fuel_costs = config
56+
.get_consume_fuel()
57+
.then(|| config.fuel_costs())
58+
.cloned();
5259
Self {
5360
instrs: alloc.instrs,
54-
fuel_costs: engine.config().fuel_costs().clone(),
61+
fuel_costs,
5562
}
5663
}
5764

@@ -66,14 +73,17 @@ impl InstrEncoder {
6673
/// # Note
6774
///
6875
/// The pushes [`Instruction::ConsumeFuel`] is initialized with base fuel costs.
69-
pub fn push_consume_fuel_instr(&mut self) -> Result<Instr, Error> {
70-
let base_costs = self.fuel_costs.base();
76+
pub fn push_consume_fuel_instr(&mut self) -> Result<Option<Instr>, Error> {
77+
let Some(fuel_costs) = &self.fuel_costs else {
78+
return Ok(None);
79+
};
80+
let base_costs = fuel_costs.base();
7181
let Ok(base_costs) = u32::try_from(base_costs) else {
7282
panic!("out of bounds base fuel costs: {base_costs}");
7383
};
7484
let instr = self.next_instr();
7585
self.instrs.push(Instruction::consume_fuel(base_costs));
76-
Ok(instr)
86+
Ok(Some(instr))
7787
}
7888

7989
/// Pushes an [`Instruction`] to the [`InstrEncoder`].
@@ -126,10 +136,17 @@ impl InstrEncoder {
126136
consume_fuel: Option<Instr>,
127137
f: impl FnOnce(&FuelCostsProvider) -> u64,
128138
) -> Result<(), Error> {
129-
let Some(consume_fuel) = consume_fuel else {
130-
return Ok(());
139+
let (fuel_costs, consume_fuel) = match (&self.fuel_costs, consume_fuel) {
140+
(None, None) => return Ok(()),
141+
(Some(fuel_costs), Some(consume_fuel)) => (fuel_costs, consume_fuel),
142+
_ => {
143+
panic!(
144+
"fuel metering state mismatch: fuel_costs: {:?}, fuel_instr: {:?}",
145+
self.fuel_costs, consume_fuel,
146+
);
147+
}
131148
};
132-
let fuel_consumed = f(&self.fuel_costs);
149+
let fuel_consumed = f(fuel_costs);
133150
self.get_mut(consume_fuel)
134151
.bump_fuel_consumption(fuel_consumed)?;
135152
Ok(())

crates/wasmi/src/engine/translator/func2/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,7 @@ impl FuncTranslator {
209209
let func_ty = self.module.get_type_of_func(self.func);
210210
let block_ty = BlockType::func_type(func_ty);
211211
let end_label = self.labels.new_label();
212-
let consume_fuel = self
213-
.fuel_costs
214-
.as_ref()
215-
.map(|_| self.instrs.push_consume_fuel_instr())
216-
.transpose()?;
212+
let consume_fuel = self.instrs.push_consume_fuel_instr()?;
217213
self.stack
218214
.push_func_block(block_ty, end_label, consume_fuel)?;
219215
Ok(())

0 commit comments

Comments
 (0)