-
Notifications
You must be signed in to change notification settings - Fork 515
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
Modifying locals potentially broken with recent Rust #236
Comments
I have the same issue. I am pretty new to the topic, but as far as I understand my findings, I think your assumption is correct that the compiler is too smart in this case. |
I played around with it some more and found that declaring
As much as I think playing around with this feature at that point in the tutorial is very interesting, I am not sure it justifies using strange code like that - unless it is explained in some detail what is happening without it. |
Thanks for looking into assembly and confirming my hypothesis. Your code makes sense but is indeed "strange". I wondered if one could make the variable volatile as it is also done in the register section of the book. I found the volatile crate, which could be used for that. Still, a developer would not per-se make this variable volatile, as it actually is not expected to change without doing live-rewrite in the debugger. But that would at least provide similar "changability" while having less contrived code. Maybe leveraging |
I also just ran into this issue. @wupdiduh thanks for finding a workaround. I had to modify it a bit:
I had to use half_period before changing the value, so the compiler wouldn't optimize it away. |
From a thread in the Rust forum: I believe this is a result of MIR constant propagation, which is done by rustc before it generates LLVM IR and runs LLVM's optimization passes. In recent versions of rustc, MIR constant propagation is enabled by default in debug mode because it reduces compile times. You can disable it using the |
Unfortunately using |
Here are a couple other solutions I came up with.
Cargo.toml to add
And the main.rs is now:
Change half_period with:
|
I'm working through the discovery book and just did the
gdb
session in this chapter.What I found is that while
info locals
properly reports my change tohalf_period
, the behaviour did not change. So I went through calls to thedelay_ms
methods again and found that the calling parameter is still the original value of 500.What I am wondering is if this is due to Rust recent changes with respect to constant value optimizations. Could it be that the
half_period
(which is a primitive type and only read twice and never written) is optimized away? I can imagine that it would be sufficient to write the value somewhere to the binary and letdelay_ms
access it directly without doing the usual stack-frame handling.My questions are:
half_period
so that it is really modifiable interactively ingdb
?I guess fixing this would save others from trying out this example and wondering why it does not work.
The text was updated successfully, but these errors were encountered: