Programs were not spawned by SystemProgram (#1533)

* SystemProgram spawns programs
This commit is contained in:
jackcmay
2018-10-18 10:33:30 -07:00
committed by GitHub
parent 57a717056e
commit 0a819ec4e2
7 changed files with 65 additions and 50 deletions

View File

@@ -486,13 +486,10 @@ impl Bank {
account: &Account,
) -> Result<()> {
// Verify the transaction
// make sure that program_id is still the same or this was just assigned by the system call contract
if !((*pre_program_id == account.program_id)
|| (SystemProgram::check_id(&tx_program_id)
&& SystemProgram::check_id(&pre_program_id)))
{
//TODO, this maybe redundant bpf should be able to guarantee this property
// return Err(BankError::ModifiedContractId(instruction_index as u8));
// Make sure that program_id is still the same or this was just assigned by the system call contract
if *pre_program_id != account.program_id && !SystemProgram::check_id(&tx_program_id) {
return Err(BankError::ModifiedContractId(instruction_index as u8));
}
// For accounts unassigned to the contract, the individual balance of each accounts cannot decrease.
if *tx_program_id != account.program_id && pre_tokens > account.tokens {

View File

@@ -65,21 +65,18 @@ pub fn process_transaction(keyed_accounts: &mut [KeyedAccount], tx_data: &[u8])
}
};
trace!("Call native {:?}", name);
{
// create native program
let path = create_path(&name);
// TODO linux tls bug can cause crash on dlclose(), workaround by never unloading
let library = Library::open(Some(path), libc::RTLD_NODELETE | libc::RTLD_NOW).unwrap();
unsafe {
let entrypoint: Symbol<Entrypoint> = match library.get(ENTRYPOINT.as_bytes()) {
Ok(s) => s,
Err(e) => {
warn!("{:?}: Unable to find {:?} in program", e, ENTRYPOINT);
return false;
}
};
return entrypoint(&mut keyed_accounts[1..], tx_data);
}
let path = create_path(&name);
// TODO linux tls bug can cause crash on dlclose(), workaround by never unloading
let library = Library::open(Some(path), libc::RTLD_NODELETE | libc::RTLD_NOW).unwrap();
unsafe {
let entrypoint: Symbol<Entrypoint> = match library.get(ENTRYPOINT.as_bytes()) {
Ok(s) => s,
Err(e) => {
warn!("{:?}: Unable to find {:?} in program", e, ENTRYPOINT);
return false;
}
};
return entrypoint(&mut keyed_accounts[1..], tx_data);
}
} else if let Ok(instruction) = deserialize(tx_data) {
match instruction {
@@ -96,23 +93,15 @@ pub fn process_transaction(keyed_accounts: &mut [KeyedAccount], tx_data: &[u8])
}
// native loader takes a name and we assume it all comes in at once
keyed_accounts[0].account.userdata = bytes;
return true;
}
LoaderInstruction::Finalize => {
keyed_accounts[0].account.executable = true;
keyed_accounts[0].account.loader_program_id = id();
keyed_accounts[0].account.program_id = *keyed_accounts[0].key;
trace!(
"NativeLoader::Finalize prog: {:?} loader {:?}",
keyed_accounts[0].account.program_id,
keyed_accounts[0].account.loader_program_id
);
return true;
trace!("NativeLoader::Finalize prog: {:?}", keyed_accounts[0].key);
}
}
} else {
warn!("Invalid program transaction: {:?}", tx_data);
}
false
true
}

View File

@@ -9,6 +9,8 @@ use transaction::Transaction;
#[derive(Debug)]
pub enum Error {
InvalidArgument,
AssignOfUnownedAccount,
AccountNotFinalized,
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
@@ -39,6 +41,9 @@ pub enum SystemProgram {
/// * Transaction::keys[0] - source
/// * Transaction::keys[1] - destination
Move { tokens: i64 },
/// Spawn a new program from an account
Spawn,
}
pub const SYSTEM_PROGRAM_ID: [u8; 32] = [0u8; 32];
@@ -85,7 +90,7 @@ impl SystemProgram {
}
SystemProgram::Assign { program_id } => {
if !Self::check_id(&accounts[0].program_id) {
Err(Error::InvalidArgument)?;
Err(Error::AssignOfUnownedAccount)?;
}
accounts[0].program_id = program_id;
}
@@ -94,6 +99,14 @@ impl SystemProgram {
accounts[0].tokens -= tokens;
accounts[1].tokens += tokens;
}
SystemProgram::Spawn => {
if !accounts[0].executable || accounts[0].loader_program_id != Pubkey::default()
{
Err(Error::AccountNotFinalized)?;
}
accounts[0].loader_program_id = accounts[0].program_id;
accounts[0].program_id = tx.account_keys[0];
}
}
Ok(())
} else {

View File

@@ -36,6 +36,8 @@ pub trait SystemTransaction {
last_id: Hash,
fee: i64,
) -> Self;
fn system_spawn(from_keypair: &Keypair, last_id: Hash, fee: i64) -> Self;
}
impl SystemTransaction for Transaction {
@@ -100,7 +102,7 @@ impl SystemTransaction for Transaction {
fee,
)
}
/// Create and sign new SystemProgram::Move transaction to many destinations
fn system_move_many(from: &Keypair, moves: &[(Pubkey, i64)], last_id: Hash, fee: i64) -> Self {
let instructions: Vec<_> = moves
.iter()
@@ -124,6 +126,19 @@ impl SystemTransaction for Transaction {
instructions,
)
}
/// Create and sign new SystemProgram::Spawn transaction
fn system_spawn(from_keypair: &Keypair, last_id: Hash, fee: i64) -> Self {
let spawn = SystemProgram::Spawn;
let userdata = serialize(&spawn).unwrap();
Transaction::new(
from_keypair,
&[],
SystemProgram::id(),
userdata,
last_id,
fee,
)
}
}
pub fn test_tx() -> Transaction {