Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dont call precompiled programs #19930

Merged
merged 1 commit into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 2 additions & 23 deletions Cargo.lock

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

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ members = [
"programs/bpf_loader",
"programs/compute-budget",
"programs/config",
"programs/ed25519",
"programs/failure",
"programs/noop",
"programs/ownable",
"programs/secp256k1",
"programs/stake",
"programs/vote",
"rbpf-cli",
Expand Down
19 changes: 13 additions & 6 deletions program-runtime/src/instruction_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,19 @@ impl InstructionProcessor {
/// Add a static entrypoint to intercept instructions before the dynamic loader.
pub fn add_program(
&mut self,
program_id: Pubkey,
program_id: &Pubkey,
process_instruction: ProcessInstructionWithContext,
) {
match self.programs.iter_mut().find(|(key, _)| program_id == *key) {
match self.programs.iter_mut().find(|(key, _)| program_id == key) {
Some((_, processor)) => *processor = process_instruction,
None => self.programs.push((program_id, process_instruction)),
None => self.programs.push((*program_id, process_instruction)),
}
}

/// Remove a program
pub fn remove_program(&mut self, program_id: &Pubkey) {
if let Some(position) = self.programs.iter().position(|(key, _)| program_id == key) {
self.programs.remove(position);
}
}

Expand Down Expand Up @@ -608,7 +615,7 @@ impl InstructionProcessor {

let mut instruction_processor = InstructionProcessor::default();
for (program_id, process_instruction) in invoke_context.get_programs().iter() {
instruction_processor.add_program(*program_id, *process_instruction);
instruction_processor.add_program(program_id, *process_instruction);
}

let mut result = instruction_processor.process_instruction(
Expand Down Expand Up @@ -1090,8 +1097,8 @@ mod tests {
Ok(())
}
let program_id = solana_sdk::pubkey::new_rand();
instruction_processor.add_program(program_id, mock_process_instruction);
instruction_processor.add_program(program_id, mock_ix_processor);
instruction_processor.add_program(&program_id, mock_process_instruction);
instruction_processor.add_program(&program_id, mock_ix_processor);

assert!(!format!("{:?}", instruction_processor).is_empty());
}
Expand Down
4 changes: 2 additions & 2 deletions program-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ impl ProgramTest {
// Add loaders
macro_rules! add_builtin {
($b:expr) => {
bank.add_builtin(&$b.0, $b.1, $b.2)
bank.add_builtin(&$b.0, &$b.1, $b.2)
};
}
add_builtin!(solana_bpf_loader_deprecated_program!());
Expand All @@ -795,7 +795,7 @@ impl ProgramTest {
for builtin in self.builtins.iter() {
bank.add_builtin(
&builtin.name,
builtin.id,
&builtin.id,
builtin.process_instruction_with_context,
);
}
Expand Down
16 changes: 0 additions & 16 deletions programs/bpf/Cargo.lock

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

2 changes: 1 addition & 1 deletion programs/bpf/benches/bpf_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ fn bench_program_execute_noop(bencher: &mut Bencher) {
} = create_genesis_config(50);
let mut bank = Bank::new_for_benches(&genesis_config);
let (name, id, entrypoint) = solana_bpf_loader_program!();
bank.add_builtin(&name, id, entrypoint);
bank.add_builtin(&name, &id, entrypoint);
let bank = Arc::new(bank);
let bank_client = BankClient::new_shared(&bank);

Expand Down
19 changes: 16 additions & 3 deletions programs/bpf/c/src/invoke/invoke.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ static const uint8_t TEST_PRIVILEGE_DEESCALATION_ESCALATION_WRITABLE = 13;
static const uint8_t TEST_WRITABLE_DEESCALATION_WRITABLE = 14;
static const uint8_t TEST_NESTED_INVOKE_TOO_DEEP = 15;
static const uint8_t TEST_EXECUTABLE_LAMPORTS = 16;
static const uint8_t ADD_LAMPORTS = 17;
static const uint8_t TEST_RETURN_DATA_TOO_LARGE = 18;
static const uint8_t TEST_CALL_PRECOMPILE = 17;
static const uint8_t ADD_LAMPORTS = 18;
static const uint8_t TEST_RETURN_DATA_TOO_LARGE = 19;

static const int MINT_INDEX = 0;
static const int ARGUMENT_INDEX = 1;
Expand All @@ -40,6 +41,8 @@ static const int DERIVED_KEY2_INDEX = 7;
static const int DERIVED_KEY3_INDEX = 8;
static const int SYSTEM_PROGRAM_INDEX = 9;
static const int FROM_INDEX = 10;
static const int ED25519_PROGRAM_INDEX = 11;
static const int INVOKE_PROGRAM_INDEX = 12;

uint64_t do_nested_invokes(uint64_t num_nested_invokes,
SolAccountInfo *accounts, uint64_t num_accounts) {
Expand Down Expand Up @@ -73,7 +76,7 @@ uint64_t do_nested_invokes(uint64_t num_nested_invokes,
extern uint64_t entrypoint(const uint8_t *input) {
sol_log("Invoke C program");

SolAccountInfo accounts[12];
SolAccountInfo accounts[13];
SolParameters params = (SolParameters){.ka = accounts};

if (!sol_deserialize(input, &params, SOL_ARRAY_SIZE(accounts))) {
Expand Down Expand Up @@ -588,6 +591,16 @@ extern uint64_t entrypoint(const uint8_t *input) {
*accounts[ARGUMENT_INDEX].lamports += 1;
break;
}
case TEST_CALL_PRECOMPILE: {
sol_log("Test calling precompile from cpi");
SolAccountMeta arguments[] = {};
uint8_t data[] = {};
const SolInstruction instruction = {accounts[ED25519_PROGRAM_INDEX].key,
arguments, SOL_ARRAY_SIZE(arguments),
data, SOL_ARRAY_SIZE(data)};
sol_invoke(&instruction, accounts, SOL_ARRAY_SIZE(accounts));
break;
}
case ADD_LAMPORTS: {
*accounts[0].lamports += 1;
break;
Expand Down
12 changes: 11 additions & 1 deletion programs/bpf/rust/invoke/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use solana_program::{
account_info::AccountInfo,
entrypoint,
entrypoint::{ProgramResult, MAX_PERMITTED_DATA_INCREASE},
instruction::Instruction,
msg,
program::{get_return_data, invoke, invoke_signed, set_return_data},
program_error::ProgramError,
Expand All @@ -32,7 +33,8 @@ const TEST_PRIVILEGE_DEESCALATION_ESCALATION_WRITABLE: u8 = 13;
const TEST_WRITABLE_DEESCALATION_WRITABLE: u8 = 14;
const TEST_NESTED_INVOKE_TOO_DEEP: u8 = 15;
const TEST_EXECUTABLE_LAMPORTS: u8 = 16;
const ADD_LAMPORTS: u8 = 17;
const TEST_CALL_PRECOMPILE: u8 = 17;
const ADD_LAMPORTS: u8 = 18;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question for @seanyoung as above


// const MINT_INDEX: usize = 0; // unused placeholder
const ARGUMENT_INDEX: usize = 1;
Expand All @@ -45,6 +47,8 @@ const DERIVED_KEY2_INDEX: usize = 7;
const DERIVED_KEY3_INDEX: usize = 8;
const SYSTEM_PROGRAM_INDEX: usize = 9;
const FROM_INDEX: usize = 10;
const ED25519_PROGRAM_INDEX: usize = 11;
// const INVOKE_PROGRAM_INDEX: usize = 12; unused placeholder

fn do_nested_invokes(num_nested_invokes: u64, accounts: &[AccountInfo]) -> ProgramResult {
assert!(accounts[ARGUMENT_INDEX].is_signer);
Expand Down Expand Up @@ -663,6 +667,12 @@ fn process_instruction(
// reset executable account
**(*accounts[ARGUMENT_INDEX].lamports).borrow_mut() += 1;
}
TEST_CALL_PRECOMPILE => {
msg!("Test calling precompiled program from cpi");
let instruction =
Instruction::new_with_bytes(*accounts[ED25519_PROGRAM_INDEX].key, &[], vec![]);
invoke(&instruction, accounts)?;
}
ADD_LAMPORTS => {
// make sure the total balance is fine
**accounts[0].lamports.borrow_mut() += 1;
Expand Down
Loading