Skip to content

Commit

Permalink
winch: Prefer using Context::without for fuel checks (#9709)
Browse files Browse the repository at this point in the history
This commit is a follow-up to
#9472.
It ensures that the fuel checking code makes use of `Context::without`
to retrieve the current fuel register.

Even though no issues have been found, it is preferable to use
`Context::without` in scenarios in which we have to introduce special
out-of-band control flow and function calls to ensure that all the
register allocation invariants are respected i.e., avoid holding onto to
registers that: are not  in the stack and that might be needed for
function calls.
  • Loading branch information
saulecabrera authored Dec 4, 2024
1 parent 06103f7 commit da7c53b
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions winch/codegen/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,14 @@ where
/// performing a zero-comparison with the number of units stored in
/// `VMRuntimeLimits`.
pub fn emit_fuel_check(&mut self) {
let fuel_var = self.emit_load_fuel_consumed();
let out_of_fuel = self.env.builtins.out_of_gas::<M::ABI, M::Ptr>();
let fuel_var =
self.context
.without::<Reg, M, _>(&out_of_fuel.sig().regs, self.masm, |cx, masm| {
cx.any_gpr(masm)
});

self.emit_load_fuel_consumed(fuel_var);
let continuation = self.masm.get_label();

// Spill locals and registers to avoid conflicts at the out-of-fuel
Expand All @@ -960,7 +967,6 @@ where
OperandSize::S64,
);
// Out-of-fuel branch.
let out_of_fuel = self.env.builtins.out_of_gas::<M::ABI, M::Ptr>();
FnCall::emit::<M>(
&mut self.env,
self.masm,
Expand Down Expand Up @@ -1018,10 +1024,9 @@ where

/// Emits a series of instructions that load the `fuel_consumed` field from
/// `VMRuntimeLimits`.
fn emit_load_fuel_consumed(&mut self) -> Reg {
fn emit_load_fuel_consumed(&mut self, fuel_var: Reg) {
let limits_offset = self.env.vmoffsets.ptr.vmctx_runtime_limits();
let fuel_offset = self.env.vmoffsets.ptr.vmruntime_limits_fuel_consumed();
let fuel_var = self.context.any_gpr(self.masm);
self.masm.load_ptr(
self.masm.address_at_vmctx(u32::from(limits_offset)),
writable!(fuel_var),
Expand All @@ -1033,8 +1038,6 @@ where
// Fuel is an i64.
OperandSize::S64,
);

fuel_var
}

/// Hook to handle fuel before visiting an operator.
Expand Down

0 comments on commit da7c53b

Please sign in to comment.