Fix undefined symbol "custom panic" in BPF test programs (#13840)

* Implements missing "custom_panic" symbol
This commit is contained in:
Alexander Meißner
2020-12-01 16:52:20 +01:00
committed by GitHub
parent 83aaf18d6e
commit aebc3a17ce
11 changed files with 62 additions and 24 deletions

View File

@ -1,7 +1,7 @@
//! @brief Example Rust-based BPF program tests loop iteration //! @brief Example Rust-based BPF program tests loop iteration
extern crate solana_program; extern crate solana_program;
use solana_program::entrypoint::SUCCESS; use solana_program::{custom_panic_default, entrypoint::SUCCESS};
#[no_mangle] #[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> u64 { pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
@ -50,6 +50,8 @@ pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
SUCCESS SUCCESS
} }
custom_panic_default!();
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;

View File

@ -2,7 +2,7 @@
#[macro_use] #[macro_use]
extern crate alloc; extern crate alloc;
use solana_program::{entrypoint::SUCCESS, msg}; use solana_program::{custom_panic_default, entrypoint::SUCCESS, msg};
use std::{alloc::Layout, mem}; use std::{alloc::Layout, mem};
#[no_mangle] #[no_mangle]
@ -81,6 +81,8 @@ pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
SUCCESS SUCCESS
} }
custom_panic_default!();
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;

View File

@ -1,6 +1,6 @@
//! @brief Example Rust-based BPF program that tests call depth and stack usage //! @brief Example Rust-based BPF program that tests call depth and stack usage
use solana_program::{entrypoint::SUCCESS, msg}; use solana_program::{custom_panic_default, entrypoint::SUCCESS, msg};
#[inline(never)] #[inline(never)]
pub fn recurse(data: &mut [u8]) { pub fn recurse(data: &mut [u8]) {
@ -25,3 +25,5 @@ pub unsafe extern "C" fn entrypoint(input: *mut u8) -> u64 {
recurse(&mut data); recurse(&mut data);
SUCCESS SUCCESS
} }
custom_panic_default!();

View File

@ -20,6 +20,12 @@ fn return_sstruct() -> SStruct {
SStruct { x: 1, y: 2, z: 3 } SStruct { x: 1, y: 2, z: 3 }
} }
#[no_mangle]
fn custom_panic(info: &core::panic::PanicInfo<'_>) {
// Full panic reporting
msg!(&format!("{}", info));
}
entrypoint_deprecated!(process_instruction); entrypoint_deprecated!(process_instruction);
fn process_instruction( fn process_instruction(
program_id: &Pubkey, program_id: &Pubkey,

View File

@ -1,7 +1,7 @@
//! @brief Example Rust-based BPF program tests loop iteration //! @brief Example Rust-based BPF program tests loop iteration
extern crate solana_program; extern crate solana_program;
use solana_program::{entrypoint::SUCCESS, msg}; use solana_program::{custom_panic_default, entrypoint::SUCCESS, msg};
#[no_mangle] #[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> u64 { pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
@ -18,6 +18,8 @@ pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
SUCCESS SUCCESS
} }
custom_panic_default!();
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;

View File

@ -2,7 +2,7 @@
mod helper; mod helper;
extern crate solana_program; extern crate solana_program;
use solana_program::{entrypoint::SUCCESS, msg}; use solana_program::{custom_panic_default, entrypoint::SUCCESS, msg};
#[no_mangle] #[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> u64 { pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
@ -26,6 +26,8 @@ pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
SUCCESS SUCCESS
} }
custom_panic_default!();
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;

View File

@ -4,7 +4,7 @@
#![feature(compiler_builtins_lib)] #![feature(compiler_builtins_lib)]
extern crate compiler_builtins; extern crate compiler_builtins;
use solana_program::entrypoint::SUCCESS; use solana_program::{custom_panic_default, entrypoint::SUCCESS, info};
#[no_mangle] #[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> u64 { pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
@ -183,3 +183,5 @@ pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
SUCCESS SUCCESS
} }
custom_panic_default!();

View File

@ -2,7 +2,7 @@
extern crate solana_program; extern crate solana_program;
use solana_bpf_rust_param_passing_dep::{Data, TestDep}; use solana_bpf_rust_param_passing_dep::{Data, TestDep};
use solana_program::{entrypoint::SUCCESS, msg}; use solana_program::{custom_panic_default, entrypoint::SUCCESS, msg};
#[no_mangle] #[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> u64 { pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
@ -23,6 +23,8 @@ pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
SUCCESS SUCCESS
} }
custom_panic_default!();
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;

View File

@ -2,6 +2,7 @@
extern crate solana_program; extern crate solana_program;
use solana_program::{ use solana_program::{
custom_panic_default,
hash::{hashv, Hasher}, hash::{hashv, Hasher},
msg, msg,
}; };
@ -22,6 +23,8 @@ pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
0 0
} }
custom_panic_default!();
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;

View File

@ -897,9 +897,9 @@ fn assert_instruction_count() {
{ {
programs.extend_from_slice(&[ programs.extend_from_slice(&[
("solana_bpf_rust_128bit", 572), ("solana_bpf_rust_128bit", 572),
("solana_bpf_rust_alloc", 12777), ("solana_bpf_rust_alloc", 12919),
("solana_bpf_rust_dep_crate", 2), ("solana_bpf_rust_dep_crate", 2),
("solana_bpf_rust_external_spend", 538), ("solana_bpf_rust_external_spend", 514),
("solana_bpf_rust_iter", 724), ("solana_bpf_rust_iter", 724),
("solana_bpf_rust_many_args", 237), ("solana_bpf_rust_many_args", 237),
("solana_bpf_rust_noop", 488), ("solana_bpf_rust_noop", 488),

View File

@ -38,23 +38,49 @@ pub const HEAP_LENGTH: usize = 32 * 1024;
/// Deserialize the program input arguments and call the user defined /// Deserialize the program input arguments and call the user defined
/// `process_instruction` function. Users must call this macro otherwise an /// `process_instruction` function. Users must call this macro otherwise an
/// entry point for their program will not be created. /// entry point for their program will not be created.
///
/// If the program defines the feature `custom-heap` then the default heap
/// implementation will not be included and the program is free to implement
/// their own `#[global_allocator]`
#[macro_export] #[macro_export]
macro_rules! entrypoint { macro_rules! entrypoint {
($process_instruction:ident) => { ($process_instruction:ident) => {
/// # Safety
#[no_mangle]
pub unsafe extern "C" fn entrypoint(input: *mut u8) -> u64 {
let (program_id, accounts, instruction_data) =
unsafe { $crate::entrypoint::deserialize(input) };
match $process_instruction(&program_id, &accounts, &instruction_data) {
Ok(()) => $crate::entrypoint::SUCCESS,
Err(error) => error.into(),
}
}
$crate::custom_heap_default!();
$crate::custom_panic_default!();
};
}
/// Fallback to default for unused custom heap feature.
#[macro_export]
macro_rules! custom_heap_default {
() => {
/// A program can provide their own custom heap implementation by adding /// A program can provide their own custom heap implementation by adding
/// a `custom-heap` feature to `Cargo.toml` and implementing their own /// a `custom-heap` feature to `Cargo.toml` and implementing their own
/// `global_allocator`. /// `global_allocator`.
///
/// If the program defines the feature `custom-heap` then the default heap
/// implementation will not be included and the program is free to implement
/// their own `#[global_allocator]`
#[cfg(all(not(feature = "custom-heap"), target_arch = "bpf"))] #[cfg(all(not(feature = "custom-heap"), target_arch = "bpf"))]
#[global_allocator] #[global_allocator]
static A: $crate::entrypoint::BumpAllocator = $crate::entrypoint::BumpAllocator { static A: $crate::entrypoint::BumpAllocator = $crate::entrypoint::BumpAllocator {
start: $crate::entrypoint::HEAP_START_ADDRESS, start: $crate::entrypoint::HEAP_START_ADDRESS,
len: $crate::entrypoint::HEAP_LENGTH, len: $crate::entrypoint::HEAP_LENGTH,
}; };
};
}
/// Fallback to default for unused custom panic feature.
/// This must be used if the entrypoint! macro is not used.
#[macro_export]
macro_rules! custom_panic_default {
() => {
/// A program can provide their own custom panic implementation by /// A program can provide their own custom panic implementation by
/// adding a `custom-panic` feature to `Cargo.toml` and implementing /// adding a `custom-panic` feature to `Cargo.toml` and implementing
/// their own `custom_panic`. /// their own `custom_panic`.
@ -69,17 +95,6 @@ macro_rules! entrypoint {
// Full panic reporting // Full panic reporting
$crate::msg!("{}", info); $crate::msg!("{}", info);
} }
/// # Safety
#[no_mangle]
pub unsafe extern "C" fn entrypoint(input: *mut u8) -> u64 {
let (program_id, accounts, instruction_data) =
unsafe { $crate::entrypoint::deserialize(input) };
match $process_instruction(&program_id, &accounts, &instruction_data) {
Ok(()) => $crate::entrypoint::SUCCESS,
Err(error) => error.into(),
}
}
}; };
} }