Skip to content
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

Fix miscompilation in transmute_unchecked introduced by bug in LLVM #25

Merged
merged 1 commit into from
Jul 4, 2024

Conversation

zheland
Copy link
Contributor

@zheland zheland commented Jul 4, 2024

Currently some casts from byte primitives to two element tuple lead to miscompilation on release builds on Rust >=1.70.0:

  • castaway::cast!(123_u8, (u8, u8)) unexpectedly returns Ok(...) that leads to UB.
  • castaway::cast!(false, (bool, u16)) leads to SIGILL: illegal instruction runtime error.

Upstream issues:

I suggest considering adding a safe "workaround" to fix the issue in this crate without having to wait for the upstream fixes. This way we will have this fixed in older Rust versions as well.

This PR adds size eq assert to transmute_unchecked. This workaround was found while preparing an MRE for an upstream issue.

Checked locally with cargo test --release for Rust 1.38, 1.68.0, 1.69.0, 1.70.0, 1.71.0, 1.72.0, stable, beta, nightly. Generated assembly for other tests cases for the release build seems the same (checks and casts are optimized away).

Btw: it might also be a good idea to run tests in --release mode as well since the crate relies heavily on optimizing the casts to zero-cost.

Copy link
Owner

@sagebind sagebind left a comment

Choose a reason for hiding this comment

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

Thanks for finding this and opening a PR! I will push out a patch release with this ASAP.

@sagebind sagebind merged commit f397e48 into sagebind:master Jul 4, 2024
5 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants