Fix more BPF alignment issues on arm64

This commit is contained in:
Michael Vines
2021-11-30 10:41:15 -08:00
parent a54fa45d5a
commit e5aa5efbac
3 changed files with 13 additions and 16 deletions

View File

@ -3,15 +3,12 @@ use solana_rbpf::{aligned_memory::AlignedMemory, ebpf::HOST_ALIGN};
use solana_sdk::{ use solana_sdk::{
account::{ReadableAccount, WritableAccount}, account::{ReadableAccount, WritableAccount},
bpf_loader_deprecated, bpf_loader_deprecated,
entrypoint::{MAX_PERMITTED_DATA_INCREASE, PARAMETER_ALIGNMENT}, entrypoint::{BPF_ALIGN_OF_U128, MAX_PERMITTED_DATA_INCREASE},
instruction::InstructionError, instruction::InstructionError,
keyed_account::KeyedAccount, keyed_account::KeyedAccount,
pubkey::Pubkey, pubkey::Pubkey,
}; };
use std::{ use std::{io::prelude::*, mem::size_of};
io::prelude::*,
mem::{align_of, size_of},
};
/// Look for a duplicate account and return its position if found /// Look for a duplicate account and return its position if found
pub fn is_dup(accounts: &[KeyedAccount], keyed_account: &KeyedAccount) -> (bool, usize) { pub fn is_dup(accounts: &[KeyedAccount], keyed_account: &KeyedAccount) -> (bool, usize) {
@ -168,7 +165,7 @@ pub fn get_serialized_account_size_aligned(
+ size_of::<u64>() // data len + size_of::<u64>() // data len
+ data_len + data_len
+ MAX_PERMITTED_DATA_INCREASE + MAX_PERMITTED_DATA_INCREASE
+ (data_len as *const u8).align_offset(align_of::<u128>()) + (data_len as *const u8).align_offset(BPF_ALIGN_OF_U128)
+ size_of::<u64>(), // rent epoch + size_of::<u64>(), // rent epoch
) )
} }
@ -227,7 +224,7 @@ pub fn serialize_parameters_aligned(
.map_err(|_| InstructionError::InvalidArgument)?; .map_err(|_| InstructionError::InvalidArgument)?;
v.resize( v.resize(
MAX_PERMITTED_DATA_INCREASE MAX_PERMITTED_DATA_INCREASE
+ (v.write_index() as *const u8).align_offset(PARAMETER_ALIGNMENT), + (v.write_index() as *const u8).align_offset(BPF_ALIGN_OF_U128),
0, 0,
) )
.map_err(|_| InstructionError::InvalidArgument)?; .map_err(|_| InstructionError::InvalidArgument)?;
@ -277,7 +274,7 @@ pub fn deserialize_parameters_aligned(
account.set_data_from_slice(&buffer[start..data_end]); account.set_data_from_slice(&buffer[start..data_end]);
start += pre_len + MAX_PERMITTED_DATA_INCREASE; // data start += pre_len + MAX_PERMITTED_DATA_INCREASE; // data
start += (start as *const u8).align_offset(align_of::<u128>()); start += (start as *const u8).align_offset(BPF_ALIGN_OF_U128);
start += size_of::<u64>(); // rent_epoch start += size_of::<u64>(); // rent_epoch
} }
} }
@ -406,7 +403,7 @@ mod tests {
assert_eq!(&program_id, de_program_id); assert_eq!(&program_id, de_program_id);
assert_eq!(instruction_data, de_instruction_data); assert_eq!(instruction_data, de_instruction_data);
assert_eq!( assert_eq!(
(&de_instruction_data[0] as *const u8).align_offset(align_of::<u128>()), (&de_instruction_data[0] as *const u8).align_offset(BPF_ALIGN_OF_U128),
0 0
); );
for ((account, account_info), key) in accounts.iter().zip(de_accounts).zip(keys.clone()) { for ((account, account_info), key) in accounts.iter().zip(de_accounts).zip(keys.clone()) {
@ -419,7 +416,7 @@ mod tests {
assert_eq!(account.rent_epoch(), account_info.rent_epoch); assert_eq!(account.rent_epoch(), account_info.rent_epoch);
assert_eq!( assert_eq!(
(*account_info.lamports.borrow() as *const u64).align_offset(align_of::<u64>()), (*account_info.lamports.borrow() as *const u64).align_offset(BPF_ALIGN_OF_U128),
0 0
); );
assert_eq!( assert_eq!(
@ -427,7 +424,7 @@ mod tests {
.data .data
.borrow() .borrow()
.as_ptr() .as_ptr()
.align_offset(align_of::<u128>()), .align_offset(BPF_ALIGN_OF_U128),
0 0
); );
} }

View File

@ -16,7 +16,7 @@ use solana_sdk::{
bpf_loader, bpf_loader_deprecated, bpf_loader, bpf_loader_deprecated,
bpf_loader_upgradeable::{self, UpgradeableLoaderState}, bpf_loader_upgradeable::{self, UpgradeableLoaderState},
clock::Clock, clock::Clock,
entrypoint::{MAX_PERMITTED_DATA_INCREASE, SUCCESS}, entrypoint::{BPF_ALIGN_OF_U128, MAX_PERMITTED_DATA_INCREASE, SUCCESS},
epoch_schedule::EpochSchedule, epoch_schedule::EpochSchedule,
feature_set::{ feature_set::{
allow_native_ids, check_seed_length, close_upgradeable_program_accounts, cpi_data_cost, allow_native_ids, check_seed_length, close_upgradeable_program_accounts, cpi_data_cost,
@ -825,7 +825,7 @@ impl SyscallObject<BpfError> for SyscallAllocFree {
result: &mut Result<u64, EbpfError<BpfError>>, result: &mut Result<u64, EbpfError<BpfError>>,
) { ) {
let align = if self.aligned { let align = if self.aligned {
align_of::<u128>() BPF_ALIGN_OF_U128
} else { } else {
align_of::<u8>() align_of::<u8>()
}; };

View File

@ -252,8 +252,8 @@ unsafe impl std::alloc::GlobalAlloc for BumpAllocator {
/// Maximum number of bytes a program may add to an account during a single realloc /// Maximum number of bytes a program may add to an account during a single realloc
pub const MAX_PERMITTED_DATA_INCREASE: usize = 1_024 * 10; pub const MAX_PERMITTED_DATA_INCREASE: usize = 1_024 * 10;
// Parameters passed to the entrypoint input buffer are aligned on 8-byte boundaries /// `assert_eq(std::mem::align_of::<u128>(), 8)` is true for BPF but not for some host machines
pub const PARAMETER_ALIGNMENT: usize = 8; pub const BPF_ALIGN_OF_U128: usize = 8;
/// Deserialize the input arguments /// Deserialize the input arguments
/// ///
@ -312,7 +312,7 @@ pub unsafe fn deserialize<'a>(input: *mut u8) -> (&'a Pubkey, Vec<AccountInfo<'a
from_raw_parts_mut(input.add(offset), data_len) from_raw_parts_mut(input.add(offset), data_len)
})); }));
offset += data_len + MAX_PERMITTED_DATA_INCREASE; offset += data_len + MAX_PERMITTED_DATA_INCREASE;
offset += (offset as *const u8).align_offset(PARAMETER_ALIGNMENT); // padding offset += (offset as *const u8).align_offset(BPF_ALIGN_OF_U128); // padding
#[allow(clippy::cast_ptr_alignment)] #[allow(clippy::cast_ptr_alignment)]
let rent_epoch = *(input.add(offset) as *const u64); let rent_epoch = *(input.add(offset) as *const u64);