From c0be6431569be191c68bd311efe503ef8adb57ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jindra=20Pet=C5=99=C3=ADk?= Date: Sun, 24 Jan 2021 14:44:48 +0100 Subject: [PATCH] chained assignments --- .../flash/abc/avm2/graph/AVM2Graph.java | 5 ++-- .../instructions/InstructionDefinition.java | 13 ++++++++-- .../localregs/GetLocalTypeIns.java | 26 +++++++++++++++++++ .../localregs/SetLocalTypeIns.java | 9 ++++--- .../instructions/other/SetGlobalSlotIns.java | 7 +++-- .../instructions/other/SetPropertyIns.java | 4 +-- .../avm2/instructions/other/SetSlotIns.java | 3 ++- .../avm2/instructions/other/SetSuperIns.java | 7 +++-- .../decompiler/graph/model/DuplicateItem.java | 9 ++++--- 9 files changed, 65 insertions(+), 18 deletions(-) diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/graph/AVM2Graph.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/graph/AVM2Graph.java index 469ce901c..bc7493a89 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/graph/AVM2Graph.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/graph/AVM2Graph.java @@ -22,6 +22,7 @@ import com.jpexs.decompiler.flash.abc.ABC; import com.jpexs.decompiler.flash.abc.AVM2LocalData; import com.jpexs.decompiler.flash.abc.avm2.AVM2Code; import com.jpexs.decompiler.flash.abc.avm2.instructions.AVM2Instruction; +import com.jpexs.decompiler.flash.abc.avm2.instructions.InstructionDefinition; import com.jpexs.decompiler.flash.abc.avm2.instructions.debug.DebugLineIns; import com.jpexs.decompiler.flash.abc.avm2.instructions.jumps.IfStrictEqIns; import com.jpexs.decompiler.flash.abc.avm2.instructions.jumps.JumpIns; @@ -1195,12 +1196,12 @@ public class AVM2Graph extends Graph { } AVM2LocalData avm2LocalData = (AVM2LocalData) localData; SetLocalAVM2Item setLocal = (SetLocalAVM2Item) output.get(output.size() - 1); - int setLocalIp = avm2LocalData.code.code.indexOf(setLocal.getSrc()); + int setLocalIp = InstructionDefinition.getItemIp(avm2LocalData, setLocal);; Set allUsages = new HashSet<>(avm2LocalData.setLocalPosToGetLocalPos.get(setLocalIp)); for (GraphTargetItem otherSide : otherSides.values()) { if (otherSide instanceof LocalRegAVM2Item) { LocalRegAVM2Item otherLog = (LocalRegAVM2Item) otherSide; - int getLocalIp = avm2LocalData.code.code.indexOf(otherLog.getSrc()); + int getLocalIp = InstructionDefinition.getItemIp(avm2LocalData, otherLog); allUsages.remove((Integer) getLocalIp); } } diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/InstructionDefinition.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/InstructionDefinition.java index d5d729969..723f40d07 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/InstructionDefinition.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/InstructionDefinition.java @@ -41,6 +41,7 @@ import com.jpexs.decompiler.graph.GraphSourceItem; import com.jpexs.decompiler.graph.GraphTargetItem; import com.jpexs.decompiler.graph.ScopeStack; import com.jpexs.decompiler.graph.TranslateStack; +import com.jpexs.decompiler.graph.model.DuplicateItem; import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; @@ -312,15 +313,23 @@ public abstract class InstructionDefinition implements Serializable { return false; } + public static int getItemIp(AVM2LocalData localData, GraphTargetItem item) { + GraphSourceItem src = item.getSrc(); + if (src == null) { + return -1; + } + return localData.code.adr2pos(src.getAddress()); + } + public void cleanTempRegisters(AVM2LocalData localData, List output, List usedLocalRegs) { for (LocalRegAVM2Item reg : usedLocalRegs) { for (int i = output.size() - 1; i >= 0; i--) { if (output.get(i) instanceof SetLocalAVM2Item) { SetLocalAVM2Item setLocal = (SetLocalAVM2Item) output.get(i); if (setLocal.regIndex == reg.regIndex) { - int setLocalIp = localData.code.code.indexOf(setLocal.getSrc()); + int setLocalIp = getItemIp(localData, setLocal); Set usages = localData.setLocalPosToGetLocalPos.get(setLocalIp); - int usageIp = localData.code.code.indexOf(reg.getSrc()); + int usageIp = getItemIp(localData, reg); if (usages.size() == 1 && usages.iterator().next().equals(usageIp)) { output.remove(i); } diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/localregs/GetLocalTypeIns.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/localregs/GetLocalTypeIns.java index bd96bfddd..f9f6abba9 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/localregs/GetLocalTypeIns.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/localregs/GetLocalTypeIns.java @@ -25,6 +25,8 @@ import com.jpexs.decompiler.flash.abc.avm2.instructions.InstructionDefinition; import com.jpexs.decompiler.flash.abc.avm2.model.ClassAVM2Item; import com.jpexs.decompiler.flash.abc.avm2.model.LocalRegAVM2Item; import com.jpexs.decompiler.flash.abc.avm2.model.ScriptAVM2Item; +import com.jpexs.decompiler.flash.abc.avm2.model.SetLocalAVM2Item; +import com.jpexs.decompiler.flash.abc.avm2.model.SetTypeAVM2Item; import com.jpexs.decompiler.flash.abc.avm2.model.ThisAVM2Item; import com.jpexs.decompiler.flash.abc.types.Multiname; import com.jpexs.decompiler.flash.abc.types.traits.Trait; @@ -32,7 +34,9 @@ import com.jpexs.decompiler.flash.ecma.Undefined; import com.jpexs.decompiler.graph.GraphTargetItem; import com.jpexs.decompiler.graph.NotCompileTimeItem; import com.jpexs.decompiler.graph.TranslateStack; +import com.jpexs.decompiler.graph.model.DuplicateItem; import java.util.List; +import java.util.Set; /** * @@ -81,6 +85,28 @@ public abstract class GetLocalTypeIns extends InstructionDefinition { if (assignCount > 5) { //Do not allow change register more than 5 - for deobfuscation //computedValue = new NotCompileTimeItem(ins, localData.lineStartInstruction, computedValue); } + + if (output.size() >= 2) { + if ((output.get(output.size() - 1) instanceof SetTypeAVM2Item) && (output.get(output.size() - 2) instanceof SetLocalAVM2Item)) { + SetLocalAVM2Item setLocal = (SetLocalAVM2Item) output.get(output.size() - 2); + GraphTargetItem setItem = output.get(output.size() - 1); + if (setLocal.regIndex == regId + && (setLocal.value instanceof DuplicateItem) + && (setLocal.value.value == setItem.value.getNotCoerced())) { + + int setLocalIp = getItemIp(localData, setLocal); + int getLocalIp = localData.code.adr2pos(ins.getAddress()); + Set usages = localData.setLocalPosToGetLocalPos.get(setLocalIp); + if (usages.size() == 1 && usages.iterator().next().equals(getLocalIp)) { + output.remove(output.size() - 1); + output.remove(output.size() - 1); + stack.push(setItem); + return; + } + } + } + } + stack.push(new LocalRegAVM2Item(ins, localData.lineStartInstruction, regId, computedValue)); } diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/localregs/SetLocalTypeIns.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/localregs/SetLocalTypeIns.java index 516b5f8b8..8514ba51b 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/localregs/SetLocalTypeIns.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/localregs/SetLocalTypeIns.java @@ -12,7 +12,8 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library. */ + * License along with this library. + */ package com.jpexs.decompiler.flash.abc.avm2.instructions.localregs; import com.jpexs.decompiler.flash.abc.ABC; @@ -61,6 +62,7 @@ public abstract class SetLocalTypeIns extends InstructionDefinition implements S public void translate(AVM2LocalData localData, TranslateStack stack, AVM2Instruction ins, List output, String path) { int regId = getRegisterId(ins); GraphTargetItem value = stack.pop(); + /*if (localRegs.containsKey(regId)) { localRegs.put(regId, new NotCompileTimeAVM2Item(ins, localData.lineStartInstruction, value)); } else { @@ -126,9 +128,8 @@ public abstract class SetLocalTypeIns extends InstructionDefinition implements S } } - //if(val.startsWith("catchscope ")) return; - //if(val.startsWith("newactivation()")) return; - output.add(new SetLocalAVM2Item(ins, localData.lineStartInstruction, regId, value)); + GraphTargetItem result = new SetLocalAVM2Item(ins, localData.lineStartInstruction, regId, value); + output.add(result); } @Override diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/other/SetGlobalSlotIns.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/other/SetGlobalSlotIns.java index 4f73b106b..77d14ab97 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/other/SetGlobalSlotIns.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/other/SetGlobalSlotIns.java @@ -12,7 +12,8 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library. */ + * License along with this library. + */ package com.jpexs.decompiler.flash.abc.avm2.instructions.other; import com.jpexs.decompiler.flash.abc.ABC; @@ -43,7 +44,9 @@ public class SetGlobalSlotIns extends InstructionDefinition implements SetTypeIn @Override public void translate(AVM2LocalData localData, TranslateStack stack, AVM2Instruction ins, List output, String path) { - output.add(new SetGlobalSlotAVM2Item(ins, localData.lineStartInstruction, ins.operands[0], stack.pop())); + GraphTargetItem value = stack.pop(); + GraphTargetItem result = new SetGlobalSlotAVM2Item(ins, localData.lineStartInstruction, ins.operands[0], value); + output.add(result); } @Override diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/other/SetPropertyIns.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/other/SetPropertyIns.java index 5a61b1c53..f71efa8e8 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/other/SetPropertyIns.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/other/SetPropertyIns.java @@ -166,8 +166,8 @@ public class SetPropertyIns extends InstructionDefinition implements SetTypeIns } } - output.add(new SetPropertyAVM2Item(ins, localData.lineStartInstruction, obj, multiname, value)); - + GraphTargetItem result = new SetPropertyAVM2Item(ins, localData.lineStartInstruction, obj, multiname, value); + output.add(result); } @Override diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/other/SetSlotIns.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/other/SetSlotIns.java index 6e32e1920..c319e1aa0 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/other/SetSlotIns.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/other/SetSlotIns.java @@ -160,7 +160,8 @@ public class SetSlotIns extends InstructionDefinition implements SetTypeIns { } } - output.add(new SetSlotAVM2Item(ins, localData.lineStartInstruction, obj, objnoreg, slotIndex, slotname, value)); + GraphTargetItem result = new SetSlotAVM2Item(ins, localData.lineStartInstruction, obj, objnoreg, slotIndex, slotname, value); + output.add(result); } @Override diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/other/SetSuperIns.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/other/SetSuperIns.java index df98806e3..2186f58d2 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/other/SetSuperIns.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/other/SetSuperIns.java @@ -12,7 +12,8 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library. */ + * License along with this library. + */ package com.jpexs.decompiler.flash.abc.avm2.instructions.other; import com.jpexs.decompiler.flash.abc.ABC; @@ -50,9 +51,11 @@ public class SetSuperIns extends InstructionDefinition implements SetTypeIns { int multinameIndex = ins.operands[0]; GraphTargetItem value = stack.pop(); + FullMultinameAVM2Item multiname = resolveMultiname(localData, true, stack, localData.getConstants(), multinameIndex, ins); GraphTargetItem obj = stack.pop(); - output.add(new SetSuperAVM2Item(ins, localData.lineStartInstruction, value, obj, multiname)); + GraphTargetItem result = new SetSuperAVM2Item(ins, localData.lineStartInstruction, value, obj, multiname); + output.add(result); } @Override diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/graph/model/DuplicateItem.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/graph/model/DuplicateItem.java index 37c08a8c9..81be012b7 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/graph/model/DuplicateItem.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/graph/model/DuplicateItem.java @@ -12,7 +12,8 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library. */ + * License along with this library. + */ package com.jpexs.decompiler.graph.model; import com.jpexs.decompiler.flash.SourceGeneratorLocalData; @@ -48,8 +49,10 @@ public class DuplicateItem extends GraphTargetItem implements SimpleValue { @Override public GraphTextWriter appendTo(GraphTextWriter writer, LocalData localData) throws InterruptedException { - if (((value instanceof SimpleValue) && (((SimpleValue) value).isSimpleValue())) || !Configuration.displayDupInstructions.get()) { - return value.appendTry(writer, localData); + if (!Configuration.displayDupInstructions.get()) { + if (((value instanceof SimpleValue) && (((SimpleValue) value).isSimpleValue())) || !Configuration.displayDupInstructions.get()) { + return value.appendTry(writer, localData); + } } writer.append("§§dup("); value.appendTry(writer, localData);