Arbitrary Cross-Program Invocation (CPI) in Solana
Cross-Program Invocation (CPI) is a fundamental feature of Solana that allows programs to call other programs while maintaining security and proper account isolation.
What is Arbitrary CPI?

Arbitrary CPI refers to the ability of a Solana program to invoke instructions on other programs where:
The target program isn't known at compile time
The instruction data and accounts are determined at runtime
This is more flexible than static CPI where the target program and instruction are known in advance.
How CPI Works in Solana
Program as a Signer: When Program A calls Program B, Program B sees Program A as the signer of the instruction (with
is_signertrue butis_writablefalse).Account Validation: All accounts passed to the inner instruction must be passed to the outer instruction first.
Privilege Escalation: CPI allows privilege escalation for:
Signer privileges (from parent instruction to child)
Writable privileges (from parent instruction to child)
Implementing Arbitrary CPI
Here's an example of how to implement arbitrary CPI in a Solana program:
use solana_program::{
account_info::{next_account_info, AccountInfo},
entrypoint::ProgramResult,
instruction::{AccountMeta, Instruction},
program::invoke,
pubkey::Pubkey,
};
pub fn process_arbitrary_cpi(
program_id: &Pubkey,
accounts: &[AccountInfo],
target_program_id: Pubkey,
instruction_data: Vec<u8>,
) -> ProgramResult {
let account_info_iter = &mut accounts.iter();
// Get all necessary accounts
let payer = next_account_info(account_info_iter)?;
let some_account = next_account_info(account_info_iter)?;
// ... other accounts needed for the CPI
// Prepare the instruction to the target program
let instruction = Instruction {
program_id: target_program_id,
accounts: vec![
AccountMeta::new(*payer.key, true),
AccountMeta::new(*some_account.key, false),
// ... other accounts for the target instruction
],
data: instruction_data,
};
// Perform the CPI
invoke(
&instruction,
&[
payer.clone(),
some_account.clone(),
// ... other accounts
],
)?;
Ok(())
}Security Considerations
Validation: Always validate the target program ID and instruction data to prevent malicious invocations.
Reentrancy: Solana programs are generally not reentrant, but be careful with indirect reentrancy through multiple CPI calls.
Account Validation: Ensure all accounts passed to the CPI are properly validated.
Program Derived Addresses (PDAs): When using PDAs in CPI, ensure they're properly derived and signed.
Common Use Cases
Program Composition: Building complex functionality by combining multiple programs.
Proxy Programs: Creating programs that route instructions to other programs based on runtime conditions.
Upgradable Systems: Allowing programs to call other programs that might be upgraded later.
Plugin Architectures: Supporting plugins where the main program calls plugin programs that aren't known at compile time.
Arbitrary CPI is a powerful feature but should be used carefully with proper validation to maintain security in your Solana applications.
