mirror of
https://github.com/LegacyModdingMC/UniMixins.git
synced 2026-11-26 07:02:59 +00:00
fix ASMRemapperTransformer (#70)
This commit is contained in:
1 parent
c5c5cbeca1
commit
c47090bcae
3 files changed
+209
-222
No files matched your search
+64
-38
@@ -9,7 +9,6 @@ import org.objectweb.asm.ClassReader;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
import org.objectweb.asm.commons.Remapper;
|
||||
import org.objectweb.asm.commons.RemappingClassAdapter;
|
||||
import org.objectweb.asm.tree.AnnotationNode;
|
||||
import org.objectweb.asm.tree.ClassNode;
|
||||
import org.objectweb.asm.tree.MethodNode;
|
||||
|
||||
@@ -31,6 +30,7 @@ public class ASMRemapperTransformer implements IClassTransformer {
|
||||
private static final String ASM_PACKAGE_UNSHADED = "org/objectweb/asm/";
|
||||
private static final String ASM_PACKAGE_LEGACY = "org/spongepowered/asm/lib/";
|
||||
private static final String ASM_PACKAGE_MBL = "org/spongepowered/libraries/org/objectweb/asm/";
|
||||
private static final String REMAP_ANNOTATION = "Lio/github/legacymoddingmc/unimixins/compat/api/RemapASMForMixin;";
|
||||
|
||||
private static final List<String> ASM_PACKAGE_PREFIXES = Arrays.asList(
|
||||
ASM_PACKAGE_UNSHADED,
|
||||
@@ -39,8 +39,9 @@ public class ASMRemapperTransformer implements IClassTransformer {
|
||||
);
|
||||
|
||||
private static String realASMPackagePrefix;
|
||||
private final ClassConstantPoolParser wrongAsmParser;
|
||||
private final ClassConstantPoolParser shadedAsmParser;
|
||||
private final BytePatternMatcher wrongAsmMatcher;
|
||||
private final BytePatternMatcher shadedAsmMatcher;
|
||||
private final BytePatternMatcher remapAnnotationMatcher;
|
||||
|
||||
public ASMRemapperTransformer() {
|
||||
final String[] wrongAsmPackagePrefixes = ASM_PACKAGE_PREFIXES.stream()
|
||||
@@ -49,59 +50,84 @@ public class ASMRemapperTransformer implements IClassTransformer {
|
||||
|
||||
final String[] shadedAsmPackagePrefixes = new String[] { ASM_PACKAGE_LEGACY, ASM_PACKAGE_MBL };
|
||||
|
||||
this.wrongAsmParser = new ClassConstantPoolParser(wrongAsmPackagePrefixes);
|
||||
this.shadedAsmParser = new ClassConstantPoolParser(shadedAsmPackagePrefixes);
|
||||
this.wrongAsmMatcher = new BytePatternMatcher(wrongAsmPackagePrefixes, BytePatternMatcher.Mode.Contains);
|
||||
this.shadedAsmMatcher = new BytePatternMatcher(shadedAsmPackagePrefixes, BytePatternMatcher.Mode.Contains);
|
||||
this.remapAnnotationMatcher = new BytePatternMatcher(REMAP_ANNOTATION, BytePatternMatcher.Mode.Equals);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] transform(String name, String transformedName, byte[] basicClass) {
|
||||
if(basicClass == null) {
|
||||
if (basicClass == null) {
|
||||
return null;
|
||||
}
|
||||
if(transformedName.startsWith("io.github.legacymoddingmc.unimixins.compat.asm.")
|
||||
if (transformedName.startsWith("io.github.legacymoddingmc.unimixins.compat.asm.")
|
||||
|| transformedName.startsWith("com.google.")
|
||||
|| transformedName.startsWith("org.apache.")
|
||||
|| transformedName.startsWith("org.objectweb.asm.")
|
||||
) return basicClass;
|
||||
) {
|
||||
return basicClass;
|
||||
}
|
||||
|
||||
boolean foundWrongAsm = wrongAsmParser.find(basicClass, true);
|
||||
if(!foundWrongAsm) return basicClass;
|
||||
ClassReader classReader = new ClassReader(basicClass);
|
||||
|
||||
boolean foundShadedAsm = shadedAsmParser.find(basicClass, true);
|
||||
boolean doRemap = foundShadedAsm;
|
||||
boolean foundWrongAsm = matchUtf8Constant(classReader, basicClass, wrongAsmMatcher);
|
||||
if (!foundWrongAsm) return basicClass;
|
||||
|
||||
if(!doRemap) {
|
||||
ClassNode classNode = new ClassNode();
|
||||
ClassReader classReaderForNode = new ClassReader(basicClass);
|
||||
classReaderForNode.accept(classNode, 0);
|
||||
boolean doRemap = matchUtf8Constant(classReader, basicClass, shadedAsmMatcher);
|
||||
|
||||
if(classNode.interfaces != null) {
|
||||
for (String itf : classNode.interfaces) {
|
||||
if (itf.equals("org/spongepowered/asm/mixin/extensibility/IMixinConfigPlugin")) {
|
||||
doRemap = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!doRemap && classNode.visibleAnnotations != null) {
|
||||
for (AnnotationNode ann : classNode.visibleAnnotations) {
|
||||
if (ann.desc.equals("Lio/github/legacymoddingmc/unimixins/compat/api/RemapASMForMixin;")) {
|
||||
doRemap = true;
|
||||
break;
|
||||
}
|
||||
if (!doRemap) {
|
||||
for (String itf : classReader.getInterfaces()) {
|
||||
if (itf.equals("org/spongepowered/asm/mixin/extensibility/IMixinConfigPlugin")) {
|
||||
doRemap = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(doRemap) {
|
||||
ClassReader classReader = new ClassReader(basicClass);
|
||||
LOGGER.info("Transforming class {} to fit current mixin environment.", transformedName);
|
||||
ClassWriter classWriter = new ClassWriter(0);
|
||||
RemappingClassAdapter remapAdapter = new SpongepoweredASMRemappingAdapter(classWriter);
|
||||
classReader.accept(remapAdapter, 0);
|
||||
basicClass = classWriter.toByteArray();
|
||||
if (!doRemap) {
|
||||
doRemap = matchUtf8Constant(classReader, basicClass, remapAnnotationMatcher);
|
||||
}
|
||||
return basicClass;
|
||||
|
||||
if (!doRemap) {
|
||||
return basicClass;
|
||||
}
|
||||
|
||||
LOGGER.info("Transforming class {} to fit current mixin environment.", transformedName);
|
||||
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
|
||||
RemappingClassAdapter remapAdapter = new SpongepoweredASMRemappingAdapter(classWriter);
|
||||
classReader.accept(remapAdapter, ClassReader.EXPAND_FRAMES);
|
||||
return classWriter.toByteArray();
|
||||
}
|
||||
|
||||
private static boolean matchUtf8Constant(ClassReader classReader, byte[] basicClass, BytePatternMatcher matcher) {
|
||||
final int itemCount = classReader.getItemCount();
|
||||
|
||||
// Constant pool entries start from index 1
|
||||
for (int i = 1; i < itemCount; i++) {
|
||||
final int itemOffset = classReader.getItem(i);
|
||||
|
||||
// Long and double take two CP slots, the second slot has no item
|
||||
if (itemOffset == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Only match the UTF8 constant (which tag is 1).
|
||||
// getItem(i) points after the tag byte, i.e. at the first length byte.
|
||||
// [tag][u2 length][bytes...]
|
||||
if (basicClass[itemOffset - 1] != 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Length takes 2 bytes, so we read them and skip these 2 bytes
|
||||
final int utfLen = classReader.readUnsignedShort(itemOffset);
|
||||
final int utfStart = itemOffset + 2;
|
||||
|
||||
if (matcher.matches(basicClass, utfStart, utfLen)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static String getRealASMPackagePrefix() {
|
||||
|
||||
+145
@@ -0,0 +1,145 @@
|
||||
package io.github.legacymoddingmc.unimixins.compat.asm;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Was taken from <a href="https://github.com/GTNewHorizons/RetroFuturaBootstrap/pull/20">RFB</a>
|
||||
*/
|
||||
public class BytePatternMatcher {
|
||||
final Mode mode;
|
||||
// first byte -> matched patterns
|
||||
final byte[][][] byFirst = new byte[256][][];
|
||||
int minPatternLen = Integer.MAX_VALUE;
|
||||
|
||||
public enum Mode {
|
||||
/** Checks if the constant pool entry contains a pattern */
|
||||
Contains,
|
||||
/** Checks if the whole constant pool entry equals a pattern */
|
||||
Equals,
|
||||
/** Checks if the constant pool entry starts with a pattern */
|
||||
StartsWith
|
||||
}
|
||||
|
||||
public BytePatternMatcher(String strPattern, Mode mode) {
|
||||
this(new String[] {strPattern}, mode);
|
||||
}
|
||||
|
||||
public BytePatternMatcher(String[] strPatterns, Mode mode) {
|
||||
this.mode = mode;
|
||||
|
||||
final byte[][] patterns = new byte[strPatterns.length][];
|
||||
final int[] bucketSizes = new int[256];
|
||||
final int[] bucketIndices = new int[Math.min(strPatterns.length, 256)];
|
||||
int bucketsCount = 0;
|
||||
|
||||
for (int i = 0; i < strPatterns.length; i++) {
|
||||
final byte[] pattern = strPatterns[i].getBytes(StandardCharsets.UTF_8);
|
||||
patterns[i] = pattern;
|
||||
|
||||
if (pattern.length < minPatternLen) {
|
||||
minPatternLen = pattern.length;
|
||||
}
|
||||
|
||||
final int bucketIndex = pattern[0] & 0xFF;
|
||||
if (bucketSizes[bucketIndex]++ == 0) {
|
||||
bucketIndices[bucketsCount++] = bucketIndex;
|
||||
}
|
||||
}
|
||||
|
||||
// Ascending sorting by length
|
||||
Arrays.sort(patterns, (a, b) -> Integer.compare(a.length, b.length));
|
||||
|
||||
for (int i = 0; i < bucketsCount; i++) {
|
||||
final int bucketIndex = bucketIndices[i];
|
||||
byFirst[bucketIndex] = new byte[bucketSizes[bucketIndex]][];
|
||||
bucketSizes[bucketIndex] = 0; // reuse as write index
|
||||
}
|
||||
|
||||
for (final byte[] pattern : patterns) {
|
||||
final int bucketIndex = pattern[0] & 0xFF;
|
||||
byFirst[bucketIndex][bucketSizes[bucketIndex]++] = pattern;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean matches(byte[] bytes, int start, int len) {
|
||||
if (len < minPatternLen) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (mode) {
|
||||
case Contains:
|
||||
return contains(bytes, start, len);
|
||||
case Equals:
|
||||
return equals(bytes, start, len);
|
||||
case StartsWith:
|
||||
return startsWith(bytes, start, len);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean contains(byte[] bytes, int start, int len) {
|
||||
final int end = start + len;
|
||||
|
||||
for (int pos = start; pos <= end - minPatternLen; pos++) {
|
||||
final byte[][] patterns = byFirst[bytes[pos] & 0xFF];
|
||||
if (patterns == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (final byte[] pattern : patterns) {
|
||||
if (pattern.length > end - pos) {
|
||||
break;
|
||||
}
|
||||
|
||||
int k = pattern.length - 1;
|
||||
while (k > 0 && bytes[pos + k] == pattern[k]) k--;
|
||||
if (k == 0) return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean equals(byte[] bytes, int start, int len) {
|
||||
final byte[][] patterns = byFirst[bytes[start] & 0xFF];
|
||||
if (patterns == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (final byte[] pattern : patterns) {
|
||||
if (pattern.length < len) {
|
||||
continue;
|
||||
}
|
||||
if (pattern.length > len) {
|
||||
break;
|
||||
}
|
||||
|
||||
int k = pattern.length - 1;
|
||||
while (k > 0 && bytes[start + k] == pattern[k]) k--;
|
||||
if (k == 0) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean startsWith(byte[] bytes, int start, int len) {
|
||||
final byte[][] patterns = byFirst[bytes[start] & 0xFF];
|
||||
if (patterns == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (final byte[] pattern : patterns) {
|
||||
if (pattern.length > len) {
|
||||
break;
|
||||
}
|
||||
|
||||
int k = pattern.length - 1;
|
||||
while (k > 0 && bytes[start + k] == pattern[k]) k--;
|
||||
if (k == 0) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
-184
@@ -1,184 +0,0 @@
|
||||
/***
|
||||
* This Class is derived from the ASM ClassReader
|
||||
* <p>
|
||||
* ASM: a very small and fast Java bytecode manipulation framework Copyright (c) 2000-2011 INRIA, France Telecom All
|
||||
* rights reserved.
|
||||
* <p>
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
|
||||
* following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation and/or other materials provided with the
|
||||
* distribution. 3. Neither the name of the copyright holders nor the names of its contributors may be used to endorse
|
||||
* or promote products derived from this software without specific prior written permission.
|
||||
* <p>
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package io.github.legacymoddingmc.unimixins.compat.asm;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* Using this class to search for a (single) String reference is > 40 times faster than parsing a class with a
|
||||
* ClassReader + ClassNode while using way less RAM
|
||||
*/
|
||||
public final class ClassConstantPoolParser {
|
||||
|
||||
enum ConstantTypes {
|
||||
|
||||
INVALID, // 0 unused, if that ever changes shift this to the next unused index
|
||||
UTF8,
|
||||
// 2 unused
|
||||
INT,
|
||||
FLOAT,
|
||||
LONG,
|
||||
DOUBLE,
|
||||
CLASS_REF,
|
||||
STR_REF,
|
||||
FIELD,
|
||||
METH_REF,
|
||||
IMETH_REF,
|
||||
NAME_TYPE,
|
||||
// 13,14 unused
|
||||
METH_HANDLE,
|
||||
METH_TYPE,
|
||||
DYNAMIC,
|
||||
INVOKE_DYNAMIC,
|
||||
MODULE,
|
||||
PACKAGE;
|
||||
|
||||
// Indices in this table directly map to the values of these constants
|
||||
static final ConstantTypes[] MAP = { INVALID,
|
||||
UTF8, INVALID, INT, FLOAT,
|
||||
LONG, DOUBLE, CLASS_REF, STR_REF,
|
||||
FIELD, METH_REF, IMETH_REF, NAME_TYPE,
|
||||
INVALID, INVALID, METH_HANDLE, METH_TYPE,
|
||||
DYNAMIC, INVOKE_DYNAMIC, MODULE, PACKAGE };
|
||||
|
||||
static ConstantTypes toType(byte code) {
|
||||
ConstantTypes ret = MAP[Byte.toUnsignedInt(code)];
|
||||
if (ret == INVALID) throw new RuntimeException("Invalid constant type: " + code);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
private byte[][] BYTES_TO_SEARCH;
|
||||
|
||||
public ClassConstantPoolParser(String... strings) {
|
||||
BYTES_TO_SEARCH = new byte[strings.length][];
|
||||
for (int i = 0; i < BYTES_TO_SEARCH.length; i++) {
|
||||
BYTES_TO_SEARCH[i] = strings[i].getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the constant pool of the class represented by this byte array contains one of the Strings we are
|
||||
* looking for
|
||||
*/
|
||||
public boolean find(byte[] basicClass) {
|
||||
return find(basicClass, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the constant pool of the class represented by this byte array contains one of the Strings we are
|
||||
* looking for.
|
||||
*
|
||||
* @param prefixes If true, it is enough for a constant pool entry to <i>start</i> with one of our Strings to count
|
||||
* as a match - otherwise, the entire String has to match.
|
||||
*/
|
||||
public boolean find(byte[] basicClass, boolean prefixes) {
|
||||
if (basicClass == null || basicClass.length == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// checks the class version
|
||||
final int maxSupported = 69; // Java 25
|
||||
short major = readShort(6, basicClass);
|
||||
if (major > maxSupported || (major == maxSupported && readShort(4, basicClass) > 0)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Loop through each entry in the constant pool, getting the size and content before jumping to the next.
|
||||
// Strings get scanned for the searched constants, and if found result in an early exit.
|
||||
final int numConstants = readUnsignedShort(8, basicClass);
|
||||
int index = 10;
|
||||
for (int i = 1; i < numConstants; ++i) {
|
||||
int size = -1;
|
||||
|
||||
switch (ConstantTypes.toType(basicClass[index])) {
|
||||
case UTF8: {
|
||||
final int strLen = readUnsignedShort(index + 1, basicClass);
|
||||
size = 3 + strLen;
|
||||
|
||||
for (byte[] bytes : BYTES_TO_SEARCH) {
|
||||
if (prefixes ? strLen < bytes.length : strLen != bytes.length) {
|
||||
continue;
|
||||
}
|
||||
|
||||
boolean found = true;
|
||||
for (int j = index + 3; j < index + 3 + bytes.length; j++) {
|
||||
if (basicClass[j] != bytes[j - (index + 3)]) {
|
||||
found = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (found) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case INT:
|
||||
case FLOAT:
|
||||
case FIELD:
|
||||
case METH_REF:
|
||||
case IMETH_REF:
|
||||
case NAME_TYPE:
|
||||
case DYNAMIC:
|
||||
case INVOKE_DYNAMIC:
|
||||
size = 5;
|
||||
break;
|
||||
|
||||
case LONG:
|
||||
case DOUBLE:
|
||||
size = 9;
|
||||
++i;
|
||||
break;
|
||||
|
||||
case METH_HANDLE:
|
||||
size = 4;
|
||||
break;
|
||||
|
||||
case CLASS_REF:
|
||||
case STR_REF:
|
||||
case METH_TYPE:
|
||||
case MODULE:
|
||||
case PACKAGE:
|
||||
size = 3;
|
||||
break;
|
||||
}
|
||||
|
||||
if (size < 0) throw new RuntimeException("Error parsing constant pool!");
|
||||
index += size;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static short readShort(final int index, byte[] basicClass) {
|
||||
return (short) readUnsignedShort(index, basicClass);
|
||||
}
|
||||
|
||||
private static int readUnsignedShort(final int index, byte[] basicClass) {
|
||||
return ((basicClass[index] & 0xFF) << 8) | (basicClass[index + 1] & 0xFF);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user