runtime icon indicating copy to clipboard operation
runtime copied to clipboard

[ARM32] System.AccessViolationException during exception handling

Open parjong opened this issue 10 years ago • 15 comments

On ARM32, CoreCLR currently shows unexpected behavior for the following code:

using System;

namespace ConsoleApplication { public class Program {

  public static void Begin()      { Console.WriteLine("BEGIN"); }
  public static void AfterTry()   { Console.WriteLine("AFTER TRY"); }
  public static void AfterCalc()  { Console.WriteLine("AFTER CALC"); }
  public static void AfterCatch() { Console.WriteLine("AFTER CATCH"); }
  public static void End()        { Console.WriteLine("END"); }

  public static void Main(string[] args) {
    Begin();

    try {
      AfterTry();

      checked
      {
        byte src = 1 + byte.MaxValue / 2;

        AfterCalc();

        byte res = (byte)( src + src );
      }
    } catch (System.OverflowException) {
      AfterCatch();
    }

    End();
  }

} }

When running the above code, CoreCLR shows the following outputs:

BEGIN
AFTER TRY
AFTER CALC

Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at ConsoleApplication.Program.Main(String[] args)

The normal behavior would be the following:

BEGIN
AFTER TRY
AFTER CALC
AFTER CATCH
END

parjong avatar Apr 19 '16 06:04 parjong

It seems that currently there is something wrong with EH funclet. I found that CoreCLR encounters access violation at the following point:

(gdb) bt
#0  0x71c274de in ?? ()
dotnet/coreclr#1  0x765445b8 in CallEHFunclet () at ehhelpers.S:468
dotnet/coreclr#2  0x76d60000 in ?? () from /mnt/dotnet-latest/libcoreclr.so
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
(gdb) info reg
r0             0x7260d010       1918947344
r1             0x71c274df       1908569311
r2             0x7effe27c       2130698876
r3             0x454e8  283880
r4             0x71c27075       1908568181
r5             0x7effefc0       2130702272
r6             0x0      0
r7             0x7effe9a0       2130700704
r8             0x0      0
r9             0x0      0
r10            0xe      14
r11            0x0      0
r12            0x76d603b0       1993737136
sp             0x7effda50       0x7effda50
lr             0x765445b9       1985234361
pc             0x71c274de       0x71c274de
cpsr           0x600b0030       1611333680
(gdb) x/16i $pc
=>   0x71c274de:  stmdb   sp!, {r2, r3, r4, r10, r11, lr}
   0x71c274e2:  add.w   r3, r11, dotnet/coreclr#8
   0x71c274e6:  str     r3, [sp, dotnet/coreclr#4]
  0x71c274e8:  str.w   r0, [r11, #-32]
   0x71c274ec:  nop
   0x71c274ee:  movw    r3, #28765      ; 0x705d
   0x71c274f2:  movt    r3, #29122      ; 0x71c2
   0x71c274f6:  blx     r3
   0x71c274f8:  nop
   0x71c274fa:  nop
   0x71c274fc:  nop
   0x71c274fe:  movw    r0, #29897      ; 0x74c9
   0x71c27502:  movt    r0, #29122      ; 0x71c2
   0x71c27506:  ldmia.w sp!, {r2, r3, r4, r10, r11, pc}
   0x71c2750a:  movs    r0, r0
   0x71c2750c:  lsls    r1, r2, dotnet/coreclr#1

Executing the instruction at 0x71c274e8 leads to invalid memory access (since R11 is 0).

parjong avatar Apr 19 '16 06:04 parjong

It looks like you've stripped out the binary and not included debug information of libcoreclr.so. There might be more information for the stacks from libcoreclr.so.

myungjoo avatar Apr 19 '16 08:04 myungjoo

It looks like r11 is being used as a frame pointer (r7 is FP in native world, r11 is FP in managed world) according to the instruction usage. We need to find out who has saved the context to r2 along with stacks beyond 2. (need to show the frame info as well)

myungjoo avatar Apr 19 '16 08:04 myungjoo

@myungjoo

It seems that calling to the managed code spoils the native call stack. CallEHFunclet (at src/vm/arm/ehhelpers.S) seems to jump into managed code whose address is provided via R1.

When it is called, GDB shows the following call stack:

(gdb) x/4i $pc
=> 0x76544758 <CallEHFunclet+4>:        sub     sp, dotnet/coreclr#4
   0x7654475a <CallEHFunclet+6>:        str.w   sp, [r3]
   0x7654475e <CallEHFunclet+10>:       ldmia.w r2, {r4, r5, r6, r7, r8, r9, r10, r11}
   0x76544762 <CallEHFunclet+14>:       blx     r1
(gdb) bt
#0  CallEHFunclet () at ehhelpers.S:1
dotnet/coreclr#1  0x7652a6fe in ExceptionTracker::CallHandler (this=0x453a0, uHandlerStartPC=1908569310, sf=..., pEHClause=0x453ec, pMD=0x75956964, funcletType=Catch, pContextRecord=0x7effe270)
    at /home/parjong/projects/toolchains/coreclr/src/vm/exceptionhandling.cpp:3331
dotnet/coreclr#2  0x7652a486 in ExceptionTracker::CallCatchHandler (this=0x453a0, pContextRecord=0x7effe270, pfAborting=0x7effddbc) at /home/parjong/projects/toolchains/coreclr/src/vm/exceptionhandling.cpp:574
dotnet/coreclr#3  0x7652b5e2 in ProcessCLRException (pExceptionRecord=0x7effe74c, MemoryStackFp=2130700784, pContextRecord=0x7effe270, pDispatcherContext=0x7effdf3c)
    at /home/parjong/projects/toolchains/coreclr/src/vm/exceptionhandling.cpp:1097
dotnet/coreclr#4  0x76530c6c in UnwindManagedExceptionPass2 (ex=..., unwindStartContext=0x7effe270) at /home/parjong/projects/toolchains/coreclr/src/vm/exceptionhandling.cpp:4413
dotnet/coreclr#5  0x76530fa0 in UnwindManagedExceptionPass1 (ex=..., frameContext=0x7effe4c8) at /home/parjong/projects/toolchains/coreclr/src/vm/exceptionhandling.cpp:4561
dotnet/coreclr#6  0x76531402 in DispatchManagedException (ex=...) at /home/parjong/projects/toolchains/coreclr/src/vm/exceptionhandling.cpp:4667
dotnet/coreclr#7  0x764818a2 in JIT_Overflow () at /home/parjong/projects/toolchains/coreclr/src/vm/jithelpers.cpp:5486
dotnet/coreclr#8  0x71c274be in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

The call stack is visible until "blk r1" is executed:

(gdb) x/4i $pc
=> 0x76544762 <CallEHFunclet+14>:       blx     r1
   0x76544764 <CallEHFunclet+16>:       add     sp, dotnet/coreclr#4
   0x76544766 <CallEHFunclet+18>:       ldmia.w sp!, {r4, r5, r6, r7, r8, r9, r10, r11, pc}
   0x7654476a <CallEHFilterFunclet>:    push    {lr}
(gdb) bt
#0  CallEHFunclet () at ehhelpers.S:468
dotnet/coreclr#1  0x7652a6fe in ExceptionTracker::CallHandler (this=0x453a0, uHandlerStartPC=1908569310, sf=..., pEHClause=0x453ec, pMD=0x75956964, funcletType=Catch, pContextRecord=0x7effe270)
    at /home/parjong/projects/toolchains/coreclr/src/vm/exceptionhandling.cpp:3331
dotnet/coreclr#2  0x7652a486 in ExceptionTracker::CallCatchHandler (this=0x453a0, pContextRecord=0x7effe270, pfAborting=0x7effddbc) at /home/parjong/projects/toolchains/coreclr/src/vm/exceptionhandling.cpp:574
dotnet/coreclr#3  0x7652b5e2 in ProcessCLRException (pExceptionRecord=0x7effe74c, MemoryStackFp=2130700784, pContextRecord=0x7effe270, pDispatcherContext=0x7effdf3c)
    at /home/parjong/projects/toolchains/coreclr/src/vm/exceptionhandling.cpp:1097
dotnet/coreclr#4  0x76530c6c in UnwindManagedExceptionPass2 (ex=..., unwindStartContext=0x7effe270) at /home/parjong/projects/toolchains/coreclr/src/vm/exceptionhandling.cpp:4413
dotnet/coreclr#5  0x76530fa0 in UnwindManagedExceptionPass1 (ex=..., frameContext=0x7effe4c8) at /home/parjong/projects/toolchains/coreclr/src/vm/exceptionhandling.cpp:4561
dotnet/coreclr#6  0x76531402 in DispatchManagedException (ex=...) at /home/parjong/projects/toolchains/coreclr/src/vm/exceptionhandling.cpp:4667
dotnet/coreclr#7  0x764818a2 in JIT_Overflow () at /home/parjong/projects/toolchains/coreclr/src/vm/jithelpers.cpp:5486
dotnet/coreclr#8  0x71c274be in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

Executing 'blk r1' spoils the call stack

(gdb) si
0x71c274de in ?? ()
(gdb) bt
#0  0x71c274de in ?? ()
dotnet/coreclr#1  0x76544764 in CallEHFunclet () at ehhelpers.S:468
dotnet/coreclr#2  0x76d60000 in ?? () from /mnt/dotnet-latest/libcoreclr.so
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

parjong avatar Apr 19 '16 09:04 parjong

GDB cannot track the managed stack. That's not a bug and that's why we need libsosplugin.so working for ARM.

myungjoo avatar Apr 19 '16 09:04 myungjoo

It looks like CallEHFunclet recoves r11 before calling EH Funclet. It includes ldm r2, {r4-r11}:

NESTED_ENTRY CallEHFunclet, _TEXT, NoHandler

...
str sp, [r3]
// apply the non-volatiles corresponding to the CrawlFrame
 ldm r2, {r4-r11}
 // Invoke the funclet
blx r1

It means that there is a memory chunk that stores R4 - R11. IMHO, it should be included in the code of Main function (as catch should use the context of Main). I checked the output from JIT (by setting COMPlus_JitDisasm as "Main"). Here is the output:

G_M28742_IG01:
000000  E92D 4C10      push    {r4,r10,r11,lr}
000004  B086           sub     sp, 24
000006  F10D 0B20      add     r11, sp, 32
00000A  2200           movs    r2, 0
00000C  9202           str     r2, [sp+0x08]    // [V01 loc0]
00000E  9201           str     r2, [sp+0x04]    // [V02 loc1]
000010  9200           str     r2, [sp] // [V03 tmp0]
000012  AA0A           add     r2, sp, 40
000014  9205           str     r2, [sp+0x14]    // [V05 PSPSym]
000016  9003           str     r0, [sp+0x0c]    // [V00 arg0]

G_M28742_IG02:
000018  F642 7398      movw    r3, 0x2f98
00001C  F2C7 538D      movt    r3, 0x758d
000020  681B           ldr     r3, [r3]
000022  2B00           cmp     r3, 0
000024  D004           beq     SHORT G_M28742_IG03
000026  F242 63F5      movw    r3, 0x26f5
00002A  F2C7 6340      movt    r3, 0x7640
00002E  4798           blx     r3               // CORINFO_HELP_DBG_IS_JUST_MY_CODE

G_M28742_IG03:
000030  BF00           nop
000032  F245 0339      movw    r3, 0x5039
000036  F2C7 030A      movt    r3, 0x700a
00003A  4798           blx     r3               // ConsoleApplication.Program:Begin()
00003C  BF00           nop

G_M28742_IG04:
00003E  BF00           nop
000040  F245 0345      movw    r3, 0x5045
000044  F2C7 030A      movt    r3, 0x700a
000048  4798           blx     r3               // ConsoleApplication.Program:AfterTry()
00004A  BF00           nop
00004C  BF00           nop
00004E  2380           movs    r3, 128
000050  9302           str     r3, [sp+0x08]    // [V01 loc0]
000052  F245 0351      movw    r3, 0x5051
000056  F2C7 030A      movt    r3, 0x700a
00005A  4798           blx     r3               // ConsoleApplication.Program:AfterCalc()
00005C  BF00           nop
00005E  9B02           ldr     r3, [sp+0x08]    // [V01 loc0]
000060  9A02           ldr     r2, [sp+0x08]    // [V01 loc0]
000062  189B           adds    r3, r3, r2
000064  D704           bvc     SHORT G_M28742_IG05
000066  F24E 63AD      movw    r3, 0xe6ad
00006A  F2C7 633F      movt    r3, 0x763f
00006E  4798           blx     r3               // CORINFO_HELP_OVERFLOW

G_M28742_IG05:
000070  F06F 02FF      mvn     r2, 255
000074  4213           tst     r3, r2
000076  D004           beq     SHORT G_M28742_IG06
000078  F24E 63AD      movw    r3, 0xe6ad
00007C  F2C7 633F      movt    r3, 0x763f
000080  4798           blx     r3               // CORINFO_HELP_OVERFLOW

G_M28742_IG06:
000082  9301           str     r3, [sp+0x04]    // [V02 loc1]
000084  BF00           nop
000086  BF00           nop
000088  BF00           nop
00008A  E7FF           b       SHORT G_M28742_IG07

G_M28742_IG07:
00008C  F245 0369      movw    r3, 0x5069
000090  F2C7 030A      movt    r3, 0x700a
000094  4798           blx     r3               // ConsoleApplication.Program:End()
000096  BF00           nop
000098  BF00           nop

G_M28742_IG08:
00009A  B006           add     sp, 24
00009C  E8BD 4C10      pop     {r4,r10,r11,lr}
0000A0  4770           bx      lr

G_M28742_IG09:
0000A2  E92D 4C1C      push    {r2,r3,r4,r10,r11,lr}
0000A6  F10B 0308      add     r3, r11, 8
0000AA  9301           str     r3, [sp+0x04]

G_M28742_IG10:
0000AC  F84B 0C20      str     r0, [r11-0x20]   // [V03 tmp0]
0000B0  BF00           nop
0000B2  F245 035D      movw    r3, 0x505d
0000B6  F2C7 030A      movt    r3, 0x700a
0000BA  4798           blx     r3               // ConsoleApplication.Program:AfterCatch()
0000BC  BF00           nop
0000BE  BF00           nop
0000C0  BF00           nop
0000C2  F245 40C9      movw    r0, LOW ADDRESS G_M28742_IG07
0000C6  F2C7 000A      movt    r0, HIGH ADDRESS G_M28742_IG07

G_M28742_IG11:
0000CA  E8BD 8C1C      pop     {r2,r3,r4,r10,r11,pc}

I guess that add r11, sp, 32 in G_M28742_IG01 initializes the frame pointer for Main function, but cannot find the code that stores r11 into somewhere.

parjong avatar Apr 19 '16 09:04 parjong

Check if the third argument of CallEhFunclet() from exceptionhandling.cpp:3331 is correct. r2 is the third argument of the function call. (r0=1st, r1=2nd, ... r3 = 4th)

myungjoo avatar Apr 19 '16 10:04 myungjoo

The 3rd argument fo CallEHFunclet seems to be correct (in my understanding). It comes from pContextRecord which is intended to capture the status of the first managed call stack.

        dwResumePC = CallEHFunclet((funcletType == EHFuncletType::Catch)?OBJECTREFToObject(throwable):(Object *)NULL, 
#ifdef _TARGET_ARM_
                        DataPointerToThumbCode<UINT_PTR, HandlerFn *>(pfnHandler), 
                        (UINT_PTR*)&(pContextRecord->R4), 
#else
                        (UINT_PTR)pfnHandler,
                        &(pContextRecord->X19), 
#endif // _TARGET_ARM_
                        pFuncletCallerSP);

It seems that PAL_VirtualUnwind failed to recover the value of registers. Here is the value of registers at various point:

; At the startup (at 000000 in IL)
(gdb) info reg
r0             0x0      0
r1             0x7efff11c       2130702620
r2             0x7efff00c       2130702348
r3             0x764856e9       1984452329
r4             0x71c27075       1908568181
r5             0x7effefc0       2130702272
r6             0x7effeea0       2130701984
r7             0x7effe9a0       2130700704
r8             0x76d60000       1993736192
r9             0x5e420800       1581385728
r10            0x76d60000       1993736192
r11            0x0      0
r12            0x71c2743d       1908569149
sp             0x7effe9a0       0x7effe9a0
lr             0x71c27447       1908569159
pc             0x71c27446       0x71c27446
cpsr           0x60010030       1610678320
; After the frame is initialized (at 000018 in IL)
(gdb) info reg
r0             0x0      0
r1             0x7efff11c       2130702620
r2             0x7effe9a0       2130700704
r3             0x764856e9       1984452329
r4             0x71c27075       1908568181
r5             0x7effefc0       2130702272
r6             0x7effeea0       2130701984
r7             0x7effe9a0       2130700704
r8             0x76d60000       1993736192
r9             0x5e420800       1581385728
r10            0x76d60000       1993736192
r11            0x7effe998       2130700696
r12            0x71c2743d       1908569149
sp             0x7effe978       0x7effe978
lr             0x71c27447       1908569159
pc             0x71c2745e       0x71c2745e
cpsr           0x60010030       1610678320
; Just before throwing exception (at 00007C in IL)
(gdb) info reg
r0             0x0      0
r1             0x0      0
r2             0xffffff00       4294967040
r3             0x16ad   5805
r4             0x71c27075       1908568181
r5             0x7effefc0       2130702272
r6             0x7effeea0       2130701984
r7             0x7effe9a0       2130700704
r8             0x76d60000       1993736192
r9             0x5e420800       1581385728
r10            0x76d60000       1993736192
r11            0x7effe998       2130700696
r12            0x76d603b0       1993737136
sp             0x7effe978       0x7effe978
lr             0x71c274a3       1908569251
pc             0x71c274c2       0x71c274c2
cpsr           0x10030  65584

However, when I checked pContextRecord before calling CallEHFunclet, it is not matched with any of them:

(gdb) p/x *pContextRecord
$2 = {ContextFlags = 0x20000b, R0 = 0x7effe478, R1 = 0x5e420800, R2 = 0x5e420800, R3 = 0x5e420800, R4 = 0x71c27075, R5 = 0x7effefc0, R6 = 0x0, R7 = 0x7effe9a0, R8 = 0x0, R9 = 0x0, R10 = 0xe, R11 = 0x0,
  R12 = 0x0, Sp = 0x7effe978, Lr = 0x71c274c9, Pc = 0x71c274c8, Cpsr = 0x600f0010, Fpscr = 0x80000010, Padding = 0x76e6c000, ... }

parjong avatar Apr 20 '16 03:04 parjong

@myungjoo Could you let me know if you have any progress with dotnet/runtime#5223?

It seems that I found the reason why the exception handling failed. When CoreCLR attempts to unwind the stack frame, unw_get_reg (which is used inside PAL_VirtualUnwind) produces incorrect values.

I found that the value of R6~R12 becomes invalid when CoreCLR attempted to unwind the following frame:

(gdb) info frame 0
Stack frame at 0x7effe628:
 pc = 0x76531296 in DispatchManagedException (/home/parjong/projects/toolchains/coreclr/src/vm/exceptionhandling.cpp:4639); saved pc = 0x764818a2
 called by frame at 0x7effe978
 source language c++.
 Arglist at 0x7effe620, args: ex=...
 Locals at 0x7effe620, Previous frame's sp is 0x7effe628
 Saved registers:
  r7 at 0x7effe620, lr at 0x7effe624

At the beginning of UnwindContextToWinContext, winContext points the valid status captured by RtlCaptureContext as follows:

(gdb) p/x *winContext
$23 = {..., R0 = 0x7effe478, R1 = 0x55da2e00, R2 = 0x55da2e00, R3 = 0x55da2e00, R4 = 0x71c27075, R5 = 0x7effefc0, R6 = 0x7effeea0, R7 = 0x7effe620, R8 = 0x76d60000, R9 = 0x55da2e00,
  R10 = 0x76d60000, R11 = 0x7effe998, R12 = 0x0, Sp = 0x7effe3d0, Lr = 0x76531295, Pc = 0x76531295, Cpsr = 0x600f0010, ...}

However, the status is spoiled when UnwindContextToWinContext returns:

(gdb) p/x *winContext
$33 = {..., R0 = 0x7effe478, R1 = 0x55da2e00, R2 = 0x55da2e00, R3 = 0x55da2e00, R4 = 0x71c27075, R5 = 0x7effefc0, R6 = 0x0, R7 = 0x7effe970, R8 = 0x0, R9 = 0x0, R10 = 0xe, R11 = 0x0,
  R12 = 0x0, Sp = 0x7effe628, Lr = 0x764818a3, Pc = 0x764818a2, Cpsr = 0x600f0010, ... }

As I understood, the frame saves only the value of R7 and LR, and thus unwinding should not affect other registers (except Sp, Pc, Lr).

Is it usual that unw_get_reg behaves like this?

parjong avatar Apr 20 '16 05:04 parjong

@parjong dotnet/runtime#5223 was about unwinding native frames until it reaches to a managed frame, which does not require us to get register information except for PC and SP. If it really requires to read registers other than such special purpose registers, it is beyond what dotnet/runtime#5223 requires.

If you want to ensure if the argument if correct, please don't feel ensured by the CPP code, but check out the actual memory/register states in runtime.

Plus is the third argument in question if acquired by UnwindContextToWinContext+PAL_VirtualUnwind? or is it acquired by context saving macros of CoreCLR itself? (it determines what you really need to look at. please don't just assume that every stack unwinding related work will be using the same API; they don't.)

myungjoo avatar Apr 20 '16 07:04 myungjoo

@myungjoo The third argument is the address of local variable unwindStartContext in function UnwindManagedExceptionPass1 whose value is copied from frameContext in function DispatchManagedException.

DispatchManagedException first captures the current status by RtlCaptureContext (As I understand, that is one of the context saving macros that you mentioned. Am I right?). I checked frameContext after executing RtlCaptureContext and there was some mismatching, but it was correct for R11 (that we are currently interested in).

After capturing the current status, DispatchManagedException tries to unwind the stack frame via VirtualUnwindToFirstManagedCallFrame.

The above issue happens at the first step of this unwind.

parjong avatar Apr 20 '16 08:04 parjong

@parjong Could you please share the disass of the DispatchManagedException and JIT_Overflow functions? If I get you correctly, the unwind issue happens when unwinding from the DispatchManagedException to the JIT_Overflow frame, right? Also, can you set breakpoints at both of those functions and see what the value of R11 is when you hit each of those?

janvorli avatar Apr 20 '16 13:04 janvorli

@janvorli Yes, you are right. The issue happens when unwinding DispatchManagedException frame.

Here is the disass of JIT_Overflow

(gdb) x/16i JIT_Overflow
   0x764816ac <JIT_Overflow()>: push    {r7, lr}
   0x764816ae <JIT_Overflow()+2>:       mov     r7, sp
   0x764816b0 <JIT_Overflow()+4>:       sub.w   sp, sp, dotnet/coreclr#840    ; 0x348
   0x764816b4 <JIT_Overflow()+8>:       ldr     r0, [pc, dotnet/runtime#4161]  ; (0x764819b4 <JIT_Overflow()+776>)
   0x764816b6 <JIT_Overflow()+10>:      ldr     r1, [pc, dotnet/coreclr#768]  ; (0x764819b8 <JIT_Overflow()+780>)
   0x764816b8 <JIT_Overflow()+12>:      add     r1, pc
   0x764816ba <JIT_Overflow()+14>:      ldr     r0, [r0, r1]
   0x764816bc <JIT_Overflow()+16>:      ldr     r0, [r0, #0]
   0x764816be <JIT_Overflow()+18>:      str     r0, [sp, dotnet/coreclr#836]  ; 0x344
   0x764816c0 <JIT_Overflow()+20>:      movs    r0, #0
   0x764816c2 <JIT_Overflow()+22>:      str     r0, [sp, dotnet/runtime#3941]  ; 0xb0
   0x764816c4 <JIT_Overflow()+24>:      str     r0, [sp, dotnet/runtime#3913]  ; 0x78
   0x764816c6 <JIT_Overflow()+26>:      bl      0x7653d974 <ResetCurrentContext()>
   0x764816ca <JIT_Overflow()+30>:      ldr     r0, [sp, dotnet/runtime#3913]  ; 0x78
   0x764816cc <JIT_Overflow()+32>:      str     r0, [sp, dotnet/coreclr#172]  ; 0xac
   0x764816ce <JIT_Overflow()+34>:      b.n     0x764816d0 <JIT_Overflow()+36>

Here is the value of registers at the beginning of JIT_Overflow

(gdb) info reg
r0             0x0      0
r1             0x0      0
r2             0xffffff00       4294967040
r3             0x764816ad       1984435885
r4             0x71c27075       1908568181
r5             0x7effefc0       2130702272
r6             0x7effeea0       2130701984
r7             0x7effe9a0       2130700704
r8             0x76d60000       1993736192
r9             0xa3f60600       2750809600
r10            0x76d60000       1993736192
r11            0x7effe998       2130700696
r12            0x76d603b0       1993737136
sp             0x7effe978       0x7effe978
lr             0x71c274c9       1908569289
pc             0x764816ac       0x764816ac <JIT_Overflow()>
cpsr           0x10030  65584

Here is the disass of DispatchManagedException (I modified the code to call fprintf at the beginning):

(gdb) x/16i DispatchManagedException
=> 0x76531268 <DispatchManagedException(PAL_SEHException&)>:    push    {r7, lr}
   0x7653126a <DispatchManagedException(PAL_SEHException&)+2>:  mov     r7, sp
   0x7653126c <DispatchManagedException(PAL_SEHException&)+4>:  sub.w   sp, sp, dotnet/runtime#4085    ; 0x250
   0x76531270 <DispatchManagedException(PAL_SEHException&)+8>:  mov     r1, r0
   0x76531272 <DispatchManagedException(PAL_SEHException&)+10>: str     r0, [sp, dotnet/runtime#4084]  ; 0x24c
   0x76531274 <DispatchManagedException(PAL_SEHException&)+12>: movs    r0, #0
   0x76531276 <DispatchManagedException(PAL_SEHException&)+14>: str     r1, [sp, dotnet/coreclr#108]  ; 0x6c
   0x76531278 <DispatchManagedException(PAL_SEHException&)+16>: bl      0x76aa2cd0 <PAL_get_stderr(int)>
   0x7653127c <DispatchManagedException(PAL_SEHException&)+20>: ldr     r2, [sp, dotnet/runtime#4084]  ; 0x24c
   0x7653127e <DispatchManagedException(PAL_SEHException&)+22>: ldr     r1, [pc, dotnet/coreclr#712]  ; (0x76531548 <DispatchManagedException(PAL_SEHException&)+736>)
   0x76531280 <DispatchManagedException(PAL_SEHException&)+24>: ldr     r3, [pc, dotnet/coreclr#712]  ; (0x7653154c <DispatchManagedException(PAL_SEHException&)+740>)
   0x76531282 <DispatchManagedException(PAL_SEHException&)+26>: add     r3, pc
   0x76531284 <DispatchManagedException(PAL_SEHException&)+28>: add     r1, r3
   0x76531286 <DispatchManagedException(PAL_SEHException&)+30>: bl      0x76aadd8c <PAL_fprintf(PAL_FILE*, char const*, ...)>
   0x7653128a <DispatchManagedException(PAL_SEHException&)+34>: str     r0, [sp, dotnet/coreclr#104]  ; 0x68
   0x7653128c <DispatchManagedException(PAL_SEHException&)+36>: b.n     0x7653128e <DispatchManagedException(PAL_SEHException&)+38>

Here is the call stack and the value of registers at the beginning of DispatchManagedException:

(gdb) bt
#0  DispatchManagedException (ex=...) at /home/parjong/projects/toolchains/coreclr/src/vm/exceptionhandling.cpp:4628
dotnet/coreclr#1  0x764818a2 in JIT_Overflow () at /home/parjong/projects/toolchains/coreclr/src/vm/jithelpers.cpp:5486
dotnet/coreclr#2  0x71c274c8 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
(gdb) info reg
r0             0x7effe6f0       2130700016
r1             0x76e6c518       1994835224
r2             0x3c9    969
r3             0x1      1
r4             0x71c27075       1908568181
r5             0x7effefc0       2130702272
r6             0x7effeea0       2130701984
r7             0x7effe970       2130700656
r8             0x76d60000       1993736192
r9             0xa3f60600       2750809600
r10            0x76d60000       1993736192
r11            0x7effe998       2130700696
r12            0x76f9f428       1996092456
sp             0x7effe628       0x7effe628
lr             0x764818a3       1984436387
pc             0x76531268       0x76531268 <DispatchManagedException(PAL_SEHException&)>
cpsr           0x20010030       536936496

FYI, here is the frame context captured by RtlCaptureContext (inside DispatchManagedException):

(gdb) p/x frameContext
$2 = {ContextFlags = 0x20000b, R0 = 0x7effe478, R1 = 0xa3f60600, R2 = 0xa3f60600, R3 = 0xa3f60600, R4 = 0x71c27075, R5 = 0x7effefc0, R6 = 0x7effeea0, R7 = 0x7effe620, R8 = 0x76d60000, R9 = 0xa3f60600,
  R10 = 0x76d60000, R11 = 0x7effe998, R12 = 0x0, Sp = 0x7effe3d0, Lr = 0x76531295, Pc = 0x76531295, Cpsr = 0x600f0010, Fpscr = 0x80000010, Padding = 0x76e6c000, {Q = {{Low = 0x76d60000a3f60600,
        High = 0x7641182d7effdda0}, {Low = 0x7effd99c76fd5010, High = 0x475287effdeb8}, {Low = 0x7300650052002e, High = 0x6300720075006f}, {Low = 0x63006f004c0065, High = 0x72006f00740061}, {Low = 0x0,
        High = 0x0}, {Low = 0x0, High = 0x0}, {Low = 0x0, High = 0x0}, {Low = 0x0, High = 0x0}, {Low = 0x4032efc92935df92, High = 0x4059000000000000}, {Low = 0x0, High = 0x0}, {Low = 0x0, High = 0x0}, {
        Low = 0x0, High = 0x0}, {Low = 0x0, High = 0x0}, {Low = 0x0, High = 0x0}, {Low = 0x0, High = 0x0}, {Low = 0x0, High = 0x0}}, D = {0x76d60000a3f60600, 0x7641182d7effdda0, 0x7effd99c76fd5010,
      0x475287effdeb8, 0x7300650052002e, 0x6300720075006f, 0x63006f004c0065, 0x72006f00740061, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4032efc92935df92, 0x4059000000000000, 0x0 <repeats 14 times>}, S = {
      0xa3f60600, 0x76d60000, 0x7effdda0, 0x7641182d, 0x76fd5010, 0x7effd99c, 0x7effdeb8, 0x47528, 0x52002e, 0x730065, 0x75006f, 0x630072, 0x4c0065, 0x63006f, 0x740061, 0x72006f, 0x0 <repeats 16 times>}}, Bvr = {
    0x76f11c00, 0x0, 0xa3f60600, 0x76d60000, 0x7effe998, 0x0, 0x76e6c000, 0x1}, Bcr = {0x0, 0x0, 0x0, 0x7effe8fc, 0x127c30, 0x0, 0x7effe6f0, 0x71c27075}, Wvr = {0x7effefc0}, Wcr = {0x7effeea0}, Padding2 = {
    0x7effe970, 0x76d60000}}

parjong avatar Apr 20 '16 22:04 parjong

It seems that I found the solution. In WinContextToUnwindCursor, it creates a fresh local variable of type unw_context_t, and uses it to re-initialize unw_cursor_t.

I can execute the above code after I change the code to update existing unw_context_t.

parjong avatar Apr 21 '16 04:04 parjong

Good catch!

unw_init_local of libunwind-arm keeps the pointer of unw_context_t.

myungjoo avatar Apr 21 '16 07:04 myungjoo