.NET 中的调用约定和堆栈遍历

问题描述

我正在学习这本书 Inside Windows Debugging 并且无法完全理解 Listing parameters and Locals for System Code 章节中使用的技术。顾名思义,我试图获取传递给某个函数的参数列表。堆栈示例:

0:012:x86> k
  *** Stack trace for last set context - .thread/.cxr resets it
 # ChildEBP RetAddr  
WARNING: Frame IP not in any kNown module. Following frames may be wrong.
0e 053eecec 04561d6e 0x4562f2e 
0f 053eed30 04561c52 0x4561d6e
10 053eed54 72617118 0x4561c52
11 053eed60 72616cc0 mscorlib_ni!System.Threading.Tasks.Task.InnerInvoke()$##6003FA0+0x28
12 053eed84 726170ea mscorlib_ni!System.Threading.Tasks.Task.Execute()$##6003F91+0x30
13 053eedec 72633fd6 mscorlib_ni!System.Threading.Tasks.Task.ExecutionContextCallback(System.Object)$##6003F9F+0x1a
14 053eee00 72616f68 mscorlib_ni!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,System.Threading.ContextCallback,System.Object,Boolean)$##6003AD3+0x16
15 053eee6c 72616e72 mscorlib_ni!System.Threading.Tasks.Task.ExecuteWithThreadLocal(System.Threading.Tasks.Task ByRef)$##6003F9E+0xd8
16 053eee7c 7268ac9c mscorlib_ni!System.Threading.Tasks.Task.ExecuteEntry(Boolean)$##6003F9D+0xb2
17 053eee8c 726340c5 mscorlib_ni!System.Threading.Tasks.ThreadPoolTaskScheduler.LongRunningThreadWork(System.Object)$##60040E8+0x1c
18 053eeef0 72633fd6 mscorlib_ni!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext,Boolean)$##6003AD4+0xe5
19 053eef04 72633f91 mscorlib_ni!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,Boolean)$##6003AD3+0x16
1a 053eef20 72688c8e mscorlib_ni!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,System.Object)$##6003AD2+0x41
1b 053eef38 736eebf6 mscorlib_ni!System.Threading.ThreadHelper.ThreadStart(System.Object)$##6003BE3+0x4e

一般来说,我有一个完整的堆栈,但尽管如此,我还是希望能够处理 k 之类的命令的输出

0:012:x86> !clrstack
OS Thread Id: 0x2518 (12)
Child SP       IP Call Site
053eecac 04562f2e Namespace.ProcessingManager.B(System.String,System.Threading.CancellationToken,System.Collections.Generic.List`1,Int32)
053eed00 04561d6e Namespace.ProcessingManager.A(System.String,Int32)
053eed40 04561c52 Namespace.ProcessingManager+c__displayClass25_0.b__0()
053eed5c 72617118 System.Threading.Tasks.Task.InnerInvoke()
053eed68 72616cc0 System.Threading.Tasks.Task.Execute()
053eed8c 726170ea System.Threading.Tasks.Task.ExecutionContextCallback(System.Object)
053eed90 726340c5 System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext,Boolean)
053eedfc 72633fd6 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,Boolean)
053eee10 72616f68 System.Threading.Tasks.Task.ExecuteWithThreadLocal(System.Threading.Tasks.Task ByRef)
053eee74 72616e72 System.Threading.Tasks.Task.ExecuteEntry(Boolean)
053eee84 7268ac9c System.Threading.Tasks.ThreadPoolTaskScheduler.LongRunningThreadWork(System.Object)
053eee88 726070e3 System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
053eee94 726340c5 System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext,Boolean)
053eef00 72633fd6 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,Boolean)
053eef14 72633f91 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,System.Object)
053eef2c 72688c8e System.Threading.ThreadHelper.ThreadStart(System.Object)

我对具有以下签名的方法 B 的参数感兴趣:

void B (string code,CancellationToken token,List<SomeObj> items,int taskCount)

我的研究假设 .NET 使用 __stdcall 调用约定:

0:012:x86> dd 053eecec
053eecec  053eed30 04561d6e 00000002 1259e288 
053eecfc  0163dae4 12592918 01698868 01639394
053eed0c  01639394 015fe3b4 00000000 00000000
053eed1c  00000000 00000000 053eed30 01791aa4
053eed2c  01791abc 053eed54 04561c52 00000005
053eed3c  0163dae4 016da0e4 0163dae4 0167402c
053eed4c  01791b04 053eedc8 053eed60 72617118
053eed5c  01791b04 053eed84 72616cc0 0163db2c

053eed30:已保存 EBP(前一帧指针 - A)。

04561d6eRetAddr 指向下一段实际代码

00000002: Arg: taskCount(我想像 00000002 这样的值代表原始类型的值)。

1259e288:Arg:项目(通过 !do 1259e288 检查)。

0163dae4:Arg:令牌(通过 !do 0163dae4 检查)。

12592918:假定给定的地址将包含 string code 参数,但像 !dodu 这样的命令不返回任何内容。一般来说,在这个值之后,会找到string code(01639394),但它似乎是指前面的方法,因为01698868处的值是在方法A处创建的。

我如何获得第一个参数?

UPD

来自评论中提到的答案:

__clrcall 是托管代码调用约定。它是其他指针的混合,这个指针像 __thiscall 一样传递,优化 参数传递如 __fastcall,参数顺序如 __cdecl 和 调用方清理,如 __stdcall。

来自__cdecl的描述:

参数传递顺序:从右到左。 堆栈维护职责:调用函数从堆栈中弹出参数。

这与我得到的相似。

0:012:x86> .frame /c /r 0e
0e 053eecec 04561d6e 0x4562f2e
eax=00000000 ebx=19223de4 ecx=00000000 edx=00000000 esi=0000000a edi=0000000a
eip=04562f2e esp=053eeca8 ebp=053eecec iopl=0         nv up ei pl nz ac po nc
cs=0023  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00000212
04562f2e eb75            jmp     04562fa5

来自__fastcall的描述:

参数传递顺序:前两个 DWORD 或更小的参数 从左到右在参数列表中找到在 ECX 中传递 和 EDX 寄存器;所有其他参数都从堆栈上传递 右到左。堆栈维护职责:调用函数弹出 来自堆栈的参数。

eipespebp 是“服务”寄存器,据我所知,参数不会通过它们传递。 ebx

解决方法

您最好在最低限度的可重现示例而不是完全成熟的应用程序上尝试此操作。我使用的代码是

using System;
using System.Text;

namespace CallingConvention
{
    class Program
    {
        static void Main(string[] args)
        {
            var ecx = new Program(); // any object ok for testing
            var edx = new int[5]; // any object ok for testing
            var stack1 = new object(); // any object ok for testing
            var stack2 = new AccessViolationException(); // any object ok for testing
            Method1(ecx,edx,stack1,stack2);
        }

        private static void Method1(Program ecx,int[] edx,object stack1,AccessViolationException stack2)
        {
            Console.WriteLine(ecx);
            Console.WriteLine(edx);
            Console.WriteLine(stack1);
            Console.WriteLine(stack2);

            var ecx2 = new ASCIIEncoding();
            var edx2 = new ArgumentException();
            Method2(ecx2,edx2);
        }

        private static void Method2(ASCIIEncoding ecx2,ArgumentException edx2)
        {
            Console.WriteLine(ecx2);
            Console.WriteLine(edx2);
            Console.WriteLine("ECX and EDX possibly destroyed.");
            Console.ReadLine();
        }
    }
}

但似乎它甚至可以更短。调试会话:

[...]
ntdll!LdrpDoDebuggerBreak+0x2b:
773d1a62 cc              int     3
0:000> sxe ld clrjit
0:000> g

[...]
ModLoad: 56be0000 56c6a000   C:\Windows\Microsoft.NET\Framework\v4.0.30319\clrjit.dll
[...]
0:000> .loadby sos clr
0:000> .load B:\...\sosex.dll
0:000> !mbm *!*Program.Main
0:000> !mbm *!*Method1
0:000> !mbm *!*Method2
0:000> !mbl
0 e : disable *!*PROGRAM.MAIN ILOffset=0: pass=1 oneshot=false thread=ANY
    CallingConvention!CallingConvention.Program.Main(string[]) (PENDING JIT)
1 e : disable *!*METHOD1 ILOffset=0: pass=1 oneshot=false thread=ANY
    CallingConvention!CallingConvention.Program.Method1(CallingConvention.Program,int[],object,System.AccessViolationException) (PENDING JIT)
2 e : disable *!*METHOD2 ILOffset=0: pass=1 oneshot=false thread=ANY
    CallingConvention!CallingConvention.Program.Method2(System.Text.ASCIIEncoding,System.ArgumentException) (PENDING JIT)
    
0:000> g

到目前为止,这只是设置必要的扩展和断点。

0:000> !clrstack -a
OS Thread Id: 0x44cc (0)
Child SP       IP Call Site
001fee28 007c084f *** WARNING: Unable to verify checksum for CallingConvention.exe
CallingConvention.Program.Main(System.String[]) [C:\...\Program.cs @ 10]
    PARAMETERS:
        args (<CLR reg>) = 0x02452430
    LOCALS:
        <no data>
        <no data>
        <no data>
001fefa4 5813f036 [GCFrame: 001fefa4] 

0:000> r ecx
ecx=02452430

在main方法的开头,还没有locals。但是我们已经可以看到 args[] 在 ECX 寄存器中传递:!clrstack<CLR reg> 并且我们在 ECX 中找到了相同的值。

0:000> !mt
0:000> !clrstack -a
OS Thread Id: 0x44cc (0)
Child SP       IP Call Site
001fee28 007c085b CallingConvention.Program.Main(System.String[]) [C:\...\Program.cs @ 11]
    PARAMETERS:
        args = <no data>
    LOCALS:
        <no data>
        <no data>
        <no data>

001fefa4 5813f036 [GCFrame: 001fefa4] 

仅仅 1 个托管行之后,ECX 的值就被覆盖了,所以我们不再知道 args[] 是什么。我们不需要,因为它从未使用过。

虽然 new Program() 已被调用,但 !clrstack 无法将其识别为局部变量。

0:000> !dumpheap -type Program
 Address       MT     Size
0245243c 00774d78       12  
[...]

0:000> r
eax=0245243c ebx=0245243c ecx=00774d78 edx=00546998 esi=00000000 edi=001fee50

但是实例在那里,而且似乎在 EAX 和 EBX 中。

0:000> !mt
eax=02452474 ebx=0245243c ecx=02452474 edx=00546998 esi=02452474 edi=02452468
eip=007c0887 esp=001fee28 ebp=001fee38 iopl=0         nv up ei pl nz na pe nc
cs=0023  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00000206
007c0887 ff15c03dd256    call    dword ptr [mscorlib_ni+0xb3dc0 (56d23dc0)] ds:002b:56d23dc0=5714f2e0
0:000> !clrstack -a
OS Thread Id: 0x44cc (0)
Child SP       IP Call Site
001fee28 007c0887 CallingConvention.Program.Main(System.String[]) [C:\...\Program.cs @ 13]
    PARAMETERS:
        args = <no data>
    LOCALS:
        0x001fee28 = 0x02452448
        <no data>
        <no data>

001fefa4 5813f036 [GCFrame: 001fefa4] 
0:000> r
eax=02452474 ebx=0245243c ecx=02452474 edx=00546998 esi=02452474 edi=02452468
[...]

稍后的另一个托管调用,我们在堆栈中看到了 int[]。 EAX 已更改,但 EBX 仍持有 Program 对象。

0:000> !mt
0:000> *** inside AccessViolation constructor
0:000> !mgu
0:000> *** get out of constructor
0:000> !mt
0:000> *** right before Method1() call

0:000> r
eax=02456f00 ebx=0245243c ecx=0245243c edx=02452448 esi=02452474 edi=02452468
eip=007c0894 esp=001fee20 ebp=001fee38 iopl=0         nv up ei pl zr na pe nc
cs=0023  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00000246
007c0894 ff15604d7700    call    dword ptr ds:[774D60h] ds:002b:00774d60=007c043d

0:000> !do ebx
Name:        CallingConvention.Program
[...]
0:000> !do ecx
Name:        CallingConvention.Program
[...]
0:000> !do edx
Name:        System.Int32[]
[...]
  • EBX = 程序对象,来自之前
  • ECX = 程序对象,为方法调用准备
  • EDX = int[],为方法调用做准备

我不知道为什么,但另外两个参数在 EDI 和 ESI 中:

0:000> !muf
[...]
        007c088d 57              push    edi
        007c088e 56              push    esi
[...]
0:000> !do edi
Name:        System.Object
[...]
0:000> !do esi
Name:        System.AccessViolationException
[...]
0:000> r edi
edi=02452468
0:000> r esi
esi=02452474
0:000> dd esp-8 L2
001fee18  02452474 02452468


0:000> !mt
Breakpoint 7 hit
[...]
0:000> !clrstack -a
OS Thread Id: 0x44cc (0)
Child SP       IP Call Site
001fee10 007c08b7 CallingConvention.Program.Method1(CallingConvention.Program,Int32[],System.Object,System.AccessViolationException) [C:\...\Program.cs @ 19]
    PARAMETERS:
        ecx (<CLR reg>) = 0x0245243c
        edx (<CLR reg>) = 0x02452448
        stack1 (0x001fee18) = 0x001fee38
        stack2 (0x001fee14) = 0x02452468
    LOCALS:
        <no data>
[...]
0:000> !do ecx
Name:        CallingConvention.Program
[...]
0:000> !do edx
Name:        System.Int32[]
[...]
0:000> ? esp+8
Evaluate expression: 2092568 = 001fee18
0:000> dd esp+8 L1
001fee18  001fee38
0:000> ? esp+4
Evaluate expression: 2092564 = 001fee14
0:000> dd esp+4 L1
001fee14  02452468

!clrstack 似乎认为推送的参数在 ESP+8 和 ESP+4 处。但这是不正确的。 0x001fee38 不是有效对象。它是堆栈上的一个地址。

0:000> !do poi(esp)
Name:        System.AccessViolationException
[...]
0:000> !do poi(esp+4)
Name:        System.Object
[...]

相反,他们似乎处于 ESP 和 ESP+4。

0:000> kb L1
 # ChildEBP RetAddr      Args to Child              
00 001fee18 007c089a     02452474 02452468 02452448 0x7c08b7

0:000> !mt
0:000> *** inside Console.WriteLine()

0:000> !clrstack -a
OS Thread Id: 0x44cc (0)
Child SP       IP Call Site
001fee10 007c08bc CallingConvention.Program.Method1(CallingConvention.Program,System.AccessViolationException) [C:\Users\T\source\repos\CallingConvention\CallingConvention\Program.cs @ 20]
    PARAMETERS:
        ecx = <no data>
        edx (<CLR reg>) = 0x02452448
        stack1 (0x001fee24) = 0x02452468
        stack2 (0x001fee20) = 0x02452474
    LOCALS:
        <no data>
[...]
0:000> r ecx
ecx=024578f4
0:000> !do ecx
Name:        System.IO.TextWriter+SyncTextWriter
[...]

此时通过ECX传递的参数已经被覆盖。

结论:.NET 使用 __clrcall。它将前两个参数作为 ECX 和 EDX 传递,然后在堆栈上从右到左传递。

我如何获得第一个参数?

只要该寄存器未被重用,您就可以从 ECX 寄存器中获取第一个参数。

eip、esp、ebp 是“服务”寄存器,据我所知,参数不会通过它们传递。 ebx?

不,EBX 不参与调用约定,但它可能用于局部变量。