Skip to content

Commit

Permalink
Adds stable layout types to pass to the runtime (solana-labs#30192)
Browse files Browse the repository at this point in the history
  • Loading branch information
brooksprumo authored and joncinque committed Aug 10, 2023
1 parent bfc0122 commit 3cde6cf
Show file tree
Hide file tree
Showing 16 changed files with 414 additions and 30 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions program-runtime/src/invoke_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ use {
cap_accounts_data_len, enable_early_verification_of_account_modifications, FeatureSet,
},
hash::Hash,
instruction::{AccountMeta, Instruction, InstructionError},
instruction::{AccountMeta, InstructionError},
native_loader,
pubkey::Pubkey,
rent::Rent,
saturating_add_assign,
stable_layout::stable_instruction::StableInstruction,
transaction_context::{InstructionAccount, TransactionAccount, TransactionContext},
},
std::{
Expand Down Expand Up @@ -563,7 +564,7 @@ impl<'a> InvokeContext<'a> {
/// Entrypoint for a cross-program invocation from a builtin program
pub fn native_invoke(
&mut self,
instruction: Instruction,
instruction: StableInstruction,
signers: &[Pubkey],
) -> Result<(), InstructionError> {
let (instruction_accounts, program_indices) =
Expand All @@ -583,7 +584,7 @@ impl<'a> InvokeContext<'a> {
#[allow(clippy::type_complexity)]
pub fn prepare_instruction(
&mut self,
instruction: &Instruction,
instruction: &StableInstruction,
signers: &[Pubkey],
) -> Result<(Vec<InstructionAccount>, Vec<usize>), InstructionError> {
// Finds the index of each account in the instruction by its pubkey.
Expand Down Expand Up @@ -1111,7 +1112,7 @@ mod tests {
super::*,
crate::compute_budget,
serde::{Deserialize, Serialize},
solana_sdk::account::WritableAccount,
solana_sdk::{account::WritableAccount, instruction::Instruction},
};

#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -1231,7 +1232,7 @@ mod tests {
assert_eq!(result, Err(InstructionError::UnbalancedInstruction));
result?;
invoke_context
.native_invoke(inner_instruction, &[])
.native_invoke(inner_instruction.into(), &[])
.and(invoke_context.pop())?;
}
MockInstruction::UnbalancedPop => instruction_context
Expand Down Expand Up @@ -1384,7 +1385,7 @@ mod tests {
let inner_instruction =
Instruction::new_with_bincode(callee_program_id, &case.0, metas.clone());
let result = invoke_context
.native_invoke(inner_instruction, &[])
.native_invoke(inner_instruction.into(), &[])
.and(invoke_context.pop());
assert_eq!(result, case.1);
}
Expand All @@ -1404,6 +1405,7 @@ mod tests {
},
metas.clone(),
);
let inner_instruction = StableInstruction::from(inner_instruction);
let (inner_instruction_accounts, program_indices) = invoke_context
.prepare_instruction(&inner_instruction, &[])
.unwrap();
Expand Down
4 changes: 3 additions & 1 deletion program-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use {
pubkey::Pubkey,
rent::Rent,
signature::{Keypair, Signer},
stable_layout::stable_instruction::StableInstruction,
sysvar::{Sysvar, SysvarId},
},
solana_vote_program::vote_state::{VoteState, VoteStateVersions},
Expand Down Expand Up @@ -226,6 +227,7 @@ impl solana_sdk::program_stubs::SyscallStubs for SyscallStubs {
account_infos: &[AccountInfo],
signers_seeds: &[&[&[u8]]],
) -> ProgramResult {
let instruction = StableInstruction::from(instruction.clone());
let invoke_context = get_invoke_context();
let log_collector = invoke_context.get_log_collector();
let transaction_context = &invoke_context.transaction_context;
Expand All @@ -248,7 +250,7 @@ impl solana_sdk::program_stubs::SyscallStubs for SyscallStubs {
.collect::<Vec<_>>();

let (instruction_accounts, program_indices) = invoke_context
.prepare_instruction(instruction, &signers)
.prepare_instruction(&instruction, &signers)
.unwrap();

// Copy caller's account_info modifications into invoke_context accounts
Expand Down
8 changes: 4 additions & 4 deletions programs/address-lookup-table/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,18 @@ impl Processor {

if required_lamports > 0 {
invoke_context.native_invoke(
system_instruction::transfer(&payer_key, &table_key, required_lamports),
system_instruction::transfer(&payer_key, &table_key, required_lamports).into(),
&[payer_key],
)?;
}

invoke_context.native_invoke(
system_instruction::allocate(&table_key, table_account_data_len as u64),
system_instruction::allocate(&table_key, table_account_data_len as u64).into(),
&[table_key],
)?;

invoke_context.native_invoke(
system_instruction::assign(&table_key, &crate::id()),
system_instruction::assign(&table_key, &crate::id()).into(),
&[table_key],
)?;

Expand Down Expand Up @@ -313,7 +313,7 @@ impl Processor {
drop(payer_account);

invoke_context.native_invoke(
system_instruction::transfer(&payer_key, &table_key, required_lamports),
system_instruction::transfer(&payer_key, &table_key, required_lamports).into(),
&[payer_key],
)?;
}
Expand Down
1 change: 1 addition & 0 deletions programs/bpf_loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ solana_rbpf = "=0.2.31"
thiserror = "1.0"

[dev-dependencies]
memoffset = "0.8"
rand = "0.7.3"
solana-runtime = { path = "../../runtime", version = "=1.14.24" }

Expand Down
5 changes: 3 additions & 2 deletions programs/bpf_loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ fn process_loader_upgradeable_instruction(
.iter()
.map(|seeds| Pubkey::create_program_address(*seeds, caller_program_id))
.collect::<Result<Vec<Pubkey>, solana_sdk::pubkey::PubkeyError>>()?;
invoke_context.native_invoke(instruction, signers.as_slice())?;
invoke_context.native_invoke(instruction.into(), signers.as_slice())?;

// Load and verify the program bits
let executor = create_executor(
Expand Down Expand Up @@ -1146,7 +1146,8 @@ fn process_loader_upgradeable_instruction(
)?;

invoke_context.native_invoke(
system_instruction::transfer(&payer_key, &programdata_key, required_payment),
system_instruction::transfer(&payer_key, &programdata_key, required_payment)
.into(),
&[],
)?;
}
Expand Down
20 changes: 11 additions & 9 deletions programs/bpf_loader/src/syscalls/cpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use {
crate::declare_syscall,
solana_sdk::syscalls::{
MAX_CPI_ACCOUNT_INFOS, MAX_CPI_INSTRUCTION_ACCOUNTS, MAX_CPI_INSTRUCTION_DATA_LEN,
stable_layout::stable_instruction::StableInstruction,
},
};

Expand All @@ -27,7 +28,7 @@ trait SyscallInvokeSigned<'a, 'b> {
addr: u64,
memory_mapping: &mut MemoryMapping,
invoke_context: &mut InvokeContext,
) -> Result<Instruction, EbpfError<BpfError>>;
) -> Result<StableInstruction, EbpfError<BpfError>>;
fn translate_accounts<'c>(
&'c self,
instruction_accounts: &[InstructionAccount],
Expand Down Expand Up @@ -84,7 +85,7 @@ impl<'a, 'b> SyscallInvokeSigned<'a, 'b> for SyscallInvokeSignedRust<'a, 'b> {
addr: u64,
memory_mapping: &mut MemoryMapping,
invoke_context: &mut InvokeContext,
) -> Result<Instruction, EbpfError<BpfError>> {
) -> Result<StableInstruction, EbpfError<BpfError>> {
let ix = translate_type::<Instruction>(
memory_mapping,
addr,
Expand Down Expand Up @@ -121,10 +122,11 @@ impl<'a, 'b> SyscallInvokeSigned<'a, 'b> for SyscallInvokeSignedRust<'a, 'b> {
invoke_context.get_check_size(),
)?
.to_vec();
Ok(Instruction {

Ok(StableInstruction {
accounts: accounts.into(),
data: data.into(),
program_id: ix.program_id,
accounts,
data,
})
}

Expand Down Expand Up @@ -394,7 +396,7 @@ impl<'a, 'b> SyscallInvokeSigned<'a, 'b> for SyscallInvokeSignedC<'a, 'b> {
addr: u64,
memory_mapping: &mut MemoryMapping,
invoke_context: &mut InvokeContext,
) -> Result<Instruction, EbpfError<BpfError>> {
) -> Result<StableInstruction, EbpfError<BpfError>> {
let ix_c = translate_type::<SolInstruction>(
memory_mapping,
addr,
Expand Down Expand Up @@ -454,10 +456,10 @@ impl<'a, 'b> SyscallInvokeSigned<'a, 'b> for SyscallInvokeSignedC<'a, 'b> {
})
.collect::<Result<Vec<AccountMeta>, EbpfError<BpfError>>>()?;

Ok(Instruction {
Ok(StableInstruction {
accounts: accounts.into(),
data: data.into(),
program_id: *program_id,
accounts,
data,
})
}

Expand Down
11 changes: 7 additions & 4 deletions programs/bpf_loader/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use {
},
hash::{Hasher, HASH_BYTES},
instruction::{
AccountMeta, Instruction, InstructionError, ProcessedSiblingInstruction,
AccountMeta, InstructionError, ProcessedSiblingInstruction,
TRANSACTION_LEVEL_STACK_HEIGHT,
},
keccak, native_loader,
Expand Down Expand Up @@ -2073,7 +2073,9 @@ mod tests {
bpf_loader,
fee_calculator::FeeCalculator,
hash::hashv,
instruction::Instruction,
program::check_type_assumptions,
stable_layout::stable_instruction::StableInstruction,
sysvar::{clock::Clock, epoch_schedule::EpochSchedule, rent::Rent},
transaction_context::TransactionContext,
},
Expand Down Expand Up @@ -2191,11 +2193,12 @@ mod tests {
&"foobar",
vec![AccountMeta::new(solana_sdk::pubkey::new_rand(), false)],
);
let instruction = StableInstruction::from(instruction);
let addr = &instruction as *const _ as u64;
let mut memory_region = MemoryRegion {
host_addr: addr,
vm_addr: 0x100000000,
len: std::mem::size_of::<Instruction>() as u64,
len: std::mem::size_of::<StableInstruction>() as u64,
vm_gap_shift: 63,
is_writable: false,
};
Expand All @@ -2205,13 +2208,13 @@ mod tests {
)
.unwrap();
let translated_instruction =
translate_type::<Instruction>(&memory_mapping, 0x100000000, true).unwrap();
translate_type::<StableInstruction>(&memory_mapping, 0x100000000, true).unwrap();
assert_eq!(instruction, *translated_instruction);
memory_region.len = 1;
memory_mapping
.replace_region::<BpfError>(1, memory_region)
.unwrap();
assert!(translate_type::<Instruction>(&memory_mapping, 0x100000000, true).is_err());
assert!(translate_type::<StableInstruction>(&memory_mapping, 0x100000000, true).is_err());
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions sdk/program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ pub mod serialize_utils;
pub mod short_vec;
pub mod slot_hashes;
pub mod slot_history;
pub mod stable_layout;
pub mod stake;
pub mod stake_history;
pub mod syscalls;
Expand Down
11 changes: 7 additions & 4 deletions sdk/program/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use crate::{
account_info::AccountInfo, entrypoint::ProgramResult, instruction::Instruction, pubkey::Pubkey,
stable_layout::stable_instruction::StableInstruction,
};

/// Invoke a cross-program instruction.
Expand Down Expand Up @@ -290,9 +291,10 @@ pub fn invoke_signed_unchecked(
) -> ProgramResult {
#[cfg(target_os = "solana")]
{
let instruction = StableInstruction::from(instruction.clone());
let result = unsafe {
crate::syscalls::sol_invoke_signed_rust(
instruction as *const _ as *const u8,
&instruction as *const _ as *const u8,
account_infos as *const _ as *const u8,
account_infos.len() as u64,
signers_seeds as *const _ as *const u8,
Expand Down Expand Up @@ -430,17 +432,18 @@ pub fn check_type_assumptions() {
accounts: vec![account_meta1.clone(), account_meta2.clone()],
data: data.clone(),
};
let instruction = StableInstruction::from(instruction);
let instruction_addr = &instruction as *const _ as u64;

// program id
assert_eq!(offset_of!(Instruction, program_id), 48);
assert_eq!(offset_of!(StableInstruction, program_id), 48);
let pubkey_ptr = (instruction_addr + 48) as *const Pubkey;
unsafe {
assert_eq!(*pubkey_ptr, pubkey1);
}

// accounts
assert_eq!(offset_of!(Instruction, accounts), 0);
assert_eq!(offset_of!(StableInstruction, accounts), 0);
let accounts_ptr = (instruction_addr) as *const *const AccountMeta;
let accounts_cap = (instruction_addr + 8) as *const usize;
let accounts_len = (instruction_addr + 16) as *const usize;
Expand All @@ -453,7 +456,7 @@ pub fn check_type_assumptions() {
}

// data
assert_eq!(offset_of!(Instruction, data), 24);
assert_eq!(offset_of!(StableInstruction, data), 24);
let data_ptr = (instruction_addr + 24) as *const *const [u8; 5];
let data_cap = (instruction_addr + 24 + 8) as *const usize;
let data_len = (instruction_addr + 24 + 16) as *const usize;
Expand Down
10 changes: 10 additions & 0 deletions sdk/program/src/stable_layout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![doc(hidden)]
//! Types with stable memory layouts
//!
//! Internal use only; here be dragons!
pub mod stable_instruction;
pub mod stable_rc;
pub mod stable_ref_cell;
pub mod stable_slice;
pub mod stable_vec;
Loading

0 comments on commit 3cde6cf

Please sign in to comment.