2020-08-11 16:11:52 -07:00
|
|
|
use byteorder::{ByteOrder, LittleEndian, WriteBytesExt};
|
2021-04-20 11:07:30 -07:00
|
|
|
use solana_rbpf::{aligned_memory::AlignedMemory, ebpf::HOST_ALIGN};
|
2020-08-11 16:11:52 -07:00
|
|
|
use solana_sdk::{
|
2021-04-23 07:34:08 -07:00
|
|
|
account::{ReadableAccount, WritableAccount},
|
|
|
|
bpf_loader_deprecated,
|
2021-11-16 07:02:22 -08:00
|
|
|
entrypoint::{MAX_PERMITTED_DATA_INCREASE, PARAMETER_ALIGNMENT},
|
2021-04-23 07:34:08 -07:00
|
|
|
instruction::InstructionError,
|
|
|
|
keyed_account::KeyedAccount,
|
|
|
|
pubkey::Pubkey,
|
2021-09-28 01:13:03 -07:00
|
|
|
system_instruction::MAX_PERMITTED_DATA_LENGTH,
|
2020-08-11 16:11:52 -07:00
|
|
|
};
|
|
|
|
use std::{
|
|
|
|
io::prelude::*,
|
2020-08-17 13:38:42 -07:00
|
|
|
mem::{align_of, size_of},
|
2020-08-11 16:11:52 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Look for a duplicate account and return its position if found
|
|
|
|
pub fn is_dup(accounts: &[KeyedAccount], keyed_account: &KeyedAccount) -> (bool, usize) {
|
|
|
|
for (i, account) in accounts.iter().enumerate() {
|
|
|
|
if account == keyed_account {
|
|
|
|
return (true, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(false, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn serialize_parameters(
|
|
|
|
loader_id: &Pubkey,
|
|
|
|
program_id: &Pubkey,
|
|
|
|
keyed_accounts: &[KeyedAccount],
|
|
|
|
data: &[u8],
|
2021-09-17 23:09:47 -07:00
|
|
|
) -> Result<(AlignedMemory, Vec<usize>), InstructionError> {
|
2020-08-11 16:11:52 -07:00
|
|
|
if *loader_id == bpf_loader_deprecated::id() {
|
|
|
|
serialize_parameters_unaligned(program_id, keyed_accounts, data)
|
|
|
|
} else {
|
|
|
|
serialize_parameters_aligned(program_id, keyed_accounts, data)
|
|
|
|
}
|
2021-09-17 23:09:47 -07:00
|
|
|
.and_then(|buffer| {
|
|
|
|
let account_lengths = keyed_accounts
|
|
|
|
.iter()
|
|
|
|
.map(|keyed_account| keyed_account.data_len())
|
|
|
|
.collect::<Result<Vec<usize>, InstructionError>>()?;
|
|
|
|
Ok((buffer, account_lengths))
|
|
|
|
})
|
2020-08-11 16:11:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deserialize_parameters(
|
|
|
|
loader_id: &Pubkey,
|
|
|
|
keyed_accounts: &[KeyedAccount],
|
|
|
|
buffer: &[u8],
|
2021-09-17 23:09:47 -07:00
|
|
|
account_lengths: &[usize],
|
2021-09-28 01:13:03 -07:00
|
|
|
do_support_realloc: bool,
|
2020-08-11 16:11:52 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
|
|
|
if *loader_id == bpf_loader_deprecated::id() {
|
2021-09-17 23:09:47 -07:00
|
|
|
deserialize_parameters_unaligned(keyed_accounts, buffer, account_lengths)
|
2020-08-11 16:11:52 -07:00
|
|
|
} else {
|
2021-09-28 01:13:03 -07:00
|
|
|
deserialize_parameters_aligned(keyed_accounts, buffer, account_lengths, do_support_realloc)
|
2020-08-11 16:11:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 19:28:21 -08:00
|
|
|
pub fn get_serialized_account_size_unaligned(
|
|
|
|
keyed_account: &KeyedAccount,
|
|
|
|
) -> Result<usize, InstructionError> {
|
|
|
|
let data_len = keyed_account.data_len()?;
|
|
|
|
Ok(
|
|
|
|
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-08-11 16:11:52 -07:00
|
|
|
pub fn serialize_parameters_unaligned(
|
|
|
|
program_id: &Pubkey,
|
|
|
|
keyed_accounts: &[KeyedAccount],
|
|
|
|
instruction_data: &[u8],
|
2021-04-20 11:07:30 -07:00
|
|
|
) -> Result<AlignedMemory, InstructionError> {
|
2020-09-10 11:13:35 -07:00
|
|
|
// Calculate size in order to alloc once
|
|
|
|
let mut size = size_of::<u64>();
|
|
|
|
for (i, keyed_account) in keyed_accounts.iter().enumerate() {
|
|
|
|
let (is_dup, _) = is_dup(&keyed_accounts[..i], keyed_account);
|
2021-03-11 19:28:21 -08:00
|
|
|
size += 1; // dup
|
2020-09-10 11:13:35 -07:00
|
|
|
if !is_dup {
|
2021-03-11 19:28:21 -08:00
|
|
|
size += get_serialized_account_size_unaligned(keyed_account)?;
|
2020-09-10 11:13:35 -07:00
|
|
|
}
|
|
|
|
}
|
2021-03-11 19:28:21 -08:00
|
|
|
size += size_of::<u64>() // instruction data len
|
|
|
|
+ instruction_data.len() // instruction data
|
|
|
|
+ size_of::<Pubkey>(); // program id
|
2021-04-20 11:07:30 -07:00
|
|
|
let mut v = AlignedMemory::new(size, HOST_ALIGN);
|
2020-09-10 11:13:35 -07:00
|
|
|
|
2020-08-11 16:11:52 -07:00
|
|
|
v.write_u64::<LittleEndian>(keyed_accounts.len() as u64)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2020-08-11 16:11:52 -07:00
|
|
|
for (i, keyed_account) in keyed_accounts.iter().enumerate() {
|
|
|
|
let (is_dup, position) = is_dup(&keyed_accounts[..i], keyed_account);
|
|
|
|
if is_dup {
|
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 {
|
2021-04-19 13:30:31 -07:00
|
|
|
v.write_u8(std::u8::MAX)
|
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2020-08-11 16:11:52 -07:00
|
|
|
v.write_u8(keyed_account.signer_key().is_some() as u8)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
|
|
|
v.write_u8(keyed_account.is_writable() as u8)
|
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
|
|
|
v.write_all(keyed_account.unsigned_key().as_ref())
|
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2020-08-11 16:11:52 -07:00
|
|
|
v.write_u64::<LittleEndian>(keyed_account.lamports()?)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2020-08-11 16:11:52 -07:00
|
|
|
v.write_u64::<LittleEndian>(keyed_account.data_len()? as u64)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2021-06-18 06:34:46 -07:00
|
|
|
v.write_all(keyed_account.try_account_ref()?.data())
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
|
|
|
v.write_all(keyed_account.owner()?.as_ref())
|
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
|
|
|
v.write_u8(keyed_account.executable()? as u8)
|
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2020-08-11 16:11:52 -07:00
|
|
|
v.write_u64::<LittleEndian>(keyed_account.rent_epoch()? as u64)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2020-08-11 16:11:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
v.write_u64::<LittleEndian>(instruction_data.len() as u64)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
|
|
|
v.write_all(instruction_data)
|
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
|
|
|
v.write_all(program_id.as_ref())
|
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2020-08-11 16:11:52 -07:00
|
|
|
Ok(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deserialize_parameters_unaligned(
|
|
|
|
keyed_accounts: &[KeyedAccount],
|
|
|
|
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
|
2021-09-17 23:09:47 -07:00
|
|
|
for (i, (keyed_account, _pre_len)) in keyed_accounts
|
|
|
|
.iter()
|
|
|
|
.zip(account_lengths.iter())
|
|
|
|
.enumerate()
|
|
|
|
{
|
2020-08-11 16:11:52 -07:00
|
|
|
let (is_dup, _) = is_dup(&keyed_accounts[..i], keyed_account);
|
|
|
|
start += 1; // is_dup
|
|
|
|
if !is_dup {
|
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
|
|
|
|
keyed_account
|
|
|
|
.try_account_ref_mut()?
|
|
|
|
.set_lamports(LittleEndian::read_u64(&buffer[start..]));
|
|
|
|
start += size_of::<u64>() // lamports
|
2020-08-17 13:38:42 -07:00
|
|
|
+ size_of::<u64>(); // data length
|
2021-05-28 09:50:25 -07:00
|
|
|
let end = start + keyed_account.data_len()?;
|
|
|
|
keyed_account
|
|
|
|
.try_account_ref_mut()?
|
|
|
|
.set_data_from_slice(&buffer[start..end]);
|
|
|
|
start += keyed_account.data_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(())
|
|
|
|
}
|
|
|
|
|
2021-03-11 19:28:21 -08:00
|
|
|
pub fn get_serialized_account_size_aligned(
|
|
|
|
keyed_account: &KeyedAccount,
|
|
|
|
) -> Result<usize, InstructionError> {
|
|
|
|
let data_len = keyed_account.data_len()?;
|
|
|
|
Ok(
|
|
|
|
size_of::<u8>() // is_signer
|
|
|
|
+ size_of::<u8>() // is_writable
|
|
|
|
+ size_of::<u8>() // executable
|
|
|
|
+ 4 // padding to 128-bit aligned
|
|
|
|
+ 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(align_of::<u128>())
|
|
|
|
+ size_of::<u64>(), // rent epoch
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-08-11 16:11:52 -07:00
|
|
|
pub fn serialize_parameters_aligned(
|
|
|
|
program_id: &Pubkey,
|
|
|
|
keyed_accounts: &[KeyedAccount],
|
|
|
|
instruction_data: &[u8],
|
2021-04-20 11:07:30 -07:00
|
|
|
) -> Result<AlignedMemory, InstructionError> {
|
2020-09-10 11:13:35 -07:00
|
|
|
// Calculate size in order to alloc once
|
|
|
|
let mut size = size_of::<u64>();
|
|
|
|
for (i, keyed_account) in keyed_accounts.iter().enumerate() {
|
|
|
|
let (is_dup, _) = is_dup(&keyed_accounts[..i], keyed_account);
|
2021-03-11 19:28:21 -08:00
|
|
|
size += 1; // dup
|
|
|
|
if is_dup {
|
|
|
|
size += 7; // padding to 64-bit aligned
|
|
|
|
} else {
|
|
|
|
size += get_serialized_account_size_aligned(keyed_account)?;
|
2020-09-10 11:13:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
size += size_of::<u64>() // data len
|
|
|
|
+ instruction_data.len()
|
|
|
|
+ size_of::<Pubkey>(); // program id;
|
2021-04-20 11:07:30 -07:00
|
|
|
let mut v = AlignedMemory::new(size, HOST_ALIGN);
|
2020-09-10 11:13:35 -07:00
|
|
|
|
|
|
|
// Serialize into the buffer
|
2020-08-11 16:11:52 -07:00
|
|
|
v.write_u64::<LittleEndian>(keyed_accounts.len() as u64)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2020-08-11 16:11:52 -07:00
|
|
|
for (i, keyed_account) in keyed_accounts.iter().enumerate() {
|
|
|
|
let (is_dup, position) = is_dup(&keyed_accounts[..i], keyed_account);
|
|
|
|
if is_dup {
|
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 {
|
2021-04-19 13:30:31 -07:00
|
|
|
v.write_u8(std::u8::MAX)
|
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2020-08-11 16:11:52 -07:00
|
|
|
v.write_u8(keyed_account.signer_key().is_some() as u8)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
|
|
|
v.write_u8(keyed_account.is_writable() as u8)
|
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
|
|
|
v.write_u8(keyed_account.executable()? as u8)
|
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
|
|
|
v.write_all(&[0u8, 0, 0, 0])
|
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?; // 4 bytes of padding to make 128-bit aligned
|
|
|
|
v.write_all(keyed_account.unsigned_key().as_ref())
|
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
|
|
|
v.write_all(keyed_account.owner()?.as_ref())
|
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2020-08-11 16:11:52 -07:00
|
|
|
v.write_u64::<LittleEndian>(keyed_account.lamports()?)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2020-08-11 16:11:52 -07:00
|
|
|
v.write_u64::<LittleEndian>(keyed_account.data_len()? as u64)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2021-06-18 06:34:46 -07:00
|
|
|
v.write_all(keyed_account.try_account_ref()?.data())
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2021-05-19 13:43:59 -07:00
|
|
|
v.resize(
|
2021-04-20 11:07:30 -07:00
|
|
|
MAX_PERMITTED_DATA_INCREASE
|
2021-11-16 07:02:22 -08:00
|
|
|
+ (v.write_index() as *const u8).align_offset(PARAMETER_ALIGNMENT),
|
2020-08-17 10:24:34 -07:00
|
|
|
0,
|
2021-04-20 11:07:30 -07:00
|
|
|
)
|
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2020-08-11 16:11:52 -07:00
|
|
|
v.write_u64::<LittleEndian>(keyed_account.rent_epoch()? as u64)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2020-08-11 16:11:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
v.write_u64::<LittleEndian>(instruction_data.len() as u64)
|
2021-04-19 13:30:31 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
|
|
|
v.write_all(instruction_data)
|
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
|
|
|
v.write_all(program_id.as_ref())
|
|
|
|
.map_err(|_| InstructionError::InvalidArgument)?;
|
2020-08-11 16:11:52 -07:00
|
|
|
Ok(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deserialize_parameters_aligned(
|
|
|
|
keyed_accounts: &[KeyedAccount],
|
|
|
|
buffer: &[u8],
|
2021-09-17 23:09:47 -07:00
|
|
|
account_lengths: &[usize],
|
2021-09-28 01:13:03 -07:00
|
|
|
do_support_realloc: bool,
|
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
|
2021-09-17 23:09:47 -07:00
|
|
|
for (i, (keyed_account, pre_len)) in keyed_accounts
|
|
|
|
.iter()
|
|
|
|
.zip(account_lengths.iter())
|
|
|
|
.enumerate()
|
|
|
|
{
|
2020-08-11 16:11:52 -07:00
|
|
|
let (is_dup, _) = is_dup(&keyed_accounts[..i], keyed_account);
|
2020-08-17 13:38:42 -07:00
|
|
|
start += size_of::<u8>(); // position
|
|
|
|
if is_dup {
|
|
|
|
start += 7; // padding to 64-bit aligned
|
2021-05-28 09:50:25 -07:00
|
|
|
} else {
|
2020-08-17 13:38:42 -07:00
|
|
|
let mut account = keyed_account.try_account_ref_mut()?;
|
|
|
|
start += size_of::<u8>() // is_signer
|
2020-09-10 11:13:35 -07:00
|
|
|
+ size_of::<u8>() // is_writable
|
|
|
|
+ size_of::<u8>() // executable
|
|
|
|
+ 4 // padding to 128-bit aligned
|
|
|
|
+ size_of::<Pubkey>(); // key
|
2021-04-23 07:34:08 -07:00
|
|
|
account.copy_into_owner_from_slice(&buffer[start..start + size_of::<Pubkey>()]);
|
2020-08-17 13:38:42 -07:00
|
|
|
start += size_of::<Pubkey>(); // owner
|
2021-04-29 08:43:26 -07:00
|
|
|
account.set_lamports(LittleEndian::read_u64(&buffer[start..]));
|
2020-08-17 13:38:42 -07:00
|
|
|
start += size_of::<u64>(); // lamports
|
|
|
|
let post_len = LittleEndian::read_u64(&buffer[start..]) as usize;
|
|
|
|
start += size_of::<u64>(); // data length
|
2021-09-28 01:13:03 -07:00
|
|
|
let data_end = if do_support_realloc {
|
|
|
|
if post_len.saturating_sub(*pre_len) > MAX_PERMITTED_DATA_INCREASE
|
|
|
|
|| post_len > MAX_PERMITTED_DATA_LENGTH as usize
|
|
|
|
{
|
|
|
|
return Err(InstructionError::InvalidRealloc);
|
|
|
|
}
|
|
|
|
start + post_len
|
|
|
|
} else {
|
|
|
|
let mut data_end = start + *pre_len;
|
|
|
|
if post_len != *pre_len
|
|
|
|
&& (post_len.saturating_sub(*pre_len)) <= MAX_PERMITTED_DATA_INCREASE
|
|
|
|
{
|
|
|
|
data_end = start + post_len;
|
|
|
|
}
|
|
|
|
data_end
|
|
|
|
};
|
2021-03-16 14:56:26 -07:00
|
|
|
account.set_data_from_slice(&buffer[start..data_end]);
|
2021-09-17 23:09:47 -07:00
|
|
|
start += *pre_len + MAX_PERMITTED_DATA_INCREASE; // data
|
2020-08-17 13:38:42 -07:00
|
|
|
start += (start as *const u8).align_offset(align_of::<u128>());
|
|
|
|
start += size_of::<u64>(); // rent_epoch
|
2020-08-11 16:11:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2021-11-08 09:51:36 -08:00
|
|
|
use solana_program_runtime::invoke_context::{prepare_mock_invoke_context, ThisInvokeContext};
|
2020-08-11 16:11:52 -07:00
|
|
|
use solana_sdk::{
|
2021-03-11 16:09:04 -08:00
|
|
|
account::{Account, AccountSharedData},
|
|
|
|
account_info::AccountInfo,
|
|
|
|
bpf_loader,
|
|
|
|
entrypoint::deserialize,
|
2021-11-08 09:51:36 -08:00
|
|
|
process_instruction::InvokeContext,
|
2020-08-11 16:11:52 -07:00
|
|
|
};
|
|
|
|
use std::{
|
|
|
|
cell::RefCell,
|
|
|
|
rc::Rc,
|
|
|
|
// Hide Result from bindgen gets confused about generics in non-generic type declarations
|
|
|
|
slice::{from_raw_parts, from_raw_parts_mut},
|
|
|
|
};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serialize_parameters() {
|
2020-10-19 12:12:08 -07:00
|
|
|
let program_id = solana_sdk::pubkey::new_rand();
|
|
|
|
let dup_key = solana_sdk::pubkey::new_rand();
|
2021-03-11 19:28:21 -08:00
|
|
|
let dup_key2 = solana_sdk::pubkey::new_rand();
|
2021-11-08 09:51:36 -08:00
|
|
|
let keyed_accounts = [
|
|
|
|
(
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
program_id,
|
|
|
|
Rc::new(RefCell::new(AccountSharedData::from(Account {
|
|
|
|
lamports: 0,
|
|
|
|
data: vec![],
|
|
|
|
owner: bpf_loader::id(),
|
|
|
|
executable: true,
|
|
|
|
rent_epoch: 0,
|
|
|
|
}))),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
dup_key,
|
|
|
|
Rc::new(RefCell::new(AccountSharedData::from(Account {
|
|
|
|
lamports: 1,
|
|
|
|
data: vec![1u8, 2, 3, 4, 5],
|
|
|
|
owner: bpf_loader::id(),
|
|
|
|
executable: false,
|
|
|
|
rent_epoch: 100,
|
|
|
|
}))),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
dup_key,
|
|
|
|
Rc::new(RefCell::new(AccountSharedData::from(Account {
|
|
|
|
lamports: 1,
|
|
|
|
data: vec![1u8, 2, 3, 4, 5],
|
|
|
|
owner: bpf_loader::id(),
|
|
|
|
executable: false,
|
|
|
|
rent_epoch: 100,
|
|
|
|
}))),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
solana_sdk::pubkey::new_rand(),
|
|
|
|
Rc::new(RefCell::new(AccountSharedData::from(Account {
|
|
|
|
lamports: 2,
|
|
|
|
data: vec![11u8, 12, 13, 14, 15, 16, 17, 18, 19],
|
|
|
|
owner: bpf_loader::id(),
|
|
|
|
executable: true,
|
|
|
|
rent_epoch: 200,
|
|
|
|
}))),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
solana_sdk::pubkey::new_rand(),
|
|
|
|
Rc::new(RefCell::new(AccountSharedData::from(Account {
|
|
|
|
lamports: 3,
|
|
|
|
data: vec![],
|
|
|
|
owner: bpf_loader::id(),
|
|
|
|
executable: false,
|
|
|
|
rent_epoch: 3100,
|
|
|
|
}))),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
dup_key2,
|
|
|
|
Rc::new(RefCell::new(AccountSharedData::from(Account {
|
|
|
|
lamports: 4,
|
|
|
|
data: vec![1u8, 2, 3, 4, 5],
|
|
|
|
owner: bpf_loader::id(),
|
|
|
|
executable: false,
|
|
|
|
rent_epoch: 100,
|
|
|
|
}))),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
dup_key2,
|
|
|
|
Rc::new(RefCell::new(AccountSharedData::from(Account {
|
|
|
|
lamports: 4,
|
|
|
|
data: vec![1u8, 2, 3, 4, 5],
|
|
|
|
owner: bpf_loader::id(),
|
|
|
|
executable: false,
|
|
|
|
rent_epoch: 100,
|
|
|
|
}))),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
solana_sdk::pubkey::new_rand(),
|
|
|
|
Rc::new(RefCell::new(AccountSharedData::from(Account {
|
|
|
|
lamports: 5,
|
|
|
|
data: vec![11u8, 12, 13, 14, 15, 16, 17, 18, 19],
|
|
|
|
owner: bpf_loader::id(),
|
|
|
|
executable: true,
|
|
|
|
rent_epoch: 200,
|
|
|
|
}))),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
solana_sdk::pubkey::new_rand(),
|
|
|
|
Rc::new(RefCell::new(AccountSharedData::from(Account {
|
|
|
|
lamports: 6,
|
|
|
|
data: vec![],
|
|
|
|
owner: bpf_loader::id(),
|
|
|
|
executable: false,
|
|
|
|
rent_epoch: 3100,
|
|
|
|
}))),
|
|
|
|
),
|
2020-10-19 12:23:14 -07:00
|
|
|
];
|
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];
|
|
|
|
let preparation =
|
|
|
|
prepare_mock_invoke_context(&program_indices, &instruction_data, &keyed_accounts);
|
|
|
|
let mut invoke_context = ThisInvokeContext::new_mock(&preparation.accounts, &[]);
|
|
|
|
invoke_context
|
|
|
|
.push(
|
|
|
|
&preparation.message,
|
|
|
|
&preparation.message.instructions[0],
|
|
|
|
&program_indices,
|
|
|
|
Some(&preparation.account_indices),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-08-11 16:11:52 -07:00
|
|
|
|
|
|
|
// check serialize_parameters_aligned
|
|
|
|
|
2021-11-08 09:51:36 -08:00
|
|
|
let ser_keyed_accounts = invoke_context.get_keyed_accounts().unwrap();
|
2021-09-17 23:09:47 -07:00
|
|
|
let (mut serialized, account_lengths) = serialize_parameters(
|
2020-08-11 16:11:52 -07:00
|
|
|
&bpf_loader::id(),
|
|
|
|
&program_id,
|
2021-11-08 09:51:36 -08:00
|
|
|
&ser_keyed_accounts[1..],
|
2020-08-11 16:11:52 -07:00
|
|
|
&instruction_data,
|
|
|
|
)
|
|
|
|
.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) =
|
2021-04-20 11:07:30 -07:00
|
|
|
unsafe { deserialize(&mut serialized.as_slice_mut()[0] 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);
|
|
|
|
assert_eq!(
|
|
|
|
(&de_instruction_data[0] as *const u8).align_offset(align_of::<u128>()),
|
|
|
|
0
|
|
|
|
);
|
2021-11-08 09:51:36 -08:00
|
|
|
for ((_, _, key, account), account_info) in keyed_accounts.iter().skip(1).zip(de_accounts) {
|
|
|
|
assert_eq!(key, account_info.key);
|
2020-08-11 16:11:52 -07:00
|
|
|
let account = account.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!(
|
|
|
|
(*account_info.lamports.borrow() as *const u64).align_offset(align_of::<u64>()),
|
|
|
|
0
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
account_info
|
|
|
|
.data
|
|
|
|
.borrow()
|
|
|
|
.as_ptr()
|
|
|
|
.align_offset(align_of::<u128>()),
|
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-08 09:51:36 -08:00
|
|
|
let de_keyed_accounts = invoke_context.get_keyed_accounts().unwrap();
|
2021-09-17 23:09:47 -07:00
|
|
|
deserialize_parameters(
|
|
|
|
&bpf_loader::id(),
|
2021-11-08 09:51:36 -08:00
|
|
|
&de_keyed_accounts[1..],
|
2021-09-17 23:09:47 -07:00
|
|
|
serialized.as_slice(),
|
|
|
|
&account_lengths,
|
2021-09-28 01:13:03 -07:00
|
|
|
true,
|
2021-09-17 23:09:47 -07:00
|
|
|
)
|
|
|
|
.unwrap();
|
2021-11-08 09:51:36 -08:00
|
|
|
for ((_, _, key, account), de_keyed_account) in keyed_accounts.iter().zip(de_keyed_accounts)
|
2021-03-11 19:28:21 -08:00
|
|
|
{
|
2021-11-08 09:51:36 -08:00
|
|
|
assert_eq!(key, de_keyed_account.unsigned_key());
|
2021-03-11 19:28:21 -08:00
|
|
|
let account = account.borrow();
|
2021-04-28 06:53:54 -07:00
|
|
|
assert_eq!(account.executable(), de_keyed_account.executable().unwrap());
|
2021-04-27 11:51:13 -07:00
|
|
|
assert_eq!(account.rent_epoch(), de_keyed_account.rent_epoch().unwrap());
|
2021-03-11 19:28:21 -08:00
|
|
|
}
|
|
|
|
|
2020-08-11 16:11:52 -07:00
|
|
|
// check serialize_parameters_unaligned
|
|
|
|
|
2021-11-08 09:51:36 -08:00
|
|
|
let ser_keyed_accounts = invoke_context.get_keyed_accounts().unwrap();
|
2021-09-17 23:09:47 -07:00
|
|
|
let (mut serialized, account_lengths) = serialize_parameters(
|
2020-08-11 16:11:52 -07:00
|
|
|
&bpf_loader_deprecated::id(),
|
|
|
|
&program_id,
|
2021-11-08 09:51:36 -08:00
|
|
|
&ser_keyed_accounts[1..],
|
2020-08-11 16:11:52 -07:00
|
|
|
&instruction_data,
|
|
|
|
)
|
|
|
|
.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) =
|
2021-04-20 11:07:30 -07:00
|
|
|
unsafe { deserialize_unaligned(&mut serialized.as_slice_mut()[0] 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-11-08 09:51:36 -08:00
|
|
|
for ((_, _, key, account), account_info) in keyed_accounts.iter().skip(1).zip(de_accounts) {
|
|
|
|
assert_eq!(key, account_info.key);
|
2020-08-11 16:11:52 -07:00
|
|
|
let account = account.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
|
|
|
|
2021-11-08 09:51:36 -08:00
|
|
|
let de_keyed_accounts = invoke_context.get_keyed_accounts().unwrap();
|
2021-03-11 19:28:21 -08:00
|
|
|
deserialize_parameters(
|
|
|
|
&bpf_loader_deprecated::id(),
|
2021-11-08 09:51:36 -08:00
|
|
|
&de_keyed_accounts[1..],
|
2021-04-20 11:07:30 -07:00
|
|
|
serialized.as_slice(),
|
2021-09-17 23:09:47 -07:00
|
|
|
&account_lengths,
|
2021-09-28 01:13:03 -07:00
|
|
|
true,
|
2021-03-11 19:28:21 -08:00
|
|
|
)
|
|
|
|
.unwrap();
|
2021-11-08 09:51:36 -08:00
|
|
|
for ((_, _, key, account), de_keyed_account) in keyed_accounts.iter().zip(de_keyed_accounts)
|
2021-03-11 19:28:21 -08:00
|
|
|
{
|
2021-11-08 09:51:36 -08:00
|
|
|
assert_eq!(key, de_keyed_account.unsigned_key());
|
2021-03-11 19:28:21 -08:00
|
|
|
let account = account.borrow();
|
2021-04-30 11:16:58 -07:00
|
|
|
assert_eq!(account.lamports(), de_keyed_account.lamports().unwrap());
|
2021-03-11 19:28:21 -08:00
|
|
|
assert_eq!(
|
2021-04-20 14:41:16 -07:00
|
|
|
account.data(),
|
|
|
|
de_keyed_account.try_account_ref().unwrap().data()
|
2021-03-11 19:28:21 -08:00
|
|
|
);
|
2021-04-26 08:40:11 -07:00
|
|
|
assert_eq!(*account.owner(), de_keyed_account.owner().unwrap());
|
2021-04-28 06:53:54 -07:00
|
|
|
assert_eq!(account.executable(), de_keyed_account.executable().unwrap());
|
2021-04-27 11:51:13 -07:00
|
|
|
assert_eq!(account.rent_epoch(), de_keyed_account.rent_epoch().unwrap());
|
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>();
|
|
|
|
if dup_info == std::u8::MAX {
|
|
|
|
#[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
|
|
|
|
accounts.push(accounts[dup_info as usize].clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|