Skip to content

Commit 4ee8c2f

Browse files
committed
Moved call frame popping to return handling functions
1 parent fc9d658 commit 4ee8c2f

File tree

2 files changed

+22
-37
lines changed

2 files changed

+22
-37
lines changed

crates/vm/levm/src/opcode_handlers/system.rs

+22-21
Original file line numberDiff line numberDiff line change
@@ -926,35 +926,35 @@ impl<'a> VM<'a> {
926926
tx_report: &ExecutionReport,
927927
retdata: RetData,
928928
) -> Result<(), VMError> {
929+
let previous_call_frame = self.call_frames.pop().ok_or(VMError::Internal(InternalError::CouldNotPopCallframe))?;
929930
// Return gas left from subcontext
930-
let gas_left_from_new_call_frame = self
931-
.current_call_frame()?
931+
let gas_left_from_new_call_frame = previous_call_frame
932932
.gas_limit
933933
.checked_sub(tx_report.gas_used)
934934
.ok_or(InternalError::GasOverflow)?;
935935
{
936-
let next_call_frame = self.next_call_frame_mut()?;
937-
next_call_frame.gas_used = next_call_frame
936+
let current_call_frame = self.current_call_frame_mut()?;
937+
current_call_frame.gas_used = current_call_frame
938938
.gas_used
939939
.checked_sub(gas_left_from_new_call_frame)
940940
.ok_or(InternalError::GasOverflow)?;
941941

942-
next_call_frame.logs.extend(tx_report.logs.clone());
942+
current_call_frame.logs.extend(tx_report.logs.clone());
943943
memory::try_store_range(
944-
&mut next_call_frame.memory,
944+
&mut current_call_frame.memory,
945945
retdata.ret_offset,
946946
retdata.ret_size,
947947
&tx_report.output,
948948
)?;
949-
next_call_frame.sub_return_data = tx_report.output.clone();
949+
current_call_frame.sub_return_data = tx_report.output.clone();
950950
}
951951

952952
// What to do, depending on TxResult
953953
match tx_report.result {
954954
TxResult::Success => {
955-
self.next_call_frame_mut()?.stack.push(SUCCESS_FOR_CALL)?;
956-
for (address, account_opt) in self.current_call_frame()?.cache_backup.clone() {
957-
self.next_call_frame_mut()?
955+
self.current_call_frame_mut()?.stack.push(SUCCESS_FOR_CALL)?;
956+
for (address, account_opt) in previous_call_frame.cache_backup.clone() {
957+
self.current_call_frame_mut()?
958958
.cache_backup
959959
.entry(address)
960960
.or_insert(account_opt);
@@ -968,11 +968,12 @@ impl<'a> VM<'a> {
968968
self.increase_account_balance(retdata.msg_sender, retdata.value)?;
969969
}
970970
// Push 0 to stack
971-
self.next_call_frame_mut()?.stack.push(REVERT_FOR_CALL)?;
971+
self.current_call_frame_mut()?.stack.push(REVERT_FOR_CALL)?;
972972
}
973973
}
974974
Ok(())
975975
}
976+
976977
pub fn handle_return_create(
977978
&mut self,
978979
tx_report: &ExecutionReport,
@@ -982,25 +983,25 @@ impl<'a> VM<'a> {
982983
.max_message_call_gas
983984
.checked_sub(tx_report.gas_used)
984985
.ok_or(InternalError::GasOverflow)?;
985-
986+
let previous_call_frame = self.call_frames.pop().ok_or(VMError::Internal(InternalError::CouldNotPopCallframe))?;
986987
{
987-
let next_call_frame = self.next_call_frame_mut()?;
988+
let current_call_frame = self.current_call_frame_mut()?;
988989
// Return reserved gas
989-
next_call_frame.gas_used = next_call_frame
990+
current_call_frame.gas_used = current_call_frame
990991
.gas_used
991992
.checked_sub(unused_gas)
992993
.ok_or(InternalError::GasOverflow)?;
993994

994-
next_call_frame.logs.extend(tx_report.logs.clone());
995+
current_call_frame.logs.extend(tx_report.logs.clone());
995996
}
996997

997998
match tx_report.result.clone() {
998999
TxResult::Success => {
999-
self.next_call_frame_mut()?
1000+
self.current_call_frame_mut()?
10001001
.stack
10011002
.push(address_to_word(retdata.to))?;
1002-
for (address, account_opt) in self.current_call_frame()?.cache_backup.clone() {
1003-
self.next_call_frame_mut()?
1003+
for (address, account_opt) in previous_call_frame.cache_backup.clone() {
1004+
self.current_call_frame_mut()?
10041005
.cache_backup
10051006
.entry(address)
10061007
.or_insert(account_opt);
@@ -1014,12 +1015,12 @@ impl<'a> VM<'a> {
10141015
cache::remove_account(&mut self.db.cache, &retdata.to);
10151016
self.accrued_substate.created_accounts.remove(&retdata.to);
10161017

1017-
let next_call_frame = self.next_call_frame_mut()?;
1018+
let current_call_frame = self.current_call_frame_mut()?;
10181019
// If revert we have to copy the return_data
10191020
if err == VMError::RevertOpcode {
1020-
next_call_frame.sub_return_data = tx_report.output.clone();
1021+
current_call_frame.sub_return_data = tx_report.output.clone();
10211022
}
1022-
next_call_frame.stack.push(CREATE_DEPLOYMENT_FAIL)?;
1023+
current_call_frame.stack.push(CREATE_DEPLOYMENT_FAIL)?;
10231024
}
10241025
}
10251026
Ok(())

crates/vm/levm/src/vm.rs

-16
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,6 @@ impl<'a> VM<'a> {
326326
.pop()
327327
.ok_or(VMError::Internal(InternalError::CouldNotPopCallframe))?;
328328
let report = self.handle_precompile_result(precompile_result, backup)?;
329-
if self.handle_return(&report)? {
330-
self.return_to_previous_call_frame()?;
331-
}
332329
self.current_call_frame_mut()?.increment_pc_by(1)?;
333330
return Ok(report);
334331
}
@@ -345,7 +342,6 @@ impl<'a> VM<'a> {
345342
Ok(OpcodeResult::Halt) => {
346343
let report = self.handle_opcode_result()?;
347344
if self.handle_return(&report)? {
348-
self.return_to_previous_call_frame()?;
349345
self.current_call_frame_mut()?.increment_pc_by(1)?;
350346
} else {
351347
return Ok(report);
@@ -354,7 +350,6 @@ impl<'a> VM<'a> {
354350
Err(error) => {
355351
let report = self.handle_opcode_error(error)?;
356352
if self.handle_return(&report)? {
357-
self.return_to_previous_call_frame()?;
358353
self.current_call_frame_mut()?.increment_pc_by(1)?;
359354
} else {
360355
return Ok(report);
@@ -450,17 +445,6 @@ impl<'a> VM<'a> {
450445
))
451446
}
452447

453-
pub fn next_call_frame_mut(&mut self) -> Result<&mut CallFrame, VMError> {
454-
let second_to_last_index =
455-
self.call_frames
456-
.len()
457-
.checked_sub(2)
458-
.ok_or(VMError::Internal(
459-
InternalError::CouldNotAccessLastCallframe,
460-
))?;
461-
Ok(&mut self.call_frames[second_to_last_index])
462-
}
463-
464448
fn handle_create_non_empty_account(&mut self) -> Result<ExecutionReport, VMError> {
465449
let mut report = ExecutionReport {
466450
result: TxResult::Revert(VMError::AddressAlreadyOccupied),

0 commit comments

Comments
 (0)