Re: Cleanup for cryptographic algorithms vs. compiler optimizations

From: Tijl Coosemans <tijl_at_coosemans.org>
Date: Fri, 11 Jun 2010 23:04:10 +0200
On Friday 11 June 2010 21:37:29 Dag-Erling Smørgrav wrote:
> Ulrich Spörlein <uqs_at_spoerlein.net> writes:
>> optimizing compilers have a tendency to remove assignments that have
>> no side effects. The code in sys/crypto/sha2/sha2.c is doing a lot
>> of zeroing variables, which is however optimized away.  [...]  Is
>> there a canonical way to zero those variables and should we use them
>> (memset perhaps? what are the performance implications?)
> 
> If you stick these variables in a struct, you can memset the struct
> to zero them; if there are many of them, it may be faster than
> zeroing them individually.
> 
> Alternatively, you can use something like this:
> 
> #define FORCE_ASSIGN(type, var, value) \
>         *(volatile type *)&(var) = (value)

memset can be optimised away as well. The only way is to declare those
variables volatile. None of the assignments below are removed for
instance:

volatile int a = 0; a = 1; a = 2;
Received on Fri Jun 11 2010 - 19:04:15 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:40:04 UTC