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

FnAbi Compatability check #4185

Merged
merged 1 commit into from
Mar 12, 2025
Merged

Conversation

geetanshjuneja
Copy link
Contributor

Links to #3842

@geetanshjuneja
Copy link
Contributor Author

I have made changes only in a single place to make sure that this what we want.

Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

Judging from the CI failures, this does not seem to be quite right. And (as noted inline) I think this will never be able to do the "proper" check. So I don't think this is the right approach.

The right approach has to be somewhat similar to what happens for regular function calls in Miri. Basically, we need a helper function that takes a list of types (the expected argument types) and a return type, and then somehow checks that against the FnAbi that is passed around in the Miri shim handling. This likely involved constructing our own FnAbi to compare, and I have no idea how to do that...

Also, I would suggest to not touch intrinsics for now. They are somewhat special ABI-wise. Let's start with normal shims, i.e. all the things on foreign_items.

@geetanshjuneja
Copy link
Contributor Author

The right approach has to be somewhat similar to what happens for regular function calls in Miri. Basically, we need a helper function that takes a list of types (the expected argument types) and a return type, and then somehow checks that against the FnAbi that is passed around in the Miri shim handling. This likely involved constructing our own FnAbi to compare, and I have no idea how to do that...

For non variadic fn's can't we just simply create FnAbi struct using the types available in docs. The issue only comes for variadic fn's as we dont know the types of the extra args.

@RalfJung
Copy link
Member

Yes we "just" have to do that but it is not necessarily so easy. :)
But we can probably construct a fn ptr type corresponding to the desired signature, and then use fn_abi_of_fn_ptr.

@geetanshjuneja
Copy link
Contributor Author

geetanshjuneja commented Feb 13, 2025

Yes we "just" have to do that but it is not necessarily so easy. :)

Why can't we directly instantiate an object of FnAbi struct?
Also are you talking about this fn_abi_of_fn_ptr ?

@RalfJung
Copy link
Member

Why can't we instantiate an object of FnAbi struct?

That involves computing a PassMode, and we most definitely do not want to compute those ourselves. We want to use the logic in the Rust compiler for computing those.

Also are you talking about this fn_abi_of_fn_ptr ?

Yes. It can be invoked in Miri via this.fn_abi_of_fn_ptr (where this is an InterpCx).

@geetanshjuneja
Copy link
Contributor Author

geetanshjuneja commented Feb 13, 2025

To resolve I was thinking of implementing the following things:

Create a fn check_fn_abi which would take a list of input types , an output type and Fnabi that is passed in the miri shim handling. This fn would create a FnSig using the args. Now we will create expected FnAbi using this fn sig and compare it with the input Fnsig. If it doesn't matches throw ub.
Now Transmute can be called safely inside shim.

@RalfJung
Copy link
Member

That sounds like a good plan. There are methods defined in https://github.com/rust-lang/rust/blob/master/compiler/rustc_const_eval/src/interpret/call.rs that are probably useful but have to be made pub before they can be called from Miri.

@geetanshjuneja
Copy link
Contributor Author

geetanshjuneja commented Feb 14, 2025

There are methods defined in https://github.com/rust-lang/rust/blob/master/compiler/rustc_const_eval/src/interpret/call.rs that are probably useful but have to be made pub before they can be called from Miri.

Are you talking about check_argument_compatmethod here? If yes now then I think we can compare the two FnAbi by iterating over the args of the two abis by calling check_argument_compat method.

Also inputs_and_output field of FnSig requires list as RawList<(), T> so I need to convert given input and output arg slice into RawList<(), T> using from_arena method?

@RalfJung
Copy link
Member

RalfJung commented Feb 14, 2025

Are you talking about check_argument_compatmethod here?

Yes.

Also inputs_and_output field of FnSig requires list as RawList<(), T> so I need to convert given input and output arg slice into RawList<(), T> using from_arena method?

You can use these methods: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/?search=mk_type_list.

@geetanshjuneja
Copy link
Contributor Author

I am mostly done with the code now I just need check_argument_compat to be public.

@RalfJung
Copy link
Member

Okay, please make a PR against the rustc repo to make that function public then. :)

jhpratt added a commit to jhpratt/rust that referenced this pull request Feb 15, 2025
made check_argument_compat public for use in miri

Links to [issue](rust-lang/miri#3842) and it's [PR](rust-lang/miri#4185 (comment))  in miri.
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Feb 15, 2025
Rollup merge of rust-lang#137056 - geetanshjuneja:pub, r=RalfJung

made check_argument_compat public for use in miri

Links to [issue](rust-lang/miri#3842) and it's [PR](rust-lang/miri#4185 (comment))  in miri.
@geetanshjuneja geetanshjuneja changed the title Added get_arg to check and transmute opty FnAbi Compatability check Feb 16, 2025
@rustbot

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-author Status: Waiting for the PR author to address review comments label Feb 16, 2025
@RalfJung
Copy link
Member

RalfJung commented Feb 16, 2025

Please don't do rustup in the middle of a regular PR. I did a regular rustup, so if you rebase your PR should pick that up. :)

@RalfJung
Copy link
Member

@rustbot author
Please post @rustbot ready when the PR is ready for the next round of review.

@geetanshjuneja
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Waiting for a review to complete and removed S-waiting-on-author Status: Waiting for the PR author to address review comments labels Feb 17, 2025
Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

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

@rustbot author

@rustbot rustbot removed S-waiting-on-review Status: Waiting for a review to complete labels Feb 18, 2025
Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

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

Sorry for the long wait!

@geetanshjuneja
Copy link
Contributor Author

Also do I need to add a test for return type mismatch?

@RalfJung
Copy link
Member

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: Waiting for the PR author to address review comments and removed S-waiting-on-review Status: Waiting for a review to complete labels Mar 11, 2025
@geetanshjuneja
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Waiting for a review to complete and removed S-waiting-on-author Status: Waiting for the PR author to address review comments labels Mar 11, 2025
@RalfJung
Copy link
Member

Also do I need to add a test for return type mismatch?

Yes please do.

@RalfJung
Copy link
Member

This looks great, thanks! Please squash the commits, then we can land this. Please use the --keep-base flag when squashing so that the force-push diff is easier to review. Then write @rustbot ready after you pushed the squashed PR.

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: Waiting for the PR author to address review comments and removed S-waiting-on-review Status: Waiting for a review to complete labels Mar 12, 2025
@geetanshjuneja
Copy link
Contributor Author

Hmm on my macOS c_char is i8

@RalfJung
Copy link
Member

Just use c_short instead of c_char.

added input arg mismatch test

added detailed ub messages

added return type mismatch test
@RalfJung
Copy link
Member

Thanks for the PR and the patience. :)

@RalfJung RalfJung enabled auto-merge March 12, 2025 17:52
@geetanshjuneja
Copy link
Contributor Author

So the next step would be to use check_shim_abi for non variadic shims? If yes I can work on that as well

@RalfJung RalfJung added this pull request to the merge queue Mar 12, 2025
@RalfJung
Copy link
Member

Yeah the next step is to use this for all the existing shims. Please don't do them all in one PR. :) Pick one file, e.g. src/shims/foreign_items.rs, and make a PR just for that. In the first pass, skip any that cause some extra complications.

Merged via the queue into rust-lang:master with commit 578098f Mar 12, 2025
7 checks passed
@geetanshjuneja geetanshjuneja deleted the abi_check branch March 13, 2025 03:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: Waiting for the PR author to address review comments
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants