FastMM / Discussion / Open Discussion: An Interesting Access Violation Crash inside FastMM Full Debug Mode, that occurs when I try to use DirectWrite.
Menu ▾ ▴

An Interesting Access Violation Crash inside FastMM Full Debug Mode, that occurs when I try to use DirectWrite.

Warren
2014-12-16
2014-12-16
  • Warren

    Warren - 2014-12-16

    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:

    1. 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.

    2. 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.

    3. 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.

    4. 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:

    {5 bytes, CALL NEAR REL32}
    if PByteArray(LCallAddress)[3] = $E8 then
    begin
    

    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;

    PFullDebugBlockHeader(Result).AllocatedByThread := GetThreadID;
    

    :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:

    1. 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?

    2. 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

     
  • Warren

    Warren - 2014-12-16

    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?

     
  • Warren

    Warren - 2014-12-16

    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
  • Warren

    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.

     

Log in to post a comment.