Skip to content

Commit

Permalink
Handle dynamic program dlopen failures gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
mvines committed Oct 13, 2018
1 parent 5ffd84c commit 1a42f18
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/dynamic_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use libc;
use libloading::os::unix::*;
#[cfg(windows)]
use libloading::os::windows::*;
use result::Result;

use solana_program_interface::account::KeyedAccount;
use solana_program_interface::pubkey::Pubkey;
Expand Down Expand Up @@ -80,12 +81,12 @@ pub enum DynamicProgram {
}

impl DynamicProgram {
pub fn new_native(name: String) -> Self {
pub fn new_native(name: String) -> Result<Self> {
// create native program
let path = ProgramPath::Native {}.create(&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();
DynamicProgram::Native { name, library }
let library = Library::open(Some(path), libc::RTLD_NODELETE | libc::RTLD_NOW)?;
Ok(DynamicProgram::Native { name, library })
}

pub fn new_bpf_from_file(name: String) -> Self {
Expand Down
8 changes: 7 additions & 1 deletion src/system_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,13 @@ impl SystemProgram {
}
SystemProgram::Load { program_id, name } => {
let mut hashmap = loaded_programs.write().unwrap();
hashmap.insert(program_id, DynamicProgram::new_native(name));
hashmap.insert(
program_id,
DynamicProgram::new_native(name).map_err(|err| {
warn!("SystemProgram::Load failure: {:?}", err);
Error::InvalidArgument
})?,
);
}
}
Ok(())
Expand Down
8 changes: 4 additions & 4 deletions tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ fn test_native_file_noop() {
.map(|(key, account)| KeyedAccount { key, account })
.collect();

let dp = DynamicProgram::new_native("noop".to_string());
let dp = DynamicProgram::new_native("noop".to_string()).unwrap();
dp.call(&mut infos, &data);
}
}
Expand All @@ -163,7 +163,7 @@ fn test_native_file_move_funds_success() {
.map(|(key, account)| KeyedAccount { key, account })
.collect();

let dp = DynamicProgram::new_native("move_funds".to_string());
let dp = DynamicProgram::new_native("move_funds".to_string()).unwrap();
dp.call(&mut infos, &data);
}
assert_eq!(0, accounts[0].tokens);
Expand All @@ -186,7 +186,7 @@ fn test_native_file_move_funds_insufficient_funds() {
.map(|(key, account)| KeyedAccount { key, account })
.collect();

let dp = DynamicProgram::new_native("move_funds".to_string());
let dp = DynamicProgram::new_native("move_funds".to_string()).unwrap();
dp.call(&mut infos, &data);
}
assert_eq!(10, accounts[0].tokens);
Expand Down Expand Up @@ -216,7 +216,7 @@ fn test_program_native_move_funds_succes_many_threads() {
.map(|(key, account)| KeyedAccount { key, account })
.collect();

let dp = DynamicProgram::new_native("move_funds".to_string());
let dp = DynamicProgram::new_native("move_funds".to_string()).unwrap();
dp.call(&mut infos, &data);
}
assert_eq!(0, accounts[0].tokens);
Expand Down

0 comments on commit 1a42f18

Please sign in to comment.