2019-03-03 09:12:36 -08:00
|
|
|
use crate::native_loader;
|
2019-04-02 07:36:10 -07:00
|
|
|
use crate::system_instruction_processor;
|
2019-05-30 21:31:35 -07:00
|
|
|
use serde::{Deserialize, Serialize};
|
2019-11-05 08:38:35 -08:00
|
|
|
use solana_sdk::account::{create_keyed_readonly_accounts, Account, KeyedAccount};
|
2019-11-05 10:57:32 -08:00
|
|
|
use solana_sdk::clock::Epoch;
|
2019-04-02 15:02:57 -07:00
|
|
|
use solana_sdk::instruction::{CompiledInstruction, InstructionError};
|
2019-04-23 17:25:03 -07:00
|
|
|
use solana_sdk::instruction_processor_utils;
|
2019-10-25 16:22:41 -07:00
|
|
|
use solana_sdk::loader_instruction::LoaderInstruction;
|
2019-03-29 09:05:06 -07:00
|
|
|
use solana_sdk::message::Message;
|
2019-02-18 22:26:22 -08:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
|
|
|
use solana_sdk::system_program;
|
2019-03-29 09:05:06 -07:00
|
|
|
use solana_sdk::transaction::TransactionError;
|
2019-04-23 17:25:03 -07:00
|
|
|
use std::collections::HashMap;
|
2019-10-25 16:22:41 -07:00
|
|
|
use std::io::Write;
|
2019-04-23 17:25:03 -07:00
|
|
|
use std::sync::RwLock;
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
use libloading::os::unix::*;
|
|
|
|
#[cfg(windows)]
|
|
|
|
use libloading::os::windows::*;
|
2019-03-13 10:55:43 -07:00
|
|
|
|
2019-02-18 22:26:22 -08:00
|
|
|
/// Return true if the slice has any duplicate elements
|
|
|
|
pub fn has_duplicates<T: PartialEq>(xs: &[T]) -> bool {
|
|
|
|
// Note: This is an O(n^2) algorithm, but requires no heap allocations. The benchmark
|
2019-04-02 08:35:38 -07:00
|
|
|
// `bench_has_duplicates` in benches/message_processor.rs shows that this implementation is
|
2019-02-18 22:26:22 -08:00
|
|
|
// ~50 times faster than using HashSet for very short slices.
|
|
|
|
for i in 1..xs.len() {
|
|
|
|
if xs[i..].contains(&xs[i - 1]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get mut references to a subset of elements.
|
2019-03-19 17:52:02 -07:00
|
|
|
fn get_subset_unchecked_mut<'a, T>(
|
2019-03-14 14:32:12 -07:00
|
|
|
xs: &'a mut [T],
|
|
|
|
indexes: &[u8],
|
|
|
|
) -> Result<Vec<&'a mut T>, InstructionError> {
|
2019-02-18 22:26:22 -08:00
|
|
|
// Since the compiler doesn't know the indexes are unique, dereferencing
|
|
|
|
// multiple mut elements is assumed to be unsafe. If, however, all
|
|
|
|
// indexes are unique, it's perfectly safe. The returned elements will share
|
|
|
|
// the liftime of the input slice.
|
|
|
|
|
2019-03-14 14:32:12 -07:00
|
|
|
// Make certain there are no duplicate indexes. If there are, return an error
|
|
|
|
// because we can't return multiple mut references to the same element.
|
2019-02-18 22:26:22 -08:00
|
|
|
if has_duplicates(indexes) {
|
2019-03-14 14:32:12 -07:00
|
|
|
return Err(InstructionError::DuplicateAccountIndex);
|
2019-02-18 22:26:22 -08:00
|
|
|
}
|
|
|
|
|
2019-03-14 14:32:12 -07:00
|
|
|
Ok(indexes
|
2019-02-18 22:26:22 -08:00
|
|
|
.iter()
|
|
|
|
.map(|i| {
|
|
|
|
let ptr = &mut xs[*i as usize] as *mut T;
|
|
|
|
unsafe { &mut *ptr }
|
|
|
|
})
|
2019-03-14 14:32:12 -07:00
|
|
|
.collect())
|
2019-02-18 22:26:22 -08:00
|
|
|
}
|
|
|
|
|
2019-11-05 10:57:32 -08:00
|
|
|
// The relevant state of an account before an Instruction executes, used
|
|
|
|
// to verify account integrity after the Instruction completes
|
|
|
|
pub struct PreInstructionAccount {
|
|
|
|
pub is_writable: bool,
|
|
|
|
pub lamports: u64,
|
|
|
|
pub data_len: usize,
|
|
|
|
pub data: Option<Vec<u8>>,
|
|
|
|
pub owner: Pubkey,
|
|
|
|
pub executable: bool,
|
|
|
|
pub rent_epoch: Epoch,
|
|
|
|
}
|
|
|
|
impl PreInstructionAccount {
|
|
|
|
pub fn new(account: &Account, is_writable: bool, copy_data: bool) -> Self {
|
|
|
|
Self {
|
|
|
|
is_writable,
|
|
|
|
lamports: account.lamports,
|
|
|
|
data_len: account.data.len(),
|
|
|
|
data: if copy_data {
|
|
|
|
Some(account.data.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
owner: account.owner,
|
|
|
|
executable: account.executable,
|
|
|
|
rent_epoch: account.rent_epoch,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn need_account_data_checked(program_id: &Pubkey, owner: &Pubkey, is_writable: bool) -> bool {
|
|
|
|
// For accounts not assigned to the program, the data may not change.
|
|
|
|
program_id != owner
|
|
|
|
// Read-only account data may not change.
|
|
|
|
|| !is_writable
|
|
|
|
}
|
2019-10-25 21:47:16 -07:00
|
|
|
pub fn verify_instruction(
|
2019-03-15 19:48:11 -07:00
|
|
|
program_id: &Pubkey,
|
2019-11-05 10:57:32 -08:00
|
|
|
pre: &PreInstructionAccount,
|
2019-08-26 11:04:20 -07:00
|
|
|
post: &Account,
|
2019-03-15 19:48:11 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
|
|
|
// Verify the transaction
|
|
|
|
|
2019-10-24 11:06:00 -07:00
|
|
|
// Only the owner of the account may change owner and
|
2019-11-05 08:38:35 -08:00
|
|
|
// only if the account is writable and
|
2019-10-24 11:06:00 -07:00
|
|
|
// only if the data is zero-initialized or empty
|
|
|
|
if pre.owner != post.owner
|
2019-11-05 10:57:32 -08:00
|
|
|
&& (!pre.is_writable // line coverage used to get branch coverage
|
2019-10-25 21:47:16 -07:00
|
|
|
|| *program_id != pre.owner // line coverage used to get branch coverage
|
2019-10-24 11:06:00 -07:00
|
|
|
|| !is_zeroed(&post.data))
|
|
|
|
{
|
2019-03-15 19:48:11 -07:00
|
|
|
return Err(InstructionError::ModifiedProgramId);
|
|
|
|
}
|
2019-10-24 11:06:00 -07:00
|
|
|
|
2019-10-19 18:23:27 -07:00
|
|
|
// An account not assigned to the program cannot have its balance decrease.
|
2019-10-25 21:47:16 -07:00
|
|
|
if *program_id != pre.owner // line coverage used to get branch coverage
|
2019-10-24 11:06:00 -07:00
|
|
|
&& pre.lamports > post.lamports
|
|
|
|
{
|
2019-03-15 19:48:11 -07:00
|
|
|
return Err(InstructionError::ExternalAccountLamportSpend);
|
|
|
|
}
|
2019-10-24 11:06:00 -07:00
|
|
|
|
2019-11-05 08:38:35 -08:00
|
|
|
// The balance of read-only accounts may not change.
|
2019-11-05 10:57:32 -08:00
|
|
|
if !pre.is_writable // line coverage used to get branch coverage
|
2019-11-05 08:38:35 -08:00
|
|
|
&& pre.lamports != post.lamports
|
2019-10-24 11:06:00 -07:00
|
|
|
{
|
2019-11-05 08:38:35 -08:00
|
|
|
return Err(InstructionError::ReadonlyLamportChange);
|
2019-06-10 19:50:02 -07:00
|
|
|
}
|
2019-10-24 11:06:00 -07:00
|
|
|
|
|
|
|
// Only the system program can change the size of the data
|
|
|
|
// and only if the system program owns the account
|
2019-11-05 10:57:32 -08:00
|
|
|
if pre.data_len != post.data.len()
|
2019-10-25 21:47:16 -07:00
|
|
|
&& (!system_program::check_id(program_id) // line coverage used to get branch coverage
|
2019-10-24 11:06:00 -07:00
|
|
|
|| !system_program::check_id(&pre.owner))
|
2019-03-15 19:48:11 -07:00
|
|
|
{
|
2019-10-24 11:06:00 -07:00
|
|
|
return Err(InstructionError::AccountDataSizeChanged);
|
2019-02-18 22:26:22 -08:00
|
|
|
}
|
2019-10-24 11:06:00 -07:00
|
|
|
|
2019-11-05 10:57:32 -08:00
|
|
|
if need_account_data_checked(&pre.owner, program_id, pre.is_writable) {
|
|
|
|
match &pre.data {
|
|
|
|
Some(data) if *data == post.data => (),
|
|
|
|
_ => {
|
|
|
|
if !pre.is_writable {
|
|
|
|
return Err(InstructionError::ReadonlyDataModified);
|
|
|
|
} else {
|
|
|
|
return Err(InstructionError::ExternalAccountDataModified);
|
|
|
|
}
|
2019-10-25 21:47:16 -07:00
|
|
|
}
|
2019-10-24 11:06:00 -07:00
|
|
|
}
|
2019-06-10 19:50:02 -07:00
|
|
|
}
|
2019-10-24 11:06:00 -07:00
|
|
|
|
2019-10-30 22:21:34 -07:00
|
|
|
// executable is one-way (false->true) and only the account owner may set it.
|
2019-08-26 11:04:20 -07:00
|
|
|
if pre.executable != post.executable
|
2019-11-05 10:57:32 -08:00
|
|
|
&& (!pre.is_writable // line coverage used to get branch coverage
|
2019-10-25 21:47:16 -07:00
|
|
|
|| pre.executable // line coverage used to get branch coverage
|
2019-10-24 11:06:00 -07:00
|
|
|
|| *program_id != pre.owner)
|
2019-07-29 15:29:20 -07:00
|
|
|
{
|
|
|
|
return Err(InstructionError::ExecutableModified);
|
|
|
|
}
|
2019-10-24 11:06:00 -07:00
|
|
|
|
2019-10-16 10:47:45 -07:00
|
|
|
// No one modifies rent_epoch (yet).
|
2019-08-26 11:04:20 -07:00
|
|
|
if pre.rent_epoch != post.rent_epoch {
|
|
|
|
return Err(InstructionError::RentEpochModified);
|
|
|
|
}
|
|
|
|
|
2019-02-18 22:26:22 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-10-25 16:22:41 -07:00
|
|
|
/// Return instruction data to pass to process_instruction().
|
|
|
|
/// When a loader is detected, the instruction data is wrapped with a LoaderInstruction
|
|
|
|
/// to signal to the loader that the instruction data should be used as arguments when
|
|
|
|
/// invoking a "main()" function.
|
|
|
|
fn get_loader_instruction_data<'a>(
|
|
|
|
loaders: &[(Pubkey, Account)],
|
|
|
|
ix_data: &'a [u8],
|
|
|
|
loader_ix_data: &'a mut Vec<u8>,
|
|
|
|
) -> &'a [u8] {
|
|
|
|
if loaders.len() > 1 {
|
|
|
|
let ix = LoaderInstruction::InvokeMain {
|
|
|
|
data: ix_data.to_vec(),
|
|
|
|
};
|
|
|
|
let ix_data = bincode::serialize(&ix).unwrap();
|
|
|
|
loader_ix_data.write_all(&ix_data).unwrap();
|
|
|
|
loader_ix_data
|
|
|
|
} else {
|
|
|
|
ix_data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 16:20:09 -07:00
|
|
|
pub type ProcessInstruction =
|
2019-05-31 15:29:21 -07:00
|
|
|
fn(&Pubkey, &mut [KeyedAccount], &[u8]) -> Result<(), InstructionError>;
|
2019-03-15 19:48:11 -07:00
|
|
|
|
2019-04-23 17:25:03 -07:00
|
|
|
pub type SymbolCache = RwLock<HashMap<Vec<u8>, Symbol<instruction_processor_utils::Entrypoint>>>;
|
|
|
|
|
2019-05-30 21:31:35 -07:00
|
|
|
#[derive(Serialize, Deserialize)]
|
2019-04-02 08:35:38 -07:00
|
|
|
pub struct MessageProcessor {
|
2019-05-30 21:31:35 -07:00
|
|
|
#[serde(skip)]
|
2019-03-16 16:20:09 -07:00
|
|
|
instruction_processors: Vec<(Pubkey, ProcessInstruction)>,
|
2019-05-30 21:31:35 -07:00
|
|
|
#[serde(skip)]
|
2019-04-23 17:25:03 -07:00
|
|
|
symbol_cache: SymbolCache,
|
2019-03-15 19:48:11 -07:00
|
|
|
}
|
|
|
|
|
2019-04-02 08:35:38 -07:00
|
|
|
impl Default for MessageProcessor {
|
2019-03-15 19:48:11 -07:00
|
|
|
fn default() -> Self {
|
2019-04-02 07:36:10 -07:00
|
|
|
let instruction_processors: Vec<(Pubkey, ProcessInstruction)> = vec![(
|
|
|
|
system_program::id(),
|
|
|
|
system_instruction_processor::process_instruction,
|
|
|
|
)];
|
2019-03-16 16:20:09 -07:00
|
|
|
|
|
|
|
Self {
|
|
|
|
instruction_processors,
|
2019-04-23 17:25:03 -07:00
|
|
|
symbol_cache: RwLock::new(HashMap::new()),
|
2019-03-16 16:20:09 -07:00
|
|
|
}
|
2019-03-15 19:48:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 08:35:38 -07:00
|
|
|
impl MessageProcessor {
|
2019-07-31 14:28:14 -07:00
|
|
|
/// Add a static entrypoint to intercept instructions before the dynamic loader.
|
2019-03-16 16:20:09 -07:00
|
|
|
pub fn add_instruction_processor(
|
|
|
|
&mut self,
|
|
|
|
program_id: Pubkey,
|
|
|
|
process_instruction: ProcessInstruction,
|
|
|
|
) {
|
|
|
|
self.instruction_processors
|
|
|
|
.push((program_id, process_instruction));
|
2019-03-07 12:42:01 -08:00
|
|
|
}
|
2019-03-15 19:48:11 -07:00
|
|
|
|
|
|
|
/// Process an instruction
|
|
|
|
/// This method calls the instruction's program entrypoint method
|
|
|
|
fn process_instruction(
|
|
|
|
&self,
|
2019-03-29 09:05:06 -07:00
|
|
|
message: &Message,
|
2019-04-02 15:02:57 -07:00
|
|
|
instruction: &CompiledInstruction,
|
2019-03-15 19:48:11 -07:00
|
|
|
executable_accounts: &mut [(Pubkey, Account)],
|
|
|
|
program_accounts: &mut [&mut Account],
|
2019-03-18 09:05:03 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
2019-05-22 15:23:16 -07:00
|
|
|
let program_id = instruction.program_id(&message.account_keys);
|
2019-10-25 16:22:41 -07:00
|
|
|
|
|
|
|
let mut loader_ix_data = vec![];
|
|
|
|
let ix_data = get_loader_instruction_data(
|
|
|
|
executable_accounts,
|
|
|
|
&instruction.data,
|
|
|
|
&mut loader_ix_data,
|
|
|
|
);
|
|
|
|
|
2019-11-05 08:38:35 -08:00
|
|
|
let mut keyed_accounts = create_keyed_readonly_accounts(executable_accounts);
|
2019-04-02 15:02:57 -07:00
|
|
|
let mut keyed_accounts2: Vec<_> = instruction
|
2019-03-07 09:35:28 -08:00
|
|
|
.accounts
|
|
|
|
.iter()
|
|
|
|
.map(|&index| {
|
|
|
|
let index = index as usize;
|
2019-03-29 09:05:06 -07:00
|
|
|
let key = &message.account_keys[index];
|
2019-11-05 08:38:35 -08:00
|
|
|
let is_writable = message.is_writable(index);
|
2019-06-27 14:25:10 -07:00
|
|
|
(
|
|
|
|
key,
|
|
|
|
index < message.header.num_required_signatures as usize,
|
2019-11-05 08:38:35 -08:00
|
|
|
is_writable,
|
2019-06-27 14:25:10 -07:00
|
|
|
)
|
2019-03-07 09:35:28 -08:00
|
|
|
})
|
2019-03-15 19:48:11 -07:00
|
|
|
.zip(program_accounts.iter_mut())
|
2019-11-05 08:38:35 -08:00
|
|
|
.map(|((key, is_signer, is_writable), account)| {
|
|
|
|
if is_writable {
|
2019-06-27 14:25:10 -07:00
|
|
|
KeyedAccount::new(key, is_signer, account)
|
|
|
|
} else {
|
2019-11-05 08:38:35 -08:00
|
|
|
KeyedAccount::new_readonly(key, is_signer, account)
|
2019-06-27 14:25:10 -07:00
|
|
|
}
|
|
|
|
})
|
2019-03-07 09:35:28 -08:00
|
|
|
.collect();
|
2019-03-15 19:48:11 -07:00
|
|
|
keyed_accounts.append(&mut keyed_accounts2);
|
|
|
|
|
2019-07-16 09:45:32 -07:00
|
|
|
assert!(
|
|
|
|
keyed_accounts[0].account.executable,
|
|
|
|
"loader not executable"
|
|
|
|
);
|
|
|
|
|
2019-09-24 04:09:53 -07:00
|
|
|
let loader_id = keyed_accounts[0].unsigned_key();
|
|
|
|
for (id, process_instruction) in &self.instruction_processors {
|
|
|
|
if id == loader_id {
|
2019-10-25 16:22:41 -07:00
|
|
|
return process_instruction(&program_id, &mut keyed_accounts[1..], &ix_data);
|
2019-09-24 04:09:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 09:45:32 -07:00
|
|
|
native_loader::invoke_entrypoint(
|
2019-03-15 19:48:11 -07:00
|
|
|
&program_id,
|
|
|
|
&mut keyed_accounts,
|
2019-10-25 16:22:41 -07:00
|
|
|
ix_data,
|
2019-04-23 17:25:03 -07:00
|
|
|
&self.symbol_cache,
|
2019-03-15 19:48:11 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-11-05 10:57:32 -08:00
|
|
|
fn sum_account_lamports(accounts: &mut [&mut Account]) -> u128 {
|
|
|
|
accounts.iter().map(|a| u128::from(a.lamports)).sum()
|
|
|
|
}
|
|
|
|
|
2019-03-15 19:48:11 -07:00
|
|
|
/// Execute an instruction
|
|
|
|
/// This method calls the instruction's program entrypoint method and verifies that the result of
|
|
|
|
/// the call does not violate the bank's accounting rules.
|
|
|
|
/// The accounts are committed back to the bank only if this function returns Ok(_).
|
|
|
|
fn execute_instruction(
|
|
|
|
&self,
|
2019-03-29 09:05:06 -07:00
|
|
|
message: &Message,
|
2019-04-02 15:02:57 -07:00
|
|
|
instruction: &CompiledInstruction,
|
2019-03-15 19:48:11 -07:00
|
|
|
executable_accounts: &mut [(Pubkey, Account)],
|
|
|
|
program_accounts: &mut [&mut Account],
|
|
|
|
) -> Result<(), InstructionError> {
|
2019-06-27 14:25:10 -07:00
|
|
|
assert_eq!(instruction.accounts.len(), program_accounts.len());
|
2019-11-05 10:57:32 -08:00
|
|
|
let program_id = instruction.program_id(&message.account_keys);
|
|
|
|
// Copy only what we need to verify after instruction processing
|
2019-08-26 11:04:20 -07:00
|
|
|
let pre_accounts: Vec<_> = program_accounts
|
2019-03-15 19:48:11 -07:00
|
|
|
.iter_mut()
|
2019-11-05 10:57:32 -08:00
|
|
|
.enumerate()
|
|
|
|
.map(|(i, account)| {
|
|
|
|
let is_writable = message.is_writable(instruction.accounts[i] as usize);
|
|
|
|
PreInstructionAccount::new(
|
|
|
|
account,
|
|
|
|
is_writable,
|
|
|
|
need_account_data_checked(&account.owner, program_id, is_writable),
|
|
|
|
)
|
|
|
|
})
|
2019-03-15 19:48:11 -07:00
|
|
|
.collect();
|
2019-11-05 10:57:32 -08:00
|
|
|
// Sum total lamports before instruction processing
|
|
|
|
let pre_total = Self::sum_account_lamports(program_accounts);
|
2019-03-15 19:48:11 -07:00
|
|
|
|
2019-05-31 15:29:21 -07:00
|
|
|
self.process_instruction(message, instruction, executable_accounts, program_accounts)?;
|
2019-11-05 10:57:32 -08:00
|
|
|
|
2019-03-15 19:48:11 -07:00
|
|
|
// Verify the instruction
|
2019-11-05 10:57:32 -08:00
|
|
|
for (pre_account, post_account) in pre_accounts.iter().zip(program_accounts.iter()) {
|
|
|
|
verify_instruction(&program_id, pre_account, post_account)?;
|
2019-03-15 19:48:11 -07:00
|
|
|
}
|
|
|
|
// The total sum of all the lamports in all the accounts cannot change.
|
2019-11-05 10:57:32 -08:00
|
|
|
let post_total = Self::sum_account_lamports(program_accounts);
|
2019-03-15 19:48:11 -07:00
|
|
|
if pre_total != post_total {
|
|
|
|
return Err(InstructionError::UnbalancedInstruction);
|
|
|
|
}
|
|
|
|
Ok(())
|
2019-03-07 09:35:28 -08:00
|
|
|
}
|
|
|
|
|
2019-03-29 09:05:06 -07:00
|
|
|
/// Process a message.
|
|
|
|
/// This method calls each instruction in the message over the set of loaded Accounts
|
2019-03-15 19:48:11 -07:00
|
|
|
/// The accounts are committed back to the bank only if every instruction succeeds
|
2019-03-29 09:05:06 -07:00
|
|
|
pub fn process_message(
|
2019-03-15 19:48:11 -07:00
|
|
|
&self,
|
2019-03-29 09:05:06 -07:00
|
|
|
message: &Message,
|
2019-03-15 19:48:11 -07:00
|
|
|
loaders: &mut [Vec<(Pubkey, Account)>],
|
2019-03-29 09:05:06 -07:00
|
|
|
accounts: &mut [Account],
|
2019-03-15 19:48:11 -07:00
|
|
|
) -> Result<(), TransactionError> {
|
2019-03-29 09:05:06 -07:00
|
|
|
for (instruction_index, instruction) in message.instructions.iter().enumerate() {
|
2019-05-23 15:19:53 -07:00
|
|
|
let executable_index = message
|
2019-07-01 17:34:22 -07:00
|
|
|
.program_position(instruction.program_id_index as usize)
|
2019-05-23 15:19:53 -07:00
|
|
|
.ok_or(TransactionError::InvalidAccountIndex)?;
|
|
|
|
let executable_accounts = &mut loaders[executable_index];
|
2019-03-29 09:05:06 -07:00
|
|
|
let mut program_accounts = get_subset_unchecked_mut(accounts, &instruction.accounts)
|
2019-03-15 19:48:11 -07:00
|
|
|
.map_err(|err| TransactionError::InstructionError(instruction_index as u8, err))?;
|
2019-05-23 15:19:53 -07:00
|
|
|
// TODO: `get_subset_unchecked_mut` panics on an index out of bounds if an executable
|
|
|
|
// account is also included as a regular account for an instruction, because the
|
|
|
|
// executable account is not passed in as part of the accounts slice
|
2019-03-15 19:48:11 -07:00
|
|
|
self.execute_instruction(
|
2019-03-29 09:05:06 -07:00
|
|
|
message,
|
2019-04-02 15:02:57 -07:00
|
|
|
instruction,
|
2019-03-15 19:48:11 -07:00
|
|
|
executable_accounts,
|
|
|
|
&mut program_accounts,
|
|
|
|
)
|
|
|
|
.map_err(|err| TransactionError::InstructionError(instruction_index as u8, err))?;
|
2019-03-11 15:35:25 -07:00
|
|
|
}
|
2019-03-15 19:48:11 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-03-11 15:35:25 -07:00
|
|
|
}
|
|
|
|
|
2019-10-24 11:06:00 -07:00
|
|
|
pub const ZEROS_LEN: usize = 1024;
|
|
|
|
static ZEROS: [u8; ZEROS_LEN] = [0; ZEROS_LEN];
|
|
|
|
pub fn is_zeroed(buf: &[u8]) -> bool {
|
|
|
|
let mut chunks = buf.chunks_exact(ZEROS_LEN);
|
|
|
|
|
|
|
|
chunks.all(|chunk| chunk == &ZEROS[..])
|
|
|
|
&& chunks.remainder() == &ZEROS[..chunks.remainder().len()]
|
|
|
|
}
|
|
|
|
|
2019-02-18 22:26:22 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-06-10 19:50:02 -07:00
|
|
|
use solana_sdk::instruction::{AccountMeta, Instruction, InstructionError};
|
|
|
|
use solana_sdk::message::Message;
|
2019-09-24 04:09:53 -07:00
|
|
|
use solana_sdk::native_loader::create_loadable_account;
|
2019-02-18 22:26:22 -08:00
|
|
|
|
2019-10-24 11:06:00 -07:00
|
|
|
#[test]
|
|
|
|
fn test_is_zeroed() {
|
|
|
|
let mut buf = [0; ZEROS_LEN];
|
|
|
|
assert_eq!(is_zeroed(&buf), true);
|
|
|
|
buf[0] = 1;
|
|
|
|
assert_eq!(is_zeroed(&buf), false);
|
|
|
|
|
|
|
|
let mut buf = [0; ZEROS_LEN - 1];
|
|
|
|
assert_eq!(is_zeroed(&buf), true);
|
|
|
|
buf[0] = 1;
|
|
|
|
assert_eq!(is_zeroed(&buf), false);
|
|
|
|
|
|
|
|
let mut buf = [0; ZEROS_LEN + 1];
|
|
|
|
assert_eq!(is_zeroed(&buf), true);
|
|
|
|
buf[0] = 1;
|
|
|
|
assert_eq!(is_zeroed(&buf), false);
|
|
|
|
|
|
|
|
let buf = vec![];
|
|
|
|
assert_eq!(is_zeroed(&buf), true);
|
|
|
|
}
|
|
|
|
|
2019-02-18 22:26:22 -08:00
|
|
|
#[test]
|
|
|
|
fn test_has_duplicates() {
|
|
|
|
assert!(!has_duplicates(&[1, 2]));
|
|
|
|
assert!(has_duplicates(&[1, 2, 1]));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-03-19 17:52:02 -07:00
|
|
|
fn test_get_subset_unchecked_mut() {
|
2019-02-18 22:26:22 -08:00
|
|
|
assert_eq!(
|
2019-03-19 17:52:02 -07:00
|
|
|
get_subset_unchecked_mut(&mut [7, 8], &[0]).unwrap(),
|
|
|
|
vec![&mut 7]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
get_subset_unchecked_mut(&mut [7, 8], &[0, 1]).unwrap(),
|
2019-02-18 22:26:22 -08:00
|
|
|
vec![&mut 7, &mut 8]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-03-19 17:52:02 -07:00
|
|
|
fn test_get_subset_unchecked_mut_duplicate_index() {
|
|
|
|
// This panics, because it assumes duplicate detection is done elsewhere.
|
2019-03-14 14:32:12 -07:00
|
|
|
assert_eq!(
|
2019-03-19 17:52:02 -07:00
|
|
|
get_subset_unchecked_mut(&mut [7, 8], &[0, 0]).unwrap_err(),
|
2019-03-14 14:32:12 -07:00
|
|
|
InstructionError::DuplicateAccountIndex
|
|
|
|
);
|
2019-02-18 22:26:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
2019-03-19 17:52:02 -07:00
|
|
|
fn test_get_subset_unchecked_mut_out_of_bounds() {
|
2019-02-18 22:26:22 -08:00
|
|
|
// This panics, because it assumes bounds validation is done elsewhere.
|
2019-03-19 17:52:02 -07:00
|
|
|
get_subset_unchecked_mut(&mut [7, 8], &[2]).unwrap();
|
2019-02-18 22:26:22 -08:00
|
|
|
}
|
2019-02-22 12:08:54 -08:00
|
|
|
|
|
|
|
#[test]
|
2019-10-24 11:06:00 -07:00
|
|
|
fn test_verify_instruction_change_owner() {
|
|
|
|
fn change_owner(
|
2019-03-13 10:46:49 -07:00
|
|
|
ix: &Pubkey,
|
|
|
|
pre: &Pubkey,
|
|
|
|
post: &Pubkey,
|
2019-11-05 08:38:35 -08:00
|
|
|
is_writable: bool,
|
2019-03-13 10:46:49 -07:00
|
|
|
) -> Result<(), InstructionError> {
|
2019-07-29 15:29:20 -07:00
|
|
|
verify_instruction(
|
|
|
|
&ix,
|
2019-11-05 10:57:32 -08:00
|
|
|
&PreInstructionAccount::new(
|
|
|
|
&Account::new(0, 0, pre),
|
|
|
|
is_writable,
|
|
|
|
need_account_data_checked(pre, ix, is_writable),
|
|
|
|
),
|
2019-07-29 15:29:20 -07:00
|
|
|
&Account::new(0, 0, post),
|
|
|
|
)
|
2019-02-22 12:08:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
let system_program_id = system_program::id();
|
2019-03-30 20:37:33 -07:00
|
|
|
let alice_program_id = Pubkey::new_rand();
|
|
|
|
let mallory_program_id = Pubkey::new_rand();
|
2019-02-22 12:08:54 -08:00
|
|
|
|
|
|
|
assert_eq!(
|
2019-10-24 11:06:00 -07:00
|
|
|
change_owner(
|
2019-07-29 15:29:20 -07:00
|
|
|
&system_program_id,
|
|
|
|
&system_program_id,
|
|
|
|
&alice_program_id,
|
|
|
|
true
|
|
|
|
),
|
2019-02-22 12:08:54 -08:00
|
|
|
Ok(()),
|
|
|
|
"system program should be able to change the account owner"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2019-11-05 08:38:35 -08:00
|
|
|
change_owner(
|
|
|
|
&system_program_id,
|
|
|
|
&system_program_id,
|
|
|
|
&alice_program_id,
|
|
|
|
false
|
|
|
|
),
|
2019-07-29 15:29:20 -07:00
|
|
|
Err(InstructionError::ModifiedProgramId),
|
2019-11-05 08:38:35 -08:00
|
|
|
"system program should not be able to change the account owner of a read-only account"
|
2019-07-29 15:29:20 -07:00
|
|
|
);
|
2019-10-24 11:06:00 -07:00
|
|
|
assert_eq!(
|
|
|
|
change_owner(
|
|
|
|
&system_program_id,
|
|
|
|
&mallory_program_id,
|
|
|
|
&alice_program_id,
|
|
|
|
true
|
|
|
|
),
|
|
|
|
Err(InstructionError::ModifiedProgramId),
|
|
|
|
"system program should not be able to change the account owner of a non-system account"
|
|
|
|
);
|
2019-07-29 15:29:20 -07:00
|
|
|
|
|
|
|
assert_eq!(
|
2019-10-24 11:06:00 -07:00
|
|
|
change_owner(
|
|
|
|
&mallory_program_id,
|
2019-07-29 15:29:20 -07:00
|
|
|
&mallory_program_id,
|
|
|
|
&alice_program_id,
|
|
|
|
true
|
|
|
|
),
|
2019-10-24 11:06:00 -07:00
|
|
|
Ok(()),
|
|
|
|
"mallory should be able to change the account owner, if she leaves clear data"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
verify_instruction(
|
|
|
|
&mallory_program_id,
|
2019-11-05 10:57:32 -08:00
|
|
|
&PreInstructionAccount::new(
|
|
|
|
&Account::new_data(0, &[42], &mallory_program_id).unwrap(),
|
|
|
|
true,
|
|
|
|
need_account_data_checked(&mallory_program_id, &mallory_program_id, true),
|
|
|
|
),
|
2019-10-24 11:06:00 -07:00
|
|
|
&Account::new_data(0, &[0], &alice_program_id,).unwrap(),
|
|
|
|
),
|
|
|
|
Ok(()),
|
|
|
|
"mallory should be able to change the account owner, if she leaves clear data"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
verify_instruction(
|
|
|
|
&mallory_program_id,
|
2019-11-05 10:57:32 -08:00
|
|
|
&PreInstructionAccount::new(
|
|
|
|
&Account::new_data(0, &[42], &mallory_program_id).unwrap(),
|
|
|
|
true,
|
|
|
|
need_account_data_checked(&mallory_program_id, &mallory_program_id, true),
|
|
|
|
),
|
2019-10-24 11:06:00 -07:00
|
|
|
&Account::new_data(0, &[42], &alice_program_id,).unwrap(),
|
|
|
|
),
|
2019-03-13 10:46:49 -07:00
|
|
|
Err(InstructionError::ModifiedProgramId),
|
2019-10-24 11:06:00 -07:00
|
|
|
"mallory should not be able to inject data into the alice program"
|
2019-02-22 12:08:54 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-07-29 15:29:20 -07:00
|
|
|
fn test_verify_instruction_change_executable() {
|
2019-10-24 11:06:00 -07:00
|
|
|
let owner = Pubkey::new_rand();
|
2019-07-29 15:29:20 -07:00
|
|
|
let change_executable = |program_id: &Pubkey,
|
2019-11-05 08:38:35 -08:00
|
|
|
is_writable: bool,
|
2019-07-29 15:29:20 -07:00
|
|
|
pre_executable: bool,
|
|
|
|
post_executable: bool|
|
|
|
|
-> Result<(), InstructionError> {
|
2019-11-05 10:57:32 -08:00
|
|
|
let pre = PreInstructionAccount::new(
|
|
|
|
&Account {
|
|
|
|
owner,
|
|
|
|
executable: pre_executable,
|
|
|
|
..Account::default()
|
|
|
|
},
|
|
|
|
is_writable,
|
|
|
|
need_account_data_checked(&owner, &program_id, is_writable),
|
|
|
|
);
|
2019-08-26 11:04:20 -07:00
|
|
|
|
|
|
|
let post = Account {
|
2019-10-24 11:06:00 -07:00
|
|
|
owner,
|
2019-08-26 11:04:20 -07:00
|
|
|
executable: post_executable,
|
|
|
|
..Account::default()
|
|
|
|
};
|
2019-11-05 10:57:32 -08:00
|
|
|
verify_instruction(&program_id, &pre, &post)
|
2019-07-29 15:29:20 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
let mallory_program_id = Pubkey::new_rand();
|
|
|
|
let system_program_id = system_program::id();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
change_executable(&system_program_id, true, false, true),
|
2019-10-24 11:06:00 -07:00
|
|
|
Err(InstructionError::ExecutableModified),
|
|
|
|
"system program can't change executable if system doesn't own the account"
|
2019-07-29 15:29:20 -07:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2019-10-24 11:06:00 -07:00
|
|
|
change_executable(&owner, true, false, true),
|
2019-07-29 15:29:20 -07:00
|
|
|
Ok(()),
|
|
|
|
"alice program should be able to change executable"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2019-10-24 11:06:00 -07:00
|
|
|
change_executable(&owner, false, false, true),
|
2019-07-29 15:29:20 -07:00
|
|
|
Err(InstructionError::ExecutableModified),
|
2019-11-05 08:38:35 -08:00
|
|
|
"system program can't modify executable of read-only accounts"
|
2019-07-29 15:29:20 -07:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2019-10-24 11:06:00 -07:00
|
|
|
change_executable(&owner, true, true, false),
|
2019-07-29 15:29:20 -07:00
|
|
|
Err(InstructionError::ExecutableModified),
|
|
|
|
"system program can't reverse executable"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
change_executable(&mallory_program_id, true, false, true),
|
|
|
|
Err(InstructionError::ExecutableModified),
|
|
|
|
"malicious Mallory should not be able to change the account executable"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-24 11:06:00 -07:00
|
|
|
#[test]
|
|
|
|
fn test_verify_instruction_change_data_len() {
|
|
|
|
assert_eq!(
|
|
|
|
verify_instruction(
|
|
|
|
&system_program::id(),
|
2019-11-05 10:57:32 -08:00
|
|
|
&PreInstructionAccount::new(
|
|
|
|
&Account::new_data(0, &[0], &system_program::id()).unwrap(),
|
|
|
|
true,
|
|
|
|
need_account_data_checked(&system_program::id(), &system_program::id(), true),
|
|
|
|
),
|
2019-10-24 11:06:00 -07:00
|
|
|
&Account::new_data(0, &[0, 0], &system_program::id()).unwrap(),
|
|
|
|
),
|
|
|
|
Ok(()),
|
|
|
|
"system program should be able to change the data len"
|
|
|
|
);
|
|
|
|
let alice_program_id = Pubkey::new_rand();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
verify_instruction(
|
|
|
|
&system_program::id(),
|
2019-11-05 10:57:32 -08:00
|
|
|
&PreInstructionAccount::new(
|
|
|
|
&Account::new_data(0, &[0], &alice_program_id).unwrap(),
|
|
|
|
true,
|
|
|
|
need_account_data_checked(&alice_program_id, &system_program::id(), true),
|
|
|
|
),
|
2019-10-24 11:06:00 -07:00
|
|
|
&Account::new_data(0, &[0, 0], &alice_program_id).unwrap(),
|
|
|
|
),
|
|
|
|
Err(InstructionError::AccountDataSizeChanged),
|
|
|
|
"system program should not be able to change the data length of accounts it does not own"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:29:20 -07:00
|
|
|
#[test]
|
|
|
|
fn test_verify_instruction_change_data() {
|
|
|
|
let alice_program_id = Pubkey::new_rand();
|
|
|
|
|
|
|
|
let change_data =
|
2019-11-05 08:38:35 -08:00
|
|
|
|program_id: &Pubkey, is_writable: bool| -> Result<(), InstructionError> {
|
2019-11-05 10:57:32 -08:00
|
|
|
let pre = PreInstructionAccount::new(
|
|
|
|
&Account::new_data(0, &[0], &alice_program_id).unwrap(),
|
|
|
|
is_writable,
|
|
|
|
need_account_data_checked(&alice_program_id, &program_id, is_writable),
|
|
|
|
);
|
2019-08-26 11:04:20 -07:00
|
|
|
let post = Account::new_data(0, &[42], &alice_program_id).unwrap();
|
2019-11-05 10:57:32 -08:00
|
|
|
verify_instruction(&program_id, &pre, &post)
|
2019-07-29 15:29:20 -07:00
|
|
|
};
|
2019-02-22 12:08:54 -08:00
|
|
|
|
2019-03-30 20:37:33 -07:00
|
|
|
let mallory_program_id = Pubkey::new_rand();
|
2019-02-22 12:08:54 -08:00
|
|
|
|
2019-07-29 15:29:20 -07:00
|
|
|
assert_eq!(
|
|
|
|
change_data(&alice_program_id, true),
|
|
|
|
Ok(()),
|
|
|
|
"alice program should be able to change the data"
|
|
|
|
);
|
2019-02-22 12:08:54 -08:00
|
|
|
assert_eq!(
|
2019-06-10 19:50:02 -07:00
|
|
|
change_data(&mallory_program_id, true),
|
2019-03-14 09:48:27 -07:00
|
|
|
Err(InstructionError::ExternalAccountDataModified),
|
2019-10-25 21:47:16 -07:00
|
|
|
"non-owner mallory should not be able to change the account data"
|
2019-02-22 12:08:54 -08:00
|
|
|
);
|
2019-06-10 19:50:02 -07:00
|
|
|
|
|
|
|
assert_eq!(
|
2019-10-25 21:47:16 -07:00
|
|
|
change_data(&alice_program_id, false),
|
2019-11-05 08:38:35 -08:00
|
|
|
Err(InstructionError::ReadonlyDataModified),
|
2019-10-25 21:47:16 -07:00
|
|
|
"alice isn't allowed to touch a CO account"
|
2019-06-10 19:50:02 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-26 11:04:20 -07:00
|
|
|
#[test]
|
|
|
|
fn test_verify_instruction_rent_epoch() {
|
|
|
|
let alice_program_id = Pubkey::new_rand();
|
2019-11-05 10:57:32 -08:00
|
|
|
let pre = PreInstructionAccount::new(
|
|
|
|
&Account::new(0, 0, &alice_program_id),
|
|
|
|
false,
|
|
|
|
need_account_data_checked(&alice_program_id, &system_program::id(), false),
|
|
|
|
);
|
2019-08-26 11:04:20 -07:00
|
|
|
let mut post = Account::new(0, 0, &alice_program_id);
|
|
|
|
|
|
|
|
assert_eq!(
|
2019-11-05 10:57:32 -08:00
|
|
|
verify_instruction(&system_program::id(), &pre, &post),
|
2019-08-26 11:04:20 -07:00
|
|
|
Ok(()),
|
|
|
|
"nothing changed!"
|
|
|
|
);
|
|
|
|
|
|
|
|
post.rent_epoch += 1;
|
|
|
|
assert_eq!(
|
2019-11-05 10:57:32 -08:00
|
|
|
verify_instruction(&system_program::id(), &pre, &post),
|
2019-08-26 11:04:20 -07:00
|
|
|
Err(InstructionError::RentEpochModified),
|
|
|
|
"no one touches rent_epoch"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-10 19:50:02 -07:00
|
|
|
#[test]
|
2019-10-24 11:06:00 -07:00
|
|
|
fn test_verify_instruction_deduct_lamports_and_reassign_account() {
|
|
|
|
let alice_program_id = Pubkey::new_rand();
|
|
|
|
let bob_program_id = Pubkey::new_rand();
|
2019-11-05 10:57:32 -08:00
|
|
|
let pre = PreInstructionAccount::new(
|
|
|
|
&Account::new_data(42, &[42], &alice_program_id).unwrap(),
|
|
|
|
true,
|
|
|
|
need_account_data_checked(&alice_program_id, &alice_program_id, true),
|
|
|
|
);
|
2019-10-24 11:06:00 -07:00
|
|
|
let post = Account::new_data(1, &[0], &bob_program_id).unwrap();
|
|
|
|
|
|
|
|
// positive test of this capability
|
|
|
|
assert_eq!(
|
2019-11-05 10:57:32 -08:00
|
|
|
verify_instruction(&alice_program_id, &pre, &post),
|
2019-10-24 11:06:00 -07:00
|
|
|
Ok(()),
|
2019-10-25 21:47:16 -07:00
|
|
|
"alice should be able to deduct lamports and give the account to bob if the data is zeroed",
|
2019-10-24 11:06:00 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_verify_instruction_change_lamports() {
|
2019-06-10 19:50:02 -07:00
|
|
|
let alice_program_id = Pubkey::new_rand();
|
2019-11-05 10:57:32 -08:00
|
|
|
let pre = PreInstructionAccount::new(
|
|
|
|
&Account::new(42, 0, &alice_program_id),
|
|
|
|
false,
|
|
|
|
need_account_data_checked(&alice_program_id, &system_program::id(), false),
|
|
|
|
);
|
2019-08-26 11:04:20 -07:00
|
|
|
let post = Account::new(0, 0, &alice_program_id);
|
2019-10-24 11:06:00 -07:00
|
|
|
|
2019-06-10 19:50:02 -07:00
|
|
|
assert_eq!(
|
2019-11-05 10:57:32 -08:00
|
|
|
verify_instruction(&system_program::id(), &pre, &post),
|
2019-06-10 19:50:02 -07:00
|
|
|
Err(InstructionError::ExternalAccountLamportSpend),
|
|
|
|
"debit should fail, even if system program"
|
|
|
|
);
|
2019-11-05 10:57:32 -08:00
|
|
|
|
|
|
|
let pre = PreInstructionAccount::new(
|
|
|
|
&Account::new(42, 0, &alice_program_id),
|
|
|
|
false,
|
|
|
|
need_account_data_checked(&alice_program_id, &alice_program_id, false),
|
|
|
|
);
|
|
|
|
|
2019-06-10 19:50:02 -07:00
|
|
|
assert_eq!(
|
2019-11-05 10:57:32 -08:00
|
|
|
verify_instruction(&alice_program_id, &pre, &post,),
|
2019-11-05 08:38:35 -08:00
|
|
|
Err(InstructionError::ReadonlyLamportChange),
|
2019-06-10 19:50:02 -07:00
|
|
|
"debit should fail, even if owning program"
|
|
|
|
);
|
2019-10-24 11:06:00 -07:00
|
|
|
|
2019-11-05 10:57:32 -08:00
|
|
|
let pre = PreInstructionAccount::new(
|
|
|
|
&Account::new(42, 0, &alice_program_id),
|
|
|
|
true,
|
|
|
|
need_account_data_checked(&alice_program_id, &system_program::id(), true),
|
|
|
|
);
|
2019-10-24 11:06:00 -07:00
|
|
|
let post = Account::new(0, 0, &system_program::id());
|
|
|
|
assert_eq!(
|
2019-11-05 10:57:32 -08:00
|
|
|
verify_instruction(&system_program::id(), &pre, &post),
|
2019-10-24 11:06:00 -07:00
|
|
|
Err(InstructionError::ModifiedProgramId),
|
|
|
|
"system program can't debit the account unless it was the pre.owner"
|
|
|
|
);
|
|
|
|
|
2019-11-05 10:57:32 -08:00
|
|
|
let pre = PreInstructionAccount::new(
|
|
|
|
&Account::new(42, 0, &system_program::id()),
|
|
|
|
true,
|
|
|
|
need_account_data_checked(&system_program::id(), &system_program::id(), true),
|
|
|
|
);
|
2019-10-24 11:06:00 -07:00
|
|
|
let post = Account::new(0, 0, &alice_program_id);
|
|
|
|
assert_eq!(
|
2019-11-05 10:57:32 -08:00
|
|
|
verify_instruction(&system_program::id(), &pre, &post),
|
2019-10-24 11:06:00 -07:00
|
|
|
Ok(()),
|
|
|
|
"system can spend (and change owner)"
|
|
|
|
);
|
2019-06-10 19:50:02 -07:00
|
|
|
}
|
|
|
|
|
2019-10-16 10:47:45 -07:00
|
|
|
#[test]
|
|
|
|
fn test_verify_instruction_data_size_changed() {
|
|
|
|
let alice_program_id = Pubkey::new_rand();
|
2019-11-05 10:57:32 -08:00
|
|
|
let pre = PreInstructionAccount::new(
|
|
|
|
&Account::new_data(42, &[0], &alice_program_id).unwrap(),
|
|
|
|
true,
|
|
|
|
need_account_data_checked(&alice_program_id, &system_program::id(), true),
|
|
|
|
);
|
2019-10-24 11:06:00 -07:00
|
|
|
let post = Account::new_data(42, &[0, 0], &alice_program_id).unwrap();
|
2019-10-16 10:47:45 -07:00
|
|
|
assert_eq!(
|
2019-11-05 10:57:32 -08:00
|
|
|
verify_instruction(&system_program::id(), &pre, &post),
|
2019-10-24 11:06:00 -07:00
|
|
|
Err(InstructionError::AccountDataSizeChanged),
|
|
|
|
"system program should not be able to change another program's account data size"
|
2019-10-16 10:47:45 -07:00
|
|
|
);
|
2019-11-05 10:57:32 -08:00
|
|
|
let pre = PreInstructionAccount::new(
|
|
|
|
&Account::new_data(42, &[0], &alice_program_id).unwrap(),
|
|
|
|
true,
|
|
|
|
need_account_data_checked(&alice_program_id, &alice_program_id, true),
|
|
|
|
);
|
2019-10-16 10:47:45 -07:00
|
|
|
assert_eq!(
|
2019-11-05 10:57:32 -08:00
|
|
|
verify_instruction(&alice_program_id, &pre, &post),
|
2019-10-16 10:47:45 -07:00
|
|
|
Err(InstructionError::AccountDataSizeChanged),
|
|
|
|
"non-system programs cannot change their data size"
|
|
|
|
);
|
2019-11-05 10:57:32 -08:00
|
|
|
let pre = PreInstructionAccount::new(
|
|
|
|
&Account::new_data(42, &[0], &system_program::id()).unwrap(),
|
|
|
|
true,
|
|
|
|
need_account_data_checked(&system_program::id(), &system_program::id(), true),
|
|
|
|
);
|
2019-10-24 11:06:00 -07:00
|
|
|
assert_eq!(
|
2019-11-05 10:57:32 -08:00
|
|
|
verify_instruction(&system_program::id(), &pre, &post),
|
2019-10-24 11:06:00 -07:00
|
|
|
Ok(()),
|
|
|
|
"system program should be able to change acount data size"
|
|
|
|
);
|
2019-10-16 10:47:45 -07:00
|
|
|
}
|
|
|
|
|
2019-06-10 19:50:02 -07:00
|
|
|
#[test]
|
2019-11-05 08:38:35 -08:00
|
|
|
fn test_process_message_readonly_handling() {
|
2019-06-10 19:50:02 -07:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
enum MockSystemInstruction {
|
2019-11-05 08:38:35 -08:00
|
|
|
Correct,
|
|
|
|
AttemptCredit { lamports: u64 },
|
|
|
|
AttemptDataChange { data: u8 },
|
2019-06-10 19:50:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn mock_system_process_instruction(
|
|
|
|
_program_id: &Pubkey,
|
|
|
|
keyed_accounts: &mut [KeyedAccount],
|
|
|
|
data: &[u8],
|
|
|
|
) -> Result<(), InstructionError> {
|
|
|
|
if let Ok(instruction) = bincode::deserialize(data) {
|
|
|
|
match instruction {
|
2019-11-05 08:38:35 -08:00
|
|
|
MockSystemInstruction::Correct => Ok(()),
|
|
|
|
MockSystemInstruction::AttemptCredit { lamports } => {
|
2019-06-10 19:50:02 -07:00
|
|
|
keyed_accounts[0].account.lamports -= lamports;
|
|
|
|
keyed_accounts[1].account.lamports += lamports;
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-11-05 08:38:35 -08:00
|
|
|
// Change data in a read-only account
|
|
|
|
MockSystemInstruction::AttemptDataChange { data } => {
|
|
|
|
keyed_accounts[1].account.data = vec![data];
|
2019-06-10 19:50:02 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(InstructionError::InvalidInstructionData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mock_system_program_id = Pubkey::new(&[2u8; 32]);
|
|
|
|
let mut message_processor = MessageProcessor::default();
|
|
|
|
message_processor
|
|
|
|
.add_instruction_processor(mock_system_program_id, mock_system_process_instruction);
|
|
|
|
|
|
|
|
let mut accounts: Vec<Account> = Vec::new();
|
|
|
|
let account = Account::new(100, 1, &mock_system_program_id);
|
|
|
|
accounts.push(account);
|
|
|
|
let account = Account::new(0, 1, &mock_system_program_id);
|
|
|
|
accounts.push(account);
|
|
|
|
|
|
|
|
let mut loaders: Vec<Vec<(Pubkey, Account)>> = Vec::new();
|
|
|
|
let account = create_loadable_account("mock_system_program");
|
2019-09-24 04:09:53 -07:00
|
|
|
loaders.push(vec![(mock_system_program_id, account)]);
|
2019-06-10 19:50:02 -07:00
|
|
|
|
|
|
|
let from_pubkey = Pubkey::new_rand();
|
|
|
|
let to_pubkey = Pubkey::new_rand();
|
|
|
|
let account_metas = vec![
|
|
|
|
AccountMeta::new(from_pubkey, true),
|
2019-11-05 08:38:35 -08:00
|
|
|
AccountMeta::new_readonly(to_pubkey, false),
|
2019-06-10 19:50:02 -07:00
|
|
|
];
|
|
|
|
let message = Message::new(vec![Instruction::new(
|
|
|
|
mock_system_program_id,
|
2019-11-05 08:38:35 -08:00
|
|
|
&MockSystemInstruction::Correct,
|
2019-06-10 19:50:02 -07:00
|
|
|
account_metas.clone(),
|
|
|
|
)]);
|
|
|
|
|
2019-11-05 08:38:35 -08:00
|
|
|
let result = message_processor.process_message(&message, &mut loaders, &mut accounts);
|
2019-06-10 19:50:02 -07:00
|
|
|
assert_eq!(result, Ok(()));
|
2019-11-05 08:38:35 -08:00
|
|
|
assert_eq!(accounts[0].lamports, 100);
|
|
|
|
assert_eq!(accounts[1].lamports, 0);
|
2019-06-10 19:50:02 -07:00
|
|
|
|
|
|
|
let message = Message::new(vec![Instruction::new(
|
|
|
|
mock_system_program_id,
|
2019-11-05 08:38:35 -08:00
|
|
|
&MockSystemInstruction::AttemptCredit { lamports: 50 },
|
2019-06-10 19:50:02 -07:00
|
|
|
account_metas.clone(),
|
|
|
|
)]);
|
|
|
|
|
2019-11-05 08:38:35 -08:00
|
|
|
let result = message_processor.process_message(&message, &mut loaders, &mut accounts);
|
2019-06-10 19:50:02 -07:00
|
|
|
assert_eq!(
|
|
|
|
result,
|
|
|
|
Err(TransactionError::InstructionError(
|
|
|
|
0,
|
2019-11-05 08:38:35 -08:00
|
|
|
InstructionError::ReadonlyLamportChange
|
2019-06-10 19:50:02 -07:00
|
|
|
))
|
|
|
|
);
|
|
|
|
|
|
|
|
let message = Message::new(vec![Instruction::new(
|
|
|
|
mock_system_program_id,
|
2019-11-05 08:38:35 -08:00
|
|
|
&MockSystemInstruction::AttemptDataChange { data: 50 },
|
2019-06-10 19:50:02 -07:00
|
|
|
account_metas,
|
|
|
|
)]);
|
|
|
|
|
2019-11-05 08:38:35 -08:00
|
|
|
let result = message_processor.process_message(&message, &mut loaders, &mut accounts);
|
2019-06-10 19:50:02 -07:00
|
|
|
assert_eq!(
|
|
|
|
result,
|
|
|
|
Err(TransactionError::InstructionError(
|
|
|
|
0,
|
2019-11-05 08:38:35 -08:00
|
|
|
InstructionError::ReadonlyDataModified
|
2019-06-10 19:50:02 -07:00
|
|
|
))
|
|
|
|
);
|
2019-02-22 12:08:54 -08:00
|
|
|
}
|
2019-10-25 16:22:41 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_loader_instruction_data() {
|
|
|
|
// First ensure the ix_data is unaffected if not invoking via a loader.
|
|
|
|
let ix_data = [1];
|
|
|
|
let mut loader_ix_data = vec![];
|
|
|
|
|
|
|
|
let native_pubkey = Pubkey::new_rand();
|
|
|
|
let native_loader = (native_pubkey, Account::new(0, 0, &native_pubkey));
|
|
|
|
assert_eq!(
|
|
|
|
get_loader_instruction_data(&[native_loader.clone()], &ix_data, &mut loader_ix_data),
|
|
|
|
&ix_data
|
|
|
|
);
|
|
|
|
|
|
|
|
// Now ensure the ix_data is wrapped when there's a loader present.
|
|
|
|
let acme_pubkey = Pubkey::new_rand();
|
|
|
|
let acme_loader = (acme_pubkey, Account::new(0, 0, &native_pubkey));
|
|
|
|
let expected_ix = LoaderInstruction::InvokeMain {
|
|
|
|
data: ix_data.to_vec(),
|
|
|
|
};
|
|
|
|
let expected_ix_data = bincode::serialize(&expected_ix).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
get_loader_instruction_data(
|
|
|
|
&[native_loader.clone(), acme_loader.clone()],
|
|
|
|
&ix_data,
|
|
|
|
&mut loader_ix_data
|
|
|
|
),
|
|
|
|
&expected_ix_data[..]
|
|
|
|
);
|
|
|
|
|
|
|
|
// Note there was an allocation in the input vector.
|
|
|
|
assert_eq!(loader_ix_data, expected_ix_data);
|
|
|
|
}
|
2019-02-18 22:26:22 -08:00
|
|
|
}
|