@@ -39,6 +39,7 @@ pub mod types;
39
39
pub mod recovery;
40
40
41
41
use core:: { slice, ptr} ;
42
+ use core:: ptr:: NonNull ;
42
43
use types:: * ;
43
44
44
45
/// Flag for context to enable no precomputation
@@ -512,7 +513,7 @@ extern "C" {
512
513
513
514
// Contexts
514
515
#[ cfg_attr( not( rust_secp_no_symbol_renaming) , link_name = "rustsecp256k1_v0_6_1_context_preallocated_destroy" ) ]
515
- pub fn secp256k1_context_preallocated_destroy ( cx : * mut Context ) ;
516
+ pub fn secp256k1_context_preallocated_destroy ( cx : NonNull < Context > ) ;
516
517
517
518
// Signatures
518
519
#[ cfg_attr( not( rust_secp_no_symbol_renaming) , link_name = "rustsecp256k1_v0_6_1_ecdsa_signature_parse_der" ) ]
@@ -585,16 +586,16 @@ extern "C" {
585
586
pub fn secp256k1_context_preallocated_size ( flags : c_uint ) -> size_t ;
586
587
587
588
#[ cfg_attr( not( rust_secp_no_symbol_renaming) , link_name = "rustsecp256k1_v0_6_1_context_preallocated_create" ) ]
588
- pub fn secp256k1_context_preallocated_create ( prealloc : * mut c_void , flags : c_uint ) -> * mut Context ;
589
+ pub fn secp256k1_context_preallocated_create ( prealloc : NonNull < c_void > , flags : c_uint ) -> NonNull < Context > ;
589
590
590
591
#[ cfg_attr( not( rust_secp_no_symbol_renaming) , link_name = "rustsecp256k1_v0_6_1_context_preallocated_clone_size" ) ]
591
592
pub fn secp256k1_context_preallocated_clone_size ( cx : * const Context ) -> size_t ;
592
593
593
594
#[ cfg_attr( not( rust_secp_no_symbol_renaming) , link_name = "rustsecp256k1_v0_6_1_context_preallocated_clone" ) ]
594
- pub fn secp256k1_context_preallocated_clone ( cx : * const Context , prealloc : * mut c_void ) -> * mut Context ;
595
+ pub fn secp256k1_context_preallocated_clone ( cx : * const Context , prealloc : NonNull < c_void > ) -> NonNull < Context > ;
595
596
596
597
#[ cfg_attr( not( rust_secp_no_symbol_renaming) , link_name = "rustsecp256k1_v0_6_1_context_randomize" ) ]
597
- pub fn secp256k1_context_randomize ( cx : * mut Context ,
598
+ pub fn secp256k1_context_randomize ( cx : NonNull < Context > ,
598
599
seed32 : * const c_uchar )
599
600
-> c_int ;
600
601
// Pubkeys
@@ -789,7 +790,7 @@ extern "C" {
789
790
/// The newly created secp256k1 raw context.
790
791
#[ cfg( all( feature = "alloc" , not( rust_secp_no_symbol_renaming) ) ) ]
791
792
#[ cfg_attr( docsrs, doc( cfg( all( feature = "alloc" , not( rust_secp_no_symbol_renaming) ) ) ) ) ]
792
- pub unsafe fn secp256k1_context_create ( flags : c_uint ) -> * mut Context {
793
+ pub unsafe fn secp256k1_context_create ( flags : c_uint ) -> NonNull < Context > {
793
794
rustsecp256k1_v0_6_1_context_create ( flags)
794
795
}
795
796
@@ -800,7 +801,7 @@ pub unsafe fn secp256k1_context_create(flags: c_uint) -> *mut Context {
800
801
#[ allow( clippy:: missing_safety_doc) ] // Documented above.
801
802
#[ cfg( all( feature = "alloc" , not( rust_secp_no_symbol_renaming) ) ) ]
802
803
#[ cfg_attr( docsrs, doc( cfg( all( feature = "alloc" , not( rust_secp_no_symbol_renaming) ) ) ) ) ]
803
- pub unsafe extern "C" fn rustsecp256k1_v0_6_1_context_create ( flags : c_uint ) -> * mut Context {
804
+ pub unsafe extern "C" fn rustsecp256k1_v0_6_1_context_create ( flags : c_uint ) -> NonNull < Context > {
804
805
use core:: mem;
805
806
use crate :: alloc:: alloc;
806
807
assert ! ( ALIGN_TO >= mem:: align_of:: <usize >( ) ) ;
@@ -817,7 +818,8 @@ pub unsafe extern "C" fn rustsecp256k1_v0_6_1_context_create(flags: c_uint) -> *
817
818
( ptr as * mut usize ) . write ( bytes) ;
818
819
// We must offset a whole ALIGN_TO in order to preserve the same alignment
819
820
// this means we "lose" ALIGN_TO-size_of(usize) for padding.
820
- let ptr = ptr. add ( ALIGN_TO ) as * mut c_void ;
821
+ let ptr = ptr. add ( ALIGN_TO ) ;
822
+ let ptr = NonNull :: new_unchecked ( ptr as * mut c_void ) ; // Checked above.
821
823
secp256k1_context_preallocated_create ( ptr, flags)
822
824
}
823
825
@@ -832,17 +834,18 @@ pub unsafe extern "C" fn rustsecp256k1_v0_6_1_context_create(flags: c_uint) -> *
832
834
/// `ctx` must be a valid pointer to a block of memory created using [`secp256k1_context_create`].
833
835
#[ cfg( all( feature = "alloc" , not( rust_secp_no_symbol_renaming) ) ) ]
834
836
#[ cfg_attr( docsrs, doc( cfg( all( feature = "alloc" , not( rust_secp_no_symbol_renaming) ) ) ) ) ]
835
- pub unsafe fn secp256k1_context_destroy ( ctx : * mut Context ) {
837
+ pub unsafe fn secp256k1_context_destroy ( ctx : NonNull < Context > ) {
836
838
rustsecp256k1_v0_6_1_context_destroy ( ctx)
837
839
}
838
840
839
841
#[ no_mangle]
840
842
#[ allow( clippy:: missing_safety_doc) ] // Documented above.
841
843
#[ cfg( all( feature = "alloc" , not( rust_secp_no_symbol_renaming) ) ) ]
842
844
#[ cfg_attr( docsrs, doc( cfg( all( feature = "alloc" , not( rust_secp_no_symbol_renaming) ) ) ) ) ]
843
- pub unsafe extern "C" fn rustsecp256k1_v0_6_1_context_destroy ( ctx : * mut Context ) {
845
+ pub unsafe extern "C" fn rustsecp256k1_v0_6_1_context_destroy ( mut ctx : NonNull < Context > ) {
844
846
use crate :: alloc:: alloc;
845
847
secp256k1_context_preallocated_destroy ( ctx) ;
848
+ let ctx: * mut Context = ctx. as_mut ( ) ;
846
849
let ptr = ( ctx as * mut u8 ) . sub ( ALIGN_TO ) ;
847
850
let bytes = ( ptr as * mut usize ) . read ( ) ;
848
851
let layout = alloc:: Layout :: from_size_align ( bytes, ALIGN_TO ) . unwrap ( ) ;
@@ -966,8 +969,8 @@ mod fuzz_dummy {
966
969
967
970
extern "C" {
968
971
fn rustsecp256k1_v0_6_1_context_preallocated_size ( flags : c_uint ) -> size_t ;
969
- fn rustsecp256k1_v0_6_1_context_preallocated_create ( prealloc : * mut c_void , flags : c_uint ) -> * mut Context ;
970
- fn rustsecp256k1_v0_6_1_context_preallocated_clone ( cx : * const Context , prealloc : * mut c_void ) -> * mut Context ;
972
+ fn rustsecp256k1_v0_6_1_context_preallocated_create ( prealloc : NonNull < c_void > , flags : c_uint ) -> NonNull < Context > ;
973
+ fn rustsecp256k1_v0_6_1_context_preallocated_clone ( cx : * const Context , prealloc : NonNull < c_void > ) -> NonNull < Context > ;
971
974
}
972
975
973
976
#[ cfg( feature = "lowmemory" ) ]
@@ -985,7 +988,7 @@ mod fuzz_dummy {
985
988
const HAVE_CONTEXT_WORKING : usize = 1 ;
986
989
const HAVE_CONTEXT_DONE : usize = 2 ;
987
990
static mut PREALLOCATED_CONTEXT : [ u8 ; CTX_SIZE ] = [ 0 ; CTX_SIZE ] ;
988
- pub unsafe fn secp256k1_context_preallocated_create ( prealloc : * mut c_void , flags : c_uint ) -> * mut Context {
991
+ pub unsafe fn secp256k1_context_preallocated_create ( prealloc : NonNull < c_void > , flags : c_uint ) -> NonNull < Context > {
989
992
// While applications should generally avoid creating too many contexts, sometimes fuzzers
990
993
// perform tasks repeatedly which real applications may only do rarely. Thus, we want to
991
994
// avoid being overly slow here. We do so by having a static context and copying it into
@@ -998,9 +1001,9 @@ mod fuzz_dummy {
998
1001
if have_ctx == HAVE_CONTEXT_NONE {
999
1002
assert ! ( rustsecp256k1_v0_6_1_context_preallocated_size( SECP256K1_START_SIGN | SECP256K1_START_VERIFY ) + std:: mem:: size_of:: <c_uint>( ) <= CTX_SIZE ) ;
1000
1003
assert_eq ! ( rustsecp256k1_v0_6_1_context_preallocated_create(
1001
- PREALLOCATED_CONTEXT [ ..] . as_ptr ( ) as * mut c_void,
1004
+ NonNull :: new_unchecked ( PREALLOCATED_CONTEXT [ ..] . as_mut_ptr ( ) as * mut c_void) ,
1002
1005
SECP256K1_START_SIGN | SECP256K1_START_VERIFY ) ,
1003
- PREALLOCATED_CONTEXT [ ..] . as_ptr ( ) as * mut Context ) ;
1006
+ NonNull :: new_unchecked ( PREALLOCATED_CONTEXT [ ..] . as_mut_ptr ( ) as * mut Context ) ) ;
1004
1007
assert_eq ! ( HAVE_PREALLOCATED_CONTEXT . swap( HAVE_CONTEXT_DONE , Ordering :: AcqRel ) ,
1005
1008
HAVE_CONTEXT_WORKING ) ;
1006
1009
} else if have_ctx == HAVE_CONTEXT_DONE {
@@ -1015,25 +1018,25 @@ mod fuzz_dummy {
1015
1018
std:: thread:: yield_now ( ) ;
1016
1019
}
1017
1020
}
1018
- ptr:: copy_nonoverlapping ( PREALLOCATED_CONTEXT [ ..] . as_ptr ( ) , prealloc as * mut u8 , CTX_SIZE ) ;
1019
- let ptr = ( prealloc as * mut u8 ) . add ( CTX_SIZE ) . sub ( std:: mem:: size_of :: < c_uint > ( ) ) ;
1021
+ ptr:: copy_nonoverlapping ( PREALLOCATED_CONTEXT [ ..] . as_ptr ( ) , prealloc. as_ptr ( ) as * mut u8 , CTX_SIZE ) ;
1022
+ let ptr = ( prealloc. as_ptr ( ) ) . add ( CTX_SIZE ) . sub ( std:: mem:: size_of :: < c_uint > ( ) ) ;
1020
1023
( ptr as * mut c_uint ) . write ( flags) ;
1021
- prealloc as * mut Context
1024
+ NonNull :: new_unchecked ( prealloc. as_ptr ( ) as * mut Context )
1022
1025
}
1023
1026
pub unsafe fn secp256k1_context_preallocated_clone_size ( _cx : * const Context ) -> size_t { CTX_SIZE }
1024
- pub unsafe fn secp256k1_context_preallocated_clone ( cx : * const Context , prealloc : * mut c_void ) -> * mut Context {
1027
+ pub unsafe fn secp256k1_context_preallocated_clone ( cx : * const Context , prealloc : NonNull < c_void > ) -> NonNull < Context > {
1025
1028
let orig_ptr = ( cx as * mut u8 ) . add ( CTX_SIZE ) . sub ( std:: mem:: size_of :: < c_uint > ( ) ) ;
1026
- let new_ptr = ( prealloc as * mut u8 ) . add ( CTX_SIZE ) . sub ( std:: mem:: size_of :: < c_uint > ( ) ) ;
1029
+ let new_ptr = ( prealloc. as_ptr ( ) as * mut u8 ) . add ( CTX_SIZE ) . sub ( std:: mem:: size_of :: < c_uint > ( ) ) ;
1027
1030
let flags = ( orig_ptr as * mut c_uint ) . read ( ) ;
1028
1031
( new_ptr as * mut c_uint ) . write ( flags) ;
1029
1032
rustsecp256k1_v0_6_1_context_preallocated_clone ( cx, prealloc)
1030
1033
}
1031
1034
1032
- pub unsafe fn secp256k1_context_randomize ( cx : * mut Context ,
1035
+ pub unsafe fn secp256k1_context_randomize ( cx : NonNull < Context > ,
1033
1036
_seed32 : * const c_uchar )
1034
1037
-> c_int {
1035
1038
// This function is really slow, and unsuitable for fuzzing
1036
- check_context_flags ( cx, 0 ) ;
1039
+ check_context_flags ( cx. as_ptr ( ) , 0 ) ;
1037
1040
1
1038
1041
}
1039
1042
0 commit comments