Merge remote-tracking branch 'origin/main' into upstream_merge

# Conflicts:
#	src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerInjector.java
#	src/main/java/org/spongepowered/asm/mixin/gen/AccessorGenerator.java
#	src/main/java/org/spongepowered/asm/mixin/gen/AccessorGeneratorMethodProxy.java
#	src/main/java/org/spongepowered/asm/mixin/struct/MemberRef.java
#	src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessorInterface.java
This commit is contained in:
modmuss50 committed 2021-06-28 18:15:10 +01:00
commit 4cd3d1de40
11 files changed
+111 -71

No files matched your search

@@ -127,15 +127,15 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler
}
public AnnotationMirror getAtErrorElement(CompilerEnvironment compilerEnvironment) {
// JDT supports hanging the error on the @At annotation directly, doing this in javac doesn't work
// JDT supports hanging the error on the @At annotation directly, doing this in javac doesn't work
return (compilerEnvironment == CompilerEnvironment.JDT ? this.getAt() : this.getAnnotation()).asMirror();
}
@Override
public IAnnotationHandle getSelectorAnnotation() {
return this.getAt();
}
public String getAtArg(String key) {
if (this.args == null) {
this.args = new HashMap<String, String>();
@@ -162,7 +162,7 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler
}
static class AnnotatedElementSliceInjectionPoint extends AnnotatedElementInjectionPoint {
private final ISelectorContext parentContext;
public AnnotatedElementSliceInjectionPoint(ExecutableElement element, AnnotationHandle inject, IMixinContext context,
@@ -177,26 +177,22 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler
}
}
AnnotatedMixinElementHandlerInjector(IMixinAnnotationProcessor ap, AnnotatedMixin mixin) {
super(ap, mixin);
}
public void registerInjector(AnnotatedElementInjector elem) {
if (this.mixin.isInterface()) {
this.ap.printMessage(Kind.ERROR, "Injector in interface is unsupported", elem.getElement());
}
for (String reference : elem.getAnnotation().<String>getList("method")) {
this.registerInjectorTarget(elem, reference, TargetSelector.parse(reference, elem), elem + ".method=\"" + reference + "\"");
}
for (IAnnotationHandle desc : elem.getAnnotation().getAnnotationList("target")) {
String subject = String.format("%s.target=@Desc(id = \"%s\")", elem, desc.<String>getValue("id", ""));
this.registerInjectorTarget(elem, null, TargetSelector.parse(desc, elem), subject);
}
}
private void registerInjectorTarget(AnnotatedElementInjector elem, String reference, ITargetSelector targetSelector, String subject) {
try {
targetSelector.validate();
@@ -207,16 +203,16 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler
if (!(targetSelector instanceof ITargetSelectorByName)) {
return;
}
ITargetSelectorByName targetMember = (ITargetSelectorByName)targetSelector;
if (targetMember.getName() == null) {
return;
}
if (targetMember.getDesc() != null) {
this.validateReferencedTarget(elem, reference, targetMember, subject);
}
if (targetSelector instanceof ITargetSelectorRemappable && elem.shouldRemap()) {
for (TypeHandle target : this.mixin.getTargets()) {
if (!this.registerInjector(elem, reference, (ITargetSelectorRemappable)targetMember, target)) {
@@ -296,10 +292,6 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler
* and process the references
*/
public void registerInjectionPoint(AnnotatedElementInjectionPoint elem, String format) {
if (this.mixin.isInterface()) {
this.ap.printMessage(Kind.ERROR, "Injector in interface is unsupported", elem.getElement());
}
ITargetSelector targetSelector = null;
String targetReference = elem.getAt().<String>getValue("target");
if (targetReference != null) {
@@ -312,7 +304,7 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler
}
String type = InjectionPointData.parseType(elem.getAt().<String>getValue("value", ""));
ITargetSelector classSelector = null;
String classReference = elem.getAtArg("class");
if ("NEW".equals(type) && classReference != null) {
@@ -338,10 +330,10 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler
if (!(selector instanceof ITargetSelectorConstructor)) {
return;
}
ITargetSelectorConstructor member = (ITargetSelectorConstructor)selector;
String target = member.toCtorType();
if (target != null) {
String desc = member.toCtorDesc();
MappingMethod m = new MappingMethod(target, ".", desc != null ? desc : "()V");
@@ -361,10 +353,10 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler
mappings.put(type, mapping.getDesc().replace(")V", ")L" + mapping.getOwner() + ";"));
}
}
this.obf.getReferenceManager().addClassMapping(this.classRef, reference, mappings);
}
elem.notifyRemapped();
}
@@ -372,7 +364,7 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler
if (!(selector instanceof ITargetSelectorRemappable)) {
return;
}
ITargetSelectorRemappable targetMember = (ITargetSelectorRemappable)selector;
AnnotationMirror errorElement = elem.getAtErrorElement(this.ap.getCompilerEnvironment());
@@ -381,7 +373,7 @@ class AnnotatedMixinElementHandlerInjector extends AnnotatedMixinElementHandler
this.ap.printMessage(Kind.ERROR, subject + " is not fully qualified, missing " + missing, elem.getElement(), errorElement);
return;
}
try {
if (targetMember.isField()) {
ObfuscationData<MappingField> obfFieldData = this.obf.getDataProvider().getObfFieldRecursive(targetMember);
@@ -662,8 +662,8 @@ public final class MixinEnvironment implements ITokenProvider {
/**
* Java 12 and above
*/
JAVA_12(12, Opcodes.V12, LanguageFeatures.METHODS_IN_INTERFACES | LanguageFeatures.PRIVATE_METHODS_IN_INTERFACES
| LanguageFeatures.NESTING | LanguageFeatures.DYNAMIC_CONSTANTS) {
JAVA_12(12, Opcodes.V12, LanguageFeatures.METHODS_IN_INTERFACES | LanguageFeatures.PRIVATE_SYNTHETIC_METHODS_IN_INTERFACES
| LanguageFeatures.PRIVATE_METHODS_IN_INTERFACES | LanguageFeatures.NESTING | LanguageFeatures.DYNAMIC_CONSTANTS) {
@Override
boolean isSupported() {
@@ -675,8 +675,8 @@ public final class MixinEnvironment implements ITokenProvider {
/**
* Java 13 and above
*/
JAVA_13(13, Opcodes.V13, LanguageFeatures.METHODS_IN_INTERFACES | LanguageFeatures.PRIVATE_METHODS_IN_INTERFACES
| LanguageFeatures.NESTING | LanguageFeatures.DYNAMIC_CONSTANTS) {
JAVA_13(13, Opcodes.V13, LanguageFeatures.METHODS_IN_INTERFACES | LanguageFeatures.PRIVATE_SYNTHETIC_METHODS_IN_INTERFACES
| LanguageFeatures.PRIVATE_METHODS_IN_INTERFACES | LanguageFeatures.NESTING | LanguageFeatures.DYNAMIC_CONSTANTS) {
@Override
boolean isSupported() {
@@ -688,8 +688,8 @@ public final class MixinEnvironment implements ITokenProvider {
/**
* Java 14 and above
*/
JAVA_14(14, Opcodes.V14, LanguageFeatures.METHODS_IN_INTERFACES | LanguageFeatures.PRIVATE_METHODS_IN_INTERFACES
| LanguageFeatures.NESTING | LanguageFeatures.DYNAMIC_CONSTANTS) {
JAVA_14(14, Opcodes.V14, LanguageFeatures.METHODS_IN_INTERFACES | LanguageFeatures.PRIVATE_SYNTHETIC_METHODS_IN_INTERFACES
| LanguageFeatures.PRIVATE_METHODS_IN_INTERFACES | LanguageFeatures.NESTING | LanguageFeatures.DYNAMIC_CONSTANTS) {
@Override
boolean isSupported() {
@@ -701,8 +701,8 @@ public final class MixinEnvironment implements ITokenProvider {
/**
* Java 16 and above
*/
JAVA_15(15, Opcodes.V15, LanguageFeatures.METHODS_IN_INTERFACES | LanguageFeatures.PRIVATE_METHODS_IN_INTERFACES
| LanguageFeatures.NESTING | LanguageFeatures.DYNAMIC_CONSTANTS) {
JAVA_15(15, Opcodes.V15, LanguageFeatures.METHODS_IN_INTERFACES | LanguageFeatures.PRIVATE_SYNTHETIC_METHODS_IN_INTERFACES
| LanguageFeatures.PRIVATE_METHODS_IN_INTERFACES | LanguageFeatures.NESTING | LanguageFeatures.DYNAMIC_CONSTANTS) {
@Override
boolean isSupported() {
@@ -714,8 +714,8 @@ public final class MixinEnvironment implements ITokenProvider {
/**
* Java 16 and above
*/
JAVA_16(16, Opcodes.V16, LanguageFeatures.METHODS_IN_INTERFACES | LanguageFeatures.PRIVATE_METHODS_IN_INTERFACES
| LanguageFeatures.NESTING | LanguageFeatures.DYNAMIC_CONSTANTS) {
JAVA_16(16, Opcodes.V16, LanguageFeatures.METHODS_IN_INTERFACES | LanguageFeatures.PRIVATE_SYNTHETIC_METHODS_IN_INTERFACES
| LanguageFeatures.PRIVATE_METHODS_IN_INTERFACES | LanguageFeatures.NESTING | LanguageFeatures.DYNAMIC_CONSTANTS) {
@Override
boolean isSupported() {
@@ -727,8 +727,8 @@ public final class MixinEnvironment implements ITokenProvider {
/**
* Java 17 and above
*/
JAVA_17(17, Opcodes.V17, LanguageFeatures.METHODS_IN_INTERFACES | LanguageFeatures.PRIVATE_METHODS_IN_INTERFACES
| LanguageFeatures.NESTING | LanguageFeatures.DYNAMIC_CONSTANTS) {
JAVA_17(17, Opcodes.V17, LanguageFeatures.METHODS_IN_INTERFACES | LanguageFeatures.PRIVATE_SYNTHETIC_METHODS_IN_INTERFACES
| LanguageFeatures.PRIVATE_METHODS_IN_INTERFACES | LanguageFeatures.NESTING | LanguageFeatures.DYNAMIC_CONSTANTS) {
@Override
boolean isSupported() {
@@ -26,11 +26,11 @@ package org.spongepowered.asm.mixin.gen;
import java.util.ArrayList;
import org.apache.logging.log4j.LogManager;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.MethodNode;
import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException;
import org.spongepowered.asm.mixin.refmap.IMixinContext;
import org.spongepowered.asm.mixin.gen.throwables.InvalidAccessorException;
import org.spongepowered.asm.util.asm.ASM;
/**
@@ -54,10 +54,12 @@ public abstract class AccessorGenerator {
}
protected void checkModifiers() {
if (this.info.isStatic() && !this.targetIsStatic) {
IMixinContext context = this.info.getMixin();
throw new InvalidInjectionException(context, String.format("%s is invalid. Accessor method is%s static but the target is not.",
this.info, this.info.isStatic() ? "" : " not"));
if (this.info.isStatic() != this.targetIsStatic) {
if (!this.targetIsStatic) {
throw new InvalidAccessorException(this.info, String.format("%s is invalid. Accessor method is static but the target is not.", this.info));
} else {
LogManager.getLogger("mixin").info("{} should be static as its target is", this.info);
}
}
}
@@ -31,6 +31,7 @@ import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.VarInsnNode;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.gen.throwables.InvalidAccessorException;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.transformer.ClassInfo.Method;
import org.spongepowered.asm.mixin.transformer.MixinTargetContext;
@@ -52,6 +53,12 @@ public class AccessorGeneratorFieldSetter extends AccessorGeneratorField {
@Override
public void validate() {
if (Bytecode.hasFlag(this.info.getClassNode(), Opcodes.ACC_INTERFACE)) {
//This will result in a ClassFormatError when the class is verified
throw new InvalidAccessorException(info, String.format("%s tried to change interface field %s::%s",
this.info, this.info.getClassNode().name, this.targetField.name));
}
super.validate();
Method method = this.info.getClassInfo().findMethod(this.info.getMethod());
@@ -75,9 +75,10 @@ public class AccessorGeneratorMethodProxy extends AccessorGenerator {
method.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
}
Bytecode.loadArgs(this.argTypes, method.instructions, this.info.isStatic ? 0 : 1);
boolean isInterface = Bytecode.hasFlag(this.info.getClassNode(), Opcodes.ACC_INTERFACE);
boolean isPrivate = Bytecode.hasFlag(this.targetMethod, Opcodes.ACC_PRIVATE);
int opcode = this.targetIsStatic ? Opcodes.INVOKESTATIC : (isPrivate ? Opcodes.INVOKESPECIAL : Opcodes.INVOKEVIRTUAL);
method.instructions.add(new MethodInsnNode(opcode, this.info.getClassNode().name, this.targetMethod.name, this.targetMethod.desc, false));
int opcode = this.targetIsStatic ? Opcodes.INVOKESTATIC : isInterface ? Opcodes.INVOKEINTERFACE : (isPrivate ? Opcodes.INVOKESPECIAL : Opcodes.INVOKEVIRTUAL);
method.instructions.add(new MethodInsnNode(opcode, this.info.getClassNode().name, this.targetMethod.name, this.targetMethod.desc, isInterface));
method.instructions.add(new InsnNode(this.returnType.getOpcode(Opcodes.IRETURN)));
return method;
}
@@ -165,6 +165,12 @@ public class CallbackInjector extends Injector {
*/
private boolean captureArgs = true;
/**
* Whether {@link #handler} uses the {@link CallbackInfo}/
* {@link CallbackInfoReturnable} it would normally be passed
*/
final boolean usesCallbackInfo;
Callback(MethodNode handler, Target target, final InjectionNode node, final LocalVariableNode[] locals, boolean captureLocals) {
this.handler = handler;
this.target = target;
@@ -208,6 +214,19 @@ public class CallbackInjector extends Injector {
if (this.canCaptureLocals) {
this.invoke.add(this.localTypes.length - this.frameSize);
}
//If the handler doesn't captureArgs, the CallbackInfo(Returnable) will be the first LVT slot, otherwise it will be at the target's frameSize
int callbackInfoSlot = handlerArgs.length == 1 ? Bytecode.isStatic(handler) ? 0 : 1 : frameSize;
boolean seenCallbackInfoUse = false;
for (AbstractInsnNode insn : handler.instructions) {
//Look for anywhere the CallbackInfo(Returnable) is loaded in the handler, it's unused if it is never loaded in
if (insn.getType() == AbstractInsnNode.VAR_INSN && insn.getOpcode() == Opcodes.ALOAD && ((VarInsnNode) insn).var == callbackInfoSlot) {
seenCallbackInfoUse = true;
break;
}
}
Injector.logger.debug("{} does{} use it's CallbackInfo{}", info, seenCallbackInfoUse ? "" : "n't", Type.VOID_TYPE == target.returnType ? "" : "Returnable");
usesCallbackInfo = seenCallbackInfoUse;
}
/**
@@ -530,12 +549,14 @@ public class CallbackInjector extends Injector {
}
}
this.dupReturnValue(callback);
if (this.cancellable || this.totalInjections > 1) {
this.createCallbackInfo(callback, true);
if (callback.usesCallbackInfo) {
this.dupReturnValue(callback);
if (this.cancellable || this.totalInjections > 1) {
this.createCallbackInfo(callback, true);
}
}
this.invokeCallback(callback, callbackMethod);
this.injectCancellationCode(callback);
if (callback.usesCallbackInfo) this.injectCancellationCode(callback);
callback.inject();
this.info.notifyInjected(callback.target);
@@ -647,7 +668,9 @@ public class CallbackInjector extends Injector {
* @param callback callback handle
*/
private void loadOrCreateCallbackInfo(final Callback callback) {
if (this.cancellable || this.totalInjections > 1) {
if (!callback.usesCallbackInfo) {
callback.add(new InsnNode(Opcodes.ACONST_NULL));
} else if (this.cancellable || this.totalInjections > 1) {
callback.add(new VarInsnNode(Opcodes.ALOAD, this.callbackInfoVar), false, true);
} else {
this.createCallbackInfo(callback, false);
@@ -422,9 +422,10 @@ public abstract class Injector {
* @return injected insn node
*/
protected AbstractInsnNode invokeHandler(InsnList insns, MethodNode handler) {
boolean isInterface = Bytecode.hasFlag(classNode, Opcodes.ACC_INTERFACE);
boolean isPrivate = (handler.access & Opcodes.ACC_PRIVATE) != 0;
int invokeOpcode = this.isStatic ? Opcodes.INVOKESTATIC : isPrivate ? Opcodes.INVOKESPECIAL : Opcodes.INVOKEVIRTUAL;
MethodInsnNode insn = new MethodInsnNode(invokeOpcode, this.classNode.name, handler.name, handler.desc, false);
int invokeOpcode = this.isStatic ? Opcodes.INVOKESTATIC : isInterface ? Opcodes.INVOKEINTERFACE : isPrivate ? Opcodes.INVOKESPECIAL : Opcodes.INVOKEVIRTUAL;
MethodInsnNode insn = new MethodInsnNode(invokeOpcode, this.classNode.name, handler.name, handler.desc, isInterface);
insns.add(insn);
this.info.addCallbackInvocation(handler);
return insn;
@@ -27,6 +27,7 @@ package org.spongepowered.asm.mixin.struct;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.FieldInsnNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.spongepowered.asm.mixin.transformer.ClassInfo;
import org.spongepowered.asm.mixin.transformer.throwables.MixinTransformerError;
import org.spongepowered.asm.util.Bytecode;
import org.spongepowered.asm.util.Handles;
@@ -24,14 +24,17 @@
*/
package org.spongepowered.asm.mixin.transformer;
import java.lang.reflect.Modifier;
import java.util.Map.Entry;
import org.objectweb.asm.tree.FieldNode;
import org.objectweb.asm.tree.MethodNode;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException;
import org.spongepowered.asm.mixin.transformer.ClassInfo.Field;
import org.spongepowered.asm.mixin.transformer.throwables.InvalidInterfaceMixinException;
import org.spongepowered.asm.util.LanguageFeatures;
/**
* Applicator for interface mixins, mainly just disables things which aren't
@@ -96,28 +99,34 @@ class MixinApplicatorInterface extends MixinApplicatorStandard {
*/
@Override
protected void prepareInjections(MixinTargetContext mixin) {
// disabled for interface mixins
for (MethodNode method : this.targetClass.methods) {
try {
InjectionInfo injectInfo = InjectionInfo.parse(mixin, method);
if (injectInfo != null) {
throw new InvalidInterfaceMixinException(mixin, injectInfo + " is not supported on interface mixin method " + method.name);
//Make sure we're running on a Java version which supports interfaces having method bodies
if (!MixinEnvironment.getCompatibilityLevel().supports(LanguageFeatures.METHODS_IN_INTERFACES)) {
throw new InvalidInterfaceMixinException(mixin, injectInfo + " is not supported on interface mixin method " + method.name);
}
}
} catch (InvalidInjectionException ex) {
String description = ex.getInjectionInfo() != null ? ex.getInjectionInfo().toString() : "Injection";
throw new InvalidInterfaceMixinException(mixin, description + " is not supported in interface mixin");
}
}
super.prepareInjections(mixin);
}
/* (non-Javadoc)
* @see org.spongepowered.asm.mixin.transformer.MixinApplicator
* #applyInjections(
* org.spongepowered.asm.mixin.transformer.MixinTargetContext)
*/
@Override
protected void applyInjections(MixinTargetContext mixin) {
// Do nothing
}
protected void checkMethodVisibility(MixinTargetContext mixin, MethodNode mixinMethod) {
//Allow injecting into static interface methods where it isn't possible to control the access of the injection method
if (Modifier.isStatic(mixinMethod.access) && !MixinEnvironment.getCompatibilityLevel().supports(LanguageFeatures.PRIVATE_METHODS_IN_INTERFACES)) {
InjectionInfo injectInfo = InjectionInfo.parse(mixin, mixinMethod);
if (injectInfo != null) {
return;
}
}
super.checkMethodVisibility(mixin, mixinMethod);
}
}
@@ -219,7 +219,7 @@ class MixinPostProcessor implements MixinConfig.IListener {
Type[] args = Type.getArgumentTypes(methodNode.desc);
Type returnType = Type.getReturnType(methodNode.desc);
Bytecode.loadArgs(args, methodNode.instructions, 0);
methodNode.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, targetClass.getName(), method.getName(), methodNode.desc, false));
methodNode.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, targetClass.getName(), method.getName(), methodNode.desc, targetClass.isInterface()));
methodNode.instructions.add(new InsnNode(returnType.getOpcode(Opcodes.IRETURN)));
methodNode.maxStack = Bytecode.getFirstNonArgLocalIndex(args, false);
methodNode.maxLocals = 0;
@@ -61,15 +61,19 @@ class MixinPreProcessorInterface extends MixinPreProcessorStandard {
protected void prepareMethod(MixinMethodNode mixinMethod, Method method) {
// Userland interfaces should not have non-public methods except for lambda bodies
if (!Bytecode.hasFlag(mixinMethod, Opcodes.ACC_PUBLIC)) {
if (!Bytecode.hasFlag(mixinMethod, Opcodes.ACC_SYNTHETIC)) {
throw new InvalidInterfaceMixinException(this.mixin, String.format("Interface mixin contains a non-public method! Found %s in %s",
method, this.mixin));
if (Bytecode.hasFlag(mixinMethod, Opcodes.ACC_SYNTHETIC)) {
CompatibilityLevel requiredLevel = CompatibilityLevel.requiredFor(LanguageFeatures.PRIVATE_SYNTHETIC_METHODS_IN_INTERFACES);
if (MixinEnvironment.getCompatibilityLevel().isLessThan(requiredLevel)) {
throw new InvalidInterfaceMixinException(this.mixin, String.format(
"Interface mixin contains a synthetic private method but compatibility level %s is required! Found %s in %s",
requiredLevel, method, this.mixin));
}
}
CompatibilityLevel requiredLevel = CompatibilityLevel.requiredFor(LanguageFeatures.PRIVATE_SYNTHETIC_METHODS_IN_INTERFACES);
if (MixinEnvironment.getCompatibilityLevel().isLessThan(requiredLevel)) {
throw new InvalidInterfaceMixinException(this.mixin, String.format(
"Interface mixin contains a synthetic private method but compatibility level %s is required! Found %s in %s",
requiredLevel, method, this.mixin));
//On versions that support it private methods are also allowed
if (!Bytecode.hasFlag(mixinMethod, Opcodes.ACC_PRIVATE) || !MixinEnvironment.getCompatibilityLevel().supports(LanguageFeatures.PRIVATE_METHODS_IN_INTERFACES)) {
throw new InvalidInterfaceMixinException(this.mixin, "Interface mixin contains a non-public method! Found " + method + " in "
+ this.mixin);
}
}