Disable optimizations on one function (was: 'pragma optimize' ...)
Martin Sebor
msebor@gmail.com
Thu Jul 16 16:32:00 GMT 2015
On 07/15/2015 11:58 PM, Jeffrey Walton wrote:
>>> I have one more question related to the use of '#pragma GCC optimize'.
>>> I have a zeroizer that must execute due to Certification and
>>> Accreditation (C&A) requirements. When the zeroizer is removed as a
>>> dead store, the compiler folks usually say, "but you asked for
>>> optimizations...".
>>>
>>> If we cannot use '#pragma GCC optimize' to turn off the optimizer for
>>> a function, then how do we tell the compiler to *not* remove the code?
>>
>> It is hard to tell without seeing the code. But see noclone, noinline
>> entries at:
>> http://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html
>>
>
> As a simplified example, imagine I have a function ClearAndFree:
>
> void ClearAndFree(void* ptr, size_t n)
> {
> if(!ptr) return;
>
> memset(ptr, 0x00, n);
> free(ptr);
> }
>
> We've seen these type of functions have the memset optimized away.
(This is why the memset_s function was added to the C standard.)
The call to memset is optimized away because GCC knows both its
semantics and those of free. To prevent the memset from being
optimized away you must make it "believe" the semantics could
be different. There are a number of ways to do that: compiling
the function with the -fno-builtin-memset option (or one it's
enabled by) is one. Another is by hiding the memset call somehow,
e.g., by calling the function via a pointer initialized outside
the calling function, or by calling the function from an asm
statement.
Martin
More information about the Gcc-help
mailing list