mirror of
https://github.com/pezkuwichain/revive.git
synced 2026-06-12 19:51:03 +00:00
extend mock runtime to allow executing constructors and cross contract calls
Signed-off-by: xermicus <cyrill@parity.io>
This commit is contained in:
@@ -46,7 +46,7 @@ pub static GLOBAL_VERBATIM_GETTER_PREFIX: &str = "get_global::";
|
||||
pub static GLOBAL_I256_SIZE: &str = "i256_size";
|
||||
|
||||
/// The static value size.
|
||||
pub static GLOBAL_I128_SIZE: &str = "i128_size";
|
||||
pub static GLOBAL_I160_SIZE: &str = "i160_size";
|
||||
|
||||
/// The static i64 size.
|
||||
pub static GLOBAL_I64_SIZE: &str = "i64_size";
|
||||
|
||||
@@ -16,7 +16,9 @@ pub struct Intrinsics<'ctx> {
|
||||
/// The memory copy from a generic page.
|
||||
pub memory_copy_from_generic: FunctionDeclaration<'ctx>,
|
||||
/// Performs endianness swaps on i256 values
|
||||
pub byte_swap: FunctionDeclaration<'ctx>,
|
||||
pub byte_swap_word: FunctionDeclaration<'ctx>,
|
||||
/// Performs endianness swaps on i160 values
|
||||
pub byte_swap_eth_address: FunctionDeclaration<'ctx>,
|
||||
}
|
||||
|
||||
impl<'ctx> Intrinsics<'ctx> {
|
||||
@@ -30,7 +32,10 @@ impl<'ctx> Intrinsics<'ctx> {
|
||||
pub const FUNCTION_MEMORY_COPY_FROM_GENERIC: &'static str = "llvm.memcpy.p3.p1.i256";
|
||||
|
||||
/// The corresponding intrinsic function name.
|
||||
pub const FUNCTION_BYTE_SWAP: &'static str = "llvm.bswap.i256";
|
||||
pub const FUNCTION_BYTE_SWAP_WORD: &'static str = "llvm.bswap.i256";
|
||||
|
||||
/// The corresponding intrinsic function name.
|
||||
pub const FUNCTION_BYTE_SWAP_ETH_ADDRESS: &'static str = "llvm.bswap.i160";
|
||||
|
||||
/// A shortcut constructor.
|
||||
pub fn new(
|
||||
@@ -40,6 +45,7 @@ impl<'ctx> Intrinsics<'ctx> {
|
||||
let void_type = llvm.void_type();
|
||||
let bool_type = llvm.bool_type();
|
||||
let word_type = llvm.custom_width_int_type(revive_common::BIT_LENGTH_WORD as u32);
|
||||
let address_type = llvm.custom_width_int_type(revive_common::BIT_LENGTH_ETH_ADDRESS as u32);
|
||||
let _stack_field_pointer_type = llvm.ptr_type(AddressSpace::Stack.into());
|
||||
let heap_field_pointer_type = llvm.ptr_type(AddressSpace::Heap.into());
|
||||
let generic_byte_pointer_type = llvm.ptr_type(AddressSpace::Generic.into());
|
||||
@@ -78,18 +84,25 @@ impl<'ctx> Intrinsics<'ctx> {
|
||||
false,
|
||||
),
|
||||
);
|
||||
let byte_swap = Self::declare(
|
||||
let byte_swap_word = Self::declare(
|
||||
llvm,
|
||||
module,
|
||||
Self::FUNCTION_BYTE_SWAP,
|
||||
Self::FUNCTION_BYTE_SWAP_WORD,
|
||||
word_type.fn_type(&[word_type.as_basic_type_enum().into()], false),
|
||||
);
|
||||
let byte_swap_eth_address = Self::declare(
|
||||
llvm,
|
||||
module,
|
||||
Self::FUNCTION_BYTE_SWAP_ETH_ADDRESS,
|
||||
address_type.fn_type(&[address_type.as_basic_type_enum().into()], false),
|
||||
);
|
||||
|
||||
Self {
|
||||
trap,
|
||||
memory_copy,
|
||||
memory_copy_from_generic,
|
||||
byte_swap,
|
||||
byte_swap_word,
|
||||
byte_swap_eth_address,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,7 +144,12 @@ impl<'ctx> Intrinsics<'ctx> {
|
||||
.as_basic_type_enum(),
|
||||
word_type.as_basic_type_enum(),
|
||||
],
|
||||
name if name == Self::FUNCTION_BYTE_SWAP => vec![word_type.as_basic_type_enum()],
|
||||
name if name == Self::FUNCTION_BYTE_SWAP_WORD => vec![word_type.as_basic_type_enum()],
|
||||
name if name == Self::FUNCTION_BYTE_SWAP_ETH_ADDRESS => {
|
||||
vec![llvm
|
||||
.custom_width_int_type(revive_common::BIT_LENGTH_ETH_ADDRESS as u32)
|
||||
.as_basic_type_enum()]
|
||||
}
|
||||
_ => vec![],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,12 +95,12 @@ impl Entry {
|
||||
);
|
||||
|
||||
context.set_global(
|
||||
crate::polkavm::GLOBAL_I128_SIZE,
|
||||
crate::polkavm::GLOBAL_I160_SIZE,
|
||||
context.xlen_type(),
|
||||
AddressSpace::Stack,
|
||||
context.integer_const(
|
||||
crate::polkavm::XLEN,
|
||||
revive_common::BYTE_LENGTH_X64 as u64 * 2,
|
||||
revive_common::BYTE_LENGTH_X64 as u64 * 2 + revive_common::BYTE_LENGTH_X32 as u64,
|
||||
),
|
||||
);
|
||||
|
||||
|
||||
@@ -622,9 +622,9 @@ where
|
||||
let buffer_pointer = self.build_alloca(self.integer_type(bit_length), name);
|
||||
let symbol = match bit_length {
|
||||
revive_common::BIT_LENGTH_WORD => GLOBAL_I256_SIZE,
|
||||
revive_common::BIT_LENGTH_VALUE => GLOBAL_I128_SIZE,
|
||||
revive_common::BIT_LENGTH_ETH_ADDRESS => GLOBAL_I160_SIZE,
|
||||
revive_common::BIT_LENGTH_BLOCK_NUMBER => GLOBAL_I64_SIZE,
|
||||
_ => unreachable!("invalid stack parameter bit width: {bit_length}"),
|
||||
_ => panic!("invalid stack parameter bit width: {bit_length}"),
|
||||
};
|
||||
let length_pointer = self.get_global(symbol).expect("should be declared");
|
||||
(buffer_pointer, length_pointer.into())
|
||||
@@ -837,13 +837,18 @@ where
|
||||
&self,
|
||||
value: inkwell::values::BasicValueEnum<'ctx>,
|
||||
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>> {
|
||||
let intrinsic = match value.get_type().into_int_type().get_bit_width() as usize {
|
||||
revive_common::BIT_LENGTH_WORD => self.intrinsics().byte_swap_word.value,
|
||||
revive_common::BIT_LENGTH_ETH_ADDRESS => self.intrinsics().byte_swap_eth_address.value,
|
||||
_ => panic!(
|
||||
"invalid byte swap parameter: {:?} {}",
|
||||
value.get_name(),
|
||||
value.get_type()
|
||||
),
|
||||
};
|
||||
Ok(self
|
||||
.builder()
|
||||
.build_call(
|
||||
self.intrinsics().byte_swap.value,
|
||||
&[value.into()],
|
||||
"call_byte_swap",
|
||||
)?
|
||||
.build_call(intrinsic, &[value.into()], "call_byte_swap")?
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.unwrap())
|
||||
@@ -1177,7 +1182,9 @@ where
|
||||
/// Truncate a memory offset to register size, trapping if it doesn't fit.
|
||||
/// Pointers are represented as opaque 256 bit integer values in EVM.
|
||||
/// In practice, they should never exceed a register sized bit value.
|
||||
/// However, we still protect against this possibility here.
|
||||
/// However, we still protect against this possibility here. Heap index
|
||||
/// offsets are generally untrusted and potentially represent valid
|
||||
/// (but wrong) pointers when truncated.
|
||||
pub fn safe_truncate_int_to_xlen(
|
||||
&self,
|
||||
value: inkwell::values::IntValue<'ctx>,
|
||||
@@ -1191,16 +1198,12 @@ where
|
||||
"expected XLEN or WORD sized int type for memory offset",
|
||||
);
|
||||
|
||||
let truncated = self.builder().build_int_truncate_or_bit_cast(
|
||||
value,
|
||||
self.xlen_type(),
|
||||
"offset_truncated",
|
||||
)?;
|
||||
let extended = self.builder().build_int_z_extend_or_bit_cast(
|
||||
truncated,
|
||||
self.word_type(),
|
||||
"offset_extended",
|
||||
)?;
|
||||
let truncated =
|
||||
self.builder()
|
||||
.build_int_truncate(value, self.xlen_type(), "offset_truncated")?;
|
||||
let extended =
|
||||
self.builder()
|
||||
.build_int_z_extend(truncated, self.word_type(), "offset_extended")?;
|
||||
let is_overflow = self.builder().build_int_compare(
|
||||
inkwell::IntPredicate::NE,
|
||||
value,
|
||||
@@ -1208,15 +1211,15 @@ where
|
||||
"compare_truncated_extended",
|
||||
)?;
|
||||
|
||||
let continue_block = self.append_basic_block("offset_pointer_ok");
|
||||
let trap_block = self.append_basic_block("offset_pointer_overflow");
|
||||
self.build_conditional_branch(is_overflow, trap_block, continue_block)?;
|
||||
let block_continue = self.append_basic_block("offset_pointer_ok");
|
||||
let block_trap = self.append_basic_block("offset_pointer_overflow");
|
||||
self.build_conditional_branch(is_overflow, block_trap, block_continue)?;
|
||||
|
||||
self.set_basic_block(trap_block);
|
||||
self.set_basic_block(block_trap);
|
||||
self.build_call(self.intrinsics().trap, &[], "invalid_trap");
|
||||
self.build_unreachable();
|
||||
|
||||
self.set_basic_block(continue_block);
|
||||
self.set_basic_block(block_continue);
|
||||
Ok(truncated)
|
||||
}
|
||||
|
||||
|
||||
@@ -172,7 +172,7 @@ where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
let (output_pointer, output_length_pointer) =
|
||||
context.build_stack_parameter(revive_common::BIT_LENGTH_WORD, "address_output");
|
||||
context.build_stack_parameter(revive_common::BIT_LENGTH_ETH_ADDRESS, "address_output");
|
||||
context.build_runtime_call(
|
||||
runtime_api::ADDRESS,
|
||||
&[
|
||||
@@ -180,7 +180,11 @@ where
|
||||
output_length_pointer.to_int(context).into(),
|
||||
],
|
||||
);
|
||||
context.build_load(output_pointer, "address")
|
||||
let value = context.build_byte_swap(context.build_load(output_pointer, "address")?)?;
|
||||
Ok(context
|
||||
.builder()
|
||||
.build_int_z_extend(value.into_int_value(), context.word_type(), "address_zext")?
|
||||
.into())
|
||||
}
|
||||
|
||||
/// Translates the `caller` instruction.
|
||||
@@ -191,7 +195,7 @@ where
|
||||
D: Dependency + Clone,
|
||||
{
|
||||
let (output_pointer, output_length_pointer) =
|
||||
context.build_stack_parameter(revive_common::BIT_LENGTH_WORD, "caller_output");
|
||||
context.build_stack_parameter(revive_common::BIT_LENGTH_ETH_ADDRESS, "caller_output");
|
||||
context.build_runtime_call(
|
||||
runtime_api::CALLER,
|
||||
&[
|
||||
@@ -199,5 +203,9 @@ where
|
||||
output_length_pointer.to_int(context).into(),
|
||||
],
|
||||
);
|
||||
context.build_load(output_pointer, "caller")
|
||||
let value = context.build_byte_swap(context.build_load(output_pointer, "caller")?)?;
|
||||
Ok(context
|
||||
.builder()
|
||||
.build_int_z_extend(value.into_int_value(), context.word_type(), "caller_zext")?
|
||||
.into())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user