Skip to content

Validate transmute in CTFE - #144030

Open
oli-obk wants to merge 2 commits into
rust-lang:mainfrom
oli-obk:validate-transmute
Open

oli-obk wants to merge 2 commits into
rust-lang:mainfrom
oli-obk:validate-transmute

Conversation

@oli-obk

@oli-obk oli-obk commented Jul 16, 2025 •

Copy link
Copy Markdown
Contributor

View all comments

fixes #142230

let's see what perf says, maybe we need to restrict it to literal transmutes, and not all the implicit ones happening in mir interpreter internal situations

r? @ghost

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 16, 2025
@rustbot

rustbot commented Jul 16, 2025

Copy link
Copy Markdown
Collaborator

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

@oli-obk

oli-obk commented Jul 16, 2025

Copy link
Copy Markdown
Contributor Author

@bors2 try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

rust-bors Bot commented Jul 16, 2025

Copy link
Copy Markdown
Contributor

⌛ Trying commit d032919 with merge e6b0b5d…

To cancel the try build, run the command @bors2 try cancel.

rust-bors Bot added a commit that referenced this pull request Jul 16, 2025
Validate transmute in CTFE

fixes #142230

let's see what perf says, maybe we need to restrict it to literal transmutes, and not all the implicit ones happening in mir interpreter internal situations

r? `@ghost`
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 16, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-bors Bot commented Jul 16, 2025

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: e6b0b5d (e6b0b5d69a1aea3e0e367e405c676642a0b35313, parent: 1c6de215099bbe33668de762f9591187f6c25eef)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (e6b0b5d): comparison URL.

Overall result: ❌ regressions - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.5% [0.2%, 1.7%] 17
Regressions ❌
(secondary)
38.4% [0.5%, 149.4%] 28
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.5% [0.2%, 1.7%] 17

Max RSS (memory usage)

Results (primary -2.0%, secondary 0.7%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
12.3% [12.3%, 12.3%] 1
Improvements ✅
(primary)
-2.0% [-2.0%, -2.0%] 1
Improvements ✅
(secondary)
-2.2% [-4.1%, -1.0%] 4
All ❌✅ (primary) -2.0% [-2.0%, -2.0%] 1

Cycles

Results (primary 2.5%, secondary 348.7%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.5% [2.5%, 2.5%] 1
Regressions ❌
(secondary)
426.9% [336.3%, 574.7%] 9
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.2% [-3.5%, -2.8%] 2
All ❌✅ (primary) 2.5% [2.5%, 2.5%] 1

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 464.891s -> 467.195s (0.50%)
Artifact size: 374.78 MiB -> 374.45 MiB (-0.09%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jul 16, 2025
@oli-obk
oli-obk force-pushed the validate-transmute branch from d032919 to 1aa2951 Compare July 17, 2025 09:04
@oli-obk

oli-obk commented Jul 17, 2025

Copy link
Copy Markdown
Contributor Author

Limited validation to explicit transmute calls

@bors2 try @rust-timer queue

@rust-timer

This comment has been minimized.

rust-bors Bot added a commit that referenced this pull request Jul 17, 2025
Validate transmute in CTFE

fixes #142230

let's see what perf says, maybe we need to restrict it to literal transmutes, and not all the implicit ones happening in mir interpreter internal situations

r? `@ghost`
@rust-bors

rust-bors Bot commented Jul 17, 2025

Copy link
Copy Markdown
Contributor

⌛ Trying commit 1aa2951 with merge c41f0b4…

To cancel the try build, run the command @bors2 try cancel.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 17, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-bors Bot commented Jul 17, 2025

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: c41f0b4 (c41f0b44c8cb4579318560cb49853d48ee948f0e, parent: f8f6997469237299c1d60814c7b9828602a1f8e4)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (c41f0b4): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.3% [0.2%, 0.5%] 2
Regressions ❌
(secondary)
0.3% [0.2%, 0.5%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.1% [-1.1%, -1.0%] 2
All ❌✅ (primary) 0.3% [0.2%, 0.5%] 2

Max RSS (memory usage)

Results (secondary 2.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.4% [2.4%, 2.4%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Cycles

Results (secondary -12.8%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-12.8% [-12.8%, -12.8%] 1
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 463.571s -> 465.557s (0.43%)
Artifact size: 374.76 MiB -> 374.31 MiB (-0.12%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 17, 2025
@oli-obk

oli-obk commented Jul 17, 2025

Copy link
Copy Markdown
Contributor Author

yay. html5ever has a small regression in const eval, but everything else seems to be fine.

@rust-lang/wg-const-eval what do you think about doing validation only for the result of transmute (and possibly raw pointer derefs in the future?)? I think it produces better errors in many cases and still checks as little as possible and is therefore cheap to do.

@RalfJung RalfJung left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This doesn't fix the underlying problem that our errors during evaluation vs during validation are worded very differently... but yeah adding extra checks here sounds good, if we can do it in a way that doesn't affect Miri.

Comment on lines +145 to +146
// Even if general validation is disabled, transmutes should always check their result.
if !M::enforce_validity(self, dest.layout) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Miri has a flag for disabling validity checking -- this code makes it not work properly any more.

So this still needs to be somehow controlled by the machine, with a way for the machine to entirely disable all validity checking. Maybe enforce_validity should return a 3-state enum?

@oli-obk oli-obk Jul 17, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmm... I was just gonna add a const block for extra validation checks, but that may work, too

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This could also be inside copy_op_allow_transmute or rather copy_op_inner. The code there knows when source and target type differ.

@bors

bors commented Jul 19, 2025

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #144145) made this pull request unmergeable. Please resolve the merge conflicts.

@Dylan-DPC Dylan-DPC added S-experimental Status: Ongoing experiment that does not require reviewing and won't be merged in its current state. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 2, 2025
@rustbot

rustbot commented Feb 11, 2026

Copy link
Copy Markdown
Collaborator

The Miri subtree was changed

cc @rust-lang/miri

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot

rustbot commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rustbot

rustbot commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator

⚠️ Warning ⚠️

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf-regression Performance regression. S-experimental Status: Ongoing experiment that does not require reviewing and won't be merged in its current state. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Transmuting fn pointer to a usize in const somehow causes UB?

7 participants