Add get_processed_sibling_instruction syscall (#22859)

This commit is contained in:
Jack May
2022-02-02 16:45:57 -08:00
committed by GitHub
parent 75563f6c7b
commit ab02dba96f
19 changed files with 898 additions and 61 deletions

View File

@ -307,7 +307,7 @@ fn process_instruction_common(
if program.executable()? {
debug_assert_eq!(
first_instruction_account,
1 - (invoke_context.get_invoke_depth() > 1) as usize,
1 - (invoke_context.get_stack_height() > 1) as usize,
);
if !check_loader_id(&program.owner()?) {
@ -1045,7 +1045,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.get_invoke_depth();
let stack_height = invoke_context.get_stack_height();
let mut serialize_time = Measure::start("serialize");
let program_id = *invoke_context.transaction_context.get_program_key()?;
@ -1074,7 +1074,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

@ -22,14 +22,17 @@ 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_sibling_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, Instruction, InstructionError, ProcessedSiblingInstruction,
TRANSACTION_LEVEL_STACK_HEIGHT,
},
keccak, native_loader,
precompiles::is_precompile,
program::MAX_RETURN_DATA,
@ -222,6 +225,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)
}
@ -262,6 +283,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_sibling_instruction_syscall = invoke_context
.feature_set
.is_active(&add_get_processed_sibling_instruction_syscall::id());
let loader_id = invoke_context
.transaction_context
@ -444,6 +468,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 {
@ -2955,6 +2997,166 @@ 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
.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
);
let stack_height = invoke_context.get_stack_height();
let instruction_trace = invoke_context.get_instruction_trace();
let instruction_context = 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_list| instruction_list.get(0))
} else {
// Walk the last list of inner instructions
instruction_trace.last().and_then(|inners| {
let mut current_index = 0;
inners.iter().rev().skip(1).find(|(this_stack_height, _)| {
if stack_height == *this_stack_height {
if index == current_index {
return true;
} else {
current_index += 1;
}
}
false
})
})
}
.map(|(_, instruction_context)| instruction_context);
if let Some(instruction_context) = instruction_context {
let ProcessedSiblingInstruction {
data_len,
accounts_len,
} = question_mark!(
translate_type_mut::<ProcessedSiblingInstruction>(
memory_mapping,
meta_addr,
&loader_id
),
result
);
if *data_len >= instruction_context.get_instruction_data().len()
&& *accounts_len == instruction_context.get_number_of_instruction_accounts()
{
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_context.get_program_id(invoke_context.transaction_context);
data.clone_from_slice(instruction_context.get_instruction_data());
let account_metas = instruction_context
.get_instruction_accounts_metas()
.iter()
.map(|meta| AccountMeta {
pubkey: *invoke_context
.get_key_of_account_at_index(meta.index_in_transaction),
is_signer: meta.is_signer,
is_writable: meta.is_writable,
})
.collect::<Vec<_>>();
accounts.clone_from_slice(account_metas.as_slice());
}
*data_len = instruction_context.get_instruction_data().len();
*accounts_len = instruction_context.get_number_of_instruction_accounts();
*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)]