Skip to content

Commit

Permalink
Add get_processed_inner_instruction syscall
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmay committed Jan 18, 2022
1 parent 4944340 commit 9f79099
Show file tree
Hide file tree
Showing 11 changed files with 398 additions and 8 deletions.
18 changes: 18 additions & 0 deletions program-runtime/src/invoke_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ pub struct InvokeContext<'a> {
pub timings: ExecuteDetailsTimings,
pub blockhash: Hash,
pub lamports_per_signature: u64,
pub processed_inner_instructions: Vec<(usize, Instruction)>,
}

impl<'a> InvokeContext<'a> {
Expand Down Expand Up @@ -233,6 +234,7 @@ impl<'a> InvokeContext<'a> {
timings: ExecuteDetailsTimings::default(),
blockhash,
lamports_per_signature,
processed_inner_instructions: Vec::new(),
}
}

Expand Down Expand Up @@ -815,6 +817,7 @@ impl<'a> InvokeContext<'a> {
if let Some(instruction_recorder) = &self.instruction_recorder {
instruction_recorder.borrow_mut().begin_next_recording();
}
self.processed_inner_instructions.clear();
} else {
// Verify the calling program hasn't misbehaved
let mut verify_caller_time = Measure::start("verify_caller_time");
Expand Down Expand Up @@ -1012,6 +1015,21 @@ impl<'a> InvokeContext<'a> {
pub fn get_sysvar_cache(&self) -> &SysvarCache {
&self.sysvar_cache
}

// Push a processed inner instruction
pub fn add_processed_inner_instruction(&mut self, instruction: Instruction) {
self.processed_inner_instructions.push((self.invoke_depth(), instruction));
}

/// Get a processed inner instruction, reverse ordered list, where last added is index 0
pub fn get_processed_inner_instruction(&self, index: usize) -> Option<(usize, &Instruction)> {
let index = self
.processed_inner_instructions
.len()
.checked_sub(1)?
.checked_sub(index)?;
self.processed_inner_instructions.get(index).map(|(usize, instruction)| (*usize, instruction))
}
}

pub struct MockInvokeContextPreparation {
Expand Down
7 changes: 7 additions & 0 deletions programs/bpf/Cargo.lock

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

1 change: 1 addition & 0 deletions programs/bpf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ members = [
"rust/log_data",
"rust/external_spend",
"rust/finalize",
"rust/inner_instruction",
"rust/instruction_introspection",
"rust/invoke",
"rust/invoke_and_error",
Expand Down
1 change: 1 addition & 0 deletions programs/bpf/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ fn main() {
"log_data",
"external_spend",
"finalize",
"inner_instruction",
"instruction_introspection",
"invoke",
"invoke_and_error",
Expand Down
23 changes: 23 additions & 0 deletions programs/bpf/rust/inner_instruction/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "solana-bpf-rust-inner-instructions"
version = "1.10.0"
description = "Solana BPF test program written in Rust"
authors = ["Solana Maintainers <[email protected]>"]
repository = "https://github.com/solana-labs/solana"
license = "Apache-2.0"
homepage = "https://solana.com/"
documentation = "https://docs.rs/solana-bpf-rust-log-data"
edition = "2021"

[dependencies]
solana-program = { path = "../../../../sdk/program", version = "=1.10.0" }

[features]
default = ["program"]
program = []

[lib]
crate-type = ["lib", "cdylib"]

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
61 changes: 61 additions & 0 deletions programs/bpf/rust/inner_instruction/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//! Example Rust-based BPF program that uses sol_get_processed_inner_instruction syscall
#![cfg(feature = "program")]

use solana_program::{
account_info::AccountInfo,
entrypoint,
entrypoint::ProgramResult,
instruction::{get_processed_inner_instruction, AccountMeta, Instruction},
msg,
program::invoke,
pubkey::Pubkey,
};

entrypoint!(process_instruction);
fn process_instruction(
_program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
msg!("inner");

// account 0 is mint
// account 1 is noop
// account 2 is invoke_and_return

let instruction3 = Instruction::new_with_bytes(*accounts[1].key, instruction_data, vec![]);
let instruction2 = Instruction::new_with_bytes(
*accounts[2].key,
instruction_data,
vec![AccountMeta::new_readonly(*accounts[1].key, false)],
);
let instruction1 = Instruction::new_with_bytes(
*accounts[1].key,
instruction_data,
vec![
AccountMeta::new_readonly(*accounts[0].key, true),
AccountMeta::new_readonly(*accounts[1].key, false),
],
);
let instruction0 = Instruction::new_with_bytes(
*accounts[1].key,
instruction_data,
vec![
AccountMeta::new_readonly(*accounts[1].key, false),
AccountMeta::new_readonly(*accounts[0].key, true),
],
);

invoke(&instruction2, accounts)?;
invoke(&instruction1, accounts)?;
invoke(&instruction0, accounts)?;

assert_eq!(Some((1, instruction0)), get_processed_inner_instruction(0));
assert_eq!(Some((1, instruction1)), get_processed_inner_instruction(1));
assert_eq!(Some((1, instruction2)), get_processed_inner_instruction(2));
assert_eq!(Some((2, instruction3)), get_processed_inner_instruction(3));
assert!(get_processed_inner_instruction(4).is_none());

Ok(())
}
50 changes: 50 additions & 0 deletions programs/bpf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3331,3 +3331,53 @@ fn test_program_bpf_realloc_invoke() {
TransactionError::InstructionError(0, InstructionError::InvalidRealloc)
);
}

#[test]
#[cfg(any(feature = "bpf_rust"))]
fn test_program_bpf_processed_inner_instruction() {
solana_logger::setup();

let GenesisConfigInfo {
genesis_config,
mint_keypair,
..
} = create_genesis_config(50);
let mut bank = Bank::new_for_tests(&genesis_config);
let (name, id, entrypoint) = solana_bpf_loader_program!();
bank.add_builtin(&name, &id, entrypoint);
let bank = Arc::new(bank);
let bank_client = BankClient::new_shared(&bank);

let program_id = load_bpf_program(
&bank_client,
&bpf_loader::id(),
&mint_keypair,
"solana_bpf_rust_inner_instructions",
);
let noop_program_id = load_bpf_program(
&bank_client,
&bpf_loader::id(),
&mint_keypair,
"solana_bpf_rust_noop",
);
let invoke_and_return_program_id = load_bpf_program(
&bank_client,
&bpf_loader::id(),
&mint_keypair,
"solana_bpf_rust_invoke_and_return",
);

let instruction = Instruction::new_with_bytes(
program_id,
&[1, 2, 3, 0, 4, 5, 6],
vec![
AccountMeta::new(mint_keypair.pubkey(), true),
AccountMeta::new_readonly(noop_program_id, false),
AccountMeta::new_readonly(invoke_and_return_program_id, false),
],
);
let message = Message::new(&[instruction], Some(&mint_keypair.pubkey()));
assert!(bank_client
.send_and_confirm_message(&[&mint_keypair], message)
.is_ok());
}
117 changes: 110 additions & 7 deletions programs/bpf_loader/src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ use {
blake3, bpf_loader, bpf_loader_deprecated, bpf_loader_upgradeable,
entrypoint::{BPF_ALIGN_OF_U128, MAX_PERMITTED_DATA_INCREASE, SUCCESS},
feature_set::{
self, blake3_syscall_enabled, disable_fees_sysvar, do_support_realloc,
fixed_memcpy_nonoverlapping_check, libsecp256k1_0_5_upgrade_enabled,
prevent_calling_precompiles_as_programs, return_data_syscall_enabled,
secp256k1_recover_syscall_enabled, sol_log_data_syscall_enabled,
update_syscall_base_costs,
self, add_get_processed_inner_instruction_syscall, blake3_syscall_enabled,
disable_fees_sysvar, do_support_realloc, fixed_memcpy_nonoverlapping_check,
libsecp256k1_0_5_upgrade_enabled, prevent_calling_precompiles_as_programs,
return_data_syscall_enabled, secp256k1_recover_syscall_enabled,
sol_log_data_syscall_enabled, update_syscall_base_costs,
},
hash::{Hasher, HASH_BYTES},
instruction::{AccountMeta, Instruction, InstructionError},
instruction::{AccountMeta, InnerMeta, Instruction, InstructionError},
keccak, native_loader,
precompiles::is_precompile,
program::MAX_RETURN_DATA,
Expand Down Expand Up @@ -222,6 +222,16 @@ pub fn register_syscalls(
syscall_registry.register_syscall_by_name(b"sol_log_data", SyscallLogData::call)?;
}

if invoke_context
.feature_set
.is_active(&add_get_processed_inner_instruction_syscall::id())
{
syscall_registry.register_syscall_by_name(
b"sol_get_processed_inner_instruction",
SyscallGetProcessedInnerInstruction::call,
)?;
}

Ok(syscall_registry)
}

Expand Down Expand Up @@ -262,6 +272,9 @@ pub fn bind_syscall_context_objects<'a, 'b>(
let is_zk_token_sdk_enabled = invoke_context
.feature_set
.is_active(&feature_set::zk_token_sdk_enabled::id());
let add_get_processed_inner_instruction_syscall = invoke_context
.feature_set
.is_active(&add_get_processed_inner_instruction_syscall::id());

let loader_id = invoke_context
.transaction_context
Expand Down Expand Up @@ -444,6 +457,15 @@ pub fn bind_syscall_context_objects<'a, 'b>(
}),
);

// processed inner instructions
bind_feature_gated_syscall_context_object!(
vm,
add_get_processed_inner_instruction_syscall,
Box::new(SyscallGetProcessedInnerInstruction {
invoke_context: invoke_context.clone(),
}),
);

// Cross-program invocation syscalls
vm.bind_syscall_context_object(
Box::new(SyscallInvokeSignedC {
Expand Down Expand Up @@ -2610,7 +2632,6 @@ fn check_authorized_program(
}
Ok(())
}

/// Call process instruction, common to both Rust and C
fn call<'a, 'b: 'a>(
syscall: &mut dyn SyscallInvokeSigned<'a, 'b>,
Expand Down Expand Up @@ -2677,6 +2698,8 @@ fn call<'a, 'b: 'a>(
)
.map_err(SyscallError::InstructionError)?;

invoke_context.add_processed_inner_instruction(instruction);

// Copy results back to caller
for (callee_account_index, caller_account) in accounts.iter_mut() {
if let Some(caller_account) = caller_account {
Expand Down Expand Up @@ -2955,6 +2978,86 @@ impl<'a, 'b> SyscallObject<BpfError> for SyscallLogData<'a, 'b> {
}
}

pub struct SyscallGetProcessedInnerInstruction<'a, 'b> {
invoke_context: Rc<RefCell<&'a mut InvokeContext<'b>>>,
}
impl<'a, 'b> SyscallObject<BpfError> for SyscallGetProcessedInnerInstruction<'a, 'b> {
fn call(
&mut self,
index: u64,
meta_addr: u64,
program_id_addr: u64,
data_addr: u64,
accounts_addr: u64,
memory_mapping: &MemoryMapping,
result: &mut Result<u64, EbpfError<BpfError>>,
) {
let invoke_context = question_mark!(
self.invoke_context
.try_borrow()
.map_err(|_| SyscallError::InvokeContextBorrowFailed),
result
);
let loader_id = question_mark!(
invoke_context
.transaction_context
.get_loader_key()
.map_err(SyscallError::InstructionError),
result
);

let budget = invoke_context.get_compute_budget();
question_mark!(
invoke_context
.get_compute_meter()
.consume(budget.syscall_base_cost),
result
);

if let Some((stack_depth, instruction)) = invoke_context.get_processed_inner_instruction(index as usize) {
let InnerMeta {
data_len,
accounts_len,
depth,
} = question_mark!(
translate_type_mut::<InnerMeta>(memory_mapping, meta_addr, &loader_id),
result
);
if *data_len >= instruction.data.len()
&& *accounts_len >= instruction.accounts.len()
{
let program_id = question_mark!(
translate_type_mut::<Pubkey>(memory_mapping, program_id_addr, &loader_id),
result
);
let data = question_mark!(
translate_slice_mut::<u8>(memory_mapping, data_addr, *data_len as u64, &loader_id,),
result
);
let accounts = question_mark!(
translate_slice_mut::<AccountMeta>(
memory_mapping,
accounts_addr,
*accounts_len as u64,
&loader_id,
),
result
);

*program_id = instruction.program_id;
data.clone_from_slice(instruction.data.as_slice());
accounts.clone_from_slice(instruction.accounts.as_slice());
}
*data_len = instruction.data.len();
*accounts_len = instruction.accounts.len();
*depth = stack_depth;
*result = Ok(true as u64);
} else {
*result = Ok(false as u64);
}
}
}

#[cfg(test)]
mod tests {
#[allow(deprecated)]
Expand Down
Loading

0 comments on commit 9f79099

Please sign in to comment.