Add get_processed_sibling_instruction syscall (#22859) (#22956)

This commit is contained in:
Jack May
2022-02-08 09:21:11 -08:00
committed by GitHub
parent 5c69af607d
commit d05b5b0902
19 changed files with 864 additions and 66 deletions

View File

@ -2772,6 +2772,20 @@ dependencies = [
"solana-program 1.9.6",
]
[[package]]
name = "solana-bpf-rust-sibling-instructions"
version = "1.9.6"
dependencies = [
"solana-program 1.9.6",
]
[[package]]
name = "solana-bpf-rust-sibling_inner-instructions"
version = "1.9.6"
dependencies = [
"solana-program 1.9.6",
]
[[package]]
name = "solana-bpf-rust-spoof1"
version = "1.9.6"

View File

@ -81,6 +81,8 @@ members = [
"rust/sanity",
"rust/secp256k1_recover",
"rust/sha",
"rust/sibling_inner_instruction",
"rust/sibling_instruction",
"rust/spoof1",
"rust/spoof1_system",
"rust/sysvar",

View File

@ -91,6 +91,8 @@ fn main() {
"sanity",
"secp256k1_recover",
"sha",
"sibling_inner_instruction",
"sibling_instruction",
"spoof1",
"spoof1_system",
"upgradeable",

View File

@ -0,0 +1,23 @@
[package]
name = "solana-bpf-rust-sibling_inner-instructions"
version = "1.9.6"
description = "Solana BPF test program written in Rust"
authors = ["Solana Maintainers <maintainers@solana.foundation>"]
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.9.6" }
[features]
default = ["program"]
program = []
[lib]
crate-type = ["lib", "cdylib"]
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

View File

@ -0,0 +1,69 @@
//! Example Rust-based BPF program that queries sibling instructions
#![cfg(feature = "program")]
use solana_program::{
account_info::AccountInfo,
entrypoint,
entrypoint::ProgramResult,
instruction::{
get_processed_sibling_instruction, get_stack_height, AccountMeta, Instruction,
TRANSACTION_LEVEL_STACK_HEIGHT,
},
msg,
pubkey::Pubkey,
};
entrypoint!(process_instruction);
fn process_instruction(
_program_id: &Pubkey,
accounts: &[AccountInfo],
_instruction_data: &[u8],
) -> ProgramResult {
msg!("sibling inner");
// account 0 is mint
// account 1 is noop
// account 2 is invoke_and_return
// Check sibling instructions
let sibling_instruction2 = Instruction::new_with_bytes(
*accounts[2].key,
&[3],
vec![AccountMeta::new_readonly(*accounts[1].key, false)],
);
let sibling_instruction1 = Instruction::new_with_bytes(
*accounts[1].key,
&[2],
vec![
AccountMeta::new_readonly(*accounts[0].key, true),
AccountMeta::new_readonly(*accounts[1].key, false),
],
);
let sibling_instruction0 = Instruction::new_with_bytes(
*accounts[1].key,
&[1],
vec![
AccountMeta::new_readonly(*accounts[1].key, false),
AccountMeta::new_readonly(*accounts[0].key, true),
],
);
assert_eq!(TRANSACTION_LEVEL_STACK_HEIGHT + 1, get_stack_height());
assert_eq!(
get_processed_sibling_instruction(0),
Some(sibling_instruction0)
);
assert_eq!(
get_processed_sibling_instruction(1),
Some(sibling_instruction1)
);
assert_eq!(
get_processed_sibling_instruction(2),
Some(sibling_instruction2)
);
assert_eq!(get_processed_sibling_instruction(3), None);
Ok(())
}

View File

@ -0,0 +1,23 @@
[package]
name = "solana-bpf-rust-sibling-instructions"
version = "1.9.6"
description = "Solana BPF test program written in Rust"
authors = ["Solana Maintainers <maintainers@solana.foundation>"]
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.9.6" }
[features]
default = ["program"]
program = []
[lib]
crate-type = ["lib", "cdylib"]
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

View File

@ -0,0 +1,100 @@
//! Example Rust-based BPF program that queries sibling instructions
#![cfg(feature = "program")]
use solana_program::{
account_info::AccountInfo,
entrypoint,
entrypoint::ProgramResult,
instruction::{
get_processed_sibling_instruction, get_stack_height, AccountMeta, Instruction,
TRANSACTION_LEVEL_STACK_HEIGHT,
},
msg,
program::invoke,
pubkey::Pubkey,
};
entrypoint!(process_instruction);
fn process_instruction(
_program_id: &Pubkey,
accounts: &[AccountInfo],
_instruction_data: &[u8],
) -> ProgramResult {
msg!("sibling");
// account 0 is mint
// account 1 is noop
// account 2 is invoke_and_return
// account 3 is sibling_inner
// Invoke child instructions
let instruction3 = Instruction::new_with_bytes(
*accounts[2].key,
&[3],
vec![AccountMeta::new_readonly(*accounts[1].key, false)],
);
let instruction2 = Instruction::new_with_bytes(
*accounts[1].key,
&[2],
vec![
AccountMeta::new_readonly(*accounts[0].key, true),
AccountMeta::new_readonly(*accounts[1].key, false),
],
);
let instruction1 = Instruction::new_with_bytes(
*accounts[1].key,
&[1],
vec![
AccountMeta::new_readonly(*accounts[1].key, false),
AccountMeta::new_readonly(*accounts[0].key, true),
],
);
let instruction0 = Instruction::new_with_bytes(
*accounts[3].key,
&[0],
vec![
AccountMeta::new_readonly(*accounts[0].key, false),
AccountMeta::new_readonly(*accounts[1].key, false),
AccountMeta::new_readonly(*accounts[2].key, false),
AccountMeta::new_readonly(*accounts[3].key, false),
],
);
invoke(&instruction3, accounts)?;
invoke(&instruction2, accounts)?;
invoke(&instruction1, accounts)?;
invoke(&instruction0, accounts)?;
// Check sibling instructions
let sibling_instruction1 = Instruction::new_with_bytes(
*accounts[1].key,
&[43],
vec![
AccountMeta::new_readonly(*accounts[1].key, false),
AccountMeta::new(*accounts[0].key, true),
],
);
let sibling_instruction0 = Instruction::new_with_bytes(
*accounts[1].key,
&[42],
vec![
AccountMeta::new(*accounts[0].key, true),
AccountMeta::new_readonly(*accounts[1].key, false),
],
);
assert_eq!(TRANSACTION_LEVEL_STACK_HEIGHT, get_stack_height());
assert_eq!(
get_processed_sibling_instruction(0),
Some(sibling_instruction0)
);
assert_eq!(
get_processed_sibling_instruction(1),
Some(sibling_instruction1)
);
assert_eq!(get_processed_sibling_instruction(2), None);
Ok(())
}

View File

@ -3327,3 +3327,79 @@ 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 sibling_program_id = load_bpf_program(
&bank_client,
&bpf_loader::id(),
&mint_keypair,
"solana_bpf_rust_sibling_instructions",
);
let sibling_inner_program_id = load_bpf_program(
&bank_client,
&bpf_loader::id(),
&mint_keypair,
"solana_bpf_rust_sibling_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 instruction2 = Instruction::new_with_bytes(
noop_program_id,
&[43],
vec![
AccountMeta::new_readonly(noop_program_id, false),
AccountMeta::new(mint_keypair.pubkey(), true),
],
);
let instruction1 = Instruction::new_with_bytes(
noop_program_id,
&[42],
vec![
AccountMeta::new(mint_keypair.pubkey(), true),
AccountMeta::new_readonly(noop_program_id, false),
],
);
let instruction0 = Instruction::new_with_bytes(
sibling_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),
AccountMeta::new_readonly(sibling_inner_program_id, false),
],
);
let message = Message::new(
&[instruction2, instruction1, instruction0],
Some(&mint_keypair.pubkey()),
);
assert!(bank_client
.send_and_confirm_message(&[&mint_keypair], message)
.is_ok());
}

View File

@ -298,7 +298,7 @@ fn process_instruction_common(
if program.executable()? {
debug_assert_eq!(
first_instruction_account,
1 - (invoke_context.invoke_depth() > 1) as usize,
1 - (invoke_context.get_stack_height() > 1) as usize,
);
if !check_loader_id(&program.owner()?) {
@ -1036,7 +1036,7 @@ impl Executor for BpfExecutor {
) -> Result<(), InstructionError> {
let log_collector = invoke_context.get_log_collector();
let compute_meter = invoke_context.get_compute_meter();
let invoke_depth = invoke_context.invoke_depth();
let stack_height = invoke_context.get_stack_height();
let mut serialize_time = Measure::start("serialize");
let keyed_accounts = invoke_context.get_keyed_accounts()?;
@ -1068,7 +1068,7 @@ impl Executor for BpfExecutor {
create_vm_time.stop();
execute_time = Measure::start("execute");
stable_log::program_invoke(&log_collector, &program_id, invoke_depth);
stable_log::program_invoke(&log_collector, &program_id, stack_height);
let mut instruction_meter = ThisInstructionMeter::new(compute_meter.clone());
let before = compute_meter.borrow().get_remaining();
let result = if use_jit {

View File

@ -21,13 +21,17 @@ use {
blake3, bpf_loader, bpf_loader_deprecated, bpf_loader_upgradeable,
entrypoint::{BPF_ALIGN_OF_U128, MAX_PERMITTED_DATA_INCREASE, SUCCESS},
feature_set::{
blake3_syscall_enabled, disable_fees_sysvar, do_support_realloc,
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,
add_get_processed_sibling_instruction_syscall, blake3_syscall_enabled,
disable_fees_sysvar, do_support_realloc, 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, Instruction, InstructionError, ProcessedSiblingInstruction,
TRANSACTION_LEVEL_STACK_HEIGHT,
},
keccak,
message::{Message, SanitizedMessage},
native_loader,
@ -204,6 +208,24 @@ 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_sibling_instruction_syscall::id())
{
syscall_registry.register_syscall_by_name(
b"sol_get_processed_sibling_instruction",
SyscallGetProcessedSiblingInstruction::call,
)?;
}
if invoke_context
.feature_set
.is_active(&add_get_processed_sibling_instruction_syscall::id())
{
syscall_registry
.register_syscall_by_name(b"sol_get_stack_height", SyscallGetStackHeight::call)?;
}
Ok(syscall_registry)
}
@ -241,6 +263,9 @@ pub fn bind_syscall_context_objects<'a, 'b>(
let is_sol_log_data_syscall_active = invoke_context
.feature_set
.is_active(&sol_log_data_syscall_enabled::id());
let add_get_processed_sibling_instruction_syscall = invoke_context
.feature_set
.is_active(&add_get_processed_sibling_instruction_syscall::id());
let loader_id = invoke_context
.get_loader()
@ -400,6 +425,24 @@ pub fn bind_syscall_context_objects<'a, 'b>(
}),
);
// processed inner instructions
bind_feature_gated_syscall_context_object!(
vm,
add_get_processed_sibling_instruction_syscall,
Box::new(SyscallGetProcessedSiblingInstruction {
invoke_context: invoke_context.clone(),
}),
);
// Get stack height
bind_feature_gated_syscall_context_object!(
vm,
add_get_processed_sibling_instruction_syscall,
Box::new(SyscallGetStackHeight {
invoke_context: invoke_context.clone(),
}),
);
// Cross-program invocation syscalls
vm.bind_syscall_context_object(
Box::new(SyscallInvokeSignedC {
@ -2387,6 +2430,7 @@ fn call<'a, 'b: 'a>(
signers_seeds_len,
memory_mapping,
)?;
let stack_height = invoke_context.get_stack_height();
let (message, caller_write_privileges, program_indices) = invoke_context
.create_message(&instruction, &signers)
.map_err(SyscallError::InstructionError)?;
@ -2401,9 +2445,7 @@ fn call<'a, 'b: 'a>(
)?;
// Record the instruction
if let Some(instruction_recorder) = &invoke_context.instruction_recorder {
instruction_recorder.record_instruction(instruction);
}
invoke_context.record_instruction(stack_height.saturating_add(1), instruction);
// Process instruction
let message = SanitizedMessage::Legacy(message);
@ -2684,6 +2726,141 @@ impl<'a, 'b> SyscallObject<BpfError> for SyscallLogData<'a, 'b> {
}
}
pub struct SyscallGetProcessedSiblingInstruction<'a, 'b> {
invoke_context: Rc<RefCell<&'a mut InvokeContext<'b>>>,
}
impl<'a, 'b> SyscallObject<BpfError> for SyscallGetProcessedSiblingInstruction<'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
.get_loader()
.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
);
let stack_height = invoke_context.get_stack_height();
let instruction_trace = invoke_context.get_instruction_trace();
let instruction = if stack_height == TRANSACTION_LEVEL_STACK_HEIGHT {
// pick one of the top-level instructions
instruction_trace
.len()
.checked_sub(2)
.and_then(|result| result.checked_sub(index as usize))
.and_then(|index| instruction_trace.get(index))
.and_then(|instruction_recorder| instruction_recorder.get(0))
} else {
// Walk the last list of inner instructions
instruction_trace
.last()
.and_then(|inners| inners.find(stack_height, index as usize))
};
if let Some(instruction) = instruction {
let ProcessedSiblingInstruction {
data_len,
accounts_len,
} = question_mark!(
translate_type_mut::<ProcessedSiblingInstruction>(
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();
*result = Ok(true as u64);
return;
}
*result = Ok(false as u64);
}
}
pub struct SyscallGetStackHeight<'a, 'b> {
invoke_context: Rc<RefCell<&'a mut InvokeContext<'b>>>,
}
impl<'a, 'b> SyscallObject<BpfError> for SyscallGetStackHeight<'a, 'b> {
fn call(
&mut self,
_arg1: u64,
_arg2: u64,
_arg3: u64,
_arg4: u64,
_arg5: 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 budget = invoke_context.get_compute_budget();
question_mark!(
invoke_context
.get_compute_meter()
.consume(budget.syscall_base_cost),
result
);
*result = Ok(invoke_context.get_stack_height() as u64);
}
}
#[cfg(test)]
mod tests {
#[allow(deprecated)]