Detect & report transformer reentrance during config prepare, fixes #333

This commit is contained in:
Mumfrey committed 2019-10-16 16:31:10 +01:00
1 parent 7567dec1d8
commit 22909dcfa2
4 files changed
+255 -69

No files matched your search

@@ -159,6 +159,11 @@ final class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
*/
private final transient Set<String> unhandledTargets = new HashSet<String>();
/**
* Mixins which have been parsed but not yet prepared
*/
private final transient List<MixinInfo> pendingMixins = new ArrayList<MixinInfo>();
/**
* All mixins loaded by this config
*/
@@ -688,7 +693,7 @@ final class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
}
}
private void prepareMixins(List<String> mixinClasses, boolean suppressPlugin) {
private void prepareMixins(List<String> mixinClasses, boolean ignorePlugin) {
if (mixinClasses == null) {
return;
}
@@ -703,9 +708,25 @@ final class MixinConfig implements Comparable<MixinConfig>, 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<MixinConfig>, 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<MixinConfig>, 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
*
@@ -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<MixinInfo>, 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<MixinInfo>, IMixinInfo {
/**
* Mixin targets, read from the {@link Mixin} annotation on the mixin class
* but not yet parsed in the current environment
*/
private final List<ClassInfo> targetClasses;
private final transient List<DeclaredTarget> declaredTargets;
/**
* Mixin targets, read from the {@link Mixin} annotation on the mixin class
*/
private final transient List<ClassInfo> targetClasses = new ArrayList<ClassInfo>();
/**
* Names of target classes
*/
private final List<String> targetClassNames;
private final List<String> targetClassNames = new ArrayList<String>();
/**
* Intrinsic order (for sorting mixins with identical priority)
@@ -777,10 +815,10 @@ class MixinInfo implements Comparable<MixinInfo>, 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<MixinInfo>, 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.<String>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<MixinInfo>, 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<ClassInfo> readTargetClasses(MixinClassNode classNode, boolean suppressPlugin) {
protected List<DeclaredTarget> readDeclaredTargets(MixinClassNode classNode, boolean ignorePlugin) {
if (classNode == null) {
return Collections.<ClassInfo>emptyList();
return Collections.<DeclaredTarget>emptyList();
}
AnnotationNode mixin = Annotations.getInvisible(classNode, Mixin.class);
@@ -854,75 +906,97 @@ class MixinInfo implements Comparable<MixinInfo>, IMixinInfo {
throw new InvalidMixinException(this, String.format("The mixin '%s' is missing an @Mixin annotation", this.className));
}
List<ClassInfo> targets = new ArrayList<ClassInfo>();
List<org.objectweb.asm.Type> publicTargets = Annotations.getValue(mixin, "value");
List<String> privateTargets = Annotations.getValue(mixin, "targets");
if (publicTargets != null) {
this.readTargets(targets, Lists.transform(publicTargets, new Function<org.objectweb.asm.Type, String>() {
@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<String, String>() {
@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<ClassInfo> outTargets, Collection<String> 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<DeclaredTarget> declaredTargets = new ArrayList<DeclaredTarget>();
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<Object> readTargets(AnnotationNode mixin) {
Iterable<Object> publicTargets = Annotations.getValue(mixin, "value");
Iterable<Object> privateTargets = Annotations.getValue(mixin, "targets");
if (publicTargets == null && privateTargets == null) {
return Collections.<Object>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<ClassInfo> readTargetClasses(MixinClassNode classNode, boolean ignorePlugin) {
return this.readTargetClasses(this.readDeclaredTargets(classNode, ignorePlugin));
}
private List<ClassInfo> readTargetClasses(List<DeclaredTarget> declaredTargets) throws InvalidMixinException {
List<ClassInfo> targetClasses = new ArrayList<ClassInfo>();
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<MixinInfo>, 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<MixinInfo>, IMixinInfo {
return this.getState().createClassNode(flags);
}
/**
* Get the target class names as declared for this mixin
*/
List<String> getDeclaredTargetClasses() {
return Collections.<String>unmodifiableList(Lists.transform(this.declaredTargets, Functions.toStringFunction()));
}
/**
* Get the target class names for this mixin
*/
@Override
public List<String> getTargetClasses() {
return this.targetClassNames;
return Collections.<String>unmodifiableList(this.targetClassNames);
}
/**
@@ -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);
@@ -0,0 +1,46 @@
/*
* This file is part of Mixin, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.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.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);
}
}