Warm tip: This article is reproduced from stackoverflow.com, please click
c r tcl

Debugging .dll with R application attachment produces "Illegal Instruction" error

发布于 2020-05-04 14:56:47

The program I am debugging is an R GUI application that utilizes several C .dll files to perform tasks. I am using Visual Studio 2019 as my debugger.

The error: Unhandled exception at 0x00000000279BB8CC (tcl86.dll) in rsession.exe: 0xC000001D: Illegal Instruction.

I am not sure how to look deeper into why tcl86.dll is acting up as it is.

This is my first time debugging a program, and it happens to be a fairly convoluted one, so I am not sure how to interpret the output I am given. I will share as much as I can, and let me know if I need to post more information.

Here is the call stack:

    tcl86.dll!00000000279bb8cc()    Unknown Non-user code. Binary was not built with debug information.
    tcl86.dll!000000002797f8b5()    Unknown Non-user code. Binary was not built with debug information.
    tcl86.dll!000000002797f9d2()    Unknown Non-user code. Binary was not built with debug information.
    tcl86.dll!0000000027999606()    Unknown Non-user code. Binary was not built with debug information.
    tcl86.dll!0000000027999c2b()    Unknown Non-user code. Binary was not built with debug information.
>   tkogl2.dll!del(void * clientData, Tcl_Interp * interp, int objc, Tcl_Obj * const * objv) Line 1148  C   Symbols loaded.
    tcl86.dll!00000000278a7a07()    Unknown Non-user code. Binary was not built with debug information.
    tcltk.dll!000000006c302378()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c7a6542()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c7f3249()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c801fe1()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c80268c()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c802f2a()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c7f6733()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c801fe1()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c803a98()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c804b1f()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c7f8bdf()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c801fe1()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c803a98()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c804b1f()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c7f8bdf()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c801fe1()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c803a98()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c804b1f()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c7f8bdf()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c801fe1()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c803a98()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c804b1f()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c7f8bdf()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c801fe1()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c803a98()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c804b1f()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c7f8bdf()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c801fe1()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c803a98()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c804b1f()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c801fa2()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c80268c()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c802f2a()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c7f6733()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c801fe1()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c80268c()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c802f2a()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c7f6733()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c801fe1()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c80268c()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c802f2a()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c7f6733()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c801fe1()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c80268c()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c802f2a()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c7f6733()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c801fe1()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c80268c()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c802f2a()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c7f6733()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c801fe1()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c803a98()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c804b1f()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c7f8bdf()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c801fe1()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c803a98()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c804b1f()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c7f8bdf()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c801fe1()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c803a98()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c804b1f()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c7f8bdf()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c801fe1()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c803a98()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c804b1f()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c7f8bdf()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c801fe1()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c803a98()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c804b1f()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c801fa2()    Unknown Non-user code. Binary was not built with debug information.
    tcltk.dll!000000006c301aae()    Unknown Non-user code. Binary was not built with debug information.
    tcl86.dll!00000000278a2492()    Unknown Non-user code. Binary was not built with debug information.
    tcl86.dll!00000000278a7a07()    Unknown Non-user code. Binary was not built with debug information.
    tcl86.dll!00000000278aa454()    Unknown Non-user code. Binary was not built with debug information.
    tk86.dll!0000000062982874() Unknown Non-user code. Binary was not built with debug information.
    tk86.dll!00000000629170bf() Unknown Non-user code. Binary was not built with debug information.
    tk86.dll!000000006292111f() Unknown Non-user code. Binary was not built with debug information.
    user32.dll!00007ffea7315b6d()   Unknown Non-user code. Cannot find or open the PDB file.
    user32.dll!00007ffea7315562()   Unknown Non-user code. Cannot find or open the PDB file.
    Rgraphapp.dll!000000006355751b()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c721365()    Unknown Non-user code. Binary was not built with debug information.
    rsession.exe!000000000085b0e9() Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c796727()    Unknown Non-user code. Binary was not built with debug information.
    rsession.exe!000000000085d670() Unknown Non-user code. Binary was not built with debug information.
    rsession.exe!000000000044de43() Unknown Non-user code. Binary was not built with debug information.
    rsession.exe!00000000004194b7() Unknown Non-user code. Binary was not built with debug information.
    rsession.exe!0000000000894845() Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c829772()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c829a01()    Unknown Non-user code. Binary was not built with debug information.
    R.dll!000000006c829a92()    Unknown Non-user code. Binary was not built with debug information.
    rsession.exe!00000000008a9801() Unknown Non-user code. Binary was not built with debug information.
    rsession.exe!00000000008915cf() Unknown Non-user code. Binary was not built with debug information.
    rsession.exe!0000000000bedf98() Unknown Non-user code. Binary was not built with debug information.
    rsession.exe!00000000004013e8() Unknown Non-user code. Binary was not built with debug information.
    rsession.exe!000000000040151b() Unknown Non-user code. Binary was not built with debug information.
    kernel32.dll!00007ffea5b47bd4() Unknown Non-user code. Cannot find or open the PDB file.
    ntdll.dll!00007ffea754ced1()    Unknown Non-user code. Cannot find or open the PDB file.

Here is the output in the debugger: alloc: invalid block: 000000002BF31390: 0 2bException thrown at 0x00000000279BB8CC (tcl86.dll) in rsession.exe: 0xC000001D: Illegal Instruction. Unhandled exception at 0x00000000279BB8CC (tcl86.dll) in rsession.exe: 0xC000001D: Illegal Instruction.

Here is a bit of code where the debugger stopped in the C code:

else if (strcmp(shape, "specimens") == 0)
    {
        specimen_del(0);

        if (models != NULL)
        {
            Tcl_Free((char*)models);
            models = NULL; //error symbol here
        }
        if (context != NULL)
        {
            Tcl_Free((char*)context);
            context = NULL;
        }
    }

The goal of this post is to understand how to interpret the error and information I am given by the debugger and potential solutions I may be able to explore. Thank you!

Questioner
hkj447
Viewed
28
Donal Fellows 2020-02-19 03:56

The first part of that message, alloc: invalid block: 000000002BF31390: 0 2b, is a message out of the Tcl library. The meaning of the message is that memory has been corrupted by an out-of-bounds memory write. The location of the generation of that message is in Tcl's thread-aware memory allocator, but is not the place where the bug is: it's just where the problem was detected. Once memory is corrupted, all bets are off about the future behaviour of the program; dying with an illegal instruction error is entirely possible (which could be due to a corrupted stack).

Neither the Tcl nor the Tk DLLs (specifically tcl86.dll and tk86.dll in your stack trace) will create out of bounds accesses if memory is not corrupted: we really do check for this sort of thing very carefully (and have developer build modes that make any such problems show up, at least in their implementation). However, if other code writes where it shouldn't (and there's plenty of code in there that I cannot vouch for at all) then all bets are off because the preconditions for the correctness of the code no longer hold.

tl;dr You've got a memory corruption problem. Which sucks a lot.

Try a static build of your application with a memory access tracing tool like electricfence or purify (there may be others too, this isn't intended to be an exhaustive list); the performance hit for doing that is profound, but the chances of it finding the bug itself are reasonably high.