[NSWI004] Is it test error or knowledge error?
Petr Tuma
petr.tuma at d3s.mff.cuni.cz
Tue Oct 13 18:37:58 CEST 2020
Hi,
your code attempts to return a local variable address as the stack pointer address. While the idea is relatively fine (stack does work that way), compiler optimizations get in the way - the assembly view reveals that the compiler considers the condition statically false (that is, there is no actual test performed at runtime, the message is always printed).
For a deeper explanation, look at the following quote from the C language standard: "If an object is referred to outside of its lifetime, the behavior is undefined. The value of a pointer becomes indeterminate when the object it points to reaches the end of its lifetime."
Essentially, what you are doing in your code is trying to use the value of a pointer beyond the lifetime of the object it refers to. The compiler is free to assume that the values in sp1 and sp2 will always differ, because they are undefined per the standard - and hence it is free to assume that `sp1 == sp2` will always be false (because it very well could be, according to the standard).
While you could probably try to obfuscate your code some more to get it to work (for example store the pointer value into a volatile variable and then read it back, and then do some more work to prevent inlining from reserving multiple frames on the stack), it is much better to follow the recommendation in the assignment (register keyword etc.).
Petr
On 13/10/2020 18:05, Jura Pelc wrote:
> It is not the final version but I committed it.
>
> út 13. 10. 2020 v 17:58 odesílatel Petr Tuma <petr.tuma at d3s.mff.cuni.cz <mailto:petr.tuma at d3s.mff.cuni.cz>> napsal:
>
> Hi,
>
> you're right in that the behavior is fishy, but the assert statement is
> fine (it should trigger when the specified condition is _not_ met).
>
> From what I saw in other cases, the compiler is quite willing to
> optimize this particular code very aggresively, which might explain the
> strange behavior you observe - in the other cases, what we had to do was
> look at the assembly to understand what is happening. If your code is
> current in the repo, we can take a peek here too.
>
> Petr
>
>
> On 13/10/2020 17:31, Jura Pelc wrote:
> > I don't think they should differ and in my result, they don't. But it
> > still generates this error.
> > I am not sure why I see this error when sp1 and sp2 are equal.
> >
>
> --
> Petr Tuma
> Distributed and Dependable Systems
> Faculty of Mathematics and Physics
> Charles University, Czech Republic
> http://d3s.mff.cuni.cz <http://d3s.mff.cuni.cz>
>
>
>
> --
>
> S pozdravem Jiří Pelc
> Matematicko-fyzikální fakulta
> Univerzita Karlova
--
Petr Tuma
Distributed and Dependable Systems
Faculty of Mathematics and Physics
Charles University, Czech Republic
http://d3s.mff.cuni.cz
More information about the NSWI004
mailing list