Skip to content

Miri: non-deterministic floating point operations in foreign_items #143906

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

LorrensP-2158466
Copy link
Contributor

@LorrensP-2158466 LorrensP-2158466 commented Jul 13, 2025

Part of rust-lang/miri/#3555, this pr does the foreign_items work.

Some things have changed since #138062 and #142514. I moved the "helpers" used for creating fixed outputs and clamping operations to their defined ranges to math.rs. These are now also extended to handle the floating-point operations in foreign_items. Tests in miri/tests/float.rs were changed/added.

Failing tests in std were extracted, run under miri with -Zmiri-many-seeds=0..1000 and changed accordingly. Double checked with -Zmiri-many-seeds.

I noticed that the C standard doesn't specify the output ranges for all of its mathematical operations; it just specifies them as:

Returns
The sinh functions return sinh x.

So I used Wolfram|Alpha.

@rustbot
Copy link
Collaborator

rustbot commented Jul 13, 2025

r? @tgross35

rustbot has assigned @tgross35.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

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

rustbot commented Jul 13, 2025

The Miri subtree was changed

cc @rust-lang/miri

@LorrensP-2158466
Copy link
Contributor Author

LorrensP-2158466 commented Jul 13, 2025

There are some holes in the behaviour of some operations, because I did not know how I could efficiently handle them, I'll mark them and add some explanation.

Also,
r? @RalfJung

@rustbot rustbot assigned RalfJung and unassigned tgross35 Jul 13, 2025
@oli-obk
Copy link
Contributor

oli-obk commented Jul 14, 2025

Please create a PR for the libstd changes on their own so a libs reviewer can review it. Once that is done and synced, you can open a PR against the miri repo with the miri changes.

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 14, 2025
@RalfJung
Copy link
Member

RalfJung commented Jul 14, 2025 via email

@LorrensP-2158466
Copy link
Contributor Author

It's the same for me, I kept the commits for Miri and stdlib separate, so if I have to split it up, it's pretty straightforward.

@RalfJung
Copy link
Member

🤷 I'm also fine either way, it'll just be a bunch of work to port the commits to the Miri repo.

I noticed that the C standard doesn't specify the output ranges for all of its mathematical operations; it just specifies them as:

Functions where C does not give an output range shouldn't get their value clamped, I would say.

@RalfJung
Copy link
Member

I moved the "helpers" used for creating fixed outputs and clamping operations to their defined ranges to helpers.rs. These are now also extended to handle the floating-point operations in foreign_items. Tests in miri/tests/float.rs were changed/added.

Please don't, that file is already too big.^^

If the helpers are only needed in one file, keep them there.
Otherwise, add them in src/tools/miri/src/math.rs.

@LorrensP-2158466
Copy link
Contributor Author

Reopening.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 14, 2025
@LorrensP-2158466
Copy link
Contributor Author

Functions where C does not give an output range shouldn't get their value clamped, I would say.

You're right, but this has some consequences. For example, the sin operation doesn't have an explicitly defined fixed output range, yet we guarantee and test this in Miri: rust-lang/miri#4207.

The C standard for the return values of sin is:

Returns
The sin functions return sin x.

The range of sine x is [-1, 1], but is not explicitly defined.

But asin has the following description:

Returns
The asin functions return arcsin x in the interval [−π/2, +π/2] radians.

I think we have 2 approaches:

  • We say this is implementation-defined and revert all the clamps that are not explicitly defined
  • We implicitly read the mathematically defined output ranges of those operations.

@tgross35
Copy link
Contributor

If we run into libm implementations that can't even guarantee to return a value within the math function's output domain, tbh I would consider that downright broken and worthy of a bug report, or strong reason to just always use our libm on that platform. I'd be a bit hesitant to encourage users to think about this via Miri unless we think there is a chance this is actually a problem in practice (do we know of any platforms where this has been a problem?).

If it's mostly a concern about following the specification to the letter, I think it might be worth a defect report to see if the C committee is willing to codeify the output domain.

@RalfJung
Copy link
Member

I think we have 2 approaches:

I would suggest a third:

  • We do this kind of clamping wherever C mandates it, plus wherever it is easy and/or useful. So, fine to do it for sin and cos, but not for the gamma function ;)

@RalfJung
Copy link
Member

RalfJung commented Jul 14, 2025

But asin has the following description:

The arcsin function likely states this not as a means of guaranteeing precision, but because the inverse sine of x is under-defined -- infinitely many different inputs map to x (for x in [-1, 1]). By giving the interval, the answer becomes uniquely defined.

bors added a commit that referenced this pull request Jul 20, 2025
Rollup of 7 pull requests

Successful merges:

 - #142097 (gpu offload host code generation)
 - #143906 (Miri: non-deterministic floating point operations in `foreign_items`)
 - #144144 (tests: Skip supported-crate-types test on musl hosts)
 - #144159 (opt-dist: change build_dir field to be an actual build dir)
 - #144162 (Debug impls for DropElaborators)
 - #144189 (Add non-regression test for #144168)
 - #144216 (Don't consider unstable fields always-inhabited)

r? `@ghost`
`@rustbot` modify labels: rollup
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Jul 20, 2025
…-foreign-items, r=RalfJung

Miri: non-deterministic floating point operations in `foreign_items`

Part of [rust-lang/miri/rust-lang#3555](rust-lang/miri#3555 (comment)), this pr does the `foreign_items` work.

Some things have changed since rust-lang#138062 and rust-lang#142514. I moved the "helpers" used for creating fixed outputs and clamping operations to their defined ranges to `helpers.rs`. These are now also extended to handle the floating-point operations in `foreign_items`. Tests in `miri/tests/float.rs` were changed/added.

Failing tests in `std` were extracted, run under miri with `-Zmiri-many-seeds=0..1000` and changed accordingly. Double checked with `-Zmiri-many-seeds`.

I noticed that the C standard doesn't specify the output ranges for all of its mathematical operations; it just specifies them as:
```
Returns
The sinh functions return sinh x.
```
So I used [Wolfram|Alpha](https://www.wolframalpha.com/).
bors added a commit that referenced this pull request Jul 21, 2025
Rollup of 6 pull requests

Successful merges:

 - #142097 (gpu offload host code generation)
 - #143906 (Miri: non-deterministic floating point operations in `foreign_items`)
 - #144144 (tests: Skip supported-crate-types test on musl hosts)
 - #144162 (Debug impls for DropElaborators)
 - #144189 (Add non-regression test for #144168)
 - #144216 (Don't consider unstable fields always-inhabited)

r? `@ghost`
`@rustbot` modify labels: rollup
@jieyouxu
Copy link
Member

jieyouxu commented Jul 21, 2025

Failed in rollup I think (#144231 (comment)):

---- num::flt2dec::strategy::dragon::shortest_sanity_test stdout ----

thread 'num::flt2dec::strategy::dragon::shortest_sanity_test' panicked at library/coretests/tests/num/flt2dec/mod.rs:443:5:
shortest mismatch for v=ldexp_f64(1.0, 64): actual ("18446744073709535", 20), expected ("18446744073709552", 20)

---- num::flt2dec::strategy::grisu::shortest_sanity_test stdout ----

thread 'num::flt2dec::strategy::grisu::shortest_sanity_test' panicked at library/coretests/tests/num/flt2dec/mod.rs:443:5:
shortest mismatch for v=ldexp_f64(1.0, 64): actual ("18446744073709543", 20), expected ("18446744073709552", 20)

@bors r-

@jieyouxu
Copy link
Member

Hi bors @bors r-

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 21, 2025
@RalfJung
Copy link
Member

Should this really randomize ldexp? Powers-of-2 multiplication seems like something that can be done exactly fairly easily. And indeed we are not even using host floats for that.

@LorrensP-2158466
Copy link
Contributor Author

LorrensP-2158466 commented Jul 21, 2025

Yeah, that's true. This is my mistake. Looking at implementations, this is the case, the exponent is changed directly with some multiplications/additions.

Want me to edit the second commit with --keep-base?

@RalfJung
Copy link
Member

Want me to edit the second commit with --keep-base?

Yes, please do. :)

@LorrensP-2158466 LorrensP-2158466 force-pushed the miri-float-nondet-foreign-items branch from 8978073 to a3e124f Compare July 21, 2025 21:07
@LorrensP-2158466
Copy link
Contributor Author

LorrensP-2158466 commented Jul 21, 2025

So I did git rebase -i upstream/master --keep-base, hope that was it 😅.

@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 Jul 21, 2025
@RalfJung
Copy link
Member

Yeah that looks good :)
@bors r+

@bors
Copy link
Collaborator

bors commented Jul 21, 2025

📌 Commit a3e124f 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 Jul 21, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 22, 2025
…-foreign-items, r=RalfJung

Miri: non-deterministic floating point operations in `foreign_items`

Part of [rust-lang/miri/rust-lang#3555](rust-lang/miri#3555 (comment)), this pr does the `foreign_items` work.

Some things have changed since rust-lang#138062 and rust-lang#142514. I moved the "helpers" used for creating fixed outputs and clamping operations to their defined ranges to `math.rs`. These are now also extended to handle the floating-point operations in `foreign_items`. Tests in `miri/tests/float.rs` were changed/added.

Failing tests in `std` were extracted, run under miri with `-Zmiri-many-seeds=0..1000` and changed accordingly. Double checked with `-Zmiri-many-seeds`.

I noticed that the C standard doesn't specify the output ranges for all of its mathematical operations; it just specifies them as:
```
Returns
The sinh functions return sinh x.
```
So I used [Wolfram|Alpha](https://www.wolframalpha.com/).
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 22, 2025
…-foreign-items, r=RalfJung

Miri: non-deterministic floating point operations in `foreign_items`

Part of [rust-lang/miri/rust-lang#3555](rust-lang/miri#3555 (comment)), this pr does the `foreign_items` work.

Some things have changed since rust-lang#138062 and rust-lang#142514. I moved the "helpers" used for creating fixed outputs and clamping operations to their defined ranges to `math.rs`. These are now also extended to handle the floating-point operations in `foreign_items`. Tests in `miri/tests/float.rs` were changed/added.

Failing tests in `std` were extracted, run under miri with `-Zmiri-many-seeds=0..1000` and changed accordingly. Double checked with `-Zmiri-many-seeds`.

I noticed that the C standard doesn't specify the output ranges for all of its mathematical operations; it just specifies them as:
```
Returns
The sinh functions return sinh x.
```
So I used [Wolfram|Alpha](https://www.wolframalpha.com/).
bors added a commit that referenced this pull request Jul 22, 2025
Rollup of 8 pull requests

Successful merges:

 - #142454 (Add modern AVR mcus like avr128db28 and attiny3224)
 - #142924 (tidy: move rustdoc js stuff into a tidy extra check)
 - #143373 (Unquerify maybe_unused_trait_imports.)
 - #143906 (Miri: non-deterministic floating point operations in `foreign_items`)
 - #144082 (tests: cover more `exported_private_dependencies` cases)
 - #144126 (Fix empty target_config in apply_rust_config bootstrap)
 - #144164 ( opt-dist: add an option for setting path to stage0 root)
 - #144265 (Dont ICE on copy error being suppressed due to overflow)

r? `@ghost`
`@rustbot` modify labels: rollup
@matthiaskrgr
Copy link
Member

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 22, 2025
@LorrensP-2158466
Copy link
Contributor Author

Can't seem to reproduce it on my machine. Might as well set everything to 1e-5 for 32 and 1e-10 for f64.

@tgross35, @RalfJung, what do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants