Compare commits

..

2 Commits

Author SHA1 Message Date
Cyrill Leutwiler 52bf16a383 wip
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2025-04-17 12:43:26 +02:00
Cyrill Leutwiler cf2fd2f1e8 pass assignment ptr
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2025-04-17 09:10:30 +02:00
18 changed files with 274 additions and 485 deletions
+5 -5
View File
@@ -1,10 +1,10 @@
{
"Baseline": 950,
"Computation": 2222,
"DivisionArithmetics": 8802,
"ERC20": 17601,
"Computation": 2262,
"DivisionArithmetics": 8915,
"ERC20": 17233,
"Events": 1628,
"FibonacciIterative": 1485,
"Flipper": 2089,
"SHA1": 8230
"Flipper": 2132,
"SHA1": 8381
}
-2
View File
@@ -48,8 +48,6 @@ pub use self::polkavm::context::Context as PolkaVMContext;
pub use self::polkavm::evm::arithmetic as polkavm_evm_arithmetic;
pub use self::polkavm::evm::bitwise as polkavm_evm_bitwise;
pub use self::polkavm::evm::call as polkavm_evm_call;
pub use self::polkavm::evm::call::Call as PolkaVMCallFunction;
pub use self::polkavm::evm::call::CallReentrancyHeuristic as PolkaVMCallReentrancyHeuristicFunction;
pub use self::polkavm::evm::calldata as polkavm_evm_calldata;
pub use self::polkavm::evm::comparison as polkavm_evm_comparison;
pub use self::polkavm::evm::context as polkavm_evm_contract_context;
@@ -66,7 +66,7 @@ impl<'ctx> Argument<'ctx> {
/// Access the underlying value.
///
/// Will emit a stack load if `self` is a pointer argument.
pub fn to_value<D: crate::polkavm::Dependency + Clone>(
pub fn access<D: crate::polkavm::Dependency + Clone>(
&self,
context: &crate::polkavm::context::Context<'ctx, D>,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>> {
@@ -78,8 +78,8 @@ impl<'ctx> Argument<'ctx> {
/// Access the underlying value.
///
/// Will emit a stack store if `self` is a value argument.
pub fn to_pointer<D: crate::polkavm::Dependency + Clone>(
/// Will emit a stack load if `self` is a pointer argument.
pub fn as_pointer<D: crate::polkavm::Dependency + Clone>(
&self,
context: &crate::polkavm::context::Context<'ctx, D>,
) -> anyhow::Result<crate::polkavm::context::Pointer<'ctx>> {
+48 -14
View File
@@ -773,27 +773,49 @@ where
.into())
}
/// Builds a stack load instruction.
/// Builds a stack load instruction with a direct assignment.
/// Sets the alignment to 256 bits for the stack and 1 bit for the heap, parent, and child.
pub fn build_load(
pub fn build_load_assign(
&self,
pointer: Pointer<'ctx>,
name: &str,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>> {
assignment_pointer: &mut Option<inkwell::values::PointerValue<'ctx>>,
) -> anyhow::Result<Option<inkwell::values::BasicValueEnum<'ctx>>> {
match pointer.address_space {
AddressSpace::Heap => {
let name = <PolkaVMLoadHeapWordFunction as RuntimeFunction<D>>::NAME;
let declaration =
<PolkaVMLoadHeapWordFunction as RuntimeFunction<D>>::declaration(self);
let arguments = [self
.builder()
.build_ptr_to_int(pointer.value, self.xlen_type(), "offset_ptrtoint")?
.as_basic_value_enum()];
Ok(self
.build_call(declaration, &arguments, "heap_load")
.unwrap_or_else(|| {
panic!("revive runtime function {name} should return a value")
}))
match assignment_pointer.take() {
Some(assignment_pointer) => {
let arguments = [
self.builder()
.build_ptr_to_int(
pointer.value,
self.xlen_type(),
"offset_ptrtoint",
)?
.as_basic_value_enum(),
assignment_pointer.into(),
];
self.build_call(declaration, &arguments, "heap_load");
Ok(None)
}
None => {
let pointer = self.build_alloca_at_entry(self.word_type(), "pointer");
let arguments = [
self.builder()
.build_ptr_to_int(
pointer.value,
self.xlen_type(),
"offset_ptrtoint",
)?
.as_basic_value_enum(),
pointer.value.into(),
];
self.build_call(declaration, &arguments, "heap_load");
Ok(Some(self.build_load(pointer, "storage_value")?))
}
}
}
AddressSpace::Stack => {
let value = self
@@ -806,11 +828,23 @@ where
.set_alignment(revive_common::BYTE_LENGTH_STACK_ALIGN as u32)
.expect("Alignment is valid");
Ok(value)
Ok(Some(value))
}
}
}
/// Builds a stack load instruction.
/// Sets the alignment to 256 bits for the stack and 1 bit for the heap, parent, and child.
pub fn build_load(
&self,
pointer: Pointer<'ctx>,
name: &str,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>> {
Ok(self
.build_load_assign(pointer, name, &mut None)?
.expect("without an assignment pointer loads return a value"))
}
/// Builds a stack store instruction.
/// Sets the alignment to 256 bits for the stack and 1 bit for the heap, parent, and child.
pub fn build_store<V>(&self, pointer: Pointer<'ctx>, value: V) -> anyhow::Result<()>
@@ -2,6 +2,8 @@
use inkwell::values::BasicValueEnum;
use crate::polkavm::context::address_space::AddressSpace;
use crate::polkavm::context::pointer::Pointer;
use crate::polkavm::context::runtime::RuntimeFunction;
use crate::polkavm::context::Context;
use crate::polkavm::Dependency;
@@ -17,9 +19,13 @@ where
const NAME: &'static str = "__revive_load_heap_word";
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
context
.word_type()
.fn_type(&[context.xlen_type().into()], false)
context.void_type().fn_type(
&[
context.xlen_type().into(),
context.llvm().ptr_type(Default::default()).into(),
],
false,
)
}
fn emit_body<'ctx>(
@@ -27,6 +33,7 @@ where
context: &mut Context<'ctx, D>,
) -> anyhow::Result<Option<BasicValueEnum<'ctx>>> {
let offset = Self::paramater(context, 0).into_int_value();
let assignment_pointer = Self::paramater(context, 1).into_pointer_value();
let length = context
.xlen_type()
.const_int(revive_common::BYTE_LENGTH_WORD as u64, false);
@@ -42,7 +49,11 @@ where
.expect("Alignment is valid");
let swapped_value = context.build_byte_swap(value)?;
Ok(Some(swapped_value))
context.build_store(
Pointer::new(context.word_type(), AddressSpace::Stack, assignment_pointer),
swapped_value,
)?;
Ok(None)
}
}
@@ -2,6 +2,8 @@
use inkwell::values::BasicValueEnum;
use crate::polkavm::context::address_space::AddressSpace;
use crate::polkavm::context::pointer::Pointer;
use crate::polkavm::context::runtime::RuntimeFunction;
use crate::polkavm::context::Context;
use crate::polkavm::Dependency;
@@ -17,20 +19,27 @@ where
const NAME: &'static str = "__revive_load_storage_word";
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
context
.word_type()
.fn_type(&[context.llvm().ptr_type(Default::default()).into()], false)
context.void_type().fn_type(
&[
context.llvm().ptr_type(Default::default()).into(),
context.llvm().ptr_type(Default::default()).into(),
],
false,
)
}
fn emit_body<'ctx>(
&self,
context: &mut Context<'ctx, D>,
) -> anyhow::Result<Option<BasicValueEnum<'ctx>>> {
Ok(Some(emit_load(
context,
Self::paramater(context, 0),
false,
)?))
let key = Self::paramater(context, 0);
let assignment_pointer = Self::paramater(context, 1).into_pointer_value();
let value = emit_load(context, key, false)?;
context.build_store(
Pointer::new(context.word_type(), AddressSpace::Stack, assignment_pointer),
value,
)?;
Ok(None)
}
}
+81 -378
View File
@@ -2,310 +2,117 @@
use inkwell::values::BasicValue;
use crate::polkavm::context::address_space::AddressSpace;
use crate::polkavm::context::argument::Argument;
use crate::polkavm::context::pointer::Pointer;
use crate::polkavm::context::runtime::RuntimeFunction;
use crate::polkavm::context::Context;
use crate::polkavm::Dependency;
use crate::polkavm::WriteLLVM;
const STATIC_CALL_FLAG: u32 = 0b0001_0000;
const REENTRANT_CALL_FLAG: u32 = 0b0000_1000;
const SOLIDITY_TRANSFER_GAS_STIPEND_THRESHOLD: u64 = 2300;
pub struct Call;
impl<D> RuntimeFunction<D> for Call
where
D: Dependency + Clone,
{
const NAME: &'static str = "__revive_call";
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
context.xlen_type().fn_type(
&[
context.xlen_type().into(),
context.xlen_type().into(),
context.xlen_type().into(),
context.xlen_type().into(),
context.xlen_type().into(),
context.llvm().ptr_type(AddressSpace::Stack.into()).into(),
context.llvm().ptr_type(AddressSpace::Stack.into()).into(),
context.llvm().ptr_type(AddressSpace::Stack.into()).into(),
],
false,
)
}
fn emit_body<'ctx>(
&self,
context: &mut Context<'ctx, D>,
) -> anyhow::Result<Option<inkwell::values::BasicValueEnum<'ctx>>> {
let flags = Self::paramater(context, 0).into_int_value();
let input_length = Self::paramater(context, 1).into_int_value();
let output_length = Self::paramater(context, 2).into_int_value();
let input_pointer = Self::paramater(context, 3).into_int_value();
let output_pointer = Self::paramater(context, 4).into_int_value();
let address_pointer = Self::paramater(context, 5).into_pointer_value();
let value_pointer = Self::paramater(context, 6).into_pointer_value();
let deposit_pointer = Self::paramater(context, 7).into_pointer_value();
let output_length_pointer =
context.build_alloca_at_entry(context.xlen_type(), "output_length");
context.build_store(output_length_pointer, output_length)?;
let input_pointer = context.build_heap_gep(input_pointer, input_length)?;
let output_pointer = context.build_heap_gep(output_pointer, output_length)?;
let flags_and_callee = revive_runtime_api::calling_convention::pack_hi_lo_reg(
context.builder(),
context.llvm(),
flags,
Pointer::new(context.word_type(), AddressSpace::Stack, address_pointer).to_int(context),
"address_and_callee",
)?;
let deposit_and_value = revive_runtime_api::calling_convention::pack_hi_lo_reg(
context.builder(),
context.llvm(),
Pointer::new(context.word_type(), AddressSpace::Stack, deposit_pointer).to_int(context),
Pointer::new(context.word_type(), AddressSpace::Stack, value_pointer).to_int(context),
"deposit_and_value",
)?;
let input_data = revive_runtime_api::calling_convention::pack_hi_lo_reg(
context.builder(),
context.llvm(),
input_length,
input_pointer.to_int(context),
"input_data",
)?;
let output_data = revive_runtime_api::calling_convention::pack_hi_lo_reg(
context.builder(),
context.llvm(),
output_length_pointer.to_int(context),
output_pointer.to_int(context),
"output_data",
)?;
let name = revive_runtime_api::polkavm_imports::CALL;
let success = context
.build_runtime_call(
name,
&[
flags_and_callee.into(),
context.register_type().const_all_ones().into(),
context.register_type().const_all_ones().into(),
deposit_and_value.into(),
input_data.into(),
output_data.into(),
],
)
.unwrap_or_else(|| panic!("{name} should return a value"))
.into_int_value();
let is_success = context.builder().build_int_compare(
inkwell::IntPredicate::EQ,
success,
context.integer_const(revive_common::BIT_LENGTH_X64, 0),
"is_success",
)?;
Ok(context
.builder()
.build_int_z_extend(is_success, context.xlen_type(), "success")?
.as_basic_value_enum()
.into())
}
}
impl<D> WriteLLVM<D> for Call
where
D: Dependency + Clone,
{
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
<Self as RuntimeFunction<_>>::declare(self, context)
}
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
<Self as RuntimeFunction<_>>::emit(&self, context)
}
}
/// Translates a contract call.
#[allow(clippy::too_many_arguments)]
pub fn call<'ctx, D>(
context: &mut Context<'ctx, D>,
gas: &Argument<'ctx>,
address: &Argument<'ctx>,
value: Option<&Argument<'ctx>>,
input_offset: &Argument<'ctx>,
input_length: &Argument<'ctx>,
output_offset: &Argument<'ctx>,
output_length: &Argument<'ctx>,
gas: inkwell::values::IntValue<'ctx>,
address: inkwell::values::IntValue<'ctx>,
value: Option<inkwell::values::IntValue<'ctx>>,
input_offset: inkwell::values::IntValue<'ctx>,
input_length: inkwell::values::IntValue<'ctx>,
output_offset: inkwell::values::IntValue<'ctx>,
output_length: inkwell::values::IntValue<'ctx>,
_constants: Vec<Option<num::BigUint>>,
static_call: bool,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
where
D: Dependency + Clone,
{
let input_offset =
context.safe_truncate_int_to_xlen(input_offset.to_value(context)?.into_int_value())?;
let input_length =
context.safe_truncate_int_to_xlen(input_length.to_value(context)?.into_int_value())?;
let address_pointer = context.build_address_argument_store(address)?;
let output_offset =
context.safe_truncate_int_to_xlen(output_offset.to_value(context)?.into_int_value())?;
let output_length =
context.safe_truncate_int_to_xlen(output_length.to_value(context)?.into_int_value())?;
let value = value.unwrap_or_else(|| context.word_const(0));
let value_pointer = context.build_alloca_at_entry(context.word_type(), "value_pointer");
context.build_store(value_pointer, value)?;
let deposit_limit_pointer =
context.build_alloca_at_entry(context.word_type(), "deposit_limit_pointer");
let input_offset = context.safe_truncate_int_to_xlen(input_offset)?;
let input_length = context.safe_truncate_int_to_xlen(input_length)?;
let output_offset = context.safe_truncate_int_to_xlen(output_offset)?;
let output_length = context.safe_truncate_int_to_xlen(output_length)?;
let flags = if static_call {
context.build_store(deposit_limit_pointer, context.word_type().const_zero())?;
let input_pointer = context.build_heap_gep(input_offset, input_length)?;
let output_pointer = context.build_heap_gep(output_offset, output_length)?;
let output_length_pointer = context.build_alloca_at_entry(context.xlen_type(), "output_length");
context.build_store(output_length_pointer, output_length)?;
let (flags, deposit_limit_value) = if static_call {
let flags = REENTRANT_CALL_FLAG | STATIC_CALL_FLAG;
context.xlen_type().const_int(flags as u64, false)
(
context.xlen_type().const_int(flags as u64, false),
context.word_type().const_zero(),
)
} else {
let name = <CallReentrancyHeuristic as RuntimeFunction<D>>::NAME;
let declaration = <CallReentrancyHeuristic as RuntimeFunction<D>>::declaration(context);
let gas = context.builder().build_int_truncate(
gas.to_value(context)?.into_int_value(),
context.xlen_type(),
"gas",
)?;
let arguments = &[
input_length.into(),
output_length.into(),
gas.into(),
deposit_limit_pointer.value.into(),
];
context
.build_call(declaration, arguments, "flags")
.unwrap_or_else(|| panic!("runtime function {name} should return a value"))
.into_int_value()
call_reentrancy_heuristic(context, gas, input_length, output_length)?
};
let value_pointer = match value {
Some(argument) => argument.to_pointer(context)?,
None => {
let value_pointer = context.build_alloca_at_entry(context.word_type(), "value_pointer");
context.build_store(value_pointer, context.word_const(0))?;
value_pointer
}
};
let deposit_pointer = context.build_alloca_at_entry(context.word_type(), "deposit_pointer");
context.build_store(deposit_pointer, deposit_limit_value)?;
let address_pointer =
context.build_address_argument_store(address.to_value(context)?.into_int_value())?;
let flags_and_callee = revive_runtime_api::calling_convention::pack_hi_lo_reg(
context.builder(),
context.llvm(),
flags,
address_pointer.to_int(context),
"address_and_callee",
)?;
let deposit_and_value = revive_runtime_api::calling_convention::pack_hi_lo_reg(
context.builder(),
context.llvm(),
deposit_pointer.to_int(context),
value_pointer.to_int(context),
"deposit_and_value",
)?;
let input_data = revive_runtime_api::calling_convention::pack_hi_lo_reg(
context.builder(),
context.llvm(),
input_length,
input_pointer.to_int(context),
"input_data",
)?;
let output_data = revive_runtime_api::calling_convention::pack_hi_lo_reg(
context.builder(),
context.llvm(),
output_length_pointer.to_int(context),
output_pointer.to_int(context),
"output_data",
)?;
let name = <Call as RuntimeFunction<D>>::NAME;
let arguments = &[
flags.into(),
input_length.into(),
output_length.into(),
input_offset.into(),
output_offset.into(),
address_pointer.value.into(),
value_pointer.value.into(),
deposit_limit_pointer.value.into(),
];
let declaration = <Call as RuntimeFunction<D>>::declaration(context);
let result = context
.build_call(declaration, arguments, "call_result_truncated")
.unwrap_or_else(|| panic!("runtime function {name} should return a value"))
let name = revive_runtime_api::polkavm_imports::CALL;
let success = context
.build_runtime_call(
name,
&[
flags_and_callee.into(),
context.register_type().const_all_ones().into(),
context.register_type().const_all_ones().into(),
deposit_and_value.into(),
input_data.into(),
output_data.into(),
],
)
.unwrap_or_else(|| panic!("{name} should return a value"))
.into_int_value();
let is_success = context.builder().build_int_compare(
inkwell::IntPredicate::EQ,
success,
context.integer_const(revive_common::BIT_LENGTH_X64, 0),
"is_success",
)?;
Ok(context
.builder()
.build_int_z_extend(result, context.word_type(), "call_result")?
.build_int_z_extend(is_success, context.word_type(), "success")?
.as_basic_value_enum())
/*
let address_pointer = context.build_address_argument_store(address)?;
let value = value.unwrap_or_else(|| context.word_const(0));
let value_pointer = context.build_alloca_at_entry(context.word_type(), "value_pointer");
context.build_store(value_pointer, value)?;
let input_offset = context.safe_truncate_int_to_xlen(input_offset)?;
let input_length = context.safe_truncate_int_to_xlen(input_length)?;
let output_offset = context.safe_truncate_int_to_xlen(output_offset)?;
let output_length = context.safe_truncate_int_to_xlen(output_length)?;
let input_pointer = context.build_heap_gep(input_offset, input_length)?;
let output_pointer = context.build_heap_gep(output_offset, output_length)?;
let output_length_pointer = context.build_alloca_at_entry(context.xlen_type(), "output_length");
context.build_store(output_length_pointer, output_length)?;
let (flags, deposit_limit_value) = if static_call {
let flags = REENTRANT_CALL_FLAG | STATIC_CALL_FLAG;
(
context.xlen_type().const_int(flags as u64, false),
context.word_type().const_zero(),
)
} else {
call_reentrancy_heuristic(context, gas, input_length, output_length)?
};
let deposit_pointer = context.build_alloca_at_entry(context.word_type(), "deposit_pointer");
context.build_store(deposit_pointer, deposit_limit_value)?;
let flags_and_callee = revive_runtime_api::calling_convention::pack_hi_lo_reg(
context.builder(),
context.llvm(),
flags,
address_pointer.to_int(context),
"address_and_callee",
)?;
let deposit_and_value = revive_runtime_api::calling_convention::pack_hi_lo_reg(
context.builder(),
context.llvm(),
deposit_pointer.to_int(context),
value_pointer.to_int(context),
"deposit_and_value",
)?;
let input_data = revive_runtime_api::calling_convention::pack_hi_lo_reg(
context.builder(),
context.llvm(),
input_length,
input_pointer.to_int(context),
"input_data",
)?;
let output_data = revive_runtime_api::calling_convention::pack_hi_lo_reg(
context.builder(),
context.llvm(),
output_length_pointer.to_int(context),
output_pointer.to_int(context),
"output_data",
)?;
let name = revive_runtime_api::polkavm_imports::CALL;
let success = context
.build_runtime_call(
name,
&[
flags_and_callee.into(),
context.register_type().const_all_ones().into(),
context.register_type().const_all_ones().into(),
deposit_and_value.into(),
input_data.into(),
output_data.into(),
],
)
.unwrap_or_else(|| panic!("{name} should return a value"))
.into_int_value();
let is_success = context.builder().build_int_compare(
inkwell::IntPredicate::EQ,
success,
context.integer_const(revive_common::BIT_LENGTH_X64, 0),
"is_success",
)?;
Ok(context
.builder()
.build_int_z_extend(is_success, context.word_type(), "success")?
.as_basic_value_enum())
*/
}
#[allow(clippy::too_many_arguments)]
@@ -409,110 +216,6 @@ where
.as_basic_value_enum())
}
pub struct CallReentrancyHeuristic;
impl<D> RuntimeFunction<D> for CallReentrancyHeuristic
where
D: Dependency + Clone,
{
const NAME: &'static str = "__revive_call_reentrancy_heuristic";
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
context.xlen_type().fn_type(
&[
// Input length
context.xlen_type().into(),
// Output length
context.xlen_type().into(),
// Gas
context.xlen_type().into(),
// Deposit limit value pointer
context.llvm().ptr_type(AddressSpace::Stack.into()).into(),
],
false,
)
}
fn emit_body<'ctx>(
&self,
context: &mut Context<'ctx, D>,
) -> anyhow::Result<Option<inkwell::values::BasicValueEnum<'ctx>>> {
let input_length = Self::paramater(context, 0).into_int_value();
let output_length = Self::paramater(context, 1).into_int_value();
let gas = Self::paramater(context, 2).into_int_value();
let deposit_pointer = Self::paramater(context, 3).into_pointer_value();
// Branch-free SSA implementation: First derive the heuristic boolean (int1) value.
let input_length_or_output_length = context.builder().build_or(
input_length,
output_length,
"input_length_or_output_length",
)?;
let is_no_input_no_output = context.builder().build_int_compare(
inkwell::IntPredicate::EQ,
context.xlen_type().const_zero(),
input_length_or_output_length,
"is_no_input_no_output",
)?;
let gas_stipend = context
.xlen_type()
.const_int(SOLIDITY_TRANSFER_GAS_STIPEND_THRESHOLD, false);
let is_gas_stipend_for_transfer_or_send = context.builder().build_int_compare(
inkwell::IntPredicate::EQ,
gas,
gas_stipend,
"is_gas_stipend_for_transfer_or_send",
)?;
let is_balance_transfer = context.builder().build_and(
is_no_input_no_output,
is_gas_stipend_for_transfer_or_send,
"is_balance_transfer",
)?;
let is_regular_call = context
.builder()
.build_not(is_balance_transfer, "is_balance_transfer_inverted")?;
// Call flag: Left shift the heuristic boolean value.
let is_regular_call_xlen = context.builder().build_int_z_extend(
is_regular_call,
context.xlen_type(),
"is_balance_transfer_xlen",
)?;
let call_flags = context.builder().build_left_shift(
is_regular_call_xlen,
context.xlen_type().const_int(3, false),
"flags",
)?;
// Deposit limit value: Sign-extended the heuristic boolean value.
let deposit_limit_value = context.builder().build_int_s_extend(
is_regular_call,
context.word_type(),
"deposit_limit_value",
)?;
context.build_store(
Pointer::new(context.word_type(), AddressSpace::Stack, deposit_pointer),
deposit_limit_value,
)?;
Ok(Some(call_flags.into()))
}
}
impl<D> WriteLLVM<D> for CallReentrancyHeuristic
where
D: Dependency + Clone,
{
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
<Self as RuntimeFunction<_>>::declare(self, context)
}
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
<Self as RuntimeFunction<_>>::emit(&self, context)
}
}
/// The Solidity `address.transfer` and `address.send` call detection heuristic.
///
/// # Why
@@ -533,7 +236,7 @@ where
///
/// # Returns
/// The call flags xlen `IntValue` and the deposit limit word `IntValue`.
fn _call_reentrancy_heuristic<'ctx, D>(
fn call_reentrancy_heuristic<'ctx, D>(
context: &mut Context<'ctx, D>,
gas: inkwell::values::IntValue<'ctx>,
input_length: inkwell::values::IntValue<'ctx>,
@@ -29,7 +29,8 @@ where
pub fn load<'ctx, D>(
context: &mut Context<'ctx, D>,
offset: inkwell::values::IntValue<'ctx>,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
assignment_pointer: &mut Option<inkwell::values::PointerValue<'ctx>>,
) -> anyhow::Result<Option<inkwell::values::BasicValueEnum<'ctx>>>
where
D: Dependency + Clone,
{
@@ -40,7 +41,7 @@ where
offset,
"memory_load_pointer",
);
context.build_load(pointer, "memory_load_result")
context.build_load_assign(pointer, "memory_load_result", assignment_pointer)
}
/// Translates the `mstore` instruction.
+27 -11
View File
@@ -13,16 +13,32 @@ use crate::PolkaVMStoreTransientStorageWordFunction;
pub fn load<'ctx, D>(
context: &mut Context<'ctx, D>,
position: &PolkaVMArgument<'ctx>,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
assignment_pointer: &mut Option<inkwell::values::PointerValue<'ctx>>,
) -> anyhow::Result<Option<inkwell::values::BasicValueEnum<'ctx>>>
where
D: Dependency + Clone,
{
let name = <PolkaVMLoadStorageWordFunction as RuntimeFunction<D>>::NAME;
let _name = <PolkaVMLoadStorageWordFunction as RuntimeFunction<D>>::NAME;
let declaration = <PolkaVMLoadStorageWordFunction as RuntimeFunction<D>>::declaration(context);
let arguments = [position.to_pointer(context)?.value.into()];
Ok(context
.build_call(declaration, &arguments, "storage_load")
.unwrap_or_else(|| panic!("runtime function {name} should return a value")))
match assignment_pointer.take() {
Some(assignment_pointer) => {
let arguments = [
position.as_pointer(context)?.value.into(),
assignment_pointer.into(),
];
context.build_call(declaration, &arguments, "storage_load");
Ok(None)
}
None => {
let pointer = context.build_alloca_at_entry(context.word_type(), "pointer");
let arguments = [
position.as_pointer(context)?.value.into(),
pointer.value.into(),
];
context.build_call(declaration, &arguments, "storage_load");
Ok(Some(context.build_load(pointer, "storage_value")?))
}
}
}
/// Translates the storage store.
@@ -36,8 +52,8 @@ where
{
let declaration = <PolkaVMStoreStorageWordFunction as RuntimeFunction<D>>::declaration(context);
let arguments = [
position.to_pointer(context)?.value.into(),
value.to_pointer(context)?.value.into(),
position.as_pointer(context)?.value.into(),
value.as_pointer(context)?.value.into(),
];
context.build_call(declaration, &arguments, "storage_store");
Ok(())
@@ -52,7 +68,7 @@ where
D: Dependency + Clone,
{
let name = <PolkaVMLoadTransientStorageWordFunction as RuntimeFunction<D>>::NAME;
let arguments = [position.to_pointer(context)?.value.into()];
let arguments = [position.as_pointer(context)?.value.into()];
let declaration =
<PolkaVMLoadTransientStorageWordFunction as RuntimeFunction<D>>::declaration(context);
Ok(context
@@ -72,8 +88,8 @@ where
let declaration =
<PolkaVMStoreTransientStorageWordFunction as RuntimeFunction<D>>::declaration(context);
let arguments = [
position.to_pointer(context)?.value.into(),
value.to_pointer(context)?.value.into(),
position.as_pointer(context)?.value.into(),
value.as_pointer(context)?.value.into(),
];
context.build_call(declaration, &arguments, "transient_storage_store");
Ok(())
@@ -121,11 +121,6 @@ where
) -> anyhow::Result<()> {
context.set_debug_location(self.location.line, 0, None)?;
let value = match self.initializer.into_llvm(context)? {
Some(value) => value,
None => return Ok(()),
};
if self.bindings.len() == 1 {
let identifier = self.bindings.remove(0);
let pointer = context
@@ -139,11 +134,28 @@ where
identifier.inner,
)
})?;
context.build_store(pointer, value.to_value(context)?)?;
let mut assignment_pointer = Some(pointer.value);
let value = match self
.initializer
.into_llvm(context, &mut assignment_pointer)?
{
Some(value) => value,
None => return Ok(()),
};
if assignment_pointer.is_some() {
context.build_store(pointer, value.access(context)?)?;
}
return Ok(());
}
let value = value.to_value(context)?;
let value = match self.initializer.into_llvm(context, &mut None)? {
Some(value) => value,
None => return Ok(()),
};
let value = value.access(context)?;
let llvm_type = value.into_struct_value().get_type();
let tuple_pointer = context.build_alloca(llvm_type, "assignment_pointer");
context.build_store(tuple_pointer, value)?;
@@ -184,7 +184,7 @@ where
block.into_llvm(context)?;
}
Statement::Expression(expression) => {
expression.into_llvm(context)?;
expression.into_llvm(context, &mut None)?;
}
Statement::VariableDeclaration(statement) => statement.into_llvm(context)?,
Statement::Assignment(statement) => statement.into_llvm(context)?,
@@ -118,6 +118,7 @@ impl FunctionCall {
pub fn into_llvm<'ctx, D>(
mut self,
context: &mut revive_llvm_context::PolkaVMContext<'ctx, D>,
assignment_pointer: &mut Option<inkwell::values::PointerValue<'ctx>>,
) -> anyhow::Result<Option<inkwell::values::BasicValueEnum<'ctx>>>
where
D: revive_llvm_context::PolkaVMDependency + Clone,
@@ -129,9 +130,9 @@ impl FunctionCall {
let mut values = Vec::with_capacity(self.arguments.len());
for argument in self.arguments.into_iter().rev() {
let value = argument
.into_llvm(context)?
.into_llvm(context, &mut None)?
.expect("Always exists")
.to_value(context)?;
.access(context)?;
values.push(value);
}
values.reverse();
@@ -416,8 +417,8 @@ impl FunctionCall {
revive_llvm_context::polkavm_evm_memory::load(
context,
arguments[0].into_int_value(),
assignment_pointer,
)
.map(Some)
}
Name::MStore => {
let arguments = self.pop_arguments_llvm::<D, 2>(context)?;
@@ -465,7 +466,11 @@ impl FunctionCall {
Name::SLoad => {
let arguments = self.pop_arguments::<D, 1>(context)?;
revive_llvm_context::polkavm_evm_storage::load(context, &arguments[0]).map(Some)
revive_llvm_context::polkavm_evm_storage::load(
context,
&arguments[0],
assignment_pointer,
)
}
Name::SStore => {
let arguments = self.pop_arguments::<D, 2>(context)?;
@@ -510,7 +515,7 @@ impl FunctionCall {
let offset = context.solidity_mut().allocate_immutable(key.as_str())
/ revive_common::BYTE_LENGTH_WORD;
let index = context.xlen_type().const_int(offset as u64, false);
let value = arguments[2].to_value(context)?.into_int_value();
let value = arguments[2].access(context)?.into_int_value();
revive_llvm_context::polkavm_evm_immutable::store(context, index, value)
.map(|_| None)
}
@@ -716,16 +721,15 @@ impl FunctionCall {
Name::Call => {
let arguments = self.pop_arguments::<D, 7>(context)?;
let gas = &arguments[0];
let address = &arguments[1];
let value = &arguments[2];
let input_offset = &arguments[3];
let input_size = &arguments[4];
let output_offset = &arguments[5];
let output_size = &arguments[6];
let gas = arguments[0].access(context)?.into_int_value();
let address = arguments[1].access(context)?.into_int_value();
let value = arguments[2].access(context)?.into_int_value();
let input_offset = arguments[3].access(context)?.into_int_value();
let input_size = arguments[4].access(context)?.into_int_value();
let output_offset = arguments[5].access(context)?.into_int_value();
let output_size = arguments[6].access(context)?.into_int_value();
let simulation_address: Vec<Option<num::BigUint>> = arguments
.clone()
.into_iter()
.map(|mut argument| argument.constant.take())
.collect();
@@ -747,15 +751,14 @@ impl FunctionCall {
Name::StaticCall => {
let arguments = self.pop_arguments::<D, 6>(context)?;
let gas = &arguments[0];
let address = &arguments[1];
let input_offset = &arguments[2];
let input_size = &arguments[3];
let output_offset = &arguments[4];
let output_size = &arguments[5];
let gas = arguments[0].access(context)?.into_int_value();
let address = arguments[1].access(context)?.into_int_value();
let input_offset = arguments[2].access(context)?.into_int_value();
let input_size = arguments[3].access(context)?.into_int_value();
let output_offset = arguments[4].access(context)?.into_int_value();
let output_size = arguments[5].access(context)?.into_int_value();
let simulation_address: Vec<Option<num::BigUint>> = arguments
.clone()
.into_iter()
.map(|mut argument| argument.constant.take())
.collect();
@@ -777,12 +780,12 @@ impl FunctionCall {
Name::DelegateCall => {
let arguments = self.pop_arguments::<D, 6>(context)?;
let gas = arguments[0].to_value(context)?.into_int_value();
let address = arguments[1].to_value(context)?.into_int_value();
let input_offset = arguments[2].to_value(context)?.into_int_value();
let input_size = arguments[3].to_value(context)?.into_int_value();
let output_offset = arguments[4].to_value(context)?.into_int_value();
let output_size = arguments[5].to_value(context)?.into_int_value();
let gas = arguments[0].access(context)?.into_int_value();
let address = arguments[1].access(context)?.into_int_value();
let input_offset = arguments[2].access(context)?.into_int_value();
let input_size = arguments[3].access(context)?.into_int_value();
let output_offset = arguments[4].access(context)?.into_int_value();
let output_size = arguments[5].access(context)?.into_int_value();
let simulation_address: Vec<Option<num::BigUint>> = arguments
.into_iter()
@@ -843,7 +846,7 @@ impl FunctionCall {
})?;
revive_llvm_context::polkavm_evm_create::contract_hash(context, identifier)
.and_then(|argument| argument.to_value(context))
.and_then(|argument| argument.access(context))
.map(Some)
}
Name::DataSize => {
@@ -854,7 +857,7 @@ impl FunctionCall {
})?;
revive_llvm_context::polkavm_evm_create::header_size(context, identifier)
.and_then(|argument| argument.to_value(context))
.and_then(|argument| argument.access(context))
.map(Some)
}
Name::DataCopy => {
@@ -991,9 +994,9 @@ impl FunctionCall {
for expression in self.arguments.drain(0..N).rev() {
arguments.push(
expression
.into_llvm(context)?
.into_llvm(context, &mut None)?
.expect("Always exists")
.to_value(context)?,
.access(context)?,
);
}
arguments.reverse();
@@ -1011,7 +1014,11 @@ impl FunctionCall {
{
let mut arguments = Vec::with_capacity(N);
for expression in self.arguments.drain(0..N).rev() {
arguments.push(expression.into_llvm(context)?.expect("Always exists"));
arguments.push(
expression
.into_llvm(context, &mut None)?
.expect("Always exists"),
);
}
arguments.reverse();
@@ -101,6 +101,7 @@ impl Expression {
pub fn into_llvm<'ctx, D>(
self,
context: &mut revive_llvm_context::PolkaVMContext<'ctx, D>,
assignment_pointer: &mut Option<inkwell::values::PointerValue<'ctx>>,
) -> anyhow::Result<Option<revive_llvm_context::PolkaVMArgument<'ctx>>>
where
D: revive_llvm_context::PolkaVMDependency + Clone,
@@ -139,7 +140,7 @@ impl Expression {
}))
}
Self::FunctionCall(call) => Ok(call
.into_llvm(context)?
.into_llvm(context, assignment_pointer)?
.map(revive_llvm_context::PolkaVMArgument::value)),
}
}
@@ -76,9 +76,9 @@ where
context.set_basic_block(condition_block);
let condition = self
.condition
.into_llvm(context)?
.into_llvm(context, &mut None)?
.expect("Always exists")
.to_value(context)?
.access(context)?
.into_int_value();
let condition = context.builder().build_int_z_extend_or_bit_cast(
condition,
@@ -55,9 +55,9 @@ where
fn into_llvm(self, context: &mut revive_llvm_context::PolkaVMContext<D>) -> anyhow::Result<()> {
let condition = self
.condition
.into_llvm(context)?
.into_llvm(context, &mut None)?
.expect("Always exists")
.to_value(context)?
.access(context)?
.into_int_value();
let condition = context.builder().build_int_z_extend_or_bit_cast(
condition,
@@ -188,9 +188,6 @@ where
revive_llvm_context::PolkaVMLoadImmutableDataFunction.declare(context)?;
revive_llvm_context::PolkaVMStoreImmutableDataFunction.declare(context)?;
revive_llvm_context::PolkaVMCallFunction.declare(context)?;
revive_llvm_context::PolkaVMCallReentrancyHeuristicFunction.declare(context)?;
revive_llvm_context::PolkaVMLoadHeapWordFunction.declare(context)?;
revive_llvm_context::PolkaVMStoreHeapWordFunction.declare(context)?;
revive_llvm_context::PolkaVMLoadStorageWordFunction.declare(context)?;
@@ -243,9 +240,6 @@ where
revive_llvm_context::PolkaVMLoadImmutableDataFunction.into_llvm(context)?;
revive_llvm_context::PolkaVMStoreImmutableDataFunction.into_llvm(context)?;
revive_llvm_context::PolkaVMCallFunction.into_llvm(context)?;
revive_llvm_context::PolkaVMCallReentrancyHeuristicFunction.into_llvm(context)?;
revive_llvm_context::PolkaVMLoadHeapWordFunction.into_llvm(context)?;
revive_llvm_context::PolkaVMStoreHeapWordFunction.into_llvm(context)?;
revive_llvm_context::PolkaVMLoadStorageWordFunction.into_llvm(context)?;
@@ -123,7 +123,7 @@ where
D: revive_llvm_context::PolkaVMDependency + Clone,
{
fn into_llvm(self, context: &mut revive_llvm_context::PolkaVMContext<D>) -> anyhow::Result<()> {
let scrutinee = self.expression.into_llvm(context)?;
let scrutinee = self.expression.into_llvm(context, &mut None)?;
if self.cases.is_empty() {
if let Some(block) = self.default {
@@ -137,7 +137,7 @@ where
let mut branches = Vec::with_capacity(self.cases.len());
for (index, case) in self.cases.into_iter().enumerate() {
let constant = case.literal.into_llvm(context)?.to_value(context)?;
let constant = case.literal.into_llvm(context)?.access(context)?;
let expression_block = context
.append_basic_block(format!("switch_case_branch_{}_block", index + 1).as_str());
@@ -163,7 +163,7 @@ where
context.builder().build_switch(
scrutinee
.expect("Always exists")
.to_value(context)?
.access(context)?
.into_int_value(),
default_block,
branches.as_slice(),
@@ -110,8 +110,9 @@ where
.borrow_mut()
.insert_stack_pointer(identifier.inner.clone(), pointer);
let mut assignment_pointer = Some(pointer.value);
let value = if let Some(expression) = self.expression {
match expression.into_llvm(context)? {
match expression.into_llvm(context, &mut assignment_pointer)? {
Some(mut value) => {
if let Some(constant) = value.constant.take() {
context
@@ -121,14 +122,16 @@ where
.insert_constant(identifier.inner.clone(), constant);
}
value.to_value(context)?
value.access(context)?
}
None => r#type.const_zero().as_basic_value_enum(),
}
} else {
r#type.const_zero().as_basic_value_enum()
};
context.build_store(pointer, value)?;
if assignment_pointer.is_some() {
context.build_store(pointer, value)?;
}
return Ok(());
}
@@ -156,7 +159,7 @@ where
None => return Ok(()),
};
let location = expression.location();
let expression = match expression.into_llvm(context)? {
let expression = match expression.into_llvm(context, &mut None)? {
Some(expression) => expression,
None => return Ok(()),
};
@@ -175,7 +178,7 @@ where
.collect::<Vec<inkwell::types::BasicTypeEnum<'ctx>>>()
.as_slice(),
);
let value = expression.to_value(context)?;
let value = expression.access(context)?;
if value.get_type() != llvm_type.as_basic_type_enum() {
anyhow::bail!(
"{} Assignment to {:?} received an invalid number of arguments",