Merge pull request #300 from devrandom/alloc

New alloc feature
This commit is contained in:
Elichai Turkel 2021-06-09 11:09:07 +03:00 committed by GitHub
commit 65a2b4f3cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 5 deletions

View File

@ -20,6 +20,8 @@ features = [ "rand", "rand-std", "serde", "recovery" ]
unstable = ["recovery", "rand-std"] unstable = ["recovery", "rand-std"]
default = ["std"] default = ["std"]
std = ["secp256k1-sys/std"] std = ["secp256k1-sys/std"]
# allow use of Secp256k1::new and related API that requires an allocator
alloc = []
rand-std = ["rand/std"] rand-std = ["rand/std"]
recovery = ["secp256k1-sys/recovery"] recovery = ["secp256k1-sys/recovery"]
lowmemory = ["secp256k1-sys/lowmemory"] lowmemory = ["secp256k1-sys/lowmemory"]

View File

@ -76,6 +76,7 @@ if [ "$DO_ASAN" = true ]; then
RUSTFLAGS='-Zsanitizer=memory -Zsanitizer-memory-track-origins -Cforce-frame-pointers=yes' \ RUSTFLAGS='-Zsanitizer=memory -Zsanitizer-memory-track-origins -Cforce-frame-pointers=yes' \
cargo test --lib --all --features="$FEATURES" -Zbuild-std --target x86_64-unknown-linux-gnu && cargo test --lib --all --features="$FEATURES" -Zbuild-std --target x86_64-unknown-linux-gnu &&
cargo run --release --manifest-path=./no_std_test/Cargo.toml | grep -q "Verified Successfully" cargo run --release --manifest-path=./no_std_test/Cargo.toml | grep -q "Verified Successfully"
cargo run --release --features=alloc --manifest-path=./no_std_test/Cargo.toml | grep -q "Verified alloc Successfully"
fi fi
# Test if panic in C code aborts the process (either with a real panic or with SIGILL) # Test if panic in C code aborts the process (either with a real panic or with SIGILL)

View File

@ -3,7 +3,11 @@ name = "no_std_test"
version = "0.1.0" version = "0.1.0"
authors = ["Elichai Turkel <elichai.turkel@gmail.com>"] authors = ["Elichai Turkel <elichai.turkel@gmail.com>"]
[features]
alloc = ["secp256k1/alloc", "wee_alloc"]
[dependencies] [dependencies]
wee_alloc = { version = "0.4.5", optional = true }
secp256k1 = { path = "../", default-features = false, features = ["serde", "rand", "recovery"] } secp256k1 = { path = "../", default-features = false, features = ["serde", "rand", "recovery"] }
libc = { version = "0.2", default-features = false } libc = { version = "0.2", default-features = false }
serde_cbor = { version = "0.10", default-features = false } # A random serializer that supports no-std. serde_cbor = { version = "0.10", default-features = false } # A random serializer that supports no-std.

View File

@ -43,11 +43,24 @@
#![feature(start)] #![feature(start)]
#![feature(core_intrinsics)] #![feature(core_intrinsics)]
#![feature(panic_info_message)] #![feature(panic_info_message)]
#![feature(alloc_error_handler)]
#![no_std] #![no_std]
extern crate libc; extern crate libc;
extern crate secp256k1; extern crate secp256k1;
extern crate serde_cbor; extern crate serde_cbor;
#[cfg(feature = "alloc")]
extern crate alloc;
use core::alloc::Layout;
#[cfg(feature = "alloc")]
extern crate wee_alloc;
#[cfg(feature = "alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
use core::fmt::{self, write, Write}; use core::fmt::{self, write, Write};
use core::intrinsics; use core::intrinsics;
use core::panic::PanicInfo; use core::panic::PanicInfo;
@ -120,6 +133,17 @@ fn start(_argc: isize, _argv: *const *const u8) -> isize {
assert_ne!(x_arr, [0u8; 32]); assert_ne!(x_arr, [0u8; 32]);
assert_ne!(&y_arr[..], &[0u8; 32][..]); assert_ne!(&y_arr[..], &[0u8; 32][..]);
#[cfg(feature = "alloc")]
{
let secp_alloc = Secp256k1::new();
let public_key = PublicKey::from_secret_key(&secp_alloc, &secret_key);
let message = Message::from_slice(&[0xab; 32]).expect("32 bytes");
let sig = secp_alloc.sign(&message, &secret_key);
assert!(secp_alloc.verify(&message, &sig, &public_key).is_ok());
unsafe { libc::printf("Verified alloc Successfully!\n\0".as_ptr() as _) };
}
unsafe { libc::printf("Verified Successfully!\n\0".as_ptr() as _) }; unsafe { libc::printf("Verified Successfully!\n\0".as_ptr() as _) };
0 0
} }
@ -171,3 +195,9 @@ fn panic(info: &PanicInfo) -> ! {
buf.print(); buf.print();
intrinsics::abort() intrinsics::abort()
} }
#[alloc_error_handler]
fn alloc_error(_layout: Layout) -> ! {
unsafe { libc::printf("alloc shi1\n\0".as_ptr() as _) };
intrinsics::abort()
}

View File

@ -5,8 +5,8 @@ use ffi::types::{c_uint, c_void};
use Error; use Error;
use Secp256k1; use Secp256k1;
#[cfg(feature = "std")] #[cfg(any(feature = "std", feature = "alloc"))]
pub use self::std_only::*; pub use self::alloc_only::*;
#[cfg(feature = "global-context-less-secure")] #[cfg(feature = "global-context-less-secure")]
/// Module implementing a singleton pattern for a global `Secp256k1` context /// Module implementing a singleton pattern for a global `Secp256k1` context
@ -93,14 +93,18 @@ mod private {
impl<'buf> Sealed for SignOnlyPreallocated<'buf> {} impl<'buf> Sealed for SignOnlyPreallocated<'buf> {}
} }
#[cfg(feature = "std")] #[cfg(any(feature = "std", feature = "alloc"))]
mod std_only { mod alloc_only {
#[cfg(feature = "std")]
use std::alloc;
#[cfg(not(feature = "std"))]
use alloc::alloc;
impl private::Sealed for SignOnly {} impl private::Sealed for SignOnly {}
impl private::Sealed for All {} impl private::Sealed for All {}
impl private::Sealed for VerifyOnly {} impl private::Sealed for VerifyOnly {}
use super::*; use super::*;
use std::alloc;
const ALIGN_TO: usize = mem::align_of::<AlignedType>(); const ALIGN_TO: usize = mem::align_of::<AlignedType>();
/// Represents the set of capabilities needed for signing. /// Represents the set of capabilities needed for signing.

View File

@ -135,6 +135,7 @@ pub use secp256k1_sys as ffi;
#[cfg(any(test, feature = "rand"))] use rand::Rng; #[cfg(any(test, feature = "rand"))] use rand::Rng;
#[cfg(any(test, feature = "std"))] extern crate core; #[cfg(any(test, feature = "std"))] extern crate core;
#[cfg(all(test, target_arch = "wasm32"))] extern crate wasm_bindgen_test; #[cfg(all(test, target_arch = "wasm32"))] extern crate wasm_bindgen_test;
#[cfg(feature = "alloc")] extern crate alloc;
use core::{fmt, ptr, str}; use core::{fmt, ptr, str};