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

Zero $flag on call #743

Merged
merged 3 commits into from
Jun 3, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

#### Breaking

- [#743](https://github.com/FuelLabs/fuel-vm/pull/743): Zeroes `$flag` on `CALL`, so that contracts can assume clean `$flag` state.
- [#737](https://github.com/FuelLabs/fuel-vm/pull/737): Panic on instructions with non-zero reserved part.

## [Version 0.50.0]
Expand Down
7 changes: 4 additions & 3 deletions fuel-vm/src/interpreter/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ struct PrepareCallSystemRegisters<'a> {
bal: RegMut<'a, BAL>,
cgas: RegMut<'a, CGAS>,
ggas: RegMut<'a, GGAS>,
flag: RegMut<'a, FLAG>,
}

struct PrepareCallRegisters<'a> {
Expand All @@ -435,7 +436,6 @@ struct PrepareCallUnusedRegisters<'a> {
err: Reg<'a, ERR>,
ret: Reg<'a, RET>,
retl: Reg<'a, RETL>,
flag: Reg<'a, FLAG>,
}

impl<'a> PrepareCallRegisters<'a> {
Expand Down Expand Up @@ -607,6 +607,7 @@ where
*self.registers.system_registers.bal = self.params.amount_of_coins_to_forward;
*self.registers.system_registers.is = *self.registers.system_registers.pc;
*self.registers.system_registers.cgas = forward_gas_amount;
*self.registers.system_registers.flag = 0;

let receipt = Receipt::call(
id,
Expand Down Expand Up @@ -659,13 +660,13 @@ impl<'a> From<&'a PrepareCallRegisters<'_>> for SystemRegistersRef<'a> {
bal: registers.system_registers.bal.as_ref(),
cgas: registers.system_registers.cgas.as_ref(),
ggas: registers.system_registers.ggas.as_ref(),
flag: registers.system_registers.flag.as_ref(),
zero: registers.unused_registers.zero,
one: registers.unused_registers.one,
of: registers.unused_registers.of,
err: registers.unused_registers.err,
ret: registers.unused_registers.ret,
retl: registers.unused_registers.retl,
flag: registers.unused_registers.flag,
}
}
}
Expand Down Expand Up @@ -699,6 +700,7 @@ impl<'reg> From<SystemRegisters<'reg>>
bal: registers.bal,
cgas: registers.cgas,
ggas: registers.ggas,
flag: registers.flag,
};

(
Expand All @@ -710,7 +712,6 @@ impl<'reg> From<SystemRegisters<'reg>>
err: registers.err.into(),
ret: registers.ret.into(),
retl: registers.retl.into(),
flag: registers.flag.into(),
},
)
}
Expand Down
69 changes: 69 additions & 0 deletions fuel-vm/src/tests/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ use crate::{
consts::*,
prelude::*,
script_with_data_offset,
tests::test_helpers::assert_success,
util::test_helpers::TestBuilder,
};
use fuel_asm::{
op,
Flags,
RegId,
};
use fuel_crypto::Hasher;
Expand Down Expand Up @@ -221,6 +223,73 @@ fn call_frame_code_offset() {
assert_eq!(ssp, sp_p);
}

#[test]
fn call_zeroes_flag() {
let mut test_context = TestBuilder::new(2322u64);
let gas_limit = 1_000_000;

let program = vec![
op::log(RegId::FLAG, RegId::ZERO, RegId::ZERO, RegId::ZERO),
op::ret(RegId::ONE),
];

let contract_id = test_context.setup_contract(program, None, None).contract_id;

let (script, _) = script_with_data_offset!(
data_offset,
vec![
op::movi(0x10, Flags::UNSAFEMATH.bits().try_into().unwrap()),
op::flag(0x10),
op::movi(0x10, data_offset as Immediate18),
op::addi(0x11, 0x10, ContractId::LEN as Immediate12),
op::call(0x10, RegId::ZERO, 0x10, 0x10),
op::log(RegId::FLAG, RegId::ZERO, RegId::ZERO, RegId::ZERO),
op::ret(0x30),
],
test_context.get_tx_params().tx_offset()
);

let mut script_data = contract_id.to_vec();
script_data.extend([0u8; WORD_SIZE * 2]);

let result = test_context
.start_script(script, script_data)
.script_gas_limit(gas_limit)
.contract_input(contract_id)
.fee_input()
.contract_output(&contract_id)
.execute();

let receipts = result.receipts();

assert_success(receipts);

let Receipt::Log {
ra: flag_in_call, ..
} = &receipts[1]
else {
panic!("Expected log receipt");
};
let Receipt::Log {
ra: flag_after_call,
..
} = &receipts[3]
else {
panic!("Expected log receipt");
};

assert_eq!(
*flag_in_call,
Flags::empty().bits(),
"Call should zero $flag"
);
assert_eq!(
*flag_after_call,
Flags::UNSAFEMATH.bits(),
"Return should restore $flag"
);
}

#[test]
fn revert_from_call_immediately_ends_execution() {
let mut test_context = TestBuilder::new(2322u64);
Expand Down
Loading