Comment 2 for bug 1968584

Revision history for this message
Liviu Ionescu (ilg) wrote (last edit ):

I did some further tests and this is definitely a bug in GCC, but it is not as severe as I was afraid initially, since it does not affect usual code.

With -O2, the compiler optimises loops that set a memory area by internally calling memset(), instead of generating code for the loop.

Unfortunately it does not check that the current function name is... memset() :-(

The result is that memset() calls memset(), and since the function prologue pushes something on the stack, you get a stack overflow, but otherwise you'll probably get an infinite loop.

To verify this, add a memset2() function with exactly the same content as memset(), and you'll see that the generated code is identical, except that memset2() calls memset(), while memset() calls itself.

To conclude, the bug is a missing check, since the compiler should not apply the optimisation to replace loops with calls to memset(), if the function is called memset() itself.

I would expect that a similar problem occurs for other such optimisations, like memcpy() and possibly for memmove().

As a workaround, if you really want to redefine the memset() function, add some pragmas and temporarily disable this optimisation.

Although redefining these functions occurs rarely, it would still be nice for Arm to fix this bug in the next release, or at least to list it as a known issue.