Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Executing result of GetInstructions #19

Open
sake402 opened this issue Jun 3, 2020 · 1 comment
Open

Executing result of GetInstructions #19

sake402 opened this issue Jun 3, 2020 · 1 comment

Comments

@sake402
Copy link

sake402 commented Jun 3, 2020

Hi.
I am using this library for generating opcodes from a CIL byte[] alongside with the Harmony project.
My concept is to recompile a changed source code on the fly, get a method from the new assembly and then its instructions and replace the new instructions with the old one.

So I have been using the library for getting the new instructions. All works very well until the library encounter a branch instruction in the new code.

Inside the MethodBodyReader.ResolveBranches you have this

switch (instruction.OpCode.OperandType) {
				case OperandType.ShortInlineBrTarget:
				case OperandType.InlineBrTarget:
					instruction.Operand = GetInstruction (instructions, (int) instruction.Operand);
					break;

Which throws exception when harmony is using the opcodes to patch the method.

Given that the operand of branches needs to be an offset to the instruction to branch to, I changed the code to this

var targetInstruction = GetInstruction(instructions, (int)instruction.Operand);
int byteOffset = targetInstruction.Offset - instruction.Offset - 1;
int targetOffset = instructions.IndexOf(targetInstruction);
int instructionOffset = targetOffset - index - 1;
instruction.Operand = (sbyte)(instructionOffset);// //GetInstruction(instructions, (int)instruction.Operand);

But I still get exception anyway. Any pointers from you please...

@jbevain
Copy link
Owner

jbevain commented Jun 3, 2020

Hi,

Unfortunately there's not much I can do to help here.

GetInstruction is going to throw if it can not find the instruction at the offset. If the patched method points at an invalid offset it should throw. You'll need to make sure the input IL makes sense. If that's a bug in the method body reader, please provide a test case that I can look at.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants