import com.jpexs.decompiler.flash.SWF; import com.jpexs.decompiler.flash.abc.ABC; import com.jpexs.decompiler.flash.abc.ScriptPack; import com.jpexs.decompiler.flash.abc.avm2.instructions.AVM2Instruction; import com.jpexs.decompiler.flash.abc.types.MethodBody; import java.io.BufferedInputStream; import java.nio.file.Files; import java.nio.file.Path; import java.util.*; /** Checks reachable operand/scope stack heights and every branch destination. */ public final class AuditBattleBytecode { public static int verify(ABC abc, MethodBody body) { var code = body.getCode(); Map positions = new HashMap<>(); for (int i = 0; i < code.code.size(); i++) positions.put(code.code.get(i).getAddress(), i); int errors = 0; for (var ins : code.code) { if (ins.isBranch() || ins.isJump()) for (long target : ins.getOffsets()) { if (!positions.containsKey(target)) { System.out.println(" INVALID BRANCH " + ins.getAddress() + " -> " + target); errors++; } } } Deque pending = new ArrayDeque<>(); pending.add(new int[]{0, 0, body.init_scope_depth}); for (var ex : body.exceptions) { if (positions.containsKey((long) ex.target)) pending.add(new int[]{positions.get((long) ex.target), 1, body.init_scope_depth}); } Map seen = new HashMap<>(); while (!pending.isEmpty()) { int[] state = pending.removeFirst(); int index = state[0], height = state[1], scope = state[2]; if (index < 0 || index >= code.code.size()) continue; String value = height + "/" + scope; String old = seen.putIfAbsent(index, value); var ins = code.code.get(index); if (old != null) { if (!old.equals(value)) { System.out.println(" STACK MERGE " + ins.getAddress() + " " + old + " != " + value); errors++; } continue; } int pops = ins.definition.getStackPopCount(ins, abc); int pushes = ins.definition.getStackPushCount(ins, abc); if (height < pops) { System.out.println(" STACK UNDERFLOW at " + ins.getAddress() + " " + ins.definition.instructionName + " height=" + height + " needs=" + pops); errors++; continue; } height += pushes - pops; scope += ins.definition.getScopeStackDelta(ins, abc); if (height > body.max_stack || scope < body.init_scope_depth || scope > body.max_scope_depth) { System.out.println(" STACK LIMIT at " + ins.getAddress() + " stack=" + height + "/" + body.max_stack + " scope=" + scope + "/" + body.max_scope_depth); errors++; } if (ins.isBranch() || ins.isJump()) for (long target : ins.getOffsets()) { Integer next = positions.get(target); if (next != null) pending.add(new int[]{next, height, scope}); } if (!ins.isExit() && !ins.isJump() && !ins.definition.instructionName.equals("lookupswitch")) pending.add(new int[]{index + 1, height, scope}); } return errors; } public static void main(String[] args) throws Exception { boolean bad = false; for (String filename : args) { if (filename.equals("--dump")) continue; boolean dump = Arrays.asList(args).contains("--dump"); try (var input = new BufferedInputStream(Files.newInputStream(Path.of(filename)))) { SWF swf = new SWF(input, false); for (ScriptPack pack : swf.getAS3Packs()) { String name = pack.getClassPath().toString(); Set methods = switch (name) { case "net.pixvi.myr.battle.view.BattleMon" -> Set.of("beHurt", "all_atk", "init"); case "net.pixvi.myr.center.TeamCenter" -> Set.of("getBattleTeamVo"); case "net.pixvi.myrloader.MYRLoaderStarter" -> Set.of("fixComplete"); case "MYRStarter" -> Set.of("MYRStart"); default -> Set.of(); }; if (methods.isEmpty()) continue; ABC abc = pack.abc; for (MethodBody body : abc.bodies) { String method = abc.constants.getString(abc.method_info.get(body.method_info).name_index); if (method == null || !methods.contains(method)) continue; System.out.println("CHECK " + filename + " " + name + "." + method + " method=" + body.method_info + " bytes=" + body.getCodeBytes().length); int errors = verify(abc, body); System.out.println((errors == 0 ? "PASS" : "FAIL") + " control-flow/stack errors=" + errors); bad |= errors != 0; if (dump) { Path target = Path.of("analysis", "bytecode_audit", Path.of(filename).getFileName() + "." + method + ".txt"); Files.createDirectories(target.getParent()); StringBuilder out = new StringBuilder(); for (var ins : body.getCode().code) out.append(String.format("%04x %s%n", ins.getAddress(), ins.toStringNoAddress(abc.constants, List.of()))); Files.writeString(target, out); } } } } } if (bad) System.exit(1); } }