Add support for BPF program custom errors (#5743)

* Add support for BPF program custom errors

* Rename SOL_SUCCESS -> SUCCESS
This commit is contained in:
Justin Starry
2019-09-06 16:05:01 -07:00
committed by GitHub
parent d3052d094c
commit 81c36699c4
26 changed files with 108 additions and 67 deletions

View File

@@ -1,10 +1,11 @@
//! @brief Example Rust-based BPF program tests loop iteration
extern crate solana_sdk;
use solana_sdk::entrypoint::SUCCESS;
use solana_sdk::info;
#[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
pub extern "C" fn entrypoint(_input: *mut u8) -> u32 {
let x: u128 = 1;
let y = x.rotate_right(1);
assert_eq!(y, 170_141_183_460_469_231_731_687_303_715_884_105_728);
@@ -48,5 +49,5 @@ pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
assert_eq!(x, 0x0001_ffff_ffff_ffff_fffe);
info!("Success");
true
SUCCESS
}

View File

@@ -3,12 +3,13 @@
#[macro_use]
extern crate alloc;
extern crate solana_sdk;
use solana_sdk::entrypoint::SUCCESS;
use solana_sdk::info;
use std::alloc::Layout;
use std::mem;
#[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
pub extern "C" fn entrypoint(_input: *mut u8) -> u32 {
unsafe {
// Confirm large allocation fails
@@ -100,5 +101,5 @@ pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
}
info!("Success");
true
SUCCESS
}

View File

@@ -2,10 +2,11 @@
extern crate solana_sdk;
use byteorder::{ByteOrder, LittleEndian};
use solana_sdk::entrypoint::SUCCESS;
use solana_sdk::info;
#[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
pub extern "C" fn entrypoint(_input: *mut u8) -> u32 {
let mut buf = [0; 4];
LittleEndian::write_u32(&mut buf, 1_000_000);
assert_eq!(1_000_000, LittleEndian::read_u32(&buf));
@@ -15,5 +16,5 @@ pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
assert_eq!(-5_000, LittleEndian::read_i16(&buf));
info!("Success");
true
SUCCESS
}

View File

@@ -6,11 +6,10 @@ use solana_sdk::entrypoint::*;
use solana_sdk::pubkey::Pubkey;
entrypoint!(process_instruction);
fn process_instruction(_program_id: &Pubkey, ka: &mut [SolKeyedAccount], _data: &[u8]) -> bool {
fn process_instruction(_program_id: &Pubkey, ka: &mut [SolKeyedAccount], _data: &[u8]) -> u32 {
// account 0 is the mint and not owned by this program, any debit of its lamports
// should result in a failed program execution. Test to ensure that this debit
// is seen by the runtime and fails as expected
*ka[0].lamports -= 1;
true
SUCCESS
}

View File

@@ -1,10 +1,11 @@
//! @brief Example Rust-based BPF program tests loop iteration
extern crate solana_sdk;
use solana_sdk::entrypoint::SUCCESS;
use solana_sdk::info;
#[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
pub extern "C" fn entrypoint(_input: *mut u8) -> u32 {
const ITERS: usize = 100;
let ones = [1_u64; ITERS];
let mut sum: u64 = 0;
@@ -16,5 +17,5 @@ pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
assert_eq!(sum, ITERS as u64);
info!("Success");
true
SUCCESS
}

View File

@@ -2,10 +2,11 @@
mod helper;
extern crate solana_sdk;
use solana_sdk::entrypoint::SUCCESS;
use solana_sdk::info;
#[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
pub extern "C" fn entrypoint(_input: *mut u8) -> u32 {
info!("Call same package");
assert_eq!(crate::helper::many_args(1, 2, 3, 4, 5, 6, 7, 8, 9), 45);
@@ -24,5 +25,5 @@ pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
);
info!("Success");
true
SUCCESS
}

View File

@@ -21,7 +21,7 @@ fn return_sstruct() -> SStruct {
}
entrypoint!(process_instruction);
fn process_instruction(program_id: &Pubkey, ka: &mut [SolKeyedAccount], data: &[u8]) -> bool {
fn process_instruction(program_id: &Pubkey, ka: &mut [SolKeyedAccount], data: &[u8]) -> u32 {
info!("Program identifier:");
program_id.log();
@@ -56,7 +56,7 @@ fn process_instruction(program_id: &Pubkey, ka: &mut [SolKeyedAccount], data: &[
}
info!("Success");
true
SUCCESS
}
#[cfg(test)]

View File

@@ -3,6 +3,6 @@
extern crate solana_sdk;
#[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
pub extern "C" fn entrypoint(_input: *mut u8) -> u32 {
panic!();
}

View File

@@ -2,10 +2,11 @@
extern crate solana_sdk;
use solana_bpf_rust_param_passing_dep::{Data, TestDep};
use solana_sdk::entrypoint::SUCCESS;
use solana_sdk::info;
#[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
pub extern "C" fn entrypoint(_input: *mut u8) -> u32 {
let array = [0xA, 0xB, 0xC, 0xD, 0xE, 0xF];
let data = Data {
twentyone: 21u64,
@@ -18,5 +19,6 @@ pub extern "C" fn entrypoint(_input: *mut u8) -> bool {
let test_dep = TestDep::new(&data, 1, 2, 3, 4, 5);
info!(0, 0, 0, 0, test_dep.thirty);
test_dep.thirty == 30
assert!(test_dep.thirty == 30);
SUCCESS
}

View File

@@ -7,10 +7,10 @@ use solana_sdk::pubkey::Pubkey;
use solana_sdk::{entrypoint, info};
entrypoint!(process_instruction);
fn process_instruction(_program_id: &Pubkey, ka: &mut [SolKeyedAccount], _data: &[u8]) -> bool {
fn process_instruction(_program_id: &Pubkey, ka: &mut [SolKeyedAccount], _data: &[u8]) -> u32 {
let tick_height = LittleEndian::read_u64(ka[2].data);
assert_eq!(10u64, tick_height);
info!("Success");
true
SUCCESS
}