diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java index 14fb753e..50d128fc 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java @@ -159,6 +159,11 @@ final class MixinConfig implements Comparable, IMixinConfig { */ private final transient Set unhandledTargets = new HashSet(); + /** + * Mixins which have been parsed but not yet prepared + */ + private final transient List pendingMixins = new ArrayList(); + /** * All mixins loaded by this config */ @@ -688,7 +693,7 @@ final class MixinConfig implements Comparable, IMixinConfig { } } - private void prepareMixins(List mixinClasses, boolean suppressPlugin) { + private void prepareMixins(List mixinClasses, boolean ignorePlugin) { if (mixinClasses == null) { return; } @@ -703,9 +708,25 @@ final class MixinConfig implements Comparable, IMixinConfig { MixinInfo mixin = null; try { - mixin = new MixinInfo(this.service, this, mixinClass, this.plugin, suppressPlugin); + this.pendingMixins.add(mixin = new MixinInfo(this.service, this, mixinClass, this.plugin, ignorePlugin)); + MixinConfig.globalMixinList.add(fqMixinClass); + } catch (InvalidMixinException ex) { + if (this.required) { + throw ex; + } + this.logger.error(ex.getMessage(), ex); + } catch (Exception ex) { + if (this.required) { + throw new InvalidMixinException(mixin, "Error initialising mixin " + mixin + " - " + ex.getClass() + ": " + ex.getMessage(), ex); + } + this.logger.error(ex.getMessage(), ex); + } + } + + for (MixinInfo mixin : this.pendingMixins) { + try { + mixin.parseTargets(); if (mixin.getTargetClasses().size() > 0) { - MixinConfig.globalMixinList.add(fqMixinClass); for (String targetClass : mixin.getTargetClasses()) { String targetClassName = targetClass.replace('/', '.'); this.mixinsFor(targetClassName).add(mixin); @@ -728,6 +749,8 @@ final class MixinConfig implements Comparable, IMixinConfig { this.logger.error(ex.getMessage(), ex); } } + + this.pendingMixins.clear(); } void postApply(String transformedName, ClassNode targetClass) { @@ -964,6 +987,18 @@ final class MixinConfig implements Comparable, IMixinConfig { return this.mixinMapping.containsKey(targetClass); } + boolean hasPendingMixinsFor(String targetClass) { + if (this.packageMatch(targetClass)) { + return false; + } + for (MixinInfo pendingMixin : this.pendingMixins) { + if (pendingMixin.hasDeclaredTarget(targetClass)) { + return true; + } + } + return false; + } + /** * Get mixins for the specified target class * diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java index eac3e63b..1ba207f1 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java @@ -39,6 +39,7 @@ import org.apache.logging.log4j.Logger; import org.objectweb.asm.ClassReader; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; +import org.objectweb.asm.Type; import org.objectweb.asm.tree.AnnotationNode; import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.FieldNode; @@ -71,8 +72,8 @@ import org.spongepowered.asm.util.asm.ASM; import org.spongepowered.asm.util.perf.Profiler; import org.spongepowered.asm.util.perf.Profiler.Section; -import com.google.common.base.Function; import com.google.common.base.Functions; +import com.google.common.collect.Iterables; import com.google.common.collect.Lists; /** @@ -670,6 +671,37 @@ class MixinInfo implements Comparable, IMixinInfo { } + /** + * Handle for a declared target on a mixin. + */ + static final class DeclaredTarget { + + final String name; + + final boolean isPrivate; + + private DeclaredTarget(String name, boolean isPrivate) { + this.name = name; + this.isPrivate = isPrivate; + } + + @Override + public String toString() { + return this.name; + } + + static DeclaredTarget of(Object target, MixinInfo info) { + if (target instanceof String) { + String remappedName = info.remapClassName((String)target); + return remappedName != null ? new DeclaredTarget(remappedName, true) : null; + } else if (target instanceof Type) { + return new DeclaredTarget(((Type)target).getClassName(), false); + } + return null; + } + + } + /** * Mixin service */ @@ -718,13 +750,19 @@ class MixinInfo implements Comparable, IMixinInfo { /** * Mixin targets, read from the {@link Mixin} annotation on the mixin class + * but not yet parsed in the current environment */ - private final List targetClasses; + private final transient List declaredTargets; + + /** + * Mixin targets, read from the {@link Mixin} annotation on the mixin class + */ + private final transient List targetClasses = new ArrayList(); /** * Names of target classes */ - private final List targetClassNames; + private final List targetClassNames = new ArrayList(); /** * Intrinsic order (for sorting mixins with identical priority) @@ -777,10 +815,10 @@ class MixinInfo implements Comparable, IMixinInfo { * @param parent configuration which owns this mixin, the parent * @param name name of this mixin (class name stub) * @param plugin mixin config companion plugin handle - * @param suppressPlugin true to suppress the plugin from filtering targets - * of this mixin + * @param ignorePlugin true to prevent the plugin from filtering targets of + * this mixin */ - MixinInfo(IMixinService service, MixinConfig parent, String name, PluginHandle plugin, boolean suppressPlugin) { + MixinInfo(IMixinService service, MixinConfig parent, String name, PluginHandle plugin, boolean ignorePlugin) { this.service = service; this.parent = parent; this.name = name; @@ -812,8 +850,22 @@ class MixinInfo implements Comparable, IMixinInfo { try { this.priority = this.readPriority(this.pendingState.getClassNode()); this.virtual = this.readPseudo(this.pendingState.getValidationClassNode()); - this.targetClasses = this.readTargetClasses(this.pendingState.getValidationClassNode(), suppressPlugin); - this.targetClassNames = Collections.unmodifiableList(Lists.transform(this.targetClasses, Functions.toStringFunction())); + this.declaredTargets = this.readDeclaredTargets(this.pendingState.getValidationClassNode(), ignorePlugin); + } catch (InvalidMixinException ex) { + throw ex; + } catch (Exception ex) { + throw new InvalidMixinException(this, ex); + } + } + + /** + * Parse the declared targets from the annotation into ClassInfo instances + * and perform initial validation of each target + */ + void parseTargets() { + try { + this.targetClasses.addAll(this.readTargetClasses(this.declaredTargets)); + this.targetClassNames.addAll(Lists.transform(this.targetClasses, Functions.toStringFunction())); } catch (InvalidMixinException ex) { throw ex; } catch (Exception ex) { @@ -838,15 +890,15 @@ class MixinInfo implements Comparable, IMixinInfo { } /** - * Read the target class names from the {@link Mixin} annotation + * Read the declared target class names from the {@link Mixin} annotation * * @param classNode mixin classnode - * @param suppressPlugin true to suppress plugin filtering targets + * @param ignorePlugin true to suppress plugin filtering targets * @return target class list read from classNode */ - protected List readTargetClasses(MixinClassNode classNode, boolean suppressPlugin) { + protected List readDeclaredTargets(MixinClassNode classNode, boolean ignorePlugin) { if (classNode == null) { - return Collections.emptyList(); + return Collections.emptyList(); } AnnotationNode mixin = Annotations.getInvisible(classNode, Mixin.class); @@ -854,75 +906,97 @@ class MixinInfo implements Comparable, IMixinInfo { throw new InvalidMixinException(this, String.format("The mixin '%s' is missing an @Mixin annotation", this.className)); } - List targets = new ArrayList(); - List publicTargets = Annotations.getValue(mixin, "value"); - List privateTargets = Annotations.getValue(mixin, "targets"); - - if (publicTargets != null) { - this.readTargets(targets, Lists.transform(publicTargets, new Function() { - @Override - public String apply(org.objectweb.asm.Type input) { - return input.getClassName(); - } - }), suppressPlugin, false); - } - - if (privateTargets != null) { - this.readTargets(targets, Lists.transform(privateTargets, new Function() { - @Override - public String apply(String input) { - return MixinInfo.this.getParent().remapClassName(MixinInfo.this.getClassRef(), input); - } - }), suppressPlugin, true); - } - - return targets; - } - - /** - * Reads a target list into the outTargets list - */ - private void readTargets(Collection outTargets, Collection inTargets, boolean suppressPlugin, boolean checkPublic) { - for (String targetRef : inTargets) { - String targetName = targetRef.replace('/', '.'); - if (MixinInfo.classLoaderUtil.isClassLoaded(targetName) && !this.isReloading()) { - String message = String.format("Critical problem: %s target %s was loaded too early.", this, targetName); + List declaredTargets = new ArrayList(); + for (Object target : this.readTargets(mixin)) { + DeclaredTarget declaredTarget = DeclaredTarget.of(target, this); + if (declaredTarget == null) { + continue; + } + if (MixinInfo.classLoaderUtil.isClassLoaded(declaredTarget.name) && !this.isReloading()) { + String message = String.format("Critical problem: %s target %s was loaded too early.", this, declaredTarget.name); if (this.parent.isRequired()) { - throw new MixinTargetAlreadyLoadedException(this, message, targetName); + throw new MixinTargetAlreadyLoadedException(this, message, declaredTarget.name); } this.logger.error(message); } - if (this.shouldApplyMixin(suppressPlugin, targetName)) { - ClassInfo targetInfo = this.getTarget(targetName, checkPublic); - if (targetInfo != null && !outTargets.contains(targetInfo)) { - outTargets.add(targetInfo); - targetInfo.addMixin(this); - } + if (this.shouldApplyMixin(ignorePlugin, declaredTarget.name)) { + declaredTargets.add(declaredTarget); } } + return declaredTargets; } - private boolean shouldApplyMixin(boolean suppressPlugin, String targetName) { + /** + * Combine the public and private mixin targets from the supplied annotation + * and return them as an interable collection + * + * @param mixin mixin annotation + * @return target list + */ + private Iterable readTargets(AnnotationNode mixin) { + Iterable publicTargets = Annotations.getValue(mixin, "value"); + Iterable privateTargets = Annotations.getValue(mixin, "targets"); + if (publicTargets == null && privateTargets == null) { + return Collections.emptyList(); + } + if (publicTargets == null) { + return privateTargets; + } + return privateTargets == null ? publicTargets : Iterables.concat(publicTargets, privateTargets); + } + + /** + * Check whether this mixin should apply to the specified taret + * + * @param ignorePlugin true to ignore the config plugin + * @param targetName target class name + * @return true if the mixin should be a pplied + */ + private boolean shouldApplyMixin(boolean ignorePlugin, String targetName) { Section pluginTimer = this.profiler.begin("plugin"); - boolean result = suppressPlugin || this.plugin.shouldApplyMixin(targetName, this.className); + boolean result = ignorePlugin || this.plugin.shouldApplyMixin(targetName, this.className); pluginTimer.end(); return result; } - private ClassInfo getTarget(String targetName, boolean checkPublic) throws InvalidMixinException { - ClassInfo targetInfo = ClassInfo.forName(targetName); + /** + * Read and parse target classes from the supplied class node + * + * @param classNode class node to parse + * @param ignorePlugin true to ignore the config plugin when deciding + * whether to apply declared targets + * @return new list of target classes + */ + List readTargetClasses(MixinClassNode classNode, boolean ignorePlugin) { + return this.readTargetClasses(this.readDeclaredTargets(classNode, ignorePlugin)); + } + + private List readTargetClasses(List declaredTargets) throws InvalidMixinException { + List targetClasses = new ArrayList(); + for (DeclaredTarget target : declaredTargets) { + ClassInfo targetClass = this.getTargetClass(target); + if (targetClass != null) { + targetClasses.add(targetClass); + targetClass.addMixin(this); + } + } + return targetClasses; + } + + private ClassInfo getTargetClass(DeclaredTarget target) throws InvalidMixinException { + ClassInfo targetInfo = ClassInfo.forName(target.name); if (targetInfo == null) { if (this.isVirtual()) { - this.logger.debug("Skipping virtual target {} for {}", targetName, this); + this.logger.debug("Skipping virtual target {} for {}", target.name, this); } else { - this.handleTargetError(String.format("@Mixin target %s was not found %s", targetName, this)); + this.handleTargetError(String.format("@Mixin target %s was not found %s", target.name, this)); } return null; } - this.type.validateTarget(targetName, targetInfo); - if (checkPublic && targetInfo.isPublic() && !this.isVirtual()) { - this.handleTargetError(String.format("@Mixin target %s is public in %s and should be specified in value", targetName, this)); + this.type.validateTarget(target.name, targetInfo); + if (target.isPrivate && targetInfo.isPublic() && !this.isVirtual()) { + this.handleTargetError(String.format("@Mixin target %s is public in %s and should be specified in value", target.name, this)); } return targetInfo; } @@ -962,6 +1036,19 @@ class MixinInfo implements Comparable, IMixinInfo { private boolean isReloading() { return this.pendingState instanceof Reloaded; } + + String remapClassName(String className) { + return this.parent.remapClassName(this.getClassRef(), className); + } + + public boolean hasDeclaredTarget(String targetClass) { + for (DeclaredTarget declaredTarget : this.declaredTargets) { + if (targetClass.equals(declaredTarget.name)) { + return true; + } + } + return false; + } /** * Current state, either the validated state or the uninitialised state if @@ -1101,12 +1188,19 @@ class MixinInfo implements Comparable, IMixinInfo { return this.getState().createClassNode(flags); } + /** + * Get the target class names as declared for this mixin + */ + List getDeclaredTargetClasses() { + return Collections.unmodifiableList(Lists.transform(this.declaredTargets, Functions.toStringFunction())); + } + /** * Get the target class names for this mixin */ @Override public List getTargetClasses() { - return this.targetClassNames; + return Collections.unmodifiableList(this.targetClassNames); } /** diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinProcessor.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinProcessor.java index 50e8474f..66cfb19b 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinProcessor.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinProcessor.java @@ -53,6 +53,7 @@ import org.spongepowered.asm.mixin.transformer.ext.extensions.ExtensionClassExpo import org.spongepowered.asm.mixin.transformer.meta.MixinMerged; import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException; import org.spongepowered.asm.mixin.transformer.throwables.MixinTransformerError; +import org.spongepowered.asm.mixin.transformer.throwables.ReEntrantTransformerError; import org.spongepowered.asm.service.IMixinService; import org.spongepowered.asm.service.MixinService; import org.spongepowered.asm.util.PrettyPrinter; @@ -282,7 +283,15 @@ public class MixinProcessor { boolean locked = this.lock.push().check(); Section mixinTimer = this.profiler.begin("mixin"); - if (!locked) { + if (locked) { + for (MixinConfig config : this.pendingConfigs) { + if (config.hasPendingMixinsFor(name)) { + ReEntrantTransformerError error = new ReEntrantTransformerError("Re-entrance error."); + MixinProcessor.logger.warn("Re-entrance detected during prepare phase, this will cause serious problems.", error); + throw error; + } + } + } else { try { this.checkSelect(environment); } catch (Exception ex) { @@ -321,15 +330,16 @@ public class MixinProcessor { mixins.addAll(config.getMixinsFor(name)); } } - + if (invalidRef) { throw new NoClassDefFoundError(String.format("%s is a mixin class and cannot be referenced directly", name)); } if (mixins != null) { + // Re-entrance is "safe" as long as we don't need to apply any mixins, if there are mixins then we need to panic now if (locked) { - MixinApplyError error = new MixinApplyError("Re-entrance error."); + ReEntrantTransformerError error = new ReEntrantTransformerError("Re-entrance error."); MixinProcessor.logger.warn("Re-entrance detected, this will cause serious problems.", error); throw error; } @@ -353,7 +363,8 @@ public class MixinProcessor { this.handleMixinApplyError(name, th, environment); } } - + } catch (MixinTransformerError er) { + throw er; } catch (Throwable th) { th.printStackTrace(); this.dumpClassOnFailure(name, targetClassNode, environment); diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/throwables/ReEntrantTransformerError.java b/src/main/java/org/spongepowered/asm/mixin/transformer/throwables/ReEntrantTransformerError.java new file mode 100644 index 00000000..a756dd06 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/throwables/ReEntrantTransformerError.java @@ -0,0 +1,46 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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.throwables; + +/** + * Error thrown when transformer re-entrance is detected + */ +public class ReEntrantTransformerError extends MixinTransformerError { + + private static final long serialVersionUID = 7073583236491579255L; + + public ReEntrantTransformerError(String message) { + super(message); + } + + public ReEntrantTransformerError(Throwable cause) { + super(cause); + } + + public ReEntrantTransformerError(String message, Throwable cause) { + super(message, cause); + } + +}