Question

I am using Delphi 7 IDE. Does Delphi compiler optimize codes, just like what the C++ compiler is doing in this following link?

http://msdn.microsoft.com/en-us/library/aa366877(VS.85).aspx

WCHAR szPassword[MAX_PATH];
// Retrieve the password
if (GetPasswordFromUser(szPassword, MAX_PATH))    
   UsePassword(szPassword);
// Clear the password from memory
SecureZeroMemory(szPassword, sizeof(szPassword));

If ZeroMemory were called in this example instead of SecureZeroMemory, the compiler could optimize the call because the szPassword buffer is not read from before it goes out of scope. The password would remain on the application stack where it could be captured in a crash dump or probed by a malicious application.

Was it helpful?

Solution

Yes, of course Delphi performs optimizations. However, it does not perform the optimization that the SecureZeroMemory function is meant to circumvent. There is no need to use that function in Delphi; just use plain old ZeroMemory, or even FillChar. They're not macros, and they don't do anything that Delphi recognizes as being unused assignment statements that could get optimized out.

OTHER TIPS

Delphi performs code optimization by default, you can disable it in Project > Options > Compiler.

The Delphi help provide a few tips of what type of optimizations are used:

The $O directive controls code optimization. In the {$O+} state, the compiler performs a number of code optimizations, such as placing variables in CPU registers, eliminating common subexpressions, and generating induction variables.

It also states that "the compiler performs no "unsafe" optimizations", but in the sense that they won't alter the execution path, not from a security point of view.

I don't believe the compiler will ever eliminate apparently dead code like this. I have never had trouble setting breakpoints on code that could have been eliminated as redundant.

For some scenarios, the compiler can detect if the code is unreachable and eliminate the code.

For instance, the compiler correctly eliminates the "unreachable" portion of the code below.
It will not generate code for that line so:

  1. So there are no blue bullets indicating there is code
  2. Breakpoints put on that line will be marked visually as 'not reachable'

Just tested in Delphi XE, but older Delphi versions have similar behaviour.

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

procedure Test;
begin
  if (True = False) then
    Writeln('Unreachable')
  else
    Writeln('Reachable');
end;

begin
  try
    Test();
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

It takes quite some while to learn when (or when not) the optimizer on code level and liker level kicks in.

For instance: When you have optimizations turned on, the compiler will also eliminate variables as soon as they are not used.
Sometimes, it even eliminates global symbols. Danny Thorpe (former Delphi Compiler engineer and Chief Scientist) once wrote a magic method Touch that prevents this.
Just call this Touch method at the end of your method to fool the optimizer during debugging:

procedure Touch(var arg);
begin
end;

--jeroen

Delphi certainly optimizes code (it is a modern, and excellent, compiler). Another example of optimization deleting lines is:

SomeFunction();  // Set breakpoint here, then step (F10)
myInt := 7;      // Next line will not hit this...
myInt := 13;     // ...but will instead skip to here

I like to ensure optimization is in the correct state (and not accidentally left switched on or off) by adding {$I MyProjectOptions.inc} in every .pas file in my project. This goes just below the unit name (right at the top of the file). In "MyProjectOptions.inc" you simply add this code:

// Is this a debug or non-debug build?
{$IF Defined(DEBUG)}
    {$O-}   // Turn optimization off
{$ELSEIF Defined(NDEBUG)}
    {$O+}   // Ensure optimisation is on
{$IFEND}

Finally, ensure you have defined "DEBUG" and "NDEBUG" (or your equivalent in older versions of Delphi) in the Conditional defines section of Project > Options > Diectories/Conditionals.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top