Skip to content

Commit 6471af8

Browse files
committed
add and use out_of_fuel! utility macro
1 parent 843baa7 commit 6471af8

File tree

2 files changed

+44
-33
lines changed

2 files changed

+44
-33
lines changed

crates/wasmi/src/engine/executor/handler/exec.rs

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,16 @@ pub fn consume_fuel(
8686
mem0_len: Mem0Len,
8787
instance: Inst,
8888
) -> Done {
89-
let (ip, crate::ir::decode::ConsumeFuel { fuel }) = unsafe { decode_op(ip) };
89+
let (next_ip, crate::ir::decode::ConsumeFuel { fuel }) = unsafe { decode_op(ip) };
9090
let consumption_result = state
9191
.store
9292
.inner_mut()
9393
.fuel_mut()
9494
.consume_fuel_unchecked(u64::from(fuel));
9595
if let Err(FuelError::OutOfFuel { required_fuel }) = consumption_result {
96-
done!(state, DoneReason::out_of_fuel(required_fuel));
96+
out_of_fuel!(state, ip, required_fuel)
9797
}
98-
dispatch!(state, ip, sp, mem0, mem0_len, instance)
98+
dispatch!(state, next_ip, sp, mem0, mem0_len, instance)
9999
}
100100

101101
pub fn copy_span_asc(
@@ -421,7 +421,7 @@ pub fn memory_grow(
421421
instance: Inst,
422422
) -> Done {
423423
let (
424-
ip,
424+
next_ip,
425425
crate::ir::decode::MemoryGrow {
426426
memory,
427427
result,
@@ -452,7 +452,7 @@ pub fn memory_grow(
452452
}
453453
}
454454
Err(StoreError::External(MemoryError::OutOfFuel { required_fuel })) => {
455-
done!(state, DoneReason::out_of_fuel(required_fuel));
455+
out_of_fuel!(state, ip, required_fuel)
456456
}
457457
Err(StoreError::External(MemoryError::ResourceLimiterDeniedAllocation)) => {
458458
trap!(TrapCode::GrowthOperationLimited);
@@ -469,7 +469,7 @@ pub fn memory_grow(
469469
}
470470
};
471471
set_value(sp, result, return_value);
472-
dispatch!(state, ip, sp, mem0, mem0_len, instance)
472+
dispatch!(state, next_ip, sp, mem0, mem0_len, instance)
473473
}
474474

475475
pub fn memory_copy(
@@ -481,7 +481,7 @@ pub fn memory_copy(
481481
instance: Inst,
482482
) -> Done {
483483
let (
484-
ip,
484+
next_ip,
485485
crate::ir::decode::MemoryCopy {
486486
dst_memory,
487487
src_memory,
@@ -503,8 +503,8 @@ pub fn memory_copy(
503503
trap!(TrapCode::MemoryOutOfBounds)
504504
};
505505
if dst_memory == src_memory {
506-
memory_copy_within(state, instance, dst_memory, dst_index, src_index, len)?;
507-
dispatch!(state, ip, sp, mem0, mem0_len, instance)
506+
memory_copy_within(state, ip, instance, dst_memory, dst_index, src_index, len)?;
507+
dispatch!(state, next_ip, sp, mem0, mem0_len, instance)
508508
}
509509
let dst_memory = fetch_memory(instance, dst_memory);
510510
let src_memory = fetch_memory(instance, src_memory);
@@ -515,14 +515,15 @@ pub fn memory_copy(
515515
// These accesses just perform the bounds checks required by the Wasm spec.
516516
let src_bytes = memory_slice(src_memory, src_index, len).into_control()?;
517517
let dst_bytes = memory_slice_mut(dst_memory, dst_index, len).into_control()?;
518-
consume_fuel!(state, fuel, |costs| costs
518+
consume_fuel!(state, ip, fuel, |costs| costs
519519
.fuel_for_copying_bytes(len as u64));
520520
dst_bytes.copy_from_slice(src_bytes);
521-
dispatch!(state, ip, sp, mem0, mem0_len, instance)
521+
dispatch!(state, next_ip, sp, mem0, mem0_len, instance)
522522
}
523523

524524
fn memory_copy_within(
525525
state: &mut VmState<'_>,
526+
ip: Ip,
526527
instance: Inst,
527528
dst_memory: index::Memory,
528529
dst_index: usize,
@@ -534,7 +535,7 @@ fn memory_copy_within(
534535
// These accesses just perform the bounds checks required by the Wasm spec.
535536
memory_slice(memory, src_index, len).into_control()?;
536537
memory_slice(memory, dst_index, len).into_control()?;
537-
consume_fuel!(state, fuel, |costs| costs
538+
consume_fuel!(state, ip, fuel, |costs| costs
538539
.fuel_for_copying_bytes(len as u64));
539540
memory
540541
.data_mut()
@@ -551,7 +552,7 @@ pub fn memory_fill(
551552
instance: Inst,
552553
) -> Done {
553554
let (
554-
ip,
555+
next_ip,
555556
crate::ir::decode::MemoryFill {
556557
memory,
557558
dst,
@@ -571,10 +572,10 @@ pub fn memory_fill(
571572
let memory = fetch_memory(instance, memory);
572573
let (memory, fuel) = state.store.inner_mut().resolve_memory_and_fuel_mut(&memory);
573574
let slice = memory_slice_mut(memory, dst, len).into_control()?;
574-
consume_fuel!(state, fuel, |costs| costs
575+
consume_fuel!(state, ip, fuel, |costs| costs
575576
.fuel_for_copying_bytes(len as u64));
576577
slice.fill(value);
577-
dispatch!(state, ip, sp, mem0, mem0_len, instance)
578+
dispatch!(state, next_ip, sp, mem0, mem0_len, instance)
578579
}
579580

580581
pub fn memory_init(
@@ -586,7 +587,7 @@ pub fn memory_init(
586587
instance: Inst,
587588
) -> Done {
588589
let (
589-
ip,
590+
next_ip,
590591
crate::ir::decode::MemoryInit {
591592
memory,
592593
data,
@@ -619,10 +620,10 @@ pub fn memory_init(
619620
else {
620621
trap!(TrapCode::MemoryOutOfBounds)
621622
};
622-
consume_fuel!(state, fuel, |costs| costs
623+
consume_fuel!(state, ip, fuel, |costs| costs
623624
.fuel_for_copying_bytes(len as u64));
624625
memory.copy_from_slice(data);
625-
dispatch!(state, ip, sp, mem0, mem0_len, instance)
626+
dispatch!(state, next_ip, sp, mem0, mem0_len, instance)
626627
}
627628

628629
pub fn data_drop(
@@ -712,7 +713,7 @@ pub fn table_copy(
712713
instance: Inst,
713714
) -> Done {
714715
let (
715-
ip,
716+
next_ip,
716717
crate::ir::decode::TableCopy {
717718
dst_table,
718719
src_table,
@@ -732,11 +733,14 @@ pub fn table_copy(
732733
let trap_code = match error {
733734
TableError::CopyOutOfBounds => TrapCode::TableOutOfBounds,
734735
TableError::OutOfSystemMemory => TrapCode::OutOfSystemMemory,
736+
TableError::OutOfFuel { required_fuel } => {
737+
out_of_fuel!(state, ip, required_fuel)
738+
}
735739
_ => panic!("table.copy: unexpected error: {error:?}"),
736740
};
737741
trap!(trap_code)
738742
}
739-
dispatch!(state, ip, sp, mem0, mem0_len, instance)
743+
dispatch!(state, next_ip, sp, mem0, mem0_len, instance)
740744
}
741745
// Case: copy between two different tables
742746
let dst_table = fetch_table(instance, dst_table);
@@ -750,13 +754,13 @@ pub fn table_copy(
750754
TableError::CopyOutOfBounds => TrapCode::TableOutOfBounds,
751755
TableError::OutOfSystemMemory => TrapCode::OutOfSystemMemory,
752756
TableError::OutOfFuel { required_fuel } => {
753-
done!(state, DoneReason::out_of_fuel(required_fuel))
757+
out_of_fuel!(state, ip, required_fuel)
754758
}
755759
_ => panic!("table.copy: unexpected error: {error:?}"),
756760
};
757761
trap!(trap_code)
758762
}
759-
dispatch!(state, ip, sp, mem0, mem0_len, instance)
763+
dispatch!(state, next_ip, sp, mem0, mem0_len, instance)
760764
}
761765

762766
pub fn table_fill(
@@ -768,7 +772,7 @@ pub fn table_fill(
768772
instance: Inst,
769773
) -> Done {
770774
let (
771-
ip,
775+
next_ip,
772776
crate::ir::decode::TableFill {
773777
table,
774778
dst,
@@ -786,13 +790,13 @@ pub fn table_fill(
786790
TableError::OutOfSystemMemory => TrapCode::OutOfSystemMemory,
787791
TableError::FillOutOfBounds => TrapCode::TableOutOfBounds,
788792
TableError::OutOfFuel { required_fuel } => {
789-
done!(state, DoneReason::out_of_fuel(required_fuel))
793+
out_of_fuel!(state, ip, required_fuel)
790794
}
791795
_ => panic!("table.fill: unexpected error: {error:?}"),
792796
};
793797
trap!(trap_code)
794798
}
795-
dispatch!(state, ip, sp, mem0, mem0_len, instance)
799+
dispatch!(state, next_ip, sp, mem0, mem0_len, instance)
796800
}
797801

798802
pub fn table_init(
@@ -804,7 +808,7 @@ pub fn table_init(
804808
instance: Inst,
805809
) -> Done {
806810
let (
807-
ip,
811+
next_ip,
808812
crate::ir::decode::TableInit {
809813
table,
810814
elem,
@@ -827,13 +831,13 @@ pub fn table_init(
827831
TableError::OutOfSystemMemory => TrapCode::OutOfSystemMemory,
828832
TableError::InitOutOfBounds => TrapCode::TableOutOfBounds,
829833
TableError::OutOfFuel { required_fuel } => {
830-
done!(state, DoneReason::out_of_fuel(required_fuel))
834+
out_of_fuel!(state, ip, required_fuel)
831835
}
832836
_ => panic!("table.init: unexpected error: {error:?}"),
833837
};
834838
trap!(trap_code)
835839
}
836-
dispatch!(state, ip, sp, mem0, mem0_len, instance)
840+
dispatch!(state, next_ip, sp, mem0, mem0_len, instance)
837841
}
838842

839843
pub fn elem_drop(

crates/wasmi/src/engine/executor/handler/utils.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -356,18 +356,25 @@ pub fn compile_or_get_func(state: &mut VmState, func: EngineFunc) -> Result<(Ip,
356356
}
357357

358358
macro_rules! consume_fuel {
359-
($state:expr, $fuel:expr, $eval:expr) => {{
359+
($state:expr, $ip:expr, $fuel:expr, $eval:expr) => {{
360360
if let ::core::result::Result::Err($crate::errors::FuelError::OutOfFuel { required_fuel }) =
361361
$fuel.consume_fuel_if($eval)
362362
{
363-
done!(
364-
$state,
365-
$crate::engine::executor::handler::DoneReason::out_of_fuel(required_fuel),
366-
)
363+
out_of_fuel!($state, $ip, required_fuel)
367364
}
368365
}};
369366
}
370367

368+
macro_rules! out_of_fuel {
369+
($state:expr, $ip:expr, $required_fuel:expr) => {{
370+
$state.stack.sync_ip($ip);
371+
done!(
372+
$state,
373+
$crate::engine::executor::handler::DoneReason::out_of_fuel($required_fuel),
374+
)
375+
}};
376+
}
377+
371378
pub fn call_wasm(
372379
state: &mut VmState,
373380
caller_ip: Ip,

0 commit comments

Comments
 (0)