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

Calling convention and stack walking in .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, System.Threading.ContextCallback, System.Object, Boolean)$##6003AD4+0xe5
19 053eef04 72633f91 mscorlib_ni!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)$##6003AD3+0x16
1a 053eef20 72688c8e mscorlib_ni!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)$##6003AD2+0x41
1b 053eef38 736eebf6 mscorlib_ni!System.Threading.ThreadHelper.ThreadStart(System.Object)$##6003BE3+0x4e

一般来说,我有一个 完整的 堆栈,但是我还是希望能够使用 k:[=48= 这样的命令的输出]

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, System.Threading.CancellationToken, 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, System.Threading.ContextCallback, System.Object, Boolean)
053eedfc 72633fd6 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, 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, System.Threading.ContextCallback, System.Object, Boolean)
053eef00 72633fd6 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
053eef14 72633f91 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, 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:参数:项目(通过 !do 1259e288 检查)。

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

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

如何获取第一个参数?

UPD

来自评论中提到的答案:

__clrcall is the calling convention for managed code. It is a blend of the other ones, this pointer passing like __thiscall, optimized argument passing like __fastcall, argument order like __cdecl and caller cleanup like __stdcall.

来自__cdecl描述:

Argument-passing order: Right to left. Stack-maintenance responsibility: Calling function pops the arguments from the stack.

这和我得到的差不多。

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

Argument-passing order: The first two DWORD or smaller arguments that are found in the argument list from left to right are passed in ECX and EDX registers; all other arguments are passed on the stack from right to left. Stack-maintenance responsibility: Called function pops the arguments from the stack.

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方法的开头,还没有本地人。但是我们已经可以看到 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, Int32[], System.Object, 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,然后从右到左在堆栈上。

How do I get the first argument?

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

eip, esp, ebp are "service" registers, as I understand it, parameters are not passed through them. ebx?

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