Revert "Add native loader entry points (#9275)" Breaks genesis_config abi (#9377)

This reverts commit ed86d8d1fc.
This commit is contained in:
Jack May 2020-04-08 14:36:18 -07:00 committed by GitHub
parent 4522e85ac4
commit ad0482be73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 157 additions and 300 deletions

6
Cargo.lock generated
View File

@ -3943,7 +3943,6 @@ dependencies = [
"serde_json 1.0.51 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.51 (registry+https://github.com/rust-lang/crates.io-index)",
"serial_test 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "serial_test 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serial_test_derive 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "serial_test_derive 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"solana-bpf-loader-program 1.2.0",
"solana-budget-program 1.2.0", "solana-budget-program 1.2.0",
"solana-chacha-cuda 1.2.0", "solana-chacha-cuda 1.2.0",
"solana-clap-utils 1.2.0", "solana-clap-utils 1.2.0",
@ -4494,8 +4493,9 @@ dependencies = [
"num_cpus 1.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.105 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.105 (registry+https://github.com/rust-lang/crates.io-index)",
"solana-bpf-loader-program 1.2.0",
"solana-logger 1.2.0", "solana-logger 1.2.0",
"solana-measure 1.2.0", "solana-measure 1.2.0",
"solana-metrics 1.2.0", "solana-metrics 1.2.0",

View File

@ -40,8 +40,7 @@ rayon = "1.3.0"
regex = "1.3.6" regex = "1.3.6"
serde = "1.0.106" serde = "1.0.106"
serde_derive = "1.0.103" serde_derive = "1.0.103"
serde_json = "1.0.51" serde_json = "1.0.49"
solana-bpf-loader-program = { path = "../programs/bpf_loader", version = "1.2.0" }
solana-budget-program = { path = "../programs/budget", version = "1.2.0" } solana-budget-program = { path = "../programs/budget", version = "1.2.0" }
solana-clap-utils = { path = "../clap-utils", version = "1.2.0" } solana-clap-utils = { path = "../clap-utils", version = "1.2.0" }
solana-client = { path = "../client", version = "1.2.0" } solana-client = { path = "../client", version = "1.2.0" }

View File

@ -58,9 +58,6 @@ pub mod verified_vote_packets;
pub mod weighted_shuffle; pub mod weighted_shuffle;
pub mod window_service; pub mod window_service;
#[macro_use]
extern crate solana_bpf_loader_program;
#[macro_use] #[macro_use]
extern crate solana_budget_program; extern crate solana_budget_program;

View File

@ -717,9 +717,6 @@ impl TestValidator {
genesis_config genesis_config
.native_instruction_processors .native_instruction_processors
.push(solana_budget_program!()); .push(solana_budget_program!());
genesis_config
.native_instruction_processors
.push(solana_bpf_loader_program!());
genesis_config.rent.lamports_per_byte_year = 1; genesis_config.rent.lamports_per_byte_year = 1;
genesis_config.rent.exemption_threshold = 1.0; genesis_config.rent.exemption_threshold = 1.0;

View File

@ -1,7 +1,6 @@
use solana_sdk::{ use solana_sdk::{
clock::Epoch, genesis_config::OperatingMode, inflation::Inflation, clock::Epoch, genesis_config::OperatingMode, inflation::Inflation,
move_loader::solana_move_loader_program, native_loader, pubkey::Pubkey, move_loader::solana_move_loader_program, pubkey::Pubkey, system_program::solana_system_program,
system_program::solana_system_program,
}; };
#[macro_use] #[macro_use]
@ -50,10 +49,7 @@ pub fn get_inflation(operating_mode: OperatingMode, epoch: Epoch) -> Option<Infl
} }
} }
pub fn get_programs( pub fn get_programs(operating_mode: OperatingMode, epoch: Epoch) -> Option<Vec<(String, Pubkey)>> {
operating_mode: OperatingMode,
epoch: Epoch,
) -> Option<Vec<(native_loader::Info, Pubkey)>> {
match operating_mode { match operating_mode {
OperatingMode::Development => { OperatingMode::Development => {
if epoch == 0 { if epoch == 0 {
@ -126,9 +122,9 @@ pub fn get_entered_epoch_callback(operating_mode: OperatingMode) -> EnteredEpoch
bank.set_inflation(inflation); bank.set_inflation(inflation);
} }
if let Some(new_programs) = get_programs(operating_mode, bank.epoch()) { if let Some(new_programs) = get_programs(operating_mode, bank.epoch()) {
for (info, program_id) in new_programs.iter() { for (name, program_id) in new_programs.iter() {
info!("Registering {:?} at {}", info, program_id); info!("Registering {} at {}", name, program_id);
bank.register_native_instruction_processor(info, program_id); bank.register_native_instruction_processor(name, program_id);
} }
} }
}) })

View File

@ -20,7 +20,6 @@ use solana_sdk::{
epoch_schedule::EpochSchedule, epoch_schedule::EpochSchedule,
genesis_config::{GenesisConfig, OperatingMode}, genesis_config::{GenesisConfig, OperatingMode},
message::Message, message::Message,
native_loader,
poh_config::PohConfig, poh_config::PohConfig,
pubkey::Pubkey, pubkey::Pubkey,
signature::{Keypair, Signer}, signature::{Keypair, Signer},
@ -82,7 +81,7 @@ pub struct ClusterConfig {
pub slots_per_epoch: u64, pub slots_per_epoch: u64,
pub slots_per_segment: u64, pub slots_per_segment: u64,
pub stakers_slot_offset: u64, pub stakers_slot_offset: u64,
pub native_instruction_processors: Vec<(native_loader::Info, Pubkey)>, pub native_instruction_processors: Vec<(String, Pubkey)>,
pub operating_mode: OperatingMode, pub operating_mode: OperatingMode,
pub poh_config: PohConfig, pub poh_config: PohConfig,
} }
@ -180,13 +179,14 @@ impl LocalCluster {
.push(solana_storage_program!()); .push(solana_storage_program!());
} }
} }
genesis_config
.native_instruction_processors
.extend_from_slice(&config.native_instruction_processors);
genesis_config.inflation = genesis_config.inflation =
solana_genesis_programs::get_inflation(genesis_config.operating_mode, 0).unwrap(); solana_genesis_programs::get_inflation(genesis_config.operating_mode, 0).unwrap();
genesis_config
.native_instruction_processors
.extend_from_slice(&config.native_instruction_processors);
let storage_keypair = Keypair::new(); let storage_keypair = Keypair::new();
genesis_config.add_account( genesis_config.add_account(
storage_keypair.pubkey(), storage_keypair.pubkey(),

View File

@ -1980,6 +1980,7 @@ dependencies = [
"rayon 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.105 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.105 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.105 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.105 (registry+https://github.com/rust-lang/crates.io-index)",
"solana-bpf-loader-program 1.2.0",
"solana-logger 1.2.0", "solana-logger 1.2.0",
"solana-measure 1.2.0", "solana-measure 1.2.0",
"solana-metrics 1.2.0", "solana-metrics 1.2.0",

View File

@ -18,7 +18,6 @@ mod bpf {
sysvar::{clock, fees, rent, rewards, slot_hashes, stake_history}, sysvar::{clock, fees, rent, rewards, slot_hashes, stake_history},
transaction::TransactionError, transaction::TransactionError,
}; };
use solana_bpf_loader_program::solana_bpf_loader_program;
use std::{env, fs::File, io::Read, path::PathBuf, sync::Arc}; use std::{env, fs::File, io::Read, path::PathBuf, sync::Arc};
/// BPF program file extension /// BPF program file extension
@ -84,11 +83,10 @@ mod bpf {
println!("Test program: {:?}", program.0); println!("Test program: {:?}", program.0);
let GenesisConfigInfo { let GenesisConfigInfo {
mut genesis_config, genesis_config,
mint_keypair, mint_keypair,
.. ..
} = create_genesis_config(50); } = create_genesis_config(50);
genesis_config.add_native_instruction_processor(solana_bpf_loader_program!());
let bank = Arc::new(Bank::new(&genesis_config)); let bank = Arc::new(Bank::new(&genesis_config));
// Create bank with specific slot, used by solana_bpf_rust_sysvar test // Create bank with specific slot, used by solana_bpf_rust_sysvar test
let bank = let bank =
@ -135,11 +133,10 @@ mod bpf {
println!("Test program: {:?}", program); println!("Test program: {:?}", program);
let GenesisConfigInfo { let GenesisConfigInfo {
mut genesis_config, genesis_config,
mint_keypair, mint_keypair,
.. ..
} = create_genesis_config(50); } = create_genesis_config(50);
genesis_config.add_native_instruction_processor(solana_bpf_loader_program!());
let bank = Arc::new(Bank::new(&genesis_config)); let bank = Arc::new(Bank::new(&genesis_config));
let bank_client = BankClient::new_shared(&bank); let bank_client = BankClient::new_shared(&bank);
let program_id = load_bpf_program(&bank_client, &mint_keypair, program); let program_id = load_bpf_program(&bank_client, &mint_keypair, program);
@ -218,11 +215,10 @@ mod bpf {
println!("Test program: {:?}", program); println!("Test program: {:?}", program);
let GenesisConfigInfo { let GenesisConfigInfo {
mut genesis_config, genesis_config,
mint_keypair, mint_keypair,
.. ..
} = create_genesis_config(50); } = create_genesis_config(50);
genesis_config.add_native_instruction_processor(solana_bpf_loader_program!());
let bank = Bank::new(&genesis_config); let bank = Bank::new(&genesis_config);
let bank_client = BankClient::new(bank); let bank_client = BankClient::new(bank);
let program_id = load_bpf_program(&bank_client, &mint_keypair, program); let program_id = load_bpf_program(&bank_client, &mint_keypair, program);

View File

@ -25,7 +25,7 @@ use solana_sdk::{
use std::{io::prelude::*, mem}; use std::{io::prelude::*, mem};
use thiserror::Error; use thiserror::Error;
solana_sdk::declare_loader!( solana_sdk::declare_program!(
solana_sdk::bpf_loader::ID, solana_sdk::bpf_loader::ID,
solana_bpf_loader_program, solana_bpf_loader_program,
process_instruction process_instruction

View File

@ -4,7 +4,6 @@ use solana_runtime::loader_utils::create_invoke_instruction;
use solana_sdk::client::SyncClient; use solana_sdk::client::SyncClient;
use solana_sdk::genesis_config::create_genesis_config; use solana_sdk::genesis_config::create_genesis_config;
use solana_sdk::instruction::InstructionError; use solana_sdk::instruction::InstructionError;
use solana_sdk::native_program_info;
use solana_sdk::pubkey::Pubkey; use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::Signer; use solana_sdk::signature::Signer;
use solana_sdk::transaction::TransactionError; use solana_sdk::transaction::TransactionError;
@ -14,10 +13,7 @@ fn test_program_native_failure() {
let (genesis_config, alice_keypair) = create_genesis_config(50); let (genesis_config, alice_keypair) = create_genesis_config(50);
let program_id = Pubkey::new_rand(); let program_id = Pubkey::new_rand();
let bank = Bank::new(&genesis_config); let bank = Bank::new(&genesis_config);
bank.register_native_instruction_processor( bank.register_native_instruction_processor("solana_failure_program", &program_id);
&native_program_info!("solana_failure_program"),
&program_id,
);
// Call user program // Call user program
let instruction = create_invoke_instruction(alice_keypair.pubkey(), program_id, &1u8); let instruction = create_invoke_instruction(alice_keypair.pubkey(), program_id, &1u8);

View File

@ -27,6 +27,7 @@ rand = "0.6.5"
rayon = "1.3.0" rayon = "1.3.0"
serde = { version = "1.0.106", features = ["rc"] } serde = { version = "1.0.106", features = ["rc"] }
serde_derive = "1.0.103" serde_derive = "1.0.103"
solana-bpf-loader-program = { path = "../programs/bpf_loader", version = "1.2.0" }
solana-logger = { path = "../logger", version = "1.2.0" } solana-logger = { path = "../logger", version = "1.2.0" }
solana-measure = { path = "../measure", version = "1.2.0" } solana-measure = { path = "../measure", version = "1.2.0" }
solana-metrics = { path = "../metrics", version = "1.2.0" } solana-metrics = { path = "../metrics", version = "1.2.0" }

View File

@ -11,7 +11,6 @@ use solana_sdk::{
clock::MAX_RECENT_BLOCKHASHES, clock::MAX_RECENT_BLOCKHASHES,
genesis_config::create_genesis_config, genesis_config::create_genesis_config,
instruction::InstructionError, instruction::InstructionError,
native_program_info,
pubkey::Pubkey, pubkey::Pubkey,
signature::{Keypair, Signer}, signature::{Keypair, Signer},
transaction::Transaction, transaction::Transaction,
@ -125,7 +124,7 @@ fn do_bench_transactions(
let mut bank = Bank::new(&genesis_config); let mut bank = Bank::new(&genesis_config);
bank.add_instruction_processor(Pubkey::new(&BUILTIN_PROGRAM_ID), process_instruction); bank.add_instruction_processor(Pubkey::new(&BUILTIN_PROGRAM_ID), process_instruction);
bank.register_native_instruction_processor( bank.register_native_instruction_processor(
&native_program_info!("solana_noop_program"), "solana_noop_program",
&Pubkey::new(&NOOP_PROGRAM_ID), &Pubkey::new(&NOOP_PROGRAM_ID),
); );
let bank = Arc::new(bank); let bank = Arc::new(bank);

View File

@ -39,8 +39,7 @@ use solana_sdk::{
hard_forks::HardForks, hard_forks::HardForks,
hash::{extend_and_hash, hashv, Hash}, hash::{extend_and_hash, hashv, Hash},
inflation::Inflation, inflation::Inflation,
native_loader::{self, create_loadable_account}, native_loader, nonce,
native_program_info, nonce,
pubkey::Pubkey, pubkey::Pubkey,
signature::{Keypair, Signature}, signature::{Keypair, Signature},
slot_hashes::SlotHashes, slot_hashes::SlotHashes,
@ -899,18 +898,14 @@ impl Bank {
); );
// Add additional native programs specified in the genesis config // Add additional native programs specified in the genesis config
for (info, program_id) in &genesis_config.native_instruction_processors { for (name, program_id) in &genesis_config.native_instruction_processors {
self.register_native_instruction_processor(&info, program_id); self.register_native_instruction_processor(name, program_id);
} }
} }
pub fn register_native_instruction_processor( pub fn register_native_instruction_processor(&self, name: &str, program_id: &Pubkey) {
&self, debug!("Adding native program {} under {:?}", name, program_id);
info: &native_loader::Info, let account = native_loader::create_loadable_account(name);
program_id: &Pubkey,
) {
debug!("Adding {:?} under {:?}", info, program_id);
let account = create_loadable_account(info);
self.store_account(program_id, &account); self.store_account(program_id, &account);
} }
@ -2161,7 +2156,7 @@ impl Bank {
assert_eq!(program_account.owner, solana_sdk::native_loader::id()); assert_eq!(program_account.owner, solana_sdk::native_loader::id());
} else { } else {
// Register a bogus executable account, which will be loaded and ignored. // Register a bogus executable account, which will be loaded and ignored.
self.register_native_instruction_processor(&native_program_info!(""), &program_id); self.register_native_instruction_processor("", &program_id);
} }
} }

View File

@ -136,6 +136,7 @@ pub fn create_genesis_config_with_leader_ex(
// Bare minimum program set // Bare minimum program set
let native_instruction_processors = vec![ let native_instruction_processors = vec![
solana_system_program(), solana_system_program(),
solana_bpf_loader_program!(),
solana_vote_program!(), solana_vote_program!(),
solana_stake_program!(), solana_stake_program!(),
]; ];

View File

@ -30,6 +30,9 @@ extern crate solana_vote_program;
#[macro_use] #[macro_use]
extern crate solana_stake_program; extern crate solana_stake_program;
#[macro_use]
extern crate solana_bpf_loader_program;
#[macro_use] #[macro_use]
extern crate serde_derive; extern crate serde_derive;

View File

@ -1,18 +1,22 @@
use crate::{ use crate::{native_loader, rent_collector::RentCollector, system_instruction_processor};
native_loader::NativeLoader, rent_collector::RentCollector, system_instruction_processor,
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use solana_sdk::{ use solana_sdk::{
account::{create_keyed_readonly_accounts, Account, KeyedAccount}, account::{create_keyed_readonly_accounts, Account, KeyedAccount},
clock::Epoch, clock::Epoch,
entrypoint_native,
instruction::{CompiledInstruction, InstructionError}, instruction::{CompiledInstruction, InstructionError},
message::Message, message::Message,
native_loader, native_loader::id as native_loader_id,
pubkey::Pubkey, pubkey::Pubkey,
system_program, system_program,
transaction::TransactionError, transaction::TransactionError,
}; };
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, collections::HashMap, rc::Rc, sync::RwLock};
#[cfg(unix)]
use libloading::os::unix::*;
#[cfg(windows)]
use libloading::os::windows::*;
// The relevant state of an account before an Instruction executes, used // The relevant state of an account before an Instruction executes, used
// to verify account integrity after the Instruction completes // to verify account integrity after the Instruction completes
@ -155,13 +159,14 @@ impl PreAccount {
} }
pub type ProcessInstruction = fn(&Pubkey, &[KeyedAccount], &[u8]) -> Result<(), InstructionError>; pub type ProcessInstruction = fn(&Pubkey, &[KeyedAccount], &[u8]) -> Result<(), InstructionError>;
pub type SymbolCache = RwLock<HashMap<Vec<u8>, Symbol<entrypoint_native::Entrypoint>>>;
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct MessageProcessor { pub struct MessageProcessor {
#[serde(skip)] #[serde(skip)]
instruction_processors: Vec<(Pubkey, ProcessInstruction)>, instruction_processors: Vec<(Pubkey, ProcessInstruction)>,
#[serde(skip)] #[serde(skip)]
native_loader: NativeLoader, symbol_cache: SymbolCache,
} }
impl Default for MessageProcessor { impl Default for MessageProcessor {
fn default() -> Self { fn default() -> Self {
@ -172,7 +177,7 @@ impl Default for MessageProcessor {
Self { Self {
instruction_processors, instruction_processors,
native_loader: NativeLoader::default(), symbol_cache: RwLock::new(HashMap::new()),
} }
} }
} }
@ -213,10 +218,10 @@ impl MessageProcessor {
}) })
.collect(); .collect();
keyed_accounts.append(&mut keyed_accounts2); keyed_accounts.append(&mut keyed_accounts2);
assert!(keyed_accounts[0].executable()?, "account not executable");
assert!(keyed_accounts[0].executable()?, "account not executable");
let root_program_id = keyed_accounts[0].unsigned_key();
for (id, process_instruction) in &self.instruction_processors { for (id, process_instruction) in &self.instruction_processors {
let root_program_id = keyed_accounts[0].unsigned_key();
if id == root_program_id { if id == root_program_id {
return process_instruction( return process_instruction(
&root_program_id, &root_program_id,
@ -226,15 +231,12 @@ impl MessageProcessor {
} }
} }
if native_loader::check_id(&keyed_accounts[0].owner()?) { native_loader::invoke_entrypoint(
self.native_loader.process_instruction( &native_loader_id(),
&native_loader::id(), &keyed_accounts,
&keyed_accounts, &instruction.data,
&instruction.data, &self.symbol_cache,
) )
} else {
Err(InstructionError::UnsupportedProgramId)
}
} }
/// Record the initial state of the accounts so that they can be compared /// Record the initial state of the accounts so that they can be compared
@ -370,7 +372,6 @@ mod tests {
instruction::{AccountMeta, Instruction, InstructionError}, instruction::{AccountMeta, Instruction, InstructionError},
message::Message, message::Message,
native_loader::create_loadable_account, native_loader::create_loadable_account,
native_program_info,
}; };
#[test] #[test]
@ -916,9 +917,7 @@ mod tests {
accounts.push(account); accounts.push(account);
let mut loaders: Vec<Vec<(Pubkey, RefCell<Account>)>> = Vec::new(); let mut loaders: Vec<Vec<(Pubkey, RefCell<Account>)>> = Vec::new();
let account = RefCell::new(create_loadable_account(&native_program_info!( let account = RefCell::new(create_loadable_account("mock_system_program"));
"mock_system_program"
)));
loaders.push(vec![(mock_system_program_id, account)]); loaders.push(vec![(mock_system_program_id, account)]);
let from_pubkey = Pubkey::new_rand(); let from_pubkey = Pubkey::new_rand();
@ -1041,9 +1040,7 @@ mod tests {
accounts.push(account); accounts.push(account);
let mut loaders: Vec<Vec<(Pubkey, RefCell<Account>)>> = Vec::new(); let mut loaders: Vec<Vec<(Pubkey, RefCell<Account>)>> = Vec::new();
let account = RefCell::new(create_loadable_account(&native_program_info!( let account = RefCell::new(create_loadable_account("mock_system_program"));
"mock_system_program"
)));
loaders.push(vec![(mock_program_id, account)]); loaders.push(vec![(mock_program_id, account)]);
let from_pubkey = Pubkey::new_rand(); let from_pubkey = Pubkey::new_rand();

View File

@ -1,30 +1,29 @@
//! Native loader //! Native loader
use crate::message_processor::SymbolCache;
#[cfg(unix)] #[cfg(unix)]
use libloading::os::unix::*; use libloading::os::unix::*;
#[cfg(windows)] #[cfg(windows)]
use libloading::os::windows::*; use libloading::os::windows::*;
use log::*; use log::*;
use num_derive::{FromPrimitive, ToPrimitive}; use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::FromPrimitive;
use solana_sdk::{ use solana_sdk::{
account::KeyedAccount, account::KeyedAccount,
entrypoint_native::{LoaderEntrypoint, ProgramEntrypoint}, entrypoint_native,
instruction::InstructionError, instruction::InstructionError,
native_loader::Kind,
program_utils::{next_keyed_account, DecodeError}, program_utils::{next_keyed_account, DecodeError},
pubkey::Pubkey, pubkey::Pubkey,
}; };
use std::{collections::HashMap, env, path::PathBuf, str, sync::RwLock}; use std::{env, path::PathBuf, str};
use thiserror::Error; use thiserror::Error;
#[derive(Error, Debug, Serialize, Clone, PartialEq, FromPrimitive, ToPrimitive)] #[derive(Error, Debug, Serialize, Clone, PartialEq, FromPrimitive, ToPrimitive)]
pub enum NativeLoaderError { pub enum NativeLoaderError {
#[error("Entrypoint name in the account data is not a valid UTF-8 string")] #[error("Entrypoint name in the account data is not a valid UTF-8 string")]
InvalidEntrypointName = 0x0aaa_0001, InvalidEntrypointName,
#[error("Entrypoint was not found in the module")] #[error("Entrypoint was not found in the module")]
EntrypointNotFound = 0x0aaa_0002, EntrypointNotFound,
#[error("Failed to load the module")] #[error("Failed to load the module")]
FailedToLoad = 0x0aaa_0003, FailedToLoad,
} }
impl<T> DecodeError<T> for NativeLoaderError { impl<T> DecodeError<T> for NativeLoaderError {
fn type_of() -> &'static str { fn type_of() -> &'static str {
@ -34,130 +33,103 @@ impl<T> DecodeError<T> for NativeLoaderError {
/// Dynamic link library prefixes /// Dynamic link library prefixes
#[cfg(unix)] #[cfg(unix)]
const PLATFORM_FILE_PREFIX: &str = "lib"; const PLATFORM_FILE_PREFIX_NATIVE: &str = "lib";
#[cfg(windows)] #[cfg(windows)]
const PLATFORM_FILE_PREFIX: &str = ""; const PLATFORM_FILE_PREFIX_NATIVE: &str = "";
/// Dynamic link library file extension specific to the platform /// Dynamic link library file extension specific to the platform
#[cfg(any(target_os = "macos", target_os = "ios"))] #[cfg(any(target_os = "macos", target_os = "ios"))]
const PLATFORM_FILE_EXTENSION: &str = "dylib"; const PLATFORM_FILE_EXTENSION_NATIVE: &str = "dylib";
/// Dynamic link library file extension specific to the platform /// Dynamic link library file extension specific to the platform
#[cfg(all(unix, not(any(target_os = "macos", target_os = "ios"))))] #[cfg(all(unix, not(any(target_os = "macos", target_os = "ios"))))]
const PLATFORM_FILE_EXTENSION: &str = "so"; const PLATFORM_FILE_EXTENSION_NATIVE: &str = "so";
/// Dynamic link library file extension specific to the platform /// Dynamic link library file extension specific to the platform
#[cfg(windows)] #[cfg(windows)]
const PLATFORM_FILE_EXTENSION: &str = "dll"; const PLATFORM_FILE_EXTENSION_NATIVE: &str = "dll";
pub type ProgramSymbolCache = RwLock<HashMap<String, Symbol<ProgramEntrypoint>>>; fn create_path(name: &str) -> PathBuf {
pub type LoaderSymbolCache = RwLock<HashMap<String, Symbol<LoaderEntrypoint>>>; let current_exe = env::current_exe()
.unwrap_or_else(|e| panic!("create_path(\"{}\"): current exe not found: {:?}", name, e));
let current_exe_directory = PathBuf::from(current_exe.parent().unwrap_or_else(|| {
panic!(
"create_path(\"{}\"): no parent directory of {:?}",
name, current_exe,
)
}));
#[derive(Debug, Default)] let library_file_name = PathBuf::from(PLATFORM_FILE_PREFIX_NATIVE.to_string() + name)
pub struct NativeLoader { .with_extension(PLATFORM_FILE_EXTENSION_NATIVE);
program_symbol_cache: ProgramSymbolCache,
loader_symbol_cache: LoaderSymbolCache, // Check the current_exe directory for the library as `cargo tests` are run
// from the deps/ subdirectory
let file_path = current_exe_directory.join(&library_file_name);
if file_path.exists() {
file_path
} else {
// `cargo build` places dependent libraries in the deps/ subdirectory
current_exe_directory.join("deps").join(library_file_name)
}
} }
impl NativeLoader {
fn create_path(name: &str) -> PathBuf {
let current_exe = env::current_exe().unwrap_or_else(|e| {
panic!("create_path(\"{}\"): current exe not found: {:?}", name, e)
});
let current_exe_directory = PathBuf::from(current_exe.parent().unwrap_or_else(|| {
panic!(
"create_path(\"{}\"): no parent directory of {:?}",
name, current_exe,
)
}));
let library_file_name = PathBuf::from(PLATFORM_FILE_PREFIX.to_string() + name) #[cfg(windows)]
.with_extension(PLATFORM_FILE_EXTENSION); fn library_open(path: &PathBuf) -> std::io::Result<Library> {
Library::new(path)
}
// Check the current_exe directory for the library as `cargo tests` are run #[cfg(not(windows))]
// from the deps/ subdirectory fn library_open(path: &PathBuf) -> std::io::Result<Library> {
let file_path = current_exe_directory.join(&library_file_name); // Linux tls bug can cause crash on dlclose(), workaround by never unloading
if file_path.exists() { Library::open(Some(path), libc::RTLD_NODELETE | libc::RTLD_NOW)
file_path }
} else {
// `cargo build` places dependent libraries in the deps/ subdirectory pub fn invoke_entrypoint(
current_exe_directory.join("deps").join(library_file_name) _program_id: &Pubkey,
keyed_accounts: &[KeyedAccount],
instruction_data: &[u8],
symbol_cache: &SymbolCache,
) -> Result<(), InstructionError> {
let mut keyed_accounts_iter = keyed_accounts.iter();
let program = next_keyed_account(&mut keyed_accounts_iter)?;
let params = keyed_accounts_iter.as_slice();
let name_vec = &program.try_account_ref()?.data;
if let Some(entrypoint) = symbol_cache.read().unwrap().get(name_vec) {
unsafe {
return entrypoint(program.unsigned_key(), params, instruction_data);
} }
} }
let name = match str::from_utf8(name_vec) {
#[cfg(windows)] Ok(v) => v,
fn library_open(path: &PathBuf) -> std::io::Result<Library> { Err(e) => {
Library::new(path) warn!("Invalid UTF-8 sequence: {}", e);
} return Err(NativeLoaderError::InvalidEntrypointName.into());
}
#[cfg(not(windows))] };
fn library_open(path: &PathBuf) -> std::io::Result<Library> { trace!("Call native {:?}", name);
// Linux tls bug can cause crash on dlclose(), workaround by never unloading let path = create_path(&name);
Library::open(Some(path), libc::RTLD_NODELETE | libc::RTLD_NOW) match library_open(&path) {
} Ok(library) => unsafe {
let entrypoint: Symbol<entrypoint_native::Entrypoint> =
fn invoke_entrypoint<T>( match library.get(name.as_bytes()) {
name: &str, Ok(s) => s,
cache: &RwLock<HashMap<String, Symbol<T>>>, Err(e) => {
) -> Result<Symbol<T>, InstructionError> { warn!(
let mut cache = cache.write().unwrap(); "Unable to find entrypoint {:?} (error: {:?})",
if let Some(entrypoint) = cache.get(name) { name.as_bytes(),
Ok(entrypoint.clone()) e
} else { );
match Self::library_open(&Self::create_path(&name)) { return Err(NativeLoaderError::EntrypointNotFound.into());
Ok(library) => {
let result = unsafe { library.get::<T>(name.as_bytes()) };
match result {
Ok(entrypoint) => {
cache.insert(name.to_string(), entrypoint.clone());
Ok(entrypoint)
}
Err(e) => {
warn!("Unable to find program entrypoint in {:?}: {:?})", name, e);
Err(NativeLoaderError::EntrypointNotFound.into())
}
} }
} };
Err(e) => { let ret = entrypoint(program.unsigned_key(), params, instruction_data);
warn!("Failed to load: {:?}", e); symbol_cache
Err(NativeLoaderError::FailedToLoad.into()) .write()
} .unwrap()
} .insert(name_vec.to_vec(), entrypoint);
} ret
} },
Err(e) => {
pub fn process_instruction( warn!("Failed to load: {:?}", e);
&self, Err(NativeLoaderError::FailedToLoad.into())
_program_id: &Pubkey,
keyed_accounts: &[KeyedAccount],
instruction_data: &[u8],
) -> Result<(), InstructionError> {
let mut keyed_accounts_iter = keyed_accounts.iter();
let program = next_keyed_account(&mut keyed_accounts_iter)?;
let params = keyed_accounts_iter.as_slice();
let data = &program.try_account_ref()?.data;
let program_kind = FromPrimitive::from_u8(data[0]);
let name = match str::from_utf8(&data[1..]) {
Ok(v) => v,
Err(e) => {
warn!("Invalid UTF-8 sequence: {}", e);
return Err(NativeLoaderError::InvalidEntrypointName.into());
}
};
trace!("Call native {:?}: {:?}", program_kind, name);
match program_kind {
Some(Kind::Program) => {
let entrypoint =
Self::invoke_entrypoint::<ProgramEntrypoint>(name, &self.program_symbol_cache)?;
unsafe { entrypoint(program.unsigned_key(), params, instruction_data) }
}
Some(Kind::Loader) => {
let entrypoint =
Self::invoke_entrypoint::<LoaderEntrypoint>(name, &self.loader_symbol_cache)?;
unsafe { entrypoint(program.unsigned_key(), params, instruction_data) }
}
None => {
warn!("Invalid native type: {:?}", data[0]);
Err(NativeLoaderError::FailedToLoad.into())
}
} }
} }
} }

View File

@ -2,8 +2,7 @@ use solana_runtime::{
bank::Bank, bank_client::BankClient, loader_utils::create_invoke_instruction, bank::Bank, bank_client::BankClient, loader_utils::create_invoke_instruction,
}; };
use solana_sdk::{ use solana_sdk::{
client::SyncClient, genesis_config::create_genesis_config, native_program_info, pubkey::Pubkey, client::SyncClient, genesis_config::create_genesis_config, pubkey::Pubkey, signature::Signer,
signature::Signer,
}; };
#[test] #[test]
@ -13,10 +12,7 @@ fn test_program_native_noop() {
let (genesis_config, alice_keypair) = create_genesis_config(50); let (genesis_config, alice_keypair) = create_genesis_config(50);
let program_id = Pubkey::new_rand(); let program_id = Pubkey::new_rand();
let bank = Bank::new(&genesis_config); let bank = Bank::new(&genesis_config);
bank.register_native_instruction_processor( bank.register_native_instruction_processor("solana_noop_program", &program_id);
&native_program_info!("solana_noop_program"),
&program_id,
);
// Call user program // Call user program
let instruction = create_invoke_instruction(alice_keypair.pubkey(), program_id, &1u8); let instruction = create_invoke_instruction(alice_keypair.pubkey(), program_id, &1u8);

View File

@ -7,19 +7,7 @@ use crate::{account::KeyedAccount, instruction::InstructionError, pubkey::Pubkey
/// program_id: Program ID of the currently executing program /// program_id: Program ID of the currently executing program
/// keyed_accounts: Accounts passed as part of the instruction /// keyed_accounts: Accounts passed as part of the instruction
/// instruction_data: Instruction data /// instruction_data: Instruction data
pub type ProgramEntrypoint = unsafe extern "C" fn( pub type Entrypoint = unsafe extern "C" fn(
program_id: &Pubkey,
keyed_accounts: &[KeyedAccount],
instruction_data: &[u8],
) -> Result<(), InstructionError>;
// Prototype of a native loader entry point
///
/// program_id: Program ID of the currently executing program
/// keyed_accounts: Accounts passed as part of the instruction
/// instruction_data: Instruction data
/// invoke_context: Invocation context
pub type LoaderEntrypoint = unsafe extern "C" fn(
program_id: &Pubkey, program_id: &Pubkey,
keyed_accounts: &[KeyedAccount], keyed_accounts: &[KeyedAccount],
instruction_data: &[u8], instruction_data: &[u8],
@ -101,32 +89,7 @@ macro_rules! declare_program(
#[macro_export] #[macro_export]
macro_rules! $name { macro_rules! $name {
() => { () => {
(solana_sdk::native_program_info!(stringify!($name).to_string()), $crate::id()) (stringify!($name).to_string(), $crate::id())
};
}
#[no_mangle]
pub extern "C" fn $name(
program_id: &$crate::pubkey::Pubkey,
keyed_accounts: &[$crate::account::KeyedAccount],
instruction_data: &[u8],
) -> Result<(), $crate::instruction::InstructionError> {
$entrypoint(program_id, keyed_accounts, instruction_data)
}
)
);
/// Same as declare_program but for native loaders
#[macro_export]
macro_rules! declare_loader(
($bs58_string:expr, $name:ident, $entrypoint:expr) => (
$crate::declare_id!($bs58_string);
#[macro_export]
macro_rules! $name {
() => {
(solana_sdk::native_loader_info!(stringify!($name).to_string()), $crate::id())
}; };
} }

View File

@ -7,7 +7,6 @@ use crate::{
fee_calculator::FeeRateGovernor, fee_calculator::FeeRateGovernor,
hash::{hash, Hash}, hash::{hash, Hash},
inflation::Inflation, inflation::Inflation,
native_loader,
native_token::lamports_to_sol, native_token::lamports_to_sol,
poh_config::PohConfig, poh_config::PohConfig,
pubkey::Pubkey, pubkey::Pubkey,
@ -42,7 +41,7 @@ pub struct GenesisConfig {
/// initial accounts /// initial accounts
pub accounts: BTreeMap<Pubkey, Account>, pub accounts: BTreeMap<Pubkey, Account>,
/// built-in programs /// built-in programs
pub native_instruction_processors: Vec<(native_loader::Info, Pubkey)>, pub native_instruction_processors: Vec<(String, Pubkey)>,
/// accounts for network rewards, these do not count towards capitalization /// accounts for network rewards, these do not count towards capitalization
pub rewards_pools: BTreeMap<Pubkey, Account>, pub rewards_pools: BTreeMap<Pubkey, Account>,
pub ticks_per_slot: u64, pub ticks_per_slot: u64,
@ -105,7 +104,7 @@ impl Default for GenesisConfig {
impl GenesisConfig { impl GenesisConfig {
pub fn new( pub fn new(
accounts: &[(Pubkey, Account)], accounts: &[(Pubkey, Account)],
native_instruction_processors: &[(native_loader::Info, Pubkey)], native_instruction_processors: &[(String, Pubkey)],
) -> Self { ) -> Self {
Self { Self {
accounts: accounts accounts: accounts
@ -173,8 +172,8 @@ impl GenesisConfig {
self.accounts.insert(pubkey, account); self.accounts.insert(pubkey, account);
} }
pub fn add_native_instruction_processor(&mut self, processor: (native_loader::Info, Pubkey)) { pub fn add_native_instruction_processor(&mut self, name: String, program_id: Pubkey) {
self.native_instruction_processors.push(processor); self.native_instruction_processors.push((name, program_id));
} }
pub fn add_rewards_pool(&mut self, pubkey: Pubkey, account: Account) { pub fn add_rewards_pool(&mut self, pubkey: Pubkey, account: Account) {
@ -231,7 +230,6 @@ impl fmt::Display for GenesisConfig {
mod tests { mod tests {
use super::*; use super::*;
use crate::signature::{Keypair, Signer}; use crate::signature::{Keypair, Signer};
use solana_sdk::native_program_info;
use std::path::PathBuf; use std::path::PathBuf;
fn make_tmp_path(name: &str) -> PathBuf { fn make_tmp_path(name: &str) -> PathBuf {
@ -263,10 +261,7 @@ mod tests {
Account::new(10_000, 0, &Pubkey::default()), Account::new(10_000, 0, &Pubkey::default()),
); );
config.add_account(Pubkey::new_rand(), Account::new(1, 0, &Pubkey::default())); config.add_account(Pubkey::new_rand(), Account::new(1, 0, &Pubkey::default()));
config.add_native_instruction_processor(( config.add_native_instruction_processor("hi".to_string(), Pubkey::new_rand());
native_program_info!("hi".to_string()),
Pubkey::new_rand(),
));
assert_eq!(config.accounts.len(), 2); assert_eq!(config.accounts.len(), 2);
assert!(config assert!(config

View File

@ -134,10 +134,6 @@ pub enum InstructionError {
/// Executable accounts must be rent exempt /// Executable accounts must be rent exempt
#[error("executable accounts must be rent exempt")] #[error("executable accounts must be rent exempt")]
ExecutableAccountNotRentExempt, ExecutableAccountNotRentExempt,
/// Unsupported program id
#[error("Unsupported program id")]
UnsupportedProgramId,
} }
impl InstructionError { impl InstructionError {

View File

@ -1,10 +1,5 @@
use crate::{native_loader, native_program_info, pubkey::Pubkey};
crate::declare_id!("MoveLdr111111111111111111111111111111111111"); crate::declare_id!("MoveLdr111111111111111111111111111111111111");
pub fn solana_move_loader_program() -> (native_loader::Info, Pubkey) { pub fn solana_move_loader_program() -> (String, crate::pubkey::Pubkey) {
( ("solana_move_loader_program".to_string(), id())
native_program_info!("solana_move_loader_program".to_string()),
id(),
)
} }

View File

@ -1,46 +1,13 @@
use crate::{account::Account, hash::Hash}; use crate::{account::Account, hash::Hash};
use num_derive::FromPrimitive;
crate::declare_id!("NativeLoader1111111111111111111111111111111"); crate::declare_id!("NativeLoader1111111111111111111111111111111");
#[derive(Debug, Clone, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct Info {
pub kind: Kind,
pub name: String,
}
#[derive(Debug, Clone, Copy, Deserialize, Eq, FromPrimitive, Hash, PartialEq, Serialize)]
pub enum Kind {
Program = 1,
Loader = 2,
}
#[macro_export]
macro_rules! native_program_info(
($name:expr) => (
$crate::native_loader::Info {
kind: $crate::native_loader::Kind::Program,
name: $name.to_string(),
}
)
);
#[macro_export]
macro_rules! native_loader_info(
($name:expr) => (
$crate::native_loader::Info {
kind: $crate::native_loader::Kind::Loader,
name: $name.to_string(),
}
)
);
/// Create an executable account with the given shared object name. /// Create an executable account with the given shared object name.
pub fn create_loadable_account(info: &Info) -> Account { pub fn create_loadable_account(name: &str) -> Account {
let mut data = vec![info.kind as u8];
data.extend_from_slice(info.name.as_bytes());
Account { Account {
lamports: 1, lamports: 1,
owner: id(), owner: id(),
data, data: name.as_bytes().to_vec(),
executable: true, executable: true,
rent_epoch: 0, rent_epoch: 0,
hash: Hash::default(), hash: Hash::default(),

View File

@ -1,10 +1,5 @@
use crate::{native_loader, native_program_info, pubkey::Pubkey};
crate::declare_id!("11111111111111111111111111111111"); crate::declare_id!("11111111111111111111111111111111");
pub fn solana_system_program() -> (native_loader::Info, Pubkey) { pub fn solana_system_program() -> (String, crate::pubkey::Pubkey) {
( ("solana_system_program".to_string(), id())
native_program_info!("solana_system_program".to_string()),
id(),
)
} }