-
Notifications
You must be signed in to change notification settings - Fork 281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented context create/destroy in rust #130
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
//! # FFI bindings | ||
//! Direct bindings to the underlying C library functions. These should | ||
//! not be needed for most users. | ||
use core::{mem, hash}; | ||
use core::{mem, hash, slice}; | ||
use types::*; | ||
|
||
/// Flag for context to enable no precomputation | ||
|
@@ -260,6 +260,50 @@ extern "C" { | |
} | ||
|
||
|
||
#[cfg(feature = "std")] | ||
#[no_mangle] | ||
/// A reimplementation of the C function `secp256k1_context_create` in rust. | ||
/// | ||
/// This function allocates memory, the pointer should be deallocated using `secp256k1_context_destroy` | ||
/// A failure to do so will result in a memory leak. | ||
/// | ||
/// This will create a secp256k1 raw context. | ||
// Returns: a newly created context object. | ||
// In: flags: which parts of the context to initialize. | ||
pub unsafe extern "C" fn secp256k1_context_create(flags: c_uint) -> *mut Context { | ||
assert!(mem::align_of::<usize>() >= mem::align_of::<u8>()); | ||
assert_eq!(mem::size_of::<usize>(), mem::size_of::<&usize>()); | ||
|
||
let word_size = mem::size_of::<usize>(); | ||
let n_words = (secp256k1_context_preallocated_size(flags) + word_size - 1) / word_size; | ||
|
||
let buf = vec![0usize; n_words + 1].into_boxed_slice(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need this +1. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, derp, the +1 is the extra word for the length. |
||
let ptr = Box::into_raw(buf) as *mut usize; | ||
::core::ptr::write(ptr, n_words); | ||
let ptr: *mut usize = ptr.offset(1); | ||
|
||
secp256k1_context_preallocated_create(ptr as *mut c_void, flags) | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
#[no_mangle] | ||
/// A reimplementation of the C function `secp256k1_context_destroy` in rust. | ||
/// | ||
/// This function destroys and deallcates the context created by `secp256k1_context_create`. | ||
/// | ||
/// The pointer shouldn't be used after passing to this function, consider it as passing it to `free()`. | ||
/// | ||
pub unsafe extern "C" fn secp256k1_context_destroy(ctx: *mut Context) { | ||
secp256k1_context_preallocated_destroy(ctx); | ||
let ctx: *mut usize = ctx as *mut usize; | ||
|
||
let n_words_ptr: *mut usize = ctx.offset(-1); | ||
let n_words: usize = ::core::ptr::read(n_words_ptr); | ||
let slice: &mut [usize] = slice::from_raw_parts_mut(n_words_ptr , n_words+1); | ||
let _ = Box::from_raw(slice as *mut [usize]); | ||
} | ||
|
||
|
||
#[no_mangle] | ||
/// **This function is an override for the C function, this is the an edited version of the original description:** | ||
/// | ||
|
@@ -280,7 +324,7 @@ extern "C" { | |
/// See also secp256k1_default_error_callback_fn. | ||
/// | ||
pub unsafe extern "C" fn secp256k1_default_illegal_callback_fn(message: *const c_char, _data: *mut c_void) { | ||
use core::{str, slice}; | ||
use core::str; | ||
let msg_slice = slice::from_raw_parts(message as *const u8, strlen(message)); | ||
let msg = str::from_utf8_unchecked(msg_slice); | ||
panic!("[libsecp256k1] illegal argument. {}", msg); | ||
|
@@ -302,7 +346,7 @@ pub unsafe extern "C" fn secp256k1_default_illegal_callback_fn(message: *const c | |
/// See also secp256k1_default_illegal_callback_fn. | ||
/// | ||
pub unsafe extern "C" fn secp256k1_default_error_callback_fn(message: *const c_char, _data: *mut c_void) { | ||
use core::{str, slice}; | ||
use core::str; | ||
let msg_slice = slice::from_raw_parts(message as *const u8, strlen(message)); | ||
let msg = str::from_utf8_unchecked(msg_slice); | ||
panic!("[libsecp256k1] internal consistency check failed {}", msg); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh, in light of our recent alignment discussion I don't think this assertion provides any value. It's better to add a comment saying that https://rust-lang.github.io/unsafe-code-guidelines/layout/pointers.html guarantees our pointers will be aligned to
size_of::<usize>
so we can offset by that amount without fear.