Skip to content
This repository was archived by the owner on Jan 27, 2019. It is now read-only.

Commit 2532a4e

Browse files
committed
Fix control flow obfuscation for C# 6 null-propagation operator
Fix #299
1 parent 7720c99 commit 2532a4e

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

Confuser.Protections/ControlFlow/SwitchMangler.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ LinkedList<Instruction[]> SpiltStatements(InstrBlock block, Trace trace, CFConte
118118
var statements = new LinkedList<Instruction[]>();
119119
var currentStatement = new List<Instruction>();
120120

121+
// Instructions that must be included in the ccurrent statement to ensure all outgoing
122+
// branches have stack = 0
123+
var requiredInstr = new HashSet<Instruction>();
124+
121125
for (int i = 0; i < block.Instructions.Count; i++) {
122126
Instruction instr = block.Instructions[i];
123127
currentStatement.Add(instr);
@@ -128,10 +132,21 @@ LinkedList<Instruction[]> SpiltStatements(InstrBlock block, Trace trace, CFConte
128132
case FlowControl.Cond_Branch:
129133
case FlowControl.Return:
130134
case FlowControl.Throw:
135+
if (trace.AfterStack[instr.Offset] != 0) {
136+
if (instr.Operand is Instruction)
137+
requiredInstr.Add((Instruction)instr.Operand);
138+
else {
139+
foreach (var target in (Instruction[])instr.Operand)
140+
requiredInstr.Add(target);
141+
shouldSpilt = false;
142+
}
143+
}
131144
shouldSpilt = true;
132145
break;
133146
}
134-
if ((instr.OpCode.OpCodeType != OpCodeType.Prefix && trace.AfterStack[instr.Offset] == 0) &&
147+
requiredInstr.Remove(instr);
148+
if ((instr.OpCode.OpCodeType != OpCodeType.Prefix && trace.AfterStack[instr.Offset] == 0 &&
149+
requiredInstr.Count == 0) &&
135150
(shouldSpilt || ctx.Intensity > ctx.Random.NextDouble())) {
136151
statements.AddLast(currentStatement.ToArray());
137152
currentStatement.Clear();

0 commit comments

Comments
 (0)