Embarcadero's WinApi.D2D1.pas in XE6 and XE7 is flawed, incomplete, and wrong. This demo includes my attempts to repair their mistakes, but my repairs might be incomplete as well, and this may be the underlying reason for this crash.
The demo links in a resource file which contains a truetype font. The demo code loads the font into DirectWrite and uses it to draw an icon-glyph on the screen using Direct Write.
The crash I see occurs in module finalization scope. I had to force it to occur in this case, by forcing something in a module finalization scope to make use of the string heap. The code that was chosen to allow fastMM to detect the problem, is in a unit called DemoHeapCorruptionDetectUnit.pas.
What is odd here is that the access violations that occur are unintentional ones caused inside the FastMM4 codebase itself.
project DirectWriteDemo.exe raised exception class $C0000005 with message 'access violation at 0x01303d35: read of address 0x6b69dd4b':
The debugger opens at this code around line 277 of FastMM_FullDebugMode.dpr in function IsValidCallSite:
The problem is only reproducible when we use DirectWrite APIs. This is made more difficult by the fact that Embarcadero did not even ship a working DirectWrite API header translation. Our repairs may be only partial here and we might be breaking things that way.
My questions are:
Is this a phantom problem in FastMM_FullDebugMode.dpr's code? Or is there any reason to suspect real evidence of heap corruption? How would I figure that out?
Has anyone else ever gotten here, and what did you do in the end? Decide that this is a phantom (FastMM full debug mode failure) or that real heap corruption is occurring?
Warren Postma
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Note: By phantom, I mean, that it is the nature of FastMM_FullDebugMode's code, which has the unenviable job of determining if an address is valid, that it must detect it by generating access violations, and that is the purpose of IsValidCallSite.
So, as a developer, my first instincts are wrong, when I suspect something is wrong when the stack trace code must detect invalid addresses, and in doing so, a first chance exception is raised that HAD to be raised in order to detect the stop condition of a stack traceback.
However, as I have never seen anything this weird before, and as I captured this reproducible example, I'm asking the gurus here if they suspect anything is wrong in my heap, or if everything is cool. How do I know?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The other crash, that occurs though, is in FastMM4.pas itself, and this one reproduces for me in Delphi XE6, but not in Delphi XE7:
function DebugGetMem(ASize: {$ifdef XE2AndUp}NativeInt{$else}Integer{$endif}): Pointer;
begin
{Scan the entire memory pool first?}
if FullDebugModeScanMemoryPoolBeforeEveryOperation then
ScanMemoryPoolForCorruptions;
{Enter the memory manager: block scans may not be performed now}
StartChangingFullDebugModeBlock;
try
{We need extra space for (a) The debug header, (b) the block debug trailer
and (c) the trailing block size pointer for free blocks}
Result := FastGetMem(ASize + FullDebugBlockOverhead);
if Result <> nil then
begin
{Large blocks are always newly allocated (and never reused), so checking
for a modify-after-free is not necessary.}
if (ASize > (MaximumMediumBlockSize - BlockHeaderSize - FullDebugBlockOverhead))
or CheckFreeBlockUnmodified(Result, GetAvailableSpaceInBlock(Result) + BlockHeaderSize, boGetMem) then
begin
{Set the allocation call stack}
GetStackTrace(@PFullDebugBlockHeader(Result).AllocationStackTrace, StackTraceDepth, 1);
{Set the thread ID of the thread that allocated the block}
PFullDebugBlockHeader(Result).AllocatedByThread := GetThreadID; <<< CRASH
The above crash does NOT seem to be an intentional crash, and essentially makes the heap inoperable, and crashes my application:
Now I'm thinking that all I see above is FastMM working PERFECTLY, and me being confused by Delphi XE6's debugger glitches in handling SEH, and first-chance access violations.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
A self contained example/demo program is available (zip file, should open and build in Delphi XE6 and XE7).
https://drive.google.com/file/d/0B07SrKpE8ErmeTdMbHJGbFBJekk/view?pli=1
Notes on this issue:
Embarcadero's WinApi.D2D1.pas in XE6 and XE7 is flawed, incomplete, and wrong. This demo includes my attempts to repair their mistakes, but my repairs might be incomplete as well, and this may be the underlying reason for this crash.
The demo links in a resource file which contains a truetype font. The demo code loads the font into DirectWrite and uses it to draw an icon-glyph on the screen using Direct Write.
The crash I see occurs in module finalization scope. I had to force it to occur in this case, by forcing something in a module finalization scope to make use of the string heap. The code that was chosen to allow fastMM to detect the problem, is in a unit called DemoHeapCorruptionDetectUnit.pas.
What is odd here is that the access violations that occur are unintentional ones caused inside the FastMM4 codebase itself.
project DirectWriteDemo.exe raised exception class $C0000005 with message 'access violation at 0x01303d35: read of address 0x6b69dd4b':
The debugger opens at this code around line 277 of FastMM_FullDebugMode.dpr in function IsValidCallSite:
At the time of the crash, the access violations are either here:
FastMM_FullDebugMode.IsValidCallSite(???) (line 277)
FastMM_FullDebugMode.GetRawStackTrace($7FE7DEA0,4,0)
FastMM4.DebugGetMem(90)
System._GetMem(???)
System._NewUnicodeString(???)
:004069ea @GetMem + $A
DemoHeapCorruptionDetectUnit.Finalization
System.FinalizeUnits
System._Halt0
Or here, near line 8739, if FullDebugModeScanMemoryPoolBeforeEveryOperation is True;
:01353d35 ; C:\dev\DirectWriteDelphi\Demo\Win32\DebugWithWorkaround\FastMM_FullDebugMode.dll
:01353f89 FastMM_FullDebugMode.GetRawStackTrace + 0xc9
FastMM4.DebugGetMem(???)
The problem is only reproducible when we use DirectWrite APIs. This is made more difficult by the fact that Embarcadero did not even ship a working DirectWrite API header translation. Our repairs may be only partial here and we might be breaking things that way.
My questions are:
Is this a phantom problem in FastMM_FullDebugMode.dpr's code? Or is there any reason to suspect real evidence of heap corruption? How would I figure that out?
Has anyone else ever gotten here, and what did you do in the end? Decide that this is a phantom (FastMM full debug mode failure) or that real heap corruption is occurring?
Warren Postma
Note: By phantom, I mean, that it is the nature of FastMM_FullDebugMode's code, which has the unenviable job of determining if an address is valid, that it must detect it by generating access violations, and that is the purpose of IsValidCallSite.
So, as a developer, my first instincts are wrong, when I suspect something is wrong when the stack trace code must detect invalid addresses, and in doing so, a first chance exception is raised that HAD to be raised in order to detect the stop condition of a stack traceback.
However, as I have never seen anything this weird before, and as I captured this reproducible example, I'm asking the gurus here if they suspect anything is wrong in my heap, or if everything is cool. How do I know?
The other crash, that occurs though, is in FastMM4.pas itself, and this one reproduces for me in Delphi XE6, but not in Delphi XE7:
function DebugGetMem(ASize: {$ifdef XE2AndUp}NativeInt{$else}Integer{$endif}): Pointer;
begin
{Scan the entire memory pool first?}
if FullDebugModeScanMemoryPoolBeforeEveryOperation then
ScanMemoryPoolForCorruptions;
{Enter the memory manager: block scans may not be performed now}
StartChangingFullDebugModeBlock;
try
{We need extra space for (a) The debug header, (b) the block debug trailer
and (c) the trailing block size pointer for free blocks}
Result := FastGetMem(ASize + FullDebugBlockOverhead);
if Result <> nil then
begin
{Large blocks are always newly allocated (and never reused), so checking
for a modify-after-free is not necessary.}
if (ASize > (MaximumMediumBlockSize - BlockHeaderSize - FullDebugBlockOverhead))
or CheckFreeBlockUnmodified(Result, GetAvailableSpaceInBlock(Result) + BlockHeaderSize, boGetMem) then
begin
{Set the allocation call stack}
GetStackTrace(@PFullDebugBlockHeader(Result).AllocationStackTrace, StackTraceDepth, 1);
{Set the thread ID of the thread that allocated the block}
PFullDebugBlockHeader(Result).AllocatedByThread := GetThreadID; <<< CRASH
The above crash does NOT seem to be an intentional crash, and essentially makes the heap inoperable, and crashes my application:
:00699099 ; C:\powerserver\demos\ramsoft-controls\icon font\IconFont\FastMM_FullDebugMode.dll
:006992e1 FastMM_FullDebugMode.GetRawStackTrace + 0xc5
FastMM4.DebugGetMem(???)
:004068f2 @GetMem + $A
DemoHeapCorruptionDetectUnit.TestFinalization
DemoHeapCorruptionDetectUnit.Finalization
I think that the above is an IDE bug, that the HANDLED exception inside the FastMM Full Debug mode.
Last edit: Warren 2014-12-16
Now I'm thinking that all I see above is FastMM working PERFECTLY, and me being confused by Delphi XE6's debugger glitches in handling SEH, and first-chance access violations.