-
Notifications
You must be signed in to change notification settings - Fork 13.3k
A way to force the compiler to accept falsely named "Undefined behavior" #63359
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
Comments
It would be good to know the type of However, I will caution you that "learning it the hard way" with UB isn't really possible; if your code has UB the compiler is free to do anything. It might even do what you expect it to do (today); but you cannot rely on this. For now, you can get this to work most likely be using a cc @RalfJung |
#63197 has more related discussion to UB from using arbitrary addresses. |
@Mark-Simulacrum I need to use const in my use case. The type of address is *const u8. Also, maybe it wouldn't be such a good idea to make this change universal, but instead to make it an option in the compiler to simply disable this type of error. |
What is your use case? Why can't you use a |
@Mark-Simulacrum I'm trying to directly execute hex opcodes in rust. For that, I need the data to be in the text section of the .o file output, which is why I need |
The data being in the text section is independent of where the address to said data is stored. How are you putting the |
@Mark-Simulacrum Data in the data section cannot be executed, however the data in the text section can be (on Linux, which is my environment). I'll take a look at users.rust-lang.org. |
UB doesn't imply the compiler will "miscompile" your code. It means your compiler may "miscompile" your code. ("miscompile" in quotes because there is actually no miscompilation going on here -- if the code is UB, the compiler can output anything, including invalid machine code, and it still constitutes are correct compilation.) It also means how your code compiles may change any time. It is fundamentally impossible to test for that by only considering the generated binary. You can learn UB by reading the documentation and playing with tools like Miri, but it is impossible to learn UB from what the compiler does. Also see my recent blog post on the topic:
|
Note that only the bytecode of your function needs to be in text. There's no need to have the function pointer in text |
I'm trying to execute some data in Rust, however I'm getting this error:
Here's the part of my code that matters:
In my opinion, there should be a way to force the compiler to accept something like this, because I'm very certain that this is not undefined behavior. Even if it is, I would like to know for certain by learning it the hard way, but sadly the compiler doesn't even let me compile it, so I can't.
The text was updated successfully, but these errors were encountered: