Merge pull request #1236 from rphmeier/hash_unsafety

remove unsafety from util/hash.rs and util/bigint/uint.rs
This commit is contained in:
Arkadiy Paronyan 2016-06-10 14:11:36 +02:00 committed by GitHub
commit 3e7d8bf1a8
2 changed files with 54 additions and 85 deletions

View File

@ -92,8 +92,8 @@ macro_rules! uint_overflowing_add_reg {
macro_rules! uint_overflowing_add { macro_rules! uint_overflowing_add {
(U256, $n_words: expr, $self_expr: expr, $other: expr) => ({ (U256, $n_words: expr, $self_expr: expr, $other: expr) => ({
let mut result: [u64; 4] = unsafe { mem::uninitialized() }; let mut result: [u64; 4] = unsafe { mem::uninitialized() };
let self_t: &[u64; 4] = unsafe { &mem::transmute($self_expr) }; let self_t: &[u64; 4] = &self.0;
let other_t: &[u64; 4] = unsafe { &mem::transmute($other) }; let other_t: &[u64; 4] = &other.0;
let overflow: u8; let overflow: u8;
unsafe { unsafe {
@ -115,8 +115,8 @@ macro_rules! uint_overflowing_add {
}); });
(U512, $n_words: expr, $self_expr: expr, $other: expr) => ({ (U512, $n_words: expr, $self_expr: expr, $other: expr) => ({
let mut result: [u64; 8] = unsafe { mem::uninitialized() }; let mut result: [u64; 8] = unsafe { mem::uninitialized() };
let self_t: &[u64; 8] = unsafe { &mem::transmute($self_expr) }; let self_t: &[u64; 8] = &self.0;
let other_t: &[u64; 8] = unsafe { &mem::transmute($other) }; let other_t: &[u64; 8] = &other.0;
let overflow: u8; let overflow: u8;
@ -196,8 +196,8 @@ macro_rules! uint_overflowing_sub_reg {
macro_rules! uint_overflowing_sub { macro_rules! uint_overflowing_sub {
(U256, $n_words: expr, $self_expr: expr, $other: expr) => ({ (U256, $n_words: expr, $self_expr: expr, $other: expr) => ({
let mut result: [u64; 4] = unsafe { mem::uninitialized() }; let mut result: [u64; 4] = unsafe { mem::uninitialized() };
let self_t: &[u64; 4] = unsafe { &mem::transmute($self_expr) }; let self_t: &[u64; 4] = &self.0;
let other_t: &[u64; 4] = unsafe { &mem::transmute($other) }; let other_t: &[u64; 4] = &other.0;
let overflow: u8; let overflow: u8;
unsafe { unsafe {
@ -218,8 +218,8 @@ macro_rules! uint_overflowing_sub {
}); });
(U512, $n_words: expr, $self_expr: expr, $other: expr) => ({ (U512, $n_words: expr, $self_expr: expr, $other: expr) => ({
let mut result: [u64; 8] = unsafe { mem::uninitialized() }; let mut result: [u64; 8] = unsafe { mem::uninitialized() };
let self_t: &[u64; 8] = unsafe { &mem::transmute($self_expr) }; let self_t: &[u64; 8] = &self.0;
let other_t: &[u64; 8] = unsafe { &mem::transmute($other) }; let other_t: &[u64; 8] = &other.0;
let overflow: u8; let overflow: u8;
@ -270,8 +270,8 @@ macro_rules! uint_overflowing_sub {
macro_rules! uint_overflowing_mul { macro_rules! uint_overflowing_mul {
(U256, $n_words: expr, $self_expr: expr, $other: expr) => ({ (U256, $n_words: expr, $self_expr: expr, $other: expr) => ({
let mut result: [u64; 4] = unsafe { mem::uninitialized() }; let mut result: [u64; 4] = unsafe { mem::uninitialized() };
let self_t: &[u64; 4] = unsafe { &mem::transmute($self_expr) }; let self_t: &[u64; 4] = &self.0;
let other_t: &[u64; 4] = unsafe { &mem::transmute($other) }; let other_t: &[u64; 4] = &self.0;
let overflow: u64; let overflow: u64;
unsafe { unsafe {
@ -548,6 +548,7 @@ pub trait Uint: Sized + Default + FromStr + From<u64> + fmt::Debug + fmt::Displa
macro_rules! construct_uint { macro_rules! construct_uint {
($name:ident, $n_words:expr) => ( ($name:ident, $n_words:expr) => (
/// Little-endian large integer type /// Little-endian large integer type
#[repr(C)]
#[derive(Copy, Clone, Eq, PartialEq)] #[derive(Copy, Clone, Eq, PartialEq)]
pub struct $name(pub [u64; $n_words]); pub struct $name(pub [u64; $n_words]);
@ -1132,8 +1133,8 @@ impl U256 {
/// No overflow possible /// No overflow possible
#[cfg(all(asm_available, target_arch="x86_64"))] #[cfg(all(asm_available, target_arch="x86_64"))]
pub fn full_mul(self, other: U256) -> U512 { pub fn full_mul(self, other: U256) -> U512 {
let self_t: &[u64; 4] = unsafe { &mem::transmute(self) }; let self_t: &[u64; 4] = &self.0;
let other_t: &[u64; 4] = unsafe { &mem::transmute(other) }; let other_t: &[u64; 4] = &other.0;
let mut result: [u64; 8] = unsafe { mem::uninitialized() }; let mut result: [u64; 8] = unsafe { mem::uninitialized() };
unsafe { unsafe {
asm!(" asm!("

View File

@ -143,6 +143,7 @@ macro_rules! impl_hash {
} }
min min
} }
fn from_slice(src: &[u8]) -> Self { fn from_slice(src: &[u8]) -> Self {
let mut r = Self::new(); let mut r = Self::new();
r.clone_from_slice(src); r.clone_from_slice(src);
@ -150,24 +151,15 @@ macro_rules! impl_hash {
} }
fn copy_to(&self, dest: &mut[u8]) { fn copy_to(&self, dest: &mut[u8]) {
unsafe { let min = ::std::cmp::min($size, dest.len());
let min = ::std::cmp::min($size, dest.len()); dest[..min].copy_from_slice(&self.0[..min]);
::std::ptr::copy(self.0.as_ptr(), dest.as_mut_ptr(), min);
}
} }
fn shift_bloomed<'a, T>(&'a mut self, b: &T) -> &'a mut Self where T: FixedHash { fn shift_bloomed<'a, T>(&'a mut self, b: &T) -> &'a mut Self where T: FixedHash {
let bp: Self = b.bloom_part($size); let bp: Self = b.bloom_part($size);
let new_self = &bp | self; let new_self = &bp | self;
// impl |= instead self.0 = new_self.0;
// TODO: that's done now!
unsafe {
use std::{mem, ptr};
ptr::copy(new_self.0.as_ptr(), self.0.as_mut_ptr(), mem::size_of::<Self>());
}
self self
} }
@ -179,7 +171,6 @@ macro_rules! impl_hash {
let bloom_bits = m * 8; let bloom_bits = m * 8;
let mask = bloom_bits - 1; let mask = bloom_bits - 1;
let bloom_bytes = (log2(bloom_bits) + 7) / 8; let bloom_bytes = (log2(bloom_bits) + 7) / 8;
//println!("bb: {}", bloom_bytes);
// must be a power of 2 // must be a power of 2
assert_eq!(m & (m - 1), 0); assert_eq!(m & (m - 1), 0);
@ -316,12 +307,9 @@ macro_rules! impl_hash {
#[cfg_attr(feature="dev", allow(expl_impl_clone_on_copy))] #[cfg_attr(feature="dev", allow(expl_impl_clone_on_copy))]
impl Clone for $from { impl Clone for $from {
fn clone(&self) -> $from { fn clone(&self) -> $from {
unsafe { let mut ret = $from::new();
use std::{mem, ptr}; ret.0.copy_from_slice(&self.0);
let mut ret: $from = mem::uninitialized(); ret
ptr::copy(self.0.as_ptr(), ret.0.as_mut_ptr(), mem::size_of::<$from>());
ret
}
} }
} }
@ -404,14 +392,11 @@ macro_rules! impl_hash {
type Output = $from; type Output = $from;
fn bitor(self, rhs: Self) -> Self::Output { fn bitor(self, rhs: Self) -> Self::Output {
unsafe { let mut ret: $from = $from::default();
use std::mem; for i in 0..$size {
let mut ret: $from = mem::uninitialized(); ret.0[i] = self.0[i] | rhs.0[i];
for i in 0..$size {
ret.0[i] = self.0[i] | rhs.0[i];
}
ret
} }
ret
} }
} }
@ -429,14 +414,11 @@ macro_rules! impl_hash {
type Output = $from; type Output = $from;
fn bitand(self, rhs: Self) -> Self::Output { fn bitand(self, rhs: Self) -> Self::Output {
unsafe { let mut ret: $from = $from::default();
use std::mem; for i in 0..$size {
let mut ret: $from = mem::uninitialized(); ret.0[i] = self.0[i] & rhs.0[i];
for i in 0..$size {
ret.0[i] = self.0[i] & rhs.0[i];
}
ret
} }
ret
} }
} }
@ -454,14 +436,11 @@ macro_rules! impl_hash {
type Output = $from; type Output = $from;
fn bitxor(self, rhs: Self) -> Self::Output { fn bitxor(self, rhs: Self) -> Self::Output {
unsafe { let mut ret: $from = $from::default();
use std::mem; for i in 0..$size {
let mut ret: $from = mem::uninitialized(); ret.0[i] = self.0[i] ^ rhs.0[i];
for i in 0..$size {
ret.0[i] = self.0[i] ^ rhs.0[i];
}
ret
} }
ret
} }
} }
@ -516,21 +495,17 @@ macro_rules! impl_hash {
impl From<U256> for H256 { impl From<U256> for H256 {
fn from(value: U256) -> H256 { fn from(value: U256) -> H256 {
unsafe { let mut ret = H256::new();
let mut ret: H256 = ::std::mem::uninitialized(); value.to_raw_bytes(&mut ret);
value.to_raw_bytes(&mut ret); ret
ret
}
} }
} }
impl<'a> From<&'a U256> for H256 { impl<'a> From<&'a U256> for H256 {
fn from(value: &'a U256) -> H256 { fn from(value: &'a U256) -> H256 {
unsafe { let mut ret: H256 = H256::new();
let mut ret: H256 = ::std::mem::uninitialized(); value.to_raw_bytes(&mut ret);
value.to_raw_bytes(&mut ret); ret
ret
}
} }
} }
@ -548,51 +523,44 @@ impl<'a> From<&'a H256> for U256 {
impl From<H256> for Address { impl From<H256> for Address {
fn from(value: H256) -> Address { fn from(value: H256) -> Address {
unsafe { let mut ret = Address::new();
let mut ret: Address = ::std::mem::uninitialized(); ret.0.copy_from_slice(&value[12..32]);
::std::ptr::copy(value.as_ptr().offset(12), ret.as_mut_ptr(), 20); ret
ret
}
} }
} }
impl From<H256> for H64 { impl From<H256> for H64 {
fn from(value: H256) -> H64 { fn from(value: H256) -> H64 {
unsafe { let mut ret = H64::new();
let mut ret: H64 = ::std::mem::uninitialized(); ret.0.copy_from_slice(&value[20..28]);
::std::ptr::copy(value.as_ptr().offset(20), ret.as_mut_ptr(), 8); ret
ret
}
} }
} }
/* /*
impl<'a> From<&'a H256> for Address { impl<'a> From<&'a H256> for Address {
fn from(value: &'a H256) -> Address { fn from(value: &'a H256) -> Address {
unsafe { let mut ret = Address::new();
let mut ret: Address = ::std::mem::uninitialized(); ret.0.copy_from_slice(&value[12..32]);
::std::ptr::copy(value.as_ptr().offset(12), ret.as_mut_ptr(), 20);
ret ret
} }
} }
} }
*/ */
impl From<Address> for H256 { impl From<Address> for H256 {
fn from(value: Address) -> H256 { fn from(value: Address) -> H256 {
unsafe { let mut ret = H256::new();
let mut ret = H256::new(); ret.0[12..32].copy_from_slice(&value);
::std::ptr::copy(value.as_ptr(), ret.as_mut_ptr().offset(12), 20); ret
ret
}
} }
} }
impl<'a> From<&'a Address> for H256 { impl<'a> From<&'a Address> for H256 {
fn from(value: &'a Address) -> H256 { fn from(value: &'a Address) -> H256 {
unsafe { let mut ret = H256::new();
let mut ret = H256::new(); ret.0[12..32].copy_from_slice(value);
::std::ptr::copy(value.as_ptr(), ret.as_mut_ptr().offset(12), 20); ret
ret
}
} }
} }