Do Pascal Compilers Need a SecureZeroMemory Function?

Consider the code:

procedure DoSmthSecret; var Seed: array[0..31] of Byte; begin // get random seed .. // use the seed to do something secret .. // erase the seed FillChar(Seed, SizeOf(Seed), 0); end; 

Code issue: FillChar is an inline compiler and potentially the compiler can β€œoptimize it”. The problem is known for C / C ++ compilers, see SecureZeroMemory . Can such a Pascal compiler (Delphi, FPC) do such an optimization, and if so, can they provide the equivalent of SecureZeroMemory?

+6
source share
2 answers

FPC cannot do such optimizations at the moment, and afaik even with C ++ they are included in the "undefined" class. (since the state of the program because of this optimization ignores what the programmer says)

The solution to this problem is the question of determining which structures can be optimized and which not. It does not need the help of the API / OS as such, any object file associated with an external object with such a function (since then global optimization will not affect it)

Please note that the article does not specify the C ++ compiler, so I expect this to be a more general utility function when the compiler user gets into problems without hitting documents too hard or when it should work easily on several (windows-only!) compilers, without overcomplicating the assembly.

Choosing a non-inlinable API function may not be optimal in other cases, especially with small, constant sizes to zero, since it will not be built-in, so I will be careful with this function and make sure that there is a complex need

This can be important mainly when an external object can change the memory (DMA, memory mapping, etc.) of a program or erase passwords and other confidential information from a memory image, even if the program will never read it according to the compiler

+3
source

Even if FreePascal optimizes a write to memory that will never be read again (which I doubt the atm does, no matter how long you guys have been discussing this), it supports an absolute type modifier that it guarantees (documents ) never optimize (somewhat similar to volatile in C / C ++).

+1
source

All Articles