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

Eliminate some more transmute()#8879

Merged
andresilva merged 4 commits into
masterfrom
na-remove-more-transmute
Jun 22, 2018
Merged

Eliminate some more transmute()#8879
andresilva merged 4 commits into
masterfrom
na-remove-more-transmute

Conversation

@niklasad1
Copy link
Copy Markdown
Collaborator

No description provided.

@niklasad1 niklasad1 added A0-pleasereview 🤓 Pull request needs code review. M4-core ⛓ Core client code / Rust. labels Jun 12, 2018
Comment thread util/network-devp2p/src/ip_utils.rs Outdated
addr[5],
addr[6],
addr[7])),
((addr[1] as u16) << 8) | (addr[0] as u16),
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
@niklasad1 niklasad1 force-pushed the na-remove-more-transmute branch from b2a2cf8 to c88c831 Compare June 13, 2018 09:02
Copy link
Copy Markdown
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.

Comment thread ethash/src/compute.rs Outdated
// 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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!

Comment thread ethash/src/compute.rs Outdated
@@ -268,10 +268,10 @@ fn hash_compute(light: &Light, full_size: usize, header_hash: &H256, nonce: u64)

let value: H256 = unsafe {
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread util/network-devp2p/src/ip_utils.rs Outdated
((addr & 0x00FF_0000) >> 16) as u8,
((addr & 0xFF00_0000) >> 24) as u8)),
port)
(IpAddr::V4(Ipv4Addr::from(addr)), port)
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
@niklasad1 niklasad1 force-pushed the na-remove-more-transmute branch from c88c831 to 7090737 Compare June 14, 2018 13:45
* Make unsafe block smaller
* Use different byte-order than `std`, read words as big endian instead
of little endian!
@niklasad1 niklasad1 force-pushed the na-remove-more-transmute branch from 7090737 to 2199be7 Compare June 18, 2018 14:01
* Use `from_be` to work both for big and little endian
* Ipv6 addresses were incorrectly `transmuted`
@niklasad1 niklasad1 force-pushed the na-remove-more-transmute branch from 2199be7 to 4a3f7d3 Compare June 18, 2018 14:23
port)
let ip_addr = Ipv6Addr::from(addr);
debug_assert!(addr == ip_addr.octets());
(IpAddr::V6(ip_addr), port)
Copy link
Copy Markdown
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
Copy Markdown
Contributor

5chdn commented Jun 22, 2018

Needs a 2nd review. cc @andresilva

Copy link
Copy Markdown
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