Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

v1.14: Adds stable layout types #32797

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions sdk/program/src/account_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,24 @@ use {

/// Account information
#[derive(Clone)]
#[repr(C)]
pub struct AccountInfo<'a> {
/// Public key of the account
pub key: &'a Pubkey,
/// Was the transaction signed by this account's public key?
pub is_signer: bool,
/// Is the account writable?
pub is_writable: bool,
/// The lamports in the account. Modifiable by programs.
pub lamports: Rc<RefCell<&'a mut u64>>,
/// The data held in this account. Modifiable by programs.
pub data: Rc<RefCell<&'a mut [u8]>>,
/// Program that owns this account
pub owner: &'a Pubkey,
/// This account's data contains a loaded program (and is now read-only)
pub executable: bool,
/// The epoch at which this account will next owe rent
pub rent_epoch: Epoch,
/// Was the transaction signed by this account's public key?
pub is_signer: bool,
/// Is the account writable?
pub is_writable: bool,
/// This account's data contains a loaded program (and is now read-only)
pub executable: bool,
}

impl<'a> fmt::Debug for AccountInfo<'a> {
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;
97 changes: 97 additions & 0 deletions sdk/program/src/stable_layout/stable_instruction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//! `Instruction`, with a stable memory layout

use {
crate::{
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
stable_layout::stable_vec::StableVec,
},
std::fmt::Debug,
};

/// `Instruction`, with a stable memory layout
///
/// This is used within the runtime to ensure memory mapping and memory accesses are valid. We
/// rely on known addresses and offsets within the runtime, and since `Instruction`'s layout is
/// allowed to change, we must provide a way to lock down the memory layout. `StableInstruction`
/// reimplements the bare minimum of `Instruction`'s API sufficient only for the runtime's needs.
///
/// # Examples
///
/// Creating a `StableInstruction` from an `Instruction`
///
/// ```
/// # use solana_program::{instruction::Instruction, pubkey::Pubkey, stable_layout::stable_instruction::StableInstruction};
/// # let program_id = Pubkey::default();
/// # let accounts = Vec::default();
/// # let data = Vec::default();
/// let instruction = Instruction { program_id, accounts, data };
/// let instruction = StableInstruction::from(instruction);
/// ```
#[derive(Debug, PartialEq)]
#[repr(C)]
pub struct StableInstruction {
pub accounts: StableVec<AccountMeta>,
pub data: StableVec<u8>,
pub program_id: Pubkey,
}

impl From<Instruction> for StableInstruction {
fn from(other: Instruction) -> Self {
Self {
accounts: other.accounts.into(),
data: other.data.into(),
program_id: other.program_id,
}
}
}

#[cfg(test)]
mod tests {
use {
super::*,
memoffset::offset_of,
std::mem::{align_of, size_of},
};

#[allow(clippy::integer_arithmetic)]
#[test]
fn test_memory_layout() {
assert_eq!(offset_of!(StableInstruction, accounts), 0);
assert_eq!(offset_of!(StableInstruction, data), 24);
assert_eq!(offset_of!(StableInstruction, program_id), 48);
assert_eq!(align_of::<StableInstruction>(), 8);
assert_eq!(size_of::<StableInstruction>(), 24 + 24 + 32);

let program_id = Pubkey::new_unique();
let account_meta1 = AccountMeta {
pubkey: Pubkey::new_unique(),
is_signer: true,
is_writable: false,
};
let account_meta2 = AccountMeta {
pubkey: Pubkey::new_unique(),
is_signer: false,
is_writable: true,
};
let accounts = vec![account_meta1, account_meta2];
let data = vec![1, 2, 3, 4, 5];
let instruction = Instruction {
program_id,
accounts: accounts.clone(),
data: data.clone(),
};
let instruction = StableInstruction::from(instruction);

let instruction_addr = &instruction as *const _ as u64;

let accounts_ptr = instruction_addr as *const StableVec<AccountMeta>;
assert_eq!(unsafe { &*accounts_ptr }, &accounts);

let data_ptr = (instruction_addr + 24) as *const StableVec<u8>;
assert_eq!(unsafe { &*data_ptr }, &data);

let pubkey_ptr = (instruction_addr + 48) as *const Pubkey;
assert_eq!(unsafe { *pubkey_ptr }, program_id);
}
}
29 changes: 29 additions & 0 deletions sdk/program/src/stable_layout/stable_rc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! Ensure Rc has a stable memory layout

#[cfg(test)]
mod tests {
use std::{
mem::{align_of, size_of},
rc::Rc,
};

#[test]
fn test_memory_layout() {
assert_eq!(align_of::<Rc<i32>>(), 8);
assert_eq!(size_of::<Rc<i32>>(), 8);

let value = 42;
let rc = Rc::new(value);
let _rc2 = Rc::clone(&rc); // used to increment strong count

let addr_rc = &rc as *const _ as usize;
let addr_ptr = addr_rc;
let addr_rcbox = unsafe { *(addr_ptr as *const *const i32) } as usize;
let addr_strong = addr_rcbox;
let addr_weak = addr_rcbox + 8;
let addr_value = addr_rcbox + 16;
assert_eq!(unsafe { *(addr_strong as *const usize) }, 2);
assert_eq!(unsafe { *(addr_weak as *const usize) }, 1);
assert_eq!(unsafe { *(addr_value as *const i32) }, 42);
}
}
25 changes: 25 additions & 0 deletions sdk/program/src/stable_layout/stable_ref_cell.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//! Ensure RefCell has a stable layout

#[cfg(test)]
mod tests {
use std::{
cell::RefCell,
mem::{align_of, size_of},
};

#[test]
fn test_memory_layout() {
assert_eq!(align_of::<RefCell<i32>>(), 8);
assert_eq!(size_of::<RefCell<i32>>(), 8 + 4 + /* padding */4);

let value = 42;
let refcell = RefCell::new(value);
let _borrow = refcell.borrow(); // used to increment borrow count

let addr_refcell = &refcell as *const _ as usize;
let addr_borrow = addr_refcell;
let addr_value = addr_refcell + 8;
assert_eq!(unsafe { *(addr_borrow as *const isize) }, 1);
assert_eq!(unsafe { *(addr_value as *const i32) }, 42);
}
}
27 changes: 27 additions & 0 deletions sdk/program/src/stable_layout/stable_slice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! Ensure slice has a stable memory layout

#[cfg(test)]
mod tests {
use std::mem::{align_of, size_of};

#[test]
fn test_memory_layout() {
assert_eq!(align_of::<&[i32]>(), 8);
assert_eq!(size_of::<&[i32]>(), /*ptr*/ 8 + /*len*/8);

let array = [11, 22, 33, 44, 55];
let slice = array.as_slice();

let addr_slice = &slice as *const _ as usize;
let addr_ptr = addr_slice;
let addr_len = addr_slice + 8;
assert_eq!(unsafe { *(addr_len as *const usize) }, 5);

let ptr_data = addr_ptr as *const *const i32;
assert_eq!(unsafe { *((*ptr_data).offset(0)) }, 11);
assert_eq!(unsafe { *((*ptr_data).offset(1)) }, 22);
assert_eq!(unsafe { *((*ptr_data).offset(2)) }, 33);
assert_eq!(unsafe { *((*ptr_data).offset(3)) }, 44);
assert_eq!(unsafe { *((*ptr_data).offset(4)) }, 55);
}
}
Loading