Boost logo

Boost :

From: Christian Mazakas (christian.mazakas_at_[hidden])
Date: 2024-07-10 17:10:43


> Andrey Semashev via Boost:
> With volatile, the compiler is not allowed to optimize away or reorder
> loads and stores of the variable. There's no restriction on what the
> compiler is allowed to do with the loaded value.

It could insert a branch against `memset` but that seems really only like a
theoretical
optimization.

This is an example where the volatile preserves the secure_erase() call:
https://godbolt.org/z/854jfarb9

Kind of neat trying to get the compiler to elide this stuff. You were
right, Andrey, you really need
LTO and LTCG and the like to get this kind of behavior. Super neat stuff.

Idk, seems like whatever OpenSSL is doing is Sound Enough in practice.

I'd be curious to know if it's actually sound for a compiler to make that
kind of optimization or at least
what are the limits on how far a compiler can actually go to ensure
soundness. If you know those limits,
you can probably make this pattern work reliably.

- Christian


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk