From 110882d7190a70c79449b8b742a3f1f40a36a9d7 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Wed, 29 Apr 2015 01:13:52 +0100 Subject: [PATCH] Fix functionality of LVT generator in production --- .../injection/callback/CallbackInjector.java | 5 +- .../asm/mixin/transformer/MixinVerifier.java | 109 ++++++++++++++++++ .../org/spongepowered/asm/util/Locals.java | 4 +- 3 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 src/main/java/org/spongepowered/asm/mixin/transformer/MixinVerifier.java diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java index e3e4ec5d..5c2d540f 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java @@ -343,10 +343,11 @@ public class CallbackInjector extends Injector { * @return generated message */ private String generateBadLVTMessage(final Callback callback) { + int position = callback.target.method.instructions.indexOf(callback.node); List expected = CallbackInjector.summariseLocals(this.methodNode.desc, callback.target.arguments.length + 1); List found = CallbackInjector.summariseLocals(callback.localTypes, callback.frameSize); - String message = String.format("Local variable table in %s::%s has incompatible changes. Expected %s but found %s. Callback %s%s", - this.classNode.name.replace('/', '.'), callback.target.method.name, expected, found, this.methodNode.name, this.methodNode.desc); + String message = String.format("LVT in %s::%s has incompatible changes at opcode %d. Expected %s but found %s. Callback %s%s", + this.classNode.name, callback.target.method.name, position, expected, found, this.methodNode.name, this.methodNode.desc); return message; } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinVerifier.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinVerifier.java new file mode 100644 index 00000000..8c03cbbc --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinVerifier.java @@ -0,0 +1,109 @@ +/* + * This file is part of Sponge, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered.org + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.asm.mixin.transformer; + +import java.util.List; + +import org.objectweb.asm.Type; +import org.objectweb.asm.tree.analysis.SimpleVerifier; + + +/** + * Verifier which handles class info lookups via {@link ClassInfo} + */ +public class MixinVerifier extends SimpleVerifier { + + private Type currentClass; + private Type currentSuperClass; + private List currentClassInterfaces; + private boolean isInterface; + + public MixinVerifier(Type currentClass, Type currentSuperClass, List currentClassInterfaces, boolean isInterface) { + super(currentClass, currentSuperClass, currentClassInterfaces, isInterface); + this.currentClass = currentClass; + this.currentSuperClass = currentSuperClass; + this.currentClassInterfaces = currentClassInterfaces; + this.isInterface = isInterface; + } + + @Override + protected boolean isInterface(final Type t) { + if (this.currentClass != null && t.equals(this.currentClass)) { + return this.isInterface; + } + return this.getClassInfo(t).isInterface(); + } + + @Override + protected Type getSuperClass(final Type t) { + if (this.currentClass != null && t.equals(this.currentClass)) { + return this.currentSuperClass; + } + ClassInfo c = this.getClassInfo(t).getSuperClass(); + return c == null ? null : Type.getType("L" + c.getName() + ";"); + } + + @Override + protected boolean isAssignableFrom(final Type t, final Type u) { + if (t.equals(u)) { + return true; + } + if (this.currentClass != null && t.equals(this.currentClass)) { + if (this.getSuperClass(u) == null) { + return false; + } + if (this.isInterface) { + return u.getSort() == Type.OBJECT || u.getSort() == Type.ARRAY; + } + return this.isAssignableFrom(t, this.getSuperClass(u)); + } + if (this.currentClass != null && u.equals(this.currentClass)) { + if (this.isAssignableFrom(t, this.currentSuperClass)) { + return true; + } + if (this.currentClassInterfaces != null) { + for (int i = 0; i < this.currentClassInterfaces.size(); ++i) { + Type v = this.currentClassInterfaces.get(i); + if (this.isAssignableFrom(t, v)) { + return true; + } + } + } + return false; + } + ClassInfo tc = this.getClassInfo(t); + if (tc.isInterface()) { + tc = ClassInfo.forName("java/lang/Object"); + } + return this.getClassInfo(u).hasSuperClass(tc); + } + + protected ClassInfo getClassInfo(final Type t) { + if (t.getSort() == Type.ARRAY) { + return ClassInfo.forName(t.getDescriptor().replace('/', '.')); + } + return ClassInfo.forName(t.getClassName().replace('.', '/')); + } +} diff --git a/src/main/java/org/spongepowered/asm/util/Locals.java b/src/main/java/org/spongepowered/asm/util/Locals.java index fde6fff1..d0603480 100644 --- a/src/main/java/org/spongepowered/asm/util/Locals.java +++ b/src/main/java/org/spongepowered/asm/util/Locals.java @@ -44,7 +44,7 @@ import org.objectweb.asm.tree.analysis.Analyzer; import org.objectweb.asm.tree.analysis.AnalyzerException; import org.objectweb.asm.tree.analysis.BasicValue; import org.objectweb.asm.tree.analysis.Frame; -import org.objectweb.asm.tree.analysis.SimpleVerifier; +import org.spongepowered.asm.mixin.transformer.MixinVerifier; /** @@ -263,7 +263,7 @@ public class Locals { // Use Analyzer to generate the bytecode frames Analyzer analyzer = new Analyzer( - new SimpleVerifier(Type.getObjectType(classNode.name), objectType, interfaces, false)); + new MixinVerifier(Type.getObjectType(classNode.name), objectType, interfaces, false)); try { analyzer.analyze(classNode.name, method); } catch (AnalyzerException ex) {