Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Eliminate some more transmute() #8879

Merged
merged 4 commits into from
Jun 22, 2018
Merged

Eliminate some more transmute() #8879

merged 4 commits into from
Jun 22, 2018

Conversation

niklasad1
Copy link
Collaborator

No description provided.

@niklasad1 niklasad1 added A0-pleasereview 🤓 Pull request needs code review. M4-core ⛓ Core client code / Rust. labels Jun 12, 2018
addr[5],
addr[6],
addr[7])),
((addr[1] as u16) << 8) | (addr[0] as u16),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addr is actually a [u8; 16] so no need to transmute it but requires some shifting to merge two bytes into a word (two bytes)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@5chdn 5chdn added this to the 1.12 milestone Jun 13, 2018
@@ -236,16 +236,15 @@ mod getinterfaces {
let sa: *const sockaddr_in6 = sa as *const sockaddr_in6;
let sa = & unsafe { *sa };
let (addr, port) = (sa.sin6_addr.s6_addr, sa.sin6_port);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the type of addr here?

Copy link
Collaborator Author

@niklasad1 niklasad1 Jun 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is u32 but I saw that this was actually from std::net and it actually implements the from trait for our types so I changed it! Sorry for changing after reviewing byt it makes the code more readable IMO!

@debris debris added A8-looksgood 🦄 Pull request is reviewed well. and removed A0-pleasereview 🤓 Pull request needs code review. labels Jun 13, 2018
Copy link
Contributor

@andresilva andresilva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some grumbles.

@@ -137,11 +137,11 @@ fn hash_compute(light: &Light, full_size: usize, header_hash: &H256, nonce: u64)
($n:expr, $value:expr) => {{
// We use explicit lifetimes to ensure that val's borrow is invalidated until the
// transmuted val dies.
unsafe fn make_const_array<'a, T, U>(val: &'a mut [T]) -> &'a mut [U; $n] {
unsafe fn make_const_array<'a, T, U>(val: &mut [T]) -> &mut [U; $n] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop unsafe?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

&mut *(val.as_mut_ptr() as *mut [U; $n]) is also unsafe because deref of raw pointer so I would like to keep the function as unsafe! Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah won't compile without unsafe ignore my comment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like the explicit lifetimes have been dropped here. should be &'a mut [T] -> &'a mut [U; $n] right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rphmeier Explicit lifetimes here are not needed here, so I removed them but forgot a in the signature!

I can revert this if you want explicit lifetime annotations!

@@ -268,10 +268,10 @@ fn hash_compute(light: &Light, full_size: usize, header_hash: &H256, nonce: u64)

let value: H256 = unsafe {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop unsafe?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, keccak_256::unchecked is unsafe.

((addr & 0x00FF_0000) >> 16) as u8,
((addr & 0xFF00_0000) >> 24) as u8)),
port)
(IpAddr::V4(Ipv4Addr::from(addr)), port)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like we're encoding the octets in reverse order compared to std.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will check but I assumed the tests actually tested this?!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you are right, I will revert this!

@andresilva andresilva added A5-grumble 🔥 Pull request has minor issues that must be addressed before merging. and removed A8-looksgood 🦄 Pull request is reviewed well. labels Jun 14, 2018
* Make unsafe block smaller
* Use different byte-order than `std`, read words as big endian instead
of little endian!
* Use `from_be` to work both for big and little endian
* Ipv6 addresses were incorrectly `transmuted`
port)
let ip_addr = Ipv6Addr::from(addr);
debug_assert!(addr == ip_addr.octets());
(IpAddr::V6(ip_addr), port)
Copy link
Collaborator Author

@niklasad1 niklasad1 Jun 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty convinced that this was a bug because

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1] // ::1

was parsed as [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]

Also with u8, endianness "shouldn´t" matter

So, I suspect casting/transmute it to u16 [0x1 0x2] will be:

  • Little endian: -> [0x2 0x1]
  • Big endian: -> [0x1 0x2]

@openethereum openethereum deleted a comment from rphmeier Jun 18, 2018
@5chdn
Copy link
Contributor

5chdn commented Jun 22, 2018

Needs a 2nd review. cc @andresilva

Copy link
Contributor

@andresilva andresilva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@andresilva andresilva added A8-looksgood 🦄 Pull request is reviewed well. and removed A5-grumble 🔥 Pull request has minor issues that must be addressed before merging. labels Jun 22, 2018
@andresilva andresilva merged commit 0cd1de7 into master Jun 22, 2018
@5chdn 5chdn deleted the na-remove-more-transmute branch June 22, 2018 16:25
dvdplm added a commit that referenced this pull request Jun 22, 2018
* master:
  Add type for passwords. (#8920)
  deps: bump fs-swap (#8953)
  Eliminate some more `transmute()` (#8879)
  Restrict vault.json permssion to owner and using random suffix for temp vault.json file (#8932)
  print SS.self_public when starting SS node (#8949)
  scripts: minor improvements (#8930)
ordian added a commit to ordian/parity that referenced this pull request Jun 27, 2018
…rp_sync_on_light_client

* 'master' of https://github.com/paritytech/parity:
  deps: bump fs-swap (openethereum#8953)
  Eliminate some more `transmute()` (openethereum#8879)
  Restrict vault.json permssion to owner and using random suffix for temp vault.json file (openethereum#8932)
  print SS.self_public when starting SS node (openethereum#8949)
  scripts: minor improvements (openethereum#8930)
  rpc: cap gas limit of local calls (openethereum#8943)
  docs: update changelogs (openethereum#8931)
  ethcore: fix compilation when using slow-blocks or evm-debug features (openethereum#8936)
  fixed blooms dir creation (openethereum#8941)
  Update hardcoded headers (openethereum#8925)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A8-looksgood 🦄 Pull request is reviewed well. M4-core ⛓ Core client code / Rust.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants