-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Code written in fn_ptr_trait is optimized away #116937
Comments
As the the To make them nullable follows what the lint is saying and wrapped your fn pointer into an - extern "C" { fn get_fn() -> extern "C" fn() -> i8; }
+ extern "C" { fn get_fn() -> Option<extern "C" fn() -> i8>; } ¹ was renamed to @rustbot labels -needs-triage -C-bug |
And relatedly, the code that runs if they are null would only run if UB occurred, so the optimizer is deleting it all in the name of speed. |
I can understand what the Rust optimizer is doing, and I agree with it, after all, rs requires that the function pointer cannot be null. In fact, the "Option" mentioned in lint is not available, because cbindgen cannot work with such a signature, it will generate an Option_xxx type in function signature, but Option does not exist in C. But on another note, it's a common fact from C that function pointers may be null. rs Can special optimization be done to extern "C" fn to avoid invalidating the null pointer check, or adding another #[nullable] function signature should also be possible? |
Currently I have bypassed this problem with read_volatile. If rs is not going to "fix" this problem, it would be nice to add proper documentation to the addr() function to guide users to other methods. |
ref: #40913 |
You only have to express the
This is still UB.
This seems to be a bug to me. That should be reported to the |
rustc version:
I'm currently writing ffi related code. Some of the codes use function pointers from the C language. There are a small number of function pointers that need to be judged to be null pointers, so I used fn_ptr_trait to get the function address. This works fine in debug mode. However, in release mode all relevant code is optimized away. How can I stop this behavior?
The text was updated successfully, but these errors were encountered: