Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Or just disable the fucking broken optimizer for such a function.

    #pragma GCC optimize ("O0")


Disabling optimizations does not necessarily result in more deterministic execution.

With "-O0", the generated code normally retains a huge number of useless register loads and stores, which lead to non-deterministic timing due to contention in the use of caches and of the main memory interface. Optimized code may run only inside registers, being thus executed in constant time regardless of what other CPU cores do.

The only good part is that this non-deterministic timing will not normally depend on the data values. The main danger of the non-constant execution time is when this time depends on the values of the processed data, which provides information about those values.

There are cases when disabling optimization may cause data-dependent timing, e.g. if with optimization the compiler would have chosen a conditional move and without optimization it chooses a data-dependent branch.

The only certain way of achieving data-independent timing is to use either assembly language or appropriate compiler intrinsics.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: