Modifying MIR from compiler plugin

Hey everyone,

I am working on a project on a verifier for Rust and basically I am trying to automatically add some sort of runtime checks into the generated executables.

This would require me to get a mutable version of the MIR, that can be fed back to the compiler. I would prefer to do this on the MIR level, since most of the analysis of our tool happens on this level anyways. Modifying the AST might also be an option, in case that is significantly simpler to modify.

However, we would like to avoid forking the compiler and do this by using rustc's interface only, but so far all the ideas I had / previous suggestions on forums I found lead me nowhere.

  • things like registering custom MirPasses have been deprecated since apparently this is a misuse of them.
  • I was hoping I could override a query like mir_built, replicate its original behavior and add some additional modifications. However, it seems like the "replicating its original behavior" is not really possible since most of the used functions and types are private.

So yeah, I would be very happy about any feedback, suggestions or help of any kind. Is there still a intended way of mutating the MIR from a compiler plugin? If so, how? If not, what might be a sensible way to "hack" it?

Also, as mentioned above, I've looked into ways of modifying the AST as well, but so far it seems like the same problems apply there. If there's easier ways for the ast than mir I would also greatly appreciate any pointers.

Cheers :slight_smile:

You can use rustc_interface::DEFAULT_QUERY_PROVIDERS to get the original provider. Then you can run this from your replacement before modifying the result and returning it. Also depending on if it should run before or after borrowck and if running optimizations first is fine, you may need to override a different query like optimized_mir.

1 Like

What was wrong with custom MIR passes?

I can only quote the message of the commit where they removed the function to register them here.

In recent months there have been a few different people investigating how to make a plugin that registers a MIR-pass – one that isn’t intended to be eventually merged into rustc proper. The interface to register MIR passes was added primarily for miri (& later was found to make prototyping of rustc-proper MIR passes a tiny bit faster). Since miri does not use this interface anymore it seems like a good time to remove this "feature". For prototyping purposes a similar interface can be added by developers themselves in their custom rustc build.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.