Split out Rust BPF no-std stuff (#4968)

This commit is contained in:
Jack May
2019-07-08 20:28:05 -08:00
committed by GitHub
parent 49250f62aa
commit f9a2254688
35 changed files with 164 additions and 217 deletions

3
sdk/bpf/rust/rust-no-std/.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
/target/
Cargo.lock

View File

@@ -0,0 +1,16 @@
[package]
name = "solana-sdk-bpf-no-std"
version = "0.17.0"
description = "Solana BPF SDK Rust no_std support"
authors = ["Solana Maintainers <maintainers@solana.com>"]
repository = "https://github.com/solana-labs/solana"
license = "Apache-2.0"
homepage = "https://solana.com/"
edition = "2018"
[dependencies]
solana-sdk-bpf-utils = { path = "../rust-utils" }
[workspace]
members = []

View File

@@ -0,0 +1,23 @@
//! @brief Solana Rust-based BPF program memory allocator shim
use core::alloc::{GlobalAlloc, Layout};
use solana_sdk_bpf_utils::log::*;
pub struct Allocator;
unsafe impl GlobalAlloc for Allocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
sol_alloc_free_(layout.size() as u64, 0)
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
sol_alloc_free_(layout.size() as u64, ptr as u64);
}
}
extern "C" {
fn sol_alloc_free_(size: u64, ptr: u64) -> *mut u8;
}
#[alloc_error_handler]
fn my_alloc_error_handler(_: core::alloc::Layout) -> ! {
sol_log("alloc_error_handler");
panic!();
}

View File

@@ -0,0 +1,20 @@
//! @brief Solana Rust-based BPF program utility functions and types
#![no_std]
#![feature(allocator_api)]
#![feature(alloc_error_handler)]
#![feature(panic_info_message)]
#![feature(compiler_builtins_lib)]
#![feature(lang_items)]
#![cfg(not(test))]
#[lang = "eh_personality"]
extern "C" fn eh_personality() {}
extern crate compiler_builtins;
pub mod allocator;
pub mod panic;
#[global_allocator]
static A: allocator::Allocator = allocator::Allocator;

View File

@@ -0,0 +1,31 @@
//! @brief Solana Rust-based BPF program panic handling
use core::panic::PanicInfo;
use core::ptr;
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
// Message is ignored for now to avoid incurring formatting program size overhead
match info.location() {
Some(location) => {
let mut file: [u8; 128] = [0; 128];
for (i, c) in location.file().as_bytes().iter().enumerate() {
if i > 127 {
break;
}
file[i] = *c;
}
unsafe {
sol_panic_(
file.as_ptr(),
u64::from(location.line()),
u64::from(location.column()),
);
}
}
None => unsafe { sol_panic_(ptr::null(), 0, 0) },
}
}
extern "C" {
pub fn sol_panic_(file: *const u8, line: u64, column: u64) -> !;
}