diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java index 656a8ff0..7e154ffb 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java @@ -47,6 +47,7 @@ import javax.tools.Diagnostic.Kind; import net.minecraftforge.srg2source.rangeapplier.MethodData; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.struct.InvalidMemberDescriptorException; import org.spongepowered.asm.mixin.injection.struct.MemberInfo; import org.spongepowered.asm.util.Constants; import org.spongepowered.asm.util.ConstraintParser; @@ -497,6 +498,12 @@ class AnnotatedMixin { if (targetMember.name == null) { return; } + + try { + targetMember.validate(); + } catch (InvalidMemberDescriptorException ex) { + this.mixins.printMessage(Kind.ERROR, ex.getMessage(), method, inject); + } if (targetMember.desc != null) { this.validateReferencedTarget(method, inject, targetMember, "@Inject"); @@ -562,6 +569,12 @@ class AnnotatedMixin { return; } + try { + targetMember.validate(); + } catch (InvalidMemberDescriptorException ex) { + this.mixins.printMessage(Kind.ERROR, ex.getMessage(), element, inject); + } + MemberInfo remappedReference = null; if (targetMember.isField()) { String obfField = this.mixins.getObfField(targetMember.toSrg()); diff --git a/src/main/java/org/spongepowered/asm/launch/MixinTweakContainer.java b/src/main/java/org/spongepowered/asm/launch/MixinTweakContainer.java index a5809cb6..d140042a 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinTweakContainer.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinTweakContainer.java @@ -34,7 +34,7 @@ import net.minecraft.launchwrapper.LaunchClassLoader; /** - * A collection of {@link IMixinLaunchAgent launch agents) + * A collection of {@link IMixinLaunchAgent} launch agents) */ public class MixinTweakContainer { diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java index 3170d598..a3880925 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java @@ -139,7 +139,7 @@ public abstract class InjectionInfo { throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + " is missing 'at' value(s)"); } - MemberInfo targetMember = MemberInfo.parse(method, this.mixin); + MemberInfo targetMember = MemberInfo.parseAndValidate(method, this.mixin); if (targetMember.owner != null && !targetMember.owner.equals(this.mixin.getTargetClassRef())) { throw new InvalidInjectionException(this, type + " annotation on " + this.method.name + " specifies a target class '" diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionPointData.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionPointData.java index 57222c07..ffd148ae 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionPointData.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionPointData.java @@ -102,7 +102,7 @@ public class InjectionPointData { } public MemberInfo get(String key) { - return MemberInfo.parse(this.get(key, ""), this.mixin); + return MemberInfo.parseAndValidate(this.get(key, ""), this.mixin); } private int parseInt(String string, int defaultValue) { @@ -122,7 +122,7 @@ public class InjectionPointData { } public MemberInfo getTarget() { - return MemberInfo.parse(this.target, this.mixin); + return MemberInfo.parseAndValidate(this.target, this.mixin); } public int getOrdinal() { diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/InvalidMemberDescriptorException.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/InvalidMemberDescriptorException.java new file mode 100644 index 00000000..3b01deb4 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/InvalidMemberDescriptorException.java @@ -0,0 +1,43 @@ +/* + * 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.injection.struct; + +public class InvalidMemberDescriptorException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + public InvalidMemberDescriptorException(String message) { + super(message); + } + + public InvalidMemberDescriptorException(Throwable cause) { + super(cause); + } + + public InvalidMemberDescriptorException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/MemberInfo.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/MemberInfo.java index 2019777f..943827f8 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/MemberInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/MemberInfo.java @@ -26,6 +26,7 @@ package org.spongepowered.asm.mixin.injection.struct; import net.minecraftforge.srg2source.rangeapplier.MethodData; +import org.spongepowered.asm.lib.Type; import org.spongepowered.asm.lib.tree.AbstractInsnNode; import org.spongepowered.asm.lib.tree.FieldInsnNode; import org.spongepowered.asm.lib.tree.MethodInsnNode; @@ -257,6 +258,50 @@ public class MemberInfo { public boolean isField() { return this.desc != null && !this.desc.startsWith("("); } + + /** + * Perform ultra-simple validation of the descriptor, checks that the parts + * of the descriptor are basically sane. + * + * @return fluent + * + * @throws InvalidMemberDescriptorException if any validation check fails + */ + public MemberInfo validate() throws InvalidMemberDescriptorException { + // Extremely naive class name validation, just to spot really egregious errors + if (this.owner != null && !this.owner.matches("(?i)^[\\w\\p{Sc}/]+$")) { + throw new InvalidMemberDescriptorException("Invalid owner: " + this.owner); + } + + // Also naive validation, we're looking for stupid errors here + if (this.name != null && !this.name.matches("(?i)^?$")) { + throw new InvalidMemberDescriptorException("Invalid name: " + this.name); + } + + if (this.desc != null) { + if (!this.desc.matches("^(\\([\\w\\[/;]*\\))?\\[?[\\w/;]+$")) { + throw new InvalidMemberDescriptorException("Invalid descriptor: " + this.desc); + } + if (this.isField()) { + if (!this.desc.equals(Type.getType(this.desc).getDescriptor())) { + throw new InvalidMemberDescriptorException("Invalid field type in descriptor: " + this.desc); + } + } else { + try { + Type.getArgumentTypes(this.desc); + } catch (Exception ex) { + throw new InvalidMemberDescriptorException("Invalid descriptor: " + this.desc); + } + + String retString = this.desc.substring(this.desc.indexOf(')') + 1); + if (!retString.equals(Type.getType(retString).getDescriptor())) { + throw new InvalidMemberDescriptorException("Invalid return type \"" + retString + "\" in descriptor: " + this.desc); + } + } + } + + return this; + } /** * Test whether this MemberInfo matches the supplied values. Null values are @@ -338,6 +383,27 @@ public class MemberInfo { return new MemberInfo(this, methodData, setOwner); } + /** + * Parse a MemberInfo from a string and perform validation + * + * @param string String to parse MemberInfo from + * @return parsed MemberInfo + */ + public static MemberInfo parseAndValidate(String string) throws InvalidMemberDescriptorException { + return MemberInfo.parse(string, null, null).validate(); + } + + /** + * Parse a MemberInfo from a string and perform validation + * + * @param string String to parse MemberInfo from + * @param context Context to use for reference mapping + * @return parsed MemberInfo + */ + public static MemberInfo parseAndValidate(String string, IReferenceMapperContext context) throws InvalidMemberDescriptorException { + return MemberInfo.parse(string, context.getReferenceMapper(), context.getClassRef()).validate(); + } + /** * Parse a MemberInfo from a string * @@ -371,6 +437,8 @@ public class MemberInfo { String desc = null; String owner = null; + name = name.replaceAll("\\s", ""); + if (refMapper != null) { name = refMapper.remap(mixinClass, name); }