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

mark extern block function signatures as FIXED #966

Merged
merged 2 commits into from
Jun 21, 2023

Conversation

aneksteind
Copy link
Contributor

@aneksteind aneksteind commented Jun 16, 2023

Fixes #965. The inputs and outputs of body-owners are already marked as fixed, but the traversal of all_fn_dids does not include function declarations in extern blocks

@aneksteind aneksteind requested a review from spernsteiner June 16, 2023 23:24
@aneksteind aneksteind force-pushed the feat.analyze.foreign branch 3 times, most recently from b4ff66a to a16883c Compare June 16, 2023 23:37
@aneksteind aneksteind force-pushed the feat.analyze.foreign branch from a16883c to 5214eda Compare June 17, 2023 00:43
c2rust-analyze/src/main.rs Outdated Show resolved Hide resolved
c2rust-analyze/src/main.rs Outdated Show resolved Hide resolved
}

// FIX the fields of structs mentioned in extern blocks
for adt_did in &gacx.adt_metadata.struct_dids {
Copy link
Contributor

Choose a reason for hiding this comment

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

By the way, it would be easier to review (though in this case, it's a simple change so it's not bad) if moves were separated from actual changes in separate commits.

Copy link
Contributor

@kkysen kkysen left a comment

Choose a reason for hiding this comment

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

LGTM once the unneeded allocations are removed and the usage of PointerInfo::ANNOTATED is confirmed (I'm assuming my latter thought is right, in which case it's all good).

Comment on lines 342 to 343
let output = gacx.assign_pointer_ids_with_info(sig.output(), PointerInfo::ANNOTATED);
make_ty_fixed(gasn, output)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This loop creates a bunch of LTys and marks them FIXED but doesn't store them anywhere, so they have no effect on anything. inputs and output should probably be bundled into an LFnSig and then stored in either gacx.fn_sigs or some new map.

Copy link
Contributor

Choose a reason for hiding this comment

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

gacx.fn_sigs just stores LFnSigs for body owners, so no body-less extern fns (how would some like a non-default trait fn be handled, by the way?) are included. Doesn't that mean we would never rewrite such an extern fn in the first place?

Copy link
Collaborator

Choose a reason for hiding this comment

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

We shouldn't rewrite extern-block fns, but we need to retrieve their signatures anywhere they're called. That's why I was thinking it might be reasonable to put them in fn_sigs, alongside the signatures of all the normal fns.

Copy link
Contributor

Choose a reason for hiding this comment

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

Don't we just currently crash on calls to extern fns? That's why I'm working on fixing with hardcoding permissions for libc fns, but that's probably going to work a bit differently from fn_sigs. Right now, and for any extern fn we don't hardcode, we'll crash on the call when we try to lookup the LFnSig from fn_sigs. I just want to make sure we don't silently do the wrong thing instead of crashing if we include these extern fns in fn_sigs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added them to gacx.fn_sigs in 7f8bddb

Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't we just currently crash on calls to extern fns? That's why I'm working on fixing with hardcoding permissions for libc fns

Yes - there are two parts to libc function handling:

  1. For analysis, we need to know the READ/WRITE/OFFSET permissions on the function signature. This is the part involving hard-coded permissions.
  2. For rewriting, we need to generate casts around calls to libc functions. Setting FIXED on the signature triggers insertion of these casts.

Right now, and for any extern fn we don't hardcode, we'll crash on the call when we try to lookup the LFnSig from fn_sigs. I just want to make sure we don't silently do the wrong thing instead of crashing if we include these extern fns in fn_sigs.

Good point. Currently util::ty_callee checks for extern-block fns directly (by looking at the parent DefKind), so that part will still work after this change. But are there other places in the code that assume extern-block fns will be absent from fn_sigs?

Copy link
Contributor

Choose a reason for hiding this comment

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

@aneksteind @spernsteiner. I could move the foreign fn sigs to a new map, right? As long as I handle it during calls (which should be quite simple as I'm already changing that stuff), it should be fine, right? I think it can simplify things with #980 and #999.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure how that helps, but yeah, you could move them as long as you update all the places where calls are handled (which includes at least dataflow::type_check and rewrite::expr::mir_op - I don't think borrowck::type_check handles calls yet).

c2rust-analyze/src/main.rs Outdated Show resolved Hide resolved
c2rust-analyze/src/main.rs Outdated Show resolved Hide resolved
@aneksteind aneksteind requested a review from spernsteiner June 21, 2023 11:09
Copy link
Collaborator

@spernsteiner spernsteiner left a comment

Choose a reason for hiding this comment

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

Would be nice to have a test for this, but I guess that might be too much trouble when you can't actually call extern-block fns yet.

@aneksteind aneksteind merged commit e0d474c into master Jun 21, 2023
@aneksteind aneksteind deleted the feat.analyze.foreign branch June 21, 2023 17:01
@aneksteind
Copy link
Contributor Author

Would be nice to have a test for this, but I guess that might be too much trouble when you can't actually call extern-block fns yet.

@spernsteiner how do you want to handle that in the short term, is it worth supporting minimally or in some more principled way? @kkysen didn't you have a WIP for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

analyze: mark the inputs and output of foreign, non-body-owners as fixed
3 participants