2022-03-02 14:50:16 -08:00
|
|
|
#![allow(clippy::integer_arithmetic)]
|
|
|
|
|
2021-12-03 09:00:31 -08:00
|
|
|
use {
|
|
|
|
byteorder::{ByteOrder, LittleEndian, WriteBytesExt},
|
|
|
|
solana_rbpf::{aligned_memory::AlignedMemory, ebpf::HOST_ALIGN},
|
|
|
|
solana_sdk::{
|
|
|
|
bpf_loader_deprecated,
|
2022-07-20 05:12:43 -07:00
|
|
|
entrypoint::{BPF_ALIGN_OF_U128, MAX_PERMITTED_DATA_INCREASE, NON_DUP_MARKER},
|
2021-12-03 09:00:31 -08:00
|
|
|
instruction::InstructionError,
|
|
|
|
pubkey::Pubkey,
|
|
|
|
system_instruction::MAX_PERMITTED_DATA_LENGTH,
|
2022-01-03 14:30:56 -08:00
|
|
|
transaction_context::{InstructionContext, TransactionContext},
|
2021-12-03 09:00:31 -08:00
|
|
|
},
|
|
|
|
std::{io::prelude::*, mem::size_of},
|
2020-08-11 16:11:52 -07:00
|
|
|
};
|
|
|
|
|
2022-07-20 05:12:43 -07:00
|
|
|
/// Maximum number of instruction accounts that can be serialized into the
|
|
|
|
/// BPF VM.
|
|
|
|
const MAX_INSTRUCTION_ACCOUNTS: u8 = NON_DUP_MARKER;
|
|
|
|
|
2020-08-11 16:11:52 -07:00
|
|
|
pub fn serialize_parameters(
|
2022-01-03 14:30:56 -08:00
|
|
|
transaction_context: &TransactionContext,
|
|
|
|
instruction_context: &InstructionContext,
|
2022-07-20 05:12:43 -07:00
|
|
|
should_cap_ix_accounts: bool,
|
2022-08-15 07:04:48 -07:00
|
|
|
) -> Result<(AlignedMemory<HOST_ALIGN>, Vec<usize>), InstructionError> {
|
2022-07-20 05:12:43 -07:00
|
|
|
let num_ix_accounts = instruction_context.get_number_of_instruction_accounts();
|
|
|
|
if should_cap_ix_accounts && num_ix_accounts > usize::from(MAX_INSTRUCTION_ACCOUNTS) {
|
|
|
|
return Err(InstructionError::MaxAccountsExceeded);
|
|
|
|
}
|
|
|
|
|
2022-01-03 14:30:56 -08:00
|
|
|
let is_loader_deprecated = *instruction_context
|
2022-06-16 09:46:17 -07:00
|
|
|
.try_borrow_last_program_account(transaction_context)?
|
2022-01-03 14:30:56 -08:00
|
|
|
.get_owner()
|
|
|
|
== bpf_loader_deprecated::id();
|
|
|
|
if is_loader_deprecated {
|
|
|
|
serialize_parameters_unaligned(transaction_context, instruction_context)
|
2020-08-11 16:11:52 -07:00
|
|
|
} else {
|
2022-01-03 14:30:56 -08:00
|
|
|
serialize_parameters_aligned(transaction_context, instruction_context)
|
2020-08-11 16:11:52 -07:00
|
|
|
}
|
2021-09-17 23:09:47 -07:00
|
|
|
.and_then(|buffer| {
|
2022-06-16 09:46:17 -07:00
|
|
|
let account_lengths = (0..instruction_context.get_number_of_instruction_accounts())
|
|
|
|
.map(|instruction_account_index| {
|
2022-01-03 14:30:56 -08:00
|
|
|
Ok(instruction_context
|
2022-06-16 09:46:17 -07:00
|
|
|
.try_borrow_instruction_account(transaction_context, instruction_account_index)?
|
2022-01-03 14:30:56 -08:00
|
|
|
.get_data()
|
|
|
|
.len())
|
|
|
|
})
|
2021-09-17 23:09:47 -07:00
|
|
|
.collect::<Result<Vec<usize>, InstructionError>>()?;
|
|
|
|
Ok((buffer, account_lengths))
|
|
|
|
})
|
2020-08-11 16:11:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deserialize_parameters(
|
2022-01-03 14:30:56 -08:00
|
|
|
transaction_context: &TransactionContext,
|
|
|
|
instruction_context: &InstructionContext,
|
2020-08-11 16:11:52 -07:00
|
|
|
buffer: &[u8],
|
2021-09-17 23:09:47 -07:00
|
|
|
account_lengths: &[usize],
|
2020-08-11 16:11:52 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
2022-01-03 14:30:56 -08:00
|
|
|
let is_loader_deprecated = *instruction_context
|
2022-06-16 09:46:17 -07:00
|
|
|
.try_borrow_last_program_account(transaction_context)?
|
2022-01-03 14:30:56 -08:00
|
|
|
.get_owner()
|
|
|
|
== bpf_loader_deprecated::id();
|
|
|
|
if is_loader_deprecated {
|
2022-01-05 00:39:37 -08:00
|
|
|
deserialize_parameters_unaligned(
|
|
|
|
transaction_context,
|
|
|
|
instruction_context,
|
|
|
|
buffer,
|
|
|
|
account_lengths,
|
|
|
|
)
|
2020-08-11 16:11:52 -07:00
|
|
|
} else {
|
2022-01-03 14:30:56 -08:00
|
|
|
deserialize_parameters_aligned(
|
|
|
|
transaction_context,
|
|
|
|
instruction_context,
|
|
|
|
buffer,
|
|
|
|
account_lengths,
|
|
|
|
)
|
2020-08-11 16:11:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn serialize_parameters_unaligned(
|
2022-01-03 14:30:56 -08:00
|
|
|
transaction_context: &TransactionContext,
|
|
|
|
instruction_context: &InstructionContext,
|
2022-08-15 07:04:48 -07:00
|
|
|
) -> Result<AlignedMemory<HOST_ALIGN>, InstructionError> {
|
2020-09-10 11:13:35 -07:00
|
|
|
// Calculate size in order to alloc once
|
|
|
|
let mut size = size_of::<u64>();
|
2022-06-16 09:46:17 -07:00
|
|
|
for instruction_account_index in 0..instruction_context.get_number_of_instruction_accounts() {
|
|
|
|
let duplicate =
|
|
|
|
instruction_context.is_instruction_account_duplicate(instruction_account_index)?;
|
2021-03-11 19:28:21 -08:00
|
|
|
size += 1; // dup
|
2022-01-03 14:30:56 -08:00
|
|
|
if duplicate.is_none() {
|
|
|
|
let data_len = instruction_context
|
2022-06-16 09:46:17 -07:00
|
|
|
.try_borrow_instruction_account(transaction_context, instruction_account_index)?
|
2022-01-03 14:30:56 -08:00
|
|
|
.get_data()
|
|
|
|
.len();
|
|
|
|
size += size_of::<u8>() // is_signer
|
|
|
|
+ size_of::<u8>() // is_writable
|
|
|
|
+ size_of::<Pubkey>() // key
|
|
|
|
+ size_of::<u64>() // lamports
|
|
|
|
+ size_of::<u64>() // data len
|
|
|
|
+ data_len // data
|
|
|
|
+ size_of::<Pubkey>() // owner
|
|
|
|
+ size_of::<u8>() // executable
|
|
|
|
+ size_of::<u64>(); // rent_epoch
|
2020-09-10 11:13:35 -07:00
|
|
|
}
|
|
|
|
}
|
2021-03-11 19:28:21 -08:00
|
|
|
size += size_of::<u64>() // instruction data len
|
2022-01-03 14:30:56 -08:00
|
|
|
+ instruction_context.get_instruction_data().len() // instruction data
|
2021-03-11 19:28:21 -08:00
|
|
|
+ size_of::<Pubkey>(); // program id
|
2022-08-15 07:04:48 -07:00
|
|
|
let mut v = AlignedMemory::<HOST_ALIGN>::with_capacity(size);
|
2020-09-10 11:13:35 -07:00
|
|
|
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_u64::<LittleEndian>(instruction_context.get_number_of_instruction_accounts() as u64)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2022-06-16 09:46:17 -07:00
|
|
|
for instruction_account_index in 0..instruction_context.get_number_of_instruction_accounts() {
|
|
|
|
let duplicate =
|
|
|
|
instruction_context.is_instruction_account_duplicate(instruction_account_index)?;
|
2022-01-03 14:30:56 -08:00
|
|
|
if let Some(position) = duplicate {
|
2021-04-19 13:30:31 -07:00
|
|
|
v.write_u8(position as u8)
|
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2020-08-11 16:11:52 -07:00
|
|
|
} else {
|
2022-01-03 14:30:56 -08:00
|
|
|
let borrowed_account = instruction_context
|
2022-06-16 09:46:17 -07:00
|
|
|
.try_borrow_instruction_account(transaction_context, instruction_account_index)?;
|
2022-07-20 05:12:43 -07:00
|
|
|
v.write_u8(NON_DUP_MARKER)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_u8(borrowed_account.is_signer() as u8)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_u8(borrowed_account.is_writable() as u8)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_all(borrowed_account.get_key().as_ref())
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_u64::<LittleEndian>(borrowed_account.get_lamports())
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_u64::<LittleEndian>(borrowed_account.get_data().len() as u64)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_all(borrowed_account.get_data())
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_all(borrowed_account.get_owner().as_ref())
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_u8(borrowed_account.is_executable() as u8)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_u64::<LittleEndian>(borrowed_account.get_rent_epoch() as u64)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2020-08-11 16:11:52 -07:00
|
|
|
}
|
|
|
|
}
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_u64::<LittleEndian>(instruction_context.get_instruction_data().len() as u64)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_all(instruction_context.get_instruction_data())
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_all(
|
|
|
|
instruction_context
|
2022-06-16 09:46:17 -07:00
|
|
|
.try_borrow_last_program_account(transaction_context)?
|
2022-01-03 14:30:56 -08:00
|
|
|
.get_key()
|
|
|
|
.as_ref(),
|
|
|
|
)
|
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2020-08-11 16:11:52 -07:00
|
|
|
Ok(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deserialize_parameters_unaligned(
|
2022-01-03 14:30:56 -08:00
|
|
|
transaction_context: &TransactionContext,
|
|
|
|
instruction_context: &InstructionContext,
|
2020-08-11 16:11:52 -07:00
|
|
|
buffer: &[u8],
|
2022-01-05 00:39:37 -08:00
|
|
|
account_lengths: &[usize],
|
2020-08-11 16:11:52 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
2020-08-17 13:38:42 -07:00
|
|
|
let mut start = size_of::<u64>(); // number of accounts
|
2022-06-16 09:46:17 -07:00
|
|
|
for (instruction_account_index, pre_len) in
|
|
|
|
(0..instruction_context.get_number_of_instruction_accounts()).zip(account_lengths.iter())
|
2021-09-17 23:09:47 -07:00
|
|
|
{
|
2022-06-16 09:46:17 -07:00
|
|
|
let duplicate =
|
|
|
|
instruction_context.is_instruction_account_duplicate(instruction_account_index)?;
|
2020-08-11 16:11:52 -07:00
|
|
|
start += 1; // is_dup
|
2022-01-03 14:30:56 -08:00
|
|
|
if duplicate.is_none() {
|
|
|
|
let mut borrowed_account = instruction_context
|
2022-06-16 09:46:17 -07:00
|
|
|
.try_borrow_instruction_account(transaction_context, instruction_account_index)?;
|
2021-05-28 09:50:25 -07:00
|
|
|
start += size_of::<u8>(); // is_signer
|
|
|
|
start += size_of::<u8>(); // is_writable
|
|
|
|
start += size_of::<Pubkey>(); // key
|
2022-07-15 00:31:34 -07:00
|
|
|
let lamports = LittleEndian::read_u64(
|
2022-03-10 11:48:33 -08:00
|
|
|
buffer
|
|
|
|
.get(start..)
|
|
|
|
.ok_or(InstructionError::InvalidArgument)?,
|
2022-07-15 00:31:34 -07:00
|
|
|
);
|
|
|
|
if borrowed_account.get_lamports() != lamports {
|
|
|
|
borrowed_account.set_lamports(lamports)?;
|
|
|
|
}
|
2021-05-28 09:50:25 -07:00
|
|
|
start += size_of::<u64>() // lamports
|
2020-08-17 13:38:42 -07:00
|
|
|
+ size_of::<u64>(); // data length
|
2022-07-15 00:31:34 -07:00
|
|
|
let data = buffer
|
|
|
|
.get(start..start + pre_len)
|
|
|
|
.ok_or(InstructionError::InvalidArgument)?;
|
|
|
|
// The redundant check helps to avoid the expensive data comparison if we can
|
|
|
|
match borrowed_account
|
|
|
|
.can_data_be_resized(data.len())
|
|
|
|
.and_then(|_| borrowed_account.can_data_be_changed())
|
|
|
|
{
|
|
|
|
Ok(()) => borrowed_account.set_data(data)?,
|
|
|
|
Err(err) if borrowed_account.get_data() != data => return Err(err),
|
|
|
|
_ => {}
|
|
|
|
}
|
2022-01-05 00:39:37 -08:00
|
|
|
start += pre_len // data
|
2020-08-17 13:38:42 -07:00
|
|
|
+ size_of::<Pubkey>() // owner
|
|
|
|
+ size_of::<u8>() // executable
|
|
|
|
+ size_of::<u64>(); // rent_epoch
|
2020-08-11 16:11:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn serialize_parameters_aligned(
|
2022-01-03 14:30:56 -08:00
|
|
|
transaction_context: &TransactionContext,
|
|
|
|
instruction_context: &InstructionContext,
|
2022-08-15 07:04:48 -07:00
|
|
|
) -> Result<AlignedMemory<HOST_ALIGN>, InstructionError> {
|
2020-09-10 11:13:35 -07:00
|
|
|
// Calculate size in order to alloc once
|
|
|
|
let mut size = size_of::<u64>();
|
2022-06-16 09:46:17 -07:00
|
|
|
for instruction_account_index in 0..instruction_context.get_number_of_instruction_accounts() {
|
|
|
|
let duplicate =
|
|
|
|
instruction_context.is_instruction_account_duplicate(instruction_account_index)?;
|
2021-03-11 19:28:21 -08:00
|
|
|
size += 1; // dup
|
2022-01-03 14:30:56 -08:00
|
|
|
if duplicate.is_some() {
|
2021-03-11 19:28:21 -08:00
|
|
|
size += 7; // padding to 64-bit aligned
|
|
|
|
} else {
|
2022-01-03 14:30:56 -08:00
|
|
|
let data_len = instruction_context
|
2022-06-16 09:46:17 -07:00
|
|
|
.try_borrow_instruction_account(transaction_context, instruction_account_index)?
|
2022-01-03 14:30:56 -08:00
|
|
|
.get_data()
|
|
|
|
.len();
|
|
|
|
size += size_of::<u8>() // is_signer
|
|
|
|
+ size_of::<u8>() // is_writable
|
|
|
|
+ size_of::<u8>() // executable
|
2022-06-27 03:01:01 -07:00
|
|
|
+ size_of::<u32>() // original_data_len
|
2022-01-03 14:30:56 -08:00
|
|
|
+ size_of::<Pubkey>() // key
|
|
|
|
+ size_of::<Pubkey>() // owner
|
|
|
|
+ size_of::<u64>() // lamports
|
|
|
|
+ size_of::<u64>() // data len
|
|
|
|
+ data_len
|
|
|
|
+ MAX_PERMITTED_DATA_INCREASE
|
|
|
|
+ (data_len as *const u8).align_offset(BPF_ALIGN_OF_U128)
|
|
|
|
+ size_of::<u64>(); // rent epoch
|
2020-09-10 11:13:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
size += size_of::<u64>() // data len
|
2022-01-03 14:30:56 -08:00
|
|
|
+ instruction_context.get_instruction_data().len()
|
2020-09-10 11:13:35 -07:00
|
|
|
+ size_of::<Pubkey>(); // program id;
|
2022-08-15 07:04:48 -07:00
|
|
|
let mut v = AlignedMemory::<HOST_ALIGN>::with_capacity(size);
|
2020-09-10 11:13:35 -07:00
|
|
|
|
|
|
|
// Serialize into the buffer
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_u64::<LittleEndian>(instruction_context.get_number_of_instruction_accounts() as u64)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2022-06-16 09:46:17 -07:00
|
|
|
for instruction_account_index in 0..instruction_context.get_number_of_instruction_accounts() {
|
|
|
|
let duplicate =
|
|
|
|
instruction_context.is_instruction_account_duplicate(instruction_account_index)?;
|
2022-01-03 14:30:56 -08:00
|
|
|
if let Some(position) = duplicate {
|
2021-04-19 13:30:31 -07:00
|
|
|
v.write_u8(position as u8)
|
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
|
|
|
v.write_all(&[0u8, 0, 0, 0, 0, 0, 0])
|
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?; // 7 bytes of padding to make 64-bit aligned
|
2020-08-11 16:11:52 -07:00
|
|
|
} else {
|
2022-01-03 14:30:56 -08:00
|
|
|
let borrowed_account = instruction_context
|
2022-06-16 09:46:17 -07:00
|
|
|
.try_borrow_instruction_account(transaction_context, instruction_account_index)?;
|
2022-07-20 05:12:43 -07:00
|
|
|
v.write_u8(NON_DUP_MARKER)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_u8(borrowed_account.is_signer() as u8)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_u8(borrowed_account.is_writable() as u8)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_u8(borrowed_account.is_executable() as u8)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
|
|
|
v.write_all(&[0u8, 0, 0, 0])
|
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?; // 4 bytes of padding to make 128-bit aligned
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_all(borrowed_account.get_key().as_ref())
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_all(borrowed_account.get_owner().as_ref())
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_u64::<LittleEndian>(borrowed_account.get_lamports())
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_u64::<LittleEndian>(borrowed_account.get_data().len() as u64)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_all(borrowed_account.get_data())
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2022-08-15 07:04:48 -07:00
|
|
|
v.fill_write(
|
2021-04-20 11:07:30 -07:00
|
|
|
MAX_PERMITTED_DATA_INCREASE
|
2021-11-30 10:41:15 -08:00
|
|
|
+ (v.write_index() as *const u8).align_offset(BPF_ALIGN_OF_U128),
|
2020-08-17 10:24:34 -07:00
|
|
|
0,
|
2021-04-20 11:07:30 -07:00
|
|
|
)
|
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_u64::<LittleEndian>(borrowed_account.get_rent_epoch() as u64)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2020-08-11 16:11:52 -07:00
|
|
|
}
|
|
|
|
}
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_u64::<LittleEndian>(instruction_context.get_instruction_data().len() as u64)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_all(instruction_context.get_instruction_data())
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2022-01-03 14:30:56 -08:00
|
|
|
v.write_all(
|
|
|
|
instruction_context
|
2022-06-16 09:46:17 -07:00
|
|
|
.try_borrow_last_program_account(transaction_context)?
|
2022-01-03 14:30:56 -08:00
|
|
|
.get_key()
|
|
|
|
.as_ref(),
|
|
|
|
)
|
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2020-08-11 16:11:52 -07:00
|
|
|
Ok(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deserialize_parameters_aligned(
|
2022-01-03 14:30:56 -08:00
|
|
|
transaction_context: &TransactionContext,
|
|
|
|
instruction_context: &InstructionContext,
|
2020-08-11 16:11:52 -07:00
|
|
|
buffer: &[u8],
|
2021-09-17 23:09:47 -07:00
|
|
|
account_lengths: &[usize],
|
2020-08-11 16:11:52 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
2020-08-17 13:38:42 -07:00
|
|
|
let mut start = size_of::<u64>(); // number of accounts
|
2022-06-16 09:46:17 -07:00
|
|
|
for (instruction_account_index, pre_len) in
|
|
|
|
(0..instruction_context.get_number_of_instruction_accounts()).zip(account_lengths.iter())
|
2021-09-17 23:09:47 -07:00
|
|
|
{
|
2022-06-16 09:46:17 -07:00
|
|
|
let duplicate =
|
|
|
|
instruction_context.is_instruction_account_duplicate(instruction_account_index)?;
|
2020-08-17 13:38:42 -07:00
|
|
|
start += size_of::<u8>(); // position
|
2022-01-03 14:30:56 -08:00
|
|
|
if duplicate.is_some() {
|
2020-08-17 13:38:42 -07:00
|
|
|
start += 7; // padding to 64-bit aligned
|
2021-05-28 09:50:25 -07:00
|
|
|
} else {
|
2022-01-03 14:30:56 -08:00
|
|
|
let mut borrowed_account = instruction_context
|
2022-06-16 09:46:17 -07:00
|
|
|
.try_borrow_instruction_account(transaction_context, instruction_account_index)?;
|
2020-08-17 13:38:42 -07:00
|
|
|
start += size_of::<u8>() // is_signer
|
2020-09-10 11:13:35 -07:00
|
|
|
+ size_of::<u8>() // is_writable
|
|
|
|
+ size_of::<u8>() // executable
|
2022-06-27 03:01:01 -07:00
|
|
|
+ size_of::<u32>() // original_data_len
|
2020-09-10 11:13:35 -07:00
|
|
|
+ size_of::<Pubkey>(); // key
|
2022-07-15 00:31:34 -07:00
|
|
|
let owner = buffer
|
|
|
|
.get(start..start + size_of::<Pubkey>())
|
|
|
|
.ok_or(InstructionError::InvalidArgument)?;
|
2020-08-17 13:38:42 -07:00
|
|
|
start += size_of::<Pubkey>(); // owner
|
2022-07-15 00:31:34 -07:00
|
|
|
let lamports = LittleEndian::read_u64(
|
2022-03-10 11:48:33 -08:00
|
|
|
buffer
|
|
|
|
.get(start..)
|
|
|
|
.ok_or(InstructionError::InvalidArgument)?,
|
2022-07-15 00:31:34 -07:00
|
|
|
);
|
|
|
|
if borrowed_account.get_lamports() != lamports {
|
|
|
|
borrowed_account.set_lamports(lamports)?;
|
|
|
|
}
|
2020-08-17 13:38:42 -07:00
|
|
|
start += size_of::<u64>(); // lamports
|
2022-03-10 11:48:33 -08:00
|
|
|
let post_len = LittleEndian::read_u64(
|
|
|
|
buffer
|
|
|
|
.get(start..)
|
|
|
|
.ok_or(InstructionError::InvalidArgument)?,
|
|
|
|
) as usize;
|
2020-08-17 13:38:42 -07:00
|
|
|
start += size_of::<u64>(); // data length
|
2022-06-10 15:33:19 -07:00
|
|
|
if post_len.saturating_sub(*pre_len) > MAX_PERMITTED_DATA_INCREASE
|
|
|
|
|| post_len > MAX_PERMITTED_DATA_LENGTH as usize
|
|
|
|
{
|
|
|
|
return Err(InstructionError::InvalidRealloc);
|
|
|
|
}
|
|
|
|
let data_end = start + post_len;
|
2022-07-15 00:31:34 -07:00
|
|
|
let data = buffer
|
|
|
|
.get(start..data_end)
|
|
|
|
.ok_or(InstructionError::InvalidArgument)?;
|
|
|
|
// The redundant check helps to avoid the expensive data comparison if we can
|
|
|
|
match borrowed_account
|
|
|
|
.can_data_be_resized(data.len())
|
|
|
|
.and_then(|_| borrowed_account.can_data_be_changed())
|
|
|
|
{
|
|
|
|
Ok(()) => borrowed_account.set_data(data)?,
|
|
|
|
Err(err) if borrowed_account.get_data() != data => return Err(err),
|
|
|
|
_ => {}
|
|
|
|
}
|
2021-09-17 23:09:47 -07:00
|
|
|
start += *pre_len + MAX_PERMITTED_DATA_INCREASE; // data
|
2021-11-30 10:41:15 -08:00
|
|
|
start += (start as *const u8).align_offset(BPF_ALIGN_OF_U128);
|
2020-08-17 13:38:42 -07:00
|
|
|
start += size_of::<u64>(); // rent_epoch
|
2022-07-15 00:31:34 -07:00
|
|
|
if borrowed_account.get_owner().to_bytes() != owner {
|
|
|
|
// Change the owner at the end so that we are allowed to change the lamports and data before
|
|
|
|
borrowed_account.set_owner(owner)?;
|
|
|
|
}
|
2020-08-11 16:11:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-12-03 09:00:31 -08:00
|
|
|
use {
|
|
|
|
super::*,
|
|
|
|
solana_program_runtime::invoke_context::{prepare_mock_invoke_context, InvokeContext},
|
|
|
|
solana_sdk::{
|
2022-01-05 00:39:37 -08:00
|
|
|
account::{Account, AccountSharedData, ReadableAccount, WritableAccount},
|
2021-12-03 09:00:31 -08:00
|
|
|
account_info::AccountInfo,
|
|
|
|
bpf_loader,
|
|
|
|
entrypoint::deserialize,
|
2021-12-17 05:01:12 -08:00
|
|
|
instruction::AccountMeta,
|
2022-07-15 00:31:34 -07:00
|
|
|
sysvar::rent::Rent,
|
2021-12-03 09:00:31 -08:00
|
|
|
},
|
|
|
|
std::{
|
|
|
|
cell::RefCell,
|
|
|
|
rc::Rc,
|
|
|
|
slice::{from_raw_parts, from_raw_parts_mut},
|
|
|
|
},
|
2020-08-11 16:11:52 -07:00
|
|
|
};
|
|
|
|
|
2022-07-20 05:12:43 -07:00
|
|
|
#[test]
|
|
|
|
fn test_serialize_parameters_with_many_accounts() {
|
|
|
|
struct TestCase {
|
|
|
|
num_ix_accounts: usize,
|
|
|
|
append_dup_account: bool,
|
|
|
|
should_cap_ix_accounts: bool,
|
|
|
|
expected_err: Option<InstructionError>,
|
|
|
|
name: &'static str,
|
|
|
|
}
|
|
|
|
|
|
|
|
for TestCase {
|
|
|
|
num_ix_accounts,
|
|
|
|
append_dup_account,
|
|
|
|
should_cap_ix_accounts,
|
|
|
|
expected_err,
|
|
|
|
name,
|
|
|
|
} in [
|
|
|
|
TestCase {
|
|
|
|
name: "serialize max accounts without cap",
|
|
|
|
num_ix_accounts: usize::from(MAX_INSTRUCTION_ACCOUNTS),
|
|
|
|
should_cap_ix_accounts: false,
|
|
|
|
append_dup_account: false,
|
|
|
|
expected_err: None,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
name: "serialize max accounts and append dup without cap",
|
|
|
|
num_ix_accounts: usize::from(MAX_INSTRUCTION_ACCOUNTS),
|
|
|
|
should_cap_ix_accounts: false,
|
|
|
|
append_dup_account: true,
|
|
|
|
expected_err: None,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
name: "serialize max accounts with cap",
|
|
|
|
num_ix_accounts: usize::from(MAX_INSTRUCTION_ACCOUNTS),
|
|
|
|
should_cap_ix_accounts: true,
|
|
|
|
append_dup_account: false,
|
|
|
|
expected_err: None,
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
name: "serialize too many accounts with cap",
|
|
|
|
num_ix_accounts: usize::from(MAX_INSTRUCTION_ACCOUNTS) + 1,
|
|
|
|
should_cap_ix_accounts: true,
|
|
|
|
append_dup_account: false,
|
|
|
|
expected_err: Some(InstructionError::MaxAccountsExceeded),
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
name: "serialize too many accounts and append dup with cap",
|
|
|
|
num_ix_accounts: usize::from(MAX_INSTRUCTION_ACCOUNTS),
|
|
|
|
should_cap_ix_accounts: true,
|
|
|
|
append_dup_account: true,
|
|
|
|
expected_err: Some(InstructionError::MaxAccountsExceeded),
|
|
|
|
},
|
|
|
|
// This test case breaks parameter deserialization and can be cleaned up
|
|
|
|
// when should_cap_ix_accounts is enabled.
|
|
|
|
//
|
|
|
|
// TestCase {
|
|
|
|
// name: "serialize too many accounts and append dup without cap",
|
|
|
|
// num_ix_accounts: usize::from(MAX_INSTRUCTION_ACCOUNTS) + 1,
|
|
|
|
// should_cap_ix_accounts: false,
|
|
|
|
// append_dup_account: true,
|
|
|
|
// expected_err: None,
|
|
|
|
// },
|
|
|
|
] {
|
|
|
|
let program_id = solana_sdk::pubkey::new_rand();
|
|
|
|
let mut transaction_accounts = vec![(
|
|
|
|
program_id,
|
|
|
|
AccountSharedData::from(Account {
|
|
|
|
lamports: 0,
|
|
|
|
data: vec![],
|
|
|
|
owner: bpf_loader::id(),
|
|
|
|
executable: true,
|
|
|
|
rent_epoch: 0,
|
|
|
|
}),
|
|
|
|
)];
|
|
|
|
|
|
|
|
let instruction_account_keys: Vec<Pubkey> =
|
|
|
|
(0..num_ix_accounts).map(|_| Pubkey::new_unique()).collect();
|
|
|
|
|
|
|
|
for key in &instruction_account_keys {
|
|
|
|
transaction_accounts.push((
|
|
|
|
*key,
|
|
|
|
AccountSharedData::from(Account {
|
|
|
|
lamports: 0,
|
|
|
|
data: vec![],
|
|
|
|
owner: program_id,
|
|
|
|
executable: false,
|
|
|
|
rent_epoch: 0,
|
|
|
|
}),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut instruction_account_metas: Vec<_> = instruction_account_keys
|
|
|
|
.iter()
|
|
|
|
.map(|key| AccountMeta::new_readonly(*key, false))
|
|
|
|
.collect();
|
|
|
|
if append_dup_account {
|
|
|
|
instruction_account_metas.push(instruction_account_metas.last().cloned().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
let program_indices = [0];
|
|
|
|
let instruction_accounts = prepare_mock_invoke_context(
|
|
|
|
transaction_accounts.clone(),
|
|
|
|
instruction_account_metas,
|
|
|
|
&program_indices,
|
|
|
|
)
|
|
|
|
.instruction_accounts;
|
2022-08-20 02:20:47 -07:00
|
|
|
let instruction_data = vec![];
|
2022-07-20 05:12:43 -07:00
|
|
|
|
2022-08-20 02:20:47 -07:00
|
|
|
let mut transaction_context =
|
2022-07-20 05:12:43 -07:00
|
|
|
TransactionContext::new(transaction_accounts, Some(Rent::default()), 1, 1);
|
2022-08-20 02:20:47 -07:00
|
|
|
transaction_context
|
|
|
|
.push(&program_indices, &instruction_accounts, &instruction_data)
|
|
|
|
.unwrap();
|
|
|
|
let instruction_context = transaction_context
|
|
|
|
.get_instruction_context_at_index_in_trace(0)
|
|
|
|
.unwrap();
|
2022-07-20 05:12:43 -07:00
|
|
|
|
|
|
|
let serialization_result = serialize_parameters(
|
|
|
|
&transaction_context,
|
2022-08-20 02:20:47 -07:00
|
|
|
instruction_context,
|
2022-07-20 05:12:43 -07:00
|
|
|
should_cap_ix_accounts,
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
serialization_result.as_ref().err(),
|
|
|
|
expected_err.as_ref(),
|
|
|
|
"{} test case failed",
|
|
|
|
name
|
|
|
|
);
|
|
|
|
if expected_err.is_some() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let (mut serialized, _account_lengths) = serialization_result.unwrap();
|
|
|
|
let (de_program_id, de_accounts, de_instruction_data) =
|
|
|
|
unsafe { deserialize(serialized.as_slice_mut().first_mut().unwrap() as *mut u8) };
|
|
|
|
assert_eq!(de_program_id, &program_id);
|
|
|
|
assert_eq!(de_instruction_data, &instruction_data);
|
|
|
|
for (index, account_info) in de_accounts.into_iter().enumerate() {
|
|
|
|
let ix_account = &instruction_accounts.get(index).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
account_info.key,
|
|
|
|
transaction_context
|
|
|
|
.get_key_of_account_at_index(ix_account.index_in_transaction)
|
|
|
|
.unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(account_info.owner, &program_id);
|
|
|
|
assert!(!account_info.executable);
|
|
|
|
assert!(account_info.data_is_empty());
|
|
|
|
assert!(!account_info.is_writable);
|
|
|
|
assert!(!account_info.is_signer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 16:11:52 -07:00
|
|
|
#[test]
|
|
|
|
fn test_serialize_parameters() {
|
2020-10-19 12:12:08 -07:00
|
|
|
let program_id = solana_sdk::pubkey::new_rand();
|
2021-12-17 05:01:12 -08:00
|
|
|
let transaction_accounts = vec![
|
2021-11-08 09:51:36 -08:00
|
|
|
(
|
|
|
|
program_id,
|
2021-12-17 05:01:12 -08:00
|
|
|
AccountSharedData::from(Account {
|
2021-11-08 09:51:36 -08:00
|
|
|
lamports: 0,
|
|
|
|
data: vec![],
|
|
|
|
owner: bpf_loader::id(),
|
|
|
|
executable: true,
|
|
|
|
rent_epoch: 0,
|
2021-12-17 05:01:12 -08:00
|
|
|
}),
|
2021-11-08 09:51:36 -08:00
|
|
|
),
|
|
|
|
(
|
2021-12-17 05:01:12 -08:00
|
|
|
solana_sdk::pubkey::new_rand(),
|
|
|
|
AccountSharedData::from(Account {
|
2021-11-08 09:51:36 -08:00
|
|
|
lamports: 1,
|
|
|
|
data: vec![1u8, 2, 3, 4, 5],
|
|
|
|
owner: bpf_loader::id(),
|
|
|
|
executable: false,
|
|
|
|
rent_epoch: 100,
|
2021-12-17 05:01:12 -08:00
|
|
|
}),
|
2021-11-08 09:51:36 -08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
solana_sdk::pubkey::new_rand(),
|
2021-12-17 05:01:12 -08:00
|
|
|
AccountSharedData::from(Account {
|
2021-11-08 09:51:36 -08:00
|
|
|
lamports: 2,
|
|
|
|
data: vec![11u8, 12, 13, 14, 15, 16, 17, 18, 19],
|
|
|
|
owner: bpf_loader::id(),
|
|
|
|
executable: true,
|
|
|
|
rent_epoch: 200,
|
2021-12-17 05:01:12 -08:00
|
|
|
}),
|
2021-11-08 09:51:36 -08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
solana_sdk::pubkey::new_rand(),
|
2021-12-17 05:01:12 -08:00
|
|
|
AccountSharedData::from(Account {
|
2021-11-08 09:51:36 -08:00
|
|
|
lamports: 3,
|
|
|
|
data: vec![],
|
|
|
|
owner: bpf_loader::id(),
|
|
|
|
executable: false,
|
|
|
|
rent_epoch: 3100,
|
2021-12-17 05:01:12 -08:00
|
|
|
}),
|
2021-11-08 09:51:36 -08:00
|
|
|
),
|
|
|
|
(
|
2021-12-17 05:01:12 -08:00
|
|
|
solana_sdk::pubkey::new_rand(),
|
|
|
|
AccountSharedData::from(Account {
|
2021-11-08 09:51:36 -08:00
|
|
|
lamports: 4,
|
|
|
|
data: vec![1u8, 2, 3, 4, 5],
|
|
|
|
owner: bpf_loader::id(),
|
|
|
|
executable: false,
|
|
|
|
rent_epoch: 100,
|
2021-12-17 05:01:12 -08:00
|
|
|
}),
|
2021-11-08 09:51:36 -08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
solana_sdk::pubkey::new_rand(),
|
2021-12-17 05:01:12 -08:00
|
|
|
AccountSharedData::from(Account {
|
2021-11-08 09:51:36 -08:00
|
|
|
lamports: 5,
|
|
|
|
data: vec![11u8, 12, 13, 14, 15, 16, 17, 18, 19],
|
|
|
|
owner: bpf_loader::id(),
|
|
|
|
executable: true,
|
|
|
|
rent_epoch: 200,
|
2021-12-17 05:01:12 -08:00
|
|
|
}),
|
2021-11-08 09:51:36 -08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
solana_sdk::pubkey::new_rand(),
|
2021-12-17 05:01:12 -08:00
|
|
|
AccountSharedData::from(Account {
|
2021-11-08 09:51:36 -08:00
|
|
|
lamports: 6,
|
|
|
|
data: vec![],
|
|
|
|
owner: bpf_loader::id(),
|
|
|
|
executable: false,
|
|
|
|
rent_epoch: 3100,
|
2021-12-17 05:01:12 -08:00
|
|
|
}),
|
2021-11-08 09:51:36 -08:00
|
|
|
),
|
2020-10-19 12:23:14 -07:00
|
|
|
];
|
2022-01-03 14:30:56 -08:00
|
|
|
let instruction_accounts = [1, 1, 2, 3, 4, 4, 5, 6]
|
|
|
|
.into_iter()
|
|
|
|
.enumerate()
|
2022-06-16 09:46:17 -07:00
|
|
|
.map(
|
|
|
|
|(instruction_account_index, index_in_transaction)| AccountMeta {
|
|
|
|
pubkey: transaction_accounts.get(index_in_transaction).unwrap().0,
|
|
|
|
is_signer: false,
|
|
|
|
is_writable: instruction_account_index >= 4,
|
|
|
|
},
|
|
|
|
)
|
2022-01-03 14:30:56 -08:00
|
|
|
.collect();
|
2020-08-11 16:11:52 -07:00
|
|
|
let instruction_data = vec![1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
|
2021-11-08 09:51:36 -08:00
|
|
|
let program_indices = [0];
|
2022-01-05 00:39:37 -08:00
|
|
|
let mut original_accounts = transaction_accounts.clone();
|
2021-12-30 06:46:36 -08:00
|
|
|
let preparation = prepare_mock_invoke_context(
|
2022-01-03 14:30:56 -08:00
|
|
|
transaction_accounts,
|
2021-12-30 06:46:36 -08:00
|
|
|
instruction_accounts,
|
|
|
|
&program_indices,
|
|
|
|
);
|
2022-07-15 00:31:34 -07:00
|
|
|
let mut transaction_context = TransactionContext::new(
|
|
|
|
preparation.transaction_accounts,
|
|
|
|
Some(Rent::default()),
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
);
|
2022-01-03 14:30:56 -08:00
|
|
|
let mut invoke_context = InvokeContext::new_mock(&mut transaction_context, &[]);
|
2021-11-08 09:51:36 -08:00
|
|
|
invoke_context
|
2022-01-03 14:30:56 -08:00
|
|
|
.push(
|
|
|
|
&preparation.instruction_accounts,
|
|
|
|
&program_indices,
|
|
|
|
&instruction_data,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let instruction_context = invoke_context
|
|
|
|
.transaction_context
|
|
|
|
.get_current_instruction_context()
|
2021-11-08 09:51:36 -08:00
|
|
|
.unwrap();
|
2020-08-11 16:11:52 -07:00
|
|
|
|
|
|
|
// check serialize_parameters_aligned
|
|
|
|
|
2022-07-20 05:12:43 -07:00
|
|
|
let (mut serialized, account_lengths) = serialize_parameters(
|
|
|
|
invoke_context.transaction_context,
|
|
|
|
instruction_context,
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-03-11 19:28:21 -08:00
|
|
|
|
2020-08-11 16:11:52 -07:00
|
|
|
let (de_program_id, de_accounts, de_instruction_data) =
|
2022-03-10 11:48:33 -08:00
|
|
|
unsafe { deserialize(serialized.as_slice_mut().first_mut().unwrap() as *mut u8) };
|
2021-04-20 11:07:30 -07:00
|
|
|
|
2020-08-11 16:11:52 -07:00
|
|
|
assert_eq!(&program_id, de_program_id);
|
|
|
|
assert_eq!(instruction_data, de_instruction_data);
|
|
|
|
assert_eq!(
|
2022-03-10 11:48:33 -08:00
|
|
|
(de_instruction_data.first().unwrap() as *const u8).align_offset(BPF_ALIGN_OF_U128),
|
2020-08-11 16:11:52 -07:00
|
|
|
0
|
|
|
|
);
|
2021-12-17 05:01:12 -08:00
|
|
|
for account_info in de_accounts {
|
2022-01-03 14:30:56 -08:00
|
|
|
let index_in_transaction = invoke_context
|
|
|
|
.transaction_context
|
|
|
|
.find_index_of_account(account_info.key)
|
|
|
|
.unwrap();
|
|
|
|
let account = invoke_context
|
|
|
|
.transaction_context
|
|
|
|
.get_account_at_index(index_in_transaction)
|
2022-02-03 02:34:51 -08:00
|
|
|
.unwrap()
|
2022-01-03 14:30:56 -08:00
|
|
|
.borrow();
|
2021-04-30 11:16:58 -07:00
|
|
|
assert_eq!(account.lamports(), account_info.lamports());
|
2021-04-20 14:41:16 -07:00
|
|
|
assert_eq!(account.data(), &account_info.data.borrow()[..]);
|
2021-04-26 08:40:11 -07:00
|
|
|
assert_eq!(account.owner(), account_info.owner);
|
2021-04-28 06:53:54 -07:00
|
|
|
assert_eq!(account.executable(), account_info.executable);
|
2021-04-27 11:51:13 -07:00
|
|
|
assert_eq!(account.rent_epoch(), account_info.rent_epoch);
|
2020-08-11 16:11:52 -07:00
|
|
|
|
|
|
|
assert_eq!(
|
2021-11-30 10:41:15 -08:00
|
|
|
(*account_info.lamports.borrow() as *const u64).align_offset(BPF_ALIGN_OF_U128),
|
2020-08-11 16:11:52 -07:00
|
|
|
0
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
account_info
|
|
|
|
.data
|
|
|
|
.borrow()
|
|
|
|
.as_ptr()
|
2021-11-30 10:41:15 -08:00
|
|
|
.align_offset(BPF_ALIGN_OF_U128),
|
2020-08-11 16:11:52 -07:00
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-17 23:09:47 -07:00
|
|
|
deserialize_parameters(
|
2022-01-03 14:30:56 -08:00
|
|
|
invoke_context.transaction_context,
|
|
|
|
instruction_context,
|
2021-09-17 23:09:47 -07:00
|
|
|
serialized.as_slice(),
|
|
|
|
&account_lengths,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2022-01-05 00:39:37 -08:00
|
|
|
for (index_in_transaction, (_key, original_account)) in original_accounts.iter().enumerate()
|
|
|
|
{
|
2022-01-03 14:30:56 -08:00
|
|
|
let account = invoke_context
|
|
|
|
.transaction_context
|
|
|
|
.get_account_at_index(index_in_transaction)
|
2022-02-03 02:34:51 -08:00
|
|
|
.unwrap()
|
2022-01-03 14:30:56 -08:00
|
|
|
.borrow();
|
2022-01-05 00:39:37 -08:00
|
|
|
assert_eq!(&*account, original_account);
|
2021-03-11 19:28:21 -08:00
|
|
|
}
|
|
|
|
|
2020-08-11 16:11:52 -07:00
|
|
|
// check serialize_parameters_unaligned
|
2022-03-10 11:48:33 -08:00
|
|
|
original_accounts
|
|
|
|
.first_mut()
|
|
|
|
.unwrap()
|
2022-01-05 00:39:37 -08:00
|
|
|
.1
|
|
|
|
.set_owner(bpf_loader_deprecated::id());
|
2022-07-15 00:31:34 -07:00
|
|
|
invoke_context
|
2022-01-03 14:30:56 -08:00
|
|
|
.transaction_context
|
2022-07-15 00:31:34 -07:00
|
|
|
.get_account_at_index(0)
|
2022-01-03 14:30:56 -08:00
|
|
|
.unwrap()
|
2022-07-15 00:31:34 -07:00
|
|
|
.borrow_mut()
|
|
|
|
.set_owner(bpf_loader_deprecated::id());
|
2020-08-11 16:11:52 -07:00
|
|
|
|
2022-07-20 05:12:43 -07:00
|
|
|
let (mut serialized, account_lengths) = serialize_parameters(
|
|
|
|
invoke_context.transaction_context,
|
|
|
|
instruction_context,
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-03-11 19:28:21 -08:00
|
|
|
|
2022-03-10 11:48:33 -08:00
|
|
|
let (de_program_id, de_accounts, de_instruction_data) = unsafe {
|
|
|
|
deserialize_unaligned(serialized.as_slice_mut().first_mut().unwrap() as *mut u8)
|
|
|
|
};
|
2020-08-11 16:11:52 -07:00
|
|
|
assert_eq!(&program_id, de_program_id);
|
|
|
|
assert_eq!(instruction_data, de_instruction_data);
|
2021-12-17 05:01:12 -08:00
|
|
|
for account_info in de_accounts {
|
2022-01-03 14:30:56 -08:00
|
|
|
let index_in_transaction = invoke_context
|
|
|
|
.transaction_context
|
|
|
|
.find_index_of_account(account_info.key)
|
|
|
|
.unwrap();
|
|
|
|
let account = invoke_context
|
|
|
|
.transaction_context
|
|
|
|
.get_account_at_index(index_in_transaction)
|
2022-02-03 02:34:51 -08:00
|
|
|
.unwrap()
|
2022-01-03 14:30:56 -08:00
|
|
|
.borrow();
|
2021-04-30 11:16:58 -07:00
|
|
|
assert_eq!(account.lamports(), account_info.lamports());
|
2021-04-20 14:41:16 -07:00
|
|
|
assert_eq!(account.data(), &account_info.data.borrow()[..]);
|
2021-04-26 08:40:11 -07:00
|
|
|
assert_eq!(account.owner(), account_info.owner);
|
2021-04-28 06:53:54 -07:00
|
|
|
assert_eq!(account.executable(), account_info.executable);
|
2021-04-27 11:51:13 -07:00
|
|
|
assert_eq!(account.rent_epoch(), account_info.rent_epoch);
|
2020-08-11 16:11:52 -07:00
|
|
|
}
|
2021-03-11 19:28:21 -08:00
|
|
|
|
|
|
|
deserialize_parameters(
|
2022-01-03 14:30:56 -08:00
|
|
|
invoke_context.transaction_context,
|
|
|
|
instruction_context,
|
2021-04-20 11:07:30 -07:00
|
|
|
serialized.as_slice(),
|
2021-09-17 23:09:47 -07:00
|
|
|
&account_lengths,
|
2021-03-11 19:28:21 -08:00
|
|
|
)
|
|
|
|
.unwrap();
|
2022-01-05 00:39:37 -08:00
|
|
|
for (index_in_transaction, (_key, original_account)) in original_accounts.iter().enumerate()
|
|
|
|
{
|
2022-01-03 14:30:56 -08:00
|
|
|
let account = invoke_context
|
|
|
|
.transaction_context
|
|
|
|
.get_account_at_index(index_in_transaction)
|
2022-02-03 02:34:51 -08:00
|
|
|
.unwrap()
|
2022-01-03 14:30:56 -08:00
|
|
|
.borrow();
|
2022-01-05 00:39:37 -08:00
|
|
|
assert_eq!(&*account, original_account);
|
2021-03-11 19:28:21 -08:00
|
|
|
}
|
2020-08-11 16:11:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// the old bpf_loader in-program deserializer bpf_loader::id()
|
|
|
|
#[allow(clippy::type_complexity)]
|
|
|
|
pub unsafe fn deserialize_unaligned<'a>(
|
|
|
|
input: *mut u8,
|
|
|
|
) -> (&'a Pubkey, Vec<AccountInfo<'a>>, &'a [u8]) {
|
|
|
|
let mut offset: usize = 0;
|
|
|
|
|
|
|
|
// number of accounts present
|
|
|
|
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
let num_accounts = *(input.add(offset) as *const u64) as usize;
|
|
|
|
offset += size_of::<u64>();
|
|
|
|
|
|
|
|
// account Infos
|
|
|
|
|
|
|
|
let mut accounts = Vec::with_capacity(num_accounts);
|
|
|
|
for _ in 0..num_accounts {
|
|
|
|
let dup_info = *(input.add(offset) as *const u8);
|
|
|
|
offset += size_of::<u8>();
|
2022-07-20 05:12:43 -07:00
|
|
|
if dup_info == NON_DUP_MARKER {
|
2020-08-11 16:11:52 -07:00
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
let is_signer = *(input.add(offset) as *const u8) != 0;
|
|
|
|
offset += size_of::<u8>();
|
|
|
|
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
let is_writable = *(input.add(offset) as *const u8) != 0;
|
|
|
|
offset += size_of::<u8>();
|
|
|
|
|
|
|
|
let key: &Pubkey = &*(input.add(offset) as *const Pubkey);
|
|
|
|
offset += size_of::<Pubkey>();
|
|
|
|
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
let lamports = Rc::new(RefCell::new(&mut *(input.add(offset) as *mut u64)));
|
|
|
|
offset += size_of::<u64>();
|
|
|
|
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
let data_len = *(input.add(offset) as *const u64) as usize;
|
|
|
|
offset += size_of::<u64>();
|
|
|
|
|
|
|
|
let data = Rc::new(RefCell::new({
|
|
|
|
from_raw_parts_mut(input.add(offset), data_len)
|
|
|
|
}));
|
|
|
|
offset += data_len;
|
|
|
|
|
|
|
|
let owner: &Pubkey = &*(input.add(offset) as *const Pubkey);
|
|
|
|
offset += size_of::<Pubkey>();
|
|
|
|
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
let executable = *(input.add(offset) as *const u8) != 0;
|
|
|
|
offset += size_of::<u8>();
|
|
|
|
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
let rent_epoch = *(input.add(offset) as *const u64);
|
|
|
|
offset += size_of::<u64>();
|
|
|
|
|
|
|
|
accounts.push(AccountInfo {
|
2021-04-18 10:27:36 -07:00
|
|
|
key,
|
2020-08-11 16:11:52 -07:00
|
|
|
is_signer,
|
|
|
|
is_writable,
|
|
|
|
lamports,
|
|
|
|
data,
|
|
|
|
owner,
|
|
|
|
executable,
|
|
|
|
rent_epoch,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// duplicate account, clone the original
|
2022-03-10 11:48:33 -08:00
|
|
|
accounts.push(accounts.get(dup_info as usize).unwrap().clone());
|
2020-08-11 16:11:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// instruction data
|
|
|
|
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
let instruction_data_len = *(input.add(offset) as *const u64) as usize;
|
|
|
|
offset += size_of::<u64>();
|
|
|
|
|
|
|
|
let instruction_data = { from_raw_parts(input.add(offset), instruction_data_len) };
|
|
|
|
offset += instruction_data_len;
|
|
|
|
|
|
|
|
// program Id
|
|
|
|
|
|
|
|
let program_id: &Pubkey = &*(input.add(offset) as *const Pubkey);
|
|
|
|
|
|
|
|
(program_id, accounts, instruction_data)
|
|
|
|
}
|
|
|
|
}
|