使用 Br_S OpCode 使用 Reflection.Emit.Label 指向下一条指令

发布于 2024-11-25 10:41:33 字数 1226 浏览 2 评论 0原文

我正在尝试解析 IL 以发出一种方法。我已经在 string[] 中获得了一个方法的 IL 代码,其中每个字符串都是一个 IL 指令。我循环遍历这个数组并使用 ILGenerator 添加操作码:

        foreach (string ins in instructions) //string representations of IL          
        {
            string opCode = ins.Split(':').ElementAt(1);

            // other conditions omitted

            if (opCode.Contains("br.s"))
            {
                Label targetInstruction = ilGenerator.DefineLabel();

                ilGenerator.MarkLabel(targetInstruction);

                ilGenerator.Emit(OpCodes.Br_S, targetInstruction); 
            }

这是我需要重现的 IL:

Source IL:
IL_0000: nop
IL_0001: ldstr "Hello, World!"
IL_0006: stloc.0
IL_0007: br.s IL_0009
IL_0009: ldloc.0
IL_000a: ret

这是我得到的输出:

Target IL:
IL_0000: nop
IL_0001: ldstr "Hello, World!"
IL_0006: stloc.0
IL_0007: br.s IL_0007   // this is wrong -- needs to point to IL_0009
IL_0009: ldloc.0
IL_000a: ret

如您所见, br.s 调用指向自身,这当然会导致无限循环。如何让它指向源中的以下指令?这与使用 Reflection.Emit.Label 有关,但我不确定它是如何工作的。

编辑顺便说一句,上面看到的 IL 是针对这个简单方法的,

    public string HelloWorld()
    {
            return "Hello, World!";
    }

I am experimenting with parsing IL in order to emit a method. I have gotten the IL code of a method in a string[] where each string is an IL instruction. I am looping over this array and adding OpCodes using an ILGenerator:

        foreach (string ins in instructions) //string representations of IL          
        {
            string opCode = ins.Split(':').ElementAt(1);

            // other conditions omitted

            if (opCode.Contains("br.s"))
            {
                Label targetInstruction = ilGenerator.DefineLabel();

                ilGenerator.MarkLabel(targetInstruction);

                ilGenerator.Emit(OpCodes.Br_S, targetInstruction); 
            }

Here is the IL that I need to reproduce:

Source IL:
IL_0000: nop
IL_0001: ldstr "Hello, World!"
IL_0006: stloc.0
IL_0007: br.s IL_0009
IL_0009: ldloc.0
IL_000a: ret

And here is what I am getting as output:

Target IL:
IL_0000: nop
IL_0001: ldstr "Hello, World!"
IL_0006: stloc.0
IL_0007: br.s IL_0007   // this is wrong -- needs to point to IL_0009
IL_0009: ldloc.0
IL_000a: ret

As you can see the br.s call is pointing to itself which of course causes an infinite loop. How do I get it to point to the following instruction as in the source? This has to do with using Reflection.Emit.Label but I'm not sure how it works.

EDIT By the way the IL seen above is for this simple method,

    public string HelloWorld()
    {
            return "Hello, World!";
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稀香 2024-12-02 10:41:33

此代码:

ilGenerator.MarkLabel(targetInstruction);
ilGenerator.Emit(OpCodes.Br_S, targetInstruction); 

清楚地表明“在此处标记标签”,然后在标记标签的位置添加指令

如果这不是你想要的,你为什么要这样做?

MarkLabel 将当前位置(即您输出的下一条指令的位置)标记为标签的目标。

在这种情况下,要得到“你想要的”,只需反转这两行,在标记标签之前输出分支指令。

我将“你想要的”放在引号中,因为我不明白该分支指令的意义。机器会很高兴地自行“移动”到下一条指令,无需添加“分支到下一条指令”指令来实现这一点。

This code:

ilGenerator.MarkLabel(targetInstruction);
ilGenerator.Emit(OpCodes.Br_S, targetInstruction); 

clearly says "mark the label here" and then you add the instruction at the point where you marked the label.

If this is not what you want, why are you doing it?

MarkLabel marks the current position, which means the position of the next instruction you output, as the target of the label.

In this case, to get "what you want", simply reverse those two lines, output the branch instruction before marking the label.

I placed "what you want" in quotes since I don't understand the point of that branch instruction. The machine will happily "move" to the next instruction all by itself, there is no need to add "branch to the next instruction" instructions for this to happen.

如梦 2024-12-02 10:41:33

您需要在发出要跳转到的操作码之前立即调用ilGenerator.MarkLabel()。您将其放置在分支之前,这意味着它将分支到自身,从而有效地创建无限循环。但正如 Lasse 所说,如果你正确地发出 IL,它将是一个空操作。

有趣的是,整个方法很容易是:

ldstr "Hello, World!"
ret

无论编译器发出原始代码,都需要对其作者进行 LART 处理。

You need to place the ilGenerator.MarkLabel() call immediately before emitting the opcode you want to jump to. You are placing it before the branch, which means it will branch to itself, effectively creating an infinite loop. But as Lasse says, if you emit the IL correctly it will be a no-op.

Interestingly, the entire method could easily be:

ldstr "Hello, World!"
ret

Whatever compiler emitted the original code needs to have its author LARTed.

凉城凉梦凉人心 2024-12-02 10:41:33

调用 ILGenerator 上的 MarkLabel() 方法可用于标记分支点,然后调用 Emit(OpCodes.Br_S, [label]) 分支到该点。

我假设您用于监视 Hello World 方法的 IL 指令的任何 API 都是在调试模式下完成的,因为添加了 nop 和分支指令以帮助确保调试器涵盖每个步骤。

在 DynamicMethod 中,无需附加调试器,并且根据平台的不同,在发布模式下使用额外指令运行它可能会导致 InvalidProgramException。

“Hello World”方法只需要 2 条指令(而且非常直观)

Ldstr "Hello, World!"
Ret

Calling the MarkLabel() method on your ILGenerator can be used to mark a branch point, followed by Emit(OpCodes.Br_S, [label]) to branch to the point.

I assume whatever API you used to spy the IL instructions of the the Hello World method was done in Debug mode, as the added nop and branch instructions are added to help ensure the debugger covers every step.

In a DynamicMethod, there is no need to attach a debugger, and depending on platform, running it with the extra instructions in Release Mode could result in an InvalidProgramException.

The "Hello World" method requires only 2 instructions (And it's quite intuitive)

Ldstr "Hello, World!"
Ret
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文