-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
#[ffi_returns_twice] #2633
#[ffi_returns_twice] #2633
Conversation
d6acad5
to
8088b92
Compare
My initial thought is that I’m not sure from the RFC what the soundness and safety implications are, e.g.:
|
This attribute, as proposed in this RFC and as implemented in the PR, can only be applied to
C FFI is already unsound.
This RFC does not extend Rust with the ability to write bare functions that return multiple times. Stable Rust already allows you to just add an This RFC doesn't allow you to write your own in Rust nor tells you what these |
The RFC only mentions that it can be added to extern functions, but it does not explicitly prohibit use anywhere else (in fact, it doesn’t mention what happens for other types of functions at all). Perhaps that can be clarified? |
Uh, indeed. I meant functions in an |
I thinks that’s a big improvement, but I think it would also be good to have a statement like “Adding this attribute to any other type of item is an error”. |
I find it confusing that the RFC always talks about returning multiple times but the attribute is called "twice". |
I do so too. This attribute is called The only people that will need this attribute is those reading C code on one side, and writing the Rust FFI wrapper on the other. So keeping the name the same, even if its unfortunate, makes the lives of the only users that this attribute will have easier. EDIT: the docs do say that it returns multiple times do, maybe it is worth it to call out why it is called |
Since you have a couple of these (I don't know if it's better, but if they interact with each other -- like IIRC you mentioned |
cc @RalfJung |
Basically all I ask is that you make sure this
is known to everyone using
|
undefined behavior of the type "use-after-move". | ||
|
||
In the presence of types that implement `Drop`, usage of APIs that return | ||
multiple times requires extreme care to avoid deallocating memory without |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth saying "deallocating pinned memory" here. At least to my knowledge that is the only case where we really care in terms of type system guarantees.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't Pin
just a normal Rust library ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is. So what?
Unsound thread spawning and Rc
are also both just normal Rust libraries, and yet having one makes the other unsound. The same goes for Pin
and functions that let you deallocate memory without running drop
.
@kornelski Do you mean with some Otherwise unwinding is UB. |
@eddyb Yes, that's what I mean. I realize it's not fully baked in Rust yet. |
I see that this is merged and available in nightly (behind a feature gate). What is the path toward stabilization? Is more input/discussion needed, or just bake time? If it helps, this addresses my use case. |
This RFC specifies what the attribute does, and there are some issues that have been raised about the RFC but have not been resolved yet, so that would need to be done. After that, the corresponding teams would need to vote on whether this should be merged or not (just normal process). This RFC leaves the syntax as an unresolved question, so even if this RFC is merged, stabilization would be blocked on the syntax issue being resolved. We will probably need a different RFC proposing a more holistic approach to FFI attributes in general. There is an issue open about that: #2637 . |
Interesting factoid: there are functions that might return twice for which this attribute would be incorrect. The naming consistency with C, along with |
@mtak- why would adding |
@gnzlbg Because on the second return of |
That doesn't make it incorrect though, just unnecessary, right? |
I guess you could say that about all functions. It is only necessary when the first return has visible side-effects, which is not the case with Essentially, consider the following code: fn foo() -> i32 {
let mut x = 0;
let second_return = setjmp(env) != 0;
x += 1;
if second_return {
return x;
} else {
longjmp(env);
}
} If the compiler is allowed to perform its normal optimizations, it will see that the address of However if you used |
@Amanieu note that This does not invalidate your point about |
Everything @Amanieu said. The CPU is really taking care to undo everything up until the point of the On powerpc you can suspend and resume transactions, which might mean power's EDIT: The compiler barrier is not really for correctness, it's just so that the optimizer doesn't move more memory accesses into the critical section than necessary, and also so that instructions which automatically trigger aborts such as cpuid are not moved into the critical section. |
FYI, I am now of the opinion that I think I would prefer a In the meantime, I think it would be better to just require code that needs this to live on nightly, and provide, for example, a general I think that for the time being, it is more important for us to be able to gain experience with this on nightly, than to ship this as a stable feature. We can always come back to this later. Thoughts? EDIT: I know this landed on nightly already, but we can just move that to a more general feature to apply LLVM attributes in a subsequent PR. |
Seems like a step backwards from not having LLVM be the only backend? |
@Centril |
@gnzlbg I appreciate that you are taking this all the way back to first principles, but it has left the problem quite open ended, and I hope there is some way we can work from both sides of the problem at once. While experimenting with a first-class The people who need it (like me) are probably better off with a supported We will need documentation around My use case, by the way, is about writing complex extensions for postgres in pure rust. I just got an extension working that creates a background worker, listens on a port, and handles requests with tokio by passing queries to SPI (postgres internal query processing API), and returns the results to the client. I've been super happy with rust in every way, and I'm presenting the work at PGCon next week. There are only two holes in this story:
|
@jeff-davis right now there is no |
@gnzlbg I would like |
Note that this RFC does not stabilize |
I'm going to go ahead and close this; this exact area of work (longjmp, in partcular) is being worked on in the ffi-unwind project group (in which @gnzlbg has been active from time to time). This seems like it should be discussed in that context. |
If you're like to learn about the ffi-unwind project group, check out this repository. =) |
…, r=compiler-errors Remove `ffi_returns_twice` feature The [tracking issue](rust-lang#58314) and [RFC](rust-lang/rfcs#2633) have been closed for a couple of years. There is also an attribute gate in R-A which should be removed if this lands.
…, r=compiler-errors Remove `ffi_returns_twice` feature The [tracking issue](rust-lang#58314) and [RFC](rust-lang/rfcs#2633) have been closed for a couple of years. There is also an attribute gate in R-A which should be removed if this lands.
…, r=compiler-errors Remove `ffi_returns_twice` feature The [tracking issue](rust-lang#58314) and [RFC](rust-lang/rfcs#2633) have been closed for a couple of years. There is also an attribute gate in R-A which should be removed if this lands.
Rollup merge of rust-lang#120502 - clubby789:remove-ffi-returns-twice, r=compiler-errors Remove `ffi_returns_twice` feature The [tracking issue](rust-lang#58314) and [RFC](rust-lang/rfcs#2633) have been closed for a couple of years. There is also an attribute gate in R-A which should be removed if this lands.
…ler-errors Remove `ffi_returns_twice` feature The [tracking issue](rust-lang/rust#58314) and [RFC](rust-lang/rfcs#2633) have been closed for a couple of years. There is also an attribute gate in R-A which should be removed if this lands.
Rendered.
Implementation PR.
Closes #2625.