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

Miri exception improvements #69297

Closed
2 tasks done
RalfJung opened this issue Feb 19, 2020 · 13 comments
Closed
2 tasks done

Miri exception improvements #69297

RalfJung opened this issue Feb 19, 2020 · 13 comments
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-cleanup Category: PRs that clean code up or issues documenting cleanup. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@RalfJung
Copy link
Member

RalfJung commented Feb 19, 2020

The Miri engine uses "exceptions" (InterpResult) to report misbehaving executions and propagate this information outwards to the machine (const-eval/const-prop/miri-the-tool). When an error is raised, we optionally capture a backtrace to ease debugging.

There are two problems with this:

  • Currently, checking whether we want to capture a backtrace is rather expensive (we check an env var). @wesleywiser proposed a scheme to use a session variable instead.
  • Sometimes, the engine catches these "exceptions". This means we caught the backtrace in vein. Check RUSTC_CTFE_BACKTRACE much less by generating fewer errors #69290 brought some nice speed-up by removing just one case of catching an interpreter error! I think we should never catch these errors. Is there something we can do to prevent accidental catching?

Cc @oli-obk

@RalfJung RalfJung added A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-cleanup Category: PRs that clean code up or issues documenting cleanup. labels Feb 19, 2020
@jonas-schievink jonas-schievink added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Feb 19, 2020
@oli-obk
Copy link
Contributor

oli-obk commented Feb 26, 2020

The obvious solution is to go through the engine (we could still use the ? operator and basically not require any changes except to type signatures if we made it an associated constant or at least not an &self function), but that requires infecting all error types.

Didn't we also discuss checking the current query name somewhere?

@wesleywiser
Copy link
Member

Didn't we also discuss checking the current query name somewhere?

Yes, it was proposed here but at least for ctfe-stress-4, that didn't pay off because most of the errors were created during const_eval-raw (see here).

@RalfJung
Copy link
Member Author

@oli-obk I take you you are talking about the first point? As I wrote above, I think @wesleywiser's proposed scheme of using a session var is a good next step there -- certainly better than what we do now.

@RalfJung
Copy link
Member Author

RalfJung commented Mar 3, 2020

I found some more cases of exceptions being caught:

The first one occurs quite a bit in Miri because we validate all the time (CTFE validates less often so this should be less of an issue, but still). I am not sure how best to fix it, short of implementing rust-lang/miri#841.

@RalfJung
Copy link
Member Author

@wesleywiser do you still plan to submit this approach as a PR or would you like someone else to take over?

@wesleywiser
Copy link
Member

@RalfJung Thanks for the reminder! I just posted a slightly more cleaned up version as #69888.

Centril added a commit to Centril/rust that referenced this issue Mar 11, 2020
…to_session_var, r=RalfJung

[Miri] Use a session variable instead of checking for an env var always

In CTFE heavy code, checking the env var everytime is inefficient. We
can do a lot better by using a `Session` variable instead.

r? @RalfJung

Part of rust-lang#69297
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Mar 25, 2020
avoid catching InterpError

Avoid raising and then capturing `InterpError` for the definedness check.

Cc rust-lang#69297
r? @oli-obk
Centril added a commit to Centril/rust that referenced this issue Mar 26, 2020
avoid catching InterpError

Avoid raising and then capturing `InterpError` for the definedness check.

Cc rust-lang#69297
r? @oli-obk
@RalfJung
Copy link
Member Author

Status: last time I looked into this, I found some more cases where exceptions are being caught, that are related to handling of pointer provenance and ptr-int casts. I consider this problem blocked on rust-lang/miri#841.

@RalfJung
Copy link
Member Author

I am also seeing many exceptions being raised and caught in ConstProp -- so, RUSTC_CTFE_BACKTRACE=1 is a serious slowdown for constprop. I am not sure if there is anything we can do about that... @oli-obk any idea?

Sadly this also masks any exceptions being raised elsewhere, making it really hard to figure out if there are non-constprop instances of this problem left.

@oli-obk
Copy link
Contributor

oli-obk commented Jul 15, 2021

I'm not sure const prop can do better other than duplicating all the checks that would lead to the exceptions being raised. We can do some half measures to eliminate obvious cases.

We could add a Drop impl to the error type and require all destructions of the errors to be made explicit. Not sure how annoying that would be in practice, but it could work well.

@RalfJung
Copy link
Member Author

We could also do something awful and have some thread-local storage so this code

let backtrace = match capture_backtrace {

knows it is inside ConstProp and always treats this as Disabled.

@oli-obk
Copy link
Contributor

oli-obk commented Jul 15, 2021

We could also do something awful and have some thread-local storage so this code

let backtrace = match capture_backtrace {

knows it is inside ConstProp and always treats this as Disabled.

and then re-enable (in a scoped manner) when inside the CTFE query. Seems hacky and annyoing and actually manageable. I think it's fine, it's just for the backtrace printing, nothing that actually affects anyone that is not already debugging CTFE

@RalfJung
Copy link
Member Author

RalfJung commented Jul 19, 2021

I think I'll just use a locally patched rustc that does not do const-prop.^^ But we should maybe make a note that RUST_CTFE_BACKTRACE=1 currently is rather expensive since it captures a lot of backtraces during const-prop. I'll open an issue for that.
EDIT: actually... not sure if a perf issue that only arises during debugging is really worth an issue. 🤷

Regarding the issue at hand, the following are the only Miri test cases that have any exceptions caught and discarded. :)

    [ui] run-pass/exit.rs
    [ui] run-pass/issue-miri-1075.rs

@RalfJung
Copy link
Member Author

Ah, and those are expected, since they are using throw_machine_stop!.

So I think this issue is solved. :-) We don't have a strategy in place to detect when it arises again in the future, but still I think we can close this one now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-cleanup Category: PRs that clean code up or issues documenting cleanup. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants