Compare commits

..

1 Commits

Author SHA1 Message Date
Cyrill Leutwiler 498d68b7e6 wip
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
2025-04-16 15:42:09 +02:00
18 changed files with 485 additions and 274 deletions
+5 -5
View File
@@ -1,10 +1,10 @@
{
"Baseline": 950,
"Computation": 2262,
"DivisionArithmetics": 8915,
"ERC20": 17233,
"Computation": 2222,
"DivisionArithmetics": 8802,
"ERC20": 17601,
"Events": 1628,
"FibonacciIterative": 1485,
"Flipper": 2132,
"SHA1": 8381
"Flipper": 2089,
"SHA1": 8230
}
+2
View File
@@ -48,6 +48,8 @@ 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 access<D: crate::polkavm::Dependency + Clone>(
pub fn to_value<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 load if `self` is a pointer argument.
pub fn as_pointer<D: crate::polkavm::Dependency + Clone>(
/// Will emit a stack store if `self` is a value argument.
pub fn to_pointer<D: crate::polkavm::Dependency + Clone>(
&self,
context: &crate::polkavm::context::Context<'ctx, D>,
) -> anyhow::Result<crate::polkavm::context::Pointer<'ctx>> {
+14 -48
View File
@@ -773,49 +773,27 @@ where
.into())
}
/// Builds a stack load instruction with a direct assignment.
/// 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_assign(
pub fn build_load(
&self,
pointer: Pointer<'ctx>,
name: &str,
assignment_pointer: &mut Option<inkwell::values::PointerValue<'ctx>>,
) -> anyhow::Result<Option<inkwell::values::BasicValueEnum<'ctx>>> {
) -> anyhow::Result<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);
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")?))
}
}
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")
}))
}
AddressSpace::Stack => {
let value = self
@@ -828,23 +806,11 @@ where
.set_alignment(revive_common::BYTE_LENGTH_STACK_ALIGN as u32)
.expect("Alignment is valid");
Ok(Some(value))
Ok(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,8 +2,6 @@
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;
@@ -19,13 +17,9 @@ where
const NAME: &'static str = "__revive_load_heap_word";
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
context.void_type().fn_type(
&[
context.xlen_type().into(),
context.llvm().ptr_type(Default::default()).into(),
],
false,
)
context
.word_type()
.fn_type(&[context.xlen_type().into()], false)
}
fn emit_body<'ctx>(
@@ -33,7 +27,6 @@ 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);
@@ -49,11 +42,7 @@ where
.expect("Alignment is valid");
let swapped_value = context.build_byte_swap(value)?;
context.build_store(
Pointer::new(context.word_type(), AddressSpace::Stack, assignment_pointer),
swapped_value,
)?;
Ok(None)
Ok(Some(swapped_value))
}
}
@@ -2,8 +2,6 @@
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;
@@ -19,27 +17,20 @@ where
const NAME: &'static str = "__revive_load_storage_word";
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
context.void_type().fn_type(
&[
context.llvm().ptr_type(Default::default()).into(),
context.llvm().ptr_type(Default::default()).into(),
],
false,
)
context
.word_type()
.fn_type(&[context.llvm().ptr_type(Default::default()).into()], false)
}
fn emit_body<'ctx>(
&self,
context: &mut Context<'ctx, D>,
) -> anyhow::Result<Option<BasicValueEnum<'ctx>>> {
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)
Ok(Some(emit_load(
context,
Self::paramater(context, 0),
false,
)?))
}
}
+378 -81
View File
@@ -2,117 +2,310 @@
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: 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>,
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>,
_constants: Vec<Option<num::BigUint>>,
static_call: bool,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
where
D: Dependency + Clone,
{
let address_pointer = context.build_address_argument_store(address)?;
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 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 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 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 deposit_limit_pointer =
context.build_alloca_at_entry(context.word_type(), "deposit_limit_pointer");
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 = if static_call {
context.build_store(deposit_limit_pointer, context.word_type().const_zero())?;
let flags = REENTRANT_CALL_FLAG | STATIC_CALL_FLAG;
(
context.xlen_type().const_int(flags as u64, false),
context.word_type().const_zero(),
)
context.xlen_type().const_int(flags as u64, false)
} else {
call_reentrancy_heuristic(context, gas, input_length, output_length)?
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()
};
let deposit_pointer = context.build_alloca_at_entry(context.word_type(), "deposit_pointer");
context.build_store(deposit_pointer, deposit_limit_value)?;
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 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 address_pointer =
context.build_address_argument_store(address.to_value(context)?.into_int_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"))
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"))
.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")?
.build_int_z_extend(result, context.word_type(), "call_result")?
.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)]
@@ -216,6 +409,110 @@ 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
@@ -236,7 +533,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,8 +29,7 @@ where
pub fn load<'ctx, D>(
context: &mut Context<'ctx, D>,
offset: inkwell::values::IntValue<'ctx>,
assignment_pointer: &mut Option<inkwell::values::PointerValue<'ctx>>,
) -> anyhow::Result<Option<inkwell::values::BasicValueEnum<'ctx>>>
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
where
D: Dependency + Clone,
{
@@ -41,7 +40,7 @@ where
offset,
"memory_load_pointer",
);
context.build_load_assign(pointer, "memory_load_result", assignment_pointer)
context.build_load(pointer, "memory_load_result")
}
/// Translates the `mstore` instruction.
+11 -27
View File
@@ -13,32 +13,16 @@ use crate::PolkaVMStoreTransientStorageWordFunction;
pub fn load<'ctx, D>(
context: &mut Context<'ctx, D>,
position: &PolkaVMArgument<'ctx>,
assignment_pointer: &mut Option<inkwell::values::PointerValue<'ctx>>,
) -> anyhow::Result<Option<inkwell::values::BasicValueEnum<'ctx>>>
) -> anyhow::Result<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);
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")?))
}
}
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")))
}
/// Translates the storage store.
@@ -52,8 +36,8 @@ where
{
let declaration = <PolkaVMStoreStorageWordFunction as RuntimeFunction<D>>::declaration(context);
let arguments = [
position.as_pointer(context)?.value.into(),
value.as_pointer(context)?.value.into(),
position.to_pointer(context)?.value.into(),
value.to_pointer(context)?.value.into(),
];
context.build_call(declaration, &arguments, "storage_store");
Ok(())
@@ -68,7 +52,7 @@ where
D: Dependency + Clone,
{
let name = <PolkaVMLoadTransientStorageWordFunction as RuntimeFunction<D>>::NAME;
let arguments = [position.as_pointer(context)?.value.into()];
let arguments = [position.to_pointer(context)?.value.into()];
let declaration =
<PolkaVMLoadTransientStorageWordFunction as RuntimeFunction<D>>::declaration(context);
Ok(context
@@ -88,8 +72,8 @@ where
let declaration =
<PolkaVMStoreTransientStorageWordFunction as RuntimeFunction<D>>::declaration(context);
let arguments = [
position.as_pointer(context)?.value.into(),
value.as_pointer(context)?.value.into(),
position.to_pointer(context)?.value.into(),
value.to_pointer(context)?.value.into(),
];
context.build_call(declaration, &arguments, "transient_storage_store");
Ok(())
@@ -121,6 +121,11 @@ 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
@@ -134,28 +139,11 @@ where
identifier.inner,
)
})?;
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)?)?;
}
context.build_store(pointer, value.to_value(context)?)?;
return Ok(());
}
let value = match self.initializer.into_llvm(context, &mut None)? {
Some(value) => value,
None => return Ok(()),
};
let value = value.access(context)?;
let value = value.to_value(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, &mut None)?;
expression.into_llvm(context)?;
}
Statement::VariableDeclaration(statement) => statement.into_llvm(context)?,
Statement::Assignment(statement) => statement.into_llvm(context)?,
@@ -118,7 +118,6 @@ 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,
@@ -130,9 +129,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, &mut None)?
.into_llvm(context)?
.expect("Always exists")
.access(context)?;
.to_value(context)?;
values.push(value);
}
values.reverse();
@@ -417,8 +416,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)?;
@@ -466,11 +465,7 @@ impl FunctionCall {
Name::SLoad => {
let arguments = self.pop_arguments::<D, 1>(context)?;
revive_llvm_context::polkavm_evm_storage::load(
context,
&arguments[0],
assignment_pointer,
)
revive_llvm_context::polkavm_evm_storage::load(context, &arguments[0]).map(Some)
}
Name::SStore => {
let arguments = self.pop_arguments::<D, 2>(context)?;
@@ -515,7 +510,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].access(context)?.into_int_value();
let value = arguments[2].to_value(context)?.into_int_value();
revive_llvm_context::polkavm_evm_immutable::store(context, index, value)
.map(|_| None)
}
@@ -721,15 +716,16 @@ impl FunctionCall {
Name::Call => {
let arguments = self.pop_arguments::<D, 7>(context)?;
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 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 simulation_address: Vec<Option<num::BigUint>> = arguments
.clone()
.into_iter()
.map(|mut argument| argument.constant.take())
.collect();
@@ -751,14 +747,15 @@ impl FunctionCall {
Name::StaticCall => {
let arguments = self.pop_arguments::<D, 6>(context)?;
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 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 simulation_address: Vec<Option<num::BigUint>> = arguments
.clone()
.into_iter()
.map(|mut argument| argument.constant.take())
.collect();
@@ -780,12 +777,12 @@ impl FunctionCall {
Name::DelegateCall => {
let arguments = self.pop_arguments::<D, 6>(context)?;
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 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 simulation_address: Vec<Option<num::BigUint>> = arguments
.into_iter()
@@ -846,7 +843,7 @@ impl FunctionCall {
})?;
revive_llvm_context::polkavm_evm_create::contract_hash(context, identifier)
.and_then(|argument| argument.access(context))
.and_then(|argument| argument.to_value(context))
.map(Some)
}
Name::DataSize => {
@@ -857,7 +854,7 @@ impl FunctionCall {
})?;
revive_llvm_context::polkavm_evm_create::header_size(context, identifier)
.and_then(|argument| argument.access(context))
.and_then(|argument| argument.to_value(context))
.map(Some)
}
Name::DataCopy => {
@@ -994,9 +991,9 @@ impl FunctionCall {
for expression in self.arguments.drain(0..N).rev() {
arguments.push(
expression
.into_llvm(context, &mut None)?
.into_llvm(context)?
.expect("Always exists")
.access(context)?,
.to_value(context)?,
);
}
arguments.reverse();
@@ -1014,11 +1011,7 @@ impl FunctionCall {
{
let mut arguments = Vec::with_capacity(N);
for expression in self.arguments.drain(0..N).rev() {
arguments.push(
expression
.into_llvm(context, &mut None)?
.expect("Always exists"),
);
arguments.push(expression.into_llvm(context)?.expect("Always exists"));
}
arguments.reverse();
@@ -101,7 +101,6 @@ 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,
@@ -140,7 +139,7 @@ impl Expression {
}))
}
Self::FunctionCall(call) => Ok(call
.into_llvm(context, assignment_pointer)?
.into_llvm(context)?
.map(revive_llvm_context::PolkaVMArgument::value)),
}
}
@@ -76,9 +76,9 @@ where
context.set_basic_block(condition_block);
let condition = self
.condition
.into_llvm(context, &mut None)?
.into_llvm(context)?
.expect("Always exists")
.access(context)?
.to_value(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, &mut None)?
.into_llvm(context)?
.expect("Always exists")
.access(context)?
.to_value(context)?
.into_int_value();
let condition = context.builder().build_int_z_extend_or_bit_cast(
condition,
@@ -188,6 +188,9 @@ 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)?;
@@ -240,6 +243,9 @@ 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, &mut None)?;
let scrutinee = self.expression.into_llvm(context)?;
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)?.access(context)?;
let constant = case.literal.into_llvm(context)?.to_value(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")
.access(context)?
.to_value(context)?
.into_int_value(),
default_block,
branches.as_slice(),
@@ -110,9 +110,8 @@ 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, &mut assignment_pointer)? {
match expression.into_llvm(context)? {
Some(mut value) => {
if let Some(constant) = value.constant.take() {
context
@@ -122,16 +121,14 @@ where
.insert_constant(identifier.inner.clone(), constant);
}
value.access(context)?
value.to_value(context)?
}
None => r#type.const_zero().as_basic_value_enum(),
}
} else {
r#type.const_zero().as_basic_value_enum()
};
if assignment_pointer.is_some() {
context.build_store(pointer, value)?;
}
context.build_store(pointer, value)?;
return Ok(());
}
@@ -159,7 +156,7 @@ where
None => return Ok(()),
};
let location = expression.location();
let expression = match expression.into_llvm(context, &mut None)? {
let expression = match expression.into_llvm(context)? {
Some(expression) => expression,
None => return Ok(()),
};
@@ -178,7 +175,7 @@ where
.collect::<Vec<inkwell::types::BasicTypeEnum<'ctx>>>()
.as_slice(),
);
let value = expression.access(context)?;
let value = expression.to_value(context)?;
if value.get_type() != llvm_type.as_basic_type_enum() {
anyhow::bail!(
"{} Assignment to {:?} received an invalid number of arguments",