mirror of
https://github.com/pezkuwichain/revive.git
synced 2026-06-13 08:21:09 +00:00
@@ -2,9 +2,9 @@
|
|||||||
"Baseline": 950,
|
"Baseline": 950,
|
||||||
"Computation": 2222,
|
"Computation": 2222,
|
||||||
"DivisionArithmetics": 8802,
|
"DivisionArithmetics": 8802,
|
||||||
"ERC20": 17602,
|
"ERC20": 17601,
|
||||||
"Events": 1628,
|
"Events": 1628,
|
||||||
"FibonacciIterative": 1485,
|
"FibonacciIterative": 1485,
|
||||||
"Flipper": 2082,
|
"Flipper": 2089,
|
||||||
"SHA1": 8230
|
"SHA1": 8230
|
||||||
}
|
}
|
||||||
@@ -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::arithmetic as polkavm_evm_arithmetic;
|
||||||
pub use self::polkavm::evm::bitwise as polkavm_evm_bitwise;
|
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 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::calldata as polkavm_evm_calldata;
|
||||||
pub use self::polkavm::evm::comparison as polkavm_evm_comparison;
|
pub use self::polkavm::evm::comparison as polkavm_evm_comparison;
|
||||||
pub use self::polkavm::evm::context as polkavm_evm_contract_context;
|
pub use self::polkavm::evm::context as polkavm_evm_contract_context;
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ impl<'ctx> Argument<'ctx> {
|
|||||||
/// Access the underlying value.
|
/// Access the underlying value.
|
||||||
///
|
///
|
||||||
/// Will emit a stack load if `self` is a pointer argument.
|
/// 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,
|
&self,
|
||||||
context: &crate::polkavm::context::Context<'ctx, D>,
|
context: &crate::polkavm::context::Context<'ctx, D>,
|
||||||
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>> {
|
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>> {
|
||||||
@@ -78,8 +78,8 @@ impl<'ctx> Argument<'ctx> {
|
|||||||
|
|
||||||
/// Access the underlying value.
|
/// Access the underlying value.
|
||||||
///
|
///
|
||||||
/// Will emit a stack load if `self` is a pointer argument.
|
/// Will emit a stack store if `self` is a value argument.
|
||||||
pub fn as_pointer<D: crate::polkavm::Dependency + Clone>(
|
pub fn to_pointer<D: crate::polkavm::Dependency + Clone>(
|
||||||
&self,
|
&self,
|
||||||
context: &crate::polkavm::context::Context<'ctx, D>,
|
context: &crate::polkavm::context::Context<'ctx, D>,
|
||||||
) -> anyhow::Result<crate::polkavm::context::Pointer<'ctx>> {
|
) -> anyhow::Result<crate::polkavm::context::Pointer<'ctx>> {
|
||||||
|
|||||||
@@ -2,117 +2,310 @@
|
|||||||
|
|
||||||
use inkwell::values::BasicValue;
|
use inkwell::values::BasicValue;
|
||||||
|
|
||||||
|
use crate::polkavm::context::address_space::AddressSpace;
|
||||||
use crate::polkavm::context::argument::Argument;
|
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::context::Context;
|
||||||
use crate::polkavm::Dependency;
|
use crate::polkavm::Dependency;
|
||||||
|
use crate::polkavm::WriteLLVM;
|
||||||
|
|
||||||
const STATIC_CALL_FLAG: u32 = 0b0001_0000;
|
const STATIC_CALL_FLAG: u32 = 0b0001_0000;
|
||||||
const REENTRANT_CALL_FLAG: u32 = 0b0000_1000;
|
const REENTRANT_CALL_FLAG: u32 = 0b0000_1000;
|
||||||
const SOLIDITY_TRANSFER_GAS_STIPEND_THRESHOLD: u64 = 2300;
|
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.
|
/// Translates a contract call.
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub fn call<'ctx, D>(
|
pub fn call<'ctx, D>(
|
||||||
context: &mut Context<'ctx, D>,
|
context: &mut Context<'ctx, D>,
|
||||||
gas: inkwell::values::IntValue<'ctx>,
|
gas: &Argument<'ctx>,
|
||||||
address: inkwell::values::IntValue<'ctx>,
|
address: &Argument<'ctx>,
|
||||||
value: Option<inkwell::values::IntValue<'ctx>>,
|
value: Option<&Argument<'ctx>>,
|
||||||
input_offset: inkwell::values::IntValue<'ctx>,
|
input_offset: &Argument<'ctx>,
|
||||||
input_length: inkwell::values::IntValue<'ctx>,
|
input_length: &Argument<'ctx>,
|
||||||
output_offset: inkwell::values::IntValue<'ctx>,
|
output_offset: &Argument<'ctx>,
|
||||||
output_length: inkwell::values::IntValue<'ctx>,
|
output_length: &Argument<'ctx>,
|
||||||
_constants: Vec<Option<num::BigUint>>,
|
_constants: Vec<Option<num::BigUint>>,
|
||||||
static_call: bool,
|
static_call: bool,
|
||||||
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
|
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
|
||||||
where
|
where
|
||||||
D: Dependency + Clone,
|
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 output_offset =
|
||||||
let value_pointer = context.build_alloca_at_entry(context.word_type(), "value_pointer");
|
context.safe_truncate_int_to_xlen(output_offset.to_value(context)?.into_int_value())?;
|
||||||
context.build_store(value_pointer, 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 deposit_limit_pointer =
|
||||||
let input_length = context.safe_truncate_int_to_xlen(input_length)?;
|
context.build_alloca_at_entry(context.word_type(), "deposit_limit_pointer");
|
||||||
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 flags = if static_call {
|
||||||
let output_pointer = context.build_heap_gep(output_offset, output_length)?;
|
context.build_store(deposit_limit_pointer, context.word_type().const_zero())?;
|
||||||
|
|
||||||
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;
|
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 {
|
} 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");
|
let value_pointer = match value {
|
||||||
context.build_store(deposit_pointer, deposit_limit_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(
|
let address_pointer =
|
||||||
context.builder(),
|
context.build_address_argument_store(address.to_value(context)?.into_int_value())?;
|
||||||
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 name = <Call as RuntimeFunction<D>>::NAME;
|
||||||
let success = context
|
let arguments = &[
|
||||||
.build_runtime_call(
|
flags.into(),
|
||||||
name,
|
input_length.into(),
|
||||||
&[
|
output_length.into(),
|
||||||
flags_and_callee.into(),
|
input_offset.into(),
|
||||||
context.register_type().const_all_ones().into(),
|
output_offset.into(),
|
||||||
context.register_type().const_all_ones().into(),
|
address_pointer.value.into(),
|
||||||
deposit_and_value.into(),
|
value_pointer.value.into(),
|
||||||
input_data.into(),
|
deposit_limit_pointer.value.into(),
|
||||||
output_data.into(),
|
];
|
||||||
],
|
let declaration = <Call as RuntimeFunction<D>>::declaration(context);
|
||||||
)
|
let result = context
|
||||||
.unwrap_or_else(|| panic!("{name} should return a value"))
|
.build_call(declaration, arguments, "call_result_truncated")
|
||||||
|
.unwrap_or_else(|| panic!("runtime function {name} should return a value"))
|
||||||
.into_int_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
|
Ok(context
|
||||||
.builder()
|
.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())
|
.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)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
@@ -216,6 +409,110 @@ where
|
|||||||
.as_basic_value_enum())
|
.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.
|
/// The Solidity `address.transfer` and `address.send` call detection heuristic.
|
||||||
///
|
///
|
||||||
/// # Why
|
/// # Why
|
||||||
@@ -236,7 +533,7 @@ where
|
|||||||
///
|
///
|
||||||
/// # Returns
|
/// # Returns
|
||||||
/// The call flags xlen `IntValue` and the deposit limit word `IntValue`.
|
/// 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>,
|
context: &mut Context<'ctx, D>,
|
||||||
gas: inkwell::values::IntValue<'ctx>,
|
gas: inkwell::values::IntValue<'ctx>,
|
||||||
input_length: inkwell::values::IntValue<'ctx>,
|
input_length: inkwell::values::IntValue<'ctx>,
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ where
|
|||||||
{
|
{
|
||||||
let name = <PolkaVMLoadStorageWordFunction as RuntimeFunction<D>>::NAME;
|
let name = <PolkaVMLoadStorageWordFunction as RuntimeFunction<D>>::NAME;
|
||||||
let declaration = <PolkaVMLoadStorageWordFunction as RuntimeFunction<D>>::declaration(context);
|
let declaration = <PolkaVMLoadStorageWordFunction as RuntimeFunction<D>>::declaration(context);
|
||||||
let arguments = [position.as_pointer(context)?.value.into()];
|
let arguments = [position.to_pointer(context)?.value.into()];
|
||||||
Ok(context
|
Ok(context
|
||||||
.build_call(declaration, &arguments, "storage_load")
|
.build_call(declaration, &arguments, "storage_load")
|
||||||
.unwrap_or_else(|| panic!("runtime function {name} should return a value")))
|
.unwrap_or_else(|| panic!("runtime function {name} should return a value")))
|
||||||
@@ -36,8 +36,8 @@ where
|
|||||||
{
|
{
|
||||||
let declaration = <PolkaVMStoreStorageWordFunction as RuntimeFunction<D>>::declaration(context);
|
let declaration = <PolkaVMStoreStorageWordFunction as RuntimeFunction<D>>::declaration(context);
|
||||||
let arguments = [
|
let arguments = [
|
||||||
position.as_pointer(context)?.value.into(),
|
position.to_pointer(context)?.value.into(),
|
||||||
value.as_pointer(context)?.value.into(),
|
value.to_pointer(context)?.value.into(),
|
||||||
];
|
];
|
||||||
context.build_call(declaration, &arguments, "storage_store");
|
context.build_call(declaration, &arguments, "storage_store");
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -52,7 +52,7 @@ where
|
|||||||
D: Dependency + Clone,
|
D: Dependency + Clone,
|
||||||
{
|
{
|
||||||
let name = <PolkaVMLoadTransientStorageWordFunction as RuntimeFunction<D>>::NAME;
|
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 =
|
let declaration =
|
||||||
<PolkaVMLoadTransientStorageWordFunction as RuntimeFunction<D>>::declaration(context);
|
<PolkaVMLoadTransientStorageWordFunction as RuntimeFunction<D>>::declaration(context);
|
||||||
Ok(context
|
Ok(context
|
||||||
@@ -72,8 +72,8 @@ where
|
|||||||
let declaration =
|
let declaration =
|
||||||
<PolkaVMStoreTransientStorageWordFunction as RuntimeFunction<D>>::declaration(context);
|
<PolkaVMStoreTransientStorageWordFunction as RuntimeFunction<D>>::declaration(context);
|
||||||
let arguments = [
|
let arguments = [
|
||||||
position.as_pointer(context)?.value.into(),
|
position.to_pointer(context)?.value.into(),
|
||||||
value.as_pointer(context)?.value.into(),
|
value.to_pointer(context)?.value.into(),
|
||||||
];
|
];
|
||||||
context.build_call(declaration, &arguments, "transient_storage_store");
|
context.build_call(declaration, &arguments, "transient_storage_store");
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -139,11 +139,11 @@ where
|
|||||||
identifier.inner,
|
identifier.inner,
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
context.build_store(pointer, value.access(context)?)?;
|
context.build_store(pointer, value.to_value(context)?)?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let value = value.access(context)?;
|
let value = value.to_value(context)?;
|
||||||
let llvm_type = value.into_struct_value().get_type();
|
let llvm_type = value.into_struct_value().get_type();
|
||||||
let tuple_pointer = context.build_alloca(llvm_type, "assignment_pointer");
|
let tuple_pointer = context.build_alloca(llvm_type, "assignment_pointer");
|
||||||
context.build_store(tuple_pointer, value)?;
|
context.build_store(tuple_pointer, value)?;
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ impl FunctionCall {
|
|||||||
let value = argument
|
let value = argument
|
||||||
.into_llvm(context)?
|
.into_llvm(context)?
|
||||||
.expect("Always exists")
|
.expect("Always exists")
|
||||||
.access(context)?;
|
.to_value(context)?;
|
||||||
values.push(value);
|
values.push(value);
|
||||||
}
|
}
|
||||||
values.reverse();
|
values.reverse();
|
||||||
@@ -510,7 +510,7 @@ impl FunctionCall {
|
|||||||
let offset = context.solidity_mut().allocate_immutable(key.as_str())
|
let offset = context.solidity_mut().allocate_immutable(key.as_str())
|
||||||
/ revive_common::BYTE_LENGTH_WORD;
|
/ revive_common::BYTE_LENGTH_WORD;
|
||||||
let index = context.xlen_type().const_int(offset as u64, false);
|
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)
|
revive_llvm_context::polkavm_evm_immutable::store(context, index, value)
|
||||||
.map(|_| None)
|
.map(|_| None)
|
||||||
}
|
}
|
||||||
@@ -716,15 +716,16 @@ impl FunctionCall {
|
|||||||
Name::Call => {
|
Name::Call => {
|
||||||
let arguments = self.pop_arguments::<D, 7>(context)?;
|
let arguments = self.pop_arguments::<D, 7>(context)?;
|
||||||
|
|
||||||
let gas = arguments[0].access(context)?.into_int_value();
|
let gas = &arguments[0];
|
||||||
let address = arguments[1].access(context)?.into_int_value();
|
let address = &arguments[1];
|
||||||
let value = arguments[2].access(context)?.into_int_value();
|
let value = &arguments[2];
|
||||||
let input_offset = arguments[3].access(context)?.into_int_value();
|
let input_offset = &arguments[3];
|
||||||
let input_size = arguments[4].access(context)?.into_int_value();
|
let input_size = &arguments[4];
|
||||||
let output_offset = arguments[5].access(context)?.into_int_value();
|
let output_offset = &arguments[5];
|
||||||
let output_size = arguments[6].access(context)?.into_int_value();
|
let output_size = &arguments[6];
|
||||||
|
|
||||||
let simulation_address: Vec<Option<num::BigUint>> = arguments
|
let simulation_address: Vec<Option<num::BigUint>> = arguments
|
||||||
|
.clone()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|mut argument| argument.constant.take())
|
.map(|mut argument| argument.constant.take())
|
||||||
.collect();
|
.collect();
|
||||||
@@ -746,14 +747,15 @@ impl FunctionCall {
|
|||||||
Name::StaticCall => {
|
Name::StaticCall => {
|
||||||
let arguments = self.pop_arguments::<D, 6>(context)?;
|
let arguments = self.pop_arguments::<D, 6>(context)?;
|
||||||
|
|
||||||
let gas = arguments[0].access(context)?.into_int_value();
|
let gas = &arguments[0];
|
||||||
let address = arguments[1].access(context)?.into_int_value();
|
let address = &arguments[1];
|
||||||
let input_offset = arguments[2].access(context)?.into_int_value();
|
let input_offset = &arguments[2];
|
||||||
let input_size = arguments[3].access(context)?.into_int_value();
|
let input_size = &arguments[3];
|
||||||
let output_offset = arguments[4].access(context)?.into_int_value();
|
let output_offset = &arguments[4];
|
||||||
let output_size = arguments[5].access(context)?.into_int_value();
|
let output_size = &arguments[5];
|
||||||
|
|
||||||
let simulation_address: Vec<Option<num::BigUint>> = arguments
|
let simulation_address: Vec<Option<num::BigUint>> = arguments
|
||||||
|
.clone()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|mut argument| argument.constant.take())
|
.map(|mut argument| argument.constant.take())
|
||||||
.collect();
|
.collect();
|
||||||
@@ -775,12 +777,12 @@ impl FunctionCall {
|
|||||||
Name::DelegateCall => {
|
Name::DelegateCall => {
|
||||||
let arguments = self.pop_arguments::<D, 6>(context)?;
|
let arguments = self.pop_arguments::<D, 6>(context)?;
|
||||||
|
|
||||||
let gas = arguments[0].access(context)?.into_int_value();
|
let gas = arguments[0].to_value(context)?.into_int_value();
|
||||||
let address = arguments[1].access(context)?.into_int_value();
|
let address = arguments[1].to_value(context)?.into_int_value();
|
||||||
let input_offset = arguments[2].access(context)?.into_int_value();
|
let input_offset = arguments[2].to_value(context)?.into_int_value();
|
||||||
let input_size = arguments[3].access(context)?.into_int_value();
|
let input_size = arguments[3].to_value(context)?.into_int_value();
|
||||||
let output_offset = arguments[4].access(context)?.into_int_value();
|
let output_offset = arguments[4].to_value(context)?.into_int_value();
|
||||||
let output_size = arguments[5].access(context)?.into_int_value();
|
let output_size = arguments[5].to_value(context)?.into_int_value();
|
||||||
|
|
||||||
let simulation_address: Vec<Option<num::BigUint>> = arguments
|
let simulation_address: Vec<Option<num::BigUint>> = arguments
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@@ -841,7 +843,7 @@ impl FunctionCall {
|
|||||||
})?;
|
})?;
|
||||||
|
|
||||||
revive_llvm_context::polkavm_evm_create::contract_hash(context, identifier)
|
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)
|
.map(Some)
|
||||||
}
|
}
|
||||||
Name::DataSize => {
|
Name::DataSize => {
|
||||||
@@ -852,7 +854,7 @@ impl FunctionCall {
|
|||||||
})?;
|
})?;
|
||||||
|
|
||||||
revive_llvm_context::polkavm_evm_create::header_size(context, identifier)
|
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)
|
.map(Some)
|
||||||
}
|
}
|
||||||
Name::DataCopy => {
|
Name::DataCopy => {
|
||||||
@@ -991,7 +993,7 @@ impl FunctionCall {
|
|||||||
expression
|
expression
|
||||||
.into_llvm(context)?
|
.into_llvm(context)?
|
||||||
.expect("Always exists")
|
.expect("Always exists")
|
||||||
.access(context)?,
|
.to_value(context)?,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
arguments.reverse();
|
arguments.reverse();
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ where
|
|||||||
.condition
|
.condition
|
||||||
.into_llvm(context)?
|
.into_llvm(context)?
|
||||||
.expect("Always exists")
|
.expect("Always exists")
|
||||||
.access(context)?
|
.to_value(context)?
|
||||||
.into_int_value();
|
.into_int_value();
|
||||||
let condition = context.builder().build_int_z_extend_or_bit_cast(
|
let condition = context.builder().build_int_z_extend_or_bit_cast(
|
||||||
condition,
|
condition,
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ where
|
|||||||
.condition
|
.condition
|
||||||
.into_llvm(context)?
|
.into_llvm(context)?
|
||||||
.expect("Always exists")
|
.expect("Always exists")
|
||||||
.access(context)?
|
.to_value(context)?
|
||||||
.into_int_value();
|
.into_int_value();
|
||||||
let condition = context.builder().build_int_z_extend_or_bit_cast(
|
let condition = context.builder().build_int_z_extend_or_bit_cast(
|
||||||
condition,
|
condition,
|
||||||
|
|||||||
@@ -188,6 +188,9 @@ where
|
|||||||
revive_llvm_context::PolkaVMLoadImmutableDataFunction.declare(context)?;
|
revive_llvm_context::PolkaVMLoadImmutableDataFunction.declare(context)?;
|
||||||
revive_llvm_context::PolkaVMStoreImmutableDataFunction.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::PolkaVMLoadHeapWordFunction.declare(context)?;
|
||||||
revive_llvm_context::PolkaVMStoreHeapWordFunction.declare(context)?;
|
revive_llvm_context::PolkaVMStoreHeapWordFunction.declare(context)?;
|
||||||
revive_llvm_context::PolkaVMLoadStorageWordFunction.declare(context)?;
|
revive_llvm_context::PolkaVMLoadStorageWordFunction.declare(context)?;
|
||||||
@@ -240,6 +243,9 @@ where
|
|||||||
revive_llvm_context::PolkaVMLoadImmutableDataFunction.into_llvm(context)?;
|
revive_llvm_context::PolkaVMLoadImmutableDataFunction.into_llvm(context)?;
|
||||||
revive_llvm_context::PolkaVMStoreImmutableDataFunction.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::PolkaVMLoadHeapWordFunction.into_llvm(context)?;
|
||||||
revive_llvm_context::PolkaVMStoreHeapWordFunction.into_llvm(context)?;
|
revive_llvm_context::PolkaVMStoreHeapWordFunction.into_llvm(context)?;
|
||||||
revive_llvm_context::PolkaVMLoadStorageWordFunction.into_llvm(context)?;
|
revive_llvm_context::PolkaVMLoadStorageWordFunction.into_llvm(context)?;
|
||||||
|
|||||||
@@ -137,7 +137,7 @@ where
|
|||||||
|
|
||||||
let mut branches = Vec::with_capacity(self.cases.len());
|
let mut branches = Vec::with_capacity(self.cases.len());
|
||||||
for (index, case) in self.cases.into_iter().enumerate() {
|
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
|
let expression_block = context
|
||||||
.append_basic_block(format!("switch_case_branch_{}_block", index + 1).as_str());
|
.append_basic_block(format!("switch_case_branch_{}_block", index + 1).as_str());
|
||||||
@@ -163,7 +163,7 @@ where
|
|||||||
context.builder().build_switch(
|
context.builder().build_switch(
|
||||||
scrutinee
|
scrutinee
|
||||||
.expect("Always exists")
|
.expect("Always exists")
|
||||||
.access(context)?
|
.to_value(context)?
|
||||||
.into_int_value(),
|
.into_int_value(),
|
||||||
default_block,
|
default_block,
|
||||||
branches.as_slice(),
|
branches.as_slice(),
|
||||||
|
|||||||
@@ -121,7 +121,7 @@ where
|
|||||||
.insert_constant(identifier.inner.clone(), constant);
|
.insert_constant(identifier.inner.clone(), constant);
|
||||||
}
|
}
|
||||||
|
|
||||||
value.access(context)?
|
value.to_value(context)?
|
||||||
}
|
}
|
||||||
None => r#type.const_zero().as_basic_value_enum(),
|
None => r#type.const_zero().as_basic_value_enum(),
|
||||||
}
|
}
|
||||||
@@ -175,7 +175,7 @@ where
|
|||||||
.collect::<Vec<inkwell::types::BasicTypeEnum<'ctx>>>()
|
.collect::<Vec<inkwell::types::BasicTypeEnum<'ctx>>>()
|
||||||
.as_slice(),
|
.as_slice(),
|
||||||
);
|
);
|
||||||
let value = expression.access(context)?;
|
let value = expression.to_value(context)?;
|
||||||
if value.get_type() != llvm_type.as_basic_type_enum() {
|
if value.get_type() != llvm_type.as_basic_type_enum() {
|
||||||
anyhow::bail!(
|
anyhow::bail!(
|
||||||
"{} Assignment to {:?} received an invalid number of arguments",
|
"{} Assignment to {:?} received an invalid number of arguments",
|
||||||
|
|||||||
Reference in New Issue
Block a user