Skip to content

Miri: handling of SNaN inputs in f*::pow operations #142514

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

Merged
merged 2 commits into from
Jun 30, 2025

Conversation

LorrensP-2158466
Copy link
Contributor

@LorrensP-2158466 LorrensP-2158466 commented Jun 14, 2025

fixes miri/#4286 and related to #138062 and miri/#4208.

For the following cases of the powf or powi operations, Miri returns either 1.0 or an arbitrary NaN:

  • powf(SNaN, 0.0)
  • powf(1.0, SNaN)
  • powi(SNaN, 0)

Also added a macro in miri/tests/pass/float.rs which conveniently checks if both are indeed returned from such an operation.

Made these changes in the rust repo so I could test against stdlib, since these were impacted some time ago and were fixed in #138062. Tested with:

env MIRIFLAGS=-Zmiri-many-seeds ./x miri --no-fail-fast std core coretests -- f32 f64

This was successful. This does take a while, so I recommend using --no-doc and separate use of f32 or f64

The pr is somewhat split up into 3 main commits, which implement the cases described above. The first commit also introduces the macro, and the last commit is just a global refactor of some things.

r? @RalfJung

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 14, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 14, 2025

The Miri subtree was changed

cc @rust-lang/miri

Comment on lines 506 to 512
fn random_nan<S: Semantics>(rng: &mut StdRng) -> IeeeFloat<S> {
if rng.random() {
IeeeFloat::<S>::snan(Some(rng.random()))
} else {
IeeeFloat::<S>::qnan(Some(rng.random()))
}
}
Copy link
Member

Choose a reason for hiding this comment

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

We already have extensive logic for random NaNs (generate_nan), please use that instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh yeah 🤦

("powf32" | "powf64", [base, exp]) if *base == one => {
let rng = ecx.machine.rng.get_mut();
// Handle both the musl and glibc cases non-deterministically.
if !exp.is_signaling() || rng.random() { one } else { random_nan(rng) }
Copy link
Member

Choose a reason for hiding this comment

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

This is too compact to be readable. Also, this needs to honor the (new) float_nondet flag.

let return_nan = exp.is_signaling() && ecx.machine.float_nondet && rng.random();
if return_nan { ...

}

// x^(SNaN) = (1 | NaN)
test_snan_nondet!(f32::powf(SNAN_F32, 0.0));
Copy link
Member

Choose a reason for hiding this comment

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

Please don't invent new logic, but use existing logic. We already have ensure_nondet.

@RalfJung
Copy link
Member

Sorry for the slow review, I was traveling. And due to an upcoming deadline I'll be quite busy for another 2 weeks, unfortunately.

@RalfJung
Copy link
Member

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 27, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 27, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@RalfJung
Copy link
Member

@bors2 try jobs=x86_64-gnu-aux

@rust-bors
Copy link

rust-bors bot commented Jun 27, 2025

⌛ Trying commit 550a13b with merge 1dc8c00

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

rust-bors bot added a commit that referenced this pull request Jun 27, 2025
Miri: handling of SNaN inputs in `f*::pow` operations

<!-- homu-ignore:start -->
<!--
If this PR is related to an unstable feature or an otherwise tracked effort,
please link to the relevant tracking issue here. If you don't know of a related
tracking issue or there are none, feel free to ignore this.

This PR will get automatically assigned to a reviewer. In case you would like
a specific user to review your work, you can assign it to them by using

    r? <reviewer name>
-->
<!-- homu-ignore:end -->

fixes [miri/#4286](rust-lang/miri#4286) and related to #138062 and [miri/#4208](rust-lang/miri#4208 (comment)).

For the following cases of the powf or powi operations, Miri returns either `1.0` or an arbitrary `NaN`:
- `powf(SNaN, 0.0)`
- `powf(1.0, SNaN)`
- `powi(SNaN, 0)`

Also added a macro in `miri/tests/pass/float.rs` which conveniently checks if both are indeed returned from such an operation.

Made these changes in the rust repo so I could test against stdlib, since these were impacted some time ago and were fixed in #138062. Tested with:
```fish
env MIRIFLAGS=-Zmiri-many-seeds ./x miri --no-fail-fast std core coretests -- f32 f64
```
This was successful. This does take a while, so I recommend using `--no-doc` and separate use of `f32` or `f64`

The pr is somewhat split up into 3 main commits, which implement the cases described above. The first commit also introduces the macro, and the last commit is just a global refactor of some things.

r? `@RalfJung`
try-job: x86_64-gnu-aux
@rust-bors
Copy link

rust-bors bot commented Jun 27, 2025

☀️ Try build successful (CI)
Build commit: 1dc8c00 (1dc8c007860b211c121063d9186c775b10a289d8, parent: fe5f3dedf7b4d6bea2cadb17343f747d70b4c66b)

@LorrensP-2158466
Copy link
Contributor Author

Sorry for the slow review, I was traveling. And due to an upcoming deadline I'll be quite busy for another 2 weeks, unfortunately.

No worries! I had exams, so I couldn't respond to the review if it had been done sooner.

@LorrensP-2158466
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 29, 2025
@RalfJung RalfJung force-pushed the miri-float-nondet-pow branch from 6718b82 to 06dd8bc Compare June 29, 2025 20:26
@RalfJung
Copy link
Member

Thanks! I added a commit with some more tests. Should be good to go now :)
@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Jun 29, 2025

📌 Commit 06dd8bc has been approved by RalfJung

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 29, 2025
@RalfJung RalfJung force-pushed the miri-float-nondet-pow branch from 06dd8bc to e059457 Compare June 29, 2025 20:31
plus various minor tweaks
@RalfJung RalfJung force-pushed the miri-float-nondet-pow branch from e059457 to 67ab61e Compare June 29, 2025 20:32
@RalfJung
Copy link
Member

@bors r+

@bors
Copy link
Collaborator

bors commented Jun 29, 2025

📌 Commit 67ab61e has been approved by RalfJung

It is now in the queue for this repository.

bors added a commit that referenced this pull request Jun 30, 2025
Rollup of 14 pull requests

Successful merges:

 - #142429 (`tests/ui`: A New Order [13/N])
 - #142514 (Miri: handling of SNaN inputs in `f*::pow` operations)
 - #143066 (Use let chains in the new solver)
 - #143090 (Workaround for memory unsafety in third party DLLs)
 - #143118 (`tests/ui`: A New Order [15/N])
 - #143159 (Do not freshen `ReError`)
 - #143168 (`tests/ui`: A New Order [16/N])
 - #143176 (fix typos and improve clarity in documentation)
 - #143187 (Add my work email to mailmap)
 - #143190 (Use the `new` method for `BasicBlockData` and `Statement`)
 - #143195 (`tests/ui`: A New Order [17/N])
 - #143196 (Port #[link_section] to the new attribute parsing infrastructure)
 - #143199 (Re-disable `tests/run-make/short-ice` on Windows MSVC again)
 - #143219 (Show auto trait and blanket impls for `!`)

r? `@ghost`
`@rustbot` modify labels: rollup
@LorrensP-2158466
Copy link
Contributor Author

Thanks! I added a commit with some more tests. Should be good to go now :)

Oh wow, thanks!

@bors bors merged commit d2dc99c into rust-lang:master Jun 30, 2025
10 checks passed
@rustbot rustbot added this to the 1.90.0 milestone Jun 30, 2025
rust-timer added a commit that referenced this pull request Jun 30, 2025
Rollup merge of #142514 - LorrensP-2158466:miri-float-nondet-pow, r=RalfJung

Miri: handling of SNaN inputs in `f*::pow` operations

fixes [miri/#4286](rust-lang/miri#4286) and related to #138062 and [miri/#4208](rust-lang/miri#4208 (comment)).

For the following cases of the powf or powi operations, Miri returns either `1.0` or an arbitrary `NaN`:
- `powf(SNaN, 0.0)`
- `powf(1.0, SNaN)`
- `powi(SNaN, 0)`

Also added a macro in `miri/tests/pass/float.rs` which conveniently checks if both are indeed returned from such an operation.

Made these changes in the rust repo so I could test against stdlib, since these were impacted some time ago and were fixed in #138062. Tested with:
```fish
env MIRIFLAGS=-Zmiri-many-seeds ./x miri --no-fail-fast std core coretests -- f32 f64
```
This was successful. This does take a while, so I recommend using `--no-doc` and separate use of `f32` or `f64`

The pr is somewhat split up into 3 main commits, which implement the cases described above. The first commit also introduces the macro, and the last commit is just a global refactor of some things.

r? `@RalfJung`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make powf/powi(sNaN, 0) over-approximate both glibc and musl
4 participants