-
Notifications
You must be signed in to change notification settings - Fork 346
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
Function pointer provenance #2926
Comments
It's not clear if function pointers have provenance. So nobody is really sure what the rules are. However...
Yes. The fact that the functions are "in the binary" is irrelevant. You are trying to use an observation about a representation that Rust can be lowered to in order to make an argument about Rust. Similarly, lifetime is also irrelevant. Pointers to The reason that it's unclear if function pointers should have provenance or not is that it's not clear if giving function pointers provenance permits any useful optimizations; the properties of an executable have nothing to do with it. If you transmute to If you really need pass this around as a fn foo(x: i32) -> i32 {
x * 2
}
pub fn main() {
let foo_addr: *const () = unsafe {
std::mem::transmute::<fn(x:i32)->i32, *const ()>(foo)
};
let exposed = foo_addr as usize;
let unexposed = exposed as *const ();
let foo_restored: fn(x:i32)->i32 = unsafe {
std::mem::transmute::<*const (), fn(x:i32)->i32>(unexposed)
};
println!("{}", foo_restored(2));
} |
Yeah, the recommended way to handle function pointers is to always transmute to/from raw pointers, and then use raw pointer APIs from there on. |
It's not as if I want to convert function pointer to I just have bunch of functions which receive It's easily doable in Rust without Miri, but I don't know how to do that with Miri. I tried to change your code just a tiny bit and it gives warnings:
And if I try to add provenance it no longer works:
|
Indeed, storing pointers in integers will warn in Miri. The warning explains why:
If you don't want to use
Yeah, you are using |
Due to provenance, The way to avoid this loss of provenance and avoid the warning is to use a different type, such as |
That's what I have already did. As you can guess I don't have tons of these casts spread around, there are just one function which adds function plus slice to the buffer and another which pulls it out and calls it. I just added hashmap from address of function to function and miri is obviously happy. Just looked a bit silly for me to do all that dance with provenance for an object which is not ever allocated or deallocated thus I decided to ask here. But given then fact that I'm doing that for yet-another-JIT… I guess having provenance for functions is important. I just wish all statically allocated functions had the same provevance: they couldn't ever be allocated or deallocated, why would these need provenance? But then, one may argue that the same idea may be applicable to any But I guess that's low-priority issue. Thanks for the answers. |
Closing as "Miri behaves as intended"; rust-lang/unsafe-code-guidelines#340 is where we track whether function pointers should have provenance at all. |
I'm trying to pack bunch of function pointers to save memory and have hit strange issue:
Here Miri complains:
error: Undefined Behavior: out-of-bounds pointer use: 0x2794b[noalloc] is a dangling pointer (it has no provenance)
.But fn doesn't have
with_addr
or anything similar. And it's not clear if it even should have it: at least in my case all functions are coming from the binary and thus all functions pointers are'static
pointers… am I wrong? Should function pointers have provenance or not?I'm just not sure what's the actual rules here and how to fix the issue.
The text was updated successfully, but these errors were encountered: