Skip to content

Commit c50b65e

Browse files
committed
Fix issue with branching to swapped stfld -> callvirt
Given the (abridged) psuedo-IL ```asm ldfld PlayerData ... ldfld bool ... brtrue.s :opt ldfld string .. br.s :set opt: ldfld string ... set: stfld PlayerData::someField ``` We were patching it to the following: ```asm ldfld PlayerData ... ldfld bool ... brtrue.s :opt ldfld string ... br.s :set opt: ldfld string ... ldstr "someField" set: callvirt instance void PlayerData::SetStringSwappedArgs(string, string) ``` and the way we did this patching is by swapping an `stfld` to a `callvirt` and then inserting the `ldstr` for the field name (which is the second arg for our SetStringSwappedArgs) before this `callvirt` we inserted. However, given this situation, we skip over the ldstr of the field name when coming from the `false` branch path as it branches directly to `set` as it had previously branched directly to an `stfld` and branching is instruction location based. The fix for this is relatively simple, just swap the `stfld` with the `ldstr` instead, and then insert our `callvirt` afterwards, which gives the following: ```asm ldfld PlayerData ... ldfld bool ... brtrue.s :opt ldfld string ... br.s :set opt: ldfld string ... set: ldstr "someField" callvirt instance void PlayerData::SetStringSwappedArgs(string, string) ```
1 parent 1472b8f commit c50b65e

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

PrePatcher/Program.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,11 @@ ILProcessor il
208208
generic.GenericArguments.Add(field.FieldType);
209209
callSet = Instruction.Create(OpCodes.Callvirt, generic);
210210
}
211+
212+
il.InsertAfter(instr, callSet);
211213

212-
instr.OpCode = callSet.OpCode;
213-
instr.Operand = callSet.Operand;
214-
215-
il.InsertBefore(instr, ldstr);
214+
instr.OpCode = ldstr.OpCode;
215+
instr.Operand = ldstr.Operand;
216216
}
217217

218218
private static void SwapLdFld
@@ -259,11 +259,11 @@ ILProcessor il
259259
generic.GenericArguments.Add(field.FieldType);
260260
callGet = Instruction.Create(OpCodes.Callvirt, generic);
261261
}
262+
263+
il.InsertAfter(instr, callGet);
262264

263-
instr.OpCode = callGet.OpCode;
264-
instr.Operand = callGet.Operand;
265-
266-
il.InsertBefore(instr, ldstr);
265+
instr.OpCode = ldstr.OpCode;
266+
instr.Operand = ldstr.Operand;
267267
}
268268

269269
private static MethodDefinition GenerateSwappedMethod(TypeDefinition methodParent, MethodReference oldMethod)

0 commit comments

Comments
 (0)